]> git.proxmox.com Git - mirror_edk2.git/blame - UnixPkg/Sec/UnixThunk.c
Integrate patch from Andrew Fish to make it run on OS X.
[mirror_edk2.git] / UnixPkg / Sec / UnixThunk.c
CommitLineData
804405e7 1/*++
2
ccd55824 3Copyright (c) 2004 - 2009, Intel Corporation
4Portions copyright (c) 2008-2009 Apple Inc. All rights reserved.
804405e7 5All rights reserved. This program and the accompanying materials
6are licensed and made available under the terms and conditions of the BSD License
7which accompanies this distribution. The full text of the license may be found at
8http://opensource.org/licenses/bsd-license.php
9
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13Module Name:
14
15 UnixThunk.c
16
17Abstract:
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"
b67f2798 36#include "Uefi.h"
804405e7 37#include "Library/UnixLib.h"
38
7492c63d 39int settimer_initialized;
40struct timeval settimer_timeval;
41void (*settimer_callback)(UINT64 delta);
804405e7 42
7492c63d 43void
804405e7 44settimer_handler (int sig)
45{
46 struct timeval timeval;
47 UINT64 delta;
48
49 gettimeofday (&timeval, NULL);
50 delta = ((UINT64)timeval.tv_sec * 1000) + (timeval.tv_usec / 1000)
51 - ((UINT64)settimer_timeval.tv_sec * 1000)
52 - (settimer_timeval.tv_usec / 1000);
53 settimer_timeval = timeval;
54 if (settimer_callback)
55 (*settimer_callback)(delta);
56}
57
804405e7 58VOID
59SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
60{
61 struct itimerval timerval;
73aa7f04 62 UINT32 remainder;
804405e7 63
64 if (!settimer_initialized) {
65 struct sigaction act;
66
67 settimer_initialized = 1;
68 act.sa_handler = settimer_handler;
69 act.sa_flags = 0;
70 sigemptyset (&act.sa_mask);
71 if (sigaction (SIGALRM, &act, NULL) != 0) {
72 printf ("SetTimer: sigaction error %s\n", strerror (errno));
73 }
74 if (gettimeofday (&settimer_timeval, NULL) != 0) {
75 printf ("SetTimer: gettimeofday error %s\n", strerror (errno));
76 }
77 }
73aa7f04 78 timerval.it_value.tv_sec = DivU64x32(PeriodMs, 1000);
79 DivU64x32Remainder(PeriodMs, 1000, &remainder);
80 timerval.it_value.tv_usec = remainder * 1000;
81 timerval.it_value.tv_sec = DivU64x32(PeriodMs, 1000);
804405e7 82 timerval.it_interval = timerval.it_value;
83
84 if (setitimer (ITIMER_REAL, &timerval, NULL) != 0) {
85 printf ("SetTimer: setitimer error %s\n", strerror (errno));
86 }
87 settimer_callback = CallBack;
88}
89
90void
91msSleep (unsigned long Milliseconds)
92{
93 struct timespec ts;
94
95 ts.tv_sec = Milliseconds / 1000;
96 ts.tv_nsec = (Milliseconds % 1000) * 1000000;
97
98 while (nanosleep (&ts, &ts) != 0 && errno == EINTR)
99 ;
100}
101
102void
103GetLocalTime (EFI_TIME *Time)
104{
105 struct tm *tm;
106 time_t t;
107
108 t = time (NULL);
109 tm = localtime (&t);
110
111 Time->Year = 1900 + tm->tm_year;
be5d189f 112 Time->Month = tm->tm_mon + 1;
804405e7 113 Time->Day = tm->tm_mday;
114 Time->Hour = tm->tm_hour;
115 Time->Minute = tm->tm_min;
116 Time->Second = tm->tm_sec;
117 Time->Nanosecond = 0;
118 Time->TimeZone = timezone;
119 Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
120 | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
121}
122
7492c63d 123void
804405e7 124TzSet (void)
125{
7492c63d 126 STATIC int done = 0;
804405e7 127 if (!done) {
128 tzset ();
129 done = 1;
130 }
131}
132
133long
134GetTimeZone(void)
135{
136 TzSet ();
137 return timezone;
138}
139
140int
141GetDayLight(void)
142{
143 TzSet ();
144 return daylight;
145}
146
147int
148GetErrno(void)
149{
150 return errno;
151}
152
ccd55824 153#if __APPLE__
154void GasketmsSleep (unsigned long Milliseconds);
155void Gasketexit (int status);
156void GasketSetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs));
157void GasketGetLocalTime (EFI_TIME *Time);
158struct tm *Gasketgmtime (const time_t *clock);
159long GasketGetTimeZone (void);
160int GasketGetDayLight (void);
161int Gasketpoll (struct pollfd *pfd, int nfds, int timeout);
162int Gasketread (int fd, void *buf, int count);
163int Gasketwrite (int fd, const void *buf, int count);
164char *Gasketgetenv (const char *name);
165int Gasketopen (const char *name, int flags, int mode);
166off_t Gasketlseek (int fd, off_t off, int whence);
167int Gasketftruncate (int fd, long int len);
168int Gasketclose (int fd);
169int Gasketmkdir (const char *pathname, mode_t mode);
170int Gasketrmdir (const char *pathname);
171int Gasketunlink (const char *pathname);
172int GasketGetErrno (void);
173DIR *Gasketopendir (const char *pathname);
174void *Gasketrewinddir (DIR *dir);
175struct dirent *Gasketreaddir (DIR *dir);
176int Gasketclosedir (DIR *dir);
177int Gasketstat (const char *path, struct stat *buf);
178int Gasketstatfs (const char *path, struct statfs *buf);
179int Gasketrename (const char *oldpath, const char *newpath);
180time_t Gasketmktime (struct tm *tm);
181int Gasketfsync (int fd);
182int Gasketchmod (const char *path, mode_t mode);
183int Gasketutime (const char *filename, const struct utimbuf *buf);
184int Gaskettcflush (int fildes, int queue_selector);
185EFI_STATUS GasketUgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
186void Gasketperror (__const char *__s);
187
188//
189// ... is always an int or pointer to device specific data structure
190//
191int Gasketioctl (int fd, unsigned long int __request, ...);
192int Gasketfcntl (int __fd, int __cmd, ...);
193
194int Gasketcfsetispeed (struct termios *__termios_p, speed_t __speed);
195int Gasketcfsetospeed (struct termios *__termios_p, speed_t __speed);
196int Gaskettcgetattr (int __fd, struct termios *__termios_p);
197int Gaskettcsetattr (int __fd, int __optional_actions, __const struct termios *__termios_p);
198int Gasketsigaction (int sig, const struct sigaction *act, struct sigaction *oact);
199int Gasketsetcontext (const ucontext_t *ucp);
200int Gasketgetcontext (ucontext_t *ucp);
201int Gasketsigemptyset (sigset_t *set);
202int Gasketsigaltstack (const stack_t *ss, stack_t *oss);
203
204RETURN_STATUS
205GasketUnixPeCoffGetEntryPoint (
206 IN VOID *Pe32Data,
207 IN OUT VOID **EntryPoint
208 );
209
210VOID
211GasketUnixPeCoffRelocateImageExtraAction (
212 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
213 );
214
215VOID
216GasketPeCoffLoaderUnloadImageExtraAction (
217 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
218 );
219
220#endif
221
804405e7 222extern EFI_STATUS
223UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
224
225EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
226 EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,
ccd55824 227#ifdef __APPLE__
228//
229// Mac OS X requires the stack to be 16-byte aligned for IA-32. So on an OS X build
230// we add an assembly wrapper that makes sure the stack ges aligned.
231// This has the nice benfit of being able to run EFI ABI code, like the EFI shell
232// that is checked in to source control in the OS X version of the emulator
233//
234 GasketmsSleep, /* Sleep */
235 Gasketexit, /* Exit */
236 GasketSetTimer,
237 GasketGetLocalTime,
238 Gasketgmtime,
239 GasketGetTimeZone,
240 GasketGetDayLight,
241 (UnixPoll)Gasketpoll,
242 (UnixRead)Gasketread,
243 (UnixWrite)Gasketwrite,
244 Gasketgetenv,
245 (UnixOpen)Gasketopen,
246 (UnixSeek)Gasketlseek,
247 (UnixFtruncate)Gasketftruncate,
248 Gasketclose,
249 Gasketmkdir,
250 Gasketrmdir,
251 Gasketunlink,
252 GasketGetErrno,
253 Gasketopendir,
254 (UnixRewindDir)Gasketrewinddir,
255 Gasketreaddir,
256 Gasketclosedir,
257 Gasketstat,
258 Gasketstatfs,
259 Gasketrename,
260 Gasketmktime,
261 Gasketfsync,
262 Gasketchmod,
263 Gasketutime,
264 Gaskettcflush,
265 GasketUgaCreate,
266 Gasketperror,
267 Gasketioctl,
268 Gasketfcntl,
269 Gasketcfsetispeed,
270 Gasketcfsetospeed,
271 Gaskettcgetattr,
272 Gaskettcsetattr,
273
274 dlopen, // Update me with a gasket
275 dlerror, // Update me with a gasket
276 dlsym, // Update me with a gasket
277
278 SecPeCoffGetEntryPoint, // Update me with a gasket
279 SecPeCoffRelocateImageExtraAction, // Update me with a gasket
280 SecPeCoffLoaderUnloadImageExtraAction // Update me with a gasket
281
282#else
804405e7 283 msSleep, /* Sleep */
284 exit, /* Exit */
285 SetTimer,
286 GetLocalTime,
287 gmtime,
288 GetTimeZone,
289 GetDayLight,
290 (UnixPoll)poll,
291 (UnixRead)read,
292 (UnixWrite)write,
293 getenv,
294 (UnixOpen)open,
ccd55824 295 (UnixSeek)lseek,
296 (UnixFtruncate)ftruncate,
804405e7 297 close,
298 mkdir,
299 rmdir,
300 unlink,
301 GetErrno,
302 opendir,
303 rewinddir,
304 readdir,
305 closedir,
306 stat,
307 statfs,
308 rename,
309 mktime,
310 fsync,
311 chmod,
312 utime,
313 tcflush,
314 UgaCreate,
315 perror,
316 ioctl,
317 fcntl,
318 cfsetispeed,
319 cfsetospeed,
320 tcgetattr,
398b646f 321 tcsetattr,
322 dlopen,
323 dlerror,
ccd55824 324 dlsym,
325 SecPeCoffGetEntryPoint,
326 SecPeCoffRelocateImageExtraAction,
327 SecPeCoffLoaderUnloadImageExtraAction
328#endif
804405e7 329};
330
331
332EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;