wok annotate linux/stuff/bootloader.sh @ rev 14862

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