DmpStore.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /** @file
  2. Main file for DmpStore shell Debug1 function.
  3. (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
  4. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "UefiShellDebug1CommandsLib.h"
  8. typedef enum {
  9. DmpStoreDisplay,
  10. DmpStoreDelete,
  11. DmpStoreSave,
  12. DmpStoreLoad
  13. } DMP_STORE_TYPE;
  14. typedef struct {
  15. UINT32 Signature;
  16. CHAR16 *Name;
  17. EFI_GUID Guid;
  18. UINT32 Attributes;
  19. UINT32 DataSize;
  20. UINT8 *Data;
  21. LIST_ENTRY Link;
  22. } DMP_STORE_VARIABLE;
  23. #define DMP_STORE_VARIABLE_SIGNATURE SIGNATURE_32 ('_', 'd', 's', 's')
  24. /**
  25. Base on the input attribute value to return the attribute string.
  26. @param[in] Atts The input attribute value
  27. @retval The attribute string info.
  28. **/
  29. CHAR16 *
  30. GetAttrType (
  31. IN CONST UINT32 Atts
  32. )
  33. {
  34. UINTN BufLen;
  35. CHAR16 *RetString;
  36. BufLen = 0;
  37. RetString = NULL;
  38. if ((Atts & EFI_VARIABLE_NON_VOLATILE) != 0) {
  39. StrnCatGrow (&RetString, &BufLen, L"+NV", 0);
  40. }
  41. if ((Atts & EFI_VARIABLE_RUNTIME_ACCESS) != 0) {
  42. StrnCatGrow (&RetString, &BufLen, L"+RT+BS", 0);
  43. } else if ((Atts & EFI_VARIABLE_BOOTSERVICE_ACCESS) != 0) {
  44. StrnCatGrow (&RetString, &BufLen, L"+BS", 0);
  45. }
  46. if ((Atts & EFI_VARIABLE_HARDWARE_ERROR_RECORD) != 0) {
  47. StrnCatGrow (&RetString, &BufLen, L"+HR", 0);
  48. }
  49. if ((Atts & EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS) != 0) {
  50. StrnCatGrow (&RetString, &BufLen, L"+AW", 0);
  51. }
  52. if ((Atts & EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS) != 0) {
  53. StrnCatGrow (&RetString, &BufLen, L"+AT", 0);
  54. }
  55. if (RetString == NULL) {
  56. RetString = StrnCatGrow (&RetString, &BufLen, L"Invalid", 0);
  57. }
  58. if ((RetString != NULL) && (RetString[0] == L'+')) {
  59. CopyMem (RetString, RetString + 1, StrSize (RetString + 1));
  60. }
  61. return RetString;
  62. }
  63. /**
  64. Convert binary to hex format string.
  65. @param[in] Buffer The binary data.
  66. @param[in] BufferSize The size in bytes of the binary data.
  67. @param[in, out] HexString Hex format string.
  68. @param[in] HexStringSize The size in bytes of the string.
  69. @return The hex format string.
  70. **/
  71. CHAR16 *
  72. BinaryToHexString (
  73. IN VOID *Buffer,
  74. IN UINTN BufferSize,
  75. IN OUT CHAR16 *HexString,
  76. IN UINTN HexStringSize
  77. )
  78. {
  79. UINTN Index;
  80. UINTN StringIndex;
  81. ASSERT (Buffer != NULL);
  82. ASSERT ((BufferSize * 2 + 1) * sizeof (CHAR16) <= HexStringSize);
  83. for (Index = 0, StringIndex = 0; Index < BufferSize; Index += 1) {
  84. StringIndex +=
  85. UnicodeSPrint (
  86. &HexString[StringIndex],
  87. HexStringSize - StringIndex * sizeof (CHAR16),
  88. L"%02x",
  89. ((UINT8 *)Buffer)[Index]
  90. );
  91. }
  92. return HexString;
  93. }
  94. /**
  95. Load the variable data from file and set to variable data base.
  96. @param[in] FileHandle The file to be read.
  97. @param[in] Name The name of the variables to be loaded.
  98. @param[in] Guid The guid of the variables to be loaded.
  99. @param[out] Found TRUE when at least one variable was loaded and set.
  100. @retval SHELL_DEVICE_ERROR Cannot access the file.
  101. @retval SHELL_VOLUME_CORRUPTED The file is in bad format.
  102. @retval SHELL_OUT_OF_RESOURCES There is not enough memory to perform the operation.
  103. @retval SHELL_SUCCESS Successfully load and set the variables.
  104. **/
  105. SHELL_STATUS
  106. LoadVariablesFromFile (
  107. IN SHELL_FILE_HANDLE FileHandle,
  108. IN CONST CHAR16 *Name,
  109. IN CONST EFI_GUID *Guid,
  110. OUT BOOLEAN *Found
  111. )
  112. {
  113. EFI_STATUS Status;
  114. SHELL_STATUS ShellStatus;
  115. UINT32 NameSize;
  116. UINT32 DataSize;
  117. UINTN BufferSize;
  118. UINTN RemainingSize;
  119. UINT64 Position;
  120. UINT64 FileSize;
  121. LIST_ENTRY List;
  122. DMP_STORE_VARIABLE *Variable;
  123. LIST_ENTRY *Link;
  124. CHAR16 *Attributes;
  125. UINT8 *Buffer;
  126. UINT32 Crc32;
  127. Status = ShellGetFileSize (FileHandle, &FileSize);
  128. if (EFI_ERROR (Status)) {
  129. return SHELL_DEVICE_ERROR;
  130. }
  131. ShellStatus = SHELL_SUCCESS;
  132. InitializeListHead (&List);
  133. Position = 0;
  134. while (Position < FileSize) {
  135. //
  136. // NameSize
  137. //
  138. BufferSize = sizeof (NameSize);
  139. Status = ShellReadFile (FileHandle, &BufferSize, &NameSize);
  140. if (EFI_ERROR (Status) || (BufferSize != sizeof (NameSize))) {
  141. ShellStatus = SHELL_VOLUME_CORRUPTED;
  142. break;
  143. }
  144. //
  145. // DataSize
  146. //
  147. BufferSize = sizeof (DataSize);
  148. Status = ShellReadFile (FileHandle, &BufferSize, &DataSize);
  149. if (EFI_ERROR (Status) || (BufferSize != sizeof (DataSize))) {
  150. ShellStatus = SHELL_VOLUME_CORRUPTED;
  151. break;
  152. }
  153. //
  154. // Name, Guid, Attributes, Data, Crc32
  155. //
  156. RemainingSize = NameSize + sizeof (EFI_GUID) + sizeof (UINT32) + DataSize + sizeof (Crc32);
  157. BufferSize = sizeof (NameSize) + sizeof (DataSize) + RemainingSize;
  158. Buffer = AllocatePool (BufferSize);
  159. if (Buffer == NULL) {
  160. ShellStatus = SHELL_OUT_OF_RESOURCES;
  161. break;
  162. }
  163. BufferSize = RemainingSize;
  164. Status = ShellReadFile (FileHandle, &BufferSize, (UINT32 *)Buffer + 2);
  165. if (EFI_ERROR (Status) || (BufferSize != RemainingSize)) {
  166. ShellStatus = SHELL_VOLUME_CORRUPTED;
  167. FreePool (Buffer);
  168. break;
  169. }
  170. //
  171. // Check Crc32
  172. //
  173. *(UINT32 *)Buffer = NameSize;
  174. *((UINT32 *)Buffer + 1) = DataSize;
  175. BufferSize = RemainingSize + sizeof (NameSize) + sizeof (DataSize) - sizeof (Crc32);
  176. gBS->CalculateCrc32 (
  177. Buffer,
  178. BufferSize,
  179. &Crc32
  180. );
  181. if (Crc32 != *(UINT32 *)(Buffer + BufferSize)) {
  182. FreePool (Buffer);
  183. ShellStatus = SHELL_VOLUME_CORRUPTED;
  184. break;
  185. }
  186. Position += BufferSize + sizeof (Crc32);
  187. Variable = AllocateZeroPool (sizeof (*Variable) + NameSize + DataSize);
  188. if (Variable == NULL) {
  189. FreePool (Buffer);
  190. ShellStatus = SHELL_OUT_OF_RESOURCES;
  191. break;
  192. }
  193. Variable->Signature = DMP_STORE_VARIABLE_SIGNATURE;
  194. Variable->Name = (CHAR16 *)(Variable + 1);
  195. Variable->DataSize = DataSize;
  196. Variable->Data = (UINT8 *)Variable->Name + NameSize;
  197. CopyMem (Variable->Name, Buffer + sizeof (NameSize) + sizeof (DataSize), NameSize);
  198. CopyMem (&Variable->Guid, Buffer + sizeof (NameSize) + sizeof (DataSize) + NameSize, sizeof (EFI_GUID));
  199. CopyMem (&Variable->Attributes, Buffer + sizeof (NameSize) + sizeof (DataSize) + NameSize + sizeof (EFI_GUID), sizeof (UINT32));
  200. CopyMem (Variable->Data, Buffer + sizeof (NameSize) + sizeof (DataSize) + NameSize + sizeof (EFI_GUID) + sizeof (UINT32), DataSize);
  201. InsertTailList (&List, &Variable->Link);
  202. FreePool (Buffer);
  203. }
  204. if ((Position != FileSize) || (ShellStatus != SHELL_SUCCESS)) {
  205. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_LOAD_BAD_FILE), gShellDebug1HiiHandle, L"dmpstore");
  206. if (Position != FileSize) {
  207. ShellStatus = SHELL_VOLUME_CORRUPTED;
  208. }
  209. }
  210. for ( Link = GetFirstNode (&List)
  211. ; !IsNull (&List, Link) && (ShellStatus == SHELL_SUCCESS)
  212. ; Link = GetNextNode (&List, Link)
  213. )
  214. {
  215. Variable = CR (Link, DMP_STORE_VARIABLE, Link, DMP_STORE_VARIABLE_SIGNATURE);
  216. if (((Name == NULL) || gUnicodeCollation->MetaiMatch (gUnicodeCollation, Variable->Name, (CHAR16 *)Name)) &&
  217. ((Guid == NULL) || CompareGuid (&Variable->Guid, Guid))
  218. )
  219. {
  220. Attributes = GetAttrType (Variable->Attributes);
  221. ShellPrintHiiEx (
  222. -1,
  223. -1,
  224. NULL,
  225. STRING_TOKEN (STR_DMPSTORE_HEADER_LINE),
  226. gShellDebug1HiiHandle,
  227. Attributes,
  228. &Variable->Guid,
  229. Variable->Name,
  230. Variable->DataSize
  231. );
  232. SHELL_FREE_NON_NULL (Attributes);
  233. *Found = TRUE;
  234. Status = gRT->SetVariable (
  235. Variable->Name,
  236. &Variable->Guid,
  237. Variable->Attributes,
  238. Variable->DataSize,
  239. Variable->Data
  240. );
  241. if (EFI_ERROR (Status)) {
  242. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_LOAD_GEN_FAIL), gShellDebug1HiiHandle, L"dmpstore", Variable->Name, Status);
  243. }
  244. }
  245. }
  246. for (Link = GetFirstNode (&List); !IsNull (&List, Link); ) {
  247. Variable = CR (Link, DMP_STORE_VARIABLE, Link, DMP_STORE_VARIABLE_SIGNATURE);
  248. Link = RemoveEntryList (&Variable->Link);
  249. FreePool (Variable);
  250. }
  251. return ShellStatus;
  252. }
  253. /**
  254. Append one variable to file.
  255. @param[in] FileHandle The file to be appended.
  256. @param[in] Name The variable name.
  257. @param[in] Guid The variable GUID.
  258. @param[in] Attributes The variable attributes.
  259. @param[in] DataSize The variable data size.
  260. @param[in] Data The variable data.
  261. @retval EFI_OUT_OF_RESOURCES There is not enough memory to perform the operation.
  262. @retval EFI_SUCCESS The variable is appended to file successfully.
  263. @retval others Failed to append the variable to file.
  264. **/
  265. EFI_STATUS
  266. AppendSingleVariableToFile (
  267. IN SHELL_FILE_HANDLE FileHandle,
  268. IN CONST CHAR16 *Name,
  269. IN CONST EFI_GUID *Guid,
  270. IN UINT32 Attributes,
  271. IN UINT32 DataSize,
  272. IN CONST UINT8 *Data
  273. )
  274. {
  275. UINT32 NameSize;
  276. UINT8 *Buffer;
  277. UINT8 *Ptr;
  278. UINTN BufferSize;
  279. EFI_STATUS Status;
  280. NameSize = (UINT32)StrSize (Name);
  281. BufferSize = sizeof (NameSize) + sizeof (DataSize)
  282. + sizeof (*Guid)
  283. + sizeof (Attributes)
  284. + NameSize + DataSize
  285. + sizeof (UINT32);
  286. Buffer = AllocatePool (BufferSize);
  287. if (Buffer == NULL) {
  288. return EFI_OUT_OF_RESOURCES;
  289. }
  290. Ptr = Buffer;
  291. //
  292. // NameSize and DataSize
  293. //
  294. *(UINT32 *)Ptr = NameSize;
  295. Ptr += sizeof (NameSize);
  296. *(UINT32 *)Ptr = DataSize;
  297. Ptr += sizeof (DataSize);
  298. //
  299. // Name
  300. //
  301. CopyMem (Ptr, Name, NameSize);
  302. Ptr += NameSize;
  303. //
  304. // Guid
  305. //
  306. CopyMem (Ptr, Guid, sizeof (*Guid));
  307. Ptr += sizeof (*Guid);
  308. //
  309. // Attributes
  310. //
  311. *(UINT32 *)Ptr = Attributes;
  312. Ptr += sizeof (Attributes);
  313. //
  314. // Data
  315. //
  316. CopyMem (Ptr, Data, DataSize);
  317. Ptr += DataSize;
  318. //
  319. // Crc32
  320. //
  321. gBS->CalculateCrc32 (Buffer, (UINTN)Ptr - (UINTN)Buffer, (UINT32 *)Ptr);
  322. Status = ShellWriteFile (FileHandle, &BufferSize, Buffer);
  323. FreePool (Buffer);
  324. if (!EFI_ERROR (Status) &&
  325. (BufferSize != sizeof (NameSize) + sizeof (DataSize) + sizeof (*Guid) + sizeof (Attributes) + NameSize + DataSize + sizeof (UINT32))
  326. )
  327. {
  328. Status = EFI_DEVICE_ERROR;
  329. }
  330. return Status;
  331. }
  332. /**
  333. Recursive function to display or delete variables.
  334. This function will call itself to create a stack-based list of allt he variables to process,
  335. then fromt he last to the first, they will do either printing or deleting.
  336. This is necessary since once a delete happens GetNextVariableName() will work.
  337. @param[in] Name The variable name of the EFI variable (or NULL).
  338. @param[in] Guid The GUID of the variable set (or NULL).
  339. @param[in] Type The operation type.
  340. @param[in] FileHandle The file to operate on (or NULL).
  341. @param[in] PrevName The previous variable name from GetNextVariableName. L"" to start.
  342. @param[in] FoundVarGuid The previous GUID from GetNextVariableName. ignored at start.
  343. @param[in] FoundOne If a VariableName or Guid was specified and one was printed or
  344. deleted, then set this to TRUE, otherwise ignored.
  345. @param[in] StandardFormatOutput TRUE indicates Standard-Format Output.
  346. @retval SHELL_SUCCESS The operation was successful.
  347. @retval SHELL_OUT_OF_RESOURCES A memorty allocation failed.
  348. @retval SHELL_ABORTED The abort message was received.
  349. @retval SHELL_DEVICE_ERROR UEFI Variable Services returned an error.
  350. @retval SHELL_NOT_FOUND the Name/Guid pair could not be found.
  351. **/
  352. SHELL_STATUS
  353. CascadeProcessVariables (
  354. IN CONST CHAR16 *Name OPTIONAL,
  355. IN CONST EFI_GUID *Guid OPTIONAL,
  356. IN DMP_STORE_TYPE Type,
  357. IN EFI_FILE_PROTOCOL *FileHandle OPTIONAL,
  358. IN CONST CHAR16 *CONST PrevName,
  359. IN EFI_GUID FoundVarGuid,
  360. IN BOOLEAN *FoundOne,
  361. IN BOOLEAN StandardFormatOutput
  362. )
  363. {
  364. EFI_STATUS Status;
  365. CHAR16 *FoundVarName;
  366. UINT8 *DataBuffer;
  367. UINTN DataSize;
  368. UINT32 Atts;
  369. SHELL_STATUS ShellStatus;
  370. UINTN NameSize;
  371. CHAR16 *AttrString;
  372. CHAR16 *HexString;
  373. EFI_STATUS SetStatus;
  374. CONST CHAR16 *GuidName;
  375. if (ShellGetExecutionBreakFlag ()) {
  376. return (SHELL_ABORTED);
  377. }
  378. NameSize = 0;
  379. FoundVarName = NULL;
  380. if (PrevName != NULL) {
  381. StrnCatGrow (&FoundVarName, &NameSize, PrevName, 0);
  382. } else {
  383. FoundVarName = AllocateZeroPool (sizeof (CHAR16));
  384. NameSize = sizeof (CHAR16);
  385. }
  386. Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);
  387. if (Status == EFI_BUFFER_TOO_SMALL) {
  388. SHELL_FREE_NON_NULL (FoundVarName);
  389. FoundVarName = AllocateZeroPool (NameSize);
  390. if (FoundVarName != NULL) {
  391. if (PrevName != NULL) {
  392. StrnCpyS (FoundVarName, NameSize/sizeof (CHAR16), PrevName, NameSize/sizeof (CHAR16) - 1);
  393. }
  394. Status = gRT->GetNextVariableName (&NameSize, FoundVarName, &FoundVarGuid);
  395. } else {
  396. Status = EFI_OUT_OF_RESOURCES;
  397. }
  398. }
  399. //
  400. // No more is fine.
  401. //
  402. if (Status == EFI_NOT_FOUND) {
  403. SHELL_FREE_NON_NULL (FoundVarName);
  404. return (SHELL_SUCCESS);
  405. } else if (EFI_ERROR (Status)) {
  406. SHELL_FREE_NON_NULL (FoundVarName);
  407. return (SHELL_DEVICE_ERROR);
  408. }
  409. //
  410. // Recurse to the next iteration. We know "our" variable's name.
  411. //
  412. ShellStatus = CascadeProcessVariables (Name, Guid, Type, FileHandle, FoundVarName, FoundVarGuid, FoundOne, StandardFormatOutput);
  413. if (ShellGetExecutionBreakFlag () || (ShellStatus == SHELL_ABORTED)) {
  414. SHELL_FREE_NON_NULL (FoundVarName);
  415. return (SHELL_ABORTED);
  416. }
  417. //
  418. // No matter what happened we process our own variable
  419. // Only continue if Guid and VariableName are each either NULL or a match
  420. //
  421. if ( ( (Name == NULL)
  422. || gUnicodeCollation->MetaiMatch (gUnicodeCollation, FoundVarName, (CHAR16 *)Name))
  423. && ( (Guid == NULL)
  424. || CompareGuid (&FoundVarGuid, Guid))
  425. )
  426. {
  427. DataSize = 0;
  428. DataBuffer = NULL;
  429. //
  430. // do the print or delete
  431. //
  432. *FoundOne = TRUE;
  433. Status = gRT->GetVariable (FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);
  434. if (Status == EFI_BUFFER_TOO_SMALL) {
  435. SHELL_FREE_NON_NULL (DataBuffer);
  436. DataBuffer = AllocatePool (DataSize);
  437. if (DataBuffer == NULL) {
  438. Status = EFI_OUT_OF_RESOURCES;
  439. } else {
  440. Status = gRT->GetVariable (FoundVarName, &FoundVarGuid, &Atts, &DataSize, DataBuffer);
  441. }
  442. }
  443. //
  444. // Last error check then print this variable out.
  445. //
  446. if (Type == DmpStoreDisplay) {
  447. if (!EFI_ERROR (Status) && (DataBuffer != NULL) && (FoundVarName != NULL)) {
  448. AttrString = GetAttrType (Atts);
  449. if (StandardFormatOutput) {
  450. HexString = AllocatePool ((DataSize * 2 + 1) * sizeof (CHAR16));
  451. if (HexString != NULL) {
  452. ShellPrintHiiEx (
  453. -1,
  454. -1,
  455. NULL,
  456. STRING_TOKEN (STR_DMPSTORE_VAR_SFO),
  457. gShellDebug1HiiHandle,
  458. FoundVarName,
  459. &FoundVarGuid,
  460. Atts,
  461. DataSize,
  462. BinaryToHexString (
  463. DataBuffer,
  464. DataSize,
  465. HexString,
  466. (DataSize * 2 + 1) * sizeof (CHAR16)
  467. )
  468. );
  469. FreePool (HexString);
  470. } else {
  471. Status = EFI_OUT_OF_RESOURCES;
  472. }
  473. } else {
  474. Status = gEfiShellProtocol->GetGuidName (&FoundVarGuid, &GuidName);
  475. if (EFI_ERROR (Status)) {
  476. ShellPrintHiiEx (
  477. -1,
  478. -1,
  479. NULL,
  480. STRING_TOKEN (STR_DMPSTORE_HEADER_LINE),
  481. gShellDebug1HiiHandle,
  482. AttrString,
  483. &FoundVarGuid,
  484. FoundVarName,
  485. DataSize
  486. );
  487. } else {
  488. ShellPrintHiiEx (
  489. -1,
  490. -1,
  491. NULL,
  492. STRING_TOKEN (STR_DMPSTORE_HEADER_LINE2),
  493. gShellDebug1HiiHandle,
  494. AttrString,
  495. GuidName,
  496. FoundVarName,
  497. DataSize
  498. );
  499. }
  500. DumpHex (2, 0, DataSize, DataBuffer);
  501. }
  502. SHELL_FREE_NON_NULL (AttrString);
  503. }
  504. } else if (Type == DmpStoreSave) {
  505. if (!EFI_ERROR (Status) && (DataBuffer != NULL) && (FoundVarName != NULL)) {
  506. AttrString = GetAttrType (Atts);
  507. ShellPrintHiiEx (
  508. -1,
  509. -1,
  510. NULL,
  511. STRING_TOKEN (STR_DMPSTORE_HEADER_LINE),
  512. gShellDebug1HiiHandle,
  513. AttrString,
  514. &FoundVarGuid,
  515. FoundVarName,
  516. DataSize
  517. );
  518. Status = AppendSingleVariableToFile (
  519. FileHandle,
  520. FoundVarName,
  521. &FoundVarGuid,
  522. Atts,
  523. (UINT32)DataSize,
  524. DataBuffer
  525. );
  526. SHELL_FREE_NON_NULL (AttrString);
  527. }
  528. } else if (Type == DmpStoreDelete) {
  529. //
  530. // We only need name to delete it...
  531. //
  532. SetStatus = gRT->SetVariable (FoundVarName, &FoundVarGuid, Atts, 0, NULL);
  533. if (StandardFormatOutput) {
  534. if (SetStatus == EFI_SUCCESS) {
  535. ShellPrintHiiEx (
  536. -1,
  537. -1,
  538. NULL,
  539. STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_NG_SFO),
  540. gShellDebug1HiiHandle,
  541. FoundVarName,
  542. &FoundVarGuid
  543. );
  544. }
  545. } else {
  546. ShellPrintHiiEx (
  547. -1,
  548. -1,
  549. NULL,
  550. STRING_TOKEN (STR_DMPSTORE_DELETE_LINE),
  551. gShellDebug1HiiHandle,
  552. &FoundVarGuid,
  553. FoundVarName,
  554. SetStatus
  555. );
  556. }
  557. }
  558. SHELL_FREE_NON_NULL (DataBuffer);
  559. }
  560. SHELL_FREE_NON_NULL (FoundVarName);
  561. if (Status == EFI_DEVICE_ERROR) {
  562. ShellStatus = SHELL_DEVICE_ERROR;
  563. } else if (Status == EFI_SECURITY_VIOLATION) {
  564. ShellStatus = SHELL_SECURITY_VIOLATION;
  565. } else if (EFI_ERROR (Status)) {
  566. ShellStatus = SHELL_NOT_READY;
  567. }
  568. return (ShellStatus);
  569. }
  570. /**
  571. Function to display or delete variables. This will set up and call into the recursive function.
  572. @param[in] Name The variable name of the EFI variable (or NULL).
  573. @param[in] Guid The GUID of the variable set (or NULL).
  574. @param[in] Type The operation type.
  575. @param[in] FileHandle The file to save or load variables.
  576. @param[in] StandardFormatOutput TRUE indicates Standard-Format Output.
  577. @retval SHELL_SUCCESS The operation was successful.
  578. @retval SHELL_OUT_OF_RESOURCES A memorty allocation failed.
  579. @retval SHELL_ABORTED The abort message was received.
  580. @retval SHELL_DEVICE_ERROR UEFI Variable Services returned an error.
  581. @retval SHELL_NOT_FOUND the Name/Guid pair could not be found.
  582. **/
  583. SHELL_STATUS
  584. ProcessVariables (
  585. IN CONST CHAR16 *Name OPTIONAL,
  586. IN CONST EFI_GUID *Guid OPTIONAL,
  587. IN DMP_STORE_TYPE Type,
  588. IN SHELL_FILE_HANDLE FileHandle OPTIONAL,
  589. IN BOOLEAN StandardFormatOutput
  590. )
  591. {
  592. SHELL_STATUS ShellStatus;
  593. BOOLEAN Found;
  594. EFI_GUID FoundVarGuid;
  595. Found = FALSE;
  596. ShellStatus = SHELL_SUCCESS;
  597. ZeroMem (&FoundVarGuid, sizeof (EFI_GUID));
  598. if (StandardFormatOutput) {
  599. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_SFO_HEADER), gShellDebug1HiiHandle, L"dmpstore");
  600. }
  601. if (Type == DmpStoreLoad) {
  602. ShellStatus = LoadVariablesFromFile (FileHandle, Name, Guid, &Found);
  603. } else {
  604. ShellStatus = CascadeProcessVariables (Name, Guid, Type, FileHandle, NULL, FoundVarGuid, &Found, StandardFormatOutput);
  605. }
  606. if (!Found) {
  607. if (ShellStatus == SHELL_OUT_OF_RESOURCES) {
  608. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellDebug1HiiHandle, L"dmpstore");
  609. return (ShellStatus);
  610. } else if ((Name != NULL) && (Guid == NULL)) {
  611. if (StandardFormatOutput) {
  612. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_N_SFO), gShellDebug1HiiHandle, Name);
  613. } else {
  614. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_N), gShellDebug1HiiHandle, L"dmpstore", Name);
  615. }
  616. } else if ((Name != NULL) && (Guid != NULL)) {
  617. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_GN), gShellDebug1HiiHandle, L"dmpstore", Guid, Name);
  618. } else if ((Name == NULL) && (Guid == NULL)) {
  619. if (StandardFormatOutput) {
  620. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_SFO), gShellDebug1HiiHandle);
  621. } else {
  622. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND), gShellDebug1HiiHandle, L"dmpstore");
  623. }
  624. } else if ((Name == NULL) && (Guid != NULL)) {
  625. if (StandardFormatOutput) {
  626. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_G_SFO), gShellDebug1HiiHandle, Guid);
  627. } else {
  628. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_NO_VAR_FOUND_G), gShellDebug1HiiHandle, L"dmpstore", Guid);
  629. }
  630. }
  631. return (SHELL_NOT_FOUND);
  632. }
  633. return (ShellStatus);
  634. }
  635. STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
  636. { L"-d", TypeFlag },
  637. { L"-l", TypeValue },
  638. { L"-s", TypeValue },
  639. { L"-all", TypeFlag },
  640. { L"-guid", TypeValue },
  641. { L"-sfo", TypeFlag },
  642. { NULL, TypeMax }
  643. };
  644. /**
  645. Function for 'dmpstore' command.
  646. @param[in] ImageHandle Handle to the Image (NULL if Internal).
  647. @param[in] SystemTable Pointer to the System Table (NULL if Internal).
  648. **/
  649. SHELL_STATUS
  650. EFIAPI
  651. ShellCommandRunDmpStore (
  652. IN EFI_HANDLE ImageHandle,
  653. IN EFI_SYSTEM_TABLE *SystemTable
  654. )
  655. {
  656. EFI_STATUS Status;
  657. RETURN_STATUS RStatus;
  658. LIST_ENTRY *Package;
  659. CHAR16 *ProblemParam;
  660. SHELL_STATUS ShellStatus;
  661. CONST CHAR16 *GuidStr;
  662. CONST CHAR16 *File;
  663. EFI_GUID *Guid;
  664. EFI_GUID GuidData;
  665. CONST CHAR16 *Name;
  666. DMP_STORE_TYPE Type;
  667. SHELL_FILE_HANDLE FileHandle;
  668. EFI_FILE_INFO *FileInfo;
  669. BOOLEAN StandardFormatOutput;
  670. ShellStatus = SHELL_SUCCESS;
  671. Package = NULL;
  672. FileHandle = NULL;
  673. File = NULL;
  674. Type = DmpStoreDisplay;
  675. StandardFormatOutput = FALSE;
  676. Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);
  677. if (EFI_ERROR (Status)) {
  678. if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {
  679. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"dmpstore", ProblemParam);
  680. FreePool (ProblemParam);
  681. ShellStatus = SHELL_INVALID_PARAMETER;
  682. } else {
  683. ASSERT (FALSE);
  684. }
  685. } else {
  686. if (ShellCommandLineGetCount (Package) > 2) {
  687. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"dmpstore");
  688. ShellStatus = SHELL_INVALID_PARAMETER;
  689. } else if (ShellCommandLineGetFlag (Package, L"-all") && ShellCommandLineGetFlag (Package, L"-guid")) {
  690. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"dmpstore", L"-all", L"-guid");
  691. ShellStatus = SHELL_INVALID_PARAMETER;
  692. } else if (ShellCommandLineGetFlag (Package, L"-s") && ShellCommandLineGetFlag (Package, L"-l")) {
  693. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"dmpstore", L"-l", L"-s");
  694. ShellStatus = SHELL_INVALID_PARAMETER;
  695. } else if ((ShellCommandLineGetFlag (Package, L"-s") || ShellCommandLineGetFlag (Package, L"-l")) && ShellCommandLineGetFlag (Package, L"-d")) {
  696. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"dmpstore", L"-l or -s", L"-d");
  697. ShellStatus = SHELL_INVALID_PARAMETER;
  698. } else if ((ShellCommandLineGetFlag (Package, L"-s") || ShellCommandLineGetFlag (Package, L"-l")) && ShellCommandLineGetFlag (Package, L"-sfo")) {
  699. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CONFLICT), gShellDebug1HiiHandle, L"dmpstore", L"-l or -s", L"-sfo");
  700. ShellStatus = SHELL_INVALID_PARAMETER;
  701. } else {
  702. //
  703. // Determine the GUID to search for based on -all and -guid parameters
  704. //
  705. if (!ShellCommandLineGetFlag (Package, L"-all")) {
  706. GuidStr = ShellCommandLineGetValue (Package, L"-guid");
  707. if (GuidStr != NULL) {
  708. RStatus = StrToGuid (GuidStr, &GuidData);
  709. if (RETURN_ERROR (RStatus) || (GuidStr[GUID_STRING_LENGTH] != L'\0')) {
  710. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellDebug1HiiHandle, L"dmpstore", GuidStr);
  711. ShellStatus = SHELL_INVALID_PARAMETER;
  712. }
  713. Guid = &GuidData;
  714. } else {
  715. Guid = &gEfiGlobalVariableGuid;
  716. }
  717. } else {
  718. Guid = NULL;
  719. }
  720. //
  721. // Get the Name of the variable to find
  722. //
  723. Name = ShellCommandLineGetRawValue (Package, 1);
  724. if (ShellStatus == SHELL_SUCCESS) {
  725. if (ShellCommandLineGetFlag (Package, L"-s")) {
  726. Type = DmpStoreSave;
  727. File = ShellCommandLineGetValue (Package, L"-s");
  728. if (File == NULL) {
  729. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"dmpstore", L"-s");
  730. ShellStatus = SHELL_INVALID_PARAMETER;
  731. } else {
  732. Status = ShellOpenFileByName (File, &FileHandle, EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
  733. if (!EFI_ERROR (Status)) {
  734. //
  735. // Delete existing file, but do not delete existing directory
  736. //
  737. FileInfo = ShellGetFileInfo (FileHandle);
  738. if (FileInfo == NULL) {
  739. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"dmpstore", File);
  740. Status = EFI_DEVICE_ERROR;
  741. } else {
  742. if ((FileInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
  743. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_IS_DIRECTORY), gShellDebug1HiiHandle, L"dmpstore", File);
  744. Status = EFI_INVALID_PARAMETER;
  745. } else {
  746. Status = ShellDeleteFile (&FileHandle);
  747. if (EFI_ERROR (Status)) {
  748. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_DELETE_FAIL), gShellDebug1HiiHandle, L"dmpstore", File);
  749. }
  750. }
  751. FreePool (FileInfo);
  752. }
  753. } else if (Status == EFI_NOT_FOUND) {
  754. //
  755. // Good when file doesn't exist
  756. //
  757. Status = EFI_SUCCESS;
  758. } else {
  759. //
  760. // Otherwise it's bad.
  761. //
  762. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"dmpstore", File);
  763. }
  764. if (!EFI_ERROR (Status)) {
  765. Status = ShellOpenFileByName (File, &FileHandle, EFI_FILE_MODE_CREATE | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ, 0);
  766. if (EFI_ERROR (Status)) {
  767. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"dmpstore", File);
  768. }
  769. }
  770. if (EFI_ERROR (Status)) {
  771. ShellStatus = SHELL_INVALID_PARAMETER;
  772. }
  773. }
  774. } else if (ShellCommandLineGetFlag (Package, L"-l")) {
  775. Type = DmpStoreLoad;
  776. File = ShellCommandLineGetValue (Package, L"-l");
  777. if (File == NULL) {
  778. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_VALUE), gShellDebug1HiiHandle, L"dmpstore", L"-l");
  779. ShellStatus = SHELL_INVALID_PARAMETER;
  780. } else {
  781. Status = ShellOpenFileByName (File, &FileHandle, EFI_FILE_MODE_READ, 0);
  782. if (EFI_ERROR (Status)) {
  783. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"dmpstore", File);
  784. ShellStatus = SHELL_INVALID_PARAMETER;
  785. } else {
  786. FileInfo = ShellGetFileInfo (FileHandle);
  787. if (FileInfo == NULL) {
  788. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"dmpstore", File);
  789. ShellStatus = SHELL_DEVICE_ERROR;
  790. } else {
  791. if ((FileInfo->Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY) {
  792. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_IS_DIRECTORY), gShellDebug1HiiHandle, L"dmpstore", File);
  793. ShellStatus = SHELL_INVALID_PARAMETER;
  794. }
  795. FreePool (FileInfo);
  796. }
  797. }
  798. }
  799. } else if (ShellCommandLineGetFlag (Package, L"-d")) {
  800. Type = DmpStoreDelete;
  801. }
  802. if (ShellCommandLineGetFlag (Package, L"-sfo")) {
  803. StandardFormatOutput = TRUE;
  804. }
  805. }
  806. if (ShellStatus == SHELL_SUCCESS) {
  807. if (Type == DmpStoreSave) {
  808. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_SAVE), gShellDebug1HiiHandle, File);
  809. } else if (Type == DmpStoreLoad) {
  810. ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_DMPSTORE_LOAD), gShellDebug1HiiHandle, File);
  811. }
  812. ShellStatus = ProcessVariables (Name, Guid, Type, FileHandle, StandardFormatOutput);
  813. if ((Type == DmpStoreLoad) || (Type == DmpStoreSave)) {
  814. ShellCloseFile (&FileHandle);
  815. }
  816. }
  817. }
  818. }
  819. if (Package != NULL) {
  820. ShellCommandLineFreeVarList (Package);
  821. }
  822. return ShellStatus;
  823. }