VirtualBox

source: vbox/trunk/src/VBox/Installer/win/StubBld/VBoxStubBld.cpp@ 93115

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.7 KB
Line 
1/* $Id: VBoxStubBld.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * VBoxStubBld - VirtualBox's Windows installer stub builder.
4 */
5
6/*
7 * Copyright (C) 2009-2022 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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <iprt/win/windows.h>
23#include <shellapi.h>
24#include <strsafe.h>
25
26#include <VBox/version.h>
27#include <iprt/types.h>
28
29#include "VBoxStubBld.h"
30
31HRESULT GetFile (const char* pszFilePath,
32 HANDLE* phFile,
33 DWORD* pdwFileSize)
34{
35 HRESULT hr = S_OK;
36 *phFile = CreateFile(pszFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
37 if (INVALID_HANDLE_VALUE == *phFile)
38 hr = HRESULT_FROM_WIN32(GetLastError());
39 else
40 {
41 *pdwFileSize = ::GetFileSize(*phFile, NULL);
42 if (!*pdwFileSize)
43 hr = HRESULT_FROM_WIN32(GetLastError());
44 }
45 return hr;
46}
47
48HRESULT UpdateResource(HANDLE hFile,
49 DWORD dwFileSize,
50 HANDLE hResourceUpdate,
51 const char *pszResourceType,
52 const char *pszResourceId)
53{
54 HRESULT hr = S_OK;
55
56 HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
57 PVOID pvFile = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, dwFileSize);
58 if (!UpdateResourceA(hResourceUpdate, pszResourceType, pszResourceId,
59 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), pvFile, dwFileSize))
60 hr = HRESULT_FROM_WIN32(GetLastError());
61
62 if (pvFile)
63 UnmapViewOfFile(pvFile);
64
65 if (hMap)
66 CloseHandle(hMap);
67
68 return hr;
69}
70
71static HRESULT IntegrateFile(HANDLE hResourceUpdate, const char *pszResourceType,
72 const char *pszResourceId, const char *pszFilePath)
73{
74 HANDLE hFile = INVALID_HANDLE_VALUE;
75 DWORD dwFileSize = 0;
76 HRESULT hr = GetFile(pszFilePath, &hFile, &dwFileSize);
77 if (SUCCEEDED(hr))
78 {
79 hr = UpdateResource(hFile, dwFileSize, hResourceUpdate, pszResourceType, pszResourceId);
80 if (FAILED(hr))
81 printf("ERROR: Error updating resource for file %s!", pszFilePath);
82 }
83 else
84 hr = HRESULT_FROM_WIN32(GetLastError());
85
86 if (hFile != INVALID_HANDLE_VALUE)
87 CloseHandle(hFile);
88 return hr;
89}
90
91static char *MyPathFilename(const char *pszPath)
92{
93 const char *pszName = pszPath;
94 for (const char *psz = pszPath;; psz++)
95 {
96 switch (*psz)
97 {
98 /* handle separators. */
99 case ':':
100 pszName = psz + 1;
101 break;
102
103 case '\\':
104 case '/':
105 pszName = psz + 1;
106 break;
107
108 /* the end */
109 case '\0':
110 if (*pszName)
111 return (char *)(void *)pszName;
112 return NULL;
113 }
114 }
115
116 /* will never get here */
117}
118
119
120int main(int argc, char* argv[])
121{
122 HRESULT hr = S_OK;
123 int rcExit = RTEXITCODE_SUCCESS;
124
125 char szSetupStub[_MAX_PATH] = {"VBoxStub.exe"};
126 char szOutput[_MAX_PATH] = {"VirtualBox-MultiArch.exe"};
127 HANDLE hUpdate = NULL;
128
129 do /* goto avoidance "loop" */
130 {
131 printf(VBOX_PRODUCT " Stub Builder v%d.%d.%d.%d\n",
132 VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
133
134 if (argc < 2)
135 printf("WARNING: No parameters given! Using default values!\n");
136
137 VBOXSTUBBUILDPKG stbBuildPkg[VBOXSTUB_MAX_PACKAGES] = {0};
138 VBOXSTUBPKG stbPkg[VBOXSTUB_MAX_PACKAGES] = {0};
139 VBOXSTUBPKGHEADER stbHeader =
140 {
141 "vbox$tub", /* File magic. */
142 1, /* Version. */
143 0 /* No files yet. */
144 };
145
146 for (int i=1; i<argc; i++)
147 {
148 if (!stricmp(argv[i], "-out") && argc > i+1)
149 {
150 hr = StringCchCopy(szOutput, _MAX_PATH, argv[i+1]);
151 i++;
152 }
153
154 else if (!stricmp(argv[i], "-stub") && argc > i+1)
155 {
156 hr = StringCchCopy(szSetupStub, _MAX_PATH, argv[i+1]);
157 i++;
158 }
159
160 else if (!stricmp(argv[i], "-target-all") && argc > i+1)
161 {
162 hr = StringCchCopy(stbBuildPkg[stbHeader.byCntPkgs].szSourcePath, _MAX_PATH, argv[i+1]);
163 stbBuildPkg[stbHeader.byCntPkgs].byArch = VBOXSTUBPKGARCH_ALL;
164 stbHeader.byCntPkgs++;
165 i++;
166 }
167
168 else if (!stricmp(argv[i], "-target-x86") && argc > i+1)
169 {
170 hr = StringCchCopy(stbBuildPkg[stbHeader.byCntPkgs].szSourcePath, _MAX_PATH, argv[i+1]);
171 stbBuildPkg[stbHeader.byCntPkgs].byArch = VBOXSTUBPKGARCH_X86;
172 stbHeader.byCntPkgs++;
173 i++;
174 }
175
176 else if (!stricmp(argv[i], "-target-amd64") && argc > i+1)
177 {
178 hr = StringCchCopy(stbBuildPkg[stbHeader.byCntPkgs].szSourcePath, _MAX_PATH, argv[i+1]);
179 stbBuildPkg[stbHeader.byCntPkgs].byArch = VBOXSTUBPKGARCH_AMD64;
180 stbHeader.byCntPkgs++;
181 i++;
182 }
183 else
184 {
185 printf("ERROR: Invalid parameter: %s\n", argv[i]);
186 hr = E_INVALIDARG;
187 break;
188 }
189 if (FAILED(hr))
190 {
191 printf("ERROR: StringCchCopy failed: %#lx\n", hr);
192 break;
193 }
194 }
195 if (FAILED(hr))
196 break;
197
198 if (stbHeader.byCntPkgs <= 0)
199 {
200 printf("ERROR: No packages defined! Exiting.\n");
201 break;
202 }
203
204 printf("Stub: %s\n", szSetupStub);
205 printf("Output: %s\n", szOutput);
206 printf("# Packages: %u\n", stbHeader.byCntPkgs);
207
208 if (!CopyFile(szSetupStub, szOutput, FALSE))
209 {
210 hr = HRESULT_FROM_WIN32(GetLastError());
211 printf("ERROR: Could not create stub loader: %#lx\n", hr);
212 break;
213 }
214
215 hUpdate = BeginUpdateResource(szOutput, FALSE);
216
217 PVBOXSTUBPKG pPackage = stbPkg;
218 char szHeaderName[_MAX_PATH] = {0};
219
220 for (BYTE i = 0; i < stbHeader.byCntPkgs; i++)
221 {
222 printf("Integrating (Platform %d): %s\n", stbBuildPkg[i].byArch, stbBuildPkg[i].szSourcePath);
223
224 /* Construct resource name. */
225 hr = StringCchPrintf(pPackage->szResourceName, _MAX_PATH, "BIN_%02d", i);
226 pPackage->byArch = stbBuildPkg[i].byArch;
227
228 /* Construct final name used when extracting. */
229 hr = StringCchCopy(pPackage->szFileName, _MAX_PATH, MyPathFilename(stbBuildPkg[i].szSourcePath));
230
231 /* Integrate header into binary. */
232 hr = StringCchPrintf(szHeaderName, _MAX_PATH, "HDR_%02d", i);
233 hr = UpdateResource(hUpdate, RT_RCDATA, szHeaderName, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), pPackage, sizeof(VBOXSTUBPKG));
234
235 /* Integrate file into binary. */
236 hr = IntegrateFile(hUpdate, RT_RCDATA, pPackage->szResourceName, stbBuildPkg[i].szSourcePath);
237 if (FAILED(hr))
238 {
239 printf("ERROR: Could not integrate binary %s (%s): %#lx\n",
240 pPackage->szResourceName, pPackage->szFileName, hr);
241 rcExit = RTEXITCODE_FAILURE;
242 }
243
244 pPackage++;
245 }
246
247 if (FAILED(hr))
248 break;
249
250 if (!UpdateResource(hUpdate, RT_RCDATA, "MANIFEST", MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), &stbHeader, sizeof(VBOXSTUBPKGHEADER)))
251 {
252 hr = HRESULT_FROM_WIN32(GetLastError());
253 break;
254 }
255
256 if (!EndUpdateResource(hUpdate, FALSE))
257 {
258 hr = HRESULT_FROM_WIN32(GetLastError());
259 break;
260 }
261
262 printf("Integration done!\n");
263
264 } while (0);
265
266 hUpdate = NULL;
267
268 if (FAILED(hr))
269 {
270 printf("ERROR: Building failed! Last error: %lu\n", GetLastError());
271 rcExit = RTEXITCODE_FAILURE;
272 }
273 return rcExit;
274}
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