Implement recovery boot detection for SPI flash
cover Implement recovery boot detection for SPI flash
As of now, there is only the boot rom log on i.MX processors that give us information on whether we have booted from the primary or recovery image.
Extract this information into a Barebox variable in order to pass this information to user space. This is useful for tools such as RAUC, that rely on that information when updating the bootloader atomically.
The variable is called bootsource_instance_index.
The intention behind the name is, that for an instance we can have multiple boot images, where the primary is depicted as zero, and the recovery/secondary is depicted as a 1.
There is no implementation on how to pass this information to user space. This could be implemented by a boot script that interprets the content of that variable into a human readable format.
Successfully tested on the congatec-qmx8p.
---
Fabian Pfitzner (2):
bootsource: add bootsource instance index variable
arch: arm: imx: detect secondary boot
Documentation/user/variables.rst | 1 + arch/arm/mach-imx/boot.c | 52 ++++++++++++++++++++++++++++++++++++++++ common/bootsource.c | 20 ++++++++++++++++ include/bootsource.h | 11 +++++++++ 4 files changed, 84 insertions(+)
---
base-commit: 1806d0f4835a1217a298f82fa2fc88b2694380f2
change-id: 20260518-fpf-bootsource-instance-index-a7bb108392a3
Best regards,
Signature · 1 line
Fabian Pfitzner <[email protected]>
PATCH 1/2 bootsource: add bootsource instance index variable
The bootsource instance index variable shows us,
from which image on the instance we have booted from.
This is a useful information, when there a multiple boot images contained on one flash (e. g. primary and recovery).
Add a C interface to set and get the bootsource instance:
int bootsource_get_instance_index(void);
void bootsource_set_instance_index(int index);Also export the shell variable "bootsource_instance_index".
--- Documentation/user/variables.rst | 1 + common/bootsource.c | 20 ++++++++++++++++++++ include/bootsource.h | 11 +++++++++++ 3 files changed, 32 insertions(+)
Documentation/user/variables.rst +1 -0
@@ -112,6 +112,7 @@ of all active variables with special meaning along with a short description: bootargs Linux Kernel parameters bootsource The source barebox has been booted from bootsource_instance The instance of the source barebox has been booted from + bootsource_instance_index Tells us whether we have booted from primary(0) or secondary(1) global.boot.default default boot order ...
common/bootsource.c +20 -0
@@ -33,6 +33,7 @@ static const char *bootsource_str[BOOTSOURCE_MAX] = {static enum bootsource bootsource =BOOTSOURCE_UNKNOWN ;static int bootsource_instance =BOOTSOURCE_INSTANCE_UNKNOWN ; +static int bootsource_instance_index =BOOTSOURCE_INSTANCE_INDEX_UNKNOWN ;const char *bootsource_to_string (enum bootsource src) {
@@ -226,6 +227,17 @@ int bootsource_set(enum bootsource src, int instance)return alias_id; } +void bootsource_set_instance_index (int index) +{ +if (index <0 || index >1 ) +pr_err ("Invalid index: %d, expected either 0 or 1" , index); + +char str[1 ]; + +sprintf (str,"%d" , index); +setenv ("bootsource_instance_index" , str); +} +enum bootsource bootsource_get (void ) {return bootsource;
@@ -240,11 +252,19 @@ int bootsource_get_instance(void)BAREBOX_MAGICVAR (bootsource_instance,"The instance of the source barebox has been booted from" ); +int bootsource_get_instance_index (void ) +{ +return bootsource_instance_index; +} + +BAREBOX_MAGICVAR (bootsource_instance_index,"Tells us whether we have booted from primary(0) or secondary(1)" ); +static int bootsource_init (void ) {bootsource_set_raw (bootsource, bootsource_instance);export ("bootsource" );export ("bootsource_instance" ); +export ("bootsource_instance_index" );return 0 ; }
include/bootsource.h +11 -0
@@ -24,11 +24,13 @@ enum bootsource { };#define BOOTSOURCE_INSTANCE_UNKNOWN -1 +#define BOOTSOURCE_INSTANCE_INDEX_UNKNOWN -1enum bootsource bootsource_get (void );enum bootsource bootsource_get_device (void );int bootsource_get_instance (void );const char *bootsource_get_alias_name (void ); +int bootsource_get_instance_index (void );const char *bootsource_to_string (enum bootsource src);const char *bootsource_get_alias_stem (enum bootsource bs);int bootsource_of_alias_xlate (enum bootsource bs,int instance);
@@ -72,4 +74,13 @@ void bootsource_set_raw(enum bootsource src, int instance);*/ void bootsource_set_raw_instance (int instance); +/** +* bootsource_set_instance_index - set bootsource_instance_index as-is +* @index: bootrom reported index +* +* Set the index that depicts the boot image we have booted from. +* This is either 0 (primary) or 1 (secondary) +*/ +void bootsource_set_instance_index (int index); +#endif /* __BOOTSOURCE_H__ */
Signature · 1 line
2.47.3
Sascha Hauer
maintainerRe: [PATCH 1/2] bootsource: add bootsource instance index variable
Quoted 45 lines · Fabian Pfitzner
The bootsource instance index variable shows us,
from which image on the instance we have booted from.
This is a useful information, when there a multiple boot images contained on one flash (e. g. primary and recovery).Add a C interface to set and get the bootsource instance:
int bootsource_get_instance_index(void); void bootsource_set_instance_index(int index);Also export the shell variable "
bootsource_instance_index".Signed-off-by Fabian Pfitzner <[email protected]>--- Documentation/user/variables.rst | 1 + common/bootsource.c | 20 ++++++++++++++++++++ include/bootsource.h | 11 +++++++++++ 3 files changed, 32 insertions(+)Documentation/user/variables.rst +1
-0@@ -112,6 +112,7 @@ of all active variables with special meaning along with a short description: bootargs Linux Kernel parameters bootsource The source barebox has been booted from bootsource_instance The instance of the source barebox has been booted from + bootsource_instance_index Tells us whether we have booted from primary(0) or secondary(1) global.boot.default default boot ordercommon/bootsource.c +1
-0@@ -33,6 +33,7 @@ static const char *bootsource_str[BOOTSOURCE_MAX] = {static enum bootsource bootsource =BOOTSOURCE_UNKNOWN ;static int bootsource_instance =BOOTSOURCE_INSTANCE_UNKNOWN ; +static int bootsource_instance_index =BOOTSOURCE_INSTANCE_INDEX_UNKNOWN ;const char *bootsource_to_string (enum bootsource src) {@@ -226,6 +227,17 @@ int bootsource_set(enum bootsource src, int instance)return alias_id;
common/bootsource.c int bootsource_set(enum bootsource src, int instance) · line 237
Quoted 10 lines
common/bootsource.c +8
-0} +void bootsource_set_instance_index (int index) +{ +if (index <0 || index >1 ) +pr_err ("Invalid index: %d, expected either 0 or 1" , index); + +char str[1 ]; + +sprintf (str,"%d" , index);
The string is too small. You need another byte for the '\0'
Sascha
--
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
PATCH 2/2 arch: arm: imx: detect secondary boot
In reply to Fabian PfitznerIntegrate support to detect secondary boot on imx machines. Copy the logic for detection from the uboot-imx project[1].
The information will be stored in the bootsource_instance_index variable.
--- arch/arm/mach-imx/boot.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
arch/arm/mach-imx/boot.c +52 -0
@@ -26,6 +26,8 @@#include <soc/fsl/fsl_udc.h> +int imx8m_detect_secondary_image_boot (void ); +static void imx_boot_save_loc (void (*get_boot_source)(enum bootsource *,int *)) {
@@ -35,6 +37,10 @@ imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *))get_boot_source (&src, &instance);bootsource_set (src, instance); + +int index =imx8m_detect_secondary_image_boot (); + +bootsource_set_instance_index (index); }
@@ -756,3 +762,49 @@ void imx8mn_get_boot_source(enum bootsource *src, int *instance) void imx8mn_boot_save_loc(void) __alias(imx8mp_boot_save_loc); + +int imx8m_detect_secondary_image_boot (void ) +{ +u32 *rom_log_addr = (u32 *)0x9e0 ; +u32 *rom_log; +u8 event_id; +int i, boot_secondary =0 ; + +/* If the ROM event log pointer is not valid. */ +if (*rom_log_addr <0x900000 || *rom_log_addr >=0xb00000 || + *rom_log_addr &0x3 ) +return -EINVAL ; + +/* Parse the ROM event ID version 2 log */ + rom_log = (u32 *)(uintptr_t )(*rom_log_addr); +for (i =0 ; i <128 ; i++) { + event_id = rom_log[i] >>24 ; +switch (event_id) { +case 0x00 :/* End of list */ +return boot_secondary; +/* Log entries with 1 parameter, skip 1 */ +case 0x80 :/* Start to perform the device initialization */ +case 0x81 :/* The boot device initialization completes */ +case 0x82 :/* Starts to execute boot device driver pre-config */ +case 0x8f :/* The boot device initialization fails */ +case 0x90 :/* Start to read data from boot device */ +case 0x91 :/* Reading data from boot device completes */ +case 0x9f :/* Reading data from boot device fails */ + i +=1 ; +continue ; +/* Log entries with 2 parameters, skip 2 */ +case 0xa0 :/* Image authentication result */ +case 0xc0 :/* Jump to the boot image soon */ + i +=2 ; +continue ; +/* Boot from the secondary boot image */ +case 0x51 : + boot_secondary =1 ; +continue ; +default : +continue ; + } + } + +return boot_secondary; +}
Signature · 1 line
2.47.3
Sascha Hauer
maintainerRe: [PATCH 2/2] arch: arm: imx: detect secondary boot
Quoted 18 lines · Fabian Pfitzner
Integrate support to detect secondary boot on imx machines. Copy the logic for detection from the uboot-imx project[1].
The information will be stored in the
bootsource_instance_indexvariable.Signed-off-by Fabian Pfitzner <[email protected]>--- arch/arm/mach-imx/boot.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)arch/arm/mach-imx/boot.c +0
-0
arch/arm/mach-imx/boot.c line 29
Quoted 4 lines
arch/arm/mach-imx/boot.c +1
-0@@ -26,6 +26,8 @@#include <soc/fsl/fsl_udc.h> +int imx8m_detect_secondary_image_boot (void );
It should be either static without forward declaration or exported with the declaration in a header file.
arch/arm/mach-imx/boot.c imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *)) · line 41
Quoted 10 lines
arch/arm/mach-imx/boot.c +3
-0+static void imx_boot_save_loc (void (*get_boot_source)(enum bootsource *,int *)) {@@ -35,6 +37,10 @@ imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *))get_boot_source (&src, &instance);bootsource_set (src, instance); + +int index =imx8m_detect_secondary_image_boot ();
You assume you are running on i.MX8M here which is not necessarily the case.
Quoted 11 lines
arch/arm/mach-imx/boot.c +4
-0+ +bootsource_set_instance_index (index); }@@ -756,3 +762,49 @@ void imx8mn_get_boot_source(enum bootsource *src, int *instance) void imx8mn_boot_save_loc(void) __alias(imx8mp_boot_save_loc); + +int imx8m_detect_secondary_image_boot (void )
arch/arm/mach-imx/boot.c void imx8mn_get_boot_source(enum bootsource *src, int *instance) · line 776
Quoted 10 lines
arch/arm/mach-imx/boot.c +10
-0+{ +u32 *rom_log_addr = (u32 *)0x9e0 ; +u32 *rom_log; +u8 event_id; +int i, boot_secondary =0 ; + +/* If the ROM event log pointer is not valid. */ +if (*rom_log_addr <0x900000 || *rom_log_addr >=0xb00000 || + *rom_log_addr &0x3 ) +return -EINVAL ;
We have imx8m_get_bootrom_log() which handles different cases like we are in PBL/EL3 or in barebox proper. Why not use it here?
I wonder how this works at all because you are derefencing the ROM which is inside the faulting zero page. I would assume barebox crashes here when this is called from barebox proper.
arch/arm/mach-imx/boot.c void imx8mn_get_boot_source(enum bootsource *src, int *instance) · line 781
Quoted 5 lines
arch/arm/mach-imx/boot.c +5
-0+ +/* Parse the ROM event ID version 2 log */ + rom_log = (u32 *)(uintptr_t )(*rom_log_addr); +for (i =0 ; i <128 ; i++) { + event_id = rom_log[i] >>24 ;
We already have defines for this, see arch/arm/mach-imx/bootrom-cmd.c
Sascha
--
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Re: [PATCH 2/2] arch: arm: imx: detect secondary boot
Quoted 67 lines · Sascha Hauer
Quoted 22 lines · Fabian Pfitzner
Integrate support to detect secondary boot on imx machines. Copy the logic for detection from the uboot-imx project[1].
The information will be stored in the
bootsource_instance_indexvariable.Signed-off-by Fabian Pfitzner <[email protected]>---
arch/arm/mach-imx/boot.c | 52
++++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 52 insertions(+)arch/arm/mach-imx/boot.c +0
-0index
3fea22d05f3bfdf2fe9caa6767e1c7c5ea3ced00..a35a1acc2e71231236d3dd5008523f6c66912000100644arch/arm/mach-imx/boot.c +0
-0@@ -26,6 +26,8 @@Quoted 4 lines
#include <soc/fsl/fsl_udc.h>
+intimx8m_detect_secondary_image_boot(void);It should be either static without forward declaration or exported with
the declaration in a header file.arch/arm/mach-imx/boot.c imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *)) · line 41
Quoted 12 lines
+
static void
imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *))
{arch/arm/mach-imx/boot.c +0
-0@@ -35,6 +37,10 @@ imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *)) get_boot_source(&src, &instance);
bootsource_set(src, instance);arch/arm/mach-imx/boot.c +2
-0+ +int index =imx8m_detect_secondary_image_boot ();You assume you are running on i.MX8M here which is not necessarily the
case.Quoted 18 lines
arch/arm/mach-imx/boot.c +2
-0+ +bootsource_set_instance_index (index);}
arch/arm/mach-imx/boot.c +0
-0@@ -756,3 +762,49 @@ void imx8mn_get_boot_source(enum bootsource *src, int *instance)
voidimx8mn_boot_save_loc(void)
__alias(imx8mp_boot_save_loc);arch/arm/mach-imx/boot.c +8
-0+ +int imx8m_detect_secondary_image_boot (void ) +{ +u32 *rom_log_addr = (u32 *)0x9e0 ; +u32 *rom_log; +u8 event_id; +int i, boot_secondary =0 ; +
Quoted 8 lines
arch/arm/mach-imx/boot.c void imx8mn_get_boot_source(enum bootsource *src, int *instance) · line 776
Quoted 5 lines
arch/arm/mach-imx/boot.c +2
-0+/* If the ROM event log pointer is not valid. */ + if (*rom_log_addr < 0x900000 || *rom_log_addr >= 0xb00000||
arch/arm/mach-imx/boot.c +2
-0+ *rom_log_addr & 0x3) +return -EINVAL ;We have
imx8m_get_bootrom_log()which handles different cases like we are in PBL/EL3 or in barebox proper. Why not use it here?
I tried using imx8m_get_bootrom_log() here, but it does not seem to work. The function returns an invalid address (0x8000) here.
Quoted 25 lines
I wonder how this works at all because you are derefencing the ROM which
is inside the faulting zero page. I would assume barebox crashes here when this is called from barebox proper.arch/arm/mach-imx/boot.c void imx8mn_get_boot_source(enum bootsource *src, int *instance) · line 781
Quoted 5 lines
arch/arm/mach-imx/boot.c +5
-0+ +/* Parse the ROM event ID version 2 log */ + rom_log = (u32 *)(uintptr_t )(*rom_log_addr); +for (i =0 ; i <128 ; i++) { + event_id = rom_log[i] >>24 ;We already have defines for this, see
arch/arm/mach-imx/bootrom-cmd.cSascha
--
Pengutronix e.K.| | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917- 0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917- 5555 |
Signature · 4 lines
Pengutronix e.K. | Fabian Pfitzner | Steuerwalder Str. 21 | https://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
Sascha Hauer
maintainerRe: [PATCH 2/2] arch: arm: imx: detect secondary boot
Quoted 77 lines · Fabian Pfitzner
Quoted 67 lines · Sascha Hauer
Quoted 22 lines · Fabian Pfitzner
Integrate support to detect secondary boot on imx machines. Copy the logic for detection from the uboot-imx project[1].
The information will be stored in the
bootsource_instance_indexvariable.Signed-off-by Fabian Pfitzner <[email protected]>---
arch/arm/mach-imx/boot.c | 52
++++++++++++++++++++++++++++++++++++++++++++++++1 file changed, 52 insertions(+)arch/arm/mach-imx/boot.c +0
-0index
3fea22d05f3bfdf2fe9caa6767e1c7c5ea3ced00..a35a1acc2e71231236d3dd5008523f6c66912000100644arch/arm/mach-imx/boot.c +0
-0@@ -26,6 +26,8 @@Quoted 4 lines
#include <soc/fsl/fsl_udc.h>
+intimx8m_detect_secondary_image_boot(void);It should be either static without forward declaration or exported with
the declaration in a header file.arch/arm/mach-imx/boot.c imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *)) · line 41
Quoted 12 lines
+
static void
imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *))
{arch/arm/mach-imx/boot.c +0
-0@@ -35,6 +37,10 @@ imx_boot_save_loc(void (*get_boot_source)(enum bootsource *, int *)) get_boot_source(&src, &instance);
bootsource_set(src, instance);arch/arm/mach-imx/boot.c +2
-0+ +int index =imx8m_detect_secondary_image_boot ();You assume you are running on i.MX8M here which is not necessarily the
case.Quoted 18 lines
arch/arm/mach-imx/boot.c +2
-0+ +bootsource_set_instance_index (index);}
arch/arm/mach-imx/boot.c +0
-0@@ -756,3 +762,49 @@ void imx8mn_get_boot_source(enum bootsource *src, int *instance)
voidimx8mn_boot_save_loc(void)
__alias(imx8mp_boot_save_loc);arch/arm/mach-imx/boot.c +8
-0+ +int imx8m_detect_secondary_image_boot (void ) +{ +u32 *rom_log_addr = (u32 *)0x9e0 ; +u32 *rom_log; +u8 event_id; +int i, boot_secondary =0 ; +Quoted 8 lines
arch/arm/mach-imx/boot.c void imx8mn_get_boot_source(enum bootsource *src, int *instance) · line 776
Quoted 5 lines
arch/arm/mach-imx/boot.c +2
-0+/* If the ROM event log pointer is not valid. */ + if (*rom_log_addr < 0x900000 || *rom_log_addr >= 0xb00000||
arch/arm/mach-imx/boot.c +2
-0+ *rom_log_addr & 0x3) +return -EINVAL ;We have
imx8m_get_bootrom_log()which handles different cases like we are in PBL/EL3 or in barebox proper. Why not use it here?
Quoted 2 lines
I tried using
imx8m_get_bootrom_log()here, but it does not seem to work. The function returns an invalid address (0x8000) here.
imx8m_get_bootrom_log() is basically the same code as you open coded here. Which path is used in imx8m_get_bootrom_log(), Are you in EL3? is IN_PROPER true?
One difference is that in imx8m_get_bootrom_log() 0x9e0 is used as a unsigned long * (64bit) whereas your code assumes it's u32, but I can't see how this explains the 0x8000 value.
Sascha
--
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |