Skip to contentSkip to conversation

rpi: copy /chosen/bootloader partition and set boot_partition

PATCH Superseded Gregor Herburger 2 participants 3 messages Aug 3 Broadcom BCM2711/BCM2835 Arm architecture

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rpi: copy /chosen/bootloader partition and set boot_partition

The Raspberry Pi has the "tryboot" feature where it can boot from a different partition enabling failsafe A/B updates. After boot the firmware sets 'partition' property in /chosen/bootloader. Copy this property to the device tree and set the env variable boot_partition. This allows boards to create a bootcmd based on the boot partition.

---
Changes in v2:
- update Reviewed-by tag

---
 board/raspberrypi/rpi/rpi.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
board/raspberrypi/rpi/rpi.c +18 -0
index 1da5df92351c..7a2ff646ac8c 100644
@@ -480,6 +480,20 @@ static void set_serial_number(void)
 	env_set("serial#", serial_string);
 }
 
+static void set_boot_partition_fdt(void)
+{
+	void *fdtp = (void *)gd->fdt_blob;
+	u32 partition;
+
+	partition = fdt_getprop_u32_default(fdtp, "/chosen/bootloader", "partition", 1);
+	if (!partition) {
+		printf("Failed to the get partition property.\n");
+		return;
+	}
+
+	env_set_ulong("boot_partition", partition);
+}
+
 int misc_init_r(void)
 {
 	set_fdt_addr();
@@ -489,6 +503,7 @@ int misc_init_r(void)
 	set_board_info();
 #endif
 	set_serial_number();
+	set_boot_partition_fdt();
 
 	return 0;
 }
@@ -641,6 +656,9 @@ void  update_fdt_from_fw(void *fdt, void *fw_fdt)
 
 	/* copy uart clk as provided by the firmware */
 	copy_property(fdt, fw_fdt, "/clocks/clk-uart", "clock-frequency");
+
+	/* Copy the boot partition */
+	copy_property(fdt, fw_fdt, "/chosen/bootloader", "partition");
 }
 
 int ft_board_setup(void *blob, struct bd_info *bd)

---
base-commit: baa64b2f892890f00a377eac4a3e685472bb56b5
change-id: 20260803-tryboot2-8c5221376e5a

Best regards,

Signature · 1 line
Gregor Herburger <[email protected]>
#1 lore raw

Hi Gregor,

Quoted 11 lines

The Raspberry Pi has the "tryboot" feature where it can boot from a different partition enabling failsafe A/B updates. After boot the firmware sets 'partition' property in /chosen/bootloader. Copy this property to the device tree and set the env variable boot_partition. This allows boards to create a bootcmd based on the boot partition.

---
Changes in v2:
- update Reviewed-by tag

You don't need to do a v2 for a tag, patchwork will deal with that automatically.

I looked at this briefly on a RPi4/5 yesterday and couldn't see the DT bits being passed through to the kernel, purely by a simple test of looking in /proc/device-tree/chosen/ directory and didn't see the bootloader/ directory or added/amended details, where if I boot off the FW DT I see:

# ls /proc/device-tree/chosen/bootloader/
boot-mode build-timestamp  capabilities  name  partition  rsts
tryboot  update-timestamp  version

Not sure if this is the versions of firmware/U-Boot/downstream DT I am using or something else. I wonder also if we should just amend everything in the branch of the DT as no doubt others will want other bits for other features.

Peter

Quoted 56 lines
---
 board/raspberrypi/rpi/rpi.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
board/raspberrypi/rpi/rpi.c +18 -1
index 1da5df92351c..7a2ff646ac8c 100644
@@ -480,6 +480,20 @@ static void set_serial_number(void)
        env_set("serial#", serial_string);
 }
 
+static void set_boot_partition_fdt(void)
+{
+       void *fdtp = (void *)gd->fdt_blob;
+       u32 partition;
+
+       partition = fdt_getprop_u32_default(fdtp, "/chosen/bootloader", "partition", 1);
+       if (!partition) {
+               printf("Failed to the get partition property.\n");
+               return;
+       }
+
+       env_set_ulong("boot_partition", partition);
+}
+
 int misc_init_r(void)
 {
        set_fdt_addr();
@@ -489,6 +503,7 @@ int misc_init_r(void)
        set_board_info();
 #endif
        set_serial_number();
+       set_boot_partition_fdt();
 
        return 0;
 }
@@ -641,6 +656,9 @@ void  update_fdt_from_fw(void *fdt, void *fw_fdt)
 
        /* copy uart clk as provided by the firmware */
        copy_property(fdt, fw_fdt, "/clocks/clk-uart", "clock-frequency");
+
+       /* Copy the boot partition */
+       copy_property(fdt, fw_fdt, "/chosen/bootloader", "partition");
 }
 
 int ft_board_setup(void *blob, struct bd_info *bd)
 
---

base-commit: baa64b2f892890f00a377eac4a3e685472bb56b5
change-id: 20260803-tryboot2-8c5221376e5a

Best regards,
--
Gregor Herburger <[email protected]>

#2 lore raw

Hi Peter,

Quoted 8 lines · Peter Robinson

Hi Gregor,

Quoted 6 lines

The Raspberry Pi has the "tryboot" feature where it can boot from a different partition enabling failsafe A/B updates. After boot the firmware sets 'partition' property in /chosen/bootloader. Copy this property to the device tree and set the env variable boot_partition. This allows boards to create a bootcmd based on the boot partition.

Quoted 8 lines
Quoted 5 lines

---
Changes in v2:
- update Reviewed-by tag

You don't need to do a v2 for a tag, patchwork will deal with that automatically.

Ok.

Quoted 7 lines

I looked at this briefly on a RPi4/5 yesterday and couldn't see the DT bits being passed through to the kernel, purely by a simple test of looking in /proc/device-tree/chosen/ directory and didn't see the bootloader/ directory or added/amended details, where if I boot off the FW DT I see:

Quoted 6 lines
# ls /proc/device-tree/chosen/bootloader/
boot-mode build-timestamp  capabilities  name  partition  rsts
tryboot  update-timestamp  version

Not sure if this is the versions of firmware/U-Boot/downstream DT I am using or something else.

No I just tested it and couldn't see it either. I think it fails silently because of the missing '/chosen/bootloader' node. Don't know how it ever worked. Maybe I had a dts with that node from somewhere.

Quoted 3 lines

I wonder also if we should just amend
everything in the branch of the DT as no doubt others will want other bits for other features.

You mean to copy the complete '/chosen/bootloader' node with all its properties? Yes I think that is a good idea, there seem to be some usable properties.

Here is how it looks on my board:

bootloader {
        boot-mode = <0x00000001>;
        version = "5855b10b2bd22b2a116dc91f6b5b1bc4b2ea849f";
        capabilities = <0x0000007f>;
        update-timestamp = <0x00000000>;
        build-timestamp = <0x684bf1ce>;
        tryboot = <0x00000000>;
        rsts = <0x00001020>;
        partition = <0x00000002>;
        arg1 = <0x00000000>;
        count = <0x00000002>;
};

I will copy the complete node in the next iteration of this patch.

Quoted 51 lines

Peter

Quoted 48 lines
---
 board/raspberrypi/rpi/rpi.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
board/raspberrypi/rpi/rpi.c +18 -0
index 1da5df92351c..7a2ff646ac8c 100644
@@ -480,6 +480,20 @@ static void set_serial_number(void)
        env_set("serial#", serial_string);
 }
 
+static void set_boot_partition_fdt(void)
+{
+       void *fdtp = (void *)gd->fdt_blob;
+       u32 partition;
+
+       partition = fdt_getprop_u32_default(fdtp, "/chosen/bootloader", "partition", 1);
+       if (!partition) {
+               printf("Failed to the get partition property.\n");
+               return;
+       }
+
+       env_set_ulong("boot_partition", partition);
+}
+
 int misc_init_r(void)
 {
        set_fdt_addr();
@@ -489,6 +503,7 @@ int misc_init_r(void)
        set_board_info();
 #endif
        set_serial_number();
+       set_boot_partition_fdt();
 
        return 0;
 }
@@ -641,6 +656,9 @@ void  update_fdt_from_fw(void *fdt, void *fw_fdt)
 
        /* copy uart clk as provided by the firmware */
        copy_property(fdt, fw_fdt, "/clocks/clk-uart", "clock-frequency");
+
+       /* Copy the boot partition */
+       copy_property(fdt, fw_fdt, "/chosen/bootloader", "partition");
 }
 
 int ft_board_setup(void *blob, struct bd_info *bd)
Quoted 8 lines
Quoted 8 lines

---
base-commit: baa64b2f892890f00a377eac4a3e685472bb56b5
change-id: 20260803-tryboot2-8c5221376e5a

Best regards,
--
Gregor Herburger <[email protected]>

Best regards
Gregor

Signature · 11 lines
Gregor Herburger
Linutronix GmbH | Bahnhofstrasse 3 | D-88690 Uhldingen-Mühlhofen
Phone: +49 7556 25 999 35; Fax.: +49 7556 25 999 99

Hinweise zum Datenschutz finden Sie hier (Informations on data privacy 
can be found here): https://linutronix.de/legal/data-protection.php

Linutronix GmbH | Firmensitz (Registered Office): Uhldingen-Mühlhofen | 
Registergericht (Registration Court): Amtsgericht Freiburg i.Br., HRB700 
806 | Geschäftsführer (Managing Directors): Dr. Wilfried Wessner, 
Katharina Kopp, Alexander Gieringer