2006-12-04 19:23:06 +01: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.
|
|
|
|
*/
|
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2006-12-04 19:23:06 +01:00
|
|
|
#include <stdint.h>
|
2008-03-21 23:15:31 +01:00
|
|
|
#include <stdlib.h>
|
2006-12-04 19:23:06 +01:00
|
|
|
#include <string.h>
|
2007-01-15 09:44:05 +01:00
|
|
|
#include <errno.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/dhcp.h>
|
|
|
|
#include <ipxe/nvs.h>
|
|
|
|
#include <ipxe/nvo.h>
|
2006-12-04 19:23:06 +01:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Non-volatile stored options
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-12-04 22:46:13 +01:00
|
|
|
/**
|
2006-12-04 23:04:10 +01:00
|
|
|
* Calculate checksum over non-volatile stored options
|
2006-12-04 22:46:13 +01:00
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
2006-12-04 23:04:10 +01:00
|
|
|
* @ret sum Checksum
|
2006-12-04 22:46:13 +01:00
|
|
|
*/
|
2006-12-04 23:04:10 +01:00
|
|
|
static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
|
2008-03-21 23:15:31 +01:00
|
|
|
uint8_t *data = nvo->data;
|
2006-12-04 23:04:10 +01:00
|
|
|
uint8_t sum = 0;
|
|
|
|
unsigned int i;
|
2006-12-04 22:46:13 +01:00
|
|
|
|
2010-11-30 02:10:38 +01:00
|
|
|
for ( i = 0 ; i < nvo->len ; i++ ) {
|
2006-12-04 23:04:10 +01:00
|
|
|
sum += *(data++);
|
2006-12-04 22:46:13 +01:00
|
|
|
}
|
2006-12-04 23:04:10 +01:00
|
|
|
return sum;
|
2006-12-04 22:46:13 +01:00
|
|
|
}
|
|
|
|
|
2011-01-11 01:53:50 +01:00
|
|
|
/**
|
|
|
|
* Reallocate non-volatile stored options block
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
* @v len New length
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int nvo_realloc ( struct nvo_block *nvo, size_t len ) {
|
|
|
|
void *new_data;
|
|
|
|
|
|
|
|
/* Reallocate data */
|
|
|
|
new_data = realloc ( nvo->data, len );
|
|
|
|
if ( ! new_data ) {
|
|
|
|
DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
|
|
|
|
nvo, len );
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
nvo->data = new_data;
|
|
|
|
nvo->len = len;
|
|
|
|
|
|
|
|
/* Update DHCP option block */
|
|
|
|
if ( len ) {
|
|
|
|
nvo->dhcpopts.data = ( nvo->data + 1 /* checksum */ );
|
|
|
|
nvo->dhcpopts.alloc_len = ( len - 1 /* checksum */ );
|
|
|
|
} else {
|
|
|
|
nvo->dhcpopts.data = NULL;
|
|
|
|
nvo->dhcpopts.used_len = 0;
|
|
|
|
nvo->dhcpopts.alloc_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reallocate non-volatile stored options DHCP option block
|
|
|
|
*
|
|
|
|
* @v options DHCP option block
|
|
|
|
* @v len New length
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int nvo_realloc_dhcpopt ( struct dhcp_options *options, size_t len ) {
|
|
|
|
struct nvo_block *nvo =
|
|
|
|
container_of ( options, struct nvo_block, dhcpopts );
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Refuse to reallocate if we have no way to resize the block */
|
|
|
|
if ( ! nvo->resize )
|
|
|
|
return dhcpopt_no_realloc ( options, len );
|
|
|
|
|
|
|
|
/* Allow one byte for the checksum (if any data is present) */
|
|
|
|
if ( len )
|
|
|
|
len += 1;
|
|
|
|
|
|
|
|
/* Resize underlying non-volatile options block */
|
|
|
|
if ( ( rc = nvo->resize ( nvo, len ) ) != 0 ) {
|
|
|
|
DBGC ( nvo, "NVO %p could not resize to %zd bytes: %s\n",
|
|
|
|
nvo, len, strerror ( rc ) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reallocate in-memory options block */
|
|
|
|
if ( ( rc = nvo_realloc ( nvo, len ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-04 22:46:13 +01:00
|
|
|
/**
|
2006-12-04 23:04:10 +01:00
|
|
|
* Load non-volatile stored options from non-volatile storage device
|
2006-12-04 22:46:13 +01:00
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2006-12-04 23:04:10 +01:00
|
|
|
static int nvo_load ( struct nvo_block *nvo ) {
|
2011-01-11 01:53:50 +01:00
|
|
|
uint8_t *options_data = nvo->dhcpopts.data;
|
2006-12-04 22:46:13 +01:00
|
|
|
int rc;
|
|
|
|
|
2011-01-11 01:53:50 +01:00
|
|
|
/* Skip reading zero-length NVO fields */
|
|
|
|
if ( nvo->len == 0 ) {
|
|
|
|
DBGC ( nvo, "NVO %p is empty; skipping load\n", nvo );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-30 02:10:38 +01:00
|
|
|
/* Read data */
|
|
|
|
if ( ( rc = nvs_read ( nvo->nvs, nvo->address, nvo->data,
|
|
|
|
nvo->len ) ) != 0 ) {
|
|
|
|
DBGC ( nvo, "NVO %p could not read %zd bytes at %#04x: %s\n",
|
|
|
|
nvo, nvo->len, nvo->address, strerror ( rc ) );
|
|
|
|
return rc;
|
2006-12-04 22:46:13 +01:00
|
|
|
}
|
2006-12-05 00:47:44 +01:00
|
|
|
|
2011-01-11 01:53:50 +01:00
|
|
|
/* If checksum fails, or options data starts with a zero,
|
|
|
|
* assume the whole block is invalid. This should capture the
|
|
|
|
* case of random initial contents.
|
|
|
|
*/
|
|
|
|
if ( ( nvo_checksum ( nvo ) != 0 ) || ( options_data[0] == 0 ) ) {
|
|
|
|
DBGC ( nvo, "NVO %p has checksum %02x and initial byte %02x; "
|
|
|
|
"assuming empty\n", nvo, nvo_checksum ( nvo ),
|
|
|
|
options_data[0] );
|
|
|
|
memset ( nvo->data, 0, nvo->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Rescan DHCP option block */
|
|
|
|
dhcpopt_update_used_len ( &nvo->dhcpopts );
|
|
|
|
|
2008-03-21 23:15:31 +01:00
|
|
|
DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
|
2006-12-04 22:46:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-04 23:04:10 +01:00
|
|
|
/**
|
|
|
|
* Save non-volatile stored options back to non-volatile storage device
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2008-03-21 23:15:31 +01:00
|
|
|
static int nvo_save ( struct nvo_block *nvo ) {
|
2010-11-30 02:10:38 +01:00
|
|
|
uint8_t *checksum = nvo->data;
|
2006-12-04 23:04:10 +01:00
|
|
|
int rc;
|
|
|
|
|
2011-01-11 01:53:50 +01:00
|
|
|
/* Recalculate checksum, if applicable */
|
|
|
|
if ( nvo->len > 0 )
|
|
|
|
*checksum -= nvo_checksum ( nvo );
|
2006-12-04 23:04:10 +01:00
|
|
|
|
2010-11-30 02:10:38 +01:00
|
|
|
/* Write data */
|
|
|
|
if ( ( rc = nvs_write ( nvo->nvs, nvo->address, nvo->data,
|
|
|
|
nvo->len ) ) != 0 ) {
|
|
|
|
DBGC ( nvo, "NVO %p could not write %zd bytes at %#04x: %s\n",
|
|
|
|
nvo, nvo->len, nvo->address, strerror ( rc ) );
|
|
|
|
return rc;
|
2006-12-04 23:04:10 +01:00
|
|
|
}
|
2006-12-05 00:47:44 +01:00
|
|
|
|
2008-03-21 23:15:31 +01:00
|
|
|
DBGC ( nvo, "NVO %p saved to non-volatile storage\n", nvo );
|
2006-12-04 23:04:10 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-22 17:56:35 +01:00
|
|
|
/**
|
|
|
|
* Check applicability of NVO setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
|
|
|
* @v setting Setting
|
|
|
|
* @ret applies Setting applies within this settings block
|
|
|
|
*/
|
|
|
|
static int nvo_applies ( struct settings *settings __unused,
|
|
|
|
struct setting *setting ) {
|
|
|
|
|
|
|
|
return dhcpopt_applies ( setting->tag );
|
|
|
|
}
|
|
|
|
|
2008-03-21 23:15:31 +01:00
|
|
|
/**
|
|
|
|
* Store value of NVO setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-21 23:15:31 +01:00
|
|
|
* @v data Setting data, or NULL to clear setting
|
|
|
|
* @v len Length of setting data
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int nvo_store ( struct settings *settings, struct setting *setting,
|
2008-03-21 23:15:31 +01:00
|
|
|
const void *data, size_t len ) {
|
|
|
|
struct nvo_block *nvo =
|
|
|
|
container_of ( settings, struct nvo_block, settings );
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Update stored options */
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( rc = dhcpopt_store ( &nvo->dhcpopts, setting->tag,
|
|
|
|
data, len ) ) != 0 ) {
|
2008-03-21 23:15:31 +01:00
|
|
|
DBGC ( nvo, "NVO %p could not store %zd bytes: %s\n",
|
|
|
|
nvo, len, strerror ( rc ) );
|
|
|
|
return rc;
|
2006-12-04 19:23:06 +01:00
|
|
|
}
|
|
|
|
|
2008-03-21 23:15:31 +01:00
|
|
|
/* Save updated options to NVS */
|
|
|
|
if ( ( rc = nvo_save ( nvo ) ) != 0 )
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch value of NVO setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-21 23:15:31 +01:00
|
|
|
* @v data Buffer to fill with setting data
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of setting data, or negative error
|
|
|
|
*
|
|
|
|
* The actual length of the setting will be returned even if
|
|
|
|
* the buffer was too small.
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int nvo_fetch ( struct settings *settings, struct setting *setting,
|
2008-03-21 23:15:31 +01:00
|
|
|
void *data, size_t len ) {
|
|
|
|
struct nvo_block *nvo =
|
|
|
|
container_of ( settings, struct nvo_block, settings );
|
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
return dhcpopt_fetch ( &nvo->dhcpopts, setting->tag, data, len );
|
2008-03-21 23:15:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** NVO settings operations */
|
|
|
|
static struct settings_operations nvo_settings_operations = {
|
2011-03-22 17:56:35 +01:00
|
|
|
.applies = nvo_applies,
|
2008-03-21 23:15:31 +01:00
|
|
|
.store = nvo_store,
|
|
|
|
.fetch = nvo_fetch,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise non-volatile stored options
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
* @v nvs Underlying non-volatile storage device
|
2010-11-30 02:10:38 +01:00
|
|
|
* @v address Address within NVS device
|
|
|
|
* @v len Length of non-volatile options data
|
2011-01-11 01:53:50 +01:00
|
|
|
* @v resize Resize method
|
2008-03-21 23:15:31 +01:00
|
|
|
* @v refcnt Containing object reference counter, or NULL
|
|
|
|
*/
|
|
|
|
void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
|
2011-01-11 01:53:50 +01:00
|
|
|
size_t address, size_t len,
|
|
|
|
int ( * resize ) ( struct nvo_block *nvo, size_t len ),
|
|
|
|
struct refcnt *refcnt ) {
|
2008-03-21 23:15:31 +01:00
|
|
|
nvo->nvs = nvs;
|
2010-11-30 02:10:38 +01:00
|
|
|
nvo->address = address;
|
|
|
|
nvo->len = len;
|
2011-01-11 01:53:50 +01:00
|
|
|
nvo->resize = resize;
|
|
|
|
dhcpopt_init ( &nvo->dhcpopts, NULL, 0, nvo_realloc_dhcpopt );
|
2010-12-01 17:34:23 +01:00
|
|
|
settings_init ( &nvo->settings, &nvo_settings_operations, refcnt, 0 );
|
2006-12-04 19:23:06 +01:00
|
|
|
}
|
|
|
|
|
2006-12-04 22:46:13 +01:00
|
|
|
/**
|
|
|
|
* Register non-volatile stored options
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
2008-03-21 23:15:31 +01:00
|
|
|
* @v parent Parent settings block, or NULL
|
2006-12-04 22:46:13 +01:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2008-03-21 23:15:31 +01:00
|
|
|
int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
|
2006-12-04 19:23:06 +01:00
|
|
|
int rc;
|
|
|
|
|
2010-11-30 02:10:38 +01:00
|
|
|
/* Allocate memory for options */
|
2011-01-11 01:53:50 +01:00
|
|
|
if ( ( rc = nvo_realloc ( nvo, nvo->len ) ) != 0 )
|
|
|
|
goto err_realloc;
|
2010-11-25 01:05:20 +01:00
|
|
|
|
|
|
|
/* Read data from NVS */
|
2006-12-04 23:04:10 +01:00
|
|
|
if ( ( rc = nvo_load ( nvo ) ) != 0 )
|
2008-03-21 23:15:31 +01:00
|
|
|
goto err_load;
|
2006-12-04 19:23:06 +01:00
|
|
|
|
2011-01-11 01:53:50 +01:00
|
|
|
/* Register settings */
|
2010-12-01 17:34:23 +01:00
|
|
|
if ( ( rc = register_settings ( &nvo->settings, parent, "nvo" ) ) != 0 )
|
2008-03-21 23:15:31 +01:00
|
|
|
goto err_register;
|
2006-12-05 00:40:35 +01:00
|
|
|
|
2008-03-21 23:15:31 +01:00
|
|
|
DBGC ( nvo, "NVO %p registered\n", nvo );
|
2006-12-04 19:23:06 +01:00
|
|
|
return 0;
|
|
|
|
|
2008-03-21 23:15:31 +01:00
|
|
|
err_register:
|
|
|
|
err_load:
|
2011-01-11 01:53:50 +01:00
|
|
|
nvo_realloc ( nvo, 0 );
|
|
|
|
err_realloc:
|
2006-12-04 19:23:06 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2006-12-04 22:46:13 +01:00
|
|
|
/**
|
|
|
|
* Unregister non-volatile stored options
|
|
|
|
*
|
|
|
|
* @v nvo Non-volatile options block
|
|
|
|
*/
|
2008-03-21 23:15:31 +01:00
|
|
|
void unregister_nvo ( struct nvo_block *nvo ) {
|
|
|
|
unregister_settings ( &nvo->settings );
|
2011-01-11 01:53:50 +01:00
|
|
|
nvo_realloc ( nvo, 0 );
|
2008-03-21 23:15:31 +01:00
|
|
|
DBGC ( nvo, "NVO %p unregistered\n", nvo );
|
2006-12-04 19:23:06 +01:00
|
|
|
}
|