[PATCH v6 1/6] smbios: Fix duplicated smbios handles

Raymond Mao
2026-02-13T22:53:24Z

Some smbios types can have multiple instances (e.g. Type 7, 9, 16, 17,
19), thus the 'handle' argument should be a pointer so that the value
can be accumulated when writing all the instances.
This also fix the observed duplicated Type 7 handles.

Fixes: bcf456dd369e ("smbios: add detailed smbios information")
Signed-off-by: Raymond Mao <raymondmaoca@gmail.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
Changes in v3:
- Initial patch.
Changes in v4:
- None.
Changes in v5:
- None.
Changes in v6:
- None.

lib/smbios.c | 42 +++++++++++++++++++++++-------------------
1 file changed, 23 insertions(+), 19 deletions(-)

diff --git a/lib/smbios.c b/lib/smbios.c
index b8c2846277a..85508c06547 100644
--- a/lib/smbios.c
+++ b/lib/smbios.c
@@ -103,7 +103,7 @@ struct smbios_ctx {
* @ctx: context for writing the tables
* Return: size of the structure
*/
-typedef int (*smbios_write_type)(ulong *addr, int handle,
+typedef int (*smbios_write_type)(ulong *addr, int *handle,
struct smbios_ctx *ctx);

/**
@@ -364,7 +364,7 @@ static int smbios_string_table_len(const struct smbios_ctx *ctx)
return (ctx->next_ptr + 1) - ctx->eos;
}

-static int smbios_write_type0(ulong *current, int handle,
+static int smbios_write_type0(ulong *current, int *handle,
struct smbios_ctx *ctx)
{
struct smbios_type0 *t;
@@ -372,7 +372,7 @@ static int smbios_write_type0(ulong *current, int handle,

t = map_sysmem(*current, len);
memset(t, 0, len);
- fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
+ fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, *handle);
smbios_set_eos(ctx, t->eos);
t->vendor = smbios_add_prop_si(ctx, NULL, SYSID_SM_BIOS_VENDOR,
"U-Boot");
@@ -423,7 +423,7 @@ static int smbios_write_type0(ulong *current, int handle,
return len;
}

-static int smbios_write_type1(ulong *current, int handle,
+static int smbios_write_type1(ulong *current, int *handle,
struct smbios_ctx *ctx)
{
struct smbios_type1 *t;
@@ -434,7 +434,7 @@ static int smbios_write_type1(ulong *current, int handle,

t = map_sysmem(*current, len);
memset(t, 0, len);
- fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
+ fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, *handle);
smbios_set_eos(ctx, t->eos);

t->manufacturer = smbios_add_prop_si(ctx, "manufacturer",
@@ -471,7 +471,7 @@ static int smbios_write_type1(ulong *current, int handle,
return len;
}

-static int smbios_write_type2(ulong *current, int handle,
+static int smbios_write_type2(ulong *current, int *handle,
struct smbios_ctx *ctx)
{
struct smbios_type2 *t;
@@ -485,7 +485,7 @@ static int smbios_write_type2(ulong *current, int handle,
*/
t = map_sysmem(*current, len);
memset(t, 0, len);
- fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
+ fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, *handle);

/* eos is at the end of the structure */
eos_addr = (u8 *)t + len - sizeof(t->eos);
@@ -519,7 +519,7 @@ static int smbios_write_type2(ulong *current, int handle,
* t->number_contained_objects = <obj_handle_num>;
*/

- t->chassis_handle = handle + 1;
+ t->chassis_handle = *handle + 1;

len = t->hdr.length + smbios_string_table_len(ctx);
*current += len;
@@ -528,7 +528,7 @@ static int smbios_write_type2(ulong *current, int handle,
return len;
}

-static int smbios_write_type3(ulong *current, int handle,
+static int smbios_write_type3(ulong *current, int *handle,
struct smbios_ctx *ctx)
{
struct smbios_type3 *t;
@@ -548,7 +548,7 @@ static int smbios_write_type3(ulong *current, int handle,

t = map_sysmem(*current, len);
memset(t, 0, len);
- fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
+ fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, *handle);
#if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE)
elem_addr = (u8 *)t + offsetof(struct smbios_type3, sku_number);
sku_num_addr = elem_addr + elem_size;
@@ -669,7 +669,7 @@ static void smbios_write_type4_dm(struct smbios_type4 *t,
#endif
}

-static int smbios_write_type4(ulong *current, int handle,
+static int smbios_write_type4(ulong *current, int *handle,
struct smbios_ctx *ctx)
{
struct smbios_type4 *t;
@@ -679,7 +679,7 @@ static int smbios_write_type4(ulong *current, int handle,

t = map_sysmem(*current, len);
memset(t, 0, len);
- fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
+ fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, *handle);
smbios_set_eos(ctx, t->eos);
t->socket_design = smbios_add_prop_si(ctx, "socket-design",
SYSID_SM_PROCESSOR_SOCKET, NULL);
@@ -828,13 +828,14 @@ static int smbios_write_type7_1level(ulong *current, int handle,
return len;
}

-static int smbios_write_type7(ulong *current, int handle,
+static int smbios_write_type7(ulong *current, int *handle,
struct smbios_ctx *ctx)
{
int len = 0;
int i, level;
ofnode parent = ctx->node;
struct smbios_ctx ctx_bak;
+ int hdl_base = *handle;

memcpy(&ctx_bak, ctx, sizeof(ctx_bak));

@@ -850,15 +851,17 @@ static int smbios_write_type7(ulong *current, int handle,
return 0;
ctx->subnode_name = buf;
ctx->node = ofnode_find_subnode(parent, ctx->subnode_name);
- len += smbios_write_type7_1level(current, handle++, ctx, i);
+ *handle = hdl_base + i;
+ len += smbios_write_type7_1level(current, *handle, ctx, i);
memcpy(ctx, &ctx_bak, sizeof(*ctx));
}
+
return len;
}

#endif /* #if IS_ENABLED(CONFIG_GENERATE_SMBIOS_TABLE_VERBOSE) */

-static int smbios_write_type32(ulong *current, int handle,
+static int smbios_write_type32(ulong *current, int *handle,
struct smbios_ctx *ctx)
{
struct smbios_type32 *t;
@@ -866,7 +869,7 @@ static int smbios_write_type32(ulong *current, int handle,

t = map_sysmem(*current, len);
memset(t, 0, len);
- fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
+ fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, *handle);
smbios_set_eos(ctx, t->eos);

*current += len;
@@ -875,7 +878,7 @@ static int smbios_write_type32(ulong *current, int handle,
return len;
}

-static int smbios_write_type127(ulong *current, int handle,
+static int smbios_write_type127(ulong *current, int *handle,
struct smbios_ctx *ctx)
{
struct smbios_type127 *t;
@@ -883,7 +886,7 @@ static int smbios_write_type127(ulong *current, int handle,

t = map_sysmem(*current, len);
memset(t, 0, len);
- fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
+ fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, *handle);

*current += len;
unmap_sysmem(t);
@@ -954,7 +957,8 @@ ulong write_smbios_table(ulong addr)
ctx.node = ofnode_find_subnode(parent_node,
method->subnode_name);
}
- len += method->write((ulong *)&addr, handle++, &ctx);
+ len += method->write((ulong *)&addr, &handle, &ctx);
+ handle++;
}

/*
--
2.25.1