14b6dddc2SAlexander Graf /*
24b6dddc2SAlexander Graf * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
34b6dddc2SAlexander Graf *
44b6dddc2SAlexander Graf * Adapted from coreboot src/arch/x86/smbios.c
54b6dddc2SAlexander Graf *
64b6dddc2SAlexander Graf * SPDX-License-Identifier: GPL-2.0+
74b6dddc2SAlexander Graf */
84b6dddc2SAlexander Graf
94b6dddc2SAlexander Graf #include <common.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 DECLARE_GLOBAL_DATA_PTR;
204b6dddc2SAlexander Graf
214b6dddc2SAlexander Graf /**
224b6dddc2SAlexander Graf * smbios_add_string() - add a string to the string area
234b6dddc2SAlexander Graf *
244b6dddc2SAlexander Graf * This adds a string to the string area which is appended directly after
254b6dddc2SAlexander Graf * the formatted portion of an SMBIOS structure.
264b6dddc2SAlexander Graf *
274b6dddc2SAlexander Graf * @start: string area start address
284b6dddc2SAlexander Graf * @str: string to add
294b6dddc2SAlexander Graf * @return: string number in the string area
304b6dddc2SAlexander Graf */
smbios_add_string(char * start,const char * str)314b6dddc2SAlexander Graf static int smbios_add_string(char *start, const char *str)
324b6dddc2SAlexander Graf {
334b6dddc2SAlexander Graf int i = 1;
344b6dddc2SAlexander Graf char *p = start;
354b6dddc2SAlexander Graf
364b6dddc2SAlexander Graf for (;;) {
374b6dddc2SAlexander Graf if (!*p) {
384b6dddc2SAlexander Graf strcpy(p, str);
394b6dddc2SAlexander Graf p += strlen(str);
404b6dddc2SAlexander Graf *p++ = '\0';
414b6dddc2SAlexander Graf *p++ = '\0';
424b6dddc2SAlexander Graf
434b6dddc2SAlexander Graf return i;
444b6dddc2SAlexander Graf }
454b6dddc2SAlexander Graf
464b6dddc2SAlexander Graf if (!strcmp(p, str))
474b6dddc2SAlexander Graf return i;
484b6dddc2SAlexander Graf
494b6dddc2SAlexander Graf p += strlen(p) + 1;
504b6dddc2SAlexander Graf i++;
514b6dddc2SAlexander Graf }
524b6dddc2SAlexander Graf }
534b6dddc2SAlexander Graf
544b6dddc2SAlexander Graf /**
554b6dddc2SAlexander Graf * smbios_string_table_len() - compute the string area size
564b6dddc2SAlexander Graf *
574b6dddc2SAlexander Graf * This computes the size of the string area including the string terminator.
584b6dddc2SAlexander Graf *
594b6dddc2SAlexander Graf * @start: string area start address
604b6dddc2SAlexander Graf * @return: string area size
614b6dddc2SAlexander Graf */
smbios_string_table_len(char * start)624b6dddc2SAlexander Graf static int smbios_string_table_len(char *start)
634b6dddc2SAlexander Graf {
644b6dddc2SAlexander Graf char *p = start;
654b6dddc2SAlexander Graf int i, len = 0;
664b6dddc2SAlexander Graf
674b6dddc2SAlexander Graf while (*p) {
684b6dddc2SAlexander Graf i = strlen(p) + 1;
694b6dddc2SAlexander Graf p += i;
704b6dddc2SAlexander Graf len += i;
714b6dddc2SAlexander Graf }
724b6dddc2SAlexander Graf
734b6dddc2SAlexander Graf return len + 1;
744b6dddc2SAlexander Graf }
754b6dddc2SAlexander Graf
smbios_write_type0(ulong * current,int handle)7642fd8c19SSimon Glass static int smbios_write_type0(ulong *current, int handle)
774b6dddc2SAlexander Graf {
784b6dddc2SAlexander Graf struct smbios_type0 *t = (struct smbios_type0 *)*current;
794b6dddc2SAlexander Graf int len = sizeof(struct smbios_type0);
804b6dddc2SAlexander Graf
814b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type0));
824b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
834b6dddc2SAlexander Graf t->vendor = smbios_add_string(t->eos, "U-Boot");
844b6dddc2SAlexander Graf t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
854b6dddc2SAlexander Graf t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
86e663b350SAlexander Graf #ifdef CONFIG_ROM_SIZE
874b6dddc2SAlexander Graf t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
88e663b350SAlexander Graf #endif
894b6dddc2SAlexander Graf t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
904b6dddc2SAlexander Graf BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
914b6dddc2SAlexander Graf BIOS_CHARACTERISTICS_UPGRADEABLE;
924b6dddc2SAlexander Graf #ifdef CONFIG_GENERATE_ACPI_TABLE
934b6dddc2SAlexander Graf t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
944b6dddc2SAlexander Graf #endif
95e663b350SAlexander Graf #ifdef CONFIG_EFI_LOADER
96e663b350SAlexander Graf t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
97e663b350SAlexander Graf #endif
984b6dddc2SAlexander Graf t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
99e663b350SAlexander Graf
1004b6dddc2SAlexander Graf t->bios_major_release = 0xff;
1014b6dddc2SAlexander Graf t->bios_minor_release = 0xff;
1024b6dddc2SAlexander Graf t->ec_major_release = 0xff;
1034b6dddc2SAlexander Graf t->ec_minor_release = 0xff;
1044b6dddc2SAlexander Graf
1054b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
1064b6dddc2SAlexander Graf *current += len;
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 {
1134b6dddc2SAlexander Graf struct smbios_type1 *t = (struct smbios_type1 *)*current;
1144b6dddc2SAlexander Graf int len = sizeof(struct smbios_type1);
115*00caae6dSSimon Glass char *serial_str = env_get("serial#");
1164b6dddc2SAlexander Graf
1174b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type1));
1184b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
1194b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
1204b6dddc2SAlexander Graf t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
1216fb580d7SAlexander Graf if (serial_str) {
1226fb580d7SAlexander Graf strncpy((char*)t->uuid, serial_str, sizeof(t->uuid));
1236fb580d7SAlexander Graf t->serial_number = smbios_add_string(t->eos, serial_str);
1246fb580d7SAlexander Graf }
1254b6dddc2SAlexander Graf
1264b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
1274b6dddc2SAlexander Graf *current += len;
1284b6dddc2SAlexander Graf
1294b6dddc2SAlexander Graf return len;
1304b6dddc2SAlexander Graf }
1314b6dddc2SAlexander Graf
smbios_write_type2(ulong * current,int handle)13242fd8c19SSimon Glass static int smbios_write_type2(ulong *current, int handle)
1334b6dddc2SAlexander Graf {
1344b6dddc2SAlexander Graf struct smbios_type2 *t = (struct smbios_type2 *)*current;
1354b6dddc2SAlexander Graf int len = sizeof(struct smbios_type2);
1364b6dddc2SAlexander Graf
1374b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type2));
1384b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
1394b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
1404b6dddc2SAlexander Graf t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
1414b6dddc2SAlexander Graf t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
1424b6dddc2SAlexander Graf t->board_type = SMBIOS_BOARD_MOTHERBOARD;
1434b6dddc2SAlexander Graf
1444b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
1454b6dddc2SAlexander Graf *current += len;
1464b6dddc2SAlexander Graf
1474b6dddc2SAlexander Graf return len;
1484b6dddc2SAlexander Graf }
1494b6dddc2SAlexander Graf
smbios_write_type3(ulong * current,int handle)15042fd8c19SSimon Glass static int smbios_write_type3(ulong *current, int handle)
1514b6dddc2SAlexander Graf {
1524b6dddc2SAlexander Graf struct smbios_type3 *t = (struct smbios_type3 *)*current;
1534b6dddc2SAlexander Graf int len = sizeof(struct smbios_type3);
1544b6dddc2SAlexander Graf
1554b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type3));
1564b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
1574b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
1584b6dddc2SAlexander Graf t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
1594b6dddc2SAlexander Graf t->bootup_state = SMBIOS_STATE_SAFE;
1604b6dddc2SAlexander Graf t->power_supply_state = SMBIOS_STATE_SAFE;
1614b6dddc2SAlexander Graf t->thermal_state = SMBIOS_STATE_SAFE;
1624b6dddc2SAlexander Graf t->security_status = SMBIOS_SECURITY_NONE;
1634b6dddc2SAlexander Graf
1644b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
1654b6dddc2SAlexander Graf *current += len;
1664b6dddc2SAlexander Graf
1674b6dddc2SAlexander Graf return len;
1684b6dddc2SAlexander Graf }
1694b6dddc2SAlexander Graf
smbios_write_type4_dm(struct smbios_type4 * t)17096476206SAlexander Graf static void smbios_write_type4_dm(struct smbios_type4 *t)
17196476206SAlexander Graf {
17296476206SAlexander Graf u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
17396476206SAlexander Graf const char *vendor = "Unknown";
17496476206SAlexander Graf const char *name = "Unknown";
17596476206SAlexander Graf
17696476206SAlexander Graf #ifdef CONFIG_CPU
17796476206SAlexander Graf char processor_name[49];
17896476206SAlexander Graf char vendor_name[49];
17996476206SAlexander Graf struct udevice *dev = NULL;
18096476206SAlexander Graf
18196476206SAlexander Graf uclass_find_first_device(UCLASS_CPU, &dev);
18296476206SAlexander Graf if (dev) {
18396476206SAlexander Graf struct cpu_platdata *plat = dev_get_parent_platdata(dev);
18496476206SAlexander Graf
18596476206SAlexander Graf if (plat->family)
18696476206SAlexander Graf processor_family = plat->family;
18796476206SAlexander Graf t->processor_id[0] = plat->id[0];
18896476206SAlexander Graf t->processor_id[1] = plat->id[1];
18996476206SAlexander Graf
19096476206SAlexander Graf if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
19196476206SAlexander Graf vendor = vendor_name;
19296476206SAlexander Graf if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
19396476206SAlexander Graf name = processor_name;
19496476206SAlexander Graf }
19596476206SAlexander Graf #endif
19696476206SAlexander Graf
19796476206SAlexander Graf t->processor_family = processor_family;
19896476206SAlexander Graf t->processor_manufacturer = smbios_add_string(t->eos, vendor);
19996476206SAlexander Graf t->processor_version = smbios_add_string(t->eos, name);
20096476206SAlexander Graf }
20196476206SAlexander Graf
smbios_write_type4(ulong * current,int handle)20242fd8c19SSimon Glass static int smbios_write_type4(ulong *current, int handle)
2034b6dddc2SAlexander Graf {
2044b6dddc2SAlexander Graf struct smbios_type4 *t = (struct smbios_type4 *)*current;
2054b6dddc2SAlexander Graf int len = sizeof(struct smbios_type4);
2064b6dddc2SAlexander Graf
2074b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type4));
2084b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
2094b6dddc2SAlexander Graf t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
21096476206SAlexander Graf smbios_write_type4_dm(t);
2114b6dddc2SAlexander Graf t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
2124b6dddc2SAlexander Graf t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
2134b6dddc2SAlexander Graf t->l1_cache_handle = 0xffff;
2144b6dddc2SAlexander Graf t->l2_cache_handle = 0xffff;
2154b6dddc2SAlexander Graf t->l3_cache_handle = 0xffff;
2164b6dddc2SAlexander Graf t->processor_family2 = t->processor_family;
2174b6dddc2SAlexander Graf
2184b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos);
2194b6dddc2SAlexander Graf *current += len;
2204b6dddc2SAlexander Graf
2214b6dddc2SAlexander Graf return len;
2224b6dddc2SAlexander Graf }
2234b6dddc2SAlexander Graf
smbios_write_type32(ulong * current,int handle)22442fd8c19SSimon Glass static int smbios_write_type32(ulong *current, int handle)
2254b6dddc2SAlexander Graf {
2264b6dddc2SAlexander Graf struct smbios_type32 *t = (struct smbios_type32 *)*current;
2274b6dddc2SAlexander Graf int len = sizeof(struct smbios_type32);
2284b6dddc2SAlexander Graf
2294b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type32));
2304b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
2314b6dddc2SAlexander Graf
2324b6dddc2SAlexander Graf *current += len;
2334b6dddc2SAlexander Graf
2344b6dddc2SAlexander Graf return len;
2354b6dddc2SAlexander Graf }
2364b6dddc2SAlexander Graf
smbios_write_type127(ulong * current,int handle)23742fd8c19SSimon Glass static int smbios_write_type127(ulong *current, int handle)
2384b6dddc2SAlexander Graf {
2394b6dddc2SAlexander Graf struct smbios_type127 *t = (struct smbios_type127 *)*current;
2404b6dddc2SAlexander Graf int len = sizeof(struct smbios_type127);
2414b6dddc2SAlexander Graf
2424b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type127));
2434b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
2444b6dddc2SAlexander Graf
2454b6dddc2SAlexander Graf *current += len;
2464b6dddc2SAlexander Graf
2474b6dddc2SAlexander Graf return len;
2484b6dddc2SAlexander Graf }
2494b6dddc2SAlexander Graf
2504b6dddc2SAlexander Graf static smbios_write_type smbios_write_funcs[] = {
2514b6dddc2SAlexander Graf smbios_write_type0,
2524b6dddc2SAlexander Graf smbios_write_type1,
2534b6dddc2SAlexander Graf smbios_write_type2,
2544b6dddc2SAlexander Graf smbios_write_type3,
2554b6dddc2SAlexander Graf smbios_write_type4,
2564b6dddc2SAlexander Graf smbios_write_type32,
2574b6dddc2SAlexander Graf smbios_write_type127
2584b6dddc2SAlexander Graf };
2594b6dddc2SAlexander Graf
write_smbios_table(ulong addr)26042fd8c19SSimon Glass ulong write_smbios_table(ulong addr)
2614b6dddc2SAlexander Graf {
2624b6dddc2SAlexander Graf struct smbios_entry *se;
26342fd8c19SSimon Glass ulong tables;
2644b6dddc2SAlexander Graf int len = 0;
2654b6dddc2SAlexander Graf int max_struct_size = 0;
2664b6dddc2SAlexander Graf int handle = 0;
2674b6dddc2SAlexander Graf char *istart;
2684b6dddc2SAlexander Graf int isize;
2694b6dddc2SAlexander Graf int i;
2704b6dddc2SAlexander Graf
2714b6dddc2SAlexander Graf /* 16 byte align the table address */
2724b6dddc2SAlexander Graf addr = ALIGN(addr, 16);
2734b6dddc2SAlexander Graf
27442fd8c19SSimon Glass se = (struct smbios_entry *)(uintptr_t)addr;
2754b6dddc2SAlexander Graf memset(se, 0, sizeof(struct smbios_entry));
2764b6dddc2SAlexander Graf
2774b6dddc2SAlexander Graf addr += sizeof(struct smbios_entry);
2784b6dddc2SAlexander Graf addr = ALIGN(addr, 16);
2794b6dddc2SAlexander Graf tables = addr;
2804b6dddc2SAlexander Graf
2814b6dddc2SAlexander Graf /* populate minimum required tables */
2824b6dddc2SAlexander Graf for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
28342fd8c19SSimon Glass int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++);
2844b6dddc2SAlexander Graf max_struct_size = max(max_struct_size, tmp);
2854b6dddc2SAlexander Graf len += tmp;
2864b6dddc2SAlexander Graf }
2874b6dddc2SAlexander Graf
2884b6dddc2SAlexander Graf memcpy(se->anchor, "_SM_", 4);
2894b6dddc2SAlexander Graf se->length = sizeof(struct smbios_entry);
2904b6dddc2SAlexander Graf se->major_ver = SMBIOS_MAJOR_VER;
2914b6dddc2SAlexander Graf se->minor_ver = SMBIOS_MINOR_VER;
2924b6dddc2SAlexander Graf se->max_struct_size = max_struct_size;
2934b6dddc2SAlexander Graf memcpy(se->intermediate_anchor, "_DMI_", 5);
2944b6dddc2SAlexander Graf se->struct_table_length = len;
2954b6dddc2SAlexander Graf se->struct_table_address = tables;
2964b6dddc2SAlexander Graf se->struct_count = handle;
2974b6dddc2SAlexander Graf
2984b6dddc2SAlexander Graf /* calculate checksums */
2994b6dddc2SAlexander Graf istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
3004b6dddc2SAlexander Graf isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
3014b6dddc2SAlexander Graf se->intermediate_checksum = table_compute_checksum(istart, isize);
3024b6dddc2SAlexander Graf se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
3034b6dddc2SAlexander Graf
3044b6dddc2SAlexander Graf return addr;
3054b6dddc2SAlexander Graf }
306