VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/VBoxBs3Obj2Hdr.cpp@ 105374

Last change on this file since 105374 was 104480, checked in by vboxsync, 7 months ago

ValKit/bootsectors/VBoxBs3Obj2Hdr: Fix the 6 other places we may 'leak' file handles on exit. bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.1 KB
Line 
1/* $Id: VBoxBs3Obj2Hdr.cpp 104480 2024-05-02 14:17:45Z vboxsync $ */
2/** @file
3 * VirtualBox Validation Kit - Boot Sector 3 Assembly Object file to C-Header converter.
4 */
5
6/*
7 * Copyright (C) 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 <errno.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45
46#include <iprt/formats/omf.h>
47#include <iprt/types.h>
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53unsigned g_cVerbose = 0;
54
55
56static RTEXITCODE ProcessObjectFile(FILE *pOutput, uint8_t const *pbFile, size_t cbFile, const char *pszFile)
57{
58 size_t off = 0;
59 unsigned cPubDefs = 0;
60 while (off + sizeof(OMFRECHDR) <= cbFile)
61 {
62 PCOMFRECHDR const pHdr = (PCOMFRECHDR)&pbFile[off];
63 if (pHdr->cbLen + sizeof(*pHdr) > cbFile)
64 {
65 fprintf(stderr, "error: %s: bogus record header lenght: %#x LB %#x, max length %#zx\n",
66 pszFile, (unsigned)pHdr->bType, (unsigned)pHdr->cbLen, cbFile - off);
67 return RTEXITCODE_FAILURE;
68 }
69
70 uint8_t const * const pbRec = (uint8_t const *)(pHdr + 1);
71 unsigned cbRec = pHdr->cbLen;
72 unsigned offRec = 0;
73#define OMF_CHECK_RET(a_cbReq, a_Name) /* Not taking the checksum into account, so we're good with 1 or 2 byte fields. */ \
74 if (offRec + (a_cbReq) <= cbRec) {/*likely*/} \
75 else \
76 { \
77 fprintf(stderr, "error: %s: Malformed " #a_Name "! off=%#zx offRec=%#x cbRec=%#x cbNeeded=%#x line=%d\n", \
78 pszFile, off, offRec, cbRec, (a_cbReq), __LINE__); \
79 return RTEXITCODE_FAILURE; \
80 }
81#define OMF_READ_IDX(a_idx, a_Name) \
82 do { \
83 OMF_CHECK_RET(2, a_Name); \
84 a_idx = pbRec[offRec++]; \
85 if ((a_idx) & 0x80) \
86 a_idx = (((a_idx) & 0x7f) << 8) | pbRec[offRec++]; \
87 } while (0)
88
89 switch (pHdr->bType)
90 {
91 case OMF_PUBDEF16:
92 case OMF_PUBDEF32:
93 {
94 const char *pszRec = "PUBDEF";
95 char chType = 'T';
96
97 uint16_t idxGrp;
98 OMF_READ_IDX(idxGrp, PUBDEF);
99
100 uint16_t idxSeg;
101 OMF_READ_IDX(idxSeg, PUBDEF);
102
103 uint16_t uFrameBase = 0;
104 if (idxSeg == 0)
105 {
106 OMF_CHECK_RET(2, PUBDEF);
107 uFrameBase = RT_MAKE_U16(pbRec[offRec], pbRec[offRec + 1]);
108 offRec += 2;
109 }
110 if (g_cVerbose > 2)
111 printf(" %s: idxGrp=%#x idxSeg=%#x uFrameBase=%#x\n", pszRec, idxGrp, idxSeg, uFrameBase);
112 uint16_t const uSeg = idxSeg ? idxSeg : uFrameBase;
113
114 while (offRec + 1 < cbRec)
115 {
116 uint8_t cch = pbRec[offRec++];
117 OMF_CHECK_RET(cch, PUBDEF);
118 const char *pchName = (const char *)&pbRec[offRec];
119 offRec += cch;
120
121 uint32_t offSeg;
122 if (pHdr->bType & OMF_REC32)
123 {
124 OMF_CHECK_RET(4, PUBDEF);
125 offSeg = RT_MAKE_U32_FROM_U8(pbRec[offRec], pbRec[offRec + 1], pbRec[offRec + 2], pbRec[offRec + 3]);
126 offRec += 4;
127 }
128 else
129 {
130 OMF_CHECK_RET(2, PUBDEF);
131 offSeg = RT_MAKE_U16(pbRec[offRec], pbRec[offRec + 1]);
132 offRec += 2;
133 }
134
135 uint16_t idxType;
136 OMF_READ_IDX(idxType, PUBDEF);
137
138 if (g_cVerbose > 2)
139 printf(" %s[%u]: off=%#010x type=%#x %-*.*s\n", pszRec, cPubDefs, offSeg, idxType, cch, cch, pchName);
140 else if (g_cVerbose > 0)
141 printf("%04x:%08x %c %-*.*s\n", uSeg, offSeg, chType, cch, cch, pchName);
142
143 /* Produce the headerfile output. */
144 if (*pchName == '_') /** @todo add more filtering */
145 {
146 cch -= 1;
147 pchName += 1;
148
149 static const char s_szEndProc[] = "_EndProc";
150 if ( cch < sizeof(s_szEndProc)
151 || memcmp(&pchName[cch - sizeof(s_szEndProc) + 1], RT_STR_TUPLE(s_szEndProc)) != 0)
152 fprintf(pOutput, "extern FNBS3FAR %-*.*s;\n", cch, cch, pchName);
153 }
154 }
155 cPubDefs++;
156 break;
157 }
158 }
159
160 off += pHdr->cbLen + sizeof(*pHdr);
161 }
162 return RTEXITCODE_SUCCESS;
163}
164
165
166/**
167 * Opens a file for binary reading or writing.
168 *
169 * @returns File stream handle.
170 * @param pszFile The name of the file.
171 * @param fWrite Whether to open for writing or reading.
172 */
173static FILE *openfile(const char *pszFile, bool fWrite)
174{
175#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
176 FILE *pFile = fopen(pszFile, fWrite ? "wb" : "rb");
177#else
178 FILE *pFile = fopen(pszFile, fWrite ? "w" : "r");
179#endif
180 if (!pFile)
181 fprintf(stderr, "error: Failed to open '%s' for %s: %s (%d)\n",
182 pszFile, fWrite ? "writing" : "reading", strerror(errno), errno);
183 return pFile;
184}
185
186
187/**
188 * Read the given file into memory.
189 *
190 * @returns true on success, false on failure.
191 * @param pszFile The file to read.
192 * @param ppvFile Where to return the memory.
193 * @param pcbFile Where to return the size.
194 */
195static bool readfile(const char *pszFile, void **ppvFile, size_t *pcbFile)
196{
197 FILE *pFile = openfile(pszFile, false);
198 if (pFile)
199 {
200 /*
201 * Figure the size.
202 */
203 if (fseek(pFile, 0, SEEK_END) == 0)
204 {
205 long cbFile = ftell(pFile);
206 if (cbFile > 0)
207 {
208 if (fseek(pFile, SEEK_SET, 0) == 0)
209 {
210 /*
211 * Allocate and read content.
212 */
213 void *pvFile = malloc((size_t)cbFile);
214 if (pvFile)
215 {
216 if (fread(pvFile, cbFile, 1, pFile) == 1)
217 {
218 *ppvFile = pvFile;
219 *pcbFile = (size_t)cbFile;
220 fclose(pFile);
221 return true;
222 }
223 free(pvFile);
224 fprintf(stderr, "error: fread failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
225 }
226 else
227 fprintf(stderr, "error: failed to allocate %ld bytes of memory for '%s'\n", cbFile, pszFile);
228 }
229 else
230 fprintf(stderr, "error: fseek #2 failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
231 }
232 else
233 fprintf(stderr, "error: ftell failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
234 }
235 else
236 fprintf(stderr, "error: fseek #1 failed in '%s': %s (%d)\n", pszFile, strerror(errno), errno);
237 fclose(pFile);
238 }
239 return false;
240}
241
242
243static RTEXITCODE makeParfaitHappy(int argc, char **argv, FILE **ppOutput, const char **ppszOutput)
244{
245 /*
246 * Parse arguments.
247 */
248 bool fDashDash = false;
249 *ppszOutput = NULL;
250 *ppOutput = NULL;
251 for (int i = 1; i < argc; i++)
252 {
253 const char *pszArg = argv[i];
254 if (*pszArg == '-' && !fDashDash)
255 {
256 if (*++pszArg == '-')
257 {
258 pszArg++;
259 if (!*pszArg)
260 {
261 fDashDash = true;
262 continue;
263 }
264 if (strcmp(pszArg, "output") == 0)
265 pszArg = "o";
266 else if (strcmp(pszArg, "help") == 0)
267 pszArg = "h";
268 else if (strcmp(pszArg, "version") == 0)
269 pszArg = "V";
270 else
271 {
272 fprintf(stderr, "syntax error: Unknown parameter: %s\n", argv[i]);
273 return RTEXITCODE_SYNTAX;
274 }
275 }
276 else if (*pszArg)
277 {
278 fprintf(stderr, "syntax error: Unknown parameter: %s\n", argv[i]);
279 return RTEXITCODE_SYNTAX;
280 }
281
282 for (;;)
283 {
284 const char *pszValue = NULL;
285 char const chOpt = *pszArg++;
286 switch (chOpt)
287 {
288 case 'o':
289 if (*pszArg)
290 pszValue = pszArg;
291 else if (i + 1 < argc)
292 pszValue = argv[++i];
293 else
294 {
295 fprintf(stderr, "syntax error: Expected value after option '%c'.\n", chOpt);
296 return RTEXITCODE_SYNTAX;
297 }
298 break;
299 }
300
301
302 switch (chOpt)
303 {
304 case '\0':
305 break;
306
307 case 'o':
308 {
309 FILE * const pOldOutput = *ppOutput;
310 *ppOutput = NULL;
311 if (pOldOutput && pOldOutput != stdout)
312 {
313 if (fclose(pOldOutput))
314 {
315 fprintf(stderr, "error: Write/close error on '%s'\n", *ppszOutput);
316 return RTEXITCODE_FAILURE;
317 }
318 }
319 *ppszOutput = pszValue;
320 continue;
321 }
322
323 case 'q':
324 g_cVerbose = 0;
325 break;
326
327 case 'v':
328 g_cVerbose++;
329 break;
330
331 case 'h':
332 printf("usage: %s --output <hdr> <obj1> [<obj2> []...]\n", argv[0]);
333 return RTEXITCODE_SUCCESS;
334
335 case 'V':
336 printf("0.0.0");
337 return RTEXITCODE_SUCCESS;
338
339 default:
340 fprintf(stderr, "syntax error: Unknown option: %c (%s)\n", chOpt, argv[i]);
341 return RTEXITCODE_SYNTAX;
342 }
343 break;
344 }
345 }
346 else
347 {
348 /* Make sure we've got an output file. */
349 if (!*ppOutput)
350 {
351 if (!*ppszOutput || strcmp(*ppszOutput, "-") == 0)
352 *ppOutput = stdout;
353 else
354 {
355 *ppOutput = fopen(*ppszOutput, "w");
356 if (!*ppOutput)
357 {
358 fprintf(stderr, "error: Failed to open '%s' for writing!\n", *ppszOutput);
359 return RTEXITCODE_FAILURE;
360 }
361 }
362 }
363
364 /* Read in the object file and process it. */
365 size_t cbFile;
366 void *pvFile;
367 if (!readfile(pszArg, &pvFile, &cbFile))
368 return RTEXITCODE_FAILURE;
369
370 RTEXITCODE rcExit = ProcessObjectFile(*ppOutput, (uint8_t const *)pvFile, cbFile, pszArg);
371
372 free(pvFile);
373 if (rcExit != RTEXITCODE_SUCCESS)
374 return rcExit;
375 }
376 }
377
378 return RTEXITCODE_SUCCESS;
379}
380
381
382int main(int argc, char **argv)
383{
384 /*
385 * Use helper function to do the actual main work so we can perform
386 * cleanup to make pedantic static analysers happy.
387 */
388 const char *pszOutput = NULL;
389 FILE *pOutput = NULL;
390 RTEXITCODE rcExit = makeParfaitHappy(argc, argv, &pOutput, &pszOutput);
391
392 /*
393 * Flush+close output before we exit.
394 */
395 if (pOutput)
396 {
397 if ( fflush(pOutput)
398 || fclose(pOutput))
399 {
400 fprintf(stderr, "error: Write error on '%s'\n", pszOutput ? pszOutput : "-");
401 rcExit = RTEXITCODE_FAILURE;
402 }
403 }
404 return rcExit;
405}
406
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