2 Main file for EfiDecompress shell Debug1 function.
4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2005 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UefiShellDebug1CommandsLib.h"
17 #include <Protocol/Decompress.h>
21 Function for 'decompress' command.
23 @param[in] ImageHandle Handle to the Image (NULL if Internal).
24 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
28 ShellCommandRunEfiDecompress (
29 IN EFI_HANDLE ImageHandle
,
30 IN EFI_SYSTEM_TABLE
*SystemTable
36 SHELL_STATUS ShellStatus
;
37 SHELL_FILE_HANDLE InFileHandle
;
38 SHELL_FILE_HANDLE OutFileHandle
;
45 CONST CHAR16
*OutFileName
;
49 EFI_DECOMPRESS_PROTOCOL
*Decompress
;
50 CONST CHAR16
*TempParam
;
56 ShellStatus
= SHELL_SUCCESS
;
66 // initialize the shell lib (we must be in non-auto-init...)
68 Status
= ShellInitialize();
69 ASSERT_EFI_ERROR(Status
);
71 Status
= CommandInit();
72 ASSERT_EFI_ERROR(Status
);
75 // parse the command line
77 Status
= ShellCommandLineParse (EmptyParamList
, &Package
, &ProblemParam
, TRUE
);
78 if (EFI_ERROR(Status
)) {
79 if (Status
== EFI_VOLUME_CORRUPTED
&& ProblemParam
!= NULL
) {
80 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellDebug1HiiHandle
, L
"efidecompress", ProblemParam
);
81 FreePool(ProblemParam
);
82 ShellStatus
= SHELL_INVALID_PARAMETER
;
87 if (ShellCommandLineGetCount(Package
) > 3) {
88 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
), gShellDebug1HiiHandle
, L
"efidecompress");
89 ShellStatus
= SHELL_INVALID_PARAMETER
;
90 } else if (ShellCommandLineGetCount(Package
) < 3) {
91 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
), gShellDebug1HiiHandle
, L
"efidecompress");
92 ShellStatus
= SHELL_INVALID_PARAMETER
;
94 TempParam
= ShellCommandLineGetRawValue(Package
, 1);
95 ASSERT(TempParam
!= NULL
);
96 InFileName
= ShellFindFilePath(TempParam
);
97 OutFileName
= ShellCommandLineGetRawValue(Package
, 2);
98 if (InFileName
== NULL
) {
99 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_FIND_FAIL
), gShellDebug1HiiHandle
, L
"efidecompress", TempParam
);
100 ShellStatus
= SHELL_NOT_FOUND
;
102 if (ShellIsDirectory(InFileName
) == EFI_SUCCESS
){
103 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_DIR
), gShellDebug1HiiHandle
, L
"efidecompress", InFileName
);
104 ShellStatus
= SHELL_INVALID_PARAMETER
;
106 if (ShellIsDirectory(OutFileName
) == EFI_SUCCESS
){
107 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_NOT_DIR
), gShellDebug1HiiHandle
, L
"efidecompress", OutFileName
);
108 ShellStatus
= SHELL_INVALID_PARAMETER
;
110 if (ShellStatus
== SHELL_SUCCESS
) {
111 Status
= ShellOpenFileByName(InFileName
, &InFileHandle
, EFI_FILE_MODE_READ
, 0);
112 if (EFI_ERROR(Status
)) {
113 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, L
"efidecompress", ShellCommandLineGetRawValue(Package
, 1));
114 ShellStatus
= SHELL_NOT_FOUND
;
118 if (ShellStatus
== SHELL_SUCCESS
) {
119 Status
= FileHandleGetSize(InFileHandle
, &Temp64Bit
);
120 ASSERT(Temp64Bit
<= (UINT32
)(-1));
121 InSize
= (UINTN
)Temp64Bit
;
122 ASSERT_EFI_ERROR(Status
);
123 InBuffer
= AllocateZeroPool(InSize
);
124 if (InBuffer
== NULL
) {
125 Status
= EFI_OUT_OF_RESOURCES
;
127 Status
= gEfiShellProtocol
->ReadFile (InFileHandle
, &InSize
, InBuffer
);
128 ASSERT_EFI_ERROR (Status
);
130 Status
= gBS
->LocateProtocol (&gEfiDecompressProtocolGuid
, NULL
, (VOID
**) &Decompress
);
131 ASSERT_EFI_ERROR (Status
);
133 Status
= Decompress
->GetInfo (Decompress
, InBuffer
, (UINT32
) InSize
, &OutSize
, &ScratchSize
);
136 if (EFI_ERROR(Status
) || OutSize
== 0) {
137 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_EFI_DECOMPRESS_NOPE
), gShellDebug1HiiHandle
, InFileName
);
138 ShellStatus
= SHELL_NOT_FOUND
;
140 Status
= ShellOpenFileByName(OutFileName
, &OutFileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
141 if (EFI_ERROR(Status
)) {
142 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_FILE_OPEN_FAIL
), gShellDebug1HiiHandle
, ShellCommandLineGetRawValue(Package
, 2), Status
);
143 ShellStatus
= SHELL_NOT_FOUND
;
145 OutBuffer
= AllocateZeroPool(OutSize
);
146 ScratchBuffer
= AllocateZeroPool(ScratchSize
);
147 if (OutBuffer
== NULL
|| ScratchBuffer
== NULL
) {
148 Status
= EFI_OUT_OF_RESOURCES
;
150 Status
= Decompress
->Decompress (Decompress
, InBuffer
, (UINT32
) InSize
, OutBuffer
, OutSize
, ScratchBuffer
, ScratchSize
);
155 if (EFI_ERROR (Status
)) {
156 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_EFI_DECOMPRESS_FAIL
), gShellDebug1HiiHandle
, Status
);
157 ShellStatus
= ((Status
== EFI_OUT_OF_RESOURCES
) ? SHELL_OUT_OF_RESOURCES
: SHELL_DEVICE_ERROR
);
159 OutSizeTemp
= OutSize
;
160 Status
= gEfiShellProtocol
->WriteFile (OutFileHandle
, &OutSizeTemp
, OutBuffer
);
161 OutSize
= (UINT32
) OutSizeTemp
;
162 if (EFI_ERROR (Status
)) {
163 ShellPrintHiiEx (-1, -1, NULL
, STRING_TOKEN (STR_FILE_WRITE_FAIL
), gShellDebug1HiiHandle
, L
"efidecompress", OutFileName
, Status
);
164 ShellStatus
= SHELL_DEVICE_ERROR
;
171 ShellCommandLineFreeVarList (Package
);
173 if (InFileHandle
!= NULL
) {
174 gEfiShellProtocol
->CloseFile(InFileHandle
);
176 if (OutFileHandle
!= NULL
) {
177 gEfiShellProtocol
->CloseFile(OutFileHandle
);
180 SHELL_FREE_NON_NULL(InFileName
);
181 SHELL_FREE_NON_NULL(InBuffer
);
182 SHELL_FREE_NON_NULL(OutBuffer
);
183 SHELL_FREE_NON_NULL(ScratchBuffer
);
185 return (ShellStatus
);