2016-08-09 13:23:24 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Copyright (C) 2016 Josua Mayer
|
2018-01-24 14:32:58 +00:00
|
|
|
#
|
2016-08-09 13:23:24 +00:00
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
|
|
|
|
usage() {
|
2019-05-11 17:37:32 +00:00
|
|
|
echo "$0 <outfile> [<bootloader> <type_partitionN> <size_partitionN> <img_partitionN>]?"
|
2016-08-09 13:23:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-14 16:28:09 +00:00
|
|
|
# always require first 2 or 3 arguments
|
2016-08-09 13:23:24 +00:00
|
|
|
# then in pairs up to 8 more for a total of up to 4 partitions
|
2018-02-14 16:28:09 +00:00
|
|
|
if [ $# -lt 1 ] || [ $# -gt 14 ] || [ $((($# - 1) % 3)) -ne 0 ]; then
|
|
|
|
if [ $# -lt 2 ] || [ $# -gt 15 ] || [ $((($# - 2) % 3)) -ne 0 ]; then
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
BOOTLOADER="$2"
|
|
|
|
fi
|
2016-08-09 13:23:24 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# parameters
|
2016-09-01 13:29:48 +00:00
|
|
|
OUTFILE="$1"; shift
|
2018-02-14 16:28:09 +00:00
|
|
|
if [ -n "$BOOTLOADER" ]; then
|
|
|
|
shift
|
|
|
|
fi
|
2016-08-09 13:23:24 +00:00
|
|
|
|
|
|
|
# generate image file
|
|
|
|
printf "Creating $OUTFILE from /dev/zero: "
|
|
|
|
dd if=/dev/zero of="$OUTFILE" bs=512 count=1 >/dev/null
|
|
|
|
printf "Done\n"
|
|
|
|
|
2016-09-01 13:29:48 +00:00
|
|
|
while [ "$#" -ge 3 ]; do
|
2019-05-11 17:37:32 +00:00
|
|
|
ptgen_args="$ptgen_args -t $1 -p $(($2 * 1024 + 256))"
|
2016-09-01 13:29:48 +00:00
|
|
|
parts="$parts$3 "
|
|
|
|
shift; shift; shift
|
2016-08-09 13:23:24 +00:00
|
|
|
done
|
|
|
|
|
2016-09-01 13:29:48 +00:00
|
|
|
head=16
|
|
|
|
sect=63
|
2016-08-09 13:23:24 +00:00
|
|
|
|
|
|
|
# create real partition table using fdisk
|
|
|
|
printf "Creating partition table: "
|
2019-07-24 22:51:27 +00:00
|
|
|
set $(ptgen -o "$OUTFILE" -h $head -s $sect -l 1024 -S 0x$SIGNATURE $ptgen_args)
|
2016-08-09 13:23:24 +00:00
|
|
|
printf "Done\n"
|
|
|
|
|
|
|
|
# install bootloader
|
2018-02-14 16:28:09 +00:00
|
|
|
if [ -n "$BOOTLOADER" ]; then
|
|
|
|
printf "Writing bootloader: "
|
|
|
|
dd of="$OUTFILE" if="$BOOTLOADER" bs=512 seek=1 conv=notrunc 2>/dev/null
|
|
|
|
printf "Done\n"
|
|
|
|
fi
|
2016-08-09 13:23:24 +00:00
|
|
|
|
2016-09-01 13:29:48 +00:00
|
|
|
i=1
|
|
|
|
while [ "$#" -ge 2 ]; do
|
|
|
|
img="${parts%% *}"
|
|
|
|
parts="${parts#* }"
|
2016-08-09 13:23:24 +00:00
|
|
|
|
|
|
|
printf "Writing %s to partition %i: " "$img" $i
|
2016-09-08 12:19:34 +00:00
|
|
|
(
|
|
|
|
cat "$img"
|
|
|
|
# add padding to avoid leaving behind old overlay fs data
|
|
|
|
dd if=/dev/zero bs=128k count=1 2>/dev/null
|
|
|
|
) | dd of="$OUTFILE" bs=512 seek=$(($1 / 512)) conv=notrunc 2>/dev/null
|
2016-08-09 13:23:24 +00:00
|
|
|
printf "Done\n"
|
|
|
|
|
2016-09-01 13:29:48 +00:00
|
|
|
let i=i+1
|
|
|
|
shift; shift
|
2016-08-09 13:23:24 +00:00
|
|
|
done
|