lib/uuid: Add UUID parsing function

Implement a simple function that parses a canonical UUID string into the
common byte representation. Inspired by acpigen_write_uuid().

Change-Id: Ia1bd883c740873699814fde6c6ddc1937a40093e
Signed-off-by: Nico Huber <nico.huber@secunet.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/36297
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
This commit is contained in:
Nico Huber 2019-10-24 15:01:33 +02:00 committed by Patrick Georgi
parent f71bb5d174
commit 2e6a0f8052
3 changed files with 53 additions and 0 deletions

View File

@ -18,6 +18,22 @@
#include <string.h> #include <string.h>
#define UUID_LEN 16
#define UUID_STRLEN 36
/*
* Parses a canonical UUID string into the common byte representation
* where the first three words are interpreted as little endian:
*
* The UUID
* "00112233-4455-6677-8899-aabbccddeeff"
* is stored as
* 33 22 11 00 55 44 77 66 88 99 aa bb cc dd ee ff
*
* Returns negative value on error, 0 on success.
*/
int parse_uuid(uint8_t *uuid, const char *uuid_str);
typedef struct { typedef struct {
uint8_t b[16]; uint8_t b[16];
} __packed guid_t; } __packed guid_t;

View File

@ -346,3 +346,5 @@ cbfs-files-$(CONFIG_GENERIC_SPD_BIN) += spd.bin
spd.bin-file := $(LIB_SPD_BIN) spd.bin-file := $(LIB_SPD_BIN)
spd.bin-type := spd spd.bin-type := spd
endif endif
ramstage-y += uuid.c

35
src/lib/uuid.c Normal file
View File

@ -0,0 +1,35 @@
/*
* This file is part of the coreboot project.
*
* 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; version 2 of the License.
*
* 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.
*/
#include <stdint.h>
#include <lib.h>
#include <uuid.h>
int parse_uuid(uint8_t *const uuid, const char *const uuid_str)
{
const uint8_t order[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };
uint8_t uuid_binstr[UUID_LEN];
unsigned int i;
if (strlen(uuid_str) != UUID_STRLEN)
return -1;
if (uuid_str[8] != '-' || uuid_str[13] != '-' ||
uuid_str[18] != '-' || uuid_str[23] != '-')
return -1;
if (hexstrtobin(uuid_str, uuid_binstr, UUID_LEN) != UUID_LEN)
return -1;
for (i = 0; i < UUID_LEN; ++i)
uuid[i] = uuid_binstr[order[i]];
return 0;
}