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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Non-volatile storage
32 * Read from non-volatile storage device
35 * @v address Address from which to read
37 * @v len Length of data buffer
38 * @ret rc Return status code
40 int nvs_read ( struct nvs_device *nvs, unsigned int address,
41 void *data, size_t len ) {
45 /* We don't even attempt to handle buffer lengths that aren't
46 * an integral number of words.
48 assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
52 /* Calculate space remaining up to next block boundary */
53 frag_len = ( ( nvs->block_size -
54 ( address & ( nvs->block_size - 1 ) ) )
55 << nvs->word_len_log2 );
57 /* Limit to space remaining in buffer */
61 /* Read this portion of the buffer from the device */
62 if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
65 /* Update parameters */
67 address += ( frag_len >> nvs->word_len_log2 );
75 * Verify content of non-volatile storage device
78 * @v address Address from which to read
79 * @v data Data to compare against
80 * @v len Length of data buffer
81 * @ret rc Return status code
83 static int nvs_verify ( struct nvs_device *nvs, unsigned int address,
84 const void *data, size_t len ) {
85 uint8_t read_data[len];
88 /* Read data into temporary buffer */
89 if ( ( rc = nvs_read ( nvs, address, read_data, len ) ) != 0 )
93 if ( memcmp ( data, read_data, len ) != 0 ) {
94 DBG ( "NVS %p verification failed at %#04x+%d\n",
103 * Write to non-volatile storage device
106 * @v address Address to which to write
107 * @v data Data buffer
108 * @v len Length of data buffer
109 * @ret rc Return status code
111 int nvs_write ( struct nvs_device *nvs, unsigned int address,
112 const void *data, size_t len ) {
116 /* We don't even attempt to handle buffer lengths that aren't
117 * an integral number of words.
119 assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
123 /* Calculate space remaining up to next block boundary */
124 frag_len = ( ( nvs->block_size -
125 ( address & ( nvs->block_size - 1 ) ) )
126 << nvs->word_len_log2 );
128 /* Limit to space remaining in buffer */
129 if ( frag_len > len )
132 /* Write this portion of the buffer to the device */
133 if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)
136 /* Read back and verify data */
137 if ( ( rc = nvs_verify ( nvs, address, data, frag_len ) ) != 0)
140 /* Update parameters */
142 address += ( frag_len >> nvs->word_len_log2 );