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