wok view linux-libre/stuff/bootloader.sh @ rev 9396

Up: talloc to 2.0.5.
author Christopher Rogers <slaxemulator@gmail.com>
date Sat Mar 26 14:57:15 2011 +0000 (2011-03-26)
parents
children 903c03d31ebf
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 n=$2; i=2; while [ $i -ne 0 ]; do
62 printf '\\\\x%02X' $(($n & 255))
63 i=$(($i-1)); n=$(($n >> 8))
64 done | xargs echo -en | \
65 dd bs=2 conv=notrunc of=$3 seek=$(( $1 / 2 )) 2> /dev/null
66 [ -n "$DEBUG" ] && printf "store16(%04X) = %04X\n" $1 $2 1>&2
67 }
69 # write a 32 bits data
70 # usage: storelong offset data32 file
71 storelong()
72 {
73 n=$2; i=4; while [ $i -ne 0 ]; do
74 printf '\\\\x%02X' $(($n & 255))
75 i=$(($i-1)); n=$(($n >> 8))
76 done | xargs echo -en | \
77 dd bs=4 conv=notrunc of=$3 seek=$(( $1 / 4 )) 2> /dev/null
78 [ -n "$DEBUG" ] && printf "storelong(%04X) = %08X\n" $1 $2 1>&2
79 }
81 # read a 32 bits data
82 # usage: getlong offset file
83 getlong()
84 {
85 dd if=$2 bs=1 skip=$(( $1 )) count=4 2> /dev/null | \
86 hexdump -e '"" 1/4 "%d" "\n"'
87 }
89 floppyset()
90 {
91 # bzImage offsets
92 CylinderCount=496
93 SetupSzOfs=497
94 FlagsOfs=498
95 SyssizeOfs=500
96 VideoModeOfs=506
97 RootDevOfs=508
98 CodeAdrOfs=0x214
99 RamfsAdrOfs=0x218
100 RamfsLenOfs=0x21C
101 ArgPtrOfs=0x228
103 # boot+setup address
104 SetupBase=0x90000
106 stacktop=0x9E00
108 bs=/tmp/bs$$
110 # Get and patch boot sector
111 # See http://hg.slitaz.org/wok/raw-file/711d076b277c/linux/stuff/linux-header-2.6.34.u
112 dd if=$KERNEL bs=512 count=1 of=$bs 2> /dev/null
113 uudecode <<EOT | dd of=$bs conv=notrunc 2> /dev/null
114 begin-base64 644 -
115 /L+6nWgAkAcGF4n8McC5HQDzq1sfD6mg8X1ABlfFd3ixBvOlZWaPR3gGH8ZF
116 +D/6l1hB6DQBvgACA3QO6HYBWwseKAJ0LFNH6AoBXuhmAbAgzRCwCM0QTuhl
117 ATwIdAOIBK05NigCdPDoPgE8CnXgiHz+ieb/TBD/TBi/9AGBTRz/gMdFMACc
118 sBCxBUi0k4lEHLABiUQUmGaY0+BIZgMFZtPoaAAQB7+AACn4nHMCAccx21BW
119 6J4AXrkAgLSH/kQczRVYnXfcoRoCvxwCsQk4RBxyuJPNE+oAACCQsEYoyL7b
120 AejSAF3rI4D5E3IEOMF3a4D+AnIEOOZ3bGCB/QAGdCoGUlFTlrQCULEGtQTB
121 xQSwDyHoBJAnFEAn6IwA/s117LAgzRDitOiWAJjNE2FSUCjIdwKwAZg5+HIC
122 ifhQtALNE5VeWFpyoJVBjuGAxwJPdFFOdfSM4ZU4wXVFiMj+xrEBOOZ1O4j0
123 /sW2AID9UHIwOi7wAXIqtQBgvt4B/kQMU+gxAFvoOAB1FlKYzRO4AQLNE1rQ
124 1Dpk/nXqRgjkdeVh64sWB7AxLAO0DrsHAM0QPA1088OwDejv/6wIwHX4w79s
125 BLFbZQINuA0BZToNdArNFnT0mM0Wju9Hw1g6AEluc2VydCBkaXNrIDEuBw0A
126 AA==
127 ====
128 EOT
130 # Get setup
131 setupsz=$(getlong $SetupSzOfs $bs)
132 setupszb=$(( $setupsz & 255 ))
133 dd if=$KERNEL bs=512 skip=1 count=$setupszb 2> /dev/null >> $bs
135 if [ -n "$TRACKS" ]; then
136 [ -n "$DEBUG" ] && echo -n "--tracks " 1>&2
137 n=$(getlong $CylinderCount $bs)
138 store16 $CylinderCount $(( ($n & -256) + $TRACKS )) $bs
139 fi
140 if [ -n "$FLAGS" ]; then
141 [ -n "$DEBUG" ] && echo -n "--flags " 1>&2
142 store16 $FlagsOfs $FLAGS $bs
143 fi
144 if [ -n "$VIDEO" ]; then
145 [ -n "$DEBUG" ] && echo -n "--video " 1>&2
146 store16 $VideoModeOfs $VIDEO $bs
147 fi
148 if [ -n "$RDEV" ]; then
149 [ -n "$DEBUG" ] && echo -n "--rdev " 1>&2
150 n=$(stat -c '0x%02t%02T' $RDEV 2> /dev/null)
151 [ -n "$n" ] || n=$RDEV
152 store16 $RootDevOfs $n $bs
153 fi
155 # Store cmdline after setup
156 if [ -n "$CMDLINE" ]; then
157 [ -n "$DEBUG" ] && echo -n "--cmdline '$CMDLINE' " 1>&2
158 echo -n "$CMDLINE" | dd bs=512 count=1 conv=sync 2> /dev/null >> $bs
159 storelong $ArgPtrOfs $(( $SetupBase + $stacktop )) $bs
160 fi
162 # Compute initramfs size
163 initrdlen=0
164 padding=0
165 for i in $( echo $INITRD | sed 's/,/ /' ); do
166 [ -s "$i" ] || continue
167 [ -n "$DEBUG" ] && echo "--initrd $i " 1>&2
168 initrdlen=$(( $initrdlen + $padding ))
169 padding=$(stat -c %s $i)
170 initrdlen=$(( $initrdlen + $padding ))
171 padding=$(( 4096 - ($padding & 4095) ))
172 [ $padding -eq 4096 ] && padding=0
173 done
174 Ksize=$(( $(getlong $SyssizeOfs $bs)*16 ))
175 Kpad=$(( (($Ksize+4095)/4096)*4096 - Ksize ))
176 if [ $initrdlen -ne 0 ]; then
177 [ -n "$DEBUG" ] && echo "initrdlen = $initrdlen " 1>&2
178 Kbase=$(getlong $CodeAdrOfs $bs)
179 storelong $RamfsAdrOfs \
180 $(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs
181 storelong $RamfsLenOfs $(( ($initrdlen + 3) & -4 )) $bs
182 fi
184 # Output boot sector + setup + cmdline
185 dd if=$bs 2> /dev/null
187 # Output kernel code
188 dd if=$KERNEL bs=512 skip=$(( $setupszb + 1 )) 2> /dev/null
190 # Pad to next sector
191 Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) ))
192 [ $Kpad -eq 512 ] || dd if=/dev/zero bs=1 count=$Kpad 2> /dev/null
194 # Output initramfs
195 padding=0
196 for i in $( echo $INITRD | sed 's/,/ /' ); do
197 [ -s "$i" ] || continue
198 [ $padding -ne 0 ] && dd if=/dev/zero bs=1 count=$padding 2> /dev/null
199 dd if=$i 2> /dev/null
200 padding=$(( 4 - ($(stat -c %s $i) & 3) ))
201 [ $padding -eq 4 ] && padding=0
202 done
204 # Cleanup
205 rm -f $bs
206 }
208 if [ "$FORMAT" == "0" ]; then # unsplitted
209 floppyset > $PREFIX
210 exit
211 fi
212 floppyset | split -b ${FORMAT}k /dev/stdin floppy$$
213 i=1
214 ls floppy$$* | while read file ; do
215 output=$PREFIX$(printf "%03d" $i)
216 cat $file /dev/zero | dd bs=1k count=$FORMAT conv=sync of=$output 2> /dev/null
217 echo $output
218 rm -f $file
219 i=$(( $i + 1 ))
220 done