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

Up zstd (1.4.9)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Apr 13 17:03:24 2021 +0000 (2021-04-13)
parents abe27fd0192d
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 Or: cat fd0*.img | $0 --extract
20 Create the kernel, cmdline and rootfs files from a floppy set
22 Example:
23 $0 /boot/vmlinuz-2.6.30.6 --rdev /dev/ram0 --video -3 \
24 --cmdline 'rw lang=fr_FR kmap=fr-latin1 laptop autologin' \
25 --initrd /boot/rootfs.gz --initrd ./myconfig.gz
26 EOT
27 exit 1
28 }
30 ddq()
31 {
32 dd $@ 2> /dev/null
33 }
35 dbg()
36 {
37 [ -n "$DEBUG" ] && echo "$@" 1>&2
38 }
40 # write a 32 bits data
41 # usage: put offset data32 file [bytes]
42 put()
43 {
44 n=$2; for i in $(seq 1 ${4:-4}); do
45 printf '\\\\x%02X' $(($n & 255))
46 n=$(($n >> 8))
47 done | xargs echo -en | ddq bs=1 conv=notrunc of=$3 seek=$1
48 [ -n "$DEBUG" ] && printf "put$4(%04X) = %X\n" $1 $2 1>&2
49 }
51 # read a 32 bits data
52 # usage: get offset file [bytes]
53 get()
54 {
55 ddq if=$2 bs=1 skip=$(($1)) count=${3:-4} | hexdump -e '"" 1/4 "%d"'
56 }
58 SetupSzOfs=0x1F1
59 SyssizeOfs=0x1F4
60 RamfsLenOfs=0x21C
61 ArgPtrOfs=0x228
63 floppyset()
64 {
65 # bzImage offsets
66 CylinderCount=0x1F0
67 FlagsOfs=0x1F2
68 VideoModeOfs=0x1FA
69 RootDevOfs=0x1FC
70 CodeAdrOfs=0x214
71 RamfsAdrOfs=0x218
73 # boot+setup address
74 SetupBase=0x90000
76 stacktop=0x9E00
78 bs=/tmp/bs$$
80 # Get and patch boot sector
81 # See http://hg.slitaz.org/wok/raw-file/711d076b277c/linux/stuff/linux-header-2.6.34.u
82 ddq if=$KERNEL bs=512 count=1 of=$bs
83 uudecode <<EOT | ddq of=$bs conv=notrunc
84 begin-base64 644 -
85 /L+6nWgAkAcGF4n8McC5HQDzq1sfD6mg8X1ABlfFd3ixBvOlZWaPR3gGH8ZF
86 +D/6l1hB6DQBvgACA3QO6HYBWwseKAJ0LFNH6AoBXuhmAbAgzRCwCM0QTuhl
87 ATwIdAOIBK05NigCdPDoPgE8CnXgiHz+ieb/TBD/TBi/9AGBTRz/gMdFMACc
88 sBCxBUi0k4lEHLABiUQUmGaY0+BIZgMFZtPoaAAQB7+AACn4nHMCAccx21BW
89 6J4AXrkAgLSH/kQczRVYnXfcoRoCvxwCsQk4RBxyuJPNE+oAACCQsEYoyL7b
90 AejSAF3rI4D5E3IEOMF3a4D+AnIEOOZ3bGCB/QAGdCoGUlFTlrQCULEGtQTB
91 xQSwDyHoBJAnFEAn6IwA/s117LAgzRDitOiWAJjNE2FSUCjIdwKwAZg5+HIC
92 ifhQtALNE5VeWFpyoJVBjuGAxwJPdFFOdfSM4ZU4wXVFiMj+xrEBOOZ1O4j0
93 /sW2AID9UHIwOi7wAXIqtQBgvt4B/kQMU+gxAFvoOAB1FlKYzRO4AQLNE1rQ
94 1Dpk/nXqRgjkdeVh64sWB7AxLAO0DrsHAM0QPA1088OwDejv/6wIwHX4w79s
95 BLFbZQINuA0BZToNdArNFnT0mM0Wju9Hw1g6AEluc2VydCBkaXNrIDEuBw0A
96 AA==
97 ====
98 EOT
100 # Get setup
101 setupsz=$(get $SetupSzOfs $bs 1)
102 ddq if=$KERNEL bs=512 skip=1 count=$setupsz >> $bs
104 if [ -n "$TRACKS" ]; then
105 dbg -n "--tracks "
106 put $CylinderCount $TRACKS $bs 1
107 fi
108 if [ -n "$FLAGS" ]; then
109 dbg -n "--flags "
110 put $FlagsOfs $FLAGS $bs 2
111 fi
112 if [ -n "$VIDEO" ]; then
113 dbg -n "--video "
114 put $VideoModeOfs $VIDEO $bs 2
115 fi
116 if [ -n "$RDEV" ]; then
117 dbg -n "--rdev "
118 n=$(stat -c '0x%02t%02T' $RDEV 2> /dev/null)
119 [ -n "$n" ] || n=$RDEV
120 put $RootDevOfs $n $bs 2
121 fi
123 # Store cmdline after setup
124 if [ -n "$CMDLINE" ]; then
125 dbg -n "--cmdline '$CMDLINE' "
126 echo -n "$CMDLINE" | ddq bs=512 count=1 conv=sync >> $bs
127 put $ArgPtrOfs $(( $SetupBase + $stacktop )) $bs
128 fi
130 # Compute initramfs size
131 initrdlen=0
132 padding=0
133 for i in $( echo $INITRD | sed 's/,/ /' ); do
134 [ -s "$i" ] || continue
135 dbg "--initrd $i "
136 initrdlen=$(( $initrdlen + $padding ))
137 padding=$(stat -c %s $i)
138 initrdlen=$(( $initrdlen + $padding ))
139 padding=$(( 4096 - ($padding & 4095) ))
140 [ $padding -eq 4096 ] && padding=0
141 done
142 Ksize=$(( $(get $SyssizeOfs $bs)*16 ))
143 Kpad=$(( (($Ksize+4095)/4096)*4096 - Ksize ))
144 if [ $initrdlen -ne 0 ]; then
145 dbg "initrdlen = $initrdlen "
146 Kbase=$(get $CodeAdrOfs $bs)
147 put $RamfsAdrOfs \
148 $(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs
149 put $RamfsLenOfs $(( ($initrdlen + 3) & -4 )) $bs
150 fi
152 # Output boot sector + setup + cmdline
153 ddq if=$bs
155 # Output kernel code
156 ddq if=$KERNEL bs=512 skip=$(( $setupsz + 1 ))
158 # Pad to next sector
159 Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) ))
160 [ $Kpad -eq 512 ] || ddq if=/dev/zero bs=1 count=$Kpad
162 # Output initramfs
163 padding=0
164 for i in $( echo $INITRD | sed 's/,/ /' ); do
165 [ -s "$i" ] || continue
166 [ $padding -ne 0 ] && ddq if=/dev/zero bs=1 count=$padding
167 ddq if=$i
168 padding=$(( 4 - ($(stat -c %s $i) & 3) ))
169 [ $padding -eq 4 ] && padding=0
170 done
172 # Cleanup
173 rm -f $bs
174 }
176 KERNEL=""
177 INITRD=""
178 CMDLINE=""
179 PREFIX="floppy."
180 FORMAT="1440"
181 RDEV=""
182 VIDEO=""
183 FLAGS=""
184 TRACKS=""
185 DEBUG=""
186 while [ -n "$1" ]; do
187 case "${1/--/-}" in
188 -c*) CMDLINE="$2"; shift;;
189 -i*) INITRD="$INITRD $2"; shift;;
190 -p*) PREFIX="$2"; shift;;
191 -fl*) FLAGS="$2"; shift;; # 1 read-only, 0 read-write
192 -f*) FORMAT="$2"; shift;;
193 -r*) RDEV="$2"; shift;; # /dev/???
194 -v*) VIDEO="$2"; shift;; # -3 .. n
195 -t*) TRACKS="$2"; shift;; # likely 81 .. 84
196 -d*) DEBUG="1";;
197 -e*) ddq bs=512 count=2 > kernel
198 setupsz=$(get $SetupSzOfs kernel 1)
199 ddq bs=512 count=$(($setupsz - 1)) >> kernel
200 [ $(get $ArgPtrOfs kernel) -ne 0 ] &&
201 ddq bs=512 count=1 | strings > cmdline
202 syssz=$(get $SyssizeOfs kernel)
203 ddq bs=512 count=$(( ($syssz + 31) / 32 )) >> kernel
204 ddq bs=16 seek=$(($syssz + 32 + $setupsz*32)) count=0 of=kernel
205 ramsz=$(get $RamfsLenOfs kernel)
206 ddq bs=512 count=$((($ramsz + 511) / 512)) of=rootfs
207 ddq bs=1 seek=$ramsz count=0 of=rootfs
208 exit ;;
209 *) KERNEL="$1";;
210 esac
211 shift
212 done
213 [ -n "$KERNEL" -a -f "$KERNEL" ] || usage
214 [ -n "$TRACKS" ] && [ $(( $FORMAT % $TRACKS )) -ne 0 ] &&
215 echo "Invalid track count for format $FORMAT." && usage
217 if [ "$FORMAT" == "0" ]; then # unsplitted
218 floppyset > $PREFIX
219 exit
220 fi
221 floppyset | split -b ${FORMAT}k /dev/stdin floppy$$
222 i=1
223 ls floppy$$* | while read file ; do
224 output=$PREFIX$(printf "%03d" $i)
225 cat $file /dev/zero | ddq bs=1k count=$FORMAT conv=sync of=$output
226 echo $output
227 rm -f $file
228 i=$(( $i + 1 ))
229 done