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.
19 FILE_LICENCE ( GPL2_OR_LATER
);
29 * Non-volatile storage
34 * Read from non-volatile storage device
37 * @v address Address from which to read
39 * @v len Length of data buffer
40 * @ret rc Return status code
42 int nvs_read ( struct nvs_device
*nvs
, unsigned int address
,
43 void *data
, size_t len
) {
47 /* We don't even attempt to handle buffer lengths that aren't
48 * an integral number of words.
50 assert ( ( len
& ( ( 1 << nvs
->word_len_log2
) - 1 ) ) == 0 );
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 space remaining in buffer */
63 /* Read this portion of the buffer from the device */
64 if ( ( rc
= nvs
->read ( nvs
, address
, data
, frag_len
) ) != 0 )
67 /* Update parameters */
69 address
+= ( frag_len
>> nvs
->word_len_log2
);
77 * Verify content of non-volatile storage device
80 * @v address Address from which to read
81 * @v data Data to compare against
82 * @v len Length of data buffer
83 * @ret rc Return status code
85 static int nvs_verify ( struct nvs_device
*nvs
, unsigned int address
,
86 const void *data
, size_t len
) {
87 uint8_t read_data
[len
];
90 /* Read data into temporary buffer */
91 if ( ( rc
= nvs_read ( nvs
, address
, read_data
, len
) ) != 0 )
95 if ( memcmp ( data
, read_data
, len
) != 0 ) {
96 DBG ( "NVS %p verification failed at %#04x+%zd\n",
105 * Write to non-volatile storage device
108 * @v address Address to which to write
109 * @v data Data buffer
110 * @v len Length of data buffer
111 * @ret rc Return status code
113 int nvs_write ( struct nvs_device
*nvs
, unsigned int address
,
114 const void *data
, size_t len
) {
118 /* We don't even attempt to handle buffer lengths that aren't
119 * an integral number of words.
121 assert ( ( len
& ( ( 1 << nvs
->word_len_log2
) - 1 ) ) == 0 );
125 /* Calculate space remaining up to next block boundary */
126 frag_len
= ( ( nvs
->block_size
-
127 ( address
& ( nvs
->block_size
- 1 ) ) )
128 << nvs
->word_len_log2
);
130 /* Limit to space remaining in buffer */
131 if ( frag_len
> len
)
134 /* Write this portion of the buffer to the device */
135 if ( ( rc
= nvs
->write ( nvs
, address
, data
, frag_len
) ) != 0)
138 /* Read back and verify data */
139 if ( ( rc
= nvs_verify ( nvs
, address
, data
, frag_len
) ) != 0)
142 /* Update parameters */
144 address
+= ( frag_len
>> nvs
->word_len_log2
);