Fix an issue where DDR4 Bank switch fail because DMI tables have multiples Type 17 structs reporting unpopulated slots as Type 2 (unknown), overwriting the valid struct with the populated slot. Code cleanup to improve readability

This commit is contained in:
Sam Demeulemeester 2022-05-20 01:59:27 +02:00
parent caa07482a0
commit d901f9e8a1

View File

@ -5,8 +5,6 @@
#include "stdint.h" #include "stdint.h"
#include "string.h" #include "string.h"
#include "display.h" #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.
#include "boot.h" #include "boot.h"
#include "bootparams.h" #include "bootparams.h"
@ -16,6 +14,9 @@ static uint32_t table_length = 0; // 16-bit in SMBIOS v2, 32-bit in SMBIOS v3.
#define LINE_DMI 23 #define LINE_DMI 23
static const uint8_t *table_start = NULL;
static uint32_t table_length = 0; // 16-bit in SMBIOS v2, 32-bit in SMBIOS v3.
static const efi_guid_t SMBIOS2_GUID = { 0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d} }; 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... // SMBIOS v3 compliant FW must include an SMBIOS v2 table, but maybe parse SM3 table later...
@ -25,7 +26,8 @@ struct system_info *dmi_system_info;
struct baseboard_info *dmi_baseboard_info; struct baseboard_info *dmi_baseboard_info;
struct mem_dev *dmi_memory_device; struct mem_dev *dmi_memory_device;
static char * get_tstruct_string(struct tstruct_header * header, uint16_t maxlen, int n) { static char *get_tstruct_string(struct tstruct_header *header, uint16_t maxlen, int n)
{
if (n < 1) if (n < 1)
return NULL; return NULL;
char *a = (char *) header + header->length; char *a = (char *) header + header->length;
@ -41,7 +43,8 @@ static char * get_tstruct_string(struct tstruct_header * header, uint16_t maxlen
} }
#ifdef __x86_64__ #ifdef __x86_64__
static smbiosv2_t * find_smbiosv2_in_efi64_system_table(efi64_system_table_t * system_table) { 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); 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);
if (config_tables == NULL) return NULL; if (config_tables == NULL) return NULL;
@ -55,7 +58,8 @@ static smbiosv2_t * find_smbiosv2_in_efi64_system_table(efi64_system_table_t * s
} }
#endif #endif
static smbiosv2_t * find_smbiosv2_in_efi32_system_table(efi32_system_table_t * system_table) { 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); 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);
if (config_tables == NULL) return NULL; if (config_tables == NULL) return NULL;
@ -68,7 +72,8 @@ static smbiosv2_t * find_smbiosv2_in_efi32_system_table(efi32_system_table_t * s
return (smbiosv2_t *) table_addr; return (smbiosv2_t *) table_addr;
} }
static uintptr_t find_smbiosv2_adr(void) { static uintptr_t find_smbiosv2_adr(void)
{
const boot_params_t *boot_params = (boot_params_t *) boot_params_addr; const boot_params_t *boot_params = (boot_params_t *) boot_params_addr;
const efi_info_t *efi_info = & boot_params->efi_info; const efi_info_t *efi_info = & boot_params->efi_info;
@ -112,16 +117,18 @@ static uintptr_t find_smbiosv2_adr(void) {
return 0; return 0;
} }
static int parse_dmi(uint16_t numstructs) { static int parse_dmi(uint16_t numstructs)
{
const uint8_t *dmi = table_start; const uint8_t *dmi = table_start;
int tstruct_count = 0; int tstruct_count = 0;
// 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). // 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).
if (table_length < sizeof(struct system_info)) { if (table_length < sizeof(struct system_info)) {
return -1; return -1;
} }
// Parse all structs (currently restricted to Type 2 only) // Parse structs
while (dmi < table_start + table_length - 2) { // -2 for header type and length. while (dmi < table_start + table_length - 2) { // -2 for header type and length.
const struct tstruct_header *header = (struct tstruct_header *) dmi; const struct tstruct_header *header = (struct tstruct_header *) dmi;
@ -137,8 +144,13 @@ static int parse_dmi(uint16_t numstructs) {
} }
// Type 17 - Memory Device // Type 17 - Memory Device
else if (header->type == 17 && header->length > offsetof(struct mem_dev, partnum)) { else if (header->type == 17 && header->length > offsetof(struct mem_dev, partnum)) {
// 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; dmi_memory_device = (struct mem_dev *) dmi;
} }
}
dmi += header->length; dmi += header->length;
@ -160,11 +172,11 @@ static int parse_dmi(uint16_t numstructs) {
return -1; return -1;
} }
} }
return 0; return 0;
} }
int smbios_init(void) { int smbios_init(void)
{
uintptr_t smb_adr; uintptr_t smb_adr;
const uint8_t *dmi_start; const uint8_t *dmi_start;
const smbiosv2_t *eps; const smbiosv2_t *eps;
@ -202,21 +214,30 @@ int smbios_init(void) {
return parse_dmi(eps->numstructs); return parse_dmi(eps->numstructs);
} }
void print_smbios_startup_info(void) { 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 latter may contain less useful information than the former, its presence is mandated by the successive revisions of the SMBIOS standard. // Use baseboard info (struct type 2) as primary source of information,
// 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. // and fall back to system info (struct type 1). Indeed, while the later
struct system_info * ptr = dmi_baseboard_info != NULL ? (struct system_info *)dmi_baseboard_info : dmi_system_info; // 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;
if (ptr != NULL) { if (ptr != NULL) {
char *sys_man, *sys_sku; char *sys_man, *sys_sku;
int sl1, sl2, dmicol; int sl1, sl2, dmicol;
sys_man = get_tstruct_string(&ptr->header, table_length - ((uint8_t *)&ptr->header - (uint8_t *)table_start), ptr->manufacturer); 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);
if (sys_man != NULL) { if (sys_man != NULL) {
sl1 = strlen(sys_man); sl1 = strlen(sys_man);
sys_sku = get_tstruct_string(&ptr->header, table_length - ((uint8_t *)&ptr->header - (uint8_t *)table_start), ptr->productname); sys_sku = get_tstruct_string(&ptr->header, struct_length, ptr->productname);
if (sys_sku != NULL) { if (sys_sku != NULL) {
sl2 = strlen(sys_sku); sl2 = strlen(sys_sku);