wok view linux/stuff/bootloader.sh @ rev 21636
updated opensc (0.13.0 -> 0.19.0)
author | Hans-G?nter Theisgen |
---|---|
date | Tue May 28 16:41:09 2019 +0100 (2019-05-28) |
parents | 1ed0f1f453b3 |
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] [--info file ]
13 [--format 1200|1440|1680|1920|2880|... ] [--mem mb]
14 [--rdev device] [--video mode] [--flags rootflags] [--tracks cnt]
15 [--cmdline 'args'] [--dont-edit-cmdline] [--no-syssize-fix]
16 [--address-initrd address] [--initrd initrdfile]...
18 Default values: --format 1440 --tracks 80 --rdev /dev/fd0 --prefix floppy. --mem 16
20 Example:
21 $0 /boot/bzImage --rdev /dev/ram0 --video -3 --cmdline 'rw lang=fr_FR kmap=fr-latin1 laptop autologin' --initrd /boot/rootfs.gz --initrd ./myconfig.gz
22 EOT
23 exit 1
24 }
26 KERNEL=""
27 INITRD=""
28 ADRSRD=""
29 CMDLINE=""
30 PREFIX="floppy."
31 FORMAT="1440"
32 RDEV=""
33 VIDEO=""
34 FLAGS=""
35 TRACKS="80"
36 MEM="16"
37 NOEDIT=""
38 NOSYSSIZEFIX=""
39 INFOFILE=""
40 DEBUG=""
41 while [ -n "$1" ]; do
42 case "${1/--/-}" in
43 -c*) CMDLINE="$2"; shift;;
44 -inf*) INFOFILE="$2"; shift;;
45 -i*) INITRD="$INITRD $2"; shift;;
46 -a*) ADRSRD="$2"; shift;;
47 -h*) HEAP="$2"; shift;;
48 -l*) LOADERTYPE="$2"; shift;;
49 -p*) PREFIX="$2"; shift;;
50 -fl*)FLAGS="$2"; shift;; # 1 read-only, 0 read-write
51 -f*) FORMAT="$2"; shift;;
52 -m*) MEM="$(echo $2 | sed 's/[^0-9]//g')"; shift;;
53 -r*) RDEV="$2"; shift;;
54 -v*) VIDEO="$2"; shift;; # -3 .. n
55 -t*) TRACKS="$2"; shift;; # likely 81 .. 84
56 -n*) NOSYSSIZEFIX="1";;
57 -debug) DEBUG="1";;
58 -d*) NOEDIT="1";;
59 *) KERNEL="$1";;
60 esac
61 shift
62 done
63 [ -n "$KERNEL" -a -f "$KERNEL" ] || usage
64 while [ -L "$KERNEL" ]; do KERNEL="$(readlink "$KERNEL")"; done
65 if [ $(( $FORMAT % $TRACKS )) -ne 0 ]; then
66 echo "Invalid track count for format $FORMAT."
67 usage
68 fi
69 [ 0$MEM -lt 4 ] && MEM=4
70 [ $MEM -gt 16 ] && MEM=16
72 ddq() { dd $@ 2> /dev/null; }
74 patch()
75 {
76 echo -en $(echo ":$2" | sed 's/:/\\x/g') | \
77 ddq bs=1 conv=notrunc of=$3 seek=$((0x$1))
78 [ -n "$DEBUG" ] && echo "patch $1 $2 $4" 1>&2
79 }
81 # usage: store bits offset data file
82 store()
83 {
84 n=$3; for i in $(seq 8 8 $1); do
85 printf '\\\\x%02X' $(($n & 255))
86 n=$(($n >> 8))
87 done | xargs echo -en | ddq bs=1 conv=notrunc of=$4 seek=$(($2))
88 [ -n "$DEBUG" ] && printf "store%d(%03X) = %0$(($1/4))X %s\n" $1 $2 $3 "$5" 1>&2
89 }
91 # usage: getlong offset file [bytes]
92 getlong()
93 {
94 echo $(od -j $1 -N ${3:-4} -t u${3:-4} -An $2)
95 }
97 error()
98 {
99 echo $@ 1>&2
100 rm -f $bs
101 exit 1
102 }
104 floppyset()
105 {
106 # bzImage offsets
107 SetupSzOfs=497
108 FlagsOfs=498
109 VideoModeOfs=506
110 RootDevOfs=508
111 Magic=0x202
112 RamfsAdrOfs=0x218
113 RamfsLenOfs=0x21C
115 # boot+setup address
116 SetupBase=0x90000
118 bs=/tmp/bs$$
120 # Get and patch boot sector
121 # See http://hg.slitaz.org/wok/raw-file/66e38bd6a132/linux/stuff/linux-header.u
122 [ -n "$DEBUG" ] && echo "Read bootsector..." 1>&2
123 ddq if=$KERNEL bs=512 count=1 of=$bs
125 [ $(getlong 0x1FE $bs 2) -eq 43605 ] || error "Not bootable"
127 uudecode <<EOT | ddq of=$bs conv=notrunc
128 begin-base64 644 -
129 /L+4nWgAkBeJ/BYHMcC5HgDzq1sfD6Gg8X1AxXd4BlexBvOlFh9kZo9HeMZF
130 +D/6l1hB6DwBvgACgUwQIIDGRCWbA3QO6GoBWwseKAJ0LFNH6AYBXuhaAbAg
131 zRCwCM0QTuhZATwIdAOIBK05NigCdPDoMgE8CnXgiHz+W4nm/0gQxkAVk4Dz
132 CHX0u/QBoRUCsQVmix9mS2bT60NoAAgHv4AAiXwTiUQbAfjR7yn7nHMCAd9Q
133 V1ZTMdvongBbXlmGzbSHFgfNFVidd9ChGQK7HAKxCTlEG3K6l80T6gAAIJCw
134 RijIvtgB6MoAXesjgPkTcgQ4wXdrgP4CcgQ45ndsYIH9AAZ0KgZSUVOWtAJQ
135 sQa1BMHFBLAPIegEkCcUQCfohAD+zXXssCDNEOK06I4AmM0TYVJQKMh3ArAB
136 mDn4cgKJ+FC0As0TlV5YWnKglUGO6YDHAk90S0519IzplTjBdT+IyP7GsQE4
137 5nU1iPT+xYD9ULYAdSq1AGC+2wH+RAxT6C8AW+g2AHUWUpjNE7gBAs0TWtDU
138 OmT+depGCOR15WHrkbAxLAO0DrsHAM0QPA1088OwDejv/6wIwHX4w79sBLFb
139 ZAINuA0BZDoNdArNFnT0mM0WjudHw1g6AEluc2VydCBkaXNrIDEHDQA=
140 ====
141 EOT
142 # Get setup
143 setupsz=$(getlong $SetupSzOfs $bs 1)
144 if [ $setupsz -eq 0 ]; then
145 setupsz=4
146 store 8 $SetupSzOfs $setupsz $bs "setup size $setupsz"
147 fi
148 [ -n "$DEBUG" ] && echo "Read setup ($setupsz sectors) ..." 1>&2
149 ddq if=$KERNEL bs=512 skip=1 count=$setupsz >> $bs
151 Version=$(getlong 0x206 $bs 2)
152 [ $(getlong $Magic $bs) -ne 1400005704 ] && Version=0
153 feature=""
154 while read prot kern info ; do
155 [ $Version -lt $((0x$prot)) ] && continue
156 feature="features $prot starting from kernel $kern "
157 done <<EOT
158 200 1.3.73 kernel_version, bzImage, initrd, loadflags/type_of_loader
159 201 1.3.76 heap_end_ptr
160 202 2.4.0 new cmdline
161 204 2.6.14 long syssize
162 EOT
163 [ -n "$DEBUG" ] && printf "Protocol %X $feature\n" $Version 1>&2
165 # Old kernels need bootsector patches to disable rescent features
166 while read minversion maxversion offset bytes rem; do
167 [ $Version -gt $(( 0x$maxversion )) ] && continue
168 [ $Version -lt $(( 0x$minversion )) ] && continue
169 patch $offset $bytes $bs "$rem"
170 done <<EOT
171 000 1FF 08D B8:00:01 force zImage (movw \$0x100, %ax)
172 000 1FF 0CB EB:0B skip initrd code
173 000 201 01E EB:1E:00:00:00:00 room for the cmdline magic
174 000 201 036 BE:00:00:E8:76:01:EB:0A:06:57:B1:06:F3:A5:EB:DE code in cmdline magic moved
175 000 1FF 039 90:90:90 no kernel version
176 000 201 04B 22:00 old cmdline ptr 1
177 000 201 06D 22:00 old cmdline ptr 2
178 000 203 1F6 00:00 syssize32
179 200 FFF 210 FF type_of_loader=FF
180 201 FFF 224 00:9B heap_end_ptr
181 EOT
182 if [ $Version -lt 514 ]; then
183 version_string=$((0x200 + $(getlong 0x20E $bs 2) ))
184 store 16 0x0037 $version_string $bs version_string
185 fi
186 if [ $Version -ge 512 -a $(getlong 0x214 $bs) -ge $((0x100000)) ]; then
187 patch 211 81 $bs loadflags=can_use_heap+loadhigh
188 patch 09D 10 $bs LOADSEG=0x1000
189 patch 0A0 00:01 $bs LOADSZ=0x10000
190 fi
191 [ -n "$CMDLINE" ] || patch 04D EB $bs "No cmdline"
192 [ -n "$NOEDIT" ] && patch 059 0D:46:EB:14 $bs 'mov CR,%al ; inc %si; jmp putal'
193 [ 1$TRACKS -ne 180 ] && store 8 0x171 $TRACKS $bs TRACKS
195 [ -n "$FLAGS" ] && store 16 $FlagsOfs $FLAGS $bs FLAGS
196 [ -n "$VIDEO" ] && store 16 $VideoModeOfs $VIDEO $bs VIDEO
197 [ -n "$RDEV" ] || case "$FORMAT" in
198 1200) RDEV=0x0208 ;;
199 1440) RDEV=0x021C ;;
200 2880) RDEV=0x0220 ;;
201 *) RDEV=0x0200 ;;
202 esac
203 while [ -L "$RDEV" ]; do RDEV="$(readlink "$RDEV")"; done
204 [ -b "$RDEV" ] && RDEV=$(stat -c '0x%02t%02T' $RDEV 2> /dev/null)
205 store 16 $RootDevOfs $RDEV $bs RDEV
207 [ $FORMAT -lt 1440 ] && store 8 0xEF 16 $bs 1.2M
208 [ $FORMAT -lt 1200 ] && store 8 0xEF 10 $bs 720K
209 [ $FORMAT -lt 720 ] && store 8 0x171 40 $bs 360K
210 [ $FORMAT -lt 360 ] && store 8 0xEF 9 $bs 320K
211 [ $FORMAT -lt 320 ] && store 8 0xF8 2 $bs 160K
213 # Info text after setup
214 if [ -s "$INFOFILE" ]; then
215 patch 048 9a:00:00:00:90 $bs lcall displayinfo
216 uudecode >$bs.infotext <<EOT
217 begin-base64 644 -
218 MdsGYI7D6AAAXoHGSgCJ8MHoCUii8QGwDbQOuwcAzRCsPAx1I79sBLFbJgIN
219 uBsBJjoNdAnNFnT0mM0Wjsc8IHPjPBt0BuvPCMB1zWEHCx4oAss=
220 ====
221 EOT
222 cat "$INFOFILE" >>$bs.infotext
223 if [ $Version -lt 514 ]; then
224 store 16 0x050 0x0022 $bs.infotext
225 fi
226 ddq if=/dev/zero bs=512 count=1 >>$bs.infotext
227 n=$(($(stat -c %s $bs.infotext)/512))
228 ddq if=$bs.infotext count=$n bs=512 >> $bs
229 rm -f $bs.infotext
230 store 8 0x1F1 $(($setupsz+$n)) $bs update setup size
231 store 8 0x04A $((2+2*$setupsz)) $bs update displayinfo call
232 fi
234 # Store cmdline after setup for kernels >= 0.99
235 if [ -n "$CMDLINE" ]; then
236 echo -n "$CMDLINE" | ddq bs=512 count=1 conv=sync >> $bs
237 CmdlineOfs=0x9E00 # Should be in 0x8000 .. 0xA000
238 ArgPtrOfs=0x228
239 ArgPtrVal=$(( $SetupBase + $CmdlineOfs ))
240 if [ $Version -lt 514 ]; then
241 ArgPtrOfs=0x0020
242 ArgPtrVal=$(( 0xA33F + ($CmdlineOfs << 16) ))
243 fi
244 store 32 $ArgPtrOfs $ArgPtrVal $bs "Cmdline '$CMDLINE'"
245 fi
247 # Compute initramfs size (protocol >= 2.00)
248 [ $Version -lt 512 ] && INITRD=""
249 initrdlen=0
250 INITRDPAD=4
251 INITRDALIGN=0x1000
252 for i in $( echo $INITRD | sed 's/,/ /' ); do
253 [ -s "$i" ] || continue
254 while [ -L "$i" ]; do i="$(readlink $i)"; done
255 size=$(( ($(stat -c %s "$i") + $INITRDPAD - 1) & -$INITRDPAD ))
256 [ -n "$DEBUG" ] && echo "initrd $i $size " 1>&2
257 initrdlen=$(( $initrdlen + $size ))
258 ADRSRD=$(( (($MEM * 0x100000) - $initrdlen) & -$INITRDALIGN ))
259 store 32 $RamfsAdrOfs $(( $ADRSRD )) $bs initrd adrs
260 store 32 $RamfsLenOfs $initrdlen $bs initrdlen
261 done
263 [ -n "$NOSYSSIZEFIX" ] || store 32 0x1F4 \
264 $(( ($(stat -c %s $KERNEL)+15)/16 - ($setupsz+1)*32)) $bs fix system size
266 # Output boot sector + setup + cmdline
267 ddq if=$bs
269 # Output kernel code
270 syssz=$(( ($(getlong 0x1F4 $bs)+31)/32 ))
271 cat $KERNEL /dev/zero | ddq bs=512 skip=$(( $setupsz+1 )) count=$syssz conv=sync
273 # Output initramfs
274 for i in $( echo $INITRD | sed 's/,/ /' ); do
275 [ -s "$i" ] || continue
276 ddq if=$i
277 padding=$(( $INITRDPAD - ($(stat -c %s $i) % $INITRDPAD) ))
278 [ $padding -eq $INITRDPAD ] || ddq if=/dev/zero bs=1 count=$padding
279 done
281 # Cleanup
282 rm -f $bs
283 }
285 if [ "$FORMAT" == "0" ]; then # unsplitted
286 floppyset > $PREFIX
287 PAD=$(( 512 - ($(stat -c %s $PREFIX) % 512) ))
288 [ $PAD -ne 512 ] && ddq if=/dev/zero bs=1 count=$PAD >> $PREFIX
289 exit
290 fi
291 floppyset | split -b ${FORMAT}k /dev/stdin floppy$$
292 i=1
293 ls floppy$$* 2> /dev/null | while read file ; do
294 output=$PREFIX$(printf "%03d" $i)
295 cat $file /dev/zero | ddq bs=1k count=$FORMAT conv=sync of=$output
296 echo $output
297 rm -f $file
298 i=$(( $i + 1 ))
299 done