On 09/09/2025 at 03:44, [email protected] wrote:

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.

I guess you're right, better stay on the safe side and let the user take responsability for breaking something with --force.

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.

It would not be very complicated to check if the actual GPT layout is compatible with U-Boot and if the old SPL signature at offset 8KiB can be erased safely (not tested much):

# return the decimal value of a GPT header field
# offset=$1 length=$2
read_gpt_header_value() {
        od --address-radix=n --endian=little --skip-bytes $(($1+512)) \
           --read-bytes $2 --format=d$2 "$DEV" | tr -d ' '
}

gpt_offset=$(($(read_gpt_header_value 72 8)*512))
gpt_size=$(($(read_gpt_header_value 80 4)*$(read_gpt_header_value 84 4)))
u_boot_offset=$(($(echo $wroff | sed -Ee 's/K/*1024/')))
u_boot_size=$(stat --format=%s "$imfile")

# check if U-Boot would overlap with GPT
if [ $u_boot_offset -lt $((gpt_offset + gpt_size)) ] && \
   [ $gpt_offset -lt $((u_boot_offset + u_boot_size)) ]; then
        echo >&2 "$0: cannot install U-Boot, it would overwrite GPT"
        exit 1
# check if old U-Boot signature at offset 8K overlaps GPT
elif [ "$disable8k" = "y" ] && [ $gpt_offset -le 8192 ] && \
     [ $((gpt_offset + gpt_size)) -gt 8192 ]; then
        echo >&2 "$0: cannot disable old U-Boot, it would overwrite GPT"
        exit 1
fi

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.

Be aware that the first partition may have been created to reserve space for U-Boot, so that it is not used by other "real" partitions. This is what trixie installer guided partitioning does on arm64 because Rockchip U-Boot require it, although this partition typically starts at 1MiB so it should not overlap with sunxi U-Boot. Additionally, if you consider doing the same with u-boot-install-rockchip script, the default partition map for Rockchip SoCs defines several partitions in the 0-16MiB area used by U-Boot [1].

[1] <https://opensource.rock-chips.com/wiki_Partitions>

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

I haven't tested it but LGTM.

Reply via email to