blob: 95840fbb3e5cefe8bfb1d2424450e30fc94c41b0 [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
Albert Chaulk42c08cb2013-04-02 14:38:07 -070015MTD="${2:-}"
16CGPT="${CGPT} ${MTD}"
17
Bill Richardson793e1b42010-08-20 07:58:43 -070018# Run tests in a dedicated directory for easy cleanup or debugging.
19DIR="${TEST_DIR}/cgpt_test_dir"
20[ -d "$DIR" ] || mkdir -p "$DIR"
Bill Richardson263ffdf2012-08-24 14:39:21 -070021warning "testing $CGPT in $DIR"
Bill Richardson793e1b42010-08-20 07:58:43 -070022cd "$DIR"
Bill Richardsonf1372d92010-06-11 09:15:55 -070023
Nam T. Nguyen6ee52d92014-10-24 13:20:39 -070024# Test failure on non existing file.
25set +e
26${CGPT} show blah_404_haha
27[ $? != 0 ] || error "CGPT should fail on non existing file."
28set -e
29
Bill Richardsonf1372d92010-06-11 09:15:55 -070030echo "Create an empty file to use as the device..."
31NUM_SECTORS=1000
Bill Richardson793e1b42010-08-20 07:58:43 -070032DEV=fake_dev.bin
Bill Richardson23429d32012-04-30 11:33:13 -070033rm -f ${DEV}
Bill Richardsonf1372d92010-06-11 09:15:55 -070034dd if=/dev/zero of=${DEV} conv=notrunc bs=512 count=${NUM_SECTORS} 2>/dev/null
Bill Richardsonf1372d92010-06-11 09:15:55 -070035
36
37echo "Create a bunch of partitions, using the real GUID types..."
38DATA_START=100
39DATA_SIZE=20
40DATA_LABEL="data stuff"
41DATA_GUID='ebd0a0a2-b9e5-4433-87c0-68b6b72699c7'
42DATA_NUM=1
43
44KERN_START=200
45KERN_SIZE=30
46KERN_LABEL="kernel stuff"
47KERN_GUID='fe3a2a5d-4f32-41a7-b725-accc3285a309'
48KERN_NUM=2
49
50ROOTFS_START=300
51ROOTFS_SIZE=40
52ROOTFS_LABEL="rootfs stuff"
53ROOTFS_GUID='3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec'
54ROOTFS_NUM=3
55
56ESP_START=400
57ESP_SIZE=50
58ESP_LABEL="ESP stuff"
59ESP_GUID='c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
60ESP_NUM=4
61
62FUTURE_START=500
63FUTURE_SIZE=60
64FUTURE_LABEL="future stuff"
65FUTURE_GUID='2e0a753d-9e48-43b0-8337-b15192cb1b5e'
66FUTURE_NUM=5
67
68RANDOM_START=600
69RANDOM_SIZE=70
70RANDOM_LABEL="random stuff"
71RANDOM_GUID='2364a860-bf63-42fb-a83d-9ad3e057fcf5'
72RANDOM_NUM=6
73
Bill Richardson263ffdf2012-08-24 14:39:21 -070074$CGPT create ${DEV}
Bill Richardsonf1372d92010-06-11 09:15:55 -070075
Bill Richardson263ffdf2012-08-24 14:39:21 -070076$CGPT add -b ${DATA_START} -s ${DATA_SIZE} -t ${DATA_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070077 -l "${DATA_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070078$CGPT add -b ${KERN_START} -s ${KERN_SIZE} -t ${KERN_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070079 -l "${KERN_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070080$CGPT add -b ${ROOTFS_START} -s ${ROOTFS_SIZE} -t ${ROOTFS_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070081 -l "${ROOTFS_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070082$CGPT add -b ${ESP_START} -s ${ESP_SIZE} -t ${ESP_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070083 -l "${ESP_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070084$CGPT add -b ${FUTURE_START} -s ${FUTURE_SIZE} -t ${FUTURE_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070085 -l "${FUTURE_LABEL}" ${DEV}
Bill Richardson263ffdf2012-08-24 14:39:21 -070086$CGPT add -b ${RANDOM_START} -s ${RANDOM_SIZE} -t ${RANDOM_GUID} \
Bill Richardsonf1372d92010-06-11 09:15:55 -070087 -l "${RANDOM_LABEL}" ${DEV}
88
89
90echo "Extract the start and size of given partitions..."
91
Bill Richardson263ffdf2012-08-24 14:39:21 -070092X=$($CGPT show -b -i $DATA_NUM ${DEV})
93Y=$($CGPT show -s -i $DATA_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080094[ "$X $Y" = "$DATA_START $DATA_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070095
Bill Richardson263ffdf2012-08-24 14:39:21 -070096X=$($CGPT show -b -i $KERN_NUM ${DEV})
97Y=$($CGPT show -s -i $KERN_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -080098[ "$X $Y" = "$KERN_START $KERN_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -070099
Bill Richardson263ffdf2012-08-24 14:39:21 -0700100X=$($CGPT show -b -i $ROOTFS_NUM ${DEV})
101Y=$($CGPT show -s -i $ROOTFS_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800102[ "$X $Y" = "$ROOTFS_START $ROOTFS_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700103
Bill Richardson263ffdf2012-08-24 14:39:21 -0700104X=$($CGPT show -b -i $ESP_NUM ${DEV})
105Y=$($CGPT show -s -i $ESP_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800106[ "$X $Y" = "$ESP_START $ESP_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700107
Bill Richardson263ffdf2012-08-24 14:39:21 -0700108X=$($CGPT show -b -i $FUTURE_NUM ${DEV})
109Y=$($CGPT show -s -i $FUTURE_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800110[ "$X $Y" = "$FUTURE_START $FUTURE_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700111
Bill Richardson263ffdf2012-08-24 14:39:21 -0700112X=$($CGPT show -b -i $RANDOM_NUM ${DEV})
113Y=$($CGPT show -s -i $RANDOM_NUM ${DEV})
Bill Richardson3430b322010-11-29 14:24:51 -0800114[ "$X $Y" = "$RANDOM_START $RANDOM_SIZE" ] || error
Bill Richardsonf1372d92010-06-11 09:15:55 -0700115
116
Bill Richardsonda77e692012-08-24 17:52:01 -0700117echo "Change the beginning..."
118DATA_START=$((DATA_START + 10))
119$CGPT add -i 1 -b ${DATA_START} ${DEV} || error
120X=$($CGPT show -b -i 1 ${DEV})
121[ "$X" = "$DATA_START" ] || error
122
123echo "Change the size..."
124DATA_SIZE=$((DATA_SIZE + 10))
125$CGPT add -i 1 -s ${DATA_SIZE} ${DEV} || error
126X=$($CGPT show -s -i 1 ${DEV})
127[ "$X" = "$DATA_SIZE" ] || error
128
129echo "Change the type..."
130$CGPT add -i 1 -t reserved ${DEV} || error
131X=$($CGPT show -t -i 1 ${DEV} | tr 'A-Z' 'a-z')
132[ "$X" = "$FUTURE_GUID" ] || error
133# arbitrary value
Albert Chaulk42c08cb2013-04-02 14:38:07 -0700134if [ -z "$MTD" ]; then
135 $CGPT add -i 1 -t 610a563a-a55c-4ae0-ab07-86e5bb9db67f ${DEV} || error
136 X=$($CGPT show -t -i 1 ${DEV})
137 [ "$X" = "610A563A-A55C-4AE0-AB07-86E5BB9DB67F" ] || error
138fi
Bill Richardsonda77e692012-08-24 17:52:01 -0700139$CGPT add -i 1 -t data ${DEV} || error
140X=$($CGPT show -t -i 1 ${DEV} | tr 'A-Z' 'a-z')
141[ "$X" = "$DATA_GUID" ] || error
142
143
Albert Chaulk42c08cb2013-04-02 14:38:07 -0700144if [ -z "$MTD" ]; then
145 echo "Set the boot partition.."
146 $CGPT boot -i ${KERN_NUM} ${DEV} >/dev/null
Bill Richardsonf1372d92010-06-11 09:15:55 -0700147
Albert Chaulk42c08cb2013-04-02 14:38:07 -0700148 echo "Check the PMBR's idea of the boot partition..."
149 X=$($CGPT boot ${DEV})
150 Y=$($CGPT show -u -i $KERN_NUM $DEV)
151 [ "$X" = "$Y" ] || error
152fi
Bill Richardson3430b322010-11-29 14:24:51 -0800153
154echo "Test the cgpt prioritize command..."
155
156# Input: sequence of priorities
157# Output: ${DEV} has kernel partitions with the given priorities
158make_pri() {
159 local idx=0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700160 $CGPT create ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800161 for pri in "$@"; do
162 idx=$((idx+1))
Bill Richardson263ffdf2012-08-24 14:39:21 -0700163 $CGPT add -t kernel -l "kern$idx" -b $((100 + 2 * $idx)) -s 1 -P $pri ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800164 done
165}
166
167# Output: returns string containing priorities of all kernels
168get_pri() {
169 echo $(
Bill Richardson263ffdf2012-08-24 14:39:21 -0700170 for idx in $($CGPT find -t kernel ${DEV} | sed -e s@${DEV}@@); do
171 $CGPT show -i $idx -P ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800172 done
173 )
174}
175
176# Input: list of priorities
177# Operation: expects ${DEV} to contain those kernel priorities
178assert_pri() {
179 local expected="$*"
180 local actual=$(get_pri)
181 [ "$actual" = "$expected" ] || \
182 error 1 "expected priority \"$expected\", actual priority \"$actual\""
183}
184
Bill Richardson3430b322010-11-29 14:24:51 -0800185# no kernels at all. This should do nothing.
Bill Richardson263ffdf2012-08-24 14:39:21 -0700186$CGPT create ${DEV}
187$CGPT add -t rootfs -b 100 -s 1 ${DEV}
188$CGPT prioritize ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800189assert_pri ""
190
191# common install/upgrade sequence
192make_pri 2 0 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700193$CGPT prioritize -i 1 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800194assert_pri 1 0 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700195$CGPT prioritize -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800196assert_pri 1 2 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700197$CGPT prioritize -i 1 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800198assert_pri 2 1 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700199$CGPT prioritize -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800200assert_pri 1 2 0
201
202# lots of kernels, all same starting priority, should go to priority 1
203make_pri 8 8 8 8 8 8 8 8 8 8 8 0 0 8
Bill Richardson263ffdf2012-08-24 14:39:21 -0700204$CGPT prioritize ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800205assert_pri 1 1 1 1 1 1 1 1 1 1 1 0 0 1
206
207# now raise them all up again
Bill Richardson263ffdf2012-08-24 14:39:21 -0700208$CGPT prioritize -P 4 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800209assert_pri 4 4 4 4 4 4 4 4 4 4 4 0 0 4
210
211# set one of them higher, should leave the rest alone
Bill Richardson263ffdf2012-08-24 14:39:21 -0700212$CGPT prioritize -P 5 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800213assert_pri 4 4 5 4 4 4 4 4 4 4 4 0 0 4
214
215# set one of them lower, should bring the rest down
Bill Richardson263ffdf2012-08-24 14:39:21 -0700216$CGPT prioritize -P 3 -i 4 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800217assert_pri 1 1 2 3 1 1 1 1 1 1 1 0 0 1
218
219# raise a group by including the friends of one partition
Bill Richardson263ffdf2012-08-24 14:39:21 -0700220$CGPT prioritize -P 6 -i 1 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800221assert_pri 6 6 4 5 6 6 6 6 6 6 6 0 0 6
222
223# resurrect one, should not affect the others
224make_pri 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700225$CGPT prioritize -i 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800226assert_pri 0 1 0 0 0 0 0 0 0 0 0 0 0 0
227
228# resurrect one and all its friends
229make_pri 0 0 0 0 0 0 0 0 1 2 0 0 0 0
Bill Richardson263ffdf2012-08-24 14:39:21 -0700230$CGPT prioritize -P 5 -i 2 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800231assert_pri 5 5 5 5 5 5 5 5 3 4 5 5 5 5
232
233# no options should maintain the same order
Bill Richardson263ffdf2012-08-24 14:39:21 -0700234$CGPT prioritize ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800235assert_pri 3 3 3 3 3 3 3 3 1 2 3 3 3 3
236
237# squish all the ranks
238make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700239$CGPT prioritize -P 6 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800240assert_pri 1 1 1 1 2 2 3 3 4 4 0 5 6 6
241
242# squish the ranks by not leaving room
243make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700244$CGPT prioritize -P 7 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800245assert_pri 1 1 7 1 2 2 3 3 4 4 0 5 6 6
246
247# squish the ranks while bringing the friends along
248make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700249$CGPT prioritize -P 6 -i 3 -f ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800250assert_pri 1 1 6 6 1 1 2 2 3 3 0 4 5 5
251
252# squish them pretty hard
253make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700254$CGPT prioritize -P 2 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800255assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 2 2
256
257# squish them really really hard (nobody gets reduced to zero, though)
258make_pri 1 1 2 2 3 3 4 4 5 5 0 6 7 7
Bill Richardson263ffdf2012-08-24 14:39:21 -0700259$CGPT prioritize -P 1 -i 3 ${DEV}
Bill Richardson3430b322010-11-29 14:24:51 -0800260assert_pri 1 1 1 1 1 1 1 1 1 1 0 1 1 1
261
Albert Chaulk42c08cb2013-04-02 14:38:07 -0700262if [ -z "$MTD" ]; then # MTD doesn't support this many partitions
263 # squish if we try to go too high
264 make_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
265 $CGPT prioritize -i 3 ${DEV}
266 assert_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
267 $CGPT prioritize -i 5 ${DEV}
268 assert_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
269 # but if I bring friends I don't have to squish
270 $CGPT prioritize -i 1 -f ${DEV}
271 assert_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
272fi
Bill Richardsonf1372d92010-06-11 09:15:55 -0700273
Bill Richardson23429d32012-04-30 11:33:13 -0700274
275# Now make sure that we don't need write access if we're just looking.
276echo "Test read vs read-write access..."
277chmod 0444 ${DEV}
278
279# These should fail
Bill Richardson263ffdf2012-08-24 14:39:21 -0700280$CGPT create -z ${DEV} 2>/dev/null && error
281$CGPT add -i 2 -P 3 ${DEV} 2>/dev/null && error
282$CGPT repair ${DEV} 2>/dev/null && error
283$CGPT prioritize -i 3 ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700284
285# Most 'boot' usage should fail too.
Bill Richardson263ffdf2012-08-24 14:39:21 -0700286$CGPT boot -p ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700287dd if=/dev/zero of=fake_mbr.bin bs=100 count=1 2>/dev/null
Bill Richardson263ffdf2012-08-24 14:39:21 -0700288$CGPT boot -b fake_mbr.bin ${DEV} 2>/dev/null && error
289$CGPT boot -i 2 ${DEV} 2>/dev/null && error
Bill Richardson23429d32012-04-30 11:33:13 -0700290
291# These should pass
Albert Chaulk42c08cb2013-04-02 14:38:07 -0700292if [ -z "$MTD" ]; then
293 $CGPT boot ${DEV} >/dev/null
294fi
Bill Richardson263ffdf2012-08-24 14:39:21 -0700295$CGPT show ${DEV} >/dev/null
296$CGPT find -t kernel ${DEV} >/dev/null
Bill Richardson23429d32012-04-30 11:33:13 -0700297
Bill Richardsonf1372d92010-06-11 09:15:55 -0700298echo "Done."
299
300happy "All tests passed."