1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI device path interface
4  *
5  *  Copyright (c) 2017 Leif Lindholm
6  */
7 
8 #define LOG_CATEGORY LOGC_EFI
9 
10 #include <efi_device_path.h>
11 #include <efi_loader.h>
12 
13 const efi_guid_t efi_guid_device_path_utilities_protocol =
14 		EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID;
15 
16 /*
17  * Get size of a device path.
18  *
19  * This function implements the GetDevicePathSize service of the device path
20  * utilities protocol. The device path length includes the end of path tag
21  * which may be an instance end.
22  *
23  * See the Unified Extensible Firmware Interface (UEFI) specification
24  * for details.
25  *
26  * @device_path		device path
27  * Return:		size in bytes
28  */
get_device_path_size(const struct efi_device_path * device_path)29 static efi_uintn_t EFIAPI get_device_path_size(
30 	const struct efi_device_path *device_path)
31 {
32 	efi_uintn_t sz = 0;
33 
34 	EFI_ENTRY("%pD", device_path);
35 	/* size includes the EFI_DP_END node: */
36 	if (device_path)
37 		sz = efi_dp_size(device_path) + sizeof(struct efi_device_path);
38 	return EFI_EXIT(sz);
39 }
40 
41 /*
42  * Duplicate a device path.
43  *
44  * This function implements the DuplicateDevicePath service of the device path
45  * utilities protocol.
46  *
47  * The UEFI spec does not indicate what happens to the end tag. We follow the
48  * EDK2 logic: In case the device path ends with an end of instance tag, the
49  * copy will also end with an end of instance tag.
50  *
51  * See the Unified Extensible Firmware Interface (UEFI) specification
52  * for details.
53  *
54  * @device_path		device path
55  * Return:		copy of the device path
56  */
duplicate_device_path(const struct efi_device_path * device_path)57 static struct efi_device_path * EFIAPI duplicate_device_path(
58 	const struct efi_device_path *device_path)
59 {
60 	EFI_ENTRY("%pD", device_path);
61 	return EFI_EXIT(efi_dp_dup(device_path));
62 }
63 
64 /*
65  * Append device path.
66  *
67  * This function implements the AppendDevicePath service of the device path
68  * utilities protocol.
69  *
70  * See the Unified Extensible Firmware Interface (UEFI) specification
71  * for details.
72  *
73  * @src1		1st device path
74  * @src2		2nd device path
75  * Return:		concatenated device path
76  */
append_device_path(const struct efi_device_path * src1,const struct efi_device_path * src2)77 static struct efi_device_path * EFIAPI append_device_path(
78 	const struct efi_device_path *src1,
79 	const struct efi_device_path *src2)
80 {
81 	EFI_ENTRY("%pD, %pD", src1, src2);
82 	return EFI_EXIT(efi_dp_concat(src1, src2, 0));
83 }
84 
85 /*
86  * Append device path node.
87  *
88  * This function implements the AppendDeviceNode service of the device path
89  * utilities protocol.
90  *
91  * See the Unified Extensible Firmware Interface (UEFI) specification
92  * for details.
93  *
94  * @device_path		device path
95  * @device_node		device node
96  * Return:		concatenated device path
97  */
append_device_node(const struct efi_device_path * device_path,const struct efi_device_path * device_node)98 static struct efi_device_path * EFIAPI append_device_node(
99 	const struct efi_device_path *device_path,
100 	const struct efi_device_path *device_node)
101 {
102 	EFI_ENTRY("%pD, %p", device_path, device_node);
103 	return EFI_EXIT(efi_dp_append_node(device_path, device_node));
104 }
105 
106 /*
107  * Append device path instance.
108  *
109  * This function implements the AppendDevicePathInstance service of the device
110  * path utilities protocol.
111  *
112  * See the Unified Extensible Firmware Interface (UEFI) specification
113  * for details.
114  *
115  * @device_path			1st device path
116  * @device_path_instance	2nd device path
117  * Return:			concatenated device path
118  */
append_device_path_instance(const struct efi_device_path * device_path,const struct efi_device_path * device_path_instance)119 static struct efi_device_path * EFIAPI append_device_path_instance(
120 	const struct efi_device_path *device_path,
121 	const struct efi_device_path *device_path_instance)
122 {
123 	EFI_ENTRY("%pD, %pD", device_path, device_path_instance);
124 	return EFI_EXIT(efi_dp_append_instance(device_path,
125 					       device_path_instance));
126 }
127 
128 /*
129  * Get next device path instance.
130  *
131  * This function implements the GetNextDevicePathInstance service of the device
132  * path utilities protocol.
133  *
134  * See the Unified Extensible Firmware Interface (UEFI) specification
135  * for details.
136  *
137  * @device_path_instance	next device path instance
138  * @device_path_instance_size	size of the device path instance
139  * Return:			concatenated device path
140  */
get_next_device_path_instance(struct efi_device_path ** device_path_instance,efi_uintn_t * device_path_instance_size)141 static struct efi_device_path * EFIAPI get_next_device_path_instance(
142 	struct efi_device_path **device_path_instance,
143 	efi_uintn_t *device_path_instance_size)
144 {
145 	EFI_ENTRY("%pD, %p", device_path_instance, device_path_instance_size);
146 	return EFI_EXIT(efi_dp_get_next_instance(device_path_instance,
147 						 device_path_instance_size));
148 }
149 
150 /*
151  * Check if a device path contains more than one instance.
152  *
153  * This function implements the AppendDeviceNode service of the device path
154  * utilities protocol.
155  *
156  * See the Unified Extensible Firmware Interface (UEFI) specification
157  * for details.
158  *
159  * @device_path		device path
160  * @device_node		device node
161  * Return:		concatenated device path
162  */
is_device_path_multi_instance(const struct efi_device_path * device_path)163 static bool EFIAPI is_device_path_multi_instance(
164 	const struct efi_device_path *device_path)
165 {
166 	EFI_ENTRY("%pD", device_path);
167 	return EFI_EXIT(efi_dp_is_multi_instance(device_path));
168 }
169 
170 /*
171  * Create device node.
172  *
173  * This function implements the CreateDeviceNode service of the device path
174  * utilities protocol.
175  *
176  * See the Unified Extensible Firmware Interface (UEFI) specification
177  * for details.
178  *
179  * @node_type		node type
180  * @node_sub_type	node sub type
181  * @node_length		node length
182  * Return:		device path node
183  */
create_device_node(uint8_t node_type,uint8_t node_sub_type,uint16_t node_length)184 static struct efi_device_path * EFIAPI create_device_node(
185 	uint8_t node_type, uint8_t node_sub_type, uint16_t node_length)
186 {
187 	EFI_ENTRY("%u, %u, %u", node_type, node_sub_type, node_length);
188 	return EFI_EXIT(efi_dp_create_device_node(node_type, node_sub_type,
189 			node_length));
190 }
191 
192 const struct efi_device_path_utilities_protocol efi_device_path_utilities = {
193 	.get_device_path_size = get_device_path_size,
194 	.duplicate_device_path = duplicate_device_path,
195 	.append_device_path = append_device_path,
196 	.append_device_node = append_device_node,
197 	.append_device_path_instance = append_device_path_instance,
198 	.get_next_device_path_instance = get_next_device_path_instance,
199 	.is_device_path_multi_instance = is_device_path_multi_instance,
200 	.create_device_node = create_device_node,
201 };
202