1 | /* $Id: preload.cpp 27430 2010-03-16 22:03:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * bin2c - Binary 2 C Structure Converter.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #ifdef RT_OS_WINDOWS
|
---|
26 | # include <Windows.h>
|
---|
27 | #else
|
---|
28 | # include <sys/mman.h>
|
---|
29 | # include <sys/stat.h>
|
---|
30 | # include <fcntl.h>
|
---|
31 | # include <unistd.h>
|
---|
32 | # include <errno.h>
|
---|
33 | #endif
|
---|
34 | #include <stdio.h>
|
---|
35 | #include <string.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | static int load(const char *pszImage)
|
---|
39 | {
|
---|
40 | #ifdef RT_OS_WINDOWS
|
---|
41 | HANDLE hFile = CreateFile(pszImage,
|
---|
42 | GENERIC_READ,
|
---|
43 | FILE_SHARE_READ,
|
---|
44 | NULL /*pSecurityAttributes*/,
|
---|
45 | OPEN_EXISTING,
|
---|
46 | FILE_ATTRIBUTE_NORMAL,
|
---|
47 | NULL /*hTemplateFile*/);
|
---|
48 | if (hFile == INVALID_HANDLE_VALUE)
|
---|
49 | {
|
---|
50 | printf("error: CreateFile('%s',): %d\n", pszImage, GetLastError());
|
---|
51 | return 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | DWORD cbHigh = 0;
|
---|
55 | DWORD cbLow = GetFileSize(hFile, &cbHigh);
|
---|
56 | size_t cbFile = cbLow != INVALID_FILE_SIZE
|
---|
57 | ? cbHigh == 0
|
---|
58 | ? cbLow
|
---|
59 | : ~(DWORD)0 / 4
|
---|
60 | : 64;
|
---|
61 |
|
---|
62 | HANDLE hMap = CreateFileMapping(hFile,
|
---|
63 | NULL /*pAttributes*/,
|
---|
64 | PAGE_READONLY | SEC_COMMIT,
|
---|
65 | 0 /*dwMaximumSizeHigh -> file size*/,
|
---|
66 | 0 /*dwMaximumSizeLow -> file size*/,
|
---|
67 | NULL /*pName*/);
|
---|
68 | if (hMap == INVALID_HANDLE_VALUE)
|
---|
69 | printf("error: CreateFile('%s',): %d\n", pszImage, GetLastError());
|
---|
70 | CloseHandle(hFile);
|
---|
71 | if (hMap == INVALID_HANDLE_VALUE)
|
---|
72 | return 1;
|
---|
73 |
|
---|
74 | void *pvWhere = MapViewOfFile(hMap,
|
---|
75 | FILE_MAP_READ,
|
---|
76 | 0 /*dwFileOffsetHigh*/,
|
---|
77 | 0 /*dwFileOffsetLow*/,
|
---|
78 | 0 /*dwNumberOfBytesToMap - file size */);
|
---|
79 | if (!pvWhere)
|
---|
80 | {
|
---|
81 | printf("error: MapViewOfView('%s',): %d\n", pszImage, GetLastError());
|
---|
82 | CloseHandle(hMap);
|
---|
83 | return 1;
|
---|
84 | }
|
---|
85 |
|
---|
86 | #else
|
---|
87 | int fd = open(pszImage, O_RDONLY, 0);
|
---|
88 | if (fd < 0)
|
---|
89 | {
|
---|
90 | printf("error: open('%s',): %d\n", pszImage, errno);
|
---|
91 | return 1;
|
---|
92 | }
|
---|
93 |
|
---|
94 | struct stat st;
|
---|
95 | memset(&st, 0, sizeof(st));
|
---|
96 | if (fstat(fd, &st))
|
---|
97 | st.st_size = 64;
|
---|
98 | size_t cbFile = st.st_size < ~(size_t)0
|
---|
99 | ? (size_t)st.st_size
|
---|
100 | : ~(size_t)0 / 4;
|
---|
101 |
|
---|
102 | void *pvWhere = mmap(NULL /*addr*/, cbFile, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0 /*offset*/);
|
---|
103 | if (pvWhere == MAP_FAILED)
|
---|
104 | printf("error: mmap(,%lu,)/'%s': %d\n", (unsigned long)cbFile, pszImage, errno);
|
---|
105 | close(fd);
|
---|
106 | if (pvWhere == MAP_FAILED)
|
---|
107 | return 1;
|
---|
108 |
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | /* Touch the whole image... do a dummy crc to keep the optimizer from begin
|
---|
112 | smart with us. */
|
---|
113 | unsigned char *puchFile = (unsigned char *)pvWhere;
|
---|
114 | size_t off = 0;
|
---|
115 | unsigned int uCrc = 0;
|
---|
116 | while (off < cbFile)
|
---|
117 | uCrc += puchFile[off++];
|
---|
118 | printf("info: %p/%#lx/%#x - %s\n", pvWhere, (unsigned long)cbFile, (unsigned char)uCrc, pszImage);
|
---|
119 |
|
---|
120 | return 0;
|
---|
121 | }
|
---|
122 |
|
---|
123 | static int usage(const char *argv0)
|
---|
124 | {
|
---|
125 | printf("Generic executable image preloader.\n"
|
---|
126 | "Usage: %s [dll|exe|file []]\n", argv0);
|
---|
127 | return 1;
|
---|
128 | }
|
---|
129 |
|
---|
130 | int main(int argc, char **argv)
|
---|
131 | {
|
---|
132 | /*
|
---|
133 | * Check for options.
|
---|
134 | */
|
---|
135 | for (int i = 1; i < argc; i++)
|
---|
136 | {
|
---|
137 | if (argv[i][0] == '-')
|
---|
138 | {
|
---|
139 | if ( argv[i][1] == '-'
|
---|
140 | && argv[i][2] == '\0')
|
---|
141 | break;
|
---|
142 | if ( !strcmp(argv[i], "--help")
|
---|
143 | || !strcmp(argv[i], "-help")
|
---|
144 | || !strcmp(argv[i], "-h")
|
---|
145 | || !strcmp(argv[i], "-?"))
|
---|
146 | {
|
---|
147 | usage(argv[0]);
|
---|
148 | return 1;
|
---|
149 | }
|
---|
150 | if ( !strcmp(argv[i], "--version")
|
---|
151 | || !strcmp(argv[i], "-V"))
|
---|
152 | {
|
---|
153 | printf("$Revision: 27430 $\n");
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 | fprintf(stderr, "syntax error: unknown option '%s'\n", argv[i]);
|
---|
157 | return 1;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | if (argc <= 1)
|
---|
161 | return usage(argv[0]);
|
---|
162 |
|
---|
163 | /*
|
---|
164 | * Do the loading.
|
---|
165 | */
|
---|
166 | for (int i = 1; i < argc; i++)
|
---|
167 | {
|
---|
168 | if (!strcmp(argv[i], "--"))
|
---|
169 | continue;
|
---|
170 | if (argv[i][0] == '@')
|
---|
171 | {
|
---|
172 | FILE *pFile = fopen(&argv[i][1], "r");
|
---|
173 | if (pFile)
|
---|
174 | {
|
---|
175 | char szLine[4096];
|
---|
176 | while (fgets(szLine, sizeof(szLine), pFile))
|
---|
177 | {
|
---|
178 | char *psz = szLine;
|
---|
179 | while (*psz == ' ' || *psz == '\t')
|
---|
180 | psz++;
|
---|
181 | size_t off = strlen(psz);
|
---|
182 | while ( off > 0
|
---|
183 | && ( psz[off - 1] == ' '
|
---|
184 | || psz[off - 1] == '\t'
|
---|
185 | || psz[off - 1] == '\n'
|
---|
186 | || psz[off - 1] == '\r')
|
---|
187 | )
|
---|
188 | psz[--off] = '\0';
|
---|
189 |
|
---|
190 | if (*psz && *psz != '#')
|
---|
191 | load(psz);
|
---|
192 | }
|
---|
193 | fclose(pFile);
|
---|
194 | }
|
---|
195 | else
|
---|
196 | fprintf(stderr, "error: fopen('%s','r'): %d\n", argv[i][1], errno);
|
---|
197 | }
|
---|
198 | else
|
---|
199 | load(argv[i]);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * Sleep for ever.
|
---|
204 | */
|
---|
205 | for (;;)
|
---|
206 | {
|
---|
207 | #ifdef RT_OS_WINDOWS
|
---|
208 | Sleep(3600*1000);
|
---|
209 | #else
|
---|
210 | sleep(3600);
|
---|
211 | #endif
|
---|
212 | }
|
---|
213 |
|
---|
214 | return 0;
|
---|
215 | }
|
---|