842e5910266a8dbb9bb307f6506a20d4f39f920e
[openwrt/openwrt.git] / target / linux / mvebu / image / gen_mvebu_sdcard_img.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2016 Josua Mayer
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #
19
20 usage() {
21 echo "$0 <outfile> [<bootloader> <type_partitionN> <size_partitionN> <img_partitionN>]?"
22 }
23
24 # always require first 2 or 3 arguments
25 # then in pairs up to 8 more for a total of up to 4 partitions
26 if [ $# -lt 1 ] || [ $# -gt 14 ] || [ $((($# - 1) % 3)) -ne 0 ]; then
27 if [ $# -lt 2 ] || [ $# -gt 15 ] || [ $((($# - 2) % 3)) -ne 0 ]; then
28 usage
29 exit 1
30 else
31 BOOTLOADER="$2"
32 fi
33 fi
34
35 set -e
36
37 # parameters
38 OUTFILE="$1"; shift
39 if [ -n "$BOOTLOADER" ]; then
40 shift
41 fi
42
43 # generate image file
44 printf "Creating %s from /dev/zero: " "$OUTFILE"
45 dd if=/dev/zero of="$OUTFILE" bs=512 count=1 >/dev/null
46 printf "Done\n"
47
48 while [ "$#" -ge 3 ]; do
49 ptgen_args="$ptgen_args -t $1 -p $(($2 * 1024 + 256))"
50 parts="$parts$3 "
51 shift; shift; shift
52 done
53
54 head=16
55 sect=63
56
57 # create real partition table using fdisk
58 printf "Creating partition table: "
59 set $(ptgen -o "$OUTFILE" -h $head -s $sect -l 1024 -S 0x$SIGNATURE $ptgen_args)
60 printf "Done\n"
61
62 # install bootloader
63 if [ -n "$BOOTLOADER" ]; then
64 printf "Writing bootloader: "
65 dd of="$OUTFILE" if="$BOOTLOADER" bs=512 seek=1 conv=notrunc 2>/dev/null
66 printf "Done\n"
67 fi
68
69 i=1
70 while [ "$#" -ge 2 ]; do
71 img="${parts%% *}"
72 parts="${parts#* }"
73
74 printf "Writing %s to partition %i: " "$img" $i
75 (
76 cat "$img"
77 # add padding to avoid leaving behind old overlay fs data
78 dd if=/dev/zero bs=128k count=1 2>/dev/null
79 ) | dd of="$OUTFILE" bs=512 seek=$(($1 / 512)) conv=notrunc 2>/dev/null
80 printf "Done\n"
81
82 i=$((i+1))
83 shift; shift
84 done