Skip to contentSkip to conversation

efi_loader: enable EFI driver provided block device

PATCH New Heinrich Schuchardt 1 participant 18 messages Jan 11, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover efi_loader: enable EFI driver provided block device

From: Heinrich Schuchardt <[email protected]>

With this patch series an EFI application or driver can supply a block device which in turn can be used to download an image.

E.g. we can load iPXE, connect iSCSI drives, download grub from the SAN and afterwards with grub download and run an EFI application. Running helloworld.efi in this configuration was successful.

Currently the Linux kernel crashes when loaded in the same way. So some work remains.

Heinrich Schuchardt (17):
efi_loader: return NULL from device path functions
efi_loader: address of the simple file system protocol
efi_loader: correct find simple file system protocol
efi_loader: print device path when entering efi_load_image
efi_loader: allocate correct memory type for EFI image
efi_loader: check tables in helloworld.efi
efi_loader: fix StartImage bootservice
efi_loader: efi_disk_register: correctly determine if_type_name
efi_loader: make efi_block_io_guid a global symbol
efi_loader: provide a function to create a partition node
efi_loader: make efi_disk_create_partitions a global symbol
efi_loader: correct EFI_BLOCK_IO_PROTOCOL definitions
efi_loader: provide function to get last node of a device path
efi_loader: provide links between devices EFI handles
tools: provide a tool to convert a binary file to an include
efi_selftest: provide a test for block io
efi_driver: EFI block driver

 MAINTAINERS                                  |   1 +
 common/board_r.c                             |   3 +
 drivers/block/blk-uclass.c                   |   4 +-
 include/blk.h                                |   1 +
 include/config_fallbacks.h                   |   1 +
 include/dm/device.h                          |   4 +
 include/dm/uclass-id.h                       |   1 +
 include/efi_api.h                            |  10 +-
 include/efi_driver.h                         |  30 ++
 include/efi_loader.h                         |  21 +-
 lib/Makefile                                 |   1 +
 lib/efi_driver/Makefile                      |  13 +
 lib/efi_driver/efi_block_device.c            | 153 +++++++++++
 lib/efi_driver/efi_uclass.c                  | 318 +++++++++++++++++++++
 lib/efi_loader/efi_boottime.c                |   6 +-
 lib/efi_loader/efi_device_path.c             | 168 +++++++++---
 lib/efi_loader/efi_disk.c                    | 137 +++++++---
 lib/efi_loader/efi_image_loader.c            |  64 +++--
 lib/efi_loader/helloworld.c                  |  26 ++
 lib/efi_selftest/Makefile                    |   4 +
 lib/efi_selftest/efi_selftest_block_device.c | 395 +++++++++++++++++++++++++++
 lib/efi_selftest/efi_selftest_disk_image.h   |  69 +++++
 tools/Makefile                               |   3 +
 tools/file2include.c                         | 106 +++++++
 24 files changed, 1424 insertions(+), 115 deletions(-)
 create mode 100644 include/efi_driver.h
 create mode 100644 lib/efi_driver/Makefile
 create mode 100644 lib/efi_driver/efi_block_device.c
 create mode 100644 lib/efi_driver/efi_uclass.c
 create mode 100644 lib/efi_selftest/efi_selftest_block_device.c
 create mode 100644 lib/efi_selftest/efi_selftest_disk_image.h
 create mode 100644 tools/file2include.c
Signature · 1 line
2.14.2
#1 lore raw

PATCH 1/17 efi_loader: return NULL from device path functions

From: Heinrich Schuchardt <[email protected]>

For the construction of device paths we need to call the AllocatePool service. We should not ignore if it fails due to an out of memory situation.

This patch changes the device path functions to return NULL if the memory allocation fails.

Additional patches will be needed to fix the callers.

---
 include/efi_loader.h             |  6 +++---
 lib/efi_loader/efi_device_path.c | 42 ++++++++++++++++++++++++++++++++++------
 2 files changed, 39 insertions(+), 9 deletions(-)
include/efi_loader.h +3 -3
index 37389c33cc..6b623d8327 100644
@@ -304,9 +304,9 @@ struct efi_device_path *efi_dp_from_eth(void);
 struct efi_device_path *efi_dp_from_mem(uint32_t mem_type,
 					uint64_t start_address,
 					uint64_t end_address);
-void efi_dp_split_file_path(struct efi_device_path *full_path,
-			    struct efi_device_path **device_path,
-			    struct efi_device_path **file_path);
+efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
+				    struct efi_device_path **device_path,
+				    struct efi_device_path **file_path);
 
 #define EFI_DP_TYPE(_dp, _type, _subtype) \
 	(((_dp)->type == DEVICE_PATH_TYPE_##_type) && \
lib/efi_loader/efi_device_path.c +36 -6

120 lines not on this page · raw message

Signature · 1 line
2.14.2
#2 lore raw

PATCH 2/17 efi_loader: address of the simple file system protocol

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

When installing the the simple file system protocol we have to path the address of the structure and not the address of a pointer ot the structure.

---
 lib/efi_loader/efi_disk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/efi_loader/efi_disk.c +1 -1
index d299fc8dea..85b4a147e2 100644
@@ -242,7 +242,7 @@ static void efi_disk_add_dev(const char *name,
 							 diskobj->dp);
 		ret = efi_add_protocol(diskobj->parent.handle,
 				       &efi_simple_file_system_protocol_guid,
-				       &diskobj->volume);
+				       diskobj->volume);
 		if (ret != EFI_SUCCESS)
 			goto out_of_memory;
 	}
Signature · 1 line
2.14.2
#3 lore raw

PATCH 3/17 efi_loader: correct find simple file system protocol

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

In contrast to the description the code did not split the device path into device part and file part.

The code should use the installed protocol and not refer to the internal structure of the the disk object.

---
 lib/efi_loader/efi_disk.c | 39 +++++++++++++++++++++++++++++----------
 1 file changed, 29 insertions(+), 10 deletions(-)
lib/efi_loader/efi_disk.c +29 -10
index 85b4a147e2..8771e880f6 100644
@@ -175,25 +175,44 @@ static const struct efi_block_io block_io_disk_template = {
 };
 
 /*
- * Find filesystem from a device-path.  The passed in path 'p' probably
- * contains one or more /File(name) nodes, so the comparison stops at
- * the first /File() node, and returns the pointer to that via 'rp'.
- * This is mostly intended to be a helper to map a device-path to an
- * efi_file_handle object.
+ * Get the simple file system protocol for a file device path.
+ *
+ * The full path provided is split into device part and into a file
+ * part. The device part is used to find the handle on which the
+ * simple file system protocol is installed.
+ *
+ * @full_path	device path including device and file
+ * @return	simple file system protocol
  */
 struct efi_simple_file_system_protocol *
-efi_fs_from_path(struct efi_device_path *fp)
+efi_fs_from_path(struct efi_device_path *full_path)
 {
 	struct efi_object *efiobj;
-	struct efi_disk_obj *diskobj;
+	struct efi_handler *handler;
+	struct efi_device_path *device_path;
+	struct efi_device_path *file_path;
+	efi_status_t ret;
 
-	efiobj = efi_dp_find_obj(fp, NULL);
+	/* Split the path into a device part and a file part */
+	ret = efi_dp_split_file_path(full_path, &device_path, &file_path);
+	if (ret != EFI_SUCCESS)
+		return NULL;
+	efi_free_pool(file_path);
+
+	/* Get the EFI object for the partition */
+	efiobj = efi_dp_find_obj(device_path, NULL);
+	efi_free_pool(device_path);
 	if (!efiobj)
 		return NULL;
 
-	diskobj = container_of(efiobj, struct efi_disk_obj, parent);
+	/* Find the simple file system protocol */
+	ret = efi_search_protocol(efiobj, &efi_simple_file_system_protocol_guid,
+				  &handler);
+	if (ret != EFI_SUCCESS)
+		return NULL;
 
-	return diskobj->volume;
+	/* Return the simple file system protocol for the partition */
+	return handler->protocol_interface;
 }
 
 /*
Signature · 1 line
2.14.2
#4 lore raw

PATCH 4/17 efi_loader: print device path when entering efi_load_image

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

Use %pD to print the device path instead of its address when entering efi_load_image.

---
 lib/efi_loader/efi_boottime.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/efi_loader/efi_boottime.c +1 -1
index 42cf197af9..e046a7be9d 100644
@@ -1474,7 +1474,7 @@ static efi_status_t EFIAPI efi_load_image(bool boot_policy,
 	struct efi_object *obj;
 	efi_status_t ret;
 
-	EFI_ENTRY("%d, %p, %p, %p, %ld, %p", boot_policy, parent_image,
+	EFI_ENTRY("%d, %p, %pD, %p, %ld, %p", boot_policy, parent_image,
 		  file_path, source_buffer, source_size, image_handle);
 
 	info = calloc(1, sizeof(*info));
Signature · 1 line
2.14.2
#5 lore raw

PATCH 5/17 efi_loader: allocate correct memory type for EFI image

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

The category of memory allocated for an EFI image should depend on its type (application, bootime service driver, runtime service driver).

Our helloworld.efi built on arm64 has an illegal image type. Treat it like an EFI application.

---
 lib/efi_loader/efi_image_loader.c | 64 ++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 24 deletions(-)
lib/efi_loader/efi_image_loader.c +40 -24
index 849d7ce377..9d2214b481 100644
@@ -73,6 +73,40 @@ void __weak invalidate_icache_all(void)
 	/* If the system doesn't support icache_all flush, cross our fingers */
 }
 
+/*
+ * Determine the memory types to be used for code and data.
+ *
+ * @loaded_image_info	image descriptor
+ * @image_type		field Subsystem of the optional header for
+ *			Windows specific field
+ */
+static void efi_set_code_and_data_type(
+			struct efi_loaded_image *loaded_image_info,
+			uint16_t image_type)
+{
+	switch (image_type) {
+	case IMAGE_SUBSYSTEM_EFI_APPLICATION:
+		loaded_image_info->image_code_type = EFI_LOADER_CODE;
+		loaded_image_info->image_data_type = EFI_LOADER_DATA;
+		break;
+	case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
+		loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
+		loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
+		break;
+	case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
+	case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
+		loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
+		loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
+		break;
+	default:
+		printf("%s: invalid image type: %u\n", __func__, image_type);
+		/* Let's assume it is an application */
+		loaded_image_info->image_code_type = EFI_LOADER_CODE;
+		loaded_image_info->image_data_type = EFI_LOADER_DATA;
+		break;
+	}
+}
+
 /*
  * This function loads all sections from a PE binary into a newly reserved
  * piece of memory. On successful load it then returns the entry point for
@@ -94,7 +128,6 @@ void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
 	unsigned long virt_size = 0;
 	bool can_run_nt64 = true;
 	bool can_run_nt32 = true;
-	uint16_t image_type;
 
 #if defined(CONFIG_ARM64)
 	can_run_nt32 = false;
@@ -131,7 +164,9 @@ void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
 		IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
 		IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
 		image_size = opt->SizeOfImage;
-		efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
+		efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
+		efi_reloc = efi_alloc(virt_size,
+				      loaded_image_info->image_code_type);
 		if (!efi_reloc) {
 			printf("%s: Could not allocate %lu bytes\n",
 			       __func__, virt_size);
@@ -140,12 +175,13 @@ void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
 		entry = efi_reloc + opt->AddressOfEntryPoint;
 		rel_size = opt->DataDirectory[rel_idx].Size;
 		rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
-		image_type = opt->Subsystem;
 	} else if (can_run_nt32 &&
 		   (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
 		IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
 		image_size = opt->SizeOfImage;
-		efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
+		efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
+		efi_reloc = efi_alloc(virt_size,
+				      loaded_image_info->image_code_type);
 		if (!efi_reloc) {
 			printf("%s: Could not allocate %lu bytes\n",
 			       __func__, virt_size);
@@ -154,32 +190,12 @@ void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
 		entry = efi_reloc + opt->AddressOfEntryPoint;
 		rel_size = opt->DataDirectory[rel_idx].Size;
 		rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
-		image_type = opt->Subsystem;
 	} else {
 		printf("%s: Invalid optional header magic %x\n", __func__,
 		       nt->OptionalHeader.Magic);
 		return NULL;
 	}
 
-	switch (image_type) {
-	case IMAGE_SUBSYSTEM_EFI_APPLICATION:
-		loaded_image_info->image_code_type = EFI_LOADER_CODE;
-		loaded_image_info->image_data_type = EFI_LOADER_DATA;
-		break;
-	case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
-		loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
-		loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
-		break;
-	case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
-	case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
-		loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
-		loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
-		break;
-	default:
-		printf("%s: invalid image type: %u\n", __func__, image_type);
-		break;
-	}
-
 	/* Load sections into RAM */
 	for (i = num_sections - 1; i >= 0; i--) {
 		IMAGE_SECTION_HEADER *sec = &sections[i];
Signature · 1 line
2.14.2
#6 lore raw

PATCH 6/17 efi_loader: check tables in helloworld.efi

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

Check if the device tree and the SMBIOS table are available.

---
 lib/efi_loader/helloworld.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
lib/efi_loader/helloworld.c +26 -0
index b8c147d7f2..1ec0179226 100644
@@ -14,6 +14,22 @@
 #include <efi_api.h>
 
 static const efi_guid_t loaded_image_guid = LOADED_IMAGE_GUID;
+static const efi_guid_t fdt_guid = EFI_FDT_GUID;
+static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
+
+static int hw_memcmp(const void *buf1, const void *buf2, size_t length)
+{
+	const u8 *pos1 = buf1;
+	const u8 *pos2 = buf2;
+
+	for (; length; --length) {
+		if (*pos1 != *pos2)
+			return *pos1 - *pos2;
+		++pos1;
+		++pos2;
+	}
+	return 0;
+}
 
 /*
  * Entry point of the EFI application.
@@ -29,6 +45,7 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle,
 	struct efi_boot_services *boottime = systable->boottime;
 	struct efi_loaded_image *loaded_image;
 	efi_status_t ret;
+	efi_uintn_t i;
 
 	con_out->output_string(con_out, L"Hello, world!\n");
 
@@ -40,6 +57,15 @@ efi_status_t EFIAPI efi_main(efi_handle_t handle,
 				       L"Cannot open loaded image protocol\n");
 		goto out;
 	}
+	/* Find configuration tables */
+	for (i = 0; i < systable->nr_tables; ++i) {
+		if (!hw_memcmp(&systable->tables[i].guid, &fdt_guid,
+			       sizeof(efi_guid_t)))
+			con_out->output_string(con_out, L"Have device tree\n");
+		if (!hw_memcmp(&systable->tables[i].guid, &smbios_guid,
+			       sizeof(efi_guid_t)))
+			con_out->output_string(con_out, L"Have SMBIOS table\n");
+	}
 	/* Output the load options */
 	con_out->output_string(con_out, L"Load options: ");
 	if (loaded_image->load_options_size && loaded_image->load_options)
Signature · 1 line
2.14.2
#7 lore raw

PATCH 7/17 efi_loader: fix StartImage bootservice

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

The calling convention for the entry point of an EFI image is always 'asmlinkage'.

---
 lib/efi_loader/efi_boottime.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/efi_loader/efi_boottime.c +2 -1
index e046a7be9d..5a3349ecb2 100644
@@ -1533,7 +1533,8 @@ static efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
 					   unsigned long *exit_data_size,
 					   s16 **exit_data)
 {
-	ulong (*entry)(efi_handle_t image_handle, struct efi_system_table *st);
+	asmlinkage ulong (*entry)(efi_handle_t image_handle,
+				  struct efi_system_table *st);
 	struct efi_loaded_image *info = image_handle;
 
 	EFI_ENTRY("%p, %p, %p", image_handle, exit_data_size, exit_data);
Signature · 1 line
2.14.2
#8 lore raw

PATCH 8/17 efi_loader: efi_disk_register: correctly determine if_type_name

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

The interface type name can be used to look up the interface type. Don't confound it with the driver name which may be different.

---
 lib/efi_loader/efi_disk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/efi_loader/efi_disk.c +1 -1
index 8771e880f6..da92729779 100644
@@ -330,7 +330,7 @@ int efi_disk_register(void)
 	     dev;
 	     uclass_next_device_check(&dev)) {
 		struct blk_desc *desc = dev_get_uclass_platdata(dev);
-		const char *if_typename = dev->driver->name;
+		const char *if_typename = blk_get_if_type_name(desc->if_type);
 
 		printf("Scanning disk %s...\n", dev->name);
 
Signature · 1 line
2.14.2
#9 lore raw

PATCH 9/17 efi_loader: make efi_block_io_guid a global symbol

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

The GUID of the EFI_BLOCK_IO_PROTOCOL is needed in different code parts. To avoid duplication make efi_block_io_guid a global symbol.

---
 include/efi_loader.h      | 2 ++
 lib/efi_loader/efi_disk.c | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/efi_loader.h +2 -0
index 6b623d8327..6d04feb0a7 100644
@@ -86,6 +86,8 @@ extern const struct efi_device_path_to_text_protocol efi_device_path_to_text;
 
 uint16_t *efi_dp_str(struct efi_device_path *dp);
 
+/* GUID of the EFI_BLOCK_IO_PROTOCOL */
+extern const efi_guid_t efi_block_io_guid;
 extern const efi_guid_t efi_global_variable_guid;
 extern const efi_guid_t efi_guid_console_control;
 extern const efi_guid_t efi_guid_device_path;
lib/efi_loader/efi_disk.c +1 -1
index da92729779..cccfc6dac5 100644
@@ -14,7 +14,7 @@
 #include <part.h>
 #include <malloc.h>
 
-static const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
+const efi_guid_t efi_block_io_guid = BLOCK_IO_GUID;
 
 struct efi_disk_obj {
 	/* Generic EFI object parent class data */
Signature · 1 line
2.14.2
#10 lore raw

PATCH 10/17 efi_loader: provide a function to create a partition node

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

Provide new function efi_dp_part_node() to create a device node for a partition.

---
 include/efi_loader.h             |   2 +
 lib/efi_loader/efi_device_path.c | 106 ++++++++++++++++++++++++++-------------
 2 files changed, 72 insertions(+), 36 deletions(-)
include/efi_loader.h +2 -0
index 6d04feb0a7..456763e83a 100644
@@ -300,6 +300,8 @@ struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
 
 struct efi_device_path *efi_dp_from_dev(struct udevice *dev);
 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part);
+/* Create a device node for a block device partition. */
+struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part);
 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
 					 const char *path);
 struct efi_device_path *efi_dp_from_eth(void);
lib/efi_loader/efi_device_path.c +70 -36

132 lines not on this page · raw message

Signature · 1 line
2.14.2
#11 lore raw

PATCH 11/17 efi_loader: make efi_disk_create_partitions a global symbol

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

Up to now we have been using efi_disk_create_partitions() to create partions for block device that existed before starting an EFI application.

We need to to call it for for block devices created by EFI applications at run time. The EFI application will define the handle for the block device and install a device path protocol on it. We have to use this device path as stem for the partition device paths.

---
 include/efi_loader.h      |  4 +++
 lib/efi_loader/efi_disk.c | 84 +++++++++++++++++++++++++++++++++--------------
 2 files changed, 64 insertions(+), 24 deletions(-)
include/efi_loader.h +4 -0
index 456763e83a..0ba7badb15 100644
@@ -174,6 +174,10 @@ extern struct list_head efi_obj_list;
 int efi_console_register(void);
 /* Called by bootefi to make all disk storage accessible as EFI objects */
 int efi_disk_register(void);
+/* Create handles and protocols for the partions of a block device */
+int efi_disk_create_partitions(efi_handle_t parent, struct blk_desc *desc,
+			       const char *if_typename, int diskid,
+			       const char *pdevname);
 /* Called by bootefi to make GOP (graphical) interface available */
 int efi_gop_register(void);
 /* Called by bootefi to make the network interface available */
lib/efi_loader/efi_disk.c +60 -24

153 lines not on this page · raw message

Signature · 1 line
2.14.2
#12 lore raw

PATCH 12/17 efi_loader: correct EFI_BLOCK_IO_PROTOCOL definitions

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

Add the revision constants.
Depending on the revision additional fields are needed in the media descriptor.
Use efi_uintn_t for number of bytes to read or write.

---
 include/efi_api.h         | 10 ++++++++--
 lib/efi_loader/efi_disk.c |  8 ++++----
 2 files changed, 12 insertions(+), 6 deletions(-)
include/efi_api.h +8 -2
index 502fffed20..0bc244444d 100644
@@ -424,18 +424,24 @@ struct efi_block_io_media
 	u32 io_align;
 	u8 pad2[4];
 	u64 last_block;
+	u64 lowest_aligned_lba;
+	u32 logical_blocks_per_physical_block;
+	u32 optimal_transfer_length_granualarity;
 };
 
+#define EFI_BLOCK_IO_PROTOCOL_REVISION2	0x00020001
+#define EFI_BLOCK_IO_PROTOCOL_REVISION3	0x0002001f
+
 struct efi_block_io {
 	u64 revision;
 	struct efi_block_io_media *media;
 	efi_status_t (EFIAPI *reset)(struct efi_block_io *this,
 			char extended_verification);
 	efi_status_t (EFIAPI *read_blocks)(struct efi_block_io *this,
-			u32 media_id, u64 lba, unsigned long buffer_size,
+			u32 media_id, u64 lba, efi_uintn_t buffer_size,
 			void *buffer);
 	efi_status_t (EFIAPI *write_blocks)(struct efi_block_io *this,
-			u32 media_id, u64 lba, unsigned long buffer_size,
+			u32 media_id, u64 lba, efi_uintn_t buffer_size,
 			void *buffer);
 	efi_status_t (EFIAPI *flush_blocks)(struct efi_block_io *this);
 };
lib/efi_loader/efi_disk.c +4 -4
index 92c3f45ca5..8f84e7788e 100644
@@ -91,7 +91,7 @@ static efi_status_t efi_disk_rw_blocks(struct efi_block_io *this,
 }
 
 static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
-			u32 media_id, u64 lba, unsigned long buffer_size,
+			u32 media_id, u64 lba, efi_uintn_t buffer_size,
 			void *buffer)
 {
 	void *real_buffer = buffer;
@@ -112,7 +112,7 @@ static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
 	real_buffer = efi_bounce_buffer;
 #endif
 
-	EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
+	EFI_ENTRY("%p, %x, %" PRIx64 ", %zx, %p", this, media_id, lba,
 		  buffer_size, buffer);
 
 	r = efi_disk_rw_blocks(this, media_id, lba, buffer_size, real_buffer,
@@ -126,7 +126,7 @@ static efi_status_t EFIAPI efi_disk_read_blocks(struct efi_block_io *this,
 }
 
 static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
-			u32 media_id, u64 lba, unsigned long buffer_size,
+			u32 media_id, u64 lba, efi_uintn_t buffer_size,
 			void *buffer)
 {
 	void *real_buffer = buffer;
@@ -147,7 +147,7 @@ static efi_status_t EFIAPI efi_disk_write_blocks(struct efi_block_io *this,
 	real_buffer = efi_bounce_buffer;
 #endif
 
-	EFI_ENTRY("%p, %x, %"PRIx64", %lx, %p", this, media_id, lba,
+	EFI_ENTRY("%p, %x, %" PRIx64 ", %zx, %p", this, media_id, lba,
 		  buffer_size, buffer);
 
 	/* Populate bounce buffer if necessary */
Signature · 1 line
2.14.2
#13 lore raw

PATCH 13/17 efi_loader: provide function to get last node of a device path

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

On a block device and its partions the same protocols can be installed. To tell the apart we can use the type of the last node of the device path which is not the end node.

The patch provides a utility function to find this last node.

---
 include/efi_loader.h             |  3 +++
 lib/efi_loader/efi_device_path.c | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/efi_loader.h +3 -0
index 0ba7badb15..4060348695 100644
@@ -312,6 +312,9 @@ struct efi_device_path *efi_dp_from_eth(void);
 struct efi_device_path *efi_dp_from_mem(uint32_t mem_type,
 					uint64_t start_address,
 					uint64_t end_address);
+/* Determine the last device path node that is not the end node. */
+const struct efi_device_path *efi_dp_last_node(
+			const struct efi_device_path *dp);
 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
 				    struct efi_device_path **device_path,
 				    struct efi_device_path **file_path);
lib/efi_loader/efi_device_path.c +20 -0
index f00a0ce645..c941ea7717 100644
@@ -208,6 +208,26 @@ struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
 	return efiobj;
 }
 
+/*
+ * Determine the last device path node that is not the end node.
+ *
+ * @dp		device path
+ * @return	last node before the end node if it exists
+ *		otherwise NULL
+ */
+const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
+{
+	struct efi_device_path *ret;
+
+	if (!dp || dp->type == DEVICE_PATH_TYPE_END)
+		return NULL;
+	while (dp) {
+		ret = (struct efi_device_path *)dp;
+		dp = efi_dp_next(dp);
+	}
+	return ret;
+}
+
 /* return size not including End node: */
 unsigned efi_dp_size(const struct efi_device_path *dp)
 {
Signature · 1 line
2.14.2
#14 lore raw

PATCH 14/17 efi_loader: provide links between devices EFI handles

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

U-Boot devices and EFI handles can be related, e.g. an IDE disk relates to a handle with the EFI_BLOCK_IO_PROTOCOL. Provide pointers to store these links.

---
 include/dm/device.h           | 4 ++++
 include/efi_loader.h          | 2 ++
 lib/efi_loader/efi_boottime.c | 1 +
 3 files changed, 7 insertions(+)
include/dm/device.h +4 -0
index 813e49f330..e5c54fe7b6 100644
@@ -11,6 +11,7 @@
 #ifndef _DM_DEVICE_H
 #define _DM_DEVICE_H
 
+#include <efi_loader.h>
 #include <dm/ofnode.h>
 #include <dm/uclass-id.h>
 #include <fdtdec.h>
@@ -144,6 +145,9 @@ struct udevice {
 	uint32_t flags;
 	int req_seq;
 	int seq;
+#ifdef EFI_LOADER
+	efi_handle_t handle;
+#endif
 #ifdef CONFIG_DEVRES
 	struct list_head devres_head;
 #endif
include/efi_loader.h +2 -0
index 4060348695..711c901eda 100644
@@ -139,6 +139,8 @@ struct efi_object {
 	struct list_head protocols;
 	/* The object spawner can either use this for data or as identifier */
 	void *handle;
+	/* Device */
+	struct udevice *dev;
 };
 
 /**
lib/efi_loader/efi_boottime.c +1 -0
index 5a3349ecb2..4b3b63e39a 100644
@@ -362,6 +362,7 @@ efi_status_t efi_create_handle(efi_handle_t *handle)
 			      (void **)&obj);
 	if (r != EFI_SUCCESS)
 		return r;
+	obj->dev = NULL;
 	efi_add_handle(obj);
 	*handle = obj->handle;
 	return r;
Signature · 1 line
2.14.2
#15 lore raw

PATCH 15/17 tools: provide a tool to convert a binary file to an include

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

For testing EFI disk management we need an in-memory image of a disk.

The tool file2include converts a file to a C include. The file is separated into strings of 8 bytes. Only the non-zero strings are written to the include. The output format has been designed to maintain readability.

#define EFI_ST_DISK_IMG { 0x00010000, { \
{0x000001b8, "\x94\x37\x69\xfc\x00\x00\x00\x00"}, /* .7i..... */ \
{0x000001c0, "\x02\x00\x83\x02\x02\x00\x01\x00"}, /* ........ */ \
{0x000001c8, "\x00\x00\x7f\x00\x00\x00\x00\x00"}, /* ........ */ \
{0x000001f8, "\x00\x00\x00\x00\x00\x00\x55\xaa"}, /* ......U. */ \
...
{0x00006000, "\x48\x65\x6c\x6c\x6f\x20\x77\x6f"}, /* Hello wo */ \
{0x00006008, "\x72\x6c\x64\x21\x0a\x00\x00\x00"}, /* rld!.... */ \
{0, NULL} } }

As the disk image needed for testing contains mostly zeroes a high compression ratio can be attained.

---
 MAINTAINERS          |   1 +
 tools/Makefile       |   3 ++
 tools/file2include.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 tools/file2include.c
MAINTAINERS +1 -0
index 9473c00ef6..0f2836cf6e 100644
@@ -290,6 +290,7 @@ F:	include/efi*
 F:	lib/efi*/
 F:	test/py/tests/test_efi*
 F:	cmd/bootefi.c
+F:	tools/file2include.c
 
 FLATTENED DEVICE TREE
 M:	Simon Glass <[email protected]>
tools/Makefile +3 -0
index 4d32fe5910..6a0e1c1ebd 100644
@@ -57,6 +57,8 @@ mkenvimage-objs := mkenvimage.o os_support.o lib/crc32.o
 hostprogs-y += dumpimage mkimage
 hostprogs-$(CONFIG_FIT_SIGNATURE) += fit_info fit_check_sign
 
+hostprogs-$(CONFIG_CMD_BOOTEFI_SELFTEST) += file2include
+
 FIT_SIG_OBJS-$(CONFIG_FIT_SIGNATURE) := common/image-sig.o
 
 # The following files are synced with upstream DTC.
@@ -118,6 +120,7 @@ dumpimage-objs := $(dumpimage-mkimage-objs) dumpimage.o
 mkimage-objs   := $(dumpimage-mkimage-objs) mkimage.o
 fit_info-objs   := $(dumpimage-mkimage-objs) fit_info.o
 fit_check_sign-objs   := $(dumpimage-mkimage-objs) fit_check_sign.o
+file2include-objs := file2include.o
 
 ifneq ($(CONFIG_MX23)$(CONFIG_MX28),)
 # Add CONFIG_MXS into host CFLAGS, so we can check whether or not register
tools/file2include.c new file +106 -0

106 lines not on this page · raw message

Signature · 1 line
2.14.2
#16 lore raw

PATCH 16/17 efi_selftest: provide a test for block io

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

---
 lib/efi_selftest/Makefile                    |   4 +
 lib/efi_selftest/efi_selftest_block_device.c | 395 +++++++++++++++++++++++++++
 lib/efi_selftest/efi_selftest_disk_image.h   |  69 +++++
 3 files changed, 468 insertions(+)
 create mode 100644 lib/efi_selftest/efi_selftest_block_device.c
 create mode 100644 lib/efi_selftest/efi_selftest_disk_image.h
lib/efi_selftest/Makefile +4 -0
index e549553c82..20f614d6ba 100644
@@ -21,3 +21,7 @@ efi_selftest_textoutput.o \
 efi_selftest_tpl.o \
 efi_selftest_util.o \
 efi_selftest_watchdog.o
+
+ifeq ($(CONFIG_BLK)$(CONFIG_PARTITIONS),yy)
+obj-$(CONFIG_CMD_BOOTEFI_SELFTEST) += efi_selftest_block_device.o
+endif
lib/efi_selftest/efi_selftest_block_device.c new file +395 -0

395 lines not on this page · raw message

lib/efi_selftest/efi_selftest_disk_image.h new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.14.2
#17 lore raw

PATCH 17/17 efi_driver: EFI block driver

In reply to Heinrich Schuchardt

From: Heinrich Schuchardt <[email protected]>

---
 common/board_r.c                  |   3 +
 drivers/block/blk-uclass.c        |   4 +-
 include/blk.h                     |   1 +
 include/config_fallbacks.h        |   1 +
 include/dm/uclass-id.h            |   1 +
 include/efi_driver.h              |  30 ++++
 include/efi_loader.h              |   2 +
 lib/Makefile                      |   1 +
 lib/efi_driver/Makefile           |  13 ++
 lib/efi_driver/efi_block_device.c | 153 ++++++++++++++++++
 lib/efi_driver/efi_uclass.c       | 318 ++++++++++++++++++++++++++++++++++++++
 11 files changed, 526 insertions(+), 1 deletion(-)
 create mode 100644 include/efi_driver.h
 create mode 100644 lib/efi_driver/Makefile
 create mode 100644 lib/efi_driver/efi_block_device.c
 create mode 100644 lib/efi_driver/efi_uclass.c
common/board_r.c +3 -0
index fcfbdfdbb1..98811edc07 100644
@@ -715,7 +715,10 @@ static init_fnc_t init_sequence_r[] = {
 	set_cpu_clk_info, /* Setup clock information */
 #endif
 #ifdef CONFIG_EFI_LOADER
+	/* Setup EFI memory before any other EFI related code */
 	efi_memory_init,
+	/* Install EFI drivers */
+	efi_driver_init,
 #endif
 	stdio_init_tables,
 	initr_serial,
drivers/block/blk-uclass.c +3 -1
index 010ed32d3a..bfda2211f0 100644
@@ -24,6 +24,7 @@ static const char *if_typename_str[IF_TYPE_COUNT] = {
 	[IF_TYPE_HOST]		= "host",
 	[IF_TYPE_SYSTEMACE]	= "ace",
 	[IF_TYPE_NVME]		= "nvme",
+	[IF_TYPE_EFI]		= "efi",
 };
 
 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
@@ -36,8 +37,9 @@ static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
 	[IF_TYPE_SD]		= UCLASS_INVALID,
 	[IF_TYPE_SATA]		= UCLASS_AHCI,
 	[IF_TYPE_HOST]		= UCLASS_ROOT,
-	[IF_TYPE_NVME]		= UCLASS_NVME,
 	[IF_TYPE_SYSTEMACE]	= UCLASS_INVALID,
+	[IF_TYPE_NVME]		= UCLASS_NVME,
+	[IF_TYPE_EFI]		= UCLASS_EFI,
 };
 
 static enum if_type if_typename_to_iftype(const char *if_typename)
include/blk.h +1 -0
index 41b4d7efa8..69b5a98e56 100644
@@ -34,6 +34,7 @@ enum if_type {
 	IF_TYPE_HOST,
 	IF_TYPE_SYSTEMACE,
 	IF_TYPE_NVME,
+	IF_TYPE_EFI,
 
 	IF_TYPE_COUNT,			/* Number of interface types */
 };
include/config_fallbacks.h +1 -0
index 2c4d43d672..524313d5aa 100644
@@ -52,6 +52,7 @@
 	defined(CONFIG_MMC) || \
 	defined(CONFIG_NVME) || \
 	defined(CONFIG_SYSTEMACE) || \
+	(defined(CONFIG_EFI_LOADER) && !defined(CONFIG_SPL_BUILD)) || \
 	defined(CONFIG_SANDBOX)
 #define HAVE_BLOCK_DEVICE
 #endif
include/dm/uclass-id.h +1 -0
index 3fc20834ae..07fabc3ce6 100644
@@ -34,6 +34,7 @@ enum uclass_id {
 	UCLASS_CROS_EC,		/* Chrome OS EC */
 	UCLASS_DISPLAY,		/* Display (e.g. DisplayPort, HDMI) */
 	UCLASS_DMA,		/* Direct Memory Access */
+	UCLASS_EFI,		/* EFI managed devices */
 	UCLASS_ETH,		/* Ethernet device */
 	UCLASS_GPIO,		/* Bank of general-purpose I/O pins */
 	UCLASS_FIRMWARE,	/* Firmware */
include/efi_driver.h new file +30 -0
new file mode 100644
index 0000000000..2bbe26c6e3
@@ -0,0 +1,30 @@
+/*
+ *  EFI application loader
+ *
+ *  Copyright (c) 2017 Heinrich Schuchardt
+ *
+ *  SPDX-License-Identifier:     GPL-2.0+
+ */
+
+#ifndef _EFI_DRIVER_H
+#define _EFI_DRIVER_H 1
+
+#include <common.h>
+#include <dm.h>
+#include <efi_loader.h>
+
+struct efi_driver_ops {
+	const efi_guid_t *protocol;
+	const efi_guid_t *child_protocol;
+	int (*bind)(efi_handle_t handle, void *interface);
+};
+
+/*
+ * This structure adds internal fields to the driver binding protocol.
+ */
+struct efi_driver_binding_extended_protocol {
+	struct efi_driver_binding_protocol bp;
+	const struct efi_driver_ops *ops;
+};
+
+#endif /* _EFI_DRIVER_H */
include/efi_loader.h +2 -0
index 711c901eda..a465175d1f 100644
@@ -273,6 +273,8 @@ efi_status_t efi_get_memory_map(efi_uintn_t *memory_map_size,
 /* Adds a range into the EFI memory map */
 uint64_t efi_add_memory_map(uint64_t start, uint64_t pages, int memory_type,
 			    bool overlap_only_ram);
+/* Called by board init to initialize the EFI drivers */
+int efi_driver_init(void);
 /* Called by board init to initialize the EFI memory map */
 int efi_memory_init(void);
 /* Adds new or overrides configuration table entry to the system table */
lib/Makefile +1 -0
index 8cd779f8ca..0db41c19f3 100644
@@ -8,6 +8,7 @@
 ifndef CONFIG_SPL_BUILD
 
 obj-$(CONFIG_EFI) += efi/
+obj-$(CONFIG_EFI_LOADER) += efi_driver/
 obj-$(CONFIG_EFI_LOADER) += efi_loader/
 obj-$(CONFIG_EFI_LOADER) += efi_selftest/
 obj-$(CONFIG_LZMA) += lzma/
lib/efi_driver/Makefile new file +13 -0
new file mode 100644
index 0000000000..e35529a952
@@ -0,0 +1,13 @@
+#
+# (C) Copyright 2017 Heinrich Schuchardt
+#
+#  SPDX-License-Identifier:     GPL-2.0+
+#
+
+# This file only gets included with CONFIG_EFI_LOADER set, so all
+# object inclusion implicitly depends on it
+
+obj-y += efi_uclass.o
+ifeq ($(CONFIG_BLK)$(CONFIG_PARTITIONS),yy)
+obj-y += efi_block_device.o
+endif
lib/efi_driver/efi_block_device.c new file +153 -0

153 lines not on this page · raw message

lib/efi_driver/efi_uclass.c new file +318 -0

318 lines not on this page · raw message

Signature · 1 line
2.14.2