forked from bottlerocket-os/bottlerocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0037-gpt-read-entries-table-at-the-same-time-as-the-heade.patch
134 lines (120 loc) · 4.04 KB
/
0037-gpt-read-entries-table-at-the-same-time-as-the-heade.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
From d9f966bfcdfbe8fe1ccb08c512e26a9deae4f60c Mon Sep 17 00:00:00 2001
From: Michael Marineau <michael.marineau@coreos.com>
Date: Wed, 21 Sep 2016 15:29:55 -0700
Subject: [PATCH] gpt: read entries table at the same time as the header
I personally think this reads easier. Also has the side effect of
directly comparing the primary and backup tables instead of presuming
they are equal if the crc32 matches.
---
grub-core/lib/gpt.c | 69 +++++++++++++++++++++++++++------------------
1 file changed, 41 insertions(+), 28 deletions(-)
diff --git a/grub-core/lib/gpt.c b/grub-core/lib/gpt.c
index c27bcc5..b93cede 100644
--- a/grub-core/lib/gpt.c
+++ b/grub-core/lib/gpt.c
@@ -32,6 +32,11 @@ GRUB_MOD_LICENSE ("GPLv3+");
static grub_uint8_t grub_gpt_magic[] = GRUB_GPT_HEADER_MAGIC;
+static grub_err_t
+grub_gpt_read_entries (grub_disk_t disk, grub_gpt_t gpt,
+ struct grub_gpt_header *header,
+ void **ret_entries,
+ grub_size_t *ret_entries_size);
char *
grub_gpt_guid_to_str (grub_gpt_guid_t *guid)
@@ -400,12 +405,21 @@ grub_gpt_read_primary (grub_disk_t disk, grub_gpt_t gpt)
return grub_errno;
gpt->status |= GRUB_GPT_PRIMARY_HEADER_VALID;
+
+ if (grub_gpt_read_entries (disk, gpt, &gpt->primary,
+ &gpt->entries, &gpt->entries_size))
+ return grub_errno;
+
+ gpt->status |= GRUB_GPT_PRIMARY_ENTRIES_VALID;
+
return GRUB_ERR_NONE;
}
static grub_err_t
grub_gpt_read_backup (grub_disk_t disk, grub_gpt_t gpt)
{
+ void *entries = NULL;
+ grub_size_t entries_size;
grub_uint64_t sector;
grub_disk_addr_t addr;
@@ -441,12 +455,35 @@ grub_gpt_read_backup (grub_disk_t disk, grub_gpt_t gpt)
return grub_error (GRUB_ERR_BAD_PART_TABLE, "invalid backup GPT LBA");
gpt->status |= GRUB_GPT_BACKUP_HEADER_VALID;
+
+ if (grub_gpt_read_entries (disk, gpt, &gpt->backup,
+ &entries, &entries_size))
+ return grub_errno;
+
+ if (gpt->status & GRUB_GPT_PRIMARY_ENTRIES_VALID)
+ {
+ if (entries_size != gpt->entries_size ||
+ grub_memcmp (entries, gpt->entries, entries_size) != 0)
+ return grub_error (GRUB_ERR_BAD_PART_TABLE, "backup GPT out of sync");
+
+ grub_free (entries);
+ }
+ else
+ {
+ gpt->entries = entries;
+ gpt->entries_size = entries_size;
+ }
+
+ gpt->status |= GRUB_GPT_BACKUP_ENTRIES_VALID;
+
return GRUB_ERR_NONE;
}
static grub_err_t
grub_gpt_read_entries (grub_disk_t disk, grub_gpt_t gpt,
- struct grub_gpt_header *header)
+ struct grub_gpt_header *header,
+ void **ret_entries,
+ grub_size_t *ret_entries_size)
{
void *entries = NULL;
grub_uint32_t count, size, crc;
@@ -488,9 +525,8 @@ grub_gpt_read_entries (grub_disk_t disk, grub_gpt_t gpt,
goto fail;
}
- grub_free (gpt->entries);
- gpt->entries = entries;
- gpt->entries_size = entries_size;
+ *ret_entries = entries;
+ *ret_entries_size = entries_size;
return GRUB_ERR_NONE;
fail:
@@ -529,30 +565,7 @@ grub_gpt_read (grub_disk_t disk)
grub_gpt_read_backup (disk, gpt);
/* If either succeeded clear any possible error from the other. */
- if (gpt->status & GRUB_GPT_PRIMARY_HEADER_VALID ||
- gpt->status & GRUB_GPT_BACKUP_HEADER_VALID)
- grub_errno = GRUB_ERR_NONE;
- else
- goto fail;
-
- /* Similarly, favor the value or error from the primary table. */
- if (gpt->status & GRUB_GPT_BACKUP_HEADER_VALID &&
- !grub_gpt_read_entries (disk, gpt, &gpt->backup))
- {
- grub_dprintf ("gpt", "read valid backup GPT from %s\n", disk->name);
- gpt->status |= GRUB_GPT_BACKUP_ENTRIES_VALID;
- }
-
- grub_errno = GRUB_ERR_NONE;
- if (gpt->status & GRUB_GPT_PRIMARY_HEADER_VALID &&
- !grub_gpt_read_entries (disk, gpt, &gpt->primary))
- {
- grub_dprintf ("gpt", "read valid primary GPT from %s\n", disk->name);
- gpt->status |= GRUB_GPT_PRIMARY_ENTRIES_VALID;
- }
-
- if (gpt->status & GRUB_GPT_PRIMARY_ENTRIES_VALID ||
- gpt->status & GRUB_GPT_BACKUP_ENTRIES_VALID)
+ if (grub_gpt_primary_valid (gpt) || grub_gpt_backup_valid (gpt))
grub_errno = GRUB_ERR_NONE;
else
goto fail;
--
2.21.3