2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2017-07-22 16:32:54 +00:00
|
|
|
* Copyright (c) Intel Corporation.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spdk_cunit.h"
|
|
|
|
|
2018-03-22 22:33:19 +00:00
|
|
|
#include "common/lib/test_env.c"
|
2017-07-22 16:32:54 +00:00
|
|
|
|
2018-02-13 15:32:22 +00:00
|
|
|
#include "bdev/gpt/gpt.c"
|
2017-07-22 16:32:54 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
test_check_mbr(void)
|
|
|
|
{
|
|
|
|
struct spdk_gpt *gpt;
|
|
|
|
struct spdk_mbr *mbr;
|
|
|
|
unsigned char a[SPDK_GPT_BUFFER_SIZE];
|
|
|
|
int re;
|
|
|
|
|
2019-01-19 06:56:14 +00:00
|
|
|
/* Set gpt is NULL */
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_mbr(NULL);
|
2019-01-19 06:56:14 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set gpt->buf is NULL */
|
2017-07-22 16:32:54 +00:00
|
|
|
gpt = calloc(1, sizeof(*gpt));
|
|
|
|
SPDK_CU_ASSERT_FATAL(gpt != NULL);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_mbr(gpt);
|
2019-01-19 06:56:14 +00:00
|
|
|
CU_ASSERT(re == -1);
|
2017-07-22 16:32:54 +00:00
|
|
|
|
|
|
|
/* Set *gpt is "aaa...", all are mismatch include mbr_signature */
|
|
|
|
memset(a, 'a', sizeof(a));
|
|
|
|
gpt->buf = &a[0];
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_check_mbr(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set mbr->mbr_signature matched, start lba mismatch */
|
|
|
|
mbr = (struct spdk_mbr *)gpt->buf;
|
|
|
|
mbr->mbr_signature = 0xAA55;
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_check_mbr(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set mbr->partitions[0].start lba matched, os_type mismatch */
|
|
|
|
mbr->partitions[0].start_lba = 1;
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_check_mbr(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set mbr->partitions[0].os_type matched, size_lba mismatch */
|
|
|
|
mbr->partitions[0].os_type = 0xEE;
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_check_mbr(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set mbr->partitions[0].size_lba matched, passing case */
|
|
|
|
mbr->partitions[0].size_lba = 0xFFFFFFFF;
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_check_mbr(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == 0);
|
|
|
|
|
|
|
|
free(gpt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_read_header(void)
|
|
|
|
{
|
|
|
|
struct spdk_gpt *gpt;
|
|
|
|
struct spdk_gpt_header *head;
|
|
|
|
unsigned char a[SPDK_GPT_BUFFER_SIZE];
|
|
|
|
int re;
|
|
|
|
|
2020-05-10 07:22:54 +00:00
|
|
|
/* gpt_read_header(NULL) does not exist, NULL is filtered out in gpt_parse_mbr() */
|
2017-07-22 16:32:54 +00:00
|
|
|
gpt = calloc(1, sizeof(*gpt));
|
|
|
|
SPDK_CU_ASSERT_FATAL(gpt != NULL);
|
2019-01-19 06:56:14 +00:00
|
|
|
gpt->parse_phase = SPDK_GPT_PARSE_PHASE_PRIMARY;
|
|
|
|
gpt->sector_size = 512;
|
2017-07-22 16:32:54 +00:00
|
|
|
|
|
|
|
/* Set *gpt is "aaa..." */
|
|
|
|
memset(a, 'a', sizeof(a));
|
|
|
|
gpt->buf = &a[0];
|
2019-01-19 06:56:14 +00:00
|
|
|
gpt->buf_size = sizeof(a);
|
2017-07-22 16:32:54 +00:00
|
|
|
|
|
|
|
/* Set header_size mismatch */
|
|
|
|
gpt->sector_size = 512;
|
|
|
|
head = (struct spdk_gpt_header *)(gpt->buf + GPT_PRIMARY_PARTITION_TABLE_LBA * gpt->sector_size);
|
|
|
|
to_le32(&head->header_size, 0x258);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_header(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set head->header_size matched, header_crc32 mismatch */
|
|
|
|
head->header_size = sizeof(*head);
|
|
|
|
to_le32(&head->header_crc32, 0x22D18C80);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_header(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set head->header_crc32 matched, gpt_signature mismatch */
|
|
|
|
to_le32(&head->header_crc32, 0xC5B2117E);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_header(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
2019-01-09 15:32:35 +00:00
|
|
|
/* Set head->gpt_signature matched, head->my_lba mismatch */
|
2017-07-22 16:32:54 +00:00
|
|
|
to_le32(&head->header_crc32, 0xD637335A);
|
|
|
|
head->gpt_signature[0] = 'E';
|
|
|
|
head->gpt_signature[1] = 'F';
|
|
|
|
head->gpt_signature[2] = 'I';
|
|
|
|
head->gpt_signature[3] = ' ';
|
|
|
|
head->gpt_signature[4] = 'P';
|
|
|
|
head->gpt_signature[5] = 'A';
|
|
|
|
head->gpt_signature[6] = 'R';
|
|
|
|
head->gpt_signature[7] = 'T';
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_header(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
2019-01-09 15:32:35 +00:00
|
|
|
/* Set head->my_lba matched, lba_end usable_lba mismatch */
|
|
|
|
to_le32(&head->header_crc32, 0xB3CDB2D2);
|
|
|
|
to_le64(&head->my_lba, 0x1);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_header(gpt);
|
2019-01-09 15:32:35 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
2017-07-22 16:32:54 +00:00
|
|
|
/* Set gpt->lba_end usable_lba matched, passing case */
|
2019-01-09 15:32:35 +00:00
|
|
|
to_le32(&head->header_crc32, 0x5531F2F0);
|
2017-07-22 16:32:54 +00:00
|
|
|
to_le64(&gpt->lba_start, 0x0);
|
|
|
|
to_le64(&gpt->lba_end, 0x2E935FFE);
|
|
|
|
to_le64(&head->first_usable_lba, 0xA);
|
|
|
|
to_le64(&head->last_usable_lba, 0xF4240);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_header(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == 0);
|
|
|
|
|
|
|
|
free(gpt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_read_partitions(void)
|
|
|
|
{
|
|
|
|
struct spdk_gpt *gpt;
|
|
|
|
struct spdk_gpt_header *head;
|
|
|
|
unsigned char a[SPDK_GPT_BUFFER_SIZE];
|
|
|
|
int re;
|
|
|
|
|
2020-05-10 07:22:54 +00:00
|
|
|
/* gpt_read_partitions(NULL) does not exist, NULL is filtered out in gpt_parse_mbr() */
|
2017-07-22 16:32:54 +00:00
|
|
|
gpt = calloc(1, sizeof(*gpt));
|
|
|
|
SPDK_CU_ASSERT_FATAL(gpt != NULL);
|
2019-01-19 06:56:14 +00:00
|
|
|
gpt->parse_phase = SPDK_GPT_PARSE_PHASE_PRIMARY;
|
|
|
|
gpt->sector_size = 512;
|
2017-07-22 16:32:54 +00:00
|
|
|
|
|
|
|
/* Set *gpt is "aaa..." */
|
|
|
|
memset(a, 'a', sizeof(a));
|
|
|
|
gpt->buf = &a[0];
|
2019-01-19 06:56:14 +00:00
|
|
|
gpt->buf_size = sizeof(a);
|
2017-07-22 16:32:54 +00:00
|
|
|
|
2018-08-27 08:24:40 +00:00
|
|
|
/* Set num_partition_entries exceeds Max value of entries GPT supported */
|
2017-07-22 16:32:54 +00:00
|
|
|
gpt->sector_size = 512;
|
|
|
|
head = (struct spdk_gpt_header *)(gpt->buf + GPT_PRIMARY_PARTITION_TABLE_LBA * gpt->sector_size);
|
|
|
|
gpt->header = head;
|
|
|
|
to_le32(&head->num_partition_entries, 0x100);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_partitions(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
2018-08-27 08:24:40 +00:00
|
|
|
/* Set num_partition_entries within Max value, size_of_partition_entry mismatch */
|
2017-07-22 16:32:54 +00:00
|
|
|
to_le32(&head->header_crc32, 0x573857BE);
|
|
|
|
to_le32(&head->num_partition_entries, 0x40);
|
|
|
|
to_le32(&head->size_of_partition_entry, 0x0);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_partitions(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set size_of_partition_entry matched, partition_entry_lba mismatch */
|
|
|
|
to_le32(&head->header_crc32, 0x5279B712);
|
|
|
|
to_le32(&head->size_of_partition_entry, 0x80);
|
|
|
|
to_le64(&head->partition_entry_lba, 0x64);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_partitions(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set partition_entry_lba matched, partition_entry_array_crc32 mismatch */
|
|
|
|
to_le32(&head->header_crc32, 0xEC093B43);
|
|
|
|
to_le64(&head->partition_entry_lba, 0x20);
|
|
|
|
to_le32(&head->partition_entry_array_crc32, 0x0);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_partitions(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set partition_entry_array_crc32 matched, passing case */
|
|
|
|
to_le32(&head->header_crc32, 0xE1A08822);
|
|
|
|
to_le32(&head->partition_entry_array_crc32, 0xEBEE44FB);
|
|
|
|
to_le32(&head->num_partition_entries, 0x80);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_read_partitions(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == 0);
|
|
|
|
|
|
|
|
free(gpt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-19 06:56:14 +00:00
|
|
|
test_parse_mbr_and_primary(void)
|
2017-07-22 16:32:54 +00:00
|
|
|
{
|
|
|
|
struct spdk_gpt *gpt;
|
|
|
|
struct spdk_mbr *mbr;
|
|
|
|
struct spdk_gpt_header *head;
|
|
|
|
unsigned char a[SPDK_GPT_BUFFER_SIZE];
|
|
|
|
int re;
|
|
|
|
|
|
|
|
/* Set gpt is NULL */
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_mbr(NULL);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set gpt->buf is NULL */
|
|
|
|
gpt = calloc(1, sizeof(*gpt));
|
|
|
|
SPDK_CU_ASSERT_FATAL(gpt != NULL);
|
2019-01-19 06:56:14 +00:00
|
|
|
gpt->parse_phase = SPDK_GPT_PARSE_PHASE_PRIMARY;
|
|
|
|
gpt->sector_size = 512;
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_mbr(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set *gpt is "aaa...", check_mbr failed */
|
|
|
|
memset(a, 'a', sizeof(a));
|
|
|
|
gpt->buf = &a[0];
|
2019-01-19 06:56:14 +00:00
|
|
|
gpt->buf_size = sizeof(a);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_mbr(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
2019-01-19 06:56:14 +00:00
|
|
|
/* Set check_mbr passed */
|
2017-07-22 16:32:54 +00:00
|
|
|
mbr = (struct spdk_mbr *)gpt->buf;
|
|
|
|
mbr->mbr_signature = 0xAA55;
|
|
|
|
mbr->partitions[0].start_lba = 1;
|
|
|
|
mbr->partitions[0].os_type = 0xEE;
|
|
|
|
mbr->partitions[0].size_lba = 0xFFFFFFFF;
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_mbr(gpt);
|
2019-01-19 06:56:14 +00:00
|
|
|
CU_ASSERT(re == 0);
|
|
|
|
|
|
|
|
/* Expect read_header failed */
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_partition_table(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set read_header passed, read_partitions failed */
|
|
|
|
head = (struct spdk_gpt_header *)(gpt->buf + GPT_PRIMARY_PARTITION_TABLE_LBA * gpt->sector_size);
|
|
|
|
head->header_size = sizeof(*head);
|
|
|
|
head->gpt_signature[0] = 'E';
|
|
|
|
head->gpt_signature[1] = 'F';
|
|
|
|
head->gpt_signature[2] = 'I';
|
|
|
|
head->gpt_signature[3] = ' ';
|
|
|
|
head->gpt_signature[4] = 'P';
|
|
|
|
head->gpt_signature[5] = 'A';
|
|
|
|
head->gpt_signature[6] = 'R';
|
|
|
|
head->gpt_signature[7] = 'T';
|
2019-01-09 15:32:35 +00:00
|
|
|
to_le32(&head->header_crc32, 0x5531F2F0);
|
|
|
|
to_le64(&head->my_lba, 0x1);
|
2017-07-22 16:32:54 +00:00
|
|
|
to_le64(&gpt->lba_start, 0x0);
|
|
|
|
to_le64(&gpt->lba_end, 0x2E935FFE);
|
|
|
|
to_le64(&head->first_usable_lba, 0xA);
|
|
|
|
to_le64(&head->last_usable_lba, 0xF4240);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_partition_table(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set read_partitions passed, all passed */
|
|
|
|
to_le32(&head->size_of_partition_entry, 0x80);
|
|
|
|
to_le64(&head->partition_entry_lba, 0x20);
|
2019-01-09 15:32:35 +00:00
|
|
|
to_le32(&head->header_crc32, 0x845A09AA);
|
2017-07-22 16:32:54 +00:00
|
|
|
to_le32(&head->partition_entry_array_crc32, 0xEBEE44FB);
|
|
|
|
to_le32(&head->num_partition_entries, 0x80);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_partition_table(gpt);
|
2019-01-19 06:56:14 +00:00
|
|
|
CU_ASSERT(re == 0);
|
|
|
|
|
|
|
|
free(gpt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_parse_secondary(void)
|
|
|
|
{
|
|
|
|
struct spdk_gpt *gpt;
|
|
|
|
struct spdk_gpt_header *head;
|
|
|
|
unsigned char a[SPDK_GPT_BUFFER_SIZE];
|
|
|
|
int re;
|
|
|
|
|
2020-05-10 07:22:54 +00:00
|
|
|
/* gpt_parse_partition_table(NULL) does not exist, NULL is filtered out in gpt_parse_mbr() */
|
2019-01-19 06:56:14 +00:00
|
|
|
gpt = calloc(1, sizeof(*gpt));
|
|
|
|
SPDK_CU_ASSERT_FATAL(gpt != NULL);
|
|
|
|
gpt->parse_phase = SPDK_GPT_PARSE_PHASE_SECONDARY;
|
|
|
|
gpt->sector_size = 512;
|
|
|
|
|
|
|
|
/* Set *gpt is "aaa...", read_header failed */
|
|
|
|
memset(a, 'a', sizeof(a));
|
|
|
|
gpt->buf = &a[0];
|
|
|
|
gpt->buf_size = sizeof(a);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_partition_table(gpt);
|
2019-01-19 06:56:14 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set read_header passed, read_partitions failed */
|
|
|
|
head = (struct spdk_gpt_header *)(gpt->buf + gpt->buf_size - gpt->sector_size);
|
|
|
|
head->header_size = sizeof(*head);
|
|
|
|
head->gpt_signature[0] = 'E';
|
|
|
|
head->gpt_signature[1] = 'F';
|
|
|
|
head->gpt_signature[2] = 'I';
|
|
|
|
head->gpt_signature[3] = ' ';
|
|
|
|
head->gpt_signature[4] = 'P';
|
|
|
|
head->gpt_signature[5] = 'A';
|
|
|
|
head->gpt_signature[6] = 'R';
|
|
|
|
head->gpt_signature[7] = 'T';
|
|
|
|
to_le32(&head->header_crc32, 0xAA68A167);
|
|
|
|
to_le64(&head->my_lba, 0x63FFFFF);
|
|
|
|
to_le64(&gpt->lba_start, 0x0);
|
|
|
|
to_le64(&gpt->lba_end, 0x63FFFFF);
|
|
|
|
to_le64(&gpt->total_sectors, 0x6400000);
|
|
|
|
to_le64(&head->first_usable_lba, 0xA);
|
|
|
|
to_le64(&head->last_usable_lba, 0x63FFFDE);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_partition_table(gpt);
|
2019-01-19 06:56:14 +00:00
|
|
|
CU_ASSERT(re == -1);
|
|
|
|
|
|
|
|
/* Set read_partitions passed, all passed */
|
|
|
|
to_le32(&head->size_of_partition_entry, 0x80);
|
|
|
|
to_le64(&head->partition_entry_lba, 0x63FFFDF);
|
|
|
|
to_le32(&head->header_crc32, 0x204129E8);
|
|
|
|
to_le32(&head->partition_entry_array_crc32, 0xEBEE44FB);
|
|
|
|
to_le32(&head->num_partition_entries, 0x80);
|
2020-05-10 07:22:54 +00:00
|
|
|
re = gpt_parse_partition_table(gpt);
|
2017-07-22 16:32:54 +00:00
|
|
|
CU_ASSERT(re == 0);
|
|
|
|
|
|
|
|
free(gpt);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
CU_pSuite suite = NULL;
|
|
|
|
unsigned int num_failures;
|
|
|
|
|
2020-03-11 17:59:24 +00:00
|
|
|
CU_set_error_action(CUEA_ABORT);
|
|
|
|
CU_initialize_registry();
|
2017-07-22 16:32:54 +00:00
|
|
|
|
|
|
|
suite = CU_add_suite("gpt_parse", NULL, NULL);
|
2020-03-11 17:59:24 +00:00
|
|
|
|
2020-03-11 19:15:39 +00:00
|
|
|
CU_ADD_TEST(suite, test_parse_mbr_and_primary);
|
|
|
|
CU_ADD_TEST(suite, test_parse_secondary);
|
|
|
|
CU_ADD_TEST(suite, test_check_mbr);
|
|
|
|
CU_ADD_TEST(suite, test_read_header);
|
|
|
|
CU_ADD_TEST(suite, test_read_partitions);
|
2017-07-22 16:32:54 +00:00
|
|
|
|
|
|
|
CU_basic_set_mode(CU_BRM_VERBOSE);
|
|
|
|
CU_basic_run_tests();
|
|
|
|
num_failures = CU_get_number_of_failures();
|
|
|
|
CU_cleanup_registry();
|
|
|
|
return num_failures;
|
|
|
|
}
|