xref: /rk3399_rockchip-uboot/lib/smbios.c (revision 96476206c54ee9c94e5994d96f52ec9d2063967a)
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(uintptr_t *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 	t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
87 	t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
88 				  BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
89 				  BIOS_CHARACTERISTICS_UPGRADEABLE;
90 #ifdef CONFIG_GENERATE_ACPI_TABLE
91 	t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
92 #endif
93 	t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
94 	t->bios_major_release = 0xff;
95 	t->bios_minor_release = 0xff;
96 	t->ec_major_release = 0xff;
97 	t->ec_minor_release = 0xff;
98 
99 	len = t->length + smbios_string_table_len(t->eos);
100 	*current += len;
101 
102 	return len;
103 }
104 
105 static int smbios_write_type1(uintptr_t *current, int handle)
106 {
107 	struct smbios_type1 *t = (struct smbios_type1 *)*current;
108 	int len = sizeof(struct smbios_type1);
109 
110 	memset(t, 0, sizeof(struct smbios_type1));
111 	fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
112 	t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
113 	t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
114 
115 	len = t->length + smbios_string_table_len(t->eos);
116 	*current += len;
117 
118 	return len;
119 }
120 
121 static int smbios_write_type2(uintptr_t *current, int handle)
122 {
123 	struct smbios_type2 *t = (struct smbios_type2 *)*current;
124 	int len = sizeof(struct smbios_type2);
125 
126 	memset(t, 0, sizeof(struct smbios_type2));
127 	fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
128 	t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
129 	t->product_name = smbios_add_string(t->eos, CONFIG_SMBIOS_PRODUCT_NAME);
130 	t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
131 	t->board_type = SMBIOS_BOARD_MOTHERBOARD;
132 
133 	len = t->length + smbios_string_table_len(t->eos);
134 	*current += len;
135 
136 	return len;
137 }
138 
139 static int smbios_write_type3(uintptr_t *current, int handle)
140 {
141 	struct smbios_type3 *t = (struct smbios_type3 *)*current;
142 	int len = sizeof(struct smbios_type3);
143 
144 	memset(t, 0, sizeof(struct smbios_type3));
145 	fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
146 	t->manufacturer = smbios_add_string(t->eos, CONFIG_SMBIOS_MANUFACTURER);
147 	t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
148 	t->bootup_state = SMBIOS_STATE_SAFE;
149 	t->power_supply_state = SMBIOS_STATE_SAFE;
150 	t->thermal_state = SMBIOS_STATE_SAFE;
151 	t->security_status = SMBIOS_SECURITY_NONE;
152 
153 	len = t->length + smbios_string_table_len(t->eos);
154 	*current += len;
155 
156 	return len;
157 }
158 
159 static void smbios_write_type4_dm(struct smbios_type4 *t)
160 {
161 	u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
162 	const char *vendor = "Unknown";
163 	const char *name = "Unknown";
164 
165 #ifdef CONFIG_CPU
166 	char processor_name[49];
167 	char vendor_name[49];
168 	struct udevice *dev = NULL;
169 
170 	uclass_find_first_device(UCLASS_CPU, &dev);
171 	if (dev) {
172 		struct cpu_platdata *plat = dev_get_parent_platdata(dev);
173 
174 		if (plat->family)
175 			processor_family = plat->family;
176 		t->processor_id[0] = plat->id[0];
177 		t->processor_id[1] = plat->id[1];
178 
179 		if (!cpu_get_vendor(dev, vendor_name, sizeof(vendor_name)))
180 			vendor = vendor_name;
181 		if (!cpu_get_desc(dev, processor_name, sizeof(processor_name)))
182 			name = processor_name;
183 	}
184 #endif
185 
186 	t->processor_family = processor_family;
187 	t->processor_manufacturer = smbios_add_string(t->eos, vendor);
188 	t->processor_version = smbios_add_string(t->eos, name);
189 }
190 
191 static int smbios_write_type4(uintptr_t *current, int handle)
192 {
193 	struct smbios_type4 *t = (struct smbios_type4 *)*current;
194 	int len = sizeof(struct smbios_type4);
195 
196 	memset(t, 0, sizeof(struct smbios_type4));
197 	fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
198 	t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
199 	smbios_write_type4_dm(t);
200 	t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
201 	t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
202 	t->l1_cache_handle = 0xffff;
203 	t->l2_cache_handle = 0xffff;
204 	t->l3_cache_handle = 0xffff;
205 	t->processor_family2 = t->processor_family;
206 
207 	len = t->length + smbios_string_table_len(t->eos);
208 	*current += len;
209 
210 	return len;
211 }
212 
213 static int smbios_write_type32(uintptr_t *current, int handle)
214 {
215 	struct smbios_type32 *t = (struct smbios_type32 *)*current;
216 	int len = sizeof(struct smbios_type32);
217 
218 	memset(t, 0, sizeof(struct smbios_type32));
219 	fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
220 
221 	*current += len;
222 
223 	return len;
224 }
225 
226 static int smbios_write_type127(uintptr_t *current, int handle)
227 {
228 	struct smbios_type127 *t = (struct smbios_type127 *)*current;
229 	int len = sizeof(struct smbios_type127);
230 
231 	memset(t, 0, sizeof(struct smbios_type127));
232 	fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
233 
234 	*current += len;
235 
236 	return len;
237 }
238 
239 static smbios_write_type smbios_write_funcs[] = {
240 	smbios_write_type0,
241 	smbios_write_type1,
242 	smbios_write_type2,
243 	smbios_write_type3,
244 	smbios_write_type4,
245 	smbios_write_type32,
246 	smbios_write_type127
247 };
248 
249 uintptr_t write_smbios_table(uintptr_t addr)
250 {
251 	struct smbios_entry *se;
252 	u32 tables;
253 	int len = 0;
254 	int max_struct_size = 0;
255 	int handle = 0;
256 	char *istart;
257 	int isize;
258 	int i;
259 
260 	/* 16 byte align the table address */
261 	addr = ALIGN(addr, 16);
262 
263 	se = (struct smbios_entry *)addr;
264 	memset(se, 0, sizeof(struct smbios_entry));
265 
266 	addr += sizeof(struct smbios_entry);
267 	addr = ALIGN(addr, 16);
268 	tables = addr;
269 
270 	/* populate minimum required tables */
271 	for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
272 		int tmp = smbios_write_funcs[i](&addr, handle++);
273 		max_struct_size = max(max_struct_size, tmp);
274 		len += tmp;
275 	}
276 
277 	memcpy(se->anchor, "_SM_", 4);
278 	se->length = sizeof(struct smbios_entry);
279 	se->major_ver = SMBIOS_MAJOR_VER;
280 	se->minor_ver = SMBIOS_MINOR_VER;
281 	se->max_struct_size = max_struct_size;
282 	memcpy(se->intermediate_anchor, "_DMI_", 5);
283 	se->struct_table_length = len;
284 	se->struct_table_address = tables;
285 	se->struct_count = handle;
286 
287 	/* calculate checksums */
288 	istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
289 	isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
290 	se->intermediate_checksum = table_compute_checksum(istart, isize);
291 	se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
292 
293 	return addr;
294 }
295