2 Main file for EfiDecompress shell Debug1 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "UefiShellDebug1CommandsLib.h"
11 #include <Protocol/Decompress.h>
15 Function for 'decompress' command.
17 @param[in] ImageHandle Handle to the Image (NULL if Internal).
18 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
22 ShellCommandRunEfiDecompress (
23 IN EFI_HANDLE ImageHandle
,
24 IN EFI_SYSTEM_TABLE
*SystemTable
30 SHELL_STATUS ShellStatus
;
31 SHELL_FILE_HANDLE InFileHandle
;
32 SHELL_FILE_HANDLE OutFileHandle
;
39 CONST CHAR16
*OutFileName
;
43 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
44 CONST CHAR16
*TempParam
;
50 ShellStatus
= SHELL_SUCCESS
;
60 // initialize the shell lib (we must be in non-auto-init...)
62 Status
= ShellInitialize();
63 ASSERT_EFI_ERROR(Status
);
65 Status
= CommandInit();
66 ASSERT_EFI_ERROR(Status
);
69 // parse the command line
71 Status
= ShellCommandLineParse (EmptyParamList
, &Package
, &ProblemParam
, TRUE
);
72 if (EFI_ERROR(Status
)) {
73 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
74 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"efidecompress", ProblemParam
);
75 FreePool(ProblemParam
);
76 ShellStatus
= SHELL_INVALID_PARAMETER
;
81 if (ShellCommandLineGetCount(Package
) > 3) {
82 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"efidecompress");
83 ShellStatus
= SHELL_INVALID_PARAMETER
;
84 } else if (ShellCommandLineGetCount(Package
) < 3) {
85 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"efidecompress");
86 ShellStatus
= SHELL_INVALID_PARAMETER
;
88 TempParam
= ShellCommandLineGetRawValue(Package
, 1);
89 ASSERT(TempParam
!= NULL
);
90 InFileName
= ShellFindFilePath(TempParam
);
91 OutFileName
= ShellCommandLineGetRawValue(Package
, 2);
92 if (InFileName
== NULL
) {
93 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_FIND_FAIL
), gShellDebug1HiiHandle
, L
"efidecompress", TempParam
);
94 ShellStatus
= SHELL_NOT_FOUND
;
96 if (ShellIsDirectory(InFileName
) == EFI_SUCCESS
){
97 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_DIR
), gShellDebug1HiiHandle
, L
"efidecompress", InFileName
);
98 ShellStatus
= SHELL_INVALID_PARAMETER
;
100 if (ShellIsDirectory(OutFileName
) == EFI_SUCCESS
){
101 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_DIR
), gShellDebug1HiiHandle
, L
"efidecompress", OutFileName
);
102 ShellStatus
= SHELL_INVALID_PARAMETER
;
104 if (ShellStatus
== SHELL_SUCCESS
) {
105 Status
= ShellOpenFileByName(InFileName
, &InFileHandle
, EFI_FILE_MODE_READ
, 0);
106 if (EFI_ERROR(Status
)) {
107 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, L
"efidecompress", ShellCommandLineGetRawValue(Package
, 1));
108 ShellStatus
= SHELL_NOT_FOUND
;
112 if (ShellStatus
== SHELL_SUCCESS
) {
113 Status
= FileHandleGetSize(InFileHandle
, &Temp64Bit
);
114 ASSERT(Temp64Bit
<= (UINT32
)(-1));
115 InSize
= (UINTN
)Temp64Bit
;
116 ASSERT_EFI_ERROR(Status
);
117 InBuffer
= AllocateZeroPool(InSize
);
118 if (InBuffer
== NULL
) {
119 Status
= EFI_OUT_OF_RESOURCES
;
121 Status
= gEfiShellProtocol
->ReadFile (InFileHandle
, &InSize
, InBuffer
);
122 ASSERT_EFI_ERROR (Status
);
124 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
125 ASSERT_EFI_ERROR (Status
);
127 Status
= Decompress
->GetInfo (Decompress
, InBuffer
, (UINT32
) InSize
, &OutSize
, &ScratchSize
);
130 if (EFI_ERROR(Status
) || OutSize
== 0) {
131 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_EFI_DECOMPRESS_NOPE
), gShellDebug1HiiHandle
, InFileName
);
132 ShellStatus
= SHELL_NOT_FOUND
;
134 Status
= ShellOpenFileByName(OutFileName
, &OutFileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
135 if (EFI_ERROR(Status
)) {
136 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, ShellCommandLineGetRawValue(Package
, 2), Status
);
137 ShellStatus
= SHELL_NOT_FOUND
;
139 OutBuffer
= AllocateZeroPool(OutSize
);
140 ScratchBuffer
= AllocateZeroPool(ScratchSize
);
141 if (OutBuffer
== NULL
|| ScratchBuffer
== NULL
) {
142 Status
= EFI_OUT_OF_RESOURCES
;
144 Status
= Decompress
->Decompress (Decompress
, InBuffer
, (UINT32
) InSize
, OutBuffer
, OutSize
, ScratchBuffer
, ScratchSize
);
149 if (EFI_ERROR (Status
)) {
150 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_EFI_DECOMPRESS_FAIL
), gShellDebug1HiiHandle
, Status
);
151 ShellStatus
= ((Status
== EFI_OUT_OF_RESOURCES
) ? SHELL_OUT_OF_RESOURCES
: SHELL_DEVICE_ERROR
);
153 OutSizeTemp
= OutSize
;
154 Status
= gEfiShellProtocol
->WriteFile (OutFileHandle
, &OutSizeTemp
, OutBuffer
);
155 OutSize
= (UINT32
) OutSizeTemp
;
156 if (EFI_ERROR (Status
)) {
157 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_FILE_WRITE_FAIL
), gShellDebug1HiiHandle
, L
"efidecompress", OutFileName
, Status
);
158 ShellStatus
= SHELL_DEVICE_ERROR
;
165 ShellCommandLineFreeVarList (Package
);
167 if (InFileHandle
!= NULL
) {
168 gEfiShellProtocol
->CloseFile(InFileHandle
);
170 if (OutFileHandle
!= NULL
) {
171 gEfiShellProtocol
->CloseFile(OutFileHandle
);
174 SHELL_FREE_NON_NULL(InFileName
);
175 SHELL_FREE_NON_NULL(InBuffer
);
176 SHELL_FREE_NON_NULL(OutBuffer
);
177 SHELL_FREE_NON_NULL(ScratchBuffer
);
179 return (ShellStatus
);