iPXE
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)
 FILE_SECBOOT (PERMITTED)
static size_t nvs_frag_len (struct nvs_device *nvs, unsigned int address, size_t max_len)
 Calculate length up to next block boundary.
int nvs_read (struct nvs_device *nvs, unsigned int address, void *data, size_t len)
 Read from non-volatile storage device.
static int nvs_verify (struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
 Verify content of non-volatile storage device.
int nvs_write (struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
 Write to non-volatile storage device.

Detailed Description

Non-volatile storage.

Definition in file nvs.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ nvs_frag_len()

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 47 of file nvs.c.

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

References address, nvs_device::block_size, 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 76 of file nvs.c.

77 {
78 size_t frag_len;
79 int rc;
80
81 /* We don't even attempt to handle buffer lengths that aren't
82 * an integral number of words.
83 */
84 assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
85
86 while ( len ) {
87
88 /* Calculate length to read, stopping at block boundaries */
89 frag_len = nvs_frag_len ( nvs, address, len );
90
91 /* Read this portion of the buffer from the device */
92 if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
93 return rc;
94
95 /* Update parameters */
96 data += frag_len;
97 address += ( frag_len >> nvs->word_len_log2 );
98 len -= frag_len;
99 }
100
101 return 0;
102}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#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
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:47
int(* read)(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read data from device.
Definition nvs.h:48

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()

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 113 of file nvs.c.

114 {
115 uint8_t read_data[len];
116 int rc;
117
118 /* Read data into temporary buffer */
119 if ( ( rc = nvs_read ( nvs, address, read_data, len ) ) != 0 )
120 return rc;
121
122 /* Compare data */
123 if ( memcmp ( data, read_data, len ) != 0 ) {
124 DBG ( "NVS %p verification failed at %#04x+%zd\n",
125 nvs, address, len );
126 return -EIO;
127 }
128
129 return 0;
130}
unsigned char uint8_t
Definition stdint.h:10
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define EIO
Input/output error.
Definition errno.h:434
int nvs_read(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read from non-volatile storage device.
Definition nvs.c:76
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115

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 141 of file nvs.c.

142 {
143 size_t frag_len;
144 int rc;
145
146 /* We don't even attempt to handle buffer lengths that aren't
147 * an integral number of words.
148 */
149 assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
150
151 while ( len ) {
152
153 /* Calculate length to write, stopping at block boundaries */
154 frag_len = nvs_frag_len ( nvs, address, len );
155
156 /* Write this portion of the buffer to the device */
157 if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)
158 return rc;
159
160 /* Read back and verify data */
161 if ( ( rc = nvs_verify ( nvs, address, data, frag_len ) ) != 0)
162 return rc;
163
164 /* Update parameters */
165 data += frag_len;
166 address += ( frag_len >> nvs->word_len_log2 );
167 len -= frag_len;
168 }
169
170 return 0;
171}
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:113
int(* write)(struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
Write data to device.
Definition nvs.h:60

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

Referenced by nvo_save().