#!/bin/sh # # This script creates a floppy image set from a linux bzImage and can merge # a cmdline and/or one or more initramfs. # # (C) 2009-2015 Pascal Bellard - GNU General Public License v3. usage() { cat < /dev/null; } get() { echo $( od -v -j $(($1)) -N ${4:-${3:-2}} -t u${3:-2} -w${3:-2} -An $2 2>/dev/null || hexdump -v -s $(($1)) -n ${4:-${3:-2}} -e "\"\" 1/${3:-2} \" %d\n\"" $2 ) } trace() { [ -n "$DEBUG" ] && printf "$@" 1>&2 && echo 1>&2 } # usage: store bits offset data file store() { n=$3; for i in $(seq 8 8 $1); do printf '\\\\x%02X' $(($n & 255)) n=$(($n >> 8)) done | xargs echo -en | ddq bs=1 conv=notrunc of=$4 seek=$(($2)) s=$1; a=$2; d=$3; shift 4; c="$@" trace "store$s(%03X) = %0$(($s/4))X $c" $a $d } die() { echo $@ 1>&2 exit 1 } extract() { shift [ ! -s "$1" ] && die "No floppy ?" [ $(get 0x1FE "$1") -ne 43605 ] && die "Not bootable" [ $(get $Magic "$1" 4) != 1400005704 ] && [ $(get 0x1F4 "$1") -gt 32768 -o $(get 0x1F6 "$1") -ne 0 ] && die "Not linux." FORMAT="$(($(stat -c "%s" $1)/1024))" cat <kernel files="kernel" if [ "$MYBB" ]; then store 8 $SetupSzOfs $(($n-1)) kernel "setup size $n" store 16 0x22 0 kernel "clear cmdline" store 16 0x1EF 0 kernel "clear info" fi [ $cmdline -ne 0 ] && files="$files cmdline" && ddq bs=512 count=1 | strings | sed q > cmdline [ $info -ne 0 ] && files="$files info" && ddq bs=512 count=$infolen | sed \ 's/'$(echo -en "\xff\xff$//;s/\xff/\f")'/g;s/\r/\n/g;q' > info syssz=$(get 0x1F4 kernel 4) ddq bs=16 count=$syssz >>kernel [ $(($syssz % 32)) -ne 0 ] && ddq bs=16 of=/dev/null count=$((32 - ($syssz % 32))) if [ $(get $Magic kernel 4) == 1400005704 ]; then ddq bs=1 count=200 skip=$((512+$(get 0x20E kernel 2))) \ if=kernel | strings | sed q len=$(get $RamfsLenOfs kernel 4) if [ $len -ne 0 ]; then adrs=$(get $RamfsAdrOfs kernel 4) printf "--address-initrd 0x%X \n" $adrs echo "--mem $(((($adrs+$len)/1024+512)/1024))" ddq bs=512 count=$((($len+511)/512)) > initrd ddq count=0 bs=1 seek=$len of=initrd files="$files initrd" store 64 $RamfsAdrOfs 0 kernel "reset initrd" fi if [ $(get 0x206 kernel) -ge 514 ]; then store 32 0x228 0 kernel "clean cmdline32" fi else len=$(get $OldRamfsLenOfs kernel) [ $len -ne 0 ] && files="$files initrd" && if [ -s "$2" ]; then ddq if=$2 bs=1024 count=$len of=initrd else ddq if=$1 bs=1024 skip=256 count=$len of=initrd fi fi ls -l $files } exit } KERNEL="" INITRD="" ADRSRD="" CMDLINE="" PREFIX="floppy." FORMAT="1440" RDEV="" VIDEO="" FLAGS="" TRACKS="80" MEM="16" HIDE="" NOEDIT="" QUIET="" NOSYSSIZEFIX="" INFOFILE="" DEBUG="" while [ -n "$1" ]; do case "${1/--/-}" in -c*) CMDLINE="$2"; shift;; -inf*) INFOFILE="$2"; shift;; -i*) INITRD="$INITRD $2"; shift;; -a*) ADRSRD="$2"; shift;; -h*) HIDE="1";; -p*) PREFIX="$2"; shift;; -fl*)FLAGS="$2"; shift;; # 1 read-only, 0 read-write -f*) FORMAT="$2"; shift;; -m*) MEM="$(echo $2 | sed 's/[^0-9]//g')"; shift;; -r*) RDEV="$2"; shift;; -v*) VIDEO="$2"; shift;; # -3 .. n -t*) TRACKS="$2"; shift;; # likely 81 .. 84 -n*) NOSYSSIZEFIX="1";; -debug) DEBUG="1";; -d*) NOEDIT="1";; -q*) QUIET="1";; -e*) extract "$@";; *) KERNEL="$1";; esac shift done [ -n "$KERNEL" -a -f "$KERNEL" ] || usage while [ -L "$KERNEL" ]; do KERNEL="$(readlink "$KERNEL")"; done if [ $(( $FORMAT % $TRACKS )) -ne 0 ]; then echo "Invalid track count for format $FORMAT." usage fi [ 0$MEM -lt 2 ] && MEM=2 patch() { echo -en $(echo ":$2" | sed 's/:/\\x/g') | \ ddq bs=1 conv=notrunc of=$3 seek=$((0x$1)) trace "patch $1 $2 $4" } error() { rm -f $bs die $@ } floppyset() { # boot+setup address SetupBase=0x90000 bs=/tmp/bs$$ # Get and patch boot sector # See http://hg.slitaz.org/wok/raw-file/13835bce7189/syslinux/stuff/iso2exe/bootloader.S trace "Read bootsector..." ddq if=$KERNEL bs=512 count=1 of=$bs [ $(get 0x1FE $bs) -eq 43605 ] || error "Not bootable" uudecode <> $bs Version=$(get 0x206 $bs) [ $(get $Magic $bs 4) != 1400005704 ] && Version=0 feature="" while read prot kern info ; do [ $Version -lt $((0x$prot)) ] && continue feature="features $prot starting from kernel $kern " done < /dev/null) [ "$(echo $RDEV | tr '[0-9A-FXa-fx]' 0 | sed 's/0//g')" ] || store 16 $RootDevOfs $RDEV $bs RDEV [ $FORMAT -lt 720 ] && store 8 0x15F 40 $bs 360K [ $FORMAT -lt 320 ] && store 8 0x158 237 $bs 160K extra=0 # Store cmdline after setup for kernels >= 0.99 if [ -n "$CMDLINE" ]; then CmdlineOfs=$(stat -c '%s' $bs) store 16 0x22 $CmdlineOfs $bs "Cmdline @$CmdlineOfs '$CMDLINE'" [ $Version -ge 514 ] && store 32 0x228 $(( $SetupBase + 0x8000 )) $bs "Cmdline32" echo -n "$CMDLINE" | ddq bs=512 count=1 conv=sync >> $bs extra=$(($extra+1)) store 8 0x1F1 $(($setupsz+$extra)) $bs setup size [ $Version -ge 512 ] && [ -n "$QUIET" ] && store 8 0x211 $(($(get 0x211 $bs 1) | 32)) $bs suppress early messages fi # Info text after setup if [ -s "$INFOFILE" ]; then InfoOfs=$(stat -c '%s' $bs) sed -e ':a;N;$!ba;s/\r\n/\r/g;s/\n/\r/g' \ -e 's/'$(echo -e "\f/\xff/g;s/$/\xff\xff")'/' \ < "$INFOFILE" > $bs.infotext ddq if=/dev/zero bs=512 count=1 >>$bs.infotext infolen=$(($(stat -c %s $bs.infotext)/512)) ddq if=$bs.infotext count=$infolen bs=512 >> $bs extra=$(($extra+$infolen)) rm -f $bs.infotext store 8 0x1F1 $(($setupsz+$extra)) $bs setup size store 16 0x1EF $InfoOfs $bs update infoptr fi syssz=$((($(stat -c %s $KERNEL)+15)/16-32*(1+$setupsz))) #syssz=$(get 0x1F4 $bs 4) sysszsect=$((($syssz+31)/32)) store 16 $OldRamfsLenOfs 0 $bs clear oldramfs INITRD="${INITRD# }" INITRDPAD=4 [ -n "$INITRD" ] && if [ $Version -lt 512 ]; then # Compute initramfs location (protocol < 2.00) [ $syssz -gt 32768 ] && syssz=$(get 0x1F4 $bs 2) [ $syssz -eq 0 ] && syssz=$((0x7F00)) sysszsect=$((($syssz+31)/32)) INITRD="${INITRD%% *}" initrdlen=$(stat -c %s "$INITRD") store 16 $OldRamfsLenOfs $(($initrdlen/1024)) $bs set oldramfs INITRDDISKALIGN=$((0x40000)) [ $(($initrdlen+$INITRDDISKALIGN)) -gt $(($FORMAT*1024)) -o \ $(((512*$sysszsect) + $(stat -c %s $bs))) -gt $INITRDDISKALIGN -o \ -n "$ADRSRD" ] && INITRDDISKALIGN=$(($FORMAT*1024)) else INITRDRAMALIGN=0x1000 # Compute initramfs size (protocol >= 2.00) initrdlen=0 INITRDDISKALIGN=0 for i in ${INITRD//,/ }; do [ -s "$i" ] || continue while [ -L "$i" ]; do i="$(readlink $i)"; done size=$(stat -c %s "$i") trace "initrd $i $size " initrdlen=$(((($initrdlen + $INITRDPAD - 1) & -$INITRDPAD) + $size)) ADRSRD2=$(( (($MEM * 0x100000) - $initrdlen) & -$INITRDRAMALIGN )) store 32 $RamfsAdrOfs $(( ${ADRSRD:-$ADRSRD2} )) $bs initrd adrs store 32 $RamfsLenOfs $initrdlen $bs initrdlen done fi [ -n "$NOSYSSIZEFIX" ] || store 32 0x1F4 $syssz $bs fix system size # Output boot sector + setup + cmdline + info ddq if=$bs # Output kernel code [ $INITRDDISKALIGN -ne 0 ] && INITRDDISKALIGN=$(($INITRDDISKALIGN/512-$sysszsect-$extra-$setupsz-1)) cat $KERNEL /dev/zero | ddq bs=512 skip=$(( $setupsz+1 )) \ count=$(($sysszsect+$INITRDDISKALIGN)) conv=sync # Output initramfs padding=$INITRDPAD for i in ${INITRD//,/ }; do [ -s "$i" ] || continue [ $padding -eq $INITRDPAD ] || ddq if=/dev/zero bs=1 count=$padding ddq if=$i trace "initrd $i ($(stat -c %s $i) bytes) padding $INITRDPAD" padding=$(( $INITRDPAD - ($(stat -c %s $i) % $INITRDPAD) )) done # Cleanup rm -f $bs } if [ "$FORMAT" == "0" ]; then # unsplitted floppyset > $PREFIX PAD=$(( 512 - ($(stat -c %s $PREFIX) % 512) )) [ $PAD -ne 512 ] && ddq if=/dev/zero bs=1 count=$PAD >> $PREFIX exit fi floppyset | split -b ${FORMAT}k /dev/stdin floppy$$ i=1 ls floppy$$* 2> /dev/null | while read file ; do output=$PREFIX$(printf "%03d" $i) cat $file /dev/zero | ddq bs=1k count=$FORMAT conv=sync of=$output echo $output rm -f $file i=$(( $i + 1 )) done .