diff options
author | Michael Olbrich <m.olbrich@pengutronix.de> | 2011-03-12 15:05:00 +0100 |
---|---|---|
committer | Michael Olbrich <m.olbrich@pengutronix.de> | 2011-03-15 14:34:21 +0100 |
commit | 886cf5cf7d2f75e5b8624a7b020c34c554b48fba (patch) | |
tree | 62e916672e1fd40938b5eba43494102c97050fac /scripts | |
parent | 773b6d3896fc34098c3145e2c504b53f0df6c033 (diff) | |
download | ptxdist-886cf5cf7d2f75e5b8624a7b020c34c554b48fba.tar.gz ptxdist-886cf5cf7d2f75e5b8624a7b020c34c554b48fba.tar.xz |
image_hd: move adding the bootloader to a separate script
Signed-off-by: Michael Olbrich <m.olbrich@pengutronix.de>
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/genhdimg | 122 | ||||
-rw-r--r-- | scripts/lib/ptxd_make_bootable.sh | 53 |
2 files changed, 129 insertions, 46 deletions
diff --git a/scripts/genhdimg b/scripts/genhdimg index 490a43ade..84d2501a0 100755 --- a/scripts/genhdimg +++ b/scripts/genhdimg @@ -3,7 +3,7 @@ # Create a bootable harddisk (or compact flash) image from # a partition definition given from command line # -# (C) 2006 Pengutronix +# (C) 2006, 2011 Pengutronix # @@ -13,11 +13,9 @@ cat <<-EOF Usage: `basename "$0"` OPTIONS <svn rep> -h this help - -m <stage1> path to grub stage1 - -n <stafe2> path to grub stage2 -o <hdimg file> resulting hd image -i <img file> filesystem image - -p <def> partition definition (can be given multiple times) + -p <def> partition definition (can be given multiple times) A partition definition has the form start_sector:end_sector:partition_type:[imagefile] @@ -31,7 +29,7 @@ pad() { to="$2" blocks="$3" - if [ ! -f "$from" ]; then + if [ ! -e "$from" ]; then echo "$from does not exist" exit 1 fi @@ -62,20 +60,16 @@ partcount=0 while getopts "hm:n:o:p:" OPT do - case "$OPT" in - h) Usage - exit 1 - ;; - m) STAGE1="$OPTARG" - ;; - n) STAGE2="$OPTARG" - ;; - o) IMAGEFILE="$OPTARG" + case "$OPT" in + h) Usage + exit 1 + ;; + o) IMAGEFILE="$OPTARG" ;; p) partcount=$(($partcount + 1)) PART[$partcount]="$OPTARG" ;; - esac + esac done if [ "$partcount" == 0 ]; then @@ -93,51 +87,43 @@ if [ -z "$IMAGEFILE" ]; then exit 1 fi -IMAGETMP=$(mktemp) +IMAGETMP1=$(mktemp -p ${PTXDIST_TEMPDIR}) +IMAGETMP2=$(mktemp -p ${PTXDIST_TEMPDIR}) +IMAGETMP3=$(mktemp -p ${PTXDIST_TEMPDIR}) +IMAGETMP4=$(mktemp -p ${PTXDIST_TEMPDIR}) -# get the first 446 bytes of grub stage1 or from /dev/zero -if [ -n "$STAGE1" ]; then - echo "using stage1 file $STAGE1" - if [ ! -r "$STAGE1" ]; then - echo "cannot open stage1 file $STAGE1" - exit 1 - fi +echo "------------------------" +echo "Creating hard disk's MBR" +echo "------------------------" - dd if="$STAGE1" bs=446 count=1 > $IMAGEFILE 2>/dev/null -else - dd if=/dev/zero bs=446 count=1 > $IMAGEFILE 2>/dev/null -fi +# create the first 512 byte sector (MBR) -# add offset to first partition and fill it with grub stage2 if given -offset=$(( $(echo ${PART[1]} | awk -F: '{print $1}') - 1)) -if [ -n "$STAGE2" ]; then - echo "using stage2 file $STAGE2" - if [ ! -r "$STAGE2" ]; then - echo "cannot open stage2 file $STAGE2" - exit 1 - fi - pad "$STAGE2" "$IMAGETMP" $(($offset)) -else - cat /dev/zero | dd bs=512 count=$offset > "$IMAGETMP" 2>/dev/null -fi +# fill it up to offset 446 +dd if=/dev/zero bs=446 count=1 > $IMAGEFILE 2>/dev/null # append the partitions # mark the first partition bootable extraarg="-c" +echo "------------------------------------" +echo "Creating hard disk's partition table" +echo "------------------------------------" + for i in $(seq 4); do pstart=$(( $(echo ${PART[$i]} | awk -F: '{print $1}') )) pend=$(( $(echo ${PART[$i]} | awk -F: '{print $2}') )) ptype=$(( $(echo ${PART[$i]} | awk -F: '{print $3}') )) pimage=$(echo ${PART[$i]} | awk -F: '{print $4}') psize=$(($pend - $pstart + 1)) - + if [ $i -gt $partcount ]; then psize=0 fi - echo "Generating partition $i (start=$pstart, size=$psize type=$ptype)" + if [ ${psize} -ne 0 ]; then + echo "Partition $i (start=$pstart, size=$psize type=$ptype)" + fi # add 0x55aa magic to last partition in table if [ $i == 4 ]; then @@ -146,13 +132,57 @@ for i in $(seq 4); do genpart $extraarg -b $pstart -s $psize -t $ptype >> $IMAGEFILE - if [ -n "$pimage" ]; then - echo "Initializing partition $i with $pimage" - pad "$pimage" "$IMAGETMP" $(($psize)) + if [ -z "${pimage}" ]; then + pimage=/dev/zero fi + echo " Initializing partition $i with '$(basename $pimage)'" + eval TMPFILE=\$IMAGETMP${i} + pad "$pimage" "${TMPFILE}" $psize + extraarg="" done -cat $IMAGETMP >> $IMAGEFILE -rm -f $IMAGETMP +# add offset to first partition +offset=$(( $(echo ${PART[1]} | awk -F: '{print $1}') - 1)) +if [ ${offset} -gt 1 ]; then + echo + echo "-------------------------------------------" + echo "Creating hard disk's first partition offset" + echo "-------------------------------------------" + + echo -n "padding ${offset} empty sectors..." + dd if=/dev/zero bs=512 count=$offset >> $IMAGEFILE 2>/dev/null + echo "done" +fi + +echo +echo "----------------------------" +echo "Appending partitions content" +echo "----------------------------" + +if [ -s ${IMAGETMP1} ]; then + echo -n "adding content for partition 1..." + cat ${IMAGETMP1} >> ${IMAGEFILE} + echo "done" +fi +# FIXME padding sectors? +if [ -s ${IMAGETMP2} ]; then + echo -n "adding content for partition 2..." + cat ${IMAGETMP2} >> ${IMAGEFILE} + echo "done" +fi +# FIXME padding sectors? +if [ -s ${IMAGETMP3} ]; then + echo -n "adding content for partition 3..." + cat ${IMAGETMP3} >> ${IMAGEFILE} + echo "done" +fi +# FIXME padding sectors? +if [ -s ${IMAGETMP4} ]; then + echo -n "adding content for partition 4..." + cat ${IMAGETMP4} >> ${IMAGEFILE} + echo "done" +fi +rm -f ${IMAGETMP1} ${IMAGETMP2} ${IMAGETMP3} ${IMAGETMP4} + diff --git a/scripts/lib/ptxd_make_bootable.sh b/scripts/lib/ptxd_make_bootable.sh new file mode 100644 index 000000000..c702ef461 --- /dev/null +++ b/scripts/lib/ptxd_make_bootable.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# +# Copyright (C) 2011 by Michael Olbrich <m.olbrich@pengutronix.de> +# +# See CREDITS for details about who has contributed to this project. +# +# For further information about the PTXdist project and license conditions +# see the README file. +# + +# +# Make a disk image bootable. The bootloader is written to the MBR and +# the following sectors without touching the partition table. +# +# This will fail if the bootloader is too large for the free sectors. +# +# $1 the image to modify +# $2 number of free sectors at the start of the image +# $3 first part of the bootloader for the code area of the MBR +# $4 second part of the bootloader for sector 2..n +# +ptxd_make_bootable() { + local image="$1" + local bytes="$[$2 * 512]" + local stage1="$3" + local stage2="$4" + local opt="conv=notrunc" + local opt2 + + if [ ! -w "${image}" ]; then + ptxd_bailout "Cannot write image file '${stage1}'" + fi + if [ ! -r "${stage1}" ]; then + ptxd_bailout "Cannot open stage1 file '${stage1}'" + fi + if [ -z "${stage2}" ]; then + stage2="${stage1}" + opt2="${opt} seek=1 skip=1" + elif [ ! -r "${stage2}" ]; then + ptxd_bailout "Cannot open stage2 file '${stage1}'" + else + bytes="$[$bytes - 512]" + fi + local needed="$(stat --printf="%s" "${stage2}")" + if [ "${needed}" -gt "${bytes}" ]; then + ptxd_bailout "Not enough space to write stage2: available: ${bytes} needed: ${needed}" + fi + + dd if="${stage1}" of="${image}" ${opt} bs=446 count=1 2>/dev/null && + dd if="${stage2}" of="${image}" ${opt} ${opt2} bs=512 2>/dev/null +} +export -f ptxd_make_bootable + |