Skip to contentSkip to conversation

firmware: raspberrypi: Add support for the tryboot mode

PATCH RESEND Superseded Gregor Herburger 3 participants 11 messages Sep 3

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover firmware: raspberrypi: Add support for the tryboot mode

This adds support for the tryboot mode on Raspberry Pis. As there is no documentation other than the downstream implementation [0] the implementation is based on this.

I tested this on Raspberry Pi 5 and therefore I only added the properties to this devicetree. But afaik this should work on all Raspberry Pis. I will add it to the correspondings dts if I get some hardware to test it.

[0] https://github.com/raspberrypi/linux/commit/eb56da0c1925c07e8929ce4c9fe8aeafa7cb8c7b

---
Changes in v3:
- fix use-after-free error by using devm_add_action_or_reset
- ensure endianness of magic byte
- refactor reboot mode registration into separate function
- Link to v2: https://patch.msgid.link/[email protected]

Changes in v2:
- Remove unnecessary reboot_mode_unregister().
- dt-binding: restrict to mode-{normal,tryboot} and only allow 32bit value.
- Link to v1: https://patch.msgid.link/[email protected]

---
Gregor Herburger (4):
firmware: raspberrypi: reorder rpi_firmware_property_tag enum
dt-bindings: raspberrypi,bcm2835-firmware: Include 'reboot-mode.yaml'
firmware: raspberrypi: Add reboot mode support
arm64: dts: broadcom: bcm2712: Add reboot modes to firmware node

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml      |  9 +++++
 .../boot/dts/broadcom/bcm2712-rpi-5-b-base.dtsi    |  2 ++
 drivers/firmware/Kconfig                           |  2 ++
 drivers/firmware/raspberrypi.c                     | 40 +++++++++++++++++++---
 include/soc/bcm2835/raspberrypi-firmware.h         | 22 ++++++------
 5 files changed, 60 insertions(+), 15 deletions(-)

---
base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6
change-id: 20260623-rpi-tryboot-4292c92b0727

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

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

PATCH RESEND v3 1/4 firmware: raspberrypi: reorder rpi_firmware_property_tag enum

The enum was once ordered by tag values. The later added tags were added in a different order. Reorder the tags again.

No functional change intended.

---
 include/soc/bcm2835/raspberrypi-firmware.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
include/soc/bcm2835/raspberrypi-firmware.h +10 -10
index e1f87fbfe5542..66cc5a426c3c5 100644
@@ -72,26 +72,26 @@ enum rpi_firmware_property_tag {
 	RPI_FIRMWARE_GET_EDID_BLOCK =                         0x00030020,
 	RPI_FIRMWARE_GET_CUSTOMER_OTP =                       0x00030021,
 	RPI_FIRMWARE_GET_DOMAIN_STATE =                       0x00030030,
+	RPI_FIRMWARE_GET_GPIO_STATE =                         0x00030041,
+	RPI_FIRMWARE_GET_GPIO_CONFIG =                        0x00030043,
+	RPI_FIRMWARE_GET_PERIPH_REG =                         0x00030045,
 	RPI_FIRMWARE_GET_THROTTLED =                          0x00030046,
 	RPI_FIRMWARE_GET_CLOCK_MEASURED =                     0x00030047,
 	RPI_FIRMWARE_NOTIFY_REBOOT =                          0x00030048,
+	RPI_FIRMWARE_GET_POE_HAT_VAL =                        0x00030049,
+	RPI_FIRMWARE_SET_POE_HAT_VAL =                        0x00030050,
+	RPI_FIRMWARE_NOTIFY_XHCI_RESET =                      0x00030058,
+	RPI_FIRMWARE_NOTIFY_DISPLAY_DONE =                    0x00030066,
 	RPI_FIRMWARE_SET_CLOCK_STATE =                        0x00038001,
 	RPI_FIRMWARE_SET_CLOCK_RATE =                         0x00038002,
 	RPI_FIRMWARE_SET_VOLTAGE =                            0x00038003,
 	RPI_FIRMWARE_SET_TURBO =                              0x00038009,
 	RPI_FIRMWARE_SET_CUSTOMER_OTP =                       0x00038021,
 	RPI_FIRMWARE_SET_DOMAIN_STATE =                       0x00038030,
-	RPI_FIRMWARE_GET_GPIO_STATE =                         0x00030041,
 	RPI_FIRMWARE_SET_GPIO_STATE =                         0x00038041,
 	RPI_FIRMWARE_SET_SDHOST_CLOCK =                       0x00038042,
-	RPI_FIRMWARE_GET_GPIO_CONFIG =                        0x00030043,
 	RPI_FIRMWARE_SET_GPIO_CONFIG =                        0x00038043,
-	RPI_FIRMWARE_GET_PERIPH_REG =                         0x00030045,
 	RPI_FIRMWARE_SET_PERIPH_REG =                         0x00038045,
-	RPI_FIRMWARE_GET_POE_HAT_VAL =                        0x00030049,
-	RPI_FIRMWARE_SET_POE_HAT_VAL =                        0x00030050,
-	RPI_FIRMWARE_NOTIFY_XHCI_RESET =                      0x00030058,
-	RPI_FIRMWARE_NOTIFY_DISPLAY_DONE =                    0x00030066,
 
 	/* Dispmanx TAGS */
 	RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE =                   0x00040001,
@@ -107,7 +107,6 @@ enum rpi_firmware_property_tag {
 	RPI_FIRMWARE_FRAMEBUFFER_GET_PALETTE =                0x0004000b,
 	RPI_FIRMWARE_FRAMEBUFFER_GET_TOUCHBUF =               0x0004000f,
 	RPI_FIRMWARE_FRAMEBUFFER_GET_GPIOVIRTBUF =            0x00040010,
-	RPI_FIRMWARE_FRAMEBUFFER_RELEASE =                    0x00048001,
 	RPI_FIRMWARE_FRAMEBUFFER_TEST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
 	RPI_FIRMWARE_FRAMEBUFFER_TEST_VIRTUAL_WIDTH_HEIGHT =  0x00044004,
 	RPI_FIRMWARE_FRAMEBUFFER_TEST_DEPTH =                 0x00044005,
@@ -117,6 +116,7 @@ enum rpi_firmware_property_tag {
 	RPI_FIRMWARE_FRAMEBUFFER_TEST_OVERSCAN =              0x0004400a,
 	RPI_FIRMWARE_FRAMEBUFFER_TEST_PALETTE =               0x0004400b,
 	RPI_FIRMWARE_FRAMEBUFFER_TEST_VSYNC =                 0x0004400e,
+	RPI_FIRMWARE_FRAMEBUFFER_RELEASE =                    0x00048001,
 	RPI_FIRMWARE_FRAMEBUFFER_SET_PHYSICAL_WIDTH_HEIGHT =  0x00048003,
 	RPI_FIRMWARE_FRAMEBUFFER_SET_VIRTUAL_WIDTH_HEIGHT =   0x00048004,
 	RPI_FIRMWARE_FRAMEBUFFER_SET_DEPTH =                  0x00048005,
@@ -125,10 +125,10 @@ enum rpi_firmware_property_tag {
 	RPI_FIRMWARE_FRAMEBUFFER_SET_VIRTUAL_OFFSET =         0x00048009,
 	RPI_FIRMWARE_FRAMEBUFFER_SET_OVERSCAN =               0x0004800a,
 	RPI_FIRMWARE_FRAMEBUFFER_SET_PALETTE =                0x0004800b,
-	RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF =               0x0004801f,
-	RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF =            0x00048020,
 	RPI_FIRMWARE_FRAMEBUFFER_SET_VSYNC =                  0x0004800e,
 	RPI_FIRMWARE_FRAMEBUFFER_SET_BACKLIGHT =              0x0004800f,
+	RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF =               0x0004801f,
+	RPI_FIRMWARE_FRAMEBUFFER_SET_GPIOVIRTBUF =            0x00048020,
 
 	RPI_FIRMWARE_VCHIQ_INIT =                             0x00048010,
 
Signature · 1 line
2.47.3
#2 lore raw

PATCH RESEND v3 2/4 dt-bindings: raspberrypi,bcm2835-firmware: Include 'reboot-mode.yaml'

In reply to Gregor Herburger

The Raspberry Pi firmware allows to set a reboot mode called tryboot that allows to try booting from a different partition to allow updating of the boot partition. Allow reboot mode properties by referencing the reboot-mode schema. The firmware allows a 32bit value to be sent as reboot flag so restrict the maxItems to 1.

---
 .../bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml           | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +9 -0
index 983ea80eaec97..28c5713860468 100644
@@ -133,6 +133,15 @@ properties:
     required:
       - compatible
 
+  mode-normal:
+    maxItems: 1
+
+  mode-tryboot:
+    maxItems: 1
+
+allOf:
+  - $ref: /schemas/power/reset/reboot-mode.yaml#
+
 required:
   - compatible
   - mboxes
Signature · 1 line
2.47.3
#3 lore raw

PATCH RESEND v3 3/4 firmware: raspberrypi: Add reboot mode support

In reply to Gregor Herburger

The Raspberry Pi firmware has a tryboot mode where it tries to boot from another partition. This can be used to create a A/B update scheme.

To enable this on the next boot, the RPI_FIRMWARE_SET_REBOOT_FLAGS message must be sent to the firmware. Add support for this by registering a reboot mode driver.

Furthermore, safely registering the reboot mode requires adjusting the teardown sequence. Replace the manually called rpi_firmware_put() with a devres-managed called (devm_add_action_or_reset). Without this, the cleanup function of devm_reboot_mode_register() would trigger a use-after-free error by attempting to access the firmware context after it had already been freed by rpi_firmware_remove().

---
 drivers/firmware/Kconfig                   |  2 ++
 drivers/firmware/raspberrypi.c             | 40 ++++++++++++++++++++++++++----
 include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
 3 files changed, 39 insertions(+), 5 deletions(-)
drivers/firmware/Kconfig +2 -0
index bbd2155d84838..34605bf7c9734 100644
@@ -115,6 +115,8 @@ config ISCSI_IBFT
 config RASPBERRYPI_FIRMWARE
 	tristate "Raspberry Pi Firmware Driver"
 	depends on BCM2835_MBOX
+	select POWER_RESET
+	select REBOOT_MODE
 	help
 	  This option enables support for communicating with the firmware on the
 	  Raspberry Pi.
drivers/firmware/raspberrypi.c +35 -5
index 0aa322e9a2e73..2a0c40b8052e5 100644
@@ -14,6 +14,7 @@
 #include <linux/of.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
+#include <linux/reboot-mode.h>
 #include <linux/slab.h>
 #include <soc/bcm2835/raspberrypi-firmware.h>
 
@@ -29,6 +30,7 @@ struct rpi_firmware {
 	struct mbox_client cl;
 	struct mbox_chan *chan; /* The property channel. */
 	struct completion c;
+	struct reboot_mode_driver reboot_mode;
 	u32 enabled;
 
 	struct kref consumers;
@@ -273,10 +275,37 @@ static void devm_rpi_firmware_put(void *data)
 	rpi_firmware_put(fw);
 }
 
+static int rpi_firmware_reboot_mode_write(struct reboot_mode_driver *reboot,
+					  unsigned int magic)
+{
+	struct rpi_firmware *fw = container_of(reboot, struct rpi_firmware,
+					       reboot_mode);
+	__le32 fw_magic = cpu_to_le32(magic);
+
+	if (!magic)
+		return 0;
+
+	return rpi_firmware_property(fw, RPI_FIRMWARE_SET_REBOOT_FLAGS,
+				     &fw_magic, sizeof(fw_magic));
+}
+
+static void rpi_register_reboot_mode(struct device *dev, struct rpi_firmware *fw)
+{
+	int ret;
+
+	fw->reboot_mode.dev = dev;
+	fw->reboot_mode.write = rpi_firmware_reboot_mode_write;
+	ret = devm_reboot_mode_register(dev, &fw->reboot_mode);
+	if (ret)
+		dev_err(dev, "Failed to register reboot mode: %d\n", ret);
+
+}
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct rpi_firmware *fw;
+	int ret;
 
 	/*
 	 * Memory will be freed by rpi_firmware_delete() once all users have
@@ -292,7 +321,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
 
 	fw->chan = mbox_request_channel(&fw->cl, 0);
 	if (IS_ERR(fw->chan)) {
-		int ret = PTR_ERR(fw->chan);
+		ret = PTR_ERR(fw->chan);
 		kfree(fw);
 		return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
 	}
@@ -302,9 +331,14 @@ static int rpi_firmware_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, fw);
 
+	ret = devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw);
+	if (ret)
+		return ret;
+
 	rpi_firmware_print_firmware_revision(fw);
 	rpi_register_hwmon_driver(dev, fw);
 	rpi_register_clk_driver(dev);
+	rpi_register_reboot_mode(dev, fw);
 
 	return 0;
 }
@@ -321,14 +355,10 @@ static void rpi_firmware_shutdown(struct platform_device *pdev)
 
 static void rpi_firmware_remove(struct platform_device *pdev)
 {
-	struct rpi_firmware *fw = platform_get_drvdata(pdev);
-
 	platform_device_unregister(rpi_hwmon);
 	rpi_hwmon = NULL;
 	platform_device_unregister(rpi_clk);
 	rpi_clk = NULL;
-
-	rpi_firmware_put(fw);
 }
 
 static const struct of_device_id rpi_firmware_of_match[] = {
include/soc/bcm2835/raspberrypi-firmware.h +2 -0
index 66cc5a426c3c5..f905bff0fb3ea 100644
@@ -81,6 +81,7 @@ enum rpi_firmware_property_tag {
 	RPI_FIRMWARE_GET_POE_HAT_VAL =                        0x00030049,
 	RPI_FIRMWARE_SET_POE_HAT_VAL =                        0x00030050,
 	RPI_FIRMWARE_NOTIFY_XHCI_RESET =                      0x00030058,
+	RPI_FIRMWARE_GET_REBOOT_FLAGS =                       0x00030064,
 	RPI_FIRMWARE_NOTIFY_DISPLAY_DONE =                    0x00030066,
 	RPI_FIRMWARE_SET_CLOCK_STATE =                        0x00038001,
 	RPI_FIRMWARE_SET_CLOCK_RATE =                         0x00038002,
@@ -92,6 +93,7 @@ enum rpi_firmware_property_tag {
 	RPI_FIRMWARE_SET_SDHOST_CLOCK =                       0x00038042,
 	RPI_FIRMWARE_SET_GPIO_CONFIG =                        0x00038043,
 	RPI_FIRMWARE_SET_PERIPH_REG =                         0x00038045,
+	RPI_FIRMWARE_SET_REBOOT_FLAGS =                       0x00038064,
 
 	/* Dispmanx TAGS */
 	RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE =                   0x00040001,
Signature · 1 line
2.47.3
#4 lore raw

Re: [PATCH RESEND v3 3/4] firmware: raspberrypi: Add reboot mode support

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Use-after-free race condition if devm_add_action_or_reset() fails after drvdata is set. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Use-after-free race condition if devm_add_action_or_reset() fails after drvdata is set. --

commit ea24b0bc9b26402781f7da38607787a0f6b47e39
Author: Gregor Herburger <[email protected]>

firmware: raspberrypi: Add reboot mode support

The Raspberry Pi firmware has a tryboot mode where it tries to boot from another partition. This can be used to create a A/B update scheme.

To enable this on the next boot, the RPI_FIRMWARE_SET_REBOOT_FLAGS message must be sent to the firmware. Add support for this by registering a reboot mode driver.

Quoted 4 lines
drivers/firmware/raspberrypi.c +0 -0
index 0aa322e9a2e73..2a0c40b8052e5 100644

[ ... ]

drivers/firmware/raspberrypi.c static int rpi_firmware_probe(struct platform_device *pdev) · line 336

Quoted 7 lines
drivers/firmware/raspberrypi.c +3 -0
@@ -302,9 +331,14 @@ static int rpi_firmware_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, fw);
 
+	ret = devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw);
+	if (ret)
+		return ret;

[Severity: High]
If devm_add_action_or_reset() fails here, could this create a use-after-free race condition?

When the failure occurs, devm_add_action_or_reset() immediately executes devm_rpi_firmware_put(), which drops the refcount and frees the fw context. Because platform_set_drvdata() was already called, the driver data will point to this freed memory until the core probe cleanup mechanism eventually clears it.

During this window, a consumer driver could call rpi_firmware_get(), which performs a lockless lookup of drvdata and attempts to dereference the freed pointer:

drivers/firmware/raspberrypi.c:rpi_firmware_get() {
    ...
	fw = platform_get_drvdata(pdev);
	if (!fw)
		goto err_put_device;

	if (!kref_get_unless_zero(&fw->consumers))
		goto err_put_device;
    ...
}
Quoted 8 lines
drivers/firmware/raspberrypi.c +2 -0
+
 	rpi_firmware_print_firmware_revision(fw);
 	rpi_register_hwmon_driver(dev, fw);
 	rpi_register_clk_driver(dev);
+	rpi_register_reboot_mode(dev, fw);
 
 	return 0;
 }

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#5 lore raw

Re: [PATCH RESEND v3 3/4] firmware: raspberrypi: Add reboot mode support

Quoted 33 lines · On Thu, Sep 03, 2026 at 03:08:44PM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Use-after-free race condition if devm_add_action_or_reset() fails after drvdata is set. --

commit ea24b0bc9b26402781f7da38607787a0f6b47e39
Author: Gregor Herburger <[email protected]>

firmware: raspberrypi: Add reboot mode support

The Raspberry Pi firmware has a tryboot mode where it tries to boot from another partition. This can be used to create a A/B update scheme.

To enable this on the next boot, the RPI_FIRMWARE_SET_REBOOT_FLAGS message must be sent to the firmware. Add support for this by registering a reboot mode driver.

Quoted 4 lines
drivers/firmware/raspberrypi.c +0 -0
index 0aa322e9a2e73..2a0c40b8052e5 100644

[ ... ]

drivers/firmware/raspberrypi.c static int rpi_firmware_probe(struct platform_device *pdev) · line 336

Quoted 7 lines
drivers/firmware/raspberrypi.c +3 -0
@@ -302,9 +331,14 @@ static int rpi_firmware_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, fw);
 
+	ret = devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw);
+	if (ret)
+		return ret;

[Severity: High]
If devm_add_action_or_reset() fails here, could this create a use-after-free race condition?

Quoted 5 lines

When the failure occurs, devm_add_action_or_reset() immediately executes devm_rpi_firmware_put(), which drops the refcount and frees the fw context. Because platform_set_drvdata() was already called, the driver data will point to this freed memory until the core probe cleanup mechanism eventually clears it.

Yes, this could happen. It should be sufficient to put the devm_add_action_or_reset call before the platform_set_drvdata call. I will send a new version with this change.

Quoted 28 lines

During this window, a consumer driver could call rpi_firmware_get(), which performs a lockless lookup of drvdata and attempts to dereference the freed pointer:

drivers/firmware/raspberrypi.c:rpi_firmware_get() {
    ...
	fw = platform_get_drvdata(pdev);
	if (!fw)
		goto err_put_device;

	if (!kref_get_unless_zero(&fw->consumers))
		goto err_put_device;
    ...
}
Quoted 8 lines
drivers/firmware/raspberrypi.c +2 -0
+
 	rpi_firmware_print_firmware_revision(fw);
 	rpi_register_hwmon_driver(dev, fw);
 	rpi_register_clk_driver(dev);
+	rpi_register_reboot_mode(dev, fw);
 
 	return 0;
 }

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
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
#6 lore raw

Re: [PATCH RESEND v3 3/4] firmware: raspberrypi: Add reboot mode support

In reply to Gregor Herburger

Hi Gregor,

Am 03.09.26 um 16:48 schrieb Gregor Herburger:

Quoted 6 lines

The Raspberry Pi firmware has a tryboot mode where it tries to boot from another partition. This can be used to create a A/B update scheme.

To enable this on the next boot, the RPI_FIRMWARE_SET_REBOOT_FLAGS message must be sent to the firmware. Add support for this by registering a reboot mode driver.

sorry for the delay.

Do you know the consequences in case the firmware doesn't support RPI_FIRMWARE_SET_REBOOT_FLAGS ?

Best regards

Quoted 146 lines

Furthermore, safely registering the reboot mode requires adjusting the teardown sequence. Replace the manually called rpi_firmware_put() with a devres-managed called (devm_add_action_or_reset). Without this, the cleanup function of devm_reboot_mode_register() would trigger a use-after-free error by attempting to access the firmware context after it had already been freed by rpi_firmware_remove().

---
  drivers/firmware/Kconfig                   |  2 ++
  drivers/firmware/raspberrypi.c             | 40 ++++++++++++++++++++++++++----
  include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
  3 files changed, 39 insertions(+), 5 deletions(-)
drivers/firmware/Kconfig +2 -0
index bbd2155d84838..34605bf7c9734 100644
@@ -115,6 +115,8 @@ config ISCSI_IBFT
  config RASPBERRYPI_FIRMWARE
  	tristate "Raspberry Pi Firmware Driver"
  	depends on BCM2835_MBOX
+	select POWER_RESET
+	select REBOOT_MODE
  	help
  	  This option enables support for communicating with the firmware on the
  	  Raspberry Pi.
drivers/firmware/raspberrypi.c +35 -5
index 0aa322e9a2e73..2a0c40b8052e5 100644
@@ -14,6 +14,7 @@
  #include <linux/of.h>
  #include <linux/of_platform.h>
  #include <linux/platform_device.h>
+#include <linux/reboot-mode.h>
  #include <linux/slab.h>
  #include <soc/bcm2835/raspberrypi-firmware.h>
 
@@ -29,6 +30,7 @@ struct rpi_firmware {
  	struct mbox_client cl;
  	struct mbox_chan *chan; /* The property channel. */
  	struct completion c;
+	struct reboot_mode_driver reboot_mode;
  	u32 enabled;
 
  	struct kref consumers;
@@ -273,10 +275,37 @@ static void devm_rpi_firmware_put(void *data)
  	rpi_firmware_put(fw);
  }
 
+static int rpi_firmware_reboot_mode_write(struct reboot_mode_driver *reboot,
+					  unsigned int magic)
+{
+	struct rpi_firmware *fw = container_of(reboot, struct rpi_firmware,
+					       reboot_mode);
+	__le32 fw_magic = cpu_to_le32(magic);
+
+	if (!magic)
+		return 0;
+
+	return rpi_firmware_property(fw, RPI_FIRMWARE_SET_REBOOT_FLAGS,
+				     &fw_magic, sizeof(fw_magic));
+}
+
+static void rpi_register_reboot_mode(struct device *dev, struct rpi_firmware *fw)
+{
+	int ret;
+
+	fw->reboot_mode.dev = dev;
+	fw->reboot_mode.write = rpi_firmware_reboot_mode_write;
+	ret = devm_reboot_mode_register(dev, &fw->reboot_mode);
+	if (ret)
+		dev_err(dev, "Failed to register reboot mode: %d\n", ret);
+
+}
+
  static int rpi_firmware_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
  	struct rpi_firmware *fw;
+	int ret;
 
  	/*
  	 * Memory will be freed by rpi_firmware_delete() once all users have
@@ -292,7 +321,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
 
  	fw->chan = mbox_request_channel(&fw->cl, 0);
  	if (IS_ERR(fw->chan)) {
-		int ret = PTR_ERR(fw->chan);
+		ret = PTR_ERR(fw->chan);
  		kfree(fw);
  		return dev_err_probe(dev, ret, "Failed to get mbox channel\n");
  	}
@@ -302,9 +331,14 @@ static int rpi_firmware_probe(struct platform_device *pdev)
 
  	platform_set_drvdata(pdev, fw);
 
+	ret = devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw);
+	if (ret)
+		return ret;
+
  	rpi_firmware_print_firmware_revision(fw);
  	rpi_register_hwmon_driver(dev, fw);
  	rpi_register_clk_driver(dev);
+	rpi_register_reboot_mode(dev, fw);
 
  	return 0;
  }
@@ -321,14 +355,10 @@ static void rpi_firmware_shutdown(struct platform_device *pdev)
 
  static void rpi_firmware_remove(struct platform_device *pdev)
  {
-	struct rpi_firmware *fw = platform_get_drvdata(pdev);
-
  	platform_device_unregister(rpi_hwmon);
  	rpi_hwmon = NULL;
  	platform_device_unregister(rpi_clk);
  	rpi_clk = NULL;
-
-	rpi_firmware_put(fw);
  }
 
  static const struct of_device_id rpi_firmware_of_match[] = {
include/soc/bcm2835/raspberrypi-firmware.h +2 -0
index 66cc5a426c3c5..f905bff0fb3ea 100644
@@ -81,6 +81,7 @@ enum rpi_firmware_property_tag {
  	RPI_FIRMWARE_GET_POE_HAT_VAL =                        0x00030049,
  	RPI_FIRMWARE_SET_POE_HAT_VAL =                        0x00030050,
  	RPI_FIRMWARE_NOTIFY_XHCI_RESET =                      0x00030058,
+	RPI_FIRMWARE_GET_REBOOT_FLAGS =                       0x00030064,
  	RPI_FIRMWARE_NOTIFY_DISPLAY_DONE =                    0x00030066,
  	RPI_FIRMWARE_SET_CLOCK_STATE =                        0x00038001,
  	RPI_FIRMWARE_SET_CLOCK_RATE =                         0x00038002,
@@ -92,6 +93,7 @@ enum rpi_firmware_property_tag {
  	RPI_FIRMWARE_SET_SDHOST_CLOCK =                       0x00038042,
  	RPI_FIRMWARE_SET_GPIO_CONFIG =                        0x00038043,
  	RPI_FIRMWARE_SET_PERIPH_REG =                         0x00038045,
+	RPI_FIRMWARE_SET_REBOOT_FLAGS =                       0x00038064,
 
  	/* Dispmanx TAGS */
  	RPI_FIRMWARE_FRAMEBUFFER_ALLOCATE =                   0x00040001,
#7 lore raw

Re: [PATCH RESEND v3 3/4] firmware: raspberrypi: Add reboot mode support

Quoted 6 lines · Stefan Wahren

Hi Gregor,

Am 03.09.26 um 16:48 schrieb Gregor Herburger:

Quoted 3 lines

The Raspberry Pi firmware has a tryboot mode where it tries to boot from another partition. This can be used to create a A/B update scheme.

Quoted 8 lines
Quoted 3 lines

To enable this on the next boot, the RPI_FIRMWARE_SET_REBOOT_FLAGS message must be sent to the firmware. Add support for this by registering a reboot mode driver.

sorry for the delay.

Do you know the consequences in case the firmware doesn't support RPI_FIRMWARE_SET_REBOOT_FLAGS ?

No I do not know what happens but I afaik tryboot is supported on all devices. At least the doucmentation [0] says so.

[0]https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#fail-safe-os-updates-tryboot

Best regards

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
#8 lore raw

Re: [PATCH RESEND v3 3/4] firmware: raspberrypi: Add reboot mode support

In reply to Gregor Herburger

Hi Gregor,

Am 03.09.26 um 16:48 schrieb Gregor Herburger:

Quoted 27 lines

The Raspberry Pi firmware has a tryboot mode where it tries to boot from another partition. This can be used to create a A/B update scheme.

To enable this on the next boot, the RPI_FIRMWARE_SET_REBOOT_FLAGS message must be sent to the firmware. Add support for this by registering a reboot mode driver.

Furthermore, safely registering the reboot mode requires adjusting the teardown sequence. Replace the manually called rpi_firmware_put() with a devres-managed called (devm_add_action_or_reset). Without this, the cleanup function of devm_reboot_mode_register() would trigger a use-after-free error by attempting to access the firmware context after it had already been freed by rpi_firmware_remove().

---
  drivers/firmware/Kconfig                   |  2 ++
  drivers/firmware/raspberrypi.c             | 40 ++++++++++++++++++++++++++----
  include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
  3 files changed, 39 insertions(+), 5 deletions(-)
drivers/firmware/Kconfig +0 -0
index bbd2155d84838..34605bf7c9734 100644

drivers/firmware/Kconfig config ISCSI_IBFT · line 122

Quoted 8 lines
drivers/firmware/Kconfig +2 -0
@@ -115,6 +115,8 @@ config ISCSI_IBFT
  config RASPBERRYPI_FIRMWARE
  	tristate "Raspberry Pi Firmware Driver"
  	depends on BCM2835_MBOX
+	select POWER_RESET
+	select REBOOT_MODE
  	help
  	  This option enables support for communicating with the firmware on the
  	  Raspberry Pi.

this part doesn't apply anymore. Did you rebased against 7.3-rc1 ?

Best regards

#9 lore raw

Re: [PATCH RESEND v3 3/4] firmware: raspberrypi: Add reboot mode support

Quoted 24 lines · Stefan Wahren

Hi Gregor,

Am 03.09.26 um 16:48 schrieb Gregor Herburger:

Quoted 21 lines

The Raspberry Pi firmware has a tryboot mode where it tries to boot from another partition. This can be used to create a A/B update scheme.

To enable this on the next boot, the RPI_FIRMWARE_SET_REBOOT_FLAGS message must be sent to the firmware. Add support for this by registering a reboot mode driver.

Furthermore, safely registering the reboot mode requires adjusting the teardown sequence. Replace the manually called rpi_firmware_put() with a devres-managed called (devm_add_action_or_reset). Without this, the cleanup function of devm_reboot_mode_register() would trigger a use-after-free error by attempting to access the firmware context after it had already been freed by rpi_firmware_remove().

---
  drivers/firmware/Kconfig                   |  2 ++
  drivers/firmware/raspberrypi.c             | 40 ++++++++++++++++++++++++++----
  include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
  3 files changed, 39 insertions(+), 5 deletions(-)
Quoted 15 lines
Quoted 6 lines
drivers/firmware/Kconfig +0 -0
index bbd2155d84838..34605bf7c9734 100644

drivers/firmware/Kconfig config ISCSI_IBFT · line 122

Quoted 8 lines
drivers/firmware/Kconfig +2 -0
@@ -115,6 +115,8 @@ config ISCSI_IBFT
  config RASPBERRYPI_FIRMWARE
  	tristate "Raspberry Pi Firmware Driver"
  	depends on BCM2835_MBOX
+	select POWER_RESET
+	select REBOOT_MODE
  	help
  	  This option enables support for communicating with the firmware on the
  	  Raspberry Pi.

this part doesn't apply anymore. Did you rebased against 7.3-rc1 ?

I have not rebased. I just sent the series out again to get some feedback. I will rebase it and send it again. Sashiko found an issue anyways.

Quoted 2 lines

Best regards

Best regards

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
#10 lore raw

PATCH RESEND v3 4/4 arm64: dts: broadcom: bcm2712: Add reboot modes to firmware node

In reply to Gregor Herburger

The Raspberry Pi firmware driver allows the tryboot reboot mode. Add this mode and normal boot mode to the node.

---
 arch/arm64/boot/dts/broadcom/bcm2712-rpi-5-b-base.dtsi | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/boot/dts/broadcom/bcm2712-rpi-5-b-base.dtsi +2 -0
index b7a6bc34ae1ab..67095c7ff770d 100644
@@ -49,6 +49,8 @@ firmware: rpi-firmware {
 			compatible = "raspberrypi,bcm2835-firmware", "simple-mfd";
 
 			mboxes = <&mailbox>;
+			mode-normal = <0>;
+			mode-tryboot = <1>;
 
 			firmware_clocks: clocks {
 				compatible = "raspberrypi,firmware-clocks";
Signature · 1 line
2.47.3