]> git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/smbios.c
smbios: Add more options for the BIOS version string
[thirdparty/u-boot.git] / lib / smbios.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4 *
5 * Adapted from coreboot src/arch/x86/smbios.c
6 */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <env.h>
11 #include <mapmem.h>
12 #include <smbios.h>
13 #include <tables_csum.h>
14 #include <version.h>
15 #ifdef CONFIG_CPU
16 #include <cpu.h>
17 #include <dm/uclass-internal.h>
18 #endif
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 enum {
23 SMBIOS_STR_MAX = 64, /* Maximum length allowed for a string */
24 };
25
26 /**
27 * struct smbios_ctx - context for writing SMBIOS tables
28 *
29 * @node: node containing the information to write (ofnode_null() if none)
30 * @dev: sysinfo device to use (NULL if none)
31 * @eos: end-of-string pointer for the table being processed. This is set
32 * up when we start processing a table
33 * @next_ptr: pointer to the start of the next string to be added. When the
34 * table is nopt empty, this points to the byte after the \0 of the
35 * previous string.
36 * @last_str: points to the last string that was written to the table, or NULL
37 * if none
38 */
39 struct smbios_ctx {
40 ofnode node;
41 struct udevice *dev;
42 char *eos;
43 char *next_ptr;
44 char *last_str;
45 };
46
47 /**
48 * Function prototype to write a specific type of SMBIOS structure
49 *
50 * @addr: start address to write the structure
51 * @handle: the structure's handle, a unique 16-bit number
52 * @ctx: context for writing the tables
53 * @return: size of the structure
54 */
55 typedef int (*smbios_write_type)(ulong *addr, int handle,
56 struct smbios_ctx *ctx);
57
58 /**
59 * struct smbios_write_method - Information about a table-writing function
60 *
61 * @write: Function to call
62 * @subnode_name: Name of subnode which has the information for this function,
63 * NULL if none
64 */
65 struct smbios_write_method {
66 smbios_write_type write;
67 const char *subnode_name;
68 };
69
70 /**
71 * smbios_add_string() - add a string to the string area
72 *
73 * This adds a string to the string area which is appended directly after
74 * the formatted portion of an SMBIOS structure.
75 *
76 * @ctx: SMBIOS context
77 * @str: string to add
78 * @return: string number in the string area (1 or more)
79 */
80 static int smbios_add_string(struct smbios_ctx *ctx, const char *str)
81 {
82 int i = 1;
83 char *p = ctx->eos;
84
85 if (!*str)
86 str = "Unknown";
87
88 for (;;) {
89 if (!*p) {
90 ctx->last_str = p;
91 strcpy(p, str);
92 p += strlen(str);
93 *p++ = '\0';
94 ctx->next_ptr = p;
95 *p++ = '\0';
96
97 return i;
98 }
99
100 if (!strcmp(p, str)) {
101 ctx->last_str = p;
102 return i;
103 }
104
105 p += strlen(p) + 1;
106 i++;
107 }
108 }
109
110 /**
111 * smbios_add_prop() - Add a property from the device tree
112 *
113 * @ctx: context for writing the tables
114 * @prop: property to write
115 * @return 0 if not found, else SMBIOS string number (1 or more)
116 */
117 static int smbios_add_prop(struct smbios_ctx *ctx, const char *prop)
118 {
119 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
120 const char *str;
121
122 str = ofnode_read_string(ctx->node, prop);
123 if (str)
124 return smbios_add_string(ctx, str);
125 }
126
127 return 0;
128 }
129
130 static void smbios_set_eos(struct smbios_ctx *ctx, char *eos)
131 {
132 ctx->eos = eos;
133 ctx->next_ptr = eos;
134 ctx->last_str = NULL;
135 }
136
137 int smbios_update_version(const char *version)
138 {
139 char *ptr = gd->smbios_version;
140 uint old_len, len;
141
142 if (!ptr)
143 return log_ret(-ENOENT);
144
145 /*
146 * This string is supposed to have at least enough bytes and is
147 * padded with spaces. Update it, taking care not to move the
148 * \0 terminator, so that other strings in the string table
149 * are not disturbed. See smbios_add_string()
150 */
151 old_len = strnlen(ptr, SMBIOS_STR_MAX);
152 len = strnlen(version, SMBIOS_STR_MAX);
153 if (len > old_len)
154 return log_ret(-ENOSPC);
155
156 log_debug("Replacing SMBIOS type 0 version string '%s'\n", ptr);
157 memcpy(ptr, version, len);
158 #ifdef LOG_DEBUG
159 print_buffer((ulong)ptr, ptr, 1, old_len + 1, 0);
160 #endif
161
162 return 0;
163 }
164
165 /**
166 * smbios_string_table_len() - compute the string area size
167 *
168 * This computes the size of the string area including the string terminator.
169 *
170 * @ctx: SMBIOS context
171 * @return: string area size
172 */
173 static int smbios_string_table_len(const struct smbios_ctx *ctx)
174 {
175 /* Allow for the final \0 after all strings */
176 return (ctx->next_ptr + 1) - ctx->eos;
177 }
178
179 static int smbios_write_type0(ulong *current, int handle,
180 struct smbios_ctx *ctx)
181 {
182 struct smbios_type0 *t;
183 int len = sizeof(struct smbios_type0);
184
185 t = map_sysmem(*current, len);
186 memset(t, 0, sizeof(struct smbios_type0));
187 fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
188 smbios_set_eos(ctx, t->eos);
189 t->vendor = smbios_add_string(ctx, "U-Boot");
190
191 t->bios_ver = smbios_add_prop(ctx, "version");
192 if (!t->bios_ver)
193 t->bios_ver = smbios_add_string(ctx, PLAIN_VERSION);
194 if (t->bios_ver)
195 gd->smbios_version = ctx->last_str;
196 log_debug("smbios_version = %p: '%s'\n", gd->smbios_version,
197 gd->smbios_version);
198 #ifdef LOG_DEBUG
199 print_buffer((ulong)gd->smbios_version, gd->smbios_version,
200 1, strlen(gd->smbios_version) + 1, 0);
201 #endif
202 t->bios_release_date = smbios_add_string(ctx, U_BOOT_DMI_DATE);
203 #ifdef CONFIG_ROM_SIZE
204 t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
205 #endif
206 t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
207 BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
208 BIOS_CHARACTERISTICS_UPGRADEABLE;
209 #ifdef CONFIG_GENERATE_ACPI_TABLE
210 t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
211 #endif
212 #ifdef CONFIG_EFI_LOADER
213 t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
214 #endif
215 t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
216
217 /* bios_major_release has only one byte, so drop century */
218 t->bios_major_release = U_BOOT_VERSION_NUM % 100;
219 t->bios_minor_release = U_BOOT_VERSION_NUM_PATCH;
220 t->ec_major_release = 0xff;
221 t->ec_minor_release = 0xff;
222
223 len = t->length + smbios_string_table_len(ctx);
224 *current += len;
225 unmap_sysmem(t);
226
227 return len;
228 }
229
230 static int smbios_write_type1(ulong *current, int handle,
231 struct smbios_ctx *ctx)
232 {
233 struct smbios_type1 *t;
234 int len = sizeof(struct smbios_type1);
235 char *serial_str = env_get("serial#");
236
237 t = map_sysmem(*current, len);
238 memset(t, 0, sizeof(struct smbios_type1));
239 fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
240 smbios_set_eos(ctx, t->eos);
241 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
242 t->product_name = smbios_add_prop(ctx, "product");
243 t->version = smbios_add_prop(ctx, "version");
244 if (serial_str) {
245 t->serial_number = smbios_add_string(ctx, serial_str);
246 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
247 } else {
248 t->serial_number = smbios_add_prop(ctx, "serial");
249 }
250 t->sku_number = smbios_add_prop(ctx, "sku");
251 t->family = smbios_add_prop(ctx, "family");
252
253 len = t->length + smbios_string_table_len(ctx);
254 *current += len;
255 unmap_sysmem(t);
256
257 return len;
258 }
259
260 static int smbios_write_type2(ulong *current, int handle,
261 struct smbios_ctx *ctx)
262 {
263 struct smbios_type2 *t;
264 int len = sizeof(struct smbios_type2);
265
266 t = map_sysmem(*current, len);
267 memset(t, 0, sizeof(struct smbios_type2));
268 fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
269 smbios_set_eos(ctx, t->eos);
270 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
271 t->product_name = smbios_add_prop(ctx, "product");
272 t->asset_tag_number = smbios_add_prop(ctx, "asset-tag");
273 t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
274 t->board_type = SMBIOS_BOARD_MOTHERBOARD;
275
276 len = t->length + smbios_string_table_len(ctx);
277 *current += len;
278 unmap_sysmem(t);
279
280 return len;
281 }
282
283 static int smbios_write_type3(ulong *current, int handle,
284 struct smbios_ctx *ctx)
285 {
286 struct smbios_type3 *t;
287 int len = sizeof(struct smbios_type3);
288
289 t = map_sysmem(*current, len);
290 memset(t, 0, sizeof(struct smbios_type3));
291 fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
292 smbios_set_eos(ctx, t->eos);
293 t->manufacturer = smbios_add_prop(ctx, "manufacturer");
294 t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
295 t->bootup_state = SMBIOS_STATE_SAFE;
296 t->power_supply_state = SMBIOS_STATE_SAFE;
297 t->thermal_state = SMBIOS_STATE_SAFE;
298 t->security_status = SMBIOS_SECURITY_NONE;
299
300 len = t->length + smbios_string_table_len(ctx);
301 *current += len;
302 unmap_sysmem(t);
303
304 return len;
305 }
306
307 static void smbios_write_type4_dm(struct smbios_type4 *t,
308 struct smbios_ctx *ctx)
309 {
310 u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
311 const char *vendor = "Unknown";
312 const char *name = "Unknown";
313
314 #ifdef CONFIG_CPU
315 char processor_name[49];
316 char vendor_name[49];
317 struct udevice *cpu = NULL;
318
319 uclass_find_first_device(UCLASS_CPU, &cpu);
320 if (cpu) {
321 struct cpu_plat *plat = dev_get_parent_plat(cpu);
322
323 if (plat->family)
324 processor_family = plat->family;
325 t->processor_id[0] = plat->id[0];
326 t->processor_id[1] = plat->id[1];
327
328 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
329 vendor = vendor_name;
330 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
331 name = processor_name;
332 }
333 #endif
334
335 t->processor_family = processor_family;
336 t->processor_manufacturer = smbios_add_string(ctx, vendor);
337 t->processor_version = smbios_add_string(ctx, name);
338 }
339
340 static int smbios_write_type4(ulong *current, int handle,
341 struct smbios_ctx *ctx)
342 {
343 struct smbios_type4 *t;
344 int len = sizeof(struct smbios_type4);
345
346 t = map_sysmem(*current, len);
347 memset(t, 0, sizeof(struct smbios_type4));
348 fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
349 smbios_set_eos(ctx, t->eos);
350 t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
351 smbios_write_type4_dm(t, ctx);
352 t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
353 t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
354 t->l1_cache_handle = 0xffff;
355 t->l2_cache_handle = 0xffff;
356 t->l3_cache_handle = 0xffff;
357 t->processor_family2 = t->processor_family;
358
359 len = t->length + smbios_string_table_len(ctx);
360 *current += len;
361 unmap_sysmem(t);
362
363 return len;
364 }
365
366 static int smbios_write_type32(ulong *current, int handle,
367 struct smbios_ctx *ctx)
368 {
369 struct smbios_type32 *t;
370 int len = sizeof(struct smbios_type32);
371
372 t = map_sysmem(*current, len);
373 memset(t, 0, sizeof(struct smbios_type32));
374 fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
375 smbios_set_eos(ctx, t->eos);
376
377 *current += len;
378 unmap_sysmem(t);
379
380 return len;
381 }
382
383 static int smbios_write_type127(ulong *current, int handle,
384 struct smbios_ctx *ctx)
385 {
386 struct smbios_type127 *t;
387 int len = sizeof(struct smbios_type127);
388
389 t = map_sysmem(*current, len);
390 memset(t, 0, sizeof(struct smbios_type127));
391 fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
392
393 *current += len;
394 unmap_sysmem(t);
395
396 return len;
397 }
398
399 static struct smbios_write_method smbios_write_funcs[] = {
400 { smbios_write_type0, "bios", },
401 { smbios_write_type1, "system", },
402 { smbios_write_type2, "baseboard", },
403 { smbios_write_type3, "chassis", },
404 { smbios_write_type4, },
405 { smbios_write_type32, },
406 { smbios_write_type127 },
407 };
408
409 ulong write_smbios_table(ulong addr)
410 {
411 ofnode parent_node = ofnode_null();
412 struct smbios_entry *se;
413 struct smbios_ctx ctx;
414 ulong table_addr;
415 ulong tables;
416 int len = 0;
417 int max_struct_size = 0;
418 int handle = 0;
419 char *istart;
420 int isize;
421 int i;
422
423 ctx.node = ofnode_null();
424 if (IS_ENABLED(CONFIG_OF_CONTROL)) {
425 uclass_first_device(UCLASS_SYSINFO, &ctx.dev);
426 if (ctx.dev)
427 parent_node = dev_read_subnode(ctx.dev, "smbios");
428 } else {
429 ctx.dev = NULL;
430 }
431
432 /* 16 byte align the table address */
433 addr = ALIGN(addr, 16);
434
435 se = map_sysmem(addr, sizeof(struct smbios_entry));
436 memset(se, 0, sizeof(struct smbios_entry));
437
438 addr += sizeof(struct smbios_entry);
439 addr = ALIGN(addr, 16);
440 tables = addr;
441
442 /* populate minimum required tables */
443 for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
444 const struct smbios_write_method *method;
445 int tmp;
446
447 method = &smbios_write_funcs[i];
448 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
449 ctx.node = ofnode_find_subnode(parent_node,
450 method->subnode_name);
451 tmp = method->write((ulong *)&addr, handle++, &ctx);
452
453 max_struct_size = max(max_struct_size, tmp);
454 len += tmp;
455 }
456
457 memcpy(se->anchor, "_SM_", 4);
458 se->length = sizeof(struct smbios_entry);
459 se->major_ver = SMBIOS_MAJOR_VER;
460 se->minor_ver = SMBIOS_MINOR_VER;
461 se->max_struct_size = max_struct_size;
462 memcpy(se->intermediate_anchor, "_DMI_", 5);
463 se->struct_table_length = len;
464
465 /*
466 * We must use a pointer here so things work correctly on sandbox. The
467 * user of this table is not aware of the mapping of addresses to
468 * sandbox's DRAM buffer.
469 */
470 table_addr = (ulong)map_sysmem(tables, 0);
471 if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
472 /*
473 * We need to put this >32-bit pointer into the table but the
474 * field is only 32 bits wide.
475 */
476 printf("WARNING: SMBIOS table_address overflow %llx\n",
477 (unsigned long long)table_addr);
478 table_addr = 0;
479 }
480 se->struct_table_address = table_addr;
481
482 se->struct_count = handle;
483
484 /* calculate checksums */
485 istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
486 isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
487 se->intermediate_checksum = table_compute_checksum(istart, isize);
488 se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
489 unmap_sysmem(se);
490
491 return addr;
492 }