These changes are the raw update to qemu-2.6.
[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  * 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
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <ipxe/nvs.h>
31
32 /** @file
33  *
34  * Non-volatile storage
35  *
36  */
37
38 /**
39  * Calculate length up to next block boundary
40  *
41  * @v nvs               NVS device
42  * @v address           Starting address
43  * @v max_len           Maximum length
44  * @ret len             Length to use, stopping at block boundaries
45  */
46 static size_t nvs_frag_len ( struct nvs_device *nvs, unsigned int address,
47                              size_t max_len ) {
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 }
65
66 /**
67  * Read from non-volatile storage device
68  *
69  * @v nvs               NVS device
70  * @v address           Address from which to read
71  * @v data              Data buffer
72  * @v len               Length of data buffer
73  * @ret rc              Return status code
74  */
75 int nvs_read ( struct nvs_device *nvs, unsigned int address,
76                void *data, size_t len ) {
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 }
102
103 /**
104  * Verify content of non-volatile storage device
105  *
106  * @v nvs               NVS device
107  * @v address           Address from which to read
108  * @v data              Data to compare against
109  * @v len               Length of data buffer
110  * @ret rc              Return status code
111  */
112 static int nvs_verify ( struct nvs_device *nvs, unsigned int address,
113                         const void *data, size_t len ) {
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 }
130
131 /**
132  * Write to non-volatile storage device
133  *
134  * @v nvs               NVS device
135  * @v address           Address to which to write
136  * @v data              Data buffer
137  * @v len               Length of data buffer
138  * @ret rc              Return status code
139  */
140 int nvs_write ( struct nvs_device *nvs, unsigned int address,
141                 const void *data, size_t len ) {
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 }