1 | /* $Id: VBoxBs2Linker.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Validation Kit - Boot Sector 2 "linker".
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 <stdio.h>
|
---|
32 | #include <string.h>
|
---|
33 | #include <stdlib.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | int main(int argc, char **argv)
|
---|
38 | {
|
---|
39 | const char *pszOutput = NULL;
|
---|
40 | const char **papszInputs = (const char **)calloc(argc, sizeof(const char *));
|
---|
41 | unsigned cInputs = 0;
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * Scan the arguments.
|
---|
45 | */
|
---|
46 | for (int i = 1; i < argc; i++)
|
---|
47 | {
|
---|
48 | if (argv[i][0] == '-')
|
---|
49 | {
|
---|
50 | const char *pszOpt = &argv[i][1];
|
---|
51 | if (*pszOpt == '-')
|
---|
52 | {
|
---|
53 | /* Convert long options to short ones. */
|
---|
54 | pszOpt--;
|
---|
55 | if (!strcmp(pszOpt, "--output"))
|
---|
56 | pszOpt = "o";
|
---|
57 | else if (!strcmp(pszOpt, "--version"))
|
---|
58 | pszOpt = "V";
|
---|
59 | else if (!strcmp(pszOpt, "--help"))
|
---|
60 | pszOpt = "h";
|
---|
61 | else
|
---|
62 | {
|
---|
63 | fprintf(stderr, "syntax errro: Unknown options '%s'\n", pszOpt);
|
---|
64 | free(papszInputs);
|
---|
65 | return 2;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /* Process the list of short options. */
|
---|
70 | while (*pszOpt)
|
---|
71 | {
|
---|
72 | switch (*pszOpt++)
|
---|
73 | {
|
---|
74 | case 'o':
|
---|
75 | {
|
---|
76 | const char *pszValue = pszOpt;
|
---|
77 | pszOpt = strchr(pszOpt, '\0');
|
---|
78 | if (*pszValue == '=')
|
---|
79 | pszValue++;
|
---|
80 | else if (!*pszValue)
|
---|
81 | {
|
---|
82 | if (i + 1 >= argc)
|
---|
83 | {
|
---|
84 | fprintf(stderr, "syntax error: The --output option expects a filename.\n");
|
---|
85 | free(papszInputs);
|
---|
86 | return 12;
|
---|
87 | }
|
---|
88 | pszValue = argv[++i];
|
---|
89 | }
|
---|
90 | if (pszOutput)
|
---|
91 | {
|
---|
92 | fprintf(stderr, "Only one output file is allowed. You've specified '%s' and '%s'\n",
|
---|
93 | pszOutput, pszValue);
|
---|
94 | free(papszInputs);
|
---|
95 | return 2;
|
---|
96 | }
|
---|
97 | pszOutput = pszValue;
|
---|
98 | pszOpt = "";
|
---|
99 | break;
|
---|
100 | }
|
---|
101 |
|
---|
102 | case 'V':
|
---|
103 | printf("%s\n", "$Revision: 69111 $");
|
---|
104 | free(papszInputs);
|
---|
105 | return 0;
|
---|
106 |
|
---|
107 | case '?':
|
---|
108 | case 'h':
|
---|
109 | printf("usage: %s [options] -o <output> <input1> [input2 ... [inputN]]\n", argv[0]);
|
---|
110 | free(papszInputs);
|
---|
111 | return 0;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else
|
---|
116 | papszInputs[cInputs++] = argv[i];
|
---|
117 | }
|
---|
118 |
|
---|
119 | if (!pszOutput)
|
---|
120 | {
|
---|
121 | fprintf(stderr, "syntax error: No output file was specified (-o or --output).\n");
|
---|
122 | free(papszInputs);
|
---|
123 | return 2;
|
---|
124 | }
|
---|
125 | if (cInputs == 0)
|
---|
126 | {
|
---|
127 | fprintf(stderr, "syntax error: No input files was specified.\n");
|
---|
128 | free(papszInputs);
|
---|
129 | return 2;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Do the job.
|
---|
135 | */
|
---|
136 | /* Open the output file. */
|
---|
137 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
138 | FILE *pOutput = fopen(pszOutput, "wb");
|
---|
139 | #else
|
---|
140 | FILE *pOutput = fopen(pszOutput, "w");
|
---|
141 | #endif
|
---|
142 | if (!pOutput)
|
---|
143 | {
|
---|
144 | fprintf(stderr, "error: Failed to open output file '%s' for writing\n", pszOutput);
|
---|
145 | free(papszInputs);
|
---|
146 | return 1;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* Copy the input files to the output file, with sector padding applied. */
|
---|
150 | int rcExit = 0;
|
---|
151 | size_t off = 0;
|
---|
152 | for (unsigned i = 0; i < cInputs && rcExit == 0; i++)
|
---|
153 | {
|
---|
154 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
155 | FILE *pInput = fopen(papszInputs[i], "rb");
|
---|
156 | #else
|
---|
157 | FILE *pInput = fopen(papszInputs[i], "r");
|
---|
158 | #endif
|
---|
159 | if (pInput)
|
---|
160 | {
|
---|
161 | for (;;)
|
---|
162 | {
|
---|
163 | /* Read a block from the input file. */
|
---|
164 | uint8_t abBuf[4096];
|
---|
165 | size_t cbRead = fread(abBuf, sizeof(uint8_t), 4096, pInput);
|
---|
166 | if (!cbRead || ferror(pInput))
|
---|
167 | break;
|
---|
168 |
|
---|
169 | /* Padd the end of the file if necessary. */
|
---|
170 | if (cbRead != 4096 && !feof(pInput))
|
---|
171 | {
|
---|
172 | fprintf(stderr, "error: fread returned %u bytes, but we're not at the end of the file yet...\n",
|
---|
173 | (unsigned)cbRead);
|
---|
174 | rcExit = 1;
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | if ((cbRead & 0x1ff) != 0)
|
---|
178 | {
|
---|
179 | memset(&abBuf[cbRead], 0, 4096 - cbRead);
|
---|
180 | cbRead = (cbRead + 0x1ff) & ~0x1ffU;
|
---|
181 | }
|
---|
182 |
|
---|
183 | /* Write the block to the output file. */
|
---|
184 | if (fwrite(abBuf, sizeof(uint8_t), cbRead, pOutput) == cbRead)
|
---|
185 | off += cbRead;
|
---|
186 | else
|
---|
187 | {
|
---|
188 | fprintf(stderr, "error: fwrite failed\n");
|
---|
189 | rcExit = 1;
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (ferror(pInput))
|
---|
195 | {
|
---|
196 | fprintf(stderr, "error: Error reading '%s'.\n", papszInputs[i]);
|
---|
197 | rcExit = 1;
|
---|
198 | }
|
---|
199 | fclose(pInput);
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | fprintf(stderr, "error: Failed to open '%s' for reading.\n", papszInputs[i]);
|
---|
204 | rcExit = 1;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | /* Finally, close the output file (can fail because of buffered data). */
|
---|
209 | if (fclose(stderr) != 0)
|
---|
210 | {
|
---|
211 | fprintf(stderr, "error: Error closing '%s'.\n", pszOutput);
|
---|
212 | rcExit = 1;
|
---|
213 | }
|
---|
214 |
|
---|
215 | fclose(pOutput);
|
---|
216 | free(papszInputs);
|
---|
217 | return rcExit;
|
---|
218 | }
|
---|
219 |
|
---|