All of lore.kernel.org
 help / color / mirror / Atom feed
From: Simon Glass <sjg@chromium.org>
To: u-boot@lists.denx.de
Subject: [PATCH v3 09/12] smbios: Add more options for the BIOS version string
Date: Sun, 24 Jan 2021 10:43:28 -0700	[thread overview]
Message-ID: <20210124104327.v3.9.I8018bd2c82cadc8ec924e7302dee4d0901b0a822@changeid> (raw)
In-Reply-To: <20210124174331.3462226-1-sjg@chromium.org>

At present the version string is obtained from PLAIN_VERSION. Some boards
may want to configure this using the device tree, since the build system
can more easily insert things there after U-Boot itself is built. Add this
option to the code.

Also in some cases the version needs to be generated programmatically,
such as when it is stored elsewhere in the ROM and must be read first.
To handle this, keep a pointer around so that it can be updated later.
This works by storing the last string in the context, since it is easier
than passing out a little-used extra parameter.

Provide a function to update the version string.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
---

Changes in v3:
- Add missing DECLARE_GLOBAL_DATA_PTR

Changes in v2:
- Correct documentation format

 include/asm-generic/global_data.h |  6 ++++
 include/smbios.h                  | 12 +++++++
 lib/smbios.c                      | 58 +++++++++++++++++++++++++++++--
 3 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/include/asm-generic/global_data.h b/include/asm-generic/global_data.h
index 19f70393b45..750e998d885 100644
--- a/include/asm-generic/global_data.h
+++ b/include/asm-generic/global_data.h
@@ -439,6 +439,12 @@ struct global_data {
 	 */
 	struct acpi_ctx *acpi_ctx;
 #endif
+#if CONFIG_IS_ENABLED(GENERATE_SMBIOS_TABLE)
+	/**
+	 * @smbios_version: Points to SMBIOS type 0 version
+	 */
+	char *smbios_version;
+#endif
 };
 
 /**
diff --git a/include/smbios.h b/include/smbios.h
index 1cbeabf9522..ecc4fd1de3b 100644
--- a/include/smbios.h
+++ b/include/smbios.h
@@ -257,4 +257,16 @@ const struct smbios_header *smbios_header(const struct smbios_entry *entry, int
  */
 const char *smbios_string(const struct smbios_header *header, int index);
 
+/**
+ * smbios_update_version() - Update the version string
+ *
+ * This can be called after the SMBIOS tables are written (e.g. after the U-Boot
+ * main loop has started) to update the BIOS version string (SMBIOS table 0).
+ *
+ * @version: New version string to use
+ * @return 0 if OK, -ENOENT if no version string was previously written,
+ *	-ENOSPC if the new string is too large to fit
+ */
+int smbios_update_version(const char *version);
+
 #endif /* _SMBIOS_H_ */
diff --git a/lib/smbios.c b/lib/smbios.c
index a7273529cc4..e8bfc9ee1ce 100644
--- a/lib/smbios.c
+++ b/lib/smbios.c
@@ -17,6 +17,12 @@
 #include <dm/uclass-internal.h>
 #endif
 
+DECLARE_GLOBAL_DATA_PTR;
+
+enum {
+	SMBIOS_STR_MAX	= 64,	/* Maximum length allowed for a string */
+};
+
 /**
  * struct smbios_ctx - context for writing SMBIOS tables
  *
@@ -27,12 +33,15 @@
  * @next_ptr:	pointer to the start of the next string to be added. When the
  *		table is nopt empty, this points to the byte after the \0 of the
  *		previous string.
+ * @last_str:	points to the last string that was written to the table, or NULL
+ *		if none
  */
 struct smbios_ctx {
 	ofnode node;
 	struct udevice *dev;
 	char *eos;
 	char *next_ptr;
+	char *last_str;
 };
 
 /**
@@ -78,6 +87,7 @@ static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
 
 	for (;;) {
 		if (!*p) {
+			ctx->last_str = p;
 			strcpy(p, str);
 			p += strlen(str);
 			*p++ = '\0';
@@ -87,8 +97,10 @@ static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
 			return i;
 		}
 
-		if (!strcmp(p, str))
+		if (!strcmp(p, str)) {
+			ctx->last_str = p;
 			return i;
+		}
 
 		p += strlen(p) + 1;
 		i++;
@@ -119,6 +131,35 @@ static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
 {
 	ctx->eos = eos;
 	ctx->next_ptr = eos;
+	ctx->last_str = NULL;
+}
+
+int smbios_update_version(const char *version)
+{
+	char *ptr = gd->smbios_version;
+	uint old_len, len;
+
+	if (!ptr)
+		return log_ret(-ENOENT);
+
+	/*
+	 * This string is supposed to have at least enough bytes and is
+	 * padded with spaces. Update it, taking care not to move the
+	 * \0 terminator, so that other strings in the string table
+	 * are not disturbed. See smbios_add_string()
+	 */
+	old_len = strnlen(ptr, SMBIOS_STR_MAX);
+	len = strnlen(version, SMBIOS_STR_MAX);
+	if (len > old_len)
+		return log_ret(-ENOSPC);
+
+	log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
+	memcpy(ptr, version, len);
+#ifdef LOG_DEBUG
+	print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
+#endif
+
+	return 0;
 }
 
 /**
@@ -146,7 +187,18 @@ static int smbios_write_type0(ulong *current, int handle,
 	fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
 	smbios_set_eos(ctx, t->eos);
 	t->vendor = smbios_add_string(ctx, "U-Boot");
-	t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
+
+	t->bios_ver = smbios_add_prop(ctx, "version");
+	if (!t->bios_ver)
+		t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
+	if (t->bios_ver)
+		gd->smbios_version = ctx->last_str;
+	log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
+		  gd->smbios_version);
+#ifdef LOG_DEBUG
+	print_buffer((ulong)gd->smbios_version, gd->smbios_version,
+		     1, strlen(gd->smbios_version) + 1, 0);
+#endif
 	t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
 #ifdef CONFIG_ROM_SIZE
 	t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
@@ -345,7 +397,7 @@ static int smbios_write_type127(ulong *current, int handle,
 }
 
 static struct smbios_write_method smbios_write_funcs[] = {
-	{ smbios_write_type0, },
+	{ smbios_write_type0, "bios", },
 	{ smbios_write_type1, "system", },
 	{ smbios_write_type2, "baseboard", },
 	{ smbios_write_type3, "chassis", },
-- 
2.30.0.280.ga3ce27912f-goog

  parent reply	other threads:[~2021-01-24 17:43 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-24 17:43 [PATCH v3 00/12] smbios: Enhancements for more flexibility Simon Glass
2021-01-24 17:43 ` [PATCH v3 01/12] README: Add doumentation for version information Simon Glass
2021-02-01  7:17   ` Bin Meng
2021-01-24 17:43 ` [PATCH v3 02/12] Makefile: Provide numeric versions Simon Glass
2021-01-24 17:43 ` [PATCH v3 03/12] smbios: Move smbios_write_type to the C file Simon Glass
2021-01-24 17:43 ` [PATCH v3 04/12] smbios: Use char consistently for the eos member Simon Glass
2021-01-24 17:43 ` [PATCH v3 05/12] smbios: Set BIOS release version Simon Glass
2021-01-24 17:43 ` [PATCH v3 06/12] smbios: Use a struct to keep track of context Simon Glass
2021-01-24 17:43 ` [PATCH v3 07/12] smbios: Drop the eos parameter Simon Glass
2021-02-01  7:25   ` Bin Meng
2021-01-24 17:43 ` [PATCH v3 08/12] smbios: Track the end of the string table Simon Glass
2021-01-24 17:43 ` Simon Glass [this message]
2021-02-01  7:25   ` [PATCH v3 09/12] smbios: Add more options for the BIOS version string Bin Meng
2021-01-24 17:43 ` [PATCH v3 10/12] sysinfo: Move #ifdef so that operations are always defined Simon Glass
2021-01-24 17:43 ` [PATCH v3 11/12] x86: coral: Add sysinfo ops Simon Glass
2021-02-01  7:25   ` Bin Meng
2021-01-24 17:43 ` [PATCH v3 12/12] smbios: Allow a few values to come from sysinfo Simon Glass
2021-02-01  7:25   ` Bin Meng
2021-02-05  3:17     ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210124104327.v3.9.I8018bd2c82cadc8ec924e7302dee4d0901b0a822@changeid \
    --to=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.