ddsect.sh: small cleanups
[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 $MAP_FILE
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 join_image()
73 {
74   MAP_FILE="$1"
75   IMAGE_FILE="$2"
76
77   IFS=','
78   parse_map_file $MAP_FILE |
79   while read NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB;
80   do
81     echo_exec dd obs=1 seek=$START_ADDRESS count=$SIZE conv=notrunc if="${NAME}.bin" of="$IMAGE_FILE"
82   done
83 }
84
85 usage()
86 {
87   cat 1>&2 << EOM
88 usage: $(basename "$0") <OPTION> [ARGS]
89
90 Where OPTION and ARGS are:
91
92         info <MAP_FILE>                         show details of the map file
93         split <INPUT_IMAGE> <MAP_FILE>          split sections as per MAP_FILE
94         join <MAP_FILE> <OUTPUT_IMAGE>          join sections as per MAP_FILE
95         -h|--help|help                          show this help message
96 EOM
97 }
98
99 check_option_argument()
100 {
101   ARGUMENT="$1"
102   DESCRIPTION="$2"
103   [ "x$ARGUMENT" != "x" ] || { echo "No $DESCRIPTION passed." 1>&2; usage; exit 1; }
104 }
105
106 check_input_file_argument()
107 {
108   FILE="$1"
109   FILE_DESCRIPTION="$2"
110   check_option_argument "$FILE" "$FILE_DESCRIPTION"
111   [ -r "$FILE" ] || { echo "Invalid $FILE_DESCRIPTION." 1>&2; exit 1; }
112 }
113
114 command -v dd >/dev/null 2>&1 || { echo "$(basename "$0"): command 'dd' is missing." 1>&2 ; exit 1; }
115
116 case $1 in
117   info)
118     MAP_FILE="$2"
119     check_input_file_argument "$MAP_FILE" "map file"
120
121     command -v column >/dev/null 2>&1 || { echo "$(basename "$0"): command 'column' is missing." 1>&2 ; exit 1; }
122
123     print_map_info "$MAP_FILE"
124     ;;
125
126   split)
127     INPUT_IMAGE="$2"
128     check_input_file_argument "$INPUT_IMAGE" "input image"
129
130     MAP_FILE="$3"
131     check_input_file_argument "$MAP_FILE" "map file"
132
133     split_image "$INPUT_IMAGE" "$MAP_FILE"
134     ;;
135
136   join)
137     MAP_FILE="$2"
138     check_input_file_argument "$MAP_FILE" "map file"
139
140     OUTPUT_IMAGE="$3"
141     check_option_argument "$OUTPUT_IMAGE" "output image"
142
143     join_image "$MAP_FILE" "$OUTPUT_IMAGE"
144     ;;
145
146   -h|--help|help)
147     usage
148     exit 0
149     ;;
150
151   *)
152     echo "Missing or unknown option." 1>&2
153     usage
154     exit 1;
155     ;;
156 esac