]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/Sec/UnixThunk.c
252c7e62a4c794fba30de8bb394e5ed237180f38
[mirror_edk2.git] / UnixPkg / Sec / UnixThunk.c
1 /*++
2
3 Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
4 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 Module Name:
14
15 UnixThunk.c
16
17 Abstract:
18
19 Since the SEC is the only program in our emulation we
20 must use a Tiano mechanism to export APIs to other modules.
21 This is the role of the EFI_UNIX_THUNK_PROTOCOL.
22
23 The mUnixThunkTable exists so that a change to EFI_UNIX_THUNK_PROTOCOL
24 will cause an error in initializing the array if all the member functions
25 are not added. It looks like adding a element to end and not initializing
26 it may cause the table to be initaliized with the members at the end being
27 set to zero. This is bad as jumping to zero will crash.
28
29
30 gUnix is a a public exported global that contains the initialized
31 data.
32
33 --*/
34
35 #include "SecMain.h"
36 #include "Uefi.h"
37 #include "Library/UnixLib.h"
38
39 #ifdef __APPLE__
40 #include "Gasket.h"
41 #endif
42
43 int settimer_initialized;
44 struct timeval settimer_timeval;
45 void (*settimer_callback)(UINT64 delta);
46
47 void
48 settimer_handler (int sig)
49 {
50 struct timeval timeval;
51 UINT64 delta;
52
53 gettimeofday (&timeval, NULL);
54 delta = ((UINT64)timeval.tv_sec * 1000) + (timeval.tv_usec / 1000)
55 - ((UINT64)settimer_timeval.tv_sec * 1000)
56 - (settimer_timeval.tv_usec / 1000);
57 settimer_timeval = timeval;
58 if (settimer_callback)
59 (*settimer_callback)(delta);
60 }
61
62 VOID
63 SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
64 {
65 struct itimerval timerval;
66 UINT32 remainder;
67
68 if (!settimer_initialized) {
69 struct sigaction act;
70
71 settimer_initialized = 1;
72 act.sa_handler = settimer_handler;
73 act.sa_flags = 0;
74 sigemptyset (&act.sa_mask);
75 if (sigaction (SIGALRM, &act, NULL) != 0) {
76 printf ("SetTimer: sigaction error %s\n", strerror (errno));
77 }
78 if (gettimeofday (&settimer_timeval, NULL) != 0) {
79 printf ("SetTimer: gettimeofday error %s\n", strerror (errno));
80 }
81 }
82 timerval.it_value.tv_sec = DivU64x32(PeriodMs, 1000);
83 DivU64x32Remainder(PeriodMs, 1000, &remainder);
84 timerval.it_value.tv_usec = remainder * 1000;
85 timerval.it_value.tv_sec = DivU64x32(PeriodMs, 1000);
86 timerval.it_interval = timerval.it_value;
87
88 if (setitimer (ITIMER_REAL, &timerval, NULL) != 0) {
89 printf ("SetTimer: setitimer error %s\n", strerror (errno));
90 }
91 settimer_callback = CallBack;
92 }
93
94 void
95 msSleep (unsigned long Milliseconds)
96 {
97 struct timespec ts;
98
99 ts.tv_sec = Milliseconds / 1000;
100 ts.tv_nsec = (Milliseconds % 1000) * 1000000;
101
102 while (nanosleep (&ts, &ts) != 0 && errno == EINTR)
103 ;
104 }
105
106 void
107 GetLocalTime (EFI_TIME *Time)
108 {
109 struct tm *tm;
110 time_t t;
111
112 t = time (NULL);
113 tm = localtime (&t);
114
115 Time->Year = 1900 + tm->tm_year;
116 Time->Month = tm->tm_mon + 1;
117 Time->Day = tm->tm_mday;
118 Time->Hour = tm->tm_hour;
119 Time->Minute = tm->tm_min;
120 Time->Second = tm->tm_sec;
121 Time->Nanosecond = 0;
122 Time->TimeZone = timezone;
123 Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
124 | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
125 }
126
127 void
128 TzSet (void)
129 {
130 STATIC int done = 0;
131 if (!done) {
132 tzset ();
133 done = 1;
134 }
135 }
136
137 long
138 GetTimeZone(void)
139 {
140 TzSet ();
141 return timezone;
142 }
143
144 int
145 GetDayLight(void)
146 {
147 TzSet ();
148 return daylight;
149 }
150
151 int
152 GetErrno(void)
153 {
154 return errno;
155 }
156
157
158 extern EFI_STATUS
159 UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
160
161 EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
162 EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,
163 #ifdef __APPLE__
164 //
165 // Mac OS X requires the stack to be 16-byte aligned for IA-32. So on an OS X build
166 // we add an assembly wrapper that makes sure the stack ges aligned.
167 // This has the nice benfit of being able to run EFI ABI code, like the EFI shell
168 // that is checked in to source control in the OS X version of the emulator
169 //
170 GasketmsSleep, /* Sleep */
171 Gasketexit, /* Exit */
172 GasketSetTimer,
173 GasketGetLocalTime,
174 Gasketgmtime,
175 GasketGetTimeZone,
176 GasketGetDayLight,
177 (UnixPoll)Gasketpoll,
178 (UnixRead)Gasketread,
179 (UnixWrite)Gasketwrite,
180 Gasketgetenv,
181 (UnixOpen)Gasketopen,
182 (UnixSeek)Gasketlseek,
183 (UnixFtruncate)Gasketftruncate,
184 Gasketclose,
185 Gasketmkdir,
186 Gasketrmdir,
187 Gasketunlink,
188 GasketGetErrno,
189 Gasketopendir,
190 (UnixRewindDir)Gasketrewinddir,
191 Gasketreaddir,
192 Gasketclosedir,
193 Gasketstat,
194 Gasketstatfs,
195 Gasketrename,
196 Gasketmktime,
197 Gasketfsync,
198 Gasketchmod,
199 Gasketutime,
200 Gaskettcflush,
201 GasketUgaCreate,
202 Gasketperror,
203 Gasketioctl,
204 Gasketfcntl,
205 Gasketcfsetispeed,
206 Gasketcfsetospeed,
207 Gaskettcgetattr,
208 Gaskettcsetattr,
209
210 dlopen, // Update me with a gasket
211 dlerror, // Update me with a gasket
212 dlsym, // Update me with a gasket
213
214 SecPeCoffGetEntryPoint, // Update me with a gasket
215 SecPeCoffRelocateImageExtraAction, // Update me with a gasket
216 SecPeCoffLoaderUnloadImageExtraAction // Update me with a gasket
217
218 #else
219 msSleep, /* Sleep */
220 exit, /* Exit */
221 SetTimer,
222 GetLocalTime,
223 gmtime,
224 GetTimeZone,
225 GetDayLight,
226 (UnixPoll)poll,
227 (UnixRead)read,
228 (UnixWrite)write,
229 getenv,
230 (UnixOpen)open,
231 (UnixSeek)lseek,
232 (UnixFtruncate)ftruncate,
233 close,
234 mkdir,
235 rmdir,
236 unlink,
237 GetErrno,
238 opendir,
239 rewinddir,
240 readdir,
241 closedir,
242 (UnixStat)stat,
243 statfs,
244 rename,
245 mktime,
246 fsync,
247 chmod,
248 utime,
249 tcflush,
250 UgaCreate,
251 perror,
252 ioctl,
253 fcntl,
254 cfsetispeed,
255 cfsetospeed,
256 tcgetattr,
257 tcsetattr,
258 dlopen,
259 dlerror,
260 dlsym,
261 SecPeCoffGetEntryPoint,
262 SecPeCoffRelocateImageExtraAction,
263 SecPeCoffLoaderUnloadImageExtraAction
264 #endif
265 };
266
267
268 EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;