]>
code.delx.au - refind/blob - refind/gpt.c
cc6c70b77c7fbb153413ca066211d78ecc7b7e5b
3 * Functions related to GPT data structures
5 * Copyright (c) 2014-2015 Roderick W. Smith
8 * This program is distributed under the terms of the GNU General Public
9 * License (GPL) version 3 (GPLv3), a copy of which must be distributed
10 * with this source code or binaries made from it.
18 #include "../include/refit_call_wrapper.h"
20 #ifdef __MAKEWITH_TIANO
21 #define BlockIoProtocol gEfiBlockIoProtocolGuid
24 extern GPT_DATA
*gPartitions
;
26 // Allocate data for the main GPT_DATA structure, as well as the ProtectiveMBR
27 // and Header structures it contains. This function does *NOT*, however,
28 // allocate memory for the Entries data structure, since its size is variable
29 // and is determined by the contents of Header.
30 GPT_DATA
* AllocateGptData(VOID
) {
33 GptData
= AllocateZeroPool(sizeof(GPT_DATA
));
34 if (GptData
!= NULL
) {
35 GptData
->ProtectiveMBR
= AllocateZeroPool(sizeof(MBR_RECORD
));
36 GptData
->Header
= AllocateZeroPool(sizeof(GPT_HEADER
));
37 if ((GptData
->ProtectiveMBR
== NULL
) || (GptData
->Header
== NULL
)) {
38 MyFreePool(GptData
->ProtectiveMBR
);
39 MyFreePool(GptData
->Header
);
45 } // GPT_DATA * AllocateGptData()
47 // Unallocate a single GPT_DATA structure. This does NOT follow the
48 // linked list, though.
49 VOID
ClearGptData(GPT_DATA
*Data
) {
51 if (Data
->ProtectiveMBR
)
52 MyFreePool(Data
->ProtectiveMBR
);
54 MyFreePool(Data
->Header
);
56 MyFreePool(Data
->Entries
);
59 } // VOID ClearGptData()
61 // TODO: Make this work on big-endian systems; at the moment, it contains
62 // little-endian assumptions!
63 // Returns TRUE if the GPT protective MBR and header data appear valid,
65 static BOOLEAN
GptHeaderValid(GPT_DATA
*GptData
) {
67 UINT32 CrcValue
, StoredCrcValue
;
68 UINTN HeaderSize
= sizeof(GPT_HEADER
);
70 if ((GptData
== NULL
) || (GptData
->ProtectiveMBR
== NULL
) || (GptData
->Header
== NULL
))
73 IsValid
= (GptData
->ProtectiveMBR
->MBRSignature
== 0xAA55);
74 IsValid
= IsValid
&& ((GptData
->ProtectiveMBR
->partitions
[0].type
== 0xEE) ||
75 (GptData
->ProtectiveMBR
->partitions
[1].type
== 0xEE) ||
76 (GptData
->ProtectiveMBR
->partitions
[2].type
== 0xEE) ||
77 (GptData
->ProtectiveMBR
->partitions
[3].type
== 0xEE));
79 IsValid
= IsValid
&& ((GptData
->Header
->signature
== 0x5452415020494645ULL
) &&
80 (GptData
->Header
->spec_revision
== 0x00010000) &&
81 (GptData
->Header
->entry_size
== 128));
83 // Looks good so far; check CRC value....
85 if (GptData
->Header
->header_size
< HeaderSize
)
86 HeaderSize
= GptData
->Header
->header_size
;
87 StoredCrcValue
= GptData
->Header
->header_crc32
;
88 GptData
->Header
->header_crc32
= 0;
89 CrcValue
= crc32(0x0, GptData
->Header
, HeaderSize
);
90 if (CrcValue
!= StoredCrcValue
)
92 GptData
->Header
->header_crc32
= StoredCrcValue
;
96 } // BOOLEAN GptHeaderValid()
98 // Read GPT data from Volume and store it in *Data. Note that this function
99 // may be called on a Volume that is not in fact a GPT disk (an MBR disk,
100 // a partition, etc.), in which case it will return EFI_LOAD_ERROR or some
101 // other error condition. In this case, *Data will be left alone.
102 // Note also that this function checks CRCs and does other sanity checks
103 // on the input data, but does NOT resort to using the backup data if the
104 // primary data structures are damaged. The intent is that the function
105 // be very conservative about reading GPT data. Currently (version 0.7.10),
106 // rEFInd uses the data only to provide access to partition names. This is
107 // non-critical data, so it's OK to return nothing, but having the program
108 // hang on reading garbage or return nonsense could be very bad.
109 EFI_STATUS
ReadGptData(REFIT_VOLUME
*Volume
, GPT_DATA
**Data
) {
110 EFI_STATUS Status
= EFI_SUCCESS
;
113 GPT_DATA
*GptData
; // Temporary holding storage; transferred to *Data later
115 if ((Volume
== NULL
) || (Data
== NULL
))
116 return EFI_INVALID_PARAMETER
;
119 if ((Status
== EFI_SUCCESS
) && (Volume
->BlockIO
== NULL
)) {
120 Status
= refit_call3_wrapper(BS
->HandleProtocol
, Volume
->DeviceHandle
, &BlockIoProtocol
, (VOID
**) &(Volume
->BlockIO
));
121 if (EFI_ERROR(Status
)) {
122 Volume
->BlockIO
= NULL
;
123 Print(L
"Warning: Can't get BlockIO protocol in ReadGptData().\n");
124 Status
= EFI_NOT_READY
;
128 if ((Status
== EFI_SUCCESS
) && ((!Volume
->BlockIO
->Media
->MediaPresent
) || (Volume
->BlockIO
->Media
->LogicalPartition
)))
129 Status
= EFI_NO_MEDIA
;
131 if (Status
== EFI_SUCCESS
) {
132 GptData
= AllocateGptData(); // Note: All but GptData->Entries
133 if (GptData
== NULL
) {
134 Status
= EFI_OUT_OF_RESOURCES
;
138 // Read the MBR and store it in GptData->ProtectiveMBR.
139 if (Status
== EFI_SUCCESS
) {
140 Status
= refit_call5_wrapper(Volume
->BlockIO
->ReadBlocks
, Volume
->BlockIO
, Volume
->BlockIO
->Media
->MediaId
,
141 0, sizeof(MBR_RECORD
), (VOID
*) GptData
->ProtectiveMBR
);
144 // Read the GPT header and store it in GptData->Header.
145 if (Status
== EFI_SUCCESS
) {
146 Status
= refit_call5_wrapper(Volume
->BlockIO
->ReadBlocks
, Volume
->BlockIO
, Volume
->BlockIO
->Media
->MediaId
,
147 1, sizeof(GPT_HEADER
), GptData
->Header
);
150 // If it looks like a valid protective MBR & GPT header, try to do more with it....
151 if (Status
== EFI_SUCCESS
) {
152 if (GptHeaderValid(GptData
)) {
153 // Load actual GPT table....
154 BufferSize
= GptData
->Header
->entry_count
* 128;
155 GptData
->Entries
= AllocatePool(BufferSize
);
156 if (GptData
->Entries
== NULL
)
157 Status
= EFI_OUT_OF_RESOURCES
;
159 if (Status
== EFI_SUCCESS
)
160 Status
= refit_call5_wrapper(Volume
->BlockIO
->ReadBlocks
, Volume
->BlockIO
, Volume
->BlockIO
->Media
->MediaId
,
161 GptData
->Header
->entry_lba
, BufferSize
, GptData
->Entries
);
163 // Check CRC status of table
164 if ((Status
== EFI_SUCCESS
) && (crc32(0x0, GptData
->Entries
, BufferSize
) != GptData
->Header
->entry_crc32
))
165 Status
= EFI_CRC_ERROR
;
167 // Now, ensure that every name is null-terminated....
168 if (Status
== EFI_SUCCESS
) {
169 for (i
= 0; i
< GptData
->Header
->entry_count
; i
++)
170 GptData
->Entries
[i
].name
[35] = '\0';
173 Status
= EFI_UNSUPPORTED
;
174 } // if/else valid header
175 } // if header read OK
177 if (Status
== EFI_SUCCESS
) {
178 // Everything looks OK, so copy it over
182 ClearGptData(GptData
);
186 } // EFI_STATUS ReadGptData()
188 // Look in gPartitions for a partition with the specified Guid. If found, return
189 // a pointer to that partition's data. If not found, return a NULL pointer.
190 // The calling function is responsible for freeing the returned memory.
191 GPT_ENTRY
* FindPartWithGuid(EFI_GUID
*Guid
) {
193 GPT_ENTRY
*Found
= NULL
;
196 if ((Guid
== NULL
) || (gPartitions
== NULL
))
199 GptData
= gPartitions
;
200 while ((GptData
!= NULL
) && (!Found
)) {
202 while ((i
< GptData
->Header
->entry_count
) && (!Found
)) {
203 if (GuidsAreEqual((EFI_GUID
*) &(GptData
->Entries
[i
].partition_guid
), Guid
)) {
204 Found
= AllocateZeroPool(sizeof(GPT_ENTRY
));
205 CopyMem(Found
, &GptData
->Entries
[i
], sizeof(GPT_ENTRY
));
209 } // while(scanning entries)
210 GptData
= GptData
->NextEntry
;
211 } // while(scanning GPTs)
213 } // GPT_ENTRY * FindPartWithGuid()
215 // Erase the gPartitions linked-list data structure
216 VOID
ForgetPartitionTables(VOID
) {
219 while (gPartitions
!= NULL
) {
220 Next
= gPartitions
->NextEntry
;
221 ClearGptData(gPartitions
);
224 } // VOID ForgetPartitionTables()
226 // If Volume points to a whole disk with a GPT, add it to the gPartitions
227 // linked list of GPTs.
228 VOID
AddPartitionTable(REFIT_VOLUME
*Volume
) {
229 GPT_DATA
*GptData
= NULL
, *GptList
;
233 Status
= ReadGptData(Volume
, &GptData
);
234 if (Status
== EFI_SUCCESS
) {
235 if (gPartitions
== NULL
) {
236 gPartitions
= GptData
;
238 GptList
= gPartitions
;
239 while (GptList
->NextEntry
!= NULL
) {
240 GptList
= GptList
->NextEntry
;
243 GptList
->NextEntry
= GptData
;
246 } else if (GptData
!= NULL
) {
247 ClearGptData(GptData
);
250 } // VOID AddPartitionTable()