Re: initarm() parsing bootargs?

From: Warner Losh <imp_at_bsdimp.com>
Date: Thu, 14 Dec 2023 17:50:34 UTC
On Thu, Dec 14, 2023 at 4:17 AM Ronald Klop <ronald@freebsd.org> 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?
> How do others debug these pieces of code?
>

You need EARLY_PRINTF defined. And maybe some more stuff as well. For
Allwinner, I could tell you how, but I see nothing in the devices that the
RPi uses. It most likely needs SOCDEV_PA and SOCDEV_VA assigned correctly.
I've never done early arg parsing.

But one thing you can do is to verify at runtime that the linux command
line is really there in the FDT exported from the kernel. RPi has weird
ways to pass stuff into the kernel historically, though I can't recall the
details beyond that.

Warner


> 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.
>
>