All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] smbios: Add a --linux argument to apply linux modalias-like filtering
@ 2020-03-03 14:54 Julian Andres Klode
  2020-03-03 15:06 ` [PATCH v2] " Julian Andres Klode
  0 siblings, 1 reply; 5+ messages in thread
From: Julian Andres Klode @ 2020-03-03 14:54 UTC (permalink / raw)
  To: grub-devel; +Cc: Julian Andres Klode

Linux creates modalias strings by filtering out non-ASCII, space,
and colon characters. Provide an option that does the same filtering
so people can create a modalias string in grub, and then match their
modalias patterns against it.

Signed-off-by: Julian Andres Klode <julian.klode@canonical.com>
---
 grub-core/commands/smbios.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/grub-core/commands/smbios.c b/grub-core/commands/smbios.c
index 7a6a391fc..bb70af76c 100644
--- a/grub-core/commands/smbios.c
+++ b/grub-core/commands/smbios.c
@@ -64,6 +64,20 @@ grub_smbios_get_eps3 (void)
   return eps;
 }
 
+static char *linux_string(const char *value)
+{
+  char *out = grub_malloc(grub_strlen(value) + 1);
+  const char *src = value;
+  char *dst = out;
+
+  for (; *src; src++)
+    if (*src > ' ' && *src < 127 && *src != ':')
+      *dst++ = *src;
+
+  *dst = 0;
+  return out;
+}
+
 /*
  * These functions convert values from the various SMBIOS structure field types
  * into a string formatted to be returned to the user.  They expect that the
@@ -176,6 +190,7 @@ static const struct {
 /* List command options, with structure field getters ordered as above. */
 #define FIRST_GETTER_OPT (3)
 #define SETTER_OPT (FIRST_GETTER_OPT + ARRAY_SIZE(field_extractors))
+#define LINUX_OPT (FIRST_GETTER_OPT + ARRAY_SIZE(field_extractors) + 1)
 
 static const struct grub_arg_option options[] = {
   {"type",       't', 0, N_("Match structures with the given type."),
@@ -198,6 +213,8 @@ static const struct grub_arg_option options[] = {
                          N_("offset"), ARG_TYPE_INT},
   {"set",       '\0', 0, N_("Store the value in the given variable name."),
                          N_("variable"), ARG_TYPE_STRING},
+  {"linux",     '\0', 0, N_("Filter the result like linux does."),
+                         N_("variable"), ARG_TYPE_NONE},
   {0, 0, 0, 0, 0, 0}
 };
 
@@ -261,6 +278,7 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
 
   const grub_uint8_t *structure;
   const char *value;
+  char *modified_value = NULL;
   grub_int32_t option;
   grub_int8_t field_type = -1;
   grub_uint8_t i;
@@ -334,12 +352,18 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
     return grub_error (GRUB_ERR_IO,
                        N_("failed to retrieve the structure field"));
 
+  if (state[LINUX_OPT].set) {
+    value = modified_value = linux_string(value);
+  }
+
   /* Store or print the formatted value. */
   if (state[SETTER_OPT].set)
     grub_env_set (state[SETTER_OPT].arg, value);
   else
     grub_printf ("%s\n", value);
 
+  grub_free(modified_value);
+
   return GRUB_ERR_NONE;
 }
 
-- 
2.25.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2] smbios: Add a --linux argument to apply linux modalias-like filtering
  2020-03-03 14:54 [PATCH] smbios: Add a --linux argument to apply linux modalias-like filtering Julian Andres Klode
@ 2020-03-03 15:06 ` Julian Andres Klode
  2020-03-03 16:44   ` Daniel Kiper
  0 siblings, 1 reply; 5+ messages in thread
From: Julian Andres Klode @ 2020-03-03 15:06 UTC (permalink / raw)
  To: grub-devel; +Cc: Julian Andres Klode

Linux creates modalias strings by filtering out non-ASCII, space,
and colon characters. Provide an option that does the same filtering
so people can create a modalias string in grub, and then match their
modalias patterns against it.

Signed-off-by: Julian Andres Klode <julian.klode@canonical.com>
---
 grub-core/commands/smbios.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

v2: Coding style, sorry about first one - got too excited about getting
this working, I guess.

diff --git a/grub-core/commands/smbios.c b/grub-core/commands/smbios.c
index 7a6a391fc..1a9086ddd 100644
--- a/grub-core/commands/smbios.c
+++ b/grub-core/commands/smbios.c
@@ -64,6 +64,21 @@ grub_smbios_get_eps3 (void)
   return eps;
 }
 
+static char *
+linux_string (const char *value)
+{
+  char *out = grub_malloc( grub_strlen (value) + 1);
+  const char *src = value;
+  char *dst = out;
+
+  for (; *src; src++)
+    if (*src > ' ' && *src < 127 && *src != ':')
+      *dst++ = *src;
+
+  *dst = 0;
+  return out;
+}
+
 /*
  * These functions convert values from the various SMBIOS structure field types
  * into a string formatted to be returned to the user.  They expect that the
@@ -176,6 +191,7 @@ static const struct {
 /* List command options, with structure field getters ordered as above. */
 #define FIRST_GETTER_OPT (3)
 #define SETTER_OPT (FIRST_GETTER_OPT + ARRAY_SIZE(field_extractors))
+#define LINUX_OPT (FIRST_GETTER_OPT + ARRAY_SIZE(field_extractors) + 1)
 
 static const struct grub_arg_option options[] = {
   {"type",       't', 0, N_("Match structures with the given type."),
@@ -198,6 +214,8 @@ static const struct grub_arg_option options[] = {
                          N_("offset"), ARG_TYPE_INT},
   {"set",       '\0', 0, N_("Store the value in the given variable name."),
                          N_("variable"), ARG_TYPE_STRING},
+  {"linux",     '\0', 0, N_("Filter the result like linux does."),
+                         N_("variable"), ARG_TYPE_NONE},
   {0, 0, 0, 0, 0, 0}
 };
 
@@ -261,6 +279,7 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
 
   const grub_uint8_t *structure;
   const char *value;
+  char *modified_value = NULL;
   grub_int32_t option;
   grub_int8_t field_type = -1;
   grub_uint8_t i;
@@ -334,12 +353,17 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
     return grub_error (GRUB_ERR_IO,
                        N_("failed to retrieve the structure field"));
 
+  if (state[LINUX_OPT].set)
+    value = modified_value = linux_string (value);
+
   /* Store or print the formatted value. */
   if (state[SETTER_OPT].set)
     grub_env_set (state[SETTER_OPT].arg, value);
   else
     grub_printf ("%s\n", value);
 
+  grub_free(modified_value);
+
   return GRUB_ERR_NONE;
 }
 
-- 
2.25.0



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] smbios: Add a --linux argument to apply linux modalias-like filtering
  2020-03-03 15:06 ` [PATCH v2] " Julian Andres Klode
@ 2020-03-03 16:44   ` Daniel Kiper
  2020-03-03 16:51     ` Julian Andres Klode
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Kiper @ 2020-03-03 16:44 UTC (permalink / raw)
  To: Julian Andres Klode; +Cc: grub-devel

On Tue, Mar 03, 2020 at 04:06:34PM +0100, Julian Andres Klode wrote:
> Linux creates modalias strings by filtering out non-ASCII, space,
> and colon characters. Provide an option that does the same filtering
> so people can create a modalias string in grub, and then match their
> modalias patterns against it.
>
> Signed-off-by: Julian Andres Klode <julian.klode@canonical.com>
> ---
>  grub-core/commands/smbios.c | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
>
> v2: Coding style, sorry about first one - got too excited about getting
> this working, I guess.

No problem, I know how it works...

> diff --git a/grub-core/commands/smbios.c b/grub-core/commands/smbios.c
> index 7a6a391fc..1a9086ddd 100644
> --- a/grub-core/commands/smbios.c
> +++ b/grub-core/commands/smbios.c
> @@ -64,6 +64,21 @@ grub_smbios_get_eps3 (void)
>    return eps;
>  }
>
> +static char *
> +linux_string (const char *value)
> +{
> +  char *out = grub_malloc( grub_strlen (value) + 1);
> +  const char *src = value;
> +  char *dst = out;
> +
> +  for (; *src; src++)
> +    if (*src > ' ' && *src < 127 && *src != ':')
> +      *dst++ = *src;
> +
> +  *dst = 0;
> +  return out;
> +}
> +
>  /*
>   * These functions convert values from the various SMBIOS structure field types
>   * into a string formatted to be returned to the user.  They expect that the
> @@ -176,6 +191,7 @@ static const struct {
>  /* List command options, with structure field getters ordered as above. */
>  #define FIRST_GETTER_OPT (3)
>  #define SETTER_OPT (FIRST_GETTER_OPT + ARRAY_SIZE(field_extractors))
> +#define LINUX_OPT (FIRST_GETTER_OPT + ARRAY_SIZE(field_extractors) + 1)
>
>  static const struct grub_arg_option options[] = {
>    {"type",       't', 0, N_("Match structures with the given type."),
> @@ -198,6 +214,8 @@ static const struct grub_arg_option options[] = {
>                           N_("offset"), ARG_TYPE_INT},
>    {"set",       '\0', 0, N_("Store the value in the given variable name."),
>                           N_("variable"), ARG_TYPE_STRING},
> +  {"linux",     '\0', 0, N_("Filter the result like linux does."),
> +                         N_("variable"), ARG_TYPE_NONE},
>    {0, 0, 0, 0, 0, 0}
>  };
>
> @@ -261,6 +279,7 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
>
>    const grub_uint8_t *structure;
>    const char *value;
> +  char *modified_value = NULL;
>    grub_int32_t option;
>    grub_int8_t field_type = -1;
>    grub_uint8_t i;
> @@ -334,12 +353,17 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
>      return grub_error (GRUB_ERR_IO,
>                         N_("failed to retrieve the structure field"));
>
> +  if (state[LINUX_OPT].set)
> +    value = modified_value = linux_string (value);
> +
>    /* Store or print the formatted value. */
>    if (state[SETTER_OPT].set)
>      grub_env_set (state[SETTER_OPT].arg, value);
>    else
>      grub_printf ("%s\n", value);
>
> +  grub_free(modified_value);

I am not sure why we need modified_value if you free it immediately here...

Daniel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] smbios: Add a --linux argument to apply linux modalias-like filtering
  2020-03-03 16:44   ` Daniel Kiper
@ 2020-03-03 16:51     ` Julian Andres Klode
  2020-03-03 17:23       ` Daniel Kiper
  0 siblings, 1 reply; 5+ messages in thread
From: Julian Andres Klode @ 2020-03-03 16:51 UTC (permalink / raw)
  To: Daniel Kiper; +Cc: grub-devel

On Tue, Mar 03, 2020 at 05:44:54PM +0100, Daniel Kiper wrote:
> On Tue, Mar 03, 2020 at 04:06:34PM +0100, Julian Andres Klode wrote:
> > @@ -261,6 +279,7 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
> >
> >    const grub_uint8_t *structure;
> >    const char *value;
> > +  char *modified_value = NULL;
> >    grub_int32_t option;
> >    grub_int8_t field_type = -1;
> >    grub_uint8_t i;
> > @@ -334,12 +353,17 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
> >      return grub_error (GRUB_ERR_IO,
> >                         N_("failed to retrieve the structure field"));
> >
> > +  if (state[LINUX_OPT].set)
> > +    value = modified_value = linux_string (value);
> > +
> >    /* Store or print the formatted value. */
> >    if (state[SETTER_OPT].set)
> >      grub_env_set (state[SETTER_OPT].arg, value);
> >    else
> >      grub_printf ("%s\n", value);
> >
> > +  grub_free(modified_value);
> 
> I am not sure why we need modified_value if you free it immediately here...

Because `value` is `const char *` that might point to static buffers or
other buffers, so we need to store this dynamically allocated string
somewhere else we can free later.

I guess we could also store the buffer statically with a size and realloc
it if it's too small, thus reusing it and not having to free it, but this
approach seemed slightly easier.

-- 
debian developer - deb.li/jak | jak-linux.org - free software dev
ubuntu core developer                              i speak de, en


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] smbios: Add a --linux argument to apply linux modalias-like filtering
  2020-03-03 16:51     ` Julian Andres Klode
@ 2020-03-03 17:23       ` Daniel Kiper
  0 siblings, 0 replies; 5+ messages in thread
From: Daniel Kiper @ 2020-03-03 17:23 UTC (permalink / raw)
  To: Julian Andres Klode; +Cc: grub-devel

On Tue, Mar 03, 2020 at 05:51:55PM +0100, Julian Andres Klode wrote:
> On Tue, Mar 03, 2020 at 05:44:54PM +0100, Daniel Kiper wrote:
> > On Tue, Mar 03, 2020 at 04:06:34PM +0100, Julian Andres Klode wrote:
> > > @@ -261,6 +279,7 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
> > >
> > >    const grub_uint8_t *structure;
> > >    const char *value;
> > > +  char *modified_value = NULL;
> > >    grub_int32_t option;
> > >    grub_int8_t field_type = -1;
> > >    grub_uint8_t i;
> > > @@ -334,12 +353,17 @@ grub_cmd_smbios (grub_extcmd_context_t ctxt,
> > >      return grub_error (GRUB_ERR_IO,
> > >                         N_("failed to retrieve the structure field"));
> > >
> > > +  if (state[LINUX_OPT].set)
> > > +    value = modified_value = linux_string (value);
> > > +
> > >    /* Store or print the formatted value. */
> > >    if (state[SETTER_OPT].set)
> > >      grub_env_set (state[SETTER_OPT].arg, value);
> > >    else
> > >      grub_printf ("%s\n", value);
> > >
> > > +  grub_free(modified_value);
> >
> > I am not sure why we need modified_value if you free it immediately here...
>
> Because `value` is `const char *` that might point to static buffers or
> other buffers, so we need to store this dynamically allocated string
> somewhere else we can free later.

Ahhh... Right, then Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>

Daniel


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-03-03 17:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-03 14:54 [PATCH] smbios: Add a --linux argument to apply linux modalias-like filtering Julian Andres Klode
2020-03-03 15:06 ` [PATCH v2] " Julian Andres Klode
2020-03-03 16:44   ` Daniel Kiper
2020-03-03 16:51     ` Julian Andres Klode
2020-03-03 17:23       ` Daniel Kiper

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.