From: Ahmad Fatoum <a.fatoum@pengutronix.de> This new variable can be set by boards from C code or from the environment to change the serial number fixed up into the kernel device tree. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> --- common/Kconfig | 5 +++++ common/misc.c | 16 ++++++++++++++++ common/oftree.c | 9 +++++++++ include/common.h | 3 +++ 4 files changed, 33 insertions(+) diff --git a/common/Kconfig b/common/Kconfig index f7a6a96e87..d08302a573 100644 --- a/common/Kconfig +++ b/common/Kconfig @@ -1066,6 +1066,11 @@ config MACHINE_ID Note: if no hashable information is available no machine id will be passed to the kernel. +config SERIAL_NUMBER_FIXUP + bool "fix up board serial number" + help + fixup serial number supplied by board code info device tree + config SYSTEMD_OF_WATCHDOG bool "inform devicetree-enabled kernel of used watchdog" depends on WATCHDOG && OFTREE && FLEXIBLE_BOOTARGS diff --git a/common/misc.c b/common/misc.c index 226635f0d4..3b3bc05bfd 100644 --- a/common/misc.c +++ b/common/misc.c @@ -149,6 +149,7 @@ EXPORT_SYMBOL(barebox_get_model); BAREBOX_MAGICVAR(global.model, "Product name of this hardware"); static char *hostname; +static char *serial_number; /* * The hostname is supposed to be the shortname of a board. It should @@ -179,6 +180,21 @@ EXPORT_SYMBOL(barebox_set_hostname_no_overwrite); BAREBOX_MAGICVAR(global.hostname, "shortname of the board. Also used as hostname for DHCP requests"); +void barebox_set_serial_number(const char *__serial_number) +{ + globalvar_add_simple_string("serial_number", &serial_number); + + free(serial_number); + serial_number = xstrdup(__serial_number); +} + +const char *barebox_get_serial_number(void) +{ + return serial_number; +} + +BAREBOX_MAGICVAR(global.serial_number, "Board serial number"); + void __noreturn panic(const char *fmt, ...) { va_list args; diff --git a/common/oftree.c b/common/oftree.c index bce0ff09d6..c6d75055cc 100644 --- a/common/oftree.c +++ b/common/oftree.c @@ -207,6 +207,15 @@ static int of_fixup_bootargs(struct device_node *root, void *unused) int instance = reset_source_get_instance(); struct device_d *dev; + if (IS_ENABLED(CONFIG_SERIAL_NUMBER_FIXUP)) { + const char *serialno; + + serialno = getenv("global.serial_number"); + if (serialno) + of_property_write_string(root, "serial-number", serialno); + + } + node = of_create_node(root, "/chosen"); if (!node) return -ENOMEM; diff --git a/include/common.h b/include/common.h index 4167d4676e..967502a7ab 100644 --- a/include/common.h +++ b/include/common.h @@ -126,4 +126,7 @@ const char *barebox_get_hostname(void); void barebox_set_hostname(const char *); void barebox_set_hostname_no_overwrite(const char *); +const char *barebox_get_serial_number(void); +void barebox_set_serial_number(const char *); + #endif /* __COMMON_H_ */ -- 2.30.2 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox
Replace board specific serial-number fixup with common barebox_set_serial_number(). Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> --- arch/arm/boards/protonic-imx6/board.c | 20 +------------------- arch/arm/mach-imx/Kconfig | 1 + 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/arch/arm/boards/protonic-imx6/board.c b/arch/arm/boards/protonic-imx6/board.c index 52cf39917a..0fadd148b4 100644 --- a/arch/arm/boards/protonic-imx6/board.c +++ b/arch/arm/boards/protonic-imx6/board.c @@ -196,30 +196,12 @@ static int prt_imx6_set_mac(struct prt_imx6_priv *priv, return 0; } -static int prt_of_fixup_serial(struct device_node *dstroot, void *arg) -{ - struct device_node *srcroot = arg; - const char *ser; - int len; - - ser = of_get_property(srcroot, "serial-number", &len); - return of_set_property(dstroot, "serial-number", ser, len, 1); -} - -static void prt_oftree_fixup_serial(const char *serial) -{ - struct device_node *root = of_get_root_node(); - - of_set_property(root, "serial-number", serial, strlen(serial) + 1, 1); - of_register_fixup(prt_of_fixup_serial, root); -} - static int prt_imx6_set_serial(struct prt_imx6_priv *priv, struct prti6q_rfid_contents *rfid) { rfid->serial[9] = 0; /* Failsafe */ dev_info(priv->dev, "Serial number: %s\n", rfid->serial); - prt_oftree_fixup_serial(rfid->serial); + barebox_set_serial_number(rfid->serial); return 0; } diff --git a/arch/arm/mach-imx/Kconfig b/arch/arm/mach-imx/Kconfig index 6b962dcf7e..ee313a1502 100644 --- a/arch/arm/mach-imx/Kconfig +++ b/arch/arm/mach-imx/Kconfig @@ -350,6 +350,7 @@ config MACH_PROTONIC_IMX6 select ARCH_IMX6 select ARCH_IMX6UL select ARM_USE_COMPRESSED_DTB + select SERIAL_NUMBER_FIXUP config MACH_PROTONIC_IMX8M bool "Protonic-Holland i.MX8Mx based boards" -- 2.30.2 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox
Add generic function to extend/fixup machine compatible. Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> --- common/oftree.c | 30 ++++++++++++++++++++++++++++++ include/of.h | 6 ++++++ 2 files changed, 36 insertions(+) diff --git a/common/oftree.c b/common/oftree.c index c6d75055cc..0a66882946 100644 --- a/common/oftree.c +++ b/common/oftree.c @@ -487,3 +487,33 @@ int of_autoenable_i2c_by_component(char *path) return ret; } + +int of_fixup_machine_compatible(struct device_node *root, const char *compat) +{ + int cclen = 0, clen = strlen(compat) + 1; + const char *curcompat; + void *buf; + + if (!root) { + root = of_get_root_node(); + if (!root) + return -ENODEV; + } + + curcompat = of_get_property(root, "compatible", &cclen); + + buf = xzalloc(cclen + clen); + + memcpy(buf, compat, clen); + memcpy(buf + clen, curcompat, cclen); + + /* + * Prepend the compatible from board entry to the machine compatible. + * Used to match bootspec entries against it. + */ + of_set_property(root, "compatible", buf, cclen + clen, true); + + free(buf); + + return 0; +} diff --git a/include/of.h b/include/of.h index cf9950e9b3..9f514c5ec2 100644 --- a/include/of.h +++ b/include/of.h @@ -316,6 +316,7 @@ struct device_node *of_find_node_by_path_or_alias(struct device_node *root, const char *str); int of_autoenable_device_by_path(char *path); int of_autoenable_i2c_by_component(char *path); +int of_fixup_machine_compatible(struct device_node *root, const char *compat); #else static inline bool of_node_name_eq(const struct device_node *np, const char *name) { @@ -834,6 +835,11 @@ static inline int of_autoenable_i2c_by_component(char *path) return -ENODEV; } +static int of_fixup_machine_compatible(struct device_node *root, + const char *compat) +{ + return -ENODEV; +} #endif -- 2.30.2 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox
Replace board specific fixup_machine_compatible() with generic of_fixup_machine_compatible() Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> --- arch/arm/boards/skov-imx6/board.c | 40 +++---------------------------- 1 file changed, 3 insertions(+), 37 deletions(-) diff --git a/arch/arm/boards/skov-imx6/board.c b/arch/arm/boards/skov-imx6/board.c index 2702bc1de9..53c213f33b 100644 --- a/arch/arm/boards/skov-imx6/board.c +++ b/arch/arm/boards/skov-imx6/board.c @@ -312,55 +312,21 @@ static int skov_board_no = -1; static bool skov_have_switch = true; static const char *no_switch_suffix = "-noswitch"; -static void fixup_machine_compatible(const char *compat, - struct device_node *root) -{ - int cclen = 0, clen = strlen(compat) + 1; - const char *curcompat; - void *buf; - - if (!root) { - root = of_get_root_node(); - if (!root) - return; - } - - curcompat = of_get_property(root, "compatible", &cclen); - - buf = xzalloc(cclen + clen); - - memcpy(buf, compat, clen); - memcpy(buf + clen, curcompat, cclen); - - /* - * Prepend the compatible from board entry to the machine compatible. - * Used to match bootspec entries against it. - */ - of_set_property(root, "compatible", buf, cclen + clen, true); - - free(buf); -} - static void fixup_noswitch_machine_compatible(struct device_node *root) { const char *compat = imx6_variants[skov_board_no].dts_compatible; const char *generic = "skov,imx6"; - size_t size, size_generic; char *buf; - size = strlen(compat) + strlen(no_switch_suffix) + 1; - size_generic = strlen(generic) + strlen(no_switch_suffix) + 1; - size = max(size, size_generic); - /* add generic compatible, so systemd&co can make right decisions */ buf = xasprintf("%s%s", generic, no_switch_suffix); - fixup_machine_compatible(buf, root); + of_fixup_machine_compatible(root, buf); /* add specific compatible as fallback, in case this board has new * challenges. */ buf = xasprintf("%s%s", compat, no_switch_suffix); - fixup_machine_compatible(buf, root); + of_fixup_machine_compatible(root, buf); free(buf); } @@ -648,7 +614,7 @@ static int skov_imx6_probe(struct device_d *dev) globalvar_add_simple("board.dts", variant->dts_compatible); globalvar_add_simple("board.display", variant->display ?: NULL); - fixup_machine_compatible(variant->dts_compatible, NULL); + of_fixup_machine_compatible(NULL, variant->dts_compatible); skov_init_board(variant); -- 2.30.2 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox
Currently we use generic/pinned machine compatible for different HW revisions. With this patch we extend this compatible string with HW revision specific. Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> --- arch/arm/boards/protonic-imx6/board.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/arch/arm/boards/protonic-imx6/board.c b/arch/arm/boards/protonic-imx6/board.c index 0fadd148b4..1a1e5540ee 100644 --- a/arch/arm/boards/protonic-imx6/board.c +++ b/arch/arm/boards/protonic-imx6/board.c @@ -85,6 +85,7 @@ struct prt_imx6_priv { const struct prt_machine_data *dcfg; unsigned int hw_id; unsigned int hw_rev; + const char *compatible; const char *name; unsigned int usb_delay; unsigned int no_usb_check; @@ -126,6 +127,20 @@ static const struct gpio prt_imx6_kvg_gpios[] = { }, }; +static int prt_of_fixup_hwrev(struct device_node *dstroot, void *arg) +{ + struct prt_imx6_priv *priv = prt_priv; + char *buf; + + buf = xasprintf("%s,model:%u,rev:%u", priv->compatible, priv->hw_id, + priv->hw_rev); + of_fixup_machine_compatible(dstroot, buf); + + free(buf); + + return 0; +} + static int prt_imx6_read_rfid(struct prt_imx6_priv *priv, void *buf, size_t size) { @@ -797,7 +812,7 @@ exit_get_dcfg: static int prt_imx6_probe(struct device_d *dev) { struct prt_imx6_priv *priv; - const char *name, *ptr; + const char *ptr; struct param_d *p; int ret; @@ -806,9 +821,9 @@ static int prt_imx6_probe(struct device_d *dev) return -ENOMEM; priv->dev = dev; - name = of_device_get_match_compatible(priv->dev); - ptr = strchr(name, ','); - priv->name = ptr ? ptr + 1 : name; + priv->compatible = of_device_get_match_compatible(priv->dev); + ptr = strchr(priv->compatible, ','); + priv->name = ptr ? ptr + 1 : priv->compatible; pr_info("Detected machine type: %s\n", priv->name); @@ -841,6 +856,8 @@ static int prt_imx6_probe(struct device_d *dev) prt_priv = priv; + of_register_fixup(prt_of_fixup_hwrev, NULL); + return 0; free_priv: kfree(priv); -- 2.30.2 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox
On 27.04.22 09:37, Oleksij Rempel wrote: > Add generic function to extend/fixup machine compatible. > > Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> Cc += Rouven, who had a similar use case > --- > common/oftree.c | 30 ++++++++++++++++++++++++++++++ > include/of.h | 6 ++++++ > 2 files changed, 36 insertions(+) > > diff --git a/common/oftree.c b/common/oftree.c > index c6d75055cc..0a66882946 100644 > --- a/common/oftree.c > +++ b/common/oftree.c > @@ -487,3 +487,33 @@ int of_autoenable_i2c_by_component(char *path) > > return ret; > } > + > +int of_fixup_machine_compatible(struct device_node *root, const char *compat) > +{ > + int cclen = 0, clen = strlen(compat) + 1; > + const char *curcompat; > + void *buf; > + > + if (!root) { > + root = of_get_root_node(); > + if (!root) > + return -ENODEV; > + } > + > + curcompat = of_get_property(root, "compatible", &cclen); > + > + buf = xzalloc(cclen + clen); > + > + memcpy(buf, compat, clen); add an if (curcompat) > + memcpy(buf + clen, curcompat, cclen); > + > + /* > + * Prepend the compatible from board entry to the machine compatible. > + * Used to match bootspec entries against it. > + */ > + of_set_property(root, "compatible", buf, cclen + clen, true); That way it should work even if there was no compatible property to begin with. If you feel like it, you could add a new $global.of_compatible to make this easier to test. :)) > + > + free(buf); > + > + return 0; > +} > diff --git a/include/of.h b/include/of.h > index cf9950e9b3..9f514c5ec2 100644 > --- a/include/of.h > +++ b/include/of.h > @@ -316,6 +316,7 @@ struct device_node *of_find_node_by_path_or_alias(struct device_node *root, > const char *str); > int of_autoenable_device_by_path(char *path); > int of_autoenable_i2c_by_component(char *path); > +int of_fixup_machine_compatible(struct device_node *root, const char *compat); > #else > static inline bool of_node_name_eq(const struct device_node *np, const char *name) > { > @@ -834,6 +835,11 @@ static inline int of_autoenable_i2c_by_component(char *path) > return -ENODEV; > } > > +static int of_fixup_machine_compatible(struct device_node *root, > + const char *compat) > +{ > + return -ENODEV; > +} > > #endif > -- 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 | _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox
On 27.04.22 09:37, Oleksij Rempel wrote: > Currently we use generic/pinned machine compatible for different HW > revisions. With this patch we extend this compatible string with HW > revision specific. > > Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de> > --- > arch/arm/boards/protonic-imx6/board.c | 25 +++++++++++++++++++++---- > 1 file changed, 21 insertions(+), 4 deletions(-) > > diff --git a/arch/arm/boards/protonic-imx6/board.c b/arch/arm/boards/protonic-imx6/board.c > index 0fadd148b4..1a1e5540ee 100644 > --- a/arch/arm/boards/protonic-imx6/board.c > +++ b/arch/arm/boards/protonic-imx6/board.c > @@ -85,6 +85,7 @@ struct prt_imx6_priv { > const struct prt_machine_data *dcfg; > unsigned int hw_id; > unsigned int hw_rev; > + const char *compatible; > const char *name; > unsigned int usb_delay; > unsigned int no_usb_check; > @@ -126,6 +127,20 @@ static const struct gpio prt_imx6_kvg_gpios[] = { > }, > }; > > +static int prt_of_fixup_hwrev(struct device_node *dstroot, void *arg) > +{ > + struct prt_imx6_priv *priv = prt_priv; > + char *buf; > + > + buf = xasprintf("%s,model:%u,rev:%u", priv->compatible, priv->hw_id, > + priv->hw_rev); : is not a valid character for device tree property names. (https://github.com/devicetree-org/devicetree-specification/releases/download/v0.4-rc1/devicetree-specification-v0.4-rc1.pdf Table 2.2) While barebox might not mind, it's better you stick to the standard character set. > + of_fixup_machine_compatible(dstroot, buf); > + > + free(buf); > + > + return 0; > +} > + > static int prt_imx6_read_rfid(struct prt_imx6_priv *priv, void *buf, > size_t size) > { > @@ -797,7 +812,7 @@ exit_get_dcfg: > static int prt_imx6_probe(struct device_d *dev) > { > struct prt_imx6_priv *priv; > - const char *name, *ptr; > + const char *ptr; > struct param_d *p; > int ret; > > @@ -806,9 +821,9 @@ static int prt_imx6_probe(struct device_d *dev) > return -ENOMEM; > > priv->dev = dev; > - name = of_device_get_match_compatible(priv->dev); > - ptr = strchr(name, ','); > - priv->name = ptr ? ptr + 1 : name; > + priv->compatible = of_device_get_match_compatible(priv->dev); > + ptr = strchr(priv->compatible, ','); > + priv->name = ptr ? ptr + 1 : priv->compatible; Could you reuse of_get_machine_compatible() here? > > pr_info("Detected machine type: %s\n", priv->name); > > @@ -841,6 +856,8 @@ static int prt_imx6_probe(struct device_d *dev) > > prt_priv = priv; > > + of_register_fixup(prt_of_fixup_hwrev, NULL); > + > return 0; > free_priv: > kfree(priv); -- 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 | _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox