2008-06-30 07:08:49 +02:00
|
|
|
/*++
|
|
|
|
|
2010-04-28 14:31:45 +02:00
|
|
|
Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
|
2010-12-01 00:38:40 +01:00
|
|
|
Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
|
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
2008-06-30 07:08:49 +02:00
|
|
|
|
|
|
|
Module Name:
|
|
|
|
|
|
|
|
UnixThunk.c
|
|
|
|
|
|
|
|
Abstract:
|
|
|
|
|
2010-12-01 00:38:40 +01:00
|
|
|
Since the SEC is the only program in our emulation we
|
2008-06-30 07:08:49 +02:00
|
|
|
must use a Tiano mechanism to export APIs to other modules.
|
|
|
|
This is the role of the EFI_UNIX_THUNK_PROTOCOL.
|
|
|
|
|
|
|
|
The mUnixThunkTable exists so that a change to EFI_UNIX_THUNK_PROTOCOL
|
|
|
|
will cause an error in initializing the array if all the member functions
|
|
|
|
are not added. It looks like adding a element to end and not initializing
|
|
|
|
it may cause the table to be initaliized with the members at the end being
|
|
|
|
set to zero. This is bad as jumping to zero will crash.
|
2010-12-01 00:38:40 +01:00
|
|
|
|
2008-06-30 07:08:49 +02:00
|
|
|
|
|
|
|
gUnix is a a public exported global that contains the initialized
|
|
|
|
data.
|
|
|
|
|
|
|
|
--*/
|
|
|
|
|
|
|
|
#include "SecMain.h"
|
2009-07-13 07:14:09 +02:00
|
|
|
#include "Uefi.h"
|
2008-06-30 07:08:49 +02:00
|
|
|
#include "Library/UnixLib.h"
|
|
|
|
|
2010-10-13 09:06:42 +02:00
|
|
|
#if defined(__APPLE__) || defined(MDE_CPU_X64)
|
2009-12-06 03:00:37 +01:00
|
|
|
#include "Gasket.h"
|
|
|
|
#endif
|
|
|
|
|
2008-10-30 07:14:35 +01:00
|
|
|
int settimer_initialized;
|
|
|
|
struct timeval settimer_timeval;
|
|
|
|
void (*settimer_callback)(UINT64 delta);
|
2008-06-30 07:08:49 +02:00
|
|
|
|
2010-12-01 00:38:40 +01:00
|
|
|
BOOLEAN gEmulatorInterruptEnabled = FALSE;
|
|
|
|
|
|
|
|
|
2008-10-30 07:14:35 +01:00
|
|
|
void
|
2008-06-30 07:08:49 +02:00
|
|
|
settimer_handler (int sig)
|
|
|
|
{
|
|
|
|
struct timeval timeval;
|
|
|
|
UINT64 delta;
|
|
|
|
|
|
|
|
gettimeofday (&timeval, NULL);
|
|
|
|
delta = ((UINT64)timeval.tv_sec * 1000) + (timeval.tv_usec / 1000)
|
2010-12-01 00:38:40 +01:00
|
|
|
- ((UINT64)settimer_timeval.tv_sec * 1000)
|
2008-06-30 07:08:49 +02:00
|
|
|
- (settimer_timeval.tv_usec / 1000);
|
|
|
|
settimer_timeval = timeval;
|
2010-12-01 00:38:40 +01:00
|
|
|
|
2010-07-22 23:21:38 +02:00
|
|
|
if (settimer_callback) {
|
2010-10-13 09:06:42 +02:00
|
|
|
#if defined(__APPLE__) || defined(MDE_CPU_X64)
|
2010-08-08 21:07:14 +02:00
|
|
|
ReverseGasketUint64 (settimer_callback, delta);
|
2010-07-22 23:21:38 +02:00
|
|
|
#else
|
2010-12-01 00:38:40 +01:00
|
|
|
(*settimer_callback)(delta);
|
2010-07-22 23:21:38 +02:00
|
|
|
#endif
|
|
|
|
}
|
2008-06-30 07:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
|
|
|
|
{
|
|
|
|
struct itimerval timerval;
|
2008-07-16 07:28:50 +02:00
|
|
|
UINT32 remainder;
|
2008-06-30 07:08:49 +02:00
|
|
|
|
|
|
|
if (!settimer_initialized) {
|
|
|
|
struct sigaction act;
|
|
|
|
|
|
|
|
settimer_initialized = 1;
|
|
|
|
act.sa_handler = settimer_handler;
|
|
|
|
act.sa_flags = 0;
|
|
|
|
sigemptyset (&act.sa_mask);
|
2010-12-01 00:38:40 +01:00
|
|
|
gEmulatorInterruptEnabled = TRUE;
|
2008-06-30 07:08:49 +02:00
|
|
|
if (sigaction (SIGALRM, &act, NULL) != 0) {
|
|
|
|
printf ("SetTimer: sigaction error %s\n", strerror (errno));
|
|
|
|
}
|
|
|
|
if (gettimeofday (&settimer_timeval, NULL) != 0) {
|
|
|
|
printf ("SetTimer: gettimeofday error %s\n", strerror (errno));
|
|
|
|
}
|
|
|
|
}
|
2008-07-16 07:28:50 +02:00
|
|
|
timerval.it_value.tv_sec = DivU64x32(PeriodMs, 1000);
|
|
|
|
DivU64x32Remainder(PeriodMs, 1000, &remainder);
|
|
|
|
timerval.it_value.tv_usec = remainder * 1000;
|
|
|
|
timerval.it_value.tv_sec = DivU64x32(PeriodMs, 1000);
|
2008-06-30 07:08:49 +02:00
|
|
|
timerval.it_interval = timerval.it_value;
|
2010-12-01 00:38:40 +01:00
|
|
|
|
2008-06-30 07:08:49 +02:00
|
|
|
if (setitimer (ITIMER_REAL, &timerval, NULL) != 0) {
|
|
|
|
printf ("SetTimer: setitimer error %s\n", strerror (errno));
|
|
|
|
}
|
|
|
|
settimer_callback = CallBack;
|
|
|
|
}
|
|
|
|
|
2010-12-01 00:38:40 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
UnixEnableInterrupt (void)
|
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
gEmulatorInterruptEnabled = TRUE;
|
|
|
|
// Since SetTimer() uses SIGALRM we emulate turning on and off interrupts
|
|
|
|
// by enabling/disabling SIGALRM.
|
|
|
|
sigemptyset (&sigset);
|
|
|
|
sigaddset (&sigset, SIGALRM);
|
|
|
|
sigprocmask (SIG_UNBLOCK, &sigset, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixDisableInterrupt (void)
|
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
// Since SetTimer() uses SIGALRM we emulate turning on and off interrupts
|
|
|
|
// by enabling/disabling SIGALRM.
|
|
|
|
sigemptyset (&sigset);
|
|
|
|
sigaddset (&sigset, SIGALRM);
|
|
|
|
sigprocmask (SIG_BLOCK, &sigset, NULL);
|
|
|
|
gEmulatorInterruptEnabled = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOLEAN
|
|
|
|
UnixInterruptEanbled (void)
|
|
|
|
{
|
|
|
|
return gEmulatorInterruptEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-06-30 07:08:49 +02:00
|
|
|
void
|
|
|
|
msSleep (unsigned long Milliseconds)
|
|
|
|
{
|
2010-07-22 23:21:38 +02:00
|
|
|
struct timespec rq, rm;
|
2008-06-30 07:08:49 +02:00
|
|
|
|
2010-07-22 23:21:38 +02:00
|
|
|
rq.tv_sec = Milliseconds / 1000;
|
|
|
|
rq.tv_nsec = (Milliseconds % 1000) * 1000000;
|
2008-06-30 07:08:49 +02:00
|
|
|
|
2010-07-22 23:21:38 +02:00
|
|
|
while (nanosleep (&rq, &rm) != -1) {
|
|
|
|
if (errno != EINTR) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rq = rm;
|
2010-12-01 00:38:40 +01:00
|
|
|
}
|
2010-10-13 09:06:42 +02:00
|
|
|
|
2008-06-30 07:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetLocalTime (EFI_TIME *Time)
|
|
|
|
{
|
|
|
|
struct tm *tm;
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
t = time (NULL);
|
|
|
|
tm = localtime (&t);
|
|
|
|
|
|
|
|
Time->Year = 1900 + tm->tm_year;
|
2008-09-18 03:49:46 +02:00
|
|
|
Time->Month = tm->tm_mon + 1;
|
2008-06-30 07:08:49 +02:00
|
|
|
Time->Day = tm->tm_mday;
|
|
|
|
Time->Hour = tm->tm_hour;
|
|
|
|
Time->Minute = tm->tm_min;
|
|
|
|
Time->Second = tm->tm_sec;
|
|
|
|
Time->Nanosecond = 0;
|
2010-08-18 22:24:08 +02:00
|
|
|
Time->TimeZone = GetTimeZone ();
|
2008-06-30 07:08:49 +02:00
|
|
|
Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
|
|
|
|
| (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
|
|
|
|
}
|
|
|
|
|
2008-10-30 07:14:35 +01:00
|
|
|
void
|
2008-06-30 07:08:49 +02:00
|
|
|
TzSet (void)
|
|
|
|
{
|
2008-10-30 07:14:35 +01:00
|
|
|
STATIC int done = 0;
|
2008-06-30 07:08:49 +02:00
|
|
|
if (!done) {
|
|
|
|
tzset ();
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
GetTimeZone(void)
|
|
|
|
{
|
|
|
|
TzSet ();
|
|
|
|
return timezone;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
GetDayLight(void)
|
|
|
|
{
|
|
|
|
TzSet ();
|
|
|
|
return daylight;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
GetErrno(void)
|
|
|
|
{
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2009-08-25 09:29:13 +02:00
|
|
|
|
2008-06-30 07:08:49 +02:00
|
|
|
extern EFI_STATUS
|
|
|
|
UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
|
|
|
|
|
|
|
|
EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
|
|
|
|
EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,
|
2010-10-13 09:06:42 +02:00
|
|
|
#if defined(__APPLE__) || defined(MDE_CPU_X64)
|
2009-08-25 09:29:13 +02:00
|
|
|
//
|
|
|
|
// Mac OS X requires the stack to be 16-byte aligned for IA-32. So on an OS X build
|
2010-12-01 00:38:40 +01:00
|
|
|
// we add an assembly wrapper that makes sure the stack ges aligned.
|
2009-08-25 09:29:13 +02:00
|
|
|
// This has the nice benfit of being able to run EFI ABI code, like the EFI shell
|
|
|
|
// that is checked in to source control in the OS X version of the emulator
|
|
|
|
//
|
|
|
|
GasketmsSleep, /* Sleep */
|
|
|
|
Gasketexit, /* Exit */
|
|
|
|
GasketSetTimer,
|
|
|
|
GasketGetLocalTime,
|
|
|
|
Gasketgmtime,
|
|
|
|
GasketGetTimeZone,
|
|
|
|
GasketGetDayLight,
|
2010-07-22 23:21:38 +02:00
|
|
|
Gasketpoll,
|
|
|
|
Gasketread,
|
|
|
|
Gasketwrite,
|
2009-08-25 09:29:13 +02:00
|
|
|
Gasketgetenv,
|
2010-07-22 23:21:38 +02:00
|
|
|
Gasketopen,
|
|
|
|
Gasketlseek,
|
|
|
|
Gasketftruncate,
|
2009-08-25 09:29:13 +02:00
|
|
|
Gasketclose,
|
|
|
|
Gasketmkdir,
|
|
|
|
Gasketrmdir,
|
|
|
|
Gasketunlink,
|
|
|
|
GasketGetErrno,
|
|
|
|
Gasketopendir,
|
2010-07-22 23:21:38 +02:00
|
|
|
Gasketrewinddir,
|
2009-08-25 09:29:13 +02:00
|
|
|
Gasketreaddir,
|
|
|
|
Gasketclosedir,
|
|
|
|
Gasketstat,
|
|
|
|
Gasketstatfs,
|
|
|
|
Gasketrename,
|
|
|
|
Gasketmktime,
|
|
|
|
Gasketfsync,
|
|
|
|
Gasketchmod,
|
|
|
|
Gasketutime,
|
|
|
|
Gaskettcflush,
|
|
|
|
GasketUgaCreate,
|
|
|
|
Gasketperror,
|
|
|
|
Gasketioctl,
|
|
|
|
Gasketfcntl,
|
|
|
|
Gasketcfsetispeed,
|
|
|
|
Gasketcfsetospeed,
|
|
|
|
Gaskettcgetattr,
|
|
|
|
Gaskettcsetattr,
|
2010-12-01 00:38:40 +01:00
|
|
|
GasketUnixPeCoffGetEntryPoint,
|
|
|
|
GasketUnixPeCoffRelocateImageExtraAction,
|
|
|
|
GasketUnixPeCoffUnloadImageExtraAction,
|
|
|
|
|
|
|
|
GasketUnixEnableInterrupt,
|
|
|
|
GasketUnixDisableInterrupt,
|
|
|
|
|
|
|
|
Gasketgetifaddrs,
|
|
|
|
Gasketfreeifaddrs,
|
|
|
|
Gasketsocket,
|
2009-08-25 09:29:13 +02:00
|
|
|
|
|
|
|
#else
|
2008-06-30 07:08:49 +02:00
|
|
|
msSleep, /* Sleep */
|
|
|
|
exit, /* Exit */
|
|
|
|
SetTimer,
|
|
|
|
GetLocalTime,
|
|
|
|
gmtime,
|
|
|
|
GetTimeZone,
|
|
|
|
GetDayLight,
|
|
|
|
(UnixPoll)poll,
|
|
|
|
(UnixRead)read,
|
|
|
|
(UnixWrite)write,
|
|
|
|
getenv,
|
|
|
|
(UnixOpen)open,
|
2009-08-25 09:29:13 +02:00
|
|
|
(UnixSeek)lseek,
|
|
|
|
(UnixFtruncate)ftruncate,
|
2008-06-30 07:08:49 +02:00
|
|
|
close,
|
|
|
|
mkdir,
|
|
|
|
rmdir,
|
|
|
|
unlink,
|
|
|
|
GetErrno,
|
|
|
|
opendir,
|
|
|
|
rewinddir,
|
|
|
|
readdir,
|
|
|
|
closedir,
|
2009-12-06 03:00:37 +01:00
|
|
|
(UnixStat)stat,
|
2008-06-30 07:08:49 +02:00
|
|
|
statfs,
|
|
|
|
rename,
|
|
|
|
mktime,
|
|
|
|
fsync,
|
|
|
|
chmod,
|
|
|
|
utime,
|
|
|
|
tcflush,
|
|
|
|
UgaCreate,
|
|
|
|
perror,
|
|
|
|
ioctl,
|
|
|
|
fcntl,
|
|
|
|
cfsetispeed,
|
|
|
|
cfsetospeed,
|
|
|
|
tcgetattr,
|
2009-03-05 10:20:08 +01:00
|
|
|
tcsetattr,
|
2009-08-25 09:29:13 +02:00
|
|
|
SecPeCoffGetEntryPoint,
|
|
|
|
SecPeCoffRelocateImageExtraAction,
|
2010-12-01 00:38:40 +01:00
|
|
|
SecPeCoffLoaderUnloadImageExtraAction,
|
|
|
|
UnixEnableInterrupt,
|
|
|
|
UnixDisableInterrupt,
|
|
|
|
getifaddrs,
|
|
|
|
freeifaddrs,
|
|
|
|
socket
|
2009-08-25 09:29:13 +02:00
|
|
|
#endif
|
2008-06-30 07:08:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;
|