183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
24b6dddc2SAlexander Graf /*
34b6dddc2SAlexander Graf * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
44b6dddc2SAlexander Graf *
54b6dddc2SAlexander Graf * Adapted from coreboot src/arch/x86/smbios.c
64b6dddc2SAlexander Graf */
74b6dddc2SAlexander Graf
84b6dddc2SAlexander Graf #include <common.h>
9*a2505fc8SSimon Glass #include <mapmem.h>
104b6dddc2SAlexander Graf #include <smbios.h>
114b6dddc2SAlexander Graf #include <tables_csum.h>
124b6dddc2SAlexander Graf #include <version.h>
1396476206SAlexander Graf #ifdef CONFIG_CPU
1496476206SAlexander Graf #include <cpu.h>
1596476206SAlexander Graf #include <dm.h>
1696476206SAlexander Graf #include <dm/uclass-internal.h>
1796476206SAlexander Graf #endif
184b6dddc2SAlexander Graf
194b6dddc2SAlexander Graf /**
204b6dddc2SAlexander Graf * smbios_add_string() - add a string to the string area
214b6dddc2SAlexander Graf *
224b6dddc2SAlexander Graf * This adds a string to the string area which is appended directly after
234b6dddc2SAlexander Graf * the formatted portion of an SMBIOS structure.
244b6dddc2SAlexander Graf *
254b6dddc2SAlexander Graf * @start: string area start address
264b6dddc2SAlexander Graf * @str: string to add
274b6dddc2SAlexander Graf * @return: string number in the string area
284b6dddc2SAlexander Graf */
smbios_add_string(char * start,const char * str)294b6dddc2SAlexander Graf static int smbios_add_string(char *start, const char *str)
304b6dddc2SAlexander Graf {
314b6dddc2SAlexander Graf int i = 1;
324b6dddc2SAlexander Graf char *p = start;
334b6dddc2SAlexander Graf
344b6dddc2SAlexander Graf for (;;) {
354b6dddc2SAlexander Graf if (!*p) {
364b6dddc2SAlexander Graf strcpy(p, str);
374b6dddc2SAlexander Graf p += strlen(str);
384b6dddc2SAlexander Graf *p++ = '\0';
394b6dddc2SAlexander Graf *p++ = '\0';
404b6dddc2SAlexander Graf
414b6dddc2SAlexander Graf return i;
424b6dddc2SAlexander Graf }
434b6dddc2SAlexander Graf
444b6dddc2SAlexander Graf if (!strcmp(p, str))
454b6dddc2SAlexander Graf return i;
464b6dddc2SAlexander Graf
474b6dddc2SAlexander Graf p += strlen(p) + 1;
484b6dddc2SAlexander Graf i++;
494b6dddc2SAlexander Graf }
504b6dddc2SAlexander Graf }
514b6dddc2SAlexander Graf
524b6dddc2SAlexander Graf /**
534b6dddc2SAlexander Graf * smbios_string_table_len() - compute the string area size
544b6dddc2SAlexander Graf *
554b6dddc2SAlexander Graf * This computes the size of the string area including the string terminator.
564b6dddc2SAlexander Graf *
574b6dddc2SAlexander Graf * @start: string area start address
584b6dddc2SAlexander Graf * @return: string area size
594b6dddc2SAlexander Graf */
smbios_string_table_len(char * start)604b6dddc2SAlexander Graf static int smbios_string_table_len(char *start)
614b6dddc2SAlexander Graf {
624b6dddc2SAlexander Graf char *p = start;
634b6dddc2SAlexander Graf int i, len = 0;
644b6dddc2SAlexander Graf
654b6dddc2SAlexander Graf while (*p) {
664b6dddc2SAlexander Graf i = strlen(p) + 1;
674b6dddc2SAlexander Graf p += i;
684b6dddc2SAlexander Graf len += i;
694b6dddc2SAlexander Graf }
704b6dddc2SAlexander Graf
714b6dddc2SAlexander Graf return len + 1;
724b6dddc2SAlexander Graf }
734b6dddc2SAlexander Graf
smbios_write_type0(ulong * current,int handle)7442fd8c19SSimon Glass static int smbios_write_type0(ulong *current, int handle)
754b6dddc2SAlexander Graf {
76*a2505fc8SSimon Glass struct smbios_type0 *t;
774b6dddc2SAlexander Graf int len = sizeof(struct smbios_type0);
784b6dddc2SAlexander Graf
79*a2505fc8SSimon Glass t = map_sysmem(*current, len);
804b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type0));
814b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
824b6dddc2SAlexander Graf t->vendor = smbios_add_string(t->eos, "U-Boot");
834b6dddc2SAlexander Graf t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
844b6dddc2SAlexander Graf t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
85e663b350SAlexander Graf #ifdef CONFIG_ROM_SIZE
864b6dddc2SAlexander Graf t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
87e663b350SAlexander Graf #endif
884b6dddc2SAlexander Graf t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
894b6dddc2SAlexander Graf BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
904b6dddc2SAlexander Graf BIOS_CHARACTERISTICS_UPGRADEABLE;
914b6dddc2SAlexander Graf #ifdef CONFIG_GENERATE_ACPI_TABLE
924b6dddc2SAlexander Graf t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
934b6dddc2SAlexander Graf #endif
94e663b350SAlexander Graf #ifdef CONFIG_EFI_LOADER
95e663b350SAlexander Graf t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
96e663b350SAlexander Graf #endif
974b6dddc2SAlexander Graf t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
98e663b350SAlexander Graf
994b6dddc2SAlexander Graf t->bios_major_release = 0xff;
1004b6dddc2SAlexander Graf t->bios_minor_release = 0xff;
1014b6dddc2SAlexander Graf t->ec_major_release = 0xff;
1024b6dddc2SAlexander Graf t->ec_minor_release = 0xff;
1034b6dddc2SAlexander Graf
1044b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
1054b6dddc2SAlexander Graf *current += len;
106*a2505fc8SSimon Glass unmap_sysmem(t);
1074b6dddc2SAlexander Graf
1084b6dddc2SAlexander Graf return len;
1094b6dddc2SAlexander Graf }
1104b6dddc2SAlexander Graf
smbios_write_type1(ulong * current,int handle)11142fd8c19SSimon Glass static int smbios_write_type1(ulong *current, int handle)
1124b6dddc2SAlexander Graf {
113*a2505fc8SSimon Glass struct smbios_type1 *t;
1144b6dddc2SAlexander Graf int len = sizeof(struct smbios_type1);
11500caae6dSSimon Glass char *serial_str = env_get("serial#");
1164b6dddc2SAlexander Graf
117*a2505fc8SSimon Glass t = map_sysmem(*current, len);
1184b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type1));
1194b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
1204b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
1214b6dddc2SAlexander Graf t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
1226fb580d7SAlexander Graf if (serial_str) {
1236fb580d7SAlexander Graf strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
1246fb580d7SAlexander Graf t->serial_number = smbios_add_string(t->eos, serial_str);
1256fb580d7SAlexander Graf }
1264b6dddc2SAlexander Graf
1274b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
1284b6dddc2SAlexander Graf *current += len;
129*a2505fc8SSimon Glass unmap_sysmem(t);
1304b6dddc2SAlexander Graf
1314b6dddc2SAlexander Graf return len;
1324b6dddc2SAlexander Graf }
1334b6dddc2SAlexander Graf
smbios_write_type2(ulong * current,int handle)13442fd8c19SSimon Glass static int smbios_write_type2(ulong *current, int handle)
1354b6dddc2SAlexander Graf {
136*a2505fc8SSimon Glass struct smbios_type2 *t;
1374b6dddc2SAlexander Graf int len = sizeof(struct smbios_type2);
1384b6dddc2SAlexander Graf
139*a2505fc8SSimon Glass t = map_sysmem(*current, len);
1404b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type2));
1414b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
1424b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
1434b6dddc2SAlexander Graf t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
1444b6dddc2SAlexander Graf t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
1454b6dddc2SAlexander Graf t->board_type = SMBIOS_BOARD_MOTHERBOARD;
1464b6dddc2SAlexander Graf
1474b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
1484b6dddc2SAlexander Graf *current += len;
149*a2505fc8SSimon Glass unmap_sysmem(t);
1504b6dddc2SAlexander Graf
1514b6dddc2SAlexander Graf return len;
1524b6dddc2SAlexander Graf }
1534b6dddc2SAlexander Graf
smbios_write_type3(ulong * current,int handle)15442fd8c19SSimon Glass static int smbios_write_type3(ulong *current, int handle)
1554b6dddc2SAlexander Graf {
156*a2505fc8SSimon Glass struct smbios_type3 *t;
1574b6dddc2SAlexander Graf int len = sizeof(struct smbios_type3);
1584b6dddc2SAlexander Graf
159*a2505fc8SSimon Glass t = map_sysmem(*current, len);
1604b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type3));
1614b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
1624b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
1634b6dddc2SAlexander Graf t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
1644b6dddc2SAlexander Graf t->bootup_state = SMBIOS_STATE_SAFE;
1654b6dddc2SAlexander Graf t->power_supply_state = SMBIOS_STATE_SAFE;
1664b6dddc2SAlexander Graf t->thermal_state = SMBIOS_STATE_SAFE;
1674b6dddc2SAlexander Graf t->security_status = SMBIOS_SECURITY_NONE;
1684b6dddc2SAlexander Graf
1694b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
1704b6dddc2SAlexander Graf *current += len;
171*a2505fc8SSimon Glass unmap_sysmem(t);
1724b6dddc2SAlexander Graf
1734b6dddc2SAlexander Graf return len;
1744b6dddc2SAlexander Graf }
1754b6dddc2SAlexander Graf
smbios_write_type4_dm(struct smbios_type4 * t)17696476206SAlexander Graf static void smbios_write_type4_dm(struct smbios_type4 *t)
17796476206SAlexander Graf {
17896476206SAlexander Graf u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
17996476206SAlexander Graf const char *vendor = "Unknown";
18096476206SAlexander Graf const char *name = "Unknown";
18196476206SAlexander Graf
18296476206SAlexander Graf #ifdef CONFIG_CPU
18396476206SAlexander Graf char processor_name[49];
18496476206SAlexander Graf char vendor_name[49];
18596476206SAlexander Graf struct udevice *dev = NULL;
18696476206SAlexander Graf
18796476206SAlexander Graf uclass_find_first_device(UCLASS_CPU, &dev);
18896476206SAlexander Graf if (dev) {
18996476206SAlexander Graf struct cpu_platdata *plat = dev_get_parent_platdata(dev);
19096476206SAlexander Graf
19196476206SAlexander Graf if (plat->family)
19296476206SAlexander Graf processor_family = plat->family;
19396476206SAlexander Graf t->processor_id[0] = plat->id[0];
19496476206SAlexander Graf t->processor_id[1] = plat->id[1];
19596476206SAlexander Graf
19696476206SAlexander Graf if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
19796476206SAlexander Graf vendor = vendor_name;
19896476206SAlexander Graf if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
19996476206SAlexander Graf name = processor_name;
20096476206SAlexander Graf }
20196476206SAlexander Graf #endif
20296476206SAlexander Graf
20396476206SAlexander Graf t->processor_family = processor_family;
20496476206SAlexander Graf t->processor_manufacturer = smbios_add_string(t->eos, vendor);
20596476206SAlexander Graf t->processor_version = smbios_add_string(t->eos, name);
20696476206SAlexander Graf }
20796476206SAlexander Graf
smbios_write_type4(ulong * current,int handle)20842fd8c19SSimon Glass static int smbios_write_type4(ulong *current, int handle)
2094b6dddc2SAlexander Graf {
210*a2505fc8SSimon Glass struct smbios_type4 *t;
2114b6dddc2SAlexander Graf int len = sizeof(struct smbios_type4);
2124b6dddc2SAlexander Graf
213*a2505fc8SSimon Glass t = map_sysmem(*current, len);
2144b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type4));
2154b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
2164b6dddc2SAlexander Graf t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
21796476206SAlexander Graf smbios_write_type4_dm(t);
2184b6dddc2SAlexander Graf t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
2194b6dddc2SAlexander Graf t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
2204b6dddc2SAlexander Graf t->l1_cache_handle = 0xffff;
2214b6dddc2SAlexander Graf t->l2_cache_handle = 0xffff;
2224b6dddc2SAlexander Graf t->l3_cache_handle = 0xffff;
2234b6dddc2SAlexander Graf t->processor_family2 = t->processor_family;
2244b6dddc2SAlexander Graf
2254b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
2264b6dddc2SAlexander Graf *current += len;
227*a2505fc8SSimon Glass unmap_sysmem(t);
2284b6dddc2SAlexander Graf
2294b6dddc2SAlexander Graf return len;
2304b6dddc2SAlexander Graf }
2314b6dddc2SAlexander Graf
smbios_write_type32(ulong * current,int handle)23242fd8c19SSimon Glass static int smbios_write_type32(ulong *current, int handle)
2334b6dddc2SAlexander Graf {
234*a2505fc8SSimon Glass struct smbios_type32 *t;
2354b6dddc2SAlexander Graf int len = sizeof(struct smbios_type32);
2364b6dddc2SAlexander Graf
237*a2505fc8SSimon Glass t = map_sysmem(*current, len);
2384b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type32));
2394b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
2404b6dddc2SAlexander Graf
2414b6dddc2SAlexander Graf *current += len;
242*a2505fc8SSimon Glass unmap_sysmem(t);
2434b6dddc2SAlexander Graf
2444b6dddc2SAlexander Graf return len;
2454b6dddc2SAlexander Graf }
2464b6dddc2SAlexander Graf
smbios_write_type127(ulong * current,int handle)24742fd8c19SSimon Glass static int smbios_write_type127(ulong *current, int handle)
2484b6dddc2SAlexander Graf {
249*a2505fc8SSimon Glass struct smbios_type127 *t;
2504b6dddc2SAlexander Graf int len = sizeof(struct smbios_type127);
2514b6dddc2SAlexander Graf
252*a2505fc8SSimon Glass t = map_sysmem(*current, len);
2534b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type127));
2544b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
2554b6dddc2SAlexander Graf
2564b6dddc2SAlexander Graf *current += len;
257*a2505fc8SSimon Glass unmap_sysmem(t);
2584b6dddc2SAlexander Graf
2594b6dddc2SAlexander Graf return len;
2604b6dddc2SAlexander Graf }
2614b6dddc2SAlexander Graf
2624b6dddc2SAlexander Graf static smbios_write_type smbios_write_funcs[] = {
2634b6dddc2SAlexander Graf smbios_write_type0,
2644b6dddc2SAlexander Graf smbios_write_type1,
2654b6dddc2SAlexander Graf smbios_write_type2,
2664b6dddc2SAlexander Graf smbios_write_type3,
2674b6dddc2SAlexander Graf smbios_write_type4,
2684b6dddc2SAlexander Graf smbios_write_type32,
2694b6dddc2SAlexander Graf smbios_write_type127
2704b6dddc2SAlexander Graf };
2714b6dddc2SAlexander Graf
write_smbios_table(ulong addr)27242fd8c19SSimon Glass ulong write_smbios_table(ulong addr)
2734b6dddc2SAlexander Graf {
2744b6dddc2SAlexander Graf struct smbios_entry *se;
275*a2505fc8SSimon Glass ulong table_addr;
27642fd8c19SSimon Glass ulong tables;
2774b6dddc2SAlexander Graf int len = 0;
2784b6dddc2SAlexander Graf int max_struct_size = 0;
2794b6dddc2SAlexander Graf int handle = 0;
2804b6dddc2SAlexander Graf char *istart;
2814b6dddc2SAlexander Graf int isize;
2824b6dddc2SAlexander Graf int i;
2834b6dddc2SAlexander Graf
2844b6dddc2SAlexander Graf /* 16 byte align the table address */
2854b6dddc2SAlexander Graf addr = ALIGN(addr, 16);
2864b6dddc2SAlexander Graf
287*a2505fc8SSimon Glass se = map_sysmem(addr, sizeof(struct smbios_entry));
2884b6dddc2SAlexander Graf memset(se, 0, sizeof(struct smbios_entry));
2894b6dddc2SAlexander Graf
2904b6dddc2SAlexander Graf addr += sizeof(struct smbios_entry);
2914b6dddc2SAlexander Graf addr = ALIGN(addr, 16);
2924b6dddc2SAlexander Graf tables = addr;
2934b6dddc2SAlexander Graf
2944b6dddc2SAlexander Graf /* populate minimum required tables */
2954b6dddc2SAlexander Graf for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
29642fd8c19SSimon Glass int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++);
29760a4df32SChristian Gmeiner
2984b6dddc2SAlexander Graf max_struct_size = max(max_struct_size, tmp);
2994b6dddc2SAlexander Graf len += tmp;
3004b6dddc2SAlexander Graf }
3014b6dddc2SAlexander Graf
3024b6dddc2SAlexander Graf memcpy(se->anchor, "_SM_", 4);
3034b6dddc2SAlexander Graf se->length = sizeof(struct smbios_entry);
3044b6dddc2SAlexander Graf se->major_ver = SMBIOS_MAJOR_VER;
3054b6dddc2SAlexander Graf se->minor_ver = SMBIOS_MINOR_VER;
3064b6dddc2SAlexander Graf se->max_struct_size = max_struct_size;
3074b6dddc2SAlexander Graf memcpy(se->intermediate_anchor, "_DMI_", 5);
3084b6dddc2SAlexander Graf se->struct_table_length = len;
309*a2505fc8SSimon Glass
310*a2505fc8SSimon Glass /*
311*a2505fc8SSimon Glass * We must use a pointer here so things work correctly on sandbox. The
312*a2505fc8SSimon Glass * user of this table is not aware of the mapping of addresses to
313*a2505fc8SSimon Glass * sandbox's DRAM buffer.
314*a2505fc8SSimon Glass */
315*a2505fc8SSimon Glass table_addr = (ulong)map_sysmem(tables, 0);
316*a2505fc8SSimon Glass if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
317*a2505fc8SSimon Glass /*
318*a2505fc8SSimon Glass * We need to put this >32-bit pointer into the table but the
319*a2505fc8SSimon Glass * field is only 32 bits wide.
320*a2505fc8SSimon Glass */
321*a2505fc8SSimon Glass printf("WARNING: SMBIOS table_address overflow %llx\n",
322*a2505fc8SSimon Glass (unsigned long long)table_addr);
323*a2505fc8SSimon Glass table_addr = 0;
324*a2505fc8SSimon Glass }
325*a2505fc8SSimon Glass se->struct_table_address = table_addr;
326*a2505fc8SSimon Glass
3274b6dddc2SAlexander Graf se->struct_count = handle;
3284b6dddc2SAlexander Graf
3294b6dddc2SAlexander Graf /* calculate checksums */
3304b6dddc2SAlexander Graf istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
3314b6dddc2SAlexander Graf isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
3324b6dddc2SAlexander Graf se->intermediate_checksum = table_compute_checksum(istart, isize);
3334b6dddc2SAlexander Graf se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
334*a2505fc8SSimon Glass unmap_sysmem(se);
3354b6dddc2SAlexander Graf
3364b6dddc2SAlexander Graf return addr;
3374b6dddc2SAlexander Graf }
338