Re: initarm() parsing bootargs?

From: Mitchell Horne <mhorne_at_freebsd.org>
Date: Thu, 14 Dec 2023 19:05:33 UTC

On 12/14/23 07:17, Ronald Klop wrote:
> Hi,
> 
> I'm looking into some code but it is really early in the boot path so I 
> find it hard to debug. Do you have some pointers?
> 
> Booting my RPI it gets a parameter "bootargs" passed. I used this in a 
> driver. See the forwarded email below for the details about this.
> 
> But in sys/arm64/arm64/machdep_boot.c I found this code. It looks likes 
> it would nicely get and parse this "bootargs" which is called 
> "linux_command_line" here.
> 
> void
> parse_fdt_bootargs(void)
> {
> 
>          if (loader_envp == NULL && 
> fdt_get_chosen_bootargs(linux_command_line,
>              LBABI_MAX_COMMAND_LINE) == 0) {
>                  init_static_kenv(static_kenv, sizeof(static_kenv));
>                  cmdline_set_env(linux_command_line, CMDLINE_GUARD);
>          }
> }
> 
> But kern_getenv() does not give me any values from this "bootargs" string.
> 
> I started adding some printf() lines from initarm() up until this method 
> but that is never printed. Is it too early in the boot to print anything?

Yes, it is too early. In general, the console is not available until 
after cn_init() is called by initarm().

In the 'typical' boot case, the kernel environment is inherited from 
loader(8), this is done in freebsd_parse_boot_param(), and note that it 
sets loader_envp.

This parse_fdt_bootargs() functionality was added to support the 
"LinuxABI" boot scheme, where the kernel is loaded and executed directly 
by u-boot (no loader(8)).

So on arm64, parsing bootargs is skipped "by default" (loader(8) case).

I think you could experiment by removing the "loader_envp == NULL" part 
of the conditional. What I am unsure about is if a second call to 
init_static_kenv() will properly append to the existing environment, or 
squash it.

> How do others debug these pieces of code?
> 

Mostly I try to avoid it, but it is not always possible :)

When you can't easily use EARLY_PRINTF, I have resorted to running in 
QEMU and attaching the debugger to its GDB stub. This allows you to set 
breakpoints and step through code of interest before the console is 
available.

I did not really check it, but there is some info here:
https://www.qemu.org/docs/master/system/gdb.html

Cheers,
Mitchell

> Regards,
> Ronald.
> 
> 
> 
> 
> -------- Forwarded Message --------
> Subject:     Re: git: 3878bbf1bb9e - main - Teach if_smsc to get MAC 
> from bootargs.
> Date:     Wed, 13 Dec 2023 21:40:20 +0100 (CET)
> From:     Ronald Klop <ronald-lists@klop.ws>
> To:     Emmanuel Vadot <manu@bidouilliste.com>
> CC:     Ronald Klop <ronald@FreeBSD.org>
> 
> 
> 
> *Van:* Emmanuel Vadot <manu@bidouilliste.com>
> *Datum:* donderdag, 7 december 2023 14:35
> *Aan:* Ronald Klop <ronald@FreeBSD.org>
> *CC:* src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, 
> dev-commits-src-main@FreeBSD.org
> *Onderwerp:* Re: git: 3878bbf1bb9e - main - Teach if_smsc to get MAC 
> from bootargs.
> 
>      On Thu, 7 Dec 2023 11:33:05 GMT
>      Ronald Klop <ronald@FreeBSD.org> wrote:
> 
>       > The branch main has been updated by ronald:
>       >
>       > URL: 
> https://cgit.FreeBSD.org/src/commit/?id=3878bbf1bb9e68f8579b57cde7d4e5c77de93320 <https://cgit.FreeBSD.org/src/commit/?id=3878bbf1bb9e68f8579b57cde7d4e5c77de93320>
>       >
>       > commit 3878bbf1bb9e68f8579b57cde7d4e5c77de93320
>       > Author:     Ronald Klop <ronald@FreeBSD.org>
>       > AuthorDate: 2023-11-04 14:14:00 +0000
>       > Commit:     Ronald Klop <ronald@FreeBSD.org>
>       > CommitDate: 2023-12-07 11:32:01 +0000
>       >
>       >     Teach if_smsc to get MAC from bootargs.
>       >
>       >     Some Raspberry Pi pass smsc95xx.macaddr=XX:XX:XX:XX:XX:XX as 
> bootargs.
>       >     Use this if no ethernet address is found in an EEPROM.
>       >     As last resort fall back to ether_gen_addr() instead of 
> random MAC.
>       >
>       >     PR:     274092
>       >     Reported by:    Patrick M. Hausen (via ML)
>       >     Reviewed by:    imp, karels, zlei
>       >     Tested by:      Patrick M. Hausen
>       >     Approved by:    karels
>       >     MFC after:      1 month
>       >     Relnotes:       yes
>       >     Differential Revision: https://reviews.freebsd.org/D42463 
> <https://reviews.freebsd.org/D42463>
>       > ---
>       >  sys/dev/usb/net/if_smsc.c | 86 
> +++++++++++++++++++++++++++++++++++++++++++++--
>       >  sys/net/ethernet.h        |  1 +
>       >  sys/net/if_ethersubr.c    | 10 ++++--
>       >  3 files changed, 92 insertions(+), 5 deletions(-)
>       >
>       > diff --git a/sys/dev/usb/net/if_smsc.c b/sys/dev/usb/net/if_smsc.c
>       > index ec8197229f17..54b18e0a67c9 100644
>       > --- a/sys/dev/usb/net/if_smsc.c
>       > +++ b/sys/dev/usb/net/if_smsc.c
>       > @@ -179,6 +179,8 @@ static const struct usb_device_id 
> smsc_devs[] = {
>       >  #define ETHER_IS_VALID(addr) \
>       >   (!ETHER_IS_MULTICAST(addr) && !ETHER_IS_ZERO(addr))
>       >
>       > +#define BOOTARGS_SMSC95XX    "smsc95xx.macaddr"
>       > +
>       >  static device_probe_t smsc_probe;
>       >  static device_attach_t smsc_attach;
>       >  static device_detach_t smsc_detach;
>       > @@ -1538,6 +1540,76 @@ smsc_ioctl(if_t ifp, u_long cmd, caddr_t 
> data)
>       >   return (rc);
>       >  }
>       >
>       > +#ifdef FDT
>       > +static bool
>       > +smsc_get_smsc95xx_macaddr(char* bootargs, size_t len, struct 
> usb_ether *ue)
>       > +{
>       > + int values[6];
>       > + int i;
>       > + char* p;
>       > +
>       > + p = strnstr(bootargs, BOOTARGS_SMSC95XX, len);
>       > + if (p == NULL)
>       > +     return (false);
>       > +
>       > + if (sscanf(p, BOOTARGS_SMSC95XX "=%x:%x:%x:%x:%x:%x%*c",
>       > +         &values[0], &values[1], &values[2],
>       > +         &values[3], &values[4], &values[5]) != 6) {
>       > +     smsc_warn_printf((struct smsc_softc *)ue->ue_sc,
>       > +             "invalid mac from bootargs '%s'.\n", p);
>       > +     return (false);
>       > + }
>       > +
>       > + for (i = 0; i < ETHER_ADDR_LEN; ++i)
>       > +     ue->ue_eaddr[i] = values[i];
>       > +
>       > + smsc_dbg_printf((struct smsc_softc *)ue->ue_sc,
>       > +         "bootargs mac=%6D.\n", ue->ue_eaddr, ":");
>       > + return (true);
>       > +}
>       > +
>       > +/**
>       > + * Raspberry Pi is known to pass 
> smsc95xx.macaddr=XX:XX:XX:XX:XX:XX via
>       > + * bootargs.
>       > + */
>       > +static bool
>       > +smsc_bootargs_get_mac_addr(device_t dev, struct usb_ether *ue)
>       > +{
>       > + char *bootargs;
>       > + ssize_t len;
>       > + phandle_t node;
>       > +
>       > + /* only use bootargs for the first device
>       > +  * to prevent duplicate mac addresses */
>       > + if (device_get_unit(dev) != 0)
>       > +     return (false);
>       > + node = OF_finddevice("/chosen");
>       > + if (node == -1)
>       > +     return (false);
>       > + if (OF_hasprop(node, "bootargs") == 0) {
>       > +     smsc_dbg_printf((struct smsc_softc *)ue->ue_sc,
>       > +             "bootargs not found");
>       > +     return (false);
>       > + }
>       > + len = OF_getprop_alloc(node, "bootargs", (void **)&bootargs);
>       > + if (len == -1 || bootargs == NULL) {
>       > +     smsc_warn_printf((struct smsc_softc *)ue->ue_sc,
>       > +             "failed alloc for bootargs (%lu)", len);
>       > +     return (false);
>       > + }
> 
>        All this can be replaced with a call to fdt_get_chosen_bootargs.
>        Also we already do this for arm and arm64 in machdep_boot.c and 
> store
>      the linux boot args in a static var called linux_command_line, maybe
>      make it not static and get it from this driver.
>        While you're at it make bcm2835_fbd.c and bcm2835_fb.c use this as
>      they also get and parse the bootargs.
> 
>        Cheers,
> 
>      [...snip...]
> 
>      --
>      Emmanuel Vadot <manu@bidouilliste.com> <manu@freebsd.org>
> 
> 
> 
> Dear Emmanuel,
> 
> I'm looking into this function fdt_get_chosen_bootargs. Unfortunately it 
> uses static memory allocation which conflicts with another remark I got 
> in the review.
> 
> But what I do see is that it parses this bootargs string into 
> kern_setenv in sys/kern/subr_boot.c: boot_parse_arg(...).
> parse_fdt_bootargs (sys/arm64/arm64/machdep_boot.c) -> cmdline_set_env 
> -> boot_parse_cmdline (subr_boot.c) -> boot_parse_cmdline_delim -> 
> boot_parse_arg
> Would that be usable?
> 
> I don't see the value in the output of kenv(1) but maybe that is not 
> related.
> 
> I'll try to come up with a testcase on my RPI to see if this bootargs 
> ends up in kern_getenv in a parsed way.
> 
> Regards,
> Ronald.
>