dm: treewide: Rename ..._platdata variables to just ..._plat
[platform/kernel/u-boot.git] / lib / smbios.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4  *
5  * Adapted from coreboot src/arch/x86/smbios.c
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <env.h>
11 #include <mapmem.h>
12 #include <smbios.h>
13 #include <tables_csum.h>
14 #include <version.h>
15 #ifdef CONFIG_CPU
16 #include <cpu.h>
17 #include <dm/uclass-internal.h>
18 #endif
19
20 /**
21  * struct smbios_write_method - Information about a table-writing function
22  *
23  * @write: Function to call
24  * @subnode_name: Name of subnode which has the information for this function,
25  *      NULL if none
26  */
27 struct smbios_write_method {
28         smbios_write_type write;
29         const char *subnode_name;
30 };
31
32 /**
33  * smbios_add_string() - add a string to the string area
34  *
35  * This adds a string to the string area which is appended directly after
36  * the formatted portion of an SMBIOS structure.
37  *
38  * @start:      string area start address
39  * @str:        string to add
40  * @return:     string number in the string area (1 or more)
41  */
42 static int smbios_add_string(char *start, const char *str)
43 {
44         int i = 1;
45         char *p = start;
46         if (!*str)
47                 str = "Unknown";
48
49         for (;;) {
50                 if (!*p) {
51                         strcpy(p, str);
52                         p += strlen(str);
53                         *p++ = '\0';
54                         *p++ = '\0';
55
56                         return i;
57                 }
58
59                 if (!strcmp(p, str))
60                         return i;
61
62                 p += strlen(p) + 1;
63                 i++;
64         }
65 }
66
67 /**
68  * smbios_add_prop() - Add a property from the device tree
69  *
70  * @start:      string area start address
71  * @node:       node containing the information to write (ofnode_null() if none)
72  * @prop:       property to write
73  * @return 0 if not found, else SMBIOS string number (1 or more)
74  */
75 static int smbios_add_prop(char *start, ofnode node, const char *prop)
76 {
77
78         if (IS_ENABLED(CONFIG_OF_CONTROL)) {
79                 const char *str;
80
81                 str = ofnode_read_string(node, prop);
82                 if (str)
83                         return smbios_add_string(start, str);
84         }
85
86         return 0;
87 }
88
89 /**
90  * smbios_string_table_len() - compute the string area size
91  *
92  * This computes the size of the string area including the string terminator.
93  *
94  * @start:      string area start address
95  * @return:     string area size
96  */
97 static int smbios_string_table_len(char *start)
98 {
99         char *p = start;
100         int i, len = 0;
101
102         while (*p) {
103                 i = strlen(p) + 1;
104                 p += i;
105                 len += i;
106         }
107
108         return len + 1;
109 }
110
111 static int smbios_write_type0(ulong *current, int handle, ofnode node)
112 {
113         struct smbios_type0 *t;
114         int len = sizeof(struct smbios_type0);
115
116         t = map_sysmem(*current, len);
117         memset(t, 0, sizeof(struct smbios_type0));
118         fill_smbios_header(t, SMBIOS_BIOS_INFORMATION, len, handle);
119         t->vendor = smbios_add_string(t->eos, "U-Boot");
120         t->bios_ver = smbios_add_string(t->eos, PLAIN_VERSION);
121         t->bios_release_date = smbios_add_string(t->eos, U_BOOT_DMI_DATE);
122 #ifdef CONFIG_ROM_SIZE
123         t->bios_rom_size = (CONFIG_ROM_SIZE / 65536) - 1;
124 #endif
125         t->bios_characteristics = BIOS_CHARACTERISTICS_PCI_SUPPORTED |
126                                   BIOS_CHARACTERISTICS_SELECTABLE_BOOT |
127                                   BIOS_CHARACTERISTICS_UPGRADEABLE;
128 #ifdef CONFIG_GENERATE_ACPI_TABLE
129         t->bios_characteristics_ext1 = BIOS_CHARACTERISTICS_EXT1_ACPI;
130 #endif
131 #ifdef CONFIG_EFI_LOADER
132         t->bios_characteristics_ext1 |= BIOS_CHARACTERISTICS_EXT1_UEFI;
133 #endif
134         t->bios_characteristics_ext2 = BIOS_CHARACTERISTICS_EXT2_TARGET;
135
136         t->bios_major_release = 0xff;
137         t->bios_minor_release = 0xff;
138         t->ec_major_release = 0xff;
139         t->ec_minor_release = 0xff;
140
141         len = t->length + smbios_string_table_len(t->eos);
142         *current += len;
143         unmap_sysmem(t);
144
145         return len;
146 }
147
148 static int smbios_write_type1(ulong *current, int handle, ofnode node)
149 {
150         struct smbios_type1 *t;
151         int len = sizeof(struct smbios_type1);
152         char *serial_str = env_get("serial#");
153
154         t = map_sysmem(*current, len);
155         memset(t, 0, sizeof(struct smbios_type1));
156         fill_smbios_header(t, SMBIOS_SYSTEM_INFORMATION, len, handle);
157         t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
158         t->product_name = smbios_add_prop(t->eos, node, "product");
159         t->version = smbios_add_prop(t->eos, node, "version");
160         if (serial_str) {
161                 t->serial_number = smbios_add_string(t->eos, serial_str);
162                 strncpy((char *)t->uuid, serial_str, sizeof(t->uuid));
163         } else {
164                 t->serial_number = smbios_add_prop(t->eos, node, "serial");
165         }
166         t->sku_number = smbios_add_prop(t->eos, node, "sku");
167         t->family = smbios_add_prop(t->eos, node, "family");
168
169         len = t->length + smbios_string_table_len(t->eos);
170         *current += len;
171         unmap_sysmem(t);
172
173         return len;
174 }
175
176 static int smbios_write_type2(ulong *current, int handle, ofnode node)
177 {
178         struct smbios_type2 *t;
179         int len = sizeof(struct smbios_type2);
180
181         t = map_sysmem(*current, len);
182         memset(t, 0, sizeof(struct smbios_type2));
183         fill_smbios_header(t, SMBIOS_BOARD_INFORMATION, len, handle);
184         t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
185         t->product_name = smbios_add_prop(t->eos, node, "product");
186         t->asset_tag_number = smbios_add_prop(t->eos, node, "asset-tag");
187         t->feature_flags = SMBIOS_BOARD_FEATURE_HOSTING;
188         t->board_type = SMBIOS_BOARD_MOTHERBOARD;
189
190         len = t->length + smbios_string_table_len(t->eos);
191         *current += len;
192         unmap_sysmem(t);
193
194         return len;
195 }
196
197 static int smbios_write_type3(ulong *current, int handle, ofnode node)
198 {
199         struct smbios_type3 *t;
200         int len = sizeof(struct smbios_type3);
201
202         t = map_sysmem(*current, len);
203         memset(t, 0, sizeof(struct smbios_type3));
204         fill_smbios_header(t, SMBIOS_SYSTEM_ENCLOSURE, len, handle);
205         t->manufacturer = smbios_add_prop(t->eos, node, "manufacturer");
206         t->chassis_type = SMBIOS_ENCLOSURE_DESKTOP;
207         t->bootup_state = SMBIOS_STATE_SAFE;
208         t->power_supply_state = SMBIOS_STATE_SAFE;
209         t->thermal_state = SMBIOS_STATE_SAFE;
210         t->security_status = SMBIOS_SECURITY_NONE;
211
212         len = t->length + smbios_string_table_len(t->eos);
213         *current += len;
214         unmap_sysmem(t);
215
216         return len;
217 }
218
219 static void smbios_write_type4_dm(struct smbios_type4 *t, ofnode node)
220 {
221         u16 processor_family = SMBIOS_PROCESSOR_FAMILY_UNKNOWN;
222         const char *vendor = "Unknown";
223         const char *name = "Unknown";
224
225 #ifdef CONFIG_CPU
226         char processor_name[49];
227         char vendor_name[49];
228         struct udevice *cpu = NULL;
229
230         uclass_find_first_device(UCLASS_CPU, &cpu);
231         if (cpu) {
232                 struct cpu_plat *plat = dev_get_parent_plat(cpu);
233
234                 if (plat->family)
235                         processor_family = plat->family;
236                 t->processor_id[0] = plat->id[0];
237                 t->processor_id[1] = plat->id[1];
238
239                 if (!cpu_get_vendor(cpu, vendor_name, sizeof(vendor_name)))
240                         vendor = vendor_name;
241                 if (!cpu_get_desc(cpu, processor_name, sizeof(processor_name)))
242                         name = processor_name;
243         }
244 #endif
245
246         t->processor_family = processor_family;
247         t->processor_manufacturer = smbios_add_string(t->eos, vendor);
248         t->processor_version = smbios_add_string(t->eos, name);
249 }
250
251 static int smbios_write_type4(ulong *current, int handle, ofnode node)
252 {
253         struct smbios_type4 *t;
254         int len = sizeof(struct smbios_type4);
255
256         t = map_sysmem(*current, len);
257         memset(t, 0, sizeof(struct smbios_type4));
258         fill_smbios_header(t, SMBIOS_PROCESSOR_INFORMATION, len, handle);
259         t->processor_type = SMBIOS_PROCESSOR_TYPE_CENTRAL;
260         smbios_write_type4_dm(t, node);
261         t->status = SMBIOS_PROCESSOR_STATUS_ENABLED;
262         t->processor_upgrade = SMBIOS_PROCESSOR_UPGRADE_NONE;
263         t->l1_cache_handle = 0xffff;
264         t->l2_cache_handle = 0xffff;
265         t->l3_cache_handle = 0xffff;
266         t->processor_family2 = t->processor_family;
267
268         len = t->length + smbios_string_table_len(t->eos);
269         *current += len;
270         unmap_sysmem(t);
271
272         return len;
273 }
274
275 static int smbios_write_type32(ulong *current, int handle, ofnode node)
276 {
277         struct smbios_type32 *t;
278         int len = sizeof(struct smbios_type32);
279
280         t = map_sysmem(*current, len);
281         memset(t, 0, sizeof(struct smbios_type32));
282         fill_smbios_header(t, SMBIOS_SYSTEM_BOOT_INFORMATION, len, handle);
283
284         *current += len;
285         unmap_sysmem(t);
286
287         return len;
288 }
289
290 static int smbios_write_type127(ulong *current, int handle, ofnode node)
291 {
292         struct smbios_type127 *t;
293         int len = sizeof(struct smbios_type127);
294
295         t = map_sysmem(*current, len);
296         memset(t, 0, sizeof(struct smbios_type127));
297         fill_smbios_header(t, SMBIOS_END_OF_TABLE, len, handle);
298
299         *current += len;
300         unmap_sysmem(t);
301
302         return len;
303 }
304
305 static struct smbios_write_method smbios_write_funcs[] = {
306         { smbios_write_type0, },
307         { smbios_write_type1, "system", },
308         { smbios_write_type2, "baseboard", },
309         { smbios_write_type3, "chassis", },
310         { smbios_write_type4, },
311         { smbios_write_type32, },
312         { smbios_write_type127 },
313 };
314
315 ulong write_smbios_table(ulong addr)
316 {
317         ofnode parent_node = ofnode_null();
318         struct smbios_entry *se;
319         struct udevice *dev;
320         ulong table_addr;
321         ulong tables;
322         int len = 0;
323         int max_struct_size = 0;
324         int handle = 0;
325         char *istart;
326         int isize;
327         int i;
328
329         if (IS_ENABLED(CONFIG_OF_CONTROL)) {
330                 uclass_first_device(UCLASS_SYSINFO, &dev);
331                 if (dev)
332                         parent_node = dev_read_subnode(dev, "smbios");
333         }
334
335         /* 16 byte align the table address */
336         addr = ALIGN(addr, 16);
337
338         se = map_sysmem(addr, sizeof(struct smbios_entry));
339         memset(se, 0, sizeof(struct smbios_entry));
340
341         addr += sizeof(struct smbios_entry);
342         addr = ALIGN(addr, 16);
343         tables = addr;
344
345         /* populate minimum required tables */
346         for (i = 0; i < ARRAY_SIZE(smbios_write_funcs); i++) {
347                 const struct smbios_write_method *method;
348                 ofnode node = ofnode_null();
349                 int tmp;
350
351                 method = &smbios_write_funcs[i];
352                 if (IS_ENABLED(CONFIG_OF_CONTROL) && method->subnode_name)
353                         node = ofnode_find_subnode(parent_node,
354                                                    method->subnode_name);
355                 tmp = method->write((ulong *)&addr, handle++, node);
356
357                 max_struct_size = max(max_struct_size, tmp);
358                 len += tmp;
359         }
360
361         memcpy(se->anchor, "_SM_", 4);
362         se->length = sizeof(struct smbios_entry);
363         se->major_ver = SMBIOS_MAJOR_VER;
364         se->minor_ver = SMBIOS_MINOR_VER;
365         se->max_struct_size = max_struct_size;
366         memcpy(se->intermediate_anchor, "_DMI_", 5);
367         se->struct_table_length = len;
368
369         /*
370          * We must use a pointer here so things work correctly on sandbox. The
371          * user of this table is not aware of the mapping of addresses to
372          * sandbox's DRAM buffer.
373          */
374         table_addr = (ulong)map_sysmem(tables, 0);
375         if (sizeof(table_addr) > sizeof(u32) && table_addr > (ulong)UINT_MAX) {
376                 /*
377                  * We need to put this >32-bit pointer into the table but the
378                  * field is only 32 bits wide.
379                  */
380                 printf("WARNING: SMBIOS table_address overflow %llx\n",
381                        (unsigned long long)table_addr);
382                 table_addr = 0;
383         }
384         se->struct_table_address = table_addr;
385
386         se->struct_count = handle;
387
388         /* calculate checksums */
389         istart = (char *)se + SMBIOS_INTERMEDIATE_OFFSET;
390         isize = sizeof(struct smbios_entry) - SMBIOS_INTERMEDIATE_OFFSET;
391         se->intermediate_checksum = table_compute_checksum(istart, isize);
392         se->checksum = table_compute_checksum(se, sizeof(struct smbios_entry));
393         unmap_sysmem(se);
394
395         return addr;
396 }