emulator: boot completed logging
[sdk/emulator/qemu.git] / tizen / src / osutil-win32.c
1 /*
2  * Emulator
3  *
4  * Copyright (C) 2012, 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
7  * SeokYeon Hwang <syeon.hwang@samsung.com>
8  * MunKyu Im <munkyu.im@samsung.com>
9  * GiWoong Kim <giwoong.kim@samsung.com>
10  * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
11  * HyunJun Son
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
26  *
27  * Contributors:
28  * - S-Core Co., Ltd
29  *
30  */
31
32 /**
33   @file     osutil-win32.c
34   @brief    Collection of utilities for win32
35  */
36
37 #include <png.h>
38 #include "maru_common.h"
39 #include "osutil.h"
40 #include "emulator.h"
41 #include "debug_ch.h"
42 #include "maru_err_table.h"
43 #include "sdb.h"
44
45 #ifndef CONFIG_WIN32
46 #error
47 #endif
48
49 #include <windows.h>
50
51 MULTI_DEBUG_CHANNEL (emulator, osutil);
52
53
54 static qemu_timeval tv = { 0, 0 };
55 static time_t ti;
56 static char buf_time[64];
57
58 extern char tizen_target_img_path[];
59
60 static const char *pactempfile = ".autoproxy";
61
62 void check_vm_lock_os(void)
63 {
64     uint32_t port;
65     char *base_port = NULL;
66     char *pBuf;
67     HANDLE hMapFile;
68
69     for (port = 26100; port < 26200; port += 10) {
70         base_port = g_strdup_printf("%d", port);
71         hMapFile = OpenFileMapping(FILE_MAP_READ, TRUE, base_port);
72         if (hMapFile == NULL) {
73             INFO("port %s is not used.\n", base_port);
74             continue;
75         } else {
76              pBuf = (char *)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 50);
77             if (pBuf == NULL) {
78                 ERR("Could not map view of file (%d).\n", GetLastError());
79                 CloseHandle(hMapFile);
80             }
81
82             if (strcmp(pBuf, tizen_target_img_path) == 0) {
83                 maru_register_exit_msg(MARU_EXIT_UNKNOWN,
84                     "Can not execute this VM.\n"
85                     "The same name is running now.");
86                 UnmapViewOfFile(pBuf);
87                 CloseHandle(hMapFile);
88                 free(base_port);
89                 exit(0);
90             } else {
91                 UnmapViewOfFile(pBuf);
92             }
93         }
94
95         CloseHandle(hMapFile);
96         free(base_port);
97     }
98 }
99
100 void make_vm_lock_os(void)
101 {
102     HANDLE hMapFile;
103     char *pBuf;
104     char *port_in_use;
105     char *shared_memory;
106     int base_port;
107
108     base_port = get_emul_vm_base_port();
109     shared_memory = g_strdup_printf("%s", tizen_target_img_path);
110     port_in_use =  g_strdup_printf("%d", base_port);
111     hMapFile = CreateFileMapping(
112                  INVALID_HANDLE_VALUE, /* use paging file */
113                  NULL,                 /* default security */
114                  PAGE_READWRITE,       /* read/write access */
115                  0,                /* maximum object size (high-order DWORD) */
116                  50,               /* maximum object size (low-order DWORD) */
117                  port_in_use);         /* name of mapping object */
118     if (hMapFile == NULL) {
119         ERR("Could not create file mapping object (%d).\n", GetLastError());
120         return;
121     }
122     pBuf = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 50);
123
124     if (pBuf == NULL) {
125         ERR("Could not map view of file (%d).\n", GetLastError());
126         CloseHandle(hMapFile);
127         return;
128     }
129
130     CopyMemory((PVOID)pBuf, shared_memory, strlen(shared_memory));
131     free(port_in_use);
132     free(shared_memory);
133 }
134
135 void set_bin_path_os(gchar * exec_argv)
136 {
137     gchar link_path[PATH_MAX] = { 0, };
138     gchar *file_name = NULL;
139
140     if (!GetModuleFileName(NULL, link_path, PATH_MAX)) {
141         return;
142     }
143
144     file_name = g_strrstr(link_path, "\\");
145     g_strlcpy(bin_path, link_path, strlen(link_path) - strlen(file_name) + 1);
146
147     g_strlcat(bin_path, "\\", PATH_MAX);
148 }
149
150 int get_number_of_processors(void)
151 {
152     SYSTEM_INFO sysi;
153     int num_processors = 0;
154
155     GetSystemInfo(&sysi);
156     TRACE("Processor type: %d, Core number: %d\n",
157         sysi.dwProcessorType, sysi.dwNumberOfProcessors);
158
159     num_processors = sysi.dwNumberOfProcessors;
160     if (num_processors < 1) {
161         num_processors = 1;
162     }
163
164     return num_processors;
165 }
166
167 void print_system_info_os(void)
168 {
169     INFO("* Windows\n");
170
171     INFO("* LibPNG Version : %s\n", PNG_LIBPNG_VER_STRING);
172
173     /* Retrieves information about the current os */
174     OSVERSIONINFO osvi;
175     ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
176     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
177
178     if (GetVersionEx(&osvi)) {
179         INFO("* MajorVersion : %d, MinorVersion : %d, BuildNumber : %d, "
180             "PlatformId : %d, CSDVersion : %s\n", osvi.dwMajorVersion,
181             osvi.dwMinorVersion, osvi.dwBuildNumber,
182             osvi.dwPlatformId, osvi.szCSDVersion);
183     }
184
185     /* Retrieves information about the current system */
186     SYSTEM_INFO sysi;
187     ZeroMemory(&sysi, sizeof(SYSTEM_INFO));
188
189 #if 0
190     GetSystemInfo(&sysi);
191     INFO("* Processor type : %d, Number of processors : %d\n",
192             sysi.dwProcessorType, sysi.dwNumberOfProcessors);
193 #endif
194     get_number_of_processors();
195
196     MEMORYSTATUSEX memInfo;
197     memInfo.dwLength = sizeof(MEMORYSTATUSEX);
198     GlobalMemoryStatusEx(&memInfo);
199     INFO("* Total Ram : %llu kB, Free: %lld kB\n",
200             memInfo.ullTotalPhys / 1024, memInfo.ullAvailPhys / 1024);
201 }
202
203 char *get_timeofday(void)
204 {
205     qemu_gettimeofday(&tv);
206     ti = tv.tv_sec;
207
208     struct tm *ptm = localtime(&ti);
209     strftime(buf_time, sizeof(buf_time),
210              "%H:%M:%S", ptm);
211
212     return buf_time;
213 }
214
215 static int get_auto_proxy(BYTE *url, char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
216 {
217     char type[MAXLEN];
218     char proxy[MAXLEN];
219     char line[MAXLEN];
220     FILE *fp_pacfile;
221     char *p = NULL;
222
223     INFO("pac address: %s\n", (char*)url);
224     download_url((char*)url);
225
226     fp_pacfile = fopen(pactempfile, "r");
227     if(fp_pacfile != NULL) {
228         while(fgets(line, MAXLEN, fp_pacfile) != NULL) {
229             if( (strstr(line, "return") != NULL) && (strstr(line, "if") == NULL)) {
230                 INFO("line found %s", line);
231                 sscanf(line, "%*[^\"]\"%s %s", type, proxy);
232             }
233         }
234
235         if(g_str_has_prefix(type, DIRECT)) {
236             INFO("auto proxy is set to direct mode\n");
237             fclose(fp_pacfile);
238         }
239         else if(g_str_has_prefix(type, PROXY)) {
240             INFO("auto proxy is set to proxy mode\n");
241             INFO("type: %s, proxy: %s\n", type, proxy);
242             p = strtok(proxy, "\";");
243             if(p != NULL) {
244                 INFO("auto proxy to set: %s\n",p);
245                 strcpy(http_proxy, p);
246                 strcpy(https_proxy, p);
247                 strcpy(ftp_proxy, p);
248                 strcpy(socks_proxy, p);
249             }
250             fclose(fp_pacfile);
251         }
252         else
253         {
254             ERR("pac file is not wrong! It could be the wrong pac address or pac file format\n");
255             fclose(fp_pacfile);
256         }
257     }
258     else {
259         ERR("fail to get pacfile fp\n");
260         return -1;
261     }
262
263     remove(pactempfile);
264
265     return 0;
266 }
267
268 void get_host_proxy_os(char *http_proxy, char *https_proxy, char *ftp_proxy, char *socks_proxy)
269 {
270     HKEY hKey;
271     int nRet;
272     LONG lRet;
273     BYTE *proxyenable, *proxyserver;
274     char *p;
275     char *real_proxy;
276     BYTE *url;
277
278     DWORD dwLength = 0;
279     nRet = RegOpenKeyEx(HKEY_CURRENT_USER,
280             "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
281             0, KEY_QUERY_VALUE, &hKey);
282     if (nRet != ERROR_SUCCESS) {
283         ERR("Failed to open registry from %s\n",
284                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
285         return 0;
286     }
287     //check auto proxy key exists
288     lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, NULL, &dwLength);
289     if (lRet != ERROR_SUCCESS && dwLength == 0) {
290         ERR("Failed to query value from %s\n",
291                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\AutoConfigURL");
292     }
293     else {
294         //if exists
295         url = (char*)malloc(dwLength);
296         if (url == NULL) {
297             ERR( "Failed to allocate a buffer\n");
298         }
299         else {
300             memset(url, 0x00, dwLength);
301             lRet = RegQueryValueEx(hKey, "AutoConfigURL", 0, NULL, url, &dwLength);
302             if (lRet == ERROR_SUCCESS && dwLength != 0) {
303                 get_auto_proxy(url, http_proxy, https_proxy, ftp_proxy, socks_proxy);
304                 RegCloseKey(hKey);
305                 return 0;
306             }
307         }
308     }
309     //check manual proxy key exists
310     lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, NULL, &dwLength);
311     if (lRet != ERROR_SUCCESS && dwLength == 0) {
312         ERR(stderr, "Failed to query value from %s\n",
313                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
314         RegCloseKey(hKey);
315         return 0;
316     }
317     proxyenable = (BYTE*)malloc(dwLength);
318     if (proxyenable == NULL) {
319         ERR( "Failed to allocate a buffer\n");
320         RegCloseKey(hKey);
321         return 0;
322     }
323
324     lRet = RegQueryValueEx(hKey, "ProxyEnable", 0, NULL, proxyenable, &dwLength);
325     if (lRet != ERROR_SUCCESS) {
326         free(proxyenable);
327         ERR("Failed to query value from %s\n",
328                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyEnable");
329         RegCloseKey(hKey);
330         return 0;
331     }
332     if (*(char*)proxyenable == 0) {
333         free(proxyenable);
334         RegCloseKey(hKey);
335         return 0;
336     }
337
338     dwLength = 0;
339     lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, NULL, &dwLength);
340     if (lRet != ERROR_SUCCESS && dwLength == 0) {
341         ERR("Failed to query value from from %s\n",
342                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
343         RegCloseKey(hKey);
344         return 0;
345     }
346
347     proxyserver = (BYTE*)malloc(dwLength);
348     if (proxyserver == NULL) {
349         ERR( "Failed to allocate a buffer\n");
350         RegCloseKey(hKey);
351         return 0;
352     }
353
354     memset(proxyserver, 0x00, dwLength);
355     lRet = RegQueryValueEx(hKey, "ProxyServer", 0, NULL, proxyserver, &dwLength);
356     if (lRet != ERROR_SUCCESS) {
357         free(proxyserver);
358         ERR( "Failed to query value from from %s\n",
359                 "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
360         RegCloseKey(hKey);
361         return 0;
362     }
363
364     if((char*)proxyserver != NULL) {
365         INFO("proxy value: %s\n", (char*)proxyserver);
366         real_proxy = malloc(MAXLEN);
367
368         for(p = strtok((char*)proxyserver, ";"); p; p = strtok(NULL, ";")){
369             if(strstr(p, HTTP_PROTOCOL)) {
370                 remove_string(p, real_proxy, HTTP_PROTOCOL);
371                 strcpy(http_proxy, real_proxy);
372             }
373             else if(strstr(p, HTTPS_PROTOCOL)) {
374                 remove_string(p, real_proxy, HTTPS_PROTOCOL);
375                 strcpy(https_proxy, real_proxy);
376             }
377             else if(strstr(p, FTP_PROTOCOL)) {
378                 remove_string(p, real_proxy, FTP_PROTOCOL);
379                 strcpy(ftp_proxy, real_proxy);
380             }
381             else if(strstr(p, SOCKS_PROTOCOL)) {
382                 remove_string(p, real_proxy, SOCKS_PROTOCOL);
383                 strcpy(socks_proxy, real_proxy);
384             }
385             else {
386                 INFO("all protocol uses the same proxy server: %s\n", p);
387                 strcpy(http_proxy, p);
388                 strcpy(https_proxy, p);
389                 strcpy(ftp_proxy, p);
390                 strcpy(socks_proxy, p);
391             }
392         }
393         free(real_proxy);
394     }
395     else {
396         INFO("proxy is null\n");
397         return 0;
398     }
399     RegCloseKey(hKey);
400 }