3 # ddsect - dissect (and reassemble) raw data dumps using a memory map file
 
   5 # Copyright (C) 2012  Antonio Ospite <ospite@studenti.unina.it>
 
   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.
 
  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.
 
  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/>.
 
  32   cat $MAP_FILE | sed -e 's/#.*$//' -e '/^[[:space:]]*$/d' | tr -d ' ' |
 
  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))
 
  39     NAME=$(echo $line | cut -d ':' -f 2 | sed -e 's/^"//'  -e 's/"$//')
 
  41     SIZE=$(($END_ADDRESS - $START_ADDRESS + 1))
 
  43     SIZE_KiB=$(($SIZE / 1024))
 
  45     echo "${NAME},${START_ADDRESS},${END_ADDRESS},${SIZE},${SIZE_KiB}"
 
  54   echo "NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB"
 
  55   parse_map_file $MAP_FILE
 
  65   parse_map_file $MAP_FILE |
 
  66   while read NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB;
 
  68     echo_exec dd ibs=1 skip=$START_ADDRESS count=$SIZE if="$IMAGE_FILE" of="${NAME}.bin"
 
  78   parse_map_file $MAP_FILE |
 
  79   while read NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB;
 
  81     echo_exec dd obs=1 seek=$START_ADDRESS count=$SIZE conv=notrunc if="${NAME}.bin" of="$IMAGE_FILE"
 
  88 usage: $(basename "$0") <OPTION> [ARGS]
 
  90 Where OPTION and ARGS are:
 
  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
 
  99 check_option_argument()
 
 103   [ "x$ARGUMENT" != "x" ] || { echo "No $DESCRIPTION passed." 1>&2; usage; exit 1; }
 
 106 check_input_file_argument()
 
 109   FILE_DESCRIPTION="$2"
 
 110   check_option_argument "$FILE" "$FILE_DESCRIPTION"
 
 111   [ -r "$FILE" ] || { echo "Invalid $FILE_DESCRIPTION." 1>&2; exit 1; }
 
 114 command -v dd >/dev/null 2>&1 || { echo "$(basename "$0"): command 'dd' is missing." 1>&2 ; exit 1; }
 
 119     check_input_file_argument "$MAP_FILE" "map file"
 
 121     command -v column >/dev/null 2>&1 || { echo "$(basename "$0"): command 'column' is missing." 1>&2 ; exit 1; }
 
 123     print_map_info "$MAP_FILE"
 
 128     check_input_file_argument "$INPUT_IMAGE" "input image"
 
 131     check_input_file_argument "$MAP_FILE" "map file"
 
 133     split_image "$INPUT_IMAGE" "$MAP_FILE"
 
 138     check_input_file_argument "$MAP_FILE" "map file"
 
 141     check_option_argument "$OUTPUT_IMAGE" "output image"
 
 143     join_image "$MAP_FILE" "$OUTPUT_IMAGE"
 
 152     echo "Missing or unknown option." 1>&2