VirtualBox

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

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