From 4e057c32d27de281ac59e90a78e8305fdb9e2eac Mon Sep 17 00:00:00 2001 From: Alexander Usyskin Date: Mon, 15 Oct 2018 21:26:46 +0300 Subject: [PATCH] dm: mei: add guid handling functions libuuid sports only uuid (big endian encoding), though mei requires guids (little endian encoding). The base types are based on header. Tracked-On: #1536 Signed-off-by: Alexander Usyskin Signed-off-by: Tomas Winkler Acked-by: Wang, Yu1 --- devicemodel/hw/pci/virtio/virtio_mei.c | 131 +++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 devicemodel/hw/pci/virtio/virtio_mei.c diff --git a/devicemodel/hw/pci/virtio/virtio_mei.c b/devicemodel/hw/pci/virtio/virtio_mei.c new file mode 100644 index 000000000..f66d88263 --- /dev/null +++ b/devicemodel/hw/pci/virtio/virtio_mei.c @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + * + */ +/* + * MEI device virtualization. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "types.h" +#include "vmmapi.h" +#include "mevent.h" +#include "pci_core.h" +#include "virtio.h" +#include "dm.h" + +#include "mei.h" + +#ifndef UUID_STR_LEN +#define UUID_STR_LEN 37 +#endif + +#ifndef GUID_INIT +#define GUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ + UUID_LE(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) +#endif + +static int guid_parse(const char *str, size_t maxlen, guid_t *guid) +{ + const char *p = "00000000-0000-0000-0000-000000000000"; + const size_t len = strnlen(p, UUID_STR_LEN); + uint32_t a; + uint16_t b, c; + uint8_t d[2], e[6]; + char buf[3]; + unsigned int i; + + if (strnlen(str, maxlen) != len) + return -1; + + for (i = 0; i < len; i++) { + if (str[i] == '-') { + if (p[i] == '-') + continue; + else + return -1; + } else if (!isxdigit(str[i])) { + return -1; + } + } + + a = strtoul(str + 0, NULL, 16); + b = strtoul(str + 9, NULL, 16); + c = strtoul(str + 14, NULL, 16); + + buf[2] = 0; + for (i = 0; i < 2; i++) { + buf[0] = str[19 + i * 2]; + buf[1] = str[19 + i * 2 + 1]; + d[i] = strtoul(buf, NULL, 16); + } + + for (i = 0; i < 6; i++) { + buf[0] = str[24 + i * 2]; + buf[1] = str[24 + i * 2 + 1]; + e[i] = strtoul(buf, NULL, 16); + } + + *guid = GUID_INIT(a, b, c, + d[0], d[1], e[0], e[1], e[2], e[3], e[4], e[5]); + + return 0; +} + +static int guid_unparse(const guid_t *guid, char *str, size_t len) +{ + unsigned int i; + size_t pos = 0; + + if (len < UUID_STR_LEN) + return -EINVAL; + + pos += snprintf(str + pos, len - pos, "%02x", guid->b[3]); + pos += snprintf(str + pos, len - pos, "%02x", guid->b[2]); + pos += snprintf(str + pos, len - pos, "%02x", guid->b[1]); + pos += snprintf(str + pos, len - pos, "%02x", guid->b[0]); + str[pos] = '-'; + pos++; + pos += snprintf(str + pos, len - pos, "%02x", guid->b[5]); + pos += snprintf(str + pos, len - pos, "%02x", guid->b[4]); + str[pos] = '-'; + pos++; + pos += snprintf(str + pos, len - pos, "%02x", guid->b[7]); + pos += snprintf(str + pos, len - pos, "%02x", guid->b[6]); + str[pos] = '-'; + pos++; + pos += snprintf(str + pos, len - pos, "%02x", guid->b[8]); + pos += snprintf(str + pos, len - pos, "%02x", guid->b[9]); + str[pos] = '-'; + pos++; + for (i = 10; i < 16; i++) + pos += snprintf(str + pos, len - pos, "%02x", guid->b[i]); + + return 0; +} +