#!/bin/sh
#
# This script creates a floppy image set from a linux bzImage and can merge
# a cmdline and/or one or more initramfs.
# The total size can not exceed 15M because INT 15H function 87H limitations.
#
# (C) 2009 Pascal Bellard - GNU General Public License v3.

usage()
{
cat <<EOT
Usage: $0 bzImage [--prefix image_prefix] [--cmdline 'args']
       [--rdev device] [--video mode] [--flags rootflags] 
       [--format 1440|1680|1720|2880 ] [--initrd initrdfile]...

Example:
$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
EOT
exit 1
}

KERNEL=""
INITRD=""
CMDLINE=""
PREFIX="floppy."
FORMAT="1440"
RDEV=""
VIDEO=""
FLAGS=""
DEBUG=""
while [ -n "$1" ]; do
	case "$1" in
	--c*|-c*)  CMDLINE="$2"; shift;;
	--i*|-i*)  INITRD="$INITRD $2"; shift;;
	--p*|-p*)  PREFIX="$2"; shift;;
	--fo*|-f*) FORMAT="$2"; shift;;
	--fl*)     FLAGS="$2"; shift;;	# 1 read-only, 0 read-write
	--r*|-r*)  RDEV="$2"; shift;;	# /dev/???
	--v*|-v*)  VIDEO="$2"; shift;;	# -3 .. n
	--debug)   DEBUG="1";;
	*) KERNEL="$1";;
	esac
	shift
done
[ -n "$KERNEL" -a -f "$KERNEL" ] || usage

# write a 16 bits data
# usage: store16 offset data16 file
store16()
{
	echo $2 | awk '{ printf "\\\\x%02X\\\\x%02X",$1%256,($1/256)%256 }' | \
		xargs echo -en | \
	dd bs=2 conv=notrunc of=$3 seek=$(( $1 / 2 )) 2> /dev/null
	[ -n "$DEBUG" ] && printf "store16(%04X) = %04X\n" $1 $2 1>&2
}

# write a 32 bits data
# usage: storelong offset data32 file
storelong()
{
	echo $2 | awk '{ printf "\\\\x%02X\\\\x%02X\\\\x%02X\\\\x%02X",
		 $1%256,($1/256)%256,($1/256/256)%256,($1/256/256/256)%256 }' | \
		xargs echo -en | \
		dd bs=4 conv=notrunc of=$3 seek=$(( $1 / 4 )) 2> /dev/null
	[ -n "$DEBUG" ] && printf "storelong(%04X) = %08X\n" $1 $2 1>&2
}

# read a 32 bits data
# usage: getlong offset file
getlong()
{
	dd if=$2 bs=1 skip=$(( $1 )) count=4 2> /dev/null | \
		hexdump -e '"" 1/4 "%d" "\n"'
}

floppyset()
{
	# bzImage offsets
	SetupSzOfs=497
	FlagsOfs=498
	SyssizeOfs=500
	VideoModeOfs=506
	RootDevOfs=508
	CodeAdrOfs=0x214
	RamfsAdrOfs=0x218
	RamfsLenOfs=0x21C
	ArgPtrOfs=0x228

	# boot+setup address
	SetupBase=0x90000

	stacktop=0x9E00

	bs=/tmp/bs$$

	# Get and patch boot sector
	# See  http://hg.slitaz.org/wok/raw-file/711d076b277c/linux/stuff/linux-header-2.6.34.u
	dd if=$KERNEL bs=512 count=1 of=$bs 2> /dev/null
	uudecode <<EOT | dd of=$bs conv=notrunc 2> /dev/null
begin-base64 644 -
[payload removed: 664 characters in 12 lines]
====
EOT

	# Get setup
	setupsz=$(getlong $SetupSzOfs $bs)
	setupszb=$(( $setupsz & 255 ))
	dd if=$KERNEL bs=512 skip=1 count=$setupszb 2> /dev/null >> $bs

	if [ -n "$FLAGS" ]; then
		[ -n "$DEBUG" ] && echo -n "--flags " 1>&2
		store16 $FlagsOfs $(( $FLAGS )) $bs
	fi
	if [ -n "$VIDEO" ]; then
		[ -n "$DEBUG" ] && echo -n "--video " 1>&2
		store16 $VideoModeOfs $(( $VIDEO )) $bs
	fi
	if [ -n "$RDEV" ]; then
		[ -n "$DEBUG" ] && echo -n "--rdev " 1>&2
		n=$(stat -c '0x%02t%02T' $RDEV 2> /dev/null)
		[ -n "$n" ] || n=$RDEV
		store16 $RootDevOfs $(( $n )) $bs
	fi

	# Store cmdline after setup
	if [ -n "$CMDLINE" ]; then
		[ -n "$DEBUG" ] && echo -n "--cmdline '$CMDLINE' " 1>&2
		echo -n "$CMDLINE" | dd bs=512 count=1 conv=sync 2> /dev/null >> $bs
		storelong $ArgPtrOfs $(( $SetupBase + $stacktop )) $bs
	fi

	# Compute initramfs size
	initrdlen=0
	padding=0
	for i in $( echo $INITRD | sed 's/,/ /' ); do
		[ -s "$i" ] || continue
		[ -n "$DEBUG" ] && echo "--initrd $i " 1>&2
		initrdlen=$(( $initrdlen + $padding ))
		padding=$(stat -c %s $i)
		initrdlen=$(( $initrdlen + $padding ))
		padding=$(( 4096 - ($padding & 4095) ))
		[ $padding -eq 4096 ] && padding=0
	done
	Ksize=$(( $(getlong $SyssizeOfs $bs)*16 ))
	Kpad=$(( (($Ksize+4095)/4096)*4096 - Ksize ))
	if [ $initrdlen -ne 0 ]; then
		[ -n "$DEBUG" ] && echo "initrdlen = $initrdlen " 1>&2
		Kbase=$(getlong $CodeAdrOfs $bs)
		storelong $RamfsAdrOfs \
			$(( (0x1000000 - $initrdlen) & 0xFFFF0000 )) $bs
		storelong $RamfsLenOfs $initrdlen $bs
	fi

	# Output boot sector + setup + cmdline
	dd if=$bs 2> /dev/null

	# Output kernel code
	dd if=$KERNEL bs=512 skip=$(( $setupszb + 1 )) 2> /dev/null

	# Pad to next sector
	Kpad=$(( 512 - ($(stat -c %s $KERNEL) & 511) ))
	[ $Kpad -eq 512 ] || dd if=/dev/zero bs=1 count=$Kpad 2> /dev/null

	# Output initramfs
	padding=0
	for i in $( echo $INITRD | sed 's/,/ /' ); do
		[ -s "$i" ] || continue
		[ $padding -ne 0 ] && dd if=/dev/zero bs=1 count=$padding 2> /dev/null
		dd if=$i 2> /dev/null
		padding=$(( 4 - ($(stat -c %s $i) & 3) ))
		[ $padding -eq 4 ] && padding=0
	done

	# Cleanup
	rm -f $bs
}

floppyset | split -b ${FORMAT}k /dev/stdin floppy$$
i=1
ls floppy$$* | while read file ; do
	output=$PREFIX$(printf "%03d" $i)
	cat $file /dev/zero | dd bs=1k count=$FORMAT conv=sync of=$output 2> /dev/null
	echo $output
	rm -f $file
	i=$(( $i + 1 ))
done
