wok-undigest annotate linux/stuff/bootloader.sh @ rev 1257

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