nvo.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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdint.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <gpxe/dhcp.h>
00026 #include <gpxe/nvs.h>
00027 #include <gpxe/nvo.h>
00028 
00029 /** @file
00030  *
00031  * Non-volatile stored options
00032  *
00033  */
00034 
00035 /**
00036  * Calculate checksum over non-volatile stored options
00037  *
00038  * @v nvo               Non-volatile options block
00039  * @ret sum             Checksum
00040  */
00041 static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
00042         uint8_t *data = nvo->data;
00043         uint8_t sum = 0;
00044         unsigned int i;
00045 
00046         for ( i = 0 ; i < nvo->total_len ; i++ ) {
00047                 sum += *(data++);
00048         }
00049         return sum;
00050 }
00051 
00052 /**
00053  * Load non-volatile stored options from non-volatile storage device
00054  *
00055  * @v nvo               Non-volatile options block
00056  * @ret rc              Return status code
00057  */
00058 static int nvo_load ( struct nvo_block *nvo ) {
00059         void *data = nvo->data;
00060         struct nvo_fragment *frag;
00061         int rc;
00062 
00063         /* Read data a fragment at a time */
00064         for ( frag = nvo->fragments ; frag->len ; frag++ ) {
00065                 if ( ( rc = nvs_read ( nvo->nvs, frag->address, data,
00066                                        frag->len ) ) != 0 ) {
00067                         DBGC ( nvo, "NVO %p could not read %zd bytes at "
00068                                "%#04x\n", nvo, frag->len, frag->address );
00069                         return rc;
00070                 }
00071                 data += frag->len;
00072         }
00073 
00074         DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
00075         return 0;
00076 }
00077 
00078 /**
00079  * Save non-volatile stored options back to non-volatile storage device
00080  *
00081  * @v nvo               Non-volatile options block
00082  * @ret rc              Return status code
00083  */
00084 static int nvo_save ( struct nvo_block *nvo ) {
00085         void *data = nvo->data;
00086         uint8_t *checksum = data;
00087         struct nvo_fragment *frag;
00088         int rc;
00089 
00090         /* Recalculate checksum */
00091         *checksum -= nvo_checksum ( nvo );
00092 
00093         /* Write data a fragment at a time */
00094         for ( frag = nvo->fragments ; frag->len ; frag++ ) {
00095                 if ( ( rc = nvs_write ( nvo->nvs, frag->address, data,
00096                                         frag->len ) ) != 0 ) {
00097                         DBGC ( nvo, "NVO %p could not write %zd bytes at "
00098                                "%#04x\n", nvo, frag->len, frag->address );
00099                         return rc;
00100                 }
00101                 data += frag->len;
00102         }
00103 
00104         DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
00105         return 0;
00106 }
00107 
00108 /**
00109  * Parse stored options
00110  *
00111  * @v nvo               Non-volatile options block
00112  *
00113  * Verifies that the options data is valid, and configures the DHCP
00114  * options block.  If the data is not valid, it is replaced with an
00115  * empty options block.
00116  */
00117 static void nvo_init_dhcpopts ( struct nvo_block *nvo ) {
00118         uint8_t *options_data;
00119         size_t options_len;
00120 
00121         /* Steal one byte for the checksum */
00122         options_data = ( nvo->data + 1 );
00123         options_len = ( nvo->total_len - 1 );
00124 
00125         /* If checksum fails, or options data starts with a zero,
00126          * assume the whole block is invalid.  This should capture the
00127          * case of random initial contents.
00128          */
00129         if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
00130                 DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
00131                        "assuming empty\n", nvo, nvo_checksum ( nvo ),
00132                        options_data[0] );
00133                 memset ( nvo->data, 0, nvo->total_len );
00134         }
00135 
00136         dhcpopt_init ( &nvo->dhcpopts, options_data, options_len );
00137 }
00138 
00139 /**
00140  * Store value of NVO setting
00141  *
00142  * @v settings          Settings block
00143  * @v setting           Setting to store
00144  * @v data              Setting data, or NULL to clear setting
00145  * @v len               Length of setting data
00146  * @ret rc              Return status code
00147  */
00148 static int nvo_store ( struct settings *settings, struct setting *setting,
00149                        const void *data, size_t len ) {
00150         struct nvo_block *nvo =
00151                 container_of ( settings, struct nvo_block, settings );
00152         int rc;
00153 
00154         /* Update stored options */
00155         if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
00156                                     data, len ) ) != 0 ) {
00157                 DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
00158                        nvo, len, strerror ( rc ) );
00159                 return rc;
00160         }
00161 
00162         /* Save updated options to NVS */
00163         if ( ( rc = nvo_save ( nvo ) ) != 0 )
00164                 return rc;
00165 
00166         return 0;
00167 }
00168 
00169 /**
00170  * Fetch value of NVO setting
00171  *
00172  * @v settings          Settings block
00173  * @v setting           Setting to fetch
00174  * @v data              Buffer to fill with setting data
00175  * @v len               Length of buffer
00176  * @ret len             Length of setting data, or negative error
00177  *
00178  * The actual length of the setting will be returned even if
00179  * the buffer was too small.
00180  */
00181 static int nvo_fetch ( struct settings *settings, struct setting *setting,
00182                        void *data, size_t len ) {
00183         struct nvo_block *nvo =
00184                 container_of ( settings, struct nvo_block, settings );
00185 
00186         return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
00187 }
00188 
00189 /** NVO settings operations */
00190 static struct settings_operations nvo_settings_operations = {
00191         .store = nvo_store,
00192         .fetch = nvo_fetch,
00193 };
00194 
00195 /**
00196  * Initialise non-volatile stored options
00197  *
00198  * @v nvo               Non-volatile options block
00199  * @v nvs               Underlying non-volatile storage device
00200  * @v fragments         List of option-containing fragments
00201  * @v refcnt            Containing object reference counter, or NULL
00202  */
00203 void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
00204                 struct nvo_fragment *fragments, struct refcnt *refcnt ) {
00205         nvo->nvs = nvs;
00206         nvo->fragments = fragments;
00207         settings_init ( &nvo->settings, &nvo_settings_operations, refcnt,
00208                         "nvo", 0 );
00209 }
00210 
00211 /**
00212  * Register non-volatile stored options
00213  *
00214  * @v nvo               Non-volatile options block
00215  * @v parent            Parent settings block, or NULL
00216  * @ret rc              Return status code
00217  */
00218 int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
00219         struct nvo_fragment *fragment = nvo->fragments;
00220         int rc;
00221 
00222         /* Calculate total length of all fragments */
00223         for ( fragment = nvo->fragments ; fragment->len ; fragment++ )
00224                 nvo->total_len += fragment->len;
00225 
00226         /* Allocate memory for options and read in from NVS */
00227         nvo->data = malloc ( nvo->total_len );
00228         if ( ! nvo->data ) {
00229                 DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
00230                        nvo, nvo->total_len );
00231                 rc = -ENOMEM;
00232                 goto err_malloc;
00233         }
00234         if ( ( rc = nvo_load ( nvo ) ) != 0 )
00235                 goto err_load;
00236 
00237         /* Verify and register options */
00238         nvo_init_dhcpopts ( nvo );
00239         if ( ( rc = register_settings ( &nvo->settings, parent ) ) != 0 )
00240                 goto err_register;
00241 
00242         DBGC ( nvo, "NVO %p registered\n", nvo );
00243         return 0;
00244         
00245  err_register:
00246  err_load:
00247         free ( nvo->data );
00248         nvo->data = NULL;
00249  err_malloc:
00250         return rc;
00251 }
00252 
00253 /**
00254  * Unregister non-volatile stored options
00255  *
00256  * @v nvo               Non-volatile options block
00257  */
00258 void unregister_nvo ( struct nvo_block *nvo ) {
00259         unregister_settings ( &nvo->settings );
00260         free ( nvo->data );
00261         nvo->data = NULL;
00262         DBGC ( nvo, "NVO %p unregistered\n", nvo );
00263 }

Generated on Tue Apr 6 20:00:51 2010 for gPXE by  doxygen 1.5.7.1