OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
DiskImage.c
Go to the documentation of this file.
1
11#include <Library/MemoryAllocationLib.h>
12#include <Library/DebugLib.h>
13
14#include <UserFile.h>
15#include <UserMemory.h>
16
17#define NUM_EXTENTS 20
18
19int
21 int argc,
22 char *argv[]
23 )
24{
25 int Index;
26 BOOLEAN DmgContextValid;
27 UINT8 *Dmg;
28 UINT32 DmgSize;
29 UINT8 *Chunklist;
30 UINT32 ChunklistSize;
31 UINT8 *UncompDmg;
32 UINT32 UncompSize;
33 BOOLEAN Result;
36 UINT32 Index2;
37 OC_APPLE_CHUNKLIST_CONTEXT ChunklistContext;
38
39 //
40 // Limit pool allocation size to 3072 MB
41 //
42 SetPoolAllocationSizeLimit (BASE_1GB | BASE_2GB);
43
44 if (argc < 2) {
45 DEBUG ((DEBUG_ERROR, "Please provide a valid Disk Image path\n"));
46 return -1;
47 }
48
49 if ((argc % 2) != 1) {
50 DEBUG ((DEBUG_ERROR, "Please provide a chunklist file for each DMG, enter \'n\' to skip\n"));
51 }
52
53 for (Index = 1; Index < (argc - 1); Index += 2) {
54 DmgContextValid = FALSE;
55 Dmg = NULL;
56 Chunklist = NULL;
57 UncompDmg = NULL;
58
59 if ((Dmg = UserReadFile (argv[Index], &DmgSize)) == NULL) {
60 DEBUG ((DEBUG_ERROR, "Read fail\n"));
61 goto ContinueDmgLoop;
62 }
63
66 ExtentTable.Reserved = 0;
68 ExtentTable.ExtentCount = MIN (NUM_EXTENTS, ARRAY_SIZE (ExtentTable.Extents));
69
70 for (Index2 = 0; Index2 < ExtentTable.ExtentCount; ++Index2) {
71 ExtentTable.Extents[Index2].Start = (UINTN)Dmg + (Index2 * (DmgSize / ExtentTable.ExtentCount));
72 ExtentTable.Extents[Index2].Length = (DmgSize / ExtentTable.ExtentCount);
73 }
74
75 if (Index2 != 0) {
76 ExtentTable.Extents[Index2 - 1].Length += (DmgSize - (Index2 * (DmgSize / ExtentTable.ExtentCount)));
77 }
78
79 Result = OcAppleDiskImageInitializeContext (&DmgContext, &ExtentTable, DmgSize);
80 if (!Result) {
81 DEBUG ((DEBUG_ERROR, "DMG Context initialization error\n"));
82 goto ContinueDmgLoop;
83 }
84
85 DmgContextValid = TRUE;
86
87 if (AsciiStrCmp (argv[Index + 1], "n") != 0) {
88 if ((Chunklist = UserReadFile (argv[Index + 1], &ChunklistSize)) == NULL) {
89 DEBUG ((DEBUG_ERROR, "Read fail\n"));
90 goto ContinueDmgLoop;
91 }
92
93 Result = OcAppleChunklistInitializeContext (&ChunklistContext, Chunklist, ChunklistSize);
94 if (!Result) {
95 DEBUG ((DEBUG_ERROR, "Chunklist Context initialization error\n"));
96 goto ContinueDmgLoop;
97 }
98
99 Result = OcAppleChunklistVerifySignature (&ChunklistContext, PkDataBase[0].PublicKey);
100 if (!Result) {
101 DEBUG ((DEBUG_ERROR, "Chunklist signature verification error\n"));
102 goto ContinueDmgLoop;
103 }
104
105 Result = OcAppleDiskImageVerifyData (&DmgContext, &ChunklistContext);
106 if (!Result) {
107 DEBUG ((DEBUG_ERROR, "Chunklist chunk verification error\n"));
108 goto ContinueDmgLoop;
109 }
110 }
111
112 UncompSize = (DmgContext.SectorCount * APPLE_DISK_IMAGE_SECTOR_SIZE);
113 UncompDmg = AllocatePool (UncompSize);
114 if (UncompDmg == NULL) {
115 DEBUG ((DEBUG_ERROR, "DMG data allocation failed\n"));
116 goto ContinueDmgLoop;
117 }
118
119 Result = OcAppleDiskImageRead (&DmgContext, 0, UncompSize, UncompDmg);
120 if (!Result) {
121 DEBUG ((DEBUG_ERROR, "DMG read error\n"));
122 goto ContinueDmgLoop;
123 }
124
125 DEBUG ((DEBUG_ERROR, "Decompressed the entire DMG...\n"));
126
127 #if 0
128 UserWriteFile ("out.bin", UncompDmg, UncompSize);
129 #endif
130
131 #if 0
132 UINT8 Digest[CC_MD5_DIGEST_LENGTH];
133 CC_MD5_CTX Context;
134 UINTN Index3;
135
136 CC_MD5_Init (&Context);
137 CC_MD5_Update (&Context, UncompDmg, UncompSize);
138 CC_MD5_Final (Digest, &Context);
139 for (Index3 = 0; Index3 < CC_MD5_DIGEST_LENGTH; ++Index3) {
140 DEBUG ((DEBUG_ERROR, "%02x", (UINT32)Digest[Index3]));
141 }
142
143 DEBUG ((DEBUG_ERROR, "\n"));
144 #endif
145
146 DEBUG ((DEBUG_ERROR, "Success...\n"));
147
148ContinueDmgLoop:
149 if (DmgContextValid) {
150 OcAppleDiskImageFreeContext (&DmgContext);
151 }
152
153 if (Dmg != NULL) {
154 FreePool (Dmg);
155 }
156
157 if (Chunklist != NULL) {
158 FreePool (Chunklist);
159 }
160
161 if (UncompDmg != NULL) {
162 FreePool (UncompDmg);
163 }
164 }
165
166 return 0;
167}
168
169int
171 const uint8_t *Data,
172 size_t Size
173 )
174{
175 #define MAX_INPUT 256
176 #define MAX_OUTPUT 4096
177
178 UINT8 *Test;
179 UINTN Index;
180 UINT32 CurrentLength;
181
182 if (Size > MAX_INPUT) {
183 return 0;
184 }
185
186 Test = AllocateZeroPool (MAX_OUTPUT);
187 if (Test == NULL) {
188 return 0;
189 }
190
191 for (Index = 0; Index < 4096; ++Index) {
192 ASAN_POISON_MEMORY_REGION (Test + Index, MAX_OUTPUT - Index);
193 CurrentLength = DecompressZLIB (
194 Test,
195 Index,
196 Data,
197 Size
198 );
199 ASAN_UNPOISON_MEMORY_REGION (Test + Index, MAX_OUTPUT - Index);
200 ASSERT (CurrentLength <= Index);
201 }
202
203 return 0;
204}
#define ARRAY_SIZE(Array)
Definition AppleMacEfi.h:34
#define APPLE_RAM_DISK_EXTENT_SIGNATURE
APPLE_RAM_DISK_EXTENT_TABLE ExtentTable
#define APPLE_RAM_DISK_EXTENT_VERSION
PACKED struct @54 APPLE_RAM_DISK_EXTENT_TABLE
#define MAX_INPUT
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
Definition DiskImage.c:170
#define MAX_OUTPUT
#define NUM_EXTENTS
Definition DiskImage.c:17
#define APPLE_DISK_IMAGE_SECTOR_SIZE
BOOLEAN OcAppleChunklistVerifySignature(IN OUT OC_APPLE_CHUNKLIST_CONTEXT *Context, IN CONST OC_RSA_PUBLIC_KEY *PublicKey)
BOOLEAN OcAppleChunklistInitializeContext(OUT OC_APPLE_CHUNKLIST_CONTEXT *Context, IN OUT VOID *Buffer, IN UINT32 BufferSize)
DMG_SIZE_DEVICE_PATH Size
BOOLEAN OcAppleDiskImageRead(IN OC_APPLE_DISK_IMAGE_CONTEXT *Context, IN UINTN Lba, IN UINTN BufferSize, OUT VOID *Buffer)
BOOLEAN OcAppleDiskImageVerifyData(IN OUT OC_APPLE_DISK_IMAGE_CONTEXT *Context, IN OUT OC_APPLE_CHUNKLIST_CONTEXT *ChunklistContext)
BOOLEAN OcAppleDiskImageInitializeContext(OUT OC_APPLE_DISK_IMAGE_CONTEXT *Context, IN CONST APPLE_RAM_DISK_EXTENT_TABLE *ExtentTable, IN UINTN FileSize)
VOID OcAppleDiskImageFreeContext(IN OC_APPLE_DISK_IMAGE_CONTEXT *Context)
CONST APPLE_PK_ENTRY PkDataBase[NUM_OF_PK]
UINTN DecompressZLIB(OUT UINT8 *Dst, IN UINTN DstLen, IN CONST UINT8 *Src, IN UINTN SrcLen)
Definition zlib_uefi.c:59
UINT8 * UserReadFile(IN CONST CHAR8 *FileName, OUT UINT32 *Size)
Definition UserFile.c:62
VOID UserWriteFile(IN CONST CHAR8 *FileName, IN CONST VOID *Data, IN UINT32 Size)
Definition UserFile.c:116
#define ASAN_POISON_MEMORY_REGION(addr, size)
#define ASAN_UNPOISON_MEMORY_REGION(addr, size)
VOID SetPoolAllocationSizeLimit(UINTN AllocationSize)
#define ASSERT(x)
Definition coder.h:55
#define MIN(a, b)
Definition deflate.c:1673
UINT8 uint8_t
int ENTRY_POINT(void)