From 5bdbf4462bd50762b308a62498f587650ebaf656 Mon Sep 17 00:00:00 2001 From: Pierre Gondois Date: Thu, 4 Jun 2026 11:19:05 +0200 Subject: [PATCH] ShellPkg/UefiShellDebug1: Extract MainCmdXXX() function (2/4) This patch aims to help breaking down the long functions present in the ShellPkg and reduce complexity/nested code and conditions. Extract a MainCmdXXX() function for each shell command. This command contains the possible operations the command aims to operate. The ShellCommandRunXXX() function from which it is extracted is only responsible of: - initializing the shell/command environment - parsing the command parameter and creating a Package - freeing the Package For the MemMap and SetVar commands, ShellCommandLineFreeVarList() calls are removed as the Package is now freed in the caller function: ShellCommandRunXXX(). To avoid having one large commit updating all the UefiShellDebug1 commands, only update these files: - Edit/Edit.c - EfiCompress.c - EfiDecompress.c - HexEdit/HexEdit.c - LoadPciRom.c No functional change should be induced by this patch. Signed-off-by: Pierre Gondois --- .../UefiShellDebug1CommandsLib/Edit/Edit.c | 92 +++++++++++------- .../UefiShellDebug1CommandsLib/EfiCompress.c | 96 ++++++++++++------- .../EfiDecompress.c | 95 +++++++++++------- .../HexEdit/HexEdit.c | 95 +++++++++++------- .../UefiShellDebug1CommandsLib/LoadPciRom.c | 85 ++++++++++------ 5 files changed, 286 insertions(+), 177 deletions(-) diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c index 34e5ddc25c..2697e71c60 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/Edit/Edit.c @@ -10,24 +10,19 @@ #include "UefiShellDebug1CommandsLib.h" #include "TextEditor.h" -/** - Function for 'edit' command. +/** Main function of the 'Edit' command. - @param[in] ImageHandle Handle to the Image (NULL if Internal). - @param[in] SystemTable Pointer to the System Table (NULL if Internal). + @param[in] Package List of input parameter for the command. **/ +STATIC SHELL_STATUS -EFIAPI -ShellCommandRunEdit ( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable +MainCmdEdit ( + LIST_ENTRY *Package ) { EFI_STATUS Status; CHAR16 *Buffer; - CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; - LIST_ENTRY *Package; CONST CHAR16 *Cwd; CHAR16 *Nfs; CHAR16 *Spot; @@ -37,31 +32,6 @@ ShellCommandRunEdit ( ShellStatus = SHELL_SUCCESS; Nfs = NULL; - // - // initialize the shell lib (we must be in non-auto-init...) - // - Status = ShellInitialize (); - ASSERT_EFI_ERROR (Status); - - Status = CommandInit (); - ASSERT_EFI_ERROR (Status); - - // - // parse the command line - // - Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); - if (EFI_ERROR (Status)) { - if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { - ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"edit", ProblemParam); - FreePool (ProblemParam); - ShellStatus = SHELL_INVALID_PARAMETER; - } else { - ASSERT (FALSE); - } - - return ShellStatus; - } - if (ShellCommandLineGetCount (Package) > 2) { ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"edit"); ShellStatus = SHELL_INVALID_PARAMETER; @@ -154,6 +124,58 @@ ShellCommandRunEdit ( } } + return ShellStatus; +} + +/** + Function for 'edit' command. + + @param[in] ImageHandle Handle to the Image (NULL if Internal). + @param[in] SystemTable Pointer to the System Table (NULL if Internal). +**/ +SHELL_STATUS +EFIAPI +ShellCommandRunEdit ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + CHAR16 *ProblemParam; + SHELL_STATUS ShellStatus; + LIST_ENTRY *Package; + + // SHELL_FILE_HANDLE TempHandle; + + ShellStatus = SHELL_SUCCESS; + + // + // initialize the shell lib (we must be in non-auto-init...) + // + Status = ShellInitialize (); + ASSERT_EFI_ERROR (Status); + + Status = CommandInit (); + ASSERT_EFI_ERROR (Status); + + // + // parse the command line + // + Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); + if (EFI_ERROR (Status)) { + if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { + ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"edit", ProblemParam); + FreePool (ProblemParam); + ShellStatus = SHELL_INVALID_PARAMETER; + } else { + ASSERT (FALSE); + } + + return ShellStatus; + } + + ShellStatus = MainCmdEdit (Package); + ShellCommandLineFreeVarList (Package); return ShellStatus; diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c index f706f81586..7bf6ce87aa 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiCompress.c @@ -10,22 +10,17 @@ #include "UefiShellDebug1CommandsLib.h" #include "Compress.h" -/** - Function for 'compress' command. +/** Main function of the 'EfiCompress' command. - @param[in] ImageHandle Handle to the Image (NULL if Internal). - @param[in] SystemTable Pointer to the System Table (NULL if Internal). + @param[in] Package List of input parameter for the command. **/ +STATIC SHELL_STATUS -EFIAPI -ShellCommandRunEfiCompress ( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable +MainCmdEfiCompress ( + LIST_ENTRY *Package ) { EFI_STATUS Status; - LIST_ENTRY *Package; - CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; SHELL_FILE_HANDLE InShellFileHandle; SHELL_FILE_HANDLE OutShellFileHandle; @@ -49,31 +44,6 @@ ShellCommandRunEfiCompress ( OutShellFileHandle = NULL; InBuffer = NULL; - // - // initialize the shell lib (we must be in non-auto-init...) - // - Status = ShellInitialize (); - ASSERT_EFI_ERROR (Status); - - Status = CommandInit (); - ASSERT_EFI_ERROR (Status); - - // - // parse the command line - // - Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); - if (EFI_ERROR (Status)) { - if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { - ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"eficompress", ProblemParam); - FreePool (ProblemParam); - ShellStatus = SHELL_INVALID_PARAMETER; - } else { - ASSERT (FALSE); - } - - return ShellStatus; - } - if (ShellCommandLineGetCount (Package) > 3) { ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"eficompress"); ShellStatus = SHELL_INVALID_PARAMETER; @@ -157,8 +127,6 @@ ShellCommandRunEfiCompress ( } Exit: - ShellCommandLineFreeVarList (Package); - if (InShellFileHandle != NULL) { gEfiShellProtocol->CloseFile (InShellFileHandle); } @@ -171,5 +139,59 @@ Exit: SHELL_FREE_NON_NULL (InBuffer); SHELL_FREE_NON_NULL (OutBuffer); + return ShellStatus; +} + +/** + Function for 'compress' command. + + @param[in] ImageHandle Handle to the Image (NULL if Internal). + @param[in] SystemTable Pointer to the System Table (NULL if Internal). +**/ +SHELL_STATUS +EFIAPI +ShellCommandRunEfiCompress ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + LIST_ENTRY *Package; + CHAR16 *ProblemParam; + SHELL_STATUS ShellStatus; + + ShellStatus = SHELL_SUCCESS; + Status = EFI_SUCCESS; + Package = NULL; + + // + // initialize the shell lib (we must be in non-auto-init...) + // + Status = ShellInitialize (); + ASSERT_EFI_ERROR (Status); + + Status = CommandInit (); + ASSERT_EFI_ERROR (Status); + + // + // parse the command line + // + Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); + if (EFI_ERROR (Status)) { + if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { + ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"eficompress", ProblemParam); + FreePool (ProblemParam); + ShellStatus = SHELL_INVALID_PARAMETER; + } else { + ASSERT (FALSE); + } + + return ShellStatus; + } + + ShellStatus = MainCmdEfiCompress (Package); + + ShellCommandLineFreeVarList (Package); + return (ShellStatus); } diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c index a31183254d..4fc547a359 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c @@ -10,22 +10,17 @@ #include "UefiShellDebug1CommandsLib.h" #include -/** - Function for 'decompress' command. +/** Main function of the 'EfiDecompress' command. - @param[in] ImageHandle Handle to the Image (NULL if Internal). - @param[in] SystemTable Pointer to the System Table (NULL if Internal). + @param[in] Package List of input parameter for the command. **/ +STATIC SHELL_STATUS -EFIAPI -ShellCommandRunEfiDecompress ( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable +MainCmdEfiDecompress ( + LIST_ENTRY *Package ) { EFI_STATUS Status; - LIST_ENTRY *Package; - CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; SHELL_FILE_HANDLE InFileHandle; SHELL_FILE_HANDLE OutFileHandle; @@ -55,31 +50,6 @@ ShellCommandRunEfiDecompress ( OutFileHandle = NULL; Decompress = NULL; - // - // initialize the shell lib (we must be in non-auto-init...) - // - Status = ShellInitialize (); - ASSERT_EFI_ERROR (Status); - - Status = CommandInit (); - ASSERT_EFI_ERROR (Status); - - // - // parse the command line - // - Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); - if (EFI_ERROR (Status)) { - if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { - ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"efidecompress", ProblemParam); - FreePool (ProblemParam); - ShellStatus = SHELL_INVALID_PARAMETER; - } else { - ASSERT (FALSE); - } - - return ShellStatus; - } - if (ShellCommandLineGetCount (Package) > 3) { ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"efidecompress"); ShellStatus = SHELL_INVALID_PARAMETER; @@ -188,8 +158,6 @@ ShellCommandRunEfiDecompress ( } Done: - ShellCommandLineFreeVarList (Package); - if (InFileHandle != NULL) { gEfiShellProtocol->CloseFile (InFileHandle); } @@ -203,5 +171,58 @@ Done: SHELL_FREE_NON_NULL (OutBuffer); SHELL_FREE_NON_NULL (ScratchBuffer); + return ShellStatus; +} + +/** + Function for 'decompress' command. + + @param[in] ImageHandle Handle to the Image (NULL if Internal). + @param[in] SystemTable Pointer to the System Table (NULL if Internal). +**/ +SHELL_STATUS +EFIAPI +ShellCommandRunEfiDecompress ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + LIST_ENTRY *Package; + CHAR16 *ProblemParam; + SHELL_STATUS ShellStatus; + + ShellStatus = SHELL_SUCCESS; + Status = EFI_SUCCESS; + + // + // initialize the shell lib (we must be in non-auto-init...) + // + Status = ShellInitialize (); + ASSERT_EFI_ERROR (Status); + + Status = CommandInit (); + ASSERT_EFI_ERROR (Status); + + // + // parse the command line + // + Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); + if (EFI_ERROR (Status)) { + if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { + ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"efidecompress", ProblemParam); + FreePool (ProblemParam); + ShellStatus = SHELL_INVALID_PARAMETER; + } else { + ASSERT (FALSE); + } + + return ShellStatus; + } + + ShellStatus = MainCmdEfiDecompress (Package); + + ShellCommandLineFreeVarList (Package); + return (ShellStatus); } diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c index 22a9c74954..4cfa5720b0 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/HexEdit/HexEdit.c @@ -20,24 +20,19 @@ STATIC CONST SHELL_PARAM_ITEM ParamList[] = { { NULL, TypeMax } }; -/** - Function for 'hexedit' command. +/** Main function of the 'HexEdit' command. - @param[in] ImageHandle Handle to the Image (NULL if Internal). - @param[in] SystemTable Pointer to the System Table (NULL if Internal). + @param[in] Package List of input parameter for the command. **/ +STATIC SHELL_STATUS -EFIAPI -ShellCommandRunHexEdit ( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable +MainCmdHexEdit ( + LIST_ENTRY *Package ) { EFI_STATUS Status; CHAR16 *Buffer; - CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; - LIST_ENTRY *Package; CHAR16 *NewName; CONST CHAR16 *Name; UINTN Offset; @@ -53,31 +48,6 @@ ShellCommandRunHexEdit ( Size = 0; WhatToDo = FileTypeNone; - // - // initialize the shell lib (we must be in non-auto-init...) - // - Status = ShellInitialize (); - ASSERT_EFI_ERROR (Status); - - Status = CommandInit (); - ASSERT_EFI_ERROR (Status); - - // - // parse the command line - // - Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE); - if (EFI_ERROR (Status)) { - if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { - ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"hexedit", ProblemParam); - FreePool (ProblemParam); - ShellStatus = SHELL_INVALID_PARAMETER; - } else { - ASSERT (FALSE); - } - - return ShellStatus; - } - // // Check for -d // @@ -270,9 +240,60 @@ ShellCommandRunHexEdit ( } } - ShellCommandLineFreeVarList (Package); - SHELL_FREE_NON_NULL (Buffer); SHELL_FREE_NON_NULL (NewName); + + return ShellStatus; +} + +/** + Function for 'hexedit' command. + + @param[in] ImageHandle Handle to the Image (NULL if Internal). + @param[in] SystemTable Pointer to the System Table (NULL if Internal). +**/ +SHELL_STATUS +EFIAPI +ShellCommandRunHexEdit ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + CHAR16 *ProblemParam; + SHELL_STATUS ShellStatus; + LIST_ENTRY *Package; + + ShellStatus = SHELL_SUCCESS; + + // + // initialize the shell lib (we must be in non-auto-init...) + // + Status = ShellInitialize (); + ASSERT_EFI_ERROR (Status); + + Status = CommandInit (); + ASSERT_EFI_ERROR (Status); + + // + // parse the command line + // + Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE); + if (EFI_ERROR (Status)) { + if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { + ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"hexedit", ProblemParam); + FreePool (ProblemParam); + ShellStatus = SHELL_INVALID_PARAMETER; + } else { + ASSERT (FALSE); + } + + return ShellStatus; + } + + ShellStatus = MainCmdHexEdit (Package); + + ShellCommandLineFreeVarList (Package); + return ShellStatus; } diff --git a/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c b/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c index 61096bd3db..687757bd23 100644 --- a/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c +++ b/ShellPkg/Library/UefiShellDebug1CommandsLib/LoadPciRom.c @@ -44,30 +44,20 @@ LoadEfiDriversFromRomImage ( CONST CHAR16 *FileName ); -STATIC CONST SHELL_PARAM_ITEM ParamList[] = { - { L"-nc", TypeFlag }, - { NULL, TypeMax } -}; +/** Main function of the 'LoadPciRom' command. -/** - Function for 'loadpcirom' command. - - @param[in] ImageHandle Handle to the Image (NULL if Internal). - @param[in] SystemTable Pointer to the System Table (NULL if Internal). + @param[in] Package List of input parameter for the command. **/ +STATIC SHELL_STATUS -EFIAPI -ShellCommandRunLoadPciRom ( - IN EFI_HANDLE ImageHandle, - IN EFI_SYSTEM_TABLE *SystemTable +MainCmdLoadPciRom ( + LIST_ENTRY *Package ) { EFI_SHELL_FILE_INFO *FileList; UINTN SourceSize; UINT8 *File1Buffer; EFI_STATUS Status; - LIST_ENTRY *Package; - CHAR16 *ProblemParam; SHELL_STATUS ShellStatus; BOOLEAN Connect; CONST CHAR16 *Param; @@ -81,22 +71,6 @@ ShellCommandRunLoadPciRom ( ShellStatus = SHELL_SUCCESS; FileList = NULL; - // - // verify number of arguments - // - Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE); - if (EFI_ERROR (Status)) { - if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { - ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"loadpcirom", ProblemParam); - FreePool (ProblemParam); - ShellStatus = SHELL_INVALID_PARAMETER; - } else { - ASSERT (FALSE); - } - - return ShellStatus; - } - if (ShellCommandLineGetCount (Package) < 2) { ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"loadpcirom"); ShellStatus = SHELL_INVALID_PARAMETER; @@ -185,6 +159,55 @@ ShellCommandRunLoadPciRom ( } } + return ShellStatus; +} + +STATIC CONST SHELL_PARAM_ITEM ParamList[] = { + { L"-nc", TypeFlag }, + { NULL, TypeMax } +}; + +/** + Function for 'loadpcirom' command. + + @param[in] ImageHandle Handle to the Image (NULL if Internal). + @param[in] SystemTable Pointer to the System Table (NULL if Internal). +**/ +SHELL_STATUS +EFIAPI +ShellCommandRunLoadPciRom ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + LIST_ENTRY *Package; + CHAR16 *ProblemParam; + SHELL_STATUS ShellStatus; + + // + // Local variable initializations + // + ShellStatus = SHELL_SUCCESS; + + // + // verify number of arguments + // + Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE); + if (EFI_ERROR (Status)) { + if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) { + ShellPrintHiiDefaultEx (STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"loadpcirom", ProblemParam); + FreePool (ProblemParam); + ShellStatus = SHELL_INVALID_PARAMETER; + } else { + ASSERT (FALSE); + } + + return ShellStatus; + } + + ShellStatus = MainCmdLoadPciRom (Package); + ShellCommandLineFreeVarList (Package); return (ShellStatus);