forked from bottlerocket-os/bottlerocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0006-gpt-add-new-repair-function-to-sync-up-primary-and-b.patch
217 lines (206 loc) · 7.09 KB
/
0006-gpt-add-new-repair-function-to-sync-up-primary-and-b.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
From 36fc577048523438ffbf51291a40fe59eb479724 Mon Sep 17 00:00:00 2001
From: Michael Marineau <michael.marineau@coreos.com>
Date: Sat, 18 Oct 2014 18:21:07 -0700
Subject: [PATCH] gpt: add new repair function to sync up primary and backup
tables.
---
grub-core/lib/gpt.c | 90 ++++++++++++++++++++++++++++++++++++
include/grub/gpt_partition.h | 3 ++
tests/gpt_unit_test.c | 49 ++++++++++++++++++++
3 files changed, 142 insertions(+)
diff --git a/grub-core/lib/gpt.c b/grub-core/lib/gpt.c
index 43a1509..2d61df4 100644
--- a/grub-core/lib/gpt.c
+++ b/grub-core/lib/gpt.c
@@ -31,6 +31,20 @@ GRUB_MOD_LICENSE ("GPLv3+");
static grub_uint8_t grub_gpt_magic[] = GRUB_GPT_HEADER_MAGIC;
+static grub_uint64_t
+grub_gpt_size_to_sectors (grub_gpt_t gpt, grub_size_t size)
+{
+ unsigned int sector_size;
+ grub_uint64_t sectors;
+
+ sector_size = 1U << gpt->log_sector_size;
+ sectors = size / sector_size;
+ if (size % sector_size)
+ sectors++;
+
+ return sectors;
+}
+
static grub_err_t
grub_gpt_lecrc32 (void *data, grub_size_t len, grub_uint32_t *crc)
{
@@ -275,6 +289,82 @@ fail:
return NULL;
}
+grub_err_t
+grub_gpt_repair (grub_disk_t disk, grub_gpt_t gpt)
+{
+ grub_uint64_t backup_header, backup_entries;
+ grub_uint32_t crc;
+
+ if (disk->log_sector_size != gpt->log_sector_size)
+ return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
+ "GPT sector size must match disk sector size");
+
+ if (!(gpt->status & GRUB_GPT_PRIMARY_ENTRIES_VALID ||
+ gpt->status & GRUB_GPT_BACKUP_ENTRIES_VALID))
+ return grub_error (GRUB_ERR_BUG, "No valid GPT entries");
+
+ if (gpt->status & GRUB_GPT_PRIMARY_HEADER_VALID)
+ {
+ backup_header = grub_le_to_cpu64 (gpt->primary.alternate_lba);
+ grub_memcpy (&gpt->backup, &gpt->primary, sizeof (gpt->backup));
+ }
+ else if (gpt->status & GRUB_GPT_BACKUP_HEADER_VALID)
+ {
+ backup_header = grub_le_to_cpu64 (gpt->backup.header_lba);
+ grub_memcpy (&gpt->primary, &gpt->backup, sizeof (gpt->primary));
+ }
+ else
+ return grub_error (GRUB_ERR_BUG, "No valid GPT header");
+
+ /* Relocate backup to end if disk whenever possible. */
+ if (disk->total_sectors != GRUB_DISK_SIZE_UNKNOWN)
+ backup_header = disk->total_sectors - 1;
+
+ backup_entries = backup_header -
+ grub_gpt_size_to_sectors (gpt, gpt->entries_size);
+
+ /* Update/fixup header and partition table locations. */
+ gpt->primary.header_lba = grub_cpu_to_le64_compile_time (1);
+ gpt->primary.alternate_lba = grub_cpu_to_le64 (backup_header);
+ gpt->primary.partitions = grub_cpu_to_le64_compile_time (2);
+ gpt->backup.header_lba = gpt->primary.alternate_lba;
+ gpt->backup.alternate_lba = gpt->primary.header_lba;
+ gpt->backup.partitions = grub_cpu_to_le64 (backup_entries);
+
+ /* Writing headers larger than our header structure are unsupported. */
+ gpt->primary.headersize =
+ grub_cpu_to_le32_compile_time (sizeof (gpt->primary));
+ gpt->backup.headersize =
+ grub_cpu_to_le32_compile_time (sizeof (gpt->backup));
+
+ /* Recompute checksums. */
+ if (grub_gpt_lecrc32 (gpt->entries, gpt->entries_size, &crc))
+ return grub_errno;
+
+ gpt->primary.partentry_crc32 = crc;
+ gpt->backup.partentry_crc32 = crc;
+
+ if (grub_gpt_header_lecrc32 (&gpt->primary, &gpt->primary.crc32))
+ return grub_errno;
+
+ if (grub_gpt_header_lecrc32 (&gpt->backup, &gpt->backup.crc32))
+ return grub_errno;
+
+ /* Sanity check. */
+ if (grub_gpt_header_check (&gpt->primary, gpt->log_sector_size))
+ return grub_error (GRUB_ERR_BUG, "Generated invalid GPT primary header");
+
+ if (grub_gpt_header_check (&gpt->backup, gpt->log_sector_size))
+ return grub_error (GRUB_ERR_BUG, "Generated invalid GPT backup header");
+
+ gpt->status |= (GRUB_GPT_PRIMARY_HEADER_VALID |
+ GRUB_GPT_PRIMARY_ENTRIES_VALID |
+ GRUB_GPT_BACKUP_HEADER_VALID |
+ GRUB_GPT_BACKUP_ENTRIES_VALID);
+
+ return GRUB_ERR_NONE;
+}
+
void
grub_gpt_free (grub_gpt_t gpt)
{
diff --git a/include/grub/gpt_partition.h b/include/grub/gpt_partition.h
index 21ea08d..b45acbd 100644
--- a/include/grub/gpt_partition.h
+++ b/include/grub/gpt_partition.h
@@ -141,6 +141,9 @@ grub_gpt_sector_to_addr (grub_gpt_t gpt, grub_uint64_t sector)
/* Allocates and fills new grub_gpt structure, free with grub_gpt_free. */
grub_gpt_t grub_gpt_read (grub_disk_t disk);
+/* Sync up primary and backup headers, recompute checksums. */
+grub_err_t grub_gpt_repair (grub_disk_t disk, grub_gpt_t gpt);
+
void grub_gpt_free (grub_gpt_t gpt);
grub_err_t grub_gpt_pmbr_check (struct grub_msdos_partition_mbr *mbr);
diff --git a/tests/gpt_unit_test.c b/tests/gpt_unit_test.c
index 4d70868..83198be 100644
--- a/tests/gpt_unit_test.c
+++ b/tests/gpt_unit_test.c
@@ -24,6 +24,7 @@
#include <grub/err.h>
#include <grub/gpt_partition.h>
#include <grub/msdos_partition.h>
+#include <grub/lib/hexdump.h>
#include <grub/test.h>
#include <errno.h>
@@ -442,6 +443,52 @@ read_fallback_test (void)
close_disk (&data);
}
+static void
+repair_test (void)
+{
+ struct test_data data;
+ grub_gpt_t gpt;
+
+ open_disk (&data);
+
+ /* Erase/Repair primary. */
+ memset (&data.raw->primary_header, 0, sizeof (data.raw->primary_header));
+ sync_disk (&data);
+ gpt = read_disk (&data);
+ grub_gpt_repair (data.dev->disk, gpt);
+ grub_test_assert (grub_errno == GRUB_ERR_NONE,
+ "repair failed: %s", grub_errmsg);
+ if (memcmp (&gpt->primary, &example_primary, sizeof (gpt->primary)))
+ {
+ printf ("Invalid restored primary header:\n");
+ hexdump (16, (char*)&gpt->primary, sizeof (gpt->primary));
+ printf ("Expected primary header:\n");
+ hexdump (16, (char*)&example_primary, sizeof (example_primary));
+ grub_test_assert (0, "repair did not restore primary header");
+ }
+ grub_gpt_free (gpt);
+ reset_disk (&data);
+
+ /* Erase/Repair backup. */
+ memset (&data.raw->backup_header, 0, sizeof (data.raw->backup_header));
+ sync_disk (&data);
+ gpt = read_disk (&data);
+ grub_gpt_repair (data.dev->disk, gpt);
+ grub_test_assert (grub_errno == GRUB_ERR_NONE,
+ "repair failed: %s", grub_errmsg);
+ if (memcmp (&gpt->backup, &example_backup, sizeof (gpt->backup)))
+ {
+ printf ("Invalid restored backup header:\n");
+ hexdump (16, (char*)&gpt->backup, sizeof (gpt->backup));
+ printf ("Expected backup header:\n");
+ hexdump (16, (char*)&example_backup, sizeof (example_backup));
+ grub_test_assert (0, "repair did not restore backup header");
+ }
+ grub_gpt_free (gpt);
+ reset_disk (&data);
+
+ close_disk (&data);
+}
void
grub_unit_test_init (void)
{
@@ -453,6 +500,7 @@ grub_unit_test_init (void)
grub_test_register ("gpt_read_valid_test", read_valid_test);
grub_test_register ("gpt_read_invalid_test", read_invalid_entries_test);
grub_test_register ("gpt_read_fallback_test", read_fallback_test);
+ grub_test_register ("gpt_repair_test", repair_test);
}
void
@@ -463,5 +511,6 @@ grub_unit_test_fini (void)
grub_test_unregister ("gpt_read_valid_test");
grub_test_unregister ("gpt_read_invalid_test");
grub_test_unregister ("gpt_read_fallback_test");
+ grub_test_unregister ("gpt_repair_test");
grub_fini_all ();
}
--
2.21.3