Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / drivers / nvs / nvs.c
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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <ipxe/nvs.h>
27
28 /** @file
29  *
30  * Non-volatile storage
31  *
32  */
33
34 /**
35  * Calculate length up to next block boundary
36  *
37  * @v nvs               NVS device
38  * @v address           Starting address
39  * @v max_len           Maximum length
40  * @ret len             Length to use, stopping at block boundaries
41  */
42 static size_t nvs_frag_len ( struct nvs_device *nvs, unsigned int address,
43                              size_t max_len ) {
44         size_t frag_len;
45
46         /* If there are no block boundaries, return the maximum length */
47         if ( ! nvs->block_size )
48                 return max_len;
49
50         /* Calculate space remaining up to next block boundary */
51         frag_len = ( ( nvs->block_size -
52                        ( address & ( nvs->block_size - 1 ) ) )
53                      << nvs->word_len_log2 );
54
55         /* Limit to maximum length */
56         if ( max_len < frag_len )
57                 return max_len;
58
59         return frag_len;
60 }
61
62 /**
63  * Read from non-volatile storage device
64  *
65  * @v nvs               NVS device
66  * @v address           Address from which to read
67  * @v data              Data buffer
68  * @v len               Length of data buffer
69  * @ret rc              Return status code
70  */
71 int nvs_read ( struct nvs_device *nvs, unsigned int address,
72                void *data, size_t len ) {
73         size_t frag_len;
74         int rc;
75
76         /* We don't even attempt to handle buffer lengths that aren't
77          * an integral number of words.
78          */
79         assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
80
81         while ( len ) {
82
83                 /* Calculate length to read, stopping at block boundaries */
84                 frag_len = nvs_frag_len ( nvs, address, len );
85
86                 /* Read this portion of the buffer from the device */
87                 if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
88                         return rc;
89
90                 /* Update parameters */
91                 data += frag_len;
92                 address += ( frag_len >> nvs->word_len_log2 );
93                 len -= frag_len;
94         }
95
96         return 0;
97 }
98
99 /**
100  * Verify content of non-volatile storage device
101  *
102  * @v nvs               NVS device
103  * @v address           Address from which to read
104  * @v data              Data to compare against
105  * @v len               Length of data buffer
106  * @ret rc              Return status code
107  */
108 static int nvs_verify ( struct nvs_device *nvs, unsigned int address,
109                         const void *data, size_t len ) {
110         uint8_t read_data[len];
111         int rc;
112
113         /* Read data into temporary buffer */
114         if ( ( rc = nvs_read ( nvs, address, read_data, len ) ) != 0 )
115                 return rc;
116
117         /* Compare data */
118         if ( memcmp ( data, read_data, len ) != 0 ) {
119                 DBG ( "NVS %p verification failed at %#04x+%zd\n",
120                       nvs, address, len );
121                 return -EIO;
122         }
123
124         return 0;
125 }
126
127 /**
128  * Write to non-volatile storage device
129  *
130  * @v nvs               NVS device
131  * @v address           Address to which to write
132  * @v data              Data buffer
133  * @v len               Length of data buffer
134  * @ret rc              Return status code
135  */
136 int nvs_write ( struct nvs_device *nvs, unsigned int address,
137                 const void *data, size_t len ) {
138         size_t frag_len;
139         int rc;
140
141         /* We don't even attempt to handle buffer lengths that aren't
142          * an integral number of words.
143          */
144         assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
145
146         while ( len ) {
147
148                 /* Calculate length to write, stopping at block boundaries */
149                 frag_len = nvs_frag_len ( nvs, address, len );
150
151                 /* Write this portion of the buffer to the device */
152                 if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)
153                         return rc;
154
155                 /* Read back and verify data */
156                 if ( ( rc = nvs_verify ( nvs, address, data, frag_len ) ) != 0)
157                         return rc;
158
159                 /* Update parameters */
160                 data += frag_len;
161                 address += ( frag_len >> nvs->word_len_log2 );
162                 len -= frag_len;
163         }
164
165         return 0;
166 }