On Tue, May 17, 2016 at 02:02:37AM +0200, Kim Lidström wrote:
> Is this patch better? I have tested it by trying both cnmac0, cnmac1 and
> cnmac2 as rootdev and it seems to work properly.
> I also removed the outdated comment, changed the pointless text (Maybe
> it'd be more appropriate to remove it?) and changed a memcpy() to
> strlcpy().
> Oh, I added that strncmp too!

The patch is better now, thanks. I committed it with a few tweaks.
I kept the strcmp() in device_register() because the buffers are
null-terminated and because the code is supposed check if the strings
are equal (not if bootdev is a prefix of dev->dv_xname).

> Index: sys/arch/octeon/octeon/autoconf.c
> ===================================================================
> RCS file: /cvs/src/sys/arch/octeon/octeon/autoconf.c,v
> retrieving revision 1.7
> diff -u -p -r1.7 autoconf.c
> --- sys/arch/octeon/octeon/autoconf.c   20 Jul 2015 19:44:32 -0000     
> 1.7
> +++ sys/arch/octeon/octeon/autoconf.c   16 May 2016 23:52:33 -0000
> @@ -44,11 +44,45 @@ cpu_configure(void)
>       cold = 0;
>  }
>  
> +struct devmap {
> +       char *dev;
> +       enum devclass class;
> +};
> +
> +struct devmap *
> +findtype(void)
> +{
> +       static struct devmap devmap[] = {
> +               { "wd", DV_DISK },
> +               { "sd", DV_DISK },
> +               { "octcf", DV_DISK },
> +               { "amdcf", DV_DISK },
> +               { "cnmac", DV_IFNET },
> +               { NULL, DV_DULL }
> +       };
> +       struct devmap *dp = &devmap[0];
> +
> +       if (strlen(bootdev) < 2)
> +               return(dp);
> +
> +       while (dp->dev) {
> +               if (strncmp(bootdev, dp->dev, strlen(dp->dev)) == 0)
> +                       break;
> +               dp++;
> +       }
> +
> +       if (dp->dev == NULL)
> +               printf("%s is not a valid rootdev\n", bootdev);
> +       
> +       return(dp);
> +}
> +
>  void
>  parse_uboot_root(void)
>  {
>       char *p;
>       size_t len;
> +       struct devmap *dp;
>  
>          /*
>           * Turn the U-Boot root device (rootdev=/dev/octcf0) into a
>           boot device.
> @@ -62,9 +96,10 @@ parse_uboot_root(void)
>       if (len <= 2 || len >= sizeof bootdev - 1)
>               return;
>  
> -       memcpy(bootdev, p, len);
> -       bootdev[len] = '\0';
> -       bootdev_class = DV_DISK;
> +       strlcpy(bootdev, p, sizeof bootdev);
> +
> +       dp = findtype();
> +       bootdev_class = dp->class;
>  }
>  
>  void
> @@ -83,27 +118,14 @@ device_register(struct device *dev, void
>       if (bootdv != NULL)
>               return;
>  
> -       const char *drvrname = dev->dv_cfdata->cf_driver->cd_name;
> -       const char *name = dev->dv_xname;
> -
>       if (dev->dv_class != bootdev_class)
>               return;
>  
>       switch (bootdev_class) {
>       case DV_DISK:
> -               if ((strcmp(drvrname, "wd") == 0 ||
> -                   strcmp(drvrname, "sd") == 0 ||
> -                   strcmp(drvrname, "octcf") == 0 ||
> -                   strcmp(drvrname, "amdcf") == 0) &&
> -                   strcmp(name, bootdev) == 0)
> -                       bootdv = dev;
> -               break;
>       case DV_IFNET:
> -               /*
> -                * This relies on the onboard Ethernet interface being
> -                * attached before any other (usb) interface.
> -                */
> -               bootdv = dev;
> +               if (strncmp(dev->dv_xname, bootdev, strlen(bootdev)) ==
> 0)
> +                       bootdv = dev;
>               break;
>       default:
>               break;
> 

Reply via email to