From 94f6d54e2f1aae6073e3d660a13ada390a674213 Mon Sep 17 00:00:00 2001 From: lorneli Date: Wed, 9 Jan 2019 23:32:35 +0800 Subject: [PATCH] bdev/gpt: examine my_lba in primary header Check my_lba in gpt's primary header equals to LBA1. Set correct my_lba in unit test to pass the test, and update related header_crc32. Change-Id: I124a7a883e9304bd3955ce3de6b589596c4195ea Signed-off-by: lorneli Reviewed-on: https://review.gerrithub.io/c/439889 Reviewed-by: Darek Stojaczyk Reviewed-by: Ben Walker Reviewed-by: Jim Harris Tested-by: SPDK CI Jenkins --- lib/bdev/gpt/gpt.c | 8 ++++++++ test/unit/lib/bdev/gpt/gpt.c/gpt_ut.c | 15 +++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/bdev/gpt/gpt.c b/lib/bdev/gpt/gpt.c index 4988ec4ce1..cea9f919ed 100644 --- a/lib/bdev/gpt/gpt.c +++ b/lib/bdev/gpt/gpt.c @@ -121,6 +121,7 @@ spdk_gpt_read_header(struct spdk_gpt *gpt) { uint32_t head_size; uint32_t new_crc, original_crc; + uint64_t my_lba; struct spdk_gpt_header *head; head = (struct spdk_gpt_header *)(gpt->buf + GPT_PRIMARY_PARTITION_TABLE_LBA * gpt->sector_size); @@ -149,6 +150,13 @@ spdk_gpt_read_header(struct spdk_gpt *gpt) return -1; } + my_lba = from_le64(&head->my_lba); + if (my_lba != GPT_PRIMARY_PARTITION_TABLE_LBA) { + SPDK_ERRLOG("head my_lba(%" PRIu64 ") != expected(%d)\n", + my_lba, GPT_PRIMARY_PARTITION_TABLE_LBA); + return -1; + } + if (spdk_gpt_lba_range_check(head, gpt->lba_end)) { SPDK_ERRLOG("lba range check error\n"); return -1; diff --git a/test/unit/lib/bdev/gpt/gpt.c/gpt_ut.c b/test/unit/lib/bdev/gpt/gpt.c/gpt_ut.c index 3182f9c4e9..5b90463142 100644 --- a/test/unit/lib/bdev/gpt/gpt.c/gpt_ut.c +++ b/test/unit/lib/bdev/gpt/gpt.c/gpt_ut.c @@ -113,7 +113,7 @@ test_read_header(void) re = spdk_gpt_read_header(gpt); CU_ASSERT(re == -1); - /* Set head->gpt_signature matched, lba_end usable_lba mismatch */ + /* Set head->gpt_signature matched, head->my_lba mismatch */ to_le32(&head->header_crc32, 0xD637335A); head->gpt_signature[0] = 'E'; head->gpt_signature[1] = 'F'; @@ -126,8 +126,14 @@ test_read_header(void) re = spdk_gpt_read_header(gpt); CU_ASSERT(re == -1); + /* Set head->my_lba matched, lba_end usable_lba mismatch */ + to_le32(&head->header_crc32, 0xB3CDB2D2); + to_le64(&head->my_lba, 0x1); + re = spdk_gpt_read_header(gpt); + CU_ASSERT(re == -1); + /* Set gpt->lba_end usable_lba matched, passing case */ - to_le32(&head->header_crc32, 0x30CB7378); + to_le32(&head->header_crc32, 0x5531F2F0); to_le64(&gpt->lba_start, 0x0); to_le64(&gpt->lba_end, 0x2E935FFE); to_le64(&head->first_usable_lba, 0xA); @@ -239,7 +245,8 @@ test_parse(void) head->gpt_signature[5] = 'A'; head->gpt_signature[6] = 'R'; head->gpt_signature[7] = 'T'; - to_le32(&head->header_crc32, 0x30CB7378); + to_le32(&head->header_crc32, 0x5531F2F0); + to_le64(&head->my_lba, 0x1); to_le64(&gpt->lba_start, 0x0); to_le64(&gpt->lba_end, 0x2E935FFE); to_le64(&head->first_usable_lba, 0xA); @@ -250,7 +257,7 @@ test_parse(void) /* Set read_partitions passed, all passed */ to_le32(&head->size_of_partition_entry, 0x80); to_le64(&head->partition_entry_lba, 0x20); - to_le32(&head->header_crc32, 0xE1A08822); + to_le32(&head->header_crc32, 0x845A09AA); to_le32(&head->partition_entry_array_crc32, 0xEBEE44FB); to_le32(&head->num_partition_entries, 0x80); re = spdk_gpt_parse(gpt);