source: tt-loader/kernel-extract.sh

Last change on this file was 12, checked in by guillaume, 11 years ago

Publication tt-loader v0.2

  • Property svn:executable set to *
File size: 2.6 KB
Line 
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
19echo
20echo "kernel extract v0.1"
21echo
22
23if [ ! -s "$1" ]; then
24        echo "You must provide the extracted MTD file as first argument"
25        exit 1
26fi
27
28MTD="$1"
29BIN="$2"
30
31# Update BIN to default if not set
32: ${BIN:=uImage.bin}
33
34function 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
50function 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
64MAGIC=$(get_int 0)
65HCRC=$(get_int 4)
66TIME=$(get_int 8)
67SIZE=$(get_int 12)
68ADDR=$(get_int 16)
69ADDS=$(get_int 20)
70DCRC=$(get_int 24)
71ARCH=$(get_int 28)
72VERS=$(get_str 32)
73
74printf "Magic=%s HCRC=%d Timestamp=%d DCRC=%d Arch=%s\n\n" $MAGIC $HCRC $TIME $DCRC $ARCH
75printf "Kernel size = %d\nKernel should be loaded at %s address, entry point is %s\n" $SIZE $ADDR $ADDS
76echo "Linux version: $VERS"
77echo
78
79if [ "$MAGIC" != "0x27051956" ]; then
80        echo "Error: Found wrong magic value at offset 0" 1>&2
81        exit 4
82fi
83
84TEST=$(get_int $(($INT1+0x40)) )
85(( TEST )) && echo "Warning: bytes after the kernel are not null"
86
87if (( SIZE > 4096000 )); then
88        echo "The kernel size seems too high, can't process"
89        exit 2
90fi
91
92# Keeping header to get uImage.bin
93let SIZE+=64
94
95# Copy kernel with dd using 4 bytes block size to have minimum performance
96dd if=$MTD of=$BIN bs=4 count=$((SIZE/4))
97
98echo
99CTRL=$( wc -c "$BIN" | cut -d ' ' -f 1 )
100if [ "$CTRL" -eq "$((SIZE+64))" ]; then
101        echo "Kernel extracted to '$BIN'"
102        echo
103else
104        echo "Kernel extraction failed ($CTRL!=$((SIZE)))"
105        echo
106        exit 3
107fi
Note: See TracBrowser for help on using the repository browser.