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