summaryrefslogtreecommitdiff
path: root/UnixPkg/Sec/UnixThunk.c
blob: c0f35d0729d59c094dd00576518085010f026e41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*++

Copyright (c) 2004 - 2009, Intel Corporation                                                         
Portions copyright (c) 2008-2009 Apple Inc. All rights reserved.
All rights reserved. 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.             

Module Name:

  UnixThunk.c

Abstract:

  Since the SEC is the only program in our emulation we 
  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.
  

  gUnix is a a public exported global that contains the initialized
  data.

--*/

#include "SecMain.h"
#include "Uefi.h"
#include "Library/UnixLib.h"

#ifdef __APPLE__
#include "Gasket.h"
#endif

int settimer_initialized;
struct timeval settimer_timeval;
void (*settimer_callback)(UINT64 delta);

void
settimer_handler (int sig)
{
  struct timeval timeval;
  UINT64 delta;

  gettimeofday (&timeval, NULL);
  delta = ((UINT64)timeval.tv_sec * 1000) + (timeval.tv_usec / 1000)
    - ((UINT64)settimer_timeval.tv_sec * 1000) 
    - (settimer_timeval.tv_usec / 1000);
  settimer_timeval = timeval;
  if (settimer_callback)
    (*settimer_callback)(delta);
}

VOID
SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
{
  struct itimerval timerval;
  UINT32 remainder;

  if (!settimer_initialized) {
    struct sigaction act;

    settimer_initialized = 1;
    act.sa_handler = settimer_handler;
    act.sa_flags = 0;
    sigemptyset (&act.sa_mask);
    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));
    }
  }
  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);
  timerval.it_interval = timerval.it_value;
  
  if (setitimer (ITIMER_REAL, &timerval, NULL) != 0) {
    printf ("SetTimer: setitimer error %s\n", strerror (errno));
  }
  settimer_callback = CallBack;
}

void
msSleep (unsigned long Milliseconds)
{
  struct timespec ts;

  ts.tv_sec = Milliseconds / 1000;
  ts.tv_nsec = (Milliseconds % 1000) * 1000000;

  while (nanosleep (&ts, &ts) != 0 && errno == EINTR)
    ;
}

void
GetLocalTime (EFI_TIME *Time)
{
  struct tm *tm;
  time_t t;

  t = time (NULL);
  tm = localtime (&t);

  Time->Year = 1900 + tm->tm_year;
  Time->Month = tm->tm_mon + 1;
  Time->Day = tm->tm_mday;
  Time->Hour = tm->tm_hour;
  Time->Minute = tm->tm_min;
  Time->Second = tm->tm_sec;
  Time->Nanosecond = 0;
  Time->TimeZone = timezone;
  Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
    | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
}

void
TzSet (void)
{
  STATIC int done = 0;
  if (!done) {
    tzset ();
    done = 1;
  }
}

long
GetTimeZone(void)
{
  TzSet ();
  return timezone;
}

int
GetDayLight(void)
{
  TzSet ();
  return daylight;
}

int
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,
  (UnixPoll)Gasketpoll,
  (UnixRead)Gasketread,
  (UnixWrite)Gasketwrite,
  Gasketgetenv,
  (UnixOpen)Gasketopen,
  (UnixSeek)Gasketlseek,
  (UnixFtruncate)Gasketftruncate,
  Gasketclose,
  Gasketmkdir,
  Gasketrmdir,
  Gasketunlink,
  GasketGetErrno,
  Gasketopendir,
  (UnixRewindDir)Gasketrewinddir,
  Gasketreaddir,
  Gasketclosedir,
  Gasketstat,
  Gasketstatfs,
  Gasketrename,
  Gasketmktime,
  Gasketfsync,
  Gasketchmod,
  Gasketutime,
  Gaskettcflush,
  GasketUgaCreate,
  Gasketperror,
  Gasketioctl,
  Gasketfcntl,
  Gasketcfsetispeed,
  Gasketcfsetospeed,
  Gaskettcgetattr,
  Gaskettcsetattr,
  
  dlopen,  // Update me with a gasket
  dlerror, // Update me with a gasket
  dlsym,   // Update me with a gasket

  SecPeCoffGetEntryPoint,                // Update me with a gasket
  SecPeCoffRelocateImageExtraAction,     // Update me with a gasket
  SecPeCoffLoaderUnloadImageExtraAction  // Update me with a gasket

#else
  msSleep, /* Sleep */
  exit, /* Exit */
  SetTimer,
  GetLocalTime,
  gmtime,
  GetTimeZone,
  GetDayLight,
  (UnixPoll)poll,
  (UnixRead)read,
  (UnixWrite)write,
  getenv,
  (UnixOpen)open,
  (UnixSeek)lseek,
  (UnixFtruncate)ftruncate,
  close,
  mkdir,
  rmdir,
  unlink,
  GetErrno,
  opendir,
  rewinddir,
  readdir,
  closedir,
  (UnixStat)stat,
  statfs,
  rename,
  mktime,
  fsync,
  chmod,
  utime,
  tcflush,
  UgaCreate,
  perror,
  ioctl,
  fcntl,
  cfsetispeed,
  cfsetospeed,
  tcgetattr,
  tcsetattr,
  dlopen,
  dlerror,
  dlsym,
  SecPeCoffGetEntryPoint,
  SecPeCoffRelocateImageExtraAction,
  SecPeCoffLoaderUnloadImageExtraAction
#endif
};


EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;