2 Main file for EfiDecompress shell Debug1 function.
4 Copyright (c) 2005 - 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellDebug1CommandsLib.h"
16 #include <Protocol/Decompress.h>
20 Function for 'decompress' command.
22 @param[in] ImageHandle Handle to the Image (NULL if Internal).
23 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
27 ShellCommandRunEfiDecompress (
28 IN EFI_HANDLE ImageHandle
,
29 IN EFI_SYSTEM_TABLE
*SystemTable
35 SHELL_STATUS ShellStatus
;
36 SHELL_FILE_HANDLE InFileHandle
;
37 SHELL_FILE_HANDLE OutFileHandle
;
44 CONST CHAR16
*OutFileName
;
48 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
49 CONST CHAR16
*TempParam
;
54 ShellStatus
= SHELL_SUCCESS
;
63 // initialize the shell lib (we must be in non-auto-init...)
65 Status
= ShellInitialize();
66 ASSERT_EFI_ERROR(Status
);
68 Status
= CommandInit();
69 ASSERT_EFI_ERROR(Status
);
72 // parse the command line
74 Status
= ShellCommandLineParse (EmptyParamList
, &Package
, &ProblemParam
, TRUE
);
75 if (EFI_ERROR(Status
)) {
76 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
77 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, ProblemParam
);
78 FreePool(ProblemParam
);
79 ShellStatus
= SHELL_INVALID_PARAMETER
;
84 if (ShellCommandLineGetCount(Package
) > 3) {
85 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
);
86 ShellStatus
= SHELL_INVALID_PARAMETER
;
87 } else if (ShellCommandLineGetCount(Package
) < 3) {
88 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
);
89 ShellStatus
= SHELL_INVALID_PARAMETER
;
91 TempParam
= ShellCommandLineGetRawValue(Package
, 1);
92 ASSERT(TempParam
!= NULL
);
93 InFileName
= ShellFindFilePath(TempParam
);
94 OutFileName
= ShellCommandLineGetRawValue(Package
, 2);
95 if (InFileName
== NULL
) {
96 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_FIND_FAIL
), gShellDebug1HiiHandle
, TempParam
);
97 ShellStatus
= SHELL_NOT_FOUND
;
99 if (ShellIsDirectory(InFileName
) == EFI_SUCCESS
){
100 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_DIR
), gShellDebug1HiiHandle
, InFileName
);
101 ShellStatus
= SHELL_INVALID_PARAMETER
;
103 if (ShellIsDirectory(OutFileName
) == EFI_SUCCESS
){
104 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_DIR
), gShellDebug1HiiHandle
, OutFileName
);
105 ShellStatus
= SHELL_INVALID_PARAMETER
;
107 if (ShellStatus
== SHELL_SUCCESS
) {
108 Status
= ShellOpenFileByName(InFileName
, &InFileHandle
, EFI_FILE_MODE_READ
, 0);
109 if (EFI_ERROR(Status
)) {
110 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, ShellCommandLineGetRawValue(Package
, 1), Status
);
111 ShellStatus
= SHELL_NOT_FOUND
;
115 if (ShellStatus
== SHELL_SUCCESS
) {
116 Status
= FileHandleGetSize(InFileHandle
, &Temp64Bit
);
117 ASSERT(Temp64Bit
<= (UINT32
)(-1));
118 InSize
= (UINTN
)Temp64Bit
;
119 ASSERT_EFI_ERROR(Status
);
120 InBuffer
= AllocateZeroPool(InSize
);
121 ASSERT(InBuffer
!= NULL
);
122 Status
= gEfiShellProtocol
->ReadFile(InFileHandle
, &InSize
, InBuffer
);
123 ASSERT_EFI_ERROR(Status
);
125 Status
= gBS
->LocateProtocol(&gEfiDecompressProtocolGuid
, NULL
, (VOID
**)&Decompress
);
126 ASSERT_EFI_ERROR(Status
);
128 Status
= Decompress
->GetInfo(Decompress
, InBuffer
, (UINT32
)InSize
, &OutSize
, &ScratchSize
);
129 if (EFI_ERROR(Status
) || OutSize
== 0) {
130 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_EFI_DECOMPRESS_NOPE
), gShellDebug1HiiHandle
, InFileName
);
131 ShellStatus
= SHELL_NOT_FOUND
;
133 Status
= ShellOpenFileByName(OutFileName
, &OutFileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
134 if (EFI_ERROR(Status
)) {
135 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, ShellCommandLineGetRawValue(Package
, 2), Status
);
136 ShellStatus
= SHELL_NOT_FOUND
;
138 OutBuffer
= AllocateZeroPool(OutSize
);
139 ScratchBuffer
= AllocateZeroPool(ScratchSize
);
140 ASSERT(OutBuffer
!= NULL
);
141 ASSERT(ScratchBuffer
!= NULL
);
143 Status
= Decompress
->Decompress(Decompress
, InBuffer
, (UINT32
)InSize
, OutBuffer
, OutSize
, ScratchBuffer
, ScratchSize
);
144 ASSERT_EFI_ERROR(Status
);
146 if (EFI_ERROR(Status
)) {
147 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_EFI_DECOMPRESS_FAIL
), gShellDebug1HiiHandle
, Status
);
148 ShellStatus
= SHELL_DEVICE_ERROR
;
150 OutSizeTemp
= OutSize
;
151 Status
= gEfiShellProtocol
->WriteFile(OutFileHandle
, &OutSizeTemp
, OutBuffer
);
152 OutSize
= (UINT32
)OutSizeTemp
;
153 if (EFI_ERROR(Status
)) {
154 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_WRITE_FAIL
), gShellDebug1HiiHandle
, OutFileName
, Status
);
155 ShellStatus
= SHELL_DEVICE_ERROR
;
164 ShellCommandLineFreeVarList (Package
);
166 if (InFileHandle
!= NULL
) {
167 gEfiShellProtocol
->CloseFile(InFileHandle
);
169 if (OutFileHandle
!= NULL
) {
170 gEfiShellProtocol
->CloseFile(OutFileHandle
);
173 SHELL_FREE_NON_NULL(InFileName
);
174 SHELL_FREE_NON_NULL(InBuffer
);
175 SHELL_FREE_NON_NULL(OutBuffer
);
176 SHELL_FREE_NON_NULL(ScratchBuffer
);
178 return (ShellStatus
);