diff --git a/src/core/settings.c b/src/core/settings.c index e80355c7..c84e1bc2 100644 --- a/src/core/settings.c +++ b/src/core/settings.c @@ -676,6 +676,46 @@ int fetch_setting_len ( struct settings *settings, struct setting *setting ) { return fetch_setting ( settings, setting, NULL, 0 ); } +/** + * Fetch copy of 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 data + * @ret len Length of setting, or negative error + * + * The caller is responsible for eventually freeing the allocated + * buffer. + * + * To allow the caller to distinguish between a non-existent setting + * and an error in allocating memory for the copy, this function will + * return success (and a NULL buffer pointer) for a non-existent + * setting. + */ +int fetch_setting_copy ( struct settings *settings, struct setting *setting, + void **data ) { + int len; + int check_len = 0; + + /* Avoid returning uninitialised data on error */ + *data = NULL; + + /* Fetch setting length, and return success if non-existent */ + len = fetch_setting_len ( settings, setting ); + if ( len < 0 ) + return 0; + + /* Allocate buffer */ + *data = malloc ( len ); + if ( ! *data ) + return -ENOMEM; + + /* Fetch setting */ + check_len = fetch_setting ( settings, setting, *data, len ); + assert ( check_len == len ); + return len; +} + /** * Fetch value of string setting * diff --git a/src/include/ipxe/settings.h b/src/include/ipxe/settings.h index 1f4af17f..34c0ed47 100644 --- a/src/include/ipxe/settings.h +++ b/src/include/ipxe/settings.h @@ -240,6 +240,8 @@ extern struct settings * fetch_setting_origin ( struct settings *settings, struct setting *setting ); extern int fetch_setting_len ( struct settings *settings, struct setting *setting ); +extern int fetch_setting_copy ( struct settings *settings, + struct setting *setting, void **data ); extern int fetch_string_setting ( struct settings *settings, struct setting *setting, char *data, size_t len );