bli.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2023 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Implementation of the Boot Loader Interface.
  19. */
  20. #include <grub/charset.h>
  21. #include <grub/efi/api.h>
  22. #include <grub/efi/disk.h>
  23. #include <grub/efi/efi.h>
  24. #include <grub/err.h>
  25. #include <grub/extcmd.h>
  26. #include <grub/gpt_partition.h>
  27. #include <grub/misc.h>
  28. #include <grub/mm.h>
  29. #include <grub/partition.h>
  30. #include <grub/types.h>
  31. GRUB_MOD_LICENSE ("GPLv3+");
  32. #define MODNAME "bli"
  33. static const grub_guid_t bli_vendor_guid = GRUB_EFI_VENDOR_BOOT_LOADER_INTERFACE_GUID;
  34. static grub_err_t
  35. get_part_uuid (const char *device_name, char **part_uuid)
  36. {
  37. grub_device_t device;
  38. grub_err_t status = GRUB_ERR_NONE;
  39. grub_disk_t disk = NULL;
  40. struct grub_gpt_partentry entry;
  41. device = grub_device_open (device_name);
  42. if (device == NULL)
  43. return grub_error (grub_errno, N_("cannot open device: %s"), device_name);
  44. if (device->disk == NULL)
  45. {
  46. grub_dprintf ("bli", "%s is not a disk device, partuuid skipped\n", device_name);
  47. *part_uuid = NULL;
  48. grub_device_close (device);
  49. return GRUB_ERR_NONE;
  50. }
  51. if (device->disk->partition == NULL)
  52. {
  53. grub_dprintf ("bli", "%s has no partition, partuuid skipped\n", device_name);
  54. *part_uuid = NULL;
  55. grub_device_close (device);
  56. return GRUB_ERR_NONE;
  57. }
  58. disk = grub_disk_open (device->disk->name);
  59. if (disk == NULL)
  60. {
  61. status = grub_error (grub_errno, N_("cannot open disk: %s"), device_name);
  62. grub_device_close (device);
  63. return status;
  64. }
  65. if (grub_strcmp (device->disk->partition->partmap->name, "gpt") != 0)
  66. {
  67. status = grub_error (GRUB_ERR_BAD_PART_TABLE,
  68. N_("this is not a GPT partition table: %s"), device_name);
  69. goto fail;
  70. }
  71. if (grub_disk_read (disk, device->disk->partition->offset,
  72. device->disk->partition->index, sizeof (entry), &entry) != GRUB_ERR_NONE)
  73. {
  74. status = grub_error (grub_errno, N_("read error: %s"), device_name);
  75. goto fail;
  76. }
  77. *part_uuid = grub_xasprintf ("%pG", &entry.guid);
  78. if (*part_uuid == NULL)
  79. status = grub_errno;
  80. fail:
  81. grub_disk_close (disk);
  82. grub_device_close (device);
  83. return status;
  84. }
  85. static grub_err_t
  86. set_loader_device_part_uuid (void)
  87. {
  88. grub_efi_loaded_image_t *image;
  89. char *device_name;
  90. grub_err_t status = GRUB_ERR_NONE;
  91. char *part_uuid = NULL;
  92. image = grub_efi_get_loaded_image (grub_efi_image_handle);
  93. if (image == NULL)
  94. return grub_error (GRUB_ERR_BAD_DEVICE, N_("unable to find boot device"));
  95. device_name = grub_efidisk_get_device_name (image->device_handle);
  96. if (device_name == NULL)
  97. return grub_error (GRUB_ERR_BAD_DEVICE, N_("unable to find boot device"));
  98. status = get_part_uuid (device_name, &part_uuid);
  99. if (status == GRUB_ERR_NONE && part_uuid)
  100. status = grub_efi_set_variable_to_string ("LoaderDevicePartUUID", &bli_vendor_guid, part_uuid,
  101. GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
  102. GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
  103. else
  104. grub_error (status, N_("unable to determine partition UUID of boot device"));
  105. grub_free (part_uuid);
  106. grub_free (device_name);
  107. return status;
  108. }
  109. GRUB_MOD_INIT (bli)
  110. {
  111. grub_efi_set_variable_to_string ("LoaderInfo", &bli_vendor_guid, PACKAGE_STRING,
  112. GRUB_EFI_VARIABLE_BOOTSERVICE_ACCESS |
  113. GRUB_EFI_VARIABLE_RUNTIME_ACCESS);
  114. set_loader_device_part_uuid ();
  115. /* No error here is critical, other than being logged */
  116. grub_print_error ();
  117. }