1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI device path interface
5 * Copyright (c) 2017 Heinrich Schuchardt
9 #include <efi_loader.h>
11 #define MAC_OUTPUT_LEN 22
12 #define UNKNOWN_OUTPUT_LEN 23
14 #define MAX_NODE_LEN 512
15 #define MAX_PATH_LEN 1024
17 const efi_guid_t efi_guid_device_path_to_text_protocol =
18 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
21 * efi_str_to_u16() - convert ASCII string to UTF-16
23 * A u16 buffer is allocated from pool. The ASCII string is copied to the u16
27 * Return: UTF-16 string. NULL if out of memory.
29 static u16 *efi_str_to_u16(char *str)
35 len = strlen(str) + 1;
36 ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len * sizeof(u16),
38 if (ret != EFI_SUCCESS)
40 ascii2unicode(out, str);
44 static char *dp_unknown(char *s, struct efi_device_path *dp)
46 s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
50 static char *dp_hardware(char *s, struct efi_device_path *dp)
52 switch (dp->sub_type) {
53 case DEVICE_PATH_SUB_TYPE_MEMORY: {
54 struct efi_device_path_memory *mdp =
55 (struct efi_device_path_memory *)dp;
56 s += sprintf(s, "MemoryMapped(0x%x,0x%llx,0x%llx)",
62 case DEVICE_PATH_SUB_TYPE_VENDOR: {
63 struct efi_device_path_vendor *vdp =
64 (struct efi_device_path_vendor *)dp;
65 s += sprintf(s, "VenHw(%pUl)", &vdp->guid);
69 s = dp_unknown(s, dp);
75 static char *dp_acpi(char *s, struct efi_device_path *dp)
77 switch (dp->sub_type) {
78 case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
79 struct efi_device_path_acpi_path *adp =
80 (struct efi_device_path_acpi_path *)dp;
81 s += sprintf(s, "Acpi(PNP%04x", EISA_PNP_NUM(adp->hid));
83 s += sprintf(s, ",%d", adp->uid);
88 s = dp_unknown(s, dp);
94 static char *dp_msging(char *s, struct efi_device_path *dp)
96 switch (dp->sub_type) {
97 case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
98 struct efi_device_path_atapi *ide =
99 (struct efi_device_path_atapi *)dp;
100 s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
101 ide->slave_master, ide->logical_unit_number);
104 case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
105 struct efi_device_path_scsi *ide =
106 (struct efi_device_path_scsi *)dp;
107 s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
108 ide->logical_unit_number);
111 case DEVICE_PATH_SUB_TYPE_MSG_USB: {
112 struct efi_device_path_usb *udp =
113 (struct efi_device_path_usb *)dp;
114 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
118 case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
119 struct efi_device_path_mac_addr *mdp =
120 (struct efi_device_path_mac_addr *)dp;
122 if (mdp->if_type != 0 && mdp->if_type != 1)
125 s += sprintf(s, "MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
126 mdp->mac.addr[0], mdp->mac.addr[1],
127 mdp->mac.addr[2], mdp->mac.addr[3],
128 mdp->mac.addr[4], mdp->mac.addr[5],
133 case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
134 struct efi_device_path_usb_class *ucdp =
135 (struct efi_device_path_usb_class *)dp;
137 s += sprintf(s, "USBClass(%x,%x,%x,%x,%x)",
138 ucdp->vendor_id, ucdp->product_id,
139 ucdp->device_class, ucdp->device_subclass,
140 ucdp->device_protocol);
144 case DEVICE_PATH_SUB_TYPE_MSG_SD:
145 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
146 const char *typename =
147 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
149 struct efi_device_path_sd_mmc_path *sddp =
150 (struct efi_device_path_sd_mmc_path *)dp;
151 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
155 s = dp_unknown(s, dp);
162 * Convert a media device path node to text.
165 * @dp device path node
166 * @return next unused buffer address
168 static char *dp_media(char *s, struct efi_device_path *dp)
170 switch (dp->sub_type) {
171 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
172 struct efi_device_path_hard_drive_path *hddp =
173 (struct efi_device_path_hard_drive_path *)dp;
174 void *sig = hddp->partition_signature;
178 /* Copy from packed structure to aligned memory */
179 memcpy(&start, &hddp->partition_start, sizeof(start));
180 memcpy(&end, &hddp->partition_end, sizeof(end));
182 switch (hddp->signature_type) {
186 memcpy(&signature, sig, sizeof(signature));
188 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
189 hddp->partition_number, signature, start, end);
194 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
195 hddp->partition_number, sig, start, end);
199 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
200 hddp->partition_number, hddp->partmap_type,
207 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
208 struct efi_device_path_cdrom_path *cddp =
209 (struct efi_device_path_cdrom_path *)dp;
210 s += sprintf(s, "CDROM(0x%x)", cddp->boot_entry);
213 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
214 struct efi_device_path_file_path *fp =
215 (struct efi_device_path_file_path *)dp;
216 int slen = (dp->length - sizeof(*dp)) / 2;
217 if (slen > MAX_NODE_LEN - 2)
218 slen = MAX_NODE_LEN - 2;
219 s += sprintf(s, "%-.*ls", slen, fp->str);
223 s = dp_unknown(s, dp);
230 * Converts a single node to a char string.
232 * @buffer output buffer
233 * @dp device path or node
234 * @return end of string
236 static char *efi_convert_single_device_node_to_text(
238 struct efi_device_path *dp)
243 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
244 str = dp_hardware(str, dp);
246 case DEVICE_PATH_TYPE_ACPI_DEVICE:
247 str = dp_acpi(str, dp);
249 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
250 str = dp_msging(str, dp);
252 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
253 str = dp_media(str, dp);
255 case DEVICE_PATH_TYPE_END:
258 str = dp_unknown(str, dp);
266 * This function implements the ConvertDeviceNodeToText service of the
267 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
268 * See the Unified Extensible Firmware Interface (UEFI) specification
271 * device_node device node to be converted
272 * display_only true if the shorter text represenation shall be used
273 * allow_shortcuts true if shortcut forms may be used
274 * @return text represenation of the device path
275 * NULL if out of memory of device_path is NULL
277 static uint16_t EFIAPI *efi_convert_device_node_to_text(
278 struct efi_device_path *device_node,
280 bool allow_shortcuts)
282 char str[MAX_NODE_LEN];
283 uint16_t *text = NULL;
285 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
289 efi_convert_single_device_node_to_text(str, device_node);
291 text = efi_str_to_u16(str);
294 EFI_EXIT(EFI_SUCCESS);
299 * This function implements the ConvertDevicePathToText service of the
300 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
301 * See the Unified Extensible Firmware Interface (UEFI) specification
304 * device_path device path to be converted
305 * display_only true if the shorter text represenation shall be used
306 * allow_shortcuts true if shortcut forms may be used
307 * @return text represenation of the device path
308 * NULL if out of memory of device_path is NULL
310 static uint16_t EFIAPI *efi_convert_device_path_to_text(
311 struct efi_device_path *device_path,
313 bool allow_shortcuts)
315 uint16_t *text = NULL;
316 char buffer[MAX_PATH_LEN];
319 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
323 while (device_path &&
324 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
326 str = efi_convert_single_device_node_to_text(str, device_path);
327 device_path = efi_dp_next(device_path);
330 text = efi_str_to_u16(buffer);
333 EFI_EXIT(EFI_SUCCESS);
337 /* helper for debug prints.. efi_free_pool() the result. */
338 uint16_t *efi_dp_str(struct efi_device_path *dp)
340 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
343 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
344 .convert_device_node_to_text = efi_convert_device_node_to_text,
345 .convert_device_path_to_text = efi_convert_device_path_to_text,