blob: 5176017a221ff9673a2b352cbdd7ba7e44818142 [file] [log] [blame]
Bill Richardsonf1372d92010-06-11 09:15:55 -07001#!/bin/bash -eu
2
3# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
Bill Richardson263ffdf2012-08-24 14:39:21 -07007# Run tests for cgpt utility.
Bill Richardsonf1372d92010-06-11 09:15:55 -07008
9# Load common constants and variables.
10. "$(dirname "$0")/common.sh"
11
Bill Richardson263ffdf2012-08-24 14:39:21 -070012CGPT=$(readlink -f "$1")
13[ -x "$CGPT" ] || error "Can't execute $CGPT"
Bill Richardson793e1b42010-08-20 07:58:43 -070014
Nam T. Nguyenab899592014-11-13 19:30:46 -080015MTD="${@:2}"
Albert Chaulk42c08cb2013-04-02 14:38:07 -070016
Bill Richardson793e1b42010-08-20 07:58:43 -070017# Run tests in a dedicated directory for easy cleanup or debugging.
18DIR="${TEST_DIR}/cgpt_test_dir"
19[ -d "$DIR" ] || mkdir -p "$DIR"
Bill Richardson263ffdf2012-08-24 14:39:21 -070020warning "testing $CGPT in $DIR"
Bill Richardson793e1b42010-08-20 07:58:43 -070021cd "$DIR"
Bill Richardsonf1372d92010-06-11 09:15:55 -070022
Nam T. Nguyenab899592014-11-13 19:30:46 -080023assert_fail() {
24 set +e
25 "$@" 2>/dev/null
26 if [ $? == 0 ]; then
27 error "$*" " should have failed but did not"
28 fi
29 set -e
30}
31
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070032# Test failure on non existing file.
Nam T. Nguyenab899592014-11-13 19:30:46 -080033assert_fail ${CGPT} show $MTD blah_404_haha
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070034
Bill Richardsonf1372d92010-06-11 09:15:55 -070035echo "Create an empty file to use as the device..."
36NUM_SECTORS=1000
Bill Richardson793e1b42010-08-20 07:58:43 -070037DEV=fake_dev.bin
Bill Richardson23429d32012-04-30 11:33:13 -070038rm -f ${DEV}
Bill Richardsonf1372d92010-06-11 09:15:55 -070039dd if=/dev/zero of=${DEV} conv=notrunc bs=512 count=${NUM_SECTORS} 2>/dev/null
Bill Richardsonf1372d92010-06-11 09:15:55 -070040
41
42echo "Create a bunch of partitions, using the real GUID types..."
43DATA_START=100
44DATA_SIZE=20
45DATA_LABEL="data stuff"
46DATA_GUID='ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
47DATA_NUM=1
48
49KERN_START=200
50KERN_SIZE=30
51KERN_LABEL="kernel stuff"
52KERN_GUID='fe3a2a5d-4f32-41a7-b725-accc3285a309'
53KERN_NUM=2
54
55ROOTFS_START=300
56ROOTFS_SIZE=40
57ROOTFS_LABEL="rootfs stuff"
58ROOTFS_GUID='3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
59ROOTFS_NUM=3
60
61ESP_START=400
62ESP_SIZE=50
63ESP_LABEL="ESP stuff"
64ESP_GUID='c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
65ESP_NUM=4
66
67FUTURE_START=500
68FUTURE_SIZE=60
69FUTURE_LABEL="future stuff"
70FUTURE_GUID='2e0a753d-9e48-43b0-8337-b15192cb1b5e'
71FUTURE_NUM=5
72
73RANDOM_START=600
74RANDOM_SIZE=70
75RANDOM_LABEL="random stuff"
76RANDOM_GUID='2364a860-bf63-42fb-a83d-9ad3e057fcf5'
77RANDOM_NUM=6
78
Nam T. Nguyenab899592014-11-13 19:30:46 -080079$CGPT create $MTD ${DEV}
Bill Richardsonf1372d92010-06-11 09:15:55 -070080
Nam T. Nguyenab899592014-11-13 19:30:46 -080081$CGPT add $MTD -b ${DATA_START} -s ${DATA_SIZE} -t ${DATA_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070082 -l "${DATA_LABEL}" ${DEV}
Nam T. Nguyenab899592014-11-13 19:30:46 -080083$CGPT add $MTD -b ${KERN_START} -s ${KERN_SIZE} -t ${KERN_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070084 -l "${KERN_LABEL}" ${DEV}
Nam T. Nguyenab899592014-11-13 19:30:46 -080085$CGPT add $MTD -b ${ROOTFS_START} -s ${ROOTFS_SIZE} -t ${ROOTFS_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070086 -l "${ROOTFS_LABEL}" ${DEV}
Nam T. Nguyenab899592014-11-13 19:30:46 -080087$CGPT add $MTD -b ${ESP_START} -s ${ESP_SIZE} -t ${ESP_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070088 -l "${ESP_LABEL}" ${DEV}
Nam T. Nguyenab899592014-11-13 19:30:46 -080089$CGPT add $MTD -b ${FUTURE_START} -s ${FUTURE_SIZE} -t ${FUTURE_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070090 -l "${FUTURE_LABEL}" ${DEV}
Nam T. Nguyenab899592014-11-13 19:30:46 -080091$CGPT add $MTD -b ${RANDOM_START} -s ${RANDOM_SIZE} -t ${RANDOM_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070092 -l "${RANDOM_LABEL}" ${DEV}
93
94
95echo "Extract the start and size of given partitions..."
96
Nam T. Nguyenab899592014-11-13 19:30:46 -080097X=$($CGPT show $MTD -b -i $DATA_NUM ${DEV})
98Y=$($CGPT show $MTD -s -i $DATA_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080099[ "$X $Y" = "$DATA_START $DATA_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700100
Nam T. Nguyenab899592014-11-13 19:30:46 -0800101X=$($CGPT show $MTD -b -i $KERN_NUM ${DEV})
102Y=$($CGPT show $MTD -s -i $KERN_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800103[ "$X $Y" = "$KERN_START $KERN_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700104
Nam T. Nguyenab899592014-11-13 19:30:46 -0800105X=$($CGPT show $MTD -b -i $ROOTFS_NUM ${DEV})
106Y=$($CGPT show $MTD -s -i $ROOTFS_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800107[ "$X $Y" = "$ROOTFS_START $ROOTFS_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700108
Nam T. Nguyenab899592014-11-13 19:30:46 -0800109X=$($CGPT show $MTD -b -i $ESP_NUM ${DEV})
110Y=$($CGPT show $MTD -s -i $ESP_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800111[ "$X $Y" = "$ESP_START $ESP_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700112
Nam T. Nguyenab899592014-11-13 19:30:46 -0800113X=$($CGPT show $MTD -b -i $FUTURE_NUM ${DEV})
114Y=$($CGPT show $MTD -s -i $FUTURE_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800115[ "$X $Y" = "$FUTURE_START $FUTURE_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700116
Nam T. Nguyenab899592014-11-13 19:30:46 -0800117X=$($CGPT show $MTD -b -i $RANDOM_NUM ${DEV})
118Y=$($CGPT show $MTD -s -i $RANDOM_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800119[ "$X $Y" = "$RANDOM_START $RANDOM_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700120
121
Bill Richardsonda77e692012-08-24 17:52:01 -0700122echo "Change the beginning..."
123DATA_START=$((DATA_START + 10))
Nam T. Nguyenab899592014-11-13 19:30:46 -0800124$CGPT add $MTD -i 1 -b ${DATA_START} ${DEV} || error
125X=$($CGPT show $MTD -b -i 1 ${DEV})
Bill Richardsonda77e692012-08-24 17:52:01 -0700126[ "$X" = "$DATA_START" ] || error
127
128echo "Change the size..."
129DATA_SIZE=$((DATA_SIZE + 10))
Nam T. Nguyenab899592014-11-13 19:30:46 -0800130$CGPT add $MTD -i 1 -s ${DATA_SIZE} ${DEV} || error
131X=$($CGPT show $MTD -s -i 1 ${DEV})
Bill Richardsonda77e692012-08-24 17:52:01 -0700132[ "$X" = "$DATA_SIZE" ] || error
133
134echo "Change the type..."
Nam T. Nguyenab899592014-11-13 19:30:46 -0800135$CGPT add $MTD -i 1 -t reserved ${DEV} || error
136X=$($CGPT show $MTD -t -i 1 ${DEV} | tr 'A-Z' 'a-z')
Bill Richardsonda77e692012-08-24 17:52:01 -0700137[ "$X" = "$FUTURE_GUID" ] || error
138# arbitrary value
Nam T. Nguyenab899592014-11-13 19:30:46 -0800139$CGPT add $MTD -i 1 -t 610a563a-a55c-4ae0-ab07-86e5bb9db67f ${DEV} || error
140X=$($CGPT show $MTD -t -i 1 ${DEV})
141[ "$X" = "610A563A-A55C-4AE0-AB07-86E5BB9DB67F" ] || error
142
143$CGPT add $MTD -i 1 -t data ${DEV} || error
144X=$($CGPT show $MTD -t -i 1 ${DEV} | tr 'A-Z' 'a-z')
Bill Richardsonda77e692012-08-24 17:52:01 -0700145[ "$X" = "$DATA_GUID" ] || error
146
147
Nam T. Nguyenab899592014-11-13 19:30:46 -0800148echo "Set the boot partition.."
149$CGPT boot $MTD -i ${KERN_NUM} ${DEV} >/dev/null
Bill Richardsonf1372d92010-06-11 09:15:55 -0700150
Nam T. Nguyenab899592014-11-13 19:30:46 -0800151echo "Check the PMBR's idea of the boot partition..."
152X=$($CGPT boot $MTD ${DEV})
153Y=$($CGPT show $MTD -u -i $KERN_NUM $DEV)
154[ "$X" = "$Y" ] || error
Bill Richardson3430b322010-11-29 14:24:51 -0800155
156echo "Test the cgpt prioritize command..."
157
158# Input: sequence of priorities
159# Output: ${DEV} has kernel partitions with the given priorities
160make_pri() {
161 local idx=0
Nam T. Nguyenab899592014-11-13 19:30:46 -0800162 $CGPT create $MTD ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800163 for pri in "$@"; do
164 idx=$((idx+1))
Nam T. Nguyenab899592014-11-13 19:30:46 -0800165 $CGPT add $MTD -t kernel -l "kern$idx" -b $((100 + 2 * $idx)) -s 1 -P $pri ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800166 done
167}
168
169# Output: returns string containing priorities of all kernels
170get_pri() {
171 echo $(
Nam T. Nguyenab899592014-11-13 19:30:46 -0800172 for idx in $($CGPT find $MTD -t kernel ${DEV} | sed -e s@${DEV}@@); do
173 $CGPT show $MTD -i $idx -P ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800174 done
175 )
176}
177
178# Input: list of priorities
179# Operation: expects ${DEV} to contain those kernel priorities
180assert_pri() {
181 local expected="$*"
182 local actual=$(get_pri)
183 [ "$actual" = "$expected" ] || \
184 error 1 "expected priority \"$expected\", actual priority \"$actual\""
185}
186
Bill Richardson3430b322010-11-29 14:24:51 -0800187# no kernels at all. This should do nothing.
Nam T. Nguyenab899592014-11-13 19:30:46 -0800188$CGPT create $MTD ${DEV}
189$CGPT add $MTD -t rootfs -b 100 -s 1 ${DEV}
190$CGPT prioritize $MTD ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800191assert_pri ""
192
193# common install/upgrade sequence
194make_pri 2 0 0
Nam T. Nguyenab899592014-11-13 19:30:46 -0800195$CGPT prioritize $MTD -i 1 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800196assert_pri 1 0 0
Nam T. Nguyenab899592014-11-13 19:30:46 -0800197$CGPT prioritize $MTD -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800198assert_pri 1 2 0
Nam T. Nguyenab899592014-11-13 19:30:46 -0800199$CGPT prioritize $MTD -i 1 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800200assert_pri 2 1 0
Nam T. Nguyenab899592014-11-13 19:30:46 -0800201$CGPT prioritize $MTD -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800202assert_pri 1 2 0
Bill Richardson3430b322010-11-29 14:24:51 -0800203# lots of kernels, all same starting priority, should go to priority 1
204make_pri 8 8 8 8 8 8 8 8 8 8 8 0 0 8
Nam T. Nguyenab899592014-11-13 19:30:46 -0800205$CGPT prioritize $MTD ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800206assert_pri 1 1 1 1 1 1 1 1 1 1 1 0 0 1
207
208# now raise them all up again
Nam T. Nguyenab899592014-11-13 19:30:46 -0800209$CGPT prioritize $MTD -P 4 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800210assert_pri 4 4 4 4 4 4 4 4 4 4 4 0 0 4
211
212# set one of them higher, should leave the rest alone
Nam T. Nguyenab899592014-11-13 19:30:46 -0800213$CGPT prioritize $MTD -P 5 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800214assert_pri 4 4 5 4 4 4 4 4 4 4 4 0 0 4
215
216# set one of them lower, should bring the rest down
Nam T. Nguyenab899592014-11-13 19:30:46 -0800217$CGPT prioritize $MTD -P 3 -i 4 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800218assert_pri 1 1 2 3 1 1 1 1 1 1 1 0 0 1
219
220# raise a group by including the friends of one partition
Nam T. Nguyenab899592014-11-13 19:30:46 -0800221$CGPT prioritize $MTD -P 6 -i 1 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800222assert_pri 6 6 4 5 6 6 6 6 6 6 6 0 0 6
223
224# resurrect one, should not affect the others
225make_pri 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Nam T. Nguyenab899592014-11-13 19:30:46 -0800226$CGPT prioritize $MTD -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800227assert_pri 0 1 0 0 0 0 0 0 0 0 0 0 0 0
228
229# resurrect one and all its friends
230make_pri 0 0 0 0 0 0 0 0 1 2 0 0 0 0
Nam T. Nguyenab899592014-11-13 19:30:46 -0800231$CGPT prioritize $MTD -P 5 -i 2 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800232assert_pri 5 5 5 5 5 5 5 5 3 4 5 5 5 5
233
234# no options should maintain the same order
Nam T. Nguyenab899592014-11-13 19:30:46 -0800235$CGPT prioritize $MTD ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800236assert_pri 3 3 3 3 3 3 3 3 1 2 3 3 3 3
237
238# squish all the ranks
239make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Nam T. Nguyenab899592014-11-13 19:30:46 -0800240$CGPT prioritize $MTD -P 6 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800241assert_pri 1 1 1 1 2 2 3 3 4 4 0 5 6 6
242
243# squish the ranks by not leaving room
244make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Nam T. Nguyenab899592014-11-13 19:30:46 -0800245$CGPT prioritize $MTD -P 7 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800246assert_pri 1 1 7 1 2 2 3 3 4 4 0 5 6 6
247
248# squish the ranks while bringing the friends along
249make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Nam T. Nguyenab899592014-11-13 19:30:46 -0800250$CGPT prioritize $MTD -P 6 -i 3 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800251assert_pri 1 1 6 6 1 1 2 2 3 3 0 4 5 5
252
253# squish them pretty hard
254make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Nam T. Nguyenab899592014-11-13 19:30:46 -0800255$CGPT prioritize $MTD -P 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800256assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 2 2
257
258# squish them really really hard (nobody gets reduced to zero, though)
259make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Nam T. Nguyenab899592014-11-13 19:30:46 -0800260$CGPT prioritize $MTD -P 1 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800261assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 1 1
262
Nam T. Nguyenab899592014-11-13 19:30:46 -0800263make_pri 15 15 14 14 13 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0
264$CGPT prioritize $MTD -i 3 ${DEV}
265assert_pri 14 14 15 13 12 12 11 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 0
266$CGPT prioritize $MTD -i 5 ${DEV}
267assert_pri 13 13 14 12 15 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
268# but if I bring friends I don't have to squish
269$CGPT prioritize $MTD -i 1 -f ${DEV}
270assert_pri 15 15 13 12 14 11 10 10 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1 1 1 1 1 0
Bill Richardson23429d32012-04-30 11:33:13 -0700271
272# Now make sure that we don't need write access if we're just looking.
273echo "Test read vs read-write access..."
274chmod 0444 ${DEV}
275
276# These should fail
Nam T. Nguyenab899592014-11-13 19:30:46 -0800277$CGPT create $MTD -z ${DEV} 2>/dev/null && error
278$CGPT add $MTD -i 2 -P 3 ${DEV} 2>/dev/null && error
279$CGPT repair $MTD ${DEV} 2>/dev/null && error
280$CGPT prioritize $MTD -i 3 ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700281
282# Most 'boot' usage should fail too.
Nam T. Nguyenab899592014-11-13 19:30:46 -0800283$CGPT boot $MTD -p ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700284dd if=/dev/zero of=fake_mbr.bin bs=100 count=1 2>/dev/null
Nam T. Nguyenab899592014-11-13 19:30:46 -0800285$CGPT boot $MTD -b fake_mbr.bin ${DEV} 2>/dev/null && error
286$CGPT boot $MTD -i 2 ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700287
Nam T. Nguyenab899592014-11-13 19:30:46 -0800288$CGPT boot $MTD ${DEV} >/dev/null
289$CGPT show $MTD ${DEV} >/dev/null
290$CGPT find $MTD -t kernel ${DEV} >/dev/null
291
292# Enable write access again to test boundary in off device storage
293chmod 600 ${DEV}
Nam T. Nguyend53b6682014-12-15 11:03:36 -0800294# GPT too small
295dd if=/dev/zero of=${DEV} bs=5632 count=1
296assert_fail $CGPT create -D 1024 ${DEV}
297# GPT is just right for 16 entries (512 + 512 + 16 * 128) * 2 = 6144
298dd if=/dev/zero of=${DEV} bs=6144 count=1
299$CGPT create -D 1024 ${DEV}
Nam T. Nguyen32004012014-12-12 09:38:35 -0800300# Create a small 8K file to simulate Flash NOR section
301dd if=/dev/zero of=${DEV} bs=8K count=1
Nam T. Nguyenab899592014-11-13 19:30:46 -0800302# Drive size is not multiple of 512
303assert_fail $CGPT create -D 511 ${DEV}
304assert_fail $CGPT create -D 513 ${DEV}
305MTD="-D 1024"
306# Create a GPT table for a device of 1024 bytes (2 sectors)
307$CGPT create $MTD ${DEV}
Nam T. Nguyen32004012014-12-12 09:38:35 -0800308# Make sure number of entries is reasonable for 8KiB GPT
309X=$($CGPT show -D 1024 -d ${DEV} | grep -c "Number of entries: 24")
310[ "$X" = "2" ] || error
Nam T. Nguyenab899592014-11-13 19:30:46 -0800311# This fails because header verification is off due to different drive size
312assert_fail $CGPT show ${DEV}
313# But this passes because we pass in correct drive size
314$CGPT show $MTD ${DEV}
315# This fails because beginning sector is over the size of the device
316assert_fail $CGPT add $MTD -b 2 -s 1 -t data ${DEV}
317# This fails because partition size is over the size of the device
318assert_fail $CGPT add $MTD -b 0 -s 3 -t data ${DEV}
Bill Richardson23429d32012-04-30 11:33:13 -0700319
Nam T. Nguyen32004012014-12-12 09:38:35 -0800320
Bill Richardsonf1372d92010-06-11 09:15:55 -0700321echo "Done."
322
323happy "All tests passed."