HttpBootDhcp4.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /** @file
  2. Functions implementation related with DHCPv4 for HTTP boot driver.
  3. Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "HttpBootDxe.h"
  7. //
  8. // This is a map from the interested DHCP4 option tags' index to the tag value.
  9. //
  10. UINT8 mInterestedDhcp4Tags[HTTP_BOOT_DHCP4_TAG_INDEX_MAX] = {
  11. DHCP4_TAG_BOOTFILE_LEN,
  12. DHCP4_TAG_OVERLOAD,
  13. DHCP4_TAG_MSG_TYPE,
  14. DHCP4_TAG_SERVER_ID,
  15. DHCP4_TAG_VENDOR_CLASS_ID,
  16. DHCP4_TAG_BOOTFILE,
  17. DHCP4_TAG_DNS_SERVER
  18. };
  19. //
  20. // There are 4 times retries with the value of 4, 8, 16 and 32, refers to UEFI 2.5 spec.
  21. //
  22. UINT32 mHttpDhcpTimeout[4] = {4, 8, 16, 32};
  23. /**
  24. Build the options buffer for the DHCPv4 request packet.
  25. @param[in] Private Pointer to HTTP boot driver private data.
  26. @param[out] OptList Pointer to the option pointer array.
  27. @param[in] Buffer Pointer to the buffer to contain the option list.
  28. @return Index The count of the built-in options.
  29. **/
  30. UINT32
  31. HttpBootBuildDhcp4Options (
  32. IN HTTP_BOOT_PRIVATE_DATA *Private,
  33. OUT EFI_DHCP4_PACKET_OPTION **OptList,
  34. IN UINT8 *Buffer
  35. )
  36. {
  37. HTTP_BOOT_DHCP4_OPTION_ENTRY OptEnt;
  38. UINT16 Value;
  39. UINT32 Index;
  40. Index = 0;
  41. OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Buffer;
  42. //
  43. // Append parameter request list option.
  44. //
  45. OptList[Index]->OpCode = DHCP4_TAG_PARA_LIST;
  46. OptList[Index]->Length = 27;
  47. OptEnt.Para = (HTTP_BOOT_DHCP4_OPTION_PARA *) OptList[Index]->Data;
  48. OptEnt.Para->ParaList[0] = DHCP4_TAG_NETMASK;
  49. OptEnt.Para->ParaList[1] = DHCP4_TAG_TIME_OFFSET;
  50. OptEnt.Para->ParaList[2] = DHCP4_TAG_ROUTER;
  51. OptEnt.Para->ParaList[3] = DHCP4_TAG_TIME_SERVER;
  52. OptEnt.Para->ParaList[4] = DHCP4_TAG_NAME_SERVER;
  53. OptEnt.Para->ParaList[5] = DHCP4_TAG_DNS_SERVER;
  54. OptEnt.Para->ParaList[6] = DHCP4_TAG_HOSTNAME;
  55. OptEnt.Para->ParaList[7] = DHCP4_TAG_BOOTFILE_LEN;
  56. OptEnt.Para->ParaList[8] = DHCP4_TAG_DOMAINNAME;
  57. OptEnt.Para->ParaList[9] = DHCP4_TAG_ROOTPATH;
  58. OptEnt.Para->ParaList[10] = DHCP4_TAG_EXTEND_PATH;
  59. OptEnt.Para->ParaList[11] = DHCP4_TAG_EMTU;
  60. OptEnt.Para->ParaList[12] = DHCP4_TAG_TTL;
  61. OptEnt.Para->ParaList[13] = DHCP4_TAG_BROADCAST;
  62. OptEnt.Para->ParaList[14] = DHCP4_TAG_NIS_DOMAIN;
  63. OptEnt.Para->ParaList[15] = DHCP4_TAG_NIS_SERVER;
  64. OptEnt.Para->ParaList[16] = DHCP4_TAG_NTP_SERVER;
  65. OptEnt.Para->ParaList[17] = DHCP4_TAG_VENDOR;
  66. OptEnt.Para->ParaList[18] = DHCP4_TAG_REQUEST_IP;
  67. OptEnt.Para->ParaList[19] = DHCP4_TAG_LEASE;
  68. OptEnt.Para->ParaList[20] = DHCP4_TAG_SERVER_ID;
  69. OptEnt.Para->ParaList[21] = DHCP4_TAG_T1;
  70. OptEnt.Para->ParaList[22] = DHCP4_TAG_T2;
  71. OptEnt.Para->ParaList[23] = DHCP4_TAG_VENDOR_CLASS_ID;
  72. OptEnt.Para->ParaList[25] = DHCP4_TAG_BOOTFILE;
  73. OptEnt.Para->ParaList[26] = DHCP4_TAG_UUID;
  74. Index++;
  75. OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
  76. //
  77. // Append UUID/Guid-based client identifier option
  78. //
  79. OptList[Index]->OpCode = DHCP4_TAG_UUID;
  80. OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UUID);
  81. OptEnt.Uuid = (HTTP_BOOT_DHCP4_OPTION_UUID *) OptList[Index]->Data;
  82. OptEnt.Uuid->Type = 0;
  83. if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) {
  84. //
  85. // Zero the Guid to indicate NOT programable if failed to get system Guid.
  86. //
  87. ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));
  88. }
  89. Index++;
  90. OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
  91. //
  92. // Append client network device interface option
  93. //
  94. OptList[Index]->OpCode = DHCP4_TAG_UNDI;
  95. OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UNDI);
  96. OptEnt.Undi = (HTTP_BOOT_DHCP4_OPTION_UNDI *) OptList[Index]->Data;
  97. if (Private->Nii != NULL) {
  98. OptEnt.Undi->Type = Private->Nii->Type;
  99. OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
  100. OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
  101. } else {
  102. OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;
  103. OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
  104. OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
  105. }
  106. Index++;
  107. OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
  108. //
  109. // Append client system architecture option
  110. //
  111. OptList[Index]->OpCode = DHCP4_TAG_ARCH;
  112. OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_ARCH);
  113. OptEnt.Arch = (HTTP_BOOT_DHCP4_OPTION_ARCH *) OptList[Index]->Data;
  114. Value = HTONS (EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE);
  115. CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
  116. Index++;
  117. OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
  118. //
  119. // Append vendor class identify option
  120. //
  121. OptList[Index]->OpCode = DHCP4_TAG_VENDOR_CLASS_ID;
  122. OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_CLID);
  123. OptEnt.Clid = (HTTP_BOOT_DHCP4_OPTION_CLID *) OptList[Index]->Data;
  124. CopyMem (
  125. OptEnt.Clid,
  126. DEFAULT_CLASS_ID_DATA,
  127. sizeof (HTTP_BOOT_DHCP4_OPTION_CLID)
  128. );
  129. HttpBootUintnToAscDecWithFormat (
  130. EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE,
  131. OptEnt.Clid->ArchitectureType,
  132. sizeof (OptEnt.Clid->ArchitectureType)
  133. );
  134. if (Private->Nii != NULL) {
  135. CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));
  136. HttpBootUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));
  137. HttpBootUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));
  138. }
  139. Index++;
  140. return Index;
  141. }
  142. /**
  143. Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.
  144. @param[in] Buffer Pointer to the option buffer.
  145. @param[in] Length Length of the option buffer.
  146. @param[in] OptTag Tag of the required option.
  147. @retval NULL Failed to find the required option.
  148. @retval Others The position of the required option.
  149. **/
  150. EFI_DHCP4_PACKET_OPTION *
  151. HttpBootParseDhcp4Options (
  152. IN UINT8 *Buffer,
  153. IN UINT32 Length,
  154. IN UINT8 OptTag
  155. )
  156. {
  157. EFI_DHCP4_PACKET_OPTION *Option;
  158. UINT32 Offset;
  159. Option = (EFI_DHCP4_PACKET_OPTION *) Buffer;
  160. Offset = 0;
  161. while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) {
  162. if (Option->OpCode == OptTag) {
  163. //
  164. // Found the required option.
  165. //
  166. return Option;
  167. }
  168. //
  169. // Skip the current option to the next.
  170. //
  171. if (Option->OpCode == DHCP4_TAG_PAD) {
  172. Offset++;
  173. } else {
  174. Offset += Option->Length + 2;
  175. }
  176. Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset);
  177. }
  178. return NULL;
  179. }
  180. /**
  181. Cache the DHCPv4 packet.
  182. @param[in] Dst Pointer to the cache buffer for DHCPv4 packet.
  183. @param[in] Src Pointer to the DHCPv4 packet to be cached.
  184. @retval EFI_SUCCESS Packet is copied.
  185. @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
  186. **/
  187. EFI_STATUS
  188. HttpBootCacheDhcp4Packet (
  189. IN EFI_DHCP4_PACKET *Dst,
  190. IN EFI_DHCP4_PACKET *Src
  191. )
  192. {
  193. if (Dst->Size < Src->Length) {
  194. return EFI_BUFFER_TOO_SMALL;
  195. }
  196. CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);
  197. Dst->Length = Src->Length;
  198. return EFI_SUCCESS;
  199. }
  200. /**
  201. Parse the cached DHCPv4 packet, including all the options.
  202. @param[in] Cache4 Pointer to cached DHCPv4 packet.
  203. @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully.
  204. @retval EFI_DEVICE_ERROR Failed to parse an invalid packet.
  205. **/
  206. EFI_STATUS
  207. HttpBootParseDhcp4Packet (
  208. IN HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4
  209. )
  210. {
  211. EFI_DHCP4_PACKET *Offer;
  212. EFI_DHCP4_PACKET_OPTION **Options;
  213. UINTN Index;
  214. EFI_DHCP4_PACKET_OPTION *Option;
  215. BOOLEAN IsProxyOffer;
  216. BOOLEAN IsHttpOffer;
  217. BOOLEAN IsDnsOffer;
  218. BOOLEAN IpExpressedUri;
  219. UINT8 *Ptr8;
  220. EFI_STATUS Status;
  221. HTTP_BOOT_OFFER_TYPE OfferType;
  222. EFI_IPv4_ADDRESS IpAddr;
  223. BOOLEAN FileFieldOverloaded;
  224. IsDnsOffer = FALSE;
  225. IpExpressedUri = FALSE;
  226. IsProxyOffer = FALSE;
  227. IsHttpOffer = FALSE;
  228. FileFieldOverloaded = FALSE;
  229. ZeroMem (Cache4->OptList, sizeof (Cache4->OptList));
  230. Offer = &Cache4->Packet.Offer;
  231. Options = Cache4->OptList;
  232. //
  233. // Parse DHCPv4 options in this offer, and store the pointers.
  234. // First, try to parse DHCPv4 options from the DHCP optional parameters field.
  235. //
  236. for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
  237. Options[Index] = HttpBootParseDhcp4Options (
  238. Offer->Dhcp4.Option,
  239. GET_OPTION_BUFFER_LEN (Offer),
  240. mInterestedDhcp4Tags[Index]
  241. );
  242. }
  243. //
  244. // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132.
  245. // If yes, try to parse options from the BootFileName field, then ServerName field.
  246. //
  247. Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_OVERLOAD];
  248. if (Option != NULL) {
  249. if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_FILE) != 0) {
  250. FileFieldOverloaded = TRUE;
  251. for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
  252. if (Options[Index] == NULL) {
  253. Options[Index] = HttpBootParseDhcp4Options (
  254. (UINT8 *) Offer->Dhcp4.Header.BootFileName,
  255. sizeof (Offer->Dhcp4.Header.BootFileName),
  256. mInterestedDhcp4Tags[Index]
  257. );
  258. }
  259. }
  260. }
  261. if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_SERVER_NAME) != 0) {
  262. for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
  263. if (Options[Index] == NULL) {
  264. Options[Index] = HttpBootParseDhcp4Options (
  265. (UINT8 *) Offer->Dhcp4.Header.ServerName,
  266. sizeof (Offer->Dhcp4.Header.ServerName),
  267. mInterestedDhcp4Tags[Index]
  268. );
  269. }
  270. }
  271. }
  272. }
  273. //
  274. // The offer with "yiaddr" is a proxy offer.
  275. //
  276. if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) {
  277. IsProxyOffer = TRUE;
  278. }
  279. //
  280. // The offer with "HTTPClient" is a Http offer.
  281. //
  282. Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_CLASS_ID];
  283. if ((Option != NULL) && (Option->Length >= 10) &&
  284. (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 10) == 0)) {
  285. IsHttpOffer = TRUE;
  286. }
  287. //
  288. // The offer with Domain Server is a DNS offer.
  289. //
  290. Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];
  291. if (Option != NULL) {
  292. IsDnsOffer = TRUE;
  293. }
  294. //
  295. // Parse boot file name:
  296. // Boot URI information is provided thru 'file' field in DHCP Header or option 67.
  297. // According to RFC 2132, boot file name should be read from DHCP option 67 (bootfile name) if present.
  298. // Otherwise, read from boot file field in DHCP header.
  299. //
  300. if (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
  301. //
  302. // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
  303. // terminated string. So force to append null terminated character at the end of string.
  304. //
  305. Ptr8 = (UINT8*)&Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];
  306. Ptr8 += Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Length;
  307. if (*(Ptr8 - 1) != '\0') {
  308. *Ptr8 = '\0';
  309. }
  310. } else if (!FileFieldOverloaded && Offer->Dhcp4.Header.BootFileName[0] != 0) {
  311. //
  312. // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.
  313. // Do not count dhcp option header here, or else will destroy the serverhostname.
  314. //
  315. Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)
  316. (&Offer->Dhcp4.Header.BootFileName[0] -
  317. OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));
  318. }
  319. //
  320. // Http offer must have a boot URI.
  321. //
  322. if (IsHttpOffer && Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {
  323. return EFI_DEVICE_ERROR;
  324. }
  325. //
  326. // Try to retrieve the IP of HTTP server from URI.
  327. //
  328. if (IsHttpOffer) {
  329. Status = HttpParseUrl (
  330. (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
  331. (UINT32) AsciiStrLen ((CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data),
  332. FALSE,
  333. &Cache4->UriParser
  334. );
  335. if (EFI_ERROR (Status)) {
  336. return EFI_DEVICE_ERROR;
  337. }
  338. Status = HttpUrlGetIp4 (
  339. (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
  340. Cache4->UriParser,
  341. &IpAddr
  342. );
  343. IpExpressedUri = !EFI_ERROR (Status);
  344. }
  345. //
  346. // Determine offer type of the DHCPv4 packet.
  347. //
  348. if (IsHttpOffer) {
  349. if (IpExpressedUri) {
  350. if (IsProxyOffer) {
  351. OfferType = HttpOfferTypeProxyIpUri;
  352. } else {
  353. OfferType = IsDnsOffer ? HttpOfferTypeDhcpIpUriDns : HttpOfferTypeDhcpIpUri;
  354. }
  355. } else {
  356. if (!IsProxyOffer) {
  357. OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri;
  358. } else {
  359. OfferType = HttpOfferTypeProxyNameUri;
  360. }
  361. }
  362. } else {
  363. if (!IsProxyOffer) {
  364. OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly;
  365. } else {
  366. if (Cache4->UriParser != NULL) {
  367. FreePool (Cache4->UriParser);
  368. }
  369. return EFI_DEVICE_ERROR;
  370. }
  371. }
  372. Cache4->OfferType = OfferType;
  373. return EFI_SUCCESS;
  374. }
  375. /**
  376. Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.
  377. @param[in] Private Pointer to HTTP boot driver private data.
  378. @param[in] RcvdOffer Pointer to the received offer packet.
  379. @retval EFI_SUCCESS Cache and parse the packet successfully.
  380. @retval Others Operation failed.
  381. **/
  382. EFI_STATUS
  383. HttpBootCacheDhcp4Offer (
  384. IN HTTP_BOOT_PRIVATE_DATA *Private,
  385. IN EFI_DHCP4_PACKET *RcvdOffer
  386. )
  387. {
  388. HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4;
  389. EFI_DHCP4_PACKET *Offer;
  390. HTTP_BOOT_OFFER_TYPE OfferType;
  391. EFI_STATUS Status;
  392. ASSERT (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM);
  393. Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;
  394. Offer = &Cache4->Packet.Offer;
  395. //
  396. // Cache the content of DHCPv4 packet firstly.
  397. //
  398. Status = HttpBootCacheDhcp4Packet (Offer, RcvdOffer);
  399. if (EFI_ERROR (Status)) {
  400. return Status;
  401. }
  402. //
  403. // Validate the DHCPv4 packet, and parse the options and offer type.
  404. //
  405. if (EFI_ERROR (HttpBootParseDhcp4Packet (Cache4))) {
  406. return EFI_ABORTED;
  407. }
  408. //
  409. // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
  410. //
  411. OfferType = Cache4->OfferType;
  412. ASSERT (OfferType < HttpOfferTypeMax);
  413. ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM);
  414. Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
  415. Private->OfferCount[OfferType]++;
  416. Private->OfferNum++;
  417. return EFI_SUCCESS;
  418. }
  419. /**
  420. Select an DHCPv4 or DHCP6 offer, and record SelectIndex and SelectProxyType.
  421. @param[in] Private Pointer to HTTP boot driver private data.
  422. **/
  423. VOID
  424. HttpBootSelectDhcpOffer (
  425. IN HTTP_BOOT_PRIVATE_DATA *Private
  426. )
  427. {
  428. Private->SelectIndex = 0;
  429. Private->SelectProxyType = HttpOfferTypeMax;
  430. if (Private->FilePathUri != NULL) {
  431. //
  432. // We are in home environment, the URI is already specified.
  433. // Just need to choose a DHCP offer.
  434. // The offer with DNS server address takes priority here.
  435. //
  436. if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0) {
  437. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
  438. } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {
  439. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;
  440. } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
  441. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
  442. } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0) {
  443. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
  444. } else if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
  445. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
  446. }
  447. } else {
  448. //
  449. // We are in corporate environment.
  450. //
  451. // Priority1: HttpOfferTypeDhcpIpUri or HttpOfferTypeDhcpIpUriDns
  452. // Priority2: HttpOfferTypeDhcpNameUriDns
  453. // Priority3: HttpOfferTypeDhcpOnly + HttpOfferTypeProxyIpUri
  454. // Priority4: HttpOfferTypeDhcpDns + HttpOfferTypeProxyIpUri
  455. // Priority5: HttpOfferTypeDhcpDns + HttpOfferTypeProxyNameUri
  456. // Priority6: HttpOfferTypeDhcpDns + HttpOfferTypeDhcpNameUri
  457. //
  458. if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
  459. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
  460. } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {
  461. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;
  462. }else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
  463. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
  464. } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0 &&
  465. Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) {
  466. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
  467. Private->SelectProxyType = HttpOfferTypeProxyIpUri;
  468. } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
  469. Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) {
  470. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
  471. Private->SelectProxyType = HttpOfferTypeProxyIpUri;
  472. } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
  473. Private->OfferCount[HttpOfferTypeProxyNameUri] > 0) {
  474. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
  475. Private->SelectProxyType = HttpOfferTypeProxyNameUri;
  476. } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
  477. Private->OfferCount[HttpOfferTypeDhcpNameUri] > 0) {
  478. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
  479. Private->SelectProxyType = HttpOfferTypeDhcpNameUri;
  480. }
  481. }
  482. }
  483. /**
  484. EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
  485. to intercept events that occurred in the configuration process.
  486. @param[in] This Pointer to the EFI DHCPv4 Protocol.
  487. @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().
  488. @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.
  489. @param[in] Dhcp4Event The event that occurs in the current state, which usually means a
  490. state transition.
  491. @param[in] Packet The DHCPv4 packet that is going to be sent or already received.
  492. @param[out] NewPacket The packet that is used to replace the above Packet.
  493. @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
  494. @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
  495. driver will continue to wait for more DHCPOFFER packets until the
  496. retry timeout expires.
  497. @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process
  498. and return to the Dhcp4Init or Dhcp4InitReboot state.
  499. **/
  500. EFI_STATUS
  501. EFIAPI
  502. HttpBootDhcp4CallBack (
  503. IN EFI_DHCP4_PROTOCOL *This,
  504. IN VOID *Context,
  505. IN EFI_DHCP4_STATE CurrentState,
  506. IN EFI_DHCP4_EVENT Dhcp4Event,
  507. IN EFI_DHCP4_PACKET *Packet OPTIONAL,
  508. OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL
  509. )
  510. {
  511. HTTP_BOOT_PRIVATE_DATA *Private;
  512. EFI_DHCP4_PACKET_OPTION *MaxMsgSize;
  513. UINT16 Value;
  514. EFI_STATUS Status;
  515. BOOLEAN Received;
  516. if ((Dhcp4Event != Dhcp4SendDiscover) &&
  517. (Dhcp4Event != Dhcp4RcvdOffer) &&
  518. (Dhcp4Event != Dhcp4SendRequest) &&
  519. (Dhcp4Event != Dhcp4RcvdAck) &&
  520. (Dhcp4Event != Dhcp4SelectOffer)) {
  521. return EFI_SUCCESS;
  522. }
  523. Private = (HTTP_BOOT_PRIVATE_DATA *) Context;
  524. //
  525. // Override the Maximum DHCP Message Size.
  526. //
  527. MaxMsgSize = HttpBootParseDhcp4Options (
  528. Packet->Dhcp4.Option,
  529. GET_OPTION_BUFFER_LEN (Packet),
  530. DHCP4_TAG_MAXMSG
  531. );
  532. if (MaxMsgSize != NULL) {
  533. Value = HTONS (HTTP_BOOT_DHCP4_PACKET_MAX_SIZE);
  534. CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));
  535. }
  536. //
  537. // Callback to user if any packets sent or received.
  538. //
  539. if (Private->HttpBootCallback != NULL && Dhcp4Event != Dhcp4SelectOffer) {
  540. Received = (BOOLEAN) (Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck);
  541. Status = Private->HttpBootCallback->Callback (
  542. Private->HttpBootCallback,
  543. HttpBootDhcp4,
  544. Received,
  545. Packet->Length,
  546. &Packet->Dhcp4
  547. );
  548. if (EFI_ERROR (Status)) {
  549. return EFI_ABORTED;
  550. }
  551. }
  552. Status = EFI_SUCCESS;
  553. switch (Dhcp4Event) {
  554. case Dhcp4RcvdOffer:
  555. Status = EFI_NOT_READY;
  556. if (Packet->Length > HTTP_BOOT_DHCP4_PACKET_MAX_SIZE) {
  557. //
  558. // Ignore the incoming packets which exceed the maximum length.
  559. //
  560. break;
  561. }
  562. if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) {
  563. //
  564. // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
  565. // the OfferIndex and OfferCount.
  566. // If error happens, just ignore this packet and continue to wait more offer.
  567. //
  568. HttpBootCacheDhcp4Offer (Private, Packet);
  569. }
  570. break;
  571. case Dhcp4SelectOffer:
  572. //
  573. // Select offer according to the priority in UEFI spec, and record the SelectIndex
  574. // and SelectProxyType.
  575. //
  576. HttpBootSelectDhcpOffer (Private);
  577. if (Private->SelectIndex == 0) {
  578. Status = EFI_ABORTED;
  579. } else {
  580. *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;
  581. }
  582. break;
  583. default:
  584. break;
  585. }
  586. return Status;
  587. }
  588. /**
  589. This function will register the IPv4 gateway address to the network device.
  590. @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
  591. @retval EFI_SUCCESS The new IP configuration has been configured successfully.
  592. @retval Others Failed to configure the address.
  593. **/
  594. EFI_STATUS
  595. HttpBootRegisterIp4Gateway (
  596. IN HTTP_BOOT_PRIVATE_DATA *Private
  597. )
  598. {
  599. EFI_STATUS Status;
  600. EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
  601. ASSERT (!Private->UsingIpv6);
  602. Ip4Config2 = Private->Ip4Config2;
  603. //
  604. // Configure the gateway if valid.
  605. //
  606. if (!EFI_IP4_EQUAL (&Private->GatewayIp, &mZeroIp4Addr)) {
  607. Status = Ip4Config2->SetData (
  608. Ip4Config2,
  609. Ip4Config2DataTypeGateway,
  610. sizeof (EFI_IPv4_ADDRESS),
  611. &Private->GatewayIp
  612. );
  613. if (EFI_ERROR (Status)) {
  614. return Status;
  615. }
  616. }
  617. return EFI_SUCCESS;
  618. }
  619. /**
  620. This function will register the default DNS addresses to the network device.
  621. @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
  622. @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes.
  623. @param[in] DnsServerData Point a list of DNS server address in an array
  624. of EFI_IPv4_ADDRESS instances.
  625. @retval EFI_SUCCESS The DNS configuration has been configured successfully.
  626. @retval Others Failed to configure the address.
  627. **/
  628. EFI_STATUS
  629. HttpBootRegisterIp4Dns (
  630. IN HTTP_BOOT_PRIVATE_DATA *Private,
  631. IN UINTN DataLength,
  632. IN VOID *DnsServerData
  633. )
  634. {
  635. EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
  636. ASSERT (!Private->UsingIpv6);
  637. Ip4Config2 = Private->Ip4Config2;
  638. return Ip4Config2->SetData (
  639. Ip4Config2,
  640. Ip4Config2DataTypeDnsServer,
  641. DataLength,
  642. DnsServerData
  643. );
  644. }
  645. /**
  646. This function will switch the IP4 configuration policy to Static.
  647. @param[in] Private Pointer to HTTP boot driver private data.
  648. @retval EFI_SUCCESS The policy is already configured to static.
  649. @retval Others Other error as indicated..
  650. **/
  651. EFI_STATUS
  652. HttpBootSetIp4Policy (
  653. IN HTTP_BOOT_PRIVATE_DATA *Private
  654. )
  655. {
  656. EFI_IP4_CONFIG2_POLICY Policy;
  657. EFI_STATUS Status;
  658. EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
  659. UINTN DataSize;
  660. Ip4Config2 = Private->Ip4Config2;
  661. DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
  662. Status = Ip4Config2->GetData (
  663. Ip4Config2,
  664. Ip4Config2DataTypePolicy,
  665. &DataSize,
  666. &Policy
  667. );
  668. if (EFI_ERROR (Status)) {
  669. return Status;
  670. }
  671. if (Policy != Ip4Config2PolicyStatic) {
  672. Policy = Ip4Config2PolicyStatic;
  673. Status= Ip4Config2->SetData (
  674. Ip4Config2,
  675. Ip4Config2DataTypePolicy,
  676. sizeof (EFI_IP4_CONFIG2_POLICY),
  677. &Policy
  678. );
  679. if (EFI_ERROR (Status)) {
  680. return Status;
  681. }
  682. }
  683. return EFI_SUCCESS;
  684. }
  685. /**
  686. Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other Http boot information.
  687. @param[in] Private Pointer to HTTP boot driver private data.
  688. @retval EFI_SUCCESS The D.O.R.A process successfully finished.
  689. @retval Others Failed to finish the D.O.R.A process.
  690. **/
  691. EFI_STATUS
  692. HttpBootDhcp4Dora (
  693. IN HTTP_BOOT_PRIVATE_DATA *Private
  694. )
  695. {
  696. EFI_DHCP4_PROTOCOL *Dhcp4;
  697. UINT32 OptCount;
  698. EFI_DHCP4_PACKET_OPTION *OptList[HTTP_BOOT_DHCP4_OPTION_MAX_NUM];
  699. UINT8 Buffer[HTTP_BOOT_DHCP4_OPTION_MAX_SIZE];
  700. EFI_DHCP4_CONFIG_DATA Config;
  701. EFI_STATUS Status;
  702. EFI_DHCP4_MODE_DATA Mode;
  703. Dhcp4 = Private->Dhcp4;
  704. ASSERT (Dhcp4 != NULL);
  705. Status = HttpBootSetIp4Policy (Private);
  706. if (EFI_ERROR (Status)) {
  707. return Status;
  708. }
  709. //
  710. // Build option list for the request packet.
  711. //
  712. OptCount = HttpBootBuildDhcp4Options (Private, OptList, Buffer);
  713. ASSERT (OptCount > 0);
  714. ZeroMem (&Config, sizeof(Config));
  715. Config.OptionCount = OptCount;
  716. Config.OptionList = OptList;
  717. Config.Dhcp4Callback = HttpBootDhcp4CallBack;
  718. Config.CallbackContext = Private;
  719. Config.DiscoverTryCount = HTTP_BOOT_DHCP_RETRIES;
  720. Config.DiscoverTimeout = mHttpDhcpTimeout;
  721. //
  722. // Configure the DHCPv4 instance for HTTP boot.
  723. //
  724. Status = Dhcp4->Configure (Dhcp4, &Config);
  725. if (EFI_ERROR (Status)) {
  726. goto ON_EXIT;
  727. }
  728. //
  729. // Initialize the record fields for DHCPv4 offer in private data.
  730. //
  731. Private->OfferNum = 0;
  732. ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
  733. ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
  734. //
  735. // Start DHCPv4 D.O.R.A. process to acquire IPv4 address.
  736. //
  737. Status = Dhcp4->Start (Dhcp4, NULL);
  738. if (EFI_ERROR (Status)) {
  739. goto ON_EXIT;
  740. }
  741. //
  742. // Get the acquired IPv4 address and store them.
  743. //
  744. Status = Dhcp4->GetModeData (Dhcp4, &Mode);
  745. if (EFI_ERROR (Status)) {
  746. goto ON_EXIT;
  747. }
  748. ASSERT (Mode.State == Dhcp4Bound);
  749. CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
  750. CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  751. CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
  752. Status = HttpBootRegisterIp4Gateway (Private);
  753. if (EFI_ERROR (Status)) {
  754. goto ON_EXIT;
  755. }
  756. AsciiPrint ("\n Station IP address is ");
  757. HttpBootShowIp4Addr (&Private->StationIp.v4);
  758. AsciiPrint ("\n");
  759. ON_EXIT:
  760. if (EFI_ERROR (Status)) {
  761. Dhcp4->Stop (Dhcp4);
  762. Dhcp4->Configure (Dhcp4, NULL);
  763. } else {
  764. ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
  765. Dhcp4->Configure (Dhcp4, &Config);
  766. }
  767. return Status;
  768. }