From ecf4f7c934e59c4a4b086d85fd7f85b4420f666a Mon Sep 17 00:00:00 2001
From: Andreas Gnau <andreas.gnau@iopsys.eu>
Date: Thu, 16 Jan 2025 15:18:17 +0100
Subject: [PATCH] airoha: an7581: Create FIP partition in software

The Airoha Boot ROM reads the FIP from 2 KB offset. The FIP goes until
512KB. Create a 510 KB partition in software only using the blkpg-part
utility, because this partition cannot be stored in the GPT. The reason
for this is that in GPT all partitions must start after the PGPT
entries, but the GPT entries (16 KB) cannot be put right after GPT
header at 1KB, because the boot ROM expects FIP to be at 2 KB.

Signed-off-by: Andreas Gnau <andreas.gnau@iopsys.eu>
---
 airoha/an7581/target.mk                       |  2 ++
 .../etc/hotplug.d/block/20-emmc-fip-partition | 26 +++++++++++++++++++
 2 files changed, 28 insertions(+)
 create mode 100644 airoha/base-files/etc/hotplug.d/block/20-emmc-fip-partition

diff --git a/airoha/an7581/target.mk b/airoha/an7581/target.mk
index bbd0d419d..e600d96f1 100644
--- a/airoha/an7581/target.mk
+++ b/airoha/an7581/target.mk
@@ -9,6 +9,8 @@ CPU_TYPE:=cortex-a53
 FEATURES+=squashfs nand ramdisk ubifs
 KERNELNAME:=vmlinux
 
+DEFAULT_PACKAGES+=blkpg-part
+
 define Target/Description
 	Build firmware images for Airoha an7581 ARM based boards.
 endef
diff --git a/airoha/base-files/etc/hotplug.d/block/20-emmc-fip-partition b/airoha/base-files/etc/hotplug.d/block/20-emmc-fip-partition
new file mode 100644
index 000000000..bdcc241b2
--- /dev/null
+++ b/airoha/base-files/etc/hotplug.d/block/20-emmc-fip-partition
@@ -0,0 +1,26 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (C) 2025 IOPSYS Software Solutions AB
+
+FIP_PARTITION_NUMBER=64
+FIP_PARTITION_LABEL=fip
+
+if [ "$DEVTYPE" = disk ] && [ "$DEVICENAME" = mmcblk0 ]; then
+	case "$ACTION" in
+	add)
+		# The Airoha Boot ROM reads FIP from 2 KB offset. FIP goes until 512KB
+		# Create a 510 KB partition in software only, because this partition
+		# cannot be stored in the GPT. The reason for this is that in GPT all
+		# partitions must start after the PGPT entries, but the GPT entries (16 KB)
+		# cannot be put right after GPT header at 1KB, because the boot ROM
+		# expects FIP to be at 2 KB.
+		blkpg-part add -l "$FIP_PARTITION_LABEL" "/dev/$DEVICENAME" "$FIP_PARTITION_NUMBER" 2048 522240
+		# While the kernel UAPI has a field for the partition label and
+		# we set it, there exists no code in the kernel that makes use of it.
+		# Create a symlink as a workaround while kernel is lacking the functionality
+		ln -sf "/dev/${DEVICENAME}p${FIP_PARTITION_NUMBER}" "/dev/${DEVICENAME}pfip"
+		;;
+	remove)
+		rm "/dev/${DEVICENAME}pfip"
+		;;
+	esac
+fi
-- 
GitLab