TianoCore EDK2 master
Loading...
Searching...
No Matches
Dblk.c
Go to the documentation of this file.
1
11#include <Protocol/BlockIo.h>
12
25 IN CONST UINT64 Lba,
26 IN CONST UINT8 BlockCount
27 )
28{
29 EFI_BLOCK_IO_PROTOCOL *BlockIo;
30 EFI_HANDLE BlockIoHandle;
31 EFI_STATUS Status;
32 SHELL_STATUS ShellStatus;
33 UINT8 *Buffer;
34 UINT8 *OriginalBuffer;
35 UINTN BufferSize;
36
37 ShellStatus = SHELL_SUCCESS;
38
39 Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, (EFI_DEVICE_PATH_PROTOCOL **)&DevPath, &BlockIoHandle);
40 if (EFI_ERROR (Status)) {
41 return (SHELL_NOT_FOUND);
42 }
43
44 Status = gBS->OpenProtocol (BlockIoHandle, &gEfiBlockIoProtocolGuid, (VOID **)&BlockIo, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
45 if (EFI_ERROR (Status)) {
46 return (SHELL_NOT_FOUND);
47 }
48
49 BufferSize = BlockIo->Media->BlockSize * BlockCount;
50 if (BlockIo->Media->IoAlign == 0) {
51 BlockIo->Media->IoAlign = 1;
52 }
53
54 if (BufferSize > 0) {
55 OriginalBuffer = AllocateZeroPool (BufferSize + BlockIo->Media->IoAlign);
56 Buffer = ALIGN_POINTER (OriginalBuffer, BlockIo->Media->IoAlign);
57 } else {
58 ShellPrintEx (-1, -1, L" BlockSize: 0x%08x, BlockCount: 0x%08x\r\n", BlockIo->Media->BlockSize, BlockCount);
59 OriginalBuffer = NULL;
60 Buffer = NULL;
61 }
62
63 Status = BlockIo->ReadBlocks (BlockIo, BlockIo->Media->MediaId, Lba, BufferSize, Buffer);
64 if (!EFI_ERROR (Status) && (Buffer != NULL)) {
66 -1,
67 -1,
68 NULL,
69 STRING_TOKEN (STR_DBLK_HEADER),
70 gShellDebug1HiiHandle,
71 Lba,
72 BufferSize,
73 BlockIo
74 );
75
76 DumpHex (2, 0, BufferSize, Buffer);
77 } else {
78 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_READ_FAIL), gShellDebug1HiiHandle, L"dblk", L"BlockIo");
79 ShellStatus = SHELL_DEVICE_ERROR;
80 }
81
82 if (OriginalBuffer != NULL) {
83 FreePool (OriginalBuffer);
84 }
85
86 gBS->CloseProtocol (BlockIoHandle, &gEfiBlockIoProtocolGuid, gImageHandle, NULL);
87 return (ShellStatus);
88}
89
97EFIAPI
99 IN EFI_HANDLE ImageHandle,
100 IN EFI_SYSTEM_TABLE *SystemTable
101 )
102{
103 EFI_STATUS Status;
104 LIST_ENTRY *Package;
105 CHAR16 *ProblemParam;
106 SHELL_STATUS ShellStatus;
107 CONST CHAR16 *BlockName;
108 CONST CHAR16 *LbaString;
109 CONST CHAR16 *BlockCountString;
110 UINT64 Lba;
111 UINT64 BlockCount;
113
114 Lba = 0;
115 BlockCount = 0;
116 ShellStatus = SHELL_SUCCESS;
117 Status = EFI_SUCCESS;
118
119 //
120 // initialize the shell lib (we must be in non-auto-init...)
121 //
122 Status = ShellInitialize ();
123 ASSERT_EFI_ERROR (Status);
124
125 Status = CommandInit ();
126 ASSERT_EFI_ERROR (Status);
127
128 //
129 // parse the command line
130 //
131 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE);
132 if (EFI_ERROR (Status)) {
133 if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
134 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"dblk", ProblemParam);
135 FreePool (ProblemParam);
136 ShellStatus = SHELL_INVALID_PARAMETER;
137 } else {
138 ASSERT (FALSE);
139 }
140 } else {
141 if (ShellCommandLineGetCount (Package) > 4) {
142 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"dblk");
143 ShellStatus = SHELL_INVALID_PARAMETER;
144 } else if (ShellCommandLineGetCount (Package) < 2) {
145 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"dblk");
146 ShellStatus = SHELL_INVALID_PARAMETER;
147 } else {
148 //
149 // Parse the params
150 //
151 BlockName = ShellCommandLineGetRawValue (Package, 1);
152 LbaString = ShellCommandLineGetRawValue (Package, 2);
153 BlockCountString = ShellCommandLineGetRawValue (Package, 3);
154
155 if (LbaString == NULL) {
156 Lba = 0;
157 } else {
158 if (!ShellIsHexOrDecimalNumber (LbaString, TRUE, FALSE)) {
159 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", LbaString);
160 ShellStatus = SHELL_INVALID_PARAMETER;
161 }
162
163 if (EFI_ERROR (ShellConvertStringToUint64 (LbaString, &Lba, TRUE, FALSE))) {
164 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", LbaString);
165 ShellStatus = SHELL_INVALID_PARAMETER;
166 }
167 }
168
169 if (BlockCountString == NULL) {
170 BlockCount = 1;
171 } else {
172 if (!ShellIsHexOrDecimalNumber (BlockCountString, TRUE, FALSE)) {
173 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", BlockCountString);
174 ShellStatus = SHELL_INVALID_PARAMETER;
175 }
176
177 if (!EFI_ERROR (ShellConvertStringToUint64 (BlockCountString, &BlockCount, TRUE, FALSE))) {
178 if (BlockCount > 0x10) {
179 BlockCount = 0x10;
180 } else if (BlockCount == 0) {
181 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", BlockCountString);
182 ShellStatus = SHELL_INVALID_PARAMETER;
183 }
184 }
185 }
186
187 if (ShellStatus == SHELL_SUCCESS) {
188 //
189 // do the work if we have a valid block identifier
190 //
191 if ((BlockName == NULL) || (gEfiShellProtocol->GetDevicePathFromMap (BlockName) == NULL)) {
192 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", BlockName);
193 ShellStatus = SHELL_INVALID_PARAMETER;
194 } else {
195 DevPath = (EFI_DEVICE_PATH_PROTOCOL *)gEfiShellProtocol->GetDevicePathFromMap (BlockName);
196 if (gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevPath, NULL) == EFI_NOT_FOUND) {
197 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_MAP_PROTOCOL), gShellDebug1HiiHandle, L"dblk", BlockName, L"BlockIo");
198 ShellStatus = SHELL_INVALID_PARAMETER;
199 } else {
200 ShellStatus = DisplayTheBlocks (gEfiShellProtocol->GetDevicePathFromMap (BlockName), Lba, (UINT8)BlockCount);
201 }
202 }
203 }
204 }
205
207 }
208
209 return (ShellStatus);
210}
UINT64 UINTN
SHELL_STATUS DisplayTheBlocks(IN CONST EFI_DEVICE_PATH_PROTOCOL *DevPath, IN CONST UINT64 Lba, IN CONST UINT8 BlockCount)
Definition: Dblk.c:23
SHELL_STATUS EFIAPI ShellCommandRunDblk(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: Dblk.c:98
VOID *EFIAPI AllocateZeroPool(IN UINTN AllocationSize)
VOID EFIAPI FreePool(IN VOID *Buffer)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define ALIGN_POINTER(Pointer, Alignment)
Definition: Base.h:963
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_DEVICE_ERROR
Definition: Shell.h:63
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
EFI_STATUS EFIAPI CommandInit(VOID)
VOID EFIAPI DumpHex(IN UINTN Indent, IN UINTN Offset, IN UINTN DataSize, IN VOID *UserData)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
BOOLEAN EFIAPI ShellIsHexOrDecimalNumber(IN CONST CHAR16 *String, IN CONST BOOLEAN ForceHex, IN CONST BOOLEAN StopAtSpace)
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
EFI_STATUS EFIAPI ShellPrintEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR16 *Format,...)
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellConvertStringToUint64(IN CONST CHAR16 *String, OUT UINT64 *Value, IN CONST BOOLEAN ForceHex, IN CONST BOOLEAN StopAtSpace)
SHELL_PARAM_ITEM EmptyParamList[]
Helper structure for no parameters (besides -? and -b)
Definition: UefiShellLib.c:19
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
#define STRING_TOKEN(t)
EFI_BLOCK_IO_MEDIA * Media
Definition: BlockIo.h:224
UINT32 BlockSize
Definition: BlockIo.h:167