iPXE
nvs.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2006 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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdint.h>
28#include <string.h>
29#include <errno.h>
30#include <assert.h>
31#include <ipxe/nvs.h>
32
33/** @file
34 *
35 * Non-volatile storage
36 *
37 */
38
39/**
40 * Calculate length up to next block boundary
41 *
42 * @v nvs NVS device
43 * @v address Starting address
44 * @v max_len Maximum length
45 * @ret len Length to use, stopping at block boundaries
46 */
47static size_t nvs_frag_len ( struct nvs_device *nvs, unsigned int address,
48 size_t max_len ) {
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}
66
67/**
68 * Read from non-volatile storage device
69 *
70 * @v nvs NVS device
71 * @v address Address from which to read
72 * @v data Data buffer
73 * @v len Length of data buffer
74 * @ret rc Return status code
75 */
76int nvs_read ( struct nvs_device *nvs, unsigned int address,
77 void *data, size_t len ) {
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}
103
104/**
105 * Verify content of non-volatile storage device
106 *
107 * @v nvs NVS device
108 * @v address Address from which to read
109 * @v data Data to compare against
110 * @v len Length of data buffer
111 * @ret rc Return status code
112 */
113static int nvs_verify ( struct nvs_device *nvs, unsigned int address,
114 const void *data, size_t len ) {
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}
131
132/**
133 * Write to non-volatile storage device
134 *
135 * @v nvs NVS device
136 * @v address Address to which to write
137 * @v data Data buffer
138 * @v len Length of data buffer
139 * @ret rc Return status code
140 */
141int nvs_write ( struct nvs_device *nvs, unsigned int address,
142 const void *data, size_t len ) {
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}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned char uint8_t
Definition stdint.h:10
Assertions.
#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
uint64_t address
Base address.
Definition ena.h:13
Error codes.
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define EIO
Input/output error.
Definition errno.h:434
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
String functions.
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 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 nvs_write(struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
Write to non-volatile storage device.
Definition nvs.c:141
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
Non-volatile storage.
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A non-volatile storage device.
Definition nvs.h:16
unsigned int block_size
Data block size (in words)
Definition nvs.h:37
unsigned int word_len_log2
Word length.
Definition nvs.h:23
int(* read)(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
Read data from device.
Definition nvs.h:48
int(* write)(struct nvs_device *nvs, unsigned int address, const void *data, size_t len)
Write data to device.
Definition nvs.h:60