Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1j99fI-0001ex-Bb for mharc-grub-devel@gnu.org; Tue, 03 Mar 2020 10:41:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40857) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j98w8-00074H-HI for grub-devel@gnu.org; Tue, 03 Mar 2020 09:54:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j98w6-0005RM-91 for grub-devel@gnu.org; Tue, 03 Mar 2020 09:54:20 -0500 Received: from youngberry.canonical.com ([91.189.89.112]:58571) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1j98w6-0005Qz-3L for grub-devel@gnu.org; Tue, 03 Mar 2020 09:54:18 -0500 Received: from mail-wm1-f69.google.com ([209.85.128.69]) by youngberry.canonical.com with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1j98w4-0004Ur-0w for grub-devel@gnu.org; Tue, 03 Mar 2020 14:54:16 +0000 Received: by mail-wm1-f69.google.com with SMTP id d129so1192010wmd.2 for ; Tue, 03 Mar 2020 06:54:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=2gtkQmVTu6DCnsY4QTXU+DcUz3c6k/3U8N+XCTyF1LQ=; b=IW6hJ19ePLjUttS/PuYa8dsU3kjyBlVIDB1HD1tP2RFVrVv/nXzKUeu8ylPPYUtMKO ZA5hcRrRPoTsZip2L4Mr7s7Gl2jvLcmFOcoYj/wDUG3bv7DDsFeGhEhU1IAak9qkzvDt ingTh8knAq+1Bhq+nVRSRZ/FIfJ5OZMDkgZS/XM27DWgwnr+oOcAPZO3GXor1T+iR1VU pIf9p4dyP1xVHUZHyAy8Rv3qojxcugp1uFGIiRaWdO9Era+qh1LovbcI6C2qJRfhJklp U6hn4yjs63FmH70YiHjJnqRbKNqzjVT0emUoq8xKDvmuujhC8KHTSKn++PKpCdxDV5Uz HryQ== X-Gm-Message-State: ANhLgQ02Gv7RBy+i66IZlbMzR2xsYxi1CTjiyVUJqeagTt+hWjA02cOy m1HpjS+LciS2gmkbqTVDKpiLpTjvacnn+gpaqXOEkDRt0YX+orMQb3Ttj6rVlvZRle3HaK79op2 S36pEHLEPoehKwHstell3aDyL36hi X-Received: by 2002:a5d:42c8:: with SMTP id t8mr5680965wrr.261.1583247255668; Tue, 03 Mar 2020 06:54:15 -0800 (PST) X-Google-Smtp-Source: ADFU+vvsNfBhZnFikyrx7sUL83DS7bxAUd/lzpnwntWFW9ieQhNkAsdIYDx8rzIVEB1F8LGBIV7dQg== X-Received: by 2002:a5d:42c8:: with SMTP id t8mr5680948wrr.261.1583247255404; Tue, 03 Mar 2020 06:54:15 -0800 (PST) Received: from jak-t480s (b2b-5-147-251-51.unitymedia.biz. [5.147.251.51]) by smtp.gmail.com with ESMTPSA id g206sm4347323wme.46.2020.03.03.06.54.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2020 06:54:14 -0800 (PST) From: Julian Andres Klode To: grub-devel@gnu.org Cc: Julian Andres Klode Subject: [PATCH] smbios: Add a --linux argument to apply linux modalias-like filtering Date: Tue, 3 Mar 2020 15:54:06 +0100 Message-Id: <20200303145406.852454-1-julian.klode@canonical.com> X-Mailer: git-send-email 2.25.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 91.189.89.112 X-Mailman-Approved-At: Tue, 03 Mar 2020 10:40:58 -0500 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Mar 2020 14:54:21 -0000 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 --- 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