david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[Settings] Start revamping the configuration settings API.

Add the concept of an abstract configuration setting, comprising a (DHCP)
tag value and an associated byte sequence.

Add the concept of a settings namespace.

Add functions for extracting string, IPv4 address, and signed and
unsigned integer values from configuration settings (analogous to
dhcp_snprintf(), dhcp_ipv4_option(), etc.).

Update functions for parsing and formatting named/typed options to work
with new settings API.

Update NVO commands and config UI to use new settings API.
This commit is contained in:
Michael Brown 2008-03-20 04:06:07 +00:00
parent 7067142fb4
commit a48b4d9948
6 changed files with 812 additions and 506 deletions

File diff suppressed because it is too large Load Diff

View File

@ -4,28 +4,27 @@
#include <gpxe/settings.h> #include <gpxe/settings.h>
#include <gpxe/settings_ui.h> #include <gpxe/settings_ui.h>
#include <gpxe/nvo.h>
extern struct nvo_block *ugly_nvo_hack;
static int config_exec ( int argc, char **argv ) { static int config_exec ( int argc, char **argv ) {
struct config_context dummy_context; struct settings *settings;
int rc; int rc;
if ( argc != 1 ) { if ( argc > 2 ) {
printf ( "Usage: %s\n" printf ( "Usage: %s [scope]\n"
"Opens the option configuration console\n", argv[0] ); "Opens the option configuration console\n", argv[0] );
return 1; return 1;
} }
if ( ! ugly_nvo_hack ) { if ( argc == 2 ) {
printf ( "No non-volatile option storage available\n" ); settings = find_settings ( argv[1] );
if ( ! settings ) {
printf ( "No such scope \"%s\"\n", argv[1] );
return 1; return 1;
} }
} else {
settings = &interactive_settings;
}
dummy_context.options = ugly_nvo_hack->options; if ( ( rc = settings_ui ( settings ) ) != 0 ) {
if ( ( rc = settings_ui ( &dummy_context ) ) != 0 ) {
printf ( "Could not save settings: %s\n", printf ( "Could not save settings: %s\n",
strerror ( rc ) ); strerror ( rc ) );
return 1; return 1;

View File

@ -4,33 +4,21 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <getopt.h> #include <getopt.h>
#include <gpxe/nvo.h>
#include <gpxe/dhcp.h>
#include <gpxe/settings.h> #include <gpxe/settings.h>
#include <gpxe/command.h> #include <gpxe/command.h>
extern struct nvo_block *ugly_nvo_hack;
static int show_exec ( int argc, char **argv ) { static int show_exec ( int argc, char **argv ) {
struct config_context dummy_context;
char buf[256]; char buf[256];
int rc; int rc;
if ( ! ugly_nvo_hack ) {
printf ( "No non-volatile option storage available\n" );
return 1;
}
if ( argc != 2 ) { if ( argc != 2 ) {
printf ( "Syntax: %s <identifier>\n", argv[0] ); printf ( "Syntax: %s <identifier>\n", argv[0] );
return 1; return 1;
} }
dummy_context.options = ugly_nvo_hack->options; if ( ( rc = get_named_setting ( argv[1], buf, sizeof ( buf ) ) ) < 0 ){
if ( ( rc = show_named_setting ( &dummy_context, argv[1], buf,
sizeof ( buf ) ) ) < 0 ) {
printf ( "Could not find \"%s\": %s\n", printf ( "Could not find \"%s\": %s\n",
argv[1], strerror ( -rc ) ); argv[1], strerror ( rc ) );
return 1; return 1;
} }
@ -39,30 +27,16 @@ static int show_exec ( int argc, char **argv ) {
} }
static int set_exec ( int argc, char **argv ) { static int set_exec ( int argc, char **argv ) {
struct config_context dummy_context;
int rc; int rc;
if ( ! ugly_nvo_hack ) {
printf ( "No non-volatile option storage available\n" );
return 1;
}
if ( argc != 3 ) { if ( argc != 3 ) {
printf ( "Syntax: %s <identifier> <value>\n", printf ( "Syntax: %s <identifier> <value>\n", argv[0] );
argv[0] );
return 1; return 1;
} }
dummy_context.options = ugly_nvo_hack->options; if ( ( rc = set_named_setting ( argv[1], argv[2] ) ) != 0 ) {
if ( ( rc = set_named_setting ( &dummy_context, argv[1],
argv[2] ) ) != 0 ) {
printf ( "Could not set \"%s\"=\"%s\": %s\n", printf ( "Could not set \"%s\"=\"%s\": %s\n",
argv[1], argv[2], strerror ( -rc ) ); argv[1], argv[2], strerror ( rc ) );
return 1;
}
if ( nvo_save ( ugly_nvo_hack ) != 0 ) {
printf ( "Could not save options to non-volatile storage\n" );
return 1; return 1;
} }
@ -70,24 +44,16 @@ static int set_exec ( int argc, char **argv ) {
} }
static int clear_exec ( int argc, char **argv ) { static int clear_exec ( int argc, char **argv ) {
struct config_context dummy_context;
int rc; int rc;
if ( ! ugly_nvo_hack ) {
printf ( "No non-volatile option storage available\n" );
return 1;
}
if ( argc != 2 ) { if ( argc != 2 ) {
printf ( "Syntax: %s <identifier>\n", printf ( "Syntax: %s <identifier>\n", argv[0] );
argv[0] );
return 1; return 1;
} }
dummy_context.options = ugly_nvo_hack->options; if ( ( rc = delete_named_setting ( argv[1] ) ) != 0 ) {
if ( ( rc = clear_named_setting ( &dummy_context, argv[1] ) ) != 0 ) {
printf ( "Could not clear \"%s\": %s\n", printf ( "Could not clear \"%s\": %s\n",
argv[1], strerror ( -rc ) ); argv[1], strerror ( rc ) );
return 1; return 1;
} }

View File

@ -33,9 +33,6 @@
* *
*/ */
#include <gpxe/nvo.h>
extern struct nvo_block *ugly_nvo_hack;
/* Colour pairs */ /* Colour pairs */
#define CPAIR_NORMAL 1 #define CPAIR_NORMAL 1
#define CPAIR_SELECT 2 #define CPAIR_SELECT 2
@ -64,10 +61,10 @@ struct setting_row {
/** A setting widget */ /** A setting widget */
struct setting_widget { struct setting_widget {
/** Configuration context */ /** Settings block */
struct config_context *context; struct settings *settings;
/** Configuration setting */ /** Configuration setting */
struct config_setting *setting; struct named_setting *setting;
/** Screen row */ /** Screen row */
unsigned int row; unsigned int row;
/** Screen column */ /** Screen column */
@ -81,32 +78,32 @@ struct setting_widget {
}; };
/** Registered configuration settings */ /** Registered configuration settings */
static struct config_setting config_settings[0] static struct named_setting named_settings[0]
__table_start ( struct config_setting, config_settings ); __table_start ( struct named_setting, named_settings );
static struct config_setting config_settings_end[0] static struct named_setting named_settings_end[0]
__table_end ( struct config_setting, config_settings ); __table_end ( struct named_setting, named_settings );
#define NUM_SETTINGS ( ( unsigned ) ( config_settings_end - config_settings ) ) #define NUM_SETTINGS ( ( unsigned ) ( named_settings_end - named_settings ) )
static void load_setting ( struct setting_widget *widget ) __nonnull; static void load_setting ( struct setting_widget *widget ) __nonnull;
static int save_setting ( struct setting_widget *widget ) __nonnull; static int save_setting ( struct setting_widget *widget ) __nonnull;
static void init_setting ( struct setting_widget *widget, static void init_setting ( struct setting_widget *widget,
struct config_context *context, struct settings *settings,
struct config_setting *setting, struct named_setting *setting,
unsigned int row, unsigned int col ) __nonnull; unsigned int row, unsigned int col ) __nonnull;
static void draw_setting ( struct setting_widget *widget ) __nonnull; static void draw_setting ( struct setting_widget *widget ) __nonnull;
static int edit_setting ( struct setting_widget *widget, int key ) __nonnull; static int edit_setting ( struct setting_widget *widget, int key ) __nonnull;
static void init_setting_index ( struct setting_widget *widget, static void init_setting_index ( struct setting_widget *widget,
struct config_context *context, struct settings *settings,
unsigned int index ) __nonnull; unsigned int index ) __nonnull;
static void vmsg ( unsigned int row, const char *fmt, va_list args ) __nonnull; static void vmsg ( unsigned int row, const char *fmt, va_list args ) __nonnull;
static void msg ( unsigned int row, const char *fmt, ... ) __nonnull; static void msg ( unsigned int row, const char *fmt, ... ) __nonnull;
static void valert ( const char *fmt, va_list args ) __nonnull; static void valert ( const char *fmt, va_list args ) __nonnull;
static void alert ( const char *fmt, ... ) __nonnull; static void alert ( const char *fmt, ... ) __nonnull;
static void draw_info_row ( struct config_setting *setting ) __nonnull; static void draw_info_row ( struct named_setting *setting ) __nonnull;
static int main_loop ( struct config_context *context ) __nonnull; static int main_loop ( struct settings *settings ) __nonnull;
/** /**
* Load setting widget value from configuration context * Load setting widget value from configuration settings
* *
* @v widget Setting widget * @v widget Setting widget
* *
@ -117,8 +114,9 @@ static void load_setting ( struct setting_widget *widget ) {
widget->editing = 0; widget->editing = 0;
/* Read current setting value */ /* Read current setting value */
if ( show_setting ( widget->context, widget->setting, if ( get_typed_setting ( widget->settings, widget->setting->tag,
widget->value, sizeof ( widget->value ) ) < 0 ) { widget->setting->type, widget->value,
sizeof ( widget->value ) ) < 0 ) {
widget->value[0] = '\0'; widget->value[0] = '\0';
} }
@ -130,31 +128,32 @@ static void load_setting ( struct setting_widget *widget ) {
} }
/** /**
* Save setting widget value back to configuration context * Save setting widget value back to configuration settings
* *
* @v widget Setting widget * @v widget Setting widget
*/ */
static int save_setting ( struct setting_widget *widget ) { static int save_setting ( struct setting_widget *widget ) {
return set_setting ( widget->context, widget->setting, widget->value ); return set_typed_setting ( widget->settings, widget->setting->tag,
widget->setting->type, widget->value );
} }
/** /**
* Initialise setting widget * Initialise setting widget
* *
* @v widget Setting widget * @v widget Setting widget
* @v context Configuration context * @v settings Settings block
* @v setting Configuration setting * @v setting Configuration setting
* @v row Screen row * @v row Screen row
* @v col Screen column * @v col Screen column
*/ */
static void init_setting ( struct setting_widget *widget, static void init_setting ( struct setting_widget *widget,
struct config_context *context, struct settings *settings,
struct config_setting *setting, struct named_setting *setting,
unsigned int row, unsigned int col ) { unsigned int row, unsigned int col ) {
/* Initialise widget structure */ /* Initialise widget structure */
memset ( widget, 0, sizeof ( *widget ) ); memset ( widget, 0, sizeof ( *widget ) );
widget->context = context; widget->settings = settings;
widget->setting = setting; widget->setting = setting;
widget->row = row; widget->row = row;
widget->col = col; widget->col = col;
@ -219,13 +218,13 @@ static int edit_setting ( struct setting_widget *widget, int key ) {
* Initialise setting widget by index * Initialise setting widget by index
* *
* @v widget Setting widget * @v widget Setting widget
* @v context Configuration context * @v settings Settings block
* @v index Index of setting with settings list * @v index Index of setting with settings list
*/ */
static void init_setting_index ( struct setting_widget *widget, static void init_setting_index ( struct setting_widget *widget,
struct config_context *context, struct settings *settings,
unsigned int index ) { unsigned int index ) {
init_setting ( widget, context, &config_settings[index], init_setting ( widget, settings, &named_settings[index],
( SETTINGS_LIST_ROW + index ), SETTINGS_LIST_COL ); ( SETTINGS_LIST_ROW + index ), SETTINGS_LIST_COL );
} }
@ -312,11 +311,10 @@ static void draw_title_row ( void ) {
* *
* @v setting Current configuration setting * @v setting Current configuration setting
*/ */
static void draw_info_row ( struct config_setting *setting ) { static void draw_info_row ( struct named_setting *setting ) {
clearmsg ( INFO_ROW ); clearmsg ( INFO_ROW );
attron ( A_BOLD ); attron ( A_BOLD );
msg ( INFO_ROW, "%s (%s) - %s", setting->name, msg ( INFO_ROW, "%s - %s", setting->name, setting->description );
setting->type->description, setting->description );
attroff ( A_BOLD ); attroff ( A_BOLD );
} }
@ -333,11 +331,11 @@ static void draw_instruction_row ( int editing ) {
"Ctrl-C - discard changes" ); "Ctrl-C - discard changes" );
} else { } else {
msg ( INSTRUCTION_ROW, msg ( INSTRUCTION_ROW,
"Ctrl-S - save configuration" ); "Ctrl-X - exit configuration utility" );
} }
} }
static int main_loop ( struct config_context *context ) { static int main_loop ( struct settings *settings ) {
struct setting_widget widget; struct setting_widget widget;
unsigned int current = 0; unsigned int current = 0;
unsigned int next; unsigned int next;
@ -349,7 +347,7 @@ static int main_loop ( struct config_context *context ) {
draw_title_row(); draw_title_row();
color_set ( CPAIR_NORMAL, NULL ); color_set ( CPAIR_NORMAL, NULL );
for ( i = ( NUM_SETTINGS - 1 ) ; i >= 0 ; i-- ) { for ( i = ( NUM_SETTINGS - 1 ) ; i >= 0 ; i-- ) {
init_setting_index ( &widget, context, i ); init_setting_index ( &widget, settings, i );
draw_setting ( &widget ); draw_setting ( &widget );
} }
@ -394,19 +392,15 @@ static int main_loop ( struct config_context *context ) {
if ( next > 0 ) if ( next > 0 )
next--; next--;
break; break;
case CTRL_S: case CTRL_X:
if ( ( rc = nvo_save ( ugly_nvo_hack ) ) != 0){ return 0;
alert ( " Could not save options: %s ",
strerror ( rc ) );
}
return rc;
default: default:
edit_setting ( &widget, key ); edit_setting ( &widget, key );
break; break;
} }
if ( next != current ) { if ( next != current ) {
draw_setting ( &widget ); draw_setting ( &widget );
init_setting_index ( &widget, context, next ); init_setting_index ( &widget, settings, next );
current = next; current = next;
} }
} }
@ -414,7 +408,7 @@ static int main_loop ( struct config_context *context ) {
} }
int settings_ui ( struct config_context *context ) { int settings_ui ( struct settings *settings ) {
int rc; int rc;
initscr(); initscr();
@ -426,7 +420,7 @@ int settings_ui ( struct config_context *context ) {
color_set ( CPAIR_NORMAL, NULL ); color_set ( CPAIR_NORMAL, NULL );
erase(); erase();
rc = main_loop ( context ); rc = main_loop ( settings );
endwin(); endwin();

View File

@ -8,73 +8,96 @@
*/ */
#include <stdint.h> #include <stdint.h>
#include <gpxe/dhcp.h>
#include <gpxe/tables.h> #include <gpxe/tables.h>
#include <gpxe/list.h>
#include <gpxe/refcnt.h>
struct config_setting; struct settings;
struct in_addr;
/** /** Settings block operations */
* A configuration context struct settings_operations {
/** Set value of setting
* *
* This identifies the context within which settings are inspected and * @v settings Settings block
* changed. For example, the context might be global, or might be * @v tag Setting tag number
* restricted to the settings stored in NVS on a particular device. * @v data Setting data, or NULL to clear setting
* @v len Length of setting data
* @ret rc Return status code
*/ */
struct config_context { int ( * set ) ( struct settings *settings, unsigned int tag,
/** DHCP options block, or NULL const void *data, size_t len );
/** Get value of setting
* *
* If NULL, all registered DHCP options blocks will be used. * @v settings Settings block
* @v tag Setting tag number
* @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.
*/ */
struct dhcp_option_block *options; int ( * get ) ( struct settings *settings, unsigned int tag,
void *data, size_t len );
};
/** A settings block */
struct settings {
/** Reference counter */
struct refcnt *refcnt;
/** Name */
char name[16];
/** List of all settings */
struct list_head list;
/** Settings block operations */
struct settings_operations *op;
}; };
/** /**
* A configuration setting type * A setting type
* *
* This represents a type of configuration setting (e.g. string, IPv4 * This represents a type of setting (e.g. string, IPv4 address,
* address, etc.). * etc.).
*/ */
struct config_setting_type { struct setting_type {
/** Name /** Name
* *
* This is the name exposed to the user (e.g. "string"). * This is the name exposed to the user (e.g. "string").
*/ */
const char *name; const char *name;
/** Description */ /** Parse and set value of setting
const char *description;
/** Show value of setting
* *
* @v context Configuration context * @v settings Settings block
* @v setting Configuration setting * @v tag Setting tag number
* @v buf Buffer to contain value * @v value Formatted setting data
* @ret rc Return status code
*/
int ( * setf ) ( struct settings *settings, unsigned int tag,
const char *value );
/** Get and format value of setting
*
* @v settings Settings block, or NULL to search all blocks
* @v tag Setting tag number
* @v buf Buffer to contain formatted value
* @v len Length of buffer * @v len Length of buffer
* @ret len Length of formatted value, or negative error * @ret len Length of formatted value, or negative error
*/ */
int ( * show ) ( struct config_context *context, int ( * getf ) ( struct settings *settings, unsigned int tag,
struct config_setting *setting,
char *buf, size_t len ); char *buf, size_t len );
/** Set value of setting
*
* @v context Configuration context
* @v setting Configuration setting
* @v value Setting value (as a string)
* @ret rc Return status code
*/
int ( * set ) ( struct config_context *context,
struct config_setting *setting,
const char *value );
}; };
/** Declare a configuration setting type */ /** Declare a configuration setting type */
#define __config_setting_type \ #define __setting_type \
__table ( struct config_setting_type, config_setting_types, 01 ) __table ( struct setting_type, setting_types, 01 )
/** /**
* A configuration setting * A named setting
* *
* This represents a single configuration setting (e.g. "hostname"). * This represents a single setting (e.g. "hostname"), encapsulating
* the information about the setting's tag number and type.
*/ */
struct config_setting { struct named_setting {
/** Name /** Name
* *
* This is the human-readable name for the setting. Where * This is the human-readable name for the setting. Where
@ -84,71 +107,90 @@ struct config_setting {
const char *name; const char *name;
/** Description */ /** Description */
const char *description; const char *description;
/** DHCP option tag /** Setting tag number */
*
* This is the DHCP tag used to identify the option in DHCP
* packets and stored option blocks.
*/
unsigned int tag; unsigned int tag;
/** Configuration setting type /** Setting type
* *
* This identifies the type of setting (e.g. string, IPv4 * This identifies the type of setting (e.g. string, IPv4
* address, etc.). * address, etc.).
*/ */
struct config_setting_type *type; struct setting_type *type;
}; };
/** Declare a configuration setting */ /** Declare a configuration setting */
#define __config_setting __table ( struct config_setting, config_settings, 01 ) #define __named_setting __table ( struct named_setting, named_settings, 01 )
extern struct settings interactive_settings;
extern int get_setting ( struct settings *settings, unsigned int tag,
void *data, size_t len );
extern int get_setting_len ( struct settings *settings, unsigned int tag );
extern int get_string_setting ( struct settings *settings, unsigned int tag,
char *data, size_t len );
extern int get_ipv4_setting ( struct settings *settings, unsigned int tag,
struct in_addr *inp );
extern int get_int_setting ( struct settings *settings, unsigned int tag,
long *value );
extern int get_uint_setting ( struct settings *settings, unsigned int tag,
unsigned long *value );
extern struct settings * find_settings ( const char *name );
extern int set_typed_setting ( struct settings *settings,
unsigned int tag, struct setting_type *type,
const char *value );
extern int set_named_setting ( const char *name, const char *value );
extern int get_named_setting ( const char *name, char *buf, size_t len );
/** /**
* Show value of setting * Set value of setting
* *
* @v context Configuration context * @v settings Settings block
* @v setting Configuration setting * @v tag Setting tag number
* @v buf Buffer to contain value * @v data Setting data, or NULL to clear setting
* @v len Length of setting data
* @ret rc Return status code
*/
static inline int set_setting ( struct settings *settings, unsigned int tag,
const void *data, size_t len ) {
return settings->op->set ( settings, tag, data, len );
}
/**
* Delete setting
*
* @v settings Settings block
* @v tag Setting tag number
* @ret rc Return status code
*/
static inline int delete_setting ( struct settings *settings,
unsigned int tag ) {
return set_setting ( settings, tag, NULL, 0 );
}
/**
* Get and format value of setting
*
* @v settings Settings block, or NULL to search all blocks
* @v tag Setting tag number
* @v type Settings type
* @v buf Buffer to contain formatted value
* @v len Length of buffer * @v len Length of buffer
* @ret len Length of formatted value, or negative error * @ret len Length of formatted value, or negative error
*/ */
static inline int show_setting ( struct config_context *context, static inline int get_typed_setting ( struct settings *settings,
struct config_setting *setting, unsigned int tag,
struct setting_type *type,
char *buf, size_t len ) { char *buf, size_t len ) {
return setting->type->show ( context, setting, buf, len ); return type->getf ( settings, tag, buf, len );
} }
extern int set_setting ( struct config_context *context,
struct config_setting *setting,
const char *value );
/** /**
* Clear setting * Delete named setting
* *
* @v context Configuration context * @v name Name of setting
* @v setting Configuration setting
* @ret rc Return status code * @ret rc Return status code
*/ */
static inline int clear_setting ( struct config_context *context, static inline int delete_named_setting ( const char *name ) {
struct config_setting *setting ) { return set_named_setting ( name, NULL );
delete_dhcp_option ( context->options, setting->tag );
return 0;
}
/* Function prototypes */
extern int show_named_setting ( struct config_context *context,
const char *name, char *buf, size_t len );
extern int set_named_setting ( struct config_context *context,
const char *name, const char *value );
/**
* Clear named setting
*
* @v context Configuration context
* @v name Configuration setting name
* @ret rc Return status code
*/
static inline int clear_named_setting ( struct config_context *context,
const char *name ) {
return set_named_setting ( context, name, NULL );
} }
#endif /* _GPXE_SETTINGS_H */ #endif /* _GPXE_SETTINGS_H */

View File

@ -7,8 +7,8 @@
* *
*/ */
struct config_context; struct settings;
extern int settings_ui ( struct config_context *context ) __nonnull; extern int settings_ui ( struct settings *settings ) __nonnull;
#endif /* _GPXE_SETTINGS_UI_H */ #endif /* _GPXE_SETTINGS_UI_H */