Debian Codegraph @

src/boot/device-path-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "device-path-util.h"
4 #include "efi-string.h"
5 #include "string-util.h"
6 #include "util.h"
7
8 static const EFI_DEVICE_PATH *device_path_find_end_node(const EFI_DEVICE_PATH *dp) {
9 while (!device_path_is_end(dp))
10 dp = device_path_next_node(dp);
11 return dp;
12 }
13
14 EFI_STATUS make_file_device_path(EFI_HANDLE device, const char16_t *file, EFI_DEVICE_PATH **ret_dp) {
15 EFI_STATUS err;
16 EFI_DEVICE_PATH *dp;
17
18 assert(file);
19 assert(ret_dp);
20
21 err = BS->HandleProtocol(device, MAKE_GUID_PTR(EFI_DEVICE_PATH_PROTOCOL), (void **) &dp);
22 if (err != EFI_SUCCESS)
23 return err;
24
25 const EFI_DEVICE_PATH *end_node = device_path_find_end_node(dp);
26
27 size_t file_size = strsize16(file);
28 size_t dp_size = (uint8_t *) end_node - (uint8_t *) dp;
29
30 /* Make a copy that can also hold a file media device path. */
31 *ret_dp = xmalloc(dp_size + file_size + sizeof(FILEPATH_DEVICE_PATH) + sizeof(EFI_DEVICE_PATH));
32 dp = mempcpy(*ret_dp, dp, dp_size);
33
34 FILEPATH_DEVICE_PATH *file_dp = (FILEPATH_DEVICE_PATH *) dp;
35 file_dp->Header = (EFI_DEVICE_PATH) {
36 .Type = MEDIA_DEVICE_PATH,
37 .SubType = MEDIA_FILEPATH_DP,
38 .Length = sizeof(FILEPATH_DEVICE_PATH) + file_size,
39 };
40 memcpy(file_dp->PathName, file, file_size);
41
42 dp = device_path_next_node(dp);
43 *dp = DEVICE_PATH_END_NODE;
44 return EFI_SUCCESS;
45 }
46
47 EFI_STATUS make_url_device_path(const char16_t *url, EFI_DEVICE_PATH **ret) {
48 assert(url);
49 assert(ret);
50
51 /* Turns a URL into a simple one-element URL device path. */
52
53 _cleanup_free_ char* u = xstr16_to_ascii(url);
54 if (!u)
55 return EFI_INVALID_PARAMETER;
56
57 size_t l = strlen8(u);
58
59 size_t t = offsetof(URI_DEVICE_PATH, Uri) + l + sizeof(EFI_DEVICE_PATH);
60 EFI_DEVICE_PATH *dp = xmalloc(t);
61
62 URI_DEVICE_PATH *udp = (URI_DEVICE_PATH*) dp;
63 udp->Header = (EFI_DEVICE_PATH) {
64 .Type = MESSAGING_DEVICE_PATH,
65 .SubType = MSG_URI_DP,
66 .Length = offsetof(URI_DEVICE_PATH, Uri) + l,
67 };
68 memcpy(udp->Uri, u, l);
69
70 EFI_DEVICE_PATH *end = device_path_next_node(dp);
71 *end = DEVICE_PATH_END_NODE;
72
73 assert(((uint8_t*) end + sizeof(EFI_DEVICE_PATH)) == ((uint8_t*) dp + t));
74
75 *ret = TAKE_PTR(dp);
76 return EFI_SUCCESS;
77 }
78
79 static char16_t *device_path_to_str_internal(const EFI_DEVICE_PATH *dp) {
80 char16_t *str = NULL;
81
82 for (const EFI_DEVICE_PATH *node = dp; !device_path_is_end(node); node = device_path_next_node(node)) {
83 _cleanup_free_ char16_t *old = str;
84
85 if (node->Type == END_DEVICE_PATH_TYPE && node->SubType == END_INSTANCE_DEVICE_PATH_SUBTYPE) {
86 str = xasprintf("%ls%s,", strempty(old), old ? "\\" : "");
87 continue;
88 }
89
90 /* Special-case this so that FilePath-only device path string look and behave nicely. */
91 if (node->Type == MEDIA_DEVICE_PATH && node->SubType == MEDIA_FILEPATH_DP) {
92 str = xasprintf("%ls%s%ls",
93 strempty(old),
94 old ? "\\" : "",
95 ((FILEPATH_DEVICE_PATH *) node)->PathName);
96 continue;
97 }
98
99 /* Instead of coding all the different types and sub-types here we just use the
100 * generic node form. This function is a best-effort for firmware that does not
101 * provide the EFI_DEVICE_PATH_TO_TEXT_PROTOCOL after all. */
102
103 size_t size = node->Length - sizeof(EFI_DEVICE_PATH);
104 _cleanup_free_ char16_t *hex_data = hexdump((uint8_t *) node + sizeof(EFI_DEVICE_PATH), size);
105 str = xasprintf("%ls%sPath(%u,%u%s%ls)",
106 strempty(old),
107 old ? "/" : "",
108 node->Type,
109 node->SubType,
110 size == 0 ? "" : ",",
111 hex_data);
112 }
113
114 return str;
115 }
116
117 EFI_STATUS device_path_to_str(const EFI_DEVICE_PATH *dp, char16_t **ret) {
118 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *dp_to_text;
119 EFI_STATUS err;
120 _cleanup_free_ char16_t *str = NULL;
121
122 assert(dp);
123 assert(ret);
124
125 err = BS->LocateProtocol(MAKE_GUID_PTR(EFI_DEVICE_PATH_TO_TEXT_PROTOCOL), NULL, (void **) &dp_to_text);
126 if (err != EFI_SUCCESS) {
127 *ret = device_path_to_str_internal(dp);
128 return EFI_SUCCESS;
129 }
130
131 str = dp_to_text->ConvertDevicePathToText(dp, /* DisplayOnly= */ false, /* AllowShortcuts= */ false);
132 if (!str)
133 return EFI_OUT_OF_RESOURCES;
134
135 *ret = TAKE_PTR(str);
136 return EFI_SUCCESS;
137 }
138
139 bool device_path_startswith(const EFI_DEVICE_PATH *dp, const EFI_DEVICE_PATH *start) {
140 if (!start)
141 return true;
142 if (!dp)
143 return false;
144 for (;;) {
145 if (device_path_is_end(start))
146 return true;
147 if (device_path_is_end(dp))
148 return false;
149 if (start->Length != dp->Length)
150 return false;
151 if (memcmp(dp, start, start->Length) != 0)
152 return false;
153 start = device_path_next_node(start);
154 dp = device_path_next_node(dp);
155 }
156 }
157
158 EFI_DEVICE_PATH *device_path_replace_node(
159 const EFI_DEVICE_PATH *path, const EFI_DEVICE_PATH *node, const EFI_DEVICE_PATH *new_node) {
160
161 /* Create a new device path as a copy of path, while chopping off the remainder starting at the given
162 * node. If new_node is provided, it is appended at the end of the new path. */
163
164 assert(path);
165
166 if (!node)
167 node = device_path_find_end_node(path);
168
169 size_t len = (uint8_t *) node - (uint8_t *) path;
170 EFI_DEVICE_PATH *ret = xmalloc(len + (new_node ? new_node->Length : 0) + sizeof(EFI_DEVICE_PATH));
171 EFI_DEVICE_PATH *end = mempcpy(ret, path, len);
172
173 if (new_node)
174 end = mempcpy(end, new_node, new_node->Length);
175
176 *end = DEVICE_PATH_END_NODE;
177 return ret;
178 }
179
180 size_t device_path_size(const EFI_DEVICE_PATH *dp) {
181 const EFI_DEVICE_PATH *i = ASSERT_PTR(dp);
182
183 for (; !device_path_is_end(i); i = device_path_next_node(i))
184 ;
185
186 return (const uint8_t*) i - (const uint8_t*) dp + sizeof(EFI_DEVICE_PATH);
187 }
188
189 EFI_DEVICE_PATH *device_path_dup(const EFI_DEVICE_PATH *dp) {
190 return xmemdup(ASSERT_PTR(dp), device_path_size(dp));
191 }