iPXE
nvs.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <assert.h>
00030 #include <ipxe/nvs.h>
00031 
00032 /** @file
00033  *
00034  * Non-volatile storage
00035  *
00036  */
00037 
00038 /**
00039  * Calculate length up to next block boundary
00040  *
00041  * @v nvs               NVS device
00042  * @v address           Starting address
00043  * @v max_len           Maximum length
00044  * @ret len             Length to use, stopping at block boundaries
00045  */
00046 static size_t nvs_frag_len ( struct nvs_device *nvs, unsigned int address,
00047                              size_t max_len ) {
00048         size_t frag_len;
00049 
00050         /* If there are no block boundaries, return the maximum length */
00051         if ( ! nvs->block_size )
00052                 return max_len;
00053 
00054         /* Calculate space remaining up to next block boundary */
00055         frag_len = ( ( nvs->block_size -
00056                        ( address & ( nvs->block_size - 1 ) ) )
00057                      << nvs->word_len_log2 );
00058 
00059         /* Limit to maximum length */
00060         if ( max_len < frag_len )
00061                 return max_len;
00062 
00063         return frag_len;
00064 }
00065 
00066 /**
00067  * Read from non-volatile storage device
00068  *
00069  * @v nvs               NVS device
00070  * @v address           Address from which to read
00071  * @v data              Data buffer
00072  * @v len               Length of data buffer
00073  * @ret rc              Return status code
00074  */
00075 int nvs_read ( struct nvs_device *nvs, unsigned int address,
00076                void *data, size_t len ) {
00077         size_t frag_len;
00078         int rc;
00079 
00080         /* We don't even attempt to handle buffer lengths that aren't
00081          * an integral number of words.
00082          */
00083         assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
00084 
00085         while ( len ) {
00086 
00087                 /* Calculate length to read, stopping at block boundaries */
00088                 frag_len = nvs_frag_len ( nvs, address, len );
00089 
00090                 /* Read this portion of the buffer from the device */
00091                 if ( ( rc = nvs->read ( nvs, address, data, frag_len ) ) != 0 )
00092                         return rc;
00093 
00094                 /* Update parameters */
00095                 data += frag_len;
00096                 address += ( frag_len >> nvs->word_len_log2 );
00097                 len -= frag_len;
00098         }
00099 
00100         return 0;
00101 }
00102 
00103 /**
00104  * Verify content of non-volatile storage device
00105  *
00106  * @v nvs               NVS device
00107  * @v address           Address from which to read
00108  * @v data              Data to compare against
00109  * @v len               Length of data buffer
00110  * @ret rc              Return status code
00111  */
00112 static int nvs_verify ( struct nvs_device *nvs, unsigned int address,
00113                         const void *data, size_t len ) {
00114         uint8_t read_data[len];
00115         int rc;
00116 
00117         /* Read data into temporary buffer */
00118         if ( ( rc = nvs_read ( nvs, address, read_data, len ) ) != 0 )
00119                 return rc;
00120 
00121         /* Compare data */
00122         if ( memcmp ( data, read_data, len ) != 0 ) {
00123                 DBG ( "NVS %p verification failed at %#04x+%zd\n",
00124                       nvs, address, len );
00125                 return -EIO;
00126         }
00127 
00128         return 0;
00129 }
00130 
00131 /**
00132  * Write to non-volatile storage device
00133  *
00134  * @v nvs               NVS device
00135  * @v address           Address to which to write
00136  * @v data              Data buffer
00137  * @v len               Length of data buffer
00138  * @ret rc              Return status code
00139  */
00140 int nvs_write ( struct nvs_device *nvs, unsigned int address,
00141                 const void *data, size_t len ) {
00142         size_t frag_len;
00143         int rc;
00144 
00145         /* We don't even attempt to handle buffer lengths that aren't
00146          * an integral number of words.
00147          */
00148         assert ( ( len & ( ( 1 << nvs->word_len_log2 ) - 1 ) ) == 0 );
00149 
00150         while ( len ) {
00151 
00152                 /* Calculate length to write, stopping at block boundaries */
00153                 frag_len = nvs_frag_len ( nvs, address, len );
00154 
00155                 /* Write this portion of the buffer to the device */
00156                 if ( ( rc = nvs->write ( nvs, address, data, frag_len ) ) != 0)
00157                         return rc;
00158 
00159                 /* Read back and verify data */
00160                 if ( ( rc = nvs_verify ( nvs, address, data, frag_len ) ) != 0)
00161                         return rc;
00162 
00163                 /* Update parameters */
00164                 data += frag_len;
00165                 address += ( frag_len >> nvs->word_len_log2 );
00166                 len -= frag_len;
00167         }
00168 
00169         return 0;
00170 }