revise ApfsDriverLoader, syncing with sf 5089 commit by Zenith432

This commit is contained in:
Sergey Isakov 2019-09-19 17:25:26 +03:00
parent 4b9b2b4251
commit 862ce3ccc8
3 changed files with 112 additions and 71 deletions

View File

@ -496,7 +496,7 @@
@"echo" [MTOC-ACPI] $(MODULE_NAME)
#@"$(STRIP)" ${src}
@"$(MTOC)" -subsystem $(MODULE_TYPE) $(MTOC_FLAGS) $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.dll $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.efi
@"$(GENFW)" -o ${dst} -c $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.efi $(GENFW_FLAGS)
@"$(GENFW)" -o ${dst} -c $(OUTPUT_DIR)(+)${s_dir}(+)${s_base}.pecoff $(GENFW_FLAGS)
@"echo" [GENFW-ACPI] $(MODULE_NAME)

View File

@ -124,8 +124,9 @@ StartApfsDriver (
DEBUG ((DEBUG_WARN, "New ImageSize after verification: %lu\n", EfiFileSize));
*/
if (!EFI_ERROR (Status)) {
*/
Status = gBS->LoadImage (
FALSE,
gImageHandle,
@ -138,8 +139,9 @@ StartApfsDriver (
DEBUG ((DEBUG_WARN, "Load image failed with Status: %r\n", Status));
return Status;
}
/*
}
/* else {
else {
DEBUG ((DEBUG_WARN, "SECURITY VIOLATION!!! Binary modified!\n"));
return Status;
}
@ -153,6 +155,7 @@ StartApfsDriver (
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Failed to Handle LoadedImage Protool with Status: %r\n", Status));
gBS->UnloadImage (ImageHandle);
return Status;
}
@ -162,7 +165,8 @@ StartApfsDriver (
NewSystemTable = (EFI_SYSTEM_TABLE *) AllocateZeroPool (gST->Hdr.HeaderSize);
if (NewSystemTable == NULL) {
return EFI_OUT_OF_RESOURCES;
gBS->UnloadImage (ImageHandle);
return EFI_OUT_OF_RESOURCES;
}
CopyMem ((VOID *) NewSystemTable, gST, gST->Hdr.HeaderSize);
@ -177,6 +181,8 @@ StartApfsDriver (
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "Failed to calculated new system table CRC32 with Status: %r\n", Status));
FreePool (NewSystemTable);
gBS->UnloadImage (ImageHandle);
return Status;
}
@ -195,6 +201,7 @@ StartApfsDriver (
// Unload ApfsDriver image from memory
//
gBS->UnloadImage (ImageHandle);
FreePool (NewSystemTable);
return Status;
}
@ -256,7 +263,7 @@ LegacyApfsContainerScan (
EFI_STATUS Status;
UINTN Index = 0;
UINT8 *Block = NULL;
UINTN Lba = 0;
EFI_LBA Lba = 0;
UINT32 PartitionNumber = 0;
UINT32 PartitionEntrySize = 0;
EFI_PARTITION_TABLE_HEADER *GptHeader = NULL;
@ -374,7 +381,7 @@ LegacyApfsContainerScan (
// Reallocate Block size to contain all of partition entries.
//
FreePool (Block);
Block = AllocateZeroPool (PartitionNumber * PartitionEntrySize);
Block = AllocateZeroPool ((UINTN)PartitionNumber * PartitionEntrySize);
if (Block == NULL) {
return EFI_OUT_OF_RESOURCES;
}
@ -388,7 +395,7 @@ LegacyApfsContainerScan (
DiskIo2,
MediaId,
MultU64x32 (Lba, BlockSize),
PartitionNumber * PartitionEntrySize,
(UINTN)PartitionNumber * PartitionEntrySize,
Block
);
@ -400,7 +407,7 @@ LegacyApfsContainerScan (
//
// Analyze partition entries.
//
for (Index = 0; Index < PartitionEntrySize * PartitionNumber; Index += PartitionEntrySize) {
for (Index = 0; Index < (UINTN)PartitionEntrySize * PartitionNumber; Index += PartitionEntrySize) {
EFI_PARTITION_ENTRY *CurrentEntry = (EFI_PARTITION_ENTRY *) (Block + Index);
if (CompareGuid (&CurrentEntry->PartitionTypeGUID, &gAppleApfsPartitionTypeGuid)) {
ApfsGptEntry = CurrentEntry;
@ -567,7 +574,7 @@ ApfsDriverLoaderSupported (
//
// Verify GPT entry GUID
//
if (CompareGuid ((EFI_GUID *) ApplePartitionInfo->PartitionType,
if (!CompareGuid ((EFI_GUID *) ApplePartitionInfo->PartitionType,
&gAppleApfsPartitionTypeGuid)) {
return EFI_UNSUPPORTED;
}
@ -584,7 +591,7 @@ ApfsDriverLoaderSupported (
//
// Verify GPT entry GUID
//
if (CompareGuid (&Edk2PartitionInfo->Info.Gpt.PartitionTypeGUID,
if (!CompareGuid (&Edk2PartitionInfo->Info.Gpt.PartitionTypeGUID,
&gAppleApfsPartitionTypeGuid)) {
return EFI_UNSUPPORTED;
}
@ -634,7 +641,7 @@ ApfsDriverLoaderStart (
UINT8 *ApfsBlock = NULL;
EFI_GUID ContainerUuid;
UINT64 EfiBootRecordBlockOffset = 0;
UINT64 EfiBootRecordBlockPtr = 0;
INT64 EfiBootRecordBlockPtr = 0;
APFS_EFI_BOOT_RECORD *EfiBootRecordBlock = NULL;
APFS_CSB *ContainerSuperBlock = NULL;
UINT64 EfiFileCurrentExtentOffset = 0;
@ -745,8 +752,8 @@ ApfsDriverLoaderStart (
//
// Verify ObjectOid and ObjectType
//
DEBUG ((DEBUG_VERBOSE, "ObjectId: %04x\n", ContainerSuperBlock->BlockHeader.ObjectOid ));
DEBUG ((DEBUG_VERBOSE, "ObjectType: %04x\n", ContainerSuperBlock->BlockHeader.ObjectType ));
DEBUG ((DEBUG_VERBOSE, "ObjectId: %016llx\n", ContainerSuperBlock->BlockHeader.ObjectOid ));
DEBUG ((DEBUG_VERBOSE, "ObjectType: %08x\n", ContainerSuperBlock->BlockHeader.ObjectType ));
if (ContainerSuperBlock->BlockHeader.ObjectOid != 1
|| ContainerSuperBlock->BlockHeader.ObjectType != 0x80000001) {
FreePool(ApfsBlock);
@ -756,8 +763,8 @@ ApfsDriverLoaderStart (
//
// Verify ContainerSuperblock magic.
//
DEBUG ((DEBUG_VERBOSE, "CsbMagic: %04x\n", ContainerSuperBlock->Magic));
DEBUG ((DEBUG_VERBOSE, "Should be: %04x\n", APFS_CSB_SIGNATURE));
DEBUG ((DEBUG_VERBOSE, "CsbMagic: %08x\n", ContainerSuperBlock->Magic));
DEBUG ((DEBUG_VERBOSE, "Should be: %08x\n", APFS_CSB_SIGNATURE));
if (ContainerSuperBlock->Magic != APFS_CSB_SIGNATURE) {
FreePool (ApfsBlock);
@ -776,7 +783,7 @@ ApfsDriverLoaderStart (
));
DEBUG ((
DEBUG_VERBOSE,
"ContainerSuperblock checksum: %08llx \n",
"ContainerSuperblock checksum: %016llx \n",
ContainerSuperBlock->BlockHeader.Checksum
));
@ -836,12 +843,12 @@ ApfsDriverLoaderStart (
//
// Calculate Offset of EfiBootRecordBlock
//
EfiBootRecordBlockOffset = MultU64x32 (EfiBootRecordBlockPtr, ApfsBlockSize)
EfiBootRecordBlockOffset = MultU64x32 ((UINT64)EfiBootRecordBlockPtr, ApfsBlockSize)
+ LegacyBaseOffset;
DEBUG ((
DEBUG_VERBOSE,
"EfiBootRecordBlock offset: %08llx \n",
"EfiBootRecordBlock offset: %016llx \n",
EfiBootRecordBlockOffset
));
@ -878,7 +885,7 @@ ApfsDriverLoaderStart (
DEBUG ((
DEBUG_VERBOSE,
"EfiBootRecordBlock checksum: %08llx\n",
"EfiBootRecordBlock checksum: %016llx\n",
EfiBootRecordBlock->BlockHeader.Checksum
));
@ -888,7 +895,7 @@ ApfsDriverLoaderStart (
//
DEBUG ((
DEBUG_VERBOSE,
"EFI embedded driver extents number %llu\n",
"EFI embedded driver extents number %u\n",
EfiBootRecordBlock->NumOfExtents
));
@ -898,21 +905,24 @@ ApfsDriverLoaderStart (
for (Index = 0; Index < EfiBootRecordBlock->NumOfExtents; Index++) {
DEBUG ((
DEBUG_VERBOSE,
"EFI embedded driver extent located at: %llu block\n with size %llu\n",
"EFI embedded driver extent located at: %lld block\n with size %llu\n",
EfiBootRecordBlock->RecordExtents[Index].StartPhysicalAddr,
EfiBootRecordBlock->RecordExtents[Index].BlockCount
));
EfiFileCurrentExtentOffset = MultU64x32 (
EfiBootRecordBlock->RecordExtents[Index].StartPhysicalAddr,
(UINT64)EfiBootRecordBlock->RecordExtents[Index].StartPhysicalAddr,
ApfsBlockSize
) + LegacyBaseOffset;
EfiFileCurrentExtentSize = MultU64x32 (
EfiFileCurrentExtentSize = (UINTN)MultU64x32 (
EfiBootRecordBlock->RecordExtents[Index].BlockCount,
ApfsBlockSize
);
if (EfiFileCurrentExtentSize == 0) {
continue;
}
//
// Adjust buffer size
//
@ -923,6 +933,7 @@ ApfsDriverLoaderStart (
);
if (EfiFileBuffer == NULL) {
FreePool (ApfsBlock);
return EFI_OUT_OF_RESOURCES;
}
@ -940,6 +951,7 @@ ApfsDriverLoaderStart (
if (EFI_ERROR (Status)) {
FreePool(EfiFileBuffer);
FreePool (ApfsBlock);
return EFI_DEVICE_ERROR;
}
//

View File

@ -59,6 +59,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// Volume Superblock definitions
//
#define APFS_VSB_SIGNATURE SIGNATURE_32 ('A', 'P', 'S', 'B')
#define APFS_VSB_MAX_HIST 8
#define APFS_VSB_VOLNAME_LEN 256
#define APFS_VSB_MODIFIED_NAMELEN 32
//
// EfiBootRecord block definitions
@ -201,7 +204,7 @@ typedef struct APFS_CSB_
//
// Pointer to JSDR block (EfiBootRecordBlock)
//
UINT64 EfiBootRecordBlock;
INT64 EfiBootRecordBlock;
EFI_GUID FusionUuid;
PhysicalRange KeyLocker;
UINT64 EphermalInfo[APFS_CSB_EPH_INFO_COUNT];
@ -212,6 +215,29 @@ typedef struct APFS_CSB_
} APFS_CSB;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct APFS_WRAPPED_META_CRYPTO_STATE_
{
UINT16 MajorVersion;
UINT16 MinorVersion;
UINT32 Cpflags;
UINT32 PersistentClass;
UINT32 KeyOsVersion;
UINT16 KeyRevision;
UINT16 Unused;
} /* __attribute__((aligned(2))) */ APFS_WRAPPED_META_CRYPTO_STATE;
#pragma pack(pop)
#pragma pack(push, 1)
typedef struct APFS_MODIFIED_BY_
{
UINT8 Id[APFS_VSB_MODIFIED_NAMELEN];
UINT64 Timestamp;
UINT64 LastXid;
} APFS_MODIFIED_BY;
#pragma pack(pop)
//
// APSB volume header structure
//
@ -228,47 +254,40 @@ typedef struct APFS_APSB_
// Volume#. First volume start with 0, (0x00)
//
UINT32 VolumeNumber;
UINT8 Reserved_1[20];
UINT64 Features;
UINT64 ReadonlyCompatibleFeatures;
//
// Case setting of the volume.
// 1 = Not case sensitive
// 8 = Case sensitive (0x01, Not C.S)
// #define APFS_INCOMPAT_CASE_INSENSITIVE 0x00000001LL
// #define APFS_INCOMPAT_DATALESS_SNAPS 0x00000002LL
// #define APFS_INCOMPAT_ENC_ROLLED 0x00000004LL
// #define APFS_INCOMPAT_NORMALIZATION_INSENSITIVE 0x00000008LL
//
UINT32 CaseSetting;
UINT8 Reserved_2[12];
//
// Size of volume in Blocks. Last volume has no
// size set and has available the rest of the blocks
//
UINT64 VolumeSize;
UINT64 Reserved_3;
//
// Blocks in use in this volumes
//
UINT64 BlocksInUseCount;
UINT8 Reserved_4[32];
//
// Block# to initial block of catalog B-Tree Object
// Map (BTOM)
//
UINT64 BlockNumberToInitialBTOM;
//
// Node Id of root-node
//
UINT64 RootNodeId;
//
// Block# to Extents B-Tree,block#
//
UINT64 BlockNumberToEBTBlockNumber;
//
// Block# to list of Snapshots
//
UINT64 BlockNumberToListOfSnapshots;
UINT8 Reserved_5[16];
UINT64 IncompatibleFeatures;
UINT64 UnmountTime;
UINT64 ReserveBlockCount;
UINT64 QuotaBlockCount;
UINT64 AllocCount;
APFS_WRAPPED_META_CRYPTO_STATE MetaCrypto;
UINT32 RootTreeType;
UINT32 ExtentrefTreeType;
UINT32 SnapMetaTreeType;
UINT64 OmapOid;
UINT64 RootTreeOid;
UINT64 ExtentrefTreeOid;
UINT64 SnapMetaTreeOid;
UINT64 RevertToXid;
UINT64 RevertToSblockOid;
//
// Next CNID
//
UINT64 NextCnid;
UINT64 NextObjId;
//
// Number of files on the volume
//
@ -276,8 +295,13 @@ typedef struct APFS_APSB_
//
// Number of folders on the volume
//
UINT64 NumberOfFolder;
UINT8 Reserved_6[40];
UINT64 NumberOfDirectories;
UINT64 NumberOfSymLinks;
UINT64 NumberOfOtherFsobjects;
UINT64 NumberOfSnapshots;
UINT64 TotalBlocksAlloced;
UINT64 TotalBlocksFreed;
//
// Volume UUID
//
@ -285,19 +309,24 @@ typedef struct APFS_APSB_
//
// Time Volume last written/modified
//
UINT64 ModificationTimestamp;
UINT64 Reserved_7;
UINT64 LastModTime;
UINT64 FsFlags;
//
// Creator/APFS-version
// Ex. (hfs_convert (apfs- 687.0.0.1.7))
//
UINT8 CreatorVersionInfo[32];
//
// Time Volume created
//
UINT64 CreationTimestamp;
//
// ???
APFS_MODIFIED_BY FormattedBy;
APFS_MODIFIED_BY ModifiedBy[APFS_VSB_MAX_HIST];
UINT8 VolName[APFS_VSB_VOLNAME_LEN];
UINT32 NextDocId;
UINT16 Role;
UINT16 Reserved;
UINT64 RootToXid;
UINT64 ErStateOid;
//
} APFS_APSB;
#pragma pack(pop)