VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTEfiFatExtract.cpp@ 82477

Last change on this file since 82477 was 81775, checked in by vboxsync, 5 years ago

Runtime/RTEfiFatExtract: Tool to extract single entries from EFI fat binaries (used by Apple)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.3 KB
Line 
1/* $Id: RTEfiFatExtract.cpp 81775 2019-11-11 18:14:36Z vboxsync $ */
2/** @file
3 * IPRT - Utility for extracting single files from a fat EFI binary.
4 */
5
6/*
7 * Copyright (C) 2019 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 <iprt/formats/efi-fat.h>
32
33#include <iprt/assert.h>
34#include <iprt/errcore.h>
35#include <iprt/getopt.h>
36#include <iprt/initterm.h>
37#include <iprt/file.h>
38#include <iprt/mem.h>
39#include <iprt/message.h>
40#include <iprt/path.h>
41#include <iprt/stream.h>
42#include <iprt/string.h>
43
44
45
46static int efiFatExtractList(const char *pszInput)
47{
48 RTFILE hFile = NIL_RTFILE;
49 int rc = RTFileOpen(&hFile, pszInput, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
50 if (RT_SUCCESS(rc))
51 {
52 EFI_FATHDR Hdr;
53 rc = RTFileReadAt(hFile, 0, &Hdr, sizeof(Hdr), NULL);
54 if (RT_SUCCESS(rc))
55 {
56 if ( RT_LE2H_U32(Hdr.u32Magic) == EFI_FATHDR_MAGIC
57 && RT_LE2H_U32(Hdr.cFilesEmbedded) <= 16 /*Arbitrary number*/)
58 {
59 RTFOFF offRead = sizeof(Hdr);
60
61 for (uint32_t i = 0; i < RT_LE2H_U32(Hdr.cFilesEmbedded); i++)
62 {
63 EFI_FATDIRENTRY Entry;
64 rc = RTFileReadAt(hFile, offRead, &Entry, sizeof(Entry), NULL);
65 if (RT_SUCCESS(rc))
66 {
67 RTPrintf("Entry %u:\n", i);
68 RTPrintf(" CPU Type: %#x\n", RT_LE2H_U32(Entry.u32CpuType));
69 RTPrintf(" CPU Subtype: %#x\n", RT_LE2H_U32(Entry.u32CpuSubType));
70 RTPrintf(" Offset: %#x\n", RT_LE2H_U32(Entry.u32OffsetStart));
71 RTPrintf(" Size: %#x\n", RT_LE2H_U32(Entry.cbFile));
72 RTPrintf(" Alignment: %#x\n", RT_LE2H_U32(Entry.u32Alignment));
73 }
74 else
75 {
76 RTPrintf("Failed to read file entry %u of '%s': %Rrc\n", pszInput, i, rc);
77 break;
78 }
79
80 offRead += sizeof(Entry);
81 }
82 }
83 else
84 {
85 rc = VERR_INVALID_MAGIC;
86 RTPrintf("The header contains invalid values\n");
87 }
88 }
89 else
90 RTPrintf("Failed to read header of '%s': %Rrc\n", pszInput, rc);
91
92 RTFileClose(hFile);
93 }
94 else
95 RTPrintf("Failed to open file '%s': %Rrc\n", pszInput, rc);
96
97 return rc;
98}
99
100
101static int efiFatExtractSave(const char *pszInput, uint32_t idxEntry, const char *pszOut)
102{
103 RTFILE hFile = NIL_RTFILE;
104 int rc = RTFileOpen(&hFile, pszInput, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
105 if (RT_SUCCESS(rc))
106 {
107 EFI_FATHDR Hdr;
108 rc = RTFileReadAt(hFile, 0, &Hdr, sizeof(Hdr), NULL);
109 if (RT_SUCCESS(rc))
110 {
111 if ( RT_LE2H_U32(Hdr.u32Magic) == EFI_FATHDR_MAGIC
112 && RT_LE2H_U32(Hdr.cFilesEmbedded) <= 16 /*Arbitrary number*/)
113 {
114 if (idxEntry < RT_LE2H_U32(Hdr.cFilesEmbedded))
115 {
116 EFI_FATDIRENTRY Entry;
117
118 rc = RTFileReadAt(hFile, sizeof(Hdr) + idxEntry * sizeof(Entry), &Entry, sizeof(Entry), NULL);
119 if (RT_SUCCESS(rc))
120 {
121 void *pvFile = RTMemAllocZ(RT_LE2H_U32(Entry.cbFile));
122 if (RT_LIKELY(pvFile))
123 {
124 rc = RTFileReadAt(hFile, RT_LE2H_U32(Entry.u32OffsetStart), pvFile, RT_LE2H_U32(Entry.cbFile), NULL);
125 if (RT_SUCCESS(rc))
126 {
127 RTFILE hFileOut;
128 rc = RTFileOpen(&hFileOut, pszOut, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE);
129 if (RT_SUCCESS(rc))
130 {
131 rc = RTFileWrite(hFileOut, pvFile, RT_LE2H_U32(Entry.cbFile), NULL);
132 if (RT_FAILURE(rc))
133 RTPrintf("Failed to write output file '%s': %Rrc\n", pszOut, rc);
134 RTFileClose(hFileOut);
135 }
136 else
137 RTPrintf("Failed to create output file '%s': %Rrc\n", pszOut, rc);
138 }
139 else
140 RTPrintf("Failed to read embedded file %u: %Rrc\n", idxEntry, rc);
141
142 RTMemFree(pvFile);
143 }
144 else
145 RTPrintf("Failed to allocate %u bytes of memory\n", RT_LE2H_U32(Entry.cbFile));
146 }
147 else
148 RTPrintf("Failed to read file entry %u of '%s': %Rrc\n", pszInput, idxEntry, rc);
149 }
150 else
151 {
152 rc = VERR_INVALID_PARAMETER;
153 RTPrintf("Given index out of range, maximum is %u\n", RT_LE2H_U32(Hdr.cFilesEmbedded));
154 }
155 }
156 else
157 {
158 rc = VERR_INVALID_MAGIC;
159 RTPrintf("The header contains invalid values\n");
160 }
161 }
162 else
163 RTPrintf("Failed to read header of '%s': %Rrc\n", pszInput, rc);
164
165 RTFileClose(hFile);
166 }
167 else
168 RTPrintf("Failed to open file '%s': %Rrc\n", pszInput, rc);
169
170 return rc;
171}
172
173
174int main(int argc, char **argv)
175{
176 int rc = RTR3InitExe(argc, &argv, 0);
177 if (RT_FAILURE(rc))
178 return RTMsgInitFailure(rc);
179
180 /*
181 * Parse arguments.
182 */
183 static const RTGETOPTDEF s_aOptions[] =
184 {
185 { "--input", 'i', RTGETOPT_REQ_STRING },
186 { "--output", 'o', RTGETOPT_REQ_STRING },
187 { "--entry", 'e', RTGETOPT_REQ_UINT32 },
188 { "--help", 'h', RTGETOPT_REQ_NOTHING },
189 { "--version", 'V', RTGETOPT_REQ_NOTHING },
190 };
191
192 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
193 const char *pszInput = NULL;
194 const char *pszOut = NULL;
195 uint32_t idxEntry = UINT32_C(0xffffffff);
196
197 RTGETOPTUNION ValueUnion;
198 RTGETOPTSTATE GetState;
199 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
200 while ((rc = RTGetOpt(&GetState, &ValueUnion)))
201 {
202 switch (rc)
203 {
204 case 'h':
205 RTPrintf("Usage: %s [options]\n"
206 "\n"
207 "Options:\n"
208 " -i,--input=<file>\n"
209 " Input file\n"
210 " -e,--entry=<idx>\n"
211 " Selects the entry for saving\n"
212 " -o,--output=file\n"
213 " Save the specified entry to this file\n"
214 " -h, -?, --help\n"
215 " Display this help text and exit successfully.\n"
216 " -V, --version\n"
217 " Display the revision and exit successfully.\n"
218 , RTPathFilename(argv[0]));
219 return RTEXITCODE_SUCCESS;
220 case 'V':
221 RTPrintf("$Revision: 81775 $\n");
222 return RTEXITCODE_SUCCESS;
223
224 case 'i':
225 pszInput = ValueUnion.psz;
226 break;
227 case 'o':
228 pszOut = ValueUnion.psz;
229 break;
230 case 'e':
231 idxEntry = ValueUnion.u32;
232 break;
233 default:
234 return RTGetOptPrintError(rc, &ValueUnion);
235 }
236 }
237
238 if (!pszInput)
239 {
240 RTPrintf("An input path must be given\n");
241 return RTEXITCODE_FAILURE;
242 }
243
244 if (!pszOut || idxEntry == UINT32_C(0xffffffff))
245 rc = efiFatExtractList(pszInput);
246 else
247 rc = efiFatExtractSave(pszInput, idxEntry, pszOut);
248 if (RT_FAILURE(rc))
249 rcExit = RTEXITCODE_FAILURE;
250
251 return rcExit;
252}
253
Note: See TracBrowser for help on using the repository browser.

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