wok-next view linux/stuff/bootloader.sh @ rev 5127
Up: privoxy (3.0.16-stable)
author | Paul Issott <paul@slitaz.org> |
---|---|
date | Sun Mar 21 17:28:20 2010 +0000 (2010-03-21) |
parents | b5013b460117 |
children | 7798d8b688a9 |
line source
1 #!/bin/sh
2 #
3 # This script creates a floppy image set from a linux bzImage and can merge
4 # a cmdline and/or one or more initramfs.
5 # The total size can not exceed 15M because INT 15H function 87H limitations.
6 #
7 # (C) 2009 Pascal Bellard - GNU General Public License v3.
9 usage()
10 {
11 cat <<EOT
12 Usage: $0 bzImage [--prefix image_prefix] [--cmdline 'args']
13 [--format 1440|1680|1720|2880 ] [--initrd initrdfile]...
15 Example:
16 $0 /boot/vmlinuz-2.6.30.6 --cmdline 'rw lang=fr_FR kmap=fr-latin1 laptop autologin' --initrd /boot/rootfs.gz --initrd ./myconfig.gz
17 EOT
18 exit 1
19 }
21 KERNEL=""
22 INITRD=""
23 CMDLINE=""
24 PREFIX="floppy"
25 FORMAT="1440"
26 while [ -n "$1" ]; do
27 case "$1" in
28 --cmdline) CMDLINE="$2"; shift;;
29 --initrd) INITRD="$INITRD $2"; shift;;
30 --prefix) PREFIX="$2"; shift;;
31 --format) FORMAT="$2"; shift;;
32 *) KERNEL="$1";;
33 esac
34 shift
35 done
36 [ -n "$KERNEL" -a -f "$KERNEL" ] || usage
38 # write a 32 bits data
39 # usage: storelong offset data32 file
40 storelong()
41 {
42 printf "00000 %02X %02X %02X %02X \n" \
43 $(( $2 & 255 )) $(( ($2>>8) & 255 )) \
44 $(( ($2>>16) & 255 )) $(( ($2>>24) & 255 )) | \
45 hexdump -R | dd bs=1 conv=notrunc of=$3 seek=$(( $1 )) 2> /dev/null
46 }
48 # read a 32 bits data
49 # usage: getlong offset file
50 getlong()
51 {
52 dd if=$2 bs=1 skip=$(( $1 )) count=4 2> /dev/null | \
53 hexdump -e '"" 1/4 "%d" "\n"'
54 }
56 floppyset()
57 {
58 # bzImage offsets
59 SetupSzOfs=497
60 SyssizeOfs=500
61 CodeAdrOfs=0x214
62 RamfsAdrOfs=0x218
63 RamfsLenOfs=0x21C
64 ArgPtrOfs=0x228
66 # boot+setup address
67 SetupBase=0x90000
69 stacktop=0x9E00
71 bs=/tmp/bs$$
73 # Get and patch boot sector
74 dd if=$KERNEL bs=512 count=1 of=$bs 2> /dev/null
75 uudecode <<EOT | dd of=$bs conv=notrunc 2> /dev/null
76 begin-base64 644 -
77 v/Sd/GgAkAcxyQYXify7eACO2cU3sQbzpY7ZiSeMRwKg8X2YQAYfxkX4P/qz
78 GFFLdfyXmEHoPgG+AAKATBGAx0QkAJwDdA7oZAG+KAI5HHIuR4scVuhAAV+L
79 NehQAbAgzRCwCM0QTpjNFjwIdAOIBK07NXTy6CoBPAp14oh8/onmsA+/9AH+
80 TRyxBbSTiUQcsAGJRBSZiVQQiVQYZjHbQ9PjS2YDHWbT62gAEAe/gAAp+5xz
81 AgHfU1Yx2+jaAF65AIC0h/5EHM0VW5133KEaAki/HAKxCThEHHKwMcDNE+oA
82 ACCQsEYoyL65AejCAF3rKYD5E3IEOMF3boD+AnIEOOZ3b4D9UHN5YIH9AAZ0
83 JwZSUVOWtAJQuQYAUbEEwcUEsA8h6ASQJxRAJ+h1AOLusCDNEFnirpjNE2Ex
84 9q2RrZKtUCjIdwKwAZg5+HICifhQUrQCzRNalV5YcpYp9wHxweYJAfM4wXUm
85 iMj+xrEBOOZ1HIj0/sW2ADwTdRKA/VByDbUAYL68AegkAJjNFmGjBABSUWaP
86 BgAACf91nhYHsDEsA7QOuwcAzRA8DXTzw7AN6O//rAjAdfjDWDoASW5zZXJ0
87 IG5leHQgZmxvcHB5IGFuZCBwcmVzcyBhbnkga2V5IHRvIGNvbnRpbnVlLgcN
88 AA==
89 ====
90 EOT
92 # Get setup
93 setupsz=$(getlong $SetupSzOfs $bs)
94 setupszb=$(( $setupsz & 255 ))
95 dd if=$KERNEL bs=512 skip=1 count=$setupszb 2> /dev/null >> $bs
97 # Store cmdline after setup
98 if [ -n "$CMDLINE" ]; then
99 echo -n "$CMDLINE" | dd bs=512 count=1 conv=sync 2> /dev/null >> $bs
100 storelong ArgPtrOfs $(( $SetupBase + $stacktop )) $bs
101 fi
103 # Compute initramfs size
104 initrdlen=0
105 padding=0
106 for i in $( echo $INITRD | sed 's/,/ /' ); do
107 [ -s "$i" ] || continue
108 initrdlen=$(( $initrdlen + $padding ))
109 padding=$(stat -c %s $i)
110 initrdlen=$(( $initrdlen + $padding ))
111 padding=$(( 4096 - ($padding & 4095) ))
112 [ $padding -eq 4096 ] && padding=0
113 done
114 Ksize=$(( $(getlong $SyssizeOfs $bs)*16 ))
115 Kpad=$(( (($Ksize+4095)/4096)*4096 - Ksize ))
116 if [ $initrdlen -ne 0 ]; then
117 Kbase=$(getlong $CodeAdrOfs $bs)
118 storelong $RamfsAdrOfs \
119 $(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs
120 storelong $RamfsLenOfs $initrdlen $bs
121 fi
123 # Output boot sector + setup + cmdline
124 dd if=$bs 2> /dev/null
126 # Output kernel code
127 dd if=$KERNEL bs=512 skip=$(( $setupszb + 1 )) 2> /dev/null
129 # Pad to next sector
130 Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) ))
131 [ $Kpad -eq 512 ] || dd if=/dev/zero bs=1 count=$Kpad 2> /dev/null
133 # Output initramfs
134 padding=0
135 for i in $( echo $INITRD | sed 's/,/ /' ); do
136 [ -s "$i" ] || continue
137 [ $padding -ne 0 ] && dd if=/dev/zero bs=1 count=$padding
138 dd if=$i 2> /dev/null
139 padding=$(( 4096 - ($(stat -c %s $i) & 4095) ))
140 [ $padding -eq 4096 ] && padding=0
141 done
143 # Cleanup
144 rm -f $bs
145 }
147 floppyset | split -b ${FORMAT}k /dev/stdin floppy$$
148 i=1
149 ls floppy$$* | while read file ; do
150 output=$PREFIX.$(printf "%03d" $i)
151 cat $file /dev/zero | dd bs=1k count=$FORMAT conv=sync of=$output 2> /dev/null
152 echo $output
153 rm -f $file
154 i=$(( $i + 1 ))
155 done