EmbeddedPkg: Fix mispellings
[mirror_edk2.git] / EmbeddedPkg / EblExternCmd / EntryPointGlue.c
1 /** @file
2 Glue code that contains the EFI entry point and converts it to an EBL
3 ASCII Argc, Argv sytle entry point
4
5
6 Copyright (c) 2007, Intel Corporation. All rights reserved.<BR>
7 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
8
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD License
11 which accompanies this distribution. The full text of the license may be found at
12 http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17
18 **/
19
20 #define CMD_SEPARATOR ';'
21 #define MAX_ARGS 32
22
23 EFI_STATUS
24 EblMain (
25 IN UINTN Argc,
26 IN CHAR8 **Argv
27 );
28
29
30 ///
31 /// EdkExternCmdEntry() & ParseArguments() convert the standard EFI entry point
32 /// into Argc, Argv form that calls EblMain().
33 ///
34
35
36 /**
37 Parse the CmdLine and break it up into Argc (arg count) and Argv (array of
38 pointers to each argument). The Cmd buffer is altered and separators are
39 converted to string terminators. This allows Argv to point into CmdLine.
40 A CmdLine can support multiple commands. The next command in the command line
41 is returned if it exists.
42
43 @param CmdLine String to parse for a set of commands
44 @param CmdLineSize Size of CmdLine in bytes
45 @param Argc Returns the number of arguments in the CmdLine current command
46 @param Argv Argc pointers to each string in CmdLine
47
48 @return Next Command in the command line or NULL if non exists
49 **/
50 VOID
51 ParseArguments (
52 IN CHAR8 *CmdLine,
53 IN UINTN CmdLineSize,
54 OUT UINTN *Argc,
55 OUT CHAR8 **Argv
56 )
57 {
58 UINTN Arg;
59 CHAR8 *Char;
60 BOOLEAN LookingForArg;
61 BOOLEAN InQuote;
62 UINTN Index;
63
64 *Argc = 0;
65 if ((CmdLineSize == 0) || (AsciiStrLen (CmdLine) == 0)) {
66 // basic error checking failed on the arguments
67 return;
68 }
69
70 // Walk a single command line. A CMD_SEPARATOR allows multiple commands on a single line
71 InQuote = FALSE;
72 LookingForArg = TRUE;
73 for (Char = CmdLine, Arg = 0, Index = 0; *Char != '\0' && *Char != CMD_SEPARATOR; Char++, Index++) {
74 // Perform any text conversion here
75 if (*Char == '\t') {
76 // TAB to space
77 *Char = ' ';
78 }
79
80 if (LookingForArg) {
81 // Look for the beginning of an Argv[] entry
82 if (*Char == '"') {
83 Argv[Arg++] = ++Char;
84 LookingForArg = FALSE;
85 InQuote = TRUE;
86 } else if (*Char != ' ') {
87 Argv[Arg++] = Char;
88 LookingForArg = FALSE;
89 }
90 } else {
91 // Looking for the terminator of an Argv[] entry
92 if ((InQuote && (*Char == '"')) || (!InQuote && (*Char == ' '))) {
93 *Char = '\0';
94 LookingForArg = TRUE;
95 }
96 }
97
98 if ((Arg >= MAX_ARGS) || (Index > CmdLineSize)) {
99 // Error check buffer and exit since it does not look valid
100 break;
101 }
102 }
103
104 *Argc = Arg;
105
106 if (*Char == CMD_SEPARATOR) {
107 // Replace the command delimiter with null
108 *Char = '\0';
109 }
110
111 return;
112 }
113
114
115
116
117 /**
118 Embedded Boot Loader (EBL) - A simple EFI command line application for embedded
119 devices. PcdEmbeddedAutomaticBootCommand is a complied in command line that
120 gets executed automatically. The ; separator allows multiple commands
121 for each command line.
122
123 @param ImageHandle EFI ImageHandle for this application.
124 @param SystemTable EFI system table
125
126 @return EFI status of the application
127
128 **/
129 EFI_STATUS
130 EFIAPI
131 EdkExternCmdEntry (
132 IN EFI_HANDLE ImageHandle,
133 IN EFI_SYSTEM_TABLE *SystemTable
134 )
135 {
136 EFI_STATUS Status;
137 EFI_LOADED_IMAGE_PROTOCOL *ImageInfo;
138 UINTN Argc;
139 CHAR8 *Argv[MAX_ARGS];
140
141 Status = gBS->HandleProtocol (ImageHandle, &gEfiLoadedImageProtocolGuid, (VOID **)&ImageInfo);
142 if (EFI_ERROR (Status)) {
143 Argc = 0;
144 } else {
145 // Looks like valid commands were passed in.
146 ParseArguments (ImageInfo->LoadOptions, ImageInfo->LoadOptionsSize, &Argc, Argv);
147 }
148
149 return EblMain (Argc, Argv);
150 }
151
152