forked from Dopi/JetQi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-6410-partition-sd.sh
executable file
·284 lines (252 loc) · 8.05 KB
/
install-6410-partition-sd.sh
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/sh
# 6410 SD Boot formatter
# (C) 2008 Openmoko, Inc
# Author: Andy Green <[email protected]>
# LAYOUT (if partition parameter is not specified)
# Partition table, then
# VFAT takes up remaining space here
# then...
#
EXT3_ROOTFS_SECTORS=$(( 256 * 1024 * 2 ))
EXT3_BACKUP_FS_SECTORS=$(( 8 * 1024 * 2 ))
QI_ALLOCATION=$(( 256 * 2 ))
#
# lastly fixed stuff: 8KByte initial boot, sig, padding
#
# ----------------------
echo "s3c6410 bootable SD partitioning utility"
echo "(C) Openmoko, Inc Andy Green <[email protected]>"
echo
# these are fixed in iROM
QI_INITIAL=$(( 8 * 2 ))
SIG=1
# display usage message and exit
# any arguments are displayed as an error message
USAGE()
{
echo
[ -z "$1" ] || echo ERROR: $*
echo
echo 'This formats a SD card for usage on SD Card boot'
echo ' on 6410 based systems'
echo
echo Usage: $(basename "$0") '<device> <card> <bootloader> <partition>'
echo ' device = disk device name for SD Card, e.g. sde /dev/sdf'
echo ' card = sd | sdhc'
echo ' bootloader = /path/to/qi-binary'
echo ' partition = vfat | NN | NN,NN | NN,NN,NN | NN,NN,NN,NN | no'
echo ' * vfat -> main-vfat[rest] + rootfs[256M] + backupfs[8M]'
echo ' NN -> rootfs1[NN%] + .. + rootfs4[NN%]'
echo ' NN=0 -> will skip the partition'
echo ' no -> leave partitions alone'
echo
echo 'Note: * => default action if no parameter specified'
echo ' sum(NN) must be in [1..100]'
echo
echo 'e.g. '$(basename "$0")' sdb sdhc images/qi 0,30,0,45'
echo ' will format an SDHC with partition 2 receiving 20% and partition 4'
echo ' receiving 45% of the disk capacity and the remaining 35% will be'
echo ' unused.'
echo ' Capacity is calculated after subtracting the space reserved for Qi.'
echo ' Partitions 1 and 3 will not be used.'
exit 1
}
[ -z "$1" -o -z "$2" -o -z "$3" ] && USAGE 'Missing arguments'
dev="$1"
card="$2"
qi="$3"
partition="$4"
case "${card}" in
[sS][dD][hH][cC])
PADDING=1025
;;
[sS][dD])
PADDING=1
;;
*)
USAGE "${card} is an unknown card type"
esac
# the amount of space that must remain unused at the end of the disk
REAR_SECTORS=$(( $QI_ALLOCATION + $QI_INITIAL + $SIG + $PADDING ))
# validate parameters
[ -b "${dev}" ] || dev="/dev/${dev}"
[ -b "${dev}" ] || USAGE "${dev} is not a valid block device"
[ X"${dev}" = X"${dev%%[0-9]}" ] || USAGE "${dev} is a partition, please use device: perhaps ${dev%%[0-9]}"
echo "Checking for mounted partitions..."
grep "${dev}" /proc/mounts && USAGE "partitions on ${dev} are mounted, please unmount them"
[ -e "${qi}" ] || USAGE "bootloader file: ${qi} does not exist"
# get size of device
bytes=$(echo p | fdisk "${dev}" 2>&1 | sed '/^Disk.*, \([0-9]*\) bytes/s//\1/p;d')
SECTORS=$(($bytes / 512))
[ -z "$SECTORS" ] && USAGE "could not find size for ${dev}"
[ "$SECTORS" -le 0 ] && USAGE "invalid size: '${SECTORS}' for ${dev}"
echo "${dev} is $SECTORS 512-byte blocks"
# Partition and format a disk (or SD card)
# Parameters to format function are:
#
# device -> device to partition e.g. /dev/sdX
#
# Partition 1 parameters:
#
# label -> file system volume label e.g. rootfs
# sizeMB -> size of the partition in MB e.g. 256
# fstype -> filesystem type e.g. ext2, ext3, vfat (look at /sbin/mkfs.* for others)
#
# Notes: 1. Repeat "label, sizeMB, fstype" for partitions 2..4
# 2. Partitions 2..4 are optional
# 3. Do not repeat device parameter
# 4. To skip a partition use: 'null 0 none' for that partition
FORMAT()
{
local device label sizeMB fstype p partition flag skip
device="$1"; shift
(
p=0
flag=0
echo o
while [ $# -gt 0 ]
do
label="$1"; shift
sizeMB="$1"; shift
fstype="$1"; shift
p=$((${p} + 1))
skip=NO
[ ${sizeMB} -le 0 ] && skip=YES
case "${label}" in
[nN][uU][lL][lL])
skip=YES
;;
*)
;;
esac
case "${skip}" in
[yY][eE][sS]|[yY])
;;
*)
echo n
echo p
echo ${p}
echo
echo +${sizeMB}M
case "${fstype}" in
[vV][fF][aA][tT]|[mM][sS][dD][oO][sS])
echo t
# fdisk is "helpful" & will auto select partition if there is only one
# so do not output partition number if this is the first partition
[ "${flag}" -eq 1 ] && echo ${p}
echo 0b
;;
*)
;;
esac
flag=1
;;
esac
done
echo p
echo w
echo q
) | fdisk "${device}"
p=0
while [ $# -gt 0 ]
do
label="$1"; shift
sizeMB="$1"; shift
fstype="$1"; shift
p=$((${p} + 1))
partition="${dev}${p}"
skip=NO
[ ${sizeMB} -eq 0 ] && skip=YES
case "${label}" in
[nN][uU][lL][lL])
skip=YES
;;
esac
case "${skip}" in
[yY][eE][sS]|[yY])
echo "Skipping: ${partition}"
;;
*)
echo "Formatting: ${partition} -> ${fstype}[${sizeMB}MB]"
case "${fstype}" in
[vV][fF][aA][tT]|[mM][sS][dD][oO][sS])
mkfs.${fstype} -n "${label}" "${partition}"
;;
*)
mkfs.${fstype} -L "${label}" "${partition}"
;;
esac
;;
esac
done
}
# format the disk
case "${partition}" in
# this case also hadles the default case (i.e. empty string: "")
[vV][fF][aA][tT]|"")
EXT3_TOTAL_SECTORS=$(( $EXT3_ROOTFS_SECTORS + $EXT3_BACKUP_FS_SECTORS ))
FAT_SECTORS=$(( $SECTORS - $EXT3_TOTAL_SECTORS - $REAR_SECTORS ))
FAT_MB=$(( $FAT_SECTORS / 2048 ))
EXT3_ROOTFS_MB=$(( ${EXT3_ROOTFS_SECTORS} / 2048 ))
EXT3_BACKUP_FS_MB=$(( ${EXT3_BACKUP_FS_SECTORS} / 2048 ))
echo Creating VFAT partition of ${FAT_MB} MB
echo Creating Linux partition of ${EXT3_ROOTFS_MB} MB
echo Creating backup Linux partition of ${EXT3_BACKUP_FS_MB} MB
FORMAT "${dev}" \
main-vfat "${FAT_MB}" vfat \
rootfs "${EXT3_ROOTFS_MB}" ext3 \
backupfs "${EXT3_BACKUP_FS_MB}" ext3
;;
# decode partition or partition list
*,*|100|[1-9][0-9]|[1-9])
arg="${partition},"
for v in 1 2 3 4
do
eval n${v}="\${arg%%,*}"
eval n${v}="\${n${v}:-0}"
arg="${arg#*,},"
done
total=$(( ${n1} + ${n2} + ${n3} + ${n4} ))
echo Percentage of disk partitioned = ${total}%
[ ${total} -gt 100 -o ${total} -lt 1 ] && USAGE partition: "'${partition}' => ${total}% outside [1..100]"
EXT3_TOTAL_SECTORS=$(( $SECTORS - $REAR_SECTORS ))
EXT3_ROOTFS1_SECTORS=$(( $EXT3_TOTAL_SECTORS * $n1 / 100 ))
EXT3_ROOTFS2_SECTORS=$(( $EXT3_TOTAL_SECTORS * $n2 / 100 ))
EXT3_ROOTFS3_SECTORS=$(( $EXT3_TOTAL_SECTORS * $n3 / 100 ))
EXT3_ROOTFS4_SECTORS=$(( $EXT3_TOTAL_SECTORS * $n4 / 100 ))
EXT3_ROOTFS1_MB=$(( ${EXT3_ROOTFS1_SECTORS} / 2048 ))
EXT3_ROOTFS2_MB=$(( ${EXT3_ROOTFS2_SECTORS} / 2048 ))
EXT3_ROOTFS3_MB=$(( ${EXT3_ROOTFS3_SECTORS} / 2048 ))
EXT3_ROOTFS4_MB=$(( ${EXT3_ROOTFS4_SECTORS} / 2048 ))
echo Creating Linux partition 1 of ${EXT3_ROOTFS1_MB} MB
echo Creating Linux partition 2 of ${EXT3_ROOTFS2_MB} MB
echo Creating Linux partition 3 of ${EXT3_ROOTFS3_MB} MB
echo Creating Linux partition 4 of ${EXT3_ROOTFS4_MB} MB
FORMAT "${dev}" \
rootfs1 "${EXT3_ROOTFS1_MB}" ext3 \
rootfs2 "${EXT3_ROOTFS2_MB}" ext3 \
rootfs3 "${EXT3_ROOTFS3_MB}" ext3 \
rootfs4 "${EXT3_ROOTFS4_MB}" ext3
;;
[Nn]*)
# do not format
;;
*)
USAGE "'${partition}' is an unknown partition config"
;;
esac
# copy the full bootloader image to the right place after the
# partitioned area
echo
echo Installing Qi bootloader from: ${qi}
dd if="${qi}" of="${dev}" bs=512 count=512 \
seek=$(( $SECTORS - $REAR_SECTORS ))
dd if="${qi}" of="${dev}" bs=512 \
seek=$(( $SECTORS - $REAR_SECTORS + $QI_ALLOCATION )) \
count=$QI_INITIAL
dd if=/dev/zero of="${dev}" bs=512 \
seek=$(( $SECTORS - $REAR_SECTORS + $QI_ALLOCATION + $QI_INITIAL )) \
count=$(( $SIG + $PADDING ))
# done
echo
echo "**** completed"