[PATCH v2 3/4] defaultenv-2: add support to automatically create an initial GPT table

Marco Felsch m.felsch at pengutronix.de
Wed Apr 23 07:09:12 PDT 2025


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 at 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




More information about the barebox mailing list