iPXE
smbios_settings.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ipxe/settings.h>
30 #include <ipxe/init.h>
31 #include <ipxe/uuid.h>
32 #include <ipxe/smbios.h>
33 
34 /** SMBIOS settings scope */
36 
37 /**
38  * Construct SMBIOS raw-data tag
39  *
40  * @v _type SMBIOS structure type number
41  * @v _structure SMBIOS structure data type
42  * @v _field Field within SMBIOS structure data type
43  * @ret tag SMBIOS setting tag
44  */
45 #define SMBIOS_RAW_TAG( _type, _structure, _field ) \
46  ( ( (_type) << 16 ) | \
47  ( offsetof ( _structure, _field ) << 8 ) | \
48  ( sizeof ( ( ( _structure * ) 0 )->_field ) ) )
49 
50 /**
51  * Construct SMBIOS string tag
52  *
53  * @v _type SMBIOS structure type number
54  * @v _structure SMBIOS structure data type
55  * @v _field Field within SMBIOS structure data type
56  * @ret tag SMBIOS setting tag
57  */
58 #define SMBIOS_STRING_TAG( _type, _structure, _field ) \
59  ( ( (_type) << 16 ) | \
60  ( offsetof ( _structure, _field ) << 8 ) )
61 
62 /**
63  * Check applicability of SMBIOS setting
64  *
65  * @v settings Settings block
66  * @v setting Setting
67  * @ret applies Setting applies within this settings block
68  */
70  const struct setting *setting ) {
71 
72  return ( setting->scope == &smbios_settings_scope );
73 }
74 
75 /**
76  * Fetch value of SMBIOS setting
77  *
78  * @v settings Settings block, or NULL to search all blocks
79  * @v setting Setting to fetch
80  * @v data Buffer to fill with setting data
81  * @v len Length of buffer
82  * @ret len Length of setting data, or negative error
83  */
84 static int smbios_fetch ( struct settings *settings, struct setting *setting,
85  void *data, size_t len ) {
86  const struct smbios_header *structure;
87  unsigned int tag_instance;
88  unsigned int tag_type;
89  unsigned int tag_offset;
90  unsigned int tag_len;
91  const void *src;
92  size_t src_len;
93  unsigned int string;
94  union uuid uuid;
95 
96  /* Split tag into instance, type, offset and length */
97  tag_instance = ( ( setting->tag >> 24 ) & 0xff );
98  tag_type = ( ( setting->tag >> 16 ) & 0xff );
99  tag_offset = ( ( setting->tag >> 8 ) & 0xff );
100  tag_len = ( setting->tag & 0xff );
101 
102  /* Find SMBIOS structure */
103  structure = smbios_structure ( tag_type, tag_instance );
104  if ( ! structure )
105  return -ENOENT;
106  src = structure;
107  src_len = structure->len;
108  string = 0;
109 
110  /* A <length> of zero indicates that the byte at <offset>
111  * contains a string index. An <offset> of zero indicates
112  * that the <length> contains a literal string index.
113  *
114  * Since the byte at offset zero can never contain a string
115  * index, and a literal string index can never be zero, the
116  * combination of both <length> and <offset> being zero
117  * indicates that the entire structure is to be read.
118  */
119  if ( ( tag_len == 0 ) && ( tag_offset == 0 ) ) {
120  /* Read whole structure */
121  } else if ( ( tag_len == 0 ) || ( tag_offset == 0 ) ) {
122  /* Read string */
123  string = tag_len;
124  if ( ( string == 0 ) && ( tag_offset < src_len ) )
125  string = *( ( uint8_t * ) src + tag_offset );
126  src = smbios_string ( structure, string );
127  if ( ! src )
128  return -ENOENT;
129  assert ( string > 0 );
130  src_len = strlen ( src );
131  } else if ( tag_offset > src_len ) {
132  /* Empty read beyond end of structure */
133  src_len = 0;
134  } else {
135  /* Read partial structure */
136  src += tag_offset;
137  src_len -= tag_offset;
138  if ( src_len > tag_len )
139  src_len = tag_len;
140  }
141 
142  /* Mangle UUIDs if necessary. iPXE treats UUIDs as being in
143  * network byte order (big-endian). SMBIOS specification
144  * version 2.6 states that UUIDs are stored with little-endian
145  * values in the first three fields; earlier versions did not
146  * specify an endianness. dmidecode assumes that the byte
147  * order is little-endian if and only if the SMBIOS version is
148  * 2.6 or higher; we match this behaviour.
149  */
150  if ( ( ( setting->type == &setting_type_uuid ) ||
151  ( setting->type == &setting_type_guid ) ) &&
152  ( src_len == sizeof ( uuid ) ) &&
153  ( smbios_version() >= SMBIOS_VERSION ( 2, 6 ) ) ) {
154  DBGC ( settings, "SMBIOS detected mangled UUID\n" );
155  memcpy ( &uuid, src, sizeof ( uuid ) );
156  uuid_mangle ( &uuid );
157  src = &uuid;
158  }
159 
160  /* Return data */
161  if ( len > src_len )
162  len = src_len;
163  memcpy ( data, src, len );
164 
165  /* Set default type */
166  if ( ! setting->type ) {
167  setting->type = ( string ? &setting_type_string :
168  &setting_type_hex );
169  }
170 
171  return src_len;
172 }
173 
174 /** SMBIOS settings operations */
177  .fetch = smbios_fetch,
178 };
179 
180 /** SMBIOS settings */
181 static struct settings smbios_settings = {
182  .refcnt = NULL,
183  .siblings = LIST_HEAD_INIT ( smbios_settings.siblings ),
184  .children = LIST_HEAD_INIT ( smbios_settings.children ),
186  .default_scope = &smbios_settings_scope,
187 };
188 
189 /** Initialise SMBIOS settings */
190 static void smbios_init ( void ) {
191  struct settings *settings = &smbios_settings;
192  int rc;
193 
194  if ( ( rc = register_settings ( settings, NULL, "smbios" ) ) != 0 ) {
195  DBGC ( settings, "SMBIOS could not register settings: %s\n",
196  strerror ( rc ) );
197  return;
198  }
199 }
200 
201 /** SMBIOS settings initialiser */
202 struct init_fn smbios_init_fn __init_fn ( INIT_NORMAL ) = {
203  .name = "smbios",
204  .initialise = smbios_init,
205 };
206 
207 /** UUID setting obtained via SMBIOS */
208 const struct setting uuid_setting __setting ( SETTING_HOST, uuid ) = {
209  .name = "uuid",
210  .description = "UUID",
213  .type = &setting_type_uuid,
214  .scope = &smbios_settings_scope,
215 };
216 
217 /** Manufacturer name setting */
218 const struct setting manufacturer_setting __setting ( SETTING_HOST_EXTRA,
219  manufacturer ) = {
220  .name = "manufacturer",
221  .description = "Manufacturer",
224  manufacturer ),
225  .type = &setting_type_string,
226  .scope = &smbios_settings_scope,
227 };
228 
229 /** Product name setting */
230 const struct setting product_setting __setting ( SETTING_HOST_EXTRA, product )={
231  .name = "product",
232  .description = "Product name",
235  product ),
236  .type = &setting_type_string,
237  .scope = &smbios_settings_scope,
238 };
239 
240 /** Serial number setting */
241 const struct setting serial_setting __setting ( SETTING_HOST_EXTRA, serial ) = {
242  .name = "serial",
243  .description = "Serial number",
246  serial ),
247  .type = &setting_type_string,
248  .scope = &smbios_settings_scope,
249 };
250 
251 /** Asset tag setting */
252 const struct setting asset_setting __setting ( SETTING_HOST_EXTRA, asset ) = {
253  .name = "asset",
254  .description = "Asset tag",
257  asset_tag ),
258  .type = &setting_type_string,
259  .scope = &smbios_settings_scope,
260 };
261 
262 /** Board serial number setting (may differ from chassis serial number) */
263 const struct setting board_serial_setting __setting ( SETTING_HOST_EXTRA,
264  board-serial ) = {
265  .name = "board-serial",
266  .description = "Base board serial",
269  serial ),
270  .type = &setting_type_string,
271  .scope = &smbios_settings_scope,
272 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
const char * smbios_string(const struct smbios_header *structure, unsigned int index)
Get indexed string within SMBIOS structure.
Definition: smbios.c:251
Error codes.
A universally unique ID.
Definition: uuid.h:15
#define DBGC(...)
Definition: compiler.h:505
const struct smbios_header * smbios_structure(unsigned int type, unsigned int instance)
Find specific structure type within SMBIOS.
Definition: smbios.c:182
#define ENOENT
No such file or directory.
Definition: errno.h:514
Universally unique IDs.
uint32_t string
Definition: multiboot.h:14
#define SMBIOS_TYPE_BASE_BOARD_INFORMATION
SMBIOS base board information structure type.
Definition: smbios.h:164
#define SETTING_HOST_EXTRA
Host identity additional settings.
Definition: settings.h:76
const char * name
Name.
Definition: settings.h:28
static void uuid_mangle(union uuid *uuid)
Change UUID endianness.
Definition: uuid.h:43
uint64_t tag
Setting tag, if applicable.
Definition: settings.h:43
#define INIT_NORMAL
Normal initialisation.
Definition: init.h:31
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint8_t len
Length.
Definition: smbios.h:123
An initialisation function.
Definition: init.h:14
#define SMBIOS_STRING_TAG(_type, _structure, _field)
Construct SMBIOS string tag.
#define SMBIOS_TYPE_ENCLOSURE_INFORMATION
SMBIOS enclosure information structure type.
Definition: smbios.h:183
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static const void * src
Definition: string.h:47
uint8_t uuid[16]
UUID.
Definition: smbios.h:22
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
ring len
Length.
Definition: dwmac.h:231
const char * name
Definition: init.h:15
static void smbios_init(void)
Initialise SMBIOS settings.
int smbios_version(void)
Get SMBIOS version.
Definition: smbios.c:284
const struct setting_type * type
Setting type.
Definition: settings.h:36
uint8_t asset_tag
Asset tag.
Definition: smbios.h:22
static struct settings smbios_settings
SMBIOS settings.
Configuration settings.
static int smbios_applies(struct settings *settings __unused, const struct setting *setting)
Check applicability of SMBIOS setting.
#define SMBIOS_VERSION(major, minor)
Calculate SMBIOS version.
Definition: smbios.h:215
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
uint64_t serial
Serial number.
Definition: edd.h:30
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
Settings block operations.
Definition: settings.h:85
A settings block.
Definition: settings.h:132
unsigned char uint8_t
Definition: stdint.h:10
A setting scope.
Definition: settings.h:176
const struct setting uuid_setting __setting(SETTING_HOST, uuid)
UUID setting obtained via SMBIOS.
struct list_head siblings
Sibling settings blocks.
Definition: settings.h:140
uint8_t manufacturer
Manufacturer string.
Definition: smbios.h:14
A setting.
Definition: settings.h:23
System Management BIOS.
static int smbios_fetch(struct settings *settings, struct setting *setting, void *data, size_t len)
Fetch value of SMBIOS setting.
An SMBIOS structure header.
Definition: smbios.h:119
SMBIOS system information structure.
Definition: smbios.h:129
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define SMBIOS_RAW_TAG(_type, _structure, _field)
Construct SMBIOS raw-data tag.
uint8_t product
Product string.
Definition: smbios.h:16
int(* applies)(struct settings *settings, const struct setting *setting)
Check applicability of setting.
Definition: settings.h:98
struct list_head children
Child settings blocks.
Definition: settings.h:142
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:475
static struct settings_operations smbios_settings_operations
SMBIOS settings operations.
static const struct settings_scope smbios_settings_scope
SMBIOS settings scope.
SMBIOS base board information structure.
Definition: smbios.h:150
struct init_fn smbios_init_fn __init_fn(INIT_NORMAL)
SMBIOS settings initialiser.
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30
const struct settings_scope * scope
Setting scope (or NULL)
Definition: settings.h:49
#define SMBIOS_TYPE_SYSTEM_INFORMATION
SMBIOS system information structure type.
Definition: smbios.h:147
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
SMBIOS enclosure information structure.
Definition: smbios.h:167
struct refcnt * refcnt
Reference counter.
Definition: settings.h:134
#define SETTING_HOST
Host identity settings.
Definition: settings.h:75