From dd2ffbc768d4ecc1d297ef29a2945e1de4ae9716 Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Mon, 26 Sep 2022 15:44:07 +0400 Subject: [PATCH] Fixes for GPT partition parsing 1. Don't handle invalid or empty pte entries num_partition_entries field in GPT typically means number of maximum entries and not the number of used entries. Empty entries are indentified with "0" partition type guid. Loop through all the entries 2. Fix the GPT partition size calculation "ending_lba" is included in the partition, it is not the start of the next one. Thus the correct size of the partition is end-start+1 Signed-off-by: Jukka Laitinen --- fs/partition/fs_gpt.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fs/partition/fs_gpt.c b/fs/partition/fs_gpt.c index 35401f0a34..8b0b384396 100644 --- a/fs/partition/fs_gpt.c +++ b/fs/partition/fs_gpt.c @@ -390,6 +390,7 @@ int parse_gpt_partition(FAR struct partition_state_s *state, FAR struct gpt_header_s *gpt; FAR struct gpt_entry_s *ptes; struct partition_s pentry; + blkcnt_t lastlba; int nb_part; int count; int ret; @@ -462,12 +463,20 @@ int parse_gpt_partition(FAR struct partition_state_s *state, goto err; } + lastlba = gpt_last_lba(state); nb_part = le32toh(gpt->num_partition_entries); for (pentry.index = 0; pentry.index < nb_part; pentry.index++) { + /* Skip the empty or invalid entries */ + + if (!gpt_pte_is_valid(&ptes[pentry.index], lastlba)) + { + continue; + } + pentry.firstblock = GPT_LBA_TO_BLOCK(ptes[pentry.index].starting_lba, state->blocksize); - pentry.nblocks = GPT_LBA_TO_BLOCK(ptes[pentry.index].ending_lba, + pentry.nblocks = GPT_LBA_TO_BLOCK(ptes[pentry.index].ending_lba + 1, state->blocksize) - pentry.firstblock; pentry.blocksize = state->blocksize;