ddsect.sh: fix calculating END_ADDRESS
[ddsect.git] / ddsect.sh
1 #!/bin/sh
2 #
3 # ddsect - dissect (and reassemble) raw data dumps using a memory map file
4 #
5 # Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 set -e
21
22 echo_exec()
23 {
24   echo $@
25   $@
26 }
27
28 parse_map_file()
29 {
30   MAP_FILE="$1"
31
32   cat $MAP_FILE | sed -e 's/#.*$//' -e '/^[[:space:]]*$/d' | tr -d ' ' |
33   while read line;
34   do
35     RANGE=$(echo $line | cut -d ':' -f 1)
36     START_ADDRESS=$(( $(echo $RANGE | cut -d '-' -f 1) ))
37     END_ADDRESS=$(( $(echo $RANGE | cut -d '-' -f 2) - 1))
38
39     NAME=$(echo $line | cut -d ':' -f 2 | sed -e 's/^"//'  -e 's/"$//')
40
41     SIZE=$(($END_ADDRESS - $START_ADDRESS + 1))
42
43     SIZE_KiB=$(($SIZE / 1024))
44
45     echo "${NAME},${START_ADDRESS},${END_ADDRESS},${SIZE},${SIZE_KiB}"
46   done
47 }
48
49 print_map_info()
50 {
51   MAP_FILE="$1"
52
53   (
54   echo "NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB"
55   parse_map_file $1
56   ) | column -s " ," -t
57 }
58
59 split_image()
60 {
61   IMAGE_FILE="$1"
62   MAP_FILE="$2"
63
64   IFS=','
65   parse_map_file $MAP_FILE |
66   while read NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB;
67   do
68     echo_exec dd ibs=1 skip=$START_ADDRESS count=$SIZE if="$IMAGE_FILE" of="${NAME}.bin"
69   done
70
71 }
72
73 join_image()
74 {
75   MAP_FILE="$1"
76   IMAGE_FILE="$2"
77
78   IFS=','
79   parse_map_file $MAP_FILE |
80   while read NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB;
81   do
82     echo_exec dd obs=1 seek=$START_ADDRESS count=$SIZE conv=notrunc if="${NAME}.bin" of="$IMAGE_FILE"
83   done
84 }
85
86 usage()
87 {
88   cat 1>&2 << EOM
89 usage: $(basename "$0") <OPTION> [ARGS]
90
91 Where OPTION and ARGS are:
92
93         info <MAP_FILE>                         show details of the map file
94         split <INPUT_IMAGE> <MAP_FILE>          split sections as per MAP_FILE
95         join <MAP_FILE> <OUTPUT_IMAGE>          join sections as per MAP_FILE
96         -h|--help|help                          show this help message
97 EOM
98 }
99
100 check_option_argument()
101 {
102   ARGUMENT="$1"
103   DESCRIPTION="$2"
104   [ "x$ARGUMENT" != "x" ] || { echo "No $DESCRIPTION passed." 1>&2; usage; exit 1; }
105 }
106
107 check_input_file_argument()
108 {
109   FILE="$1"
110   FILE_DESCRIPTION="$2"
111   check_option_argument "$FILE" "$FILE_DESCRIPTION"
112   [ -r "$FILE" ] || { echo "Invalid $FILE_DESCRIPTION." 1>&2; exit 1; }
113 }
114
115 command -v dd >/dev/null 2>&1 || { echo "$(basename "$0"): command 'dd' is missing." 1>&2 ; exit 1; }
116
117 case $1 in
118   info)
119     MAP_FILE="$2"
120     check_input_file_argument "$MAP_FILE" "map file"
121
122     command -v column >/dev/null 2>&1 || { echo "$(basename "$0"): command 'column' is missing." 1>&2 ; exit 1; }
123
124     print_map_info "$MAP_FILE"
125     ;;
126
127   split)
128     INPUT_IMAGE="$2"
129     check_input_file_argument "$INPUT_IMAGE" "input image"
130
131     MAP_FILE="$3"
132     check_input_file_argument "$MAP_FILE" "map file"
133
134     split_image "$INPUT_IMAGE" "$MAP_FILE"
135     ;;
136
137   join)
138     MAP_FILE="$2"
139     check_input_file_argument "$MAP_FILE" "map file"
140
141     OUTPUT_IMAGE="$3"
142     check_option_argument "$OUTPUT_IMAGE" "output image"
143
144     join_image "$MAP_FILE" "$OUTPUT_IMAGE"
145     ;;
146
147   -h|--help|help)
148     usage
149     exit 0
150     ;;
151
152   *)
153     echo "Missing or unknown option." 1>&2
154     usage
155     exit 1;
156     ;;
157 esac