1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
4  *
5  * Adapted from coreboot src/include/smbios.h
6  */
7 
8 #ifndef _SMBIOS_H_
9 #define _SMBIOS_H_
10 
11 #include <linux/types.h>
12 #include <smbios_def.h>
13 
14 /* SMBIOS spec version implemented */
15 #define SMBIOS_MAJOR_VER	3
16 #define SMBIOS_MINOR_VER	7
17 
18 enum {
19 	SMBIOS_STR_MAX	= 64,	/* Maximum length allowed for a string */
20 };
21 
22 /* SMBIOS structure types */
23 enum {
24 	SMBIOS_BIOS_INFORMATION = 0,
25 	SMBIOS_SYSTEM_INFORMATION = 1,
26 	SMBIOS_BOARD_INFORMATION = 2,
27 	SMBIOS_SYSTEM_ENCLOSURE = 3,
28 	SMBIOS_PROCESSOR_INFORMATION = 4,
29 	SMBIOS_CACHE_INFORMATION = 7,
30 	SMBIOS_SYSTEM_SLOTS = 9,
31 	SMBIOS_PHYS_MEMORY_ARRAY = 16,
32 	SMBIOS_MEMORY_DEVICE = 17,
33 	SMBIOS_MEMORY_ARRAY_MAPPED_ADDRESS = 19,
34 	SMBIOS_SYSTEM_BOOT_INFORMATION = 32,
35 	SMBIOS_END_OF_TABLE = 127
36 };
37 
38 #define SMBIOS_INTERMEDIATE_OFFSET	16
39 #define SMBIOS_STRUCT_EOS_BYTES		2
40 
41 struct str_lookup_table {
42 	u16 idx;
43 	const char *str;
44 };
45 
46 struct __packed smbios_entry {
47 	u8 anchor[4];
48 	u8 checksum;
49 	u8 length;
50 	u8 major_ver;
51 	u8 minor_ver;
52 	u16 max_struct_size;
53 	u8 entry_point_rev;
54 	u8 formatted_area[5];
55 	u8 intermediate_anchor[5];
56 	u8 intermediate_checksum;
57 	u16 struct_table_length;
58 	u32 struct_table_address;
59 	u16 struct_count;
60 	u8 bcd_rev;
61 };
62 
63 /**
64  * struct smbios3_entry - SMBIOS 3.0 (64-bit) Entry Point structure
65  */
66 struct __packed smbios3_entry {
67 	/** @anchor: anchor string */
68 	u8 anchor[5];
69 	/** @checksum: checksum of the entry point structure */
70 	u8 checksum;
71 	/** @length: length of the entry point structure */
72 	u8 length;
73 	/** @major_ver: major version of the SMBIOS specification */
74 	u8 major_ver;
75 	/** @minor_ver: minor version of the SMBIOS specification */
76 	u8 minor_ver;
77 	/** @docrev: revision of the SMBIOS specification */
78 	u8 doc_rev;
79 	/** @entry_point_rev: revision of the entry point structure */
80 	u8 entry_point_rev;
81 	/** @reserved: reserved */
82 	u8 reserved;
83 	/** maximum size of SMBIOS table */
84 	u32 table_maximum_size;
85 	/** @struct_table_address: 64-bit physical starting address */
86 	u64 struct_table_address;
87 };
88 
89 struct __packed smbios_header {
90 	u8 type;
91 	u8 length;
92 	u16 handle;
93 };
94 
95 struct __packed smbios_type0 {
96 	struct smbios_header hdr;
97 	u8 vendor;
98 	u8 bios_ver;
99 	u16 bios_start_segment;
100 	u8 bios_release_date;
101 	u8 bios_rom_size;
102 	u64 bios_characteristics;
103 	u8 bios_characteristics_ext1;
104 	u8 bios_characteristics_ext2;
105 	u8 bios_major_release;
106 	u8 bios_minor_release;
107 	u8 ec_major_release;
108 	u8 ec_minor_release;
109 	u16 extended_bios_rom_size;
110 	char eos[SMBIOS_STRUCT_EOS_BYTES];
111 };
112 
113 #define SMBIOS_TYPE1_LENGTH_V20		0x08
114 #define SMBIOS_TYPE1_LENGTH_V21		0x19
115 #define SMBIOS_TYPE1_LENGTH_V24		0x1b
116 
117 struct __packed smbios_type1 {
118 	struct smbios_header hdr;
119 	u8 manufacturer;
120 	u8 product_name;
121 	u8 version;
122 	u8 serial_number;
123 	u8 uuid[16];
124 	u8 wakeup_type;
125 	u8 sku_number;
126 	u8 family;
127 	char eos[SMBIOS_STRUCT_EOS_BYTES];
128 };
129 
130 #define SMBIOS_TYPE2_CON_OBJ_HANDLE_SIZE sizeof(u16)
131 
132 struct __packed smbios_type2 {
133 	struct smbios_header hdr;
134 	u8 manufacturer;
135 	u8 product_name;
136 	u8 version;
137 	u8 serial_number;
138 	u8 asset_tag_number;
139 	u8 feature_flags;
140 	u8 chassis_location;
141 	u16 chassis_handle;
142 	u8 board_type;
143 	u8 number_contained_objects;
144 	/*
145 	 * Dynamic bytes will be inserted here to store the objects.
146 	 * length is equal to 'number_contained_objects'.
147 	 */
148 	char eos[SMBIOS_STRUCT_EOS_BYTES];
149 };
150 
151 struct __packed smbios_type3 {
152 	struct smbios_header hdr;
153 	u8 manufacturer;
154 	u8 chassis_type;
155 	u8 version;
156 	u8 serial_number;
157 	u8 asset_tag_number;
158 	u8 bootup_state;
159 	u8 power_supply_state;
160 	u8 thermal_state;
161 	u8 security_status;
162 	u32 oem_defined;
163 	u8 height;
164 	u8 number_of_power_cords;
165 	u8 element_count;
166 	u8 element_record_length;
167 	/*
168 	 * Dynamic bytes will be inserted here to store the elements.
169 	 * length is equal to 'element_record_length' * 'element_record_length'
170 	 */
171 	u8 sku_number;
172 	char eos[SMBIOS_STRUCT_EOS_BYTES];
173 };
174 
175 struct __packed smbios_type4 {
176 	struct smbios_header hdr;
177 	u8 socket_design;
178 	u8 processor_type;
179 	u8 processor_family;
180 	u8 processor_manufacturer;
181 	u32 processor_id[2];
182 	u8 processor_version;
183 	u8 voltage;
184 	u16 external_clock;
185 	u16 max_speed;
186 	u16 current_speed;
187 	u8 status;
188 	u8 processor_upgrade;
189 	u16 l1_cache_handle;
190 	u16 l2_cache_handle;
191 	u16 l3_cache_handle;
192 	u8 serial_number;
193 	u8 asset_tag;
194 	u8 part_number;
195 	u8 core_count;
196 	u8 core_enabled;
197 	u8 thread_count;
198 	u16 processor_characteristics;
199 	u16 processor_family2;
200 	u16 core_count2;
201 	u16 core_enabled2;
202 	u16 thread_count2;
203 	u16 thread_enabled;
204 	char eos[SMBIOS_STRUCT_EOS_BYTES];
205 };
206 
207 union cache_config {
208 	struct {
209 		u16 level:3;
210 		u16 bsocketed:1;
211 		u16 rsvd0:1;
212 		u16 locate:2;
213 		u16 benabled:1;
214 		u16 opmode:2;
215 		u16 rsvd1:6;
216 	} fields;
217 	u16 data;
218 };
219 
220 union cache_size_word {
221 	struct {
222 		u16 size:15;
223 		u16 granu:1;
224 	} fields;
225 	u16 data;
226 };
227 
228 union cache_size_dword {
229 	struct {
230 		u32 size:31;
231 		u32 granu:1;
232 	} fields;
233 	u32 data;
234 };
235 
236 union cache_sram_type {
237 	struct {
238 		u16 other:1;
239 		u16 unknown:1;
240 		u16 nonburst:1;
241 		u16 burst:1;
242 		u16 plburst:1;
243 		u16 sync:1;
244 		u16 async:1;
245 		u16 rsvd:9;
246 	} fields;
247 	u16 data;
248 };
249 
250 struct __packed smbios_type7 {
251 	struct smbios_header hdr;
252 	u8 socket_design;
253 	union cache_config config;
254 	union cache_size_word max_size;
255 	union cache_size_word inst_size;
256 	union cache_sram_type supp_sram_type;
257 	union cache_sram_type curr_sram_type;
258 	u8 speed;
259 	u8 err_corr_type;
260 	u8 sys_cache_type;
261 	u8 associativity;
262 	union cache_size_dword max_size2;
263 	union cache_size_dword inst_size2;
264 	char eos[SMBIOS_STRUCT_EOS_BYTES];
265 };
266 
267 struct __packed smbios_type32 {
268 	u8 type;
269 	u8 length;
270 	u16 handle;
271 	u8 reserved[6];
272 	u8 boot_status;
273 	char eos[SMBIOS_STRUCT_EOS_BYTES];
274 };
275 
276 struct __packed smbios_type127 {
277 	u8 type;
278 	u8 length;
279 	u16 handle;
280 	char eos[SMBIOS_STRUCT_EOS_BYTES];
281 };
282 
283 /**
284  * fill_smbios_header() - Fill the header of an SMBIOS table
285  *
286  * This fills the header of an SMBIOS table structure.
287  *
288  * @table:	start address of the structure
289  * @type:	the type of structure
290  * @length:	the length of the formatted area of the structure
291  * @handle:	the structure's handle, a unique 16-bit number
292  */
fill_smbios_header(void * table,int type,int length,int handle)293 static inline void fill_smbios_header(void *table, int type,
294 				      int length, int handle)
295 {
296 	struct smbios_header *header = table;
297 
298 	header->type = type;
299 	header->length = length - SMBIOS_STRUCT_EOS_BYTES;
300 	header->handle = handle;
301 }
302 
303 /**
304  * write_smbios_table() - Write SMBIOS table
305  *
306  * This writes SMBIOS table at a given address.
307  *
308  * @addr:	start address to write SMBIOS table, 16-byte-alignment
309  * recommended. Note that while the SMBIOS tables themself have no alignment
310  * requirement, some systems may requires alignment. For example x86 systems
311  * which put tables at f0000 require 16-byte alignment
312  *
313  * Return:	end address of SMBIOS table (and start address for next entry)
314  *		or NULL in case of an error
315  */
316 ulong write_smbios_table(ulong addr);
317 
318 /**
319  * smbios_entry() - Get a valid struct smbios_entry pointer
320  *
321  * @address:   address where smbios tables is located
322  * @size:      size of smbios table
323  * @return:    NULL or a valid pointer to a struct smbios_entry
324  */
325 const struct smbios_entry *smbios_entry(u64 address, u32 size);
326 
327 /**
328  * smbios_header() - Search for SMBIOS header type
329  *
330  * @entry:     pointer to a struct smbios_entry
331  * @type:      SMBIOS type
332  * @return:    NULL or a valid pointer to a struct smbios_header
333  */
334 const struct smbios_header *smbios_header(const struct smbios_entry *entry, int type);
335 
336 /**
337  * smbios_string() - Return string from SMBIOS
338  *
339  * @header:    pointer to struct smbios_header
340  * @index:     string index
341  * @return:    NULL or a valid char pointer
342  */
343 char *smbios_string(const struct smbios_header *header, int index);
344 
345 /**
346  * smbios_update_version() - Update the version string
347  *
348  * This can be called after the SMBIOS tables are written (e.g. after the U-Boot
349  * main loop has started) to update the BIOS version string (SMBIOS table 0).
350  *
351  * @version: New version string to use
352  * Return: 0 if OK, -ENOENT if no version string was previously written,
353  *	-ENOSPC if the new string is too large to fit
354  */
355 int smbios_update_version(const char *version);
356 
357 /**
358  * smbios_update_version_full() - Update the version string
359  *
360  * This can be called after the SMBIOS tables are written (e.g. after the U-Boot
361  * main loop has started) to update the BIOS version string (SMBIOS table 0).
362  * It scans for the correct place to put the version, so does not need U-Boot
363  * to have actually written the tables itself (e.g. if a previous bootloader
364  * did it).
365  *
366  * @smbios_tab: Start of SMBIOS tables
367  * @version: New version string to use
368  * Return: 0 if OK, -ENOENT if no version string was previously written,
369  *	-ENOSPC if the new string is too large to fit
370  */
371 int smbios_update_version_full(void *smbios_tab, const char *version);
372 
373 /**
374  * smbios_prepare_measurement() - Update smbios table for the measurement
375  *
376  * TCG specification requires to measure static configuration information.
377  * This function clear the device dependent parameters such as
378  * serial number for the measurement.
379  *
380  * @entry: pointer to a struct smbios3_entry
381  * @header: pointer to a struct smbios_header
382  */
383 void smbios_prepare_measurement(const struct smbios3_entry *entry,
384 				struct smbios_header *header);
385 
386 #endif /* _SMBIOS_H_ */
387