* [PATCH v2 1/4] partitions: efi: add support to specify gpt-location
@ 2025-04-23 14:09 Marco Felsch
2025-04-23 14:09 ` [PATCH v2 2/4] parted: add support for gpt-location Marco Felsch
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Marco Felsch @ 2025-04-23 14:09 UTC (permalink / raw)
To: barebox
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 2/4] parted: add support for gpt-location
2025-04-23 14:09 [PATCH v2 1/4] partitions: efi: add support to specify gpt-location Marco Felsch
@ 2025-04-23 14:09 ` 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
2 siblings, 0 replies; 4+ messages in thread
From: Marco Felsch @ 2025-04-23 14:09 UTC (permalink / raw)
To: barebox
Add support to place the GPT partition entries to random places instead
of LBA2.
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
v2:
- new
commands/parted.c | 29 +++++++++++++++++++++++++++--
1 file changed, 27 insertions(+), 2 deletions(-)
diff --git a/commands/parted.c b/commands/parted.c
index 90cee1ede10e..895c06524d4c 100644
--- a/commands/parted.c
+++ b/commands/parted.c
@@ -224,6 +224,30 @@ static int do_mklabel(struct block_device *blk, int argc, char *argv[])
return -EINVAL;
}
+ if (argc == 3) {
+ uint64_t mult;
+ int ret;
+
+ if (!strcmp(argv[1], "msdos")) {
+ printf("gpt-location not supported for msdos partitions\n");
+ return -EINVAL;
+ }
+
+ ret = parted_strtoull(argv[2], &gpt_location, &mult);
+ if (ret)
+ return ret;
+
+ if (!mult)
+ mult = gunit;
+
+ gpt_location *= mult;
+
+ /* Ensure alignment */
+ gpt_location = ALIGN(gpt_location, SECTOR_SIZE);
+ /* convert to LBA */
+ gpt_location >>= SECTOR_SHIFT;
+ }
+
pdesc = partition_table_new(blk, argv[1], gpt_location);
if (IS_ERR(pdesc)) {
printf("Error: Cannot create partition table: %pe\n", pdesc);
@@ -236,7 +260,7 @@ static int do_mklabel(struct block_device *blk, int argc, char *argv[])
partition_table_free(gpdesc);
gpdesc = pdesc;
- return 2;
+ return argc > 2 ? 3 : 2;
}
static int do_refresh(struct block_device *blk, int argc, char *argv[])
@@ -352,7 +376,7 @@ BAREBOX_CMD_HELP_TEXT("GNU Parted")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("commands:")
BAREBOX_CMD_HELP_OPT ("print", "print partitions")
-BAREBOX_CMD_HELP_OPT ("mklabel <type>", "create a new partition table")
+BAREBOX_CMD_HELP_OPT ("mklabel <type> <gpt-location>", "create a new partition table")
BAREBOX_CMD_HELP_OPT ("rm <num>", "remove a partition")
BAREBOX_CMD_HELP_OPT ("mkpart <name> <fstype> <start> <end>", "create a new partition")
BAREBOX_CMD_HELP_OPT ("unit <unit>", "change display/input units")
@@ -361,6 +385,7 @@ BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("<unit> can be one of \"s\" (sectors), \"B\" (bytes), \"kB\", \"MB\", \"GB\", \"TB\",")
BAREBOX_CMD_HELP_TEXT("\"KiB\", \"MiB\", \"GiB\" or \"TiB\"")
BAREBOX_CMD_HELP_TEXT("<type> must be \"gpt\" or \"msdos\"")
+BAREBOX_CMD_HELP_TEXT("<gpt-location> Location of the GPT table, ignored for MBR. Specified in <unit> size.")
BAREBOX_CMD_HELP_TEXT("<fstype> can be one of \"ext2\", \"ext3\", \"ext4\", \"fat16\", \"fat32\" or \"bbenv\"")
BAREBOX_CMD_HELP_TEXT("<name> for MBR partition tables can be one of \"primary\", \"extended\" or")
BAREBOX_CMD_HELP_TEXT("\"logical\". For GPT this is a name string.")
--
2.39.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 3/4] defaultenv-2: add support to automatically create an initial GPT table
2025-04-23 14:09 [PATCH v2 1/4] partitions: efi: add support to specify gpt-location Marco Felsch
2025-04-23 14:09 ` [PATCH v2 2/4] parted: add support for gpt-location Marco Felsch
@ 2025-04-23 14:09 ` Marco Felsch
2025-04-23 14:09 ` [PATCH v2 4/4] ARM: riotboard: drop static barebox-environment handling Marco Felsch
2 siblings, 0 replies; 4+ messages in thread
From: Marco Felsch @ 2025-04-23 14:09 UTC (permalink / raw)
To: barebox
This adds the support to automatically create a minimal GPT table.
gpt-add: A script to add a minimal GPT table compatible to i.MX3/5/6/7
devices. The initial GPT only contains a barebox-environment partition.
The script checks for the bootsource to decide the GPT location.
gpt-rm: A script to remove the GPT header and the protective MBR. The
script checks for the bootsource to decide which GPT should be scrubbed.
bbenv-gpt: An initscript which checks for a barebox-environment
partition if booted from MMC. If no parition was found the script
triggers the gpt-add script.
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
v2:
- new
defaultenv/defaultenv-2-base/bin/gpt-add | 40 +++++++++++++++++++++
defaultenv/defaultenv-2-base/bin/gpt-rm | 10 ++++++
defaultenv/defaultenv-2-base/init/bbenv-gpt | 16 +++++++++
3 files changed, 66 insertions(+)
create mode 100644 defaultenv/defaultenv-2-base/bin/gpt-add
create mode 100644 defaultenv/defaultenv-2-base/bin/gpt-rm
create mode 100644 defaultenv/defaultenv-2-base/init/bbenv-gpt
diff --git a/defaultenv/defaultenv-2-base/bin/gpt-add b/defaultenv/defaultenv-2-base/bin/gpt-add
new file mode 100644
index 000000000000..fa61b1929e20
--- /dev/null
+++ b/defaultenv/defaultenv-2-base/bin/gpt-add
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+bootdev="/dev/${bootsource}${bootsource_instance}"
+yesno=""
+
+if [ ${bootsource} != "mmc" ]; then
+ exit 0
+fi
+
+# If successful, there is already a GPT or MBR header
+# TODO: Add hush redirect support to avoid user confusion
+if parted ${bootdev} print; then
+ exit 0
+fi
+
+readline "Create an initial GPT for barebox? [y|N]: " yesno
+
+# No per default
+if [ "${yesno}" == "" ]; then
+ exit 0
+elif [ "${yesno}" == "n" ]; then
+ exit 0
+fi
+
+# Check for other user input than 'y'
+if [ "${yesno}" != "y" ]; then
+ echo "Unkown value, only 'y' and 'n' are supported"
+ exit 1
+fi
+
+# Make a default GPT table, with the GPT partition entrie array starting at 2MiB
+# to have enough space for barebox at the beginning and to overcome legacy
+# platforms like i.MX3/5/6.
+parted ${bootdev} mklabel gpt 2MiB || gpt-rm
+
+# Add a barebox environment partition with the size of 1MiB which should be
+# enough for development.
+parted ${bootdev} mkpart barebox-environment bbenv 3MiB 4MiB || gpt-rm
+
+parted ${bootdev} refresh
diff --git a/defaultenv/defaultenv-2-base/bin/gpt-rm b/defaultenv/defaultenv-2-base/bin/gpt-rm
new file mode 100644
index 000000000000..e45c79395504
--- /dev/null
+++ b/defaultenv/defaultenv-2-base/bin/gpt-rm
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+bootdev="/dev/${bootsource}${bootsource_instance}"
+
+if [ ${bootsource} != "mmc" ]; then
+ exit 0
+fi
+
+# Remove protective MBR and GPT from boot device
+memset -d ${bootdev} 0x100 0x0 0x300
diff --git a/defaultenv/defaultenv-2-base/init/bbenv-gpt b/defaultenv/defaultenv-2-base/init/bbenv-gpt
new file mode 100644
index 000000000000..6756cac9885e
--- /dev/null
+++ b/defaultenv/defaultenv-2-base/init/bbenv-gpt
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+bbenvpart="/dev/${bootsource}${bootsource_instance}.barebox-environment"
+
+if [ ${bootsource} != "mmc" ]; then
+ exit 0
+fi
+
+# TODO:
+# Add command support to search for a given GPT PartUUID on a block device to
+# be partition name independent.
+if [ -e ${bbenvpart} ]; then
+ exit 0
+fi
+
+gpt-add
--
2.39.5
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 4/4] ARM: riotboard: drop static barebox-environment handling
2025-04-23 14:09 [PATCH v2 1/4] partitions: efi: add support to specify gpt-location Marco Felsch
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 ` Marco Felsch
2 siblings, 0 replies; 4+ messages in thread
From: Marco Felsch @ 2025-04-23 14:09 UTC (permalink / raw)
To: barebox
Since commit 9f868f78bc54 ("environment: use barebox environment from
GPT partitions") barebox can try to find the barebox-environment based
on a GPT partition GUID.
Make use of this mechanism and drop the barebox local of-partition. This
allows the BSP integrators to provide a proper GPT or if no GPT was
provided we fallback to the autom. minimal GPT mechanism added recently.
This also fixes environment handling if booted from a SD card since SD
card boots make use of eMMC barebox environment at the moment.
Signed-off-by: Marco Felsch <m.felsch@pengutronix.de>
---
v2:
- Adapt commit message
arch/arm/dts/imx6s-riotboard.dts | 10 ----------
1 file changed, 10 deletions(-)
diff --git a/arch/arm/dts/imx6s-riotboard.dts b/arch/arm/dts/imx6s-riotboard.dts
index 57817c1197a7..cca34d6dba95 100644
--- a/arch/arm/dts/imx6s-riotboard.dts
+++ b/arch/arm/dts/imx6s-riotboard.dts
@@ -11,11 +11,6 @@
/ {
chosen {
stdout-path = &uart2;
-
- environment {
- compatible = "barebox,environment";
- device-path = &environment_usdhc4;
- };
};
};
@@ -31,11 +26,6 @@ partition@0 {
label = "barebox";
reg = <0x0 0xe0000>;
};
-
- environment_usdhc4: partition@e0000 {
- label = "barebox-environment";
- reg = <0xe0000 0x20000>;
- };
};
&clks {
--
2.39.5
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-04-23 17:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-23 14:09 [PATCH v2 1/4] partitions: efi: add support to specify gpt-location Marco Felsch
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox