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"
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
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-03-23 15:11:56 -05:00
|
|
|
struct system_map *dmi_system_info;
|
|
|
|
struct mem_dev *dmi_memory_device;
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-04-02 08:43:46 -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;
|
|
|
|
char * a = (char *) header + header->length;
|
2022-03-12 10:32:51 -06:00
|
|
|
n--;
|
|
|
|
do {
|
|
|
|
if (! * a)
|
|
|
|
n--;
|
|
|
|
if (!n && * a)
|
|
|
|
return a;
|
|
|
|
a++;
|
2022-04-02 08:43:46 -05:00
|
|
|
} while (a < ((char *) header + maxlen) && !( * a == 0 && * (a - 1) == 0));
|
|
|
|
return NULL;
|
2022-03-11 20:09:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __x86_64__
|
2022-04-02 08:43:46 -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-04-02 08:43:46 -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-04-02 08:43:46 -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-04-02 08:43:46 -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-04-02 08:43:46 -05:00
|
|
|
uint8_t * dmi, * dmi_search_start;
|
|
|
|
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) {
|
|
|
|
if ( * dmi == '_' && * (dmi + 1) == 'S' && * (dmi + 2) == 'M' && * (dmi + 3) == '_')
|
|
|
|
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-04-02 08:43:46 -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
|
|
|
|
|
|
|
if (table_length < sizeof(struct system_map) + 2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse all structs (currently restricted to Type 2 only)
|
|
|
|
while (dmi < table_start + table_length - 2) { // -2 for header type and length.
|
|
|
|
const struct tstruct_header * header = (struct tstruct_header *) dmi;
|
|
|
|
|
|
|
|
// Type 2 - Baseboard Information
|
|
|
|
if (header->type == 2 && header->length > offsetof(struct system_map, serialnumber)) {
|
|
|
|
// Multiple type 2 structs are allowed by the standard. Effectively pick up the last one.
|
|
|
|
dmi_system_info = (struct system_map *) dmi;
|
|
|
|
}
|
|
|
|
// Type 17 - Memory Device
|
|
|
|
else if (header->type == 17 && header->length > offsetof(struct mem_dev, partnum)) {
|
|
|
|
dmi_memory_device = (struct mem_dev *) dmi;
|
|
|
|
}
|
|
|
|
|
|
|
|
dmi += header->length;
|
|
|
|
|
|
|
|
if (dmi >= table_start + table_length) {
|
|
|
|
dmi_system_info = NULL;
|
|
|
|
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;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int smbios_init(void) {
|
2022-03-12 10:32:51 -06:00
|
|
|
uintptr_t smb_adr;
|
2022-04-02 08:43:46 -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-04-02 08:43:46 -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-03-12 10:32:51 -06:00
|
|
|
checksum += * dmi;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-12 10:32:51 -06:00
|
|
|
void print_smbios_startup_info(void) {
|
2022-04-02 08:43:46 -05:00
|
|
|
if (dmi_system_info != NULL) {
|
|
|
|
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-04-02 08:43:46 -05:00
|
|
|
sys_man = get_tstruct_string(&dmi_system_info->header, table_length - ((uint8_t *)&dmi_system_info->header - (uint8_t *)table_start), dmi_system_info->manufacturer);
|
|
|
|
if (sys_man != NULL) {
|
|
|
|
sl1 = strlen(sys_man);
|
2022-03-12 10:32:51 -06:00
|
|
|
|
2022-04-02 08:43:46 -05:00
|
|
|
sys_sku = get_tstruct_string(&dmi_system_info->header, table_length - ((uint8_t *)&dmi_system_info->header - (uint8_t *)table_start), dmi_system_info->productname);
|
|
|
|
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
|
|
|
}
|