]> git.proxmox.com Git - mirror_edk2.git/blame - EdkUnixPkg/Sec/UnixThunk.c
Fix the wrong checking for UnixSerialIo
[mirror_edk2.git] / EdkUnixPkg / Sec / UnixThunk.c
CommitLineData
c9093a06 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
8ba2f441 92void
93msSleep (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
c9093a06 104void
105GetLocalTime (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
c9093a06 125static void
126TzSet (void)
127{
128 static int done = 0;
129 if (!done) {
130 tzset ();
131 done = 1;
132 }
133}
134
135long
136GetTimeZone(void)
137{
138 TzSet ();
139 return timezone;
140}
141
142int
143GetDayLight(void)
144{
145 TzSet ();
146 return daylight;
147}
148
149int
150GetErrno(void)
151{
152 return errno;
153}
154
155extern EFI_STATUS
8ef571df 156UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
c9093a06 157
158EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {\r
159 EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,\r
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};\r
193\r
194\r
195EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;\r