david/ipxe
Archived
1
0

[nvo] Allow resizing of non-volatile stored option blocks

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2011-01-11 00:53:50 +00:00
parent 1651d4f6d7
commit 17d28f4877
8 changed files with 120 additions and 54 deletions

View File

@ -49,6 +49,73 @@ static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
return sum; return sum;
} }
/**
* 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 * Load non-volatile stored options from non-volatile storage device
* *
@ -56,8 +123,15 @@ static unsigned int nvo_checksum ( struct nvo_block *nvo ) {
* @ret rc Return status code * @ret rc Return status code
*/ */
static int nvo_load ( struct nvo_block *nvo ) { static int nvo_load ( struct nvo_block *nvo ) {
uint8_t *options_data = nvo->dhcpopts.data;
int rc; 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 */ /* Read data */
if ( ( rc = nvs_read ( nvo->nvs, nvo->address, nvo->data, if ( ( rc = nvs_read ( nvo->nvs, nvo->address, nvo->data,
nvo->len ) ) != 0 ) { nvo->len ) ) != 0 ) {
@ -66,6 +140,20 @@ static int nvo_load ( struct nvo_block *nvo ) {
return rc; 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 ); DBGC ( nvo, "NVO %p loaded from non-volatile storage\n", nvo );
return 0; return 0;
} }
@ -80,7 +168,8 @@ static int nvo_save ( struct nvo_block *nvo ) {
uint8_t *checksum = nvo->data; uint8_t *checksum = nvo->data;
int rc; int rc;
/* Recalculate checksum */ /* Recalculate checksum, if applicable */
if ( nvo->len > 0 )
*checksum -= nvo_checksum ( nvo ); *checksum -= nvo_checksum ( nvo );
/* Write data */ /* Write data */
@ -95,38 +184,6 @@ static int nvo_save ( struct nvo_block *nvo ) {
return 0; return 0;
} }
/**
* 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 * Store value of NVO setting
* *
@ -190,13 +247,18 @@ static struct settings_operations nvo_settings_operations = {
* @v nvs Underlying non-volatile storage device * @v nvs Underlying non-volatile storage device
* @v address Address within NVS device * @v address Address within NVS device
* @v len Length of non-volatile options data * @v len Length of non-volatile options data
* @v resize Resize method
* @v refcnt Containing object reference counter, or NULL * @v refcnt Containing object reference counter, or NULL
*/ */
void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs, 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->nvs = nvs;
nvo->address = address; nvo->address = address;
nvo->len = len; nvo->len = len;
nvo->resize = resize;
dhcpopt_init ( &nvo->dhcpopts, NULL, 0, nvo_realloc_dhcpopt );
settings_init ( &nvo->settings, &nvo_settings_operations, refcnt, 0 ); 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; int rc;
/* Allocate memory for options */ /* Allocate memory for options */
nvo->data = zalloc ( nvo->len ); if ( ( rc = nvo_realloc ( nvo, nvo->len ) ) != 0 )
if ( ! nvo->data ) { goto err_realloc;
DBGC ( nvo, "NVO %p could not allocate %zd bytes\n",
nvo, nvo->len );
rc = -ENOMEM;
goto err_malloc;
}
/* Read data from NVS */ /* Read data from NVS */
if ( ( rc = nvo_load ( nvo ) ) != 0 ) if ( ( rc = nvo_load ( nvo ) ) != 0 )
goto err_load; goto err_load;
/* Verify and register options */ /* Register settings */
nvo_init_dhcpopts ( nvo );
if ( ( rc = register_settings ( &nvo->settings, parent, "nvo" ) ) != 0 ) if ( ( rc = register_settings ( &nvo->settings, parent, "nvo" ) ) != 0 )
goto err_register; goto err_register;
@ -233,9 +289,8 @@ int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
err_register: err_register:
err_load: err_load:
free ( nvo->data ); nvo_realloc ( nvo, 0 );
nvo->data = NULL; err_realloc:
err_malloc:
return rc; return rc;
} }
@ -246,7 +301,6 @@ int register_nvo ( struct nvo_block *nvo, struct settings *parent ) {
*/ */
void unregister_nvo ( struct nvo_block *nvo ) { void unregister_nvo ( struct nvo_block *nvo ) {
unregister_settings ( &nvo->settings ); unregister_settings ( &nvo->settings );
free ( nvo->data ); nvo_realloc ( nvo, 0 );
nvo->data = NULL;
DBGC ( nvo, "NVO %p unregistered\n", nvo ); DBGC ( nvo, "NVO %p unregistered\n", nvo );
} }

View File

@ -3273,7 +3273,7 @@ falcon_probe_spi ( struct efab_nic *efab )
/* If the device has EEPROM attached, then advertise NVO space */ /* If the device has EEPROM attached, then advertise NVO space */
if ( has_eeprom ) { if ( has_eeprom ) {
nvo_init ( &efab->nvo, &efab->spi_eeprom.nvs, 0x100, 0xf0, nvo_init ( &efab->nvo, &efab->spi_eeprom.nvs, 0x100, 0xf0,
&efab->netdev->refcnt ); NULL, &efab->netdev->refcnt );
} }
return 0; return 0;

View File

@ -732,6 +732,7 @@ static int myri10ge_nv_init ( struct myri10ge_private *priv )
nvo_init ( &priv->nvo, nvo_init ( &priv->nvo,
&priv->nvs, &priv->nvs,
nvo_fragment_pos, 0x200, nvo_fragment_pos, 0x200,
NULL,
& myri10ge_netdev (priv) -> refcnt ); & myri10ge_netdev (priv) -> refcnt );
rc = register_nvo ( &priv->nvo, rc = register_nvo ( &priv->nvo,
netdev_settings ( myri10ge_netdev ( priv ) ) ); netdev_settings ( myri10ge_netdev ( priv ) ) );

View File

@ -154,7 +154,7 @@ static void natsemi_init_eeprom ( struct natsemi_private *np ) {
* this region. Currently it is not working. But with some * this region. Currently it is not working. But with some
* efforts it can. * efforts it can.
*/ */
nvo_init ( &np->nvo, &np->eeprom.nvs, 0x0c, 0x68, NULL ); nvo_init ( &np->nvo, &np->eeprom.nvs, 0x0c, 0x68, NULL, NULL );
} }
/** /**

View File

@ -288,7 +288,7 @@ static void rtl_init_eeprom ( struct net_device *netdev ) {
DBGC ( rtl, "rtl8139 %p EEPROM in use for VPD; cannot use " DBGC ( rtl, "rtl8139 %p EEPROM in use for VPD; cannot use "
"for options\n", rtl ); "for options\n", rtl );
} else { } else {
nvo_init ( &rtl->nvo, &rtl->eeprom.nvs, 0x20, 0x40, nvo_init ( &rtl->nvo, &rtl->eeprom.nvs, 0x20, 0x40, NULL,
&netdev->refcnt ); &netdev->refcnt );
} }
} }

View File

@ -36,6 +36,7 @@ extern void dhcpopt_init ( struct dhcp_options *options,
void *data, size_t alloc_len, void *data, size_t alloc_len,
int ( * realloc ) ( struct dhcp_options *options, int ( * realloc ) ( struct dhcp_options *options,
size_t len ) ); size_t len ) );
extern void dhcpopt_update_used_len ( struct dhcp_options *options );
extern int dhcpopt_no_realloc ( struct dhcp_options *options, size_t len ); extern int dhcpopt_no_realloc ( struct dhcp_options *options, size_t len );
#endif /* _IPXE_DHCPOPTS_H */ #endif /* _IPXE_DHCPOPTS_H */

View File

@ -30,12 +30,22 @@ struct nvo_block {
size_t len; size_t len;
/** Option-containing data */ /** Option-containing data */
void *data; void *data;
/**
* Resize non-volatile stored option block
*
* @v nvo Non-volatile options block
* @v len New size
* @ret rc Return status code
*/
int ( * resize ) ( struct nvo_block *nvo, size_t len );
/** DHCP options block */ /** DHCP options block */
struct dhcp_options dhcpopts; struct dhcp_options dhcpopts;
}; };
extern void nvo_init ( struct nvo_block *nvo, struct nvs_device *nvs, extern 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 );
extern int register_nvo ( struct nvo_block *nvo, struct settings *parent ); extern int register_nvo ( struct nvo_block *nvo, struct settings *parent );
extern void unregister_nvo ( struct nvo_block *nvo ); extern void unregister_nvo ( struct nvo_block *nvo );

View File

@ -402,7 +402,7 @@ int dhcpopt_fetch ( struct dhcp_options *options, unsigned int tag,
* The "used length" field will be updated based on scanning through * The "used length" field will be updated based on scanning through
* the block to find the end of the options. * the block to find the end of the options.
*/ */
static void dhcpopt_update_used_len ( struct dhcp_options *options ) { void dhcpopt_update_used_len ( struct dhcp_options *options ) {
struct dhcp_option *option; struct dhcp_option *option;
int offset = 0; int offset = 0;
ssize_t remaining = options->alloc_len; ssize_t remaining = options->alloc_len;