From 85ff655a5eea0655a97bda948426d61e8a977c4d Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Wed, 17 Oct 2012 15:02:52 +0200 Subject: [PATCH] Initial import of ddsect --- .gitignore | 2 + README | 14 ++++++ ddsect.sh | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ example.map | 9 ++++ test.sh | 14 ++++++ 5 files changed, 196 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100755 ddsect.sh create mode 100644 example.map create mode 100755 test.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..220aa08 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.dump +*.bin diff --git a/README b/README new file mode 100644 index 0000000..0c292b2 --- /dev/null +++ b/README @@ -0,0 +1,14 @@ +ddsect is a data dump dissecting tool based on dd. + +It can be used, for example, to dissect firmware files after their structure +has been derived with tools like binwalk (https://code.google.com/p/binwalk/), +or to split a raw flash dump into its MTD partitions. + +The format for describing each memory section in a map file is: + + HexStartingAddres-HexEndingAddress : "name" + +just like the output of the MTD driver in linux. + +Empty lines and lines starting with the '#' character will be ignored +in the map file. diff --git a/ddsect.sh b/ddsect.sh new file mode 100755 index 0000000..b1c5122 --- /dev/null +++ b/ddsect.sh @@ -0,0 +1,157 @@ +#!/bin/sh +# +# ddsect - dissect (and reassemble) raw data dumps using a memory map file +# +# Copyright (C) 2012 Antonio Ospite +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +set -e + +echo_exec() +{ + echo $@ + $@ +} + +parse_map_file() +{ + MAP_FILE="$1" + + cat $MAP_FILE | sed -e 's/#.*$//' -e '/^[[:space:]]*$/d' | tr -d ' ' | + while read line; + do + RANGE=$(echo $line | cut -d ':' -f 1) + START_ADDRESS=$(( $(echo $RANGE | cut -d '-' -f 1) )) + END_ADDRESS=$(( $(echo $RANGE | cut -d '-' -f 2) )) + + NAME=$(echo $line | cut -d ':' -f 2 | sed -e 's/^"//' -e 's/"$//') + + SIZE=$(($END_ADDRESS - $START_ADDRESS)) + + SIZE_KiB=$(($SIZE / 1024)) + + echo "${NAME},${START_ADDRESS},${END_ADDRESS},${SIZE},${SIZE_KiB}" + done +} + +print_map_info() +{ + MAP_FILE="$1" + + ( + echo "NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB" + parse_map_file $1 + ) | column -s " ," -t +} + +split_image() +{ + IMAGE_FILE="$1" + MAP_FILE="$2" + + IFS=',' + parse_map_file $MAP_FILE | + while read NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB; + do + echo_exec dd ibs=1 skip=$START_ADDRESS count=$SIZE if="$IMAGE_FILE" of="${NAME}.bin" + done + +} + +join_image() +{ + MAP_FILE="$1" + IMAGE_FILE="$2" + + IFS=',' + parse_map_file $MAP_FILE | + while read NAME START_ADDRESS END_ADDRESS SIZE SIZE_KiB; + do + echo_exec dd obs=1 seek=$START_ADDRESS count=$SIZE conv=notrunc if="${NAME}.bin" of="$IMAGE_FILE" + done +} + +usage() +{ + cat 1>&2 << EOM +usage: $(basename "$0")