--- /dev/null
+/*\r
+ * GRUB -- GRand Unified Bootloader\r
+ * Copyright (C) 2022 Free Software Foundation, Inc.\r
+ *\r
+ * GRUB is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * GRUB is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.\r
+ *\r
+ */\r
+\r
+#include <grub/dl.h>\r
+#include <grub/efi/api.h>\r
+#include <grub/efi/efi.h>\r
+#include <grub/err.h>\r
+#include <grub/extcmd.h>\r
+#include <grub/file.h>\r
+#include <grub/i18n.h>\r
+#include <grub/misc.h>\r
+#include <grub/mm.h>\r
+#include <grub/types.h>\r
+\r
+GRUB_MOD_LICENSE ("GPLv3+");\r
+\r
+static grub_efi_guid_t loaded_image_guid = GRUB_EFI_LOADED_IMAGE_GUID;\r
+\r
+static grub_efi_status_t\r
+grub_efi_connect_all (void)\r
+{\r
+ grub_efi_status_t status;\r
+ grub_efi_uintn_t handle_count;\r
+ grub_efi_handle_t *handle_buffer;\r
+ grub_efi_uintn_t index;\r
+ grub_efi_boot_services_t *b;\r
+ grub_dprintf ("efi", "Connecting ...\n");\r
+ b = grub_efi_system_table->boot_services;\r
+ status = efi_call_5 (b->locate_handle_buffer,\r
+ GRUB_EFI_ALL_HANDLES, NULL, NULL,\r
+ &handle_count, &handle_buffer);\r
+\r
+ if (status != GRUB_EFI_SUCCESS)\r
+ return status;\r
+\r
+ for (index = 0; index < handle_count; index++)\r
+ {\r
+ status = efi_call_4 (b->connect_controller,\r
+ handle_buffer[index], NULL, NULL, 1);\r
+ }\r
+\r
+ if (handle_buffer)\r
+ {\r
+ efi_call_1 (b->free_pool, handle_buffer);\r
+ }\r
+ return GRUB_EFI_SUCCESS;\r
+}\r
+\r
+static grub_err_t\r
+grub_efi_load_driver (grub_size_t size, void *boot_image, int connect)\r
+{\r
+ grub_efi_status_t status;\r
+ grub_efi_handle_t driver_handle;\r
+ grub_efi_boot_services_t *b;\r
+ grub_efi_loaded_image_t *loaded_image;\r
+\r
+ b = grub_efi_system_table->boot_services;\r
+\r
+ status = efi_call_6 (b->load_image, 0, grub_efi_image_handle, NULL,\r
+ boot_image, size, &driver_handle);\r
+ if (status != GRUB_EFI_SUCCESS)\r
+ {\r
+ if (status == GRUB_EFI_OUT_OF_RESOURCES)\r
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, "out of resources");\r
+ else\r
+ grub_error (GRUB_ERR_BAD_OS, "cannot load image");\r
+ goto fail;\r
+ }\r
+ loaded_image = grub_efi_get_loaded_image (driver_handle);\r
+ if (! loaded_image)\r
+ {\r
+ grub_error (GRUB_ERR_BAD_OS, "no loaded image available");\r
+ goto fail;\r
+ }\r
+ grub_dprintf ("efi", "Registering loaded image\n");\r
+ status = efi_call_3 (b->handle_protocol, driver_handle,\r
+ &loaded_image_guid, (void **)&loaded_image);\r
+ if (status != GRUB_EFI_SUCCESS)\r
+ {\r
+ grub_error (GRUB_ERR_BAD_OS, "not a dirver");\r
+ goto fail;\r
+ }\r
+ grub_dprintf ("efi", "StartImage: %p\n", boot_image);\r
+ status = efi_call_3 (b->start_image, driver_handle, NULL, NULL);\r
+ if (status != GRUB_EFI_SUCCESS)\r
+ {\r
+ grub_error (GRUB_ERR_BAD_OS, "StartImage failed");\r
+ goto fail;\r
+ }\r
+ if (connect)\r
+ {\r
+ status = grub_efi_connect_all ();\r
+ if (status != GRUB_EFI_SUCCESS)\r
+ {\r
+ grub_error (GRUB_ERR_BAD_OS, "cannot connect controllers\n");\r
+ goto fail;\r
+ }\r
+ }\r
+ grub_dprintf ("efi", "Driver installed\n");\r
+ return 0;\r
+fail:\r
+ return grub_errno;\r
+}\r
+\r
+static const struct grub_arg_option options_fwload[] =\r
+{\r
+ {"nc", 'n', 0, N_("Loads the driver, but does not connect the driver."), 0, 0},\r
+ {0, 0, 0, 0, 0, 0}\r
+};\r
+\r
+static grub_err_t\r
+grub_cmd_fwload (grub_extcmd_context_t ctxt, int argc, char **args)\r
+{\r
+ struct grub_arg_list *state = ctxt->state;\r
+ int connect = 1;\r
+ grub_file_t file = 0;\r
+ grub_efi_boot_services_t *b;\r
+ grub_efi_status_t status;\r
+ grub_efi_uintn_t pages = 0;\r
+ grub_ssize_t size;\r
+ grub_efi_physical_address_t address;\r
+ void *boot_image = 0;\r
+\r
+ b = grub_efi_system_table->boot_services;\r
+ if (argc != 1)\r
+ goto fail;\r
+\r
+ file = grub_file_open (args[0], GRUB_FILE_TYPE_EFI_CHAINLOADED_IMAGE);\r
+ if (! file)\r
+ goto fail;\r
+ size = grub_file_size (file);\r
+ if (!size)\r
+ {\r
+ grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), args[0]);\r
+ goto fail;\r
+ }\r
+ pages = (((grub_efi_uintn_t) size + ((1 << 12) - 1)) >> 12);\r
+ status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ANY_PAGES,\r
+ GRUB_EFI_LOADER_CODE, pages, &address);\r
+ if (status != GRUB_EFI_SUCCESS)\r
+ {\r
+ grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));\r
+ goto fail;\r
+ }\r
+ boot_image = (void *) ((grub_addr_t) address);\r
+ if (grub_file_read (file, boot_image, size) != size)\r
+ {\r
+ if (grub_errno == GRUB_ERR_NONE)\r
+ grub_error (GRUB_ERR_BAD_OS, N_("premature end of file %s"), args[0]);\r
+ goto fail;\r
+ }\r
+ grub_file_close (file);\r
+ if (state[0].set)\r
+ connect = 0;\r
+ if (grub_efi_load_driver (size, boot_image, connect))\r
+ goto fail;\r
+ return GRUB_ERR_NONE;\r
+fail:\r
+ if (file)\r
+ grub_file_close (file);\r
+ if (address)\r
+ efi_call_2 (b->free_pages, address, pages);\r
+ return grub_errno;\r
+}\r
+\r
+static grub_err_t\r
+grub_cmd_fwconnect (grub_extcmd_context_t ctxt __attribute__ ((unused)),\r
+ int argc __attribute__ ((unused)),\r
+ char **args __attribute__ ((unused)))\r
+{\r
+ grub_efi_connect_all ();\r
+ return GRUB_ERR_NONE;\r
+}\r
+\r
+static grub_extcmd_t cmd_fwload, cmd_fwconnect;\r
+\r
+GRUB_MOD_INIT(fwload)\r
+{\r
+ cmd_fwload = grub_register_extcmd ("fwload", grub_cmd_fwload, 0, N_("FILE"),\r
+ N_("Install UEFI driver."), options_fwload);\r
+ cmd_fwconnect = grub_register_extcmd ("fwconnect", grub_cmd_fwconnect, 0,\r
+ NULL, N_("Connect drivers."), 0);\r
+}\r
+\r
+GRUB_MOD_FINI(fwload)\r
+{\r
+ grub_unregister_extcmd (cmd_fwload);\r
+ grub_unregister_extcmd (cmd_fwconnect);\r
+}\r