summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Brown2024-02-29 15:04:47 +0100
committerMichael Brown2024-02-29 16:20:44 +0100
commit0eb8fbd0bfc9331ecdcd74c9406251e5a6e450af (patch)
treed97117bda5d15127bbb272ef70a007ee14aebc62
parent[uuid] Add uuid_aton() to parse a UUID from a string (diff)
downloadipxe-0eb8fbd0bfc9331ecdcd74c9406251e5a6e450af.tar.gz
ipxe-0eb8fbd0bfc9331ecdcd74c9406251e5a6e450af.tar.xz
ipxe-0eb8fbd0bfc9331ecdcd74c9406251e5a6e450af.zip
[settings] Add parsing for UUID and GUID settings types
The ":uuid" and ":guid" settings types are currently format-only: it is possible to format a setting as a UUID (via e.g. "show foo:uuid") but it is not currently possible to parse a string into a UUID setting (via e.g. "set foo:uuid 406343fe-998b-44be-8a28-44ca38cb202b"). Use uuid_aton() to implement parsing of these settings types, and add appropriate test cases for both. Signed-off-by: Michael Brown <mcb30@ipxe.org>
-rw-r--r--src/core/settings.c33
-rw-r--r--src/tests/settings_test.c14
2 files changed, 44 insertions, 3 deletions
diff --git a/src/core/settings.c b/src/core/settings.c
index da075baa..4593876f 100644
--- a/src/core/settings.c
+++ b/src/core/settings.c
@@ -2195,6 +2195,37 @@ const struct setting_type setting_type_base64 __setting_type = {
};
/**
+ * Parse UUID/GUID setting value
+ *
+ * @v type Setting type
+ * @v value Formatted setting value
+ * @v buf Buffer to contain raw value
+ * @v len Length of buffer
+ * @v size Integer size, in bytes
+ * @ret len Length of raw value, or negative error
+ */
+static int parse_uuid_setting ( const struct setting_type *type,
+ const char *value, void *buf, size_t len ) {
+ union uuid uuid;
+ int rc;
+
+ /* Parse UUID */
+ if ( ( rc = uuid_aton ( value, &uuid ) ) != 0 )
+ return rc;
+
+ /* Mangle GUID byte ordering */
+ if ( type == &setting_type_guid )
+ uuid_mangle ( &uuid );
+
+ /* Copy value */
+ if ( len > sizeof ( uuid ) )
+ len = sizeof ( uuid );
+ memcpy ( buf, uuid.raw, len );
+
+ return ( sizeof ( uuid ) );
+}
+
+/**
* Format UUID/GUID setting value
*
* @v type Setting type
@@ -2227,12 +2258,14 @@ static int format_uuid_setting ( const struct setting_type *type,
/** UUID setting type */
const struct setting_type setting_type_uuid __setting_type = {
.name = "uuid",
+ .parse = parse_uuid_setting,
.format = format_uuid_setting,
};
/** GUID setting type */
const struct setting_type setting_type_guid __setting_type = {
.name = "guid",
+ .parse = parse_uuid_setting,
.format = format_uuid_setting,
};
diff --git a/src/tests/settings_test.c b/src/tests/settings_test.c
index 5da7eb00..edd7b9d7 100644
--- a/src/tests/settings_test.c
+++ b/src/tests/settings_test.c
@@ -420,13 +420,21 @@ static void settings_test_exec ( void ) {
RAW ( 0x80, 0x81, 0x82, 0x83, 0x84, 0x00, 0xff ),
"gIGCg4QA/w==" );
- /* "uuid" setting type (no store capability) */
+ /* "uuid" setting type */
+ storef_ok ( &test_settings, &test_uuid_setting,
+ "36d22ed9-b64f-4fdb-941b-a54a0854f991",
+ RAW ( 0x36, 0xd2, 0x2e, 0xd9, 0xb6, 0x4f, 0x4f, 0xdb, 0x94,
+ 0x1b, 0xa5, 0x4a, 0x08, 0x54, 0xf9, 0x91 ) );
+ storef_ok ( &test_settings, &test_guid_setting,
+ "7ad4478f-c270-4601-a245-78598f25a984",
+ RAW ( 0x8f, 0x47, 0xd4, 0x7a, 0x70, 0xc2, 0x01, 0x46, 0xa2,
+ 0x45, 0x78, 0x59, 0x8f, 0x25, 0xa9, 0x84 ) );
fetchf_ok ( &test_settings, &test_uuid_setting,
- RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
+ RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a, 0xa8,
0x7a, 0x7c, 0xfe, 0x4f, 0xca, 0x4a, 0x57 ),
"1a6a749d-0eda-461a-a87a-7cfe4fca4a57" );
fetchf_ok ( &test_settings, &test_guid_setting,
- RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a,0xa8,
+ RAW ( 0x1a, 0x6a, 0x74, 0x9d, 0x0e, 0xda, 0x46, 0x1a, 0xa8,
0x7a, 0x7c, 0xfe, 0x4f, 0xca, 0x4a, 0x57 ),
"9d746a1a-da0e-1a46-a87a-7cfe4fca4a57" );