pair RaiseTPL with RestoreTPL
[mirror_edk2.git] / EdkUnixPkg / Sec / UnixThunk.c
1 /*++
2
3 Copyright (c) 2004 - 2006, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 UnixThunk.c
15
16 Abstract:
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 #include <sys/time.h>
37 #include <time.h>
38 #include <signal.h>
39 #include <string.h>
40 #include <stdlib.h>
41
42 static int settimer_initialized;
43 static struct timeval settimer_timeval;
44 static void (*settimer_callback)(UINT64 delta);
45
46 static void
47 settimer_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
61 static
62 VOID
63 SetTimer (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
92 void
93 msSleep (unsigned long Milliseconds)
94 {
95 struct timespec ts;
96
97 ts.tv_sec = Milliseconds / 1000;
98 ts.tv_nsec = (Milliseconds % 1000) * 1000000;
99
100 while (nanosleep (&ts, &ts) != 0 && errno == EINTR)
101 ;
102 }
103
104 void
105 GetLocalTime (EFI_TIME *Time)
106 {
107 struct tm *tm;
108 time_t t;
109
110 t = time (NULL);
111 tm = localtime (&t);
112
113 Time->Year = 1900 + tm->tm_year;
114 Time->Month = tm->tm_mon;
115 Time->Day = tm->tm_mday;
116 Time->Hour = tm->tm_hour;
117 Time->Minute = tm->tm_min;
118 Time->Second = tm->tm_sec;
119 Time->Nanosecond = 0;
120 Time->TimeZone = timezone;
121 Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
122 | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
123 }
124
125 static void
126 TzSet (void)
127 {
128 static int done = 0;
129 if (!done) {
130 tzset ();
131 done = 1;
132 }
133 }
134
135 long
136 GetTimeZone(void)
137 {
138 TzSet ();
139 return timezone;
140 }
141
142 int
143 GetDayLight(void)
144 {
145 TzSet ();
146 return daylight;
147 }
148
149 int
150 GetErrno(void)
151 {
152 return errno;
153 }
154
155 extern EFI_STATUS
156 UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
157
158 EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
159 EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,
160 msSleep, /* Sleep */
161 exit, /* Exit */
162 SetTimer,
163 GetLocalTime,
164 gmtime,
165 GetTimeZone,
166 GetDayLight,
167 (UnixPoll)poll,
168 (UnixRead)read,
169 (UnixWrite)write,
170 getenv,
171 (UnixOpen)open,
172 lseek,
173 ftruncate,
174 close,
175 mkdir,
176 rmdir,
177 unlink,
178 GetErrno,
179 opendir,
180 rewinddir,
181 readdir,
182 closedir,
183 stat,
184 statfs,
185 rename,
186 mktime,
187 fsync,
188 chmod,
189 utime,
190
191 UgaCreate,
192 };
193
194
195 EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;