blob: d3bd8b12a675be9a1888ac11bcaaf93c24de6386 [file] [log] [blame]
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +01001// 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
15DECLARE_GLOBAL_DATA_PTR;
16
Heinrich Schuchardt23109d02024-02-10 12:06:47 +010017static const char * const wakeup_type_strings[] = {
18 "Reserved", /* 0x00 */
19 "Other", /* 0x01 */
20 "Unknown", /* 0x02 */
21 "APM Timer", /* 0x03 */
22 "Modem Ring", /* 0x04 */
23 "Lan Remote", /* 0x05 */
24 "Power Switch", /* 0x06 */
25 "PCI PME#", /* 0x07 */
26 "AC Power Restored", /* 0x08 */
27};
28
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +010029/**
30 * smbios_get_string() - get SMBIOS string from table
31 *
32 * @table: SMBIOS table
33 * @index: index of the string
34 * Return: address of string, may point to empty string
35 */
36static const char *smbios_get_string(void *table, int index)
37{
38 const char *str = (char *)table +
39 ((struct smbios_header *)table)->length;
Heinrich Schuchardtc11f1762024-01-29 18:01:27 +010040 static const char fallback[] = "Not Specified";
41
42 if (!index)
43 return fallback;
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +010044
45 if (!*str)
46 ++str;
47 for (--index; *str && index; --index)
48 str += strlen(str) + 1;
49
50 return str;
51}
52
53static struct smbios_header *next_table(struct smbios_header *table)
54{
55 const char *str;
56
57 if (table->type == SMBIOS_END_OF_TABLE)
58 return NULL;
59
Heinrich Schuchardtc11f1762024-01-29 18:01:27 +010060 str = smbios_get_string(table, -1);
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +010061 return (struct smbios_header *)(++str);
62}
63
64static void smbios_print_generic(struct smbios_header *table)
65{
66 char *str = (char *)table + table->length;
67
68 if (CONFIG_IS_ENABLED(HEXDUMP)) {
69 printf("Header and Data:\n");
70 print_hex_dump("\t", DUMP_PREFIX_OFFSET, 16, 1,
71 table, table->length, false);
72 }
73 if (*str) {
74 printf("Strings:\n");
75 for (int index = 1; *str; ++index) {
76 printf("\tString %u: %s\n", index, str);
77 str += strlen(str) + 1;
78 }
79 }
80}
81
82void smbios_print_str(const char *label, void *table, u8 index)
83{
84 printf("\t%s: %s\n", label, smbios_get_string(table, index));
85}
86
Heinrich Schuchardt23109d02024-02-10 12:06:47 +010087const char *smbios_wakeup_type_str(u8 wakeup_type)
88{
89 if (wakeup_type >= ARRAY_SIZE(wakeup_type_strings))
90 /* Values over 0x08 are reserved. */
91 wakeup_type = 0;
92 return wakeup_type_strings[wakeup_type];
93}
94
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +010095static void smbios_print_type1(struct smbios_type1 *table)
96{
97 printf("System Information\n");
98 smbios_print_str("Manufacturer", table, table->manufacturer);
99 smbios_print_str("Product Name", table, table->product_name);
100 smbios_print_str("Version", table, table->version);
101 smbios_print_str("Serial Number", table, table->serial_number);
102 if (table->length >= 0x19) {
Heinrich Schuchardte799f8a2024-01-29 18:51:24 +0100103 printf("\tUUID: %pUl\n", table->uuid);
Heinrich Schuchardt23109d02024-02-10 12:06:47 +0100104 printf("\tWake-up Type: %s\n",
105 smbios_wakeup_type_str(table->wakeup_type));
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100106 }
107 if (table->length >= 0x1b) {
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100108 smbios_print_str("SKU Number", table, table->sku_number);
Heinrich Schuchardt23109d02024-02-10 12:06:47 +0100109 smbios_print_str("Family", table, table->family);
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100110 }
111}
112
113static void smbios_print_type2(struct smbios_type2 *table)
114{
115 u16 *handle;
116
117 printf("Base Board Information\n");
118 smbios_print_str("Manufacturer", table, table->manufacturer);
119 smbios_print_str("Product Name", table, table->product_name);
120 smbios_print_str("Version", table, table->version);
121 smbios_print_str("Serial Number", table, table->serial_number);
122 smbios_print_str("Asset Tag", table, table->asset_tag_number);
Heinrich Schuchardt7ca4b0e2024-01-29 18:09:50 +0100123 printf("\tFeature Flags: 0x%04x\n", table->feature_flags);
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100124 smbios_print_str("Chassis Location", table, table->chassis_location);
Heinrich Schuchardt7ca4b0e2024-01-29 18:09:50 +0100125 printf("\tChassis Handle: 0x%04x\n", table->chassis_handle);
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100126 smbios_print_str("Board Type", table, table->board_type);
127 printf("\tContained Object Handles: ");
128 handle = (void *)table->eos;
129 for (int i = 0; i < table->number_contained_objects; ++i)
130 printf("0x%04x ", handle[i]);
131 printf("\n");
132}
133
134static void smbios_print_type127(struct smbios_type127 *table)
135{
136 printf("End Of Table\n");
137}
138
139static int do_smbios(struct cmd_tbl *cmdtp, int flag, int argc,
140 char *const argv[])
141{
142 ulong addr;
143 void *entry;
144 u32 size;
145 char version[12];
146 struct smbios_header *table;
147 static const char smbios_sig[] = "_SM_";
148 static const char smbios3_sig[] = "_SM3_";
149 size_t count = 0;
Heinrich Schuchardt551bc962024-01-31 23:34:46 +0100150 u32 table_maximum_size;
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100151
152 addr = gd_smbios_start();
153 if (!addr) {
154 log_warning("SMBIOS not available\n");
155 return CMD_RET_FAILURE;
156 }
157 entry = map_sysmem(addr, 0);
158 if (!memcmp(entry, smbios3_sig, sizeof(smbios3_sig) - 1)) {
159 struct smbios3_entry *entry3 = entry;
160
161 table = (void *)(uintptr_t)entry3->struct_table_address;
162 snprintf(version, sizeof(version), "%d.%d.%d",
163 entry3->major_ver, entry3->minor_ver, entry3->doc_rev);
164 table = (void *)(uintptr_t)entry3->struct_table_address;
165 size = entry3->length;
Heinrich Schuchardt406c410e2024-01-31 23:49:34 +0100166 table_maximum_size = entry3->table_maximum_size;
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100167 } else if (!memcmp(entry, smbios_sig, sizeof(smbios_sig) - 1)) {
168 struct smbios_entry *entry2 = entry;
169
170 snprintf(version, sizeof(version), "%d.%d",
171 entry2->major_ver, entry2->minor_ver);
172 table = (void *)(uintptr_t)entry2->struct_table_address;
173 size = entry2->length;
Heinrich Schuchardt551bc962024-01-31 23:34:46 +0100174 table_maximum_size = entry2->struct_table_length;
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100175 } else {
176 log_err("Unknown SMBIOS anchor format\n");
177 return CMD_RET_FAILURE;
178 }
179 if (table_compute_checksum(entry, size)) {
180 log_err("Invalid anchor checksum\n");
181 return CMD_RET_FAILURE;
182 }
183 printf("SMBIOS %s present.\n", version);
184
185 for (struct smbios_header *pos = table; pos; pos = next_table(pos))
186 ++count;
Heinrich Schuchardt551bc962024-01-31 23:34:46 +0100187 printf("%zd structures occupying %d bytes\n", count, table_maximum_size);
Heinrich Schuchardt9de4ec82024-01-25 16:54:34 +0100188 printf("Table at 0x%llx\n", (unsigned long long)map_to_sysmem(table));
189
190 for (struct smbios_header *pos = table; pos; pos = next_table(pos)) {
191 printf("\nHandle 0x%04x, DMI type %d, %d bytes at 0x%llx\n",
192 pos->handle, pos->type, pos->length,
193 (unsigned long long)map_to_sysmem(pos));
194 switch (pos->type) {
195 case 1:
196 smbios_print_type1((struct smbios_type1 *)pos);
197 break;
198 case 2:
199 smbios_print_type2((struct smbios_type2 *)pos);
200 break;
201 case 127:
202 smbios_print_type127((struct smbios_type127 *)pos);
203 break;
204 default:
205 smbios_print_generic(pos);
206 break;
207 }
208 }
209
210 return CMD_RET_SUCCESS;
211}
212
213U_BOOT_LONGHELP(smbios, "- display SMBIOS information");
214
215U_BOOT_CMD(smbios, 1, 0, do_smbios, "display SMBIOS information",
216 smbios_help_text);