]>
Commit | Line | Data |
---|---|---|
2aa62f2b | 1 | /** @file\r |
2 | Establish the program environment and the "main" entry point.\r | |
3 | \r | |
4 | All of the global data in the gMD structure is initialized to 0, NULL, or\r | |
5 | SIG_DFL; as appropriate.\r | |
6 | \r | |
d78fab6b | 7 | Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>\r |
2aa62f2b | 8 | This program and the accompanying materials are licensed and made available under\r |
9 | the terms and conditions of the BSD License that accompanies this distribution.\r | |
10 | The full text of the license may be found at\r | |
d7ce7006 | 11 | http://opensource.org/licenses/bsd-license.\r |
2aa62f2b | 12 | \r |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
14 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
15 | **/\r | |
16 | #include <Uefi.h>\r | |
17 | #include <Library/UefiLib.h>\r | |
d7ce7006 | 18 | #include <Library/DebugLib.h>\r |
2aa62f2b | 19 | \r |
20 | #include <Library/ShellCEntryLib.h>\r | |
21 | #include <Library/MemoryAllocationLib.h>\r | |
22 | #include <Library/TimerLib.h>\r | |
23 | \r | |
24 | #include <LibConfig.h>\r | |
25 | \r | |
26 | #include <errno.h>\r | |
27 | #include <stdio.h>\r | |
53e1e5c6 | 28 | #include <stdlib.h>\r |
2aa62f2b | 29 | #include <string.h>\r |
8aa163da | 30 | #include <time.h>\r |
2aa62f2b | 31 | #include <MainData.h>\r |
8aa163da | 32 | #include <unistd.h>\r |
2aa62f2b | 33 | \r |
53e1e5c6 | 34 | extern int main( int, char**);\r |
2aa62f2b | 35 | extern int __sse2_available;\r |
36 | \r | |
37 | struct __MainData *gMD;\r | |
38 | \r | |
39 | /* Worker function to keep GCC happy. */\r | |
40 | void __main()\r | |
41 | {\r | |
42 | ;\r | |
43 | }\r | |
44 | \r | |
d7ce7006 | 45 | /** Clean up data as required by the exit() function.\r |
46 | \r | |
47 | **/\r | |
53e1e5c6 | 48 | void\r |
d7ce7006 | 49 | exitCleanup(INTN ExitVal)\r |
53e1e5c6 | 50 | {\r |
d7ce7006 | 51 | void (*CleanUp)(void); // Pointer to Cleanup Function\r |
53e1e5c6 | 52 | int i;\r |
53 | \r | |
d7ce7006 | 54 | if(gMD != NULL) {\r |
55 | gMD->ExitValue = (int)ExitVal;\r | |
56 | CleanUp = gMD->cleanup; // Preserve the pointer to the Cleanup Function\r | |
57 | \r | |
58 | // Call all registered atexit functions in reverse order\r | |
59 | i = gMD->num_atexit;\r | |
60 | if( i > 0) {\r | |
61 | do {\r | |
62 | (gMD->atexit_handler[--i])();\r | |
63 | } while( i > 0);\r | |
53e1e5c6 | 64 | }\r |
65 | \r | |
d7ce7006 | 66 | if (CleanUp != NULL) {\r |
67 | CleanUp();\r | |
53e1e5c6 | 68 | }\r |
53e1e5c6 | 69 | }\r |
70 | }\r | |
71 | \r | |
72 | /* Create mbcs versions of the Argv strings. */\r | |
73 | static\r | |
74 | char **\r | |
75 | ArgvConvert(UINTN Argc, CHAR16 **Argv)\r | |
76 | {\r | |
d78fab6b | 77 | ssize_t AVsz; /* Size of a single nArgv string, or -1 */\r |
53e1e5c6 | 78 | UINTN count;\r |
79 | char **nArgv;\r | |
80 | char *string;\r | |
81 | INTN nArgvSize; /* Cumulative size of narrow Argv[i] */\r | |
82 | \r | |
d7ce7006 | 83 | DEBUG_CODE_BEGIN();\r |
84 | Print(L"ArgvConvert called with %d arguments.\n", Argc);\r | |
85 | for(count = 0; count < ((Argc > 5)? 5: Argc); ++count) {\r | |
86 | Print(L"Argument[%d] = \"%s\".\n", count, Argv[count]);\r | |
87 | }\r | |
88 | DEBUG_CODE_END();\r | |
89 | \r | |
53e1e5c6 | 90 | nArgvSize = Argc;\r |
91 | /* Determine space needed for narrow Argv strings. */\r | |
92 | for(count = 0; count < Argc; ++count) {\r | |
d78fab6b | 93 | AVsz = (ssize_t)wcstombs(NULL, Argv[count], ARG_MAX);\r |
53e1e5c6 | 94 | if(AVsz < 0) {\r |
95 | Print(L"ABORTING: Argv[%d] contains an unconvertable character.\n", count);\r | |
96 | exit(EXIT_FAILURE);\r | |
97 | /* Not Reached */\r | |
98 | }\r | |
99 | nArgvSize += AVsz;\r | |
100 | }\r | |
101 | \r | |
102 | /* Reserve space for the converted strings. */\r | |
103 | gMD->NCmdLine = (char *)AllocateZeroPool(nArgvSize+1);\r | |
104 | if(gMD->NCmdLine == NULL) {\r | |
105 | Print(L"ABORTING: Insufficient memory.\n");\r | |
106 | exit(EXIT_FAILURE);\r | |
107 | /* Not Reached */\r | |
108 | }\r | |
109 | \r | |
110 | /* Convert Argument Strings. */\r | |
111 | nArgv = gMD->NArgV;\r | |
112 | string = gMD->NCmdLine;\r | |
113 | for(count = 0; count < Argc; ++count) {\r | |
114 | nArgv[count] = string;\r | |
115 | AVsz = wcstombs(string, Argv[count], nArgvSize);\r | |
116 | string[AVsz] = 0; /* NULL terminate the argument */\r | |
d7ce7006 | 117 | DEBUG((DEBUG_INFO, "Cvt[%d] %d \"%s\" --> \"%a\"\n", (INT32)count, (INT32)AVsz, Argv[count], nArgv[count]));\r |
53e1e5c6 | 118 | string += AVsz + 1;\r |
119 | nArgvSize -= AVsz + 1;\r | |
120 | if(nArgvSize < 0) {\r | |
121 | Print(L"ABORTING: Internal Argv[%d] conversion error.\n", count);\r | |
122 | exit(EXIT_FAILURE);\r | |
123 | /* Not Reached */\r | |
124 | }\r | |
125 | }\r | |
126 | return gMD->NArgV;\r | |
127 | }\r | |
128 | \r | |
2aa62f2b | 129 | INTN\r |
130 | EFIAPI\r | |
131 | ShellAppMain (\r | |
132 | IN UINTN Argc,\r | |
133 | IN CHAR16 **Argv\r | |
134 | )\r | |
135 | {\r | |
53e1e5c6 | 136 | struct __filedes *mfd;\r |
137 | char **nArgv;\r | |
2aa62f2b | 138 | INTN ExitVal;\r |
d7ce7006 | 139 | int i;\r |
2aa62f2b | 140 | \r |
141 | ExitVal = (INTN)RETURN_SUCCESS;\r | |
142 | gMD = AllocateZeroPool(sizeof(struct __MainData));\r | |
143 | if( gMD == NULL ) {\r | |
144 | ExitVal = (INTN)RETURN_OUT_OF_RESOURCES;\r | |
145 | }\r | |
146 | else {\r | |
147 | /* Initialize data */\r | |
148 | __sse2_available = 0;\r | |
149 | _fltused = 1;\r | |
150 | errno = 0;\r | |
151 | EFIerrno = 0;\r | |
152 | \r | |
8aa163da | 153 | gMD->ClocksPerSecond = 1;\r |
154 | gMD->AppStartTime = (clock_t)((UINT32)time(NULL));\r | |
2aa62f2b | 155 | \r |
156 | // Initialize file descriptors\r | |
157 | mfd = gMD->fdarray;\r | |
158 | for(i = 0; i < (FOPEN_MAX); ++i) {\r | |
159 | mfd[i].MyFD = (UINT16)i;\r | |
160 | }\r | |
161 | \r | |
53e1e5c6 | 162 | i = open("stdin:", O_RDONLY, 0444);\r |
163 | if(i == 0) {\r | |
164 | i = open("stdout:", O_WRONLY, 0222);\r | |
165 | if(i == 1) {\r | |
166 | i = open("stderr:", O_WRONLY, 0222);\r | |
2aa62f2b | 167 | }\r |
168 | }\r | |
53e1e5c6 | 169 | if(i != 2) {\r |
2aa62f2b | 170 | Print(L"ERROR Initializing Standard IO: %a.\n %r\n",\r |
171 | strerror(errno), EFIerrno);\r | |
172 | }\r | |
173 | \r | |
53e1e5c6 | 174 | /* Create mbcs versions of the Argv strings. */\r |
175 | nArgv = ArgvConvert(Argc, Argv);\r | |
176 | if(nArgv == NULL) {\r | |
177 | ExitVal = (INTN)RETURN_INVALID_PARAMETER;\r | |
2aa62f2b | 178 | }\r |
53e1e5c6 | 179 | else {\r |
d7ce7006 | 180 | if( setjmp(gMD->MainExit) == 0) {\r |
181 | ExitVal = (INTN)main( (int)Argc, gMD->NArgV);\r | |
182 | exitCleanup(ExitVal);\r | |
183 | }\r | |
184 | /* You reach here if:\r | |
185 | * normal return from main()\r | |
186 | * call to _Exit(), either directly or through exit().\r | |
187 | */\r | |
188 | ExitVal = (INTN)gMD->ExitValue;\r | |
189 | }\r | |
190 | \r | |
191 | if( ExitVal == EXIT_FAILURE) {\r | |
192 | ExitVal = RETURN_ABORTED;\r | |
193 | }\r | |
194 | \r | |
195 | /* Close any open files */\r | |
196 | for(i = OPEN_MAX - 1; i >= 0; --i) {\r | |
197 | (void)close(i); // Close properly handles closing a closed file.\r | |
198 | }\r | |
199 | \r | |
200 | /* Free the global MainData structure */\r | |
201 | if(gMD != NULL) {\r | |
202 | if(gMD->NCmdLine != NULL) {\r | |
203 | FreePool( gMD->NCmdLine );\r | |
204 | }\r | |
205 | FreePool( gMD );\r | |
2aa62f2b | 206 | }\r |
2aa62f2b | 207 | }\r |
208 | return ExitVal;\r | |
209 | }\r |