Skip to contentSkip to conversation

sunxi: Support automated booting from 128KB

PATCH New Andre Przywara 2 participants 4 messages Jan 10, 2020 – Jan 26, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover sunxi: Support automated booting from 128KB

The Allwinner Boot ROM on all later SoCs can load the initial SPL code from offset 128KB or from offset 8KB of an SD card or eMMC. We support this in the SPL for a while now, but so far needed to manually adjust the U-Boot image MMC load sector during compile time.

Since the Boot ROM writes a different boot source ID into the SRAM when loaded from the higher offset, we can check this value and dynamically adjust the raw MMC load sector for the U-Boot proper image.

This allows to generate *one* image file, which can be written to either offset 8KB or to offset 128KB. The latter has the advantange of not overlapping with a standard GPT partition table.

Tested on Bananapi M2 Berry (R40), Orangepi Zero (H2+), Orangepi PC 2 (H5), Pine64-LTS (A64), Bananapi-M64 (A64, both SD card and eMMC) and Pine H64 (H6), on all boards writing the same image to both 8K and 128K.

Cheers,
Andre.

Andre Przywara (2):
sunxi: SPL: Factor out sunxi_get_boot_source()
sunxi: Automate loading from 128KB MMC offset

 arch/arm/mach-sunxi/board.c | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)
Signature · 1 line
2.14.5
#1 lore raw

PATCH 1/2 sunxi: SPL: Factor out sunxi_get_boot_source()

The Boot ROM write some boot source ID (SD card, eMMC, SPI, ...) into a certain location in SRAM, so the SPL can easily determine where to load U-Boot proper from.
Factor out reading this value, as it will come in handy again shortly.

---
 arch/arm/mach-sunxi/board.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
arch/arm/mach-sunxi/board.c +13 -5
index aa1d2230c9..9c0cf718b8 100644
@@ -221,12 +221,22 @@ void s_init(void)
 	eth_init_board();
 }
 
+#define SUNXI_INVALID_BOOT_SOURCE	-1
+
+static int sunxi_get_boot_source(void)
+{
+	if (!is_boot0_magic(SPL_ADDR + 4)) /* eGON.BT0 */
+		return SUNXI_INVALID_BOOT_SOURCE;
+
+	return readb(SPL_ADDR + 0x28);
+}
+
 /* The sunxi internal brom will try to loader external bootloader
  * from mmc0, nand flash, mmc2.
  */
 uint32_t sunxi_get_boot_device(void)
 {
-	int boot_source;
+	int boot_source = sunxi_get_boot_source();
 
 	/*
 	 * When booting from the SD card or NAND memory, the "eGON.BT0"
@@ -244,11 +254,9 @@ uint32_t sunxi_get_boot_device(void)
 	 * binary over USB. If it is found, it determines where SPL was
 	 * read from.
 	 */
-	if (!is_boot0_magic(SPL_ADDR + 4)) /* eGON.BT0 */
-		return BOOT_DEVICE_BOARD;
-
-	boot_source = readb(SPL_ADDR + 0x28);
 	switch (boot_source) {
+	case SUNXI_INVALID_BOOT_SOURCE:
+		return BOOT_DEVICE_BOARD;
 	case SUNXI_BOOTED_FROM_MMC0:
 	case SUNXI_BOOTED_FROM_MMC0_HIGH:
 		return BOOT_DEVICE_MMC1;
Signature · 1 line
2.14.5
#2 lore raw

PATCH 2/2 sunxi: Automate loading from 128KB MMC offset

In reply to Andre Przywara

Since commit 067e0b9684d4 ("sunxi: Allow booting from 128KB SD/eMMC offset") we support having the SPL loaded from either the traditional 8KB SD card/eMMC offset, or from the alternative location at 128KB. However the sector to find the U-Boot image was still hard-coded at compile time, and had to be adjusted for one of the two choices.

Since we can actually override the function to return the sector offset, we can just check the boot source byte there to select the proper offset based on from where the SPL was loaded.

This allows the very same binary image to be loaded from either 128KB or 8KB, with the U-Boot proper image always being located just behind the SPL.

---
 arch/arm/mach-sunxi/board.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/arm/mach-sunxi/board.c +20 -0
index 9c0cf718b8..b487b265af 100644
@@ -274,6 +274,26 @@ uint32_t sunxi_get_boot_device(void)
 }
 
 #ifdef CONFIG_SPL_BUILD
+/*
+ * The eGON SPL image can be located at 8KB or at 128KB into an SD card or
+ * an eMMC device. The boot source has bit 4 set in the latter case.
+ * By adding 120KB to the normal offset when booting from a "high" location
+ * we can support both cases.
+ */
+unsigned long spl_mmc_get_uboot_raw_sector(struct mmc *mmc)
+{
+	unsigned long sector = CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_SECTOR;
+
+	switch (sunxi_get_boot_source()) {
+	case SUNXI_BOOTED_FROM_MMC0_HIGH:
+	case SUNXI_BOOTED_FROM_MMC2_HIGH:
+		sector += (128 - 8) * 2;
+		break;
+	}
+
+	return sector;
+}
+
 u32 spl_boot_device(void)
 {
 	return sunxi_get_boot_device();
Signature · 1 line
2.14.5
#3 lore raw

cover sunxi: Support automated booting from 128KB

In reply to Andre Przywara
Quoted 18 lines · Andre Przywara

The Allwinner Boot ROM on all later SoCs can load the initial SPL code from offset 128KB or from offset 8KB of an SD card or eMMC. We support this in the SPL for a while now, but so far needed to manually adjust the U-Boot image MMC load sector during compile time.

Since the Boot ROM writes a different boot source ID into the SRAM when loaded from the higher offset, we can check this value and dynamically adjust the raw MMC load sector for the U-Boot proper image.

This allows to generate *one* image file, which can be written to either offset 8KB or to offset 128KB. The latter has the advantange of not overlapping with a standard GPT partition table.

Tested on Bananapi M2 Berry (R40), Orangepi Zero (H2+), Orangepi PC 2 (H5), Pine64-LTS (A64), Bananapi-M64 (A64, both SD card and eMMC) and Pine H64 (H6), on all boards writing the same image to both 8K and 128K.

Quoted 6 lines

Cheers,
Andre.

Andre Przywara (2):
sunxi: SPL: Factor out sunxi_get_boot_source()
sunxi: Automate loading from 128KB MMC offset

Applied to u-boot-sunxi/master