2006-12-04 18:23:06 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <gpxe/dhcp.h>
|
|
|
|
#include <gpxe/nvs.h>
|
|
|
|
#include <gpxe/nvo.h>
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Non-volatile stored options
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-12-04 23:40:35 +00:00
|
|
|
#warning "Temporary hack"
|
|
|
|
struct nvo_block *ugly_nvo_hack = NULL;
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/**
|
2006-12-04 22:04:10 +00:00
|
|
|
* Calculate checksum over non-volatile stored options
|
2006-12-04 21:46:13 +00:00
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
2006-12-04 22:04:10 +00:00
|
|
|
* @ret sum Checksum
|
2006-12-04 21:46:13 +00:00
|
|
|
*/
|
2006-12-04 22:04:10 +00:00
|
|
|
static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
|
|
|
|
uint8_t *data = nvo->options->data;
|
|
|
|
uint8_t sum = 0;
|
|
|
|
unsigned int i;
|
2006-12-04 21:46:13 +00:00
|
|
|
|
2006-12-04 22:04:10 +00:00
|
|
|
for ( i = 0 ; i < nvo->total_len ; i++ ) {
|
|
|
|
sum += *(data++);
|
2006-12-04 21:46:13 +00:00
|
|
|
}
|
2006-12-04 22:04:10 +00:00
|
|
|
return sum;
|
2006-12-04 21:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-12-04 22:04:10 +00:00
|
|
|
* Load non-volatile stored options from non-volatile storage device
|
2006-12-04 21:46:13 +00:00
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2006-12-04 22:04:10 +00:00
|
|
|
static int nvo_load ( struct nvo_block *nvo ) {
|
2006-12-04 21:46:13 +00:00
|
|
|
void *data = nvo->options->data;
|
2006-12-04 22:04:10 +00:00
|
|
|
struct nvo_fragment *fragment;
|
2006-12-04 21:46:13 +00:00
|
|
|
int rc;
|
|
|
|
|
2006-12-04 22:04:10 +00:00
|
|
|
/* Read data a fragment at a time */
|
|
|
|
for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
|
2006-12-04 21:46:13 +00:00
|
|
|
if ( ( rc = nvs_read ( nvo->nvs, fragment->address,
|
|
|
|
data, fragment->len ) ) != 0 ) {
|
|
|
|
DBG ( "NVO %p could not read %zd bytes at %#04x\n",
|
|
|
|
nvo, fragment->len, fragment->address );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
data += fragment->len;
|
|
|
|
}
|
2006-12-04 23:47:44 +00:00
|
|
|
|
|
|
|
DBG ( "NVO %p loaded from non-volatile storage\n", nvo );
|
2006-12-04 21:46:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-04 22:04:10 +00:00
|
|
|
/**
|
|
|
|
* Save non-volatile stored options back to non-volatile storage device
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int nvo_save ( struct nvo_block *nvo ) {
|
|
|
|
void *data = nvo->options->data;
|
|
|
|
uint8_t *checksum = ( data + nvo->total_len - 1 );
|
|
|
|
struct nvo_fragment *fragment;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Recalculate checksum */
|
2006-12-04 23:40:35 +00:00
|
|
|
*checksum -= nvo_checksum ( nvo );
|
2006-12-04 22:04:10 +00:00
|
|
|
|
|
|
|
/* Write data a fragment at a time */
|
|
|
|
for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
|
|
|
|
if ( ( rc = nvs_write ( nvo->nvs, fragment->address,
|
|
|
|
data, fragment->len ) ) != 0 ) {
|
|
|
|
DBG ( "NVO %p could not write %zd bytes at %#04x\n",
|
|
|
|
nvo, fragment->len, fragment->address );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
data += fragment->len;
|
|
|
|
}
|
2006-12-04 23:47:44 +00:00
|
|
|
|
|
|
|
DBG ( "NVO %p saved to non-volatile storage\n", nvo );
|
2006-12-04 22:04:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/**
|
|
|
|
* Parse stored options
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
*
|
|
|
|
* Verifies that the options data is valid, and configures the DHCP
|
|
|
|
* options block. If the data is not valid, it is replaced with an
|
|
|
|
* empty options block.
|
|
|
|
*/
|
2006-12-04 22:04:10 +00:00
|
|
|
static void nvo_init_dhcp ( struct nvo_block *nvo ) {
|
2006-12-04 21:46:13 +00:00
|
|
|
struct dhcp_option_block *options = nvo->options;
|
2006-12-04 18:23:06 +00:00
|
|
|
struct dhcp_option *option;
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/* Steal one byte for the checksum */
|
2006-12-04 22:04:10 +00:00
|
|
|
options->max_len = ( nvo->total_len - 1 );
|
2006-12-04 21:46:13 +00:00
|
|
|
|
|
|
|
/* Verify checksum over whole block */
|
2006-12-04 22:04:10 +00:00
|
|
|
if ( nvo_checksum ( nvo ) != 0 ) {
|
2006-12-04 18:23:06 +00:00
|
|
|
DBG ( "NVO %p has bad checksum %02x; assuming empty\n",
|
2006-12-04 22:04:10 +00:00
|
|
|
nvo, nvo_checksum ( nvo ) );
|
2006-12-04 21:46:13 +00:00
|
|
|
goto empty;
|
2006-12-04 18:23:06 +00:00
|
|
|
}
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/* Check that we don't just have a block full of zeroes */
|
|
|
|
option = options->data;
|
2006-12-04 18:23:06 +00:00
|
|
|
if ( option->tag == DHCP_PAD ) {
|
|
|
|
DBG ( "NVO %p has bad start; assuming empty\n", nvo );
|
2006-12-04 21:46:13 +00:00
|
|
|
goto empty;
|
2006-12-04 18:23:06 +00:00
|
|
|
}
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/* Search for the DHCP_END tag */
|
|
|
|
options->len = options->max_len;
|
|
|
|
option = find_dhcp_option ( options, DHCP_END );
|
2006-12-04 18:23:06 +00:00
|
|
|
if ( ! option ) {
|
|
|
|
DBG ( "NVO %p has no end tag; assuming empty\n", nvo );
|
2006-12-04 21:46:13 +00:00
|
|
|
goto empty;
|
2006-12-04 18:23:06 +00:00
|
|
|
}
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/* Set correct length of DHCP options */
|
|
|
|
options->len = ( ( void * ) option - options->data + 1 );
|
2006-12-04 18:23:06 +00:00
|
|
|
DBG ( "NVO %p contains %zd bytes of options (maximum %zd)\n",
|
2006-12-04 21:46:13 +00:00
|
|
|
nvo, options->len, options->max_len );
|
|
|
|
return;
|
2006-12-04 18:23:06 +00:00
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
empty:
|
|
|
|
/* No options found; initialise an empty options block */
|
|
|
|
option = options->data;
|
|
|
|
option->tag = DHCP_END;
|
|
|
|
options->len = 1;
|
|
|
|
return;
|
2006-12-04 18:23:06 +00:00
|
|
|
}
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/**
|
|
|
|
* Register non-volatile stored options
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int nvo_register ( struct nvo_block *nvo ) {
|
2006-12-04 22:04:10 +00:00
|
|
|
struct nvo_fragment *fragment = nvo->fragments;
|
2006-12-04 18:23:06 +00:00
|
|
|
int rc;
|
|
|
|
|
2006-12-04 22:04:10 +00:00
|
|
|
/* Calculate total length of all fragments */
|
|
|
|
nvo->total_len = 0;
|
|
|
|
for ( fragment = nvo->fragments ; fragment->len ; fragment++ ) {
|
|
|
|
nvo->total_len += fragment->len;
|
|
|
|
}
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/* Allocate memory for options and read in from NVS */
|
2006-12-04 22:04:10 +00:00
|
|
|
nvo->options = alloc_dhcp_options ( nvo->total_len );
|
2006-12-04 18:23:06 +00:00
|
|
|
if ( ! nvo->options ) {
|
|
|
|
DBG ( "NVO %p could not allocate %zd bytes\n",
|
2006-12-04 22:04:10 +00:00
|
|
|
nvo, nvo->total_len );
|
2006-12-04 18:23:06 +00:00
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
2006-12-04 22:04:10 +00:00
|
|
|
if ( ( rc = nvo_load ( nvo ) ) != 0 )
|
2006-12-04 18:23:06 +00:00
|
|
|
goto err;
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/* Verify and register options */
|
2006-12-04 22:04:10 +00:00
|
|
|
nvo_init_dhcp ( nvo );
|
2006-12-04 18:23:06 +00:00
|
|
|
register_dhcp_options ( nvo->options );
|
|
|
|
|
2006-12-04 23:40:35 +00:00
|
|
|
ugly_nvo_hack = nvo;
|
|
|
|
|
2006-12-04 23:47:44 +00:00
|
|
|
DBG ( "NVO %p registered\n", nvo );
|
2006-12-04 18:23:06 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
free_dhcp_options ( nvo->options );
|
|
|
|
nvo->options = NULL;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2006-12-04 21:46:13 +00:00
|
|
|
/**
|
|
|
|
* Unregister non-volatile stored options
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
*/
|
|
|
|
void nvo_unregister ( struct nvo_block *nvo ) {
|
2006-12-04 23:47:44 +00:00
|
|
|
|
2006-12-04 18:23:06 +00:00
|
|
|
if ( nvo->options ) {
|
|
|
|
unregister_dhcp_options ( nvo->options );
|
|
|
|
free_dhcp_options ( nvo->options );
|
|
|
|
nvo->options = NULL;
|
|
|
|
}
|
2006-12-04 23:40:35 +00:00
|
|
|
|
2006-12-04 23:47:44 +00:00
|
|
|
DBG ( "NVO %p unregistered\n", nvo );
|
|
|
|
|
2006-12-04 23:40:35 +00:00
|
|
|
ugly_nvo_hack = NULL;
|
2006-12-04 18:23:06 +00:00
|
|
|
}
|