Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com> |
| 3 | * |
| 4 | * Adapted from coreboot src/arch/x86/smbios.c |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Alexander Graf | fb22808 | 2016-08-19 01:23:23 +0200 | [diff] [blame] | 10 | #include <smbios.h> |
| 11 | #include <tables_csum.h> |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 12 | #include <version.h> |
Alexander Graf | e44b3c0 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 13 | #ifdef CONFIG_CPU |
| 14 | #include <cpu.h> |
| 15 | #include <dm.h> |
| 16 | #include <dm/uclass-internal.h> |
| 17 | #endif |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 18 | |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 19 | /** |
| 20 | * smbios_add_string() - add a string to the string area |
| 21 | * |
| 22 | * This adds a string to the string area which is appended directly after |
| 23 | * the formatted portion of an SMBIOS structure. |
| 24 | * |
| 25 | * @start: string area start address |
| 26 | * @str: string to add |
| 27 | * @return: string number in the string area |
| 28 | */ |
| 29 | static int smbios_add_string(char *start, const char *str) |
| 30 | { |
| 31 | int i = 1; |
| 32 | char *p = start; |
| 33 | |
| 34 | for (;;) { |
| 35 | if (!*p) { |
| 36 | strcpy(p, str); |
| 37 | p += strlen(str); |
| 38 | *p++ = '\0'; |
| 39 | *p++ = '\0'; |
| 40 | |
| 41 | return i; |
| 42 | } |
| 43 | |
| 44 | if (!strcmp(p, str)) |
| 45 | return i; |
| 46 | |
| 47 | p += strlen(p) + 1; |
| 48 | i++; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * smbios_string_table_len() - compute the string area size |
| 54 | * |
| 55 | * This computes the size of the string area including the string terminator. |
| 56 | * |
| 57 | * @start: string area start address |
| 58 | * @return: string area size |
| 59 | */ |
| 60 | static int smbios_string_table_len(char *start) |
| 61 | { |
| 62 | char *p = start; |
| 63 | int i, len = 0; |
| 64 | |
| 65 | while (*p) { |
| 66 | i = strlen(p) + 1; |
| 67 | p += i; |
| 68 | len += i; |
| 69 | } |
| 70 | |
| 71 | return len + 1; |
| 72 | } |
| 73 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 74 | static int smbios_write_type0(ulong *current, int handle) |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 75 | { |
| 76 | struct smbios_type0 *t = (struct smbios_type0 *)*current; |
| 77 | int len = sizeof(struct smbios_type0); |
| 78 | |
| 79 | memset(t, 0, sizeof(struct smbios_type0)); |
| 80 | fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle); |
| 81 | t->vendor = smbios_add_string(t->eos, "U-Boot"); |
| 82 | t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION); |
| 83 | t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE); |
Alexander Graf | 66f96e1 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 84 | #ifdef CONFIG_ROM_SIZE |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 85 | t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1; |
Alexander Graf | 66f96e1 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 86 | #endif |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 87 | t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED | |
| 88 | BIOS_CHARACTERISTICS_SELECTABLE_BOOT | |
| 89 | BIOS_CHARACTERISTICS_UPGRADEABLE; |
| 90 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 91 | t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI; |
| 92 | #endif |
Alexander Graf | 66f96e1 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 93 | #ifdef CONFIG_EFI_LOADER |
| 94 | t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI; |
| 95 | #endif |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 96 | t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET; |
Alexander Graf | 66f96e1 | 2016-08-19 01:23:29 +0200 | [diff] [blame] | 97 | |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 98 | t->bios_major_release = 0xff; |
| 99 | t->bios_minor_release = 0xff; |
| 100 | t->ec_major_release = 0xff; |
| 101 | t->ec_minor_release = 0xff; |
| 102 | |
| 103 | len = t->length + smbios_string_table_len(t->eos); |
| 104 | *current += len; |
| 105 | |
| 106 | return len; |
| 107 | } |
| 108 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 109 | static int smbios_write_type1(ulong *current, int handle) |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 110 | { |
| 111 | struct smbios_type1 *t = (struct smbios_type1 *)*current; |
| 112 | int len = sizeof(struct smbios_type1); |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 113 | char *serial_str = env_get("serial#"); |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 114 | |
| 115 | memset(t, 0, sizeof(struct smbios_type1)); |
| 116 | fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle); |
Bin Meng | 543a82b | 2016-05-22 01:45:38 -0700 | [diff] [blame] | 117 | t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER); |
| 118 | t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME); |
Alexander Graf | 9a3e3e2 | 2016-08-19 01:23:31 +0200 | [diff] [blame] | 119 | if (serial_str) { |
| 120 | strncpy((char*)t->uuid, serial_str, sizeof(t->uuid)); |
| 121 | t->serial_number = smbios_add_string(t->eos, serial_str); |
| 122 | } |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 123 | |
| 124 | len = t->length + smbios_string_table_len(t->eos); |
| 125 | *current += len; |
| 126 | |
| 127 | return len; |
| 128 | } |
| 129 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 130 | static int smbios_write_type2(ulong *current, int handle) |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 131 | { |
| 132 | struct smbios_type2 *t = (struct smbios_type2 *)*current; |
| 133 | int len = sizeof(struct smbios_type2); |
| 134 | |
| 135 | memset(t, 0, sizeof(struct smbios_type2)); |
| 136 | fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle); |
Bin Meng | 543a82b | 2016-05-22 01:45:38 -0700 | [diff] [blame] | 137 | t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER); |
| 138 | t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME); |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 139 | t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING; |
| 140 | t->board_type = SMBIOS_BOARD_MOTHERBOARD; |
| 141 | |
| 142 | len = t->length + smbios_string_table_len(t->eos); |
| 143 | *current += len; |
| 144 | |
| 145 | return len; |
| 146 | } |
| 147 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 148 | static int smbios_write_type3(ulong *current, int handle) |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 149 | { |
| 150 | struct smbios_type3 *t = (struct smbios_type3 *)*current; |
| 151 | int len = sizeof(struct smbios_type3); |
| 152 | |
| 153 | memset(t, 0, sizeof(struct smbios_type3)); |
| 154 | fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle); |
Bin Meng | 543a82b | 2016-05-22 01:45:38 -0700 | [diff] [blame] | 155 | t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER); |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 156 | t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP; |
| 157 | t->bootup_state = SMBIOS_STATE_SAFE; |
| 158 | t->power_supply_state = SMBIOS_STATE_SAFE; |
| 159 | t->thermal_state = SMBIOS_STATE_SAFE; |
| 160 | t->security_status = SMBIOS_SECURITY_NONE; |
| 161 | |
| 162 | len = t->length + smbios_string_table_len(t->eos); |
| 163 | *current += len; |
| 164 | |
| 165 | return len; |
| 166 | } |
| 167 | |
Alexander Graf | e44b3c0 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 168 | static void smbios_write_type4_dm(struct smbios_type4 *t) |
| 169 | { |
| 170 | u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN; |
| 171 | const char *vendor = "Unknown"; |
| 172 | const char *name = "Unknown"; |
| 173 | |
| 174 | #ifdef CONFIG_CPU |
| 175 | char processor_name[49]; |
| 176 | char vendor_name[49]; |
| 177 | struct udevice *dev = NULL; |
| 178 | |
| 179 | uclass_find_first_device(UCLASS_CPU, &dev); |
| 180 | if (dev) { |
| 181 | struct cpu_platdata *plat = dev_get_parent_platdata(dev); |
| 182 | |
| 183 | if (plat->family) |
| 184 | processor_family = plat->family; |
| 185 | t->processor_id[0] = plat->id[0]; |
| 186 | t->processor_id[1] = plat->id[1]; |
| 187 | |
| 188 | if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name))) |
| 189 | vendor = vendor_name; |
| 190 | if (!cpu_get_desc(dev, processor_name, sizeof(processor_name))) |
| 191 | name = processor_name; |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | t->processor_family = processor_family; |
| 196 | t->processor_manufacturer = smbios_add_string(t->eos, vendor); |
| 197 | t->processor_version = smbios_add_string(t->eos, name); |
| 198 | } |
| 199 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 200 | static int smbios_write_type4(ulong *current, int handle) |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 201 | { |
| 202 | struct smbios_type4 *t = (struct smbios_type4 *)*current; |
| 203 | int len = sizeof(struct smbios_type4); |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 204 | |
| 205 | memset(t, 0, sizeof(struct smbios_type4)); |
| 206 | fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle); |
| 207 | t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL; |
Alexander Graf | e44b3c0 | 2016-08-19 01:23:28 +0200 | [diff] [blame] | 208 | smbios_write_type4_dm(t); |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 209 | t->status = SMBIOS_PROCESSOR_STATUS_ENABLED; |
| 210 | t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE; |
| 211 | t->l1_cache_handle = 0xffff; |
| 212 | t->l2_cache_handle = 0xffff; |
| 213 | t->l3_cache_handle = 0xffff; |
| 214 | t->processor_family2 = t->processor_family; |
| 215 | |
| 216 | len = t->length + smbios_string_table_len(t->eos); |
| 217 | *current += len; |
| 218 | |
| 219 | return len; |
| 220 | } |
| 221 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 222 | static int smbios_write_type32(ulong *current, int handle) |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 223 | { |
| 224 | struct smbios_type32 *t = (struct smbios_type32 *)*current; |
| 225 | int len = sizeof(struct smbios_type32); |
| 226 | |
| 227 | memset(t, 0, sizeof(struct smbios_type32)); |
| 228 | fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle); |
| 229 | |
| 230 | *current += len; |
| 231 | |
| 232 | return len; |
| 233 | } |
| 234 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 235 | static int smbios_write_type127(ulong *current, int handle) |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 236 | { |
| 237 | struct smbios_type127 *t = (struct smbios_type127 *)*current; |
| 238 | int len = sizeof(struct smbios_type127); |
| 239 | |
| 240 | memset(t, 0, sizeof(struct smbios_type127)); |
| 241 | fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle); |
| 242 | |
| 243 | *current += len; |
| 244 | |
| 245 | return len; |
| 246 | } |
| 247 | |
| 248 | static smbios_write_type smbios_write_funcs[] = { |
| 249 | smbios_write_type0, |
| 250 | smbios_write_type1, |
| 251 | smbios_write_type2, |
| 252 | smbios_write_type3, |
| 253 | smbios_write_type4, |
| 254 | smbios_write_type32, |
| 255 | smbios_write_type127 |
| 256 | }; |
| 257 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 258 | ulong write_smbios_table(ulong addr) |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 259 | { |
| 260 | struct smbios_entry *se; |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 261 | ulong tables; |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 262 | int len = 0; |
| 263 | int max_struct_size = 0; |
| 264 | int handle = 0; |
| 265 | char *istart; |
| 266 | int isize; |
| 267 | int i; |
| 268 | |
| 269 | /* 16 byte align the table address */ |
| 270 | addr = ALIGN(addr, 16); |
| 271 | |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 272 | se = (struct smbios_entry *)(uintptr_t)addr; |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 273 | memset(se, 0, sizeof(struct smbios_entry)); |
| 274 | |
| 275 | addr += sizeof(struct smbios_entry); |
| 276 | addr = ALIGN(addr, 16); |
| 277 | tables = addr; |
| 278 | |
| 279 | /* populate minimum required tables */ |
| 280 | for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) { |
Simon Glass | ca37a39 | 2017-01-16 07:03:35 -0700 | [diff] [blame] | 281 | int tmp = smbios_write_funcs[i]((ulong *)&addr, handle++); |
Bin Meng | ae5bedb | 2015-10-12 05:23:41 -0700 | [diff] [blame] | 282 | max_struct_size = max(max_struct_size, tmp); |
| 283 | len += tmp; |
| 284 | } |
| 285 | |
| 286 | memcpy(se->anchor, "_SM_", 4); |
| 287 | se->length = sizeof(struct smbios_entry); |
| 288 | se->major_ver = SMBIOS_MAJOR_VER; |
| 289 | se->minor_ver = SMBIOS_MINOR_VER; |
| 290 | se->max_struct_size = max_struct_size; |
| 291 | memcpy(se->intermediate_anchor, "_DMI_", 5); |
| 292 | se->struct_table_length = len; |
| 293 | se->struct_table_address = tables; |
| 294 | se->struct_count = handle; |
| 295 | |
| 296 | /* calculate checksums */ |
| 297 | istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET; |
| 298 | isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET; |
| 299 | se->intermediate_checksum = table_compute_checksum(istart, isize); |
| 300 | se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry)); |
| 301 | |
| 302 | return addr; |
| 303 | } |