From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 1/5] partitions: efi: calculate instead of hardcode gpt header fields
Date: Mon, 02 Jun 2025 15:28:35 +0200 [thread overview]
Message-ID: <20250602-createnv-v1-1-c3239ff875d5@pengutronix.de> (raw)
In-Reply-To: <20250602-createnv-v1-0-c3239ff875d5@pengutronix.de>
Calculate the lba values of the GPT header instead of hardcoding them.
This will allow us to modify some fields while preserving a consistent
partition table in the next step.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
common/partitions/efi.c | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index 840d6a83b7950501debc943864e20e797628720d..92868e4a77e5688d547ee3f2cac3ab9534a4ebdb 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -583,10 +583,13 @@ static __maybe_unused struct partition_desc *efi_partition_create_table(struct b
{
struct efi_partition_desc *epd = xzalloc(sizeof(*epd));
gpt_header *gpt;
+ unsigned int num_partition_entries = 128;
+ unsigned int gpt_size = (sizeof(gpt_entry) * num_partition_entries) / SECTOR_SIZE;
+ unsigned int first_usable_lba = gpt_size + 2;
partition_desc_init(&epd->pd, blk);
- epd->gpt = xzalloc(512);
+ epd->gpt = xzalloc(SECTOR_SIZE);
gpt = epd->gpt;
gpt->signature = cpu_to_le64(GPT_HEADER_SIGNATURE);
@@ -594,18 +597,18 @@ static __maybe_unused struct partition_desc *efi_partition_create_table(struct b
gpt->header_size = cpu_to_le32(sizeof(*gpt));
gpt->my_lba = cpu_to_le64(1);
gpt->alternate_lba = cpu_to_le64(last_lba(blk));
- gpt->first_usable_lba = cpu_to_le64(34);
- gpt->last_usable_lba = cpu_to_le64(last_lba(blk) - 34);;
+ gpt->first_usable_lba = cpu_to_le64(first_usable_lba);
+ gpt->last_usable_lba = cpu_to_le64(last_lba(blk) - (gpt_size + 2));;
generate_random_guid((unsigned char *)&gpt->disk_guid);
- gpt->partition_entry_lba = cpu_to_le64(2);
- gpt->num_partition_entries = cpu_to_le32(128);
+ gpt->partition_entry_lba = cpu_to_le64(first_usable_lba - gpt_size);
+ gpt->num_partition_entries = cpu_to_le32(num_partition_entries);
gpt->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
add_gpt_diskuuid_param(epd, blk);
pr_info("Created new disk label with GUID %pU\n", &gpt->disk_guid);
- epd->ptes = xzalloc(128 * sizeof(gpt_entry));
+ epd->ptes = xzalloc(num_partition_entries * sizeof(gpt_entry));
return &epd->pd;
}
--
2.39.5
next prev parent reply other threads:[~2025-06-02 13:29 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-02 13:28 [PATCH 0/5] add createnv command to create environment partition Sascha Hauer
2025-06-02 13:28 ` Sascha Hauer [this message]
2025-06-02 13:28 ` [PATCH 2/5] partitions: Start partitions at 8MiB offset Sascha Hauer
2025-06-02 22:16 ` Marco Felsch
2025-06-03 7:46 ` Sascha Hauer
2025-06-02 13:28 ` [PATCH 3/5] cdev: fix cdev_open_by_name() misuse Sascha Hauer
2025-06-02 13:28 ` [PATCH 4/5] commands: create createnv command Sascha Hauer
2025-06-02 13:48 ` Ahmad Fatoum
2025-06-02 14:50 ` Sascha Hauer
2025-06-02 13:28 ` [PATCH 5/5] mci: add option to detect non-removable cards during startup Sascha Hauer
2025-06-02 13:42 ` Ahmad Fatoum
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250602-createnv-v1-1-c3239ff875d5@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox