UefiShellCommandLib.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  1. /** @file
  2. Provides interface to shell internal functions for shell commands.
  3. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  4. (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
  5. (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "UefiShellCommandLib.h"
  9. // STATIC local variables
  10. STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList;
  11. STATIC SCRIPT_FILE_LIST mScriptList;
  12. STATIC ALIAS_LIST mAliasList;
  13. STATIC BOOLEAN mEchoState;
  14. STATIC BOOLEAN mExitRequested;
  15. STATIC UINT64 mExitCode;
  16. STATIC BOOLEAN mExitScript;
  17. STATIC CHAR16 *mProfileList;
  18. STATIC UINTN mProfileListSize;
  19. STATIC UINTN mFsMaxCount = 0;
  20. STATIC UINTN mBlkMaxCount = 0;
  21. STATIC BUFFER_LIST mFileHandleList;
  22. STATIC CONST CHAR8 Hex[] = {
  23. '0',
  24. '1',
  25. '2',
  26. '3',
  27. '4',
  28. '5',
  29. '6',
  30. '7',
  31. '8',
  32. '9',
  33. 'A',
  34. 'B',
  35. 'C',
  36. 'D',
  37. 'E',
  38. 'F'
  39. };
  40. // global variables required by library class.
  41. EFI_UNICODE_COLLATION_PROTOCOL *gUnicodeCollation = NULL;
  42. SHELL_MAP_LIST gShellMapList;
  43. SHELL_MAP_LIST *gShellCurMapping = NULL;
  44. CONST CHAR16 *SupportLevel[] = {
  45. L"Minimal",
  46. L"Scripting",
  47. L"Basic",
  48. L"Interactive"
  49. };
  50. /**
  51. Function to make sure that the global protocol pointers are valid.
  52. must be called after constructor before accessing the pointers.
  53. **/
  54. EFI_STATUS
  55. EFIAPI
  56. CommandInit (
  57. VOID
  58. )
  59. {
  60. UINTN NumHandles;
  61. EFI_HANDLE *Handles;
  62. EFI_UNICODE_COLLATION_PROTOCOL *Uc;
  63. CHAR8 *BestLanguage;
  64. UINTN Index;
  65. EFI_STATUS Status;
  66. CHAR8 *PlatformLang;
  67. if (gUnicodeCollation == NULL) {
  68. GetEfiGlobalVariable2 (EFI_PLATFORM_LANG_VARIABLE_NAME, (VOID **)&PlatformLang, NULL);
  69. Status = gBS->LocateHandleBuffer (
  70. ByProtocol,
  71. &gEfiUnicodeCollation2ProtocolGuid,
  72. NULL,
  73. &NumHandles,
  74. &Handles
  75. );
  76. if (EFI_ERROR (Status)) {
  77. NumHandles = 0;
  78. Handles = NULL;
  79. }
  80. for (Index = 0; Index < NumHandles; Index++) {
  81. //
  82. // Open Unicode Collation Protocol
  83. //
  84. Status = gBS->OpenProtocol (
  85. Handles[Index],
  86. &gEfiUnicodeCollation2ProtocolGuid,
  87. (VOID **)&Uc,
  88. gImageHandle,
  89. NULL,
  90. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  91. );
  92. if (EFI_ERROR (Status)) {
  93. continue;
  94. }
  95. //
  96. // Without clue provided use the first Unicode Collation2 protocol.
  97. // This may happen when PlatformLang is NULL or when no installed Unicode
  98. // Collation2 protocol instance supports PlatformLang.
  99. //
  100. if (gUnicodeCollation == NULL) {
  101. gUnicodeCollation = Uc;
  102. }
  103. if (PlatformLang == NULL) {
  104. break;
  105. }
  106. //
  107. // Find the best matching matching language from the supported languages
  108. // of Unicode Collation2 protocol.
  109. //
  110. BestLanguage = GetBestLanguage (
  111. Uc->SupportedLanguages,
  112. FALSE,
  113. PlatformLang,
  114. NULL
  115. );
  116. if (BestLanguage != NULL) {
  117. FreePool (BestLanguage);
  118. gUnicodeCollation = Uc;
  119. break;
  120. }
  121. }
  122. if (Handles != NULL) {
  123. FreePool (Handles);
  124. }
  125. if (PlatformLang != NULL) {
  126. FreePool (PlatformLang);
  127. }
  128. }
  129. return (gUnicodeCollation == NULL) ? EFI_UNSUPPORTED : EFI_SUCCESS;
  130. }
  131. /**
  132. Constructor for the Shell Command library.
  133. Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
  134. @param ImageHandle the image handle of the process
  135. @param SystemTable the EFI System Table pointer
  136. @retval EFI_SUCCESS the initialization was complete sucessfully
  137. **/
  138. RETURN_STATUS
  139. EFIAPI
  140. ShellCommandLibConstructor (
  141. IN EFI_HANDLE ImageHandle,
  142. IN EFI_SYSTEM_TABLE *SystemTable
  143. )
  144. {
  145. EFI_STATUS Status;
  146. InitializeListHead (&gShellMapList.Link);
  147. InitializeListHead (&mCommandList.Link);
  148. InitializeListHead (&mAliasList.Link);
  149. InitializeListHead (&mScriptList.Link);
  150. InitializeListHead (&mFileHandleList.Link);
  151. mEchoState = TRUE;
  152. mExitRequested = FALSE;
  153. mExitScript = FALSE;
  154. mProfileListSize = 0;
  155. mProfileList = NULL;
  156. Status = CommandInit ();
  157. if (EFI_ERROR (Status)) {
  158. return EFI_DEVICE_ERROR;
  159. }
  160. return (RETURN_SUCCESS);
  161. }
  162. /**
  163. Frees list of file handles.
  164. @param[in] List The list to free.
  165. **/
  166. VOID
  167. FreeFileHandleList (
  168. IN BUFFER_LIST *List
  169. )
  170. {
  171. BUFFER_LIST *BufferListEntry;
  172. if (List == NULL) {
  173. return;
  174. }
  175. //
  176. // enumerate through the buffer list and free all memory
  177. //
  178. for ( BufferListEntry = (BUFFER_LIST *)GetFirstNode (&List->Link)
  179. ; !IsListEmpty (&List->Link)
  180. ; BufferListEntry = (BUFFER_LIST *)GetFirstNode (&List->Link)
  181. )
  182. {
  183. RemoveEntryList (&BufferListEntry->Link);
  184. ASSERT (BufferListEntry->Buffer != NULL);
  185. SHELL_FREE_NON_NULL (((SHELL_COMMAND_FILE_HANDLE *)(BufferListEntry->Buffer))->Path);
  186. SHELL_FREE_NON_NULL (BufferListEntry->Buffer);
  187. SHELL_FREE_NON_NULL (BufferListEntry);
  188. }
  189. }
  190. /**
  191. Destructor for the library. free any resources.
  192. @param ImageHandle the image handle of the process
  193. @param SystemTable the EFI System Table pointer
  194. @retval RETURN_SUCCESS this function always returns success
  195. **/
  196. RETURN_STATUS
  197. EFIAPI
  198. ShellCommandLibDestructor (
  199. IN EFI_HANDLE ImageHandle,
  200. IN EFI_SYSTEM_TABLE *SystemTable
  201. )
  202. {
  203. SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
  204. ALIAS_LIST *Node2;
  205. SCRIPT_FILE_LIST *Node3;
  206. SHELL_MAP_LIST *MapNode;
  207. //
  208. // enumerate throught the list and free all the memory
  209. //
  210. while (!IsListEmpty (&mCommandList.Link)) {
  211. Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link);
  212. RemoveEntryList (&Node->Link);
  213. SHELL_FREE_NON_NULL (Node->CommandString);
  214. FreePool (Node);
  215. DEBUG_CODE (
  216. Node = NULL;
  217. );
  218. }
  219. //
  220. // enumerate through the alias list and free all memory
  221. //
  222. while (!IsListEmpty (&mAliasList.Link)) {
  223. Node2 = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link);
  224. RemoveEntryList (&Node2->Link);
  225. SHELL_FREE_NON_NULL (Node2->CommandString);
  226. SHELL_FREE_NON_NULL (Node2->Alias);
  227. SHELL_FREE_NON_NULL (Node2);
  228. DEBUG_CODE (
  229. Node2 = NULL;
  230. );
  231. }
  232. //
  233. // enumerate throught the list and free all the memory
  234. //
  235. while (!IsListEmpty (&mScriptList.Link)) {
  236. Node3 = (SCRIPT_FILE_LIST *)GetFirstNode (&mScriptList.Link);
  237. RemoveEntryList (&Node3->Link);
  238. DeleteScriptFileStruct (Node3->Data);
  239. FreePool (Node3);
  240. }
  241. //
  242. // enumerate throught the mappings list and free all the memory
  243. //
  244. if (!IsListEmpty (&gShellMapList.Link)) {
  245. for (MapNode = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
  246. ; !IsListEmpty (&gShellMapList.Link)
  247. ; MapNode = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
  248. )
  249. {
  250. ASSERT (MapNode != NULL);
  251. RemoveEntryList (&MapNode->Link);
  252. SHELL_FREE_NON_NULL (MapNode->DevicePath);
  253. SHELL_FREE_NON_NULL (MapNode->MapName);
  254. SHELL_FREE_NON_NULL (MapNode->CurrentDirectoryPath);
  255. FreePool (MapNode);
  256. }
  257. }
  258. if (!IsListEmpty (&mFileHandleList.Link)) {
  259. FreeFileHandleList (&mFileHandleList);
  260. }
  261. if (mProfileList != NULL) {
  262. FreePool (mProfileList);
  263. }
  264. gUnicodeCollation = NULL;
  265. gShellCurMapping = NULL;
  266. return (RETURN_SUCCESS);
  267. }
  268. /**
  269. Find a dynamic command protocol instance given a command name string.
  270. @param CommandString the command name string
  271. @return instance the command protocol instance, if dynamic command instance found
  272. @retval NULL no dynamic command protocol instance found for name
  273. **/
  274. CONST EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *
  275. ShellCommandFindDynamicCommand (
  276. IN CONST CHAR16 *CommandString
  277. )
  278. {
  279. EFI_STATUS Status;
  280. EFI_HANDLE *CommandHandleList;
  281. EFI_HANDLE *NextCommand;
  282. EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand;
  283. CommandHandleList = GetHandleListByProtocol (&gEfiShellDynamicCommandProtocolGuid);
  284. if (CommandHandleList == NULL) {
  285. //
  286. // not found or out of resources
  287. //
  288. return NULL;
  289. }
  290. for (NextCommand = CommandHandleList; *NextCommand != NULL; NextCommand++) {
  291. Status = gBS->HandleProtocol (
  292. *NextCommand,
  293. &gEfiShellDynamicCommandProtocolGuid,
  294. (VOID **)&DynamicCommand
  295. );
  296. if (EFI_ERROR (Status)) {
  297. continue;
  298. }
  299. if (gUnicodeCollation->StriColl (
  300. gUnicodeCollation,
  301. (CHAR16 *)CommandString,
  302. (CHAR16 *)DynamicCommand->CommandName
  303. ) == 0
  304. )
  305. {
  306. FreePool (CommandHandleList);
  307. return (DynamicCommand);
  308. }
  309. }
  310. FreePool (CommandHandleList);
  311. return (NULL);
  312. }
  313. /**
  314. Checks if a command exists as a dynamic command protocol instance
  315. @param[in] CommandString The command string to check for on the list.
  316. **/
  317. BOOLEAN
  318. ShellCommandDynamicCommandExists (
  319. IN CONST CHAR16 *CommandString
  320. )
  321. {
  322. return (BOOLEAN)((ShellCommandFindDynamicCommand (CommandString) != NULL));
  323. }
  324. /**
  325. Checks if a command is already on the internal command list.
  326. @param[in] CommandString The command string to check for on the list.
  327. **/
  328. BOOLEAN
  329. ShellCommandIsCommandOnInternalList (
  330. IN CONST CHAR16 *CommandString
  331. )
  332. {
  333. SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
  334. //
  335. // assert for NULL parameter
  336. //
  337. ASSERT (CommandString != NULL);
  338. //
  339. // check for the command
  340. //
  341. for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link)
  342. ; !IsNull (&mCommandList.Link, &Node->Link)
  343. ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Node->Link)
  344. )
  345. {
  346. ASSERT (Node->CommandString != NULL);
  347. if (gUnicodeCollation->StriColl (
  348. gUnicodeCollation,
  349. (CHAR16 *)CommandString,
  350. Node->CommandString
  351. ) == 0
  352. )
  353. {
  354. return (TRUE);
  355. }
  356. }
  357. return (FALSE);
  358. }
  359. /**
  360. Checks if a command exists, either internally or through the dynamic command protocol.
  361. @param[in] CommandString The command string to check for on the list.
  362. **/
  363. BOOLEAN
  364. EFIAPI
  365. ShellCommandIsCommandOnList (
  366. IN CONST CHAR16 *CommandString
  367. )
  368. {
  369. if (ShellCommandIsCommandOnInternalList (CommandString)) {
  370. return TRUE;
  371. }
  372. return ShellCommandDynamicCommandExists (CommandString);
  373. }
  374. /**
  375. Get the help text for a dynamic command.
  376. @param[in] CommandString The command name.
  377. @retval NULL No help text was found.
  378. @return String of help text. Caller required to free.
  379. **/
  380. CHAR16 *
  381. ShellCommandGetDynamicCommandHelp (
  382. IN CONST CHAR16 *CommandString
  383. )
  384. {
  385. EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand;
  386. DynamicCommand = (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *)ShellCommandFindDynamicCommand (CommandString);
  387. if (DynamicCommand == NULL) {
  388. return (NULL);
  389. }
  390. //
  391. // TODO: how to get proper language?
  392. //
  393. return DynamicCommand->GetHelp (DynamicCommand, "en");
  394. }
  395. /**
  396. Get the help text for an internal command.
  397. @param[in] CommandString The command name.
  398. @retval NULL No help text was found.
  399. @return String of help text. Caller reuiqred to free.
  400. **/
  401. CHAR16 *
  402. ShellCommandGetInternalCommandHelp (
  403. IN CONST CHAR16 *CommandString
  404. )
  405. {
  406. SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
  407. //
  408. // assert for NULL parameter
  409. //
  410. ASSERT (CommandString != NULL);
  411. //
  412. // check for the command
  413. //
  414. for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link)
  415. ; !IsNull (&mCommandList.Link, &Node->Link)
  416. ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Node->Link)
  417. )
  418. {
  419. ASSERT (Node->CommandString != NULL);
  420. if (gUnicodeCollation->StriColl (
  421. gUnicodeCollation,
  422. (CHAR16 *)CommandString,
  423. Node->CommandString
  424. ) == 0
  425. )
  426. {
  427. return (HiiGetString (Node->HiiHandle, Node->ManFormatHelp, NULL));
  428. }
  429. }
  430. return (NULL);
  431. }
  432. /**
  433. Get the help text for a command.
  434. @param[in] CommandString The command name.
  435. @retval NULL No help text was found.
  436. @return String of help text.Caller reuiqred to free.
  437. **/
  438. CHAR16 *
  439. EFIAPI
  440. ShellCommandGetCommandHelp (
  441. IN CONST CHAR16 *CommandString
  442. )
  443. {
  444. CHAR16 *HelpStr;
  445. HelpStr = ShellCommandGetInternalCommandHelp (CommandString);
  446. if (HelpStr == NULL) {
  447. HelpStr = ShellCommandGetDynamicCommandHelp (CommandString);
  448. }
  449. return HelpStr;
  450. }
  451. /**
  452. Registers handlers of type SHELL_RUN_COMMAND and
  453. SHELL_GET_MAN_FILENAME for each shell command.
  454. If the ShellSupportLevel is greater than the value of the
  455. PcdShellSupportLevel then return RETURN_UNSUPPORTED.
  456. Registers the handlers specified by GetHelpInfoHandler and CommandHandler
  457. with the command specified by CommandString. If the command named by
  458. CommandString has already been registered, then return
  459. RETURN_ALREADY_STARTED.
  460. If there are not enough resources available to register the handlers then
  461. RETURN_OUT_OF_RESOURCES is returned.
  462. If CommandString is NULL, then ASSERT().
  463. If GetHelpInfoHandler is NULL, then ASSERT().
  464. If CommandHandler is NULL, then ASSERT().
  465. If ProfileName is NULL, then ASSERT().
  466. @param[in] CommandString Pointer to the command name. This is the
  467. name to look for on the command line in
  468. the shell.
  469. @param[in] CommandHandler Pointer to a function that runs the
  470. specified command.
  471. @param[in] GetManFileName Pointer to a function that provides man
  472. filename.
  473. @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
  474. function.
  475. @param[in] ProfileName profile name to require for support of this
  476. function.
  477. @param[in] CanAffectLE indicates whether this command's return value
  478. can change the LASTERROR environment variable.
  479. @param[in] HiiHandle Handle of this command's HII entry.
  480. @param[in] ManFormatHelp HII locator for the help text.
  481. @retval RETURN_SUCCESS The handlers were registered.
  482. @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
  483. register the shell command.
  484. @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
  485. currently allowed support level.
  486. @retval RETURN_ALREADY_STARTED The CommandString represents a command that
  487. is already registered. Only 1 handler set for
  488. a given command is allowed.
  489. @sa SHELL_GET_MAN_FILENAME
  490. @sa SHELL_RUN_COMMAND
  491. **/
  492. RETURN_STATUS
  493. EFIAPI
  494. ShellCommandRegisterCommandName (
  495. IN CONST CHAR16 *CommandString,
  496. IN SHELL_RUN_COMMAND CommandHandler,
  497. IN SHELL_GET_MAN_FILENAME GetManFileName,
  498. IN UINT32 ShellMinSupportLevel,
  499. IN CONST CHAR16 *ProfileName,
  500. IN CONST BOOLEAN CanAffectLE,
  501. IN CONST EFI_HII_HANDLE HiiHandle,
  502. IN CONST EFI_STRING_ID ManFormatHelp
  503. )
  504. {
  505. SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
  506. SHELL_COMMAND_INTERNAL_LIST_ENTRY *Command;
  507. SHELL_COMMAND_INTERNAL_LIST_ENTRY *PrevCommand;
  508. INTN LexicalMatchValue;
  509. //
  510. // Initialize local variables.
  511. //
  512. Command = NULL;
  513. PrevCommand = NULL;
  514. LexicalMatchValue = 0;
  515. //
  516. // ASSERTs for NULL parameters
  517. //
  518. ASSERT (CommandString != NULL);
  519. ASSERT (GetManFileName != NULL);
  520. ASSERT (CommandHandler != NULL);
  521. ASSERT (ProfileName != NULL);
  522. //
  523. // check for shell support level
  524. //
  525. if (PcdGet8 (PcdShellSupportLevel) < ShellMinSupportLevel) {
  526. return (RETURN_UNSUPPORTED);
  527. }
  528. //
  529. // check for already on the list
  530. //
  531. if (ShellCommandIsCommandOnList (CommandString)) {
  532. return (RETURN_ALREADY_STARTED);
  533. }
  534. //
  535. // allocate memory for new struct
  536. //
  537. Node = AllocateZeroPool (sizeof (SHELL_COMMAND_INTERNAL_LIST_ENTRY));
  538. if (Node == NULL) {
  539. return RETURN_OUT_OF_RESOURCES;
  540. }
  541. Node->CommandString = AllocateCopyPool (StrSize (CommandString), CommandString);
  542. if (Node->CommandString == NULL) {
  543. FreePool (Node);
  544. return RETURN_OUT_OF_RESOURCES;
  545. }
  546. Node->GetManFileName = GetManFileName;
  547. Node->CommandHandler = CommandHandler;
  548. Node->LastError = CanAffectLE;
  549. Node->HiiHandle = HiiHandle;
  550. Node->ManFormatHelp = ManFormatHelp;
  551. if ( (StrLen (ProfileName) > 0)
  552. && (( (mProfileList != NULL)
  553. && (StrStr (mProfileList, ProfileName) == NULL)) || (mProfileList == NULL))
  554. )
  555. {
  556. ASSERT ((mProfileList == NULL && mProfileListSize == 0) || (mProfileList != NULL));
  557. if (mProfileList == NULL) {
  558. //
  559. // If this is the first make a leading ';'
  560. //
  561. StrnCatGrow (&mProfileList, &mProfileListSize, L";", 0);
  562. }
  563. StrnCatGrow (&mProfileList, &mProfileListSize, ProfileName, 0);
  564. StrnCatGrow (&mProfileList, &mProfileListSize, L";", 0);
  565. }
  566. //
  567. // Insert a new entry on top of the list
  568. //
  569. InsertHeadList (&mCommandList.Link, &Node->Link);
  570. //
  571. // Move a new registered command to its sorted ordered location in the list
  572. //
  573. for (Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link),
  574. PrevCommand = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link)
  575. ; !IsNull (&mCommandList.Link, &Command->Link)
  576. ; Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Command->Link))
  577. {
  578. //
  579. // Get Lexical Comparison Value between PrevCommand and Command list entry
  580. //
  581. LexicalMatchValue = gUnicodeCollation->StriColl (
  582. gUnicodeCollation,
  583. PrevCommand->CommandString,
  584. Command->CommandString
  585. );
  586. //
  587. // Swap PrevCommand and Command list entry if PrevCommand list entry
  588. // is alphabetically greater than Command list entry
  589. //
  590. if (LexicalMatchValue > 0) {
  591. Command = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)SwapListEntries (&PrevCommand->Link, &Command->Link);
  592. } else if (LexicalMatchValue < 0) {
  593. //
  594. // PrevCommand entry is lexically lower than Command entry
  595. //
  596. break;
  597. }
  598. }
  599. return (RETURN_SUCCESS);
  600. }
  601. /**
  602. Function to get the current Profile string.
  603. @retval NULL There are no installed profiles.
  604. @return A semi-colon delimited list of profiles.
  605. **/
  606. CONST CHAR16 *
  607. EFIAPI
  608. ShellCommandGetProfileList (
  609. VOID
  610. )
  611. {
  612. return (mProfileList);
  613. }
  614. /**
  615. Checks if a command string has been registered for CommandString and if so it runs
  616. the previously registered handler for that command with the command line.
  617. If CommandString is NULL, then ASSERT().
  618. If Sections is specified, then each section name listed will be compared in a casesensitive
  619. manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
  620. it will be appended to the returned help text. If the section does not exist, no
  621. information will be returned. If Sections is NULL, then all help text information
  622. available will be returned.
  623. @param[in] CommandString Pointer to the command name. This is the name
  624. found on the command line in the shell.
  625. @param[in, out] RetVal Pointer to the return vaule from the command handler.
  626. @param[in, out] CanAffectLE indicates whether this command's return value
  627. needs to be placed into LASTERROR environment variable.
  628. @retval RETURN_SUCCESS The handler was run.
  629. @retval RETURN_NOT_FOUND The CommandString did not match a registered
  630. command name.
  631. @sa SHELL_RUN_COMMAND
  632. **/
  633. RETURN_STATUS
  634. EFIAPI
  635. ShellCommandRunCommandHandler (
  636. IN CONST CHAR16 *CommandString,
  637. IN OUT SHELL_STATUS *RetVal,
  638. IN OUT BOOLEAN *CanAffectLE OPTIONAL
  639. )
  640. {
  641. SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
  642. EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *DynamicCommand;
  643. //
  644. // assert for NULL parameters
  645. //
  646. ASSERT (CommandString != NULL);
  647. //
  648. // check for the command
  649. //
  650. for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link)
  651. ; !IsNull (&mCommandList.Link, &Node->Link)
  652. ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Node->Link)
  653. )
  654. {
  655. ASSERT (Node->CommandString != NULL);
  656. if (gUnicodeCollation->StriColl (
  657. gUnicodeCollation,
  658. (CHAR16 *)CommandString,
  659. Node->CommandString
  660. ) == 0
  661. )
  662. {
  663. if (CanAffectLE != NULL) {
  664. *CanAffectLE = Node->LastError;
  665. }
  666. if (RetVal != NULL) {
  667. *RetVal = Node->CommandHandler (NULL, gST);
  668. } else {
  669. Node->CommandHandler (NULL, gST);
  670. }
  671. return (RETURN_SUCCESS);
  672. }
  673. }
  674. //
  675. // An internal command was not found, try to find a dynamic command
  676. //
  677. DynamicCommand = (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL *)ShellCommandFindDynamicCommand (CommandString);
  678. if (DynamicCommand != NULL) {
  679. if (RetVal != NULL) {
  680. *RetVal = DynamicCommand->Handler (DynamicCommand, gST, gEfiShellParametersProtocol, gEfiShellProtocol);
  681. } else {
  682. DynamicCommand->Handler (DynamicCommand, gST, gEfiShellParametersProtocol, gEfiShellProtocol);
  683. }
  684. return (RETURN_SUCCESS);
  685. }
  686. return (RETURN_NOT_FOUND);
  687. }
  688. /**
  689. Checks if a command string has been registered for CommandString and if so it
  690. returns the MAN filename specified for that command.
  691. If CommandString is NULL, then ASSERT().
  692. @param[in] CommandString Pointer to the command name. This is the name
  693. found on the command line in the shell.\
  694. @retval NULL the commandString was not a registered command.
  695. @return other the name of the MAN file.
  696. @sa SHELL_GET_MAN_FILENAME
  697. **/
  698. CONST CHAR16 *
  699. EFIAPI
  700. ShellCommandGetManFileNameHandler (
  701. IN CONST CHAR16 *CommandString
  702. )
  703. {
  704. SHELL_COMMAND_INTERNAL_LIST_ENTRY *Node;
  705. //
  706. // assert for NULL parameters
  707. //
  708. ASSERT (CommandString != NULL);
  709. //
  710. // check for the command
  711. //
  712. for ( Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetFirstNode (&mCommandList.Link)
  713. ; !IsNull (&mCommandList.Link, &Node->Link)
  714. ; Node = (SHELL_COMMAND_INTERNAL_LIST_ENTRY *)GetNextNode (&mCommandList.Link, &Node->Link)
  715. )
  716. {
  717. ASSERT (Node->CommandString != NULL);
  718. if (gUnicodeCollation->StriColl (
  719. gUnicodeCollation,
  720. (CHAR16 *)CommandString,
  721. Node->CommandString
  722. ) == 0
  723. )
  724. {
  725. return (Node->GetManFileName ());
  726. }
  727. }
  728. return (NULL);
  729. }
  730. /**
  731. Get the list of all available shell internal commands. This is a linked list
  732. (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
  733. list functions. do not modify the values.
  734. @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
  735. @return a Linked list of all available shell commands.
  736. **/
  737. CONST COMMAND_LIST *
  738. EFIAPI
  739. ShellCommandGetCommandList (
  740. IN CONST BOOLEAN Sort
  741. )
  742. {
  743. // if (!Sort) {
  744. // return ((COMMAND_LIST*)(&mCommandList));
  745. // }
  746. return ((COMMAND_LIST *)(&mCommandList));
  747. }
  748. /**
  749. Registers aliases to be set as part of the initialization of the shell application.
  750. If Command is NULL, then ASSERT().
  751. If Alias is NULL, then ASSERT().
  752. @param[in] Command Pointer to the Command
  753. @param[in] Alias Pointer to Alias
  754. @retval RETURN_SUCCESS The handlers were registered.
  755. @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
  756. register the shell command.
  757. **/
  758. RETURN_STATUS
  759. EFIAPI
  760. ShellCommandRegisterAlias (
  761. IN CONST CHAR16 *Command,
  762. IN CONST CHAR16 *Alias
  763. )
  764. {
  765. ALIAS_LIST *Node;
  766. ALIAS_LIST *CommandAlias;
  767. ALIAS_LIST *PrevCommandAlias;
  768. INTN LexicalMatchValue;
  769. //
  770. // Asserts for NULL
  771. //
  772. ASSERT (Command != NULL);
  773. ASSERT (Alias != NULL);
  774. //
  775. // allocate memory for new struct
  776. //
  777. Node = AllocateZeroPool (sizeof (ALIAS_LIST));
  778. if (Node == NULL) {
  779. return RETURN_OUT_OF_RESOURCES;
  780. }
  781. Node->CommandString = AllocateCopyPool (StrSize (Command), Command);
  782. if (Node->CommandString == NULL) {
  783. FreePool (Node);
  784. return RETURN_OUT_OF_RESOURCES;
  785. }
  786. Node->Alias = AllocateCopyPool (StrSize (Alias), Alias);
  787. if (Node->Alias == NULL) {
  788. FreePool (Node->CommandString);
  789. FreePool (Node);
  790. return RETURN_OUT_OF_RESOURCES;
  791. }
  792. InsertHeadList (&mAliasList.Link, &Node->Link);
  793. //
  794. // Move a new pre-defined registered alias to its sorted ordered location in the list
  795. //
  796. for ( CommandAlias = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link),
  797. PrevCommandAlias = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link)
  798. ; !IsNull (&mAliasList.Link, &CommandAlias->Link)
  799. ; CommandAlias = (ALIAS_LIST *)GetNextNode (&mAliasList.Link, &CommandAlias->Link))
  800. {
  801. //
  802. // Get Lexical comparison value between PrevCommandAlias and CommandAlias List Entry
  803. //
  804. LexicalMatchValue = gUnicodeCollation->StriColl (
  805. gUnicodeCollation,
  806. PrevCommandAlias->Alias,
  807. CommandAlias->Alias
  808. );
  809. //
  810. // Swap PrevCommandAlias and CommandAlias list entry if PrevCommandAlias list entry
  811. // is alphabetically greater than CommandAlias list entry
  812. //
  813. if (LexicalMatchValue > 0) {
  814. CommandAlias = (ALIAS_LIST *)SwapListEntries (&PrevCommandAlias->Link, &CommandAlias->Link);
  815. } else if (LexicalMatchValue < 0) {
  816. //
  817. // PrevCommandAlias entry is lexically lower than CommandAlias entry
  818. //
  819. break;
  820. }
  821. }
  822. return (RETURN_SUCCESS);
  823. }
  824. /**
  825. Get the list of all shell alias commands. This is a linked list
  826. (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
  827. list functions. do not modify the values.
  828. @return a Linked list of all requested shell alias'.
  829. **/
  830. CONST ALIAS_LIST *
  831. EFIAPI
  832. ShellCommandGetInitAliasList (
  833. VOID
  834. )
  835. {
  836. return (&mAliasList);
  837. }
  838. /**
  839. Determine if a given alias is on the list of built in alias'.
  840. @param[in] Alias The alias to test for
  841. @retval TRUE The alias is a built in alias
  842. @retval FALSE The alias is not a built in alias
  843. **/
  844. BOOLEAN
  845. EFIAPI
  846. ShellCommandIsOnAliasList (
  847. IN CONST CHAR16 *Alias
  848. )
  849. {
  850. ALIAS_LIST *Node;
  851. //
  852. // assert for NULL parameter
  853. //
  854. ASSERT (Alias != NULL);
  855. //
  856. // check for the Alias
  857. //
  858. for ( Node = (ALIAS_LIST *)GetFirstNode (&mAliasList.Link)
  859. ; !IsNull (&mAliasList.Link, &Node->Link)
  860. ; Node = (ALIAS_LIST *)GetNextNode (&mAliasList.Link, &Node->Link)
  861. )
  862. {
  863. ASSERT (Node->CommandString != NULL);
  864. ASSERT (Node->Alias != NULL);
  865. if (gUnicodeCollation->StriColl (
  866. gUnicodeCollation,
  867. (CHAR16 *)Alias,
  868. Node->CommandString
  869. ) == 0
  870. )
  871. {
  872. return (TRUE);
  873. }
  874. if (gUnicodeCollation->StriColl (
  875. gUnicodeCollation,
  876. (CHAR16 *)Alias,
  877. Node->Alias
  878. ) == 0
  879. )
  880. {
  881. return (TRUE);
  882. }
  883. }
  884. return (FALSE);
  885. }
  886. /**
  887. Function to determine current state of ECHO. Echo determines if lines from scripts
  888. and ECHO commands are enabled.
  889. @retval TRUE Echo is currently enabled
  890. @retval FALSE Echo is currently disabled
  891. **/
  892. BOOLEAN
  893. EFIAPI
  894. ShellCommandGetEchoState (
  895. VOID
  896. )
  897. {
  898. return (mEchoState);
  899. }
  900. /**
  901. Function to set current state of ECHO. Echo determines if lines from scripts
  902. and ECHO commands are enabled.
  903. If State is TRUE, Echo will be enabled.
  904. If State is FALSE, Echo will be disabled.
  905. @param[in] State How to set echo.
  906. **/
  907. VOID
  908. EFIAPI
  909. ShellCommandSetEchoState (
  910. IN BOOLEAN State
  911. )
  912. {
  913. mEchoState = State;
  914. }
  915. /**
  916. Indicate that the current shell or script should exit.
  917. @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
  918. @param[in] ErrorCode The 64 bit error code to return.
  919. **/
  920. VOID
  921. EFIAPI
  922. ShellCommandRegisterExit (
  923. IN BOOLEAN ScriptOnly,
  924. IN CONST UINT64 ErrorCode
  925. )
  926. {
  927. mExitRequested = (BOOLEAN)(!mExitRequested);
  928. if (mExitRequested) {
  929. mExitScript = ScriptOnly;
  930. } else {
  931. mExitScript = FALSE;
  932. }
  933. mExitCode = ErrorCode;
  934. }
  935. /**
  936. Retrieve the Exit indicator.
  937. @retval TRUE Exit was indicated.
  938. @retval FALSE Exis was not indicated.
  939. **/
  940. BOOLEAN
  941. EFIAPI
  942. ShellCommandGetExit (
  943. VOID
  944. )
  945. {
  946. return (mExitRequested);
  947. }
  948. /**
  949. Retrieve the Exit code.
  950. If ShellCommandGetExit returns FALSE than the return from this is undefined.
  951. @return the value passed into RegisterExit.
  952. **/
  953. UINT64
  954. EFIAPI
  955. ShellCommandGetExitCode (
  956. VOID
  957. )
  958. {
  959. return (mExitCode);
  960. }
  961. /**
  962. Retrieve the Exit script indicator.
  963. If ShellCommandGetExit returns FALSE than the return from this is undefined.
  964. @retval TRUE ScriptOnly was indicated.
  965. @retval FALSE ScriptOnly was not indicated.
  966. **/
  967. BOOLEAN
  968. EFIAPI
  969. ShellCommandGetScriptExit (
  970. VOID
  971. )
  972. {
  973. return (mExitScript);
  974. }
  975. /**
  976. Function to cleanup all memory from a SCRIPT_FILE structure.
  977. @param[in] Script The pointer to the structure to cleanup.
  978. **/
  979. VOID
  980. EFIAPI
  981. DeleteScriptFileStruct (
  982. IN SCRIPT_FILE *Script
  983. )
  984. {
  985. UINT8 LoopVar;
  986. if (Script == NULL) {
  987. return;
  988. }
  989. for (LoopVar = 0; LoopVar < Script->Argc; LoopVar++) {
  990. SHELL_FREE_NON_NULL (Script->Argv[LoopVar]);
  991. }
  992. if (Script->Argv != NULL) {
  993. SHELL_FREE_NON_NULL (Script->Argv);
  994. }
  995. Script->CurrentCommand = NULL;
  996. while (!IsListEmpty (&Script->CommandList)) {
  997. Script->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode (&Script->CommandList);
  998. if (Script->CurrentCommand != NULL) {
  999. RemoveEntryList (&Script->CurrentCommand->Link);
  1000. if (Script->CurrentCommand->Cl != NULL) {
  1001. SHELL_FREE_NON_NULL (Script->CurrentCommand->Cl);
  1002. }
  1003. if (Script->CurrentCommand->Data != NULL) {
  1004. SHELL_FREE_NON_NULL (Script->CurrentCommand->Data);
  1005. }
  1006. SHELL_FREE_NON_NULL (Script->CurrentCommand);
  1007. }
  1008. }
  1009. SHELL_FREE_NON_NULL (Script->ScriptName);
  1010. SHELL_FREE_NON_NULL (Script);
  1011. }
  1012. /**
  1013. Function to return a pointer to the currently running script file object.
  1014. @retval NULL A script file is not currently running.
  1015. @return A pointer to the current script file object.
  1016. **/
  1017. SCRIPT_FILE *
  1018. EFIAPI
  1019. ShellCommandGetCurrentScriptFile (
  1020. VOID
  1021. )
  1022. {
  1023. SCRIPT_FILE_LIST *List;
  1024. if (IsListEmpty (&mScriptList.Link)) {
  1025. return (NULL);
  1026. }
  1027. List = ((SCRIPT_FILE_LIST *)GetFirstNode (&mScriptList.Link));
  1028. return (List->Data);
  1029. }
  1030. /**
  1031. Function to set a new script as the currently running one.
  1032. This function will correctly stack and unstack nested scripts.
  1033. @param[in] Script Pointer to new script information structure. if NULL
  1034. will remove and de-allocate the top-most Script structure.
  1035. @return A pointer to the current running script file after this
  1036. change. NULL if removing the final script.
  1037. **/
  1038. SCRIPT_FILE *
  1039. EFIAPI
  1040. ShellCommandSetNewScript (
  1041. IN SCRIPT_FILE *Script OPTIONAL
  1042. )
  1043. {
  1044. SCRIPT_FILE_LIST *Node;
  1045. if (Script == NULL) {
  1046. if (IsListEmpty (&mScriptList.Link)) {
  1047. return (NULL);
  1048. }
  1049. Node = (SCRIPT_FILE_LIST *)GetFirstNode (&mScriptList.Link);
  1050. RemoveEntryList (&Node->Link);
  1051. DeleteScriptFileStruct (Node->Data);
  1052. FreePool (Node);
  1053. } else {
  1054. Node = AllocateZeroPool (sizeof (SCRIPT_FILE_LIST));
  1055. if (Node == NULL) {
  1056. return (NULL);
  1057. }
  1058. Node->Data = Script;
  1059. InsertHeadList (&mScriptList.Link, &Node->Link);
  1060. }
  1061. return (ShellCommandGetCurrentScriptFile ());
  1062. }
  1063. /**
  1064. Function to generate the next default mapping name.
  1065. If the return value is not NULL then it must be callee freed.
  1066. @param Type What kind of mapping name to make.
  1067. @retval NULL a memory allocation failed.
  1068. @return a new map name string
  1069. **/
  1070. CHAR16 *
  1071. EFIAPI
  1072. ShellCommandCreateNewMappingName (
  1073. IN CONST SHELL_MAPPING_TYPE Type
  1074. )
  1075. {
  1076. CHAR16 *String;
  1077. ASSERT (Type < MappingTypeMax);
  1078. String = NULL;
  1079. String = AllocateZeroPool (PcdGet8 (PcdShellMapNameLength) * sizeof (String[0]));
  1080. UnicodeSPrint (
  1081. String,
  1082. PcdGet8 (PcdShellMapNameLength) * sizeof (String[0]),
  1083. Type == MappingTypeFileSystem ? L"FS%d:" : L"BLK%d:",
  1084. Type == MappingTypeFileSystem ? mFsMaxCount++ : mBlkMaxCount++
  1085. );
  1086. return (String);
  1087. }
  1088. /**
  1089. Function to add a map node to the list of map items and update the "path" environment variable (optionally).
  1090. If Path is TRUE (during initialization only), the path environment variable will also be updated to include
  1091. default paths on the new map name...
  1092. Path should be FALSE when this function is called from the protocol SetMap function.
  1093. @param[in] Name The human readable mapped name.
  1094. @param[in] DevicePath The Device Path for this map.
  1095. @param[in] Flags The Flags attribute for this map item.
  1096. @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
  1097. @retval EFI_SUCCESS The addition was sucessful.
  1098. @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
  1099. @retval EFI_INVALID_PARAMETER A parameter was invalid.
  1100. **/
  1101. EFI_STATUS
  1102. EFIAPI
  1103. ShellCommandAddMapItemAndUpdatePath (
  1104. IN CONST CHAR16 *Name,
  1105. IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  1106. IN CONST UINT64 Flags,
  1107. IN CONST BOOLEAN Path
  1108. )
  1109. {
  1110. EFI_STATUS Status;
  1111. SHELL_MAP_LIST *MapListNode;
  1112. CONST CHAR16 *OriginalPath;
  1113. CHAR16 *NewPath;
  1114. UINTN NewPathSize;
  1115. NewPathSize = 0;
  1116. NewPath = NULL;
  1117. OriginalPath = NULL;
  1118. Status = EFI_SUCCESS;
  1119. MapListNode = AllocateZeroPool (sizeof (SHELL_MAP_LIST));
  1120. if (MapListNode == NULL) {
  1121. Status = EFI_OUT_OF_RESOURCES;
  1122. } else {
  1123. MapListNode->Flags = Flags;
  1124. MapListNode->MapName = AllocateCopyPool (StrSize (Name), Name);
  1125. MapListNode->DevicePath = DuplicateDevicePath (DevicePath);
  1126. if ((MapListNode->MapName == NULL) || (MapListNode->DevicePath == NULL)) {
  1127. Status = EFI_OUT_OF_RESOURCES;
  1128. } else {
  1129. InsertTailList (&gShellMapList.Link, &MapListNode->Link);
  1130. }
  1131. }
  1132. if (EFI_ERROR (Status)) {
  1133. if (MapListNode != NULL) {
  1134. if (MapListNode->DevicePath != NULL) {
  1135. FreePool (MapListNode->DevicePath);
  1136. }
  1137. if (MapListNode->MapName != NULL) {
  1138. FreePool (MapListNode->MapName);
  1139. }
  1140. FreePool (MapListNode);
  1141. }
  1142. } else if (Path) {
  1143. //
  1144. // Since there was no error and Path was TRUE
  1145. // Now add the correct path for that mapping
  1146. //
  1147. OriginalPath = gEfiShellProtocol->GetEnv (L"path");
  1148. ASSERT ((NewPath == NULL && NewPathSize == 0) || (NewPath != NULL));
  1149. if (OriginalPath != NULL) {
  1150. StrnCatGrow (&NewPath, &NewPathSize, OriginalPath, 0);
  1151. StrnCatGrow (&NewPath, &NewPathSize, L";", 0);
  1152. }
  1153. StrnCatGrow (&NewPath, &NewPathSize, Name, 0);
  1154. StrnCatGrow (&NewPath, &NewPathSize, L"\\efi\\tools\\;", 0);
  1155. StrnCatGrow (&NewPath, &NewPathSize, Name, 0);
  1156. StrnCatGrow (&NewPath, &NewPathSize, L"\\efi\\boot\\;", 0);
  1157. StrnCatGrow (&NewPath, &NewPathSize, Name, 0);
  1158. StrnCatGrow (&NewPath, &NewPathSize, L"\\", 0);
  1159. Status = gEfiShellProtocol->SetEnv (L"path", NewPath, TRUE);
  1160. ASSERT_EFI_ERROR (Status);
  1161. FreePool (NewPath);
  1162. }
  1163. return (Status);
  1164. }
  1165. /**
  1166. Creates the default map names for each device path in the system with
  1167. a protocol depending on the Type.
  1168. Creates the consistent map names for each device path in the system with
  1169. a protocol depending on the Type.
  1170. Note: This will reset all mappings in the system("map -r").
  1171. Also sets up the default path environment variable if Type is FileSystem.
  1172. @retval EFI_SUCCESS All map names were created sucessfully.
  1173. @retval EFI_NOT_FOUND No protocols were found in the system.
  1174. @return Error returned from gBS->LocateHandle().
  1175. @sa LocateHandle
  1176. **/
  1177. EFI_STATUS
  1178. EFIAPI
  1179. ShellCommandCreateInitialMappingsAndPaths (
  1180. VOID
  1181. )
  1182. {
  1183. EFI_STATUS Status;
  1184. EFI_HANDLE *HandleList;
  1185. UINTN Count;
  1186. EFI_DEVICE_PATH_PROTOCOL **DevicePathList;
  1187. CHAR16 *NewDefaultName;
  1188. CHAR16 *NewConsistName;
  1189. EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;
  1190. SHELL_MAP_LIST *MapListNode;
  1191. CONST CHAR16 *CurDir;
  1192. CHAR16 *SplitCurDir;
  1193. CHAR16 *MapName;
  1194. SHELL_MAP_LIST *MapListItem;
  1195. ConsistMappingTable = NULL;
  1196. SplitCurDir = NULL;
  1197. MapName = NULL;
  1198. MapListItem = NULL;
  1199. HandleList = NULL;
  1200. //
  1201. // Reset the static members back to zero
  1202. //
  1203. mFsMaxCount = 0;
  1204. mBlkMaxCount = 0;
  1205. gEfiShellProtocol->SetEnv (L"path", L"", TRUE);
  1206. //
  1207. // First empty out the existing list.
  1208. //
  1209. if (!IsListEmpty (&gShellMapList.Link)) {
  1210. for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
  1211. ; !IsListEmpty (&gShellMapList.Link)
  1212. ; MapListNode = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
  1213. )
  1214. {
  1215. RemoveEntryList (&MapListNode->Link);
  1216. SHELL_FREE_NON_NULL (MapListNode->DevicePath);
  1217. SHELL_FREE_NON_NULL (MapListNode->MapName);
  1218. SHELL_FREE_NON_NULL (MapListNode->CurrentDirectoryPath);
  1219. FreePool (MapListNode);
  1220. } // for loop
  1221. }
  1222. //
  1223. // Find each handle with Simple File System
  1224. //
  1225. HandleList = GetHandleListByProtocol (&gEfiSimpleFileSystemProtocolGuid);
  1226. if (HandleList != NULL) {
  1227. //
  1228. // Do a count of the handles
  1229. //
  1230. for (Count = 0; HandleList[Count] != NULL; Count++) {
  1231. }
  1232. //
  1233. // Get all Device Paths
  1234. //
  1235. DevicePathList = AllocateZeroPool (sizeof (EFI_DEVICE_PATH_PROTOCOL *) * Count);
  1236. if (DevicePathList == NULL) {
  1237. SHELL_FREE_NON_NULL (HandleList);
  1238. return EFI_OUT_OF_RESOURCES;
  1239. }
  1240. for (Count = 0; HandleList[Count] != NULL; Count++) {
  1241. DevicePathList[Count] = DevicePathFromHandle (HandleList[Count]);
  1242. }
  1243. //
  1244. // Sort all DevicePaths
  1245. //
  1246. PerformQuickSort (DevicePathList, Count, sizeof (EFI_DEVICE_PATH_PROTOCOL *), DevicePathCompare);
  1247. if (!EFI_ERROR (ShellCommandConsistMappingInitialize (&ConsistMappingTable))) {
  1248. //
  1249. // Assign new Mappings to all...
  1250. //
  1251. for (Count = 0; HandleList[Count] != NULL; Count++) {
  1252. //
  1253. // Get default name first
  1254. //
  1255. NewDefaultName = ShellCommandCreateNewMappingName (MappingTypeFileSystem);
  1256. ASSERT (NewDefaultName != NULL);
  1257. Status = ShellCommandAddMapItemAndUpdatePath (NewDefaultName, DevicePathList[Count], 0, TRUE);
  1258. ASSERT_EFI_ERROR (Status);
  1259. FreePool (NewDefaultName);
  1260. //
  1261. // Now do consistent name
  1262. //
  1263. NewConsistName = ShellCommandConsistMappingGenMappingName (DevicePathList[Count], ConsistMappingTable);
  1264. if (NewConsistName != NULL) {
  1265. Status = ShellCommandAddMapItemAndUpdatePath (NewConsistName, DevicePathList[Count], 0, FALSE);
  1266. ASSERT_EFI_ERROR (Status);
  1267. FreePool (NewConsistName);
  1268. }
  1269. }
  1270. }
  1271. if (ConsistMappingTable != NULL) {
  1272. ShellCommandConsistMappingUnInitialize (ConsistMappingTable);
  1273. }
  1274. SHELL_FREE_NON_NULL (HandleList);
  1275. SHELL_FREE_NON_NULL (DevicePathList);
  1276. HandleList = NULL;
  1277. //
  1278. // gShellCurMapping point to node of current file system in the gShellMapList. When reset all mappings,
  1279. // all nodes in the gShellMapList will be free. Then gShellCurMapping will be a dangling pointer, So,
  1280. // after created new mappings, we should reset the gShellCurMapping pointer back to node of current file system.
  1281. //
  1282. if (gShellCurMapping != NULL) {
  1283. gShellCurMapping = NULL;
  1284. CurDir = gEfiShellProtocol->GetEnv (L"cwd");
  1285. if (CurDir != NULL) {
  1286. MapName = AllocateCopyPool (StrSize (CurDir), CurDir);
  1287. if (MapName == NULL) {
  1288. return EFI_OUT_OF_RESOURCES;
  1289. }
  1290. SplitCurDir = StrStr (MapName, L":");
  1291. if (SplitCurDir == NULL) {
  1292. SHELL_FREE_NON_NULL (MapName);
  1293. return EFI_UNSUPPORTED;
  1294. }
  1295. *(SplitCurDir + 1) = CHAR_NULL;
  1296. MapListItem = ShellCommandFindMapItem (MapName);
  1297. if (MapListItem != NULL) {
  1298. gShellCurMapping = MapListItem;
  1299. }
  1300. SHELL_FREE_NON_NULL (MapName);
  1301. }
  1302. }
  1303. } else {
  1304. Count = (UINTN)-1;
  1305. }
  1306. //
  1307. // Find each handle with Block Io
  1308. //
  1309. HandleList = GetHandleListByProtocol (&gEfiBlockIoProtocolGuid);
  1310. if (HandleList != NULL) {
  1311. for (Count = 0; HandleList[Count] != NULL; Count++) {
  1312. }
  1313. //
  1314. // Get all Device Paths
  1315. //
  1316. DevicePathList = AllocateZeroPool (sizeof (EFI_DEVICE_PATH_PROTOCOL *) * Count);
  1317. if (DevicePathList == NULL) {
  1318. SHELL_FREE_NON_NULL (HandleList);
  1319. return EFI_OUT_OF_RESOURCES;
  1320. }
  1321. for (Count = 0; HandleList[Count] != NULL; Count++) {
  1322. DevicePathList[Count] = DevicePathFromHandle (HandleList[Count]);
  1323. }
  1324. //
  1325. // Sort all DevicePaths
  1326. //
  1327. PerformQuickSort (DevicePathList, Count, sizeof (EFI_DEVICE_PATH_PROTOCOL *), DevicePathCompare);
  1328. //
  1329. // Assign new Mappings to all...
  1330. //
  1331. for (Count = 0; HandleList[Count] != NULL; Count++) {
  1332. //
  1333. // Get default name first
  1334. //
  1335. NewDefaultName = ShellCommandCreateNewMappingName (MappingTypeBlockIo);
  1336. ASSERT (NewDefaultName != NULL);
  1337. Status = ShellCommandAddMapItemAndUpdatePath (NewDefaultName, DevicePathList[Count], 0, FALSE);
  1338. ASSERT_EFI_ERROR (Status);
  1339. FreePool (NewDefaultName);
  1340. }
  1341. SHELL_FREE_NON_NULL (HandleList);
  1342. SHELL_FREE_NON_NULL (DevicePathList);
  1343. } else if (Count == (UINTN)-1) {
  1344. return (EFI_NOT_FOUND);
  1345. }
  1346. return (EFI_SUCCESS);
  1347. }
  1348. /**
  1349. Add mappings for any devices without one. Do not change any existing maps.
  1350. @retval EFI_SUCCESS The operation was successful.
  1351. **/
  1352. EFI_STATUS
  1353. EFIAPI
  1354. ShellCommandUpdateMapping (
  1355. VOID
  1356. )
  1357. {
  1358. EFI_STATUS Status;
  1359. EFI_HANDLE *HandleList;
  1360. UINTN Count;
  1361. EFI_DEVICE_PATH_PROTOCOL **DevicePathList;
  1362. CHAR16 *NewDefaultName;
  1363. CHAR16 *NewConsistName;
  1364. EFI_DEVICE_PATH_PROTOCOL **ConsistMappingTable;
  1365. HandleList = NULL;
  1366. Status = EFI_SUCCESS;
  1367. //
  1368. // remove mappings that represent removed devices.
  1369. //
  1370. //
  1371. // Find each handle with Simple File System
  1372. //
  1373. HandleList = GetHandleListByProtocol (&gEfiSimpleFileSystemProtocolGuid);
  1374. if (HandleList != NULL) {
  1375. //
  1376. // Do a count of the handles
  1377. //
  1378. for (Count = 0; HandleList[Count] != NULL; Count++) {
  1379. }
  1380. //
  1381. // Get all Device Paths
  1382. //
  1383. DevicePathList = AllocateZeroPool (sizeof (EFI_DEVICE_PATH_PROTOCOL *) * Count);
  1384. if (DevicePathList == NULL) {
  1385. return (EFI_OUT_OF_RESOURCES);
  1386. }
  1387. for (Count = 0; HandleList[Count] != NULL; Count++) {
  1388. DevicePathList[Count] = DevicePathFromHandle (HandleList[Count]);
  1389. }
  1390. //
  1391. // Sort all DevicePaths
  1392. //
  1393. PerformQuickSort (DevicePathList, Count, sizeof (EFI_DEVICE_PATH_PROTOCOL *), DevicePathCompare);
  1394. Status = ShellCommandConsistMappingInitialize (&ConsistMappingTable);
  1395. //
  1396. // Assign new Mappings to remainders
  1397. //
  1398. for (Count = 0; !EFI_ERROR (Status) && HandleList[Count] != NULL; Count++) {
  1399. //
  1400. // Skip ones that already have
  1401. //
  1402. if (gEfiShellProtocol->GetMapFromDevicePath (&DevicePathList[Count]) != NULL) {
  1403. continue;
  1404. }
  1405. //
  1406. // Get default name
  1407. //
  1408. NewDefaultName = ShellCommandCreateNewMappingName (MappingTypeFileSystem);
  1409. if (NewDefaultName == NULL) {
  1410. Status = EFI_OUT_OF_RESOURCES;
  1411. break;
  1412. }
  1413. //
  1414. // Call shell protocol SetMap function now...
  1415. //
  1416. Status = gEfiShellProtocol->SetMap (DevicePathList[Count], NewDefaultName);
  1417. if (!EFI_ERROR (Status)) {
  1418. //
  1419. // Now do consistent name
  1420. //
  1421. NewConsistName = ShellCommandConsistMappingGenMappingName (DevicePathList[Count], ConsistMappingTable);
  1422. if (NewConsistName != NULL) {
  1423. Status = gEfiShellProtocol->SetMap (DevicePathList[Count], NewConsistName);
  1424. FreePool (NewConsistName);
  1425. }
  1426. }
  1427. FreePool (NewDefaultName);
  1428. }
  1429. ShellCommandConsistMappingUnInitialize (ConsistMappingTable);
  1430. SHELL_FREE_NON_NULL (HandleList);
  1431. SHELL_FREE_NON_NULL (DevicePathList);
  1432. HandleList = NULL;
  1433. } else {
  1434. Count = (UINTN)-1;
  1435. }
  1436. //
  1437. // Do it all over again for gEfiBlockIoProtocolGuid
  1438. //
  1439. return (Status);
  1440. }
  1441. /**
  1442. Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
  1443. @param[in] Handle The SHELL_FILE_HANDLE to convert.
  1444. @return a EFI_FILE_PROTOCOL* representing the same file.
  1445. **/
  1446. EFI_FILE_PROTOCOL *
  1447. EFIAPI
  1448. ConvertShellHandleToEfiFileProtocol (
  1449. IN CONST SHELL_FILE_HANDLE Handle
  1450. )
  1451. {
  1452. return ((EFI_FILE_PROTOCOL *)(Handle));
  1453. }
  1454. /**
  1455. Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
  1456. @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
  1457. @param[in] Path The path to the file for verification.
  1458. @return A SHELL_FILE_HANDLE representing the same file.
  1459. @retval NULL There was not enough memory.
  1460. **/
  1461. SHELL_FILE_HANDLE
  1462. EFIAPI
  1463. ConvertEfiFileProtocolToShellHandle (
  1464. IN CONST EFI_FILE_PROTOCOL *Handle,
  1465. IN CONST CHAR16 *Path
  1466. )
  1467. {
  1468. SHELL_COMMAND_FILE_HANDLE *Buffer;
  1469. BUFFER_LIST *NewNode;
  1470. if (Path != NULL) {
  1471. Buffer = AllocateZeroPool (sizeof (SHELL_COMMAND_FILE_HANDLE));
  1472. if (Buffer == NULL) {
  1473. return (NULL);
  1474. }
  1475. NewNode = AllocateZeroPool (sizeof (BUFFER_LIST));
  1476. if (NewNode == NULL) {
  1477. SHELL_FREE_NON_NULL (Buffer);
  1478. return (NULL);
  1479. }
  1480. Buffer->FileHandle = (EFI_FILE_PROTOCOL *)Handle;
  1481. Buffer->Path = StrnCatGrow (&Buffer->Path, NULL, Path, 0);
  1482. if (Buffer->Path == NULL) {
  1483. SHELL_FREE_NON_NULL (NewNode);
  1484. SHELL_FREE_NON_NULL (Buffer);
  1485. return (NULL);
  1486. }
  1487. NewNode->Buffer = Buffer;
  1488. InsertHeadList (&mFileHandleList.Link, &NewNode->Link);
  1489. }
  1490. return ((SHELL_FILE_HANDLE)(Handle));
  1491. }
  1492. /**
  1493. Find the path that was logged with the specified SHELL_FILE_HANDLE.
  1494. @param[in] Handle The SHELL_FILE_HANDLE to query on.
  1495. @return A pointer to the path for the file.
  1496. **/
  1497. CONST CHAR16 *
  1498. EFIAPI
  1499. ShellFileHandleGetPath (
  1500. IN CONST SHELL_FILE_HANDLE Handle
  1501. )
  1502. {
  1503. BUFFER_LIST *Node;
  1504. for (Node = (BUFFER_LIST *)GetFirstNode (&mFileHandleList.Link)
  1505. ; !IsNull (&mFileHandleList.Link, &Node->Link)
  1506. ; Node = (BUFFER_LIST *)GetNextNode (&mFileHandleList.Link, &Node->Link)
  1507. )
  1508. {
  1509. if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)) {
  1510. return (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);
  1511. }
  1512. }
  1513. return (NULL);
  1514. }
  1515. /**
  1516. Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
  1517. @param[in] Handle The SHELL_FILE_HANDLE to remove.
  1518. @retval TRUE The item was removed.
  1519. @retval FALSE The item was not found.
  1520. **/
  1521. BOOLEAN
  1522. EFIAPI
  1523. ShellFileHandleRemove (
  1524. IN CONST SHELL_FILE_HANDLE Handle
  1525. )
  1526. {
  1527. BUFFER_LIST *Node;
  1528. for (Node = (BUFFER_LIST *)GetFirstNode (&mFileHandleList.Link)
  1529. ; !IsNull (&mFileHandleList.Link, &Node->Link)
  1530. ; Node = (BUFFER_LIST *)GetNextNode (&mFileHandleList.Link, &Node->Link)
  1531. )
  1532. {
  1533. if ((Node->Buffer) && (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->FileHandle == Handle)) {
  1534. RemoveEntryList (&Node->Link);
  1535. SHELL_FREE_NON_NULL (((SHELL_COMMAND_FILE_HANDLE *)Node->Buffer)->Path);
  1536. SHELL_FREE_NON_NULL (Node->Buffer);
  1537. SHELL_FREE_NON_NULL (Node);
  1538. return (TRUE);
  1539. }
  1540. }
  1541. return (FALSE);
  1542. }
  1543. /**
  1544. Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
  1545. This will NOT work on directories.
  1546. If Handle is NULL, then ASSERT.
  1547. @param[in] Handle the file handle
  1548. @retval TRUE the position is at the end of the file
  1549. @retval FALSE the position is not at the end of the file
  1550. **/
  1551. BOOLEAN
  1552. EFIAPI
  1553. ShellFileHandleEof (
  1554. IN SHELL_FILE_HANDLE Handle
  1555. )
  1556. {
  1557. EFI_FILE_INFO *Info;
  1558. UINT64 Pos;
  1559. BOOLEAN RetVal;
  1560. //
  1561. // ASSERT if Handle is NULL
  1562. //
  1563. ASSERT (Handle != NULL);
  1564. gEfiShellProtocol->GetFilePosition (Handle, &Pos);
  1565. Info = gEfiShellProtocol->GetFileInfo (Handle);
  1566. gEfiShellProtocol->SetFilePosition (Handle, Pos);
  1567. if (Info == NULL) {
  1568. return (FALSE);
  1569. }
  1570. if (Pos == Info->FileSize) {
  1571. RetVal = TRUE;
  1572. } else {
  1573. RetVal = FALSE;
  1574. }
  1575. FreePool (Info);
  1576. return (RetVal);
  1577. }
  1578. /**
  1579. Frees any BUFFER_LIST defined type.
  1580. @param[in] List The BUFFER_LIST object to free.
  1581. **/
  1582. VOID
  1583. EFIAPI
  1584. FreeBufferList (
  1585. IN BUFFER_LIST *List
  1586. )
  1587. {
  1588. BUFFER_LIST *BufferListEntry;
  1589. if (List == NULL) {
  1590. return;
  1591. }
  1592. //
  1593. // enumerate through the buffer list and free all memory
  1594. //
  1595. for ( BufferListEntry = (BUFFER_LIST *)GetFirstNode (&List->Link)
  1596. ; !IsListEmpty (&List->Link)
  1597. ; BufferListEntry = (BUFFER_LIST *)GetFirstNode (&List->Link)
  1598. )
  1599. {
  1600. RemoveEntryList (&BufferListEntry->Link);
  1601. if (BufferListEntry->Buffer != NULL) {
  1602. FreePool (BufferListEntry->Buffer);
  1603. }
  1604. FreePool (BufferListEntry);
  1605. }
  1606. }
  1607. /**
  1608. Dump some hexadecimal data to the screen.
  1609. @param[in] Indent How many spaces to indent the output.
  1610. @param[in] Offset The offset of the printing.
  1611. @param[in] DataSize The size in bytes of UserData.
  1612. @param[in] UserData The data to print out.
  1613. **/
  1614. VOID
  1615. EFIAPI
  1616. DumpHex (
  1617. IN UINTN Indent,
  1618. IN UINTN Offset,
  1619. IN UINTN DataSize,
  1620. IN VOID *UserData
  1621. )
  1622. {
  1623. UINT8 *Data;
  1624. CHAR8 Val[50];
  1625. CHAR8 Str[20];
  1626. UINT8 TempByte;
  1627. UINTN Size;
  1628. UINTN Index;
  1629. Data = UserData;
  1630. while (DataSize != 0) {
  1631. Size = 16;
  1632. if (Size > DataSize) {
  1633. Size = DataSize;
  1634. }
  1635. for (Index = 0; Index < Size; Index += 1) {
  1636. TempByte = Data[Index];
  1637. Val[Index * 3 + 0] = Hex[TempByte >> 4];
  1638. Val[Index * 3 + 1] = Hex[TempByte & 0xF];
  1639. Val[Index * 3 + 2] = (CHAR8)((Index == 7) ? '-' : ' ');
  1640. Str[Index] = (CHAR8)((TempByte < ' ' || TempByte > '~') ? '.' : TempByte);
  1641. }
  1642. Val[Index * 3] = 0;
  1643. Str[Index] = 0;
  1644. ShellPrintEx (-1, -1, L"%*a%08X: %-48a *%a*\r\n", Indent, "", Offset, Val, Str);
  1645. Data += Size;
  1646. Offset += Size;
  1647. DataSize -= Size;
  1648. }
  1649. }
  1650. /**
  1651. Dump HEX data into buffer.
  1652. @param[in] Buffer HEX data to be dumped in Buffer.
  1653. @param[in] Indent How many spaces to indent the output.
  1654. @param[in] Offset The offset of the printing.
  1655. @param[in] DataSize The size in bytes of UserData.
  1656. @param[in] UserData The data to print out.
  1657. **/
  1658. CHAR16 *
  1659. EFIAPI
  1660. CatSDumpHex (
  1661. IN CHAR16 *Buffer,
  1662. IN UINTN Indent,
  1663. IN UINTN Offset,
  1664. IN UINTN DataSize,
  1665. IN VOID *UserData
  1666. )
  1667. {
  1668. UINT8 *Data;
  1669. UINT8 TempByte;
  1670. UINTN Size;
  1671. UINTN Index;
  1672. CHAR8 Val[50];
  1673. CHAR8 Str[20];
  1674. CHAR16 *RetVal;
  1675. CHAR16 *TempRetVal;
  1676. Data = UserData;
  1677. RetVal = Buffer;
  1678. while (DataSize != 0) {
  1679. Size = 16;
  1680. if (Size > DataSize) {
  1681. Size = DataSize;
  1682. }
  1683. for (Index = 0; Index < Size; Index += 1) {
  1684. TempByte = Data[Index];
  1685. Val[Index * 3 + 0] = Hex[TempByte >> 4];
  1686. Val[Index * 3 + 1] = Hex[TempByte & 0xF];
  1687. Val[Index * 3 + 2] = (CHAR8)((Index == 7) ? '-' : ' ');
  1688. Str[Index] = (CHAR8)((TempByte < ' ' || TempByte > 'z') ? '.' : TempByte);
  1689. }
  1690. Val[Index * 3] = 0;
  1691. Str[Index] = 0;
  1692. TempRetVal = CatSPrint (RetVal, L"%*a%08X: %-48a *%a*\r\n", Indent, "", Offset, Val, Str);
  1693. SHELL_FREE_NON_NULL (RetVal);
  1694. RetVal = TempRetVal;
  1695. Data += Size;
  1696. Offset += Size;
  1697. DataSize -= Size;
  1698. }
  1699. return RetVal;
  1700. }
  1701. /**
  1702. ORDERED_COLLECTION_USER_COMPARE function for SHELL_SORT_UNIQUE_NAME objects.
  1703. @param[in] Unique1AsVoid The first SHELL_SORT_UNIQUE_NAME object (Unique1),
  1704. passed in as a pointer-to-VOID.
  1705. @param[in] Unique2AsVoid The second SHELL_SORT_UNIQUE_NAME object (Unique2),
  1706. passed in as a pointer-to-VOID.
  1707. @retval <0 If Unique1 compares less than Unique2.
  1708. @retval 0 If Unique1 compares equal to Unique2.
  1709. @retval >0 If Unique1 compares greater than Unique2.
  1710. **/
  1711. STATIC
  1712. INTN
  1713. EFIAPI
  1714. UniqueNameCompare (
  1715. IN CONST VOID *Unique1AsVoid,
  1716. IN CONST VOID *Unique2AsVoid
  1717. )
  1718. {
  1719. CONST SHELL_SORT_UNIQUE_NAME *Unique1;
  1720. CONST SHELL_SORT_UNIQUE_NAME *Unique2;
  1721. Unique1 = Unique1AsVoid;
  1722. Unique2 = Unique2AsVoid;
  1723. //
  1724. // We need to cast away CONST for EFI_UNICODE_COLLATION_STRICOLL.
  1725. //
  1726. return gUnicodeCollation->StriColl (
  1727. gUnicodeCollation,
  1728. (CHAR16 *)Unique1->Alias,
  1729. (CHAR16 *)Unique2->Alias
  1730. );
  1731. }
  1732. /**
  1733. ORDERED_COLLECTION_KEY_COMPARE function for SHELL_SORT_UNIQUE_NAME objects.
  1734. @param[in] UniqueAliasAsVoid The CHAR16 string UniqueAlias, passed in as a
  1735. pointer-to-VOID.
  1736. @param[in] UniqueAsVoid The SHELL_SORT_UNIQUE_NAME object (Unique),
  1737. passed in as a pointer-to-VOID.
  1738. @retval <0 If UniqueAlias compares less than Unique->Alias.
  1739. @retval 0 If UniqueAlias compares equal to Unique->Alias.
  1740. @retval >0 If UniqueAlias compares greater than Unique->Alias.
  1741. **/
  1742. STATIC
  1743. INTN
  1744. EFIAPI
  1745. UniqueNameAliasCompare (
  1746. IN CONST VOID *UniqueAliasAsVoid,
  1747. IN CONST VOID *UniqueAsVoid
  1748. )
  1749. {
  1750. CONST CHAR16 *UniqueAlias;
  1751. CONST SHELL_SORT_UNIQUE_NAME *Unique;
  1752. UniqueAlias = UniqueAliasAsVoid;
  1753. Unique = UniqueAsVoid;
  1754. //
  1755. // We need to cast away CONST for EFI_UNICODE_COLLATION_STRICOLL.
  1756. //
  1757. return gUnicodeCollation->StriColl (
  1758. gUnicodeCollation,
  1759. (CHAR16 *)UniqueAlias,
  1760. (CHAR16 *)Unique->Alias
  1761. );
  1762. }
  1763. /**
  1764. Sort an EFI_SHELL_FILE_INFO list, optionally moving duplicates to a separate
  1765. list.
  1766. @param[in,out] FileList The list of EFI_SHELL_FILE_INFO objects to sort.
  1767. If FileList is NULL on input, then FileList is
  1768. considered an empty, hence already sorted, list.
  1769. Otherwise, if (*FileList) is NULL on input, then
  1770. EFI_INVALID_PARAMETER is returned.
  1771. Otherwise, the caller is responsible for having
  1772. initialized (*FileList)->Link with
  1773. InitializeListHead(). No other fields in the
  1774. (**FileList) head element are accessed by this
  1775. function.
  1776. On output, (*FileList) is sorted according to Order.
  1777. If Duplicates is NULL on input, then duplicate
  1778. elements are preserved, sorted stably, on
  1779. (*FileList). If Duplicates is not NULL on input,
  1780. then duplicates are moved (stably sorted) to the
  1781. new, dynamically allocated (*Duplicates) list.
  1782. @param[out] Duplicates If Duplicates is NULL on input, (*FileList) will be
  1783. a monotonically ordered list on output, with
  1784. duplicates stably sorted.
  1785. If Duplicates is not NULL on input, (*FileList) will
  1786. be a strictly monotonically oredered list on output,
  1787. with duplicates separated (stably sorted) to
  1788. (*Duplicates). All fields except Link will be
  1789. zero-initialized in the (**Duplicates) head element.
  1790. If no duplicates exist, then (*Duplicates) is set to
  1791. NULL on output.
  1792. @param[in] Order Determines the comparison operation between
  1793. EFI_SHELL_FILE_INFO objects.
  1794. @retval EFI_INVALID_PARAMETER (UINTN)Order is greater than or equal to
  1795. (UINTN)ShellSortFileListMax. Neither the
  1796. (*FileList) nor the (*Duplicates) list has
  1797. been modified.
  1798. @retval EFI_INVALID_PARAMETER (*FileList) was NULL on input. Neither the
  1799. (*FileList) nor the (*Duplicates) list has
  1800. been modified.
  1801. @retval EFI_OUT_OF_RESOURCES Memory allocation failed. Neither the
  1802. (*FileList) nor the (*Duplicates) list has
  1803. been modified.
  1804. @retval EFI_SUCCESS Sorting successful, including the case when
  1805. FileList is NULL on input.
  1806. **/
  1807. EFI_STATUS
  1808. EFIAPI
  1809. ShellSortFileList (
  1810. IN OUT EFI_SHELL_FILE_INFO **FileList,
  1811. OUT EFI_SHELL_FILE_INFO **Duplicates OPTIONAL,
  1812. IN SHELL_SORT_FILE_LIST Order
  1813. )
  1814. {
  1815. LIST_ENTRY *FilesHead;
  1816. ORDERED_COLLECTION *Sort;
  1817. LIST_ENTRY *FileEntry;
  1818. EFI_SHELL_FILE_INFO *FileInfo;
  1819. SHELL_SORT_UNIQUE_NAME *Unique;
  1820. EFI_STATUS Status;
  1821. EFI_SHELL_FILE_INFO *Dupes;
  1822. LIST_ENTRY *NextFileEntry;
  1823. CONST CHAR16 *Alias;
  1824. ORDERED_COLLECTION_ENTRY *SortEntry;
  1825. LIST_ENTRY *TargetFileList;
  1826. ORDERED_COLLECTION_ENTRY *NextSortEntry;
  1827. VOID *UniqueAsVoid;
  1828. if ((UINTN)Order >= (UINTN)ShellSortFileListMax) {
  1829. return EFI_INVALID_PARAMETER;
  1830. }
  1831. if (FileList == NULL) {
  1832. //
  1833. // FileList is considered empty, hence already sorted, with no duplicates.
  1834. //
  1835. if (Duplicates != NULL) {
  1836. *Duplicates = NULL;
  1837. }
  1838. return EFI_SUCCESS;
  1839. }
  1840. if (*FileList == NULL) {
  1841. return EFI_INVALID_PARAMETER;
  1842. }
  1843. FilesHead = &(*FileList)->Link;
  1844. //
  1845. // Collect all the unique names.
  1846. //
  1847. Sort = OrderedCollectionInit (UniqueNameCompare, UniqueNameAliasCompare);
  1848. if (Sort == NULL) {
  1849. return EFI_OUT_OF_RESOURCES;
  1850. }
  1851. BASE_LIST_FOR_EACH (FileEntry, FilesHead) {
  1852. FileInfo = (EFI_SHELL_FILE_INFO *)FileEntry;
  1853. //
  1854. // Try to record the name of this file as a unique name.
  1855. //
  1856. Unique = AllocatePool (sizeof (*Unique));
  1857. if (Unique == NULL) {
  1858. Status = EFI_OUT_OF_RESOURCES;
  1859. goto UninitSort;
  1860. }
  1861. Unique->Alias = ((Order == ShellSortFileListByFileName) ?
  1862. FileInfo->FileName :
  1863. FileInfo->FullName);
  1864. InitializeListHead (&Unique->SameNameList);
  1865. Status = OrderedCollectionInsert (Sort, NULL, Unique);
  1866. if (EFI_ERROR (Status)) {
  1867. //
  1868. // Only two errors are possible: memory allocation failed, or this name
  1869. // has been encountered before. In either case, the
  1870. // SHELL_SORT_UNIQUE_NAME object being constructed has to be released.
  1871. //
  1872. FreePool (Unique);
  1873. //
  1874. // Memory allocation failure is fatal, while having seen the same name
  1875. // before is normal.
  1876. //
  1877. if (Status == EFI_OUT_OF_RESOURCES) {
  1878. goto UninitSort;
  1879. }
  1880. ASSERT (Status == EFI_ALREADY_STARTED);
  1881. }
  1882. }
  1883. //
  1884. // Set Dupes to suppress incorrect compiler/analyzer warnings.
  1885. //
  1886. Dupes = NULL;
  1887. //
  1888. // If separation of duplicates has been requested, allocate the list for
  1889. // them.
  1890. //
  1891. if (Duplicates != NULL) {
  1892. Dupes = AllocateZeroPool (sizeof (*Dupes));
  1893. if (Dupes == NULL) {
  1894. Status = EFI_OUT_OF_RESOURCES;
  1895. goto UninitSort;
  1896. }
  1897. InitializeListHead (&Dupes->Link);
  1898. }
  1899. //
  1900. // No memory allocation beyond this point; thus, no chance to fail. We can
  1901. // now migrate the EFI_SHELL_FILE_INFO objects from (*FileList) to Sort.
  1902. //
  1903. BASE_LIST_FOR_EACH_SAFE (FileEntry, NextFileEntry, FilesHead) {
  1904. FileInfo = (EFI_SHELL_FILE_INFO *)FileEntry;
  1905. //
  1906. // Look up the SHELL_SORT_UNIQUE_NAME that matches FileInfo's name.
  1907. //
  1908. Alias = ((Order == ShellSortFileListByFileName) ?
  1909. FileInfo->FileName :
  1910. FileInfo->FullName);
  1911. SortEntry = OrderedCollectionFind (Sort, Alias);
  1912. ASSERT (SortEntry != NULL);
  1913. Unique = OrderedCollectionUserStruct (SortEntry);
  1914. //
  1915. // Move FileInfo from (*FileList) to the end of the list of files whose
  1916. // names all compare identical to FileInfo's name.
  1917. //
  1918. RemoveEntryList (&FileInfo->Link);
  1919. InsertTailList (&Unique->SameNameList, &FileInfo->Link);
  1920. }
  1921. //
  1922. // All EFI_SHELL_FILE_INFO objects originally in (*FileList) have been
  1923. // distributed to Sort. Now migrate them back to (*FileList), advancing in
  1924. // unique name order.
  1925. //
  1926. for (SortEntry = OrderedCollectionMin (Sort);
  1927. SortEntry != NULL;
  1928. SortEntry = OrderedCollectionNext (SortEntry))
  1929. {
  1930. Unique = OrderedCollectionUserStruct (SortEntry);
  1931. //
  1932. // The first FileInfo encountered for each unique name goes back on
  1933. // (*FileList) unconditionally. Further FileInfo instances for the same
  1934. // unique name -- that is, duplicates -- are either returned to (*FileList)
  1935. // or separated, dependent on the caller's request.
  1936. //
  1937. TargetFileList = FilesHead;
  1938. BASE_LIST_FOR_EACH_SAFE (FileEntry, NextFileEntry, &Unique->SameNameList) {
  1939. RemoveEntryList (FileEntry);
  1940. InsertTailList (TargetFileList, FileEntry);
  1941. if (Duplicates != NULL) {
  1942. TargetFileList = &Dupes->Link;
  1943. }
  1944. }
  1945. }
  1946. //
  1947. // We're done. If separation of duplicates has been requested, output the
  1948. // list of duplicates -- and free that list at once, if it's empty (i.e., if
  1949. // no duplicates have been found).
  1950. //
  1951. if (Duplicates != NULL) {
  1952. if (IsListEmpty (&Dupes->Link)) {
  1953. FreePool (Dupes);
  1954. *Duplicates = NULL;
  1955. } else {
  1956. *Duplicates = Dupes;
  1957. }
  1958. }
  1959. Status = EFI_SUCCESS;
  1960. //
  1961. // Fall through.
  1962. //
  1963. UninitSort:
  1964. for (SortEntry = OrderedCollectionMin (Sort);
  1965. SortEntry != NULL;
  1966. SortEntry = NextSortEntry)
  1967. {
  1968. NextSortEntry = OrderedCollectionNext (SortEntry);
  1969. OrderedCollectionDelete (Sort, SortEntry, &UniqueAsVoid);
  1970. Unique = UniqueAsVoid;
  1971. ASSERT (IsListEmpty (&Unique->SameNameList));
  1972. FreePool (Unique);
  1973. }
  1974. OrderedCollectionUninit (Sort);
  1975. return Status;
  1976. }