summaryrefslogtreecommitdiffstats
path: root/src/core/nvo.c
diff options
context:
space:
mode:
authorMichael Brown2011-01-11 01:53:50 +0100
committerMichael Brown2011-01-19 14:52:48 +0100
commit17d28f48776b909d031bcb0435c852ade1bd8988 (patch)
tree476b71c1eff4ec7ec5999504c112ae321d3acbb6 /src/core/nvo.c
parent[nvo] Remove the non-volatile options fragment list (diff)
downloadipxe-17d28f48776b909d031bcb0435c852ade1bd8988.tar.gz
ipxe-17d28f48776b909d031bcb0435c852ade1bd8988.tar.xz
ipxe-17d28f48776b909d031bcb0435c852ade1bd8988.zip
[nvo] Allow resizing of non-volatile stored option blocks
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/core/nvo.c')
-rw-r--r--src/core/nvo.c152
1 files changed, 103 insertions, 49 deletions
diff --git a/src/core/nvo.c b/src/core/nvo.c
index c5968c6b..f4da407a 100644
--- a/src/core/nvo.c
+++ b/src/core/nvo.c
@@ -50,14 +50,88 @@ static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
}
/**
+ * 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;
+}
+
+/**
* Load non-volatile stored options from non-volatile storage device
*
* @v nvo Non-volatile options block
* @ret rc Return status code
*/
static int nvo_load ( struct nvo_block *nvo ) {
+ uint8_t *options_data = nvo->dhcpopts.data;
int rc;
+ /* Skip reading zero-length NVO fields */
+ if ( nvo->len == 0 ) {
+ DBGC ( nvo, "NVO %p is empty; skipping load\n", nvo );
+ return 0;
+ }
+
/* Read data */
if ( ( rc = nvs_read ( nvo->nvs, nvo->address, nvo->data,
nvo->len ) ) != 0 ) {
@@ -66,6 +140,20 @@ static int nvo_load ( struct nvo_block *nvo ) {
return rc;
}
+ /* 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 );
+
DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
return 0;
}
@@ -80,8 +168,9 @@ static int nvo_save ( struct nvo_block *nvo ) {
uint8_t *checksum = nvo->data;
int rc;
- /* Recalculate checksum */
- *checksum -= nvo_checksum ( nvo );
+ /* Recalculate checksum, if applicable */
+ if ( nvo->len > 0 )
+ *checksum -= nvo_checksum ( nvo );
/* Write data */
if ( ( rc = nvs_write ( nvo->nvs, nvo->address, nvo->data,
@@ -96,38 +185,6 @@ static int nvo_save ( struct nvo_block *nvo ) {
}
/**
- * 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.
- */
-static void nvo_init_dhcpopts ( struct nvo_block *nvo ) {
- uint8_t *options_data;
- size_t options_len;
-
- /* Steal one byte for the checksum */
- options_data = ( nvo->data + 1 );
- options_len = ( nvo->len - 1 );
-
- /* 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 );
- }
-
- dhcpopt_init ( &nvo->dhcpopts, options_data, options_len,
- dhcpopt_no_realloc );
-}
-
-/**
* Store value of NVO setting
*
* @v settings Settings block
@@ -190,13 +247,18 @@ static struct settings_operations nvo_settings_operations = {
* @v nvs Underlying non-volatile storage device
* @v address Address within NVS device
* @v len Length of non-volatile options data
+ * @v resize Resize method
* @v refcnt Containing object reference counter, or NULL
*/
void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs,
- size_t address, size_t len, struct refcnt *refcnt ) {
+ size_t address, size_t len,
+ int ( * resize ) ( struct nvo_block *nvo, size_t len ),
+ struct refcnt *refcnt ) {
nvo->nvs = nvs;
nvo->address = address;
nvo->len = len;
+ nvo->resize = resize;
+ dhcpopt_init ( &nvo->dhcpopts, NULL, 0, nvo_realloc_dhcpopt );
settings_init ( &nvo->settings, &nvo_settings_operations, refcnt, 0 );
}
@@ -211,20 +273,14 @@ int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
int rc;
/* Allocate memory for options */
- nvo->data = zalloc ( nvo->len );
- if ( ! nvo->data ) {
- DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
- nvo, nvo->len );
- rc = -ENOMEM;
- goto err_malloc;
- }
+ if ( ( rc = nvo_realloc ( nvo, nvo->len ) ) != 0 )
+ goto err_realloc;
/* Read data from NVS */
if ( ( rc = nvo_load ( nvo ) ) != 0 )
goto err_load;
- /* Verify and register options */
- nvo_init_dhcpopts ( nvo );
+ /* Register settings */
if ( ( rc = register_settings ( &nvo->settings, parent, "nvo" ) ) != 0 )
goto err_register;
@@ -233,9 +289,8 @@ int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
err_register:
err_load:
- free ( nvo->data );
- nvo->data = NULL;
- err_malloc:
+ nvo_realloc ( nvo, 0 );
+ err_realloc:
return rc;
}
@@ -246,7 +301,6 @@ int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
*/
void unregister_nvo ( struct nvo_block *nvo ) {
unregister_settings ( &nvo->settings );
- free ( nvo->data );
- nvo->data = NULL;
+ nvo_realloc ( nvo, 0 );
DBGC ( nvo, "NVO %p unregistered\n", nvo );
}