VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/VBoxPkg/VBoxApfsJmpStartDxe/VBoxApfsJmpStartDxe.c@ 85716

Last change on this file since 85716 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.1 KB
Line 
1/* $Id: VBoxApfsJmpStartDxe.c 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * VBoxApfsJmpStartDxe.c - VirtualBox APFS jumpstart driver.
4 */
5
6/*
7 * Copyright (C) 2019-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <Protocol/ComponentName.h>
32#include <Protocol/ComponentName2.h>
33#include <Protocol/DriverBinding.h>
34#include <Protocol/BlockIo.h>
35#include <Protocol/DiskIo.h>
36#include <Library/BaseMemoryLib.h>
37#include <Library/MemoryAllocationLib.h>
38#include <Library/DebugLib.h>
39#include <Library/UefiBootServicesTableLib.h>
40#include <Library/UefiLib.h>
41
42#define IN_RING0
43#include <iprt/cdefs.h>
44#include <iprt/formats/apfs.h>
45
46/**
47 * Contains the full jump start context being worked on.
48 */
49typedef struct
50{
51 /** Block I/O protocol. */
52 EFI_BLOCK_IO *pBlockIo;
53 /** Disk I/O protocol. */
54 EFI_DISK_IO *pDiskIo;
55 /** Block size. */
56 uint32_t cbBlock;
57 /** Controller handle. */
58 EFI_HANDLE hController;
59 /** APFS UUID. */
60 APFSUUID Uuid;
61} APFSJMPSTARTCTX;
62typedef APFSJMPSTARTCTX *PAPFSJMPSTARTCTX;
63typedef const APFSJMPSTARTCTX *PCAPFSJMPSTARTCTX;
64
65static EFI_GUID g_ApfsDrvLoadedFromThisControllerGuid = { 0x01aaf8bc, 0x9c37, 0x4dc1,
66 { 0xb1, 0x68, 0xe9, 0x67, 0xd4, 0x2c, 0x79, 0x25 } };
67
68typedef struct APFS_DRV_LOADED_INFO
69{
70 EFI_HANDLE hController;
71 EFI_GUID GuidContainer;
72} APFS_DRV_LOADED_INFO;
73
74/** Driver name translation table. */
75static CONST EFI_UNICODE_STRING_TABLE g_aVBoxApfsJmpStartDriverLangAndNames[] =
76{
77 { "eng;en", L"VBox APFS Jumpstart Wrapper Driver" },
78 { NULL, NULL }
79};
80
81
82/*********************************************************************************************************************************
83* Internal Functions *
84*********************************************************************************************************************************/
85
86/**
87 * Reads data from the given offset into the buffer.
88 *
89 * @returns EFI status code.
90 * @param pCtx The jump start context.
91 * @param offRead Where to start reading from.
92 * @param pvBuf Where to read into.
93 * @param cbRead Number of bytes to read.
94 */
95static EFI_STATUS vboxApfsJmpStartRead(IN PAPFSJMPSTARTCTX pCtx, IN APFSPADDR offRead, IN void *pvBuf, IN size_t cbRead)
96{
97 return pCtx->pDiskIo->ReadDisk(pCtx->pDiskIo, pCtx->pBlockIo->Media->MediaId, offRead * pCtx->cbBlock, cbRead, pvBuf);
98}
99
100/**
101 * Calculates the fletcher64 checksum of the given APFS block and returns TRUE if it matches the one given in the object header.
102 *
103 * @returns Flag indicating whether the checksum matched.
104 * @param pObjHdr The object header containing the checksum to check against.
105 * @param pvStruct Pointer to the struct to create the checksum of.
106 * @param cbStruct Size of the struct in bytes.
107 */
108static BOOLEAN vboxApfsObjPhysIsChksumValid(PCAPFSOBJPHYS pObjHdr, void *pvStruct, size_t cbStruct)
109{
110 if (cbStruct % sizeof(uint32_t) == 0)
111 {
112 uint32_t *pu32Data = (uint32_t *)pvStruct + 2; /* Start after the checksum field at the beginning. */
113 size_t cWordsLeft = (cbStruct >> 2) - 2;
114
115 uint64_t u64C0 = 0;
116 uint64_t u64C1 = 0;
117 uint64_t u64ChksumFletcher64 = 0;
118 uint64_t u64Check0 = 0;
119 uint64_t u64Check1 = 0;
120
121 while (cWordsLeft)
122 {
123 u64C0 += (uint64_t)*pu32Data++;
124 u64C0 %= UINT32_C(0xffffffff);
125
126 u64C1 += u64C0;
127 u64C1 %= UINT32_C(0xffffffff);
128
129 cWordsLeft--;
130 }
131
132 u64Check0 = UINT32_C(0xffffffff) - (u64C0 + u64C1) % UINT32_C(0xffffffff);
133 u64Check1 = UINT32_C(0xffffffff) - (u64C0 + u64Check0) % UINT32_C(0xffffffff);
134
135 u64ChksumFletcher64 = (uint64_t)u64Check1 << 32 | u64Check0;
136 if (!CompareMem(&u64ChksumFletcher64, &pObjHdr->abChkSum[0], sizeof(pObjHdr->abChkSum)))
137 return TRUE;
138 else
139 DEBUG((DEBUG_INFO, "vboxApfsObjPhysIsChksumValid: Checksum mismatch, expected 0x%llx got 0x%llx", u64ChksumFletcher64, *(uint64_t *)&pObjHdr->abChkSum[0]));
140 }
141 else
142 DEBUG((DEBUG_INFO, "vboxApfsObjPhysIsChksumValid: Structure not a multiple of 32bit\n"));
143
144 return FALSE;
145}
146
147/**
148 * Loads and starts the EFI driver contained in the given jump start structure.
149 *
150 * @returns EFI status code.
151 * @param pCtx APFS jump start driver context structure.
152 * @param pJmpStart APFS jump start structure describing the EFI file to load and start.
153 */
154static EFI_STATUS vboxApfsJmpStartLoadAndExecEfiDriver(IN PAPFSJMPSTARTCTX pCtx, IN PCAPFSEFIJMPSTART pJmpStart)
155{
156 PCAPFSPRANGE paExtents = (PCAPFSPRANGE)(pJmpStart + 1);
157 UINTN cbReadLeft = RT_LE2H_U32(pJmpStart->cbEfiFile);
158 EFI_STATUS rc = EFI_SUCCESS;
159
160 void *pvApfsDrv = AllocateZeroPool(cbReadLeft);
161 if (pvApfsDrv)
162 {
163 uint32_t i = 0;
164 uint8_t *pbBuf = (uint8_t *)pvApfsDrv;
165
166 for (i = 0; i < RT_LE2H_U32(pJmpStart->cExtents) && !EFI_ERROR(rc) && cbReadLeft; i++)
167 {
168 UINTN cbRead = RT_MIN(cbReadLeft, (UINTN)RT_LE2H_U64(paExtents[i].cBlocks) * pCtx->cbBlock);
169
170 rc = vboxApfsJmpStartRead(pCtx, RT_LE2H_U64(paExtents[i].PAddrStart), pbBuf, cbRead);
171 pbBuf += cbRead;
172 cbReadLeft -= cbRead;
173 }
174
175 if (!EFI_ERROR(rc))
176 {
177 /* Retrieve the parent device path. */
178 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
179
180 rc = gBS->HandleProtocol(pCtx->hController, &gEfiDevicePathProtocolGuid, (VOID **)&ParentDevicePath);
181 if (!EFI_ERROR(rc))
182 {
183 /* Load image and execute it. */
184 EFI_HANDLE hImage;
185
186 rc = gBS->LoadImage(FALSE, gImageHandle, ParentDevicePath,
187 pvApfsDrv, RT_LE2H_U32(pJmpStart->cbEfiFile),
188 &hImage);
189 if (!EFI_ERROR(rc))
190 {
191 /* Try to start the image. */
192 rc = gBS->StartImage(hImage, NULL, NULL);
193 if (!EFI_ERROR(rc))
194 {
195 APFS_DRV_LOADED_INFO *pApfsDrvLoadedInfo = (APFS_DRV_LOADED_INFO *)AllocatePool (sizeof(APFS_DRV_LOADED_INFO));
196 if (pApfsDrvLoadedInfo)
197 {
198 pApfsDrvLoadedInfo->hController = pCtx->hController;
199 CopyMem(&pApfsDrvLoadedInfo->GuidContainer, &pCtx->Uuid, sizeof(pApfsDrvLoadedInfo->GuidContainer));
200
201 rc = gBS->InstallMultipleProtocolInterfaces(&pCtx->hController, &g_ApfsDrvLoadedFromThisControllerGuid, pApfsDrvLoadedInfo, NULL);
202 if (!EFI_ERROR(rc))
203 {
204 /* Connect the driver with the controller it came from. */
205 EFI_HANDLE ahImage[2];
206
207 ahImage[0] = hImage;
208 ahImage[1] = NULL;
209
210 gBS->ConnectController(pCtx->hController, &ahImage[0], NULL, TRUE);
211 return EFI_SUCCESS;
212 }
213 else
214 {
215 FreePool(pApfsDrvLoadedInfo);
216 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Failed to install APFS driver loaded info protocol with %r\n", rc));
217 }
218 }
219 else
220 {
221 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Failed to allocate %u bytes for the driver loaded structure\n", sizeof(APFS_DRV_LOADED_INFO)));
222 rc = EFI_OUT_OF_RESOURCES;
223 }
224 }
225 else
226 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Starting APFS driver failed with %r\n", rc));
227
228 gBS->UnloadImage(hImage);
229 }
230 else
231 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Loading read image failed with %r\n", rc));
232 }
233 else
234 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Querying device path protocol failed with %r\n", rc));
235 }
236 else
237 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Reading the jump start extents failed with %r\n", rc));
238
239 FreePool(pvApfsDrv);
240 }
241 else
242 {
243 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Failed to allocate %u bytes for the APFS driver image\n", cbReadLeft));
244 rc = EFI_OUT_OF_RESOURCES;
245 }
246
247 return rc;
248}
249
250/**
251 * @copydoc EFI_DRIVER_BINDING_SUPPORTED
252 */
253static EFI_STATUS EFIAPI
254VBoxApfsJmpStart_Supported(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE ControllerHandle,
255 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL)
256{
257 /* Check whether the controller supports the block I/O protocol. */
258 EFI_STATUS rc = gBS->OpenProtocol(ControllerHandle,
259 &gEfiBlockIoProtocolGuid,
260 NULL,
261 This->DriverBindingHandle,
262 ControllerHandle,
263 EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
264 if (EFI_ERROR(rc))
265 return rc;
266
267 rc = gBS->OpenProtocol(ControllerHandle,
268 &gEfiDiskIoProtocolGuid,
269 NULL,
270 This->DriverBindingHandle,
271 ControllerHandle,
272 EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
273 if (EFI_ERROR(rc))
274 return rc;
275
276 return EFI_SUCCESS;
277}
278
279
280/**
281 * @copydoc EFI_DRIVER_BINDING_START
282 */
283static EFI_STATUS EFIAPI
284VBoxApfsJmpStart_Start(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE ControllerHandle,
285 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL)
286{
287 APFSJMPSTARTCTX Ctx;
288
289 /* Check whether the driver was already loaded from this controller. */
290 EFI_STATUS rc = gBS->OpenProtocol(ControllerHandle,
291 &g_ApfsDrvLoadedFromThisControllerGuid,
292 NULL,
293 This->DriverBindingHandle,
294 ControllerHandle,
295 EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
296 if (!EFI_ERROR(rc))
297 return EFI_UNSUPPORTED;
298
299 Ctx.cbBlock = 0; /* Will get filled when the superblock was read (starting at 0 anyway). */
300 Ctx.hController = ControllerHandle;
301
302 rc = gBS->OpenProtocol(ControllerHandle,
303 &gEfiBlockIoProtocolGuid,
304 (void **)&Ctx.pBlockIo,
305 This->DriverBindingHandle,
306 ControllerHandle,
307 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
308 if (!EFI_ERROR(rc))
309 {
310 rc = gBS->OpenProtocol(ControllerHandle,
311 &gEfiDiskIoProtocolGuid,
312 (void **)&Ctx.pDiskIo,
313 This->DriverBindingHandle,
314 ControllerHandle,
315 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
316 if (!EFI_ERROR(rc))
317 {
318 /* Read the NX superblock structure from the first block and verify it. */
319 APFSNXSUPERBLOCK Sb;
320
321 rc = vboxApfsJmpStartRead(&Ctx, 0, &Sb, sizeof(Sb));
322 if ( !EFI_ERROR(rc)
323 && RT_LE2H_U32(Sb.u32Magic) == APFS_NX_SUPERBLOCK_MAGIC)
324 {
325 uint8_t *pbBlock = (uint8_t *)AllocateZeroPool(RT_LE2H_U32(Sb.cbBlock));
326
327 if (pbBlock)
328 {
329 PCAPFSNXSUPERBLOCK pSb = (PCAPFSNXSUPERBLOCK)pbBlock;
330
331 /* Read in the complete block (checksums always cover the whole block and not just the structure...). */
332 Ctx.cbBlock = RT_LE2H_U32(Sb.cbBlock);
333
334 rc = vboxApfsJmpStartRead(&Ctx, 0, pbBlock, Ctx.cbBlock);
335 if ( !EFI_ERROR(rc)
336 && RT_LE2H_U64(Sb.PAddrEfiJmpStart) > 0
337 && vboxApfsObjPhysIsChksumValid(&pSb->ObjHdr, pbBlock, Ctx.cbBlock))
338 {
339 PCAPFSEFIJMPSTART pJmpStart = (PCAPFSEFIJMPSTART)pbBlock;
340
341 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Found APFS superblock, reading jumpstart structure from %llx\n", RT_LE2H_U64(Sb.PAddrEfiJmpStart)));
342
343 CopyMem(&Ctx.Uuid, &pSb->Uuid, sizeof(Ctx.Uuid));
344
345 rc = vboxApfsJmpStartRead(&Ctx, RT_LE2H_U64(Sb.PAddrEfiJmpStart), pbBlock, Ctx.cbBlock);
346 if ( !EFI_ERROR(rc)
347 && RT_H2LE_U32(pJmpStart->u32Magic) == APFS_EFIJMPSTART_MAGIC
348 && RT_H2LE_U32(pJmpStart->u32Version) == APFS_EFIJMPSTART_VERSION
349 && vboxApfsObjPhysIsChksumValid(&pJmpStart->ObjHdr, pbBlock, Ctx.cbBlock)
350 && RT_H2LE_U32(pJmpStart->cExtents) <= (Ctx.cbBlock - sizeof(*pJmpStart)) / sizeof(APFSPRANGE))
351 rc = vboxApfsJmpStartLoadAndExecEfiDriver(&Ctx, pJmpStart);
352 else
353 {
354 rc = EFI_UNSUPPORTED;
355 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: The APFS EFI jumpstart structure is invalid\n"));
356 }
357 }
358 else
359 {
360 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Invalid APFS superblock -> no APFS filesystem (%r %x %llx)\n", rc, Sb.u32Magic, Sb.PAddrEfiJmpStart));
361 rc = EFI_UNSUPPORTED;
362 }
363
364 FreePool(pbBlock);
365 }
366 else
367 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Failed to allocate memory for APFS block data (%u bytes)\n", RT_LE2H_U32(Sb.cbBlock)));
368 }
369 else
370 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Invalid APFS superblock -> no APFS filesystem (%r %x)\n", rc, Sb.u32Magic));
371
372 gBS->CloseProtocol(ControllerHandle,
373 &gEfiDiskIoProtocolGuid,
374 This->DriverBindingHandle,
375 ControllerHandle);
376 }
377 else
378 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Opening the Disk I/O protocol failed with %r\n", rc));
379
380 gBS->CloseProtocol(ControllerHandle,
381 &gEfiBlockIoProtocolGuid,
382 This->DriverBindingHandle,
383 ControllerHandle);
384 }
385 else
386 DEBUG((DEBUG_INFO, "VBoxApfsJmpStart: Opening the Block I/O protocol failed with %r\n", rc));
387
388 return rc;
389}
390
391
392/**
393 * @copydoc EFI_DRIVER_BINDING_STOP
394 */
395static EFI_STATUS EFIAPI
396VBoxApfsJmpStart_Stop(IN EFI_DRIVER_BINDING_PROTOCOL *This, IN EFI_HANDLE ControllerHandle,
397 IN UINTN NumberOfChildren, IN EFI_HANDLE *ChildHandleBuffer OPTIONAL)
398{
399 /* EFI_STATUS rc; */
400
401 return EFI_UNSUPPORTED;
402}
403
404
405/** @copydoc EFI_COMPONENT_NAME_GET_DRIVER_NAME */
406static EFI_STATUS EFIAPI
407VBoxApfsJmpStartCN_GetDriverName(IN EFI_COMPONENT_NAME_PROTOCOL *This,
408 IN CHAR8 *Language, OUT CHAR16 **DriverName)
409{
410 return LookupUnicodeString2(Language,
411 This->SupportedLanguages,
412 &g_aVBoxApfsJmpStartDriverLangAndNames[0],
413 DriverName,
414 TRUE);
415}
416
417/** @copydoc EFI_COMPONENT_NAME_GET_CONTROLLER_NAME */
418static EFI_STATUS EFIAPI
419VBoxApfsJmpStartCN_GetControllerName(IN EFI_COMPONENT_NAME_PROTOCOL *This,
420 IN EFI_HANDLE ControllerHandle,
421 IN EFI_HANDLE ChildHandle OPTIONAL,
422 IN CHAR8 *Language, OUT CHAR16 **ControllerName)
423{
424 /** @todo try query the protocol from the controller and forward the query. */
425 return EFI_UNSUPPORTED;
426}
427
428/** @copydoc EFI_COMPONENT_NAME2_GET_DRIVER_NAME */
429static EFI_STATUS EFIAPI
430VBoxApfsJmpStartCN2_GetDriverName(IN EFI_COMPONENT_NAME2_PROTOCOL *This,
431 IN CHAR8 *Language, OUT CHAR16 **DriverName)
432{
433 return LookupUnicodeString2(Language,
434 This->SupportedLanguages,
435 &g_aVBoxApfsJmpStartDriverLangAndNames[0],
436 DriverName,
437 FALSE);
438}
439
440/** @copydoc EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME */
441static EFI_STATUS EFIAPI
442VBoxApfsJmpStartCN2_GetControllerName(IN EFI_COMPONENT_NAME2_PROTOCOL *This,
443 IN EFI_HANDLE ControllerHandle,
444 IN EFI_HANDLE ChildHandle OPTIONAL,
445 IN CHAR8 *Language, OUT CHAR16 **ControllerName)
446{
447 /** @todo try query the protocol from the controller and forward the query. */
448 return EFI_UNSUPPORTED;
449}
450
451
452
453/*********************************************************************************************************************************
454* Entry point and driver registration *
455*********************************************************************************************************************************/
456
457/** EFI Driver Binding Protocol. */
458static EFI_DRIVER_BINDING_PROTOCOL g_VBoxApfsJmpStartDB =
459{
460 VBoxApfsJmpStart_Supported,
461 VBoxApfsJmpStart_Start,
462 VBoxApfsJmpStart_Stop,
463 /* .Version = */ 1,
464 /* .ImageHandle = */ NULL,
465 /* .DriverBindingHandle = */ NULL
466};
467
468/** EFI Component Name Protocol. */
469static const EFI_COMPONENT_NAME_PROTOCOL g_VBoxApfsJmpStartCN =
470{
471 VBoxApfsJmpStartCN_GetDriverName,
472 VBoxApfsJmpStartCN_GetControllerName,
473 "eng"
474};
475
476/** EFI Component Name 2 Protocol. */
477static const EFI_COMPONENT_NAME2_PROTOCOL g_VBoxApfsJmpStartCN2 =
478{
479 VBoxApfsJmpStartCN2_GetDriverName,
480 VBoxApfsJmpStartCN2_GetControllerName,
481 "en"
482};
483
484
485/**
486 * VBoxApfsJmpStart entry point.
487 *
488 * @returns EFI status code.
489 *
490 * @param ImageHandle The image handle.
491 * @param SystemTable The system table pointer.
492 */
493EFI_STATUS EFIAPI
494VBoxApfsjmpStartEntryDxe(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
495{
496 EFI_STATUS rc;
497 DEBUG((DEBUG_INFO, "VBoxApfsjmpStartEntryDxe\n"));
498
499 rc = EfiLibInstallDriverBindingComponentName2(ImageHandle, SystemTable,
500 &g_VBoxApfsJmpStartDB, ImageHandle,
501 &g_VBoxApfsJmpStartCN, &g_VBoxApfsJmpStartCN2);
502 ASSERT_EFI_ERROR(rc);
503 return rc;
504}
505
506EFI_STATUS EFIAPI
507VBoxApfsjmpStartUnloadDxe(IN EFI_HANDLE ImageHandle)
508{
509 return EFI_SUCCESS;
510}
511
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette