On 02/09/2025 22:38, Pascal Hambourg wrote:
> Should the combination of GPT + boot loader found at offset 8KiB really 
> be a blocker here ? I see two cases:
> - GPT and boot loader overlap (e.g. standard GPT), the primary GPT is 
> invalid, partitioning tools would rewrite it from the backup GPT and 
> overwrite the boot loader.
> - GPT and boot loader do not overlap (non-standard GPT, e.g. partition 
> entries <= 56 or primary GPT at offset ~1MiB).
> So IMO 'eGON.BT0' can safely be deleted in both cases.


Hi Pascal,

That's sound reasoning, but I like the policy of not writing to an area 
belonging to something else, even if the write would do no harm.

(Out of curiosity, I found that fdisk and gdisk did allow me to set the 
type of partition 57 to 2A2A2A2A-4765-4E4F-2E42-54302A2A2A2A, so a GPT 
really could contain 'eGON.BT0' at offset 8196, though implausibly.)

One restriction I should have made earlier is that the script will only 
work with a GPT that has the typical layout (occupying the 0-17K area). 
This is in order to keep the script very simple without making it less 
safe than before.

A future version of u-boot-install-sunxi, maybe rewritten in a different 
language, could be more thorough: work with any MBR or GPT, parse them 
to locate the first partition, select a suitable location for U-Boot and 
ensure there was room before installing it.

Another revised patch is below; please would you take a look?

Thanks and best wishes,
Harold.


diff --git a/debian/bin/u-boot-install-sunxi b/debian/bin/u-boot-install-sunxi
index fca4ddc436..64a07242ff 100755
--- a/debian/bin/u-boot-install-sunxi
+++ b/debian/bin/u-boot-install-sunxi
@@ -79,6 +79,33 @@ if [ ! -w "$DEV" ] && [ -z "$FORCE" ]; then
     exit 1
 fi
 
+board=$(basename "$TARGET")
+echo >&2 "Selected board: ${board}"
+
+# Select write offset based on board
+case "$board" in
+       # H2+ or H3 SoC
+       "nanopi_neo" | "nanopi_neo_air" | "orangepi_pc_plus" | "orangepi_plus" 
| "orangepi_zero" )
+               wroff="128K" ;;
+       # A64, H5 or H6 SoC
+       "a64-olinuxino" | "a64-olinuxino-emmc" | "nanopi_neo2" | 
"nanopi_neo_plus2" |\
+       "orangepi_one_plus" | "orangepi_zero_plus2" | "pine64-lts" | 
"pine64_plus" |\
+       "pinebook" | "pinephone" | "pinetab" | "sopine_baseboard" | "teres_i" )
+               wroff="128K" ;;
+       # Older SoC
+       *)
+               wroff="8K" ;;
+esac
+echo >&2 "Selected offset: ${wroff}"
+
+# Check for any other boot program that would take precedence
+if [ "$wroff" != "8K" ] && printf 'eGON.BT0' | cmp -s -i 0:8196 -n 8 - "$DEV"; 
then
+       echo >&2 "Found boot program in ${DEV} at offset 8K, needs disabling"
+       disable8k="y"
+else
+       disable8k="n"
+fi
+
 if [ -z "$FORCE" ]; then
     # A very simple sanity check.  GPT mandates a "protective MBR" so this 
works
     # even with GPT partitioning.
@@ -87,10 +114,18 @@ if [ -z "$FORCE" ]; then
        exit 1
     fi
 
-    # But, on sunxi64, spl will trample upon GPT.
+    # Detect GPT
     if printf 'EFI PART' | cmp -s -i 0:512 -n 8 - "$DEV"; then
-       echo >&2 "$0: device/image ($DEV) uses GPT partition table, unusable on 
sunxi64"
-       exit 1
+        # Only work with conventional layout (occupying first 17K)
+        if ! printf '\2\0\0\0\0\0\0\0\200\0\0\0\200\0\0\0' | cmp -s -i 0:584 
-n 16 - "$DEV"; then
+            echo >&2 "$0: device/image ($DEV) has unconventional GPT layout, 
might overwrite it"
+            exit 1
+        fi
+        # Writing at offset 8K will trample upon GPT.
+        if [ "$wroff" = "8K" ] || [ "$disable8k" = "y" ]; then
+            echo >&2 "$0: device/image ($DEV) uses GUID partition table, would 
overwrite it"
+            exit 1
+        fi
     fi
 fi
 
@@ -100,5 +135,10 @@ if [ ! -f "$imfile" ]; then
     exit 1
 fi
 
-echo "Writing U-Boot image ${imfile} to ${DEV}"
-dd conv=fsync,notrunc if="$imfile" of="$DEV" bs=8K seek=1
+if [ "$disable8k" = "y" ]; then
+       echo >&2 "Disabling boot program in ${DEV} at offset 8K"
+       printf 'DISUSED!' | dd conv=fsync,notrunc of="$DEV" bs=8196 seek=1
+fi
+
+echo >&2 "Writing U-Boot image ${imfile} to ${DEV} at offset ${wroff}"
+dd conv=fsync,notrunc if="$imfile" of="$DEV" bs="$wroff" seek=1
diff --git a/debian/manpages/u-boot-install-sunxi.8 
b/debian/manpages/u-boot-install-sunxi.8
index 744679ec78..6e88b70fd2 100644
--- a/debian/manpages/u-boot-install-sunxi.8
+++ b/debian/manpages/u-boot-install-sunxi.8
@@ -35,6 +35,5 @@ environment variable is set, its contents are used instead.
 Skip partition table sanity checks.  Usually, a MBR partition table is
 required (so u-boot has something to work with), but in rare setups you
 may put the data on another disk.  Likewise, GPT partition tables are
-incompatible with the layout used on sunxi devices (spl is written at
-offset 16384 while GPT occupies bytes [512..33280) ) but this option lets
+incompatible with U-Boot's location in some cases, but this option lets
 you trample upon them anyway.

Reply via email to