]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellDebug1CommandsLib/EfiDecompress.c
MdePkg,ShellPkg: Fix typo in SMBIOS_TABLE_TYPE17 field FirmwareVersion
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / EfiDecompress.c
1 /** @file
2 Main file for EfiDecompress shell Debug1 function.
3
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
7
8 **/
9
10 #include "UefiShellDebug1CommandsLib.h"
11 #include <Protocol/Decompress.h>
12
13
14 /**
15 Function for 'decompress' command.
16
17 @param[in] ImageHandle Handle to the Image (NULL if Internal).
18 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
19 **/
20 SHELL_STATUS
21 EFIAPI
22 ShellCommandRunEfiDecompress (
23 IN EFI_HANDLE ImageHandle,
24 IN EFI_SYSTEM_TABLE *SystemTable
25 )
26 {
27 EFI_STATUS Status;
28 LIST_ENTRY *Package;
29 CHAR16 *ProblemParam;
30 SHELL_STATUS ShellStatus;
31 SHELL_FILE_HANDLE InFileHandle;
32 SHELL_FILE_HANDLE OutFileHandle;
33 UINT32 OutSize;
34 UINTN OutSizeTemp;
35 VOID *OutBuffer;
36 UINTN InSize;
37 VOID *InBuffer;
38 CHAR16 *InFileName;
39 CONST CHAR16 *OutFileName;
40 UINT64 Temp64Bit;
41 UINT32 ScratchSize;
42 VOID *ScratchBuffer;
43 EFI_DECOMPRESS_PROTOCOL *Decompress;
44 CONST CHAR16 *TempParam;
45
46 InFileName = NULL;
47 OutFileName = NULL;
48 OutSize = 0;
49 ScratchSize = 0;
50 ShellStatus = SHELL_SUCCESS;
51 Status = EFI_SUCCESS;
52 OutBuffer = NULL;
53 InBuffer = NULL;
54 ScratchBuffer = NULL;
55 InFileHandle = NULL;
56 OutFileHandle = NULL;
57 Decompress = NULL;
58
59 //
60 // initialize the shell lib (we must be in non-auto-init...)
61 //
62 Status = ShellInitialize();
63 ASSERT_EFI_ERROR(Status);
64
65 Status = CommandInit();
66 ASSERT_EFI_ERROR(Status);
67
68 //
69 // parse the command line
70 //
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;
77 } else {
78 ASSERT(FALSE);
79 }
80 } else {
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;
87 } else {
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;
95 } else {
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;
99 }
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;
103 }
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;
109 }
110 }
111
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;
120 } else {
121 Status = gEfiShellProtocol->ReadFile (InFileHandle, &InSize, InBuffer);
122 ASSERT_EFI_ERROR (Status);
123
124 Status = gBS->LocateProtocol (&gEfiDecompressProtocolGuid, NULL, (VOID**) &Decompress);
125 ASSERT_EFI_ERROR (Status);
126
127 Status = Decompress->GetInfo (Decompress, InBuffer, (UINT32) InSize, &OutSize, &ScratchSize);
128 }
129
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;
133 } else {
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;
138 } else {
139 OutBuffer = AllocateZeroPool(OutSize);
140 ScratchBuffer = AllocateZeroPool(ScratchSize);
141 if (OutBuffer == NULL || ScratchBuffer == NULL) {
142 Status = EFI_OUT_OF_RESOURCES;
143 } else {
144 Status = Decompress->Decompress (Decompress, InBuffer, (UINT32) InSize, OutBuffer, OutSize, ScratchBuffer, ScratchSize);
145 }
146 }
147 }
148
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);
152 } else {
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;
159 }
160 }
161 }
162 }
163 }
164
165 ShellCommandLineFreeVarList (Package);
166 }
167 if (InFileHandle != NULL) {
168 gEfiShellProtocol->CloseFile(InFileHandle);
169 }
170 if (OutFileHandle != NULL) {
171 gEfiShellProtocol->CloseFile(OutFileHandle);
172 }
173
174 SHELL_FREE_NON_NULL(InFileName);
175 SHELL_FREE_NON_NULL(InBuffer);
176 SHELL_FREE_NON_NULL(OutBuffer);
177 SHELL_FREE_NON_NULL(ScratchBuffer);
178
179 return (ShellStatus);
180 }