#!/bin/sh

rd()
{
	echo $(od -j $((0x$1)) -N ${2:-4} -t u${2:-4} -An kernel)
}

ds()
{
	dd count=$1 bs=${2:-512} 2>/dev/null
}

case "$1" in
extract) (
	ds 1
	ds $(rd 1F1 1)
	[ $(rd 228) -ne 0 ] && ds 1 | strings >cmdline
	ds $(( ($(rd 1F4) +31) /32)) ) >kernel
	ds $(( ($(rd 21C) +3) /4)) 4 >rootfs
	;;
*)	cat <<EOT
usage: 
# cat fd0*.img | $0 extract
	creates kernel, rootfs and cmdline files
EOT
	for i in 1 2 3; do
		j=" cat fd$i*.img |"
		echo "#$j cpio -i 2>/dev/null ||$j unlzma | cpio -idmu"
	done
esac
