From d13dbb6f1d5e28737a3319af035a6cb991bc6f8f Mon Sep 17 00:00:00 2001 From: David Horstmann Date: Mon, 1 Mar 2021 19:34:37 +0000 Subject: [PATCH] feat(fdt): introduce wrapper function to read DT UUIDs TF-A does not have the capability to read UUIDs in string form from the device tree. This capability is useful for readability, so add a wrapper function, fdtw_read_uuid() to parse UUIDs from the DT. This function should parse a string of the form: "aabbccdd-eeff-4099-8877-665544332211" to the byte sequence in memory: [aa bb cc dd ee ff 40 99 88 77 66 55 44 33 22 11] Change-Id: I99a92fbeb40f4f4713f3458b36cb3863354d2bdf Signed-off-by: David Horstmann --- common/fdt_wrappers.c | 37 +++++++++- common/uuid.c | 133 ++++++++++++++++++++++++++++++++++ include/common/fdt_wrappers.h | 4 +- include/common/uuid.h | 15 ++++ 4 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 common/uuid.c create mode 100644 include/common/uuid.h diff --git a/common/fdt_wrappers.c b/common/fdt_wrappers.c index 5aad14e38..dd7a0faef 100644 --- a/common/fdt_wrappers.c +++ b/common/fdt_wrappers.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved. + * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ @@ -7,12 +7,14 @@ /* Helper functions to offer easier navigation of Device Tree Blob */ #include +#include #include #include #include #include +#include /* * Read cells from a given property of the given node. Any number of 32-bit @@ -151,6 +153,39 @@ int fdtw_read_string(const void *dtb, int node, const char *prop, return 0; } +/* + * Read UUID from a given property of the given node. Returns 0 on success, + * and a negative value upon error. + */ +int fdtw_read_uuid(const void *dtb, int node, const char *prop, + unsigned int length, uint8_t *uuid) +{ + /* Buffer for UUID string (plus NUL terminator) */ + char uuid_string[UUID_STRING_LENGTH + 1U]; + int err; + + assert(dtb != NULL); + assert(prop != NULL); + assert(uuid != NULL); + assert(node >= 0); + + if (length < UUID_BYTES_LENGTH) { + return -EINVAL; + } + + err = fdtw_read_string(dtb, node, prop, uuid_string, + UUID_STRING_LENGTH + 1U); + if (err != 0) { + return err; + } + + if (read_uuid(uuid, uuid_string) != 0) { + return -FDT_ERR_BADVALUE; + } + + return 0; +} + /* * Write cells in place to a given property of the given node. At most 2 cells * of the property are written. Returns 0 on success, and -1 upon error. diff --git a/common/uuid.c b/common/uuid.c new file mode 100644 index 000000000..dd3c7b02f --- /dev/null +++ b/common/uuid.c @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include + +#include +#include + +/* Return the hex nibble value of a char */ +static int8_t hex_val(char hex) +{ + int8_t val = 0; + + if ((hex >= '0') && (hex <= '9')) { + val = (int8_t)(hex - '0'); + } else if ((hex >= 'a') && (hex <= 'f')) { + val = (int8_t)(hex - 'a' + 0xa); + } else if ((hex >= 'A') && (hex <= 'F')) { + val = (int8_t)(hex - 'A' + 0xa); + } else { + val = -1; + } + + return val; +} + +/* + * Read hex_src_len hex characters from hex_src, convert to bytes and + * store in buffer pointed to by dest + */ +static int read_hex(uint8_t *dest, char *hex_src, unsigned int hex_src_len) +{ + int8_t nibble; + uint8_t byte; + + /* + * The string length must be a multiple of 2 to represent an + * exact number of bytes. + */ + assert((hex_src_len % 2U) == 0U); + + for (unsigned int i = 0U; i < (hex_src_len / 2U); i++) { + nibble = 0; + byte = 0U; + + nibble = hex_val(hex_src[2U * i]); + if (nibble < 0) { + return -1; + } + byte = (uint8_t)nibble; + byte <<= 4U; + + nibble = hex_val(hex_src[(2U * i) + 1U]); + if (nibble < 0) { + return -1; + } + byte |= (uint8_t)nibble; + + *dest = byte; + dest++; + } + + return 0; +} + +/* Parse UUIDs of the form aabbccdd-eeff-4099-8877-665544332211 */ +int read_uuid(uint8_t *dest, char *uuid) +{ + int err; + + /* Check that we have enough characters */ + if (strnlen(uuid, UUID_STRING_LENGTH) != UUID_STRING_LENGTH) { + WARN("UUID string is too short\n"); + return -EINVAL; + } + + /* aabbccdd */ + err = read_hex(dest, uuid, 8); + uuid += 8; + dest += 4; + + /* Check for '-' */ + err |= ((*uuid == '-') ? 0 : -1); + uuid++; + + /* eeff */ + err |= read_hex(dest, uuid, 4); + uuid += 4; + dest += 2; + + /* Check for '-' */ + err |= ((*uuid == '-') ? 0 : -1); + uuid++; + + /* 4099 */ + err |= read_hex(dest, uuid, 4); + uuid += 4; + dest += 2; + + /* Check for '-' */ + err |= ((*uuid == '-') ? 0 : -1); + uuid++; + + /* 8877 */ + err |= read_hex(dest, uuid, 4); + uuid += 4; + dest += 2; + + /* Check for '-' */ + err |= ((*uuid == '-') ? 0 : -1); + uuid++; + + /* 665544332211 */ + err |= read_hex(dest, uuid, 12); + uuid += 12; + dest += 6; + + if (err < 0) { + WARN("Error parsing UUID\n"); + /* Clear the buffer on error */ + memset((void *)dest, '\0', UUID_BYTES_LENGTH * sizeof(uint8_t)); + return -EINVAL; + } + + return 0; +} + diff --git a/include/common/fdt_wrappers.h b/include/common/fdt_wrappers.h index a571092e3..e8b39335d 100644 --- a/include/common/fdt_wrappers.h +++ b/include/common/fdt_wrappers.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved. + * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ @@ -24,6 +24,8 @@ int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name, unsigned int cells, uint32_t *value); int fdtw_read_string(const void *dtb, int node, const char *prop, char *str, size_t size); +int fdtw_read_uuid(const void *dtb, int node, const char *prop, + unsigned int length, uint8_t *uuid); int fdtw_write_inplace_cells(void *dtb, int node, const char *prop, unsigned int cells, void *value); int fdtw_read_bytes(const void *dtb, int node, const char *prop, diff --git a/include/common/uuid.h b/include/common/uuid.h new file mode 100644 index 000000000..5651d0d58 --- /dev/null +++ b/include/common/uuid.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef UUID_H +#define UUID_H + +#define UUID_BYTES_LENGTH 16 +#define UUID_STRING_LENGTH 36 + +int read_uuid(uint8_t *dest, char *uuid); + +#endif /* UUID_H */