]> glassweightruler.freedombox.rocks Git - Ventoy.git/commitdiff
add support for uefi driver loading (#1552)
authorA1ive <10670106+a1ive@users.noreply.github.com>
Wed, 6 Apr 2022 07:13:37 +0000 (15:13 +0800)
committerGitHub <noreply@github.com>
Wed, 6 Apr 2022 07:13:37 +0000 (15:13 +0800)
GRUB2/MOD_SRC/grub-2.04/grub-core/Makefile.core.def
GRUB2/MOD_SRC/grub-2.04/grub-core/commands/efi/fwload.c [new file with mode: 0644]
GRUB2/MOD_SRC/grub-2.04/install.sh

index 5daba477db5241d88587176705d022118cc6203b..dbb1f1cc20edd30e1281b89c698d14b4f1836d93 100644 (file)
@@ -913,6 +913,12 @@ module = {
   enable = x86_64_efi;
 };
 
+module = {
+  name = fwload;
+  efi = commands/efi/fwload.c;
+  enable = efi;
+};
+
 module = {
   name = gptsync;
   common = commands/gptsync.c;
diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/commands/efi/fwload.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/commands/efi/fwload.c
new file mode 100644 (file)
index 0000000..6adaf6a
--- /dev/null
@@ -0,0 +1,205 @@
+/*\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
index c6b0d0f267619fb3b72dbbf293e6daaf535157ef..6222037b6e4ffdb45fba35a2718e2d6f140256c0 100644 (file)
@@ -15,7 +15,7 @@ net_modules_legacy="net tftp http"
 all_modules_legacy="file setkey date drivemap blocklist regexp newc vga_text ntldr search at_keyboard usb_keyboard  gcry_md5 hashsum gzio xzio lzopio lspci pci ext2 xfs ventoy chain read halt iso9660 linux16 test true sleep reboot echo videotest videoinfo videotest_checksum video_colors video_cirrus video_bochs vga vbe video_fb font video gettext extcmd terminal  linux minicmd help configfile tr trig boot biosdisk disk ls tar squash4 password_pbkdf2 all_video png jpeg part_gpt part_msdos fat exfat ntfs loopback gzio normal  udf gfxmenu gfxterm gfxterm_background gfxterm_menu"
 
 net_modules_uefi="efinet net tftp http"
-all_modules_uefi="file setkey blocklist ventoy test true regexp newc search at_keyboard usb_keyboard  gcry_md5 hashsum gzio xzio lzopio ext2 xfs read halt sleep serial terminfo png password_pbkdf2 gcry_sha512 pbkdf2 part_gpt part_msdos ls tar squash4 loopback part_apple minicmd diskfilter linux relocator jpeg iso9660 udf hfsplus halt acpi mmap gfxmenu video_colors trig bitmap_scale gfxterm bitmap font fat exfat ntfs fshelp efifwsetup reboot echo configfile normal terminal gettext chain  priority_queue bufio datetime cat extcmd crypto gzio boot all_video efi_gop efi_uga video_bochs video_cirrus video video_fb gfxterm_background gfxterm_menu mouse"
+all_modules_uefi="file setkey blocklist ventoy test true regexp newc search at_keyboard usb_keyboard  gcry_md5 hashsum gzio xzio lzopio ext2 xfs read halt sleep serial terminfo png password_pbkdf2 gcry_sha512 pbkdf2 part_gpt part_msdos ls tar squash4 loopback part_apple minicmd diskfilter linux relocator jpeg iso9660 udf hfsplus halt acpi mmap gfxmenu video_colors trig bitmap_scale gfxterm bitmap font fat exfat ntfs fshelp efifwsetup reboot echo configfile normal terminal gettext chain  priority_queue bufio datetime cat extcmd crypto gzio boot all_video efi_gop efi_uga video_bochs video_cirrus video video_fb gfxterm_background gfxterm_menu mouse fwload"
 
 all_modules_arm64_uefi="file setkey blocklist ventoy test true regexp newc search  gcry_md5 hashsum gzio xzio lzopio ext2 xfs read halt sleep serial terminfo png password_pbkdf2 gcry_sha512 pbkdf2 part_gpt part_msdos ls tar squash4 loopback part_apple minicmd diskfilter linux jpeg iso9660 udf hfsplus halt acpi mmap gfxmenu video_colors trig bitmap_scale gfxterm bitmap font fat exfat ntfs fshelp efifwsetup reboot echo configfile normal terminal gettext chain  priority_queue bufio datetime cat extcmd crypto gzio boot all_video efi_gop video video_fb gfxterm_background gfxterm_menu"