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