xref: /aosp_15_r20/external/coreboot/util/smmstoretool/guids.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "guids.h"
4 
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include <commonlib/bsd/helpers.h>
9 
10 #include "udk2017.h"
11 #include "utils.h"
12 
13 const struct guid_alias_t known_guids[] = {
14 	{
15 		"coreboot",
16 		{
17 			0xceae4c1d, 0x335b, 0x4685,
18 			{ 0xa4, 0xa0, 0xfc, 0x4a, 0x94, 0xee, 0xa0, 0x85 }
19 		},
20 	},
21 	{
22 		"dasharo",
23 		{
24 			0xd15b327e, 0xff2d, 0x4fc1,
25 			{ 0xab, 0xf6, 0xc1, 0x2b, 0xd0, 0x8c, 0x13, 0x59 }
26 		},
27 	},
28 	{
29 		"global",
30 		{
31 			0x8be4df61, 0x93ca, 0x11d2,
32 			{ 0xaa, 0x0d, 0x00, 0xe0, 0x98, 0x03, 0x2b, 0x8c }
33 		},
34 	},
35 	{
36 		"microsoft",
37 		{
38 			0x77fa9abd, 0x0359, 0x4d32,
39 			{ 0xbd, 0x60, 0x28, 0xf4, 0xe7, 0x8f, 0x78, 0x4b }
40 		},
41 	},
42 };
43 
44 const int known_guid_count = ARRAY_SIZE(known_guids);
45 
format_guid(const EFI_GUID * guid,bool use_alias)46 char *format_guid(const EFI_GUID *guid, bool use_alias)
47 {
48 	if (use_alias) {
49 		for (int i = 0; i < known_guid_count; ++i) {
50 			const struct guid_alias_t *known_guid = &known_guids[i];
51 			if (memcmp(&known_guid->guid, guid, sizeof(*guid)) == 0)
52 				return strdup(known_guid->alias);
53 		}
54 	}
55 
56 	char *str = xmalloc(GUID_LEN + 1);
57 	snprintf(str, GUID_LEN + 1,
58 		 "%08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x",
59 		 guid->Data1, guid->Data2, guid->Data3,
60 		 guid->Data4[0], guid->Data4[1],
61 		 guid->Data4[2], guid->Data4[3],
62 		 guid->Data4[4], guid->Data4[5],
63 		 guid->Data4[6], guid->Data4[7]);
64 	return str;
65 }
66 
parse_guid(const char str[],EFI_GUID * guid)67 bool parse_guid(const char str[], EFI_GUID *guid)
68 {
69 	for (int i = 0; i < known_guid_count; ++i) {
70 		const struct guid_alias_t *known_guid = &known_guids[i];
71 		if (str_eq(known_guid->alias, str)) {
72 			*guid = known_guid->guid;
73 			return true;
74 		}
75 	}
76 
77 	if (strlen(str) != GUID_LEN)
78 		return false;
79 
80 	int n = sscanf(str,
81 		       "%08x-%04hx-%04hx-"
82 		       "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx",
83 		       &guid->Data1, &guid->Data2, &guid->Data3,
84 		       &guid->Data4[0], &guid->Data4[1],
85 		       &guid->Data4[2], &guid->Data4[3],
86 		       &guid->Data4[4], &guid->Data4[5],
87 		       &guid->Data4[6], &guid->Data4[7]);
88 	return n == 11;
89 }
90