TianoCore EDK2 master
Loading...
Searching...
No Matches
HttpBootDhcp4.c
Go to the documentation of this file.
1
9#include "HttpBootDxe.h"
10
11//
12// This is a map from the interested DHCP4 option tags' index to the tag value.
13//
14UINT8 mInterestedDhcp4Tags[HTTP_BOOT_DHCP4_TAG_INDEX_MAX] = {
15 DHCP4_TAG_BOOTFILE_LEN,
16 DHCP4_TAG_OVERLOAD,
17 DHCP4_TAG_MSG_TYPE,
18 DHCP4_TAG_SERVER_ID,
19 DHCP4_TAG_VENDOR_CLASS_ID,
20 DHCP4_TAG_BOOTFILE,
21 DHCP4_TAG_DNS_SERVER
22};
23
24//
25// There are 4 times retries with the value of 4, 8, 16 and 32, refers to UEFI 2.5 spec.
26//
27UINT32 mHttpDhcpTimeout[4] = { 4, 8, 16, 32 };
28
39UINT32
43 IN UINT8 *Buffer
44 )
45{
47 UINT16 Value;
48 UINT32 Index;
49
50 Index = 0;
51 OptList[0] = (EFI_DHCP4_PACKET_OPTION *)Buffer;
52
53 //
54 // Append parameter request list option.
55 //
56 OptList[Index]->OpCode = DHCP4_TAG_PARA_LIST;
57 OptList[Index]->Length = 27;
58 OptEnt.Para = (HTTP_BOOT_DHCP4_OPTION_PARA *)OptList[Index]->Data;
59 OptEnt.Para->ParaList[0] = DHCP4_TAG_NETMASK;
60 OptEnt.Para->ParaList[1] = DHCP4_TAG_TIME_OFFSET;
61 OptEnt.Para->ParaList[2] = DHCP4_TAG_ROUTER;
62 OptEnt.Para->ParaList[3] = DHCP4_TAG_TIME_SERVER;
63 OptEnt.Para->ParaList[4] = DHCP4_TAG_NAME_SERVER;
64 OptEnt.Para->ParaList[5] = DHCP4_TAG_DNS_SERVER;
65 OptEnt.Para->ParaList[6] = DHCP4_TAG_HOSTNAME;
66 OptEnt.Para->ParaList[7] = DHCP4_TAG_BOOTFILE_LEN;
67 OptEnt.Para->ParaList[8] = DHCP4_TAG_DOMAINNAME;
68 OptEnt.Para->ParaList[9] = DHCP4_TAG_ROOTPATH;
69 OptEnt.Para->ParaList[10] = DHCP4_TAG_EXTEND_PATH;
70 OptEnt.Para->ParaList[11] = DHCP4_TAG_EMTU;
71 OptEnt.Para->ParaList[12] = DHCP4_TAG_TTL;
72 OptEnt.Para->ParaList[13] = DHCP4_TAG_BROADCAST;
73 OptEnt.Para->ParaList[14] = DHCP4_TAG_NIS_DOMAIN;
74 OptEnt.Para->ParaList[15] = DHCP4_TAG_NIS_SERVER;
75 OptEnt.Para->ParaList[16] = DHCP4_TAG_NTP_SERVER;
76 OptEnt.Para->ParaList[17] = DHCP4_TAG_VENDOR;
77 OptEnt.Para->ParaList[18] = DHCP4_TAG_REQUEST_IP;
78 OptEnt.Para->ParaList[19] = DHCP4_TAG_LEASE;
79 OptEnt.Para->ParaList[20] = DHCP4_TAG_SERVER_ID;
80 OptEnt.Para->ParaList[21] = DHCP4_TAG_T1;
81 OptEnt.Para->ParaList[22] = DHCP4_TAG_T2;
82 OptEnt.Para->ParaList[23] = DHCP4_TAG_VENDOR_CLASS_ID;
83 OptEnt.Para->ParaList[25] = DHCP4_TAG_BOOTFILE;
84 OptEnt.Para->ParaList[26] = DHCP4_TAG_UUID;
85 Index++;
86 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
87
88 //
89 // Append UUID/Guid-based client identifier option
90 //
91 OptList[Index]->OpCode = DHCP4_TAG_UUID;
92 OptList[Index]->Length = (UINT8)sizeof (HTTP_BOOT_DHCP4_OPTION_UUID);
93 OptEnt.Uuid = (HTTP_BOOT_DHCP4_OPTION_UUID *)OptList[Index]->Data;
94 OptEnt.Uuid->Type = 0;
95 if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *)OptEnt.Uuid->Guid))) {
96 //
97 // Zero the Guid to indicate NOT programmable if failed to get system Guid.
98 //
99 ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));
100 }
101
102 Index++;
103 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
104
105 //
106 // Append client network device interface option
107 //
108 OptList[Index]->OpCode = DHCP4_TAG_UNDI;
109 OptList[Index]->Length = (UINT8)sizeof (HTTP_BOOT_DHCP4_OPTION_UNDI);
110 OptEnt.Undi = (HTTP_BOOT_DHCP4_OPTION_UNDI *)OptList[Index]->Data;
111
112 if (Private->Nii != NULL) {
113 OptEnt.Undi->Type = Private->Nii->Type;
114 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
115 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
116 } else {
117 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;
118 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
119 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
120 }
121
122 Index++;
123 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
124
125 //
126 // Append client system architecture option
127 //
128 OptList[Index]->OpCode = DHCP4_TAG_ARCH;
129 OptList[Index]->Length = (UINT8)sizeof (HTTP_BOOT_DHCP4_OPTION_ARCH);
130 OptEnt.Arch = (HTTP_BOOT_DHCP4_OPTION_ARCH *)OptList[Index]->Data;
131 Value = HTONS (EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE);
132 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
133 Index++;
134 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
135
136 //
137 // Append vendor class identify option
138 //
139 OptList[Index]->OpCode = DHCP4_TAG_VENDOR_CLASS_ID;
140 OptList[Index]->Length = (UINT8)sizeof (HTTP_BOOT_DHCP4_OPTION_CLID);
141 OptEnt.Clid = (HTTP_BOOT_DHCP4_OPTION_CLID *)OptList[Index]->Data;
142 CopyMem (
143 OptEnt.Clid,
144 DEFAULT_CLASS_ID_DATA,
146 );
148 EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE,
149 OptEnt.Clid->ArchitectureType,
150 sizeof (OptEnt.Clid->ArchitectureType)
151 );
152
153 if (Private->Nii != NULL) {
154 CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));
155 HttpBootUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));
156 HttpBootUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));
157 }
158
159 Index++;
160
161 return Index;
162}
163
177 IN UINT8 *Buffer,
178 IN UINT32 Length,
179 IN UINT8 OptTag
180 )
181{
183 UINT32 Offset;
184
185 Option = (EFI_DHCP4_PACKET_OPTION *)Buffer;
186 Offset = 0;
187
188 while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) {
189 if (Option->OpCode == OptTag) {
190 //
191 // Found the required option.
192 //
193 return Option;
194 }
195
196 //
197 // Skip the current option to the next.
198 //
199 if (Option->OpCode == DHCP4_TAG_PAD) {
200 Offset++;
201 } else {
202 Offset += Option->Length + 2;
203 }
204
205 Option = (EFI_DHCP4_PACKET_OPTION *)(Buffer + Offset);
206 }
207
208 return NULL;
209}
210
223 IN EFI_DHCP4_PACKET *Dst,
225 )
226{
227 if (Dst->Size < Src->Length) {
228 return EFI_BUFFER_TOO_SMALL;
229 }
230
231 CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);
232 Dst->Length = Src->Length;
233
234 return EFI_SUCCESS;
235}
236
249 )
250{
251 EFI_DHCP4_PACKET *Offer;
252 EFI_DHCP4_PACKET_OPTION **Options;
253 UINTN Index;
255 BOOLEAN IsProxyOffer;
256 BOOLEAN IsHttpOffer;
257 BOOLEAN IsDnsOffer;
258 BOOLEAN IpExpressedUri;
259 UINT8 *Ptr8;
260 EFI_STATUS Status;
261 HTTP_BOOT_OFFER_TYPE OfferType;
262 EFI_IPv4_ADDRESS IpAddr;
263 BOOLEAN FileFieldOverloaded;
264
265 IsDnsOffer = FALSE;
266 IpExpressedUri = FALSE;
267 IsProxyOffer = FALSE;
268 IsHttpOffer = FALSE;
269 FileFieldOverloaded = FALSE;
270
271 ZeroMem (Cache4->OptList, sizeof (Cache4->OptList));
272
273 Offer = &Cache4->Packet.Offer;
274 Options = Cache4->OptList;
275
276 //
277 // Parse DHCPv4 options in this offer, and store the pointers.
278 // First, try to parse DHCPv4 options from the DHCP optional parameters field.
279 //
280 for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
281 Options[Index] = HttpBootParseDhcp4Options (
282 Offer->Dhcp4.Option,
283 GET_OPTION_BUFFER_LEN (Offer),
284 mInterestedDhcp4Tags[Index]
285 );
286 }
287
288 //
289 // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132.
290 // If yes, try to parse options from the BootFileName field, then ServerName field.
291 //
292 Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_OVERLOAD];
293 if (Option != NULL) {
294 if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_FILE) != 0) {
295 FileFieldOverloaded = TRUE;
296 for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
297 if (Options[Index] == NULL) {
298 Options[Index] = HttpBootParseDhcp4Options (
299 (UINT8 *)Offer->Dhcp4.Header.BootFileName,
300 sizeof (Offer->Dhcp4.Header.BootFileName),
301 mInterestedDhcp4Tags[Index]
302 );
303 }
304 }
305 }
306
307 if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_SERVER_NAME) != 0) {
308 for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
309 if (Options[Index] == NULL) {
310 Options[Index] = HttpBootParseDhcp4Options (
311 (UINT8 *)Offer->Dhcp4.Header.ServerName,
312 sizeof (Offer->Dhcp4.Header.ServerName),
313 mInterestedDhcp4Tags[Index]
314 );
315 }
316 }
317 }
318 }
319
320 //
321 // The offer with "yiaddr" is a proxy offer.
322 //
323 if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) {
324 IsProxyOffer = TRUE;
325 }
326
327 //
328 // The offer with "HTTPClient" is a Http offer.
329 //
330 Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_CLASS_ID];
331 if ((Option != NULL) && (Option->Length >= 10) &&
332 (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 10) == 0))
333 {
334 IsHttpOffer = TRUE;
335 }
336
337 //
338 // The offer with Domain Server is a DNS offer.
339 //
340 Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];
341 if (Option != NULL) {
342 IsDnsOffer = TRUE;
343 }
344
345 //
346 // Parse boot file name:
347 // Boot URI information is provided thru 'file' field in DHCP Header or option 67.
348 // According to RFC 2132, boot file name should be read from DHCP option 67 (bootfile name) if present.
349 // Otherwise, read from boot file field in DHCP header.
350 //
351 if (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
352 //
353 // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
354 // terminated string. So force to append null terminated character at the end of string.
355 //
356 Ptr8 = (UINT8 *)&Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];
357 Ptr8 += Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Length;
358 if (*(Ptr8 - 1) != '\0') {
359 *Ptr8 = '\0';
360 }
361 } else if (!FileFieldOverloaded && (Offer->Dhcp4.Header.BootFileName[0] != 0)) {
362 //
363 // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.
364 // Do not count dhcp option header here, or else will destroy the serverhostname.
365 //
366 Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)
367 (&Offer->Dhcp4.Header.BootFileName[0] -
369 }
370
371 //
372 // Http offer must have a boot URI.
373 //
374 if (IsHttpOffer && (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] == NULL)) {
375 return EFI_DEVICE_ERROR;
376 }
377
378 //
379 // Try to retrieve the IP of HTTP server from URI.
380 //
381 if (IsHttpOffer) {
382 Status = HttpParseUrl (
383 (CHAR8 *)Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
384 (UINT32)AsciiStrLen ((CHAR8 *)Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data),
385 FALSE,
386 &Cache4->UriParser
387 );
388 if (EFI_ERROR (Status)) {
389 return EFI_DEVICE_ERROR;
390 }
391
392 Status = HttpUrlGetIp4 (
393 (CHAR8 *)Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
394 Cache4->UriParser,
395 &IpAddr
396 );
397 IpExpressedUri = !EFI_ERROR (Status);
398 }
399
400 //
401 // Determine offer type of the DHCPv4 packet.
402 //
403 if (IsHttpOffer) {
404 if (IpExpressedUri) {
405 if (IsProxyOffer) {
406 OfferType = HttpOfferTypeProxyIpUri;
407 } else {
408 OfferType = IsDnsOffer ? HttpOfferTypeDhcpIpUriDns : HttpOfferTypeDhcpIpUri;
409 }
410 } else {
411 if (!IsProxyOffer) {
412 OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri;
413 } else {
414 OfferType = HttpOfferTypeProxyNameUri;
415 }
416 }
417 } else {
418 if (!IsProxyOffer) {
419 OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly;
420 } else {
421 if (Cache4->UriParser != NULL) {
422 FreePool (Cache4->UriParser);
423 }
424
425 return EFI_DEVICE_ERROR;
426 }
427 }
428
429 Cache4->OfferType = OfferType;
430 return EFI_SUCCESS;
431}
432
444 IN HTTP_BOOT_PRIVATE_DATA *Private,
445 IN EFI_DHCP4_PACKET *RcvdOffer
446 )
447{
449 EFI_DHCP4_PACKET *Offer;
450 HTTP_BOOT_OFFER_TYPE OfferType;
451 EFI_STATUS Status;
452
453 ASSERT (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM);
454 Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;
455 Offer = &Cache4->Packet.Offer;
456
457 //
458 // Cache the content of DHCPv4 packet firstly.
459 //
460 Status = HttpBootCacheDhcp4Packet (Offer, RcvdOffer);
461 if (EFI_ERROR (Status)) {
462 return Status;
463 }
464
465 //
466 // Validate the DHCPv4 packet, and parse the options and offer type.
467 //
468 if (EFI_ERROR (HttpBootParseDhcp4Packet (Cache4))) {
469 return EFI_ABORTED;
470 }
471
472 //
473 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
474 //
475 OfferType = Cache4->OfferType;
476 ASSERT (OfferType < HttpOfferTypeMax);
477 ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM);
478 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
479 Private->OfferCount[OfferType]++;
480 Private->OfferNum++;
481
482 return EFI_SUCCESS;
483}
484
491VOID
494 )
495{
496 Private->SelectIndex = 0;
497 Private->SelectProxyType = HttpOfferTypeMax;
498
499 if (Private->FilePathUri != NULL) {
500 //
501 // We are in home environment, the URI is already specified.
502 // Just need to choose a DHCP offer.
503 // The offer with DNS server address takes priority here.
504 //
505 if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0) {
506 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
507 } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {
508 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;
509 } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
510 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
511 } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0) {
512 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
513 } else if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
514 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
515 }
516 } else {
517 //
518 // We are in corporate environment.
519 //
520 // Priority1: HttpOfferTypeDhcpIpUri or HttpOfferTypeDhcpIpUriDns
521 // Priority2: HttpOfferTypeDhcpNameUriDns
522 // Priority3: HttpOfferTypeDhcpOnly + HttpOfferTypeProxyIpUri
523 // Priority4: HttpOfferTypeDhcpDns + HttpOfferTypeProxyIpUri
524 // Priority5: HttpOfferTypeDhcpDns + HttpOfferTypeProxyNameUri
525 // Priority6: HttpOfferTypeDhcpDns + HttpOfferTypeDhcpNameUri
526 //
527 if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
528 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
529 } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {
530 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;
531 } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
532 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
533 } else if ((Private->OfferCount[HttpOfferTypeDhcpOnly] > 0) &&
534 (Private->OfferCount[HttpOfferTypeProxyIpUri] > 0))
535 {
536 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
537 Private->SelectProxyType = HttpOfferTypeProxyIpUri;
538 } else if ((Private->OfferCount[HttpOfferTypeDhcpDns] > 0) &&
539 (Private->OfferCount[HttpOfferTypeProxyIpUri] > 0))
540 {
541 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
542 Private->SelectProxyType = HttpOfferTypeProxyIpUri;
543 } else if ((Private->OfferCount[HttpOfferTypeDhcpDns] > 0) &&
544 (Private->OfferCount[HttpOfferTypeProxyNameUri] > 0))
545 {
546 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
547 Private->SelectProxyType = HttpOfferTypeProxyNameUri;
548 } else if ((Private->OfferCount[HttpOfferTypeDhcpDns] > 0) &&
549 (Private->OfferCount[HttpOfferTypeDhcpNameUri] > 0))
550 {
551 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
552 Private->SelectProxyType = HttpOfferTypeDhcpNameUri;
553 }
554 }
555}
556
578EFIAPI
581 IN VOID *Context,
582 IN EFI_DHCP4_STATE CurrentState,
583 IN EFI_DHCP4_EVENT Dhcp4Event,
584 IN EFI_DHCP4_PACKET *Packet OPTIONAL,
585 OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL
586 )
587{
588 HTTP_BOOT_PRIVATE_DATA *Private;
589 EFI_DHCP4_PACKET_OPTION *MaxMsgSize;
590 UINT16 Value;
591 EFI_STATUS Status;
592 BOOLEAN Received;
593
594 if ((Dhcp4Event != Dhcp4SendDiscover) &&
595 (Dhcp4Event != Dhcp4RcvdOffer) &&
596 (Dhcp4Event != Dhcp4SendRequest) &&
597 (Dhcp4Event != Dhcp4RcvdAck) &&
598 (Dhcp4Event != Dhcp4SelectOffer))
599 {
600 return EFI_SUCCESS;
601 }
602
603 Private = (HTTP_BOOT_PRIVATE_DATA *)Context;
604
605 //
606 // Override the Maximum DHCP Message Size.
607 //
608 MaxMsgSize = HttpBootParseDhcp4Options (
609 Packet->Dhcp4.Option,
610 GET_OPTION_BUFFER_LEN (Packet),
611 DHCP4_TAG_MAXMSG
612 );
613 if (MaxMsgSize != NULL) {
614 Value = HTONS (HTTP_BOOT_DHCP4_PACKET_MAX_SIZE);
615 CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));
616 }
617
618 //
619 // Callback to user if any packets sent or received.
620 //
621 if ((Private->HttpBootCallback != NULL) && (Dhcp4Event != Dhcp4SelectOffer)) {
622 Received = (BOOLEAN)(Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck);
623 Status = Private->HttpBootCallback->Callback (
624 Private->HttpBootCallback,
626 Received,
627 Packet->Length,
628 &Packet->Dhcp4
629 );
630 if (EFI_ERROR (Status)) {
631 return EFI_ABORTED;
632 }
633 }
634
635 Status = EFI_SUCCESS;
636 switch (Dhcp4Event) {
637 case Dhcp4RcvdOffer:
638 Status = EFI_NOT_READY;
639 if (Packet->Length > HTTP_BOOT_DHCP4_PACKET_MAX_SIZE) {
640 //
641 // Ignore the incoming packets which exceed the maximum length.
642 //
643 break;
644 }
645
646 if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) {
647 //
648 // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
649 // the OfferIndex and OfferCount.
650 // If error happens, just ignore this packet and continue to wait more offer.
651 //
652 HttpBootCacheDhcp4Offer (Private, Packet);
653 }
654
655 break;
656
657 case Dhcp4SelectOffer:
658 //
659 // Select offer according to the priority in UEFI spec, and record the SelectIndex
660 // and SelectProxyType.
661 //
662 HttpBootSelectDhcpOffer (Private);
663
664 if (Private->SelectIndex == 0) {
665 Status = EFI_ABORTED;
666 } else {
667 *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;
668 }
669
670 break;
671
672 default:
673 break;
674 }
675
676 return Status;
677}
678
691 )
692{
693 EFI_STATUS Status;
694 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
695
696 ASSERT (!Private->UsingIpv6);
697
698 Ip4Config2 = Private->Ip4Config2;
699
700 //
701 // Configure the gateway if valid.
702 //
703 if (!EFI_IP4_EQUAL (&Private->GatewayIp, &mZeroIp4Addr)) {
704 Status = Ip4Config2->SetData (
705 Ip4Config2,
707 sizeof (EFI_IPv4_ADDRESS),
708 &Private->GatewayIp
709 );
710 if (EFI_ERROR (Status)) {
711 return Status;
712 }
713 }
714
715 return EFI_SUCCESS;
716}
717
732 IN HTTP_BOOT_PRIVATE_DATA *Private,
733 IN UINTN DataLength,
734 IN VOID *DnsServerData
735 )
736{
737 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
738
739 ASSERT (!Private->UsingIpv6);
740
741 Ip4Config2 = Private->Ip4Config2;
742
743 return Ip4Config2->SetData (
744 Ip4Config2,
746 DataLength,
747 DnsServerData
748 );
749}
750
763 )
764{
766 EFI_STATUS Status;
767 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
768 UINTN DataSize;
769
770 Ip4Config2 = Private->Ip4Config2;
771
772 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
773 Status = Ip4Config2->GetData (
774 Ip4Config2,
776 &DataSize,
777 &Policy
778 );
779 if (EFI_ERROR (Status)) {
780 return Status;
781 }
782
783 if (Policy != Ip4Config2PolicyStatic) {
784 Policy = Ip4Config2PolicyStatic;
785 Status = Ip4Config2->SetData (
786 Ip4Config2,
788 sizeof (EFI_IP4_CONFIG2_POLICY),
789 &Policy
790 );
791 if (EFI_ERROR (Status)) {
792 return Status;
793 }
794 }
795
796 return EFI_SUCCESS;
797}
798
811 )
812{
813 EFI_DHCP4_PROTOCOL *Dhcp4;
814 UINT32 OptCount;
815 EFI_DHCP4_PACKET_OPTION *OptList[HTTP_BOOT_DHCP4_OPTION_MAX_NUM];
816 UINT8 Buffer[HTTP_BOOT_DHCP4_OPTION_MAX_SIZE];
818 EFI_STATUS Status;
820
821 Dhcp4 = Private->Dhcp4;
822 ASSERT (Dhcp4 != NULL);
823
824 Status = HttpBootSetIp4Policy (Private);
825 if (EFI_ERROR (Status)) {
826 return Status;
827 }
828
829 //
830 // Build option list for the request packet.
831 //
832 OptCount = HttpBootBuildDhcp4Options (Private, OptList, Buffer);
833 ASSERT (OptCount > 0);
834
835 ZeroMem (&Config, sizeof (Config));
836 Config.OptionCount = OptCount;
837 Config.OptionList = OptList;
839 Config.CallbackContext = Private;
840 Config.DiscoverTryCount = HTTP_BOOT_DHCP_RETRIES;
841 Config.DiscoverTimeout = mHttpDhcpTimeout;
842
843 //
844 // Configure the DHCPv4 instance for HTTP boot.
845 //
846 Status = Dhcp4->Configure (Dhcp4, &Config);
847 if (EFI_ERROR (Status)) {
848 goto ON_EXIT;
849 }
850
851 //
852 // Initialize the record fields for DHCPv4 offer in private data.
853 //
854 Private->OfferNum = 0;
855 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
856 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
857
858 //
859 // Start DHCPv4 D.O.R.A. process to acquire IPv4 address.
860 //
861 Status = Dhcp4->Start (Dhcp4, NULL);
862 if (EFI_ERROR (Status)) {
863 goto ON_EXIT;
864 }
865
866 //
867 // Get the acquired IPv4 address and store them.
868 //
869 Status = Dhcp4->GetModeData (Dhcp4, &Mode);
870 if (EFI_ERROR (Status)) {
871 goto ON_EXIT;
872 }
873
874 ASSERT (Mode.State == Dhcp4Bound);
875 CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
876 CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
877 CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
878
879 Status = HttpBootRegisterIp4Gateway (Private);
880 if (EFI_ERROR (Status)) {
881 goto ON_EXIT;
882 }
883
884 AsciiPrint ("\n Station IP address is ");
885 HttpBootShowIp4Addr (&Private->StationIp.v4);
886 AsciiPrint ("\n");
887
888ON_EXIT:
889 if (EFI_ERROR (Status)) {
890 Dhcp4->Stop (Dhcp4);
891 Dhcp4->Configure (Dhcp4, NULL);
892 } else {
893 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
894 Dhcp4->Configure (Dhcp4, &Config);
895 }
896
897 return Status;
898}
UINT64 UINTN
UINTN EFIAPI AsciiStrLen(IN CONST CHAR8 *String)
Definition: String.c:641
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_DHCP4_STATE
Definition: Dhcp4.h:98
@ Dhcp4Bound
Definition: Dhcp4.h:118
EFI_DHCP4_EVENT
Definition: Dhcp4.h:141
@ Dhcp4RcvdAck
Definition: Dhcp4.h:161
@ Dhcp4SendRequest
Definition: Dhcp4.h:157
@ Dhcp4SelectOffer
Definition: Dhcp4.h:153
@ Dhcp4RcvdOffer
Definition: Dhcp4.h:149
@ Dhcp4SendDiscover
Definition: Dhcp4.h:145
#define DHCP4_TAG_PAD
Definition: Dhcp.h:19
VOID EFIAPI FreePool(IN VOID *Buffer)
@ HttpBootDhcp4
EFI_STATUS HttpBootParseDhcp4Packet(IN HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4)
EFI_STATUS HttpBootSetIp4Policy(IN HTTP_BOOT_PRIVATE_DATA *Private)
EFI_STATUS HttpBootRegisterIp4Gateway(IN HTTP_BOOT_PRIVATE_DATA *Private)
EFI_DHCP4_PACKET_OPTION * HttpBootParseDhcp4Options(IN UINT8 *Buffer, IN UINT32 Length, IN UINT8 OptTag)
VOID HttpBootSelectDhcpOffer(IN HTTP_BOOT_PRIVATE_DATA *Private)
EFI_STATUS HttpBootDhcp4Dora(IN HTTP_BOOT_PRIVATE_DATA *Private)
EFI_STATUS HttpBootRegisterIp4Dns(IN HTTP_BOOT_PRIVATE_DATA *Private, IN UINTN DataLength, IN VOID *DnsServerData)
EFI_STATUS EFIAPI HttpBootDhcp4CallBack(IN EFI_DHCP4_PROTOCOL *This, IN VOID *Context, IN EFI_DHCP4_STATE CurrentState, IN EFI_DHCP4_EVENT Dhcp4Event, IN EFI_DHCP4_PACKET *Packet OPTIONAL, OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL)
EFI_STATUS HttpBootCacheDhcp4Packet(IN EFI_DHCP4_PACKET *Dst, IN EFI_DHCP4_PACKET *Src)
EFI_STATUS HttpBootCacheDhcp4Offer(IN HTTP_BOOT_PRIVATE_DATA *Private, IN EFI_DHCP4_PACKET *RcvdOffer)
UINT32 HttpBootBuildDhcp4Options(IN HTTP_BOOT_PRIVATE_DATA *Private, OUT EFI_DHCP4_PACKET_OPTION **OptList, IN UINT8 *Buffer)
Definition: HttpBootDhcp4.c:40
HTTP_BOOT_OFFER_TYPE
Definition: HttpBootDhcp4.h:56
VOID HttpBootUintnToAscDecWithFormat(IN UINTN Number, IN UINT8 *Buffer, IN INTN Length)
VOID HttpBootShowIp4Addr(IN EFI_IPv4_ADDRESS *Ip)
EFI_STATUS EFIAPI HttpUrlGetIp4(IN CHAR8 *Url, IN VOID *UrlParser, OUT EFI_IPv4_ADDRESS *Ip4Address)
Definition: DxeHttpLib.c:565
EFI_STATUS EFIAPI HttpParseUrl(IN CHAR8 *Url, IN UINT32 Length, IN BOOLEAN IsConnectMethod, OUT VOID **UrlParser)
Definition: DxeHttpLib.c:370
EFI_IP4_CONFIG2_POLICY
Definition: Ip4Config2.h:127
@ Ip4Config2PolicyStatic
Definition: Ip4Config2.h:135
@ Ip4Config2DataTypeGateway
Definition: Ip4Config2.h:63
@ Ip4Config2DataTypePolicy
Definition: Ip4Config2.h:43
@ Ip4Config2DataTypeDnsServer
Definition: Ip4Config2.h:74
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OFFSET_OF(TYPE, Field)
Definition: Base.h:758
#define OUT
Definition: Base.h:284
EFI_STATUS EFIAPI NetLibGetSystemGuid(OUT EFI_GUID *SystemGuid)
Definition: DxeNetLib.c:3325
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
UINTN EFIAPI AsciiPrint(IN CONST CHAR8 *Format,...)
Definition: UefiLibPrint.c:250
VOID * CallbackContext
Definition: Dhcp4.h:282
EFI_DHCP4_PACKET_OPTION ** OptionList
Definition: Dhcp4.h:294
UINT32 * DiscoverTimeout
Definition: Dhcp4.h:253
UINT32 OptionCount
Definition: Dhcp4.h:286
EFI_DHCP4_CALLBACK Dhcp4Callback
Definition: Dhcp4.h:278
UINT32 DiscoverTryCount
Definition: Dhcp4.h:247
EFI_IPv4_ADDRESS YourAddr
Client IP address from server.
Definition: Dhcp4.h:60
EFI_IPv4_ADDRESS SubnetMask
Definition: Dhcp4.h:327
EFI_DHCP4_STATE State
Definition: Dhcp4.h:301
EFI_IPv4_ADDRESS RouterAddress
Definition: Dhcp4.h:323
EFI_IPv4_ADDRESS ClientAddress
Definition: Dhcp4.h:310
UINT8 Option[1]
Definition: Dhcp4.h:93
EFI_DHCP4_HEADER Header
Definition: Dhcp4.h:85
Definition: Base.h:213