wok-next diff linux-libre/stuff/bootloader.sh @ rev 9857
obconf: fix BUILD_DEPENDS (again)
author | Christophe Lincoln <pankso@slitaz.org> |
---|---|
date | Mon May 16 15:34:53 2011 +0200 (2011-05-16) |
parents | |
children | 903c03d31ebf |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/linux-libre/stuff/bootloader.sh Mon May 16 15:34:53 2011 +0200 1.3 @@ -0,0 +1,220 @@ 1.4 +#!/bin/sh 1.5 +# 1.6 +# This script creates a floppy image set from a linux bzImage and can merge 1.7 +# a cmdline and/or one or more initramfs. 1.8 +# The total size can not exceed 15M because INT 15H function 87H limitations. 1.9 +# 1.10 +# (C) 2009 Pascal Bellard - GNU General Public License v3. 1.11 + 1.12 +usage() 1.13 +{ 1.14 +cat <<EOT 1.15 +Usage: $0 bzImage [--prefix image_prefix] [--cmdline 'args'] 1.16 + [--rdev device] [--video mode] [--flags rootflags] [--tracks cnt] 1.17 + [--format 1440|1680|1920|2880 ] [--initrd initrdfile]... 1.18 + 1.19 +Default values: --format 1440 --tracks 80 --prefix floppy. 1.20 + 1.21 +Example: 1.22 +$0 /boot/vmlinuz-2.6.30.6 --rdev /dev/ram0 --video -3 --cmdline 'rw lang=fr_FR kmap=fr-latin1 laptop autologin' --initrd /boot/rootfs.gz --initrd ./myconfig.gz 1.23 +EOT 1.24 +exit 1 1.25 +} 1.26 + 1.27 +KERNEL="" 1.28 +INITRD="" 1.29 +CMDLINE="" 1.30 +PREFIX="floppy." 1.31 +FORMAT="1440" 1.32 +RDEV="" 1.33 +VIDEO="" 1.34 +FLAGS="" 1.35 +TRACKS="" 1.36 +DEBUG="" 1.37 +while [ -n "$1" ]; do 1.38 + case "$1" in 1.39 + --c*|-c*) CMDLINE="$2"; shift;; 1.40 + --i*|-i*) INITRD="$INITRD $2"; shift;; 1.41 + --p*|-p*) PREFIX="$2"; shift;; 1.42 + --fo*|-f*) FORMAT="$2"; shift;; 1.43 + --fl*) FLAGS="$2"; shift;; # 1 read-only, 0 read-write 1.44 + --r*|-r*) RDEV="$2"; shift;; # /dev/??? 1.45 + --v*|-v*) VIDEO="$2"; shift;; # -3 .. n 1.46 + --t*|-t*) TRACKS="$2"; shift;; # likely 81 .. 84 1.47 + --debug) DEBUG="1";; 1.48 + *) KERNEL="$1";; 1.49 + esac 1.50 + shift 1.51 +done 1.52 +[ -n "$KERNEL" -a -f "$KERNEL" ] || usage 1.53 +if [ -n "$TRACKS" ]; then 1.54 + if [ $(( $FORMAT % $TRACKS )) -ne 0 ]; then 1.55 + echo "Invalid track count for format $FORMAT." 1.56 + usage 1.57 + fi 1.58 +fi 1.59 + 1.60 +# write a 16 bits data 1.61 +# usage: store16 offset data16 file 1.62 +store16() 1.63 +{ 1.64 + n=$2; i=2; while [ $i -ne 0 ]; do 1.65 + printf '\\\\x%02X' $(($n & 255)) 1.66 + i=$(($i-1)); n=$(($n >> 8)) 1.67 + done | xargs echo -en | \ 1.68 + dd bs=2 conv=notrunc of=$3 seek=$(( $1 / 2 )) 2> /dev/null 1.69 + [ -n "$DEBUG" ] && printf "store16(%04X) = %04X\n" $1 $2 1>&2 1.70 +} 1.71 + 1.72 +# write a 32 bits data 1.73 +# usage: storelong offset data32 file 1.74 +storelong() 1.75 +{ 1.76 + n=$2; i=4; while [ $i -ne 0 ]; do 1.77 + printf '\\\\x%02X' $(($n & 255)) 1.78 + i=$(($i-1)); n=$(($n >> 8)) 1.79 + done | xargs echo -en | \ 1.80 + dd bs=4 conv=notrunc of=$3 seek=$(( $1 / 4 )) 2> /dev/null 1.81 + [ -n "$DEBUG" ] && printf "storelong(%04X) = %08X\n" $1 $2 1>&2 1.82 +} 1.83 + 1.84 +# read a 32 bits data 1.85 +# usage: getlong offset file 1.86 +getlong() 1.87 +{ 1.88 + dd if=$2 bs=1 skip=$(( $1 )) count=4 2> /dev/null | \ 1.89 + hexdump -e '"" 1/4 "%d" "\n"' 1.90 +} 1.91 + 1.92 +floppyset() 1.93 +{ 1.94 + # bzImage offsets 1.95 + CylinderCount=496 1.96 + SetupSzOfs=497 1.97 + FlagsOfs=498 1.98 + SyssizeOfs=500 1.99 + VideoModeOfs=506 1.100 + RootDevOfs=508 1.101 + CodeAdrOfs=0x214 1.102 + RamfsAdrOfs=0x218 1.103 + RamfsLenOfs=0x21C 1.104 + ArgPtrOfs=0x228 1.105 + 1.106 + # boot+setup address 1.107 + SetupBase=0x90000 1.108 + 1.109 + stacktop=0x9E00 1.110 + 1.111 + bs=/tmp/bs$$ 1.112 + 1.113 + # Get and patch boot sector 1.114 + # See http://hg.slitaz.org/wok/raw-file/711d076b277c/linux/stuff/linux-header-2.6.34.u 1.115 + dd if=$KERNEL bs=512 count=1 of=$bs 2> /dev/null 1.116 + uudecode <<EOT | dd of=$bs conv=notrunc 2> /dev/null 1.117 +begin-base64 644 - 1.118 +/L+6nWgAkAcGF4n8McC5HQDzq1sfD6mg8X1ABlfFd3ixBvOlZWaPR3gGH8ZF 1.119 ++D/6l1hB6DQBvgACA3QO6HYBWwseKAJ0LFNH6AoBXuhmAbAgzRCwCM0QTuhl 1.120 +ATwIdAOIBK05NigCdPDoPgE8CnXgiHz+ieb/TBD/TBi/9AGBTRz/gMdFMACc 1.121 +sBCxBUi0k4lEHLABiUQUmGaY0+BIZgMFZtPoaAAQB7+AACn4nHMCAccx21BW 1.122 +6J4AXrkAgLSH/kQczRVYnXfcoRoCvxwCsQk4RBxyuJPNE+oAACCQsEYoyL7b 1.123 +AejSAF3rI4D5E3IEOMF3a4D+AnIEOOZ3bGCB/QAGdCoGUlFTlrQCULEGtQTB 1.124 +xQSwDyHoBJAnFEAn6IwA/s117LAgzRDitOiWAJjNE2FSUCjIdwKwAZg5+HIC 1.125 +ifhQtALNE5VeWFpyoJVBjuGAxwJPdFFOdfSM4ZU4wXVFiMj+xrEBOOZ1O4j0 1.126 +/sW2AID9UHIwOi7wAXIqtQBgvt4B/kQMU+gxAFvoOAB1FlKYzRO4AQLNE1rQ 1.127 +1Dpk/nXqRgjkdeVh64sWB7AxLAO0DrsHAM0QPA1088OwDejv/6wIwHX4w79s 1.128 +BLFbZQINuA0BZToNdArNFnT0mM0Wju9Hw1g6AEluc2VydCBkaXNrIDEuBw0A 1.129 +AA== 1.130 +==== 1.131 +EOT 1.132 + 1.133 + # Get setup 1.134 + setupsz=$(getlong $SetupSzOfs $bs) 1.135 + setupszb=$(( $setupsz & 255 )) 1.136 + dd if=$KERNEL bs=512 skip=1 count=$setupszb 2> /dev/null >> $bs 1.137 + 1.138 + if [ -n "$TRACKS" ]; then 1.139 + [ -n "$DEBUG" ] && echo -n "--tracks " 1>&2 1.140 + n=$(getlong $CylinderCount $bs) 1.141 + store16 $CylinderCount $(( ($n & -256) + $TRACKS )) $bs 1.142 + fi 1.143 + if [ -n "$FLAGS" ]; then 1.144 + [ -n "$DEBUG" ] && echo -n "--flags " 1>&2 1.145 + store16 $FlagsOfs $FLAGS $bs 1.146 + fi 1.147 + if [ -n "$VIDEO" ]; then 1.148 + [ -n "$DEBUG" ] && echo -n "--video " 1>&2 1.149 + store16 $VideoModeOfs $VIDEO $bs 1.150 + fi 1.151 + if [ -n "$RDEV" ]; then 1.152 + [ -n "$DEBUG" ] && echo -n "--rdev " 1>&2 1.153 + n=$(stat -c '0x%02t%02T' $RDEV 2> /dev/null) 1.154 + [ -n "$n" ] || n=$RDEV 1.155 + store16 $RootDevOfs $n $bs 1.156 + fi 1.157 + 1.158 + # Store cmdline after setup 1.159 + if [ -n "$CMDLINE" ]; then 1.160 + [ -n "$DEBUG" ] && echo -n "--cmdline '$CMDLINE' " 1>&2 1.161 + echo -n "$CMDLINE" | dd bs=512 count=1 conv=sync 2> /dev/null >> $bs 1.162 + storelong $ArgPtrOfs $(( $SetupBase + $stacktop )) $bs 1.163 + fi 1.164 + 1.165 + # Compute initramfs size 1.166 + initrdlen=0 1.167 + padding=0 1.168 + for i in $( echo $INITRD | sed 's/,/ /' ); do 1.169 + [ -s "$i" ] || continue 1.170 + [ -n "$DEBUG" ] && echo "--initrd $i " 1>&2 1.171 + initrdlen=$(( $initrdlen + $padding )) 1.172 + padding=$(stat -c %s $i) 1.173 + initrdlen=$(( $initrdlen + $padding )) 1.174 + padding=$(( 4096 - ($padding & 4095) )) 1.175 + [ $padding -eq 4096 ] && padding=0 1.176 + done 1.177 + Ksize=$(( $(getlong $SyssizeOfs $bs)*16 )) 1.178 + Kpad=$(( (($Ksize+4095)/4096)*4096 - Ksize )) 1.179 + if [ $initrdlen -ne 0 ]; then 1.180 + [ -n "$DEBUG" ] && echo "initrdlen = $initrdlen " 1>&2 1.181 + Kbase=$(getlong $CodeAdrOfs $bs) 1.182 + storelong $RamfsAdrOfs \ 1.183 + $(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs 1.184 + storelong $RamfsLenOfs $(( ($initrdlen + 3) & -4 )) $bs 1.185 + fi 1.186 + 1.187 + # Output boot sector + setup + cmdline 1.188 + dd if=$bs 2> /dev/null 1.189 + 1.190 + # Output kernel code 1.191 + dd if=$KERNEL bs=512 skip=$(( $setupszb + 1 )) 2> /dev/null 1.192 + 1.193 + # Pad to next sector 1.194 + Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) )) 1.195 + [ $Kpad -eq 512 ] || dd if=/dev/zero bs=1 count=$Kpad 2> /dev/null 1.196 + 1.197 + # Output initramfs 1.198 + padding=0 1.199 + for i in $( echo $INITRD | sed 's/,/ /' ); do 1.200 + [ -s "$i" ] || continue 1.201 + [ $padding -ne 0 ] && dd if=/dev/zero bs=1 count=$padding 2> /dev/null 1.202 + dd if=$i 2> /dev/null 1.203 + padding=$(( 4 - ($(stat -c %s $i) & 3) )) 1.204 + [ $padding -eq 4 ] && padding=0 1.205 + done 1.206 + 1.207 + # Cleanup 1.208 + rm -f $bs 1.209 +} 1.210 + 1.211 +if [ "$FORMAT" == "0" ]; then # unsplitted 1.212 + floppyset > $PREFIX 1.213 + exit 1.214 +fi 1.215 +floppyset | split -b ${FORMAT}k /dev/stdin floppy$$ 1.216 +i=1 1.217 +ls floppy$$* | while read file ; do 1.218 + output=$PREFIX$(printf "%03d" $i) 1.219 + cat $file /dev/zero | dd bs=1k count=$FORMAT conv=sync of=$output 2> /dev/null 1.220 + echo $output 1.221 + rm -f $file 1.222 + i=$(( $i + 1 )) 1.223 +done