mvebu: add sdcard image creation script
[openwrt/staging/mkresin.git] / target / linux / mvebu / image / gen_mvebu_sdcard_img.sh
1 #!/usr/bin/env bash
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 <sectors> <outfile> <bootloader> [<type_partitionN> <sectors_partitionN> <img_partitionN>]?"
22 }
23
24 # always require first 3 arguments
25 # then in pairs up to 8 more for a total of up to 4 partitions
26 if [ $# -lt 3 ] || [ $# -gt 15 ] || [ $(($#%3)) -ne 0 ]; then
27 usage
28 exit 1
29 fi
30
31 set -e
32
33 # parameters
34 IMGSIZE=$1
35 OUTFILE="$2"
36 BOOTLOADER="$3"
37
38 # calculate number of partitions from argument list
39 NUMPARTS=$#
40 ((NUMPARTS=(NUMPARTS-3)/3))
41
42 # find required applications
43 FDISK=$(env PATH="/usr/local/sbin:/usr/sbin:/sbin:$PATH" which fdisk)
44
45 # generate image file
46 printf "Creating $OUTFILE from /dev/zero: "
47 dd if=/dev/zero of="$OUTFILE" bs=512 count=1 >/dev/null
48 printf "Done\n"
49
50 # generate fdisk argument list
51 printf "Generating fdisk argument list: "
52 ARGSFILE=$(mktemp)
53
54 # empty partition table
55 printf "o\n" >> $ARGSFILE
56
57 # actual partitions
58 offset=2048
59 for i in $(seq 1 1 $NUMPARTS); do
60 ((n=3+3*i-2)); type=$(eval echo \${$n})
61 ((n=3+3*i-1)); size=$(eval echo \${$n})
62 ((end=offset+size-1))
63
64 printf "n\np\n%i\n\n%i\n" $i $end >> $ARGSFILE
65
66 # special case on first aprtition: fdisk wont ask which one
67 if [ $i -eq 1 ]; then
68 printf "t\n%s\n" $type >> $ARGSFILE
69 else
70 printf "t\n%i\n%s\n" $i $type >> $ARGSFILE
71 fi
72
73 # add this partitions size to offset for next partition
74 ((offset=end+1))
75 done
76
77 # write and exit
78 printf "w\n" >> $ARGSFILE
79
80 printf "Done\n"
81
82 # create real partition table using fdisk
83 printf "Creating partition table: "
84 cat $ARGSFILE | $FDISK "$OUTFILE" >/dev/null
85 printf "Done\n"
86
87 # remove temporary files
88 printf "Cleaning up: "
89 rm -f $ARGSFILE
90 printf "Done\n"
91
92 # install bootloader
93 printf "Writing bootloader: "
94 dd of="$OUTFILE" if="$BOOTLOADER" bs=512 seek=1 conv=notrunc 2>/dev/null
95 printf "Done\n"
96
97 # write partition data
98
99 # offset of first partition is 2048
100 offset=2048
101 for i in $(seq 1 1 $NUMPARTS); do
102 ((n=3+3*i-1)); size=$(eval echo \${$n})
103 ((n=3+3*i)); img="$(eval echo \${$n})"
104
105 printf "Writing %s to partition %i: " "$img" $i
106 dd if="$img" of="$OUTFILE" bs=512 seek=$offset conv=notrunc 2>/dev/null
107 printf "Done\n"
108
109 # add this partitions size to offset for next partition
110 ((offset=offset+size))
111 done