wok view mirror-tools/stuff/var/www/slitaz/mirror/floppies/builder/bootloader @ rev 8067

mirror-tools: add css & floppies
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jan 21 11:58:56 2011 +0100 (2011-01-21)
parents
children
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 if [ "$(dirname $RDEV)" == "/dev" -a -b $RDEV ]; then
148 [ -n "$DEBUG" ] && echo -n "--rdev " 1>&2
149 RDEV=$(stat -c '0x%02t%02T' $RDEV 2> /dev/null)
150 store16 $RootDevOfs $RDEV $bs
151 fi
152 fi
154 # Store cmdline after setup
155 if [ -n "$CMDLINE" ]; then
156 [ -n "$DEBUG" ] && echo -n "--cmdline '$CMDLINE' " 1>&2
157 echo -n "$CMDLINE" | dd bs=512 count=1 conv=sync 2> /dev/null >> $bs
158 storelong $ArgPtrOfs $(( $SetupBase + $stacktop )) $bs
159 fi
161 # Compute initramfs size
162 initrdlen=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 + $(stat -c %s $i) + 3) & -4 ))
167 done
168 if [ $initrdlen -ne 0 ]; then
169 [ -n "$DEBUG" ] && echo "initrdlen = $initrdlen " 1>&2
170 storelong $RamfsAdrOfs \
171 $(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs
172 storelong $RamfsLenOfs $initrdlen $bs
173 fi
175 # Output boot sector + setup + cmdline
176 dd if=$bs 2> /dev/null
178 # Output kernel code
179 dd if=$KERNEL bs=512 skip=$(( $setupszb + 1 )) 2> /dev/null
181 # Pad to next sector
182 Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) ))
183 [ $Kpad -eq 512 ] || dd if=/dev/zero bs=1 count=$Kpad 2> /dev/null
185 # Output initramfs
186 padding=0
187 for i in $( echo $INITRD | sed 's/,/ /' ); do
188 [ -s "$i" ] || continue
189 [ $padding -ne 0 ] && dd if=/dev/zero bs=1 count=$padding 2> /dev/null
190 dd if=$i 2> /dev/null
191 padding=$(( 4 - ($(stat -c %s $i) & 3) ))
192 [ $padding -eq 4 ] && padding=0
193 done
195 # Cleanup
196 rm -f $bs
197 }
199 if [ "$FORMAT" == "0" ]; then # unsplitted
200 floppyset > $PREFIX
201 exit
202 fi
203 floppyset | split -b ${FORMAT}k /dev/stdin floppy$$
204 i=1
205 ls floppy$$* | while read file ; do
206 output=$PREFIX$(printf "%03d" $i)
207 cat $file /dev/zero | dd bs=1k count=$FORMAT conv=sync of=$output 2> /dev/null
208 echo $output
209 rm -f $file
210 i=$(( $i + 1 ))
211 done