]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - EdkUnixPkg/Sec/UnixThunk.c
Unix version of EFI emulator
[mirror_edk2.git] / EdkUnixPkg / Sec / UnixThunk.c
... / ...
CommitLineData
1/*++\r
2\r
3Copyright (c) 2004 - 2006, Intel Corporation \r
4All rights reserved. This program and the accompanying materials \r
5are licensed and made available under the terms and conditions of the BSD License \r
6which accompanies this distribution. The full text of the license may be found at \r
7http://opensource.org/licenses/bsd-license.php \r
8 \r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
11\r
12Module Name:\r
13\r
14 UnixThunk.c\r
15\r
16Abstract:\r
17\r
18 Since the SEC is the only program in our emulation we \r
19 must use a Tiano mechanism to export APIs to other modules.\r
20 This is the role of the EFI_UNIX_THUNK_PROTOCOL.\r
21\r
22 The mUnixThunkTable exists so that a change to EFI_UNIX_THUNK_PROTOCOL\r
23 will cause an error in initializing the array if all the member functions\r
24 are not added. It looks like adding a element to end and not initializing\r
25 it may cause the table to be initaliized with the members at the end being\r
26 set to zero. This is bad as jumping to zero will crash.\r
27 \r
28\r
29 gUnix is a a public exported global that contains the initialized\r
30 data.\r
31\r
32--*/\r
33\r
34#include "SecMain.h"\r
35#include "Library/UnixLib.h"
36#include <sys/time.h>
37#include <time.h>
38#include <signal.h>
39#include <string.h>
40#include <stdlib.h>
41
42static int settimer_initialized;
43static struct timeval settimer_timeval;
44static void (*settimer_callback)(UINT64 delta);
45
46static void
47settimer_handler (int sig)
48{
49 struct timeval timeval;
50 UINT64 delta;
51
52 gettimeofday (&timeval, NULL);
53 delta = ((UINT64)timeval.tv_sec * 1000) + (timeval.tv_usec / 1000)
54 - ((UINT64)settimer_timeval.tv_sec * 1000)
55 - (settimer_timeval.tv_usec / 1000);
56 settimer_timeval = timeval;
57 if (settimer_callback)
58 (*settimer_callback)(delta);
59}
60
61static
62VOID
63SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
64{
65 struct itimerval timerval;
66
67 if (!settimer_initialized) {
68 struct sigaction act;
69
70 settimer_initialized = 1;
71 act.sa_handler = settimer_handler;
72 act.sa_flags = 0;
73 sigemptyset (&act.sa_mask);
74 if (sigaction (SIGALRM, &act, NULL) != 0) {
75 printf ("SetTimer: sigaction error %s\n", strerror (errno));
76 }
77 if (gettimeofday (&settimer_timeval, NULL) != 0) {
78 printf ("SetTimer: gettimeofday error %s\n", strerror (errno));
79 }
80 }
81 timerval.it_value.tv_sec = PeriodMs / 1000;
82 timerval.it_value.tv_usec = (PeriodMs % 1000) * 1000;
83 timerval.it_value.tv_sec = PeriodMs / 1000;
84 timerval.it_interval = timerval.it_value;
85
86 if (setitimer (ITIMER_REAL, &timerval, NULL) != 0) {
87 printf ("SetTimer: setitimer error %s\n", strerror (errno));
88 }
89 settimer_callback = CallBack;
90}
91
92void
93GetLocalTime (EFI_TIME *Time)
94{
95 struct tm *tm;
96 time_t t;
97
98 t = time (NULL);
99 tm = localtime (&t);
100
101 Time->Year = 1900 + tm->tm_year;
102 Time->Month = tm->tm_mon;
103 Time->Day = tm->tm_mday;
104 Time->Hour = tm->tm_hour;
105 Time->Minute = tm->tm_min;
106 Time->Second = tm->tm_sec;
107 Time->Nanosecond = 0;
108 Time->TimeZone = timezone;
109 Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
110 | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
111}
112
113void
114msSleep (unsigned long Milliseconds)
115{
116 usleep (Milliseconds * 1000);
117}
118
119static void
120TzSet (void)
121{
122 static int done = 0;
123 if (!done) {
124 tzset ();
125 done = 1;
126 }
127}
128
129long
130GetTimeZone(void)
131{
132 TzSet ();
133 return timezone;
134}
135
136int
137GetDayLight(void)
138{
139 TzSet ();
140 return daylight;
141}
142
143int
144GetErrno(void)
145{
146 return errno;
147}
148
149extern EFI_STATUS
150UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL *UgaIo, CONST CHAR16 *Title);
151
152EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {\r
153 EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,\r
154 msSleep, /* Sleep */
155 exit, /* Exit */
156 SetTimer,
157 GetLocalTime,
158 gmtime,
159 GetTimeZone,
160 GetDayLight,
161 (UnixPoll)poll,
162 (UnixRead)read,
163 (UnixWrite)write,
164 getenv,
165 (UnixOpen)open,
166 lseek,
167 ftruncate,
168 close,
169 mkdir,
170 rmdir,
171 unlink,
172 GetErrno,
173 opendir,
174 rewinddir,
175 readdir,
176 closedir,
177 stat,
178 statfs,
179 rename,
180 mktime,
181 fsync,
182 chmod,
183 utime,
184
185 UgaCreate,
186};\r
187\r
188\r
189EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;\r