VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/VBoxPkg/VBoxApfsJmpStartDxe/VBoxApfsJmpStartDxe.c

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

© 2023 Oracle
ContactPrivacy policyTerms of Use