HttpBootDhcp4.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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 programmable 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. {
  286. IsHttpOffer = TRUE;
  287. }
  288. //
  289. // The offer with Domain Server is a DNS offer.
  290. //
  291. Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];
  292. if (Option != NULL) {
  293. IsDnsOffer = TRUE;
  294. }
  295. //
  296. // Parse boot file name:
  297. // Boot URI information is provided thru 'file' field in DHCP Header or option 67.
  298. // According to RFC 2132, boot file name should be read from DHCP option 67 (bootfile name) if present.
  299. // Otherwise, read from boot file field in DHCP header.
  300. //
  301. if (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
  302. //
  303. // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
  304. // terminated string. So force to append null terminated character at the end of string.
  305. //
  306. Ptr8 = (UINT8 *)&Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];
  307. Ptr8 += Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Length;
  308. if (*(Ptr8 - 1) != '\0') {
  309. *Ptr8 = '\0';
  310. }
  311. } else if (!FileFieldOverloaded && (Offer->Dhcp4.Header.BootFileName[0] != 0)) {
  312. //
  313. // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.
  314. // Do not count dhcp option header here, or else will destroy the serverhostname.
  315. //
  316. Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)
  317. (&Offer->Dhcp4.Header.BootFileName[0] -
  318. OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));
  319. }
  320. //
  321. // Http offer must have a boot URI.
  322. //
  323. if (IsHttpOffer && (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] == NULL)) {
  324. return EFI_DEVICE_ERROR;
  325. }
  326. //
  327. // Try to retrieve the IP of HTTP server from URI.
  328. //
  329. if (IsHttpOffer) {
  330. Status = HttpParseUrl (
  331. (CHAR8 *)Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
  332. (UINT32)AsciiStrLen ((CHAR8 *)Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data),
  333. FALSE,
  334. &Cache4->UriParser
  335. );
  336. if (EFI_ERROR (Status)) {
  337. return EFI_DEVICE_ERROR;
  338. }
  339. Status = HttpUrlGetIp4 (
  340. (CHAR8 *)Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
  341. Cache4->UriParser,
  342. &IpAddr
  343. );
  344. IpExpressedUri = !EFI_ERROR (Status);
  345. }
  346. //
  347. // Determine offer type of the DHCPv4 packet.
  348. //
  349. if (IsHttpOffer) {
  350. if (IpExpressedUri) {
  351. if (IsProxyOffer) {
  352. OfferType = HttpOfferTypeProxyIpUri;
  353. } else {
  354. OfferType = IsDnsOffer ? HttpOfferTypeDhcpIpUriDns : HttpOfferTypeDhcpIpUri;
  355. }
  356. } else {
  357. if (!IsProxyOffer) {
  358. OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri;
  359. } else {
  360. OfferType = HttpOfferTypeProxyNameUri;
  361. }
  362. }
  363. } else {
  364. if (!IsProxyOffer) {
  365. OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly;
  366. } else {
  367. if (Cache4->UriParser != NULL) {
  368. FreePool (Cache4->UriParser);
  369. }
  370. return EFI_DEVICE_ERROR;
  371. }
  372. }
  373. Cache4->OfferType = OfferType;
  374. return EFI_SUCCESS;
  375. }
  376. /**
  377. Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.
  378. @param[in] Private Pointer to HTTP boot driver private data.
  379. @param[in] RcvdOffer Pointer to the received offer packet.
  380. @retval EFI_SUCCESS Cache and parse the packet successfully.
  381. @retval Others Operation failed.
  382. **/
  383. EFI_STATUS
  384. HttpBootCacheDhcp4Offer (
  385. IN HTTP_BOOT_PRIVATE_DATA *Private,
  386. IN EFI_DHCP4_PACKET *RcvdOffer
  387. )
  388. {
  389. HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4;
  390. EFI_DHCP4_PACKET *Offer;
  391. HTTP_BOOT_OFFER_TYPE OfferType;
  392. EFI_STATUS Status;
  393. ASSERT (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM);
  394. Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;
  395. Offer = &Cache4->Packet.Offer;
  396. //
  397. // Cache the content of DHCPv4 packet firstly.
  398. //
  399. Status = HttpBootCacheDhcp4Packet (Offer, RcvdOffer);
  400. if (EFI_ERROR (Status)) {
  401. return Status;
  402. }
  403. //
  404. // Validate the DHCPv4 packet, and parse the options and offer type.
  405. //
  406. if (EFI_ERROR (HttpBootParseDhcp4Packet (Cache4))) {
  407. return EFI_ABORTED;
  408. }
  409. //
  410. // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
  411. //
  412. OfferType = Cache4->OfferType;
  413. ASSERT (OfferType < HttpOfferTypeMax);
  414. ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM);
  415. Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
  416. Private->OfferCount[OfferType]++;
  417. Private->OfferNum++;
  418. return EFI_SUCCESS;
  419. }
  420. /**
  421. Select an DHCPv4 or DHCP6 offer, and record SelectIndex and SelectProxyType.
  422. @param[in] Private Pointer to HTTP boot driver private data.
  423. **/
  424. VOID
  425. HttpBootSelectDhcpOffer (
  426. IN HTTP_BOOT_PRIVATE_DATA *Private
  427. )
  428. {
  429. Private->SelectIndex = 0;
  430. Private->SelectProxyType = HttpOfferTypeMax;
  431. if (Private->FilePathUri != NULL) {
  432. //
  433. // We are in home environment, the URI is already specified.
  434. // Just need to choose a DHCP offer.
  435. // The offer with DNS server address takes priority here.
  436. //
  437. if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0) {
  438. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
  439. } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {
  440. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;
  441. } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
  442. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
  443. } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0) {
  444. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
  445. } else if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
  446. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
  447. }
  448. } else {
  449. //
  450. // We are in corporate environment.
  451. //
  452. // Priority1: HttpOfferTypeDhcpIpUri or HttpOfferTypeDhcpIpUriDns
  453. // Priority2: HttpOfferTypeDhcpNameUriDns
  454. // Priority3: HttpOfferTypeDhcpOnly + HttpOfferTypeProxyIpUri
  455. // Priority4: HttpOfferTypeDhcpDns + HttpOfferTypeProxyIpUri
  456. // Priority5: HttpOfferTypeDhcpDns + HttpOfferTypeProxyNameUri
  457. // Priority6: HttpOfferTypeDhcpDns + HttpOfferTypeDhcpNameUri
  458. //
  459. if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
  460. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
  461. } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {
  462. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;
  463. } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
  464. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
  465. } else if ((Private->OfferCount[HttpOfferTypeDhcpOnly] > 0) &&
  466. (Private->OfferCount[HttpOfferTypeProxyIpUri] > 0))
  467. {
  468. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
  469. Private->SelectProxyType = HttpOfferTypeProxyIpUri;
  470. } else if ((Private->OfferCount[HttpOfferTypeDhcpDns] > 0) &&
  471. (Private->OfferCount[HttpOfferTypeProxyIpUri] > 0))
  472. {
  473. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
  474. Private->SelectProxyType = HttpOfferTypeProxyIpUri;
  475. } else if ((Private->OfferCount[HttpOfferTypeDhcpDns] > 0) &&
  476. (Private->OfferCount[HttpOfferTypeProxyNameUri] > 0))
  477. {
  478. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
  479. Private->SelectProxyType = HttpOfferTypeProxyNameUri;
  480. } else if ((Private->OfferCount[HttpOfferTypeDhcpDns] > 0) &&
  481. (Private->OfferCount[HttpOfferTypeDhcpNameUri] > 0))
  482. {
  483. Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
  484. Private->SelectProxyType = HttpOfferTypeDhcpNameUri;
  485. }
  486. }
  487. }
  488. /**
  489. EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
  490. to intercept events that occurred in the configuration process.
  491. @param[in] This Pointer to the EFI DHCPv4 Protocol.
  492. @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().
  493. @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.
  494. @param[in] Dhcp4Event The event that occurs in the current state, which usually means a
  495. state transition.
  496. @param[in] Packet The DHCPv4 packet that is going to be sent or already received.
  497. @param[out] NewPacket The packet that is used to replace the above Packet.
  498. @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
  499. @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
  500. driver will continue to wait for more DHCPOFFER packets until the
  501. retry timeout expires.
  502. @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process
  503. and return to the Dhcp4Init or Dhcp4InitReboot state.
  504. **/
  505. EFI_STATUS
  506. EFIAPI
  507. HttpBootDhcp4CallBack (
  508. IN EFI_DHCP4_PROTOCOL *This,
  509. IN VOID *Context,
  510. IN EFI_DHCP4_STATE CurrentState,
  511. IN EFI_DHCP4_EVENT Dhcp4Event,
  512. IN EFI_DHCP4_PACKET *Packet OPTIONAL,
  513. OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL
  514. )
  515. {
  516. HTTP_BOOT_PRIVATE_DATA *Private;
  517. EFI_DHCP4_PACKET_OPTION *MaxMsgSize;
  518. UINT16 Value;
  519. EFI_STATUS Status;
  520. BOOLEAN Received;
  521. if ((Dhcp4Event != Dhcp4SendDiscover) &&
  522. (Dhcp4Event != Dhcp4RcvdOffer) &&
  523. (Dhcp4Event != Dhcp4SendRequest) &&
  524. (Dhcp4Event != Dhcp4RcvdAck) &&
  525. (Dhcp4Event != Dhcp4SelectOffer))
  526. {
  527. return EFI_SUCCESS;
  528. }
  529. Private = (HTTP_BOOT_PRIVATE_DATA *)Context;
  530. //
  531. // Override the Maximum DHCP Message Size.
  532. //
  533. MaxMsgSize = HttpBootParseDhcp4Options (
  534. Packet->Dhcp4.Option,
  535. GET_OPTION_BUFFER_LEN (Packet),
  536. DHCP4_TAG_MAXMSG
  537. );
  538. if (MaxMsgSize != NULL) {
  539. Value = HTONS (HTTP_BOOT_DHCP4_PACKET_MAX_SIZE);
  540. CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));
  541. }
  542. //
  543. // Callback to user if any packets sent or received.
  544. //
  545. if ((Private->HttpBootCallback != NULL) && (Dhcp4Event != Dhcp4SelectOffer)) {
  546. Received = (BOOLEAN)(Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck);
  547. Status = Private->HttpBootCallback->Callback (
  548. Private->HttpBootCallback,
  549. HttpBootDhcp4,
  550. Received,
  551. Packet->Length,
  552. &Packet->Dhcp4
  553. );
  554. if (EFI_ERROR (Status)) {
  555. return EFI_ABORTED;
  556. }
  557. }
  558. Status = EFI_SUCCESS;
  559. switch (Dhcp4Event) {
  560. case Dhcp4RcvdOffer:
  561. Status = EFI_NOT_READY;
  562. if (Packet->Length > HTTP_BOOT_DHCP4_PACKET_MAX_SIZE) {
  563. //
  564. // Ignore the incoming packets which exceed the maximum length.
  565. //
  566. break;
  567. }
  568. if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) {
  569. //
  570. // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
  571. // the OfferIndex and OfferCount.
  572. // If error happens, just ignore this packet and continue to wait more offer.
  573. //
  574. HttpBootCacheDhcp4Offer (Private, Packet);
  575. }
  576. break;
  577. case Dhcp4SelectOffer:
  578. //
  579. // Select offer according to the priority in UEFI spec, and record the SelectIndex
  580. // and SelectProxyType.
  581. //
  582. HttpBootSelectDhcpOffer (Private);
  583. if (Private->SelectIndex == 0) {
  584. Status = EFI_ABORTED;
  585. } else {
  586. *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;
  587. }
  588. break;
  589. default:
  590. break;
  591. }
  592. return Status;
  593. }
  594. /**
  595. This function will register the IPv4 gateway address to the network device.
  596. @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
  597. @retval EFI_SUCCESS The new IP configuration has been configured successfully.
  598. @retval Others Failed to configure the address.
  599. **/
  600. EFI_STATUS
  601. HttpBootRegisterIp4Gateway (
  602. IN HTTP_BOOT_PRIVATE_DATA *Private
  603. )
  604. {
  605. EFI_STATUS Status;
  606. EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
  607. ASSERT (!Private->UsingIpv6);
  608. Ip4Config2 = Private->Ip4Config2;
  609. //
  610. // Configure the gateway if valid.
  611. //
  612. if (!EFI_IP4_EQUAL (&Private->GatewayIp, &mZeroIp4Addr)) {
  613. Status = Ip4Config2->SetData (
  614. Ip4Config2,
  615. Ip4Config2DataTypeGateway,
  616. sizeof (EFI_IPv4_ADDRESS),
  617. &Private->GatewayIp
  618. );
  619. if (EFI_ERROR (Status)) {
  620. return Status;
  621. }
  622. }
  623. return EFI_SUCCESS;
  624. }
  625. /**
  626. This function will register the default DNS addresses to the network device.
  627. @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
  628. @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes.
  629. @param[in] DnsServerData Point a list of DNS server address in an array
  630. of EFI_IPv4_ADDRESS instances.
  631. @retval EFI_SUCCESS The DNS configuration has been configured successfully.
  632. @retval Others Failed to configure the address.
  633. **/
  634. EFI_STATUS
  635. HttpBootRegisterIp4Dns (
  636. IN HTTP_BOOT_PRIVATE_DATA *Private,
  637. IN UINTN DataLength,
  638. IN VOID *DnsServerData
  639. )
  640. {
  641. EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
  642. ASSERT (!Private->UsingIpv6);
  643. Ip4Config2 = Private->Ip4Config2;
  644. return Ip4Config2->SetData (
  645. Ip4Config2,
  646. Ip4Config2DataTypeDnsServer,
  647. DataLength,
  648. DnsServerData
  649. );
  650. }
  651. /**
  652. This function will switch the IP4 configuration policy to Static.
  653. @param[in] Private Pointer to HTTP boot driver private data.
  654. @retval EFI_SUCCESS The policy is already configured to static.
  655. @retval Others Other error as indicated..
  656. **/
  657. EFI_STATUS
  658. HttpBootSetIp4Policy (
  659. IN HTTP_BOOT_PRIVATE_DATA *Private
  660. )
  661. {
  662. EFI_IP4_CONFIG2_POLICY Policy;
  663. EFI_STATUS Status;
  664. EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
  665. UINTN DataSize;
  666. Ip4Config2 = Private->Ip4Config2;
  667. DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
  668. Status = Ip4Config2->GetData (
  669. Ip4Config2,
  670. Ip4Config2DataTypePolicy,
  671. &DataSize,
  672. &Policy
  673. );
  674. if (EFI_ERROR (Status)) {
  675. return Status;
  676. }
  677. if (Policy != Ip4Config2PolicyStatic) {
  678. Policy = Ip4Config2PolicyStatic;
  679. Status = Ip4Config2->SetData (
  680. Ip4Config2,
  681. Ip4Config2DataTypePolicy,
  682. sizeof (EFI_IP4_CONFIG2_POLICY),
  683. &Policy
  684. );
  685. if (EFI_ERROR (Status)) {
  686. return Status;
  687. }
  688. }
  689. return EFI_SUCCESS;
  690. }
  691. /**
  692. Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other Http boot information.
  693. @param[in] Private Pointer to HTTP boot driver private data.
  694. @retval EFI_SUCCESS The D.O.R.A process successfully finished.
  695. @retval Others Failed to finish the D.O.R.A process.
  696. **/
  697. EFI_STATUS
  698. HttpBootDhcp4Dora (
  699. IN HTTP_BOOT_PRIVATE_DATA *Private
  700. )
  701. {
  702. EFI_DHCP4_PROTOCOL *Dhcp4;
  703. UINT32 OptCount;
  704. EFI_DHCP4_PACKET_OPTION *OptList[HTTP_BOOT_DHCP4_OPTION_MAX_NUM];
  705. UINT8 Buffer[HTTP_BOOT_DHCP4_OPTION_MAX_SIZE];
  706. EFI_DHCP4_CONFIG_DATA Config;
  707. EFI_STATUS Status;
  708. EFI_DHCP4_MODE_DATA Mode;
  709. Dhcp4 = Private->Dhcp4;
  710. ASSERT (Dhcp4 != NULL);
  711. Status = HttpBootSetIp4Policy (Private);
  712. if (EFI_ERROR (Status)) {
  713. return Status;
  714. }
  715. //
  716. // Build option list for the request packet.
  717. //
  718. OptCount = HttpBootBuildDhcp4Options (Private, OptList, Buffer);
  719. ASSERT (OptCount > 0);
  720. ZeroMem (&Config, sizeof (Config));
  721. Config.OptionCount = OptCount;
  722. Config.OptionList = OptList;
  723. Config.Dhcp4Callback = HttpBootDhcp4CallBack;
  724. Config.CallbackContext = Private;
  725. Config.DiscoverTryCount = HTTP_BOOT_DHCP_RETRIES;
  726. Config.DiscoverTimeout = mHttpDhcpTimeout;
  727. //
  728. // Configure the DHCPv4 instance for HTTP boot.
  729. //
  730. Status = Dhcp4->Configure (Dhcp4, &Config);
  731. if (EFI_ERROR (Status)) {
  732. goto ON_EXIT;
  733. }
  734. //
  735. // Initialize the record fields for DHCPv4 offer in private data.
  736. //
  737. Private->OfferNum = 0;
  738. ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
  739. ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
  740. //
  741. // Start DHCPv4 D.O.R.A. process to acquire IPv4 address.
  742. //
  743. Status = Dhcp4->Start (Dhcp4, NULL);
  744. if (EFI_ERROR (Status)) {
  745. goto ON_EXIT;
  746. }
  747. //
  748. // Get the acquired IPv4 address and store them.
  749. //
  750. Status = Dhcp4->GetModeData (Dhcp4, &Mode);
  751. if (EFI_ERROR (Status)) {
  752. goto ON_EXIT;
  753. }
  754. ASSERT (Mode.State == Dhcp4Bound);
  755. CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
  756. CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
  757. CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
  758. Status = HttpBootRegisterIp4Gateway (Private);
  759. if (EFI_ERROR (Status)) {
  760. goto ON_EXIT;
  761. }
  762. AsciiPrint ("\n Station IP address is ");
  763. HttpBootShowIp4Addr (&Private->StationIp.v4);
  764. AsciiPrint ("\n");
  765. ON_EXIT:
  766. if (EFI_ERROR (Status)) {
  767. Dhcp4->Stop (Dhcp4);
  768. Dhcp4->Configure (Dhcp4, NULL);
  769. } else {
  770. ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
  771. Dhcp4->Configure (Dhcp4, &Config);
  772. }
  773. return Status;
  774. }