2022-03-11 20:09:22 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
// Copyright (C) 2022 Samuel Demeulemeester
|
|
|
|
//
|
|
|
|
|
2022-03-12 10:32:51 -06:00
|
|
|
#include "stdint.h"
|
|
|
|
#include "string.h"
|
2022-04-02 08:43:46 -05:00
|
|
|
#include "display.h"
|
2022-03-11 20:09:22 -06:00
|
|
|
|
2022-04-02 08:43:46 -05:00
|
|
|
#include "boot.h"
|
2022-03-11 20:09:22 -06:00
|
|
|
#include "bootparams.h"
|
|
|
|
#include "efi.h"
|
|
|
|
#include "vmem.h"
|
2022-03-23 15:11:56 -05:00
|
|
|
#include "smbios.h"
|
2022-03-11 20:09:22 -06:00
|
|
|
|
|
|
|
#define LINE_DMI 23
|
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
static const uint8_t *table_start = NULL;
|
|
|
|
static uint32_t table_length = 0; // 16-bit in SMBIOS v2, 32-bit in SMBIOS v3.
|
|
|
|
|
2022-03-11 20:09:22 -06:00
|
|
|
static const efi_guid_t SMBIOS2_GUID = { 0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d} };
|
|
|
|
|
|
|
|
// SMBIOS v3 compliant FW must include an SMBIOS v2 table, but maybe parse SM3 table later...
|
|
|
|
// static const efi_guid_t SMBIOS3_GUID = { 0xf2fd1544, 0x9794, 0x4a2c, {0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94} };
|
|
|
|
|
2022-04-16 06:40:50 -05:00
|
|
|
struct system_info *dmi_system_info;
|
|
|
|
struct baseboard_info *dmi_baseboard_info;
|
2022-03-23 15:11:56 -05:00
|
|
|
struct mem_dev *dmi_memory_device;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
static char *get_tstruct_string(struct tstruct_header *header, uint16_t maxlen, int n)
|
|
|
|
{
|
2022-03-12 10:32:51 -06:00
|
|
|
if (n < 1)
|
2022-04-02 08:43:46 -05:00
|
|
|
return NULL;
|
2022-05-19 18:59:27 -05:00
|
|
|
char *a = (char *) header + header->length;
|
2022-03-12 10:32:51 -06:00
|
|
|
n--;
|
|
|
|
do {
|
2022-05-19 18:59:27 -05:00
|
|
|
if (! *a)
|
2022-03-12 10:32:51 -06:00
|
|
|
n--;
|
2022-05-19 18:59:27 -05:00
|
|
|
if (!n && *a)
|
2022-03-12 10:32:51 -06:00
|
|
|
return a;
|
|
|
|
a++;
|
2022-05-19 18:59:27 -05:00
|
|
|
} while (a < ((char *) header + maxlen) && !( *a == 0 && *(a - 1) == 0));
|
2022-04-02 08:43:46 -05:00
|
|
|
return NULL;
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __x86_64__
|
2022-05-19 18:59:27 -05:00
|
|
|
static smbiosv2_t *find_smbiosv2_in_efi64_system_table(efi64_system_table_t *system_table)
|
|
|
|
{
|
|
|
|
efi64_config_table_t *config_tables = (efi64_config_table_t *) map_region(system_table->config_tables, system_table->num_config_tables * sizeof(efi64_config_table_t), true);
|
2022-03-11 20:09:22 -06:00
|
|
|
if (config_tables == NULL) return NULL;
|
|
|
|
|
|
|
|
uintptr_t table_addr = 0;
|
2022-04-02 08:43:46 -05:00
|
|
|
for (uint32_t i = 0; i < system_table->num_config_tables; i++) {
|
2022-03-12 10:32:51 -06:00
|
|
|
if (memcmp( & config_tables[i].guid, & SMBIOS2_GUID, sizeof(efi_guid_t)) == 0) {
|
2022-03-11 20:09:22 -06:00
|
|
|
table_addr = config_tables[i].table;
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 08:43:46 -05:00
|
|
|
return (smbiosv2_t *) table_addr;
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
static smbiosv2_t *find_smbiosv2_in_efi32_system_table(efi32_system_table_t *system_table)
|
|
|
|
{
|
|
|
|
efi32_config_table_t *config_tables = (efi32_config_table_t *) map_region(system_table->config_tables, system_table->num_config_tables * sizeof(efi32_config_table_t), true);
|
2022-03-11 20:09:22 -06:00
|
|
|
if (config_tables == NULL) return NULL;
|
|
|
|
|
|
|
|
uintptr_t table_addr = 0;
|
2022-04-02 08:43:46 -05:00
|
|
|
for (uint32_t i = 0; i < system_table->num_config_tables; i++) {
|
2022-03-12 10:32:51 -06:00
|
|
|
if (memcmp( & config_tables[i].guid, & SMBIOS2_GUID, sizeof(efi_guid_t)) == 0) {
|
2022-03-11 20:09:22 -06:00
|
|
|
table_addr = config_tables[i].table;
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 08:43:46 -05:00
|
|
|
return (smbiosv2_t *) table_addr;
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
static uintptr_t find_smbiosv2_adr(void)
|
|
|
|
{
|
|
|
|
const boot_params_t *boot_params = (boot_params_t *) boot_params_addr;
|
|
|
|
const efi_info_t *efi_info = & boot_params->efi_info;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
smbiosv2_t *rp = NULL;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-04-02 08:43:46 -05:00
|
|
|
if (efi_info->loader_signature == EFI32_LOADER_SIGNATURE) {
|
2022-03-12 10:32:51 -06:00
|
|
|
// EFI32
|
2022-04-02 08:43:46 -05:00
|
|
|
if (rp == NULL && efi_info->loader_signature == EFI32_LOADER_SIGNATURE) {
|
|
|
|
uintptr_t system_table_addr = map_region(efi_info->sys_tab, sizeof(efi32_system_table_t), true);
|
2022-03-12 10:32:51 -06:00
|
|
|
system_table_addr = map_region(system_table_addr, sizeof(efi32_system_table_t), true);
|
|
|
|
if (system_table_addr != 0) {
|
2022-04-02 08:43:46 -05:00
|
|
|
rp = find_smbiosv2_in_efi32_system_table((efi32_system_table_t *) system_table_addr);
|
2022-03-12 10:32:51 -06:00
|
|
|
return (uintptr_t) rp;
|
|
|
|
}
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
|
|
|
}
|
2022-03-13 23:35:36 -05:00
|
|
|
#ifdef __x86_64__
|
2022-03-12 10:32:51 -06:00
|
|
|
if (rp == NULL && efi_info -> loader_signature == EFI64_LOADER_SIGNATURE) {
|
|
|
|
// EFI64
|
2022-04-02 08:43:46 -05:00
|
|
|
if (rp == NULL && efi_info->loader_signature == EFI64_LOADER_SIGNATURE) {
|
|
|
|
uintptr_t system_table_addr = (uintptr_t) efi_info->sys_tab_hi << 32 | (uintptr_t) efi_info->sys_tab;
|
2022-03-12 10:32:51 -06:00
|
|
|
system_table_addr = map_region(system_table_addr, sizeof(efi64_system_table_t), true);
|
|
|
|
if (system_table_addr != 0) {
|
2022-04-02 08:43:46 -05:00
|
|
|
rp = find_smbiosv2_in_efi64_system_table((efi64_system_table_t *) system_table_addr);
|
2022-03-12 10:32:51 -06:00
|
|
|
return (uintptr_t) rp;
|
|
|
|
}
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
|
|
|
}
|
2022-03-13 23:35:36 -05:00
|
|
|
#endif
|
2022-03-12 10:32:51 -06:00
|
|
|
if (rp == NULL) {
|
|
|
|
// BIOS
|
2022-05-19 18:59:27 -05:00
|
|
|
uint8_t *dmi, *dmi_search_start;
|
2022-04-02 08:43:46 -05:00
|
|
|
dmi_search_start = (uint8_t *) 0x000F0000;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
|
|
|
for (dmi = dmi_search_start; dmi < dmi_search_start + 0xffff0; dmi += 16) {
|
2022-05-19 18:59:27 -05:00
|
|
|
if ( *dmi == '_' && *(dmi + 1) == 'S' && *(dmi + 2) == 'M' && *(dmi + 3) == '_')
|
2022-03-12 10:32:51 -06:00
|
|
|
return (uintptr_t) dmi;
|
|
|
|
}
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
|
|
|
|
2022-03-12 10:32:51 -06:00
|
|
|
return 0;
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
static int parse_dmi(uint16_t numstructs)
|
|
|
|
{
|
|
|
|
const uint8_t *dmi = table_start;
|
2022-03-12 10:32:51 -06:00
|
|
|
int tstruct_count = 0;
|
2022-04-02 08:43:46 -05:00
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
// Struct type 1 is one of the mandatory types, so we're dealing with invalid data
|
|
|
|
// if its size is lower than that of a minimal type 1 struct (plus a couple bytes).
|
2022-04-16 06:40:50 -05:00
|
|
|
if (table_length < sizeof(struct system_info)) {
|
2022-04-02 08:43:46 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
// Parse structs
|
2022-04-02 08:43:46 -05:00
|
|
|
while (dmi < table_start + table_length - 2) { // -2 for header type and length.
|
2022-05-19 18:59:27 -05:00
|
|
|
const struct tstruct_header *header = (struct tstruct_header *) dmi;
|
2022-04-02 08:43:46 -05:00
|
|
|
|
2022-04-16 06:40:50 -05:00
|
|
|
// Type 1 - System Information
|
|
|
|
if (header->type == 1 && header->length > offsetof(struct system_info, wut)) {
|
|
|
|
// Multiple type 1 structs are not allowed by the standard. Still, effectively pick up the last one.
|
|
|
|
dmi_system_info = (struct system_info *) dmi;
|
|
|
|
}
|
2022-04-02 08:43:46 -05:00
|
|
|
// Type 2 - Baseboard Information
|
2022-04-16 06:40:50 -05:00
|
|
|
else if (header->type == 2 && header->length > offsetof(struct baseboard_info, serialnumber)) {
|
2022-04-02 08:43:46 -05:00
|
|
|
// Multiple type 2 structs are allowed by the standard. Effectively pick up the last one.
|
2022-04-16 06:40:50 -05:00
|
|
|
dmi_baseboard_info = (struct baseboard_info *) dmi;
|
2022-04-02 08:43:46 -05:00
|
|
|
}
|
|
|
|
// Type 17 - Memory Device
|
|
|
|
else if (header->type == 17 && header->length > offsetof(struct mem_dev, partnum)) {
|
2022-05-19 18:59:27 -05:00
|
|
|
// Multiple type 17 structs are allowed, with unpopulated slots sometimes
|
|
|
|
// reported as type 2 (unknown). If type is 0 (uninitialized) or 1/2 (previously
|
|
|
|
// initialized with unknown value) => set or overwrite the struct
|
|
|
|
if (dmi_memory_device->type <= 2) {
|
|
|
|
dmi_memory_device = (struct mem_dev *) dmi;
|
|
|
|
}
|
2022-04-02 08:43:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dmi += header->length;
|
|
|
|
|
|
|
|
if (dmi >= table_start + table_length) {
|
|
|
|
dmi_system_info = NULL;
|
2022-04-16 06:40:50 -05:00
|
|
|
dmi_baseboard_info = NULL;
|
2022-04-02 08:43:46 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dmi < table_start + table_length - 1) && !(*dmi == 0 && *(dmi + 1) == 0)) {
|
|
|
|
dmi++;
|
|
|
|
}
|
|
|
|
|
|
|
|
dmi += 2;
|
|
|
|
|
|
|
|
if ((dmi > table_start + table_length) || (++tstruct_count > numstructs)) {
|
|
|
|
dmi_system_info = NULL;
|
2022-04-16 06:40:50 -05:00
|
|
|
dmi_baseboard_info = NULL;
|
2022-04-02 08:43:46 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
int smbios_init(void)
|
|
|
|
{
|
2022-03-12 10:32:51 -06:00
|
|
|
uintptr_t smb_adr;
|
2022-05-19 18:59:27 -05:00
|
|
|
const uint8_t *dmi_start;
|
|
|
|
const smbiosv2_t *eps;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
|
|
|
// Get SMBIOS Address
|
2022-04-02 08:43:46 -05:00
|
|
|
smb_adr = find_smbiosv2_adr();
|
2022-03-12 10:32:51 -06:00
|
|
|
|
|
|
|
if (smb_adr == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-04-02 08:43:46 -05:00
|
|
|
dmi_start = (const uint8_t *) smb_adr;
|
|
|
|
eps = (const smbiosv2_t *) smb_adr;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
|
|
|
// Verify checksum
|
|
|
|
int8_t checksum = 0;
|
2022-05-19 18:59:27 -05:00
|
|
|
const uint8_t *dmi = dmi_start;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-03-23 15:11:56 -05:00
|
|
|
for (; dmi < (dmi_start + eps->length); dmi++) {
|
2022-05-19 18:59:27 -05:00
|
|
|
checksum += *dmi;
|
2022-03-12 10:32:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (checksum) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SMBIOS 2.3 required
|
2022-04-02 08:43:46 -05:00
|
|
|
if (eps->majorversion < 2 && eps->minorversion < 3) {
|
2022-03-12 10:32:51 -06:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-04-02 08:43:46 -05:00
|
|
|
table_start = (const uint8_t *)(uintptr_t)eps->tableaddress;
|
|
|
|
table_length = (uint32_t)eps->tablelength;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-04-02 08:43:46 -05:00
|
|
|
return parse_dmi(eps->numstructs);
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
void print_smbios_startup_info(void)
|
|
|
|
{
|
|
|
|
// Use baseboard info (struct type 2) as primary source of information,
|
|
|
|
// and fall back to system info (struct type 1). Indeed, while the later
|
|
|
|
// may contain less useful information than the former, its presence is
|
|
|
|
// mandated by the successive revisions of the SMBIOS standard.
|
|
|
|
// NOTE: we can get away with this ugly cast because the offsets of
|
|
|
|
// .manufacturer and .productname are the same in system_info and baseboard_info.
|
|
|
|
|
|
|
|
struct system_info *ptr = dmi_baseboard_info != NULL ?
|
|
|
|
(struct system_info *)dmi_baseboard_info : dmi_system_info;
|
|
|
|
|
2022-04-16 06:40:50 -05:00
|
|
|
if (ptr != NULL) {
|
2022-05-19 18:59:27 -05:00
|
|
|
char *sys_man, *sys_sku;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-04-02 08:43:46 -05:00
|
|
|
int sl1, sl2, dmicol;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
uint16_t struct_length = table_length - ((uint8_t *)&ptr->header - (uint8_t *)table_start);
|
|
|
|
|
|
|
|
sys_man = get_tstruct_string(&ptr->header, struct_length, ptr->manufacturer);
|
2022-04-02 08:43:46 -05:00
|
|
|
if (sys_man != NULL) {
|
|
|
|
sl1 = strlen(sys_man);
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-05-19 18:59:27 -05:00
|
|
|
sys_sku = get_tstruct_string(&ptr->header, struct_length, ptr->productname);
|
2022-04-02 08:43:46 -05:00
|
|
|
if (sys_sku != NULL) {
|
|
|
|
sl2 = strlen(sys_sku);
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-04-02 08:43:46 -05:00
|
|
|
if (sl1 && sl2) {
|
|
|
|
dmicol = 40 - ((sl1 + sl2) / 2);
|
|
|
|
dmicol = prints(LINE_DMI, dmicol, sys_man);
|
|
|
|
prints(LINE_DMI, dmicol + 1, sys_sku);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-12 10:32:51 -06:00
|
|
|
}
|
2022-03-13 23:35:36 -05:00
|
|
|
}
|