]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UnixPkg/Sec/UnixThunk.c
Port UnixPkg to also support X64. Currently only supports Unix x86_64 ABI. In the...
[mirror_edk2.git] / UnixPkg / Sec / UnixThunk.c
index 44dfb1682fb14cce555a2a14f9a225acd84ded1b..08cdd6215708292901acdd9f541e87b7231f5059 100644 (file)
@@ -1,7 +1,8 @@
 /*++
 
-Copyright (c) 2004 - 2006, Intel Corporation                                                         
-All rights reserved. This program and the accompanying materials                          
+Copyright (c) 2004 - 2009, Intel Corporation. All rights reserved.<BR>
+Portions copyright (c) 2008 - 2009, 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                                            
@@ -32,13 +33,18 @@ Abstract:
 --*/
 
 #include "SecMain.h"
+#include "Uefi.h"
 #include "Library/UnixLib.h"
 
-static int settimer_initialized;
-static struct timeval settimer_timeval;
-static void (*settimer_callback)(UINT64 delta);
+#ifdef __APPLE__
+#include "Gasket.h"
+#endif
 
-static void
+int settimer_initialized;
+struct timeval settimer_timeval;
+void (*settimer_callback)(UINT64 delta);
+
+void
 settimer_handler (int sig)
 {
   struct timeval timeval;
@@ -49,11 +55,16 @@ settimer_handler (int sig)
     - ((UINT64)settimer_timeval.tv_sec * 1000) 
     - (settimer_timeval.tv_usec / 1000);
   settimer_timeval = timeval;
-  if (settimer_callback)
-    (*settimer_callback)(delta);
+  
+  if (settimer_callback) {
+#ifdef __APPLE__
+    ReverseGasketUint64 (settimer_callback, delta);
+#else
+   (*settimer_callback)(delta);
+#endif
+  }
 }
 
-static
 VOID
 SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
 {
@@ -89,13 +100,18 @@ SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
 void
 msSleep (unsigned long Milliseconds)
 {
-  struct timespec ts;
+  struct timespec rq, rm;
 
-  ts.tv_sec = Milliseconds / 1000;
-  ts.tv_nsec = (Milliseconds % 1000) * 1000000;
+  rq.tv_sec = Milliseconds / 1000;
+  rq.tv_nsec = (Milliseconds % 1000) * 1000000;
 
-  while (nanosleep (&ts, &ts) != 0 && errno == EINTR)
-    ;
+  while (nanosleep (&rq, &rm) != -1) {
+    if (errno != EINTR) {
+      break;
+    }
+    rq = rm;
+  } 
+    
 }
 
 void
@@ -108,7 +124,7 @@ GetLocalTime (EFI_TIME *Time)
   tm = localtime (&t);
 
   Time->Year = 1900 + tm->tm_year;
-  Time->Month = tm->tm_mon;
+  Time->Month = tm->tm_mon + 1;
   Time->Day = tm->tm_mday;
   Time->Hour = tm->tm_hour;
   Time->Minute = tm->tm_min;
@@ -119,10 +135,10 @@ GetLocalTime (EFI_TIME *Time)
     | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
 }
 
-static void
+void
 TzSet (void)
 {
-  static int done = 0;
+  STATIC int done = 0;
   if (!done) {
     tzset ();
     done = 1;
@@ -149,11 +165,63 @@ GetErrno(void)
   return errno;
 }
 
+
 extern EFI_STATUS
 UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
 
 EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
   EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,
+#ifdef __APPLE__
+//
+// Mac OS X requires the stack to be 16-byte aligned for IA-32. So on an OS X build
+// we add an assembly wrapper that makes sure the stack ges aligned. 
+// 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,
+  Gasketpoll,
+  Gasketread,
+  Gasketwrite,
+  Gasketgetenv,
+  Gasketopen,
+  Gasketlseek,
+  Gasketftruncate,
+  Gasketclose,
+  Gasketmkdir,
+  Gasketrmdir,
+  Gasketunlink,
+  GasketGetErrno,
+  Gasketopendir,
+  Gasketrewinddir,
+  Gasketreaddir,
+  Gasketclosedir,
+  Gasketstat,
+  Gasketstatfs,
+  Gasketrename,
+  Gasketmktime,
+  Gasketfsync,
+  Gasketchmod,
+  Gasketutime,
+  Gaskettcflush,
+  GasketUgaCreate,
+  Gasketperror,
+  Gasketioctl,
+  Gasketfcntl,
+  Gasketcfsetispeed,
+  Gasketcfsetospeed,
+  Gaskettcgetattr,
+  Gaskettcsetattr,
+  GasketUnixPeCoffGetEntryPoint,                
+  GasketUnixPeCoffRelocateImageExtraAction,     
+  GasketUnixPeCoffUnloadImageExtraAction  
+
+#else
   msSleep, /* Sleep */
   exit, /* Exit */
   SetTimer,
@@ -166,8 +234,8 @@ EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
   (UnixWrite)write,
   getenv,
   (UnixOpen)open,
-  lseek,
-  ftruncate,
+  (UnixSeek)lseek,
+  (UnixFtruncate)ftruncate,
   close,
   mkdir,
   rmdir,
@@ -177,7 +245,7 @@ EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
   rewinddir,
   readdir,
   closedir,
-  stat,
+  (UnixStat)stat,
   statfs,
   rename,
   mktime,
@@ -192,7 +260,11 @@ EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
   cfsetispeed,
   cfsetospeed,
   tcgetattr,
-  tcsetattr
+  tcsetattr,
+  SecPeCoffGetEntryPoint,
+  SecPeCoffRelocateImageExtraAction,
+  SecPeCoffLoaderUnloadImageExtraAction
+#endif
 };