summaryrefslogtreecommitdiffstats
path: root/src/VBox/Installer/win/StubBld/VBoxStubBld.cpp
blob: 3e1320cc7358a4fe6f2438f361f0dbfe6478ff24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/* $Id: VBoxStubBld.cpp $ */
/** @file
 * VBoxStubBld - VirtualBox's Windows installer stub builder.
 */

/*
 * Copyright (C) 2009-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/win/windows.h>
#include <shellapi.h>
#include <strsafe.h>

#include <VBox/version.h>
#include <iprt/types.h>

#include "VBoxStubBld.h"


static HRESULT GetFile(const char *pszFilePath, HANDLE *phFile, DWORD *pcbFile)
{
    HANDLE hFile = CreateFileA(pszFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
                               OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        SetLastError(NO_ERROR);
        LARGE_INTEGER cbFile;
        if (GetFileSizeEx(hFile, &cbFile))
        {
            if (cbFile.HighPart == 0)
            {
                *pcbFile = cbFile.LowPart;
                *phFile  = hFile;
                return S_OK;
            }
            fprintf(stderr, "error: File '%s' is too large: %llu bytes\n", pszFilePath, cbFile.QuadPart);
        }
        else
            fprintf(stderr, "error: GetFileSizeEx failed on '%s': %lu\n", pszFilePath, GetLastError());
        CloseHandle(hFile);
    }
    else
        fprintf(stderr, "error: CreateFileA failed on '%s': %lu\n", pszFilePath, GetLastError());
    *phFile = INVALID_HANDLE_VALUE;
    return E_FAIL;
}

static HRESULT MyUpdateResource(HANDLE hFile, DWORD cbFile, HANDLE hResourceUpdate,
                                const char *pszResourceType, const char *pszResourceId)
{
    HRESULT hr   = E_FAIL;
    HANDLE  hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    if (hMap != NULL)
    {
        PVOID pvFile = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, cbFile);
        if (pvFile)
        {
            if (UpdateResourceA(hResourceUpdate, pszResourceType, pszResourceId,
                                MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), pvFile, cbFile))
                hr = S_OK;
            else
                fprintf(stderr, "error: UpdateResourceA failed: %lu\n", GetLastError());

            UnmapViewOfFile(pvFile);
        }
        else
            fprintf(stderr, "error: MapViewOfFile failed: %lu\n", GetLastError());
        CloseHandle(hMap);
    }
    else
        fprintf(stderr, "error: CreateFileMappingW failed: %lu\n", GetLastError());
    return hr;
}

static HRESULT IntegrateFile(HANDLE hResourceUpdate, const char *pszResourceType,
                             const char *pszResourceId, const char *pszFilePath)
{
    HANDLE hFile = INVALID_HANDLE_VALUE;
    DWORD cbFile = 0;
    HRESULT hr = GetFile(pszFilePath, &hFile, &cbFile);
    if (SUCCEEDED(hr))
    {
        hr = MyUpdateResource(hFile, cbFile, hResourceUpdate, pszResourceType, pszResourceId);
        if (FAILED(hr))
            printf("ERROR: Error updating resource for file %s!", pszFilePath);
        CloseHandle(hFile);
    }
    else
        hr = HRESULT_FROM_WIN32(GetLastError());
    return hr;
}

static char *MyPathFilename(const char *pszPath)
{
    const char *pszName = pszPath;
    for (const char *psz = pszPath;; psz++)
    {
        switch (*psz)
        {
            /* handle separators. */
            case ':':
                pszName = psz + 1;
                break;

            case '\\':
            case '/':
                pszName = psz + 1;
                break;

            /* the end */
            case '\0':
                if (*pszName)
                    return (char *)(void *)pszName;
                return NULL;
        }
    }

    /* will never get here */
}


int main(int argc, char* argv[])
{
    /*
     * Parse arguments.
     */
    const char *pszSetupStub = "VBoxStub.exe";
    const char *pszOutput    = "VirtualBox-MultiArch.exe";

    printf(VBOX_PRODUCT " Stub Builder v%d.%d.%d.%d\n",
           VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);

    struct VBOXSTUBBUILDPKG
    {
        const char     *pszSrcPath;
        VBOXSTUBPKGARCH enmArch;
    }                   aBuildPkgs[VBOXSTUB_MAX_PACKAGES] = { { NULL } };
    VBOXSTUBPKGHEADER   StubHdr =
    {
        /*.szMagic = */   VBOXSTUBPKGHEADER_MAGIC_SZ,
        /*.cPackages = */ 0
    };

    for (int i = 1; i < argc; i++)
    {
        const char *pszArg = argv[i];
        if (   strcmp(pszArg, "--help") == 0
            || strcmp(pszArg, "-help") == 0
            || strcmp(pszArg, "-h") == 0
            || strcmp(pszArg, "-?") == 0)
        {
            printf("usage: %s -out <installer.exe> -stub <stub.exe> [-target-all <file>] [-target-<arch> <file>]\n", argv[0]);
            return RTEXITCODE_SUCCESS;
        }

        /* The remaining options all take a while. */
        if (   strcmp(pszArg, "-out")
            && strcmp(pszArg, "-stub")
            && strcmp(pszArg, "-target-all")
            && strcmp(pszArg, "-target-x86")
            && strcmp(pszArg, "-target-amd64"))
        {
            fprintf(stderr, "syntax error: Invalid parameter: %s\n", argv[i]);
            return RTEXITCODE_SYNTAX;
        }

        i++;
        if (i >= argc)
        {
            fprintf(stderr, "syntax error: Option '%s' takes a value argument!\n", pszArg);
            return RTEXITCODE_SYNTAX;
        }
        const char *pszValue = argv[i];

        /* Process the individual options. */
        if (strcmp(pszArg, "-out") == 0)
            pszOutput = pszValue;
        else if (strcmp(pszArg, "-stub") == 0)
            pszSetupStub = pszValue;
        else
        {
            if (StubHdr.cPackages >= RT_ELEMENTS(aBuildPkgs))
            {
                fprintf(stderr, "error: Too many packages specified!\n");
                return RTEXITCODE_FAILURE;
            }
            aBuildPkgs[StubHdr.cPackages].pszSrcPath = pszValue;
            if (strcmp(pszArg, "-target-all") == 0)
                aBuildPkgs[StubHdr.cPackages].enmArch = VBOXSTUBPKGARCH_ALL;
            else if (strcmp(pszArg, "-target-amd64") == 0)
                aBuildPkgs[StubHdr.cPackages].enmArch = VBOXSTUBPKGARCH_AMD64;
            else if (strcmp(pszArg, "-target-x86") == 0)
                aBuildPkgs[StubHdr.cPackages].enmArch = VBOXSTUBPKGARCH_X86;
            else
            {
                fprintf(stderr, "internal error: %u\n", __LINE__);
                return RTEXITCODE_FAILURE;
            }
            StubHdr.cPackages++;
        }
    }

    if (StubHdr.cPackages == 0)
    {
        fprintf(stderr, "syntax error: No packages specified! Exiting.\n");
        return RTEXITCODE_SYNTAX;
    }

    printf("Stub:       %s\n", pszSetupStub);
    printf("Output:     %s\n", pszOutput);
    printf("# Packages: %u\n", StubHdr.cPackages);

    /*
     * Copy the stub over the output file.
     */
    if (!CopyFile(pszSetupStub, pszOutput, FALSE))
    {
        fprintf(stderr, "ERROR: Could not copy the stub loader: %lu\n", GetLastError());
        return RTEXITCODE_SYNTAX;
    }

    /*
     * Start updating the resources of the output file.
     */
    HANDLE hUpdate = BeginUpdateResourceA(pszOutput, FALSE);
    if (hUpdate)
    {
        /*
         * Add the file one by one to the output file.
         */
        HRESULT hrc = S_OK;
        for (BYTE i = 0; i < StubHdr.cPackages; i++)
        {
            printf("Integrating (Platform %d): %s\n", aBuildPkgs[i].enmArch, aBuildPkgs[i].pszSrcPath);

            /*
             * Create the package header.
             */
            VBOXSTUBPKG Package = {0};
            Package.enmArch = aBuildPkgs[i].enmArch;

            /* The resource name */
            hrc = StringCchPrintf(Package.szResourceName, sizeof(Package.szResourceName), "BIN_%02d", i);
            if (FAILED(hrc))
            {
                fprintf(stderr, "Internal error: %u\n", __LINE__);
                break;
            }

            /* Construct final name used when extracting. */
            hrc = StringCchCopy(Package.szFilename, sizeof(Package.szFilename), MyPathFilename(aBuildPkgs[i].pszSrcPath));
            if (FAILED(hrc))
            {
                fprintf(stderr, "ERROR: Filename is too long: %s\n", aBuildPkgs[i].pszSrcPath);
                break;
            }

            /*
             * Add the package header to the binary.
             */
            char szHeaderName[32];
            hrc = StringCchPrintf(szHeaderName, sizeof(szHeaderName), "HDR_%02d", i);
            if (FAILED(hrc))
            {
                fprintf(stderr, "Internal error: %u\n", __LINE__);
                break;
            }

            if (!UpdateResourceA(hUpdate, RT_RCDATA, szHeaderName, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
                                 &Package, sizeof(Package)))
            {
                fprintf(stderr, "ERROR: UpdateResourceA failed for the package header: %lu\n", GetLastError());
                hrc = E_FAIL;
                break;
            }

            /*
             * Add the file content under the BIN_xx resource name.
             */
            hrc = IntegrateFile(hUpdate, RT_RCDATA, Package.szResourceName, aBuildPkgs[i].pszSrcPath);
            if (FAILED(hrc))
                break;
        }
        if (SUCCEEDED(hrc))
        {
            /*
             * Now add the header/manifest and complete the operation.
             */
            if (UpdateResourceA(hUpdate, RT_RCDATA, "MANIFEST", MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
                                &StubHdr, sizeof(StubHdr)))
            {
                if (EndUpdateResourceA(hUpdate, FALSE /*fDiscard*/))
                {
                    printf("Successfully created the installer\n");
                    return RTEXITCODE_SUCCESS;
                }
                fprintf(stderr, "ERROR: EndUpdateResourceA failed: %lu\n", GetLastError());
            }
            else
                fprintf(stderr, "ERROR: UpdateResourceA failed for the installer header/manifest: %lu\n", GetLastError());
        }

        EndUpdateResourceA(hUpdate, TRUE /*fDiscard*/);
        hUpdate = NULL;
    }
    else
        fprintf(stderr, "error: BeginUpdateResource failed: %lu\n", GetLastError());
    return RTEXITCODE_FAILURE;
}