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 */ 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 */ 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 76e824cf3fSAlexander Graf static int smbios_write_type0(uintptr_t *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); 86*e663b350SAlexander Graf #ifdef CONFIG_ROM_SIZE 874b6dddc2SAlexander Graf t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1; 88*e663b350SAlexander 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 95*e663b350SAlexander Graf #ifdef CONFIG_EFI_LOADER 96*e663b350SAlexander Graf t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI; 97*e663b350SAlexander Graf #endif 984b6dddc2SAlexander Graf t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET; 99*e663b350SAlexander 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 111e824cf3fSAlexander Graf static int smbios_write_type1(uintptr_t *current, int handle) 1124b6dddc2SAlexander Graf { 1134b6dddc2SAlexander Graf struct smbios_type1 *t = (struct smbios_type1 *)*current; 1144b6dddc2SAlexander Graf int len = sizeof(struct smbios_type1); 1154b6dddc2SAlexander Graf 1164b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type1)); 1174b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle); 1184b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER); 1194b6dddc2SAlexander Graf t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME); 1204b6dddc2SAlexander Graf 1214b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos); 1224b6dddc2SAlexander Graf *current += len; 1234b6dddc2SAlexander Graf 1244b6dddc2SAlexander Graf return len; 1254b6dddc2SAlexander Graf } 1264b6dddc2SAlexander Graf 127e824cf3fSAlexander Graf static int smbios_write_type2(uintptr_t *current, int handle) 1284b6dddc2SAlexander Graf { 1294b6dddc2SAlexander Graf struct smbios_type2 *t = (struct smbios_type2 *)*current; 1304b6dddc2SAlexander Graf int len = sizeof(struct smbios_type2); 1314b6dddc2SAlexander Graf 1324b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type2)); 1334b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle); 1344b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER); 1354b6dddc2SAlexander Graf t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME); 1364b6dddc2SAlexander Graf t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING; 1374b6dddc2SAlexander Graf t->board_type = SMBIOS_BOARD_MOTHERBOARD; 1384b6dddc2SAlexander Graf 1394b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos); 1404b6dddc2SAlexander Graf *current += len; 1414b6dddc2SAlexander Graf 1424b6dddc2SAlexander Graf return len; 1434b6dddc2SAlexander Graf } 1444b6dddc2SAlexander Graf 145e824cf3fSAlexander Graf static int smbios_write_type3(uintptr_t *current, int handle) 1464b6dddc2SAlexander Graf { 1474b6dddc2SAlexander Graf struct smbios_type3 *t = (struct smbios_type3 *)*current; 1484b6dddc2SAlexander Graf int len = sizeof(struct smbios_type3); 1494b6dddc2SAlexander Graf 1504b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type3)); 1514b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle); 1524b6dddc2SAlexander Graf t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER); 1534b6dddc2SAlexander Graf t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP; 1544b6dddc2SAlexander Graf t->bootup_state = SMBIOS_STATE_SAFE; 1554b6dddc2SAlexander Graf t->power_supply_state = SMBIOS_STATE_SAFE; 1564b6dddc2SAlexander Graf t->thermal_state = SMBIOS_STATE_SAFE; 1574b6dddc2SAlexander Graf t->security_status = SMBIOS_SECURITY_NONE; 1584b6dddc2SAlexander Graf 1594b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos); 1604b6dddc2SAlexander Graf *current += len; 1614b6dddc2SAlexander Graf 1624b6dddc2SAlexander Graf return len; 1634b6dddc2SAlexander Graf } 1644b6dddc2SAlexander Graf 16596476206SAlexander Graf static void smbios_write_type4_dm(struct smbios_type4 *t) 16696476206SAlexander Graf { 16796476206SAlexander Graf u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN; 16896476206SAlexander Graf const char *vendor = "Unknown"; 16996476206SAlexander Graf const char *name = "Unknown"; 17096476206SAlexander Graf 17196476206SAlexander Graf #ifdef CONFIG_CPU 17296476206SAlexander Graf char processor_name[49]; 17396476206SAlexander Graf char vendor_name[49]; 17496476206SAlexander Graf struct udevice *dev = NULL; 17596476206SAlexander Graf 17696476206SAlexander Graf uclass_find_first_device(UCLASS_CPU, &dev); 17796476206SAlexander Graf if (dev) { 17896476206SAlexander Graf struct cpu_platdata *plat = dev_get_parent_platdata(dev); 17996476206SAlexander Graf 18096476206SAlexander Graf if (plat->family) 18196476206SAlexander Graf processor_family = plat->family; 18296476206SAlexander Graf t->processor_id[0] = plat->id[0]; 18396476206SAlexander Graf t->processor_id[1] = plat->id[1]; 18496476206SAlexander Graf 18596476206SAlexander Graf if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name))) 18696476206SAlexander Graf vendor = vendor_name; 18796476206SAlexander Graf if (!cpu_get_desc(dev, processor_name, sizeof(processor_name))) 18896476206SAlexander Graf name = processor_name; 18996476206SAlexander Graf } 19096476206SAlexander Graf #endif 19196476206SAlexander Graf 19296476206SAlexander Graf t->processor_family = processor_family; 19396476206SAlexander Graf t->processor_manufacturer = smbios_add_string(t->eos, vendor); 19496476206SAlexander Graf t->processor_version = smbios_add_string(t->eos, name); 19596476206SAlexander Graf } 19696476206SAlexander Graf 197e824cf3fSAlexander Graf static int smbios_write_type4(uintptr_t *current, int handle) 1984b6dddc2SAlexander Graf { 1994b6dddc2SAlexander Graf struct smbios_type4 *t = (struct smbios_type4 *)*current; 2004b6dddc2SAlexander Graf int len = sizeof(struct smbios_type4); 2014b6dddc2SAlexander Graf 2024b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type4)); 2034b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle); 2044b6dddc2SAlexander Graf t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL; 20596476206SAlexander Graf smbios_write_type4_dm(t); 2064b6dddc2SAlexander Graf t->status = SMBIOS_PROCESSOR_STATUS_ENABLED; 2074b6dddc2SAlexander Graf t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE; 2084b6dddc2SAlexander Graf t->l1_cache_handle = 0xffff; 2094b6dddc2SAlexander Graf t->l2_cache_handle = 0xffff; 2104b6dddc2SAlexander Graf t->l3_cache_handle = 0xffff; 2114b6dddc2SAlexander Graf t->processor_family2 = t->processor_family; 2124b6dddc2SAlexander Graf 2134b6dddc2SAlexander Graf len = t->length + smbios_string_table_len(t->eos); 2144b6dddc2SAlexander Graf *current += len; 2154b6dddc2SAlexander Graf 2164b6dddc2SAlexander Graf return len; 2174b6dddc2SAlexander Graf } 2184b6dddc2SAlexander Graf 219e824cf3fSAlexander Graf static int smbios_write_type32(uintptr_t *current, int handle) 2204b6dddc2SAlexander Graf { 2214b6dddc2SAlexander Graf struct smbios_type32 *t = (struct smbios_type32 *)*current; 2224b6dddc2SAlexander Graf int len = sizeof(struct smbios_type32); 2234b6dddc2SAlexander Graf 2244b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type32)); 2254b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle); 2264b6dddc2SAlexander Graf 2274b6dddc2SAlexander Graf *current += len; 2284b6dddc2SAlexander Graf 2294b6dddc2SAlexander Graf return len; 2304b6dddc2SAlexander Graf } 2314b6dddc2SAlexander Graf 232e824cf3fSAlexander Graf static int smbios_write_type127(uintptr_t *current, int handle) 2334b6dddc2SAlexander Graf { 2344b6dddc2SAlexander Graf struct smbios_type127 *t = (struct smbios_type127 *)*current; 2354b6dddc2SAlexander Graf int len = sizeof(struct smbios_type127); 2364b6dddc2SAlexander Graf 2374b6dddc2SAlexander Graf memset(t, 0, sizeof(struct smbios_type127)); 2384b6dddc2SAlexander Graf fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle); 2394b6dddc2SAlexander Graf 2404b6dddc2SAlexander Graf *current += len; 2414b6dddc2SAlexander Graf 2424b6dddc2SAlexander Graf return len; 2434b6dddc2SAlexander Graf } 2444b6dddc2SAlexander Graf 2454b6dddc2SAlexander Graf static smbios_write_type smbios_write_funcs[] = { 2464b6dddc2SAlexander Graf smbios_write_type0, 2474b6dddc2SAlexander Graf smbios_write_type1, 2484b6dddc2SAlexander Graf smbios_write_type2, 2494b6dddc2SAlexander Graf smbios_write_type3, 2504b6dddc2SAlexander Graf smbios_write_type4, 2514b6dddc2SAlexander Graf smbios_write_type32, 2524b6dddc2SAlexander Graf smbios_write_type127 2534b6dddc2SAlexander Graf }; 2544b6dddc2SAlexander Graf 255e824cf3fSAlexander Graf uintptr_t write_smbios_table(uintptr_t addr) 2564b6dddc2SAlexander Graf { 2574b6dddc2SAlexander Graf struct smbios_entry *se; 2584b6dddc2SAlexander Graf u32 tables; 2594b6dddc2SAlexander Graf int len = 0; 2604b6dddc2SAlexander Graf int max_struct_size = 0; 2614b6dddc2SAlexander Graf int handle = 0; 2624b6dddc2SAlexander Graf char *istart; 2634b6dddc2SAlexander Graf int isize; 2644b6dddc2SAlexander Graf int i; 2654b6dddc2SAlexander Graf 2664b6dddc2SAlexander Graf /* 16 byte align the table address */ 2674b6dddc2SAlexander Graf addr = ALIGN(addr, 16); 2684b6dddc2SAlexander Graf 2694b6dddc2SAlexander Graf se = (struct smbios_entry *)addr; 2704b6dddc2SAlexander Graf memset(se, 0, sizeof(struct smbios_entry)); 2714b6dddc2SAlexander Graf 2724b6dddc2SAlexander Graf addr += sizeof(struct smbios_entry); 2734b6dddc2SAlexander Graf addr = ALIGN(addr, 16); 2744b6dddc2SAlexander Graf tables = addr; 2754b6dddc2SAlexander Graf 2764b6dddc2SAlexander Graf /* populate minimum required tables */ 2774b6dddc2SAlexander Graf for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) { 2784b6dddc2SAlexander Graf int tmp = smbios_write_funcs[i](&addr, handle++); 2794b6dddc2SAlexander Graf max_struct_size = max(max_struct_size, tmp); 2804b6dddc2SAlexander Graf len += tmp; 2814b6dddc2SAlexander Graf } 2824b6dddc2SAlexander Graf 2834b6dddc2SAlexander Graf memcpy(se->anchor, "_SM_", 4); 2844b6dddc2SAlexander Graf se->length = sizeof(struct smbios_entry); 2854b6dddc2SAlexander Graf se->major_ver = SMBIOS_MAJOR_VER; 2864b6dddc2SAlexander Graf se->minor_ver = SMBIOS_MINOR_VER; 2874b6dddc2SAlexander Graf se->max_struct_size = max_struct_size; 2884b6dddc2SAlexander Graf memcpy(se->intermediate_anchor, "_DMI_", 5); 2894b6dddc2SAlexander Graf se->struct_table_length = len; 2904b6dddc2SAlexander Graf se->struct_table_address = tables; 2914b6dddc2SAlexander Graf se->struct_count = handle; 2924b6dddc2SAlexander Graf 2934b6dddc2SAlexander Graf /* calculate checksums */ 2944b6dddc2SAlexander Graf istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET; 2954b6dddc2SAlexander Graf isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET; 2964b6dddc2SAlexander Graf se->intermediate_checksum = table_compute_checksum(istart, isize); 2974b6dddc2SAlexander Graf se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry)); 2984b6dddc2SAlexander Graf 2994b6dddc2SAlexander Graf return addr; 3004b6dddc2SAlexander Graf } 301