2006-08-11 20:00:22 +02:00
|
|
|
/*
|
2008-03-20 05:06:07 +01:00
|
|
|
* Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
|
2006-08-11 20:00:22 +02:00
|
|
|
*
|
|
|
|
* 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-08-11 20:00:22 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2007-01-19 02:13:12 +01:00
|
|
|
#include <stdio.h>
|
2006-08-11 20:00:22 +02:00
|
|
|
#include <string.h>
|
2006-12-20 05:58:26 +01:00
|
|
|
#include <strings.h>
|
2006-12-08 17:30:14 +01:00
|
|
|
#include <byteswap.h>
|
2006-08-11 20:00:22 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2010-04-19 21:16:01 +02:00
|
|
|
#include <ipxe/in.h>
|
|
|
|
#include <ipxe/vsprintf.h>
|
|
|
|
#include <ipxe/dhcp.h>
|
|
|
|
#include <ipxe/uuid.h>
|
|
|
|
#include <ipxe/uri.h>
|
|
|
|
#include <ipxe/settings.h>
|
2006-08-11 20:00:22 +02:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* Configuration settings
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
2009-05-26 12:05:58 +02:00
|
|
|
* Generic settings blocks
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2008-03-22 01:24:50 +01:00
|
|
|
/**
|
2009-05-26 12:05:58 +02:00
|
|
|
* A generic setting
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct generic_setting {
|
|
|
|
/** List of generic settings */
|
|
|
|
struct list_head list;
|
|
|
|
/** Setting */
|
|
|
|
struct setting setting;
|
|
|
|
/** Size of setting name */
|
|
|
|
size_t name_len;
|
|
|
|
/** Size of setting data */
|
|
|
|
size_t data_len;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get generic setting name
|
|
|
|
*
|
|
|
|
* @v generic Generic setting
|
|
|
|
* @ret name Generic setting name
|
|
|
|
*/
|
|
|
|
static inline void * generic_setting_name ( struct generic_setting *generic ) {
|
|
|
|
return ( ( ( void * ) generic ) + sizeof ( *generic ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get generic setting data
|
|
|
|
*
|
|
|
|
* @v generic Generic setting
|
|
|
|
* @ret data Generic setting data
|
|
|
|
*/
|
|
|
|
static inline void * generic_setting_data ( struct generic_setting *generic ) {
|
|
|
|
return ( ( ( void * ) generic ) + sizeof ( *generic ) +
|
|
|
|
generic->name_len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find generic setting
|
2008-03-22 01:24:50 +01:00
|
|
|
*
|
2009-05-26 12:05:58 +02:00
|
|
|
* @v generics Generic settings block
|
|
|
|
* @v setting Setting to find
|
|
|
|
* @ret generic Generic setting, or NULL
|
|
|
|
*/
|
|
|
|
static struct generic_setting *
|
|
|
|
find_generic_setting ( struct generic_settings *generics,
|
|
|
|
struct setting *setting ) {
|
|
|
|
struct generic_setting *generic;
|
|
|
|
|
|
|
|
list_for_each_entry ( generic, &generics->list, list ) {
|
|
|
|
if ( setting_cmp ( &generic->setting, setting ) == 0 )
|
|
|
|
return generic;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store value of generic setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-22 01:24:50 +01:00
|
|
|
* @v data Setting data, or NULL to clear setting
|
|
|
|
* @v len Length of setting data
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2009-05-26 12:05:58 +02:00
|
|
|
int generic_settings_store ( struct settings *settings,
|
|
|
|
struct setting *setting,
|
|
|
|
const void *data, size_t len ) {
|
|
|
|
struct generic_settings *generics =
|
|
|
|
container_of ( settings, struct generic_settings, settings );
|
|
|
|
struct generic_setting *old;
|
|
|
|
struct generic_setting *new = NULL;
|
|
|
|
size_t name_len;
|
|
|
|
|
|
|
|
/* Identify existing generic setting, if any */
|
|
|
|
old = find_generic_setting ( generics, setting );
|
|
|
|
|
|
|
|
/* Create new generic setting, if required */
|
|
|
|
if ( len ) {
|
|
|
|
/* Allocate new generic setting */
|
|
|
|
name_len = ( strlen ( setting->name ) + 1 );
|
|
|
|
new = zalloc ( sizeof ( *new ) + name_len + len );
|
|
|
|
if ( ! new )
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Populate new generic setting */
|
|
|
|
new->name_len = name_len;
|
|
|
|
new->data_len = len;
|
|
|
|
memcpy ( &new->setting, setting, sizeof ( new->setting ) );
|
|
|
|
new->setting.name = generic_setting_name ( new );
|
|
|
|
memcpy ( generic_setting_name ( new ),
|
|
|
|
setting->name, name_len );
|
|
|
|
memcpy ( generic_setting_data ( new ), data, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete existing generic setting, if any */
|
|
|
|
if ( old ) {
|
|
|
|
list_del ( &old->list );
|
|
|
|
free ( old );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add new setting to list, if any */
|
|
|
|
if ( new )
|
|
|
|
list_add ( &new->list, &generics->list );
|
|
|
|
|
|
|
|
return 0;
|
2008-03-20 05:06:07 +01:00
|
|
|
}
|
|
|
|
|
2008-03-22 01:24:50 +01:00
|
|
|
/**
|
2009-05-26 12:05:58 +02:00
|
|
|
* Fetch value of generic setting
|
2008-03-22 01:24:50 +01:00
|
|
|
*
|
2009-05-26 12:05:58 +02:00
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-22 01:24:50 +01:00
|
|
|
* @v data Buffer to fill with setting data
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of setting data, or negative error
|
|
|
|
*/
|
2009-05-26 12:05:58 +02:00
|
|
|
int generic_settings_fetch ( struct settings *settings,
|
|
|
|
struct setting *setting,
|
|
|
|
void *data, size_t len ) {
|
|
|
|
struct generic_settings *generics =
|
|
|
|
container_of ( settings, struct generic_settings, settings );
|
|
|
|
struct generic_setting *generic;
|
|
|
|
|
|
|
|
/* Find generic setting */
|
|
|
|
generic = find_generic_setting ( generics, setting );
|
|
|
|
if ( ! generic )
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
/* Copy out generic setting data */
|
|
|
|
if ( len > generic->data_len )
|
|
|
|
len = generic->data_len;
|
|
|
|
memcpy ( data, generic_setting_data ( generic ), len );
|
|
|
|
return generic->data_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear generic settings block
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
|
|
|
*/
|
|
|
|
void generic_settings_clear ( struct settings *settings ) {
|
|
|
|
struct generic_settings *generics =
|
|
|
|
container_of ( settings, struct generic_settings, settings );
|
|
|
|
struct generic_setting *generic;
|
|
|
|
struct generic_setting *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe ( generic, tmp, &generics->list, list ) {
|
|
|
|
list_del ( &generic->list );
|
|
|
|
free ( generic );
|
|
|
|
}
|
|
|
|
assert ( list_empty ( &generics->list ) );
|
2008-03-20 05:06:07 +01:00
|
|
|
}
|
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/** Generic settings operations */
|
|
|
|
struct settings_operations generic_settings_operations = {
|
|
|
|
.store = generic_settings_store,
|
|
|
|
.fetch = generic_settings_fetch,
|
|
|
|
.clear = generic_settings_clear,
|
2008-03-20 05:06:07 +01:00
|
|
|
};
|
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Registered settings blocks
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Root generic settings block */
|
|
|
|
struct generic_settings generic_settings_root = {
|
2008-03-22 01:24:50 +01:00
|
|
|
.settings = {
|
|
|
|
.refcnt = NULL,
|
|
|
|
.name = "",
|
|
|
|
.siblings =
|
2009-05-26 12:05:58 +02:00
|
|
|
LIST_HEAD_INIT ( generic_settings_root.settings.siblings ),
|
2008-03-22 01:24:50 +01:00
|
|
|
.children =
|
2009-05-26 12:05:58 +02:00
|
|
|
LIST_HEAD_INIT ( generic_settings_root.settings.children ),
|
|
|
|
.op = &generic_settings_operations,
|
2008-03-22 01:24:50 +01:00
|
|
|
},
|
2009-05-26 12:05:58 +02:00
|
|
|
.list = LIST_HEAD_INIT ( generic_settings_root.list ),
|
2008-03-20 05:06:07 +01:00
|
|
|
};
|
|
|
|
|
2008-03-22 01:24:50 +01:00
|
|
|
/** Root settings block */
|
2009-05-26 12:05:58 +02:00
|
|
|
#define settings_root generic_settings_root.settings
|
2008-03-22 01:24:50 +01:00
|
|
|
|
2009-03-06 15:40:44 +01:00
|
|
|
/**
|
|
|
|
* Find child named settings block
|
|
|
|
*
|
|
|
|
* @v parent Parent settings block
|
|
|
|
* @v name Name within this parent
|
|
|
|
* @ret settings Settings block, or NULL
|
|
|
|
*/
|
|
|
|
static struct settings * find_child_settings ( struct settings *parent,
|
|
|
|
const char *name ) {
|
|
|
|
struct settings *settings;
|
|
|
|
|
|
|
|
/* Treat empty name as meaning "this block" */
|
|
|
|
if ( ! *name )
|
|
|
|
return parent;
|
|
|
|
|
|
|
|
/* Look for child with matching name */
|
|
|
|
list_for_each_entry ( settings, &parent->children, siblings ) {
|
|
|
|
if ( strcmp ( settings->name, name ) == 0 )
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find or create child named settings block
|
|
|
|
*
|
|
|
|
* @v parent Parent settings block
|
|
|
|
* @v name Name within this parent
|
|
|
|
* @ret settings Settings block, or NULL
|
|
|
|
*/
|
|
|
|
static struct settings * autovivify_child_settings ( struct settings *parent,
|
|
|
|
const char *name ) {
|
|
|
|
struct {
|
2009-05-26 12:05:58 +02:00
|
|
|
struct generic_settings generic;
|
2009-03-06 15:40:44 +01:00
|
|
|
char name[ strlen ( name ) + 1 /* NUL */ ];
|
|
|
|
} *new_child;
|
|
|
|
struct settings *settings;
|
|
|
|
|
|
|
|
/* Return existing settings, if existent */
|
|
|
|
if ( ( settings = find_child_settings ( parent, name ) ) != NULL )
|
|
|
|
return settings;
|
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/* Create new generic settings block */
|
2009-03-06 15:40:44 +01:00
|
|
|
new_child = zalloc ( sizeof ( *new_child ) );
|
|
|
|
if ( ! new_child ) {
|
|
|
|
DBGC ( parent, "Settings %p could not create child %s\n",
|
|
|
|
parent, name );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy ( new_child->name, name, sizeof ( new_child->name ) );
|
2009-05-26 12:05:58 +02:00
|
|
|
generic_settings_init ( &new_child->generic, NULL, new_child->name );
|
|
|
|
settings = &new_child->generic.settings;
|
2009-03-06 15:40:44 +01:00
|
|
|
register_settings ( settings, parent );
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/**
|
|
|
|
* Return settings block name (for debug only)
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
|
|
|
* @ret name Settings block name
|
|
|
|
*/
|
|
|
|
static const char * settings_name ( struct settings *settings ) {
|
|
|
|
static char buf[64];
|
|
|
|
char tmp[ sizeof ( buf ) ];
|
|
|
|
int count;
|
|
|
|
|
|
|
|
for ( count = 0 ; settings ; settings = settings->parent ) {
|
|
|
|
memcpy ( tmp, buf, sizeof ( tmp ) );
|
|
|
|
snprintf ( buf, sizeof ( buf ), "%s%c%s", settings->name,
|
|
|
|
( count++ ? '.' : '\0' ), tmp );
|
|
|
|
}
|
|
|
|
return ( buf + 1 );
|
|
|
|
}
|
|
|
|
|
2009-03-06 15:40:44 +01:00
|
|
|
/**
|
|
|
|
* Parse settings block name
|
|
|
|
*
|
|
|
|
* @v name Name
|
|
|
|
* @v get_child Function to find or create child settings block
|
|
|
|
* @ret settings Settings block, or NULL
|
|
|
|
*/
|
|
|
|
static struct settings *
|
|
|
|
parse_settings_name ( const char *name,
|
|
|
|
struct settings * ( * get_child ) ( struct settings *,
|
|
|
|
const char * ) ) {
|
|
|
|
struct settings *settings = &settings_root;
|
|
|
|
char name_copy[ strlen ( name ) + 1 ];
|
|
|
|
char *subname;
|
|
|
|
char *remainder;
|
|
|
|
|
|
|
|
/* Create modifiable copy of name */
|
|
|
|
memcpy ( name_copy, name, sizeof ( name_copy ) );
|
|
|
|
remainder = name_copy;
|
|
|
|
|
|
|
|
/* Parse each name component in turn */
|
|
|
|
while ( remainder ) {
|
2009-10-22 06:55:08 +02:00
|
|
|
struct net_device *netdev;
|
|
|
|
|
2009-03-06 15:40:44 +01:00
|
|
|
subname = remainder;
|
|
|
|
remainder = strchr ( subname, '.' );
|
|
|
|
if ( remainder )
|
|
|
|
*(remainder++) = '\0';
|
2009-10-22 06:55:08 +02:00
|
|
|
|
|
|
|
/* Special case "netX" root settings block */
|
|
|
|
if ( ( subname == name_copy ) && ! strcmp ( subname, "netX" ) &&
|
|
|
|
( ( netdev = last_opened_netdev() ) != NULL ) )
|
|
|
|
settings = get_child ( settings, netdev->name );
|
|
|
|
else
|
|
|
|
settings = get_child ( settings, subname );
|
|
|
|
|
2009-03-06 15:40:44 +01:00
|
|
|
if ( ! settings )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find named settings block
|
|
|
|
*
|
|
|
|
* @v name Name
|
|
|
|
* @ret settings Settings block, or NULL
|
|
|
|
*/
|
|
|
|
struct settings * find_settings ( const char *name ) {
|
|
|
|
|
|
|
|
return parse_settings_name ( name, find_child_settings );
|
|
|
|
}
|
|
|
|
|
2008-03-21 00:15:48 +01:00
|
|
|
/**
|
|
|
|
* Apply all settings
|
|
|
|
*
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int apply_settings ( void ) {
|
|
|
|
struct settings_applicator *applicator;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Call all settings applicators */
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( applicator, SETTINGS_APPLICATORS ) {
|
2008-03-21 00:15:48 +01:00
|
|
|
if ( ( rc = applicator->apply() ) != 0 ) {
|
|
|
|
DBG ( "Could not apply settings using applicator "
|
|
|
|
"%p: %s\n", applicator, strerror ( rc ) );
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reprioritise settings
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
|
|
|
*
|
|
|
|
* Reorders the settings block amongst its siblings according to its
|
|
|
|
* priority.
|
|
|
|
*/
|
|
|
|
static void reprioritise_settings ( struct settings *settings ) {
|
|
|
|
struct settings *parent = settings->parent;
|
|
|
|
long priority;
|
|
|
|
struct settings *tmp;
|
|
|
|
long tmp_priority;
|
|
|
|
|
|
|
|
/* Stop when we reach the top of the tree */
|
|
|
|
if ( ! parent )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Read priority, if present */
|
2008-03-25 21:46:16 +01:00
|
|
|
priority = fetch_intz_setting ( settings, &priority_setting );
|
2008-03-21 00:15:48 +01:00
|
|
|
|
|
|
|
/* Remove from siblings list */
|
|
|
|
list_del ( &settings->siblings );
|
|
|
|
|
|
|
|
/* Reinsert after any existing blocks which have a higher priority */
|
|
|
|
list_for_each_entry ( tmp, &parent->children, siblings ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
tmp_priority = fetch_intz_setting ( tmp, &priority_setting );
|
2008-03-21 00:15:48 +01:00
|
|
|
if ( priority > tmp_priority )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
list_add_tail ( &settings->siblings, &tmp->siblings );
|
|
|
|
|
|
|
|
/* Recurse up the tree */
|
|
|
|
reprioritise_settings ( parent );
|
|
|
|
}
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Register settings block
|
2006-08-11 20:00:22 +02:00
|
|
|
*
|
2008-03-20 22:01:32 +01:00
|
|
|
* @v settings Settings block
|
|
|
|
* @v parent Parent settings block, or NULL
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int register_settings ( struct settings *settings, struct settings *parent ) {
|
2009-01-31 08:32:20 +01:00
|
|
|
struct settings *old_settings;
|
2008-03-20 22:01:32 +01:00
|
|
|
|
|
|
|
/* NULL parent => add to settings root */
|
|
|
|
assert ( settings != NULL );
|
|
|
|
if ( parent == NULL )
|
|
|
|
parent = &settings_root;
|
|
|
|
|
2009-01-31 08:32:20 +01:00
|
|
|
/* Remove any existing settings with the same name */
|
|
|
|
if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
|
|
|
|
unregister_settings ( old_settings );
|
|
|
|
|
2008-03-20 22:01:32 +01:00
|
|
|
/* Add to list of settings */
|
|
|
|
ref_get ( settings->refcnt );
|
|
|
|
ref_get ( parent->refcnt );
|
|
|
|
settings->parent = parent;
|
|
|
|
list_add_tail ( &settings->siblings, &parent->children );
|
2009-05-26 12:05:58 +02:00
|
|
|
DBGC ( settings, "Settings %p (\"%s\") registered\n",
|
|
|
|
settings, settings_name ( settings ) );
|
2008-03-20 22:01:32 +01:00
|
|
|
|
2008-03-21 00:15:48 +01:00
|
|
|
/* Fix up settings priority */
|
|
|
|
reprioritise_settings ( settings );
|
|
|
|
|
2008-03-20 22:01:32 +01:00
|
|
|
/* Apply potentially-updated settings */
|
|
|
|
apply_settings();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister settings block
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
|
|
|
*/
|
|
|
|
void unregister_settings ( struct settings *settings ) {
|
2010-07-14 22:25:55 +02:00
|
|
|
struct settings *child;
|
|
|
|
struct settings *tmp;
|
|
|
|
|
|
|
|
/* Unregister child settings */
|
|
|
|
list_for_each_entry_safe ( child, tmp, &settings->children, siblings ) {
|
|
|
|
unregister_settings ( child );
|
|
|
|
}
|
2008-03-20 22:01:32 +01:00
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
DBGC ( settings, "Settings %p (\"%s\") unregistered\n",
|
|
|
|
settings, settings_name ( settings ) );
|
|
|
|
|
2008-03-20 22:01:32 +01:00
|
|
|
/* Remove from list of settings */
|
|
|
|
ref_put ( settings->parent->refcnt );
|
2008-03-21 23:15:31 +01:00
|
|
|
settings->parent = NULL;
|
2008-03-20 22:01:32 +01:00
|
|
|
list_del ( &settings->siblings );
|
2010-07-14 22:25:55 +02:00
|
|
|
ref_put ( settings->refcnt );
|
2008-03-20 22:01:32 +01:00
|
|
|
|
|
|
|
/* Apply potentially-updated settings */
|
|
|
|
apply_settings();
|
|
|
|
}
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Core settings routines
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2008-03-21 00:15:48 +01:00
|
|
|
/**
|
|
|
|
* Store value of setting
|
|
|
|
*
|
2009-02-17 12:57:31 +01:00
|
|
|
* @v settings Settings block, or NULL
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-21 00:15:48 +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
|
|
|
int store_setting ( struct settings *settings, struct setting *setting,
|
2008-03-21 00:15:48 +01:00
|
|
|
const void *data, size_t len ) {
|
|
|
|
int rc;
|
|
|
|
|
2009-02-17 12:57:31 +01:00
|
|
|
/* NULL settings implies storing into the global settings root */
|
2008-03-21 23:15:31 +01:00
|
|
|
if ( ! settings )
|
2009-02-17 12:57:31 +01:00
|
|
|
settings = &settings_root;
|
2008-03-21 23:15:31 +01:00
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/* Sanity check */
|
|
|
|
if ( ! settings->op->store )
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
2008-03-21 00:15:48 +01:00
|
|
|
/* Store setting */
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( rc = settings->op->store ( settings, setting,
|
|
|
|
data, len ) ) != 0 )
|
2008-03-21 00:15:48 +01:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Reprioritise settings if necessary */
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( setting_cmp ( setting, &priority_setting ) == 0 )
|
2008-03-21 00:15:48 +01:00
|
|
|
reprioritise_settings ( settings );
|
|
|
|
|
2008-03-21 23:15:31 +01:00
|
|
|
/* If these settings are registered, apply potentially-updated
|
|
|
|
* settings
|
|
|
|
*/
|
2008-03-22 01:40:33 +01:00
|
|
|
for ( ; settings ; settings = settings->parent ) {
|
|
|
|
if ( settings == &settings_root ) {
|
2008-03-21 23:15:31 +01:00
|
|
|
if ( ( rc = apply_settings() ) != 0 )
|
|
|
|
return rc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-21 00:15:48 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch value of setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +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
|
|
|
int fetch_setting ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
void *data, size_t len ) {
|
|
|
|
struct settings *child;
|
2008-03-20 05:06:07 +01:00
|
|
|
int ret;
|
|
|
|
|
2008-10-22 22:18:15 +02:00
|
|
|
/* Avoid returning uninitialised data on error */
|
|
|
|
memset ( data, 0, len );
|
|
|
|
|
2008-03-20 22:01:32 +01:00
|
|
|
/* NULL settings implies starting at the global settings root */
|
|
|
|
if ( ! settings )
|
|
|
|
settings = &settings_root;
|
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/* Sanity check */
|
|
|
|
if ( ! settings->op->fetch )
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
2008-03-20 22:01:32 +01:00
|
|
|
/* Try this block first */
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( ret = settings->op->fetch ( settings, setting,
|
|
|
|
data, len ) ) >= 0 )
|
2008-03-20 22:01:32 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Recurse into each child block in turn */
|
|
|
|
list_for_each_entry ( child, &settings->children, siblings ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( ret = fetch_setting ( child, setting,
|
|
|
|
data, len ) ) >= 0 )
|
2008-03-20 22:01:32 +01:00
|
|
|
return ret;
|
2008-03-20 05:06:07 +01:00
|
|
|
}
|
2008-03-20 22:01:32 +01:00
|
|
|
|
|
|
|
return -ENOENT;
|
2008-03-20 05:06:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch length of setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @ret len Length of setting data, or negative error
|
|
|
|
*
|
|
|
|
* This function can also be used as an existence check for the
|
|
|
|
* setting.
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int fetch_setting_len ( struct settings *settings, struct setting *setting ) {
|
|
|
|
return fetch_setting ( settings, setting, NULL, 0 );
|
2008-03-20 05:06:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch value of string setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v data Buffer to fill with setting string data
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of string setting, or negative error
|
|
|
|
*
|
|
|
|
* The resulting string is guaranteed to be correctly NUL-terminated.
|
|
|
|
* The returned length will be the length of the underlying setting
|
|
|
|
* data.
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int fetch_string_setting ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
char *data, size_t len ) {
|
2008-11-07 04:46:21 +01:00
|
|
|
memset ( data, 0, len );
|
2008-08-14 04:03:53 +02:00
|
|
|
return fetch_setting ( settings, setting, data,
|
|
|
|
( ( len > 0 ) ? ( len - 1 ) : 0 ) );
|
2008-03-20 05:06:07 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 20:13:47 +01:00
|
|
|
/**
|
|
|
|
* Fetch value of string setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
|
|
|
* @v setting Setting to fetch
|
|
|
|
* @v data Buffer to allocate and fill with setting string data
|
|
|
|
* @ret len Length of string setting, or negative error
|
|
|
|
*
|
|
|
|
* The resulting string is guaranteed to be correctly NUL-terminated.
|
|
|
|
* The returned length will be the length of the underlying setting
|
|
|
|
* data. The caller is responsible for eventually freeing the
|
|
|
|
* allocated buffer.
|
|
|
|
*/
|
|
|
|
int fetch_string_setting_copy ( struct settings *settings,
|
|
|
|
struct setting *setting,
|
|
|
|
char **data ) {
|
|
|
|
int len;
|
2009-03-26 10:50:52 +01:00
|
|
|
int check_len = 0;
|
2009-01-27 20:13:47 +01:00
|
|
|
|
|
|
|
len = fetch_setting_len ( settings, setting );
|
|
|
|
if ( len < 0 )
|
|
|
|
return len;
|
|
|
|
|
|
|
|
*data = malloc ( len + 1 );
|
|
|
|
if ( ! *data )
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2009-03-26 10:50:52 +01:00
|
|
|
check_len = fetch_string_setting ( settings, setting, *data,
|
|
|
|
( len + 1 ) );
|
2009-01-27 20:13:47 +01:00
|
|
|
assert ( check_len == len );
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2006-08-11 20:00:22 +02:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch value of IPv4 address setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v inp IPv4 address to fill in
|
|
|
|
* @ret len Length of setting, or negative error
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int fetch_ipv4_setting ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
struct in_addr *inp ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
int len;
|
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
len = fetch_setting ( settings, setting, inp, sizeof ( *inp ) );
|
2008-03-20 05:06:07 +01:00
|
|
|
if ( len < 0 )
|
|
|
|
return len;
|
2008-03-21 00:15:48 +01:00
|
|
|
if ( len < ( int ) sizeof ( *inp ) )
|
2008-03-20 05:06:07 +01:00
|
|
|
return -ERANGE;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch value of signed integer setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Integer value to fill in
|
|
|
|
* @ret len Length of setting, or negative error
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int fetch_int_setting ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
long *value ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
union {
|
|
|
|
uint8_t u8[ sizeof ( long ) ];
|
|
|
|
int8_t s8[ sizeof ( long ) ];
|
|
|
|
} buf;
|
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
|
2008-10-22 22:18:15 +02:00
|
|
|
/* Avoid returning uninitialised data on error */
|
|
|
|
*value = 0;
|
|
|
|
|
|
|
|
/* Fetch raw (network-ordered, variable-length) setting */
|
2008-03-25 21:46:16 +01:00
|
|
|
len = fetch_setting ( settings, setting, &buf, sizeof ( buf ) );
|
2008-03-20 05:06:07 +01:00
|
|
|
if ( len < 0 )
|
|
|
|
return len;
|
|
|
|
if ( len > ( int ) sizeof ( buf ) )
|
|
|
|
return -ERANGE;
|
|
|
|
|
2008-10-22 22:18:15 +02:00
|
|
|
/* Convert to host-ordered signed long */
|
2008-03-20 05:06:07 +01:00
|
|
|
*value = ( ( buf.s8[0] >= 0 ) ? 0 : -1L );
|
|
|
|
for ( i = 0 ; i < len ; i++ ) {
|
|
|
|
*value = ( ( *value << 8 ) | buf.u8[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch value of unsigned integer setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Integer value to fill in
|
|
|
|
* @ret len Length of setting, or negative error
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int fetch_uint_setting ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
unsigned long *value ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
long svalue;
|
|
|
|
int len;
|
|
|
|
|
2008-10-22 22:18:15 +02:00
|
|
|
/* Avoid returning uninitialised data on error */
|
|
|
|
*value = 0;
|
|
|
|
|
|
|
|
/* Fetch as a signed long */
|
2008-03-25 21:46:16 +01:00
|
|
|
len = fetch_int_setting ( settings, setting, &svalue );
|
2008-03-20 05:06:07 +01:00
|
|
|
if ( len < 0 )
|
|
|
|
return len;
|
|
|
|
|
2008-10-22 22:18:15 +02:00
|
|
|
/* Mask off sign-extended bits */
|
2009-05-20 09:54:41 +02:00
|
|
|
assert ( len <= ( int ) sizeof ( long ) );
|
|
|
|
*value = ( svalue & ( -1UL >> ( 8 * ( sizeof ( long ) - len ) ) ) );
|
2008-03-20 05:06:07 +01:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2008-03-21 00:42:11 +01:00
|
|
|
/**
|
|
|
|
* Fetch value of signed integer setting, or zero
|
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-21 00:42:11 +01:00
|
|
|
* @ret value Setting value, or zero
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
long fetch_intz_setting ( struct settings *settings, struct setting *setting ){
|
2008-10-22 22:18:15 +02:00
|
|
|
long value;
|
2008-03-21 00:42:11 +01:00
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
fetch_int_setting ( settings, setting, &value );
|
2008-03-21 00:42:11 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch value of unsigned integer setting, or zero
|
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-21 00:42:11 +01:00
|
|
|
* @ret value Setting value, or zero
|
|
|
|
*/
|
|
|
|
unsigned long fetch_uintz_setting ( struct settings *settings,
|
2008-03-25 21:46:16 +01:00
|
|
|
struct setting *setting ) {
|
2008-10-22 22:18:15 +02:00
|
|
|
unsigned long value;
|
2008-03-21 00:42:11 +01:00
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
fetch_uint_setting ( settings, setting, &value );
|
2008-03-21 00:42:11 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2008-03-28 16:35:06 +01:00
|
|
|
/**
|
|
|
|
* Fetch value of UUID setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
|
|
|
* @v setting Setting to fetch
|
|
|
|
* @v uuid UUID to fill in
|
|
|
|
* @ret len Length of setting, or negative error
|
|
|
|
*/
|
|
|
|
int fetch_uuid_setting ( struct settings *settings, struct setting *setting,
|
|
|
|
union uuid *uuid ) {
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = fetch_setting ( settings, setting, uuid, sizeof ( *uuid ) );
|
|
|
|
if ( len < 0 )
|
|
|
|
return len;
|
|
|
|
if ( len != sizeof ( *uuid ) )
|
|
|
|
return -ERANGE;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/**
|
|
|
|
* Clear settings block
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
|
|
|
*/
|
|
|
|
void clear_settings ( struct settings *settings ) {
|
|
|
|
if ( settings->op->clear )
|
|
|
|
settings->op->clear ( settings );
|
|
|
|
}
|
|
|
|
|
2008-03-21 23:15:31 +01:00
|
|
|
/**
|
2008-03-25 21:46:16 +01:00
|
|
|
* Compare two settings
|
2008-03-21 23:15:31 +01:00
|
|
|
*
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v a Setting to compare
|
|
|
|
* @v b Setting to compare
|
|
|
|
* @ret 0 Settings are the same
|
|
|
|
* @ret non-zero Settings are not the same
|
2008-03-21 23:15:31 +01:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int setting_cmp ( struct setting *a, struct setting *b ) {
|
2008-03-21 23:15:31 +01:00
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
/* If the settings have tags, compare them */
|
|
|
|
if ( a->tag && ( a->tag == b->tag ) )
|
|
|
|
return 0;
|
2008-03-21 23:15:31 +01:00
|
|
|
|
2009-06-11 21:12:27 +02:00
|
|
|
/* Otherwise, if the settings have names, compare them */
|
|
|
|
if ( a->name && b->name && a->name[0] )
|
|
|
|
return strcmp ( a->name, b->name );
|
|
|
|
|
|
|
|
/* Otherwise, return a non-match */
|
|
|
|
return ( ! 0 );
|
2008-03-21 23:15:31 +01:00
|
|
|
}
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
2008-03-25 21:46:16 +01:00
|
|
|
* Formatted setting routines
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Store value of typed setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v type Settings type
|
|
|
|
* @v value Formatted setting data, or NULL
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int storef_setting ( struct settings *settings, struct setting *setting,
|
|
|
|
const char *value ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
|
|
|
|
/* NULL value implies deletion. Avoid imposing the burden of
|
2008-03-20 22:01:32 +01:00
|
|
|
* checking for NULL values on each typed setting's storef()
|
2008-03-20 05:06:07 +01:00
|
|
|
* method.
|
|
|
|
*/
|
|
|
|
if ( ! value )
|
2008-03-25 21:46:16 +01:00
|
|
|
return delete_setting ( settings, setting );
|
2008-03-20 05:06:07 +01:00
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
return setting->type->storef ( settings, setting, value );
|
2008-03-20 05:06:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find named setting
|
2006-08-11 20:00:22 +02:00
|
|
|
*
|
|
|
|
* @v name Name
|
2008-03-20 05:06:07 +01:00
|
|
|
* @ret setting Named setting, or NULL
|
2006-08-11 20:00:22 +02:00
|
|
|
*/
|
2010-05-31 19:08:23 +02:00
|
|
|
struct setting * find_setting ( const char *name ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
struct setting *setting;
|
2006-08-11 20:00:22 +02:00
|
|
|
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( setting, SETTINGS ) {
|
2008-03-20 22:01:32 +01:00
|
|
|
if ( strcmp ( name, setting->name ) == 0 )
|
2006-08-11 20:00:22 +02:00
|
|
|
return setting;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/**
|
|
|
|
* Parse setting name as tag number
|
|
|
|
*
|
|
|
|
* @v name Name
|
|
|
|
* @ret tag Tag number, or 0 if not a valid number
|
|
|
|
*/
|
|
|
|
static unsigned int parse_setting_tag ( const char *name ) {
|
|
|
|
char *tmp = ( ( char * ) name );
|
|
|
|
unsigned int tag = 0;
|
|
|
|
|
|
|
|
while ( 1 ) {
|
|
|
|
tag = ( ( tag << 8 ) | strtoul ( tmp, &tmp, 0 ) );
|
|
|
|
if ( *tmp == 0 )
|
|
|
|
return tag;
|
|
|
|
if ( *tmp != '.' )
|
|
|
|
return 0;
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-11 20:00:22 +02:00
|
|
|
/**
|
2008-03-20 05:06:07 +01:00
|
|
|
* Find setting type
|
2006-08-11 20:00:22 +02:00
|
|
|
*
|
|
|
|
* @v name Name
|
2008-03-20 05:06:07 +01:00
|
|
|
* @ret type Setting type, or NULL
|
|
|
|
*/
|
|
|
|
static struct setting_type * find_setting_type ( const char *name ) {
|
|
|
|
struct setting_type *type;
|
|
|
|
|
2009-03-12 20:41:40 +01:00
|
|
|
for_each_table_entry ( type, SETTING_TYPES ) {
|
2008-03-20 22:01:32 +01:00
|
|
|
if ( strcmp ( name, type->name ) == 0 )
|
2008-03-20 05:06:07 +01:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse setting name
|
|
|
|
*
|
|
|
|
* @v name Name of setting
|
2009-03-06 15:40:44 +01:00
|
|
|
* @v get_child Function to find or create child settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v settings Settings block to fill in
|
|
|
|
* @v setting Setting to fill in
|
2009-05-26 12:05:58 +02:00
|
|
|
* @v tmp_name Buffer for copy of setting name
|
2008-03-18 16:51:03 +01:00
|
|
|
* @ret rc Return status code
|
2006-08-11 20:00:22 +02:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* Interprets a name of the form
|
|
|
|
* "[settings_name/]tag_name[:type_name]" and fills in the appropriate
|
|
|
|
* fields.
|
2009-05-26 12:05:58 +02:00
|
|
|
*
|
|
|
|
* The @c tmp_name buffer must be large enough to hold a copy of the
|
|
|
|
* setting name.
|
2006-08-11 20:00:22 +02:00
|
|
|
*/
|
2009-03-06 15:40:44 +01:00
|
|
|
static int
|
|
|
|
parse_setting_name ( const char *name,
|
|
|
|
struct settings * ( * get_child ) ( struct settings *,
|
|
|
|
const char * ),
|
2009-05-26 12:05:58 +02:00
|
|
|
struct settings **settings, struct setting *setting,
|
|
|
|
char *tmp_name ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
char *settings_name;
|
2008-03-25 21:46:16 +01:00
|
|
|
char *setting_name;
|
2008-03-20 05:06:07 +01:00
|
|
|
char *type_name;
|
2008-03-25 21:46:16 +01:00
|
|
|
struct setting *named_setting;
|
2008-03-18 16:51:03 +01:00
|
|
|
|
|
|
|
/* Set defaults */
|
2008-03-20 22:01:32 +01:00
|
|
|
*settings = &settings_root;
|
2008-03-25 21:46:16 +01:00
|
|
|
memset ( setting, 0, sizeof ( *setting ) );
|
2009-05-26 12:05:58 +02:00
|
|
|
setting->name = "";
|
|
|
|
setting->type = &setting_type_string;
|
2008-03-18 16:51:03 +01:00
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
/* Split name into "[settings_name/]setting_name[:type_name]" */
|
2009-05-26 12:05:58 +02:00
|
|
|
strcpy ( tmp_name, name );
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( setting_name = strchr ( tmp_name, '/' ) ) != NULL ) {
|
|
|
|
*(setting_name++) = 0;
|
2008-03-20 05:06:07 +01:00
|
|
|
settings_name = tmp_name;
|
|
|
|
} else {
|
2008-03-25 21:46:16 +01:00
|
|
|
setting_name = tmp_name;
|
2008-03-20 05:06:07 +01:00
|
|
|
settings_name = NULL;
|
|
|
|
}
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( type_name = strchr ( setting_name, ':' ) ) != NULL )
|
2008-03-20 05:06:07 +01:00
|
|
|
*(type_name++) = 0;
|
|
|
|
|
|
|
|
/* Identify settings block, if specified */
|
|
|
|
if ( settings_name ) {
|
2009-03-06 15:40:44 +01:00
|
|
|
*settings = parse_settings_name ( settings_name, get_child );
|
2008-03-20 05:06:07 +01:00
|
|
|
if ( *settings == NULL ) {
|
|
|
|
DBG ( "Unrecognised settings block \"%s\" in \"%s\"\n",
|
|
|
|
settings_name, name );
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
}
|
2008-03-18 16:51:03 +01:00
|
|
|
|
2009-05-26 12:05:58 +02:00
|
|
|
/* Identify setting */
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( named_setting = find_setting ( setting_name ) ) != NULL ) {
|
2009-05-26 12:05:58 +02:00
|
|
|
/* Matches a defined named setting; use that setting */
|
2008-03-25 21:46:16 +01:00
|
|
|
memcpy ( setting, named_setting, sizeof ( *setting ) );
|
2009-05-26 12:05:58 +02:00
|
|
|
} else if ( ( setting->tag = parse_setting_tag ( setting_name ) ) !=0){
|
|
|
|
/* Is a valid numeric tag; use the tag */
|
2008-10-29 19:17:02 +01:00
|
|
|
setting->tag |= (*settings)->tag_magic;
|
2009-05-26 12:05:58 +02:00
|
|
|
} else {
|
|
|
|
/* Use the arbitrary name */
|
|
|
|
setting->name = setting_name;
|
2008-03-18 05:13:11 +01:00
|
|
|
}
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/* Identify setting type, if specified */
|
|
|
|
if ( type_name ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
setting->type = find_setting_type ( type_name );
|
|
|
|
if ( setting->type == NULL ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
DBG ( "Invalid setting type \"%s\" in \"%s\"\n",
|
|
|
|
type_name, name );
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2008-03-18 04:23:36 +01:00
|
|
|
}
|
2008-03-18 16:51:03 +01:00
|
|
|
|
|
|
|
return 0;
|
2006-08-11 20:00:22 +02:00
|
|
|
}
|
|
|
|
|
2006-12-12 16:49:46 +01:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Parse and store value of named setting
|
2006-08-11 20:00:22 +02:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v name Name of setting
|
|
|
|
* @v value Formatted setting data, or NULL
|
|
|
|
* @ret rc Return status code
|
2006-08-11 20:00:22 +02:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int storef_named_setting ( const char *name, const char *value ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
struct settings *settings;
|
2008-03-25 21:46:16 +01:00
|
|
|
struct setting setting;
|
2009-05-26 12:05:58 +02:00
|
|
|
char tmp_name[ strlen ( name ) + 1 ];
|
2008-03-18 16:51:03 +01:00
|
|
|
int rc;
|
2006-08-11 20:00:22 +02:00
|
|
|
|
2009-03-06 15:40:44 +01:00
|
|
|
if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
|
2009-05-26 12:05:58 +02:00
|
|
|
&settings, &setting, tmp_name )) != 0)
|
2008-03-18 16:51:03 +01:00
|
|
|
return rc;
|
2008-03-25 21:46:16 +01:00
|
|
|
return storef_setting ( settings, &setting, value );
|
2006-08-11 20:00:22 +02:00
|
|
|
}
|
|
|
|
|
2006-12-12 16:49:46 +01:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch and format value of named setting
|
2006-08-11 20:00:22 +02:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v name Name of setting
|
|
|
|
* @v buf Buffer to contain formatted value
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of formatted value, or negative error
|
2006-08-11 20:00:22 +02:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
int fetchf_named_setting ( const char *name, char *buf, size_t len ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
struct settings *settings;
|
2008-03-25 21:46:16 +01:00
|
|
|
struct setting setting;
|
2009-05-26 12:05:58 +02:00
|
|
|
char tmp_name[ strlen ( name ) + 1 ];
|
2008-03-18 16:51:03 +01:00
|
|
|
int rc;
|
2006-08-11 20:00:22 +02:00
|
|
|
|
2009-03-06 15:40:44 +01:00
|
|
|
if ( ( rc = parse_setting_name ( name, find_child_settings,
|
2009-05-26 12:05:58 +02:00
|
|
|
&settings, &setting, tmp_name )) != 0)
|
2008-03-18 16:51:03 +01:00
|
|
|
return rc;
|
2008-03-25 21:46:16 +01:00
|
|
|
return fetchf_setting ( settings, &setting, buf, len );
|
2006-08-11 20:00:22 +02:00
|
|
|
}
|
2006-08-11 20:16:57 +02:00
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Setting types
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2006-12-12 16:49:46 +01:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Parse and store value of string setting
|
2006-12-12 16:49:46 +01:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Formatted setting data
|
2006-12-12 16:49:46 +01:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int storef_string ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
const char *value ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
return store_setting ( settings, setting, value, strlen ( value ) );
|
2006-12-12 16:49:46 +01:00
|
|
|
}
|
|
|
|
|
2006-08-11 20:16:57 +02:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch and format value of string setting
|
2006-08-11 20:16:57 +02:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v buf Buffer to contain formatted value
|
2006-08-11 20:16:57 +02:00
|
|
|
* @v len Length of buffer
|
2008-03-18 01:48:23 +01:00
|
|
|
* @ret len Length of formatted value, or negative error
|
2006-08-11 20:16:57 +02:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int fetchf_string ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
char *buf, size_t len ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
return fetch_string_setting ( settings, setting, buf, len );
|
2006-08-11 20:16:57 +02:00
|
|
|
}
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/** A string setting type */
|
|
|
|
struct setting_type setting_type_string __setting_type = {
|
|
|
|
.name = "string",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_string,
|
|
|
|
.fetchf = fetchf_string,
|
2008-03-20 05:06:07 +01:00
|
|
|
};
|
|
|
|
|
2008-09-24 08:22:42 +02:00
|
|
|
/**
|
|
|
|
* Parse and store value of URI-encoded string setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
|
|
|
* @v setting Setting to store
|
|
|
|
* @v value Formatted setting data
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int storef_uristring ( struct settings *settings,
|
|
|
|
struct setting *setting,
|
|
|
|
const char *value ) {
|
|
|
|
char buf[ strlen ( value ) + 1 ]; /* Decoding never expands string */
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = uri_decode ( value, buf, sizeof ( buf ) );
|
|
|
|
return store_setting ( settings, setting, buf, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and format value of URI-encoded string setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
|
|
|
* @v setting Setting to fetch
|
|
|
|
* @v buf Buffer to contain formatted value
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of formatted value, or negative error
|
|
|
|
*/
|
|
|
|
static int fetchf_uristring ( struct settings *settings,
|
|
|
|
struct setting *setting,
|
|
|
|
char *buf, size_t len ) {
|
2009-02-12 10:16:53 +01:00
|
|
|
ssize_t raw_len;
|
2008-09-24 08:22:42 +02:00
|
|
|
|
|
|
|
/* We need to always retrieve the full raw string to know the
|
|
|
|
* length of the encoded string.
|
|
|
|
*/
|
|
|
|
raw_len = fetch_setting ( settings, setting, NULL, 0 );
|
2009-02-12 10:16:53 +01:00
|
|
|
if ( raw_len < 0 )
|
|
|
|
return raw_len;
|
|
|
|
|
2008-09-24 08:22:42 +02:00
|
|
|
{
|
|
|
|
char raw_buf[ raw_len + 1 ];
|
|
|
|
|
|
|
|
fetch_string_setting ( settings, setting, raw_buf,
|
|
|
|
sizeof ( raw_buf ) );
|
2009-12-30 04:36:04 +01:00
|
|
|
return uri_encode ( raw_buf, buf, len, URI_FRAGMENT );
|
2008-09-24 08:22:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A URI-encoded string setting type */
|
|
|
|
struct setting_type setting_type_uristring __setting_type = {
|
|
|
|
.name = "uristring",
|
|
|
|
.storef = storef_uristring,
|
|
|
|
.fetchf = fetchf_uristring,
|
|
|
|
};
|
|
|
|
|
2006-12-08 17:30:14 +01:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Parse and store value of IPv4 address setting
|
2006-08-11 20:16:57 +02:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Formatted setting data
|
2006-08-11 20:16:57 +02:00
|
|
|
* @ret rc Return status code
|
2008-03-20 05:06:07 +01:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int storef_ipv4 ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
const char *value ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
struct in_addr ipv4;
|
2006-08-11 20:16:57 +02:00
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
if ( inet_aton ( value, &ipv4 ) == 0 )
|
|
|
|
return -EINVAL;
|
2008-03-25 21:46:16 +01:00
|
|
|
return store_setting ( settings, setting, &ipv4, sizeof ( ipv4 ) );
|
2006-08-11 20:16:57 +02:00
|
|
|
}
|
|
|
|
|
2006-08-11 20:22:41 +02:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch and format value of IPv4 address setting
|
2006-08-11 20:22:41 +02:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v buf Buffer to contain formatted value
|
2006-08-11 20:22:41 +02:00
|
|
|
* @v len Length of buffer
|
2008-03-18 01:48:23 +01:00
|
|
|
* @ret len Length of formatted value, or negative error
|
2006-08-11 20:22:41 +02:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int fetchf_ipv4 ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
char *buf, size_t len ) {
|
2006-08-11 20:22:41 +02:00
|
|
|
struct in_addr ipv4;
|
2008-03-28 16:35:06 +01:00
|
|
|
int raw_len;
|
2006-08-11 20:22:41 +02:00
|
|
|
|
2008-03-28 16:35:06 +01:00
|
|
|
if ( ( raw_len = fetch_ipv4_setting ( settings, setting, &ipv4 ) ) < 0)
|
|
|
|
return raw_len;
|
|
|
|
return snprintf ( buf, len, "%s", inet_ntoa ( ipv4 ) );
|
2006-08-11 20:22:41 +02:00
|
|
|
}
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/** An IPv4 address setting type */
|
|
|
|
struct setting_type setting_type_ipv4 __setting_type = {
|
2006-08-11 20:22:41 +02:00
|
|
|
.name = "ipv4",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_ipv4,
|
|
|
|
.fetchf = fetchf_ipv4,
|
2006-08-11 20:22:41 +02:00
|
|
|
};
|
|
|
|
|
2006-12-08 17:30:14 +01:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Parse and store value of integer setting
|
2006-12-08 17:30:14 +01:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Formatted setting data
|
|
|
|
* @v size Integer size, in bytes
|
2006-12-08 17:30:14 +01:00
|
|
|
* @ret rc Return status code
|
2008-03-20 05:06:07 +01:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int storef_int ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
const char *value, unsigned int size ) {
|
2006-12-08 17:30:14 +01:00
|
|
|
union {
|
|
|
|
uint32_t num;
|
|
|
|
uint8_t bytes[4];
|
|
|
|
} u;
|
|
|
|
char *endp;
|
|
|
|
|
|
|
|
u.num = htonl ( strtoul ( value, &endp, 0 ) );
|
|
|
|
if ( *endp )
|
|
|
|
return -EINVAL;
|
2008-03-25 21:46:16 +01:00
|
|
|
return store_setting ( settings, setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
&u.bytes[ sizeof ( u ) - size ], size );
|
2006-12-08 17:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Parse and store value of 8-bit integer setting
|
2006-12-08 17:30:14 +01:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Formatted setting data
|
|
|
|
* @v size Integer size, in bytes
|
2006-12-08 17:30:14 +01:00
|
|
|
* @ret rc Return status code
|
2008-03-20 05:06:07 +01:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int storef_int8 ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
const char *value ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
return storef_int ( settings, setting, value, 1 );
|
2006-12-08 17:30:14 +01:00
|
|
|
}
|
|
|
|
|
2008-03-18 04:23:36 +01:00
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Parse and store value of 16-bit integer setting
|
2008-03-18 04:23:36 +01:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Formatted setting data
|
|
|
|
* @v size Integer size, in bytes
|
2008-03-18 04:23:36 +01:00
|
|
|
* @ret rc Return status code
|
2008-03-20 05:06:07 +01:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int storef_int16 ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
const char *value ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
return storef_int ( settings, setting, value, 2 );
|
2008-03-18 04:23:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Parse and store value of 32-bit integer setting
|
2008-03-18 04:23:36 +01:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Formatted setting data
|
|
|
|
* @v size Integer size, in bytes
|
2008-03-18 04:23:36 +01:00
|
|
|
* @ret rc Return status code
|
2008-03-20 05:06:07 +01:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int storef_int32 ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
const char *value ) {
|
2008-03-25 21:46:16 +01:00
|
|
|
return storef_int ( settings, setting, value, 4 );
|
2008-03-20 05:06:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch and format value of signed integer setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v buf Buffer to contain formatted value
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of formatted value, or negative error
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int fetchf_int ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
char *buf, size_t len ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
long value;
|
|
|
|
int rc;
|
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( rc = fetch_int_setting ( settings, setting, &value ) ) < 0 )
|
2008-03-20 05:06:07 +01:00
|
|
|
return rc;
|
|
|
|
return snprintf ( buf, len, "%ld", value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch and format value of unsigned integer setting
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v buf Buffer to contain formatted value
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of formatted value, or negative error
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int fetchf_uint ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
char *buf, size_t len ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
unsigned long value;
|
|
|
|
int rc;
|
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
if ( ( rc = fetch_uint_setting ( settings, setting, &value ) ) < 0 )
|
2008-03-20 05:06:07 +01:00
|
|
|
return rc;
|
|
|
|
return snprintf ( buf, len, "%#lx", value );
|
2008-03-18 04:23:36 +01:00
|
|
|
}
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/** A signed 8-bit integer setting type */
|
|
|
|
struct setting_type setting_type_int8 __setting_type = {
|
2006-12-08 17:30:14 +01:00
|
|
|
.name = "int8",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_int8,
|
|
|
|
.fetchf = fetchf_int,
|
2006-12-08 17:30:14 +01:00
|
|
|
};
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/** A signed 16-bit integer setting type */
|
|
|
|
struct setting_type setting_type_int16 __setting_type = {
|
2008-03-18 04:23:36 +01:00
|
|
|
.name = "int16",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_int16,
|
|
|
|
.fetchf = fetchf_int,
|
2008-03-18 04:23:36 +01:00
|
|
|
};
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/** A signed 32-bit integer setting type */
|
|
|
|
struct setting_type setting_type_int32 __setting_type = {
|
2008-03-18 04:23:36 +01:00
|
|
|
.name = "int32",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_int32,
|
|
|
|
.fetchf = fetchf_int,
|
2008-03-20 05:06:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** An unsigned 8-bit integer setting type */
|
|
|
|
struct setting_type setting_type_uint8 __setting_type = {
|
|
|
|
.name = "uint8",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_int8,
|
|
|
|
.fetchf = fetchf_uint,
|
2008-03-20 05:06:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** An unsigned 16-bit integer setting type */
|
|
|
|
struct setting_type setting_type_uint16 __setting_type = {
|
|
|
|
.name = "uint16",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_int16,
|
|
|
|
.fetchf = fetchf_uint,
|
2008-03-20 05:06:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** An unsigned 32-bit integer setting type */
|
|
|
|
struct setting_type setting_type_uint32 __setting_type = {
|
|
|
|
.name = "uint32",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_int32,
|
|
|
|
.fetchf = fetchf_uint,
|
2008-03-18 04:23:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Parse and store value of hex string setting
|
2008-03-18 04:23:36 +01:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to store
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v value Formatted setting data
|
2008-03-18 04:23:36 +01:00
|
|
|
* @ret rc Return status code
|
2008-03-20 05:06:07 +01:00
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int storef_hex ( struct settings *settings, struct setting *setting,
|
2008-03-20 22:01:32 +01:00
|
|
|
const char *value ) {
|
2008-03-18 04:23:36 +01:00
|
|
|
char *ptr = ( char * ) value;
|
|
|
|
uint8_t bytes[ strlen ( value ) ]; /* cannot exceed strlen(value) */
|
|
|
|
unsigned int len = 0;
|
|
|
|
|
|
|
|
while ( 1 ) {
|
|
|
|
bytes[len++] = strtoul ( ptr, &ptr, 16 );
|
|
|
|
switch ( *ptr ) {
|
|
|
|
case '\0' :
|
2008-03-25 21:46:16 +01:00
|
|
|
return store_setting ( settings, setting, bytes, len );
|
2008-03-18 04:23:36 +01:00
|
|
|
case ':' :
|
2010-11-13 01:22:23 +01:00
|
|
|
case '-' :
|
2008-03-18 04:23:36 +01:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
default :
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-03-20 22:01:32 +01:00
|
|
|
* Fetch and format value of hex string setting
|
2008-03-18 04:23:36 +01:00
|
|
|
*
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
2008-03-25 21:46:16 +01:00
|
|
|
* @v setting Setting to fetch
|
2008-03-20 05:06:07 +01:00
|
|
|
* @v buf Buffer to contain formatted value
|
2008-03-18 04:23:36 +01:00
|
|
|
* @v len Length of buffer
|
2010-11-13 01:22:23 +01:00
|
|
|
* @v delimiter Byte delimiter
|
2008-03-18 04:23:36 +01:00
|
|
|
* @ret len Length of formatted value, or negative error
|
|
|
|
*/
|
2008-03-25 21:46:16 +01:00
|
|
|
static int fetchf_hex ( struct settings *settings, struct setting *setting,
|
2010-11-13 01:22:23 +01:00
|
|
|
char *buf, size_t len, const char *delimiter ) {
|
2008-03-20 05:06:07 +01:00
|
|
|
int raw_len;
|
|
|
|
int check_len;
|
2008-03-18 04:23:36 +01:00
|
|
|
int used = 0;
|
|
|
|
int i;
|
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
raw_len = fetch_setting_len ( settings, setting );
|
2008-03-20 05:06:07 +01:00
|
|
|
if ( raw_len < 0 )
|
|
|
|
return raw_len;
|
2008-03-18 04:23:36 +01:00
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
{
|
|
|
|
uint8_t raw[raw_len];
|
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
check_len = fetch_setting ( settings, setting, raw,
|
|
|
|
sizeof ( raw ) );
|
2008-09-26 04:39:35 +02:00
|
|
|
if ( check_len < 0 )
|
|
|
|
return check_len;
|
2008-03-20 05:06:07 +01:00
|
|
|
assert ( check_len == raw_len );
|
|
|
|
|
|
|
|
if ( len )
|
|
|
|
buf[0] = 0; /* Ensure that a terminating NUL exists */
|
|
|
|
for ( i = 0 ; i < raw_len ; i++ ) {
|
|
|
|
used += ssnprintf ( ( buf + used ), ( len - used ),
|
2010-11-13 01:22:23 +01:00
|
|
|
"%s%02x", ( used ? delimiter : "" ),
|
2008-03-20 05:06:07 +01:00
|
|
|
raw[i] );
|
|
|
|
}
|
|
|
|
return used;
|
2008-03-18 04:23:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 01:22:23 +01:00
|
|
|
/**
|
|
|
|
* Fetch and format value of hex string setting (using colon delimiter)
|
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
|
|
|
* @v setting Setting to fetch
|
|
|
|
* @v buf Buffer to contain formatted value
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of formatted value, or negative error
|
|
|
|
*/
|
|
|
|
static int fetchf_hex_colon ( struct settings *settings,
|
|
|
|
struct setting *setting,
|
|
|
|
char *buf, size_t len ) {
|
|
|
|
return fetchf_hex ( settings, setting, buf, len, ":" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and format value of hex string setting (using hyphen delimiter)
|
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
|
|
|
* @v setting Setting to fetch
|
|
|
|
* @v buf Buffer to contain formatted value
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of formatted value, or negative error
|
|
|
|
*/
|
|
|
|
static int fetchf_hex_hyphen ( struct settings *settings,
|
|
|
|
struct setting *setting,
|
|
|
|
char *buf, size_t len ) {
|
|
|
|
return fetchf_hex ( settings, setting, buf, len, "-" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** A hex-string setting (colon-delimited) */
|
2008-03-20 05:06:07 +01:00
|
|
|
struct setting_type setting_type_hex __setting_type = {
|
2008-03-18 04:23:36 +01:00
|
|
|
.name = "hex",
|
2008-03-20 22:01:32 +01:00
|
|
|
.storef = storef_hex,
|
2010-11-13 01:22:23 +01:00
|
|
|
.fetchf = fetchf_hex_colon,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** A hex-string setting (hyphen-delimited) */
|
|
|
|
struct setting_type setting_type_hexhyp __setting_type = {
|
|
|
|
.name = "hexhyp",
|
|
|
|
.storef = storef_hex,
|
|
|
|
.fetchf = fetchf_hex_hyphen,
|
2008-03-18 04:23:36 +01:00
|
|
|
};
|
|
|
|
|
2008-03-28 16:35:06 +01:00
|
|
|
/**
|
|
|
|
* Parse and store value of UUID setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block
|
|
|
|
* @v setting Setting to store
|
|
|
|
* @v value Formatted setting data
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
static int storef_uuid ( struct settings *settings __unused,
|
|
|
|
struct setting *setting __unused,
|
|
|
|
const char *value __unused ) {
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and format value of UUID setting
|
|
|
|
*
|
|
|
|
* @v settings Settings block, or NULL to search all blocks
|
|
|
|
* @v setting Setting to fetch
|
|
|
|
* @v buf Buffer to contain formatted value
|
|
|
|
* @v len Length of buffer
|
|
|
|
* @ret len Length of formatted value, or negative error
|
|
|
|
*/
|
|
|
|
static int fetchf_uuid ( struct settings *settings, struct setting *setting,
|
|
|
|
char *buf, size_t len ) {
|
|
|
|
union uuid uuid;
|
|
|
|
int raw_len;
|
|
|
|
|
|
|
|
if ( ( raw_len = fetch_uuid_setting ( settings, setting, &uuid ) ) < 0)
|
|
|
|
return raw_len;
|
|
|
|
return snprintf ( buf, len, "%s", uuid_ntoa ( &uuid ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** UUID setting type */
|
|
|
|
struct setting_type setting_type_uuid __setting_type = {
|
|
|
|
.name = "uuid",
|
|
|
|
.storef = storef_uuid,
|
|
|
|
.fetchf = fetchf_uuid,
|
|
|
|
};
|
|
|
|
|
2008-03-20 05:06:07 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
2008-03-25 21:46:16 +01:00
|
|
|
* Settings
|
2008-03-20 05:06:07 +01:00
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2008-03-25 21:46:16 +01:00
|
|
|
/** Hostname setting */
|
|
|
|
struct setting hostname_setting __setting = {
|
|
|
|
.name = "hostname",
|
|
|
|
.description = "Host name",
|
|
|
|
.tag = DHCP_HOST_NAME,
|
|
|
|
.type = &setting_type_string,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Filename setting */
|
|
|
|
struct setting filename_setting __setting = {
|
|
|
|
.name = "filename",
|
|
|
|
.description = "Boot filename",
|
|
|
|
.tag = DHCP_BOOTFILE_NAME,
|
|
|
|
.type = &setting_type_string,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Root path setting */
|
|
|
|
struct setting root_path_setting __setting = {
|
|
|
|
.name = "root-path",
|
2010-01-30 10:48:21 +01:00
|
|
|
.description = "iSCSI root path",
|
2008-03-25 21:46:16 +01:00
|
|
|
.tag = DHCP_ROOT_PATH,
|
|
|
|
.type = &setting_type_string,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Username setting */
|
|
|
|
struct setting username_setting __setting = {
|
|
|
|
.name = "username",
|
|
|
|
.description = "User name",
|
|
|
|
.tag = DHCP_EB_USERNAME,
|
|
|
|
.type = &setting_type_string,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Password setting */
|
|
|
|
struct setting password_setting __setting = {
|
|
|
|
.name = "password",
|
|
|
|
.description = "Password",
|
|
|
|
.tag = DHCP_EB_PASSWORD,
|
|
|
|
.type = &setting_type_string,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Priority setting */
|
|
|
|
struct setting priority_setting __setting = {
|
|
|
|
.name = "priority",
|
|
|
|
.description = "Priority of these settings",
|
|
|
|
.tag = DHCP_EB_PRIORITY,
|
|
|
|
.type = &setting_type_int8,
|
2006-12-21 17:08:37 +01:00
|
|
|
};
|