wok view linux/stuff/bootloader.sh @ rev 8238

busybox: remove revision in stuff files
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Jan 27 12:11:34 2011 +0100 (2011-01-27)
parents 3bff7f88b1c8
children 7d497422d803
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 [--rdev device] [--video mode] [--flags rootflags] [--tracks cnt]
14 [--format 1440|1680|1920|2880 ] [--initrd initrdfile]...
16 Default values: --format 1440 --tracks 80 --prefix floppy.
18 Example:
19 $0 /boot/vmlinuz-2.6.30.6 --rdev /dev/ram0 --video -3 --cmdline 'rw lang=fr_FR kmap=fr-latin1 laptop autologin' --initrd /boot/rootfs.gz --initrd ./myconfig.gz
20 EOT
21 exit 1
22 }
24 KERNEL=""
25 INITRD=""
26 CMDLINE=""
27 PREFIX="floppy."
28 FORMAT="1440"
29 RDEV=""
30 VIDEO=""
31 FLAGS=""
32 TRACKS=""
33 DEBUG=""
34 while [ -n "$1" ]; do
35 case "$1" in
36 --c*|-c*) CMDLINE="$2"; shift;;
37 --i*|-i*) INITRD="$INITRD $2"; shift;;
38 --p*|-p*) PREFIX="$2"; shift;;
39 --fo*|-f*) FORMAT="$2"; shift;;
40 --fl*) FLAGS="$2"; shift;; # 1 read-only, 0 read-write
41 --r*|-r*) RDEV="$2"; shift;; # /dev/???
42 --v*|-v*) VIDEO="$2"; shift;; # -3 .. n
43 --t*|-t*) TRACKS="$2"; shift;; # likely 81 .. 84
44 --debug) DEBUG="1";;
45 *) KERNEL="$1";;
46 esac
47 shift
48 done
49 [ -n "$KERNEL" -a -f "$KERNEL" ] || usage
50 if [ -n "$TRACKS" ]; then
51 if [ $(( $FORMAT % $TRACKS )) -ne 0 ]; then
52 echo "Invalid track count for format $FORMAT."
53 usage
54 fi
55 fi
57 # write a 16 bits data
58 # usage: store16 offset data16 file
59 store16()
60 {
61 echo $(( $2 + 0x10000 )) | \
62 awk '{ printf "\\\\x%02X\\\\x%02X",$1%256,($1/256)%256 }' | \
63 xargs echo -en | \
64 dd bs=2 conv=notrunc of=$3 seek=$(( $1 / 2 )) 2> /dev/null
65 [ -n "$DEBUG" ] && printf "store16(%04X) = %04X\n" $1 $2 1>&2
66 }
68 # write a 32 bits data
69 # usage: storelong offset data32 file
70 storelong()
71 {
72 echo $2 | awk '{ printf "\\\\x%02X\\\\x%02X\\\\x%02X\\\\x%02X",
73 $1%256,($1/256)%256,($1/256/256)%256,($1/256/256/256)%256 }' | \
74 xargs echo -en | \
75 dd bs=4 conv=notrunc of=$3 seek=$(( $1 / 4 )) 2> /dev/null
76 [ -n "$DEBUG" ] && printf "storelong(%04X) = %08X\n" $1 $2 1>&2
77 }
79 # read a 32 bits data
80 # usage: getlong offset file
81 getlong()
82 {
83 dd if=$2 bs=1 skip=$(( $1 )) count=4 2> /dev/null | \
84 hexdump -e '"" 1/4 "%d" "\n"'
85 }
87 floppyset()
88 {
89 # bzImage offsets
90 CylinderCount=496
91 SetupSzOfs=497
92 FlagsOfs=498
93 SyssizeOfs=500
94 VideoModeOfs=506
95 RootDevOfs=508
96 CodeAdrOfs=0x214
97 RamfsAdrOfs=0x218
98 RamfsLenOfs=0x21C
99 ArgPtrOfs=0x228
101 # boot+setup address
102 SetupBase=0x90000
104 stacktop=0x9E00
106 bs=/tmp/bs$$
108 # Get and patch boot sector
109 # See http://hg.slitaz.org/wok/raw-file/711d076b277c/linux/stuff/linux-header-2.6.34.u
110 dd if=$KERNEL bs=512 count=1 of=$bs 2> /dev/null
111 uudecode <<EOT | dd of=$bs conv=notrunc 2> /dev/null
112 begin-base64 644 -
113 /L+6nWgAkAcGF4n8McC5HQDzq1sfD6mg8X1ABlfFd3ixBvOlZWaPR3gGH8ZF
114 +D/6l1hB6DQBvgACA3QO6HYBWwseKAJ0LFNH6AoBXuhmAbAgzRCwCM0QTuhl
115 ATwIdAOIBK05NigCdPDoPgE8CnXgiHz+ieb/TBD/TBi/9AGBTRz/gMdFMACc
116 sBCxBUi0k4lEHLABiUQUmGaY0+BIZgMFZtPoaAAQB7+AACn4nHMCAccx21BW
117 6J4AXrkAgLSH/kQczRVYnXfcoRoCvxwCsQk4RBxyuJPNE+oAACCQsEYoyL7b
118 AejSAF3rI4D5E3IEOMF3a4D+AnIEOOZ3bGCB/QAGdCoGUlFTlrQCULEGtQTB
119 xQSwDyHoBJAnFEAn6IwA/s117LAgzRDitOiWAJjNE2FSUCjIdwKwAZg5+HIC
120 ifhQtALNE5VeWFpyoJVBjuGAxwJPdFFOdfSM4ZU4wXVFiMj+xrEBOOZ1O4j0
121 /sW2AID9UHIwOi7wAXIqtQBgvt4B/kQMU+gxAFvoOAB1FlKYzRO4AQLNE1rQ
122 1Dpk/nXqRgjkdeVh64sWB7AxLAO0DrsHAM0QPA1088OwDejv/6wIwHX4w79s
123 BLFbZQINuA0BZToNdArNFnT0mM0Wju9Hw1g6AEluc2VydCBkaXNrIDEuBw0A
124 AA==
125 ====
126 EOT
128 # Get setup
129 setupsz=$(getlong $SetupSzOfs $bs)
130 setupszb=$(( $setupsz & 255 ))
131 dd if=$KERNEL bs=512 skip=1 count=$setupszb 2> /dev/null >> $bs
133 if [ -n "$TRACKS" ]; then
134 [ -n "$DEBUG" ] && echo -n "--tracks " 1>&2
135 n=$(getlong $CylinderCount $bs)
136 store16 $CylinderCount $(( ($n & -256) + $TRACKS )) $bs
137 fi
138 if [ -n "$FLAGS" ]; then
139 [ -n "$DEBUG" ] && echo -n "--flags " 1>&2
140 store16 $FlagsOfs $FLAGS $bs
141 fi
142 if [ -n "$VIDEO" ]; then
143 [ -n "$DEBUG" ] && echo -n "--video " 1>&2
144 store16 $VideoModeOfs $VIDEO $bs
145 fi
146 if [ -n "$RDEV" ]; then
147 [ -n "$DEBUG" ] && echo -n "--rdev " 1>&2
148 n=$(stat -c '0x%02t%02T' $RDEV 2> /dev/null)
149 [ -n "$n" ] || n=$RDEV
150 store16 $RootDevOfs $n $bs
151 fi
153 # Store cmdline after setup
154 if [ -n "$CMDLINE" ]; then
155 [ -n "$DEBUG" ] && echo -n "--cmdline '$CMDLINE' " 1>&2
156 echo -n "$CMDLINE" | dd bs=512 count=1 conv=sync 2> /dev/null >> $bs
157 storelong $ArgPtrOfs $(( $SetupBase + $stacktop )) $bs
158 fi
160 # Compute initramfs size
161 initrdlen=0
162 padding=0
163 for i in $( echo $INITRD | sed 's/,/ /' ); do
164 [ -s "$i" ] || continue
165 [ -n "$DEBUG" ] && echo "--initrd $i " 1>&2
166 initrdlen=$(( $initrdlen + $padding ))
167 padding=$(stat -c %s $i)
168 initrdlen=$(( $initrdlen + $padding ))
169 padding=$(( 4096 - ($padding & 4095) ))
170 [ $padding -eq 4096 ] && padding=0
171 done
172 Ksize=$(( $(getlong $SyssizeOfs $bs)*16 ))
173 Kpad=$(( (($Ksize+4095)/4096)*4096 - Ksize ))
174 if [ $initrdlen -ne 0 ]; then
175 [ -n "$DEBUG" ] && echo "initrdlen = $initrdlen " 1>&2
176 Kbase=$(getlong $CodeAdrOfs $bs)
177 storelong $RamfsAdrOfs \
178 $(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs
179 storelong $RamfsLenOfs $(( ($initrdlen + 3) & -4 )) $bs
180 fi
182 # Output boot sector + setup + cmdline
183 dd if=$bs 2> /dev/null
185 # Output kernel code
186 dd if=$KERNEL bs=512 skip=$(( $setupszb + 1 )) 2> /dev/null
188 # Pad to next sector
189 Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) ))
190 [ $Kpad -eq 512 ] || dd if=/dev/zero bs=1 count=$Kpad 2> /dev/null
192 # Output initramfs
193 padding=0
194 for i in $( echo $INITRD | sed 's/,/ /' ); do
195 [ -s "$i" ] || continue
196 [ $padding -ne 0 ] && dd if=/dev/zero bs=1 count=$padding 2> /dev/null
197 dd if=$i 2> /dev/null
198 padding=$(( 4 - ($(stat -c %s $i) & 3) ))
199 [ $padding -eq 4 ] && padding=0
200 done
202 # Cleanup
203 rm -f $bs
204 }
206 if [ "$FORMAT" == "0" ]; then # unsplitted
207 floppyset > $PREFIX
208 exit
209 fi
210 floppyset | split -b ${FORMAT}k /dev/stdin floppy$$
211 i=1
212 ls floppy$$* | while read file ; do
213 output=$PREFIX$(printf "%03d" $i)
214 cat $file /dev/zero | dd bs=1k count=$FORMAT conv=sync of=$output 2> /dev/null
215 echo $output
216 rm -f $file
217 i=$(( $i + 1 ))
218 done