Heinrich Schuchardt | 9de4ec8 | 2024-01-25 16:54:34 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * The 'smbios' command displays information from the SMBIOS table. |
| 4 | * |
| 5 | * Copyright (c) 2023, Heinrich Schuchardt <heinrich.schuchardt@canonical.com> |
| 6 | */ |
| 7 | |
| 8 | #include <command.h> |
| 9 | #include <hexdump.h> |
| 10 | #include <mapmem.h> |
| 11 | #include <smbios.h> |
| 12 | #include <tables_csum.h> |
| 13 | #include <asm/global_data.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | /** |
| 18 | * smbios_get_string() - get SMBIOS string from table |
| 19 | * |
| 20 | * @table: SMBIOS table |
| 21 | * @index: index of the string |
| 22 | * Return: address of string, may point to empty string |
| 23 | */ |
| 24 | static const char *smbios_get_string(void *table, int index) |
| 25 | { |
| 26 | const char *str = (char *)table + |
| 27 | ((struct smbios_header *)table)->length; |
| 28 | |
| 29 | if (!*str) |
| 30 | ++str; |
| 31 | for (--index; *str && index; --index) |
| 32 | str += strlen(str) + 1; |
| 33 | |
| 34 | return str; |
| 35 | } |
| 36 | |
| 37 | static struct smbios_header *next_table(struct smbios_header *table) |
| 38 | { |
| 39 | const char *str; |
| 40 | |
| 41 | if (table->type == SMBIOS_END_OF_TABLE) |
| 42 | return NULL; |
| 43 | |
| 44 | str = smbios_get_string(table, 0); |
| 45 | return (struct smbios_header *)(++str); |
| 46 | } |
| 47 | |
| 48 | static void smbios_print_generic(struct smbios_header *table) |
| 49 | { |
| 50 | char *str = (char *)table + table->length; |
| 51 | |
| 52 | if (CONFIG_IS_ENABLED(HEXDUMP)) { |
| 53 | printf("Header and Data:\n"); |
| 54 | print_hex_dump("\t", DUMP_PREFIX_OFFSET, 16, 1, |
| 55 | table, table->length, false); |
| 56 | } |
| 57 | if (*str) { |
| 58 | printf("Strings:\n"); |
| 59 | for (int index = 1; *str; ++index) { |
| 60 | printf("\tString %u: %s\n", index, str); |
| 61 | str += strlen(str) + 1; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void smbios_print_str(const char *label, void *table, u8 index) |
| 67 | { |
| 68 | printf("\t%s: %s\n", label, smbios_get_string(table, index)); |
| 69 | } |
| 70 | |
| 71 | static void smbios_print_type1(struct smbios_type1 *table) |
| 72 | { |
| 73 | printf("System Information\n"); |
| 74 | smbios_print_str("Manufacturer", table, table->manufacturer); |
| 75 | smbios_print_str("Product Name", table, table->product_name); |
| 76 | smbios_print_str("Version", table, table->version); |
| 77 | smbios_print_str("Serial Number", table, table->serial_number); |
| 78 | if (table->length >= 0x19) { |
Heinrich Schuchardt | e799f8a | 2024-01-29 18:51:24 +0100 | [diff] [blame^] | 79 | printf("\tUUID: %pUl\n", table->uuid); |
Heinrich Schuchardt | 9de4ec8 | 2024-01-25 16:54:34 +0100 | [diff] [blame] | 80 | smbios_print_str("Wake Up Type", table, table->serial_number); |
| 81 | } |
| 82 | if (table->length >= 0x1b) { |
| 83 | smbios_print_str("Serial Number", table, table->serial_number); |
| 84 | smbios_print_str("SKU Number", table, table->sku_number); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | static void smbios_print_type2(struct smbios_type2 *table) |
| 89 | { |
| 90 | u16 *handle; |
| 91 | |
| 92 | printf("Base Board Information\n"); |
| 93 | smbios_print_str("Manufacturer", table, table->manufacturer); |
| 94 | smbios_print_str("Product Name", table, table->product_name); |
| 95 | smbios_print_str("Version", table, table->version); |
| 96 | smbios_print_str("Serial Number", table, table->serial_number); |
| 97 | smbios_print_str("Asset Tag", table, table->asset_tag_number); |
Heinrich Schuchardt | 7ca4b0e | 2024-01-29 18:09:50 +0100 | [diff] [blame] | 98 | printf("\tFeature Flags: 0x%04x\n", table->feature_flags); |
Heinrich Schuchardt | 9de4ec8 | 2024-01-25 16:54:34 +0100 | [diff] [blame] | 99 | smbios_print_str("Chassis Location", table, table->chassis_location); |
Heinrich Schuchardt | 7ca4b0e | 2024-01-29 18:09:50 +0100 | [diff] [blame] | 100 | printf("\tChassis Handle: 0x%04x\n", table->chassis_handle); |
Heinrich Schuchardt | 9de4ec8 | 2024-01-25 16:54:34 +0100 | [diff] [blame] | 101 | smbios_print_str("Board Type", table, table->board_type); |
| 102 | printf("\tContained Object Handles: "); |
| 103 | handle = (void *)table->eos; |
| 104 | for (int i = 0; i < table->number_contained_objects; ++i) |
| 105 | printf("0x%04x ", handle[i]); |
| 106 | printf("\n"); |
| 107 | } |
| 108 | |
| 109 | static void smbios_print_type127(struct smbios_type127 *table) |
| 110 | { |
| 111 | printf("End Of Table\n"); |
| 112 | } |
| 113 | |
| 114 | static int do_smbios(struct cmd_tbl *cmdtp, int flag, int argc, |
| 115 | char *const argv[]) |
| 116 | { |
| 117 | ulong addr; |
| 118 | void *entry; |
| 119 | u32 size; |
| 120 | char version[12]; |
| 121 | struct smbios_header *table; |
| 122 | static const char smbios_sig[] = "_SM_"; |
| 123 | static const char smbios3_sig[] = "_SM3_"; |
| 124 | size_t count = 0; |
| 125 | u32 max_struct_size; |
| 126 | |
| 127 | addr = gd_smbios_start(); |
| 128 | if (!addr) { |
| 129 | log_warning("SMBIOS not available\n"); |
| 130 | return CMD_RET_FAILURE; |
| 131 | } |
| 132 | entry = map_sysmem(addr, 0); |
| 133 | if (!memcmp(entry, smbios3_sig, sizeof(smbios3_sig) - 1)) { |
| 134 | struct smbios3_entry *entry3 = entry; |
| 135 | |
| 136 | table = (void *)(uintptr_t)entry3->struct_table_address; |
| 137 | snprintf(version, sizeof(version), "%d.%d.%d", |
| 138 | entry3->major_ver, entry3->minor_ver, entry3->doc_rev); |
| 139 | table = (void *)(uintptr_t)entry3->struct_table_address; |
| 140 | size = entry3->length; |
| 141 | max_struct_size = entry3->max_struct_size; |
| 142 | } else if (!memcmp(entry, smbios_sig, sizeof(smbios_sig) - 1)) { |
| 143 | struct smbios_entry *entry2 = entry; |
| 144 | |
| 145 | snprintf(version, sizeof(version), "%d.%d", |
| 146 | entry2->major_ver, entry2->minor_ver); |
| 147 | table = (void *)(uintptr_t)entry2->struct_table_address; |
| 148 | size = entry2->length; |
| 149 | max_struct_size = entry2->max_struct_size; |
| 150 | } else { |
| 151 | log_err("Unknown SMBIOS anchor format\n"); |
| 152 | return CMD_RET_FAILURE; |
| 153 | } |
| 154 | if (table_compute_checksum(entry, size)) { |
| 155 | log_err("Invalid anchor checksum\n"); |
| 156 | return CMD_RET_FAILURE; |
| 157 | } |
| 158 | printf("SMBIOS %s present.\n", version); |
| 159 | |
| 160 | for (struct smbios_header *pos = table; pos; pos = next_table(pos)) |
| 161 | ++count; |
| 162 | printf("%zd structures occupying %d bytes\n", count, max_struct_size); |
| 163 | printf("Table at 0x%llx\n", (unsigned long long)map_to_sysmem(table)); |
| 164 | |
| 165 | for (struct smbios_header *pos = table; pos; pos = next_table(pos)) { |
| 166 | printf("\nHandle 0x%04x, DMI type %d, %d bytes at 0x%llx\n", |
| 167 | pos->handle, pos->type, pos->length, |
| 168 | (unsigned long long)map_to_sysmem(pos)); |
| 169 | switch (pos->type) { |
| 170 | case 1: |
| 171 | smbios_print_type1((struct smbios_type1 *)pos); |
| 172 | break; |
| 173 | case 2: |
| 174 | smbios_print_type2((struct smbios_type2 *)pos); |
| 175 | break; |
| 176 | case 127: |
| 177 | smbios_print_type127((struct smbios_type127 *)pos); |
| 178 | break; |
| 179 | default: |
| 180 | smbios_print_generic(pos); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return CMD_RET_SUCCESS; |
| 186 | } |
| 187 | |
| 188 | U_BOOT_LONGHELP(smbios, "- display SMBIOS information"); |
| 189 | |
| 190 | U_BOOT_CMD(smbios, 1, 0, do_smbios, "display SMBIOS information", |
| 191 | smbios_help_text); |