aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/mvebu/image/gen_mvebu_sdcard_img.sh
blob: 842e5910266a8dbb9bb307f6506a20d4f39f920e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/sh
#
# Copyright (C) 2016 Josua Mayer
#
# 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() {
	echo "$0 <outfile> [<bootloader> <type_partitionN> <size_partitionN> <img_partitionN>]?"
}

# always require first 2 or 3 arguments
# then in pairs up to 8 more for a total of up to 4 partitions
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
fi

set -e

# parameters
OUTFILE="$1"; shift
if [ -n "$BOOTLOADER" ]; then
	shift
fi

# generate image file
printf "Creating %s from /dev/zero: " "$OUTFILE"
dd if=/dev/zero of="$OUTFILE" bs=512 count=1 >/dev/null
printf "Done\n"

while [ "$#" -ge 3 ]; do
	ptgen_args="$ptgen_args -t $1 -p $(($2 * 1024 + 256))"
	parts="$parts$3 "
	shift; shift; shift
done

head=16
sect=63

# create real partition table using fdisk
printf "Creating partition table: "
set $(ptgen -o "$OUTFILE" -h $head -s $sect -l 1024 -S 0x$SIGNATURE $ptgen_args)
printf "Done\n"

# install bootloader
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

i=1
while [ "$#" -ge 2 ]; do
	img="${parts%% *}"
	parts="${parts#* }"

	printf "Writing %s to partition %i: " "$img" $i
	(
		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
	printf "Done\n"

	i=$((i+1))
	shift; shift
done