iPXE
Functions | Variables
nvo.c File Reference

Non-volatile stored options. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ipxe/dhcp.h>
#include <ipxe/nvs.h>
#include <ipxe/nvo.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static unsigned int nvo_checksum (struct nvo_block *nvo)
 Calculate checksum over non-volatile stored options. More...
 
static int nvo_realloc (struct nvo_block *nvo, size_t len)
 Reallocate non-volatile stored options block. More...
 
static int nvo_realloc_dhcpopt (struct dhcp_options *options, size_t len)
 Reallocate non-volatile stored options DHCP option block. More...
 
static int nvo_load (struct nvo_block *nvo)
 Load non-volatile stored options from non-volatile storage device. More...
 
static int nvo_save (struct nvo_block *nvo)
 Save non-volatile stored options back to non-volatile storage device. More...
 
int nvo_applies (struct settings *settings __unused, const struct setting *setting)
 Check applicability of NVO setting. More...
 
static int nvo_store (struct settings *settings, const struct setting *setting, const void *data, size_t len)
 Store value of NVO setting. More...
 
static int nvo_fetch (struct settings *settings, struct setting *setting, void *data, size_t len)
 Fetch value of NVO setting. More...
 
void nvo_init (struct nvo_block *nvo, struct nvs_device *nvs, size_t address, size_t len, int(*resize)(struct nvo_block *nvo, size_t len), struct refcnt *refcnt)
 Initialise non-volatile stored options. More...
 
int register_nvo (struct nvo_block *nvo, struct settings *parent)
 Register non-volatile stored options. More...
 
void unregister_nvo (struct nvo_block *nvo)
 Unregister non-volatile stored options. More...
 

Variables

static struct settings_operations nvo_settings_operations
 NVO settings operations. More...
 

Detailed Description

Non-volatile stored options.

Definition in file nvo.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ nvo_checksum()

static unsigned int nvo_checksum ( struct nvo_block nvo)
static

Calculate checksum over non-volatile stored options.

Parameters
nvoNon-volatile options block
Return values
sumChecksum

Definition at line 46 of file nvo.c.

46  {
47  uint8_t *data = nvo->data;
48  uint8_t sum = 0;
49  unsigned int i;
50 
51  for ( i = 0 ; i < nvo->len ; i++ ) {
52  sum += *(data++);
53  }
54  return sum;
55 }
void * data
Option-containing data.
Definition: nvo.h:32
unsigned char uint8_t
Definition: stdint.h:10
uint8_t data[48]
Additional event data.
Definition: ena.h:22
size_t len
Length of options data.
Definition: nvo.h:30

References data, nvo_block::data, and nvo_block::len.

Referenced by nvo_load(), and nvo_save().

◆ nvo_realloc()

static int nvo_realloc ( struct nvo_block nvo,
size_t  len 
)
static

Reallocate non-volatile stored options block.

Parameters
nvoNon-volatile options block
lenNew length
Return values
rcReturn status code

Definition at line 64 of file nvo.c.

64  {
65  void *new_data;
66 
67  /* Reallocate data */
68  new_data = realloc ( nvo->data, len );
69  if ( ! new_data ) {
70  DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
71  nvo, len );
72  return -ENOMEM;
73  }
74  nvo->data = new_data;
75  nvo->len = len;
76 
77  /* Update DHCP option block */
78  if ( len ) {
79  nvo->dhcpopts.data = ( nvo->data + 1 /* checksum */ );
80  nvo->dhcpopts.alloc_len = ( len - 1 /* checksum */ );
81  } else {
82  nvo->dhcpopts.data = NULL;
83  nvo->dhcpopts.used_len = 0;
84  nvo->dhcpopts.alloc_len = 0;
85  }
86 
87  return 0;
88 }
struct dhcp_options dhcpopts
DHCP options block.
Definition: nvo.h:42
#define DBGC(...)
Definition: compiler.h:505
void * data
Option-containing data.
Definition: nvo.h:32
size_t alloc_len
Option block allocated length.
Definition: dhcpopts.h:21
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * data
Option block raw data.
Definition: dhcpopts.h:17
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:521
uint32_t len
Length.
Definition: ena.h:14
size_t len
Length of options data.
Definition: nvo.h:30
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
size_t used_len
Option block used length.
Definition: dhcpopts.h:19

References dhcp_options::alloc_len, dhcp_options::data, nvo_block::data, DBGC, nvo_block::dhcpopts, ENOMEM, len, nvo_block::len, NULL, realloc(), and dhcp_options::used_len.

Referenced by nvo_realloc_dhcpopt(), register_nvo(), and unregister_nvo().

◆ nvo_realloc_dhcpopt()

static int nvo_realloc_dhcpopt ( struct dhcp_options options,
size_t  len 
)
static

Reallocate non-volatile stored options DHCP option block.

Parameters
optionsDHCP option block
lenNew length
Return values
rcReturn status code

Definition at line 97 of file nvo.c.

97  {
98  struct nvo_block *nvo =
100  int rc;
101 
102  /* Refuse to reallocate if we have no way to resize the block */
103  if ( ! nvo->resize )
104  return dhcpopt_no_realloc ( options, len );
105 
106  /* Allow one byte for the checksum (if any data is present) */
107  if ( len )
108  len += 1;
109 
110  /* Resize underlying non-volatile options block */
111  if ( ( rc = nvo->resize ( nvo, len ) ) != 0 ) {
112  DBGC ( nvo, "NVO %p could not resize to %zd bytes: %s\n",
113  nvo, len, strerror ( rc ) );
114  return rc;
115  }
116 
117  /* Reallocate in-memory options block */
118  if ( ( rc = nvo_realloc ( nvo, len ) ) != 0 )
119  return rc;
120 
121  return 0;
122 }
struct dhcp_options dhcpopts
DHCP options block.
Definition: nvo.h:42
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static int options
Definition: 3c515.c:286
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
A block of non-volatile stored options.
Definition: nvo.h:22
int(* resize)(struct nvo_block *nvo, size_t len)
Resize non-volatile stored option block.
Definition: nvo.h:40
int dhcpopt_no_realloc(struct dhcp_options *options, size_t len)
Refuse to reallocate DHCP option block.
Definition: dhcpopts.c:184
static int nvo_realloc(struct nvo_block *nvo, size_t len)
Reallocate non-volatile stored options block.
Definition: nvo.c:64
uint32_t len
Length.
Definition: ena.h:14

References container_of, DBGC, dhcpopt_no_realloc(), nvo_block::dhcpopts, len, nvo_realloc(), options, rc, nvo_block::resize, and strerror().

Referenced by nvo_init().

◆ nvo_load()

static int nvo_load ( struct nvo_block nvo)
static

Load non-volatile stored options from non-volatile storage device.

Parameters
nvoNon-volatile options block
Return values
rcReturn status code

Definition at line 130 of file nvo.c.

130  {
131  uint8_t *options_data = nvo->dhcpopts.data;
132  int rc;
133 
134  /* Skip reading zero-length NVO fields */
135  if ( nvo->len == 0 ) {
136  DBGC ( nvo, "NVO %p is empty; skipping load\n", nvo );
137  return 0;
138  }
139 
140  /* Read data */
141  if ( ( rc = nvs_read ( nvo->nvs, nvo->address, nvo->data,
142  nvo->len ) ) != 0 ) {
143  DBGC ( nvo, "NVO %p could not read %zd bytes at %#04x: %s\n",
144  nvo, nvo->len, nvo->address, strerror ( rc ) );
145  return rc;
146  }
147 
148  /* If checksum fails, or options data starts with a zero,
149  * assume the whole block is invalid. This should capture the
150  * case of random initial contents.
151  */
152  if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
153  DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
154  "assuming empty\n", nvo, nvo_checksum ( nvo ),
155  options_data[0] );
156  memset ( nvo->data, 0, nvo->len );
157  }
158 
159  /* Rescan DHCP option block */
161 
162  DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
163  return 0;
164 }
struct dhcp_options dhcpopts
DHCP options block.
Definition: nvo.h:42
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
void * data
Option-containing data.
Definition: nvo.h:32
struct nvs_device * nvs
Underlying non-volatile storage device.
Definition: nvo.h:26
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned char uint8_t
Definition: stdint.h:10
unsigned int address
Address within NVS device.
Definition: nvo.h:28
void * data
Option block raw data.
Definition: dhcpopts.h:17
void dhcpopt_update_used_len(struct dhcp_options *options)
Recalculate length of DHCP options block.
Definition: dhcpopts.c:420
static unsigned int nvo_checksum(struct nvo_block *nvo)
Calculate checksum over non-volatile stored options.
Definition: nvo.c:46
int nvs_read(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read from non-volatile storage device.
Definition: nvs.c:75
size_t len
Length of options data.
Definition: nvo.h:30
void * memset(void *dest, int character, size_t len) __nonnull

References nvo_block::address, dhcp_options::data, nvo_block::data, DBGC, dhcpopt_update_used_len(), nvo_block::dhcpopts, nvo_block::len, memset(), nvo_checksum(), nvo_block::nvs, nvs_read(), rc, and strerror().

Referenced by register_nvo().

◆ nvo_save()

static int nvo_save ( struct nvo_block nvo)
static

Save non-volatile stored options back to non-volatile storage device.

Parameters
nvoNon-volatile options block
Return values
rcReturn status code

Definition at line 172 of file nvo.c.

172  {
173  uint8_t *checksum = nvo->data;
174  int rc;
175 
176  /* Recalculate checksum, if applicable */
177  if ( nvo->len > 0 )
178  *checksum -= nvo_checksum ( nvo );
179 
180  /* Write data */
181  if ( ( rc = nvs_write ( nvo->nvs, nvo->address, nvo->data,
182  nvo->len ) ) != 0 ) {
183  DBGC ( nvo, "NVO %p could not write %zd bytes at %#04x: %s\n",
184  nvo, nvo->len, nvo->address, strerror ( rc ) );
185  return rc;
186  }
187 
188  DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
189  return 0;
190 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint8_t checksum
Checksum.
Definition: pnpbios.c:37
#define DBGC(...)
Definition: compiler.h:505
int nvs_write(struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
Write to non-volatile storage device.
Definition: nvs.c:140
void * data
Option-containing data.
Definition: nvo.h:32
struct nvs_device * nvs
Underlying non-volatile storage device.
Definition: nvo.h:26
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
unsigned char uint8_t
Definition: stdint.h:10
unsigned int address
Address within NVS device.
Definition: nvo.h:28
static unsigned int nvo_checksum(struct nvo_block *nvo)
Calculate checksum over non-volatile stored options.
Definition: nvo.c:46
size_t len
Length of options data.
Definition: nvo.h:30

References nvo_block::address, checksum, nvo_block::data, DBGC, nvo_block::len, nvo_checksum(), nvo_block::nvs, nvs_write(), rc, and strerror().

Referenced by nvo_store().

◆ nvo_applies()

int nvo_applies ( struct settings *settings  __unused,
const struct setting setting 
)

Check applicability of NVO setting.

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

Definition at line 199 of file nvo.c.

200  {
201 
202  return ( ( setting->scope == NULL ) &&
203  dhcpopt_applies ( setting->tag ) );
204 }
uint64_t tag
Setting tag, if applicable.
Definition: settings.h:43
A setting.
Definition: settings.h:23
const struct settings_scope * scope
Setting scope (or NULL)
Definition: settings.h:49
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
int dhcpopt_applies(unsigned int tag)
Check applicability of DHCP option setting.
Definition: dhcpopts.c:359

References dhcpopt_applies(), NULL, setting::scope, and setting::tag.

Referenced by efi_snp_hii_setting_applies().

◆ nvo_store()

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

Store value of NVO setting.

Parameters
settingsSettings block
settingSetting to store
dataSetting data, or NULL to clear setting
lenLength of setting data
Return values
rcReturn status code

Definition at line 215 of file nvo.c.

216  {
217  struct nvo_block *nvo =
219  int rc;
220 
221  /* Update stored options */
222  if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
223  data, len ) ) != 0 ) {
224  DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
225  nvo, len, strerror ( rc ) );
226  return rc;
227  }
228 
229  /* Save updated options to NVS */
230  if ( ( rc = nvo_save ( nvo ) ) != 0 )
231  return rc;
232 
233  return 0;
234 }
struct dhcp_options dhcpopts
DHCP options block.
Definition: nvo.h:42
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
uint64_t tag
Setting tag, if applicable.
Definition: settings.h:43
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
A block of non-volatile stored options.
Definition: nvo.h:22
A settings block.
Definition: settings.h:132
A setting.
Definition: settings.h:23
static int nvo_save(struct nvo_block *nvo)
Save non-volatile stored options back to non-volatile storage device.
Definition: nvo.c:172
uint8_t data[48]
Additional event data.
Definition: ena.h:22
int dhcpopt_store(struct dhcp_options *options, unsigned int tag, const void *data, size_t len)
Store value of DHCP option setting.
Definition: dhcpopts.c:374
uint32_t len
Length.
Definition: ena.h:14

References container_of, data, DBGC, dhcpopt_store(), nvo_block::dhcpopts, len, nvo_save(), rc, strerror(), and setting::tag.

◆ nvo_fetch()

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

Fetch value of NVO setting.

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

The actual length of the setting will be returned even if the buffer was too small.

Definition at line 248 of file nvo.c.

249  {
250  struct nvo_block *nvo =
252 
253  return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
254 }
struct dhcp_options dhcpopts
DHCP options block.
Definition: nvo.h:42
uint64_t tag
Setting tag, if applicable.
Definition: settings.h:43
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
A block of non-volatile stored options.
Definition: nvo.h:22
A settings block.
Definition: settings.h:132
A setting.
Definition: settings.h:23
uint8_t data[48]
Additional event data.
Definition: ena.h:22
int dhcpopt_fetch(struct dhcp_options *options, unsigned int tag, void *data, size_t len)
Fetch value of DHCP option setting.
Definition: dhcpopts.c:393
uint32_t len
Length.
Definition: ena.h:14

References container_of, data, dhcpopt_fetch(), nvo_block::dhcpopts, len, and setting::tag.

◆ nvo_init()

void nvo_init ( struct nvo_block nvo,
struct nvs_device nvs,
size_t  address,
size_t  len,
int(*)(struct nvo_block *nvo, size_t len resize,
struct refcnt refcnt 
)

Initialise non-volatile stored options.

Parameters
nvoNon-volatile options block
nvsUnderlying non-volatile storage device
addressAddress within NVS device
lenLength of non-volatile options data
resizeResize method
refcntContaining object reference counter, or NULL

Definition at line 273 of file nvo.c.

276  {
277  nvo->nvs = nvs;
278  nvo->address = address;
279  nvo->len = len;
280  nvo->resize = resize;
283  refcnt, NULL );
284 }
struct dhcp_options dhcpopts
DHCP options block.
Definition: nvo.h:42
void dhcpopt_init(struct dhcp_options *options, void *data, size_t alloc_len, int(*realloc)(struct dhcp_options *options, size_t len))
Initialise prepopulated block of DHCP options.
Definition: dhcpopts.c:451
uint64_t address
Base address.
Definition: ena.h:24
static void settings_init(struct settings *settings, struct settings_operations *op, struct refcnt *refcnt, const struct settings_scope *default_scope)
Initialise a settings block.
Definition: settings.h:500
A reference counter.
Definition: refcnt.h:26
static int nvo_realloc_dhcpopt(struct dhcp_options *options, size_t len)
Reallocate non-volatile stored options DHCP option block.
Definition: nvo.c:97
struct nvs_device * nvs
Underlying non-volatile storage device.
Definition: nvo.h:26
int(* resize)(struct nvo_block *nvo, size_t len)
Resize non-volatile stored option block.
Definition: nvo.h:40
struct settings settings
Settings block.
Definition: nvo.h:24
unsigned int address
Address within NVS device.
Definition: nvo.h:28
static struct settings_operations nvo_settings_operations
NVO settings operations.
Definition: nvo.c:257
uint32_t len
Length.
Definition: ena.h:14
size_t len
Length of options data.
Definition: nvo.h:30
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References address, nvo_block::address, dhcpopt_init(), nvo_block::dhcpopts, len, nvo_block::len, NULL, nvo_realloc_dhcpopt(), nvo_settings_operations, nvo_block::nvs, nvo_block::resize, nvo_block::settings, and settings_init().

Referenced by falcon_probe_spi(), myri10ge_nv_init(), nvs_vpd_nvo_init(), and realtek_init_eeprom().

◆ register_nvo()

int register_nvo ( struct nvo_block nvo,
struct settings parent 
)

Register non-volatile stored options.

Parameters
nvoNon-volatile options block
parentParent settings block, or NULL
Return values
rcReturn status code

Definition at line 293 of file nvo.c.

293  {
294  int rc;
295 
296  /* Allocate memory for options */
297  if ( ( rc = nvo_realloc ( nvo, nvo->len ) ) != 0 )
298  goto err_realloc;
299 
300  /* Read data from NVS */
301  if ( ( rc = nvo_load ( nvo ) ) != 0 )
302  goto err_load;
303 
304  /* Register settings */
305  if ( ( rc = register_settings ( &nvo->settings, parent,
306  NVO_SETTINGS_NAME ) ) != 0 )
307  goto err_register;
308 
309  DBGC ( nvo, "NVO %p registered\n", nvo );
310  return 0;
311 
312  err_register:
313  err_load:
314  nvo_realloc ( nvo, 0 );
315  err_realloc:
316  return rc;
317 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
struct settings settings
Settings block.
Definition: nvo.h:24
static int nvo_realloc(struct nvo_block *nvo, size_t len)
Reallocate non-volatile stored options block.
Definition: nvo.c:64
static int nvo_load(struct nvo_block *nvo)
Load non-volatile stored options from non-volatile storage device.
Definition: nvo.c:130
#define NVO_SETTINGS_NAME
Name of non-volatile options settings block.
Definition: nvo.h:46
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:475
size_t len
Length of options data.
Definition: nvo.h:30

References DBGC, nvo_block::len, nvo_load(), nvo_realloc(), NVO_SETTINGS_NAME, rc, register_settings(), and nvo_block::settings.

Referenced by efab_probe(), hermon_register_netdev(), myri10ge_nv_init(), and realtek_probe().

◆ unregister_nvo()

void unregister_nvo ( struct nvo_block nvo)

Unregister non-volatile stored options.

Parameters
nvoNon-volatile options block

Definition at line 324 of file nvo.c.

324  {
325  unregister_settings ( &nvo->settings );
326  nvo_realloc ( nvo, 0 );
327  DBGC ( nvo, "NVO %p unregistered\n", nvo );
328 }
void unregister_settings(struct settings *settings)
Unregister settings block.
Definition: settings.c:514
#define DBGC(...)
Definition: compiler.h:505
struct settings settings
Settings block.
Definition: nvo.h:24
static int nvo_realloc(struct nvo_block *nvo, size_t len)
Reallocate non-volatile stored options block.
Definition: nvo.c:64

References DBGC, nvo_realloc(), nvo_block::settings, and unregister_settings().

Referenced by efab_remove(), hermon_register_netdev(), hermon_unregister_netdev(), myri10ge_nv_fini(), and realtek_remove().

Variable Documentation

◆ nvo_settings_operations

struct settings_operations nvo_settings_operations
static
Initial value:
= {
.applies = nvo_applies,
.store = nvo_store,
.fetch = nvo_fetch,
}
int nvo_applies(struct settings *settings __unused, const struct setting *setting)
Check applicability of NVO setting.
Definition: nvo.c:199
static int nvo_fetch(struct settings *settings, struct setting *setting, void *data, size_t len)
Fetch value of NVO setting.
Definition: nvo.c:248
static int nvo_store(struct settings *settings, const struct setting *setting, const void *data, size_t len)
Store value of NVO setting.
Definition: nvo.c:215

NVO settings operations.

Definition at line 257 of file nvo.c.

Referenced by nvo_init().