aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNam T. Nguyen <namnguyen@chromium.org>2014-10-24 13:20:39 -0700
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2014-11-13 18:29:09 +0000
commit6ee52d9a929d00e871e7316240b54f381146fbc6 (patch)
treeca403414469d9364d144a67e63f8f439874802e3 /tests
parent43e0a9ed6c0b332631442fcf581e7456d62e4532 (diff)
downloadvboot_reference-6ee52d9a929d00e871e7316240b54f381146fbc6.tar.gz
vboot: cgpt: Support writing GPT structs to NOR flash
This CL allows the GPT headers and partition entry arrays to be stored in a NOR flash device. Instead of treating both the NOR and NAND devices as one (in a sandwich way), this CL writes and reads the GPT structs independently of the actual device that houses the partitions. Therefore, the first usable LBA of the partitions will be at 0, and the last usable LBA is at the end of the NAND. +------------------------+ | NOR houses GPT structs | +------------------------+ | 0 | Index into v v +------------------------+ | NAND houses partitions | +------------------------+ Note that the "my_lba", "alternate_lba", "entries_lba" in the GPT headers are no longer meaningful. Consumers of cgptlib will have to set "stored_on_device" to either GPT_STORED_ON_DEVICE or GPT_STORED_OFF_DEVICE, and "gpt_drive_sectors" to the number of 512-byte sectors available to store GPT structs. The NOR read and write operations are done by "flashrom". BUG=chromium:425677 BRANCH=none TEST=unittest TEST=build with DEBUG, cgpt create/add/show on a stumpy-moblab Change-Id: I083b3c94da3b0bb3da1a7b10c6969774080a2afd Reviewed-on: https://chromium-review.googlesource.com/226800 Reviewed-by: Nam Nguyen <namnguyen@chromium.org> Commit-Queue: Nam Nguyen <namnguyen@chromium.org> Tested-by: Nam Nguyen <namnguyen@chromium.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/cgptlib_test.c139
-rwxr-xr-xtests/run_cgpt_tests.sh6
-rw-r--r--tests/vboot_kernel_tests.c34
3 files changed, 124 insertions, 55 deletions
diff --git a/tests/cgptlib_test.c b/tests/cgptlib_test.c
index 363ecc8a..12b366ad 100644
--- a/tests/cgptlib_test.c
+++ b/tests/cgptlib_test.c
@@ -164,7 +164,7 @@ static void BuildTestGptData(GptData *gpt)
Guid chromeos_rootfs = GPT_ENT_TYPE_CHROMEOS_ROOTFS;
gpt->sector_bytes = DEFAULT_SECTOR_SIZE;
- gpt->drive_sectors = DEFAULT_DRIVE_SECTORS;
+ gpt->drive_sectors = gpt->gpt_drive_sectors = DEFAULT_DRIVE_SECTORS;
gpt->current_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND;
gpt->valid_headers = MASK_BOTH;
gpt->valid_entries = MASK_BOTH;
@@ -353,7 +353,7 @@ static int ParameterTests(void)
for (i = 0; i < ARRAY_SIZE(cases); ++i) {
BuildTestGptData(gpt);
gpt->sector_bytes = cases[i].sector_bytes;
- gpt->drive_sectors = cases[i].drive_sectors;
+ gpt->drive_sectors = gpt->gpt_drive_sectors = cases[i].drive_sectors;
EXPECT(cases[i].expected_retval == CheckParameters(gpt));
}
@@ -461,15 +461,15 @@ static int SignatureTest(void)
GptHeader *h2 = (GptHeader *)gpt->secondary_header;
int i;
- EXPECT(1 == CheckHeader(NULL, 0, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(NULL, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
for (i = 0; i < 8; ++i) {
BuildTestGptData(gpt);
h1->signature[i] ^= 0xff;
h2->signature[i] ^= 0xff;
RefreshCrc32(gpt);
- EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
}
return TEST_OK;
@@ -503,9 +503,9 @@ static int RevisionTest(void)
h2->revision = cases[i].value_to_test;
RefreshCrc32(gpt);
- EXPECT(CheckHeader(h1, 0, gpt->drive_sectors) ==
+ EXPECT(CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE) ==
cases[i].expect_rv);
- EXPECT(CheckHeader(h2, 1, gpt->drive_sectors) ==
+ EXPECT(CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE) ==
cases[i].expect_rv);
}
return TEST_OK;
@@ -536,9 +536,9 @@ static int SizeTest(void)
h2->size = cases[i].value_to_test;
RefreshCrc32(gpt);
- EXPECT(CheckHeader(h1, 0, gpt->drive_sectors) ==
+ EXPECT(CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE) ==
cases[i].expect_rv);
- EXPECT(CheckHeader(h2, 1, gpt->drive_sectors) ==
+ EXPECT(CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE) ==
cases[i].expect_rv);
}
return TEST_OK;
@@ -555,12 +555,12 @@ static int CrcFieldTest(void)
/* Modify a field that the header verification doesn't care about */
h1->entries_crc32++;
h2->entries_crc32++;
- EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
/* Refresh the CRC; should pass now */
RefreshCrc32(gpt);
- EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(0 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(0 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
return TEST_OK;
}
@@ -576,8 +576,8 @@ static int ReservedFieldsTest(void)
h1->reserved_zero ^= 0x12345678; /* whatever random */
h2->reserved_zero ^= 0x12345678; /* whatever random */
RefreshCrc32(gpt);
- EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
#ifdef PADDING_CHECKED
/* TODO: padding check is currently disabled */
@@ -585,8 +585,8 @@ static int ReservedFieldsTest(void)
h1->padding[12] ^= 0x34; /* whatever random */
h2->padding[56] ^= 0x78; /* whatever random */
RefreshCrc32(gpt);
- EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
#endif
return TEST_OK;
@@ -624,9 +624,9 @@ static int SizeOfPartitionEntryTest(void) {
cases[i].value_to_test;
RefreshCrc32(gpt);
- EXPECT(CheckHeader(h1, 0, gpt->drive_sectors) ==
+ EXPECT(CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE) ==
cases[i].expect_rv);
- EXPECT(CheckHeader(h2, 1, gpt->drive_sectors) ==
+ EXPECT(CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE) ==
cases[i].expect_rv);
}
@@ -647,8 +647,11 @@ static int NumberOfPartitionEntriesTest(void)
h1->number_of_entries--;
h2->number_of_entries /= 2;
RefreshCrc32(gpt);
- EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ /* But it's okay to have less if the GPT structs are stored elsewhere. */
+ EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_OFF_DEVICE));
+ EXPECT(0 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_OFF_DEVICE));
return TEST_OK;
}
@@ -663,37 +666,37 @@ static int MyLbaTest(void)
/* myLBA depends on primary vs secondary flag */
BuildTestGptData(gpt);
- EXPECT(1 == CheckHeader(h1, 1, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 0, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
BuildTestGptData(gpt);
h1->my_lba--;
h2->my_lba--;
RefreshCrc32(gpt);
- EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
BuildTestGptData(gpt);
h1->my_lba = 2;
h2->my_lba--;
RefreshCrc32(gpt);
- EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
/* We should ignore the alternate_lba field entirely */
BuildTestGptData(gpt);
h1->alternate_lba++;
h2->alternate_lba++;
RefreshCrc32(gpt);
- EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(0 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(0 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
BuildTestGptData(gpt);
h1->alternate_lba--;
h2->alternate_lba--;
RefreshCrc32(gpt);
- EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(0 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(0 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
BuildTestGptData(gpt);
h1->entries_lba++;
@@ -703,19 +706,19 @@ static int MyLbaTest(void)
* We support a padding between primary GPT header and its entries. So
* this still passes.
*/
- EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors));
+ EXPECT(0 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
/*
* But the secondary table should fail because it would overlap the
* header, which is now lying after its entry array.
*/
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
BuildTestGptData(gpt);
h1->entries_lba--;
h2->entries_lba--;
RefreshCrc32(gpt);
- EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors));
- EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors));
+ EXPECT(1 == CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
+ EXPECT(1 == CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE));
return TEST_OK;
}
@@ -723,7 +726,8 @@ static int MyLbaTest(void)
/* Test if FirstUsableLBA and LastUsableLBA are checked.
* FirstUsableLBA must be after the end of the primary GPT table array.
* LastUsableLBA must be before the start of the secondary GPT table array.
- * FirstUsableLBA <= LastUsableLBA. */
+ * FirstUsableLBA <= LastUsableLBA.
+ * Also see CheckHeaderOffDevice() test. */
static int FirstUsableLbaAndLastUsableLbaTest(void)
{
GptData *gpt = GetEmptyGptData();
@@ -763,9 +767,9 @@ static int FirstUsableLbaAndLastUsableLbaTest(void)
h2->last_usable_lba = cases[i].secondary_last_usable_lba;
RefreshCrc32(gpt);
- EXPECT(CheckHeader(h1, 0, gpt->drive_sectors) ==
+ EXPECT(CheckHeader(h1, 0, gpt->drive_sectors, GPT_STORED_ON_DEVICE) ==
cases[i].primary_rv);
- EXPECT(CheckHeader(h2, 1, gpt->drive_sectors) ==
+ EXPECT(CheckHeader(h2, 1, gpt->drive_sectors, GPT_STORED_ON_DEVICE) ==
cases[i].secondary_rv);
}
@@ -1966,6 +1970,64 @@ static int MtdFtsTest() {
return TEST_OK;
}
+static int CheckHeaderOffDevice()
+{
+ GptData* gpt = GetEmptyGptData();
+ BuildTestGptData(gpt);
+
+ GptHeader* primary_header = (GptHeader*)gpt->primary_header;
+ primary_header->first_usable_lba = 0;
+ RefreshCrc32(gpt);
+ // GPT is stored on the same device so first usable lba should not
+ // start at 0.
+ EXPECT(1 == CheckHeader(primary_header, 0, gpt->drive_sectors,
+ GPT_STORED_ON_DEVICE));
+ // But off device, it is okay to accept this GPT header.
+ EXPECT(0 == CheckHeader(primary_header, 0, gpt->drive_sectors,
+ GPT_STORED_OFF_DEVICE));
+
+ BuildTestGptData(gpt);
+ primary_header->number_of_entries = 100;
+ RefreshCrc32(gpt);
+ // Normally, number of entries is 128. So this should fail.
+ EXPECT(1 == CheckHeader(primary_header, 0, gpt->drive_sectors,
+ GPT_STORED_ON_DEVICE));
+ // But off device, it is okay.
+ EXPECT(0 == CheckHeader(primary_header, 0, gpt->drive_sectors,
+ GPT_STORED_OFF_DEVICE));
+
+ primary_header->number_of_entries = MIN_NUMBER_OF_ENTRIES - 1;
+ RefreshCrc32(gpt);
+ // However, too few entries is not good.
+ EXPECT(1 == CheckHeader(primary_header, 0, gpt->drive_sectors,
+ GPT_STORED_OFF_DEVICE));
+
+ // Repeat for secondary header.
+ BuildTestGptData(gpt);
+ GptHeader* secondary_header = (GptHeader*)gpt->secondary_header;
+ secondary_header->first_usable_lba = 0;
+ RefreshCrc32(gpt);
+ EXPECT(1 == CheckHeader(secondary_header, 1, gpt->drive_sectors,
+ GPT_STORED_ON_DEVICE));
+ EXPECT(0 == CheckHeader(secondary_header, 1, gpt->drive_sectors,
+ GPT_STORED_OFF_DEVICE));
+
+ BuildTestGptData(gpt);
+ secondary_header->number_of_entries = 100;
+ RefreshCrc32(gpt);
+ EXPECT(1 == CheckHeader(secondary_header, 1, gpt->drive_sectors,
+ GPT_STORED_ON_DEVICE));
+ EXPECT(0 == CheckHeader(secondary_header, 1, gpt->drive_sectors,
+ GPT_STORED_OFF_DEVICE));
+
+ secondary_header->number_of_entries = MIN_NUMBER_OF_ENTRIES - 1;
+ RefreshCrc32(gpt);
+ EXPECT(1 == CheckHeader(secondary_header, 1, gpt->drive_sectors,
+ GPT_STORED_OFF_DEVICE));
+
+ return TEST_OK;
+}
+
int main(int argc, char *argv[])
{
int i;
@@ -2013,6 +2075,7 @@ int main(int argc, char *argv[])
{ TEST_CASE(GetKernelGuidTest), },
{ TEST_CASE(ErrorTextTest), },
{ TEST_CASE(MtdFtsTest), },
+ { TEST_CASE(CheckHeaderOffDevice), },
};
for (i = 0; i < sizeof(test_cases)/sizeof(test_cases[0]); ++i) {
diff --git a/tests/run_cgpt_tests.sh b/tests/run_cgpt_tests.sh
index 847c0813..95840fbb 100755
--- a/tests/run_cgpt_tests.sh
+++ b/tests/run_cgpt_tests.sh
@@ -21,6 +21,12 @@ DIR="${TEST_DIR}/cgpt_test_dir"
warning "testing $CGPT in $DIR"
cd "$DIR"
+# Test failure on non existing file.
+set +e
+${CGPT} show blah_404_haha
+[ $? != 0 ] || error "CGPT should fail on non existing file."
+set -e
+
echo "Create an empty file to use as the device..."
NUM_SECTORS=1000
DEV=fake_dev.bin
diff --git a/tests/vboot_kernel_tests.c b/tests/vboot_kernel_tests.c
index 0c26d212..05035f28 100644
--- a/tests/vboot_kernel_tests.c
+++ b/tests/vboot_kernel_tests.c
@@ -289,7 +289,7 @@ static void ReadWriteGptTest(void)
GptHeader *h;
g.sector_bytes = MOCK_SECTOR_SIZE;
- g.drive_sectors = MOCK_SECTOR_COUNT;
+ g.drive_sectors = g.gpt_drive_sectors = MOCK_SECTOR_COUNT;
g.valid_headers = g.valid_entries = MASK_BOTH;
ResetMocks();
@@ -315,10 +315,10 @@ static void ReadWriteGptTest(void)
Memset(mock_gpt_primary, '\0', sizeof(*mock_gpt_primary));
TEST_EQ(AllocAndReadGptData(handle, &g), 0,
"AllocAndRead primary invalid");
- TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.drive_sectors), 1,
- "Primary header is invalid");
- TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.drive_sectors), 0,
- "Secondary header is valid");
+ TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.drive_sectors,
+ GPT_STORED_ON_DEVICE), 1, "Primary header is invalid");
+ TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.drive_sectors,
+ GPT_STORED_ON_DEVICE), 0, "Secondary header is valid");
TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
"VbExDiskRead(h, 1023, 1)\n"
"VbExDiskRead(h, 991, 32)\n");
@@ -332,10 +332,10 @@ static void ReadWriteGptTest(void)
Memset(mock_gpt_secondary, '\0', sizeof(*mock_gpt_secondary));
TEST_EQ(AllocAndReadGptData(handle, &g), 0,
"AllocAndRead secondary invalid");
- TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.drive_sectors), 0,
- "Primary header is valid");
- TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.drive_sectors), 1,
- "Secondary header is invalid");
+ TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.drive_sectors,
+ GPT_STORED_ON_DEVICE), 0, "Primary header is valid");
+ TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.drive_sectors,
+ GPT_STORED_ON_DEVICE), 1, "Secondary header is invalid");
TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
"VbExDiskRead(h, 2, 32)\n"
"VbExDiskRead(h, 1023, 1)\n");
@@ -350,10 +350,10 @@ static void ReadWriteGptTest(void)
Memset(mock_gpt_secondary, '\0', sizeof(*mock_gpt_secondary));
TEST_EQ(AllocAndReadGptData(handle, &g), 1,
"AllocAndRead primary and secondary invalid");
- TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.drive_sectors), 1,
- "Primary header is invalid");
- TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.drive_sectors), 1,
- "Secondary header is invalid");
+ TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.drive_sectors,
+ GPT_STORED_ON_DEVICE), 1, "Primary header is invalid");
+ TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.drive_sectors,
+ GPT_STORED_ON_DEVICE), 1, "Secondary header is invalid");
TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
"VbExDiskRead(h, 1023, 1)\n");
WriteAndFreeGptData(handle, &g);
@@ -379,8 +379,8 @@ static void ReadWriteGptTest(void)
"VbExDiskRead(h, 991, 32)\n"
"VbExDiskWrite(h, 1, 1)\n"
"VbExDiskWrite(h, 2, 32)\n");
- TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.drive_sectors), 0,
- "Fix Primary GPT: Primary header is valid");
+ TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.drive_sectors,
+ GPT_STORED_ON_DEVICE), 0, "Fix Primary GPT: Primary header is valid");
/*
* Invalidate secondary GPT header and check that it can be
@@ -403,8 +403,8 @@ static void ReadWriteGptTest(void)
"VbExDiskRead(h, 1023, 1)\n"
"VbExDiskWrite(h, 1023, 1)\n"
"VbExDiskWrite(h, 991, 32)\n");
- TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.drive_sectors), 0,
- "Fix Secondary GPT: Secondary header is valid");
+ TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.drive_sectors,
+ GPT_STORED_ON_DEVICE), 0, "Fix Secondary GPT: Secondary header is valid");
/* Data which is changed is written */
ResetMocks();