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
20 FILE_LICENCE ( GPL2_OR_LATER );
30 * Non-volatile storage
35 * Calculate length up to next block boundary
38 * @v address Starting address
39 * @v max_len Maximum length
40 * @ret len Length to use, stopping at block boundaries
42 static size_t nvs_frag_len ( struct nvs_device *nvs, unsigned int address,
46 /* If there are no block boundaries, return the maximum length */
47 if ( ! nvs->block_size )
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 );
55 /* Limit to maximum length */
56 if ( max_len < frag_len )
63 * Read from non-volatile storage device
66 * @v address Address from which to read
68 * @v len Length of data buffer
69 * @ret rc Return status code
71 int nvs_read ( struct nvs_device *nvs, unsigned int address,
72 void *data, size_t len ) {
76 /* We don't even attempt to handle buffer lengths that aren't
77 * an integral number of words.
79 assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
83 /* Calculate length to read, stopping at block boundaries */
84 frag_len = nvs_frag_len ( nvs, address, len );
86 /* Read this portion of the buffer from the device */
87 if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
90 /* Update parameters */
92 address += ( frag_len >> nvs->word_len_log2 );
100 * Verify content of non-volatile storage 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
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];
113 /* Read data into temporary buffer */
114 if ( ( rc = nvs_read ( nvs, address, read_data, len ) ) != 0 )
118 if ( memcmp ( data, read_data, len ) != 0 ) {
119 DBG ( "NVS %p verification failed at %#04x+%zd\n",
128 * Write to non-volatile storage 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
136 int nvs_write ( struct nvs_device *nvs, unsigned int address,
137 const void *data, size_t len ) {
141 /* We don't even attempt to handle buffer lengths that aren't
142 * an integral number of words.
144 assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
148 /* Calculate length to write, stopping at block boundaries */
149 frag_len = nvs_frag_len ( nvs, address, len );
151 /* Write this portion of the buffer to the device */
152 if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)
155 /* Read back and verify data */
156 if ( ( rc = nvs_verify ( nvs, address, data, frag_len ) ) != 0)
159 /* Update parameters */
161 address += ( frag_len >> nvs->word_len_log2 );