iPXE
Functions
nvs.c File Reference

Non-volatile storage. More...

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/nvs.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static size_t nvs_frag_len (struct nvs_device *nvs, unsigned int address, size_t max_len)
 Calculate length up to next block boundary. More...
 
int nvs_read (struct nvs_device *nvs, unsigned int address, void *data, size_t len)
 Read from non-volatile storage device. More...
 
static int nvs_verify (struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
 Verify content of non-volatile storage device. More...
 
int nvs_write (struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
 Write to non-volatile storage device. More...
 

Detailed Description

Non-volatile storage.

Definition in file nvs.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ nvs_frag_len()

static size_t nvs_frag_len ( struct nvs_device nvs,
unsigned int  address,
size_t  max_len 
)
static

Calculate length up to next block boundary.

Parameters
nvsNVS device
addressStarting address
max_lenMaximum length
Return values
lenLength to use, stopping at block boundaries

Definition at line 46 of file nvs.c.

47  {
48  size_t frag_len;
49 
50  /* If there are no block boundaries, return the maximum length */
51  if ( ! nvs->block_size )
52  return max_len;
53 
54  /* Calculate space remaining up to next block boundary */
55  frag_len = ( ( nvs->block_size -
56  ( address & ( nvs->block_size - 1 ) ) )
57  << nvs->word_len_log2 );
58 
59  /* Limit to maximum length */
60  if ( max_len < frag_len )
61  return max_len;
62 
63  return frag_len;
64 }
unsigned int word_len_log2
Word length.
Definition: nvs.h:22
static void size_t size_t max_len
Definition: entropy.h:153
uint64_t address
Base address.
Definition: ena.h:24
unsigned int block_size
Data block size (in words)
Definition: nvs.h:36

References address, nvs_device::block_size, max_len, and nvs_device::word_len_log2.

Referenced by nvs_read(), and nvs_write().

◆ nvs_read()

int nvs_read ( struct nvs_device nvs,
unsigned int  address,
void *  data,
size_t  len 
)

Read from non-volatile storage device.

Parameters
nvsNVS device
addressAddress from which to read
dataData buffer
lenLength of data buffer
Return values
rcReturn status code

Definition at line 75 of file nvs.c.

76  {
77  size_t frag_len;
78  int rc;
79 
80  /* We don't even attempt to handle buffer lengths that aren't
81  * an integral number of words.
82  */
83  assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
84 
85  while ( len ) {
86 
87  /* Calculate length to read, stopping at block boundaries */
88  frag_len = nvs_frag_len ( nvs, address, len );
89 
90  /* Read this portion of the buffer from the device */
91  if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
92  return rc;
93 
94  /* Update parameters */
95  data += frag_len;
96  address += ( frag_len >> nvs->word_len_log2 );
97  len -= frag_len;
98  }
99 
100  return 0;
101 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int word_len_log2
Word length.
Definition: nvs.h:22
uint64_t address
Base address.
Definition: ena.h:24
static size_t nvs_frag_len(struct nvs_device *nvs, unsigned int address, size_t max_len)
Calculate length up to next block boundary.
Definition: nvs.c:46
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
int(* read)(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read data from device.
Definition: nvs.h:47
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

References address, assert(), data, len, nvs_frag_len(), rc, nvs_device::read, and nvs_device::word_len_log2.

Referenced by a3c90x_internal_ReadEepromContents(), falcon_probe_nvram(), icplus_probe(), ifec_pci_probe(), intel_fetch_mac_eeprom(), natsemi_hwaddr(), nvo_load(), nvs_verify(), realtek_init_eeprom(), realtek_probe(), and rtl818x_probe().

◆ nvs_verify()

static int nvs_verify ( struct nvs_device nvs,
unsigned int  address,
const void *  data,
size_t  len 
)
static

Verify content of non-volatile storage device.

Parameters
nvsNVS device
addressAddress from which to read
dataData to compare against
lenLength of data buffer
Return values
rcReturn status code

Definition at line 112 of file nvs.c.

113  {
114  uint8_t read_data[len];
115  int rc;
116 
117  /* Read data into temporary buffer */
118  if ( ( rc = nvs_read ( nvs, address, read_data, len ) ) != 0 )
119  return rc;
120 
121  /* Compare data */
122  if ( memcmp ( data, read_data, len ) != 0 ) {
123  DBG ( "NVS %p verification failed at %#04x+%zd\n",
124  nvs, address, len );
125  return -EIO;
126  }
127 
128  return 0;
129 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint64_t address
Base address.
Definition: ena.h:24
unsigned char uint8_t
Definition: stdint.h:10
uint32_t len
Length.
Definition: ena.h:14
#define EIO
Input/output error.
Definition: errno.h:433
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
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98

References address, data, DBG, EIO, len, memcmp(), nvs_read(), and rc.

Referenced by nvs_write().

◆ nvs_write()

int nvs_write ( struct nvs_device nvs,
unsigned int  address,
const void *  data,
size_t  len 
)

Write to non-volatile storage device.

Parameters
nvsNVS device
addressAddress to which to write
dataData buffer
lenLength of data buffer
Return values
rcReturn status code

Definition at line 140 of file nvs.c.

141  {
142  size_t frag_len;
143  int rc;
144 
145  /* We don't even attempt to handle buffer lengths that aren't
146  * an integral number of words.
147  */
148  assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
149 
150  while ( len ) {
151 
152  /* Calculate length to write, stopping at block boundaries */
153  frag_len = nvs_frag_len ( nvs, address, len );
154 
155  /* Write this portion of the buffer to the device */
156  if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)
157  return rc;
158 
159  /* Read back and verify data */
160  if ( ( rc = nvs_verify ( nvs, address, data, frag_len ) ) != 0)
161  return rc;
162 
163  /* Update parameters */
164  data += frag_len;
165  address += ( frag_len >> nvs->word_len_log2 );
166  len -= frag_len;
167  }
168 
169  return 0;
170 }
int(* write)(struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
Write data to device.
Definition: nvs.h:59
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int word_len_log2
Word length.
Definition: nvs.h:22
uint64_t address
Base address.
Definition: ena.h:24
static size_t nvs_frag_len(struct nvs_device *nvs, unsigned int address, size_t max_len)
Calculate length up to next block boundary.
Definition: nvs.c:46
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint32_t len
Length.
Definition: ena.h:14
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
static int nvs_verify(struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
Verify content of non-volatile storage device.
Definition: nvs.c:112

References address, assert(), data, len, nvs_frag_len(), nvs_verify(), rc, nvs_device::word_len_log2, and nvs_device::write.

Referenced by nvo_save().