forked from acidanthera/OpenCorePkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fat.c
152 lines (129 loc) · 3.88 KB
/
Fat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/** @file
Copyright (C) 2020, Goldfish64. All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include <Uefi.h>
#include <IndustryStandard/AppleMachoImage.h>
#include <IndustryStandard/AppleFatBinaryImage.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/OcGuardLib.h>
#include <Library/OcMachoLib.h>
#include "OcMachoLibInternal.h"
EFI_STATUS
FatGetArchitectureOffset (
IN CONST UINT8 *Buffer,
IN UINT32 BufferSize,
IN UINT32 FullSize,
IN MACH_CPU_TYPE CpuType,
OUT UINT32 *FatOffset,
OUT UINT32 *FatSize
)
{
BOOLEAN SwapBytes;
MACH_FAT_HEADER *FatHeader;
UINT32 NumberOfFatArch;
UINT32 Offset;
MACH_CPU_TYPE TmpCpuType;
UINT32 TmpSize;
UINT32 Index;
UINT32 Size;
ASSERT (Buffer != NULL);
ASSERT (BufferSize > 0);
ASSERT (FullSize > 0);
ASSERT (FatOffset != NULL);
ASSERT (FatSize != NULL);
if (BufferSize < sizeof (MACH_FAT_HEADER)
|| !OC_TYPE_ALIGNED (MACH_FAT_HEADER, Buffer)) {
return EFI_INVALID_PARAMETER;
}
FatHeader = (MACH_FAT_HEADER*) Buffer;
if (FatHeader->Signature != MACH_FAT_BINARY_INVERT_SIGNATURE
&& FatHeader->Signature != MACH_FAT_BINARY_SIGNATURE
&& FatHeader->Signature != EFI_FAT_BINARY_SIGNATURE) {
//
// Non-fat binary.
//
*FatOffset = 0;
*FatSize = FullSize;
return EFI_SUCCESS;
}
SwapBytes = FatHeader->Signature == MACH_FAT_BINARY_INVERT_SIGNATURE;
NumberOfFatArch = FatHeader->NumberOfFatArch;
if (SwapBytes) {
NumberOfFatArch = SwapBytes32 (NumberOfFatArch);
}
if (OcOverflowMulAddU32 (NumberOfFatArch, sizeof (MACH_FAT_ARCH), sizeof (MACH_FAT_HEADER), &TmpSize)
|| TmpSize > BufferSize) {
return EFI_INVALID_PARAMETER;
}
//
// TODO: extend the interface to support subtypes (i.e. MachCpuSubtypeX8664H) some day.
//
for (Index = 0; Index < NumberOfFatArch; ++Index) {
TmpCpuType = FatHeader->FatArch[Index].CpuType;
if (SwapBytes) {
TmpCpuType = SwapBytes32 (TmpCpuType);
}
if (TmpCpuType == CpuType) {
Offset = FatHeader->FatArch[Index].Offset;
Size = FatHeader->FatArch[Index].Size;
if (SwapBytes) {
Offset = SwapBytes32 (Offset);
Size = SwapBytes32 (Size);
}
if (Offset == 0
|| OcOverflowAddU32 (Offset, Size, &TmpSize)
|| TmpSize > FullSize) {
return EFI_INVALID_PARAMETER;
}
*FatOffset = Offset;
*FatSize = Size;
return EFI_SUCCESS;
}
}
return EFI_NOT_FOUND;
}
EFI_STATUS
FatFilterArchitectureByType (
IN OUT UINT8 **FileData,
IN OUT UINT32 *FileSize,
IN MACH_CPU_TYPE CpuType
)
{
EFI_STATUS Status;
UINT32 FatOffset;
UINT32 FatSize;
ASSERT (FileData != NULL);
ASSERT (FileSize != NULL);
ASSERT (*FileSize > 0);
Status = FatGetArchitectureOffset (*FileData, *FileSize, *FileSize, CpuType, &FatOffset, &FatSize);
if (EFI_ERROR (Status)) {
return Status;
}
*FileData += FatOffset;
*FileSize = FatSize;
return EFI_SUCCESS;
}
EFI_STATUS
FatFilterArchitecture32 (
IN OUT UINT8 **FileData,
IN OUT UINT32 *FileSize
)
{
return FatFilterArchitectureByType (FileData, FileSize, MachCpuTypeX86);
}
EFI_STATUS
FatFilterArchitecture64 (
IN OUT UINT8 **FileData,
IN OUT UINT32 *FileSize
)
{
return FatFilterArchitectureByType (FileData, FileSize, MachCpuTypeX8664);
}