Root/qiboot/6410-partition-sd.sh

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

Archive Download this file



interactive