1 /** @file 2 Main file for Dblk shell Debug1 function. 3 4 (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR> 5 Copyright (c) 2005 - 2011, 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 10 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. 13 14 **/ 15 16 #include "UefiShellDebug1CommandsLib.h" 17 #include <Protocol/BlockIo.h> 18 19 /** 20 Display blocks to the screen. 21 22 @param[in] DevPath The device path to get the blocks from. 23 @param[in] Lba The Lba number to start from. 24 @param[in] BlockCount How many blocks to display. 25 26 @retval SHELL_SUCCESS The display was successful. 27 **/ 28 SHELL_STATUS 29 DisplayTheBlocks( 30 IN CONST EFI_DEVICE_PATH_PROTOCOL *DevPath, 31 IN CONST UINT64 Lba, 32 IN CONST UINT8 BlockCount 33 ) 34 { 35 EFI_BLOCK_IO_PROTOCOL *BlockIo; 36 EFI_HANDLE BlockIoHandle; 37 EFI_STATUS Status; 38 SHELL_STATUS ShellStatus; 39 UINT8 *Buffer; 40 UINTN BufferSize; 41 42 ShellStatus = SHELL_SUCCESS; 43 44 Status = gBS->LocateDevicePath(&gEfiBlockIoProtocolGuid, (EFI_DEVICE_PATH_PROTOCOL **)&DevPath, &BlockIoHandle); 45 if (EFI_ERROR(Status)) { 46 return (SHELL_NOT_FOUND); 47 } 48 49 Status = gBS->OpenProtocol(BlockIoHandle, &gEfiBlockIoProtocolGuid, (VOID**)&BlockIo, gImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL); 50 if (EFI_ERROR(Status)) { 51 return (SHELL_NOT_FOUND); 52 } 53 54 BufferSize = BlockIo->Media->BlockSize * BlockCount; 55 if (BufferSize > 0) { 56 Buffer = AllocateZeroPool(BufferSize); 57 } else { 58 ShellPrintEx(-1,-1,L" BlockSize: 0x%08x, BlockCount: 0x%08x\r\n", BlockIo->Media->BlockSize, BlockCount); 59 Buffer = NULL; 60 } 61 62 Status = BlockIo->ReadBlocks(BlockIo, BlockIo->Media->MediaId, Lba, BufferSize, Buffer); 63 if (!EFI_ERROR(Status) && Buffer != NULL) { 64 ShellPrintHiiEx( 65 -1, 66 -1, 67 NULL, 68 STRING_TOKEN (STR_DBLK_HEADER), 69 gShellDebug1HiiHandle, 70 Lba, 71 BufferSize, 72 BlockIo 73 ); 74 75 DumpHex(2,0,BufferSize,Buffer); 76 } else { 77 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_READ_FAIL), gShellDebug1HiiHandle, L"dblk", L"BlockIo"); 78 ShellStatus = SHELL_DEVICE_ERROR; 79 } 80 81 if (Buffer != NULL) { 82 FreePool(Buffer); 83 } 84 85 gBS->CloseProtocol(BlockIoHandle, &gEfiBlockIoProtocolGuid, gImageHandle, NULL); 86 return (ShellStatus); 87 } 88 89 /** 90 Function for 'dblk' command. 91 92 @param[in] ImageHandle Handle to the Image (NULL if Internal). 93 @param[in] SystemTable Pointer to the System Table (NULL if Internal). 94 **/ 95 SHELL_STATUS 96 EFIAPI 97 ShellCommandRunDblk ( 98 IN EFI_HANDLE ImageHandle, 99 IN EFI_SYSTEM_TABLE *SystemTable 100 ) 101 { 102 EFI_STATUS Status; 103 LIST_ENTRY *Package; 104 CHAR16 *ProblemParam; 105 SHELL_STATUS ShellStatus; 106 CONST CHAR16 *BlockName; 107 CONST CHAR16 *LbaString; 108 CONST CHAR16 *BlockCountString; 109 UINT64 Lba; 110 UINT64 BlockCount; 111 EFI_DEVICE_PATH_PROTOCOL *DevPath; 112 113 ShellStatus = SHELL_SUCCESS; 114 Status = EFI_SUCCESS; 115 116 // 117 // initialize the shell lib (we must be in non-auto-init...) 118 // 119 Status = ShellInitialize(); 120 ASSERT_EFI_ERROR(Status); 121 122 Status = CommandInit(); 123 ASSERT_EFI_ERROR(Status); 124 125 // 126 // parse the command line 127 // 128 Status = ShellCommandLineParse (EmptyParamList, &Package, &ProblemParam, TRUE); 129 if (EFI_ERROR(Status)) { 130 if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) { 131 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"dblk", ProblemParam); 132 FreePool(ProblemParam); 133 ShellStatus = SHELL_INVALID_PARAMETER; 134 } else { 135 ASSERT(FALSE); 136 } 137 } else { 138 if (ShellCommandLineGetCount(Package) > 4) { 139 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"dblk"); 140 ShellStatus = SHELL_INVALID_PARAMETER; 141 } else if (ShellCommandLineGetCount(Package) < 2) { 142 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"dblk"); 143 ShellStatus = SHELL_INVALID_PARAMETER; 144 } else { 145 // 146 // Parse the params 147 // 148 BlockName = ShellCommandLineGetRawValue(Package, 1); 149 LbaString = ShellCommandLineGetRawValue(Package, 2); 150 BlockCountString = ShellCommandLineGetRawValue(Package, 3); 151 152 if (LbaString == NULL) { 153 Lba = 0; 154 } else { 155 if (!ShellIsHexOrDecimalNumber(LbaString, TRUE, FALSE)) { 156 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", LbaString); 157 ShellStatus = SHELL_INVALID_PARAMETER; 158 } 159 ShellConvertStringToUint64(LbaString, &Lba, TRUE, FALSE); 160 } 161 162 if (BlockCountString == NULL) { 163 BlockCount = 1; 164 } else { 165 if (!ShellIsHexOrDecimalNumber(BlockCountString, TRUE, FALSE)) { 166 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", BlockCountString); 167 ShellStatus = SHELL_INVALID_PARAMETER; 168 } 169 ShellConvertStringToUint64(BlockCountString, &BlockCount, TRUE, FALSE); 170 if (BlockCount > 0x10) { 171 BlockCount = 0x10; 172 } else if (BlockCount == 0) { 173 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", BlockCountString); 174 ShellStatus = SHELL_INVALID_PARAMETER; 175 } 176 } 177 178 if (ShellStatus == SHELL_SUCCESS) { 179 // 180 // do the work if we have a valid block identifier 181 // 182 if (gEfiShellProtocol->GetDevicePathFromMap(BlockName) == NULL) { 183 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dblk", BlockName); 184 ShellStatus = SHELL_INVALID_PARAMETER; 185 } else { 186 DevPath = (EFI_DEVICE_PATH_PROTOCOL*)gEfiShellProtocol->GetDevicePathFromMap(BlockName); 187 if (gBS->LocateDevicePath(&gEfiBlockIoProtocolGuid, &DevPath, NULL) == EFI_NOT_FOUND) { 188 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MAP_PROTOCOL), gShellDebug1HiiHandle, L"dblk", BlockName, L"BlockIo"); 189 ShellStatus = SHELL_INVALID_PARAMETER; 190 } else { 191 ShellStatus = DisplayTheBlocks(gEfiShellProtocol->GetDevicePathFromMap(BlockName), Lba, (UINT8)BlockCount); 192 } 193 } 194 } 195 } 196 197 ShellCommandLineFreeVarList (Package); 198 } 199 return (ShellStatus); 200 } 201