]> git.proxmox.com Git - mirror_edk2.git/blob - UnixPkg/Sec/UnixThunk.c
8cf64b9d2e6dc45eb411384d324f4b81c5de5e89
[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 "Uefi.h"
36 #include "Library/UnixLib.h"
37
38 int settimer_initialized;
39 struct timeval settimer_timeval;
40 void (*settimer_callback)(UINT64 delta);
41
42 void
43 settimer_handler (int sig)
44 {
45 struct timeval timeval;
46 UINT64 delta;
47
48 gettimeofday (&timeval, NULL);
49 delta = ((UINT64)timeval.tv_sec * 1000) + (timeval.tv_usec / 1000)
50 - ((UINT64)settimer_timeval.tv_sec * 1000)
51 - (settimer_timeval.tv_usec / 1000);
52 settimer_timeval = timeval;
53 if (settimer_callback)
54 (*settimer_callback)(delta);
55 }
56
57 VOID
58 SetTimer (UINT64 PeriodMs, VOID (*CallBack)(UINT64 DeltaMs))
59 {
60 struct itimerval timerval;
61 UINT32 remainder;
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 }
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);
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
89 void
90 msSleep (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
101 void
102 GetLocalTime (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 + 1;
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
122 void
123 TzSet (void)
124 {
125 STATIC int done = 0;
126 if (!done) {
127 tzset ();
128 done = 1;
129 }
130 }
131
132 long
133 GetTimeZone(void)
134 {
135 TzSet ();
136 return timezone;
137 }
138
139 int
140 GetDayLight(void)
141 {
142 TzSet ();
143 return daylight;
144 }
145
146 int
147 GetErrno(void)
148 {
149 return errno;
150 }
151
152 extern EFI_STATUS
153 UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
154
155 EFI_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 dlopen,
197 dlerror,
198 dlsym
199 };
200
201
202 EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;