]> git.proxmox.com Git - mirror_edk2.git/blob - EdkUnixPkg/Sec/UnixThunk.c
Remove USER_DEFINED in IncludePkgHeader in spd files as it does not make sense to...
[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 GetLocalTime (EFI_TIME *Time)
94 {
95 struct tm *tm;
96 time_t t;
97
98 t = time (NULL);
99 tm = localtime (&t);
100
101 Time->Year = 1900 + tm->tm_year;
102 Time->Month = tm->tm_mon;
103 Time->Day = tm->tm_mday;
104 Time->Hour = tm->tm_hour;
105 Time->Minute = tm->tm_min;
106 Time->Second = tm->tm_sec;
107 Time->Nanosecond = 0;
108 Time->TimeZone = timezone;
109 Time->Daylight = (daylight ? EFI_TIME_ADJUST_DAYLIGHT : 0)
110 | (tm->tm_isdst > 0 ? EFI_TIME_IN_DAYLIGHT : 0);
111 }
112
113 void
114 msSleep (unsigned long Milliseconds)
115 {
116 usleep (Milliseconds * 1000);
117 }
118
119 static void
120 TzSet (void)
121 {
122 static int done = 0;
123 if (!done) {
124 tzset ();
125 done = 1;
126 }
127 }
128
129 long
130 GetTimeZone(void)
131 {
132 TzSet ();
133 return timezone;
134 }
135
136 int
137 GetDayLight(void)
138 {
139 TzSet ();
140 return daylight;
141 }
142
143 int
144 GetErrno(void)
145 {
146 return errno;
147 }
148
149 extern EFI_STATUS
150 UgaCreate(struct _EFI_UNIX_UGA_IO_PROTOCOL **UgaIo, CONST CHAR16 *Title);
151
152 EFI_UNIX_THUNK_PROTOCOL mUnixThunkTable = {
153 EFI_UNIX_THUNK_PROTOCOL_SIGNATURE,
154 msSleep, /* Sleep */
155 exit, /* Exit */
156 SetTimer,
157 GetLocalTime,
158 gmtime,
159 GetTimeZone,
160 GetDayLight,
161 (UnixPoll)poll,
162 (UnixRead)read,
163 (UnixWrite)write,
164 getenv,
165 (UnixOpen)open,
166 lseek,
167 ftruncate,
168 close,
169 mkdir,
170 rmdir,
171 unlink,
172 GetErrno,
173 opendir,
174 rewinddir,
175 readdir,
176 closedir,
177 stat,
178 statfs,
179 rename,
180 mktime,
181 fsync,
182 chmod,
183 utime,
184
185 UgaCreate,
186 };
187
188
189 EFI_UNIX_THUNK_PROTOCOL *gUnix = &mUnixThunkTable;