OpenCore  1.0.8
OpenCore Bootloader
Loading...
Searching...
No Matches
TestAtapiPath.c
Go to the documentation of this file.
1
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <Uefi.h>
10#include <UserMemory.h>
11#include <Protocol/BlockIo.h>
12#include <Library/BaseMemoryLib.h>
13#include <Library/DevicePathLib.h>
14#include <Library/MemoryAllocationLib.h>
15#include <Library/UefiBootServicesTableLib.h>
18
19//
20// Diagnostic output does not affect device-path resolution.
21//
22VOID
24 UINTN ErrorLevel,
25 CONST CHAR8 *Message,
26 EFI_DEVICE_PATH_PROTOCOL *DevicePath
27 )
28{
29}
30
31STATIC CONST UINT8 mImage[] = {
32 2, 1, 12, 0, 0xd0, 0x41, 3, 0x0a, 0, 0, 0, 0,
33 1, 1, 6, 0, 0, 0x1c, 1, 1, 6, 0, 0, 0,
34 3, 1, 8, 0, 0, 1, 0, 0,
35 4, 4, 16, 0, '1', 0, '0', 0, '0', 0, ':', 0,'A',0, 0, 0,
36 0x7f, 0xff, 4, 0
37};
38STATIC UINT8 mPaths[3][86];
39STATIC EFI_BLOCK_IO_MEDIA mMedia[3];
40STATIC EFI_BLOCK_IO_PROTOCOL mBlockIo[3];
41STATIC UINTN mHandleCount;
42STATIC BOOLEAN mAlreadyValid;
43STATIC BOOLEAN mRejectFixed;
44STATIC BOOLEAN mNoHandles;
45STATIC BOOLEAN mRequireRootFix;
46STATIC UINT32 mTests;
47
48STATIC
49VOID
51 BOOLEAN Condition,
52 CONST CHAR8 *Description
53 )
54{
55 if (!Condition) {
56 fprintf (stderr, "FAIL: %s\n", Description);
57 exit (1);
58 }
59}
60
61STATIC
62EFI_STATUS
63EFIAPI
65 EFI_HANDLE Handle,
66 EFI_GUID *Protocol,
67 VOID **Interface
68 )
69{
70 UINTN Index;
71
72 Index = (UINTN)Handle - 1;
73
74 if (Index >= mHandleCount) {
75 return EFI_NOT_FOUND;
76 }
77
78 if (CompareGuid (Protocol, &gEfiBlockIoProtocolGuid)) {
79 *Interface = &mBlockIo[Index];
80 } else if (CompareGuid (Protocol, &gEfiDevicePathProtocolGuid)) {
81 *Interface = mPaths[Index];
82 } else {
83 return EFI_UNSUPPORTED;
84 }
85
86 return EFI_SUCCESS;
87}
88
89STATIC
90EFI_STATUS
91EFIAPI
93 EFI_LOCATE_SEARCH_TYPE SearchType,
94 EFI_GUID *Protocol,
95 VOID *SearchKey,
96 UINTN *Count,
97 EFI_HANDLE **HandleBuffer
98 )
99{
100 UINTN Index;
101
102 if (mNoHandles || !mHandleCount) {
103 return EFI_NOT_FOUND;
104 }
105
106 *Count = mHandleCount;
107 *HandleBuffer = AllocatePool (mHandleCount * sizeof (**HandleBuffer));
108 for (Index = 0; Index < mHandleCount; ++Index) {
109 (*HandleBuffer)[Index] = (EFI_HANDLE)(UINTN)(Index + 1);
110 }
111
112 return EFI_SUCCESS;
113}
114
115STATIC
116EFI_STATUS
117EFIAPI
119 EFI_GUID *Protocol,
120 EFI_DEVICE_PATH_PROTOCOL **DevicePath,
121 EFI_HANDLE *Handle
122 )
123{
124 UINTN Index;
125 UINTN PrefixSize;
126 UINT8 *Bytes;
127
128 Bytes = (UINT8 *)*DevicePath;
129
130 if (mRequireRootFix && (memcmp (Bytes, mPaths[0], 12) != 0)) {
131 *Handle = NULL;
132 return EFI_NOT_FOUND;
133 }
134
135 if (mAlreadyValid) {
136 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)(Bytes + 24 + Bytes[26]);
137 *Handle = (EFI_HANDLE)1;
138 return EFI_SUCCESS;
139 }
140
141 for (Index = 0; Index < mHandleCount; ++Index) {
142 PrefixSize = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)mPaths[Index]) - END_DEVICE_PATH_LENGTH;
143 if ((GetDevicePathSize (*DevicePath) >= PrefixSize + END_DEVICE_PATH_LENGTH) && (memcmp (Bytes, mPaths[Index], PrefixSize) == 0)) {
144 if (mRejectFixed) {
145 return EFI_NOT_FOUND;
146 }
147
148 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)(Bytes + PrefixSize);
149 // Partition Block I/O may consume HD too; DevicePath can resolve APFS.
150 if ((DevicePathType (*DevicePath) == MEDIA_DEVICE_PATH) && (DevicePathSubType (*DevicePath) == MEDIA_HARDDRIVE_DP)) {
151 *DevicePath = NextDevicePathNode (*DevicePath);
152 }
153
154 if (CompareGuid (Protocol, &gEfiDevicePathProtocolGuid)) {
155 while (!IsDevicePathEnd (*DevicePath) && DevicePathSubType (*DevicePath) != MEDIA_FILEPATH_DP) {
156 *DevicePath = NextDevicePathNode (*DevicePath);
157 }
158 }
159
160 *Handle = (EFI_HANDLE)(UINTN)(Index + 1);
161 return EFI_SUCCESS;
162 }
163 }
164
165 if (CompareGuid (Protocol, &gEfiDevicePathProtocolGuid)) {
166 // Firmware can locate the controller, but not its malformed storage node.
167 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)(Bytes + 24);
168 *Handle = (EFI_HANDLE)1;
169 return EFI_SUCCESS;
170 }
171
172 return EFI_NOT_FOUND;
173}
174
175STATIC
176VOID
178 VOID
179 )
180{
181 UINTN Index;
182
183 mHandleCount = 1;
185 memset (mPaths, 0, sizeof (mPaths));
186 memset (mMedia, 0, sizeof (mMedia));
187 memset (mBlockIo, 0, sizeof (mBlockIo));
188 for (Index = 0; Index < 3; ++Index) {
189 memcpy (mPaths[Index], mImage, 24);
190 mPaths[Index][24] = 3;
191 mPaths[Index][25] = MSG_SASEX_DP;
192 mPaths[Index][26] = 16;
193 mPaths[Index][28] = (UINT8)(Index + 1);
194 memcpy (mPaths[Index] + 40, mImage + sizeof (mImage) - 4, 4);
195 mMedia[Index].MediaPresent = TRUE;
196 mMedia[Index].BlockSize = 4096;
197 mMedia[Index].LastBlock = 1000000;
198 mBlockIo[Index].Media = &mMedia[Index];
199 }
200
201 gBS->HandleProtocol = MockHandleProtocol;
202 gBS->LocateHandleBuffer = MockLocateHandleBuffer;
203 gBS->LocateDevicePath = MockLocateDevicePath;
204}
205
206STATIC
207VOID
209 CONST CHAR8 *Name,
210 CONST UINT8 *Input,
211 UINTN Size,
212 BOOLEAN Expected
213 )
214{
215 UINTN Allocations;
216 UINTN SuffixOffset;
217 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
218 EFI_DEVICE_PATH_PROTOCOL *OriginalPath;
219 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
220 INTN Result;
221
222 Allocations = mPoolAllocations;
223 SuffixOffset = 24 + DevicePathNodeLength ((EFI_DEVICE_PATH_PROTOCOL *)(Input + 24));
224 DevicePath = AllocateCopyPool (Size, Input);
225 OriginalPath = DevicePath;
226 Result = OcFixAppleBootDevicePath (&DevicePath, &RemainingDevicePath);
227
228 Check ((Result > 0) == Expected, Name);
229 Check ((UINTN)RemainingDevicePath >= (UINTN)DevicePath && (UINTN)RemainingDevicePath < (UINTN)DevicePath + GetDevicePathSize (DevicePath), "valid remainder allocation");
230 if (Expected) {
231 Check (GetDevicePathSize (DevicePath) == Size + 40 - SuffixOffset, "correct replacement size");
232 Check (memcmp (DevicePath, mPaths[0], 40) == 0, "exact firmware disk path");
233 Check (memcmp ((UINT8 *)DevicePath + 40, Input + SuffixOffset, Size - SuffixOffset) == 0, "unchanged suffix");
234 } else {
235 Check (DevicePath == OriginalPath && memcmp (DevicePath, Input, Size) == 0, "failure preserves allocation and bytes");
236 }
237
238 FreePool (DevicePath);
239 Check (mPoolAllocations == Allocations, "resolver releases temporary allocations");
240 ++mTests;
241 printf ("PASS: %s\n", Name);
242}
243
244STATIC
245VOID
247 VOID
248 )
249{
250 UINT8 Input[24 + 36 + 16 + 4];
251 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
252 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
253 UINTN Allocations;
254
255 Reset ();
256 memcpy (Input, mImage, 24);
257 memset (Input + 24, 0x42, 36);
258 Input[24] = MEDIA_DEVICE_PATH;
259 Input[25] = MEDIA_VENDOR_DP;
260 Input[26] = 36;
261 Input[27] = 0;
262 memcpy (Input + 60, mImage + 32, sizeof (mImage) - 32);
263
264 memset (mPaths[0] + 24, 0, 42);
265 mPaths[0][24] = MEDIA_DEVICE_PATH;
266 mPaths[0][25] = MEDIA_HARDDRIVE_DP;
267 mPaths[0][26] = 42;
268 memcpy (mPaths[0] + 66, mImage + sizeof (mImage) - 4, 4);
269 mMedia[0].LogicalPartition = TRUE;
270
271 Allocations = mPoolAllocations;
272 DevicePath = AllocateCopyPool (sizeof (Input), Input);
273 Check (OcFixAppleBootDevicePath (&DevicePath, &RemainingDevicePath) == 1, "existing VirtIO expansion");
274 Check (GetDevicePathSize (DevicePath) == sizeof (Input) + 42, "VirtIO inserts HD node");
275 Check (memcmp (DevicePath, mPaths[0], 66) == 0 && memcmp ((UINT8 *)DevicePath + 66, Input + 24, sizeof (Input) - 24) == 0, "VirtIO retains suffix");
276 FreePool (DevicePath);
277 Check (mPoolAllocations == Allocations, "VirtIO expansion releases original allocation");
278 ++mTests;
279 printf ("PASS: existing VirtIO expansion releases original allocation\n");
280}
281
282int
284 int argc,
285 char **argv
286 )
287{
288 UINT8 OtherNode[sizeof (mImage)];
289 UINT8 Extended[sizeof (mImage) + 4];
290 UINT8 MultiInstance[sizeof (mImage) * 2];
291 UINT8 Sata[sizeof (mImage) + 2];
292 UINT8 Disk[36];
293 UINT8 Partition[32 + 42 + 4];
294 UINT8 Apfs[sizeof (mImage) + 42 + 36];
295 EFI_DEVICE_PATH_PROTOCOL *DevicePath, *Node;
297
299 Reset ();
300 mPaths[0][6] = 8;
301 mRequireRootFix = 1;
302 Run ("ACPI correction followed by ATAPI expansion", mImage, sizeof (mImage), 1);
303 Reset ();
304 memcpy (Extended, mImage, 32);
305 memset (Extended + 32, 0, 4);
306 Extended[26] = 12;
307 memcpy (Extended + 36, mImage + 32, sizeof (mImage) - 32);
308 Run ("extended ATAPI node", Extended, sizeof (Extended), 1);
309 Reset ();
310 Run ("Apple NVMe node", mImage, sizeof (mImage), 1);
311 Reset ();
312 mPaths[0][25] = MSG_NVME_NAMESPACE_DP;
313 Run ("UEFI NVMe node", mImage, sizeof (mImage), 1);
314 Reset ();
315 Run ("working NVMe path unchanged", mPaths[0], GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)mPaths[0]), 0);
316 Reset ();
317 memcpy (Sata, mImage, 24);
318 memset (Sata + 24, 0, 10);
319 Sata[24] = MESSAGING_DEVICE_PATH;
320 Sata[25] = MSG_SATA_DP;
321 Sata[26] = 10;
322 memcpy (Sata + 34, mImage + 32, sizeof (mImage) - 32);
323 mAlreadyValid = 1;
324 Run ("working SATA path unchanged", Sata, sizeof (Sata), 0);
325 Reset ();
326 mHandleCount = 2;
327 Run ("two namespaces rejected", mImage, sizeof (mImage), 0);
328 Reset ();
329 mHandleCount = 2;
330 mPaths[1][17] = 0x1d;
331 Run ("other PCI controller ignored", mImage, sizeof (mImage), 1);
332 Reset ();
333 mPaths[0][17] = 0x1d;
334 Run ("wrong PCI controller rejected", mImage, sizeof (mImage), 0);
335 Reset ();
336 mMedia[0].LogicalPartition = TRUE;
337 memset (mPaths[0] + 40, 0, 42);
338 mPaths[0][40] = MEDIA_DEVICE_PATH;
339 mPaths[0][41] = MEDIA_HARDDRIVE_DP;
340 mPaths[0][42] = 42;
341 memcpy (mPaths[0] + 82, mImage + sizeof (mImage) - 4, 4);
342 Run ("partition handle excluded by path", mImage, sizeof (mImage), 0);
343 Reset ();
344 mMedia[0].RemovableMedia = TRUE;
345 Run ("removable NVMe disk", mImage, sizeof (mImage), 1);
346 Reset ();
347 mMedia[0].MediaPresent = FALSE;
348 Run ("unset media-present flag", mImage, sizeof (mImage), 1);
349 Reset ();
350 mBlockIo[0].Media = NULL;
351 Run ("device path does not require media metadata", mImage, sizeof (mImage), 1);
352 Reset ();
353 mNoHandles = 1;
354 Run ("enumeration failure unchanged", mImage, sizeof (mImage), 0);
355 Reset ();
356 mAlreadyValid = 1;
357 Run ("working original path unchanged", mImage, sizeof (mImage), 0);
358 Reset ();
359 mRejectFixed = 1;
360 Run ("replacement must resolve", mImage, sizeof (mImage), 0);
361 Reset ();
362 mPaths[0][25] = MSG_USB_DP;
363 Run ("non-NVMe disk rejected", mImage, sizeof (mImage), 0);
364 Reset ();
365 memcpy (OtherNode, mImage, sizeof (mImage));
366 OtherNode[25] = MSG_USB_DP;
367 Run ("non-ATAPI input unchanged", OtherNode, sizeof (OtherNode), 0);
368 Reset ();
369 memcpy (Disk, mImage, 32);
370 memcpy (Disk + 32, mImage + sizeof (mImage) - 4, 4);
371 Run ("whole disk without file suffix", Disk, sizeof (Disk), 1);
372 Reset ();
373 memset (Partition, 0, sizeof (Partition));
374 memcpy (Partition, mImage, 32);
375 Partition[32] = MEDIA_DEVICE_PATH;
376 Partition[33] = MEDIA_HARDDRIVE_DP;
377 Partition[34] = 42;
378 memcpy (Partition + 74, Disk + 32, 4);
379 Run ("partition path", Partition, sizeof (Partition), 1);
380 Reset ();
381 memcpy (Apfs, Partition, 74);
382 memset (Apfs + 74, 0x42, 36);
383 Apfs[74] = MEDIA_DEVICE_PATH;
384 Apfs[75] = MEDIA_VENDOR_DP;
385 Apfs[76] = 36;
386 Apfs[77] = 0;
387 memcpy (Apfs + 110, mImage + 32, sizeof (mImage) - 32);
388 Run ("partition APFS file path", Apfs, sizeof (Apfs), 1);
389 Reset ();
390 mAlreadyValid = 1;
391 Run ("working ATA partition path", Partition, sizeof (Partition), 0);
392 Reset ();
393 mAlreadyValid = 1;
394 Run ("working ATA disk path", Disk, sizeof (Disk), 0);
395 Reset ();
396 memcpy (MultiInstance, mImage, sizeof (mImage));
397 memcpy (MultiInstance+sizeof (mImage), mImage, sizeof (mImage));
398 MultiInstance[49] = 1;
399 Run ("first instance ATAPI expansion", MultiInstance, sizeof (MultiInstance), 1);
400 Reset ();
401 mAlreadyValid = 1;
402 DevicePath = AllocateCopyPool (sizeof (mImage), mImage);
403 Node = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePath + 24);
404 Check (OcFixAppleBootDevicePathNode (&DevicePath, &Node, &Context, NULL) == 0, "node API preserves working ATA");
405 Check (Context.OldPath == NULL && memcmp (DevicePath, mImage, sizeof (mImage)) == 0, "working node has no restore allocation");
406 FreePool (DevicePath);
407 ++mTests;
408 Reset ();
409 DevicePath = AllocateCopyPool (sizeof (mImage), mImage);
410 Node = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePath + 24);
411 Check (OcFixAppleBootDevicePathNode (&DevicePath, &Node, &Context, NULL) == 1, "node API repairs ATAPI");
412 Check (Context.OldPath != NULL && memcmp (Context.OldPath, mImage, sizeof (mImage)) == 0, "restore context keeps old allocation");
413 OcFixAppleBootDevicePathNodeRestore (&DevicePath, &Node, &Context);
414 Check (memcmp (DevicePath, mImage, sizeof (mImage)) == 0 && (UINT8 *)Node == (UINT8 *)DevicePath + 24, "node API rollback restores bytes and cursor");
415 FreePool (DevicePath);
416 ++mTests;
417 Reset ();
418 DevicePath = AllocateCopyPool (sizeof (mImage), mImage);
419 Node = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePath + 24);
420 Check (OcFixAppleBootDevicePathNode (&DevicePath, &Node, &Context, NULL) == 1, "node API commits repair");
421 OcFixAppleBootDevicePathNodeRestoreFree (DevicePath, &Context);
422 FreePool (DevicePath);
423 ++mTests;
424 Reset ();
425 DevicePath = AllocateCopyPool (sizeof (mImage), mImage);
426 Node = (EFI_DEVICE_PATH_PROTOCOL *)((UINT8 *)DevicePath + 24);
427 Check (OcFixAppleBootDevicePathNode (&DevicePath, &Node, NULL, NULL) == 1, "node API without restore context");
428 Check (memcmp (DevicePath, mPaths[0], 40) == 0 && memcmp ((UINT8 *)DevicePath + 40, mImage + 32, sizeof (mImage) - 32) == 0, "no-context replacement retains suffix");
429 FreePool (DevicePath);
430 ++mTests;
431 if (argc == 2) {
432 UINT8 Captured[4096];
433 FILE *File = fopen (argv[1], "rb");
434 Check (File != NULL, "open captured fixture");
435 size_t Size = fread (Captured, 1, sizeof (Captured), File);
436 Check (feof (File) && Size >= sizeof (mImage), "read captured fixture");
437 fclose (File);
438 Reset ();
439 Run ("actual captured boot-image", Captured, Size, 1);
440 }
441
442 Check (mPoolAllocations == 0, "all pool allocations released");
443 printf ("%u regression checks passed\n", mTests);
444 return 0;
445}
DMG_SIZE_DEVICE_PATH Size
EFI_BOOT_SERVICES * gBS
INTN OcFixAppleBootDevicePath(IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, OUT EFI_DEVICE_PATH_PROTOCOL **RemainingDevicePath)
VOID OcFixAppleBootDevicePathNodeRestoreFree(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePathNode, IN OUT APPLE_BOOT_DP_PATCH_CONTEXT *RestoreContext)
VOID OcFixAppleBootDevicePathNodeRestore(IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePathNode, IN CONST APPLE_BOOT_DP_PATCH_CONTEXT *RestoreContext)
INTN OcFixAppleBootDevicePathNode(IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath, IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePathNode, OUT APPLE_BOOT_DP_PATCH_CONTEXT *RestoreContext OPTIONAL, IN EFI_HANDLE ValidDevice OPTIONAL)
APPLE_EVENT_HANDLE Handle
Definition OcTypingLib.h:45
STATIC VOID TestVirtioCleanup(VOID)
STATIC CONST UINT8 mImage[]
STATIC BOOLEAN mRejectFixed
STATIC BOOLEAN mNoHandles
STATIC EFI_BLOCK_IO_MEDIA mMedia[3]
STATIC BOOLEAN mRequireRootFix
STATIC EFI_STATUS EFIAPI MockLocateHandleBuffer(EFI_LOCATE_SEARCH_TYPE SearchType, EFI_GUID *Protocol, VOID *SearchKey, UINTN *Count, EFI_HANDLE **HandleBuffer)
VOID DebugPrintDevicePath(UINTN ErrorLevel, CONST CHAR8 *Message, EFI_DEVICE_PATH_PROTOCOL *DevicePath)
STATIC EFI_STATUS EFIAPI MockLocateDevicePath(EFI_GUID *Protocol, EFI_DEVICE_PATH_PROTOCOL **DevicePath, EFI_HANDLE *Handle)
STATIC VOID Check(BOOLEAN Condition, CONST CHAR8 *Description)
STATIC BOOLEAN mAlreadyValid
STATIC UINT8 mPaths[3][86]
STATIC UINTN mHandleCount
STATIC VOID Reset(VOID)
STATIC UINT32 mTests
STATIC VOID Run(CONST CHAR8 *Name, CONST UINT8 *Input, UINTN Size, BOOLEAN Expected)
STATIC EFI_BLOCK_IO_PROTOCOL mBlockIo[3]
STATIC EFI_STATUS EFIAPI MockHandleProtocol(EFI_HANDLE Handle, EFI_GUID *Protocol, VOID **Interface)
BOOLEAN EFIAPI CompareGuid(IN CONST GUID *Guid1, IN CONST GUID *Guid2)
EFI_GUID gEfiBlockIoProtocolGuid
EFI_GUID gEfiDevicePathProtocolGuid
UINTN mPoolAllocations
int main()
Definition acdtinfo.c:181
#define memset(ptr, c, len)
#define memcmp(b1, b2, len)
#define memcpy(Dst, Src, Size)
Definition lzvn.h:57
EFI_DEVICE_PATH_PROTOCOL * OldPath