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
15DECLARE_GLOBAL_DATA_PTR;
16
17static 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
29/**
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;
40	static const char fallback[] = "Not Specified";
41
42	if (!index)
43		return fallback;
44
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
60	str = smbios_get_string(table, -1);
61	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
87const 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
95static 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) {
103		printf("\tUUID: %pUl\n", table->uuid);
104		printf("\tWake-up Type: %s\n",
105		       smbios_wakeup_type_str(table->wakeup_type));
106	}
107	if (table->length >= 0x1b) {
108		smbios_print_str("SKU Number", table, table->sku_number);
109		smbios_print_str("Family", table, table->family);
110	}
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);
123	printf("\tFeature Flags: 0x%04x\n", table->feature_flags);
124	smbios_print_str("Chassis Location", table, table->chassis_location);
125	printf("\tChassis Handle: 0x%04x\n", table->chassis_handle);
126	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;
150	u32 table_maximum_size;
151
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;
166		table_maximum_size = entry3->table_maximum_size;
167	} 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;
174		table_maximum_size = entry2->struct_table_length;
175	} 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;
187	printf("%zd structures occupying %d bytes\n", count, table_maximum_size);
188	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);
217