1*8617a60dSAndroid Build Coastguard Worker /* Copyright 2013 The ChromiumOS Authors
2*8617a60dSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
3*8617a60dSAndroid Build Coastguard Worker * found in the LICENSE file.
4*8617a60dSAndroid Build Coastguard Worker *
5*8617a60dSAndroid Build Coastguard Worker * Tests for vboot_kernel.c
6*8617a60dSAndroid Build Coastguard Worker */
7*8617a60dSAndroid Build Coastguard Worker
8*8617a60dSAndroid Build Coastguard Worker #include "2api.h"
9*8617a60dSAndroid Build Coastguard Worker #include "cgptlib.h"
10*8617a60dSAndroid Build Coastguard Worker #include "cgptlib_internal.h"
11*8617a60dSAndroid Build Coastguard Worker #include "common/tests.h"
12*8617a60dSAndroid Build Coastguard Worker #include "gpt.h"
13*8617a60dSAndroid Build Coastguard Worker
14*8617a60dSAndroid Build Coastguard Worker #define LOGCALL(fmt, args...) sprintf(call_log + strlen(call_log), fmt, ##args)
15*8617a60dSAndroid Build Coastguard Worker #define TEST_CALLS(expect_log) TEST_STR_EQ(call_log, expect_log, " calls")
16*8617a60dSAndroid Build Coastguard Worker
17*8617a60dSAndroid Build Coastguard Worker /* Assumes 512-byte disk sectors */
18*8617a60dSAndroid Build Coastguard Worker #define MOCK_SECTOR_SIZE 512
19*8617a60dSAndroid Build Coastguard Worker #define MOCK_SECTOR_COUNT 1024
20*8617a60dSAndroid Build Coastguard Worker
21*8617a60dSAndroid Build Coastguard Worker /* Mock kernel partition */
22*8617a60dSAndroid Build Coastguard Worker struct mock_part {
23*8617a60dSAndroid Build Coastguard Worker uint32_t start;
24*8617a60dSAndroid Build Coastguard Worker uint32_t size;
25*8617a60dSAndroid Build Coastguard Worker };
26*8617a60dSAndroid Build Coastguard Worker
27*8617a60dSAndroid Build Coastguard Worker /* Mock data */
28*8617a60dSAndroid Build Coastguard Worker static char call_log[4096];
29*8617a60dSAndroid Build Coastguard Worker static int disk_read_to_fail;
30*8617a60dSAndroid Build Coastguard Worker static int disk_write_to_fail;
31*8617a60dSAndroid Build Coastguard Worker
32*8617a60dSAndroid Build Coastguard Worker static vb2ex_disk_handle_t handle;
33*8617a60dSAndroid Build Coastguard Worker static uint8_t mock_disk[MOCK_SECTOR_SIZE * MOCK_SECTOR_COUNT];
34*8617a60dSAndroid Build Coastguard Worker static GptHeader *mock_gpt_primary =
35*8617a60dSAndroid Build Coastguard Worker (GptHeader*)&mock_disk[MOCK_SECTOR_SIZE * 1];
36*8617a60dSAndroid Build Coastguard Worker static GptHeader *mock_gpt_secondary =
37*8617a60dSAndroid Build Coastguard Worker (GptHeader*)&mock_disk[MOCK_SECTOR_SIZE * (MOCK_SECTOR_COUNT - 1)];
38*8617a60dSAndroid Build Coastguard Worker
39*8617a60dSAndroid Build Coastguard Worker /**
40*8617a60dSAndroid Build Coastguard Worker * Prepare a valid GPT header that will pass CheckHeader() tests
41*8617a60dSAndroid Build Coastguard Worker */
SetupGptHeader(GptHeader * h,int is_secondary)42*8617a60dSAndroid Build Coastguard Worker static void SetupGptHeader(GptHeader *h, int is_secondary)
43*8617a60dSAndroid Build Coastguard Worker {
44*8617a60dSAndroid Build Coastguard Worker memset(h, '\0', MOCK_SECTOR_SIZE);
45*8617a60dSAndroid Build Coastguard Worker
46*8617a60dSAndroid Build Coastguard Worker /* "EFI PART" */
47*8617a60dSAndroid Build Coastguard Worker memcpy(h->signature, GPT_HEADER_SIGNATURE, GPT_HEADER_SIGNATURE_SIZE);
48*8617a60dSAndroid Build Coastguard Worker h->revision = GPT_HEADER_REVISION;
49*8617a60dSAndroid Build Coastguard Worker h->size = MIN_SIZE_OF_HEADER;
50*8617a60dSAndroid Build Coastguard Worker
51*8617a60dSAndroid Build Coastguard Worker /* 16KB: 128 entries of 128 bytes */
52*8617a60dSAndroid Build Coastguard Worker h->size_of_entry = sizeof(GptEntry);
53*8617a60dSAndroid Build Coastguard Worker h->number_of_entries = MAX_NUMBER_OF_ENTRIES;
54*8617a60dSAndroid Build Coastguard Worker
55*8617a60dSAndroid Build Coastguard Worker /* Set LBA pointers for primary or secondary header */
56*8617a60dSAndroid Build Coastguard Worker if (is_secondary) {
57*8617a60dSAndroid Build Coastguard Worker h->my_lba = MOCK_SECTOR_COUNT - GPT_HEADER_SECTORS;
58*8617a60dSAndroid Build Coastguard Worker h->entries_lba = h->my_lba - CalculateEntriesSectors(h,
59*8617a60dSAndroid Build Coastguard Worker MOCK_SECTOR_SIZE);
60*8617a60dSAndroid Build Coastguard Worker } else {
61*8617a60dSAndroid Build Coastguard Worker h->my_lba = GPT_PMBR_SECTORS;
62*8617a60dSAndroid Build Coastguard Worker h->entries_lba = h->my_lba + 1;
63*8617a60dSAndroid Build Coastguard Worker }
64*8617a60dSAndroid Build Coastguard Worker
65*8617a60dSAndroid Build Coastguard Worker h->first_usable_lba = 2 + CalculateEntriesSectors(h, MOCK_SECTOR_SIZE);
66*8617a60dSAndroid Build Coastguard Worker h->last_usable_lba = MOCK_SECTOR_COUNT - 2 - CalculateEntriesSectors(h,
67*8617a60dSAndroid Build Coastguard Worker MOCK_SECTOR_SIZE);
68*8617a60dSAndroid Build Coastguard Worker
69*8617a60dSAndroid Build Coastguard Worker h->header_crc32 = HeaderCrc(h);
70*8617a60dSAndroid Build Coastguard Worker }
71*8617a60dSAndroid Build Coastguard Worker
ResetCallLog(void)72*8617a60dSAndroid Build Coastguard Worker static void ResetCallLog(void)
73*8617a60dSAndroid Build Coastguard Worker {
74*8617a60dSAndroid Build Coastguard Worker *call_log = 0;
75*8617a60dSAndroid Build Coastguard Worker }
76*8617a60dSAndroid Build Coastguard Worker
77*8617a60dSAndroid Build Coastguard Worker /**
78*8617a60dSAndroid Build Coastguard Worker * Reset mock data (for use before each test)
79*8617a60dSAndroid Build Coastguard Worker */
ResetMocks(void)80*8617a60dSAndroid Build Coastguard Worker static void ResetMocks(void)
81*8617a60dSAndroid Build Coastguard Worker {
82*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
83*8617a60dSAndroid Build Coastguard Worker
84*8617a60dSAndroid Build Coastguard Worker memset(&mock_disk, 0, sizeof(mock_disk));
85*8617a60dSAndroid Build Coastguard Worker SetupGptHeader(mock_gpt_primary, 0);
86*8617a60dSAndroid Build Coastguard Worker SetupGptHeader(mock_gpt_secondary, 1);
87*8617a60dSAndroid Build Coastguard Worker
88*8617a60dSAndroid Build Coastguard Worker disk_read_to_fail = -1;
89*8617a60dSAndroid Build Coastguard Worker disk_write_to_fail = -1;
90*8617a60dSAndroid Build Coastguard Worker }
91*8617a60dSAndroid Build Coastguard Worker
92*8617a60dSAndroid Build Coastguard Worker /* Mocks */
93*8617a60dSAndroid Build Coastguard Worker
VbExDiskRead(vb2ex_disk_handle_t h,uint64_t lba_start,uint64_t lba_count,void * buffer)94*8617a60dSAndroid Build Coastguard Worker vb2_error_t VbExDiskRead(vb2ex_disk_handle_t h, uint64_t lba_start,
95*8617a60dSAndroid Build Coastguard Worker uint64_t lba_count, void *buffer)
96*8617a60dSAndroid Build Coastguard Worker {
97*8617a60dSAndroid Build Coastguard Worker LOGCALL("VbExDiskRead(h, %d, %d)\n", (int)lba_start, (int)lba_count);
98*8617a60dSAndroid Build Coastguard Worker
99*8617a60dSAndroid Build Coastguard Worker if ((int)lba_start == disk_read_to_fail)
100*8617a60dSAndroid Build Coastguard Worker return VB2_ERROR_MOCK;
101*8617a60dSAndroid Build Coastguard Worker
102*8617a60dSAndroid Build Coastguard Worker memcpy(buffer, &mock_disk[lba_start * MOCK_SECTOR_SIZE],
103*8617a60dSAndroid Build Coastguard Worker lba_count * MOCK_SECTOR_SIZE);
104*8617a60dSAndroid Build Coastguard Worker
105*8617a60dSAndroid Build Coastguard Worker return VB2_SUCCESS;
106*8617a60dSAndroid Build Coastguard Worker }
107*8617a60dSAndroid Build Coastguard Worker
VbExDiskWrite(vb2ex_disk_handle_t h,uint64_t lba_start,uint64_t lba_count,const void * buffer)108*8617a60dSAndroid Build Coastguard Worker vb2_error_t VbExDiskWrite(vb2ex_disk_handle_t h, uint64_t lba_start,
109*8617a60dSAndroid Build Coastguard Worker uint64_t lba_count, const void *buffer)
110*8617a60dSAndroid Build Coastguard Worker {
111*8617a60dSAndroid Build Coastguard Worker LOGCALL("VbExDiskWrite(h, %d, %d)\n", (int)lba_start, (int)lba_count);
112*8617a60dSAndroid Build Coastguard Worker
113*8617a60dSAndroid Build Coastguard Worker if ((int)lba_start == disk_write_to_fail)
114*8617a60dSAndroid Build Coastguard Worker return VB2_ERROR_MOCK;
115*8617a60dSAndroid Build Coastguard Worker
116*8617a60dSAndroid Build Coastguard Worker memcpy(&mock_disk[lba_start * MOCK_SECTOR_SIZE], buffer,
117*8617a60dSAndroid Build Coastguard Worker lba_count * MOCK_SECTOR_SIZE);
118*8617a60dSAndroid Build Coastguard Worker
119*8617a60dSAndroid Build Coastguard Worker return VB2_SUCCESS;
120*8617a60dSAndroid Build Coastguard Worker }
121*8617a60dSAndroid Build Coastguard Worker
122*8617a60dSAndroid Build Coastguard Worker /**
123*8617a60dSAndroid Build Coastguard Worker * Test reading/writing GPT
124*8617a60dSAndroid Build Coastguard Worker */
ReadWriteGptTest(void)125*8617a60dSAndroid Build Coastguard Worker static void ReadWriteGptTest(void)
126*8617a60dSAndroid Build Coastguard Worker {
127*8617a60dSAndroid Build Coastguard Worker GptData g;
128*8617a60dSAndroid Build Coastguard Worker GptHeader *h;
129*8617a60dSAndroid Build Coastguard Worker
130*8617a60dSAndroid Build Coastguard Worker g.sector_bytes = MOCK_SECTOR_SIZE;
131*8617a60dSAndroid Build Coastguard Worker g.streaming_drive_sectors = g.gpt_drive_sectors = MOCK_SECTOR_COUNT;
132*8617a60dSAndroid Build Coastguard Worker g.valid_headers = g.valid_entries = MASK_BOTH;
133*8617a60dSAndroid Build Coastguard Worker
134*8617a60dSAndroid Build Coastguard Worker ResetMocks();
135*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0, "AllocAndRead");
136*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
137*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 2, 32)\n"
138*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 1023, 1)\n"
139*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 991, 32)\n");
140*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
141*8617a60dSAndroid Build Coastguard Worker /*
142*8617a60dSAndroid Build Coastguard Worker * Valgrind complains about access to uninitialized memory here, so
143*8617a60dSAndroid Build Coastguard Worker * zero the primary header before each test.
144*8617a60dSAndroid Build Coastguard Worker */
145*8617a60dSAndroid Build Coastguard Worker memset(g.primary_header, '\0', g.sector_bytes);
146*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree");
147*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("");
148*8617a60dSAndroid Build Coastguard Worker
149*8617a60dSAndroid Build Coastguard Worker /*
150*8617a60dSAndroid Build Coastguard Worker * Invalidate primary GPT header,
151*8617a60dSAndroid Build Coastguard Worker * check that AllocAndReadGptData still succeeds
152*8617a60dSAndroid Build Coastguard Worker */
153*8617a60dSAndroid Build Coastguard Worker ResetMocks();
154*8617a60dSAndroid Build Coastguard Worker memset(mock_gpt_primary, '\0', sizeof(*mock_gpt_primary));
155*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0,
156*8617a60dSAndroid Build Coastguard Worker "AllocAndRead primary invalid");
157*8617a60dSAndroid Build Coastguard Worker TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.streaming_drive_sectors,
158*8617a60dSAndroid Build Coastguard Worker g.gpt_drive_sectors, 0, g.sector_bytes),
159*8617a60dSAndroid Build Coastguard Worker 1, "Primary header is invalid");
160*8617a60dSAndroid Build Coastguard Worker TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.streaming_drive_sectors,
161*8617a60dSAndroid Build Coastguard Worker g.gpt_drive_sectors, 0, g.sector_bytes),
162*8617a60dSAndroid Build Coastguard Worker 0, "Secondary header is valid");
163*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
164*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 1023, 1)\n"
165*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 991, 32)\n");
166*8617a60dSAndroid Build Coastguard Worker WriteAndFreeGptData(handle, &g);
167*8617a60dSAndroid Build Coastguard Worker
168*8617a60dSAndroid Build Coastguard Worker /*
169*8617a60dSAndroid Build Coastguard Worker * Invalidate secondary GPT header,
170*8617a60dSAndroid Build Coastguard Worker * check that AllocAndReadGptData still succeeds
171*8617a60dSAndroid Build Coastguard Worker */
172*8617a60dSAndroid Build Coastguard Worker ResetMocks();
173*8617a60dSAndroid Build Coastguard Worker memset(mock_gpt_secondary, '\0', sizeof(*mock_gpt_secondary));
174*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0,
175*8617a60dSAndroid Build Coastguard Worker "AllocAndRead secondary invalid");
176*8617a60dSAndroid Build Coastguard Worker TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.streaming_drive_sectors,
177*8617a60dSAndroid Build Coastguard Worker g.gpt_drive_sectors, 0, g.sector_bytes),
178*8617a60dSAndroid Build Coastguard Worker 0, "Primary header is valid");
179*8617a60dSAndroid Build Coastguard Worker TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.streaming_drive_sectors,
180*8617a60dSAndroid Build Coastguard Worker g.gpt_drive_sectors, 0, g.sector_bytes),
181*8617a60dSAndroid Build Coastguard Worker 1, "Secondary header is invalid");
182*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
183*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 2, 32)\n"
184*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 1023, 1)\n");
185*8617a60dSAndroid Build Coastguard Worker WriteAndFreeGptData(handle, &g);
186*8617a60dSAndroid Build Coastguard Worker
187*8617a60dSAndroid Build Coastguard Worker /*
188*8617a60dSAndroid Build Coastguard Worker * Invalidate primary AND secondary GPT header,
189*8617a60dSAndroid Build Coastguard Worker * check that AllocAndReadGptData fails.
190*8617a60dSAndroid Build Coastguard Worker */
191*8617a60dSAndroid Build Coastguard Worker ResetMocks();
192*8617a60dSAndroid Build Coastguard Worker memset(mock_gpt_primary, '\0', sizeof(*mock_gpt_primary));
193*8617a60dSAndroid Build Coastguard Worker memset(mock_gpt_secondary, '\0', sizeof(*mock_gpt_secondary));
194*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 1,
195*8617a60dSAndroid Build Coastguard Worker "AllocAndRead primary and secondary invalid");
196*8617a60dSAndroid Build Coastguard Worker TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.streaming_drive_sectors,
197*8617a60dSAndroid Build Coastguard Worker g.gpt_drive_sectors, 0, g.sector_bytes),
198*8617a60dSAndroid Build Coastguard Worker 1, "Primary header is invalid");
199*8617a60dSAndroid Build Coastguard Worker TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.streaming_drive_sectors,
200*8617a60dSAndroid Build Coastguard Worker g.gpt_drive_sectors, 0, g.sector_bytes),
201*8617a60dSAndroid Build Coastguard Worker 1, "Secondary header is invalid");
202*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
203*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 1023, 1)\n");
204*8617a60dSAndroid Build Coastguard Worker WriteAndFreeGptData(handle, &g);
205*8617a60dSAndroid Build Coastguard Worker
206*8617a60dSAndroid Build Coastguard Worker /*
207*8617a60dSAndroid Build Coastguard Worker * Invalidate primary GPT header and check that it is
208*8617a60dSAndroid Build Coastguard Worker * repaired by GptRepair().
209*8617a60dSAndroid Build Coastguard Worker *
210*8617a60dSAndroid Build Coastguard Worker * This would normally be called by vb2api_load_kernel()->GptInit()
211*8617a60dSAndroid Build Coastguard Worker * but this callback is mocked in these tests.
212*8617a60dSAndroid Build Coastguard Worker */
213*8617a60dSAndroid Build Coastguard Worker ResetMocks();
214*8617a60dSAndroid Build Coastguard Worker memset(mock_gpt_primary, '\0', sizeof(*mock_gpt_primary));
215*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0,
216*8617a60dSAndroid Build Coastguard Worker "Fix Primary GPT: AllocAndRead");
217*8617a60dSAndroid Build Coastguard Worker /* Call GptRepair() with input indicating secondary GPT is valid */
218*8617a60dSAndroid Build Coastguard Worker g.valid_headers = g.valid_entries = MASK_SECONDARY;
219*8617a60dSAndroid Build Coastguard Worker GptRepair(&g);
220*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0,
221*8617a60dSAndroid Build Coastguard Worker "Fix Primary GPT: WriteAndFreeGptData");
222*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
223*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 1023, 1)\n"
224*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 991, 32)\n"
225*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 1, 1)\n"
226*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 2, 32)\n");
227*8617a60dSAndroid Build Coastguard Worker TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.streaming_drive_sectors,
228*8617a60dSAndroid Build Coastguard Worker g.gpt_drive_sectors, 0, g.sector_bytes),
229*8617a60dSAndroid Build Coastguard Worker 0, "Fix Primary GPT: Primary header is valid");
230*8617a60dSAndroid Build Coastguard Worker
231*8617a60dSAndroid Build Coastguard Worker /*
232*8617a60dSAndroid Build Coastguard Worker * Invalidate secondary GPT header and check that it can be
233*8617a60dSAndroid Build Coastguard Worker * repaired by GptRepair().
234*8617a60dSAndroid Build Coastguard Worker *
235*8617a60dSAndroid Build Coastguard Worker * This would normally be called by vb2api_load_kernel()->GptInit()
236*8617a60dSAndroid Build Coastguard Worker * but this callback is mocked in these tests.
237*8617a60dSAndroid Build Coastguard Worker */
238*8617a60dSAndroid Build Coastguard Worker ResetMocks();
239*8617a60dSAndroid Build Coastguard Worker memset(mock_gpt_secondary, '\0', sizeof(*mock_gpt_secondary));
240*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0,
241*8617a60dSAndroid Build Coastguard Worker "Fix Secondary GPT: AllocAndRead");
242*8617a60dSAndroid Build Coastguard Worker /* Call GptRepair() with input indicating primary GPT is valid */
243*8617a60dSAndroid Build Coastguard Worker g.valid_headers = g.valid_entries = MASK_PRIMARY;
244*8617a60dSAndroid Build Coastguard Worker GptRepair(&g);
245*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0,
246*8617a60dSAndroid Build Coastguard Worker "Fix Secondary GPT: WriteAndFreeGptData");
247*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskRead(h, 1, 1)\n"
248*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 2, 32)\n"
249*8617a60dSAndroid Build Coastguard Worker "VbExDiskRead(h, 1023, 1)\n"
250*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 1023, 1)\n"
251*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 991, 32)\n");
252*8617a60dSAndroid Build Coastguard Worker TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.streaming_drive_sectors,
253*8617a60dSAndroid Build Coastguard Worker g.gpt_drive_sectors, 0, g.sector_bytes),
254*8617a60dSAndroid Build Coastguard Worker 0, "Fix Secondary GPT: Secondary header is valid");
255*8617a60dSAndroid Build Coastguard Worker
256*8617a60dSAndroid Build Coastguard Worker /* Data which is changed is written */
257*8617a60dSAndroid Build Coastguard Worker ResetMocks();
258*8617a60dSAndroid Build Coastguard Worker AllocAndReadGptData(handle, &g);
259*8617a60dSAndroid Build Coastguard Worker g.modified |= GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1;
260*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
261*8617a60dSAndroid Build Coastguard Worker memset(g.primary_header, '\0', g.sector_bytes);
262*8617a60dSAndroid Build Coastguard Worker h = (GptHeader*)g.primary_header;
263*8617a60dSAndroid Build Coastguard Worker h->entries_lba = 2;
264*8617a60dSAndroid Build Coastguard Worker h->number_of_entries = MAX_NUMBER_OF_ENTRIES;
265*8617a60dSAndroid Build Coastguard Worker h->size_of_entry = sizeof(GptEntry);
266*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree mod 1");
267*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskWrite(h, 1, 1)\n"
268*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 2, 32)\n");
269*8617a60dSAndroid Build Coastguard Worker
270*8617a60dSAndroid Build Coastguard Worker /* Data which is changed is written */
271*8617a60dSAndroid Build Coastguard Worker ResetMocks();
272*8617a60dSAndroid Build Coastguard Worker AllocAndReadGptData(handle, &g);
273*8617a60dSAndroid Build Coastguard Worker g.modified = -1;
274*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
275*8617a60dSAndroid Build Coastguard Worker memset(g.primary_header, '\0', g.sector_bytes);
276*8617a60dSAndroid Build Coastguard Worker h = (GptHeader*)g.primary_header;
277*8617a60dSAndroid Build Coastguard Worker h->entries_lba = 2;
278*8617a60dSAndroid Build Coastguard Worker h->number_of_entries = MAX_NUMBER_OF_ENTRIES;
279*8617a60dSAndroid Build Coastguard Worker h->size_of_entry = sizeof(GptEntry);
280*8617a60dSAndroid Build Coastguard Worker h = (GptHeader*)g.secondary_header;
281*8617a60dSAndroid Build Coastguard Worker h->entries_lba = 991;
282*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree mod all");
283*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskWrite(h, 1, 1)\n"
284*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 2, 32)\n"
285*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 1023, 1)\n"
286*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 991, 32)\n");
287*8617a60dSAndroid Build Coastguard Worker
288*8617a60dSAndroid Build Coastguard Worker /* If legacy signature, don't modify GPT header/entries 1 */
289*8617a60dSAndroid Build Coastguard Worker ResetMocks();
290*8617a60dSAndroid Build Coastguard Worker AllocAndReadGptData(handle, &g);
291*8617a60dSAndroid Build Coastguard Worker h = (GptHeader *)g.primary_header;
292*8617a60dSAndroid Build Coastguard Worker memcpy(h->signature, GPT_HEADER_SIGNATURE2, GPT_HEADER_SIGNATURE_SIZE);
293*8617a60dSAndroid Build Coastguard Worker g.modified = -1;
294*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
295*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree mod all");
296*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskWrite(h, 1023, 1)\n"
297*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 991, 32)\n");
298*8617a60dSAndroid Build Coastguard Worker
299*8617a60dSAndroid Build Coastguard Worker /* Error reading */
300*8617a60dSAndroid Build Coastguard Worker ResetMocks();
301*8617a60dSAndroid Build Coastguard Worker disk_read_to_fail = 1;
302*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0, "AllocAndRead disk fail");
303*8617a60dSAndroid Build Coastguard Worker g.valid_headers = g.valid_entries = MASK_SECONDARY;
304*8617a60dSAndroid Build Coastguard Worker GptRepair(&g);
305*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
306*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree mod 1");
307*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskWrite(h, 1, 1)\n"
308*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 2, 32)\n");
309*8617a60dSAndroid Build Coastguard Worker
310*8617a60dSAndroid Build Coastguard Worker ResetMocks();
311*8617a60dSAndroid Build Coastguard Worker disk_read_to_fail = 2;
312*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0, "AllocAndRead disk fail");
313*8617a60dSAndroid Build Coastguard Worker g.valid_headers = MASK_BOTH;
314*8617a60dSAndroid Build Coastguard Worker g.valid_entries = MASK_SECONDARY;
315*8617a60dSAndroid Build Coastguard Worker GptRepair(&g);
316*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
317*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree mod 1");
318*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskWrite(h, 2, 32)\n");
319*8617a60dSAndroid Build Coastguard Worker
320*8617a60dSAndroid Build Coastguard Worker ResetMocks();
321*8617a60dSAndroid Build Coastguard Worker disk_read_to_fail = 991;
322*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0, "AllocAndRead disk fail");
323*8617a60dSAndroid Build Coastguard Worker g.valid_headers = MASK_BOTH;
324*8617a60dSAndroid Build Coastguard Worker g.valid_entries = MASK_PRIMARY;
325*8617a60dSAndroid Build Coastguard Worker GptRepair(&g);
326*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
327*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree mod 2");
328*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskWrite(h, 991, 32)\n");
329*8617a60dSAndroid Build Coastguard Worker
330*8617a60dSAndroid Build Coastguard Worker ResetMocks();
331*8617a60dSAndroid Build Coastguard Worker disk_read_to_fail = 1023;
332*8617a60dSAndroid Build Coastguard Worker TEST_EQ(AllocAndReadGptData(handle, &g), 0, "AllocAndRead disk fail");
333*8617a60dSAndroid Build Coastguard Worker g.valid_headers = g.valid_entries = MASK_PRIMARY;
334*8617a60dSAndroid Build Coastguard Worker GptRepair(&g);
335*8617a60dSAndroid Build Coastguard Worker ResetCallLog();
336*8617a60dSAndroid Build Coastguard Worker TEST_EQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree mod 2");
337*8617a60dSAndroid Build Coastguard Worker TEST_CALLS("VbExDiskWrite(h, 1023, 1)\n"
338*8617a60dSAndroid Build Coastguard Worker "VbExDiskWrite(h, 991, 32)\n");
339*8617a60dSAndroid Build Coastguard Worker
340*8617a60dSAndroid Build Coastguard Worker /* Error writing */
341*8617a60dSAndroid Build Coastguard Worker ResetMocks();
342*8617a60dSAndroid Build Coastguard Worker disk_write_to_fail = 1;
343*8617a60dSAndroid Build Coastguard Worker AllocAndReadGptData(handle, &g);
344*8617a60dSAndroid Build Coastguard Worker g.modified = -1;
345*8617a60dSAndroid Build Coastguard Worker memset(g.primary_header, '\0', g.sector_bytes);
346*8617a60dSAndroid Build Coastguard Worker TEST_NEQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree disk fail");
347*8617a60dSAndroid Build Coastguard Worker
348*8617a60dSAndroid Build Coastguard Worker ResetMocks();
349*8617a60dSAndroid Build Coastguard Worker disk_write_to_fail = 2;
350*8617a60dSAndroid Build Coastguard Worker AllocAndReadGptData(handle, &g);
351*8617a60dSAndroid Build Coastguard Worker g.modified = -1;
352*8617a60dSAndroid Build Coastguard Worker memset(g.primary_header, '\0', g.sector_bytes);
353*8617a60dSAndroid Build Coastguard Worker h = (GptHeader*)g.primary_header;
354*8617a60dSAndroid Build Coastguard Worker h->entries_lba = 2;
355*8617a60dSAndroid Build Coastguard Worker TEST_NEQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree disk fail");
356*8617a60dSAndroid Build Coastguard Worker
357*8617a60dSAndroid Build Coastguard Worker ResetMocks();
358*8617a60dSAndroid Build Coastguard Worker disk_write_to_fail = 991;
359*8617a60dSAndroid Build Coastguard Worker AllocAndReadGptData(handle, &g);
360*8617a60dSAndroid Build Coastguard Worker g.modified = -1;
361*8617a60dSAndroid Build Coastguard Worker memset(g.primary_header, '\0', g.sector_bytes);
362*8617a60dSAndroid Build Coastguard Worker TEST_NEQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree disk fail");
363*8617a60dSAndroid Build Coastguard Worker
364*8617a60dSAndroid Build Coastguard Worker ResetMocks();
365*8617a60dSAndroid Build Coastguard Worker disk_write_to_fail = 1023;
366*8617a60dSAndroid Build Coastguard Worker AllocAndReadGptData(handle, &g);
367*8617a60dSAndroid Build Coastguard Worker g.modified = -1;
368*8617a60dSAndroid Build Coastguard Worker memset(g.primary_header, '\0', g.sector_bytes);
369*8617a60dSAndroid Build Coastguard Worker TEST_NEQ(WriteAndFreeGptData(handle, &g), 0, "WriteAndFree disk fail");
370*8617a60dSAndroid Build Coastguard Worker
371*8617a60dSAndroid Build Coastguard Worker }
372*8617a60dSAndroid Build Coastguard Worker
main(void)373*8617a60dSAndroid Build Coastguard Worker int main(void)
374*8617a60dSAndroid Build Coastguard Worker {
375*8617a60dSAndroid Build Coastguard Worker ReadWriteGptTest();
376*8617a60dSAndroid Build Coastguard Worker
377*8617a60dSAndroid Build Coastguard Worker return gTestSuccess ? 0 : 255;
378*8617a60dSAndroid Build Coastguard Worker }
379