| 1 | #! /bin/sh |
|---|
| 2 | # |
|---|
| 3 | # copyright - PKG.fr - 2008 |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU Library General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA |
|---|
| 18 | |
|---|
| 19 | echo |
|---|
| 20 | echo "kernel extract v0.1" |
|---|
| 21 | echo |
|---|
| 22 | |
|---|
| 23 | if [ ! -s "$1" ]; then |
|---|
| 24 | echo "You must provide the extracted MTD file as first argument" |
|---|
| 25 | exit 1 |
|---|
| 26 | fi |
|---|
| 27 | |
|---|
| 28 | MTD="$1" |
|---|
| 29 | BIN="$2" |
|---|
| 30 | |
|---|
| 31 | # Update BIN to default if not set |
|---|
| 32 | : ${BIN:=uImage.bin} |
|---|
| 33 | |
|---|
| 34 | function get_int () { |
|---|
| 35 | Hex="0x" |
|---|
| 36 | let i=0 offset=$1 |
|---|
| 37 | while (( i < 4 )) |
|---|
| 38 | do |
|---|
| 39 | Chr=$( dd if="$MTD" bs=1 count=1 skip=$((offset+i)) 2>/dev/null ) |
|---|
| 40 | Chr=$( printf '%d' "'$Chr" ) |
|---|
| 41 | #echo $Chr 1>&2 |
|---|
| 42 | (( Chr < 0 )) && let Chr+=256 |
|---|
| 43 | Hex="$Hex$( printf '%02lX' $Chr )" |
|---|
| 44 | #echo $Hex 1>&2 |
|---|
| 45 | let i++ |
|---|
| 46 | done |
|---|
| 47 | echo "$Hex" |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | function get_str () { |
|---|
| 51 | Str="" |
|---|
| 52 | let Ascii=1 offset=$1 |
|---|
| 53 | while (( Ascii > 0 )) |
|---|
| 54 | do |
|---|
| 55 | Chr=$( dd if="$MTD" bs=1 count=1 skip=$((offset)) 2>/dev/null ) |
|---|
| 56 | Ascii=$( printf '%d' "'$Chr" ) |
|---|
| 57 | (( Ascii > 31 && Ascii < 127 )) && Str="$Str$Chr" |
|---|
| 58 | (( ++offset >= 0x40 )) && break |
|---|
| 59 | done |
|---|
| 60 | echo "$Str" |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | # uImage.bin header used by u-boot |
|---|
| 64 | MAGIC=$(get_int 0) |
|---|
| 65 | HCRC=$(get_int 4) |
|---|
| 66 | TIME=$(get_int 8) |
|---|
| 67 | SIZE=$(get_int 12) |
|---|
| 68 | ADDR=$(get_int 16) |
|---|
| 69 | ADDS=$(get_int 20) |
|---|
| 70 | DCRC=$(get_int 24) |
|---|
| 71 | ARCH=$(get_int 28) |
|---|
| 72 | VERS=$(get_str 32) |
|---|
| 73 | |
|---|
| 74 | printf "Magic=%s HCRC=%d Timestamp=%d DCRC=%d Arch=%s\n\n" $MAGIC $HCRC $TIME $DCRC $ARCH |
|---|
| 75 | printf "Kernel size = %d\nKernel should be loaded at %s address, entry point is %s\n" $SIZE $ADDR $ADDS |
|---|
| 76 | echo "Linux version: $VERS" |
|---|
| 77 | echo |
|---|
| 78 | |
|---|
| 79 | if [ "$MAGIC" != "0x27051956" ]; then |
|---|
| 80 | echo "Error: Found wrong magic value at offset 0" 1>&2 |
|---|
| 81 | exit 4 |
|---|
| 82 | fi |
|---|
| 83 | |
|---|
| 84 | TEST=$(get_int $(($INT1+0x40)) ) |
|---|
| 85 | (( TEST )) && echo "Warning: bytes after the kernel are not null" |
|---|
| 86 | |
|---|
| 87 | if (( SIZE > 4096000 )); then |
|---|
| 88 | echo "The kernel size seems too high, can't process" |
|---|
| 89 | exit 2 |
|---|
| 90 | fi |
|---|
| 91 | |
|---|
| 92 | # Keeping header to get uImage.bin |
|---|
| 93 | let SIZE+=64 |
|---|
| 94 | |
|---|
| 95 | # Copy kernel with dd using 4 bytes block size to have minimum performance |
|---|
| 96 | dd if=$MTD of=$BIN bs=4 count=$((SIZE/4)) |
|---|
| 97 | |
|---|
| 98 | echo |
|---|
| 99 | CTRL=$( wc -c "$BIN" | cut -d ' ' -f 1 ) |
|---|
| 100 | if [ "$CTRL" -eq "$((SIZE+64))" ]; then |
|---|
| 101 | echo "Kernel extracted to '$BIN'" |
|---|
| 102 | echo |
|---|
| 103 | else |
|---|
| 104 | echo "Kernel extraction failed ($CTRL!=$((SIZE)))" |
|---|
| 105 | echo |
|---|
| 106 | exit 3 |
|---|
| 107 | fi |
|---|