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