|
| 1 | +From a8dbd32d74f5e4ae0f1adac9b1486d6f05c21e0c Mon Sep 17 00:00:00 2001 |
| 2 | +From: Mikhail Malyshev < [email protected]> |
| 3 | +Date: Tue, 30 May 2023 13:59:05 +0000 |
| 4 | +Subject: [PATCH 14/14] Put removable hard drives detected by UEFI at the end |
| 5 | + of the drive list |
| 6 | + |
| 7 | +When grub queries for available disks it doesn't take into account that |
| 8 | +the disk can be removable e.g. USB stick. The disk can appear in front of regular |
| 9 | +HDDs and the numbering will be different e.g. hd0 become hd1 when the |
| 10 | +USB stick is plugged in. It is not a problem for GRUB to find a correct |
| 11 | +partition in this case and the system can be booted just fine. However |
| 12 | +every command from grub.cfg is measured into PCR8 while being executed |
| 13 | +and HDD names appear in those commands e.g. 'set root=(hd2,gpt5)'. If |
| 14 | +any key is sealed into TPM using PCR8 then that key cannot be unsealed when a |
| 15 | +random USB stick is inserted (or removed if it was inserted when the key |
| 16 | +was sealed) |
| 17 | + |
| 18 | +The original issue should not affect PC BIOS case because USB devices |
| 19 | +are usually emulated as either CD or floppy drives and have their unique |
| 20 | +numbering |
| 21 | + |
| 22 | +The behaviour is controlled by reorder_removable_media flag set through |
| 23 | +eve_quirks environment variable |
| 24 | + |
| 25 | +Signed-off-by: Mikhail Malyshev < [email protected]> |
| 26 | +--- |
| 27 | + grub-core/disk/efi/efidisk.c | 49 ++++++++++++++++++++++++++++++------ |
| 28 | + 1 file changed, 41 insertions(+), 8 deletions(-) |
| 29 | + |
| 30 | +diff --git a/grub-core/disk/efi/efidisk.c b/grub-core/disk/efi/efidisk.c |
| 31 | +index 5d2400f66..916e8ec23 100644 |
| 32 | +--- a/grub-core/disk/efi/efidisk.c |
| 33 | ++++ b/grub-core/disk/efi/efidisk.c |
| 34 | +@@ -41,6 +41,7 @@ static grub_efi_guid_t block_io_guid = GRUB_EFI_BLOCK_IO_GUID; |
| 35 | + |
| 36 | + static struct grub_efidisk_data *fd_devices; |
| 37 | + static struct grub_efidisk_data *hd_devices; |
| 38 | ++static struct grub_efidisk_data *hd_removable_devices; |
| 39 | + static struct grub_efidisk_data *cd_devices; |
| 40 | + |
| 41 | + static struct grub_efidisk_data * |
| 42 | +@@ -256,14 +257,25 @@ name_devices (struct grub_efidisk_data *devices) |
| 43 | + } |
| 44 | + if (is_hard_drive) |
| 45 | + { |
| 46 | ++ if (parent->block_io->media->removable_media == 1) |
| 47 | ++ { |
| 48 | + #ifdef DEBUG_NAMES |
| 49 | +- grub_printf ("adding a hard drive by a partition: "); |
| 50 | +- grub_efi_print_device_path (parent->device_path); |
| 51 | ++ grub_printf("adding a REMOVABLE hard drive by a partition: "); |
| 52 | ++ grub_efi_print_device_path(parent->device_path); |
| 53 | + #endif |
| 54 | +- add_device (&hd_devices, parent); |
| 55 | +- } |
| 56 | +- else |
| 57 | +- { |
| 58 | ++ add_device(&hd_removable_devices, parent); |
| 59 | ++ } |
| 60 | ++ else |
| 61 | ++ { |
| 62 | ++#ifdef DEBUG_NAMES |
| 63 | ++ grub_printf("adding a hard drive by a partition: "); |
| 64 | ++ grub_efi_print_device_path(parent->device_path); |
| 65 | ++#endif |
| 66 | ++ add_device(&hd_devices, parent); |
| 67 | ++ } |
| 68 | ++ } |
| 69 | ++ else |
| 70 | ++ { |
| 71 | + #ifdef DEBUG_NAMES |
| 72 | + grub_printf ("adding a cdrom by a partition: "); |
| 73 | + grub_efi_print_device_path (parent->device_path); |
| 74 | +@@ -359,9 +371,28 @@ name_devices (struct grub_efidisk_data *devices) |
| 75 | + grub_printf ("adding a hard drive by guessing: "); |
| 76 | + grub_efi_print_device_path (d->device_path); |
| 77 | + #endif |
| 78 | +- add_device (&hd_devices, d); |
| 79 | +- } |
| 80 | ++ if (m->removable_media == 0) |
| 81 | ++ { |
| 82 | ++ add_device(&hd_devices, d); |
| 83 | ++ } |
| 84 | ++ else |
| 85 | ++ { |
| 86 | ++ add_device(&hd_removable_devices, d); |
| 87 | ++ } |
| 88 | ++ } |
| 89 | + } |
| 90 | ++ // link the removable devices to the end of the hd_devices list |
| 91 | ++ if (hd_devices) |
| 92 | ++ { |
| 93 | ++ struct grub_efidisk_data *p; |
| 94 | ++ for (p = hd_devices; p->next; p = p->next) |
| 95 | ++ ; |
| 96 | ++ p->next = hd_removable_devices; |
| 97 | ++ } |
| 98 | ++ else |
| 99 | ++ { |
| 100 | ++ hd_devices = hd_removable_devices; |
| 101 | ++ } |
| 102 | + } |
| 103 | + |
| 104 | + static void |
| 105 | +@@ -641,8 +672,10 @@ grub_efidisk_fini (void) |
| 106 | + free_devices (fd_devices); |
| 107 | + free_devices (hd_devices); |
| 108 | + free_devices (cd_devices); |
| 109 | ++ // do not free hd_removable_devices, as it is a subset of hd_devices |
| 110 | + fd_devices = 0; |
| 111 | + hd_devices = 0; |
| 112 | ++ hd_removable_devices = 0; |
| 113 | + cd_devices = 0; |
| 114 | + grub_disk_dev_unregister (&grub_efidisk_dev); |
| 115 | + } |
| 116 | +-- |
| 117 | +2.34.1 |
| 118 | + |
0 commit comments