Skip to content

Commit 4e057c3

Browse files
ausyskinwenlingz
authored andcommitted
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 <linux/uuid.h> header. Tracked-On: #1536 Signed-off-by: Alexander Usyskin <alexander.usyskin@intel.com> Signed-off-by: Tomas Winkler <tomas.winkler@intel.com> Acked-by: Wang, Yu1 <yu1.wang@intel.com>
1 parent d141aeb commit 4e057c3

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*
6+
*/
7+
/*
8+
* MEI device virtualization.
9+
*/
10+
11+
#include <sys/types.h>
12+
#include <sys/stat.h>
13+
#include <sys/ioctl.h>
14+
#include <sys/queue.h>
15+
#include <sys/uio.h>
16+
#include <fcntl.h>
17+
#include <unistd.h>
18+
#include <stddef.h>
19+
#include <limits.h>
20+
#include <stdio.h>
21+
#include <string.h>
22+
#include <dirent.h>
23+
#include <stdlib.h>
24+
#include <ctype.h>
25+
#include <errno.h>
26+
#include <stdbool.h>
27+
#include <stdint.h>
28+
#include <assert.h>
29+
#include <pthread.h>
30+
#include <time.h>
31+
32+
#include <linux/uuid.h>
33+
#include <linux/mei.h>
34+
35+
#include "types.h"
36+
#include "vmmapi.h"
37+
#include "mevent.h"
38+
#include "pci_core.h"
39+
#include "virtio.h"
40+
#include "dm.h"
41+
42+
#include "mei.h"
43+
44+
#ifndef UUID_STR_LEN
45+
#define UUID_STR_LEN 37
46+
#endif
47+
48+
#ifndef GUID_INIT
49+
#define GUID_INIT(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \
50+
UUID_LE(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7)
51+
#endif
52+
53+
static int guid_parse(const char *str, size_t maxlen, guid_t *guid)
54+
{
55+
const char *p = "00000000-0000-0000-0000-000000000000";
56+
const size_t len = strnlen(p, UUID_STR_LEN);
57+
uint32_t a;
58+
uint16_t b, c;
59+
uint8_t d[2], e[6];
60+
char buf[3];
61+
unsigned int i;
62+
63+
if (strnlen(str, maxlen) != len)
64+
return -1;
65+
66+
for (i = 0; i < len; i++) {
67+
if (str[i] == '-') {
68+
if (p[i] == '-')
69+
continue;
70+
else
71+
return -1;
72+
} else if (!isxdigit(str[i])) {
73+
return -1;
74+
}
75+
}
76+
77+
a = strtoul(str + 0, NULL, 16);
78+
b = strtoul(str + 9, NULL, 16);
79+
c = strtoul(str + 14, NULL, 16);
80+
81+
buf[2] = 0;
82+
for (i = 0; i < 2; i++) {
83+
buf[0] = str[19 + i * 2];
84+
buf[1] = str[19 + i * 2 + 1];
85+
d[i] = strtoul(buf, NULL, 16);
86+
}
87+
88+
for (i = 0; i < 6; i++) {
89+
buf[0] = str[24 + i * 2];
90+
buf[1] = str[24 + i * 2 + 1];
91+
e[i] = strtoul(buf, NULL, 16);
92+
}
93+
94+
*guid = GUID_INIT(a, b, c,
95+
d[0], d[1], e[0], e[1], e[2], e[3], e[4], e[5]);
96+
97+
return 0;
98+
}
99+
100+
static int guid_unparse(const guid_t *guid, char *str, size_t len)
101+
{
102+
unsigned int i;
103+
size_t pos = 0;
104+
105+
if (len < UUID_STR_LEN)
106+
return -EINVAL;
107+
108+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[3]);
109+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[2]);
110+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[1]);
111+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[0]);
112+
str[pos] = '-';
113+
pos++;
114+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[5]);
115+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[4]);
116+
str[pos] = '-';
117+
pos++;
118+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[7]);
119+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[6]);
120+
str[pos] = '-';
121+
pos++;
122+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[8]);
123+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[9]);
124+
str[pos] = '-';
125+
pos++;
126+
for (i = 10; i < 16; i++)
127+
pos += snprintf(str + pos, len - pos, "%02x", guid->b[i]);
128+
129+
return 0;
130+
}
131+

0 commit comments

Comments
 (0)