]> git.proxmox.com Git - mirror_edk2.git/blobdiff - InOsEmuPkg/Unix/Sec/EmuThunk.c
InOsEmuPkg: Add TimerLib for PEI, DXE_CORE, and DXE/EFI drivers/applications.
[mirror_edk2.git] / InOsEmuPkg / Unix / Sec / EmuThunk.c
index abae70b89abcc805fca513a6622d49ef06fcb07f..194cf137f93fe8531d9ee2b21190e403952d6eaa 100644 (file)
@@ -49,12 +49,75 @@ SecWriteStdErr (
 {
   ssize_t Return;
   
-  Return = write (1, (const void *)Buffer, (size_t)NumberOfBytes);
+  Return = write (STDERR_FILENO, (const void *)Buffer, (size_t)NumberOfBytes);
   
   return (Return == -1) ? 0 : Return;
 }
 
 
+EFI_STATUS
+SecConfigStdIn (
+  VOID
+  )
+{
+  struct termios tty;
+  
+  //
+  // Need to turn off line buffering, ECHO, and make it unbuffered.
+  //
+  tcgetattr (STDIN_FILENO, &tty);
+  tty.c_lflag &= ~(ICANON | ECHO);
+  tcsetattr (STDIN_FILENO, TCSANOW, &tty);
+  
+//  setvbuf (STDIN_FILENO, NULL, _IONBF, 0);
+  
+  // now ioctl FIONREAD will do what we need
+  return EFI_SUCCESS;
+}
+
+UINTN
+SecWriteStdOut (
+  IN UINT8     *Buffer,
+  IN UINTN     NumberOfBytes
+  )
+{
+  ssize_t Return;
+  
+  Return = write (STDOUT_FILENO, (const void *)Buffer, (size_t)NumberOfBytes);
+  
+  return (Return == -1) ? 0 : Return;
+}
+
+UINTN
+SecReadStdIn (
+  IN UINT8     *Buffer,
+  IN UINTN     NumberOfBytes
+  )
+{
+  ssize_t Return;
+  
+  Return = read (STDIN_FILENO, Buffer, (size_t)NumberOfBytes);
+  
+  return (Return == -1) ? 0 : Return;
+}
+
+BOOLEAN
+SecPollStdIn (
+  VOID
+  )
+{
+  int Result;
+  int Bytes;
+  
+  Result = ioctl (STDIN_FILENO, FIONREAD, &Bytes);
+  if (Result == -1) {
+    return FALSE;
+  }
+  
+  return (BOOLEAN)(Bytes > 0);
+}
+
+
 
 void
 settimer_handler (int sig)
@@ -187,15 +250,15 @@ QueryPerformanceCounter (
 
 VOID
 SecSleep (
-  IN  UINT64 Milliseconds
+  IN  UINT64 Nanoseconds
   )
 {
   struct timespec rq, rm;
   struct timeval  start, end;
   unsigned long  MicroSec;
   
-  rq.tv_sec = Milliseconds / 1000;
-  rq.tv_nsec = (Milliseconds % 1000) * 1000000;
+  rq.tv_sec  = Nanoseconds / 1000000000;
+  rq.tv_nsec = Nanoseconds % 1000000000;
 
   //
   // nanosleep gets interrupted by our timer tic. 
@@ -223,6 +286,22 @@ SecSleep (
   } 
 }
 
+
+VOID
+SecCpuSleep (
+  VOID
+  )
+{
+  struct timespec rq, rm;
+
+  // nanosleep gets interrupted by the timer tic
+  rq.tv_sec  = 1;
+  rq.tv_nsec = 0;
+  
+  nanosleep (&rq, &rm);
+}
+
+
 VOID
 SecExit (
   UINTN   Status
@@ -287,6 +366,10 @@ SecGetNextProtocol (
 
 EMU_THUNK_PROTOCOL gEmuThunkProtocol = {
   GasketSecWriteStdErr,
+  GasketSecConfigStdIn,
+  GasketSecWriteStdOut,
+  GasketSecReadStdIn,
+  GasketSecPollStdIn,
   GasketSecPeCoffGetEntryPoint,
   GasketSecPeCoffRelocateImageExtraAction,
   GasketSecPeCoffUnloadImageExtraAction,
@@ -295,6 +378,7 @@ EMU_THUNK_PROTOCOL gEmuThunkProtocol = {
   GasketQueryPerformanceFrequency,
   GasketQueryPerformanceCounter,
   GasketSecSleep,
+  GasketSecCpuSleep,
   GasketSecExit,
   GasketSecGetTime,                
   GasketSecSetTime,