]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/Sec/UnixThunk.c
500568ad52e6ea1479eb984a2c7732c505b680e2
[mirror_edk2.git] / UnixPkg / 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
37 static int settimer_initialized;
38 static struct timeval settimer_timeval;
39 static void (*settimer_callback)(UINT64 delta);
40
41 static void
42 settimer_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
56 static
57 VOID
58 SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
59 {
60 struct itimerval timerval;
61
62 if (!settimer_initialized) {
63 struct sigaction act;
64
65 settimer_initialized = 1;
66 act.sa_handler = settimer_handler;
67 act.sa_flags = 0;
68 sigemptyset (&act.sa_mask);
69 if (sigaction (SIGALRM, &act, NULL) != 0) {
70 printf ("SetTimer: sigaction error %s\n", strerror (errno));
71 }
72 if (gettimeofday (&settimer_timeval, NULL) != 0) {
73 printf ("SetTimer: gettimeofday error %s\n", strerror (errno));
74 }
75 }
76 timerval.it_value.tv_sec = PeriodMs / 1000;
77 timerval.it_value.tv_usec = (PeriodMs % 1000) * 1000;
78 timerval.it_value.tv_sec = PeriodMs / 1000;
79 timerval.it_interval = timerval.it_value;
80
81 if (setitimer (ITIMER_REAL, &timerval, NULL) != 0) {
82 printf ("SetTimer: setitimer error %s\n", strerror (errno));
83 }
84 settimer_callback = CallBack;
85 }
86
87 void
88 msSleep (unsigned long Milliseconds)
89 {
90 struct timespec ts;
91
92 ts.tv_sec = Milliseconds / 1000;
93 ts.tv_nsec = (Milliseconds % 1000) * 1000000;
94
95 while (nanosleep (&ts, &ts) != 0 && errno == EINTR)
96 ;
97 }
98
99 void
100 GetLocalTime (EFI_TIME *Time)
101 {
102 struct tm *tm;
103 time_t t;
104
105 t = time (NULL);
106 tm = localtime (&t);
107
108 Time->Year = 1900 + tm->tm_year;
109 Time->Month = tm->tm_mon;
110 Time->Day = tm->tm_mday;
111 Time->Hour = tm->tm_hour;
112 Time->Minute = tm->tm_min;
113 Time->Second = tm->tm_sec;
114 Time->Nanosecond = 0;
115 Time->TimeZone = timezone;
116 Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
117 | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
118 }
119
120 static void
121 TzSet (void)
122 {
123 static int done = 0;
124 if (!done) {
125 tzset ();
126 done = 1;
127 }
128 }
129
130 long
131 GetTimeZone(void)
132 {
133 TzSet ();
134 return timezone;
135 }
136
137 int
138 GetDayLight(void)
139 {
140 TzSet ();
141 return daylight;
142 }
143
144 int
145 GetErrno(void)
146 {
147 return errno;
148 }
149
150 extern EFI_STATUS
151 UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
152
153 EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
154 EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,
155 msSleep, /* Sleep */
156 exit, /* Exit */
157 SetTimer,
158 GetLocalTime,
159 gmtime,
160 GetTimeZone,
161 GetDayLight,
162 (UnixPoll)poll,
163 (UnixRead)read,
164 (UnixWrite)write,
165 getenv,
166 (UnixOpen)open,
167 lseek,
168 ftruncate,
169 close,
170 mkdir,
171 rmdir,
172 unlink,
173 GetErrno,
174 opendir,
175 rewinddir,
176 readdir,
177 closedir,
178 stat,
179 statfs,
180 rename,
181 mktime,
182 fsync,
183 chmod,
184 utime,
185 tcflush,
186 UgaCreate,
187 perror,
188 ioctl,
189 fcntl,
190 cfsetispeed,
191 cfsetospeed,
192 tcgetattr,
193 tcsetattr
194 };
195
196
197 EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;