1 /** @file
2   Provides application point extension for "C" style main function
3 
4   Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include <Base.h>
10 
11 #include <Protocol/SimpleFileSystem.h>
12 #include <Protocol/LoadedImage.h>
13 #include <Protocol/EfiShellInterface.h>
14 #include <Protocol/ShellParameters.h>
15 
16 #include <Library/ShellCEntryLib.h>
17 #include <Library/DebugLib.h>
18 
19 /**
20   UEFI entry point for an application that will in turn call the
21   ShellAppMain function which has parameters similar to a standard C
22   main function.
23 
24   An application that uses UefiShellCEntryLib must have a ShellAppMain
25   function as prototyped in Include/Library/ShellCEntryLib.h.
26 
27   Note that the Shell uses POSITIVE integers for error values, while UEFI
28   uses NEGATIVE values.  If the application is to be used within a script,
29   it needs to return one of the SHELL_STATUS values defined in Protocol/Shell.h.
30 
31   @param  ImageHandle  The image handle of the UEFI Application.
32   @param  SystemTable  A pointer to the EFI System Table.
33 
34   @retval  EFI_SUCCESS               The application exited normally.
35   @retval  Other                     An error occurred.
36 
37 **/
38 EFI_STATUS
39 EFIAPI
ShellCEntryLib(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)40 ShellCEntryLib (
41   IN EFI_HANDLE        ImageHandle,
42   IN EFI_SYSTEM_TABLE  *SystemTable
43   )
44 {
45   INTN                           ReturnFromMain;
46   EFI_SHELL_PARAMETERS_PROTOCOL *EfiShellParametersProtocol;
47   EFI_SHELL_INTERFACE           *EfiShellInterface;
48   EFI_STATUS                    Status;
49 
50   ReturnFromMain = -1;
51   EfiShellParametersProtocol = NULL;
52   EfiShellInterface = NULL;
53 
54   Status = SystemTable->BootServices->OpenProtocol(ImageHandle,
55                              &gEfiShellParametersProtocolGuid,
56                              (VOID **)&EfiShellParametersProtocol,
57                              ImageHandle,
58                              NULL,
59                              EFI_OPEN_PROTOCOL_GET_PROTOCOL
60                             );
61   if (!EFI_ERROR(Status)) {
62     //
63     // use shell 2.0 interface
64     //
65     ReturnFromMain = ShellAppMain (
66                        EfiShellParametersProtocol->Argc,
67                        EfiShellParametersProtocol->Argv
68                       );
69   } else {
70     //
71     // try to get shell 1.0 interface instead.
72     //
73     Status = SystemTable->BootServices->OpenProtocol(ImageHandle,
74                                &gEfiShellInterfaceGuid,
75                                (VOID **)&EfiShellInterface,
76                                ImageHandle,
77                                NULL,
78                                EFI_OPEN_PROTOCOL_GET_PROTOCOL
79                               );
80     if (!EFI_ERROR(Status)) {
81       //
82       // use shell 1.0 interface
83       //
84       ReturnFromMain = ShellAppMain (
85                          EfiShellInterface->Argc,
86                          EfiShellInterface->Argv
87                         );
88     } else {
89       ASSERT(FALSE);
90     }
91   }
92   return ReturnFromMain;
93 }
94