| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "efi-string.h" |
| 4 |
#include "smbios.h" |
| 5 |
#include "util.h" |
| 6 |
|
| 7 |
#define SMBIOS_TABLE_GUID \ |
| 8 |
GUID_DEF(0xeb9d2d31, 0x2d88, 0x11d3, 0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d) |
| 9 |
#define SMBIOS3_TABLE_GUID \ |
| 10 |
GUID_DEF(0xf2fd1544, 0x9794, 0x4a2c, 0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94) |
| 11 |
|
| 12 |
typedef struct { |
| 13 |
uint8_t anchor_string[4]; |
| 14 |
uint8_t entry_point_structure_checksum; |
| 15 |
uint8_t entry_point_length; |
| 16 |
uint8_t major_version; |
| 17 |
uint8_t minor_version; |
| 18 |
uint16_t max_structure_size; |
| 19 |
uint8_t entry_point_revision; |
| 20 |
uint8_t formatted_area[5]; |
| 21 |
uint8_t intermediate_anchor_string[5]; |
| 22 |
uint8_t intermediate_checksum; |
| 23 |
uint16_t table_length; |
| 24 |
uint32_t table_address; |
| 25 |
uint16_t number_of_smbios_structures; |
| 26 |
uint8_t smbios_bcd_revision; |
| 27 |
} _packed_ SmbiosEntryPoint; |
| 28 |
|
| 29 |
typedef struct { |
| 30 |
uint8_t anchor_string[5]; |
| 31 |
uint8_t entry_point_structure_checksum; |
| 32 |
uint8_t entry_point_length; |
| 33 |
uint8_t major_version; |
| 34 |
uint8_t minor_version; |
| 35 |
uint8_t docrev; |
| 36 |
uint8_t entry_point_revision; |
| 37 |
uint8_t reserved; |
| 38 |
uint32_t table_maximum_size; |
| 39 |
uint64_t table_address; |
| 40 |
} _packed_ Smbios3EntryPoint; |
| 41 |
|
| 42 |
typedef struct { |
| 43 |
SmbiosHeader header; |
| 44 |
uint8_t vendor; |
| 45 |
uint8_t bios_version; |
| 46 |
uint16_t bios_segment; |
| 47 |
uint8_t bios_release_date; |
| 48 |
uint8_t bios_size; |
| 49 |
uint64_t bios_characteristics; |
| 50 |
uint8_t bios_characteristics_ext[2]; |
| 51 |
} _packed_ SmbiosTableType0; |
| 52 |
|
| 53 |
typedef struct { |
| 54 |
SmbiosHeader header; |
| 55 |
uint8_t manufacturer; |
| 56 |
uint8_t product_name; |
| 57 |
uint8_t version; |
| 58 |
uint8_t serial_number; |
| 59 |
} _packed_ SmbiosTableType2; |
| 60 |
|
| 61 |
typedef struct { |
| 62 |
SmbiosHeader header; |
| 63 |
uint8_t count; |
| 64 |
char contents[]; |
| 65 |
} _packed_ SmbiosTableType11; |
| 66 |
|
| 67 |
static const void* find_smbios_configuration_table(uint64_t *ret_size) { |
| 68 |
assert(ret_size); |
| 69 |
|
| 70 |
const Smbios3EntryPoint *entry3 = find_configuration_table(MAKE_GUID_PTR(SMBIOS3_TABLE)); |
| 71 |
if (entry3 && memcmp(entry3->anchor_string, "_SM3_", 5) == 0 && |
| 72 |
entry3->entry_point_length <= sizeof(*entry3)) { |
| 73 |
*ret_size = entry3->table_maximum_size; |
| 74 |
return PHYSICAL_ADDRESS_TO_POINTER(entry3->table_address); |
| 75 |
} |
| 76 |
|
| 77 |
const SmbiosEntryPoint *entry = find_configuration_table(MAKE_GUID_PTR(SMBIOS_TABLE)); |
| 78 |
if (entry && memcmp(entry->anchor_string, "_SM_", 4) == 0 && |
| 79 |
entry->entry_point_length <= sizeof(*entry)) { |
| 80 |
*ret_size = entry->table_length; |
| 81 |
return PHYSICAL_ADDRESS_TO_POINTER(entry->table_address); |
| 82 |
} |
| 83 |
|
| 84 |
*ret_size = 0; |
| 85 |
return NULL; |
| 86 |
} |
| 87 |
|
| 88 |
/* Given 'p' pointing at a structure header with 'size' bytes left in the table from there onwards, |
| 89 |
* returns a pointer just past the end of this structure (i.e. the start of the next one), accounting |
| 90 |
* for the formatted area and the trailing string set (terminated by a double NUL byte). Returns NULL |
| 91 |
* if the structure is malformed or runs past the end of the table. */ |
| 92 |
static const uint8_t* smbios_structure_end(const uint8_t *p, uint64_t size) { |
| 93 |
assert(p); |
| 94 |
|
| 95 |
if (size < sizeof(SmbiosHeader)) |
| 96 |
return NULL; |
| 97 |
|
| 98 |
const SmbiosHeader *header = (const SmbiosHeader *) p; |
| 99 |
if (size < header->length) |
| 100 |
return NULL; |
| 101 |
|
| 102 |
/* Skip over formatted area. */ |
| 103 |
const uint8_t *q = p + header->length; |
| 104 |
size -= header->length; |
| 105 |
|
| 106 |
/* Special case: if there are no strings appended, we'll see two NUL bytes. */ |
| 107 |
if (size >= 2 && q[0] == 0 && q[1] == 0) |
| 108 |
return q + 2; |
| 109 |
|
| 110 |
/* Skip over a populated string table. */ |
| 111 |
bool first = true; |
| 112 |
for (;;) { |
| 113 |
const uint8_t *e = memchr(q, 0, size); |
| 114 |
if (!e) |
| 115 |
return NULL; |
| 116 |
|
| 117 |
if (!first && e == q) /* Double NUL byte means we've reached the end of the string table. */ |
| 118 |
return q + 1; |
| 119 |
|
| 120 |
size -= e + 1 - q; |
| 121 |
q = e + 1; |
| 122 |
first = false; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
static const SmbiosHeader* get_smbios_table(uint8_t type, size_t min_size, uint64_t *ret_size_left) { |
| 127 |
uint64_t size; |
| 128 |
const uint8_t *p = find_smbios_configuration_table(&size); |
| 129 |
if (!p) |
| 130 |
goto not_found; |
| 131 |
|
| 132 |
for (;;) { |
| 133 |
if (size < sizeof(SmbiosHeader)) |
| 134 |
goto not_found; |
| 135 |
|
| 136 |
const SmbiosHeader *header = (const SmbiosHeader *) p; |
| 137 |
|
| 138 |
/* End of table. */ |
| 139 |
if (header->type == 127) |
| 140 |
goto not_found; |
| 141 |
|
| 142 |
if (size < header->length) |
| 143 |
goto not_found; |
| 144 |
|
| 145 |
if (header->type == type) { |
| 146 |
/* Table is smaller than the minimum expected size? Refuse */ |
| 147 |
if (header->length < min_size) |
| 148 |
goto not_found; |
| 149 |
|
| 150 |
if (ret_size_left) |
| 151 |
*ret_size_left = size; |
| 152 |
return header; /* Yay! */ |
| 153 |
} |
| 154 |
|
| 155 |
const uint8_t *next = smbios_structure_end(p, size); |
| 156 |
if (!next) |
| 157 |
goto not_found; |
| 158 |
|
| 159 |
size -= next - p; |
| 160 |
p = next; |
| 161 |
} |
| 162 |
|
| 163 |
not_found: |
| 164 |
if (ret_size_left) |
| 165 |
*ret_size_left = 0; |
| 166 |
|
| 167 |
return NULL; |
| 168 |
} |
| 169 |
|
| 170 |
void smbios_foreach(SmbiosForeachFunc func, void *userdata) { |
| 171 |
assert(func); |
| 172 |
|
| 173 |
uint64_t size; |
| 174 |
const uint8_t *p = find_smbios_configuration_table(&size); |
| 175 |
if (!p) |
| 176 |
return; |
| 177 |
|
| 178 |
/* Walks the SMBIOS table exactly once, invoking 'func' for every structure. 'func' receives the |
| 179 |
* structure's header (which carries its type) and the structure's total length (formatted area + |
| 180 |
* trailing string set); returning false stops the iteration early. */ |
| 181 |
|
| 182 |
for (;;) { |
| 183 |
if (size < sizeof(SmbiosHeader)) |
| 184 |
return; |
| 185 |
|
| 186 |
const SmbiosHeader *header = (const SmbiosHeader *) p; |
| 187 |
|
| 188 |
/* End of table. */ |
| 189 |
if (header->type == 127) |
| 190 |
return; |
| 191 |
|
| 192 |
const uint8_t *next = smbios_structure_end(p, size); |
| 193 |
if (!next) |
| 194 |
return; |
| 195 |
|
| 196 |
if (!func(header, next - p, userdata)) |
| 197 |
return; |
| 198 |
|
| 199 |
size -= next - p; |
| 200 |
p = next; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
bool smbios_in_hypervisor(void) { |
| 205 |
/* Look up BIOS Information (Type 0). */ |
| 206 |
const SmbiosTableType0 *type0 = (const SmbiosTableType0 *) get_smbios_table(0, sizeof(SmbiosTableType0), /* ret_size_left= */ NULL); |
| 207 |
if (!type0) |
| 208 |
return false; |
| 209 |
|
| 210 |
/* Bit 4 of 2nd BIOS characteristics extension bytes indicates virtualization. */ |
| 211 |
return FLAGS_SET(type0->bios_characteristics_ext[1], 1 << 4); |
| 212 |
} |
| 213 |
|
| 214 |
const char* smbios_find_oem_string(const char *name, const char *after) { |
| 215 |
uint64_t left; |
| 216 |
|
| 217 |
assert(name); |
| 218 |
|
| 219 |
const SmbiosTableType11 *type11 = (const SmbiosTableType11 *) get_smbios_table(11, sizeof(SmbiosTableType11), &left); |
| 220 |
if (!type11) |
| 221 |
return NULL; |
| 222 |
|
| 223 |
assert(left >= type11->header.length); /* get_smbios_table() already validated this */ |
| 224 |
left -= type11->header.length; |
| 225 |
|
| 226 |
for (const char *p = type11->contents, *limit = type11->contents + left; p < limit; ) { |
| 227 |
const char *e = memchr(p, 0, limit - p); |
| 228 |
if (!e || e == p) /* Double NUL byte means we've reached the end of the OEM strings. */ |
| 229 |
break; |
| 230 |
|
| 231 |
const char *suffix = startswith8(p, name); |
| 232 |
if (suffix && (!after || suffix > after)) |
| 233 |
return suffix; |
| 234 |
|
| 235 |
p = e + 1; |
| 236 |
} |
| 237 |
|
| 238 |
return NULL; |
| 239 |
} |
| 240 |
|
| 241 |
static const char* smbios_get_string(const SmbiosHeader *header, size_t nr, uint64_t left) { |
| 242 |
const char *s = (const char *) ASSERT_PTR(header); |
| 243 |
|
| 244 |
/* We assume that get_smbios_table() already validated the header size making some superficial sense */ |
| 245 |
assert(left >= header->length); |
| 246 |
s += header->length; |
| 247 |
left -= header->length; |
| 248 |
|
| 249 |
size_t index = 1; |
| 250 |
for (const char *p = s, *limit = s + left; index <= nr && p < limit; index++) { |
| 251 |
const char *e = memchr(p, 0, limit - p); |
| 252 |
if (!e || e == p) /* Double NUL byte means we've reached the end of the strings. */ |
| 253 |
break; |
| 254 |
|
| 255 |
if (index == nr) |
| 256 |
return p; |
| 257 |
|
| 258 |
p = e + 1; |
| 259 |
} |
| 260 |
|
| 261 |
return NULL; |
| 262 |
} |
| 263 |
|
| 264 |
void smbios_raw_info_populate(RawSmbiosInfo *ret_info) { |
| 265 |
uint64_t left; |
| 266 |
|
| 267 |
assert(ret_info); |
| 268 |
|
| 269 |
const SmbiosTableType1 *type1 = (const SmbiosTableType1 *) get_smbios_table(1, sizeof(SmbiosTableType1), &left); |
| 270 |
if (type1) { |
| 271 |
ret_info->manufacturer = smbios_get_string(&type1->header, type1->manufacturer, left); |
| 272 |
ret_info->product_name = smbios_get_string(&type1->header, type1->product_name, left); |
| 273 |
ret_info->product_sku = smbios_get_string(&type1->header, type1->sku_number, left); |
| 274 |
ret_info->family = smbios_get_string(&type1->header, type1->family, left); |
| 275 |
} else { |
| 276 |
ret_info->manufacturer = NULL; |
| 277 |
ret_info->product_name = NULL; |
| 278 |
ret_info->product_sku = NULL; |
| 279 |
ret_info->family = NULL; |
| 280 |
} |
| 281 |
|
| 282 |
const SmbiosTableType2 *type2 = (const SmbiosTableType2 *) get_smbios_table(2, sizeof(SmbiosTableType2), &left); |
| 283 |
if (type2) { |
| 284 |
ret_info->baseboard_manufacturer = smbios_get_string(&type2->header, type2->manufacturer, left); |
| 285 |
ret_info->baseboard_product = smbios_get_string(&type2->header, type2->product_name, left); |
| 286 |
} else { |
| 287 |
ret_info->baseboard_manufacturer = NULL; |
| 288 |
ret_info->baseboard_product = NULL; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
void smbios_raw_info_get_cached(RawSmbiosInfo *ret_info) { |
| 293 |
static RawSmbiosInfo info = {}; |
| 294 |
static bool cached = false; |
| 295 |
|
| 296 |
assert(ret_info); |
| 297 |
|
| 298 |
if (!cached) { |
| 299 |
smbios_raw_info_populate(&info); |
| 300 |
cached = true; |
| 301 |
} |
| 302 |
|
| 303 |
*ret_info = info; |
| 304 |
} |