From: Marco Felsch <m.felsch@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH v2 1/4] partitions: efi: add support to specify gpt-location
Date: Wed, 23 Apr 2025 16:09:10 +0200 [thread overview]
Message-ID: <20250423140913.1530237-1-m.felsch@pengutronix.de> (raw)
The actual GPT partition entries array can be placed everywhere, only
the GPT header needs to be placed at LBA1. Add support for this feature
to allow platform like i.MX3/5/6 to create a GPT partition table without
overriding the actual barebox image.
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
v2:
- new
commands/parted.c | 3 ++-
common/partitions.c | 5 +++--
common/partitions/dos.c | 3 ++-
common/partitions/efi.c | 15 +++++++++++----
include/partitions.h | 5 +++--
5 files changed, 21 insertions(+), 10 deletions(-)
diff --git a/commands/parted.c b/commands/parted.c
index 68b056a0758a..90cee1ede10e 100644
--- a/commands/parted.c
+++ b/commands/parted.c
@@ -217,13 +217,14 @@ static int do_rmpart(struct block_device *blk, int argc, char *argv[])
static int do_mklabel(struct block_device *blk, int argc, char *argv[])
{
struct partition_desc *pdesc;
+ uint64_t gpt_location = 2;
if (argc < 2) {
printf("Error: Expecting a disk label type.\n");
return -EINVAL;
}
- pdesc = partition_table_new(blk, argv[1]);
+ pdesc = partition_table_new(blk, argv[1], gpt_location);
if (IS_ERR(pdesc)) {
printf("Error: Cannot create partition table: %pe\n", pdesc);
return PTR_ERR(pdesc);
diff --git a/common/partitions.c b/common/partitions.c
index bc90f51f6112..ec50368c3e98 100644
--- a/common/partitions.c
+++ b/common/partitions.c
@@ -101,7 +101,8 @@ static struct partition_parser *partition_parser_get_by_filetype(uint8_t *buf)
return NULL;
}
-struct partition_desc *partition_table_new(struct block_device *blk, const char *type)
+struct partition_desc *
+partition_table_new(struct block_device *blk, const char *type, sector_t gpt_location)
{
struct partition_desc *pdesc;
struct partition_parser *parser;
@@ -116,7 +117,7 @@ struct partition_desc *partition_table_new(struct block_device *blk, const char
return ERR_PTR(-ENOSYS);
found:
- pdesc = parser->create(blk);
+ pdesc = parser->create(blk, gpt_location);
if (IS_ERR(pdesc))
return ERR_CAST(pdesc);
diff --git a/common/partitions/dos.c b/common/partitions/dos.c
index 6204fdabc81f..fcd43bf6aaa8 100644
--- a/common/partitions/dos.c
+++ b/common/partitions/dos.c
@@ -316,7 +316,8 @@ static void dos_partition_free(struct partition_desc *pd)
free(pd);
}
-static __maybe_unused struct partition_desc *dos_partition_create_table(struct block_device *blk)
+static __maybe_unused struct partition_desc *
+dos_partition_create_table(struct block_device *blk, sector_t gpt_location)
{
struct dos_partition_desc *dpd = xzalloc(512);
diff --git a/common/partitions/efi.c b/common/partitions/efi.c
index 840d6a83b795..8f9f9665c75b 100644
--- a/common/partitions/efi.c
+++ b/common/partitions/efi.c
@@ -579,11 +579,18 @@ static void efi_partition_free(struct partition_desc *pd)
free(epd);
}
-static __maybe_unused struct partition_desc *efi_partition_create_table(struct block_device *blk)
+static __maybe_unused struct partition_desc *
+efi_partition_create_table(struct block_device *blk, sector_t gpt_location)
{
struct efi_partition_desc *epd = xzalloc(sizeof(*epd));
gpt_header *gpt;
+ if (gpt_location < 2) {
+ pr_err("Invalid starting LBA (%llu) of array of partition entries\n",
+ gpt_location);
+ return ERR_PTR(-EINVAL);
+ }
+
partition_desc_init(&epd->pd, blk);
epd->gpt = xzalloc(512);
@@ -594,10 +601,10 @@ 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->first_usable_lba = cpu_to_le64(gpt_location + 32);
gpt->last_usable_lba = cpu_to_le64(last_lba(blk) - 34);;
generate_random_guid((unsigned char *)&gpt->disk_guid);
- gpt->partition_entry_lba = cpu_to_le64(2);
+ gpt->partition_entry_lba = cpu_to_le64(gpt_location);
gpt->num_partition_entries = cpu_to_le32(128);
gpt->sizeof_partition_entry = cpu_to_le32(sizeof(gpt_entry));
@@ -710,7 +717,7 @@ static int efi_protective_mbr(struct block_device *blk)
struct partition_desc *pdesc;
int ret;
- pdesc = partition_table_new(blk, "msdos");
+ pdesc = partition_table_new(blk, "msdos", 0);
if (IS_ERR(pdesc)) {
printf("Error: Cannot create partition table: %pe\n", pdesc);
return PTR_ERR(pdesc);
diff --git a/include/partitions.h b/include/partitions.h
index 785fb77ab167..f893f94b259f 100644
--- a/include/partitions.h
+++ b/include/partitions.h
@@ -41,7 +41,7 @@ struct partition_desc {
struct partition_parser {
struct partition_desc *(*parse)(void *buf, struct block_device *blk);
void (*partition_free)(struct partition_desc *pd);
- struct partition_desc *(*create)(struct block_device *blk);
+ struct partition_desc *(*create)(struct block_device *blk, sector_t gpt_location);
int (*mkpart)(struct partition_desc *pd, const char *name, const char *fs_type,
uint64_t start, uint64_t end);
int (*rmpart)(struct partition_desc *pd, struct partition *part);
@@ -58,7 +58,8 @@ struct partition_parser {
void partition_desc_init(struct partition_desc *pd, struct block_device *blk);
int partition_parser_register(struct partition_parser *p);
struct partition_desc *partition_table_read(struct block_device *blk);
-struct partition_desc *partition_table_new(struct block_device *blk, const char *type);
+struct partition_desc *
+partition_table_new(struct block_device *blk, const char *type, sector_t gpt_location);
int partition_table_write(struct partition_desc *pdesc);
int partition_create(struct partition_desc *pdesc, const char *name,
const char *fs_type, uint64_t lba_start, uint64_t lba_end);
--
2.39.5
next reply other threads:[~2025-04-23 17:41 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-23 14:09 Marco Felsch [this message]
2025-04-23 14:09 ` [PATCH v2 2/4] parted: add support for gpt-location Marco Felsch
2025-04-23 14:09 ` [PATCH v2 3/4] defaultenv-2: add support to automatically create an initial GPT table Marco Felsch
2025-04-23 14:09 ` [PATCH v2 4/4] ARM: riotboard: drop static barebox-environment handling Marco Felsch
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=20250423140913.1530237-1-m.felsch@pengutronix.de \
--to=m.felsch@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