summaryrefslogtreecommitdiff
path: root/StdLib/LibC/Main/Main.c
blob: 0336d8b36a771643ac2e378ce11954126a6bd3c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/** @file
  Establish the program environment and the "main" entry point.

  All of the global data in the gMD structure is initialized to 0, NULL, or
  SIG_DFL; as appropriate.

  Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>
  This program and the accompanying materials are licensed and made available under
  the terms and conditions of the BSD License that accompanies this distribution.
  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php.

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include  <Uefi.h>
#include  <Library/UefiLib.h>

#include  <Library/ShellCEntryLib.h>
#include  <Library/MemoryAllocationLib.h>
#include  <Library/TimerLib.h>

#include  <LibConfig.h>

#include  <errno.h>
#include  <stdio.h>
#include  <string.h>
#include  <MainData.h>

extern int main( int, wchar_t**);
extern int __sse2_available;

struct  __MainData  *gMD;

/* Worker function to keep GCC happy. */
void __main()
{
  ;
}

INTN
EFIAPI
ShellAppMain (
  IN UINTN Argc,
  IN CHAR16 **Argv
  )
{
  INTN   ExitVal;
  INTN   i;
  struct __filedes *mfd;
  FILE  *fp;

  ExitVal = (INTN)RETURN_SUCCESS;
  gMD = AllocateZeroPool(sizeof(struct __MainData));
  if( gMD == NULL ) {
    ExitVal = (INTN)RETURN_OUT_OF_RESOURCES;
  }
  else {
    /* Initialize data */
    __sse2_available      = 0;
    _fltused              = 1;
    errno                 = 0;
    EFIerrno              = 0;

#ifdef NT32dvm
    gMD->ClocksPerSecond  = 0;  // For NT32 only
    gMD->AppStartTime     = 0;  // For NT32 only
#else
    gMD->ClocksPerSecond = (clock_t)GetPerformanceCounterProperties( NULL, NULL);
    gMD->AppStartTime = (clock_t)GetPerformanceCounter();
#endif  /* NT32 dvm */

    // Initialize file descriptors
    mfd = gMD->fdarray;
    for(i = 0; i < (FOPEN_MAX); ++i) {
      mfd[i].MyFD = (UINT16)i;
    }

    // Open stdin, stdout, stderr
    fp = freopen("stdin:", "r", stdin);
    if(fp != NULL) {
      fp = freopen("stdout:", "w", stdout);
      if(fp != NULL) {
        fp = freopen("stderr:", "w", stderr);
      }
    }
    if(fp == NULL) {
      Print(L"ERROR Initializing Standard IO: %a.\n    %r\n",
            strerror(errno), EFIerrno);
    }

    ExitVal = (INTN)main( (int)Argc, (wchar_t **)Argv);

    if (gMD->cleanup != NULL) {
      gMD->cleanup();
    }
  }
  if(gMD != NULL) {
    FreePool( gMD );
  }
  return ExitVal;
}