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