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