PxeBcImpl.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439
  1. /** @file
  2. This implementation of EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL.
  3. Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "PxeBcImpl.h"
  7. /**
  8. Enables the use of the PXE Base Code Protocol functions.
  9. This function enables the use of the PXE Base Code Protocol functions. If the
  10. Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then
  11. EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted
  12. addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted
  13. addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported
  14. field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will
  15. be returned. If there is not enough memory or other resources to start the PXE
  16. Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the
  17. PXE Base Code Protocol will be started.
  18. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  19. @param[in] UseIpv6 Specifies the type of IP addresses that are to be
  20. used during the session that is being started.
  21. Set to TRUE for IPv6, and FALSE for IPv4.
  22. @retval EFI_SUCCESS The PXE Base Code Protocol was started.
  23. @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
  24. @retval EFI_UNSUPPORTED UseIpv6 is TRUE, but the Ipv6Supported field of the
  25. EFI_PXE_BASE_CODE_MODE structure is FALSE.
  26. @retval EFI_ALREADY_STARTED The PXE Base Code Protocol is already in the started state.
  27. @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
  28. EFI_PXE_BASE_CODE_PROTOCOL structure.
  29. @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory or other resources to start the
  30. PXE Base Code Protocol.
  31. **/
  32. EFI_STATUS
  33. EFIAPI
  34. EfiPxeBcStart (
  35. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  36. IN BOOLEAN UseIpv6
  37. )
  38. {
  39. PXEBC_PRIVATE_DATA *Private;
  40. EFI_PXE_BASE_CODE_MODE *Mode;
  41. UINTN Index;
  42. EFI_STATUS Status;
  43. if (This == NULL) {
  44. return EFI_INVALID_PARAMETER;
  45. }
  46. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  47. Mode = Private->PxeBc.Mode;
  48. if (Mode->Started) {
  49. return EFI_ALREADY_STARTED;
  50. }
  51. //
  52. // Detect whether using IPv6 or not, and set it into mode data.
  53. //
  54. if (UseIpv6 && Mode->Ipv6Available && Mode->Ipv6Supported && (Private->Ip6Nic != NULL)) {
  55. Mode->UsingIpv6 = TRUE;
  56. } else if (!UseIpv6 && (Private->Ip4Nic != NULL)) {
  57. Mode->UsingIpv6 = FALSE;
  58. } else {
  59. return EFI_UNSUPPORTED;
  60. }
  61. if (Mode->UsingIpv6) {
  62. AsciiPrint ("\n>>Start PXE over IPv6");
  63. //
  64. // Configure udp6 instance to receive data.
  65. //
  66. Status = Private->Udp6Read->Configure (
  67. Private->Udp6Read,
  68. &Private->Udp6CfgData
  69. );
  70. if (EFI_ERROR (Status)) {
  71. goto ON_ERROR;
  72. }
  73. //
  74. // Configure block size for TFTP as a default value to handle all link layers.
  75. //
  76. Private->BlockSize = Private->Ip6MaxPacketSize -
  77. PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE;
  78. //
  79. // PXE over IPv6 starts here, initialize the fields and list header.
  80. //
  81. Private->Ip6Policy = PXEBC_IP6_POLICY_MAX;
  82. Private->ProxyOffer.Dhcp6.Packet.Offer.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE;
  83. Private->DhcpAck.Dhcp6.Packet.Ack.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE;
  84. Private->PxeReply.Dhcp6.Packet.Ack.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE;
  85. for (Index = 0; Index < PXEBC_OFFER_MAX_NUM; Index++) {
  86. Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = PXEBC_CACHED_DHCP6_PACKET_MAX_SIZE;
  87. }
  88. //
  89. // Create event and set status for token to capture ICMP6 error message.
  90. //
  91. Private->Icmp6Token.Status = EFI_NOT_READY;
  92. Status = gBS->CreateEvent (
  93. EVT_NOTIFY_SIGNAL,
  94. TPL_NOTIFY,
  95. PxeBcIcmp6ErrorUpdate,
  96. Private,
  97. &Private->Icmp6Token.Event
  98. );
  99. if (EFI_ERROR (Status)) {
  100. goto ON_ERROR;
  101. }
  102. //
  103. // Set Ip6 policy to Automatic to start the IP6 router discovery.
  104. //
  105. Status = PxeBcSetIp6Policy (Private);
  106. if (EFI_ERROR (Status)) {
  107. goto ON_ERROR;
  108. }
  109. } else {
  110. AsciiPrint ("\n>>Start PXE over IPv4");
  111. //
  112. // Configure udp4 instance to receive data.
  113. //
  114. Status = Private->Udp4Read->Configure (
  115. Private->Udp4Read,
  116. &Private->Udp4CfgData
  117. );
  118. if (EFI_ERROR (Status)) {
  119. goto ON_ERROR;
  120. }
  121. //
  122. // Configure block size for TFTP as a default value to handle all link layers.
  123. //
  124. Private->BlockSize = Private->Ip4MaxPacketSize -
  125. PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE;
  126. //
  127. // PXE over IPv4 starts here, initialize the fields.
  128. //
  129. Private->ProxyOffer.Dhcp4.Packet.Offer.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE;
  130. Private->DhcpAck.Dhcp4.Packet.Ack.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE;
  131. Private->PxeReply.Dhcp4.Packet.Ack.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE;
  132. for (Index = 0; Index < PXEBC_OFFER_MAX_NUM; Index++) {
  133. Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = PXEBC_CACHED_DHCP4_PACKET_MAX_SIZE;
  134. }
  135. PxeBcSeedDhcp4Packet (&Private->SeedPacket, Private->Udp4Read);
  136. //
  137. // Create the event for Arp cache update.
  138. //
  139. Status = gBS->CreateEvent (
  140. EVT_TIMER | EVT_NOTIFY_SIGNAL,
  141. TPL_CALLBACK,
  142. PxeBcArpCacheUpdate,
  143. Private,
  144. &Private->ArpUpdateEvent
  145. );
  146. if (EFI_ERROR (Status)) {
  147. goto ON_ERROR;
  148. }
  149. //
  150. // Start a periodic timer by second to update Arp cache.
  151. //
  152. Status = gBS->SetTimer (
  153. Private->ArpUpdateEvent,
  154. TimerPeriodic,
  155. TICKS_PER_SECOND
  156. );
  157. if (EFI_ERROR (Status)) {
  158. goto ON_ERROR;
  159. }
  160. //
  161. // Create event and set status for token to capture ICMP error message.
  162. //
  163. Private->Icmp6Token.Status = EFI_NOT_READY;
  164. Status = gBS->CreateEvent (
  165. EVT_NOTIFY_SIGNAL,
  166. TPL_NOTIFY,
  167. PxeBcIcmpErrorUpdate,
  168. Private,
  169. &Private->IcmpToken.Event
  170. );
  171. if (EFI_ERROR (Status)) {
  172. goto ON_ERROR;
  173. }
  174. //
  175. // DHCP4 service allows only one of its children to be configured in
  176. // the active state, If the DHCP4 D.O.R.A started by IP4 auto
  177. // configuration and has not been completed, the Dhcp4 state machine
  178. // will not be in the right state for the PXE to start a new round D.O.R.A.
  179. // so we need to switch its policy to static.
  180. //
  181. Status = PxeBcSetIp4Policy (Private);
  182. if (EFI_ERROR (Status)) {
  183. goto ON_ERROR;
  184. }
  185. }
  186. //
  187. // If PcdTftpBlockSize is set to non-zero, override the default value.
  188. //
  189. if (PcdGet64 (PcdTftpBlockSize) != 0) {
  190. Private->BlockSize = (UINTN)PcdGet64 (PcdTftpBlockSize);
  191. }
  192. //
  193. // Create event for UdpRead/UdpWrite timeout since they are both blocking API.
  194. //
  195. Status = gBS->CreateEvent (
  196. EVT_TIMER,
  197. TPL_CALLBACK,
  198. NULL,
  199. NULL,
  200. &Private->UdpTimeOutEvent
  201. );
  202. if (EFI_ERROR (Status)) {
  203. goto ON_ERROR;
  204. }
  205. Private->IsAddressOk = FALSE;
  206. Mode->Started = TRUE;
  207. return EFI_SUCCESS;
  208. ON_ERROR:
  209. if (Mode->UsingIpv6) {
  210. if (Private->Icmp6Token.Event != NULL) {
  211. gBS->CloseEvent (Private->Icmp6Token.Event);
  212. Private->Icmp6Token.Event = NULL;
  213. }
  214. Private->Udp6Read->Configure (Private->Udp6Read, NULL);
  215. Private->Ip6->Configure (Private->Ip6, NULL);
  216. } else {
  217. if (Private->ArpUpdateEvent != NULL) {
  218. gBS->CloseEvent (Private->ArpUpdateEvent);
  219. Private->ArpUpdateEvent = NULL;
  220. }
  221. if (Private->IcmpToken.Event != NULL) {
  222. gBS->CloseEvent (Private->IcmpToken.Event);
  223. Private->IcmpToken.Event = NULL;
  224. }
  225. Private->Udp4Read->Configure (Private->Udp4Read, NULL);
  226. Private->Ip4->Configure (Private->Ip4, NULL);
  227. }
  228. return Status;
  229. }
  230. /**
  231. Disable the use of the PXE Base Code Protocol functions.
  232. This function stops all activity on the network device. All the resources allocated
  233. in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is
  234. set to FALSE, and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE
  235. structure is already FALSE, then EFI_NOT_STARTED will be returned.
  236. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  237. @retval EFI_SUCCESS The PXE Base Code Protocol was stopped.
  238. @retval EFI_NOT_STARTED The PXE Base Code Protocol is already in the stopped state.
  239. @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
  240. EFI_PXE_BASE_CODE_PROTOCOL structure.
  241. @retval Others
  242. **/
  243. EFI_STATUS
  244. EFIAPI
  245. EfiPxeBcStop (
  246. IN EFI_PXE_BASE_CODE_PROTOCOL *This
  247. )
  248. {
  249. PXEBC_PRIVATE_DATA *Private;
  250. EFI_PXE_BASE_CODE_MODE *Mode;
  251. BOOLEAN Ipv6Supported;
  252. BOOLEAN Ipv6Available;
  253. if (This == NULL) {
  254. return EFI_INVALID_PARAMETER;
  255. }
  256. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  257. Mode = Private->PxeBc.Mode;
  258. Ipv6Supported = Mode->Ipv6Supported;
  259. Ipv6Available = Mode->Ipv6Available;
  260. if (!Mode->Started) {
  261. return EFI_NOT_STARTED;
  262. }
  263. if (Mode->UsingIpv6) {
  264. //
  265. // Configure all the instances for IPv6 as NULL.
  266. //
  267. ZeroMem (&Private->Udp6CfgData.StationAddress, sizeof (EFI_IPv6_ADDRESS));
  268. ZeroMem (&Private->Ip6CfgData.StationAddress, sizeof (EFI_IPv6_ADDRESS));
  269. Private->Dhcp6->Stop (Private->Dhcp6);
  270. Private->Dhcp6->Configure (Private->Dhcp6, NULL);
  271. Private->Udp6Write->Configure (Private->Udp6Write, NULL);
  272. Private->Udp6Read->Groups (Private->Udp6Read, FALSE, NULL);
  273. Private->Udp6Read->Configure (Private->Udp6Read, NULL);
  274. Private->Ip6->Cancel (Private->Ip6, &Private->Icmp6Token);
  275. Private->Ip6->Configure (Private->Ip6, NULL);
  276. PxeBcUnregisterIp6Address (Private);
  277. if (Private->Icmp6Token.Event != NULL) {
  278. gBS->CloseEvent (Private->Icmp6Token.Event);
  279. Private->Icmp6Token.Event = NULL;
  280. }
  281. if (Private->Dhcp6Request != NULL) {
  282. FreePool (Private->Dhcp6Request);
  283. Private->Dhcp6Request = NULL;
  284. }
  285. if (Private->BootFileName != NULL) {
  286. FreePool (Private->BootFileName);
  287. Private->BootFileName = NULL;
  288. }
  289. } else {
  290. //
  291. // Configure all the instances for IPv4 as NULL.
  292. //
  293. ZeroMem (&Private->Udp4CfgData.StationAddress, sizeof (EFI_IPv4_ADDRESS));
  294. ZeroMem (&Private->Udp4CfgData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  295. ZeroMem (&Private->Ip4CfgData.StationAddress, sizeof (EFI_IPv4_ADDRESS));
  296. ZeroMem (&Private->Ip4CfgData.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  297. Private->Dhcp4->Stop (Private->Dhcp4);
  298. Private->Dhcp4->Configure (Private->Dhcp4, NULL);
  299. Private->Udp4Write->Configure (Private->Udp4Write, NULL);
  300. Private->Udp4Read->Groups (Private->Udp4Read, FALSE, NULL);
  301. Private->Udp4Read->Configure (Private->Udp4Read, NULL);
  302. Private->Ip4->Cancel (Private->Ip4, &Private->IcmpToken);
  303. Private->Ip4->Configure (Private->Ip4, NULL);
  304. if (Private->ArpUpdateEvent != NULL) {
  305. gBS->CloseEvent (Private->ArpUpdateEvent);
  306. Private->ArpUpdateEvent = NULL;
  307. }
  308. if (Private->IcmpToken.Event != NULL) {
  309. gBS->CloseEvent (Private->IcmpToken.Event);
  310. Private->IcmpToken.Event = NULL;
  311. }
  312. Private->BootFileName = NULL;
  313. }
  314. gBS->CloseEvent (Private->UdpTimeOutEvent);
  315. Private->CurSrcPort = 0;
  316. Private->BootFileSize = 0;
  317. Private->SolicitTimes = 0;
  318. Private->ElapsedTime = 0;
  319. ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));
  320. ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));
  321. ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));
  322. ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));
  323. //
  324. // Reset the mode data.
  325. //
  326. ZeroMem (Mode, sizeof (EFI_PXE_BASE_CODE_MODE));
  327. Mode->Ipv6Available = Ipv6Available;
  328. Mode->Ipv6Supported = Ipv6Supported;
  329. Mode->AutoArp = TRUE;
  330. Mode->TTL = DEFAULT_TTL;
  331. Mode->ToS = DEFAULT_ToS;
  332. return EFI_SUCCESS;
  333. }
  334. /**
  335. Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6
  336. S.A.R.R (solicit / advertise / request / reply) sequence.
  337. If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before
  338. they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will
  339. be tried in the order in which they are received. Please see the Preboot Execution
  340. Environment (PXE) Specification and Unified Extensible Firmware Interface (UEFI)
  341. Specification for additional details on the implementation of DHCP.
  342. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
  343. then the DHCP sequence will be stopped and EFI_ABORTED will be returned.
  344. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  345. @param[in] SortOffers TRUE if the offers received should be sorted. Set to FALSE to
  346. try the offers in the order that they are received.
  347. @retval EFI_SUCCESS Valid DHCP has completed.
  348. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  349. @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid
  350. EFI_PXE_BASE_CODE_PROTOCOL structure.
  351. @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
  352. @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete the DHCP Protocol.
  353. @retval EFI_ABORTED The callback function aborted the DHCP Protocol.
  354. @retval EFI_TIMEOUT The DHCP Protocol timed out.
  355. @retval EFI_ICMP_ERROR An ICMP error packet was received during the DHCP session.
  356. @retval EFI_NO_RESPONSE Valid PXE offer was not received.
  357. **/
  358. EFI_STATUS
  359. EFIAPI
  360. EfiPxeBcDhcp (
  361. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  362. IN BOOLEAN SortOffers
  363. )
  364. {
  365. PXEBC_PRIVATE_DATA *Private;
  366. EFI_PXE_BASE_CODE_MODE *Mode;
  367. EFI_STATUS Status;
  368. EFI_PXE_BASE_CODE_IP_FILTER IpFilter;
  369. if (This == NULL) {
  370. return EFI_INVALID_PARAMETER;
  371. }
  372. Status = EFI_SUCCESS;
  373. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  374. Mode = Private->PxeBc.Mode;
  375. Mode->IcmpErrorReceived = FALSE;
  376. Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DHCP;
  377. Private->IsOfferSorted = SortOffers;
  378. Private->SolicitTimes = 0;
  379. Private->ElapsedTime = 0;
  380. if (!Mode->Started) {
  381. return EFI_NOT_STARTED;
  382. }
  383. if (Mode->UsingIpv6) {
  384. //
  385. // Stop Udp6Read instance
  386. //
  387. Private->Udp6Read->Configure (Private->Udp6Read, NULL);
  388. //
  389. // Start S.A.R.R. process to get a IPv6 address and other boot information.
  390. //
  391. Status = PxeBcDhcp6Sarr (Private, Private->Dhcp6);
  392. } else {
  393. //
  394. // Stop Udp4Read instance
  395. //
  396. Private->Udp4Read->Configure (Private->Udp4Read, NULL);
  397. //
  398. // Start D.O.R.A. process to get a IPv4 address and other boot information.
  399. //
  400. Status = PxeBcDhcp4Dora (Private, Private->Dhcp4);
  401. }
  402. //
  403. // Reconfigure the UDP instance with the default configuration.
  404. //
  405. if (Mode->UsingIpv6) {
  406. Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
  407. } else {
  408. Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
  409. }
  410. //
  411. // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
  412. // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
  413. //
  414. ZeroMem (&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));
  415. IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;
  416. This->SetIpFilter (This, &IpFilter);
  417. return Status;
  418. }
  419. /**
  420. Attempts to complete the PXE Boot Server and/or boot image discovery sequence.
  421. This function attempts to complete the PXE Boot Server and/or boot image discovery
  422. sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the
  423. PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the
  424. EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the
  425. PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure
  426. will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.
  427. In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],
  428. has two uses: It is the Boot Server IP address list used for unicast discovery
  429. (if the UseUCast field is TRUE), and it is the list used for Boot Server verification
  430. (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure
  431. is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot
  432. Server reply of that type will be accepted. If the AcceptAnyResponse field is
  433. FALSE, only responses from Boot Servers with matching IP addresses will be accepted.
  434. This function can take at least 10 seconds to timeout and return control to the
  435. caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be
  436. returned. Please see the Preboot Execution Environment (PXE) Specification for
  437. additional details on the implementation of the Discovery sequence.
  438. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
  439. then the Discovery sequence is stopped and EFI_ABORTED will be returned.
  440. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  441. @param[in] Type The type of bootstrap to perform.
  442. @param[in] Layer Pointer to the boot server layer number to discover, which must be
  443. PXE_BOOT_LAYER_INITIAL when a new server type is being
  444. discovered.
  445. @param[in] UseBis TRUE if Boot Integrity Services are to be used. FALSE otherwise.
  446. @param[in] Info Pointer to a data structure that contains additional information
  447. on the type of discovery operation that is to be performed.
  448. It is optional.
  449. @retval EFI_SUCCESS The Discovery sequence has been completed.
  450. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  451. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  452. @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
  453. @retval EFI_OUT_OF_RESOURCES Could not allocate enough memory to complete Discovery.
  454. @retval EFI_ABORTED The callback function aborted the Discovery sequence.
  455. @retval EFI_TIMEOUT The Discovery sequence timed out.
  456. @retval EFI_ICMP_ERROR An ICMP error packet was received during the PXE discovery
  457. session.
  458. **/
  459. EFI_STATUS
  460. EFIAPI
  461. EfiPxeBcDiscover (
  462. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  463. IN UINT16 Type,
  464. IN UINT16 *Layer,
  465. IN BOOLEAN UseBis,
  466. IN EFI_PXE_BASE_CODE_DISCOVER_INFO *Info OPTIONAL
  467. )
  468. {
  469. PXEBC_PRIVATE_DATA *Private;
  470. EFI_PXE_BASE_CODE_MODE *Mode;
  471. EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;
  472. EFI_PXE_BASE_CODE_SRVLIST *SrvList;
  473. PXEBC_BOOT_SVR_ENTRY *BootSvrEntry;
  474. UINT16 Index;
  475. EFI_STATUS Status;
  476. EFI_PXE_BASE_CODE_IP_FILTER IpFilter;
  477. EFI_PXE_BASE_CODE_DISCOVER_INFO *NewCreatedInfo;
  478. if (This == NULL) {
  479. return EFI_INVALID_PARAMETER;
  480. }
  481. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  482. Mode = Private->PxeBc.Mode;
  483. Mode->IcmpErrorReceived = FALSE;
  484. BootSvrEntry = NULL;
  485. SrvList = NULL;
  486. Status = EFI_DEVICE_ERROR;
  487. Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;
  488. NewCreatedInfo = NULL;
  489. if (!Mode->Started) {
  490. return EFI_NOT_STARTED;
  491. }
  492. //
  493. // Station address should be ready before do discover.
  494. //
  495. if (!Private->IsAddressOk) {
  496. return EFI_INVALID_PARAMETER;
  497. }
  498. if (Mode->UsingIpv6) {
  499. //
  500. // Stop Udp6Read instance
  501. //
  502. Private->Udp6Read->Configure (Private->Udp6Read, NULL);
  503. } else {
  504. //
  505. // Stop Udp4Read instance
  506. //
  507. Private->Udp4Read->Configure (Private->Udp4Read, NULL);
  508. }
  509. //
  510. // There are 3 methods to get the information for discover.
  511. //
  512. ZeroMem (&DefaultInfo, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO));
  513. if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {
  514. //
  515. // 1. Take the previous setting as the discover info.
  516. //
  517. if (!Mode->PxeDiscoverValid ||
  518. !Mode->PxeReplyReceived ||
  519. (!Mode->PxeBisReplyReceived && UseBis))
  520. {
  521. Status = EFI_INVALID_PARAMETER;
  522. goto ON_EXIT;
  523. }
  524. Info = &DefaultInfo;
  525. Info->IpCnt = 1;
  526. Info->UseUCast = TRUE;
  527. SrvList = Info->SrvList;
  528. SrvList[0].Type = Type;
  529. SrvList[0].AcceptAnyResponse = FALSE;
  530. CopyMem (&SrvList->IpAddr, &Private->ServerIp, sizeof (EFI_IP_ADDRESS));
  531. } else if (Info == NULL) {
  532. //
  533. // 2. Extract the discover information from the cached packets if unspecified.
  534. //
  535. NewCreatedInfo = &DefaultInfo;
  536. Status = PxeBcExtractDiscoverInfo (Private, Type, &NewCreatedInfo, &BootSvrEntry, &SrvList);
  537. if (EFI_ERROR (Status)) {
  538. goto ON_EXIT;
  539. }
  540. ASSERT (NewCreatedInfo != NULL);
  541. Info = NewCreatedInfo;
  542. } else {
  543. //
  544. // 3. Take the pass-in information as the discover info, and validate the server list.
  545. //
  546. SrvList = Info->SrvList;
  547. if (!SrvList[0].AcceptAnyResponse) {
  548. for (Index = 1; Index < Info->IpCnt; Index++) {
  549. if (SrvList[Index].AcceptAnyResponse) {
  550. break;
  551. }
  552. }
  553. if (Index != Info->IpCnt) {
  554. //
  555. // It's invalid if the first server doesn't accept any response
  556. // but any of the other servers does accept any response.
  557. //
  558. Status = EFI_INVALID_PARAMETER;
  559. goto ON_EXIT;
  560. }
  561. }
  562. }
  563. //
  564. // Info and BootSvrEntry/SrvList are all ready by now, so execute discover by UniCast/BroadCast/MultiCast.
  565. //
  566. if ((!Info->UseUCast && !Info->UseBCast && !Info->UseMCast) ||
  567. (Info->MustUseList && (Info->IpCnt == 0)))
  568. {
  569. Status = EFI_INVALID_PARAMETER;
  570. goto ON_EXIT;
  571. }
  572. Private->IsDoDiscover = TRUE;
  573. if (Info->UseMCast) {
  574. //
  575. // Do discover by multicast.
  576. //
  577. Status = PxeBcDiscoverBootServer (
  578. Private,
  579. Type,
  580. Layer,
  581. UseBis,
  582. &Info->ServerMCastIp,
  583. Info->IpCnt,
  584. SrvList
  585. );
  586. } else if (Info->UseBCast) {
  587. //
  588. // Do discover by broadcast, but only valid for IPv4.
  589. //
  590. ASSERT (!Mode->UsingIpv6);
  591. Status = PxeBcDiscoverBootServer (
  592. Private,
  593. Type,
  594. Layer,
  595. UseBis,
  596. NULL,
  597. Info->IpCnt,
  598. SrvList
  599. );
  600. } else if (Info->UseUCast) {
  601. //
  602. // Do discover by unicast.
  603. //
  604. for (Index = 0; Index < Info->IpCnt; Index++) {
  605. if (BootSvrEntry == NULL) {
  606. CopyMem (&Private->ServerIp, &SrvList[Index].IpAddr, sizeof (EFI_IP_ADDRESS));
  607. } else {
  608. ASSERT (!Mode->UsingIpv6);
  609. ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));
  610. CopyMem (&Private->ServerIp, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));
  611. }
  612. Status = PxeBcDiscoverBootServer (
  613. Private,
  614. Type,
  615. Layer,
  616. UseBis,
  617. &Private->ServerIp,
  618. Info->IpCnt,
  619. SrvList
  620. );
  621. }
  622. }
  623. if (!EFI_ERROR (Status)) {
  624. //
  625. // Parse the cached PXE reply packet, and store it into mode data if valid.
  626. //
  627. if (Mode->UsingIpv6) {
  628. Status = PxeBcParseDhcp6Packet (&Private->PxeReply.Dhcp6);
  629. if (!EFI_ERROR (Status)) {
  630. CopyMem (
  631. &Mode->PxeReply.Dhcpv6,
  632. &Private->PxeReply.Dhcp6.Packet.Ack.Dhcp6,
  633. Private->PxeReply.Dhcp6.Packet.Ack.Length
  634. );
  635. Mode->PxeReplyReceived = TRUE;
  636. Mode->PxeDiscoverValid = TRUE;
  637. }
  638. } else {
  639. Status = PxeBcParseDhcp4Packet (&Private->PxeReply.Dhcp4);
  640. if (!EFI_ERROR (Status)) {
  641. CopyMem (
  642. &Mode->PxeReply.Dhcpv4,
  643. &Private->PxeReply.Dhcp4.Packet.Ack.Dhcp4,
  644. Private->PxeReply.Dhcp4.Packet.Ack.Length
  645. );
  646. Mode->PxeReplyReceived = TRUE;
  647. Mode->PxeDiscoverValid = TRUE;
  648. }
  649. }
  650. }
  651. ON_EXIT:
  652. if ((NewCreatedInfo != NULL) && (NewCreatedInfo != &DefaultInfo)) {
  653. FreePool (NewCreatedInfo);
  654. }
  655. if (Mode->UsingIpv6) {
  656. Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
  657. } else {
  658. Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
  659. }
  660. //
  661. // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
  662. // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
  663. //
  664. ZeroMem (&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));
  665. IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;
  666. This->SetIpFilter (This, &IpFilter);
  667. return Status;
  668. }
  669. /**
  670. Used to perform TFTP and MTFTP services.
  671. This function is used to perform TFTP and MTFTP services. This includes the
  672. TFTP operations to get the size of a file, read a directory, read a file, and
  673. write a file. It also includes the MTFTP operations to get the size of a file,
  674. read a directory, and read a file. The type of operation is specified by Operation.
  675. If the callback function that is invoked during the TFTP/MTFTP operation does
  676. not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will
  677. be returned.
  678. For read operations, the return data will be placed in the buffer specified by
  679. BufferPtr. If BufferSize is too small to contain the entire downloaded file,
  680. then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero,
  681. or the size of the requested file. (NOTE: the size of the requested file is only returned
  682. if the TFTP server supports TFTP options). If BufferSize is large enough for the
  683. read operation, then BufferSize will be set to the size of the downloaded file,
  684. and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services
  685. should use the get-file-size operations to determine the size of the downloaded
  686. file prior to using the read-file operations-especially when downloading large
  687. (greater than 64 MB) files-instead of making two calls to the read-file operation.
  688. Following this recommendation will save time if the file is larger than expected
  689. and the TFTP server does not support TFTP option extensions. Without TFTP option
  690. extension support, the client must download the entire file, counting and discarding
  691. the received packets, to determine the file size.
  692. For write operations, the data to be sent is in the buffer specified by BufferPtr.
  693. BufferSize specifies the number of bytes to send. If the write operation completes
  694. successfully, then EFI_SUCCESS will be returned.
  695. For TFTP "get file size" operations, the size of the requested file or directory
  696. is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server
  697. does not support options, the file will be downloaded into a bit bucket and the
  698. length of the downloaded file will be returned. For MTFTP "get file size" operations,
  699. if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED
  700. will be returned.
  701. This function can take up to 10 seconds to timeout and return control to the caller.
  702. If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.
  703. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
  704. then the TFTP sequence is stopped and EFI_ABORTED will be returned.
  705. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  706. @param[in] Operation The type of operation to perform.
  707. @param[in, out] BufferPtr A pointer to the data buffer.
  708. @param[in] Overwrite Only used on write file operations. TRUE if a file on a remote
  709. server can be overwritten.
  710. @param[in, out] BufferSize For get-file-size operations, *BufferSize returns the size of the
  711. requested file.
  712. @param[in] BlockSize The requested block size to be used during a TFTP transfer.
  713. @param[in] ServerIp The TFTP / MTFTP server IP address.
  714. @param[in] Filename A Null-terminated ASCII string that specifies a directory name
  715. or a file name.
  716. @param[in] Info Pointer to the MTFTP information.
  717. @param[in] DontUseBuffer Set to FALSE for normal TFTP and MTFTP read file operation.
  718. @retval EFI_SUCCESS The TFTP/MTFTP operation was completed.
  719. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  720. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  721. @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
  722. @retval EFI_BUFFER_TOO_SMALL The buffer is not large enough to complete the read operation.
  723. @retval EFI_ABORTED The callback function aborted the TFTP/MTFTP operation.
  724. @retval EFI_TIMEOUT The TFTP/MTFTP operation timed out.
  725. @retval EFI_ICMP_ERROR An ICMP error packet was received during the MTFTP session.
  726. @retval EFI_TFTP_ERROR A TFTP error packet was received during the MTFTP session.
  727. **/
  728. EFI_STATUS
  729. EFIAPI
  730. EfiPxeBcMtftp (
  731. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  732. IN EFI_PXE_BASE_CODE_TFTP_OPCODE Operation,
  733. IN OUT VOID *BufferPtr OPTIONAL,
  734. IN BOOLEAN Overwrite,
  735. IN OUT UINT64 *BufferSize,
  736. IN UINTN *BlockSize OPTIONAL,
  737. IN EFI_IP_ADDRESS *ServerIp,
  738. IN UINT8 *Filename,
  739. IN EFI_PXE_BASE_CODE_MTFTP_INFO *Info OPTIONAL,
  740. IN BOOLEAN DontUseBuffer
  741. )
  742. {
  743. PXEBC_PRIVATE_DATA *Private;
  744. EFI_PXE_BASE_CODE_MODE *Mode;
  745. EFI_MTFTP4_CONFIG_DATA Mtftp4Config;
  746. EFI_MTFTP6_CONFIG_DATA Mtftp6Config;
  747. VOID *Config;
  748. EFI_STATUS Status;
  749. EFI_PXE_BASE_CODE_IP_FILTER IpFilter;
  750. UINTN WindowSize;
  751. if ((This == NULL) ||
  752. (Filename == NULL) ||
  753. (BufferSize == NULL) ||
  754. (ServerIp == NULL) ||
  755. ((BlockSize != NULL) && (*BlockSize < PXE_MTFTP_DEFAULT_BLOCK_SIZE)))
  756. {
  757. return EFI_INVALID_PARAMETER;
  758. }
  759. if ((Operation == EFI_PXE_BASE_CODE_TFTP_READ_FILE) ||
  760. (Operation == EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY) ||
  761. (Operation == EFI_PXE_BASE_CODE_MTFTP_READ_FILE) ||
  762. (Operation == EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY))
  763. {
  764. if ((BufferPtr == NULL) && !DontUseBuffer) {
  765. return EFI_INVALID_PARAMETER;
  766. }
  767. }
  768. Config = NULL;
  769. Status = EFI_DEVICE_ERROR;
  770. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  771. Mode = Private->PxeBc.Mode;
  772. //
  773. // Get PcdPxeTftpWindowSize.
  774. //
  775. WindowSize = (UINTN)PcdGet64 (PcdPxeTftpWindowSize);
  776. if (Mode->UsingIpv6) {
  777. if (!NetIp6IsValidUnicast (&ServerIp->v6)) {
  778. return EFI_INVALID_PARAMETER;
  779. }
  780. } else {
  781. if (IP4_IS_UNSPECIFIED (NTOHL (ServerIp->Addr[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (ServerIp->Addr[0]))) {
  782. return EFI_INVALID_PARAMETER;
  783. }
  784. }
  785. if (Mode->UsingIpv6) {
  786. //
  787. // Set configuration data for Mtftp6 instance.
  788. //
  789. ZeroMem (&Mtftp6Config, sizeof (EFI_MTFTP6_CONFIG_DATA));
  790. Config = &Mtftp6Config;
  791. Mtftp6Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;
  792. Mtftp6Config.TryCount = PXEBC_MTFTP_RETRIES;
  793. CopyMem (&Mtftp6Config.StationIp, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));
  794. CopyMem (&Mtftp6Config.ServerIp, &ServerIp->v6, sizeof (EFI_IPv6_ADDRESS));
  795. //
  796. // Stop Udp6Read instance
  797. //
  798. Private->Udp6Read->Configure (Private->Udp6Read, NULL);
  799. } else {
  800. //
  801. // Set configuration data for Mtftp4 instance.
  802. //
  803. ZeroMem (&Mtftp4Config, sizeof (EFI_MTFTP4_CONFIG_DATA));
  804. Config = &Mtftp4Config;
  805. Mtftp4Config.UseDefaultSetting = FALSE;
  806. Mtftp4Config.TimeoutValue = PXEBC_MTFTP_TIMEOUT;
  807. Mtftp4Config.TryCount = PXEBC_MTFTP_RETRIES;
  808. CopyMem (&Mtftp4Config.StationIp, &Private->StationIp.v4, sizeof (EFI_IPv4_ADDRESS));
  809. CopyMem (&Mtftp4Config.SubnetMask, &Private->SubnetMask.v4, sizeof (EFI_IPv4_ADDRESS));
  810. CopyMem (&Mtftp4Config.GatewayIp, &Private->GatewayIp.v4, sizeof (EFI_IPv4_ADDRESS));
  811. CopyMem (&Mtftp4Config.ServerIp, &ServerIp->v4, sizeof (EFI_IPv4_ADDRESS));
  812. //
  813. // Stop Udp4Read instance
  814. //
  815. Private->Udp4Read->Configure (Private->Udp4Read, NULL);
  816. }
  817. Mode->TftpErrorReceived = FALSE;
  818. Mode->IcmpErrorReceived = FALSE;
  819. switch (Operation) {
  820. case EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE:
  821. //
  822. // Send TFTP request to get file size.
  823. //
  824. Status = PxeBcTftpGetFileSize (
  825. Private,
  826. Config,
  827. Filename,
  828. BlockSize,
  829. (WindowSize > 1) ? &WindowSize : NULL,
  830. BufferSize
  831. );
  832. break;
  833. case EFI_PXE_BASE_CODE_TFTP_READ_FILE:
  834. //
  835. // Send TFTP request to read file.
  836. //
  837. Status = PxeBcTftpReadFile (
  838. Private,
  839. Config,
  840. Filename,
  841. BlockSize,
  842. (WindowSize > 1) ? &WindowSize : NULL,
  843. BufferPtr,
  844. BufferSize,
  845. DontUseBuffer
  846. );
  847. break;
  848. case EFI_PXE_BASE_CODE_TFTP_WRITE_FILE:
  849. //
  850. // Send TFTP request to write file.
  851. //
  852. Status = PxeBcTftpWriteFile (
  853. Private,
  854. Config,
  855. Filename,
  856. Overwrite,
  857. BlockSize,
  858. BufferPtr,
  859. BufferSize
  860. );
  861. break;
  862. case EFI_PXE_BASE_CODE_TFTP_READ_DIRECTORY:
  863. //
  864. // Send TFTP request to read directory.
  865. //
  866. Status = PxeBcTftpReadDirectory (
  867. Private,
  868. Config,
  869. Filename,
  870. BlockSize,
  871. (WindowSize > 1) ? &WindowSize : NULL,
  872. BufferPtr,
  873. BufferSize,
  874. DontUseBuffer
  875. );
  876. break;
  877. case EFI_PXE_BASE_CODE_MTFTP_GET_FILE_SIZE:
  878. case EFI_PXE_BASE_CODE_MTFTP_READ_FILE:
  879. case EFI_PXE_BASE_CODE_MTFTP_READ_DIRECTORY:
  880. Status = EFI_UNSUPPORTED;
  881. break;
  882. default:
  883. Status = EFI_INVALID_PARAMETER;
  884. break;
  885. }
  886. if (Status == EFI_ICMP_ERROR) {
  887. Mode->IcmpErrorReceived = TRUE;
  888. }
  889. //
  890. // Reconfigure the UDP instance with the default configuration.
  891. //
  892. if (Mode->UsingIpv6) {
  893. Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);
  894. } else {
  895. Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);
  896. }
  897. //
  898. // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP
  899. // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
  900. //
  901. ZeroMem (&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));
  902. IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;
  903. This->SetIpFilter (This, &IpFilter);
  904. return Status;
  905. }
  906. /**
  907. Writes a UDP packet to the network interface.
  908. This function writes a UDP packet specified by the (optional HeaderPtr and)
  909. BufferPtr parameters to the network interface. The UDP header is automatically
  910. built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,
  911. SrcIp, and SrcPort to build this header. If the packet is successfully built and
  912. transmitted through the network interface, then EFI_SUCCESS will be returned.
  913. If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will
  914. be returned. If an ICMP error occurs during the transmission of the packet, then
  915. the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and
  916. EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return
  917. EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.
  918. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  919. @param[in] OpFlags The UDP operation flags.
  920. @param[in] DestIp The destination IP address.
  921. @param[in] DestPort The destination UDP port number.
  922. @param[in] GatewayIp The gateway IP address.
  923. @param[in] SrcIp The source IP address.
  924. @param[in, out] SrcPort The source UDP port number.
  925. @param[in] HeaderSize An optional field which may be set to the length of a header
  926. at HeaderPtr to be prefixed to the data at BufferPtr.
  927. @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
  928. prefixed to the data at BufferPtr.
  929. @param[in] BufferSize A pointer to the size of the data at BufferPtr.
  930. @param[in] BufferPtr A pointer to the data to be written.
  931. @retval EFI_SUCCESS The UDP Write operation completed.
  932. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  933. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  934. @retval EFI_BAD_BUFFER_SIZE The buffer is too long to be transmitted.
  935. @retval EFI_ABORTED The callback function aborted the UDP Write operation.
  936. @retval EFI_TIMEOUT The UDP Write operation timed out.
  937. @retval EFI_ICMP_ERROR An ICMP error packet was received during the UDP write session.
  938. **/
  939. EFI_STATUS
  940. EFIAPI
  941. EfiPxeBcUdpWrite (
  942. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  943. IN UINT16 OpFlags,
  944. IN EFI_IP_ADDRESS *DestIp,
  945. IN EFI_PXE_BASE_CODE_UDP_PORT *DestPort,
  946. IN EFI_IP_ADDRESS *GatewayIp OPTIONAL,
  947. IN EFI_IP_ADDRESS *SrcIp OPTIONAL,
  948. IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
  949. IN UINTN *HeaderSize OPTIONAL,
  950. IN VOID *HeaderPtr OPTIONAL,
  951. IN UINTN *BufferSize,
  952. IN VOID *BufferPtr
  953. )
  954. {
  955. PXEBC_PRIVATE_DATA *Private;
  956. EFI_PXE_BASE_CODE_MODE *Mode;
  957. EFI_UDP4_SESSION_DATA Udp4Session;
  958. EFI_UDP6_SESSION_DATA Udp6Session;
  959. EFI_STATUS Status;
  960. BOOLEAN DoNotFragment;
  961. if ((This == NULL) || (DestIp == NULL) || (DestPort == NULL)) {
  962. return EFI_INVALID_PARAMETER;
  963. }
  964. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  965. Mode = Private->PxeBc.Mode;
  966. if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_MAY_FRAGMENT) != 0) {
  967. DoNotFragment = FALSE;
  968. } else {
  969. DoNotFragment = TRUE;
  970. }
  971. if (!Mode->UsingIpv6 && (GatewayIp != NULL) && (Mode->SubnetMask.Addr[0] != 0) &&
  972. !NetIp4IsUnicast (NTOHL (GatewayIp->Addr[0]), EFI_NTOHL (Mode->SubnetMask)))
  973. {
  974. //
  975. // Gateway is provided but it's not a unicast IPv4 address, while it will be ignored for IPv6.
  976. //
  977. return EFI_INVALID_PARAMETER;
  978. }
  979. if ((HeaderSize != NULL) && ((*HeaderSize == 0) || (HeaderPtr == NULL))) {
  980. return EFI_INVALID_PARAMETER;
  981. }
  982. if ((BufferSize == NULL) || ((*BufferSize != 0) && (BufferPtr == NULL))) {
  983. return EFI_INVALID_PARAMETER;
  984. }
  985. if (!Mode->Started) {
  986. return EFI_NOT_STARTED;
  987. }
  988. if (!Private->IsAddressOk && (SrcIp == NULL)) {
  989. return EFI_INVALID_PARAMETER;
  990. }
  991. if ((Private->CurSrcPort == 0) ||
  992. ((SrcPort != NULL) && (*SrcPort != Private->CurSrcPort)))
  993. {
  994. //
  995. // Reconfigure UDPv4/UDPv6 for UdpWrite if the source port changed.
  996. //
  997. if (SrcPort != NULL) {
  998. Private->CurSrcPort = *SrcPort;
  999. }
  1000. }
  1001. if (Mode->UsingIpv6) {
  1002. Status = PxeBcConfigUdp6Write (
  1003. Private->Udp6Write,
  1004. &Private->StationIp.v6,
  1005. &Private->CurSrcPort
  1006. );
  1007. } else {
  1008. //
  1009. // Configure the UDPv4 instance with gateway information from DHCP server as default.
  1010. //
  1011. Status = PxeBcConfigUdp4Write (
  1012. Private->Udp4Write,
  1013. &Private->StationIp.v4,
  1014. &Private->SubnetMask.v4,
  1015. &Private->GatewayIp.v4,
  1016. &Private->CurSrcPort,
  1017. DoNotFragment,
  1018. Private->Mode.TTL,
  1019. Private->Mode.ToS
  1020. );
  1021. }
  1022. if (EFI_ERROR (Status)) {
  1023. Private->CurSrcPort = 0;
  1024. return EFI_INVALID_PARAMETER;
  1025. } else if (SrcPort != NULL) {
  1026. *SrcPort = Private->CurSrcPort;
  1027. }
  1028. //
  1029. // Start a timer as timeout event for this blocking API.
  1030. //
  1031. gBS->SetTimer (Private->UdpTimeOutEvent, TimerRelative, PXEBC_UDP_TIMEOUT);
  1032. if (Mode->UsingIpv6) {
  1033. //
  1034. // Construct UDPv6 session data.
  1035. //
  1036. ZeroMem (&Udp6Session, sizeof (EFI_UDP6_SESSION_DATA));
  1037. CopyMem (&Udp6Session.DestinationAddress, DestIp, sizeof (EFI_IPv6_ADDRESS));
  1038. Udp6Session.DestinationPort = *DestPort;
  1039. if (SrcIp != NULL) {
  1040. CopyMem (&Udp6Session.SourceAddress, SrcIp, sizeof (EFI_IPv6_ADDRESS));
  1041. }
  1042. if (SrcPort != NULL) {
  1043. Udp6Session.SourcePort = *SrcPort;
  1044. }
  1045. Status = PxeBcUdp6Write (
  1046. Private->Udp6Write,
  1047. &Udp6Session,
  1048. Private->UdpTimeOutEvent,
  1049. HeaderSize,
  1050. HeaderPtr,
  1051. BufferSize,
  1052. BufferPtr
  1053. );
  1054. } else {
  1055. //
  1056. // Construct UDPv4 session data.
  1057. //
  1058. ZeroMem (&Udp4Session, sizeof (EFI_UDP4_SESSION_DATA));
  1059. CopyMem (&Udp4Session.DestinationAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));
  1060. Udp4Session.DestinationPort = *DestPort;
  1061. if (SrcIp != NULL) {
  1062. CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS));
  1063. }
  1064. if (SrcPort != NULL) {
  1065. Udp4Session.SourcePort = *SrcPort;
  1066. }
  1067. //
  1068. // Override the gateway information if user specified.
  1069. //
  1070. Status = PxeBcUdp4Write (
  1071. Private->Udp4Write,
  1072. &Udp4Session,
  1073. Private->UdpTimeOutEvent,
  1074. (EFI_IPv4_ADDRESS *)GatewayIp,
  1075. HeaderSize,
  1076. HeaderPtr,
  1077. BufferSize,
  1078. BufferPtr
  1079. );
  1080. }
  1081. gBS->SetTimer (Private->UdpTimeOutEvent, TimerCancel, 0);
  1082. //
  1083. // Reset the UdpWrite instance.
  1084. //
  1085. if (Mode->UsingIpv6) {
  1086. Private->Udp6Write->Configure (Private->Udp6Write, NULL);
  1087. } else {
  1088. Private->Udp4Write->Configure (Private->Udp4Write, NULL);
  1089. }
  1090. return Status;
  1091. }
  1092. /**
  1093. Reads a UDP packet from the network interface.
  1094. +
  1095. This function reads a UDP packet from a network interface. The data contents
  1096. are returned in (the optional HeaderPtr and) BufferPtr, and the size of the
  1097. buffer received is returned in BufferSize . If the input BufferSize is smaller
  1098. than the UDP packet received (less optional HeaderSize), it will be set to the
  1099. required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the
  1100. contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is
  1101. successfully received, then EFI_SUCCESS will be returned, and the information
  1102. from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if
  1103. they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,
  1104. SrcIp, and SrcPort input values, different types of UDP packet receive filtering
  1105. will be performed. The following tables summarize these receive filter operations.
  1106. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  1107. @param[in] OpFlags The UDP operation flags.
  1108. @param[in, out] DestIp The destination IP address.
  1109. @param[in, out] DestPort The destination UDP port number.
  1110. @param[in, out] SrcIp The source IP address.
  1111. @param[in, out] SrcPort The source UDP port number.
  1112. @param[in] HeaderSize An optional field which may be set to the length of a
  1113. header at HeaderPtr to be prefixed to the data at BufferPtr.
  1114. @param[in] HeaderPtr If HeaderSize is not NULL, a pointer to a header to be
  1115. prefixed to the data at BufferPtr.
  1116. @param[in, out] BufferSize A pointer to the size of the data at BufferPtr.
  1117. @param[in] BufferPtr A pointer to the data to be read.
  1118. @retval EFI_SUCCESS The UDP Read operation was completed.
  1119. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  1120. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  1121. @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
  1122. @retval EFI_BUFFER_TOO_SMALL The packet is larger than Buffer can hold.
  1123. @retval EFI_ABORTED The callback function aborted the UDP Read operation.
  1124. @retval EFI_TIMEOUT The UDP Read operation timed out.
  1125. **/
  1126. EFI_STATUS
  1127. EFIAPI
  1128. EfiPxeBcUdpRead (
  1129. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  1130. IN UINT16 OpFlags,
  1131. IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL,
  1132. IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL,
  1133. IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL,
  1134. IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
  1135. IN UINTN *HeaderSize OPTIONAL,
  1136. IN VOID *HeaderPtr OPTIONAL,
  1137. IN OUT UINTN *BufferSize,
  1138. IN VOID *BufferPtr
  1139. )
  1140. {
  1141. PXEBC_PRIVATE_DATA *Private;
  1142. EFI_PXE_BASE_CODE_MODE *Mode;
  1143. EFI_UDP4_COMPLETION_TOKEN Udp4Token;
  1144. EFI_UDP6_COMPLETION_TOKEN Udp6Token;
  1145. EFI_UDP4_RECEIVE_DATA *Udp4Rx;
  1146. EFI_UDP6_RECEIVE_DATA *Udp6Rx;
  1147. EFI_STATUS Status;
  1148. BOOLEAN IsDone;
  1149. BOOLEAN IsMatched;
  1150. UINTN CopiedLen;
  1151. UINTN HeaderLen;
  1152. UINTN HeaderCopiedLen;
  1153. UINTN BufferCopiedLen;
  1154. UINT32 FragmentLength;
  1155. UINTN FragmentIndex;
  1156. UINT8 *FragmentBuffer;
  1157. if (This == NULL) {
  1158. return EFI_INVALID_PARAMETER;
  1159. }
  1160. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  1161. Mode = Private->PxeBc.Mode;
  1162. IsDone = FALSE;
  1163. IsMatched = FALSE;
  1164. Udp4Rx = NULL;
  1165. Udp6Rx = NULL;
  1166. if ((((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0) && (DestPort == NULL)) ||
  1167. (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) == 0) && (SrcIp == NULL)) ||
  1168. (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) == 0) && (SrcPort == NULL)))
  1169. {
  1170. return EFI_INVALID_PARAMETER;
  1171. }
  1172. if (((HeaderSize != NULL) && (*HeaderSize == 0)) || ((HeaderSize != NULL) && (HeaderPtr == NULL))) {
  1173. return EFI_INVALID_PARAMETER;
  1174. }
  1175. if ((BufferSize == NULL) || (BufferPtr == NULL)) {
  1176. return EFI_INVALID_PARAMETER;
  1177. }
  1178. if (!Mode->Started) {
  1179. return EFI_NOT_STARTED;
  1180. }
  1181. ZeroMem (&Udp6Token, sizeof (EFI_UDP6_COMPLETION_TOKEN));
  1182. ZeroMem (&Udp4Token, sizeof (EFI_UDP4_COMPLETION_TOKEN));
  1183. if (Mode->UsingIpv6) {
  1184. Status = gBS->CreateEvent (
  1185. EVT_NOTIFY_SIGNAL,
  1186. TPL_NOTIFY,
  1187. PxeBcCommonNotify,
  1188. &IsDone,
  1189. &Udp6Token.Event
  1190. );
  1191. if (EFI_ERROR (Status)) {
  1192. return EFI_OUT_OF_RESOURCES;
  1193. }
  1194. } else {
  1195. Status = gBS->CreateEvent (
  1196. EVT_NOTIFY_SIGNAL,
  1197. TPL_NOTIFY,
  1198. PxeBcCommonNotify,
  1199. &IsDone,
  1200. &Udp4Token.Event
  1201. );
  1202. if (EFI_ERROR (Status)) {
  1203. return EFI_OUT_OF_RESOURCES;
  1204. }
  1205. }
  1206. //
  1207. // Start a timer as timeout event for this blocking API.
  1208. //
  1209. gBS->SetTimer (Private->UdpTimeOutEvent, TimerRelative, PXEBC_UDP_TIMEOUT);
  1210. Mode->IcmpErrorReceived = FALSE;
  1211. //
  1212. // Read packet by Udp4Read/Udp6Read until matched or timeout.
  1213. //
  1214. while (!IsMatched && !EFI_ERROR (Status)) {
  1215. if (Mode->UsingIpv6) {
  1216. Status = PxeBcUdp6Read (
  1217. Private->Udp6Read,
  1218. &Udp6Token,
  1219. Mode,
  1220. Private->UdpTimeOutEvent,
  1221. OpFlags,
  1222. &IsDone,
  1223. &IsMatched,
  1224. DestIp,
  1225. DestPort,
  1226. SrcIp,
  1227. SrcPort
  1228. );
  1229. } else {
  1230. Status = PxeBcUdp4Read (
  1231. Private->Udp4Read,
  1232. &Udp4Token,
  1233. Mode,
  1234. Private->UdpTimeOutEvent,
  1235. OpFlags,
  1236. &IsDone,
  1237. &IsMatched,
  1238. DestIp,
  1239. DestPort,
  1240. SrcIp,
  1241. SrcPort
  1242. );
  1243. }
  1244. }
  1245. if ((Status == EFI_ICMP_ERROR) ||
  1246. (Status == EFI_NETWORK_UNREACHABLE) ||
  1247. (Status == EFI_HOST_UNREACHABLE) ||
  1248. (Status == EFI_PROTOCOL_UNREACHABLE) ||
  1249. (Status == EFI_PORT_UNREACHABLE))
  1250. {
  1251. //
  1252. // Get different return status for icmp error from Udp, refers to UEFI spec.
  1253. //
  1254. Mode->IcmpErrorReceived = TRUE;
  1255. }
  1256. gBS->SetTimer (Private->UdpTimeOutEvent, TimerCancel, 0);
  1257. if (IsMatched) {
  1258. //
  1259. // Copy the received packet to user if matched by filter.
  1260. //
  1261. if (Mode->UsingIpv6) {
  1262. Udp6Rx = Udp6Token.Packet.RxData;
  1263. ASSERT (Udp6Rx != NULL);
  1264. HeaderLen = 0;
  1265. if (HeaderSize != NULL) {
  1266. HeaderLen = MIN (*HeaderSize, Udp6Rx->DataLength);
  1267. }
  1268. if (Udp6Rx->DataLength - HeaderLen > *BufferSize) {
  1269. Status = EFI_BUFFER_TOO_SMALL;
  1270. } else {
  1271. if (HeaderSize != NULL) {
  1272. *HeaderSize = HeaderLen;
  1273. }
  1274. *BufferSize = Udp6Rx->DataLength - HeaderLen;
  1275. HeaderCopiedLen = 0;
  1276. BufferCopiedLen = 0;
  1277. for (FragmentIndex = 0; FragmentIndex < Udp6Rx->FragmentCount; FragmentIndex++) {
  1278. FragmentLength = Udp6Rx->FragmentTable[FragmentIndex].FragmentLength;
  1279. FragmentBuffer = Udp6Rx->FragmentTable[FragmentIndex].FragmentBuffer;
  1280. if (HeaderCopiedLen + FragmentLength < HeaderLen) {
  1281. //
  1282. // Copy the header part of received data.
  1283. //
  1284. CopyMem ((UINT8 *)HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength);
  1285. HeaderCopiedLen += FragmentLength;
  1286. } else if (HeaderCopiedLen < HeaderLen) {
  1287. //
  1288. // Copy the header part of received data.
  1289. //
  1290. CopiedLen = HeaderLen - HeaderCopiedLen;
  1291. CopyMem ((UINT8 *)HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen);
  1292. HeaderCopiedLen += CopiedLen;
  1293. //
  1294. // Copy the other part of received data.
  1295. //
  1296. CopyMem ((UINT8 *)BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen);
  1297. BufferCopiedLen += (FragmentLength - CopiedLen);
  1298. } else {
  1299. //
  1300. // Copy the other part of received data.
  1301. //
  1302. CopyMem ((UINT8 *)BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength);
  1303. BufferCopiedLen += FragmentLength;
  1304. }
  1305. }
  1306. }
  1307. //
  1308. // Recycle the receiving buffer after copy to user.
  1309. //
  1310. gBS->SignalEvent (Udp6Rx->RecycleSignal);
  1311. } else {
  1312. Udp4Rx = Udp4Token.Packet.RxData;
  1313. ASSERT (Udp4Rx != NULL);
  1314. HeaderLen = 0;
  1315. if (HeaderSize != NULL) {
  1316. HeaderLen = MIN (*HeaderSize, Udp4Rx->DataLength);
  1317. }
  1318. if (Udp4Rx->DataLength - HeaderLen > *BufferSize) {
  1319. Status = EFI_BUFFER_TOO_SMALL;
  1320. } else {
  1321. if (HeaderSize != NULL) {
  1322. *HeaderSize = HeaderLen;
  1323. }
  1324. *BufferSize = Udp4Rx->DataLength - HeaderLen;
  1325. HeaderCopiedLen = 0;
  1326. BufferCopiedLen = 0;
  1327. for (FragmentIndex = 0; FragmentIndex < Udp4Rx->FragmentCount; FragmentIndex++) {
  1328. FragmentLength = Udp4Rx->FragmentTable[FragmentIndex].FragmentLength;
  1329. FragmentBuffer = Udp4Rx->FragmentTable[FragmentIndex].FragmentBuffer;
  1330. if (HeaderCopiedLen + FragmentLength < HeaderLen) {
  1331. //
  1332. // Copy the header part of received data.
  1333. //
  1334. CopyMem ((UINT8 *)HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength);
  1335. HeaderCopiedLen += FragmentLength;
  1336. } else if (HeaderCopiedLen < HeaderLen) {
  1337. //
  1338. // Copy the header part of received data.
  1339. //
  1340. CopiedLen = HeaderLen - HeaderCopiedLen;
  1341. CopyMem ((UINT8 *)HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen);
  1342. HeaderCopiedLen += CopiedLen;
  1343. //
  1344. // Copy the other part of received data.
  1345. //
  1346. CopyMem ((UINT8 *)BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen);
  1347. BufferCopiedLen += (FragmentLength - CopiedLen);
  1348. } else {
  1349. //
  1350. // Copy the other part of received data.
  1351. //
  1352. CopyMem ((UINT8 *)BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength);
  1353. BufferCopiedLen += FragmentLength;
  1354. }
  1355. }
  1356. }
  1357. //
  1358. // Recycle the receiving buffer after copy to user.
  1359. //
  1360. gBS->SignalEvent (Udp4Rx->RecycleSignal);
  1361. }
  1362. }
  1363. if (Mode->UsingIpv6) {
  1364. Private->Udp6Read->Cancel (Private->Udp6Read, &Udp6Token);
  1365. gBS->CloseEvent (Udp6Token.Event);
  1366. } else {
  1367. Private->Udp4Read->Cancel (Private->Udp4Read, &Udp4Token);
  1368. gBS->CloseEvent (Udp4Token.Event);
  1369. }
  1370. return Status;
  1371. }
  1372. /**
  1373. Updates the IP receive filters of a network device and enables software filtering.
  1374. The NewFilter field is used to modify the network device's current IP receive
  1375. filter settings and to enable a software filter. This function updates the IpFilter
  1376. field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.
  1377. The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().
  1378. The current hardware filter remains in effect no matter what the settings of OpFlags.
  1379. This is so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those
  1380. packets whose reception is enabled in hardware-physical NIC address (unicast),
  1381. broadcast address, logical address or addresses (multicast), or all (promiscuous).
  1382. UdpRead() does not modify the IP filter settings.
  1383. Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive
  1384. filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.
  1385. If an application or driver wishes to preserve the IP receive filter settings,
  1386. it will have to preserve the IP receive filter settings before these calls, and
  1387. use SetIpFilter() to restore them after the calls. If incompatible filtering is
  1388. requested (for example, PROMISCUOUS with anything else), or if the device does not
  1389. support a requested filter setting and it cannot be accommodated in software
  1390. (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.
  1391. The IPlist field is used to enable IPs other than the StationIP. They may be
  1392. multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,
  1393. then both the StationIP and the IPs from the IPlist will be used.
  1394. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  1395. @param[in] NewFilter Pointer to the new set of IP receive filters.
  1396. @retval EFI_SUCCESS The IP receive filter settings were updated.
  1397. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  1398. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  1399. **/
  1400. EFI_STATUS
  1401. EFIAPI
  1402. EfiPxeBcSetIpFilter (
  1403. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  1404. IN EFI_PXE_BASE_CODE_IP_FILTER *NewFilter
  1405. )
  1406. {
  1407. EFI_STATUS Status;
  1408. PXEBC_PRIVATE_DATA *Private;
  1409. EFI_PXE_BASE_CODE_MODE *Mode;
  1410. EFI_UDP4_CONFIG_DATA *Udp4Cfg;
  1411. EFI_UDP6_CONFIG_DATA *Udp6Cfg;
  1412. UINTN Index;
  1413. BOOLEAN NeedPromiscuous;
  1414. BOOLEAN AcceptPromiscuous;
  1415. BOOLEAN AcceptBroadcast;
  1416. BOOLEAN MultiCastUpdate;
  1417. if ((This == NULL) || (NewFilter == NULL)) {
  1418. return EFI_INVALID_PARAMETER;
  1419. }
  1420. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  1421. Mode = Private->PxeBc.Mode;
  1422. Status = EFI_SUCCESS;
  1423. NeedPromiscuous = FALSE;
  1424. if (!Mode->Started) {
  1425. return EFI_NOT_STARTED;
  1426. }
  1427. for (Index = 0; Index < NewFilter->IpCnt; Index++) {
  1428. ASSERT (Index < EFI_PXE_BASE_CODE_MAX_IPCNT);
  1429. if (!Mode->UsingIpv6 &&
  1430. IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter->IpList[Index].v4)))
  1431. {
  1432. //
  1433. // IPv4 broadcast address should not be in IP filter.
  1434. //
  1435. return EFI_INVALID_PARAMETER;
  1436. }
  1437. if (Mode->UsingIpv6) {
  1438. if (((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) &&
  1439. NetIp6IsValidUnicast (&NewFilter->IpList[Index].v6))
  1440. {
  1441. NeedPromiscuous = TRUE;
  1442. }
  1443. } else if ((EFI_NTOHL (Mode->StationIp) != 0) &&
  1444. (EFI_NTOHL (Mode->SubnetMask) != 0) &&
  1445. IP4_NET_EQUAL (EFI_NTOHL (Mode->StationIp), EFI_NTOHL (NewFilter->IpList[Index].v4), EFI_NTOHL (Mode->SubnetMask.v4)) &&
  1446. NetIp4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), EFI_NTOHL (Mode->SubnetMask)) &&
  1447. ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0))
  1448. {
  1449. NeedPromiscuous = TRUE;
  1450. }
  1451. }
  1452. AcceptPromiscuous = FALSE;
  1453. AcceptBroadcast = FALSE;
  1454. MultiCastUpdate = FALSE;
  1455. if (NeedPromiscuous ||
  1456. ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) ||
  1457. ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0))
  1458. {
  1459. //
  1460. // Configure UDPv4/UDPv6 as promiscuous mode to receive all packets.
  1461. //
  1462. AcceptPromiscuous = TRUE;
  1463. } else if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) {
  1464. //
  1465. // Configure UDPv4 to receive all broadcast packets.
  1466. //
  1467. AcceptBroadcast = TRUE;
  1468. }
  1469. //
  1470. // In multicast condition when Promiscuous FALSE and IpCnt no-zero.
  1471. // Here check if there is any update of the multicast ip address. If yes,
  1472. // we need leave the old multicast group (by Config UDP instance to NULL),
  1473. // and join the new multicast group.
  1474. //
  1475. if (!AcceptPromiscuous) {
  1476. if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) {
  1477. if (Mode->IpFilter.IpCnt != NewFilter->IpCnt) {
  1478. MultiCastUpdate = TRUE;
  1479. } else if (CompareMem (Mode->IpFilter.IpList, NewFilter->IpList, NewFilter->IpCnt * sizeof (EFI_IP_ADDRESS)) != 0 ) {
  1480. MultiCastUpdate = TRUE;
  1481. }
  1482. }
  1483. }
  1484. if (!Mode->UsingIpv6) {
  1485. //
  1486. // Check whether we need reconfigure the UDP4 instance.
  1487. //
  1488. Udp4Cfg = &Private->Udp4CfgData;
  1489. if ((AcceptPromiscuous != Udp4Cfg->AcceptPromiscuous) ||
  1490. (AcceptBroadcast != Udp4Cfg->AcceptBroadcast) || MultiCastUpdate)
  1491. {
  1492. //
  1493. // Clear the UDP4 instance configuration, all joined groups will be left
  1494. // during the operation.
  1495. //
  1496. Private->Udp4Read->Configure (Private->Udp4Read, NULL);
  1497. //
  1498. // Configure the UDP instance with the new configuration.
  1499. //
  1500. Udp4Cfg->AcceptPromiscuous = AcceptPromiscuous;
  1501. Udp4Cfg->AcceptBroadcast = AcceptBroadcast;
  1502. Status = Private->Udp4Read->Configure (Private->Udp4Read, Udp4Cfg);
  1503. if (EFI_ERROR (Status)) {
  1504. return Status;
  1505. }
  1506. //
  1507. // In not Promiscuous mode, need to join the new multicast group.
  1508. //
  1509. if (!AcceptPromiscuous) {
  1510. for (Index = 0; Index < NewFilter->IpCnt; ++Index) {
  1511. if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {
  1512. //
  1513. // Join the multicast group.
  1514. //
  1515. Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);
  1516. if (EFI_ERROR (Status)) {
  1517. return Status;
  1518. }
  1519. }
  1520. }
  1521. }
  1522. }
  1523. } else {
  1524. //
  1525. // Check whether we need reconfigure the UDP6 instance.
  1526. //
  1527. Udp6Cfg = &Private->Udp6CfgData;
  1528. if ((AcceptPromiscuous != Udp6Cfg->AcceptPromiscuous) || MultiCastUpdate) {
  1529. //
  1530. // Clear the UDP6 instance configuration, all joined groups will be left
  1531. // during the operation.
  1532. //
  1533. Private->Udp6Read->Configure (Private->Udp6Read, NULL);
  1534. //
  1535. // Configure the UDP instance with the new configuration.
  1536. //
  1537. Udp6Cfg->AcceptPromiscuous = AcceptPromiscuous;
  1538. Status = Private->Udp6Read->Configure (Private->Udp6Read, Udp6Cfg);
  1539. if (EFI_ERROR (Status)) {
  1540. return Status;
  1541. }
  1542. //
  1543. // In not Promiscuous mode, need to join the new multicast group.
  1544. //
  1545. if (!AcceptPromiscuous) {
  1546. for (Index = 0; Index < NewFilter->IpCnt; ++Index) {
  1547. if (IP6_IS_MULTICAST (&NewFilter->IpList[Index].v6)) {
  1548. //
  1549. // Join the multicast group.
  1550. //
  1551. Status = Private->Udp6Read->Groups (Private->Udp6Read, TRUE, &NewFilter->IpList[Index].v6);
  1552. if (EFI_ERROR (Status)) {
  1553. return Status;
  1554. }
  1555. }
  1556. }
  1557. }
  1558. }
  1559. }
  1560. //
  1561. // Save the new IP filter into mode data.
  1562. //
  1563. CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter));
  1564. return Status;
  1565. }
  1566. /**
  1567. Uses the ARP protocol to resolve a MAC address. It is not supported for IPv6.
  1568. This function uses the ARP protocol to resolve a MAC address. The IP address specified
  1569. by IpAddr is used to resolve a MAC address. If the ARP protocol succeeds in resolving
  1570. the specified address, then the ArpCacheEntries and ArpCache fields of the mode data
  1571. are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved
  1572. MAC address is placed there as well. If the PXE Base Code protocol is in the
  1573. stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters
  1574. a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is
  1575. returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,
  1576. then EFI_ABORTED is returned.
  1577. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  1578. @param[in] IpAddr Pointer to the IP address that is used to resolve a MAC address.
  1579. @param[in] MacAddr If not NULL, a pointer to the MAC address that was resolved with the
  1580. ARP protocol.
  1581. @retval EFI_SUCCESS The IP or MAC address was resolved.
  1582. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  1583. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  1584. @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
  1585. @retval EFI_ICMP_ERROR An error occur with the ICMP packet message.
  1586. **/
  1587. EFI_STATUS
  1588. EFIAPI
  1589. EfiPxeBcArp (
  1590. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  1591. IN EFI_IP_ADDRESS *IpAddr,
  1592. IN EFI_MAC_ADDRESS *MacAddr OPTIONAL
  1593. )
  1594. {
  1595. PXEBC_PRIVATE_DATA *Private;
  1596. EFI_PXE_BASE_CODE_MODE *Mode;
  1597. EFI_EVENT ResolvedEvent;
  1598. EFI_STATUS Status;
  1599. EFI_MAC_ADDRESS TempMac;
  1600. EFI_MAC_ADDRESS ZeroMac;
  1601. BOOLEAN IsResolved;
  1602. if ((This == NULL) || (IpAddr == NULL)) {
  1603. return EFI_INVALID_PARAMETER;
  1604. }
  1605. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  1606. Mode = Private->PxeBc.Mode;
  1607. ResolvedEvent = NULL;
  1608. Status = EFI_SUCCESS;
  1609. IsResolved = FALSE;
  1610. if (!Mode->Started) {
  1611. return EFI_NOT_STARTED;
  1612. }
  1613. if (Mode->UsingIpv6) {
  1614. return EFI_UNSUPPORTED;
  1615. }
  1616. //
  1617. // Station address should be ready before do arp.
  1618. //
  1619. if (!Private->IsAddressOk) {
  1620. return EFI_INVALID_PARAMETER;
  1621. }
  1622. Mode->IcmpErrorReceived = FALSE;
  1623. ZeroMem (&TempMac, sizeof (EFI_MAC_ADDRESS));
  1624. ZeroMem (&ZeroMac, sizeof (EFI_MAC_ADDRESS));
  1625. if (!Mode->AutoArp) {
  1626. //
  1627. // If AutoArp is FALSE, only search in the current Arp cache.
  1628. //
  1629. PxeBcArpCacheUpdate (NULL, Private);
  1630. if (!PxeBcCheckArpCache (Mode, &IpAddr->v4, &TempMac)) {
  1631. Status = EFI_DEVICE_ERROR;
  1632. goto ON_EXIT;
  1633. }
  1634. } else {
  1635. Status = gBS->CreateEvent (
  1636. EVT_NOTIFY_SIGNAL,
  1637. TPL_NOTIFY,
  1638. PxeBcCommonNotify,
  1639. &IsResolved,
  1640. &ResolvedEvent
  1641. );
  1642. if (EFI_ERROR (Status)) {
  1643. goto ON_EXIT;
  1644. }
  1645. //
  1646. // If AutoArp is TRUE, try to send Arp request on initiative.
  1647. //
  1648. Status = Private->Arp->Request (Private->Arp, &IpAddr->v4, ResolvedEvent, &TempMac);
  1649. if (EFI_ERROR (Status) && (Status != EFI_NOT_READY)) {
  1650. goto ON_EXIT;
  1651. }
  1652. while (!IsResolved) {
  1653. if (CompareMem (&TempMac, &ZeroMac, sizeof (EFI_MAC_ADDRESS)) != 0) {
  1654. break;
  1655. }
  1656. }
  1657. if (CompareMem (&TempMac, &ZeroMac, sizeof (EFI_MAC_ADDRESS)) != 0) {
  1658. Status = EFI_SUCCESS;
  1659. } else {
  1660. Status = EFI_TIMEOUT;
  1661. }
  1662. }
  1663. //
  1664. // Copy the Mac address to user if needed.
  1665. //
  1666. if ((MacAddr != NULL) && !EFI_ERROR (Status)) {
  1667. CopyMem (MacAddr, &TempMac, sizeof (EFI_MAC_ADDRESS));
  1668. }
  1669. ON_EXIT:
  1670. if (ResolvedEvent != NULL) {
  1671. gBS->CloseEvent (ResolvedEvent);
  1672. }
  1673. return Status;
  1674. }
  1675. /**
  1676. Updates the parameters that affect the operation of the PXE Base Code Protocol.
  1677. This function sets parameters that affect the operation of the PXE Base Code Protocol.
  1678. The parameter specified by NewAutoArp is used to control the generation of ARP
  1679. protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated
  1680. as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP
  1681. Protocol packets will be generated. In this case, the only mappings that are
  1682. available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.
  1683. If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol
  1684. service, then the service will fail. This function updates the AutoArp field of
  1685. the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.
  1686. The SetParameters() call must be invoked after a Callback Protocol is installed
  1687. to enable the use of callbacks.
  1688. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  1689. @param[in] NewAutoArp If not NULL, a pointer to a value that specifies whether to replace the
  1690. current value of AutoARP.
  1691. @param[in] NewSendGUID If not NULL, a pointer to a value that specifies whether to replace the
  1692. current value of SendGUID.
  1693. @param[in] NewTTL If not NULL, a pointer to be used in place of the current value of TTL,
  1694. the "time to live" field of the IP header.
  1695. @param[in] NewToS If not NULL, a pointer to be used in place of the current value of ToS,
  1696. the "type of service" field of the IP header.
  1697. @param[in] NewMakeCallback If not NULL, a pointer to a value that specifies whether to replace the
  1698. current value of the MakeCallback field of the Mode structure.
  1699. @retval EFI_SUCCESS The new parameters values were updated.
  1700. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  1701. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  1702. **/
  1703. EFI_STATUS
  1704. EFIAPI
  1705. EfiPxeBcSetParameters (
  1706. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  1707. IN BOOLEAN *NewAutoArp OPTIONAL,
  1708. IN BOOLEAN *NewSendGUID OPTIONAL,
  1709. IN UINT8 *NewTTL OPTIONAL,
  1710. IN UINT8 *NewToS OPTIONAL,
  1711. IN BOOLEAN *NewMakeCallback OPTIONAL
  1712. )
  1713. {
  1714. PXEBC_PRIVATE_DATA *Private;
  1715. EFI_PXE_BASE_CODE_MODE *Mode;
  1716. EFI_GUID SystemGuid;
  1717. EFI_STATUS Status;
  1718. if (This == NULL) {
  1719. return EFI_INVALID_PARAMETER;
  1720. }
  1721. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  1722. Mode = Private->PxeBc.Mode;
  1723. if (!Mode->Started) {
  1724. return EFI_NOT_STARTED;
  1725. }
  1726. if (NewMakeCallback != NULL) {
  1727. if (*NewMakeCallback) {
  1728. //
  1729. // Update the previous PxeBcCallback protocol.
  1730. //
  1731. Status = gBS->HandleProtocol (
  1732. Mode->UsingIpv6 ? Private->Ip6Nic->Controller : Private->Ip4Nic->Controller,
  1733. &gEfiPxeBaseCodeCallbackProtocolGuid,
  1734. (VOID **)&Private->PxeBcCallback
  1735. );
  1736. if (EFI_ERROR (Status) || (Private->PxeBcCallback->Callback == NULL)) {
  1737. return EFI_INVALID_PARAMETER;
  1738. }
  1739. } else {
  1740. Private->PxeBcCallback = NULL;
  1741. }
  1742. Mode->MakeCallbacks = *NewMakeCallback;
  1743. }
  1744. if (NewSendGUID != NULL) {
  1745. if (*NewSendGUID && EFI_ERROR (NetLibGetSystemGuid (&SystemGuid))) {
  1746. DEBUG ((DEBUG_WARN, "PXE: Failed to read system GUID from the smbios table!\n"));
  1747. return EFI_INVALID_PARAMETER;
  1748. }
  1749. Mode->SendGUID = *NewSendGUID;
  1750. }
  1751. if (NewAutoArp != NULL) {
  1752. Mode->AutoArp = *NewAutoArp;
  1753. }
  1754. if (NewTTL != NULL) {
  1755. Mode->TTL = *NewTTL;
  1756. }
  1757. if (NewToS != NULL) {
  1758. Mode->ToS = *NewToS;
  1759. }
  1760. return EFI_SUCCESS;
  1761. }
  1762. /**
  1763. Updates the station IP address and/or subnet mask values of a network device.
  1764. This function updates the station IP address and/or subnet mask values of a network
  1765. device. The NewStationIp field is used to modify the network device's current IP address.
  1766. If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,
  1767. this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure
  1768. with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet
  1769. mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.
  1770. Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE
  1771. structure with NewSubnetMask.
  1772. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  1773. @param[in] NewStationIp Pointer to the new IP address to be used by the network device.
  1774. @param[in] NewSubnetMask Pointer to the new subnet mask to be used by the network device.
  1775. @retval EFI_SUCCESS The new station IP address and/or subnet mask were updated.
  1776. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  1777. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  1778. **/
  1779. EFI_STATUS
  1780. EFIAPI
  1781. EfiPxeBcSetStationIP (
  1782. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  1783. IN EFI_IP_ADDRESS *NewStationIp OPTIONAL,
  1784. IN EFI_IP_ADDRESS *NewSubnetMask OPTIONAL
  1785. )
  1786. {
  1787. EFI_STATUS Status;
  1788. PXEBC_PRIVATE_DATA *Private;
  1789. EFI_PXE_BASE_CODE_MODE *Mode;
  1790. if (This == NULL) {
  1791. return EFI_INVALID_PARAMETER;
  1792. }
  1793. if ((NewStationIp != NULL) && !NetIp6IsValidUnicast (&NewStationIp->v6)) {
  1794. return EFI_INVALID_PARAMETER;
  1795. }
  1796. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  1797. Mode = Private->PxeBc.Mode;
  1798. Status = EFI_SUCCESS;
  1799. if (!Mode->UsingIpv6 &&
  1800. (NewSubnetMask != NULL) &&
  1801. !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0])))
  1802. {
  1803. return EFI_INVALID_PARAMETER;
  1804. }
  1805. if (!Mode->UsingIpv6 && (NewStationIp != NULL)) {
  1806. if (IP4_IS_UNSPECIFIED (NTOHL (NewStationIp->Addr[0])) ||
  1807. IP4_IS_LOCAL_BROADCAST (NTOHL (NewStationIp->Addr[0])) ||
  1808. ((NewSubnetMask != NULL) && (NewSubnetMask->Addr[0] != 0) && !NetIp4IsUnicast (NTOHL (NewStationIp->Addr[0]), NTOHL (NewSubnetMask->Addr[0]))))
  1809. {
  1810. return EFI_INVALID_PARAMETER;
  1811. }
  1812. }
  1813. if (!Mode->Started) {
  1814. return EFI_NOT_STARTED;
  1815. }
  1816. if (Mode->UsingIpv6 && (NewStationIp != NULL)) {
  1817. //
  1818. // Set the IPv6 address by Ip6Config protocol.
  1819. //
  1820. Status = PxeBcRegisterIp6Address (Private, &NewStationIp->v6);
  1821. if (EFI_ERROR (Status)) {
  1822. goto ON_EXIT;
  1823. }
  1824. }
  1825. if (NewStationIp != NULL) {
  1826. CopyMem (&Mode->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));
  1827. CopyMem (&Private->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));
  1828. }
  1829. if (!Mode->UsingIpv6 && (NewSubnetMask != NULL)) {
  1830. CopyMem (&Mode->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));
  1831. CopyMem (&Private->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));
  1832. }
  1833. Status = PxeBcFlushStationIp (Private, NewStationIp, NewSubnetMask);
  1834. if (!EFI_ERROR (Status)) {
  1835. Private->IsAddressOk = TRUE;
  1836. }
  1837. ON_EXIT:
  1838. return Status;
  1839. }
  1840. /**
  1841. Updates the contents of the cached DHCP and Discover packets.
  1842. The pointers to the new packets are used to update the contents of the cached
  1843. packets in the EFI_PXE_BASE_CODE_MODE structure.
  1844. @param[in] This Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.
  1845. @param[in] NewDhcpDiscoverValid Pointer to a value that will replace the current
  1846. DhcpDiscoverValid field.
  1847. @param[in] NewDhcpAckReceived Pointer to a value that will replace the current
  1848. DhcpAckReceived field.
  1849. @param[in] NewProxyOfferReceived Pointer to a value that will replace the current
  1850. ProxyOfferReceived field.
  1851. @param[in] NewPxeDiscoverValid Pointer to a value that will replace the current
  1852. ProxyOfferReceived field.
  1853. @param[in] NewPxeReplyReceived Pointer to a value that will replace the current
  1854. PxeReplyReceived field.
  1855. @param[in] NewPxeBisReplyReceived Pointer to a value that will replace the current
  1856. PxeBisReplyReceived field.
  1857. @param[in] NewDhcpDiscover Pointer to the new cached DHCP Discover packet contents.
  1858. @param[in] NewDhcpAck Pointer to the new cached DHCP Ack packet contents.
  1859. @param[in] NewProxyOffer Pointer to the new cached Proxy Offer packet contents.
  1860. @param[in] NewPxeDiscover Pointer to the new cached PXE Discover packet contents.
  1861. @param[in] NewPxeReply Pointer to the new cached PXE Reply packet contents.
  1862. @param[in] NewPxeBisReply Pointer to the new cached PXE BIS Reply packet contents.
  1863. @retval EFI_SUCCESS The cached packet contents were updated.
  1864. @retval EFI_NOT_STARTED The PXE Base Code Protocol is in the stopped state.
  1865. @retval EFI_INVALID_PARAMETER This is NULL or does not point to a valid
  1866. EFI_PXE_BASE_CODE_PROTOCOL structure.
  1867. **/
  1868. EFI_STATUS
  1869. EFIAPI
  1870. EfiPxeBcSetPackets (
  1871. IN EFI_PXE_BASE_CODE_PROTOCOL *This,
  1872. IN BOOLEAN *NewDhcpDiscoverValid OPTIONAL,
  1873. IN BOOLEAN *NewDhcpAckReceived OPTIONAL,
  1874. IN BOOLEAN *NewProxyOfferReceived OPTIONAL,
  1875. IN BOOLEAN *NewPxeDiscoverValid OPTIONAL,
  1876. IN BOOLEAN *NewPxeReplyReceived OPTIONAL,
  1877. IN BOOLEAN *NewPxeBisReplyReceived OPTIONAL,
  1878. IN EFI_PXE_BASE_CODE_PACKET *NewDhcpDiscover OPTIONAL,
  1879. IN EFI_PXE_BASE_CODE_PACKET *NewDhcpAck OPTIONAL,
  1880. IN EFI_PXE_BASE_CODE_PACKET *NewProxyOffer OPTIONAL,
  1881. IN EFI_PXE_BASE_CODE_PACKET *NewPxeDiscover OPTIONAL,
  1882. IN EFI_PXE_BASE_CODE_PACKET *NewPxeReply OPTIONAL,
  1883. IN EFI_PXE_BASE_CODE_PACKET *NewPxeBisReply OPTIONAL
  1884. )
  1885. {
  1886. PXEBC_PRIVATE_DATA *Private;
  1887. EFI_PXE_BASE_CODE_MODE *Mode;
  1888. if (This == NULL) {
  1889. return EFI_INVALID_PARAMETER;
  1890. }
  1891. Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);
  1892. Mode = Private->PxeBc.Mode;
  1893. if (!Mode->Started) {
  1894. return EFI_NOT_STARTED;
  1895. }
  1896. if (NewDhcpDiscoverValid != NULL) {
  1897. Mode->DhcpDiscoverValid = *NewDhcpDiscoverValid;
  1898. }
  1899. if (NewDhcpAckReceived != NULL) {
  1900. Mode->DhcpAckReceived = *NewDhcpAckReceived;
  1901. }
  1902. if (NewProxyOfferReceived != NULL) {
  1903. Mode->ProxyOfferReceived = *NewProxyOfferReceived;
  1904. }
  1905. if (NewPxeDiscoverValid != NULL) {
  1906. Mode->PxeDiscoverValid = *NewPxeDiscoverValid;
  1907. }
  1908. if (NewPxeReplyReceived != NULL) {
  1909. Mode->PxeReplyReceived = *NewPxeReplyReceived;
  1910. }
  1911. if (NewPxeBisReplyReceived != NULL) {
  1912. Mode->PxeBisReplyReceived = *NewPxeBisReplyReceived;
  1913. }
  1914. if (NewDhcpDiscover != NULL) {
  1915. CopyMem (&Mode->DhcpDiscover, NewDhcpDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));
  1916. }
  1917. if (NewDhcpAck != NULL) {
  1918. CopyMem (&Mode->DhcpAck, NewDhcpAck, sizeof (EFI_PXE_BASE_CODE_PACKET));
  1919. }
  1920. if (NewProxyOffer != NULL) {
  1921. CopyMem (&Mode->ProxyOffer, NewProxyOffer, sizeof (EFI_PXE_BASE_CODE_PACKET));
  1922. }
  1923. if (NewPxeDiscover != NULL) {
  1924. CopyMem (&Mode->PxeDiscover, NewPxeDiscover, sizeof (EFI_PXE_BASE_CODE_PACKET));
  1925. }
  1926. if (NewPxeReply != NULL) {
  1927. CopyMem (&Mode->PxeReply, NewPxeReply, sizeof (EFI_PXE_BASE_CODE_PACKET));
  1928. }
  1929. if (NewPxeBisReply != NULL) {
  1930. CopyMem (&Mode->PxeBisReply, NewPxeBisReply, sizeof (EFI_PXE_BASE_CODE_PACKET));
  1931. }
  1932. return EFI_SUCCESS;
  1933. }
  1934. EFI_PXE_BASE_CODE_PROTOCOL gPxeBcProtocolTemplate = {
  1935. EFI_PXE_BASE_CODE_PROTOCOL_REVISION,
  1936. EfiPxeBcStart,
  1937. EfiPxeBcStop,
  1938. EfiPxeBcDhcp,
  1939. EfiPxeBcDiscover,
  1940. EfiPxeBcMtftp,
  1941. EfiPxeBcUdpWrite,
  1942. EfiPxeBcUdpRead,
  1943. EfiPxeBcSetIpFilter,
  1944. EfiPxeBcArp,
  1945. EfiPxeBcSetParameters,
  1946. EfiPxeBcSetStationIP,
  1947. EfiPxeBcSetPackets,
  1948. NULL
  1949. };
  1950. /**
  1951. Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has
  1952. received, or is waiting to receive a packet.
  1953. This function is invoked when the PXE Base Code Protocol is about to transmit, has received,
  1954. or is waiting to receive a packet. Parameters Function and Received specify the type of event.
  1955. Parameters PacketLen and Packet specify the packet that generated the event. If these fields
  1956. are zero and NULL respectively, then this is a status update callback. If the operation specified
  1957. by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation
  1958. specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to
  1959. the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.
  1960. The SetParameters() function must be called after a Callback Protocol is installed to enable the
  1961. use of callbacks.
  1962. @param[in] This Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.
  1963. @param[in] Function The PXE Base Code Protocol function that is waiting for an event.
  1964. @param[in] Received TRUE if the callback is being invoked due to a receive event. FALSE if
  1965. the callback is being invoked due to a transmit event.
  1966. @param[in] PacketLength The length, in bytes, of Packet. This field will have a value of zero if
  1967. this is a wait for receive event.
  1968. @param[in] PacketPtr If Received is TRUE, a pointer to the packet that was just received;
  1969. otherwise a pointer to the packet that is about to be transmitted.
  1970. @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE If Function specifies a continue operation.
  1971. @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT If Function specifies an abort operation.
  1972. **/
  1973. EFI_PXE_BASE_CODE_CALLBACK_STATUS
  1974. EFIAPI
  1975. EfiPxeLoadFileCallback (
  1976. IN EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL *This,
  1977. IN EFI_PXE_BASE_CODE_FUNCTION Function,
  1978. IN BOOLEAN Received,
  1979. IN UINT32 PacketLength,
  1980. IN EFI_PXE_BASE_CODE_PACKET *PacketPtr OPTIONAL
  1981. )
  1982. {
  1983. EFI_INPUT_KEY Key;
  1984. EFI_STATUS Status;
  1985. //
  1986. // Catch Ctrl-C or ESC to abort.
  1987. //
  1988. Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
  1989. if (!EFI_ERROR (Status)) {
  1990. if ((Key.ScanCode == SCAN_ESC) || (Key.UnicodeChar == (0x1F & 'c'))) {
  1991. return EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT;
  1992. }
  1993. }
  1994. //
  1995. // No print if receive packet
  1996. //
  1997. if (Received) {
  1998. return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
  1999. }
  2000. //
  2001. // Print only for three functions
  2002. //
  2003. switch (Function) {
  2004. case EFI_PXE_BASE_CODE_FUNCTION_MTFTP:
  2005. //
  2006. // Print only for open MTFTP packets, not every MTFTP packets
  2007. //
  2008. if ((PacketLength != 0) && (PacketPtr != NULL)) {
  2009. if ((PacketPtr->Raw[0x1C] != 0x00) || (PacketPtr->Raw[0x1D] != 0x01)) {
  2010. return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
  2011. }
  2012. }
  2013. break;
  2014. case EFI_PXE_BASE_CODE_FUNCTION_DHCP:
  2015. case EFI_PXE_BASE_CODE_FUNCTION_DISCOVER:
  2016. break;
  2017. default:
  2018. return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
  2019. }
  2020. if ((PacketLength != 0) && (PacketPtr != NULL)) {
  2021. //
  2022. // Print '.' when transmit a packet
  2023. //
  2024. AsciiPrint (".");
  2025. }
  2026. return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE;
  2027. }
  2028. EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL gPxeBcCallBackTemplate = {
  2029. EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL_REVISION,
  2030. EfiPxeLoadFileCallback
  2031. };
  2032. /**
  2033. Causes the driver to load a specified file.
  2034. @param[in] This Protocol instance pointer.
  2035. @param[in] FilePath The device specific path of the file to load.
  2036. @param[in] BootPolicy If TRUE, indicates that the request originates from the
  2037. boot manager is attempting to load FilePath as a boot
  2038. selection. If FALSE, then FilePath must match an exact file
  2039. to be loaded.
  2040. @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
  2041. code of EFI_SUCCESS, the amount of data transferred to
  2042. Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
  2043. the size of Buffer required to retrieve the requested file.
  2044. @param[in] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
  2045. then no the size of the requested file is returned in
  2046. BufferSize.
  2047. @retval EFI_SUCCESS The file was loaded.
  2048. @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy.
  2049. @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
  2050. BufferSize is NULL.
  2051. @retval EFI_NO_MEDIA No medium was present to load the file.
  2052. @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
  2053. @retval EFI_NO_RESPONSE The remote system did not respond.
  2054. @retval EFI_NOT_FOUND The file was not found.
  2055. @retval EFI_ABORTED The file load process was manually cancelled.
  2056. **/
  2057. EFI_STATUS
  2058. EFIAPI
  2059. EfiPxeLoadFile (
  2060. IN EFI_LOAD_FILE_PROTOCOL *This,
  2061. IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
  2062. IN BOOLEAN BootPolicy,
  2063. IN OUT UINTN *BufferSize,
  2064. IN VOID *Buffer OPTIONAL
  2065. )
  2066. {
  2067. PXEBC_PRIVATE_DATA *Private;
  2068. PXEBC_VIRTUAL_NIC *VirtualNic;
  2069. EFI_PXE_BASE_CODE_PROTOCOL *PxeBc;
  2070. BOOLEAN UsingIpv6;
  2071. EFI_STATUS Status;
  2072. EFI_STATUS MediaStatus;
  2073. if ((This == NULL) || (BufferSize == NULL) || (FilePath == NULL) || !IsDevicePathEnd (FilePath)) {
  2074. return EFI_INVALID_PARAMETER;
  2075. }
  2076. //
  2077. // Only support BootPolicy
  2078. //
  2079. if (!BootPolicy) {
  2080. return EFI_UNSUPPORTED;
  2081. }
  2082. VirtualNic = PXEBC_VIRTUAL_NIC_FROM_LOADFILE (This);
  2083. Private = VirtualNic->Private;
  2084. PxeBc = &Private->PxeBc;
  2085. UsingIpv6 = FALSE;
  2086. Status = EFI_DEVICE_ERROR;
  2087. //
  2088. // Check media status before PXE start
  2089. //
  2090. MediaStatus = EFI_SUCCESS;
  2091. NetLibDetectMediaWaitTimeout (Private->Controller, PXEBC_CHECK_MEDIA_WAITING_TIME, &MediaStatus);
  2092. if (MediaStatus != EFI_SUCCESS) {
  2093. return EFI_NO_MEDIA;
  2094. }
  2095. //
  2096. // Check whether the virtual nic is using IPv6 or not.
  2097. //
  2098. if (VirtualNic == Private->Ip6Nic) {
  2099. UsingIpv6 = TRUE;
  2100. }
  2101. //
  2102. // Start Pxe Base Code to initialize PXE boot.
  2103. //
  2104. Status = PxeBc->Start (PxeBc, UsingIpv6);
  2105. if ((Status == EFI_ALREADY_STARTED) && (UsingIpv6 != PxeBc->Mode->UsingIpv6)) {
  2106. //
  2107. // PxeBc protocol has already been started but not on the required IP version, restart it.
  2108. //
  2109. Status = PxeBc->Stop (PxeBc);
  2110. if (!EFI_ERROR (Status)) {
  2111. Status = PxeBc->Start (PxeBc, UsingIpv6);
  2112. }
  2113. }
  2114. if ((Status == EFI_SUCCESS) || (Status == EFI_ALREADY_STARTED)) {
  2115. Status = PxeBcLoadBootFile (Private, BufferSize, Buffer);
  2116. }
  2117. if ((Status != EFI_SUCCESS) &&
  2118. (Status != EFI_UNSUPPORTED) &&
  2119. (Status != EFI_BUFFER_TOO_SMALL))
  2120. {
  2121. //
  2122. // There are three cases, which needn't stop pxebc here.
  2123. // 1. success to download file.
  2124. // 2. success to get file size.
  2125. // 3. unsupported.
  2126. //
  2127. PxeBc->Stop (PxeBc);
  2128. } else {
  2129. //
  2130. // The DHCP4 can have only one configured child instance so we need to stop
  2131. // reset the DHCP4 child before we return. Otherwise these programs which
  2132. // also need to use DHCP4 will be impacted.
  2133. //
  2134. if (!PxeBc->Mode->UsingIpv6) {
  2135. Private->Dhcp4->Stop (Private->Dhcp4);
  2136. Private->Dhcp4->Configure (Private->Dhcp4, NULL);
  2137. }
  2138. }
  2139. return Status;
  2140. }
  2141. EFI_LOAD_FILE_PROTOCOL gLoadFileProtocolTemplate = { EfiPxeLoadFile };