ShellProtocol.c 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049
  1. /** @file
  2. Member functions of EFI_SHELL_PROTOCOL and functions for creation,
  3. manipulation, and initialization of EFI_SHELL_PROTOCOL.
  4. (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
  5. (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
  6. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  7. SPDX-License-Identifier: BSD-2-Clause-Patent
  8. **/
  9. #include "Shell.h"
  10. #define INIT_NAME_BUFFER_SIZE 128
  11. /**
  12. Close an open file handle.
  13. This function closes a specified file handle. All "dirty" cached file data is
  14. flushed to the device, and the file is closed. In all cases the handle is
  15. closed.
  16. @param[in] FileHandle The file handle to close.
  17. @retval EFI_SUCCESS The file handle was closed successfully.
  18. **/
  19. EFI_STATUS
  20. EFIAPI
  21. EfiShellClose (
  22. IN SHELL_FILE_HANDLE FileHandle
  23. )
  24. {
  25. ShellFileHandleRemove (FileHandle);
  26. return (FileHandleClose (ConvertShellHandleToEfiFileProtocol (FileHandle)));
  27. }
  28. /**
  29. Internal worker to determine whether there is a BlockIo somewhere
  30. upon the device path specified.
  31. @param[in] DevicePath The device path to test.
  32. @retval TRUE gEfiBlockIoProtocolGuid was installed on a handle with this device path
  33. @retval FALSE gEfiBlockIoProtocolGuid was not found.
  34. **/
  35. BOOLEAN
  36. InternalShellProtocolIsBlockIoPresent (
  37. IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
  38. )
  39. {
  40. EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
  41. EFI_STATUS Status;
  42. EFI_HANDLE Handle;
  43. Handle = NULL;
  44. DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
  45. Status = gBS->LocateDevicePath (&gEfiBlockIoProtocolGuid, &DevicePathCopy, &Handle);
  46. if ((Handle != NULL) && (!EFI_ERROR (Status))) {
  47. return (TRUE);
  48. }
  49. return (FALSE);
  50. }
  51. /**
  52. Internal worker to determine whether there is a file system somewhere
  53. upon the device path specified.
  54. @param[in] DevicePath The device path to test.
  55. @retval TRUE gEfiSimpleFileSystemProtocolGuid was installed on a handle with this device path
  56. @retval FALSE gEfiSimpleFileSystemProtocolGuid was not found.
  57. **/
  58. BOOLEAN
  59. InternalShellProtocolIsSimpleFileSystemPresent (
  60. IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath
  61. )
  62. {
  63. EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
  64. EFI_STATUS Status;
  65. EFI_HANDLE Handle;
  66. Handle = NULL;
  67. DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL *)DevicePath;
  68. Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &Handle);
  69. if ((Handle != NULL) && (!EFI_ERROR (Status))) {
  70. return (TRUE);
  71. }
  72. return (FALSE);
  73. }
  74. /**
  75. This function creates a mapping for a device path.
  76. If both DevicePath and Mapping are NULL, this will reset the mapping to default values.
  77. @param DevicePath Points to the device path. If this is NULL and Mapping points to a valid mapping,
  78. then the mapping will be deleted.
  79. @param Mapping Points to the NULL-terminated mapping for the device path. Must end with a ':'
  80. @retval EFI_SUCCESS Mapping created or deleted successfully.
  81. @retval EFI_NO_MAPPING There is no handle that corresponds exactly to DevicePath. See the
  82. boot service function LocateDevicePath().
  83. @retval EFI_ACCESS_DENIED The mapping is a built-in alias.
  84. @retval EFI_INVALID_PARAMETER Mapping was NULL
  85. @retval EFI_INVALID_PARAMETER Mapping did not end with a ':'
  86. @retval EFI_INVALID_PARAMETER DevicePath was not pointing at a device that had a SIMPLE_FILE_SYSTEM_PROTOCOL installed.
  87. @retval EFI_NOT_FOUND There was no mapping found to delete
  88. @retval EFI_OUT_OF_RESOURCES Memory allocation failed
  89. **/
  90. EFI_STATUS
  91. EFIAPI
  92. EfiShellSetMap (
  93. IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,
  94. IN CONST CHAR16 *Mapping
  95. )
  96. {
  97. EFI_STATUS Status;
  98. SHELL_MAP_LIST *MapListNode;
  99. if (Mapping == NULL) {
  100. return (EFI_INVALID_PARAMETER);
  101. }
  102. if (Mapping[StrLen (Mapping)-1] != ':') {
  103. return (EFI_INVALID_PARAMETER);
  104. }
  105. //
  106. // Delete the mapping
  107. //
  108. if (DevicePath == NULL) {
  109. if (IsListEmpty (&gShellMapList.Link)) {
  110. return (EFI_NOT_FOUND);
  111. }
  112. for ( MapListNode = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
  113. ; !IsNull (&gShellMapList.Link, &MapListNode->Link)
  114. ; MapListNode = (SHELL_MAP_LIST *)GetNextNode (&gShellMapList.Link, &MapListNode->Link)
  115. )
  116. {
  117. if (StringNoCaseCompare (&MapListNode->MapName, &Mapping) == 0) {
  118. RemoveEntryList (&MapListNode->Link);
  119. SHELL_FREE_NON_NULL (MapListNode->DevicePath);
  120. SHELL_FREE_NON_NULL (MapListNode->MapName);
  121. SHELL_FREE_NON_NULL (MapListNode->CurrentDirectoryPath);
  122. FreePool (MapListNode);
  123. return (EFI_SUCCESS);
  124. }
  125. } // for loop
  126. //
  127. // We didn't find one to delete
  128. //
  129. return (EFI_NOT_FOUND);
  130. }
  131. //
  132. // make sure this is a valid to add device path
  133. //
  134. /// @todo add BlockIo to this test...
  135. if ( !InternalShellProtocolIsSimpleFileSystemPresent (DevicePath)
  136. && !InternalShellProtocolIsBlockIoPresent (DevicePath))
  137. {
  138. return (EFI_INVALID_PARAMETER);
  139. }
  140. //
  141. // First make sure there is no old mapping
  142. //
  143. Status = EfiShellSetMap (NULL, Mapping);
  144. if ((Status != EFI_SUCCESS) && (Status != EFI_NOT_FOUND)) {
  145. return (Status);
  146. }
  147. //
  148. // now add the new one.
  149. //
  150. Status = ShellCommandAddMapItemAndUpdatePath (Mapping, DevicePath, 0, FALSE);
  151. return (Status);
  152. }
  153. /**
  154. Gets the device path from the mapping.
  155. This function gets the device path associated with a mapping.
  156. @param Mapping A pointer to the mapping
  157. @retval !=NULL Pointer to the device path that corresponds to the
  158. device mapping. The returned pointer does not need
  159. to be freed.
  160. @retval NULL There is no device path associated with the
  161. specified mapping.
  162. **/
  163. CONST EFI_DEVICE_PATH_PROTOCOL *
  164. EFIAPI
  165. EfiShellGetDevicePathFromMap (
  166. IN CONST CHAR16 *Mapping
  167. )
  168. {
  169. SHELL_MAP_LIST *MapListItem;
  170. CHAR16 *NewName;
  171. UINTN Size;
  172. NewName = NULL;
  173. Size = 0;
  174. StrnCatGrow (&NewName, &Size, Mapping, 0);
  175. if (Mapping[StrLen (Mapping)-1] != L':') {
  176. StrnCatGrow (&NewName, &Size, L":", 0);
  177. }
  178. MapListItem = ShellCommandFindMapItem (NewName);
  179. FreePool (NewName);
  180. if (MapListItem != NULL) {
  181. return (MapListItem->DevicePath);
  182. }
  183. return (NULL);
  184. }
  185. /**
  186. Gets the mapping(s) that most closely matches the device path.
  187. This function gets the mapping which corresponds to the device path *DevicePath. If
  188. there is no exact match, then the mapping which most closely matches *DevicePath
  189. is returned, and *DevicePath is updated to point to the remaining portion of the
  190. device path. If there is an exact match, the mapping is returned and *DevicePath
  191. points to the end-of-device-path node.
  192. If there are multiple map names they will be semi-colon separated in the
  193. NULL-terminated string.
  194. @param DevicePath On entry, points to a device path pointer. On
  195. exit, updates the pointer to point to the
  196. portion of the device path after the mapping.
  197. @retval NULL No mapping was found.
  198. @return !=NULL Pointer to NULL-terminated mapping. The buffer
  199. is callee allocated and should be freed by the caller.
  200. **/
  201. CONST CHAR16 *
  202. EFIAPI
  203. EfiShellGetMapFromDevicePath (
  204. IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
  205. )
  206. {
  207. SHELL_MAP_LIST *Node;
  208. CHAR16 *PathForReturn;
  209. UINTN PathSize;
  210. // EFI_HANDLE PathHandle;
  211. // EFI_HANDLE MapHandle;
  212. // EFI_STATUS Status;
  213. // EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
  214. // EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
  215. if ((DevicePath == NULL) || (*DevicePath == NULL)) {
  216. return (NULL);
  217. }
  218. PathForReturn = NULL;
  219. PathSize = 0;
  220. for ( Node = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
  221. ; !IsNull (&gShellMapList.Link, &Node->Link)
  222. ; Node = (SHELL_MAP_LIST *)GetNextNode (&gShellMapList.Link, &Node->Link)
  223. )
  224. {
  225. //
  226. // check for exact match
  227. //
  228. if (DevicePathCompare (DevicePath, &Node->DevicePath) == 0) {
  229. ASSERT ((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
  230. if (PathSize != 0) {
  231. PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, L";", 0);
  232. }
  233. PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, Node->MapName, 0);
  234. }
  235. }
  236. if (PathForReturn != NULL) {
  237. while (!IsDevicePathEndType (*DevicePath)) {
  238. *DevicePath = NextDevicePathNode (*DevicePath);
  239. }
  240. //
  241. // Do not call SetDevicePathEndNode() if the device path node is already the
  242. // end of an entire device path.
  243. //
  244. if (!IsDevicePathEnd (*DevicePath)) {
  245. SetDevicePathEndNode (*DevicePath);
  246. }
  247. }
  248. /*
  249. ///@todo finish code for inexact matches.
  250. if (PathForReturn == NULL) {
  251. PathSize = 0;
  252. DevicePathCopy = DuplicateDevicePath(*DevicePath);
  253. ASSERT(DevicePathCopy != NULL);
  254. Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
  255. ASSERT_EFI_ERROR(Status);
  256. //
  257. // check each of the device paths we have to get the root of the path for consist mappings
  258. //
  259. for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
  260. ; !IsNull(&gShellMapList.Link, &Node->Link)
  261. ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
  262. ){
  263. if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) == 0) {
  264. continue;
  265. }
  266. MapPathCopy = DuplicateDevicePath(Node->DevicePath);
  267. ASSERT(MapPathCopy != NULL);
  268. Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
  269. if (MapHandle == PathHandle) {
  270. *DevicePath = DevicePathCopy;
  271. MapPathCopy = NULL;
  272. DevicePathCopy = NULL;
  273. PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
  274. PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
  275. break;
  276. }
  277. }
  278. //
  279. // now add on the non-consistent mappings
  280. //
  281. for ( Node = (SHELL_MAP_LIST *)GetFirstNode(&gShellMapList.Link)
  282. ; !IsNull(&gShellMapList.Link, &Node->Link)
  283. ; Node = (SHELL_MAP_LIST *)GetNextNode(&gShellMapList.Link, &Node->Link)
  284. ){
  285. if ((Node->Flags & SHELL_MAP_FLAGS_CONSIST) != 0) {
  286. continue;
  287. }
  288. MapPathCopy = Node->DevicePath;
  289. ASSERT(MapPathCopy != NULL);
  290. Status = gBS->LocateDevicePath(&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
  291. if (MapHandle == PathHandle) {
  292. PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, Node->MapName, 0);
  293. PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L";", 0);
  294. break;
  295. }
  296. }
  297. }
  298. */
  299. return (AddBufferToFreeList (PathForReturn));
  300. }
  301. /**
  302. Converts a device path to a file system-style path.
  303. This function converts a device path to a file system path by replacing part, or all, of
  304. the device path with the file-system mapping. If there are more than one application
  305. file system mappings, the one that most closely matches Path will be used.
  306. @param Path The pointer to the device path
  307. @retval NULL the device path could not be found.
  308. @return all The pointer of the NULL-terminated file path. The path
  309. is callee-allocated and should be freed by the caller.
  310. **/
  311. CHAR16 *
  312. EFIAPI
  313. EfiShellGetFilePathFromDevicePath (
  314. IN CONST EFI_DEVICE_PATH_PROTOCOL *Path
  315. )
  316. {
  317. EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
  318. EFI_DEVICE_PATH_PROTOCOL *MapPathCopy;
  319. SHELL_MAP_LIST *MapListItem;
  320. CHAR16 *PathForReturn;
  321. UINTN PathSize;
  322. EFI_HANDLE PathHandle;
  323. EFI_HANDLE MapHandle;
  324. EFI_STATUS Status;
  325. FILEPATH_DEVICE_PATH *FilePath;
  326. FILEPATH_DEVICE_PATH *AlignedNode;
  327. PathForReturn = NULL;
  328. PathSize = 0;
  329. DevicePathCopy = (EFI_DEVICE_PATH_PROTOCOL *)Path;
  330. ASSERT (DevicePathCopy != NULL);
  331. if (DevicePathCopy == NULL) {
  332. return (NULL);
  333. }
  334. /// @todo BlockIo?
  335. Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &PathHandle);
  336. if (EFI_ERROR (Status)) {
  337. return (NULL);
  338. }
  339. //
  340. // check each of the device paths we have to get the root of the path
  341. //
  342. for ( MapListItem = (SHELL_MAP_LIST *)GetFirstNode (&gShellMapList.Link)
  343. ; !IsNull (&gShellMapList.Link, &MapListItem->Link)
  344. ; MapListItem = (SHELL_MAP_LIST *)GetNextNode (&gShellMapList.Link, &MapListItem->Link)
  345. )
  346. {
  347. MapPathCopy = (EFI_DEVICE_PATH_PROTOCOL *)MapListItem->DevicePath;
  348. ASSERT (MapPathCopy != NULL);
  349. /// @todo BlockIo?
  350. Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &MapPathCopy, &MapHandle);
  351. if (MapHandle == PathHandle) {
  352. ASSERT ((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
  353. PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, MapListItem->MapName, 0);
  354. //
  355. // go through all the remaining nodes in the device path
  356. //
  357. for ( FilePath = (FILEPATH_DEVICE_PATH *)DevicePathCopy
  358. ; !IsDevicePathEnd (&FilePath->Header)
  359. ; FilePath = (FILEPATH_DEVICE_PATH *)NextDevicePathNode (&FilePath->Header)
  360. )
  361. {
  362. //
  363. // If any node is not a file path node, then the conversion can not be completed
  364. //
  365. if ((DevicePathType (&FilePath->Header) != MEDIA_DEVICE_PATH) ||
  366. (DevicePathSubType (&FilePath->Header) != MEDIA_FILEPATH_DP))
  367. {
  368. FreePool (PathForReturn);
  369. return NULL;
  370. }
  371. //
  372. // append the path part onto the filepath.
  373. //
  374. ASSERT ((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));
  375. AlignedNode = AllocateCopyPool (DevicePathNodeLength (FilePath), FilePath);
  376. if (AlignedNode == NULL) {
  377. FreePool (PathForReturn);
  378. return NULL;
  379. }
  380. // File Path Device Path Nodes 'can optionally add a "\" separator to
  381. // the beginning and/or the end of the Path Name string.'
  382. // (UEFI Spec 2.4 section 9.3.6.4).
  383. // If necessary, add a "\", but otherwise don't
  384. // (This is specified in the above section, and also implied by the
  385. // UEFI Shell spec section 3.7)
  386. if ((PathSize != 0) &&
  387. (PathForReturn != NULL) &&
  388. (PathForReturn[PathSize / sizeof (CHAR16) - 1] != L'\\') &&
  389. (AlignedNode->PathName[0] != L'\\'))
  390. {
  391. PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, L"\\", 1);
  392. }
  393. PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, AlignedNode->PathName, 0);
  394. FreePool (AlignedNode);
  395. } // for loop of remaining nodes
  396. }
  397. if (PathForReturn != NULL) {
  398. break;
  399. }
  400. } // for loop of paths to check
  401. return (PathForReturn);
  402. }
  403. /**
  404. Converts a file system style name to a device path.
  405. This function converts a file system style name to a device path, by replacing any
  406. mapping references to the associated device path.
  407. @param[in] Path The pointer to the path.
  408. @return The pointer of the file path. The file path is callee
  409. allocated and should be freed by the caller.
  410. @retval NULL The path could not be found.
  411. @retval NULL There was not enough available memory.
  412. **/
  413. EFI_DEVICE_PATH_PROTOCOL *
  414. EFIAPI
  415. EfiShellGetDevicePathFromFilePath (
  416. IN CONST CHAR16 *Path
  417. )
  418. {
  419. CHAR16 *MapName;
  420. CHAR16 *NewPath;
  421. CONST CHAR16 *Cwd;
  422. UINTN Size;
  423. CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  424. EFI_DEVICE_PATH_PROTOCOL *DevicePathCopy;
  425. EFI_DEVICE_PATH_PROTOCOL *DevicePathCopyForFree;
  426. EFI_DEVICE_PATH_PROTOCOL *DevicePathForReturn;
  427. EFI_HANDLE Handle;
  428. EFI_STATUS Status;
  429. if (Path == NULL) {
  430. return (NULL);
  431. }
  432. MapName = NULL;
  433. NewPath = NULL;
  434. if (StrStr (Path, L":") == NULL) {
  435. Cwd = EfiShellGetCurDir (NULL);
  436. if (Cwd == NULL) {
  437. return (NULL);
  438. }
  439. Size = StrSize (Cwd) + StrSize (Path);
  440. NewPath = AllocateZeroPool (Size);
  441. if (NewPath == NULL) {
  442. return (NULL);
  443. }
  444. StrCpyS (NewPath, Size/sizeof (CHAR16), Cwd);
  445. StrCatS (NewPath, Size/sizeof (CHAR16), L"\\");
  446. if (*Path == L'\\') {
  447. Path++;
  448. while (PathRemoveLastItem (NewPath)) {
  449. }
  450. }
  451. StrCatS (NewPath, Size/sizeof (CHAR16), Path);
  452. DevicePathForReturn = EfiShellGetDevicePathFromFilePath (NewPath);
  453. FreePool (NewPath);
  454. return (DevicePathForReturn);
  455. }
  456. Size = 0;
  457. //
  458. // find the part before (but including) the : for the map name
  459. //
  460. ASSERT ((MapName == NULL && Size == 0) || (MapName != NULL));
  461. MapName = StrnCatGrow (&MapName, &Size, Path, (StrStr (Path, L":")-Path+1));
  462. if ((MapName == NULL) || (MapName[StrLen (MapName)-1] != L':')) {
  463. return (NULL);
  464. }
  465. //
  466. // look up the device path in the map
  467. //
  468. DevicePath = EfiShellGetDevicePathFromMap (MapName);
  469. if (DevicePath == NULL) {
  470. //
  471. // Must have been a bad Mapname
  472. //
  473. return (NULL);
  474. }
  475. //
  476. // make a copy for LocateDevicePath to modify (also save a pointer to call FreePool with)
  477. //
  478. DevicePathCopyForFree = DevicePathCopy = DuplicateDevicePath (DevicePath);
  479. if (DevicePathCopy == NULL) {
  480. FreePool (MapName);
  481. return (NULL);
  482. }
  483. //
  484. // get the handle
  485. //
  486. /// @todo BlockIo?
  487. Status = gBS->LocateDevicePath (&gEfiSimpleFileSystemProtocolGuid, &DevicePathCopy, &Handle);
  488. if (EFI_ERROR (Status)) {
  489. if (DevicePathCopyForFree != NULL) {
  490. FreePool (DevicePathCopyForFree);
  491. }
  492. FreePool (MapName);
  493. return (NULL);
  494. }
  495. //
  496. // build the full device path
  497. //
  498. if ((*(Path+StrLen (MapName)) != CHAR_NULL) &&
  499. (*(Path+StrLen (MapName)+1) == CHAR_NULL))
  500. {
  501. DevicePathForReturn = FileDevicePath (Handle, L"\\");
  502. } else {
  503. DevicePathForReturn = FileDevicePath (Handle, Path+StrLen (MapName));
  504. }
  505. FreePool (MapName);
  506. if (DevicePathCopyForFree != NULL) {
  507. FreePool (DevicePathCopyForFree);
  508. }
  509. return (DevicePathForReturn);
  510. }
  511. /**
  512. Gets the name of the device specified by the device handle.
  513. This function gets the user-readable name of the device specified by the device
  514. handle. If no user-readable name could be generated, then *BestDeviceName will be
  515. NULL and EFI_NOT_FOUND will be returned.
  516. If EFI_DEVICE_NAME_USE_COMPONENT_NAME is set, then the function will return the
  517. device's name using the EFI_COMPONENT_NAME2_PROTOCOL, if present on
  518. DeviceHandle.
  519. If EFI_DEVICE_NAME_USE_DEVICE_PATH is set, then the function will return the
  520. device's name using the EFI_DEVICE_PATH_PROTOCOL, if present on DeviceHandle.
  521. If both EFI_DEVICE_NAME_USE_COMPONENT_NAME and
  522. EFI_DEVICE_NAME_USE_DEVICE_PATH are set, then
  523. EFI_DEVICE_NAME_USE_COMPONENT_NAME will have higher priority.
  524. @param DeviceHandle The handle of the device.
  525. @param Flags Determines the possible sources of component names.
  526. Valid bits are:
  527. EFI_DEVICE_NAME_USE_COMPONENT_NAME
  528. EFI_DEVICE_NAME_USE_DEVICE_PATH
  529. @param Language A pointer to the language specified for the device
  530. name, in the same format as described in the UEFI
  531. specification, Appendix M
  532. @param BestDeviceName On return, points to the callee-allocated NULL-
  533. terminated name of the device. If no device name
  534. could be found, points to NULL. The name must be
  535. freed by the caller...
  536. @retval EFI_SUCCESS Get the name successfully.
  537. @retval EFI_NOT_FOUND Fail to get the device name.
  538. @retval EFI_INVALID_PARAMETER Flags did not have a valid bit set.
  539. @retval EFI_INVALID_PARAMETER BestDeviceName was NULL
  540. @retval EFI_INVALID_PARAMETER DeviceHandle was NULL
  541. **/
  542. EFI_STATUS
  543. EFIAPI
  544. EfiShellGetDeviceName (
  545. IN EFI_HANDLE DeviceHandle,
  546. IN EFI_SHELL_DEVICE_NAME_FLAGS Flags,
  547. IN CHAR8 *Language,
  548. OUT CHAR16 **BestDeviceName
  549. )
  550. {
  551. EFI_STATUS Status;
  552. EFI_COMPONENT_NAME2_PROTOCOL *CompName2;
  553. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  554. EFI_HANDLE *HandleList;
  555. UINTN HandleCount;
  556. UINTN LoopVar;
  557. CHAR16 *DeviceNameToReturn;
  558. CHAR8 *Lang;
  559. UINTN ParentControllerCount;
  560. EFI_HANDLE *ParentControllerBuffer;
  561. UINTN ParentDriverCount;
  562. EFI_HANDLE *ParentDriverBuffer;
  563. if ((BestDeviceName == NULL) ||
  564. (DeviceHandle == NULL)
  565. )
  566. {
  567. return (EFI_INVALID_PARAMETER);
  568. }
  569. //
  570. // make sure one of the 2 supported bits is on
  571. //
  572. if (((Flags & EFI_DEVICE_NAME_USE_COMPONENT_NAME) == 0) &&
  573. ((Flags & EFI_DEVICE_NAME_USE_DEVICE_PATH) == 0))
  574. {
  575. return (EFI_INVALID_PARAMETER);
  576. }
  577. DeviceNameToReturn = NULL;
  578. *BestDeviceName = NULL;
  579. HandleList = NULL;
  580. HandleCount = 0;
  581. Lang = NULL;
  582. if ((Flags & EFI_DEVICE_NAME_USE_COMPONENT_NAME) != 0) {
  583. Status = ParseHandleDatabaseByRelationship (
  584. NULL,
  585. DeviceHandle,
  586. HR_DRIVER_BINDING_HANDLE|HR_DEVICE_DRIVER,
  587. &HandleCount,
  588. &HandleList
  589. );
  590. for (LoopVar = 0; LoopVar < HandleCount; LoopVar++) {
  591. //
  592. // Go through those handles until we get one that passes for GetComponentName
  593. //
  594. Status = gBS->OpenProtocol (
  595. HandleList[LoopVar],
  596. &gEfiComponentName2ProtocolGuid,
  597. (VOID **)&CompName2,
  598. gImageHandle,
  599. NULL,
  600. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  601. );
  602. if (EFI_ERROR (Status)) {
  603. Status = gBS->OpenProtocol (
  604. HandleList[LoopVar],
  605. &gEfiComponentNameProtocolGuid,
  606. (VOID **)&CompName2,
  607. gImageHandle,
  608. NULL,
  609. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  610. );
  611. }
  612. if (EFI_ERROR (Status)) {
  613. continue;
  614. }
  615. Lang = GetBestLanguageForDriver (CompName2->SupportedLanguages, Language, FALSE);
  616. Status = CompName2->GetControllerName (CompName2, DeviceHandle, NULL, Lang, &DeviceNameToReturn);
  617. FreePool (Lang);
  618. Lang = NULL;
  619. if (!EFI_ERROR (Status) && (DeviceNameToReturn != NULL)) {
  620. break;
  621. }
  622. }
  623. if (HandleList != NULL) {
  624. FreePool (HandleList);
  625. }
  626. //
  627. // Now check the parent controller using this as the child.
  628. //
  629. Status = PARSE_HANDLE_DATABASE_PARENTS (DeviceHandle, &ParentControllerCount, &ParentControllerBuffer);
  630. if ((DeviceNameToReturn == NULL) && !EFI_ERROR (Status)) {
  631. for (LoopVar = 0; LoopVar < ParentControllerCount; LoopVar++) {
  632. Status = PARSE_HANDLE_DATABASE_UEFI_DRIVERS (ParentControllerBuffer[LoopVar], &ParentDriverCount, &ParentDriverBuffer);
  633. if (!EFI_ERROR (Status)) {
  634. for (HandleCount = 0; HandleCount < ParentDriverCount; HandleCount++) {
  635. //
  636. // try using that driver's component name with controller and our driver as the child.
  637. //
  638. Status = gBS->OpenProtocol (
  639. ParentDriverBuffer[HandleCount],
  640. &gEfiComponentName2ProtocolGuid,
  641. (VOID **)&CompName2,
  642. gImageHandle,
  643. NULL,
  644. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  645. );
  646. if (EFI_ERROR (Status)) {
  647. Status = gBS->OpenProtocol (
  648. ParentDriverBuffer[HandleCount],
  649. &gEfiComponentNameProtocolGuid,
  650. (VOID **)&CompName2,
  651. gImageHandle,
  652. NULL,
  653. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  654. );
  655. }
  656. if (EFI_ERROR (Status)) {
  657. continue;
  658. }
  659. Lang = GetBestLanguageForDriver (CompName2->SupportedLanguages, Language, FALSE);
  660. Status = CompName2->GetControllerName (CompName2, ParentControllerBuffer[LoopVar], DeviceHandle, Lang, &DeviceNameToReturn);
  661. FreePool (Lang);
  662. Lang = NULL;
  663. if (!EFI_ERROR (Status) && (DeviceNameToReturn != NULL)) {
  664. break;
  665. }
  666. }
  667. SHELL_FREE_NON_NULL (ParentDriverBuffer);
  668. if (!EFI_ERROR (Status) && (DeviceNameToReturn != NULL)) {
  669. break;
  670. }
  671. }
  672. }
  673. SHELL_FREE_NON_NULL (ParentControllerBuffer);
  674. }
  675. //
  676. // dont return on fail since we will try device path if that bit is on
  677. //
  678. if (DeviceNameToReturn != NULL) {
  679. ASSERT (BestDeviceName != NULL);
  680. StrnCatGrow (BestDeviceName, NULL, DeviceNameToReturn, 0);
  681. return (EFI_SUCCESS);
  682. }
  683. }
  684. if ((Flags & EFI_DEVICE_NAME_USE_DEVICE_PATH) != 0) {
  685. Status = gBS->OpenProtocol (
  686. DeviceHandle,
  687. &gEfiDevicePathProtocolGuid,
  688. (VOID **)&DevicePath,
  689. gImageHandle,
  690. NULL,
  691. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  692. );
  693. if (!EFI_ERROR (Status)) {
  694. //
  695. // use device path to text on the device path
  696. //
  697. *BestDeviceName = ConvertDevicePathToText (DevicePath, TRUE, TRUE);
  698. return (EFI_SUCCESS);
  699. }
  700. }
  701. //
  702. // none of the selected bits worked.
  703. //
  704. return (EFI_NOT_FOUND);
  705. }
  706. /**
  707. Opens the root directory of a device on a handle
  708. This function opens the root directory of a device and returns a file handle to it.
  709. @param DeviceHandle The handle of the device that contains the volume.
  710. @param FileHandle On exit, points to the file handle corresponding to the root directory on the
  711. device.
  712. @retval EFI_SUCCESS Root opened successfully.
  713. @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
  714. could not be opened.
  715. @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
  716. @retval EFI_DEVICE_ERROR The device had an error.
  717. @retval Others Error status returned from EFI_SIMPLE_FILE_SYSTEM_PROTOCOL->OpenVolume().
  718. **/
  719. EFI_STATUS
  720. EFIAPI
  721. EfiShellOpenRootByHandle (
  722. IN EFI_HANDLE DeviceHandle,
  723. OUT SHELL_FILE_HANDLE *FileHandle
  724. )
  725. {
  726. EFI_STATUS Status;
  727. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFileSystem;
  728. EFI_FILE_PROTOCOL *RealFileHandle;
  729. EFI_DEVICE_PATH_PROTOCOL *DevPath;
  730. //
  731. // get the simple file system interface
  732. //
  733. Status = gBS->OpenProtocol (
  734. DeviceHandle,
  735. &gEfiSimpleFileSystemProtocolGuid,
  736. (VOID **)&SimpleFileSystem,
  737. gImageHandle,
  738. NULL,
  739. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  740. );
  741. if (EFI_ERROR (Status)) {
  742. return (EFI_NOT_FOUND);
  743. }
  744. Status = gBS->OpenProtocol (
  745. DeviceHandle,
  746. &gEfiDevicePathProtocolGuid,
  747. (VOID **)&DevPath,
  748. gImageHandle,
  749. NULL,
  750. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  751. );
  752. if (EFI_ERROR (Status)) {
  753. return (EFI_NOT_FOUND);
  754. }
  755. //
  756. // Open the root volume now...
  757. //
  758. Status = SimpleFileSystem->OpenVolume (SimpleFileSystem, &RealFileHandle);
  759. if (EFI_ERROR (Status)) {
  760. return Status;
  761. }
  762. *FileHandle = ConvertEfiFileProtocolToShellHandle (RealFileHandle, EfiShellGetMapFromDevicePath (&DevPath));
  763. return (EFI_SUCCESS);
  764. }
  765. /**
  766. Opens the root directory of a device.
  767. This function opens the root directory of a device and returns a file handle to it.
  768. @param DevicePath Points to the device path corresponding to the device where the
  769. EFI_SIMPLE_FILE_SYSTEM_PROTOCOL is installed.
  770. @param FileHandle On exit, points to the file handle corresponding to the root directory on the
  771. device.
  772. @retval EFI_SUCCESS Root opened successfully.
  773. @retval EFI_NOT_FOUND EFI_SIMPLE_FILE_SYSTEM could not be found or the root directory
  774. could not be opened.
  775. @retval EFI_VOLUME_CORRUPTED The data structures in the volume were corrupted.
  776. @retval EFI_DEVICE_ERROR The device had an error
  777. @retval EFI_INVALID_PARAMETER FileHandle is NULL.
  778. **/
  779. EFI_STATUS
  780. EFIAPI
  781. EfiShellOpenRoot (
  782. IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  783. OUT SHELL_FILE_HANDLE *FileHandle
  784. )
  785. {
  786. EFI_STATUS Status;
  787. EFI_HANDLE Handle;
  788. if (FileHandle == NULL) {
  789. return (EFI_INVALID_PARAMETER);
  790. }
  791. //
  792. // find the handle of the device with that device handle and the file system
  793. //
  794. /// @todo BlockIo?
  795. Status = gBS->LocateDevicePath (
  796. &gEfiSimpleFileSystemProtocolGuid,
  797. &DevicePath,
  798. &Handle
  799. );
  800. if (EFI_ERROR (Status)) {
  801. return (EFI_NOT_FOUND);
  802. }
  803. return (EfiShellOpenRootByHandle (Handle, FileHandle));
  804. }
  805. /**
  806. Returns whether any script files are currently being processed.
  807. @retval TRUE There is at least one script file active.
  808. @retval FALSE No script files are active now.
  809. **/
  810. BOOLEAN
  811. EFIAPI
  812. EfiShellBatchIsActive (
  813. VOID
  814. )
  815. {
  816. if (ShellCommandGetCurrentScriptFile () == NULL) {
  817. return (FALSE);
  818. }
  819. return (TRUE);
  820. }
  821. /**
  822. Worker function to open a file based on a device path. this will open the root
  823. of the volume and then traverse down to the file itself.
  824. @param DevicePath Device Path of the file.
  825. @param FileHandle Pointer to the file upon a successful return.
  826. @param OpenMode mode to open file in.
  827. @param Attributes the File Attributes to use when creating a new file.
  828. @retval EFI_SUCCESS the file is open and FileHandle is valid
  829. @retval EFI_UNSUPPORTED the device path contained non-path elements
  830. @retval other an error occurred.
  831. **/
  832. EFI_STATUS
  833. InternalOpenFileDevicePath (
  834. IN OUT EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  835. OUT SHELL_FILE_HANDLE *FileHandle,
  836. IN UINT64 OpenMode,
  837. IN UINT64 Attributes OPTIONAL
  838. )
  839. {
  840. EFI_STATUS Status;
  841. FILEPATH_DEVICE_PATH *FilePathNode;
  842. EFI_HANDLE Handle;
  843. SHELL_FILE_HANDLE ShellHandle;
  844. EFI_FILE_PROTOCOL *Handle1;
  845. EFI_FILE_PROTOCOL *Handle2;
  846. FILEPATH_DEVICE_PATH *AlignedNode;
  847. if (FileHandle == NULL) {
  848. return (EFI_INVALID_PARAMETER);
  849. }
  850. *FileHandle = NULL;
  851. Handle1 = NULL;
  852. Handle2 = NULL;
  853. Handle = NULL;
  854. ShellHandle = NULL;
  855. FilePathNode = NULL;
  856. AlignedNode = NULL;
  857. Status = EfiShellOpenRoot (DevicePath, &ShellHandle);
  858. if (!EFI_ERROR (Status)) {
  859. Handle1 = ConvertShellHandleToEfiFileProtocol (ShellHandle);
  860. if (Handle1 != NULL) {
  861. //
  862. // chop off the beginning part before the file system part...
  863. //
  864. /// @todo BlockIo?
  865. Status = gBS->LocateDevicePath (
  866. &gEfiSimpleFileSystemProtocolGuid,
  867. &DevicePath,
  868. &Handle
  869. );
  870. if (!EFI_ERROR (Status)) {
  871. //
  872. // To access as a file system, the file path should only
  873. // contain file path components. Follow the file path nodes
  874. // and find the target file
  875. //
  876. for ( FilePathNode = (FILEPATH_DEVICE_PATH *)DevicePath
  877. ; !IsDevicePathEnd (&FilePathNode->Header)
  878. ; FilePathNode = (FILEPATH_DEVICE_PATH *)NextDevicePathNode (&FilePathNode->Header)
  879. )
  880. {
  881. SHELL_FREE_NON_NULL (AlignedNode);
  882. AlignedNode = AllocateCopyPool (DevicePathNodeLength (FilePathNode), FilePathNode);
  883. //
  884. // For file system access each node should be a file path component
  885. //
  886. if ((DevicePathType (&FilePathNode->Header) != MEDIA_DEVICE_PATH) ||
  887. (DevicePathSubType (&FilePathNode->Header) != MEDIA_FILEPATH_DP)
  888. )
  889. {
  890. Status = EFI_UNSUPPORTED;
  891. break;
  892. }
  893. //
  894. // Open this file path node
  895. //
  896. Handle2 = Handle1;
  897. Handle1 = NULL;
  898. //
  899. // if this is the last node in the DevicePath always create (if that was requested).
  900. //
  901. if (IsDevicePathEnd ((NextDevicePathNode (&FilePathNode->Header)))) {
  902. Status = Handle2->Open (
  903. Handle2,
  904. &Handle1,
  905. AlignedNode->PathName,
  906. OpenMode,
  907. Attributes
  908. );
  909. } else {
  910. //
  911. // This is not the last node and we dont want to 'create' existing
  912. // directory entries...
  913. //
  914. //
  915. // open without letting it create
  916. // prevents error on existing files/directories
  917. //
  918. Status = Handle2->Open (
  919. Handle2,
  920. &Handle1,
  921. AlignedNode->PathName,
  922. OpenMode &~EFI_FILE_MODE_CREATE,
  923. Attributes
  924. );
  925. //
  926. // if above failed now open and create the 'item'
  927. // if OpenMode EFI_FILE_MODE_CREATE bit was on (but disabled above)
  928. //
  929. if ((EFI_ERROR (Status)) && ((OpenMode & EFI_FILE_MODE_CREATE) != 0)) {
  930. Status = Handle2->Open (
  931. Handle2,
  932. &Handle1,
  933. AlignedNode->PathName,
  934. OpenMode,
  935. Attributes
  936. );
  937. }
  938. }
  939. //
  940. // Close the last node
  941. //
  942. ShellInfoObject.NewEfiShellProtocol->CloseFile (Handle2);
  943. //
  944. // If there's been an error, stop
  945. //
  946. if (EFI_ERROR (Status)) {
  947. break;
  948. }
  949. } // for loop
  950. }
  951. }
  952. }
  953. SHELL_FREE_NON_NULL (AlignedNode);
  954. if (EFI_ERROR (Status)) {
  955. if (Handle1 != NULL) {
  956. ShellInfoObject.NewEfiShellProtocol->CloseFile (Handle1);
  957. }
  958. } else {
  959. *FileHandle = ConvertEfiFileProtocolToShellHandle (Handle1, ShellFileHandleGetPath (ShellHandle));
  960. }
  961. return (Status);
  962. }
  963. /**
  964. Creates a file or directory by name.
  965. This function creates an empty new file or directory with the specified attributes and
  966. returns the new file's handle. If the file already exists and is read-only, then
  967. EFI_INVALID_PARAMETER will be returned.
  968. If the file already existed, it is truncated and its attributes updated. If the file is
  969. created successfully, the FileHandle is the file's handle, else, the FileHandle is NULL.
  970. If the file name begins with >v, then the file handle which is returned refers to the
  971. shell environment variable with the specified name. If the shell environment variable
  972. already exists and is non-volatile then EFI_INVALID_PARAMETER is returned.
  973. @param FileName Pointer to NULL-terminated file path
  974. @param FileAttribs The new file's attributes. the different attributes are
  975. described in EFI_FILE_PROTOCOL.Open().
  976. @param FileHandle On return, points to the created file handle or directory's handle
  977. @retval EFI_SUCCESS The file was opened. FileHandle points to the new file's handle.
  978. @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value.
  979. @retval EFI_UNSUPPORTED could not open the file path
  980. @retval EFI_NOT_FOUND the specified file could not be found on the device, or could not
  981. file the file system on the device.
  982. @retval EFI_NO_MEDIA the device has no medium.
  983. @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
  984. longer supported.
  985. @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
  986. the DirName.
  987. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  988. @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
  989. when the media is write-protected.
  990. @retval EFI_ACCESS_DENIED The service denied access to the file.
  991. @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file.
  992. @retval EFI_VOLUME_FULL The volume is full.
  993. **/
  994. EFI_STATUS
  995. EFIAPI
  996. EfiShellCreateFile (
  997. IN CONST CHAR16 *FileName,
  998. IN UINT64 FileAttribs,
  999. OUT SHELL_FILE_HANDLE *FileHandle
  1000. )
  1001. {
  1002. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  1003. EFI_STATUS Status;
  1004. BOOLEAN Volatile;
  1005. //
  1006. // Is this for an environment variable
  1007. // do we start with >v
  1008. //
  1009. if (StrStr (FileName, L">v") == FileName) {
  1010. Status = IsVolatileEnv (FileName + 2, &Volatile);
  1011. if (EFI_ERROR (Status)) {
  1012. return Status;
  1013. }
  1014. if (!Volatile) {
  1015. return (EFI_INVALID_PARAMETER);
  1016. }
  1017. *FileHandle = CreateFileInterfaceEnv (FileName+2);
  1018. return (EFI_SUCCESS);
  1019. }
  1020. //
  1021. // We are opening a regular file.
  1022. //
  1023. DevicePath = EfiShellGetDevicePathFromFilePath (FileName);
  1024. if (DevicePath == NULL) {
  1025. return (EFI_NOT_FOUND);
  1026. }
  1027. Status = InternalOpenFileDevicePath (DevicePath, FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, FileAttribs);
  1028. FreePool (DevicePath);
  1029. return (Status);
  1030. }
  1031. /**
  1032. Register a GUID and a localized human readable name for it.
  1033. If Guid is not assigned a name, then assign GuidName to Guid. This list of GUID
  1034. names must be used whenever a shell command outputs GUID information.
  1035. This function is only available when the major and minor versions in the
  1036. EfiShellProtocol are greater than or equal to 2 and 1, respectively.
  1037. @param[in] Guid A pointer to the GUID being registered.
  1038. @param[in] GuidName A pointer to the localized name for the GUID being registered.
  1039. @retval EFI_SUCCESS The operation was successful.
  1040. @retval EFI_INVALID_PARAMETER Guid was NULL.
  1041. @retval EFI_INVALID_PARAMETER GuidName was NULL.
  1042. @retval EFI_ACCESS_DENIED Guid already is assigned a name.
  1043. **/
  1044. EFI_STATUS
  1045. EFIAPI
  1046. EfiShellRegisterGuidName (
  1047. IN CONST EFI_GUID *Guid,
  1048. IN CONST CHAR16 *GuidName
  1049. )
  1050. {
  1051. return (AddNewGuidNameMapping (Guid, GuidName, NULL));
  1052. }
  1053. /**
  1054. Opens a file or a directory by file name.
  1055. This function opens the specified file in the specified OpenMode and returns a file
  1056. handle.
  1057. If the file name begins with >v, then the file handle which is returned refers to the
  1058. shell environment variable with the specified name. If the shell environment variable
  1059. exists, is non-volatile and the OpenMode indicates EFI_FILE_MODE_WRITE, then
  1060. EFI_INVALID_PARAMETER is returned.
  1061. If the file name is >i, then the file handle which is returned refers to the standard
  1062. input. If the OpenMode indicates EFI_FILE_MODE_WRITE, then EFI_INVALID_PARAMETER
  1063. is returned.
  1064. If the file name is >o, then the file handle which is returned refers to the standard
  1065. output. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
  1066. is returned.
  1067. If the file name is >e, then the file handle which is returned refers to the standard
  1068. error. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER
  1069. is returned.
  1070. If the file name is NUL, then the file handle that is returned refers to the standard NUL
  1071. file. If the OpenMode indicates EFI_FILE_MODE_READ, then EFI_INVALID_PARAMETER is
  1072. returned.
  1073. If return EFI_SUCCESS, the FileHandle is the opened file's handle, else, the
  1074. FileHandle is NULL.
  1075. @param FileName Points to the NULL-terminated UCS-2 encoded file name.
  1076. @param FileHandle On return, points to the file handle.
  1077. @param OpenMode File open mode. Either EFI_FILE_MODE_READ or
  1078. EFI_FILE_MODE_WRITE from section 12.4 of the UEFI
  1079. Specification.
  1080. @retval EFI_SUCCESS The file was opened. FileHandle has the opened file's handle.
  1081. @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value. FileHandle is NULL.
  1082. @retval EFI_UNSUPPORTED Could not open the file path. FileHandle is NULL.
  1083. @retval EFI_NOT_FOUND The specified file could not be found on the device or the file
  1084. system could not be found on the device. FileHandle is NULL.
  1085. @retval EFI_NO_MEDIA The device has no medium. FileHandle is NULL.
  1086. @retval EFI_MEDIA_CHANGED The device has a different medium in it or the medium is no
  1087. longer supported. FileHandle is NULL.
  1088. @retval EFI_DEVICE_ERROR The device reported an error or can't get the file path according
  1089. the FileName. FileHandle is NULL.
  1090. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted. FileHandle is NULL.
  1091. @retval EFI_WRITE_PROTECTED An attempt was made to create a file, or open a file for write
  1092. when the media is write-protected. FileHandle is NULL.
  1093. @retval EFI_ACCESS_DENIED The service denied access to the file. FileHandle is NULL.
  1094. @retval EFI_OUT_OF_RESOURCES Not enough resources were available to open the file. FileHandle
  1095. is NULL.
  1096. @retval EFI_VOLUME_FULL The volume is full. FileHandle is NULL.
  1097. **/
  1098. EFI_STATUS
  1099. EFIAPI
  1100. EfiShellOpenFileByName (
  1101. IN CONST CHAR16 *FileName,
  1102. OUT SHELL_FILE_HANDLE *FileHandle,
  1103. IN UINT64 OpenMode
  1104. )
  1105. {
  1106. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  1107. EFI_STATUS Status;
  1108. BOOLEAN Volatile;
  1109. *FileHandle = NULL;
  1110. //
  1111. // Is this for StdIn
  1112. //
  1113. if (StrCmp (FileName, L">i") == 0) {
  1114. //
  1115. // make sure not writing to StdIn
  1116. //
  1117. if ((OpenMode & EFI_FILE_MODE_WRITE) != 0) {
  1118. return (EFI_INVALID_PARAMETER);
  1119. }
  1120. *FileHandle = ShellInfoObject.NewShellParametersProtocol->StdIn;
  1121. ASSERT (*FileHandle != NULL);
  1122. return (EFI_SUCCESS);
  1123. }
  1124. //
  1125. // Is this for StdOut
  1126. //
  1127. if (StrCmp (FileName, L">o") == 0) {
  1128. //
  1129. // make sure not writing to StdIn
  1130. //
  1131. if ((OpenMode & EFI_FILE_MODE_READ) != 0) {
  1132. return (EFI_INVALID_PARAMETER);
  1133. }
  1134. *FileHandle = &FileInterfaceStdOut;
  1135. return (EFI_SUCCESS);
  1136. }
  1137. //
  1138. // Is this for NUL / NULL file
  1139. //
  1140. if ((gUnicodeCollation->StriColl (gUnicodeCollation, (CHAR16 *)FileName, L"NUL") == 0) ||
  1141. (gUnicodeCollation->StriColl (gUnicodeCollation, (CHAR16 *)FileName, L"NULL") == 0))
  1142. {
  1143. *FileHandle = &FileInterfaceNulFile;
  1144. return (EFI_SUCCESS);
  1145. }
  1146. //
  1147. // Is this for StdErr
  1148. //
  1149. if (StrCmp (FileName, L">e") == 0) {
  1150. //
  1151. // make sure not writing to StdIn
  1152. //
  1153. if ((OpenMode & EFI_FILE_MODE_READ) != 0) {
  1154. return (EFI_INVALID_PARAMETER);
  1155. }
  1156. *FileHandle = &FileInterfaceStdErr;
  1157. return (EFI_SUCCESS);
  1158. }
  1159. //
  1160. // Is this for an environment variable
  1161. // do we start with >v
  1162. //
  1163. if (StrStr (FileName, L">v") == FileName) {
  1164. Status = IsVolatileEnv (FileName + 2, &Volatile);
  1165. if (EFI_ERROR (Status)) {
  1166. return Status;
  1167. }
  1168. if (!Volatile &&
  1169. ((OpenMode & EFI_FILE_MODE_WRITE) != 0))
  1170. {
  1171. return (EFI_INVALID_PARAMETER);
  1172. }
  1173. *FileHandle = CreateFileInterfaceEnv (FileName+2);
  1174. return (EFI_SUCCESS);
  1175. }
  1176. //
  1177. // We are opening a regular file.
  1178. //
  1179. DevicePath = EfiShellGetDevicePathFromFilePath (FileName);
  1180. if (DevicePath == NULL) {
  1181. return (EFI_NOT_FOUND);
  1182. }
  1183. //
  1184. // Copy the device path, open the file, then free the memory
  1185. //
  1186. Status = InternalOpenFileDevicePath (DevicePath, FileHandle, OpenMode, 0); // 0 = no specific file attributes
  1187. FreePool (DevicePath);
  1188. return (Status);
  1189. }
  1190. /**
  1191. Deletes the file specified by the file name.
  1192. This function deletes a file.
  1193. @param FileName Points to the NULL-terminated file name.
  1194. @retval EFI_SUCCESS The file was closed and deleted, and the handle was closed.
  1195. @retval EFI_WARN_DELETE_FAILURE The handle was closed but the file was not deleted.
  1196. @sa EfiShellCreateFile
  1197. **/
  1198. EFI_STATUS
  1199. EFIAPI
  1200. EfiShellDeleteFileByName (
  1201. IN CONST CHAR16 *FileName
  1202. )
  1203. {
  1204. SHELL_FILE_HANDLE FileHandle;
  1205. EFI_STATUS Status;
  1206. FileHandle = NULL;
  1207. //
  1208. // get a handle to the file
  1209. //
  1210. Status = EfiShellCreateFile (
  1211. FileName,
  1212. 0,
  1213. &FileHandle
  1214. );
  1215. if (EFI_ERROR (Status)) {
  1216. return (Status);
  1217. }
  1218. //
  1219. // now delete the file
  1220. //
  1221. ShellFileHandleRemove (FileHandle);
  1222. return (ShellInfoObject.NewEfiShellProtocol->DeleteFile (FileHandle));
  1223. }
  1224. /**
  1225. Disables the page break output mode.
  1226. **/
  1227. VOID
  1228. EFIAPI
  1229. EfiShellDisablePageBreak (
  1230. VOID
  1231. )
  1232. {
  1233. ShellInfoObject.PageBreakEnabled = FALSE;
  1234. }
  1235. /**
  1236. Enables the page break output mode.
  1237. **/
  1238. VOID
  1239. EFIAPI
  1240. EfiShellEnablePageBreak (
  1241. VOID
  1242. )
  1243. {
  1244. ShellInfoObject.PageBreakEnabled = TRUE;
  1245. }
  1246. /**
  1247. internal worker function to load and run an image via device path.
  1248. @param ParentImageHandle A handle of the image that is executing the specified
  1249. command line.
  1250. @param DevicePath device path of the file to execute
  1251. @param CommandLine Points to the NULL-terminated UCS-2 encoded string
  1252. containing the command line. If NULL then the command-
  1253. line will be empty.
  1254. @param Environment Points to a NULL-terminated array of environment
  1255. variables with the format 'x=y', where x is the
  1256. environment variable name and y is the value. If this
  1257. is NULL, then the current shell environment is used.
  1258. @param[out] StartImageStatus Returned status from gBS->StartImage.
  1259. @retval EFI_SUCCESS The command executed successfully. The status code
  1260. returned by the command is pointed to by StatusCode.
  1261. @retval EFI_INVALID_PARAMETER The parameters are invalid.
  1262. @retval EFI_OUT_OF_RESOURCES Out of resources.
  1263. @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
  1264. **/
  1265. EFI_STATUS
  1266. InternalShellExecuteDevicePath (
  1267. IN CONST EFI_HANDLE *ParentImageHandle,
  1268. IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,
  1269. IN CONST CHAR16 *CommandLine OPTIONAL,
  1270. IN CONST CHAR16 **Environment OPTIONAL,
  1271. OUT EFI_STATUS *StartImageStatus OPTIONAL
  1272. )
  1273. {
  1274. EFI_STATUS Status;
  1275. EFI_STATUS StartStatus;
  1276. EFI_STATUS CleanupStatus;
  1277. EFI_HANDLE NewHandle;
  1278. EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
  1279. LIST_ENTRY OrigEnvs;
  1280. EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol;
  1281. CHAR16 *ImagePath;
  1282. UINTN Index;
  1283. CHAR16 *Walker;
  1284. CHAR16 *NewCmdLine;
  1285. if (ParentImageHandle == NULL) {
  1286. return (EFI_INVALID_PARAMETER);
  1287. }
  1288. InitializeListHead (&OrigEnvs);
  1289. ZeroMem (&ShellParamsProtocol, sizeof (EFI_SHELL_PARAMETERS_PROTOCOL));
  1290. NewHandle = NULL;
  1291. NewCmdLine = AllocateCopyPool (StrSize (CommandLine), CommandLine);
  1292. if (NewCmdLine == NULL) {
  1293. return EFI_OUT_OF_RESOURCES;
  1294. }
  1295. for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL; Walker++) {
  1296. if ((*Walker == L'^') && (*(Walker+1) == L'#')) {
  1297. CopyMem (Walker, Walker+1, StrSize (Walker) - sizeof (Walker[0]));
  1298. }
  1299. }
  1300. //
  1301. // Load the image with:
  1302. // FALSE - not from boot manager and NULL, 0 being not already in memory
  1303. //
  1304. Status = gBS->LoadImage (
  1305. FALSE,
  1306. *ParentImageHandle,
  1307. (EFI_DEVICE_PATH_PROTOCOL *)DevicePath,
  1308. NULL,
  1309. 0,
  1310. &NewHandle
  1311. );
  1312. if (EFI_ERROR (Status)) {
  1313. if (NewHandle != NULL) {
  1314. gBS->UnloadImage (NewHandle);
  1315. }
  1316. FreePool (NewCmdLine);
  1317. return (Status);
  1318. }
  1319. Status = gBS->OpenProtocol (
  1320. NewHandle,
  1321. &gEfiLoadedImageProtocolGuid,
  1322. (VOID **)&LoadedImage,
  1323. gImageHandle,
  1324. NULL,
  1325. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  1326. );
  1327. if (!EFI_ERROR (Status)) {
  1328. //
  1329. // If the image is not an app abort it.
  1330. //
  1331. if (LoadedImage->ImageCodeType != EfiLoaderCode) {
  1332. ShellPrintHiiEx (
  1333. -1,
  1334. -1,
  1335. NULL,
  1336. STRING_TOKEN (STR_SHELL_IMAGE_NOT_APP),
  1337. ShellInfoObject.HiiHandle
  1338. );
  1339. goto UnloadImage;
  1340. }
  1341. ASSERT (LoadedImage->LoadOptionsSize == 0);
  1342. if (NewCmdLine != NULL) {
  1343. LoadedImage->LoadOptionsSize = (UINT32)StrSize (NewCmdLine);
  1344. LoadedImage->LoadOptions = (VOID *)NewCmdLine;
  1345. }
  1346. //
  1347. // Save our current environment settings for later restoration if necessary
  1348. //
  1349. if (Environment != NULL) {
  1350. Status = GetEnvironmentVariableList (&OrigEnvs);
  1351. if (!EFI_ERROR (Status)) {
  1352. Status = SetEnvironmentVariables (Environment);
  1353. }
  1354. }
  1355. //
  1356. // Initialize and install a shell parameters protocol on the image.
  1357. //
  1358. ShellParamsProtocol.StdIn = ShellInfoObject.NewShellParametersProtocol->StdIn;
  1359. ShellParamsProtocol.StdOut = ShellInfoObject.NewShellParametersProtocol->StdOut;
  1360. ShellParamsProtocol.StdErr = ShellInfoObject.NewShellParametersProtocol->StdErr;
  1361. Status = UpdateArgcArgv (&ShellParamsProtocol, NewCmdLine, Efi_Application, NULL, NULL);
  1362. if (EFI_ERROR (Status)) {
  1363. goto UnloadImage;
  1364. }
  1365. //
  1366. // Replace Argv[0] with the full path of the binary we're executing:
  1367. // If the command line was "foo", the binary might be called "foo.efi".
  1368. // "The first entry in [Argv] is always the full file path of the
  1369. // executable" - UEFI Shell Spec section 2.3
  1370. //
  1371. ImagePath = EfiShellGetFilePathFromDevicePath (DevicePath);
  1372. // The image we're executing isn't necessarily in a filesystem - it might
  1373. // be memory mapped. In this case EfiShellGetFilePathFromDevicePath will
  1374. // return NULL, and we'll leave Argv[0] as UpdateArgcArgv set it.
  1375. if (ImagePath != NULL) {
  1376. if (ShellParamsProtocol.Argv == NULL) {
  1377. // Command line was empty or null.
  1378. // (UpdateArgcArgv sets Argv to NULL when CommandLine is "" or NULL)
  1379. ShellParamsProtocol.Argv = AllocatePool (sizeof (CHAR16 *));
  1380. if (ShellParamsProtocol.Argv == NULL) {
  1381. Status = EFI_OUT_OF_RESOURCES;
  1382. goto UnloadImage;
  1383. }
  1384. ShellParamsProtocol.Argc = 1;
  1385. } else {
  1386. // Free the string UpdateArgcArgv put in Argv[0];
  1387. FreePool (ShellParamsProtocol.Argv[0]);
  1388. }
  1389. ShellParamsProtocol.Argv[0] = ImagePath;
  1390. }
  1391. Status = gBS->InstallProtocolInterface (&NewHandle, &gEfiShellParametersProtocolGuid, EFI_NATIVE_INTERFACE, &ShellParamsProtocol);
  1392. ASSERT_EFI_ERROR (Status);
  1393. /// @todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)
  1394. //
  1395. // now start the image and if the caller wanted the return code pass it to them...
  1396. //
  1397. if (!EFI_ERROR (Status)) {
  1398. StartStatus = gBS->StartImage (
  1399. NewHandle,
  1400. 0,
  1401. NULL
  1402. );
  1403. if (StartImageStatus != NULL) {
  1404. *StartImageStatus = StartStatus;
  1405. }
  1406. CleanupStatus = gBS->UninstallProtocolInterface (
  1407. NewHandle,
  1408. &gEfiShellParametersProtocolGuid,
  1409. &ShellParamsProtocol
  1410. );
  1411. ASSERT_EFI_ERROR (CleanupStatus);
  1412. goto FreeAlloc;
  1413. }
  1414. UnloadImage:
  1415. // Unload image - We should only get here if we didn't call StartImage
  1416. gBS->UnloadImage (NewHandle);
  1417. FreeAlloc:
  1418. // Free Argv (Allocated in UpdateArgcArgv)
  1419. if (ShellParamsProtocol.Argv != NULL) {
  1420. for (Index = 0; Index < ShellParamsProtocol.Argc; Index++) {
  1421. if (ShellParamsProtocol.Argv[Index] != NULL) {
  1422. FreePool (ShellParamsProtocol.Argv[Index]);
  1423. }
  1424. }
  1425. FreePool (ShellParamsProtocol.Argv);
  1426. }
  1427. }
  1428. // Restore environment variables
  1429. if (!IsListEmpty (&OrigEnvs)) {
  1430. CleanupStatus = SetEnvironmentVariableList (&OrigEnvs);
  1431. ASSERT_EFI_ERROR (CleanupStatus);
  1432. }
  1433. FreePool (NewCmdLine);
  1434. return (Status);
  1435. }
  1436. /**
  1437. internal worker function to load and run an image in the current shell.
  1438. @param CommandLine Points to the NULL-terminated UCS-2 encoded string
  1439. containing the command line. If NULL then the command-
  1440. line will be empty.
  1441. @param Environment Points to a NULL-terminated array of environment
  1442. variables with the format 'x=y', where x is the
  1443. environment variable name and y is the value. If this
  1444. is NULL, then the current shell environment is used.
  1445. @param[out] StartImageStatus Returned status from the command line.
  1446. @retval EFI_SUCCESS The command executed successfully. The status code
  1447. returned by the command is pointed to by StatusCode.
  1448. @retval EFI_INVALID_PARAMETER The parameters are invalid.
  1449. @retval EFI_OUT_OF_RESOURCES Out of resources.
  1450. @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
  1451. **/
  1452. EFI_STATUS
  1453. InternalShellExecute (
  1454. IN CONST CHAR16 *CommandLine OPTIONAL,
  1455. IN CONST CHAR16 **Environment OPTIONAL,
  1456. OUT EFI_STATUS *StartImageStatus OPTIONAL
  1457. )
  1458. {
  1459. EFI_STATUS Status;
  1460. EFI_STATUS CleanupStatus;
  1461. LIST_ENTRY OrigEnvs;
  1462. InitializeListHead (&OrigEnvs);
  1463. //
  1464. // Save our current environment settings for later restoration if necessary
  1465. //
  1466. if (Environment != NULL) {
  1467. Status = GetEnvironmentVariableList (&OrigEnvs);
  1468. if (!EFI_ERROR (Status)) {
  1469. Status = SetEnvironmentVariables (Environment);
  1470. } else {
  1471. return Status;
  1472. }
  1473. }
  1474. Status = RunShellCommand (CommandLine, StartImageStatus);
  1475. // Restore environment variables
  1476. if (!IsListEmpty (&OrigEnvs)) {
  1477. CleanupStatus = SetEnvironmentVariableList (&OrigEnvs);
  1478. ASSERT_EFI_ERROR (CleanupStatus);
  1479. }
  1480. return (Status);
  1481. }
  1482. /**
  1483. Determine if the UEFI Shell is currently running with nesting enabled or disabled.
  1484. @retval FALSE nesting is required
  1485. @retval other nesting is enabled
  1486. **/
  1487. STATIC
  1488. BOOLEAN
  1489. NestingEnabled (
  1490. VOID
  1491. )
  1492. {
  1493. EFI_STATUS Status;
  1494. CHAR16 *Temp;
  1495. CHAR16 *Temp2;
  1496. UINTN TempSize;
  1497. BOOLEAN RetVal;
  1498. RetVal = TRUE;
  1499. Temp = NULL;
  1500. Temp2 = NULL;
  1501. if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest) {
  1502. TempSize = 0;
  1503. Temp = NULL;
  1504. Status = SHELL_GET_ENVIRONMENT_VARIABLE (mNoNestingEnvVarName, &TempSize, Temp);
  1505. if (Status == EFI_BUFFER_TOO_SMALL) {
  1506. Temp = AllocateZeroPool (TempSize + sizeof (CHAR16));
  1507. if (Temp != NULL) {
  1508. Status = SHELL_GET_ENVIRONMENT_VARIABLE (mNoNestingEnvVarName, &TempSize, Temp);
  1509. }
  1510. }
  1511. Temp2 = StrnCatGrow (&Temp2, NULL, mNoNestingTrue, 0);
  1512. if ((Temp != NULL) && (Temp2 != NULL) && (StringNoCaseCompare (&Temp, &Temp2) == 0)) {
  1513. //
  1514. // Use the no nesting method.
  1515. //
  1516. RetVal = FALSE;
  1517. }
  1518. }
  1519. SHELL_FREE_NON_NULL (Temp);
  1520. SHELL_FREE_NON_NULL (Temp2);
  1521. return (RetVal);
  1522. }
  1523. /**
  1524. Execute the command line.
  1525. This function creates a nested instance of the shell and executes the specified
  1526. command (CommandLine) with the specified environment (Environment). Upon return,
  1527. the status code returned by the specified command is placed in StatusCode.
  1528. If Environment is NULL, then the current environment is used and all changes made
  1529. by the commands executed will be reflected in the current environment. If the
  1530. Environment is non-NULL, then the changes made will be discarded.
  1531. The CommandLine is executed from the current working directory on the current
  1532. device.
  1533. @param ParentImageHandle A handle of the image that is executing the specified
  1534. command line.
  1535. @param CommandLine Points to the NULL-terminated UCS-2 encoded string
  1536. containing the command line. If NULL then the command-
  1537. line will be empty.
  1538. @param Environment Points to a NULL-terminated array of environment
  1539. variables with the format 'x=y', where x is the
  1540. environment variable name and y is the value. If this
  1541. is NULL, then the current shell environment is used.
  1542. @param StatusCode Points to the status code returned by the CommandLine.
  1543. @retval EFI_SUCCESS The command executed successfully. The status code
  1544. returned by the command is pointed to by StatusCode.
  1545. @retval EFI_INVALID_PARAMETER The parameters are invalid.
  1546. @retval EFI_OUT_OF_RESOURCES Out of resources.
  1547. @retval EFI_UNSUPPORTED Nested shell invocations are not allowed.
  1548. @retval EFI_UNSUPPORTED The support level required for this function is not present.
  1549. @sa InternalShellExecuteDevicePath
  1550. **/
  1551. EFI_STATUS
  1552. EFIAPI
  1553. EfiShellExecute (
  1554. IN EFI_HANDLE *ParentImageHandle,
  1555. IN CHAR16 *CommandLine OPTIONAL,
  1556. IN CHAR16 **Environment OPTIONAL,
  1557. OUT EFI_STATUS *StatusCode OPTIONAL
  1558. )
  1559. {
  1560. EFI_STATUS Status;
  1561. CHAR16 *Temp;
  1562. EFI_DEVICE_PATH_PROTOCOL *DevPath;
  1563. UINTN Size;
  1564. if ((PcdGet8 (PcdShellSupportLevel) < 1)) {
  1565. return (EFI_UNSUPPORTED);
  1566. }
  1567. if (NestingEnabled ()) {
  1568. DevPath = AppendDevicePath (ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);
  1569. DEBUG_CODE_BEGIN ();
  1570. Temp = ConvertDevicePathToText (ShellInfoObject.FileDevPath, TRUE, TRUE);
  1571. FreePool (Temp);
  1572. Temp = ConvertDevicePathToText (ShellInfoObject.ImageDevPath, TRUE, TRUE);
  1573. FreePool (Temp);
  1574. Temp = ConvertDevicePathToText (DevPath, TRUE, TRUE);
  1575. FreePool (Temp);
  1576. DEBUG_CODE_END ();
  1577. Temp = NULL;
  1578. Size = 0;
  1579. ASSERT ((Temp == NULL && Size == 0) || (Temp != NULL));
  1580. StrnCatGrow (&Temp, &Size, L"Shell.efi -exit ", 0);
  1581. StrnCatGrow (&Temp, &Size, CommandLine, 0);
  1582. Status = InternalShellExecuteDevicePath (
  1583. ParentImageHandle,
  1584. DevPath,
  1585. Temp,
  1586. (CONST CHAR16 **)Environment,
  1587. StatusCode
  1588. );
  1589. //
  1590. // de-allocate and return
  1591. //
  1592. FreePool (DevPath);
  1593. FreePool (Temp);
  1594. } else {
  1595. Status = InternalShellExecute (
  1596. (CONST CHAR16 *)CommandLine,
  1597. (CONST CHAR16 **)Environment,
  1598. StatusCode
  1599. );
  1600. }
  1601. return (Status);
  1602. }
  1603. /**
  1604. Utility cleanup function for EFI_SHELL_FILE_INFO objects.
  1605. 1) frees all pointers (non-NULL)
  1606. 2) Closes the SHELL_FILE_HANDLE
  1607. @param FileListNode pointer to the list node to free
  1608. **/
  1609. VOID
  1610. InternalFreeShellFileInfoNode (
  1611. IN EFI_SHELL_FILE_INFO *FileListNode
  1612. )
  1613. {
  1614. if (FileListNode->Info != NULL) {
  1615. FreePool ((VOID *)FileListNode->Info);
  1616. }
  1617. if (FileListNode->FileName != NULL) {
  1618. FreePool ((VOID *)FileListNode->FileName);
  1619. }
  1620. if (FileListNode->FullName != NULL) {
  1621. FreePool ((VOID *)FileListNode->FullName);
  1622. }
  1623. if (FileListNode->Handle != NULL) {
  1624. ShellInfoObject.NewEfiShellProtocol->CloseFile (FileListNode->Handle);
  1625. }
  1626. FreePool (FileListNode);
  1627. }
  1628. /**
  1629. Frees the file list.
  1630. This function cleans up the file list and any related data structures. It has no
  1631. impact on the files themselves.
  1632. @param FileList The file list to free. Type EFI_SHELL_FILE_INFO is
  1633. defined in OpenFileList()
  1634. @retval EFI_SUCCESS Free the file list successfully.
  1635. @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
  1636. **/
  1637. EFI_STATUS
  1638. EFIAPI
  1639. EfiShellFreeFileList (
  1640. IN EFI_SHELL_FILE_INFO **FileList
  1641. )
  1642. {
  1643. EFI_SHELL_FILE_INFO *ShellFileListItem;
  1644. if ((FileList == NULL) || (*FileList == NULL)) {
  1645. return (EFI_INVALID_PARAMETER);
  1646. }
  1647. for ( ShellFileListItem = (EFI_SHELL_FILE_INFO *)GetFirstNode (&(*FileList)->Link)
  1648. ; !IsListEmpty (&(*FileList)->Link)
  1649. ; ShellFileListItem = (EFI_SHELL_FILE_INFO *)GetFirstNode (&(*FileList)->Link)
  1650. )
  1651. {
  1652. RemoveEntryList (&ShellFileListItem->Link);
  1653. InternalFreeShellFileInfoNode (ShellFileListItem);
  1654. }
  1655. InternalFreeShellFileInfoNode (*FileList);
  1656. *FileList = NULL;
  1657. return (EFI_SUCCESS);
  1658. }
  1659. /**
  1660. Deletes the duplicate file names files in the given file list.
  1661. This function deletes the reduplicate files in the given file list.
  1662. @param FileList A pointer to the first entry in the file list.
  1663. @retval EFI_SUCCESS Always success.
  1664. @retval EFI_INVALID_PARAMETER FileList was NULL or *FileList was NULL;
  1665. **/
  1666. EFI_STATUS
  1667. EFIAPI
  1668. EfiShellRemoveDupInFileList (
  1669. IN EFI_SHELL_FILE_INFO **FileList
  1670. )
  1671. {
  1672. EFI_STATUS Status;
  1673. EFI_SHELL_FILE_INFO *Duplicates;
  1674. EFI_SHELL_FILE_INFO *ShellFileListItem;
  1675. EFI_SHELL_FILE_INFO *ShellFileListItem2;
  1676. EFI_SHELL_FILE_INFO *TempNode;
  1677. if ((FileList == NULL) || (*FileList == NULL)) {
  1678. return (EFI_INVALID_PARAMETER);
  1679. }
  1680. Status = ShellSortFileList (
  1681. FileList,
  1682. &Duplicates,
  1683. ShellSortFileListByFullName
  1684. );
  1685. if (!EFI_ERROR (Status)) {
  1686. EfiShellFreeFileList (&Duplicates);
  1687. return EFI_SUCCESS;
  1688. }
  1689. //
  1690. // Fall back to the slow method that needs no extra memory, and so cannot
  1691. // fail.
  1692. //
  1693. for ( ShellFileListItem = (EFI_SHELL_FILE_INFO *)GetFirstNode (&(*FileList)->Link)
  1694. ; !IsNull (&(*FileList)->Link, &ShellFileListItem->Link)
  1695. ; ShellFileListItem = (EFI_SHELL_FILE_INFO *)GetNextNode (&(*FileList)->Link, &ShellFileListItem->Link)
  1696. )
  1697. {
  1698. for ( ShellFileListItem2 = (EFI_SHELL_FILE_INFO *)GetNextNode (&(*FileList)->Link, &ShellFileListItem->Link)
  1699. ; !IsNull (&(*FileList)->Link, &ShellFileListItem2->Link)
  1700. ; ShellFileListItem2 = (EFI_SHELL_FILE_INFO *)GetNextNode (&(*FileList)->Link, &ShellFileListItem2->Link)
  1701. )
  1702. {
  1703. if (gUnicodeCollation->StriColl (
  1704. gUnicodeCollation,
  1705. (CHAR16 *)ShellFileListItem->FullName,
  1706. (CHAR16 *)ShellFileListItem2->FullName
  1707. ) == 0
  1708. )
  1709. {
  1710. TempNode = (EFI_SHELL_FILE_INFO *)GetPreviousNode (
  1711. &(*FileList)->Link,
  1712. &ShellFileListItem2->Link
  1713. );
  1714. RemoveEntryList (&ShellFileListItem2->Link);
  1715. InternalFreeShellFileInfoNode (ShellFileListItem2);
  1716. // Set ShellFileListItem2 to PreviousNode so we don't access Freed
  1717. // memory in GetNextNode in the loop expression above.
  1718. ShellFileListItem2 = TempNode;
  1719. }
  1720. }
  1721. }
  1722. return (EFI_SUCCESS);
  1723. }
  1724. //
  1725. // This is the same structure as the external version, but it has no CONST qualifiers.
  1726. //
  1727. typedef struct {
  1728. LIST_ENTRY Link; ///< Linked list members.
  1729. EFI_STATUS Status; ///< Status of opening the file. Valid only if Handle != NULL.
  1730. CHAR16 *FullName; ///< Fully qualified filename.
  1731. CHAR16 *FileName; ///< name of this file.
  1732. SHELL_FILE_HANDLE Handle; ///< Handle for interacting with the opened file or NULL if closed.
  1733. EFI_FILE_INFO *Info; ///< Pointer to the FileInfo struct for this file or NULL.
  1734. } EFI_SHELL_FILE_INFO_NO_CONST;
  1735. /**
  1736. Allocates and duplicates a EFI_SHELL_FILE_INFO node.
  1737. @param[in] Node The node to copy from.
  1738. @param[in] Save TRUE to set Node->Handle to NULL, FALSE otherwise.
  1739. @retval NULL a memory allocation error occurred
  1740. @return != NULL a pointer to the new node
  1741. **/
  1742. EFI_SHELL_FILE_INFO *
  1743. InternalDuplicateShellFileInfo (
  1744. IN EFI_SHELL_FILE_INFO *Node,
  1745. IN BOOLEAN Save
  1746. )
  1747. {
  1748. EFI_SHELL_FILE_INFO_NO_CONST *NewNode;
  1749. //
  1750. // try to confirm that the objects are in sync
  1751. //
  1752. ASSERT (sizeof (EFI_SHELL_FILE_INFO_NO_CONST) == sizeof (EFI_SHELL_FILE_INFO));
  1753. NewNode = AllocateZeroPool (sizeof (EFI_SHELL_FILE_INFO));
  1754. if (NewNode == NULL) {
  1755. return (NULL);
  1756. }
  1757. NewNode->FullName = AllocateCopyPool (StrSize (Node->FullName), Node->FullName);
  1758. NewNode->FileName = AllocateCopyPool (StrSize (Node->FileName), Node->FileName);
  1759. NewNode->Info = AllocateCopyPool ((UINTN)Node->Info->Size, Node->Info);
  1760. if ( (NewNode->FullName == NULL)
  1761. || (NewNode->FileName == NULL)
  1762. || (NewNode->Info == NULL)
  1763. )
  1764. {
  1765. SHELL_FREE_NON_NULL (NewNode->FullName);
  1766. SHELL_FREE_NON_NULL (NewNode->FileName);
  1767. SHELL_FREE_NON_NULL (NewNode->Info);
  1768. SHELL_FREE_NON_NULL (NewNode);
  1769. return (NULL);
  1770. }
  1771. NewNode->Status = Node->Status;
  1772. NewNode->Handle = Node->Handle;
  1773. if (!Save) {
  1774. Node->Handle = NULL;
  1775. }
  1776. return ((EFI_SHELL_FILE_INFO *)NewNode);
  1777. }
  1778. /**
  1779. Allocates and populates a EFI_SHELL_FILE_INFO structure. if any memory operation
  1780. failed it will return NULL.
  1781. @param[in] BasePath the Path to prepend onto filename for FullPath
  1782. @param[in] Status Status member initial value.
  1783. @param[in] FileName FileName member initial value.
  1784. @param[in] Handle Handle member initial value.
  1785. @param[in] Info Info struct to copy.
  1786. @retval NULL An error occurred.
  1787. @return a pointer to the newly allocated structure.
  1788. **/
  1789. EFI_SHELL_FILE_INFO *
  1790. CreateAndPopulateShellFileInfo (
  1791. IN CONST CHAR16 *BasePath,
  1792. IN CONST EFI_STATUS Status,
  1793. IN CONST CHAR16 *FileName,
  1794. IN CONST SHELL_FILE_HANDLE Handle,
  1795. IN CONST EFI_FILE_INFO *Info
  1796. )
  1797. {
  1798. EFI_SHELL_FILE_INFO *ShellFileListItem;
  1799. CHAR16 *TempString;
  1800. UINTN Size;
  1801. TempString = NULL;
  1802. Size = 0;
  1803. ShellFileListItem = AllocateZeroPool (sizeof (EFI_SHELL_FILE_INFO));
  1804. if (ShellFileListItem == NULL) {
  1805. return (NULL);
  1806. }
  1807. if ((Info != NULL) && (Info->Size != 0)) {
  1808. ShellFileListItem->Info = AllocateZeroPool ((UINTN)Info->Size);
  1809. if (ShellFileListItem->Info == NULL) {
  1810. FreePool (ShellFileListItem);
  1811. return (NULL);
  1812. }
  1813. CopyMem (ShellFileListItem->Info, Info, (UINTN)Info->Size);
  1814. } else {
  1815. ShellFileListItem->Info = NULL;
  1816. }
  1817. if (FileName != NULL) {
  1818. ASSERT (TempString == NULL);
  1819. ShellFileListItem->FileName = StrnCatGrow (&TempString, 0, FileName, 0);
  1820. if (ShellFileListItem->FileName == NULL) {
  1821. FreePool (ShellFileListItem->Info);
  1822. FreePool (ShellFileListItem);
  1823. return (NULL);
  1824. }
  1825. } else {
  1826. ShellFileListItem->FileName = NULL;
  1827. }
  1828. Size = 0;
  1829. TempString = NULL;
  1830. if (BasePath != NULL) {
  1831. ASSERT ((TempString == NULL && Size == 0) || (TempString != NULL));
  1832. TempString = StrnCatGrow (&TempString, &Size, BasePath, 0);
  1833. if (TempString == NULL) {
  1834. FreePool ((VOID *)ShellFileListItem->FileName);
  1835. SHELL_FREE_NON_NULL (ShellFileListItem->Info);
  1836. FreePool (ShellFileListItem);
  1837. return (NULL);
  1838. }
  1839. }
  1840. if (ShellFileListItem->FileName != NULL) {
  1841. ASSERT ((TempString == NULL && Size == 0) || (TempString != NULL));
  1842. TempString = StrnCatGrow (&TempString, &Size, ShellFileListItem->FileName, 0);
  1843. if (TempString == NULL) {
  1844. FreePool ((VOID *)ShellFileListItem->FileName);
  1845. FreePool (ShellFileListItem->Info);
  1846. FreePool (ShellFileListItem);
  1847. return (NULL);
  1848. }
  1849. }
  1850. TempString = PathCleanUpDirectories (TempString);
  1851. ShellFileListItem->FullName = TempString;
  1852. ShellFileListItem->Status = Status;
  1853. ShellFileListItem->Handle = Handle;
  1854. return (ShellFileListItem);
  1855. }
  1856. /**
  1857. Find all files in a specified directory.
  1858. @param FileDirHandle Handle of the directory to search.
  1859. @param FileList On return, points to the list of files in the directory
  1860. or NULL if there are no files in the directory.
  1861. @retval EFI_SUCCESS File information was returned successfully.
  1862. @retval EFI_VOLUME_CORRUPTED The file system structures have been corrupted.
  1863. @retval EFI_DEVICE_ERROR The device reported an error.
  1864. @retval EFI_NO_MEDIA The device media is not present.
  1865. @retval EFI_INVALID_PARAMETER The FileDirHandle was not a directory.
  1866. @return An error from FileHandleGetFileName().
  1867. **/
  1868. EFI_STATUS
  1869. EFIAPI
  1870. EfiShellFindFilesInDir (
  1871. IN SHELL_FILE_HANDLE FileDirHandle,
  1872. OUT EFI_SHELL_FILE_INFO **FileList
  1873. )
  1874. {
  1875. EFI_SHELL_FILE_INFO *ShellFileList;
  1876. EFI_SHELL_FILE_INFO *ShellFileListItem;
  1877. EFI_FILE_INFO *FileInfo;
  1878. EFI_STATUS Status;
  1879. BOOLEAN NoFile;
  1880. CHAR16 *TempString;
  1881. CHAR16 *BasePath;
  1882. UINTN Size;
  1883. CHAR16 *TempSpot;
  1884. BasePath = NULL;
  1885. Status = FileHandleGetFileName (FileDirHandle, &BasePath);
  1886. if (EFI_ERROR (Status)) {
  1887. return (Status);
  1888. }
  1889. if (ShellFileHandleGetPath (FileDirHandle) != NULL) {
  1890. TempString = NULL;
  1891. Size = 0;
  1892. TempString = StrnCatGrow (&TempString, &Size, ShellFileHandleGetPath (FileDirHandle), 0);
  1893. if (TempString == NULL) {
  1894. SHELL_FREE_NON_NULL (BasePath);
  1895. return (EFI_OUT_OF_RESOURCES);
  1896. }
  1897. TempSpot = StrStr (TempString, L";");
  1898. if (TempSpot != NULL) {
  1899. *TempSpot = CHAR_NULL;
  1900. }
  1901. TempString = StrnCatGrow (&TempString, &Size, BasePath, 0);
  1902. if (TempString == NULL) {
  1903. SHELL_FREE_NON_NULL (BasePath);
  1904. return (EFI_OUT_OF_RESOURCES);
  1905. }
  1906. SHELL_FREE_NON_NULL (BasePath);
  1907. BasePath = TempString;
  1908. }
  1909. NoFile = FALSE;
  1910. ShellFileList = NULL;
  1911. ShellFileListItem = NULL;
  1912. FileInfo = NULL;
  1913. Status = EFI_SUCCESS;
  1914. for ( Status = FileHandleFindFirstFile (FileDirHandle, &FileInfo)
  1915. ; !EFI_ERROR (Status) && !NoFile
  1916. ; Status = FileHandleFindNextFile (FileDirHandle, FileInfo, &NoFile)
  1917. )
  1918. {
  1919. if (ShellFileList == NULL) {
  1920. ShellFileList = (EFI_SHELL_FILE_INFO *)AllocateZeroPool (sizeof (EFI_SHELL_FILE_INFO));
  1921. if (ShellFileList == NULL) {
  1922. SHELL_FREE_NON_NULL (BasePath);
  1923. return EFI_OUT_OF_RESOURCES;
  1924. }
  1925. InitializeListHead (&ShellFileList->Link);
  1926. }
  1927. //
  1928. // allocate a new EFI_SHELL_FILE_INFO and populate it...
  1929. //
  1930. ShellFileListItem = CreateAndPopulateShellFileInfo (
  1931. BasePath,
  1932. EFI_SUCCESS, // success since we didn't fail to open it...
  1933. FileInfo->FileName,
  1934. NULL, // no handle since not open
  1935. FileInfo
  1936. );
  1937. if (ShellFileListItem == NULL) {
  1938. Status = EFI_OUT_OF_RESOURCES;
  1939. //
  1940. // Free resources outside the loop.
  1941. //
  1942. break;
  1943. }
  1944. InsertTailList (&ShellFileList->Link, &ShellFileListItem->Link);
  1945. }
  1946. if (EFI_ERROR (Status)) {
  1947. EfiShellFreeFileList (&ShellFileList);
  1948. *FileList = NULL;
  1949. } else {
  1950. *FileList = ShellFileList;
  1951. }
  1952. SHELL_FREE_NON_NULL (BasePath);
  1953. return (Status);
  1954. }
  1955. /**
  1956. Get the GUID value from a human readable name.
  1957. If GuidName is a known GUID name, then update Guid to have the correct value for
  1958. that GUID.
  1959. This function is only available when the major and minor versions in the
  1960. EfiShellProtocol are greater than or equal to 2 and 1, respectively.
  1961. @param[in] GuidName A pointer to the localized name for the GUID being queried.
  1962. @param[out] Guid A pointer to the GUID structure to be filled in.
  1963. @retval EFI_SUCCESS The operation was successful.
  1964. @retval EFI_INVALID_PARAMETER Guid was NULL.
  1965. @retval EFI_INVALID_PARAMETER GuidName was NULL.
  1966. @retval EFI_NOT_FOUND GuidName is not a known GUID Name.
  1967. **/
  1968. EFI_STATUS
  1969. EFIAPI
  1970. EfiShellGetGuidFromName (
  1971. IN CONST CHAR16 *GuidName,
  1972. OUT EFI_GUID *Guid
  1973. )
  1974. {
  1975. EFI_GUID *NewGuid;
  1976. EFI_STATUS Status;
  1977. if ((Guid == NULL) || (GuidName == NULL)) {
  1978. return (EFI_INVALID_PARAMETER);
  1979. }
  1980. Status = GetGuidFromStringName (GuidName, NULL, &NewGuid);
  1981. if (!EFI_ERROR (Status)) {
  1982. CopyGuid (Guid, NewGuid);
  1983. }
  1984. return (Status);
  1985. }
  1986. /**
  1987. Get the human readable name for a GUID from the value.
  1988. If Guid is assigned a name, then update *GuidName to point to the name. The callee
  1989. should not modify the value.
  1990. This function is only available when the major and minor versions in the
  1991. EfiShellProtocol are greater than or equal to 2 and 1, respectively.
  1992. @param[in] Guid A pointer to the GUID being queried.
  1993. @param[out] GuidName A pointer to a pointer the localized to name for the GUID being requested
  1994. @retval EFI_SUCCESS The operation was successful.
  1995. @retval EFI_INVALID_PARAMETER Guid was NULL.
  1996. @retval EFI_INVALID_PARAMETER GuidName was NULL.
  1997. @retval EFI_NOT_FOUND Guid is not assigned a name.
  1998. **/
  1999. EFI_STATUS
  2000. EFIAPI
  2001. EfiShellGetGuidName (
  2002. IN CONST EFI_GUID *Guid,
  2003. OUT CONST CHAR16 **GuidName
  2004. )
  2005. {
  2006. CHAR16 *Name;
  2007. if ((Guid == NULL) || (GuidName == NULL)) {
  2008. return (EFI_INVALID_PARAMETER);
  2009. }
  2010. Name = GetStringNameFromGuid (Guid, NULL);
  2011. if ((Name == NULL) || (StrLen (Name) == 0)) {
  2012. SHELL_FREE_NON_NULL (Name);
  2013. return (EFI_NOT_FOUND);
  2014. }
  2015. *GuidName = AddBufferToFreeList (Name);
  2016. return (EFI_SUCCESS);
  2017. }
  2018. /**
  2019. If FileHandle is a directory then the function reads from FileHandle and reads in
  2020. each of the FileInfo structures. If one of them matches the Pattern's first
  2021. "level" then it opens that handle and calls itself on that handle.
  2022. If FileHandle is a file and matches all of the remaining Pattern (which would be
  2023. on its last node), then add a EFI_SHELL_FILE_INFO object for this file to fileList.
  2024. Upon a EFI_SUCCESS return fromt he function any the caller is responsible to call
  2025. FreeFileList with FileList.
  2026. @param[in] FilePattern The FilePattern to check against.
  2027. @param[in] UnicodeCollation The pointer to EFI_UNICODE_COLLATION_PROTOCOL structure
  2028. @param[in] FileHandle The FileHandle to start with
  2029. @param[in, out] FileList pointer to pointer to list of found files.
  2030. @param[in] ParentNode The node for the parent. Same file as identified by HANDLE.
  2031. @param[in] MapName The file system name this file is on.
  2032. @retval EFI_SUCCESS all files were found and the FileList contains a list.
  2033. @retval EFI_NOT_FOUND no files were found
  2034. @retval EFI_OUT_OF_RESOURCES a memory allocation failed
  2035. **/
  2036. EFI_STATUS
  2037. ShellSearchHandle (
  2038. IN CONST CHAR16 *FilePattern,
  2039. IN EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation,
  2040. IN SHELL_FILE_HANDLE FileHandle,
  2041. IN OUT EFI_SHELL_FILE_INFO **FileList,
  2042. IN CONST EFI_SHELL_FILE_INFO *ParentNode OPTIONAL,
  2043. IN CONST CHAR16 *MapName
  2044. )
  2045. {
  2046. EFI_STATUS Status;
  2047. CONST CHAR16 *NextFilePatternStart;
  2048. CHAR16 *CurrentFilePattern;
  2049. EFI_SHELL_FILE_INFO *ShellInfo;
  2050. EFI_SHELL_FILE_INFO *ShellInfoNode;
  2051. EFI_SHELL_FILE_INFO *NewShellNode;
  2052. EFI_FILE_INFO *FileInfo;
  2053. BOOLEAN Directory;
  2054. CHAR16 *NewFullName;
  2055. UINTN Size;
  2056. if ( (FilePattern == NULL)
  2057. || (UnicodeCollation == NULL)
  2058. || (FileList == NULL)
  2059. )
  2060. {
  2061. return (EFI_INVALID_PARAMETER);
  2062. }
  2063. ShellInfo = NULL;
  2064. CurrentFilePattern = NULL;
  2065. if (*FilePattern == L'\\') {
  2066. FilePattern++;
  2067. }
  2068. for ( NextFilePatternStart = FilePattern
  2069. ; *NextFilePatternStart != CHAR_NULL && *NextFilePatternStart != L'\\'
  2070. ; NextFilePatternStart++)
  2071. {
  2072. }
  2073. CurrentFilePattern = AllocateZeroPool ((NextFilePatternStart-FilePattern+1)*sizeof (CHAR16));
  2074. if (CurrentFilePattern == NULL) {
  2075. return EFI_OUT_OF_RESOURCES;
  2076. }
  2077. StrnCpyS (CurrentFilePattern, NextFilePatternStart-FilePattern+1, FilePattern, NextFilePatternStart-FilePattern);
  2078. if ( (CurrentFilePattern[0] == CHAR_NULL)
  2079. && (NextFilePatternStart[0] == CHAR_NULL)
  2080. )
  2081. {
  2082. //
  2083. // we want the parent or root node (if no parent)
  2084. //
  2085. if (ParentNode == NULL) {
  2086. //
  2087. // We want the root node. create the node.
  2088. //
  2089. FileInfo = FileHandleGetInfo (FileHandle);
  2090. NewShellNode = CreateAndPopulateShellFileInfo (
  2091. MapName,
  2092. EFI_SUCCESS,
  2093. L"\\",
  2094. FileHandle,
  2095. FileInfo
  2096. );
  2097. SHELL_FREE_NON_NULL (FileInfo);
  2098. } else {
  2099. //
  2100. // Add the current parameter FileHandle to the list, then end...
  2101. //
  2102. NewShellNode = InternalDuplicateShellFileInfo ((EFI_SHELL_FILE_INFO *)ParentNode, TRUE);
  2103. }
  2104. if (NewShellNode == NULL) {
  2105. Status = EFI_OUT_OF_RESOURCES;
  2106. } else {
  2107. NewShellNode->Handle = NULL;
  2108. if (*FileList == NULL) {
  2109. *FileList = AllocateZeroPool (sizeof (EFI_SHELL_FILE_INFO));
  2110. InitializeListHead (&((*FileList)->Link));
  2111. }
  2112. //
  2113. // Add to the returning to use list
  2114. //
  2115. InsertTailList (&(*FileList)->Link, &NewShellNode->Link);
  2116. Status = EFI_SUCCESS;
  2117. }
  2118. } else {
  2119. Status = EfiShellFindFilesInDir (FileHandle, &ShellInfo);
  2120. if (!EFI_ERROR (Status)) {
  2121. if (StrStr (NextFilePatternStart, L"\\") != NULL) {
  2122. Directory = TRUE;
  2123. } else {
  2124. Directory = FALSE;
  2125. }
  2126. for ( ShellInfoNode = (EFI_SHELL_FILE_INFO *)GetFirstNode (&ShellInfo->Link)
  2127. ; !IsNull (&ShellInfo->Link, &ShellInfoNode->Link)
  2128. ; ShellInfoNode = (EFI_SHELL_FILE_INFO *)GetNextNode (&ShellInfo->Link, &ShellInfoNode->Link)
  2129. )
  2130. {
  2131. if (UnicodeCollation->MetaiMatch (UnicodeCollation, (CHAR16 *)ShellInfoNode->FileName, CurrentFilePattern)) {
  2132. if ((ShellInfoNode->FullName != NULL) && (StrStr (ShellInfoNode->FullName, L":") == NULL)) {
  2133. Size = StrSize (ShellInfoNode->FullName) + StrSize (MapName);
  2134. NewFullName = AllocateZeroPool (Size);
  2135. if (NewFullName == NULL) {
  2136. Status = EFI_OUT_OF_RESOURCES;
  2137. } else {
  2138. StrCpyS (NewFullName, Size / sizeof (CHAR16), MapName);
  2139. StrCatS (NewFullName, Size / sizeof (CHAR16), ShellInfoNode->FullName);
  2140. FreePool ((VOID *)ShellInfoNode->FullName);
  2141. ShellInfoNode->FullName = NewFullName;
  2142. }
  2143. }
  2144. if (Directory && !EFI_ERROR (Status) && (ShellInfoNode->FullName != NULL) && (ShellInfoNode->FileName != NULL)) {
  2145. //
  2146. // should be a directory
  2147. //
  2148. //
  2149. // don't open the . and .. directories
  2150. //
  2151. if ( (StrCmp (ShellInfoNode->FileName, L".") != 0)
  2152. && (StrCmp (ShellInfoNode->FileName, L"..") != 0)
  2153. )
  2154. {
  2155. //
  2156. //
  2157. //
  2158. if (EFI_ERROR (Status)) {
  2159. break;
  2160. }
  2161. //
  2162. // Open the directory since we need that handle in the next recursion.
  2163. //
  2164. ShellInfoNode->Status = EfiShellOpenFileByName (ShellInfoNode->FullName, &ShellInfoNode->Handle, EFI_FILE_MODE_READ);
  2165. //
  2166. // recurse with the next part of the pattern
  2167. //
  2168. Status = ShellSearchHandle (NextFilePatternStart, UnicodeCollation, ShellInfoNode->Handle, FileList, ShellInfoNode, MapName);
  2169. EfiShellClose (ShellInfoNode->Handle);
  2170. ShellInfoNode->Handle = NULL;
  2171. }
  2172. } else if (!EFI_ERROR (Status)) {
  2173. //
  2174. // should be a file
  2175. //
  2176. //
  2177. // copy the information we need into a new Node
  2178. //
  2179. NewShellNode = InternalDuplicateShellFileInfo (ShellInfoNode, FALSE);
  2180. if (NewShellNode == NULL) {
  2181. Status = EFI_OUT_OF_RESOURCES;
  2182. }
  2183. if (*FileList == NULL) {
  2184. *FileList = AllocateZeroPool (sizeof (EFI_SHELL_FILE_INFO));
  2185. InitializeListHead (&((*FileList)->Link));
  2186. }
  2187. //
  2188. // Add to the returning to use list
  2189. //
  2190. InsertTailList (&(*FileList)->Link, &NewShellNode->Link);
  2191. }
  2192. }
  2193. if (EFI_ERROR (Status)) {
  2194. break;
  2195. }
  2196. }
  2197. if (EFI_ERROR (Status)) {
  2198. EfiShellFreeFileList (&ShellInfo);
  2199. } else {
  2200. Status = EfiShellFreeFileList (&ShellInfo);
  2201. }
  2202. }
  2203. }
  2204. if ((*FileList == NULL) || ((*FileList != NULL) && IsListEmpty (&(*FileList)->Link))) {
  2205. Status = EFI_NOT_FOUND;
  2206. }
  2207. FreePool (CurrentFilePattern);
  2208. return (Status);
  2209. }
  2210. /**
  2211. Find files that match a specified pattern.
  2212. This function searches for all files and directories that match the specified
  2213. FilePattern. The FilePattern can contain wild-card characters. The resulting file
  2214. information is placed in the file list FileList.
  2215. Wildcards are processed
  2216. according to the rules specified in UEFI Shell 2.0 spec section 3.7.1.
  2217. The files in the file list are not opened. The OpenMode field is set to 0 and the FileInfo
  2218. field is set to NULL.
  2219. if *FileList is not NULL then it must be a pre-existing and properly initialized list.
  2220. @param FilePattern Points to a NULL-terminated shell file path, including wildcards.
  2221. @param FileList On return, points to the start of a file list containing the names
  2222. of all matching files or else points to NULL if no matching files
  2223. were found. only on a EFI_SUCCESS return will; this be non-NULL.
  2224. @retval EFI_SUCCESS Files found. FileList is a valid list.
  2225. @retval EFI_NOT_FOUND No files found.
  2226. @retval EFI_NO_MEDIA The device has no media
  2227. @retval EFI_DEVICE_ERROR The device reported an error
  2228. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted
  2229. **/
  2230. EFI_STATUS
  2231. EFIAPI
  2232. EfiShellFindFiles (
  2233. IN CONST CHAR16 *FilePattern,
  2234. OUT EFI_SHELL_FILE_INFO **FileList
  2235. )
  2236. {
  2237. EFI_STATUS Status;
  2238. CHAR16 *PatternCopy;
  2239. CHAR16 *PatternCurrentLocation;
  2240. EFI_DEVICE_PATH_PROTOCOL *RootDevicePath;
  2241. SHELL_FILE_HANDLE RootFileHandle;
  2242. CHAR16 *MapName;
  2243. UINTN Count;
  2244. if ( (FilePattern == NULL)
  2245. || (FileList == NULL)
  2246. || (StrStr (FilePattern, L":") == NULL)
  2247. )
  2248. {
  2249. return (EFI_INVALID_PARAMETER);
  2250. }
  2251. Status = EFI_SUCCESS;
  2252. RootDevicePath = NULL;
  2253. RootFileHandle = NULL;
  2254. MapName = NULL;
  2255. PatternCopy = AllocateCopyPool (StrSize (FilePattern), FilePattern);
  2256. if (PatternCopy == NULL) {
  2257. return (EFI_OUT_OF_RESOURCES);
  2258. }
  2259. PatternCopy = PathCleanUpDirectories (PatternCopy);
  2260. Count = StrStr (PatternCopy, L":") - PatternCopy + 1;
  2261. ASSERT (Count <= StrLen (PatternCopy));
  2262. ASSERT (MapName == NULL);
  2263. MapName = StrnCatGrow (&MapName, NULL, PatternCopy, Count);
  2264. if (MapName == NULL) {
  2265. Status = EFI_OUT_OF_RESOURCES;
  2266. } else {
  2267. RootDevicePath = EfiShellGetDevicePathFromFilePath (PatternCopy);
  2268. if (RootDevicePath == NULL) {
  2269. Status = EFI_INVALID_PARAMETER;
  2270. } else {
  2271. Status = EfiShellOpenRoot (RootDevicePath, &RootFileHandle);
  2272. if (!EFI_ERROR (Status)) {
  2273. for ( PatternCurrentLocation = PatternCopy
  2274. ; *PatternCurrentLocation != ':'
  2275. ; PatternCurrentLocation++)
  2276. {
  2277. }
  2278. PatternCurrentLocation++;
  2279. Status = ShellSearchHandle (PatternCurrentLocation, gUnicodeCollation, RootFileHandle, FileList, NULL, MapName);
  2280. EfiShellClose (RootFileHandle);
  2281. }
  2282. FreePool (RootDevicePath);
  2283. }
  2284. }
  2285. SHELL_FREE_NON_NULL (PatternCopy);
  2286. SHELL_FREE_NON_NULL (MapName);
  2287. return (Status);
  2288. }
  2289. /**
  2290. Opens the files that match the path specified.
  2291. This function opens all of the files specified by Path. Wildcards are processed
  2292. according to the rules specified in UEFI Shell 2.0 spec section 3.7.1. Each
  2293. matching file has an EFI_SHELL_FILE_INFO structure created in a linked list.
  2294. @param Path A pointer to the path string.
  2295. @param OpenMode Specifies the mode used to open each file, EFI_FILE_MODE_READ or
  2296. EFI_FILE_MODE_WRITE.
  2297. @param FileList Points to the start of a list of files opened.
  2298. @retval EFI_SUCCESS Create the file list successfully.
  2299. @return Others Can't create the file list.
  2300. **/
  2301. EFI_STATUS
  2302. EFIAPI
  2303. EfiShellOpenFileList (
  2304. IN CHAR16 *Path,
  2305. IN UINT64 OpenMode,
  2306. IN OUT EFI_SHELL_FILE_INFO **FileList
  2307. )
  2308. {
  2309. EFI_STATUS Status;
  2310. EFI_SHELL_FILE_INFO *ShellFileListItem;
  2311. CHAR16 *Path2;
  2312. UINTN Path2Size;
  2313. CONST CHAR16 *CurDir;
  2314. BOOLEAN Found;
  2315. PathCleanUpDirectories (Path);
  2316. Path2Size = 0;
  2317. Path2 = NULL;
  2318. if ((FileList == NULL) || (*FileList == NULL)) {
  2319. return (EFI_INVALID_PARAMETER);
  2320. }
  2321. if ((*Path == L'.') && (*(Path+1) == L'\\')) {
  2322. Path += 2;
  2323. }
  2324. //
  2325. // convert a local path to an absolute path
  2326. //
  2327. if (StrStr (Path, L":") == NULL) {
  2328. CurDir = EfiShellGetCurDir (NULL);
  2329. ASSERT ((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));
  2330. StrnCatGrow (&Path2, &Path2Size, CurDir, 0);
  2331. StrnCatGrow (&Path2, &Path2Size, L"\\", 0);
  2332. if (*Path == L'\\') {
  2333. Path++;
  2334. while (PathRemoveLastItem (Path2)) {
  2335. }
  2336. }
  2337. ASSERT ((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));
  2338. StrnCatGrow (&Path2, &Path2Size, Path, 0);
  2339. } else {
  2340. ASSERT (Path2 == NULL);
  2341. StrnCatGrow (&Path2, NULL, Path, 0);
  2342. }
  2343. PathCleanUpDirectories (Path2);
  2344. //
  2345. // do the search
  2346. //
  2347. Status = EfiShellFindFiles (Path2, FileList);
  2348. FreePool (Path2);
  2349. if (EFI_ERROR (Status)) {
  2350. return (Status);
  2351. }
  2352. Found = FALSE;
  2353. //
  2354. // We had no errors so open all the files (that are not already opened...)
  2355. //
  2356. for ( ShellFileListItem = (EFI_SHELL_FILE_INFO *)GetFirstNode (&(*FileList)->Link)
  2357. ; !IsNull (&(*FileList)->Link, &ShellFileListItem->Link)
  2358. ; ShellFileListItem = (EFI_SHELL_FILE_INFO *)GetNextNode (&(*FileList)->Link, &ShellFileListItem->Link)
  2359. )
  2360. {
  2361. if ((ShellFileListItem->Status == 0) && (ShellFileListItem->Handle == NULL)) {
  2362. ShellFileListItem->Status = EfiShellOpenFileByName (ShellFileListItem->FullName, &ShellFileListItem->Handle, OpenMode);
  2363. Found = TRUE;
  2364. }
  2365. }
  2366. if (!Found) {
  2367. return (EFI_NOT_FOUND);
  2368. }
  2369. return (EFI_SUCCESS);
  2370. }
  2371. /**
  2372. Gets the environment variable and Attributes, or list of environment variables. Can be
  2373. used instead of GetEnv().
  2374. This function returns the current value of the specified environment variable and
  2375. the Attributes. If no variable name was specified, then all of the known
  2376. variables will be returned.
  2377. @param[in] Name A pointer to the environment variable name. If Name is NULL,
  2378. then the function will return all of the defined shell
  2379. environment variables. In the case where multiple environment
  2380. variables are being returned, each variable will be terminated
  2381. by a NULL, and the list will be terminated by a double NULL.
  2382. @param[out] Attributes If not NULL, a pointer to the returned attributes bitmask for
  2383. the environment variable. In the case where Name is NULL, and
  2384. multiple environment variables are being returned, Attributes
  2385. is undefined.
  2386. @retval NULL The environment variable doesn't exist.
  2387. @return A non-NULL value points to the variable's value. The returned
  2388. pointer does not need to be freed by the caller.
  2389. **/
  2390. CONST CHAR16 *
  2391. EFIAPI
  2392. EfiShellGetEnvEx (
  2393. IN CONST CHAR16 *Name,
  2394. OUT UINT32 *Attributes OPTIONAL
  2395. )
  2396. {
  2397. EFI_STATUS Status;
  2398. VOID *Buffer;
  2399. UINTN Size;
  2400. ENV_VAR_LIST *Node;
  2401. CHAR16 *CurrentWriteLocation;
  2402. Size = 0;
  2403. Buffer = NULL;
  2404. if (Name == NULL) {
  2405. //
  2406. // Build the semi-colon delimited list. (2 passes)
  2407. //
  2408. for ( Node = (ENV_VAR_LIST *)GetFirstNode (&gShellEnvVarList.Link)
  2409. ; !IsNull (&gShellEnvVarList.Link, &Node->Link)
  2410. ; Node = (ENV_VAR_LIST *)GetNextNode (&gShellEnvVarList.Link, &Node->Link)
  2411. )
  2412. {
  2413. ASSERT (Node->Key != NULL);
  2414. Size += StrSize (Node->Key);
  2415. }
  2416. Size += 2*sizeof (CHAR16);
  2417. Buffer = AllocateZeroPool (Size);
  2418. if (Buffer == NULL) {
  2419. return (NULL);
  2420. }
  2421. CurrentWriteLocation = (CHAR16 *)Buffer;
  2422. for ( Node = (ENV_VAR_LIST *)GetFirstNode (&gShellEnvVarList.Link)
  2423. ; !IsNull (&gShellEnvVarList.Link, &Node->Link)
  2424. ; Node = (ENV_VAR_LIST *)GetNextNode (&gShellEnvVarList.Link, &Node->Link)
  2425. )
  2426. {
  2427. ASSERT (Node->Key != NULL);
  2428. StrCpyS (
  2429. CurrentWriteLocation,
  2430. (Size)/sizeof (CHAR16) - (CurrentWriteLocation - ((CHAR16 *)Buffer)),
  2431. Node->Key
  2432. );
  2433. CurrentWriteLocation += StrLen (CurrentWriteLocation) + 1;
  2434. }
  2435. } else {
  2436. //
  2437. // We are doing a specific environment variable
  2438. //
  2439. Status = ShellFindEnvVarInList (Name, (CHAR16 **)&Buffer, &Size, Attributes);
  2440. if (EFI_ERROR (Status)) {
  2441. //
  2442. // get the size we need for this EnvVariable
  2443. //
  2444. Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES (Name, Attributes, &Size, Buffer);
  2445. if (Status == EFI_BUFFER_TOO_SMALL) {
  2446. //
  2447. // Allocate the space and recall the get function
  2448. //
  2449. Buffer = AllocateZeroPool (Size);
  2450. Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES (Name, Attributes, &Size, Buffer);
  2451. }
  2452. //
  2453. // we didn't get it (might not exist)
  2454. // free the memory if we allocated any and return NULL
  2455. //
  2456. if (EFI_ERROR (Status)) {
  2457. if (Buffer != NULL) {
  2458. FreePool (Buffer);
  2459. }
  2460. return (NULL);
  2461. } else {
  2462. //
  2463. // If we did not find the environment variable in the gShellEnvVarList
  2464. // but get it from UEFI variable storage successfully then we need update
  2465. // the gShellEnvVarList.
  2466. //
  2467. ShellFreeEnvVarList ();
  2468. Status = ShellInitEnvVarList ();
  2469. ASSERT (Status == EFI_SUCCESS);
  2470. }
  2471. }
  2472. }
  2473. //
  2474. // return the buffer
  2475. //
  2476. return (AddBufferToFreeList (Buffer));
  2477. }
  2478. /**
  2479. Gets either a single or list of environment variables.
  2480. If name is not NULL then this function returns the current value of the specified
  2481. environment variable.
  2482. If Name is NULL, then a list of all environment variable names is returned. Each is a
  2483. NULL terminated string with a double NULL terminating the list.
  2484. @param Name A pointer to the environment variable name. If
  2485. Name is NULL, then the function will return all
  2486. of the defined shell environment variables. In
  2487. the case where multiple environment variables are
  2488. being returned, each variable will be terminated by
  2489. a NULL, and the list will be terminated by a double
  2490. NULL.
  2491. @retval !=NULL A pointer to the returned string.
  2492. The returned pointer does not need to be freed by the caller.
  2493. @retval NULL The environment variable doesn't exist or there are
  2494. no environment variables.
  2495. **/
  2496. CONST CHAR16 *
  2497. EFIAPI
  2498. EfiShellGetEnv (
  2499. IN CONST CHAR16 *Name
  2500. )
  2501. {
  2502. return (EfiShellGetEnvEx (Name, NULL));
  2503. }
  2504. /**
  2505. Internal variable setting function. Allows for setting of the read only variables.
  2506. @param Name Points to the NULL-terminated environment variable name.
  2507. @param Value Points to the NULL-terminated environment variable value. If the value is an
  2508. empty string then the environment variable is deleted.
  2509. @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
  2510. @retval EFI_SUCCESS The environment variable was successfully updated.
  2511. **/
  2512. EFI_STATUS
  2513. InternalEfiShellSetEnv (
  2514. IN CONST CHAR16 *Name,
  2515. IN CONST CHAR16 *Value,
  2516. IN BOOLEAN Volatile
  2517. )
  2518. {
  2519. EFI_STATUS Status;
  2520. if ((Value == NULL) || (StrLen (Value) == 0)) {
  2521. Status = SHELL_DELETE_ENVIRONMENT_VARIABLE (Name);
  2522. if (!EFI_ERROR (Status)) {
  2523. ShellRemvoeEnvVarFromList (Name);
  2524. }
  2525. } else {
  2526. SHELL_DELETE_ENVIRONMENT_VARIABLE (Name);
  2527. Status = ShellAddEnvVarToList (
  2528. Name,
  2529. Value,
  2530. StrSize (Value),
  2531. EFI_VARIABLE_BOOTSERVICE_ACCESS | (Volatile ? 0 : EFI_VARIABLE_NON_VOLATILE)
  2532. );
  2533. if (!EFI_ERROR (Status)) {
  2534. Status = Volatile
  2535. ? SHELL_SET_ENVIRONMENT_VARIABLE_V (Name, StrSize (Value) - sizeof (CHAR16), Value)
  2536. : SHELL_SET_ENVIRONMENT_VARIABLE_NV (Name, StrSize (Value) - sizeof (CHAR16), Value);
  2537. if (EFI_ERROR (Status)) {
  2538. ShellRemvoeEnvVarFromList (Name);
  2539. }
  2540. }
  2541. }
  2542. return Status;
  2543. }
  2544. /**
  2545. Sets the environment variable.
  2546. This function changes the current value of the specified environment variable. If the
  2547. environment variable exists and the Value is an empty string, then the environment
  2548. variable is deleted. If the environment variable exists and the Value is not an empty
  2549. string, then the value of the environment variable is changed. If the environment
  2550. variable does not exist and the Value is an empty string, there is no action. If the
  2551. environment variable does not exist and the Value is a non-empty string, then the
  2552. environment variable is created and assigned the specified value.
  2553. For a description of volatile and non-volatile environment variables, see UEFI Shell
  2554. 2.0 specification section 3.6.1.
  2555. @param Name Points to the NULL-terminated environment variable name.
  2556. @param Value Points to the NULL-terminated environment variable value. If the value is an
  2557. empty string then the environment variable is deleted.
  2558. @param Volatile Indicates whether the variable is non-volatile (FALSE) or volatile (TRUE).
  2559. @retval EFI_SUCCESS The environment variable was successfully updated.
  2560. **/
  2561. EFI_STATUS
  2562. EFIAPI
  2563. EfiShellSetEnv (
  2564. IN CONST CHAR16 *Name,
  2565. IN CONST CHAR16 *Value,
  2566. IN BOOLEAN Volatile
  2567. )
  2568. {
  2569. if ((Name == NULL) || (*Name == CHAR_NULL)) {
  2570. return (EFI_INVALID_PARAMETER);
  2571. }
  2572. //
  2573. // Make sure we dont 'set' a predefined read only variable
  2574. //
  2575. if ((StrCmp (Name, L"cwd") == 0) ||
  2576. (StrCmp (Name, L"lasterror") == 0) ||
  2577. (StrCmp (Name, L"profiles") == 0) ||
  2578. (StrCmp (Name, L"uefishellsupport") == 0) ||
  2579. (StrCmp (Name, L"uefishellversion") == 0) ||
  2580. (StrCmp (Name, L"uefiversion") == 0) ||
  2581. (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest &&
  2582. (StrCmp (Name, mNoNestingEnvVarName) == 0))
  2583. )
  2584. {
  2585. return (EFI_INVALID_PARAMETER);
  2586. }
  2587. return (InternalEfiShellSetEnv (Name, Value, Volatile));
  2588. }
  2589. /**
  2590. Returns the current directory on the specified device.
  2591. If FileSystemMapping is NULL, it returns the current working directory. If the
  2592. FileSystemMapping is not NULL, it returns the current directory associated with the
  2593. FileSystemMapping. In both cases, the returned name includes the file system
  2594. mapping (i.e. fs0:\current-dir).
  2595. Note that the current directory string should exclude the tailing backslash character.
  2596. @param FileSystemMapping A pointer to the file system mapping. If NULL,
  2597. then the current working directory is returned.
  2598. @retval !=NULL The current directory.
  2599. @retval NULL Current directory does not exist.
  2600. **/
  2601. CONST CHAR16 *
  2602. EFIAPI
  2603. EfiShellGetCurDir (
  2604. IN CONST CHAR16 *FileSystemMapping OPTIONAL
  2605. )
  2606. {
  2607. CHAR16 *PathToReturn;
  2608. UINTN Size;
  2609. SHELL_MAP_LIST *MapListItem;
  2610. if (!IsListEmpty (&gShellMapList.Link)) {
  2611. //
  2612. // if parameter is NULL, use current
  2613. //
  2614. if (FileSystemMapping == NULL) {
  2615. return (EfiShellGetEnv (L"cwd"));
  2616. } else {
  2617. Size = 0;
  2618. PathToReturn = NULL;
  2619. MapListItem = ShellCommandFindMapItem (FileSystemMapping);
  2620. if (MapListItem != NULL) {
  2621. ASSERT ((PathToReturn == NULL && Size == 0) || (PathToReturn != NULL));
  2622. PathToReturn = StrnCatGrow (&PathToReturn, &Size, MapListItem->MapName, 0);
  2623. PathToReturn = StrnCatGrow (&PathToReturn, &Size, MapListItem->CurrentDirectoryPath, 0);
  2624. }
  2625. }
  2626. return (AddBufferToFreeList (PathToReturn));
  2627. } else {
  2628. return (NULL);
  2629. }
  2630. }
  2631. /**
  2632. Changes the current directory on the specified device.
  2633. If the FileSystem is NULL, and the directory Dir does not contain a file system's
  2634. mapped name, this function changes the current working directory.
  2635. If the FileSystem is NULL and the directory Dir contains a mapped name, then the
  2636. current file system and the current directory on that file system are changed.
  2637. If FileSystem is NULL, and Dir is not NULL, then this changes the current working file
  2638. system.
  2639. If FileSystem is not NULL and Dir is not NULL, then this function changes the current
  2640. directory on the specified file system.
  2641. If the current working directory or the current working file system is changed then the
  2642. %cwd% environment variable will be updated
  2643. Note that the current directory string should exclude the tailing backslash character.
  2644. @param FileSystem A pointer to the file system's mapped name. If NULL, then the current working
  2645. directory is changed.
  2646. @param Dir Points to the NULL-terminated directory on the device specified by FileSystem.
  2647. @retval EFI_SUCCESS The operation was successful
  2648. @retval EFI_NOT_FOUND The file system could not be found
  2649. **/
  2650. EFI_STATUS
  2651. EFIAPI
  2652. EfiShellSetCurDir (
  2653. IN CONST CHAR16 *FileSystem OPTIONAL,
  2654. IN CONST CHAR16 *Dir
  2655. )
  2656. {
  2657. CHAR16 *MapName;
  2658. SHELL_MAP_LIST *MapListItem;
  2659. UINTN Size;
  2660. EFI_STATUS Status;
  2661. CHAR16 *TempString;
  2662. CHAR16 *DirectoryName;
  2663. UINTN TempLen;
  2664. Size = 0;
  2665. MapName = NULL;
  2666. MapListItem = NULL;
  2667. TempString = NULL;
  2668. DirectoryName = NULL;
  2669. if (((FileSystem == NULL) && (Dir == NULL)) || (Dir == NULL)) {
  2670. return (EFI_INVALID_PARAMETER);
  2671. }
  2672. if (IsListEmpty (&gShellMapList.Link)) {
  2673. return (EFI_NOT_FOUND);
  2674. }
  2675. DirectoryName = StrnCatGrow (&DirectoryName, NULL, Dir, 0);
  2676. ASSERT (DirectoryName != NULL);
  2677. PathCleanUpDirectories (DirectoryName);
  2678. if (FileSystem == NULL) {
  2679. //
  2680. // determine the file system mapping to use
  2681. //
  2682. if (StrStr (DirectoryName, L":") != NULL) {
  2683. ASSERT (MapName == NULL);
  2684. MapName = StrnCatGrow (&MapName, NULL, DirectoryName, (StrStr (DirectoryName, L":")-DirectoryName+1));
  2685. }
  2686. //
  2687. // find the file system mapping's entry in the list
  2688. // or use current
  2689. //
  2690. if (MapName != NULL) {
  2691. MapListItem = ShellCommandFindMapItem (MapName);
  2692. //
  2693. // make that the current file system mapping
  2694. //
  2695. if (MapListItem != NULL) {
  2696. gShellCurMapping = MapListItem;
  2697. }
  2698. } else {
  2699. MapListItem = gShellCurMapping;
  2700. }
  2701. if (MapListItem == NULL) {
  2702. FreePool (DirectoryName);
  2703. SHELL_FREE_NON_NULL (MapName);
  2704. return (EFI_NOT_FOUND);
  2705. }
  2706. //
  2707. // now update the MapListItem's current directory
  2708. //
  2709. if ((MapListItem->CurrentDirectoryPath != NULL) && (DirectoryName[StrLen (DirectoryName) - 1] != L':')) {
  2710. FreePool (MapListItem->CurrentDirectoryPath);
  2711. MapListItem->CurrentDirectoryPath = NULL;
  2712. }
  2713. if (MapName != NULL) {
  2714. TempLen = StrLen (MapName);
  2715. if (TempLen != StrLen (DirectoryName)) {
  2716. ASSERT ((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
  2717. MapListItem->CurrentDirectoryPath = StrnCatGrow (&MapListItem->CurrentDirectoryPath, &Size, DirectoryName+StrLen (MapName), 0);
  2718. }
  2719. FreePool (MapName);
  2720. } else {
  2721. ASSERT ((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
  2722. MapListItem->CurrentDirectoryPath = StrnCatGrow (&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);
  2723. }
  2724. if (((MapListItem->CurrentDirectoryPath != NULL) && (MapListItem->CurrentDirectoryPath[StrLen (MapListItem->CurrentDirectoryPath)-1] == L'\\')) || (MapListItem->CurrentDirectoryPath == NULL)) {
  2725. ASSERT ((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
  2726. if (MapListItem->CurrentDirectoryPath != NULL) {
  2727. MapListItem->CurrentDirectoryPath[StrLen (MapListItem->CurrentDirectoryPath)-1] = CHAR_NULL;
  2728. }
  2729. }
  2730. } else {
  2731. //
  2732. // cant have a mapping in the directory...
  2733. //
  2734. if (StrStr (DirectoryName, L":") != NULL) {
  2735. FreePool (DirectoryName);
  2736. return (EFI_INVALID_PARAMETER);
  2737. }
  2738. //
  2739. // FileSystem != NULL
  2740. //
  2741. MapListItem = ShellCommandFindMapItem (FileSystem);
  2742. if (MapListItem == NULL) {
  2743. FreePool (DirectoryName);
  2744. return (EFI_INVALID_PARAMETER);
  2745. }
  2746. // gShellCurMapping = MapListItem;
  2747. if (DirectoryName != NULL) {
  2748. //
  2749. // change current dir on that file system
  2750. //
  2751. if (MapListItem->CurrentDirectoryPath != NULL) {
  2752. FreePool (MapListItem->CurrentDirectoryPath);
  2753. DEBUG_CODE (
  2754. MapListItem->CurrentDirectoryPath = NULL;
  2755. );
  2756. }
  2757. // ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
  2758. // MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, FileSystem, 0);
  2759. ASSERT ((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
  2760. MapListItem->CurrentDirectoryPath = StrnCatGrow (&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);
  2761. ASSERT ((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
  2762. MapListItem->CurrentDirectoryPath = StrnCatGrow (&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);
  2763. if ((MapListItem->CurrentDirectoryPath != NULL) && (MapListItem->CurrentDirectoryPath[StrLen (MapListItem->CurrentDirectoryPath)-1] == L'\\')) {
  2764. ASSERT ((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));
  2765. MapListItem->CurrentDirectoryPath[StrLen (MapListItem->CurrentDirectoryPath)-1] = CHAR_NULL;
  2766. }
  2767. }
  2768. }
  2769. FreePool (DirectoryName);
  2770. //
  2771. // if updated the current directory then update the environment variable
  2772. //
  2773. if (MapListItem == gShellCurMapping) {
  2774. Size = 0;
  2775. ASSERT ((TempString == NULL && Size == 0) || (TempString != NULL));
  2776. StrnCatGrow (&TempString, &Size, MapListItem->MapName, 0);
  2777. ASSERT ((TempString == NULL && Size == 0) || (TempString != NULL));
  2778. StrnCatGrow (&TempString, &Size, MapListItem->CurrentDirectoryPath, 0);
  2779. Status = InternalEfiShellSetEnv (L"cwd", TempString, TRUE);
  2780. FreePool (TempString);
  2781. return (Status);
  2782. }
  2783. return (EFI_SUCCESS);
  2784. }
  2785. /**
  2786. Return help information about a specific command.
  2787. This function returns the help information for the specified command. The help text
  2788. can be internal to the shell or can be from a UEFI Shell manual page.
  2789. If Sections is specified, then each section name listed will be compared in a casesensitive
  2790. manner, to the section names described in Appendix B. If the section exists,
  2791. it will be appended to the returned help text. If the section does not exist, no
  2792. information will be returned. If Sections is NULL, then all help text information
  2793. available will be returned.
  2794. @param Command Points to the NULL-terminated UEFI Shell command name.
  2795. @param Sections Points to the NULL-terminated comma-delimited
  2796. section names to return. If NULL, then all
  2797. sections will be returned.
  2798. @param HelpText On return, points to a callee-allocated buffer
  2799. containing all specified help text.
  2800. @retval EFI_SUCCESS The help text was returned.
  2801. @retval EFI_OUT_OF_RESOURCES The necessary buffer could not be allocated to hold the
  2802. returned help text.
  2803. @retval EFI_INVALID_PARAMETER HelpText is NULL
  2804. @retval EFI_NOT_FOUND There is no help text available for Command.
  2805. **/
  2806. EFI_STATUS
  2807. EFIAPI
  2808. EfiShellGetHelpText (
  2809. IN CONST CHAR16 *Command,
  2810. IN CONST CHAR16 *Sections OPTIONAL,
  2811. OUT CHAR16 **HelpText
  2812. )
  2813. {
  2814. CONST CHAR16 *ManFileName;
  2815. CHAR16 *FixCommand;
  2816. EFI_STATUS Status;
  2817. ASSERT (HelpText != NULL);
  2818. FixCommand = NULL;
  2819. ManFileName = ShellCommandGetManFileNameHandler (Command);
  2820. if (ManFileName != NULL) {
  2821. return (ProcessManFile (ManFileName, Command, Sections, NULL, HelpText));
  2822. } else {
  2823. if ( (StrLen (Command) > 4)
  2824. && ((Command[StrLen (Command)-1] == L'i') || (Command[StrLen (Command)-1] == L'I'))
  2825. && ((Command[StrLen (Command)-2] == L'f') || (Command[StrLen (Command)-2] == L'F'))
  2826. && ((Command[StrLen (Command)-3] == L'e') || (Command[StrLen (Command)-3] == L'E'))
  2827. && (Command[StrLen (Command)-4] == L'.')
  2828. )
  2829. {
  2830. FixCommand = AllocateZeroPool (StrSize (Command) - 4 * sizeof (CHAR16));
  2831. if (FixCommand == NULL) {
  2832. return EFI_OUT_OF_RESOURCES;
  2833. }
  2834. StrnCpyS (
  2835. FixCommand,
  2836. (StrSize (Command) - 4 * sizeof (CHAR16))/sizeof (CHAR16),
  2837. Command,
  2838. StrLen (Command)-4
  2839. );
  2840. Status = ProcessManFile (FixCommand, FixCommand, Sections, NULL, HelpText);
  2841. FreePool (FixCommand);
  2842. return Status;
  2843. } else {
  2844. return (ProcessManFile (Command, Command, Sections, NULL, HelpText));
  2845. }
  2846. }
  2847. }
  2848. /**
  2849. Gets the enable status of the page break output mode.
  2850. User can use this function to determine current page break mode.
  2851. @retval TRUE The page break output mode is enabled.
  2852. @retval FALSE The page break output mode is disabled.
  2853. **/
  2854. BOOLEAN
  2855. EFIAPI
  2856. EfiShellGetPageBreak (
  2857. VOID
  2858. )
  2859. {
  2860. return (ShellInfoObject.PageBreakEnabled);
  2861. }
  2862. /**
  2863. Judges whether the active shell is the root shell.
  2864. This function makes the user to know that whether the active Shell is the root shell.
  2865. @retval TRUE The active Shell is the root Shell.
  2866. @retval FALSE The active Shell is NOT the root Shell.
  2867. **/
  2868. BOOLEAN
  2869. EFIAPI
  2870. EfiShellIsRootShell (
  2871. VOID
  2872. )
  2873. {
  2874. return (ShellInfoObject.RootShellInstance);
  2875. }
  2876. /**
  2877. function to return a semi-colon delimited list of all alias' in the current shell
  2878. up to caller to free the memory.
  2879. @retval NULL No alias' were found
  2880. @retval NULL An error occurred getting alias'
  2881. @return !NULL a list of all alias'
  2882. **/
  2883. CHAR16 *
  2884. InternalEfiShellGetListAlias (
  2885. VOID
  2886. )
  2887. {
  2888. EFI_STATUS Status;
  2889. EFI_GUID Guid;
  2890. CHAR16 *VariableName;
  2891. UINTN NameSize;
  2892. UINTN NameBufferSize;
  2893. CHAR16 *RetVal;
  2894. UINTN RetSize;
  2895. NameBufferSize = INIT_NAME_BUFFER_SIZE;
  2896. VariableName = AllocateZeroPool (NameBufferSize);
  2897. RetSize = 0;
  2898. RetVal = NULL;
  2899. if (VariableName == NULL) {
  2900. return (NULL);
  2901. }
  2902. VariableName[0] = CHAR_NULL;
  2903. while (TRUE) {
  2904. NameSize = NameBufferSize;
  2905. Status = gRT->GetNextVariableName (&NameSize, VariableName, &Guid);
  2906. if (Status == EFI_NOT_FOUND) {
  2907. break;
  2908. } else if (Status == EFI_BUFFER_TOO_SMALL) {
  2909. NameBufferSize = NameSize > NameBufferSize * 2 ? NameSize : NameBufferSize * 2;
  2910. SHELL_FREE_NON_NULL (VariableName);
  2911. VariableName = AllocateZeroPool (NameBufferSize);
  2912. if (VariableName == NULL) {
  2913. Status = EFI_OUT_OF_RESOURCES;
  2914. SHELL_FREE_NON_NULL (RetVal);
  2915. RetVal = NULL;
  2916. break;
  2917. }
  2918. NameSize = NameBufferSize;
  2919. Status = gRT->GetNextVariableName (&NameSize, VariableName, &Guid);
  2920. }
  2921. if (EFI_ERROR (Status)) {
  2922. SHELL_FREE_NON_NULL (RetVal);
  2923. RetVal = NULL;
  2924. break;
  2925. }
  2926. if (CompareGuid (&Guid, &gShellAliasGuid)) {
  2927. ASSERT ((RetVal == NULL && RetSize == 0) || (RetVal != NULL));
  2928. RetVal = StrnCatGrow (&RetVal, &RetSize, VariableName, 0);
  2929. RetVal = StrnCatGrow (&RetVal, &RetSize, L";", 0);
  2930. } // compare guid
  2931. } // while
  2932. SHELL_FREE_NON_NULL (VariableName);
  2933. return (RetVal);
  2934. }
  2935. /**
  2936. Convert a null-terminated unicode string, in-place, to all lowercase.
  2937. Then return it.
  2938. @param Str The null-terminated string to be converted to all lowercase.
  2939. @return The null-terminated string converted into all lowercase.
  2940. **/
  2941. CHAR16 *
  2942. ToLower (
  2943. CHAR16 *Str
  2944. )
  2945. {
  2946. UINTN Index;
  2947. for (Index = 0; Str[Index] != L'\0'; Index++) {
  2948. if ((Str[Index] >= L'A') && (Str[Index] <= L'Z')) {
  2949. Str[Index] -= (CHAR16)(L'A' - L'a');
  2950. }
  2951. }
  2952. return Str;
  2953. }
  2954. /**
  2955. This function returns the command associated with a alias or a list of all
  2956. alias'.
  2957. @param[in] Alias Points to the NULL-terminated shell alias.
  2958. If this parameter is NULL, then all
  2959. aliases will be returned in ReturnedData.
  2960. @param[out] Volatile upon return of a single command if TRUE indicates
  2961. this is stored in a volatile fashion. FALSE otherwise.
  2962. @return If Alias is not NULL, it will return a pointer to
  2963. the NULL-terminated command for that alias.
  2964. If Alias is NULL, ReturnedData points to a ';'
  2965. delimited list of alias (e.g.
  2966. ReturnedData = "dir;del;copy;mfp") that is NULL-terminated.
  2967. @retval NULL an error occurred
  2968. @retval NULL Alias was not a valid Alias
  2969. **/
  2970. CONST CHAR16 *
  2971. EFIAPI
  2972. EfiShellGetAlias (
  2973. IN CONST CHAR16 *Alias,
  2974. OUT BOOLEAN *Volatile OPTIONAL
  2975. )
  2976. {
  2977. CHAR16 *RetVal;
  2978. UINTN RetSize;
  2979. UINT32 Attribs;
  2980. EFI_STATUS Status;
  2981. CHAR16 *AliasLower;
  2982. CHAR16 *AliasVal;
  2983. // Convert to lowercase to make aliases case-insensitive
  2984. if (Alias != NULL) {
  2985. AliasLower = AllocateCopyPool (StrSize (Alias), Alias);
  2986. if (AliasLower == NULL) {
  2987. return NULL;
  2988. }
  2989. ToLower (AliasLower);
  2990. if (Volatile == NULL) {
  2991. GetVariable2 (AliasLower, &gShellAliasGuid, (VOID **)&AliasVal, NULL);
  2992. FreePool (AliasLower);
  2993. return (AddBufferToFreeList (AliasVal));
  2994. }
  2995. RetSize = 0;
  2996. RetVal = NULL;
  2997. Status = gRT->GetVariable (AliasLower, &gShellAliasGuid, &Attribs, &RetSize, RetVal);
  2998. if (Status == EFI_BUFFER_TOO_SMALL) {
  2999. RetVal = AllocateZeroPool (RetSize);
  3000. Status = gRT->GetVariable (AliasLower, &gShellAliasGuid, &Attribs, &RetSize, RetVal);
  3001. }
  3002. if (EFI_ERROR (Status)) {
  3003. if (RetVal != NULL) {
  3004. FreePool (RetVal);
  3005. }
  3006. FreePool (AliasLower);
  3007. return (NULL);
  3008. }
  3009. if ((EFI_VARIABLE_NON_VOLATILE & Attribs) == EFI_VARIABLE_NON_VOLATILE) {
  3010. *Volatile = FALSE;
  3011. } else {
  3012. *Volatile = TRUE;
  3013. }
  3014. FreePool (AliasLower);
  3015. return (AddBufferToFreeList (RetVal));
  3016. }
  3017. return (AddBufferToFreeList (InternalEfiShellGetListAlias ()));
  3018. }
  3019. /**
  3020. Changes a shell command alias.
  3021. This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
  3022. this function does not check for built in alias'.
  3023. @param[in] Command Points to the NULL-terminated shell command or existing alias.
  3024. @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
  3025. Command refers to an alias, that alias will be deleted.
  3026. @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
  3027. Alias being set will be stored in a non-volatile fashion.
  3028. @retval EFI_SUCCESS Alias created or deleted successfully.
  3029. @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
  3030. **/
  3031. EFI_STATUS
  3032. InternalSetAlias (
  3033. IN CONST CHAR16 *Command,
  3034. IN CONST CHAR16 *Alias,
  3035. IN BOOLEAN Volatile
  3036. )
  3037. {
  3038. EFI_STATUS Status;
  3039. CHAR16 *AliasLower;
  3040. BOOLEAN DeleteAlias;
  3041. DeleteAlias = FALSE;
  3042. if (Alias == NULL) {
  3043. //
  3044. // We must be trying to remove one if Alias is NULL
  3045. // remove an alias (but passed in COMMAND parameter)
  3046. //
  3047. Alias = Command;
  3048. DeleteAlias = TRUE;
  3049. }
  3050. ASSERT (Alias != NULL);
  3051. //
  3052. // Convert to lowercase to make aliases case-insensitive
  3053. //
  3054. AliasLower = AllocateCopyPool (StrSize (Alias), Alias);
  3055. if (AliasLower == NULL) {
  3056. return EFI_OUT_OF_RESOURCES;
  3057. }
  3058. ToLower (AliasLower);
  3059. if (DeleteAlias) {
  3060. Status = gRT->SetVariable (AliasLower, &gShellAliasGuid, 0, 0, NULL);
  3061. } else {
  3062. Status = gRT->SetVariable (
  3063. AliasLower,
  3064. &gShellAliasGuid,
  3065. EFI_VARIABLE_BOOTSERVICE_ACCESS | (Volatile ? 0 : EFI_VARIABLE_NON_VOLATILE),
  3066. StrSize (Command),
  3067. (VOID *)Command
  3068. );
  3069. }
  3070. FreePool (AliasLower);
  3071. return Status;
  3072. }
  3073. /**
  3074. Changes a shell command alias.
  3075. This function creates an alias for a shell command or if Alias is NULL it will delete an existing alias.
  3076. @param[in] Command Points to the NULL-terminated shell command or existing alias.
  3077. @param[in] Alias Points to the NULL-terminated alias for the shell command. If this is NULL, and
  3078. Command refers to an alias, that alias will be deleted.
  3079. @param[in] Replace If TRUE and the alias already exists, then the existing alias will be replaced. If
  3080. FALSE and the alias already exists, then the existing alias is unchanged and
  3081. EFI_ACCESS_DENIED is returned.
  3082. @param[in] Volatile if TRUE the Alias being set will be stored in a volatile fashion. if FALSE the
  3083. Alias being set will be stored in a non-volatile fashion.
  3084. @retval EFI_SUCCESS Alias created or deleted successfully.
  3085. @retval EFI_NOT_FOUND the Alias intended to be deleted was not found
  3086. @retval EFI_ACCESS_DENIED The alias is a built-in alias or already existed and Replace was set to
  3087. FALSE.
  3088. @retval EFI_INVALID_PARAMETER Command is null or the empty string.
  3089. **/
  3090. EFI_STATUS
  3091. EFIAPI
  3092. EfiShellSetAlias (
  3093. IN CONST CHAR16 *Command,
  3094. IN CONST CHAR16 *Alias,
  3095. IN BOOLEAN Replace,
  3096. IN BOOLEAN Volatile
  3097. )
  3098. {
  3099. if (ShellCommandIsOnAliasList ((Alias == NULL) ? Command : Alias)) {
  3100. //
  3101. // cant set over a built in alias
  3102. //
  3103. return (EFI_ACCESS_DENIED);
  3104. } else if ((Command == NULL) || (*Command == CHAR_NULL) || (StrLen (Command) == 0)) {
  3105. //
  3106. // Command is null or empty
  3107. //
  3108. return (EFI_INVALID_PARAMETER);
  3109. } else if ((EfiShellGetAlias (Command, NULL) != NULL) && !Replace) {
  3110. //
  3111. // Alias already exists, Replace not set
  3112. //
  3113. return (EFI_ACCESS_DENIED);
  3114. } else {
  3115. return (InternalSetAlias (Command, Alias, Volatile));
  3116. }
  3117. }
  3118. // Pure FILE_HANDLE operations are passed to FileHandleLib
  3119. // these functions are indicated by the *
  3120. EFI_SHELL_PROTOCOL mShellProtocol = {
  3121. EfiShellExecute,
  3122. EfiShellGetEnv,
  3123. EfiShellSetEnv,
  3124. EfiShellGetAlias,
  3125. EfiShellSetAlias,
  3126. EfiShellGetHelpText,
  3127. EfiShellGetDevicePathFromMap,
  3128. EfiShellGetMapFromDevicePath,
  3129. EfiShellGetDevicePathFromFilePath,
  3130. EfiShellGetFilePathFromDevicePath,
  3131. EfiShellSetMap,
  3132. EfiShellGetCurDir,
  3133. EfiShellSetCurDir,
  3134. EfiShellOpenFileList,
  3135. EfiShellFreeFileList,
  3136. EfiShellRemoveDupInFileList,
  3137. EfiShellBatchIsActive,
  3138. EfiShellIsRootShell,
  3139. EfiShellEnablePageBreak,
  3140. EfiShellDisablePageBreak,
  3141. EfiShellGetPageBreak,
  3142. EfiShellGetDeviceName,
  3143. (EFI_SHELL_GET_FILE_INFO)FileHandleGetInfo, // *
  3144. (EFI_SHELL_SET_FILE_INFO)FileHandleSetInfo, // *
  3145. EfiShellOpenFileByName,
  3146. EfiShellClose,
  3147. EfiShellCreateFile,
  3148. (EFI_SHELL_READ_FILE)FileHandleRead, // *
  3149. (EFI_SHELL_WRITE_FILE)FileHandleWrite, // *
  3150. (EFI_SHELL_DELETE_FILE)FileHandleDelete, // *
  3151. EfiShellDeleteFileByName,
  3152. (EFI_SHELL_GET_FILE_POSITION)FileHandleGetPosition, // *
  3153. (EFI_SHELL_SET_FILE_POSITION)FileHandleSetPosition, // *
  3154. (EFI_SHELL_FLUSH_FILE)FileHandleFlush, // *
  3155. EfiShellFindFiles,
  3156. EfiShellFindFilesInDir,
  3157. (EFI_SHELL_GET_FILE_SIZE)FileHandleGetSize, // *
  3158. EfiShellOpenRoot,
  3159. EfiShellOpenRootByHandle,
  3160. NULL,
  3161. SHELL_MAJOR_VERSION,
  3162. SHELL_MINOR_VERSION,
  3163. // New for UEFI Shell 2.1
  3164. EfiShellRegisterGuidName,
  3165. EfiShellGetGuidName,
  3166. EfiShellGetGuidFromName,
  3167. EfiShellGetEnvEx
  3168. };
  3169. /**
  3170. Function to create and install on the current handle.
  3171. Will overwrite any existing ShellProtocols in the system to be sure that
  3172. the current shell is in control.
  3173. This must be removed via calling CleanUpShellProtocol().
  3174. @param[in, out] NewShell The pointer to the pointer to the structure
  3175. to install.
  3176. @retval EFI_SUCCESS The operation was successful.
  3177. @return An error from LocateHandle, CreateEvent, or other core function.
  3178. **/
  3179. EFI_STATUS
  3180. CreatePopulateInstallShellProtocol (
  3181. IN OUT EFI_SHELL_PROTOCOL **NewShell
  3182. )
  3183. {
  3184. EFI_STATUS Status;
  3185. UINTN BufferSize;
  3186. EFI_HANDLE *Buffer;
  3187. UINTN HandleCounter;
  3188. SHELL_PROTOCOL_HANDLE_LIST *OldProtocolNode;
  3189. EFI_SHELL_PROTOCOL *OldShell;
  3190. if (NewShell == NULL) {
  3191. return (EFI_INVALID_PARAMETER);
  3192. }
  3193. BufferSize = 0;
  3194. Buffer = NULL;
  3195. OldProtocolNode = NULL;
  3196. InitializeListHead (&ShellInfoObject.OldShellList.Link);
  3197. //
  3198. // Initialize EfiShellProtocol object...
  3199. //
  3200. Status = gBS->CreateEvent (
  3201. 0,
  3202. 0,
  3203. NULL,
  3204. NULL,
  3205. &mShellProtocol.ExecutionBreak
  3206. );
  3207. if (EFI_ERROR (Status)) {
  3208. return (Status);
  3209. }
  3210. //
  3211. // Get the size of the buffer we need.
  3212. //
  3213. Status = gBS->LocateHandle (
  3214. ByProtocol,
  3215. &gEfiShellProtocolGuid,
  3216. NULL,
  3217. &BufferSize,
  3218. Buffer
  3219. );
  3220. if (Status == EFI_BUFFER_TOO_SMALL) {
  3221. //
  3222. // Allocate and recall with buffer of correct size
  3223. //
  3224. Buffer = AllocateZeroPool (BufferSize);
  3225. if (Buffer == NULL) {
  3226. return (EFI_OUT_OF_RESOURCES);
  3227. }
  3228. Status = gBS->LocateHandle (
  3229. ByProtocol,
  3230. &gEfiShellProtocolGuid,
  3231. NULL,
  3232. &BufferSize,
  3233. Buffer
  3234. );
  3235. if (EFI_ERROR (Status)) {
  3236. FreePool (Buffer);
  3237. return (Status);
  3238. }
  3239. //
  3240. // now overwrite each of them, but save the info to restore when we end.
  3241. //
  3242. for (HandleCounter = 0; HandleCounter < (BufferSize/sizeof (EFI_HANDLE)); HandleCounter++) {
  3243. Status = gBS->OpenProtocol (
  3244. Buffer[HandleCounter],
  3245. &gEfiShellProtocolGuid,
  3246. (VOID **)&OldShell,
  3247. gImageHandle,
  3248. NULL,
  3249. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  3250. );
  3251. if (!EFI_ERROR (Status)) {
  3252. OldProtocolNode = AllocateZeroPool (sizeof (SHELL_PROTOCOL_HANDLE_LIST));
  3253. if (OldProtocolNode == NULL) {
  3254. if (!IsListEmpty (&ShellInfoObject.OldShellList.Link)) {
  3255. CleanUpShellProtocol (&mShellProtocol);
  3256. }
  3257. Status = EFI_OUT_OF_RESOURCES;
  3258. break;
  3259. }
  3260. //
  3261. // reinstall over the old one...
  3262. //
  3263. OldProtocolNode->Handle = Buffer[HandleCounter];
  3264. OldProtocolNode->Interface = OldShell;
  3265. Status = gBS->ReinstallProtocolInterface (
  3266. OldProtocolNode->Handle,
  3267. &gEfiShellProtocolGuid,
  3268. OldProtocolNode->Interface,
  3269. (VOID *)(&mShellProtocol)
  3270. );
  3271. if (!EFI_ERROR (Status)) {
  3272. //
  3273. // we reinstalled successfully. log this so we can reverse it later.
  3274. //
  3275. //
  3276. // add to the list for subsequent...
  3277. //
  3278. InsertTailList (&ShellInfoObject.OldShellList.Link, &OldProtocolNode->Link);
  3279. }
  3280. }
  3281. }
  3282. FreePool (Buffer);
  3283. } else if (Status == EFI_NOT_FOUND) {
  3284. ASSERT (IsListEmpty (&ShellInfoObject.OldShellList.Link));
  3285. //
  3286. // no one else published yet. just publish it ourselves.
  3287. //
  3288. Status = gBS->InstallProtocolInterface (
  3289. &gImageHandle,
  3290. &gEfiShellProtocolGuid,
  3291. EFI_NATIVE_INTERFACE,
  3292. (VOID *)(&mShellProtocol)
  3293. );
  3294. }
  3295. if (PcdGetBool (PcdShellSupportOldProtocols)) {
  3296. /// @todo support ShellEnvironment2
  3297. /// @todo do we need to support ShellEnvironment (not ShellEnvironment2) also?
  3298. }
  3299. if (!EFI_ERROR (Status)) {
  3300. *NewShell = &mShellProtocol;
  3301. }
  3302. return (Status);
  3303. }
  3304. /**
  3305. Opposite of CreatePopulateInstallShellProtocol.
  3306. Free all memory and restore the system to the state it was in before calling
  3307. CreatePopulateInstallShellProtocol.
  3308. @param[in, out] NewShell The pointer to the new shell protocol structure.
  3309. @retval EFI_SUCCESS The operation was successful.
  3310. **/
  3311. EFI_STATUS
  3312. CleanUpShellProtocol (
  3313. IN OUT EFI_SHELL_PROTOCOL *NewShell
  3314. )
  3315. {
  3316. SHELL_PROTOCOL_HANDLE_LIST *Node2;
  3317. //
  3318. // if we need to restore old protocols...
  3319. //
  3320. if (!IsListEmpty (&ShellInfoObject.OldShellList.Link)) {
  3321. for (Node2 = (SHELL_PROTOCOL_HANDLE_LIST *)GetFirstNode (&ShellInfoObject.OldShellList.Link)
  3322. ; !IsListEmpty (&ShellInfoObject.OldShellList.Link)
  3323. ; Node2 = (SHELL_PROTOCOL_HANDLE_LIST *)GetFirstNode (&ShellInfoObject.OldShellList.Link)
  3324. )
  3325. {
  3326. RemoveEntryList (&Node2->Link);
  3327. gBS->ReinstallProtocolInterface (Node2->Handle, &gEfiShellProtocolGuid, NewShell, Node2->Interface);
  3328. FreePool (Node2);
  3329. }
  3330. } else {
  3331. //
  3332. // no need to restore
  3333. //
  3334. gBS->UninstallProtocolInterface (gImageHandle, &gEfiShellProtocolGuid, NewShell);
  3335. }
  3336. return EFI_SUCCESS;
  3337. }
  3338. /**
  3339. Cleanup the shell environment.
  3340. @param[in, out] NewShell The pointer to the new shell protocol structure.
  3341. @retval EFI_SUCCESS The operation was successful.
  3342. **/
  3343. EFI_STATUS
  3344. CleanUpShellEnvironment (
  3345. IN OUT EFI_SHELL_PROTOCOL *NewShell
  3346. )
  3347. {
  3348. EFI_STATUS Status;
  3349. EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;
  3350. CleanUpShellProtocol (NewShell);
  3351. Status = gBS->CloseEvent (NewShell->ExecutionBreak);
  3352. NewShell->ExecutionBreak = NULL;
  3353. Status = gBS->OpenProtocol (
  3354. gST->ConsoleInHandle,
  3355. &gEfiSimpleTextInputExProtocolGuid,
  3356. (VOID **)&SimpleEx,
  3357. gImageHandle,
  3358. NULL,
  3359. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  3360. );
  3361. if (!EFI_ERROR (Status)) {
  3362. Status = SimpleEx->UnregisterKeyNotify (SimpleEx, ShellInfoObject.CtrlCNotifyHandle1);
  3363. Status = SimpleEx->UnregisterKeyNotify (SimpleEx, ShellInfoObject.CtrlCNotifyHandle2);
  3364. Status = SimpleEx->UnregisterKeyNotify (SimpleEx, ShellInfoObject.CtrlCNotifyHandle3);
  3365. Status = SimpleEx->UnregisterKeyNotify (SimpleEx, ShellInfoObject.CtrlCNotifyHandle4);
  3366. Status = SimpleEx->UnregisterKeyNotify (SimpleEx, ShellInfoObject.CtrlSNotifyHandle1);
  3367. Status = SimpleEx->UnregisterKeyNotify (SimpleEx, ShellInfoObject.CtrlSNotifyHandle2);
  3368. Status = SimpleEx->UnregisterKeyNotify (SimpleEx, ShellInfoObject.CtrlSNotifyHandle3);
  3369. Status = SimpleEx->UnregisterKeyNotify (SimpleEx, ShellInfoObject.CtrlSNotifyHandle4);
  3370. }
  3371. return (Status);
  3372. }
  3373. /**
  3374. Notification function for keystrokes.
  3375. @param[in] KeyData The key that was pressed.
  3376. @retval EFI_SUCCESS The operation was successful.
  3377. **/
  3378. EFI_STATUS
  3379. EFIAPI
  3380. NotificationFunction (
  3381. IN EFI_KEY_DATA *KeyData
  3382. )
  3383. {
  3384. if (((KeyData->Key.UnicodeChar == L'c') &&
  3385. ((KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED)) || (KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED)))) ||
  3386. (KeyData->Key.UnicodeChar == 3)
  3387. )
  3388. {
  3389. if (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak == NULL) {
  3390. return (EFI_UNSUPPORTED);
  3391. }
  3392. return (gBS->SignalEvent (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak));
  3393. } else if ((KeyData->Key.UnicodeChar == L's') &&
  3394. ((KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED)) || (KeyData->KeyState.KeyShiftState == (EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED)))
  3395. )
  3396. {
  3397. ShellInfoObject.HaltOutput = TRUE;
  3398. }
  3399. return (EFI_SUCCESS);
  3400. }
  3401. /**
  3402. Function to start monitoring for CTRL-C using SimpleTextInputEx. This
  3403. feature's enabled state was not known when the shell initially launched.
  3404. @retval EFI_SUCCESS The feature is enabled.
  3405. @retval EFI_OUT_OF_RESOURCES There is not enough memory available.
  3406. **/
  3407. EFI_STATUS
  3408. InernalEfiShellStartMonitor (
  3409. VOID
  3410. )
  3411. {
  3412. EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;
  3413. EFI_KEY_DATA KeyData;
  3414. EFI_STATUS Status;
  3415. Status = gBS->OpenProtocol (
  3416. gST->ConsoleInHandle,
  3417. &gEfiSimpleTextInputExProtocolGuid,
  3418. (VOID **)&SimpleEx,
  3419. gImageHandle,
  3420. NULL,
  3421. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  3422. );
  3423. if (EFI_ERROR (Status)) {
  3424. ShellPrintHiiEx (
  3425. -1,
  3426. -1,
  3427. NULL,
  3428. STRING_TOKEN (STR_SHELL_NO_IN_EX),
  3429. ShellInfoObject.HiiHandle
  3430. );
  3431. return (EFI_SUCCESS);
  3432. }
  3433. if (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak == NULL) {
  3434. return (EFI_UNSUPPORTED);
  3435. }
  3436. KeyData.KeyState.KeyToggleState = 0;
  3437. KeyData.Key.ScanCode = 0;
  3438. KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;
  3439. KeyData.Key.UnicodeChar = L'c';
  3440. Status = SimpleEx->RegisterKeyNotify (
  3441. SimpleEx,
  3442. &KeyData,
  3443. NotificationFunction,
  3444. &ShellInfoObject.CtrlCNotifyHandle1
  3445. );
  3446. KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;
  3447. if (!EFI_ERROR (Status)) {
  3448. Status = SimpleEx->RegisterKeyNotify (
  3449. SimpleEx,
  3450. &KeyData,
  3451. NotificationFunction,
  3452. &ShellInfoObject.CtrlCNotifyHandle2
  3453. );
  3454. }
  3455. KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;
  3456. KeyData.Key.UnicodeChar = 3;
  3457. if (!EFI_ERROR (Status)) {
  3458. Status = SimpleEx->RegisterKeyNotify (
  3459. SimpleEx,
  3460. &KeyData,
  3461. NotificationFunction,
  3462. &ShellInfoObject.CtrlCNotifyHandle3
  3463. );
  3464. }
  3465. KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;
  3466. if (!EFI_ERROR (Status)) {
  3467. Status = SimpleEx->RegisterKeyNotify (
  3468. SimpleEx,
  3469. &KeyData,
  3470. NotificationFunction,
  3471. &ShellInfoObject.CtrlCNotifyHandle4
  3472. );
  3473. }
  3474. return (Status);
  3475. }