]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/Main/Main.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Main / Main.c
1 /** @file
2 Establish the program environment and the "main" entry point.
3
4 All of the global data in the gMD structure is initialized to 0, NULL, or
5 SIG_DFL; as appropriate.
6
7 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
8 This program and the accompanying materials are licensed and made available under
9 the terms and conditions of the BSD License that accompanies this distribution.
10 The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php.
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 **/
16 #include <Uefi.h>
17 #include <Library/UefiLib.h>
18
19 #include <Library/ShellCEntryLib.h>
20 #include <Library/MemoryAllocationLib.h>
21 #include <Library/TimerLib.h>
22
23 #include <LibConfig.h>
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <MainData.h>
29
30 extern int main( int, wchar_t**);
31 extern int __sse2_available;
32
33 struct __MainData *gMD;
34
35 /* Worker function to keep GCC happy. */
36 void __main()
37 {
38 ;
39 }
40
41 INTN
42 EFIAPI
43 ShellAppMain (
44 IN UINTN Argc,
45 IN CHAR16 **Argv
46 )
47 {
48 INTN ExitVal;
49 INTN i;
50 struct __filedes *mfd;
51 FILE *fp;
52
53 ExitVal = (INTN)RETURN_SUCCESS;
54 gMD = AllocateZeroPool(sizeof(struct __MainData));
55 if( gMD == NULL ) {
56 ExitVal = (INTN)RETURN_OUT_OF_RESOURCES;
57 }
58 else {
59 /* Initialize data */
60 __sse2_available = 0;
61 _fltused = 1;
62 errno = 0;
63 EFIerrno = 0;
64
65 #ifdef NT32dvm
66 gMD->ClocksPerSecond = 0; // For NT32 only
67 gMD->AppStartTime = 0; // For NT32 only
68 #else
69 gMD->ClocksPerSecond = (clock_t)GetPerformanceCounterProperties( NULL, NULL);
70 gMD->AppStartTime = (clock_t)GetPerformanceCounter();
71 #endif /* NT32 dvm */
72
73 // Initialize file descriptors
74 mfd = gMD->fdarray;
75 for(i = 0; i < (FOPEN_MAX); ++i) {
76 mfd[i].MyFD = (UINT16)i;
77 }
78
79 // Open stdin, stdout, stderr
80 fp = freopen("stdin:", "r", stdin);
81 if(fp != NULL) {
82 fp = freopen("stdout:", "w", stdout);
83 if(fp != NULL) {
84 fp = freopen("stderr:", "w", stderr);
85 }
86 }
87 if(fp == NULL) {
88 Print(L"ERROR Initializing Standard IO: %a.\n %r\n",
89 strerror(errno), EFIerrno);
90 }
91
92 ExitVal = (INTN)main( (int)Argc, (wchar_t **)Argv);
93
94 if (gMD->cleanup != NULL) {
95 gMD->cleanup();
96 }
97 }
98 if(gMD != NULL) {
99 FreePool( gMD );
100 }
101 return ExitVal;
102 }