
From: Simon Glass <sjg@chromium.org> The 'smbios' command has some parsing code, as does the SMBIOS parser in the lib/ directory. Start to unify these by moving over a few functions. Require CONFIG_SMBIOS_PARSER to be enabled when using the command. Signed-off-by: Simon Glass <sjg@chromium.org> --- (no changes since v1) cmd/Kconfig | 1 + cmd/smbios.c | 44 ++++++-------------------------------------- include/smbios.h | 17 +++++++++++++++++ lib/smbios-parser.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 38 deletions(-) diff --git a/cmd/Kconfig b/cmd/Kconfig index 37f47e5976e..4d564ab5ac6 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -267,6 +267,7 @@ config CMD_SBI config CMD_SMBIOS bool "smbios" depends on SMBIOS + select SMBIOS_PARSER help Display the SMBIOS information. diff --git a/cmd/smbios.c b/cmd/smbios.c index 562dd7959be..8dd31190a01 100644 --- a/cmd/smbios.c +++ b/cmd/smbios.c @@ -6,6 +6,7 @@ */ #include <command.h> +#include <errno.h> #include <hexdump.h> #include <mapmem.h> #include <smbios.h> @@ -119,42 +120,7 @@ static const struct str_lookup_table associativity_strings[] = { }; -/** - * smbios_get_string() - get SMBIOS string from table - * - * @table: SMBIOS table - * @index: index of the string - * Return: address of string, may point to empty string - */ -static const char *smbios_get_string(void *table, int index) -{ - const char *str = (char *)table + - ((struct smbios_header *)table)->length; - static const char fallback[] = ""; - - if (!index) - return fallback; - - if (!*str) - ++str; - for (--index; *str && index; --index) - str += strlen(str) + 1; - - return str; -} - -static struct smbios_header *next_table(struct smbios_header *table) -{ - const char *str; - - if (table->type == SMBIOS_END_OF_TABLE) - return NULL; - - str = smbios_get_string(table, -1); - return (struct smbios_header *)(++str); -} - -static void smbios_print_generic(struct smbios_header *table) +static void smbios_print_generic(const struct smbios_header *table) { char *str = (char *)table + table->length; @@ -454,12 +420,14 @@ static int do_smbios(struct cmd_tbl *cmdtp, int flag, int argc, } printf("SMBIOS %s present.\n", version); - for (struct smbios_header *pos = table; pos; pos = next_table(pos)) + for (struct smbios_header *pos = table; pos; + pos = smbios_next_table(pos)) ++count; printf("%zd structures occupying %d bytes\n", count, table_maximum_size); printf("Table at 0x%llx\n", (unsigned long long)map_to_sysmem(table)); - for (struct smbios_header *pos = table; pos; pos = next_table(pos)) { + for (struct smbios_header *pos = table; pos; + pos = smbios_next_table(pos)) { printf("\nHandle 0x%04x, DMI type %d, %d bytes at 0x%llx\n", pos->handle, pos->type, pos->length, (unsigned long long)map_to_sysmem(pos)); diff --git a/include/smbios.h b/include/smbios.h index 41e25bd31b3..1415de1c5d0 100644 --- a/include/smbios.h +++ b/include/smbios.h @@ -384,4 +384,21 @@ int smbios_update_version_full(void *smbios_tab, const char *version); void smbios_prepare_measurement(const struct smbios3_entry *entry, struct smbios_header *header); +/** + * smbios_get_string() - get SMBIOS string from table + * + * @table: SMBIOS table + * @index: index of the string + * Return: address of string, may point to empty string + */ +const char *smbios_get_string(void *table, int index); + +/** + * smbios_next_table() - Find the next table + * + * @table: Table to start from + * Return: Pointer to the next table, or NULL if @table is the last + */ +struct smbios_header *smbios_next_table(struct smbios_header *table); + #endif /* _SMBIOS_H_ */ diff --git a/lib/smbios-parser.c b/lib/smbios-parser.c index 2db90d8235b..42c095f3d08 100644 --- a/lib/smbios-parser.c +++ b/lib/smbios-parser.c @@ -11,6 +11,34 @@ #include <tables_csum.h> #include <linux/kernel.h> +const char *smbios_get_string(void *table, int index) +{ + const char *str = (char *)table + + ((struct smbios_header *)table)->length; + static const char fallback[] = ""; + + if (!index) + return fallback; + + if (!*str) + ++str; + for (--index; *str && index; --index) + str += strlen(str) + 1; + + return str; +} + +struct smbios_header *smbios_next_table(struct smbios_header *table) +{ + const char *str; + + if (table->type == SMBIOS_END_OF_TABLE) + return NULL; + + str = smbios_get_string(table, -1); + return (struct smbios_header *)(++str); +} + const struct smbios_entry *smbios_entry(u64 address, u32 size) { const struct smbios_entry *entry = (struct smbios_entry *)(uintptr_t)address; -- 2.43.0