iPXE
smbios_settings.c File Reference
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <ipxe/settings.h>
#include <ipxe/init.h>
#include <ipxe/uuid.h>
#include <ipxe/smbios.h>

Go to the source code of this file.

Macros

#define SMBIOS_RAW_TAG(_type, _structure, _field)
 Construct SMBIOS raw-data tag.
#define SMBIOS_STRING_TAG(_type, _structure, _field)
 Construct SMBIOS string tag.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static int smbios_applies (struct settings *settings __unused, const struct setting *setting)
 Check applicability of SMBIOS setting.
static int smbios_fetch (struct settings *settings, struct setting *setting, void *data, size_t len)
 Fetch value of SMBIOS setting.
static void smbios_init (void)
 Initialise SMBIOS settings.
struct init_fn smbios_init_fn __init_fn (INIT_NORMAL)
 SMBIOS settings initialiser.
const struct setting uuid_setting __setting (SETTING_HOST, uuid)
 UUID setting obtained via SMBIOS.
const struct setting manufacturer_setting __setting (SETTING_HOST_EXTRA, manufacturer)
 Manufacturer name setting.
const struct setting product_setting __setting (SETTING_HOST_EXTRA, product)
 Product name setting.
const struct setting serial_setting __setting (SETTING_HOST_EXTRA, serial)
 Serial number setting.
const struct setting asset_setting __setting (SETTING_HOST_EXTRA, asset)
 Asset tag setting.
const struct setting board_serial_setting __setting (SETTING_HOST_EXTRA, board-serial)
 Board serial number setting (may differ from chassis serial number)

Variables

static const struct settings_scope smbios_settings_scope
 SMBIOS settings scope.
static struct settings_operations smbios_settings_operations
 SMBIOS settings operations.
static struct settings smbios_settings
 SMBIOS settings.

Macro Definition Documentation

◆ SMBIOS_RAW_TAG

#define SMBIOS_RAW_TAG ( _type,
_structure,
_field )
Value:
( ( (_type) << 16 ) | \
( offsetof ( _structure, _field ) << 8 ) | \
( sizeof ( ( ( _structure * ) 0 )->_field ) ) )
#define offsetof(type, field)
Get offset of a field within a structure.
Definition stddef.h:25

Construct SMBIOS raw-data tag.

Parameters
_typeSMBIOS structure type number
_structureSMBIOS structure data type
_fieldField within SMBIOS structure data type
Return values
tagSMBIOS setting tag

Definition at line 46 of file smbios_settings.c.

46#define SMBIOS_RAW_TAG( _type, _structure, _field ) \
47 ( ( (_type) << 16 ) | \
48 ( offsetof ( _structure, _field ) << 8 ) | \
49 ( sizeof ( ( ( _structure * ) 0 )->_field ) ) )

Referenced by __setting().

◆ SMBIOS_STRING_TAG

#define SMBIOS_STRING_TAG ( _type,
_structure,
_field )
Value:
( ( (_type) << 16 ) | \
( offsetof ( _structure, _field ) << 8 ) )

Construct SMBIOS string tag.

Parameters
_typeSMBIOS structure type number
_structureSMBIOS structure data type
_fieldField within SMBIOS structure data type
Return values
tagSMBIOS setting tag

Definition at line 59 of file smbios_settings.c.

59#define SMBIOS_STRING_TAG( _type, _structure, _field ) \
60 ( ( (_type) << 16 ) | \
61 ( offsetof ( _structure, _field ) << 8 ) )

Referenced by __setting(), __setting(), __setting(), __setting(), and __setting().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ smbios_applies()

int smbios_applies ( struct settings *settings __unused,
const struct setting * setting )
static

Check applicability of SMBIOS setting.

Parameters
settingsSettings block
settingSetting
Return values
appliesSetting applies within this settings block

Definition at line 70 of file smbios_settings.c.

71 {
72
73 return ( setting->scope == &smbios_settings_scope );
74}
static const struct settings_scope smbios_settings_scope
SMBIOS settings scope.
A setting.
Definition settings.h:24
const struct settings_scope * scope
Setting scope (or NULL)
Definition settings.h:50

References __unused, setting::scope, and smbios_settings_scope.

◆ smbios_fetch()

int smbios_fetch ( struct settings * settings,
struct setting * setting,
void * data,
size_t len )
static

Fetch value of SMBIOS setting.

Parameters
settingsSettings block, or NULL to search all blocks
settingSetting to fetch
dataBuffer to fill with setting data
lenLength of buffer
Return values
lenLength of setting data, or negative error

Definition at line 85 of file smbios_settings.c.

86 {
87 const struct smbios_header *structure;
88 unsigned int tag_instance;
89 unsigned int tag_type;
90 unsigned int tag_offset;
91 unsigned int tag_len;
92 const void *src;
93 size_t src_len;
94 unsigned int string;
95 union uuid uuid;
96
97 /* Split tag into instance, type, offset and length */
98 tag_instance = ( ( setting->tag >> 24 ) & 0xff );
99 tag_type = ( ( setting->tag >> 16 ) & 0xff );
100 tag_offset = ( ( setting->tag >> 8 ) & 0xff );
101 tag_len = ( setting->tag & 0xff );
102
103 /* Find SMBIOS structure */
104 structure = smbios_structure ( tag_type, tag_instance );
105 if ( ! structure )
106 return -ENOENT;
107 src = structure;
108 src_len = structure->len;
109 string = 0;
110
111 /* A <length> of zero indicates that the byte at <offset>
112 * contains a string index. An <offset> of zero indicates
113 * that the <length> contains a literal string index.
114 *
115 * Since the byte at offset zero can never contain a string
116 * index, and a literal string index can never be zero, the
117 * combination of both <length> and <offset> being zero
118 * indicates that the entire structure is to be read.
119 */
120 if ( ( tag_len == 0 ) && ( tag_offset == 0 ) ) {
121 /* Read whole structure */
122 } else if ( ( tag_len == 0 ) || ( tag_offset == 0 ) ) {
123 /* Read string */
124 string = tag_len;
125 if ( ( string == 0 ) && ( tag_offset < src_len ) )
126 string = *( ( uint8_t * ) src + tag_offset );
127 src = smbios_string ( structure, string );
128 if ( ! src )
129 return -ENOENT;
130 assert ( string > 0 );
131 src_len = strlen ( src );
132 } else if ( tag_offset > src_len ) {
133 /* Empty read beyond end of structure */
134 src_len = 0;
135 } else {
136 /* Read partial structure */
137 src += tag_offset;
138 src_len -= tag_offset;
139 if ( src_len > tag_len )
140 src_len = tag_len;
141 }
142
143 /* Mangle UUIDs if necessary. iPXE treats UUIDs as being in
144 * network byte order (big-endian). SMBIOS specification
145 * version 2.6 states that UUIDs are stored with little-endian
146 * values in the first three fields; earlier versions did not
147 * specify an endianness. dmidecode assumes that the byte
148 * order is little-endian if and only if the SMBIOS version is
149 * 2.6 or higher; we match this behaviour.
150 */
151 if ( ( ( setting->type == &setting_type_uuid ) ||
152 ( setting->type == &setting_type_guid ) ) &&
153 ( src_len == sizeof ( uuid ) ) &&
154 ( smbios_version() >= SMBIOS_VERSION ( 2, 6 ) ) ) {
155 DBGC ( settings, "SMBIOS detected mangled UUID\n" );
156 memcpy ( &uuid, src, sizeof ( uuid ) );
157 uuid_mangle ( &uuid );
158 src = &uuid;
159 }
160
161 /* Return data */
162 if ( len > src_len )
163 len = src_len;
164 memcpy ( data, src, len );
165
166 /* Set default type */
167 if ( ! setting->type ) {
168 setting->type = ( string ? &setting_type_string :
169 &setting_type_hex );
170 }
171
172 return src_len;
173}
unsigned char uint8_t
Definition stdint.h:10
static const void * src
Definition string.h:48
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define DBGC(...)
Definition compiler.h:505
#define ENOENT
No such file or directory.
Definition errno.h:515
#define SMBIOS_VERSION(major, minor)
Calculate SMBIOS version.
Definition smbios.h:216
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint32_t string
Definition multiboot.h:2
const char * smbios_string(const struct smbios_header *structure, unsigned int index)
Get indexed string within SMBIOS structure.
Definition smbios.c:252
const struct smbios_header * smbios_structure(unsigned int type, unsigned int instance)
Find specific structure type within SMBIOS.
Definition smbios.c:183
int smbios_version(void)
Get SMBIOS version.
Definition smbios.c:285
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
const struct setting_type * type
Setting type.
Definition settings.h:37
uint64_t tag
Setting tag, if applicable.
Definition settings.h:44
A settings block.
Definition settings.h:133
An SMBIOS structure header.
Definition smbios.h:120
uint8_t len
Length.
Definition smbios.h:124
A universally unique ID.
Definition uuid.h:16
static void uuid_mangle(union uuid *uuid)
Change UUID endianness.
Definition uuid.h:44

References assert, data, DBGC, ENOENT, len, smbios_header::len, memcpy(), smbios_string(), smbios_structure(), SMBIOS_VERSION, smbios_version(), src, string, strlen(), setting::tag, setting::type, and uuid_mangle().

◆ smbios_init()

void smbios_init ( void )
static

Initialise SMBIOS settings.

Definition at line 191 of file smbios_settings.c.

191 {
193 int rc;
194
195 if ( ( rc = register_settings ( settings, NULL, "smbios" ) ) != 0 ) {
196 DBGC ( settings, "SMBIOS could not register settings: %s\n",
197 strerror ( rc ) );
198 return;
199 }
200}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition settings.c:476
static struct settings smbios_settings
SMBIOS settings.
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79

References DBGC, NULL, rc, register_settings(), smbios_settings, and strerror().

Referenced by __init_fn().

◆ __init_fn()

struct init_fn smbios_init_fn __init_fn ( INIT_NORMAL )

SMBIOS settings initialiser.

References __init_fn, INIT_NORMAL, and smbios_init().

◆ __setting() [1/6]

const struct setting uuid_setting __setting ( SETTING_HOST ,
uuid  )

UUID setting obtained via SMBIOS.

References __setting, SETTING_HOST, SMBIOS_RAW_TAG, smbios_settings_scope, and SMBIOS_TYPE_SYSTEM_INFORMATION.

◆ __setting() [2/6]

const struct setting manufacturer_setting __setting ( SETTING_HOST_EXTRA ,
manufacturer  )

◆ __setting() [3/6]

const struct setting product_setting __setting ( SETTING_HOST_EXTRA ,
product  )

◆ __setting() [4/6]

const struct setting serial_setting __setting ( SETTING_HOST_EXTRA ,
serial  )

◆ __setting() [5/6]

const struct setting asset_setting __setting ( SETTING_HOST_EXTRA ,
asset  )

◆ __setting() [6/6]

const struct setting board_serial_setting __setting ( SETTING_HOST_EXTRA ,
board- serial )

Board serial number setting (may differ from chassis serial number)

References __setting, serial, SETTING_HOST_EXTRA, smbios_settings_scope, SMBIOS_STRING_TAG, and SMBIOS_TYPE_BASE_BOARD_INFORMATION.

Variable Documentation

◆ smbios_settings_scope

const struct settings_scope smbios_settings_scope
static

SMBIOS settings scope.

Definition at line 36 of file smbios_settings.c.

Referenced by __setting(), __setting(), __setting(), __setting(), __setting(), __setting(), and smbios_applies().

◆ smbios_settings_operations

struct settings_operations smbios_settings_operations
static
Initial value:
= {
.applies = smbios_applies,
.fetch = smbios_fetch,
}
static int smbios_applies(struct settings *settings __unused, const struct setting *setting)
Check applicability of SMBIOS setting.
static int smbios_fetch(struct settings *settings, struct setting *setting, void *data, size_t len)
Fetch value of SMBIOS setting.

SMBIOS settings operations.

Definition at line 176 of file smbios_settings.c.

176 {
177 .applies = smbios_applies,
178 .fetch = smbios_fetch,
179};

◆ smbios_settings

struct settings smbios_settings
static
Initial value:
= {
.refcnt = NULL,
.siblings = LIST_HEAD_INIT ( smbios_settings.siblings ),
.children = LIST_HEAD_INIT ( smbios_settings.children ),
.default_scope = &smbios_settings_scope,
}
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition list.h:31
static struct settings_operations smbios_settings_operations
SMBIOS settings operations.

SMBIOS settings.

Definition at line 182 of file smbios_settings.c.

182 {
183 .refcnt = NULL,
184 .siblings = LIST_HEAD_INIT ( smbios_settings.siblings ),
185 .children = LIST_HEAD_INIT ( smbios_settings.children ),
187 .default_scope = &smbios_settings_scope,
188};

Referenced by smbios_init().