OLD | NEW |
1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 */ | 4 */ |
5 | 5 |
6 #ifndef VBOOT_REFERENCE_CGPT_H_ | 6 #ifndef VBOOT_REFERENCE_CGPT_H_ |
7 #define VBOOT_REFERENCE_CGPT_H_ | 7 #define VBOOT_REFERENCE_CGPT_H_ |
8 | 8 |
| 9 #include "gpt.h" |
9 #include <stdint.h> | 10 #include <stdint.h> |
10 | 11 |
11 enum { | 12 enum { |
12 GPT_SUCCESS = 0, | 13 GPT_SUCCESS = 0, |
13 GPT_ERROR_NO_VALID_KERNEL, | 14 GPT_ERROR_NO_VALID_KERNEL, |
14 GPT_ERROR_INVALID_HEADERS, | 15 GPT_ERROR_INVALID_HEADERS, |
15 GPT_ERROR_INVALID_ENTRIES, | 16 GPT_ERROR_INVALID_ENTRIES, |
16 GPT_ERROR_INVALID_SECTOR_SIZE, | 17 GPT_ERROR_INVALID_SECTOR_SIZE, |
17 GPT_ERROR_INVALID_SECTOR_NUMBER, | 18 GPT_ERROR_INVALID_SECTOR_NUMBER, |
18 }; | 19 }; |
19 | 20 |
| 21 /* Bit masks for GptData.modified field. */ |
20 #define GPT_MODIFIED_HEADER1 0x01 | 22 #define GPT_MODIFIED_HEADER1 0x01 |
21 #define GPT_MODIFIED_HEADER2 0x02 | 23 #define GPT_MODIFIED_HEADER2 0x02 |
22 #define GPT_MODIFIED_ENTRIES1 0x04 | 24 #define GPT_MODIFIED_ENTRIES1 0x04 |
23 #define GPT_MODIFIED_ENTRIES2 0x08 | 25 #define GPT_MODIFIED_ENTRIES2 0x08 |
24 | 26 |
25 #define GPT_UPDATE_ENTRY_TRY 1 | 27 #define GPT_UPDATE_ENTRY_TRY 1 |
26 /* System will be trying to boot the currently selected kernel partition. | 28 /* System will be trying to boot the currently selected kernel partition. |
27 * Update its try count if necessary. */ | 29 * Update its try count if necessary. */ |
28 #define GPT_UPDATE_ENTRY_BAD 2 | 30 #define GPT_UPDATE_ENTRY_BAD 2 |
29 /* The currently selected kernel partition failed validation. Mark entry as | 31 /* The currently selected kernel partition failed validation. Mark entry as |
30 * invalid. */ | 32 * invalid. */ |
31 | 33 |
32 struct GptData { | 34 /* Defines ChromeOS-specific limitation on GPT */ |
| 35 #define MIN_SIZE_OF_HEADER 92 |
| 36 #define MAX_SIZE_OF_HEADER 512 |
| 37 #define MIN_SIZE_OF_ENTRY 128 |
| 38 #define MAX_SIZE_OF_ENTRY 512 |
| 39 #define SIZE_OF_ENTRY_MULTIPLE 8 |
| 40 #define MIN_NUMBER_OF_ENTRIES 32 |
| 41 #define MAX_NUMBER_OF_ENTRIES 512 |
| 42 #define TOTAL_ENTRIES_SIZE 16384 /* usual case is 128 bytes * 128 entries */ |
| 43 |
| 44 /* Defines GPT sizes */ |
| 45 #define GPT_PMBR_SECTOR 1 /* size (in sectors) of PMBR */ |
| 46 #define GPT_HEADER_SECTOR 1 |
| 47 #define GPT_ENTRIES_SECTORS 32 /* assume sector size if 512 bytes, then |
| 48 * (TOTAL_ENTRIES_SIZE / 512) = 32 */ |
| 49 |
| 50 /* alias name of index in internal array for primary and secondary header and |
| 51 * entries. */ |
| 52 enum { |
| 53 PRIMARY = 0, |
| 54 SECONDARY = 1, |
| 55 MASK_NONE = 0, |
| 56 MASK_PRIMARY = 1, |
| 57 MASK_SECONDARY = 2, |
| 58 MASK_BOTH = 3, |
| 59 }; |
| 60 |
| 61 typedef struct { |
33 /* Fill in the following fields before calling GptInit() */ | 62 /* Fill in the following fields before calling GptInit() */ |
34 uint8_t *primary_header; /* GPT primary header, from sector 1 of disk | 63 uint8_t *primary_header; /* GPT primary header, from sector 1 of disk |
35 * (size: 512 bytes) */ | 64 * (size: 512 bytes) */ |
36 uint8_t *secondary_header; /* GPT secondary header, from last sector of | 65 uint8_t *secondary_header; /* GPT secondary header, from last sector of |
37 * disk (size: 512 bytes) */ | 66 * disk (size: 512 bytes) */ |
38 uint8_t *primary_entries; /* primary GPT table, follows primary header | 67 uint8_t *primary_entries; /* primary GPT table, follows primary header |
39 * (size: 16 KB) */ | 68 * (size: 16 KB) */ |
40 uint8_t *secondary_entries; /* secondary GPT table, precedes secondary | 69 uint8_t *secondary_entries; /* secondary GPT table, precedes secondary |
41 * header (size: 16 KB) */ | 70 * header (size: 16 KB) */ |
42 uint32_t sector_bytes; /* Size of a LBA sector, in bytes */ | 71 uint32_t sector_bytes; /* Size of a LBA sector, in bytes */ |
43 uint64_t drive_sectors; /* Size of drive in LBA sectors, in sectors */ | 72 uint64_t drive_sectors; /* Size of drive in LBA sectors, in sectors */ |
44 | 73 |
45 /* Outputs */ | 74 /* Outputs */ |
46 uint8_t modified; /* Which inputs have been modified? | 75 uint8_t modified; /* Which inputs have been modified? |
47 * 0x01 = header1 | 76 * 0x01 = header1 |
48 * 0x02 = header2 | 77 * 0x02 = header2 |
49 * 0x04 = table1 | 78 * 0x04 = table1 |
50 * 0x08 = table2 */ | 79 * 0x08 = table2 */ |
51 | 80 |
52 /* Internal state */ | 81 /* Internal state */ |
53 uint8_t current_kernel; /* the current kernel index */ | 82 uint8_t current_kernel; /* the current kernel index */ |
54 }; | 83 } GptData; |
55 typedef struct GptData GptData_t; | |
56 | 84 |
57 int GptInit(GptData_t *gpt); | 85 int GptInit(GptData *gpt); |
58 /* Initializes the GPT data structure's internal state. The header1, header2, | 86 /* Initializes the GPT data structure's internal state. The header1, header2, |
59 * table1, table2, and drive_size fields should be filled in first. | 87 * table1, table2, and drive_size fields should be filled in first. |
60 * | 88 * |
61 * On return the modified field may be set, if the GPT data has been modified | 89 * On return the modified field may be set, if the GPT data has been modified |
62 * and should be written to disk. | 90 * and should be written to disk. |
63 * | 91 * |
64 * Returns 0 if successful, non-zero if error: | 92 * Returns 0 if successful, non-zero if error: |
65 * GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters | 93 * GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters |
66 * recovery mode, | 94 * recovery mode, |
67 * GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters | 95 * GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters |
68 * recovery mode, | 96 * recovery mode, |
69 * GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported, | 97 * GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported, |
70 * GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too | 98 * GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too |
71 * small) */ | 99 * small) */ |
72 | 100 |
73 int GptNextKernelEntry(GptData_t *gpt, uint64_t *start_sector, uint64_t *size); | 101 int GptNextKernelEntry(GptData *gpt, uint64_t *start_sector, uint64_t *size); |
74 /* Provides the location of the next kernel partition, in order of decreasing | 102 /* Provides the location of the next kernel partition, in order of decreasing |
75 * priority. On return the start_sector parameter contains the LBA sector | 103 * priority. On return the start_sector parameter contains the LBA sector |
76 * for the start of the kernel partition, and the size parameter contains the | 104 * for the start of the kernel partition, and the size parameter contains the |
77 * size of the kernel partition in LBA sectors. | 105 * size of the kernel partition in LBA sectors. |
78 * | 106 * |
79 * Returns 0 if successful, else | 107 * Returns 0 if successful, else |
80 * GPT_ERROR_NO_VALID_KERNEL, no avaliable kernel, enters recovery mode */ | 108 * GPT_ERROR_NO_VALID_KERNEL, no avaliable kernel, enters recovery mode */ |
81 | 109 |
82 int GptUpdateKernelEntry(GptData_t *gpt, uint32_t update_type); | 110 int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type); |
83 /* Updates the kernel entry with the specified index, using the specified type | 111 /* Updates the kernel entry with the specified index, using the specified type |
84 * of update (GPT_UPDATE_ENTRY_*). | 112 * of update (GPT_UPDATE_ENTRY_*). |
85 * | 113 * |
86 * On return the modified field may be set, if the GPT data has been modified | 114 * On return the modified field may be set, if the GPT data has been modified |
87 * and should be written to disk. | 115 * and should be written to disk. |
88 * | 116 * |
89 * Returns 0 if successful, 1 if error. */ | 117 * Returns 0 if successful, 1 if error. */ |
90 | 118 |
91 #endif /* VBOOT_REFERENCE_CGPT_H_ */ | 119 #endif /* VBOOT_REFERENCE_CGPT_H_ */ |
OLD | NEW |