2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
34 * Non-volatile storage
39 * Calculate length up to next block boundary
42 * @v address Starting address
43 * @v max_len Maximum length
44 * @ret len Length to use, stopping at block boundaries
46 static size_t nvs_frag_len ( struct nvs_device *nvs, unsigned int address,
50 /* If there are no block boundaries, return the maximum length */
51 if ( ! nvs->block_size )
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 );
59 /* Limit to maximum length */
60 if ( max_len < frag_len )
67 * Read from non-volatile storage device
70 * @v address Address from which to read
72 * @v len Length of data buffer
73 * @ret rc Return status code
75 int nvs_read ( struct nvs_device *nvs, unsigned int address,
76 void *data, size_t len ) {
80 /* We don't even attempt to handle buffer lengths that aren't
81 * an integral number of words.
83 assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
87 /* Calculate length to read, stopping at block boundaries */
88 frag_len = nvs_frag_len ( nvs, address, len );
90 /* Read this portion of the buffer from the device */
91 if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
94 /* Update parameters */
96 address += ( frag_len >> nvs->word_len_log2 );
104 * Verify content of non-volatile storage 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
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];
117 /* Read data into temporary buffer */
118 if ( ( rc = nvs_read ( nvs, address, read_data, len ) ) != 0 )
122 if ( memcmp ( data, read_data, len ) != 0 ) {
123 DBG ( "NVS %p verification failed at %#04x+%zd\n",
132 * Write to non-volatile storage 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
140 int nvs_write ( struct nvs_device *nvs, unsigned int address,
141 const void *data, size_t len ) {
145 /* We don't even attempt to handle buffer lengths that aren't
146 * an integral number of words.
148 assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
152 /* Calculate length to write, stopping at block boundaries */
153 frag_len = nvs_frag_len ( nvs, address, len );
155 /* Write this portion of the buffer to the device */
156 if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)
159 /* Read back and verify data */
160 if ( ( rc = nvs_verify ( nvs, address, data, frag_len ) ) != 0)
163 /* Update parameters */
165 address += ( frag_len >> nvs->word_len_log2 );