1 /** @file
2   Main file for attrib shell level 2 function.
3 
4   (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
5   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
6   Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved. <BR>
7   SPDX-License-Identifier: BSD-2-Clause-Patent
8 
9 **/
10 
11 #include "UefiShellLevel3CommandsLib.h"
12 
13 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
14   {L"-sfo", TypeFlag},
15   {NULL,    TypeMax}
16   };
17 
18 /**
19   Function for 'cls' command.
20 
21   @param[in] ImageHandle  Handle to the Image (NULL if Internal).
22   @param[in] SystemTable  Pointer to the System Table (NULL if Internal).
23 **/
24 SHELL_STATUS
25 EFIAPI
ShellCommandRunCls(IN EFI_HANDLE ImageHandle,IN EFI_SYSTEM_TABLE * SystemTable)26 ShellCommandRunCls (
27   IN EFI_HANDLE        ImageHandle,
28   IN EFI_SYSTEM_TABLE  *SystemTable
29   )
30 {
31   EFI_STATUS    Status;
32   LIST_ENTRY    *Package;
33   UINTN         Background;
34   UINTN         Foreground;
35   CHAR16        *ProblemParam;
36   SHELL_STATUS  ShellStatus;
37   CONST CHAR16  *BackColorStr;
38   CONST CHAR16  *ForeColorStr;
39 
40   //
41   // Initialize variables
42   //
43   ShellStatus   = SHELL_SUCCESS;
44   ProblemParam  = NULL;
45   Background    = 0;
46   Foreground    = 0;
47 
48   //
49   // initialize the shell lib (we must be in non-auto-init...)
50   //
51   Status = ShellInitialize();
52   ASSERT_EFI_ERROR(Status);
53 
54   //
55   // parse the command line
56   //
57   Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
58   if (EFI_ERROR(Status)) {
59     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {
60       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"cls", ProblemParam);
61       FreePool(ProblemParam);
62       ShellStatus = SHELL_INVALID_PARAMETER;
63     } else {
64       ASSERT(FALSE);
65     }
66   } else {
67     //
68     // check for "-?"
69     //
70     if (ShellCommandLineGetFlag(Package, L"-?")) {
71       ASSERT(FALSE);
72     } else if (ShellCommandLineGetFlag (Package, L"-sfo")) {
73       if (ShellCommandLineGetCount (Package) > 1) {
74         ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel3HiiHandle, L"cls");
75         ShellStatus = SHELL_INVALID_PARAMETER;
76       } else {
77         Background = (gST->ConOut->Mode->Attribute >> 4) & 0x7;
78         Foreground = gST->ConOut->Mode->Attribute & 0x0F;
79         ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_SFO_HEADER), gShellLevel3HiiHandle, L"cls");
80         ShellPrintHiiEx (
81           -1,
82           -1,
83           NULL,
84           STRING_TOKEN (STR_CLS_OUTPUT_SFO),
85           gShellLevel3HiiHandle,
86           gST->ConOut->Mode->Attribute,
87           Foreground,
88           Background
89           );
90       }
91     } else {
92       //
93       // If there are 0 value parameters, clear sceen
94       //
95       BackColorStr = ShellCommandLineGetRawValue (Package, 1);
96       ForeColorStr = ShellCommandLineGetRawValue (Package, 2);
97 
98       if (BackColorStr == NULL && ForeColorStr == NULL) {
99         //
100         // clear screen
101         //
102         gST->ConOut->ClearScreen (gST->ConOut);
103       } else if (ShellCommandLineGetCount (Package) > 3) {
104         ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel3HiiHandle, L"cls");
105         ShellStatus = SHELL_INVALID_PARAMETER;
106       } else {
107         if (BackColorStr != NULL) {
108           if ((ShellStrToUintn (BackColorStr) > 7) || (StrLen (BackColorStr) > 1) || (!ShellIsDecimalDigitCharacter (*BackColorStr))) {
109             ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel3HiiHandle, L"cls", BackColorStr);
110             ShellStatus = SHELL_INVALID_PARAMETER;
111           } else {
112             switch (ShellStrToUintn (BackColorStr)) {
113               case 0:
114                 Background = EFI_BACKGROUND_BLACK;
115                 break;
116               case 1:
117                 Background = EFI_BACKGROUND_BLUE;
118                 break;
119               case 2:
120                 Background = EFI_BACKGROUND_GREEN;
121                 break;
122               case 3:
123                 Background = EFI_BACKGROUND_CYAN;
124                 break;
125               case 4:
126                 Background = EFI_BACKGROUND_RED;
127                 break;
128               case 5:
129                 Background = EFI_BACKGROUND_MAGENTA;
130                 break;
131               case 6:
132                 Background = EFI_BACKGROUND_BROWN;
133                 break;
134               case 7:
135                 Background = EFI_BACKGROUND_LIGHTGRAY;
136                 break;
137             }
138 
139             if (ForeColorStr != NULL) {
140               if ((ShellStrToUintn (ForeColorStr) > 15) || (StrLen (ForeColorStr) > 2) || (!ShellIsDecimalDigitCharacter (*ForeColorStr))) {
141                 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellLevel3HiiHandle, L"cls", ForeColorStr);
142                 ShellStatus = SHELL_INVALID_PARAMETER;
143               } else {
144                 switch (ShellStrToUintn (ForeColorStr)) {
145                   case 0:
146                     Foreground = EFI_BLACK;
147                     break;
148                   case 1:
149                     Foreground = EFI_BLUE;
150                     break;
151                   case 2:
152                     Foreground = EFI_GREEN;
153                     break;
154                   case 3:
155                     Foreground = EFI_CYAN;
156                     break;
157                   case 4:
158                     Foreground = EFI_RED;
159                     break;
160                   case 5:
161                     Foreground = EFI_MAGENTA;
162                     break;
163                   case 6:
164                     Foreground = EFI_BROWN;
165                     break;
166                   case 7:
167                     Foreground = EFI_LIGHTGRAY;
168                     break;
169                   case 8:
170                     Foreground = EFI_DARKGRAY;
171                     break;
172                   case 9:
173                     Foreground = EFI_LIGHTBLUE;
174                     break;
175                   case 10:
176                     Foreground = EFI_LIGHTGREEN;
177                     break;
178                   case 11:
179                     Foreground = EFI_LIGHTCYAN;
180                     break;
181                   case 12:
182                     Foreground = EFI_LIGHTRED;
183                     break;
184                   case 13:
185                     Foreground = EFI_LIGHTMAGENTA;
186                     break;
187                   case 14:
188                     Foreground = EFI_YELLOW;
189                     break;
190                   case 15:
191                     Foreground = EFI_WHITE;
192                     break;
193                 }
194               }
195             } else {
196               //
197               // Since foreground color is not modified, so retain
198               // existing foreground color without any change to it.
199               //
200               Foreground = gST->ConOut->Mode->Attribute & 0x0F;
201             }
202 
203             if (ShellStatus == SHELL_SUCCESS) {
204               Status = gST->ConOut->SetAttribute (gST->ConOut, (Foreground | Background) & 0x7F);
205               ASSERT_EFI_ERROR (Status);
206               Status = gST->ConOut->ClearScreen (gST->ConOut);
207               ASSERT_EFI_ERROR (Status);
208             }
209           }
210         }
211       }
212     }
213   }
214   //
215   // free the command line package
216   //
217   ShellCommandLineFreeVarList (Package);
218 
219   //
220   // return the status
221   //
222   return (ShellStatus);
223 }
224 
225