]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpBootDxe/HttpBootDhcp4.c
NetworkPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootDhcp4.c
1 /** @file
2 Functions implementation related with DHCPv4 for HTTP boot driver.
3
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "HttpBootDxe.h"
10
11 //
12 // This is a map from the interested DHCP4 option tags' index to the tag value.
13 //
14 UINT8 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 //
27 UINT32 mHttpDhcpTimeout[4] = {4, 8, 16, 32};
28
29 /**
30 Build the options buffer for the DHCPv4 request packet.
31
32 @param[in] Private Pointer to HTTP boot driver private data.
33 @param[out] OptList Pointer to the option pointer array.
34 @param[in] Buffer Pointer to the buffer to contain the option list.
35
36 @return Index The count of the built-in options.
37
38 **/
39 UINT32
40 HttpBootBuildDhcp4Options (
41 IN HTTP_BOOT_PRIVATE_DATA *Private,
42 OUT EFI_DHCP4_PACKET_OPTION **OptList,
43 IN UINT8 *Buffer
44 )
45 {
46 HTTP_BOOT_DHCP4_OPTION_ENTRY OptEnt;
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 programable if failed to get system Guid.
98 //
99 ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));
100 }
101 Index++;
102 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
103
104 //
105 // Append client network device interface option
106 //
107 OptList[Index]->OpCode = DHCP4_TAG_UNDI;
108 OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UNDI);
109 OptEnt.Undi = (HTTP_BOOT_DHCP4_OPTION_UNDI *) OptList[Index]->Data;
110
111 if (Private->Nii != NULL) {
112 OptEnt.Undi->Type = Private->Nii->Type;
113 OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
114 OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
115 } else {
116 OptEnt.Undi->Type = DEFAULT_UNDI_TYPE;
117 OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
118 OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
119 }
120
121 Index++;
122 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
123
124 //
125 // Append client system architecture option
126 //
127 OptList[Index]->OpCode = DHCP4_TAG_ARCH;
128 OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_ARCH);
129 OptEnt.Arch = (HTTP_BOOT_DHCP4_OPTION_ARCH *) OptList[Index]->Data;
130 Value = HTONS (EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE);
131 CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
132 Index++;
133 OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
134
135 //
136 // Append vendor class identify option
137 //
138 OptList[Index]->OpCode = DHCP4_TAG_VENDOR_CLASS_ID;
139 OptList[Index]->Length = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_CLID);
140 OptEnt.Clid = (HTTP_BOOT_DHCP4_OPTION_CLID *) OptList[Index]->Data;
141 CopyMem (
142 OptEnt.Clid,
143 DEFAULT_CLASS_ID_DATA,
144 sizeof (HTTP_BOOT_DHCP4_OPTION_CLID)
145 );
146 HttpBootUintnToAscDecWithFormat (
147 EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE,
148 OptEnt.Clid->ArchitectureType,
149 sizeof (OptEnt.Clid->ArchitectureType)
150 );
151
152 if (Private->Nii != NULL) {
153 CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));
154 HttpBootUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));
155 HttpBootUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));
156 }
157
158 Index++;
159
160 return Index;
161 }
162
163 /**
164 Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.
165
166 @param[in] Buffer Pointer to the option buffer.
167 @param[in] Length Length of the option buffer.
168 @param[in] OptTag Tag of the required option.
169
170 @retval NULL Failed to find the required option.
171 @retval Others The position of the required option.
172
173 **/
174 EFI_DHCP4_PACKET_OPTION *
175 HttpBootParseDhcp4Options (
176 IN UINT8 *Buffer,
177 IN UINT32 Length,
178 IN UINT8 OptTag
179 )
180 {
181 EFI_DHCP4_PACKET_OPTION *Option;
182 UINT32 Offset;
183
184 Option = (EFI_DHCP4_PACKET_OPTION *) Buffer;
185 Offset = 0;
186
187 while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) {
188
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
211 /**
212 Cache the DHCPv4 packet.
213
214 @param[in] Dst Pointer to the cache buffer for DHCPv4 packet.
215 @param[in] Src Pointer to the DHCPv4 packet to be cached.
216
217 @retval EFI_SUCCESS Packet is copied.
218 @retval EFI_BUFFER_TOO_SMALL Cache buffer is not big enough to hold the packet.
219
220 **/
221 EFI_STATUS
222 HttpBootCacheDhcp4Packet (
223 IN EFI_DHCP4_PACKET *Dst,
224 IN EFI_DHCP4_PACKET *Src
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
237 /**
238 Parse the cached DHCPv4 packet, including all the options.
239
240 @param[in] Cache4 Pointer to cached DHCPv4 packet.
241
242 @retval EFI_SUCCESS Parsed the DHCPv4 packet successfully.
243 @retval EFI_DEVICE_ERROR Failed to parse an invalid packet.
244
245 **/
246 EFI_STATUS
247 HttpBootParseDhcp4Packet (
248 IN HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4
249 )
250 {
251 EFI_DHCP4_PACKET *Offer;
252 EFI_DHCP4_PACKET_OPTION **Options;
253 UINTN Index;
254 EFI_DHCP4_PACKET_OPTION *Option;
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 // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132.
289 // If yes, try to parse options from the BootFileName field, then ServerName field.
290 //
291 Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_OVERLOAD];
292 if (Option != NULL) {
293 if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_FILE) != 0) {
294 FileFieldOverloaded = TRUE;
295 for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
296 if (Options[Index] == NULL) {
297 Options[Index] = HttpBootParseDhcp4Options (
298 (UINT8 *) Offer->Dhcp4.Header.BootFileName,
299 sizeof (Offer->Dhcp4.Header.BootFileName),
300 mInterestedDhcp4Tags[Index]
301 );
302 }
303 }
304 }
305 if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_SERVER_NAME) != 0) {
306 for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
307 if (Options[Index] == NULL) {
308 Options[Index] = HttpBootParseDhcp4Options (
309 (UINT8 *) Offer->Dhcp4.Header.ServerName,
310 sizeof (Offer->Dhcp4.Header.ServerName),
311 mInterestedDhcp4Tags[Index]
312 );
313 }
314 }
315 }
316 }
317
318 //
319 // The offer with "yiaddr" is a proxy offer.
320 //
321 if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) {
322 IsProxyOffer = TRUE;
323 }
324
325 //
326 // The offer with "HTTPClient" is a Http offer.
327 //
328 Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_CLASS_ID];
329 if ((Option != NULL) && (Option->Length >= 10) &&
330 (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 10) == 0)) {
331 IsHttpOffer = TRUE;
332 }
333
334 //
335 // The offer with Domain Server is a DNS offer.
336 //
337 Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];
338 if (Option != NULL) {
339 IsDnsOffer = TRUE;
340 }
341
342 //
343 // Parse boot file name:
344 // Boot URI information is provided thru 'file' field in DHCP Header or option 67.
345 // According to RFC 2132, boot file name should be read from DHCP option 67 (bootfile name) if present.
346 // Otherwise, read from boot file field in DHCP header.
347 //
348 if (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
349 //
350 // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
351 // terminated string. So force to append null terminated character at the end of string.
352 //
353 Ptr8 = (UINT8*)&Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];
354 Ptr8 += Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Length;
355 if (*(Ptr8 - 1) != '\0') {
356 *Ptr8 = '\0';
357 }
358 } else if (!FileFieldOverloaded && Offer->Dhcp4.Header.BootFileName[0] != 0) {
359 //
360 // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.
361 // Do not count dhcp option header here, or else will destroy the serverhostname.
362 //
363 Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)
364 (&Offer->Dhcp4.Header.BootFileName[0] -
365 OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));
366 }
367
368 //
369 // Http offer must have a boot URI.
370 //
371 if (IsHttpOffer && Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {
372 return EFI_DEVICE_ERROR;
373 }
374
375 //
376 // Try to retrieve the IP of HTTP server from URI.
377 //
378 if (IsHttpOffer) {
379 Status = HttpParseUrl (
380 (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
381 (UINT32) AsciiStrLen ((CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data),
382 FALSE,
383 &Cache4->UriParser
384 );
385 if (EFI_ERROR (Status)) {
386 return EFI_DEVICE_ERROR;
387 }
388
389 Status = HttpUrlGetIp4 (
390 (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
391 Cache4->UriParser,
392 &IpAddr
393 );
394 IpExpressedUri = !EFI_ERROR (Status);
395 }
396
397 //
398 // Determine offer type of the DHCPv4 packet.
399 //
400 if (IsHttpOffer) {
401 if (IpExpressedUri) {
402 if (IsProxyOffer) {
403 OfferType = HttpOfferTypeProxyIpUri;
404 } else {
405 OfferType = IsDnsOffer ? HttpOfferTypeDhcpIpUriDns : HttpOfferTypeDhcpIpUri;
406 }
407 } else {
408 if (!IsProxyOffer) {
409 OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri;
410 } else {
411 OfferType = HttpOfferTypeProxyNameUri;
412 }
413 }
414
415 } else {
416 if (!IsProxyOffer) {
417 OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly;
418 } else {
419 if (Cache4->UriParser != NULL) {
420 FreePool (Cache4->UriParser);
421 }
422 return EFI_DEVICE_ERROR;
423 }
424 }
425
426 Cache4->OfferType = OfferType;
427 return EFI_SUCCESS;
428 }
429
430 /**
431 Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.
432
433 @param[in] Private Pointer to HTTP boot driver private data.
434 @param[in] RcvdOffer Pointer to the received offer packet.
435
436 @retval EFI_SUCCESS Cache and parse the packet successfully.
437 @retval Others Operation failed.
438 **/
439 EFI_STATUS
440 HttpBootCacheDhcp4Offer (
441 IN HTTP_BOOT_PRIVATE_DATA *Private,
442 IN EFI_DHCP4_PACKET *RcvdOffer
443 )
444 {
445 HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4;
446 EFI_DHCP4_PACKET *Offer;
447 HTTP_BOOT_OFFER_TYPE OfferType;
448 EFI_STATUS Status;
449
450 ASSERT (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM);
451 Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;
452 Offer = &Cache4->Packet.Offer;
453
454 //
455 // Cache the content of DHCPv4 packet firstly.
456 //
457 Status = HttpBootCacheDhcp4Packet (Offer, RcvdOffer);
458 if (EFI_ERROR (Status)) {
459 return Status;
460 }
461
462 //
463 // Validate the DHCPv4 packet, and parse the options and offer type.
464 //
465 if (EFI_ERROR (HttpBootParseDhcp4Packet (Cache4))) {
466 return EFI_ABORTED;
467 }
468
469 //
470 // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
471 //
472 OfferType = Cache4->OfferType;
473 ASSERT (OfferType < HttpOfferTypeMax);
474 ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM);
475 Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
476 Private->OfferCount[OfferType]++;
477 Private->OfferNum++;
478
479 return EFI_SUCCESS;
480 }
481
482 /**
483 Select an DHCPv4 or DHCP6 offer, and record SelectIndex and SelectProxyType.
484
485 @param[in] Private Pointer to HTTP boot driver private data.
486
487 **/
488 VOID
489 HttpBootSelectDhcpOffer (
490 IN HTTP_BOOT_PRIVATE_DATA *Private
491 )
492 {
493 Private->SelectIndex = 0;
494 Private->SelectProxyType = HttpOfferTypeMax;
495
496 if (Private->FilePathUri != NULL) {
497 //
498 // We are in home environment, the URI is already specified.
499 // Just need to choose a DHCP offer.
500 // The offer with DNS server address takes priority here.
501 //
502 if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0) {
503
504 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
505
506 } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {
507
508 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;
509
510 } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
511
512 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
513
514 } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0) {
515
516 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
517
518 } else if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
519
520 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
521 }
522
523 } else {
524 //
525 // We are in corporate environment.
526 //
527 // Priority1: HttpOfferTypeDhcpIpUri or HttpOfferTypeDhcpIpUriDns
528 // Priority2: HttpOfferTypeDhcpNameUriDns
529 // Priority3: HttpOfferTypeDhcpOnly + HttpOfferTypeProxyIpUri
530 // Priority4: HttpOfferTypeDhcpDns + HttpOfferTypeProxyIpUri
531 // Priority5: HttpOfferTypeDhcpDns + HttpOfferTypeProxyNameUri
532 // Priority6: HttpOfferTypeDhcpDns + HttpOfferTypeDhcpNameUri
533 //
534 if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
535
536 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
537
538 } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {
539
540 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;
541
542 }else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
543
544 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
545
546 } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0 &&
547 Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) {
548
549 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
550 Private->SelectProxyType = HttpOfferTypeProxyIpUri;
551
552 } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
553 Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) {
554
555 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
556 Private->SelectProxyType = HttpOfferTypeProxyIpUri;
557
558 } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
559 Private->OfferCount[HttpOfferTypeProxyNameUri] > 0) {
560
561 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
562 Private->SelectProxyType = HttpOfferTypeProxyNameUri;
563
564 } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
565 Private->OfferCount[HttpOfferTypeDhcpNameUri] > 0) {
566
567 Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
568 Private->SelectProxyType = HttpOfferTypeDhcpNameUri;
569 }
570 }
571 }
572
573
574 /**
575 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
576 to intercept events that occurred in the configuration process.
577
578 @param[in] This Pointer to the EFI DHCPv4 Protocol.
579 @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().
580 @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.
581 @param[in] Dhcp4Event The event that occurs in the current state, which usually means a
582 state transition.
583 @param[in] Packet The DHCPv4 packet that is going to be sent or already received.
584 @param[out] NewPacket The packet that is used to replace the above Packet.
585
586 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
587 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
588 driver will continue to wait for more DHCPOFFER packets until the
589 retry timeout expires.
590 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process
591 and return to the Dhcp4Init or Dhcp4InitReboot state.
592
593 **/
594 EFI_STATUS
595 EFIAPI
596 HttpBootDhcp4CallBack (
597 IN EFI_DHCP4_PROTOCOL *This,
598 IN VOID *Context,
599 IN EFI_DHCP4_STATE CurrentState,
600 IN EFI_DHCP4_EVENT Dhcp4Event,
601 IN EFI_DHCP4_PACKET *Packet OPTIONAL,
602 OUT EFI_DHCP4_PACKET **NewPacket OPTIONAL
603 )
604 {
605 HTTP_BOOT_PRIVATE_DATA *Private;
606 EFI_DHCP4_PACKET_OPTION *MaxMsgSize;
607 UINT16 Value;
608 EFI_STATUS Status;
609 BOOLEAN Received;
610
611 if ((Dhcp4Event != Dhcp4SendDiscover) &&
612 (Dhcp4Event != Dhcp4RcvdOffer) &&
613 (Dhcp4Event != Dhcp4SendRequest) &&
614 (Dhcp4Event != Dhcp4RcvdAck) &&
615 (Dhcp4Event != Dhcp4SelectOffer)) {
616 return EFI_SUCCESS;
617 }
618
619 Private = (HTTP_BOOT_PRIVATE_DATA *) Context;
620
621 //
622 // Override the Maximum DHCP Message Size.
623 //
624 MaxMsgSize = HttpBootParseDhcp4Options (
625 Packet->Dhcp4.Option,
626 GET_OPTION_BUFFER_LEN (Packet),
627 DHCP4_TAG_MAXMSG
628 );
629 if (MaxMsgSize != NULL) {
630 Value = HTONS (HTTP_BOOT_DHCP4_PACKET_MAX_SIZE);
631 CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));
632 }
633
634 //
635 // Callback to user if any packets sent or received.
636 //
637 if (Private->HttpBootCallback != NULL && Dhcp4Event != Dhcp4SelectOffer) {
638 Received = (BOOLEAN) (Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck);
639 Status = Private->HttpBootCallback->Callback (
640 Private->HttpBootCallback,
641 HttpBootDhcp4,
642 Received,
643 Packet->Length,
644 &Packet->Dhcp4
645 );
646 if (EFI_ERROR (Status)) {
647 return EFI_ABORTED;
648 }
649 }
650
651 Status = EFI_SUCCESS;
652 switch (Dhcp4Event) {
653 case Dhcp4RcvdOffer:
654 Status = EFI_NOT_READY;
655 if (Packet->Length > HTTP_BOOT_DHCP4_PACKET_MAX_SIZE) {
656 //
657 // Ignore the incoming packets which exceed the maximum length.
658 //
659 break;
660 }
661 if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) {
662 //
663 // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
664 // the OfferIndex and OfferCount.
665 // If error happens, just ignore this packet and continue to wait more offer.
666 //
667 HttpBootCacheDhcp4Offer (Private, Packet);
668 }
669 break;
670
671 case Dhcp4SelectOffer:
672 //
673 // Select offer according to the priority in UEFI spec, and record the SelectIndex
674 // and SelectProxyType.
675 //
676 HttpBootSelectDhcpOffer (Private);
677
678 if (Private->SelectIndex == 0) {
679 Status = EFI_ABORTED;
680 } else {
681 *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;
682 }
683 break;
684
685 default:
686 break;
687 }
688
689 return Status;
690 }
691
692 /**
693 This function will register the IPv4 gateway address to the network device.
694
695 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
696
697 @retval EFI_SUCCESS The new IP configuration has been configured successfully.
698 @retval Others Failed to configure the address.
699
700 **/
701 EFI_STATUS
702 HttpBootRegisterIp4Gateway (
703 IN HTTP_BOOT_PRIVATE_DATA *Private
704 )
705 {
706 EFI_STATUS Status;
707 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
708
709 ASSERT (!Private->UsingIpv6);
710
711 Ip4Config2 = Private->Ip4Config2;
712
713 //
714 // Configure the gateway if valid.
715 //
716 if (!EFI_IP4_EQUAL (&Private->GatewayIp, &mZeroIp4Addr)) {
717 Status = Ip4Config2->SetData (
718 Ip4Config2,
719 Ip4Config2DataTypeGateway,
720 sizeof (EFI_IPv4_ADDRESS),
721 &Private->GatewayIp
722 );
723 if (EFI_ERROR (Status)) {
724 return Status;
725 }
726 }
727
728 return EFI_SUCCESS;
729 }
730
731 /**
732 This function will register the default DNS addresses to the network device.
733
734 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
735 @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes.
736 @param[in] DnsServerData Point a list of DNS server address in an array
737 of EFI_IPv4_ADDRESS instances.
738
739 @retval EFI_SUCCESS The DNS configuration has been configured successfully.
740 @retval Others Failed to configure the address.
741
742 **/
743 EFI_STATUS
744 HttpBootRegisterIp4Dns (
745 IN HTTP_BOOT_PRIVATE_DATA *Private,
746 IN UINTN DataLength,
747 IN VOID *DnsServerData
748 )
749 {
750 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
751
752 ASSERT (!Private->UsingIpv6);
753
754 Ip4Config2 = Private->Ip4Config2;
755
756 return Ip4Config2->SetData (
757 Ip4Config2,
758 Ip4Config2DataTypeDnsServer,
759 DataLength,
760 DnsServerData
761 );
762 }
763
764
765 /**
766 This function will switch the IP4 configuration policy to Static.
767
768 @param[in] Private Pointer to HTTP boot driver private data.
769
770 @retval EFI_SUCCESS The policy is already configured to static.
771 @retval Others Other error as indicated..
772
773 **/
774 EFI_STATUS
775 HttpBootSetIp4Policy (
776 IN HTTP_BOOT_PRIVATE_DATA *Private
777 )
778 {
779 EFI_IP4_CONFIG2_POLICY Policy;
780 EFI_STATUS Status;
781 EFI_IP4_CONFIG2_PROTOCOL *Ip4Config2;
782 UINTN DataSize;
783
784 Ip4Config2 = Private->Ip4Config2;
785
786 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
787 Status = Ip4Config2->GetData (
788 Ip4Config2,
789 Ip4Config2DataTypePolicy,
790 &DataSize,
791 &Policy
792 );
793 if (EFI_ERROR (Status)) {
794 return Status;
795 }
796
797 if (Policy != Ip4Config2PolicyStatic) {
798 Policy = Ip4Config2PolicyStatic;
799 Status= Ip4Config2->SetData (
800 Ip4Config2,
801 Ip4Config2DataTypePolicy,
802 sizeof (EFI_IP4_CONFIG2_POLICY),
803 &Policy
804 );
805 if (EFI_ERROR (Status)) {
806 return Status;
807 }
808 }
809
810 return EFI_SUCCESS;
811 }
812
813 /**
814 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other Http boot information.
815
816 @param[in] Private Pointer to HTTP boot driver private data.
817
818 @retval EFI_SUCCESS The D.O.R.A process successfully finished.
819 @retval Others Failed to finish the D.O.R.A process.
820
821 **/
822 EFI_STATUS
823 HttpBootDhcp4Dora (
824 IN HTTP_BOOT_PRIVATE_DATA *Private
825 )
826 {
827 EFI_DHCP4_PROTOCOL *Dhcp4;
828 UINT32 OptCount;
829 EFI_DHCP4_PACKET_OPTION *OptList[HTTP_BOOT_DHCP4_OPTION_MAX_NUM];
830 UINT8 Buffer[HTTP_BOOT_DHCP4_OPTION_MAX_SIZE];
831 EFI_DHCP4_CONFIG_DATA Config;
832 EFI_STATUS Status;
833 EFI_DHCP4_MODE_DATA Mode;
834
835 Dhcp4 = Private->Dhcp4;
836 ASSERT (Dhcp4 != NULL);
837
838 Status = HttpBootSetIp4Policy (Private);
839 if (EFI_ERROR (Status)) {
840 return Status;
841 }
842
843 //
844 // Build option list for the request packet.
845 //
846 OptCount = HttpBootBuildDhcp4Options (Private, OptList, Buffer);
847 ASSERT (OptCount > 0);
848
849 ZeroMem (&Config, sizeof(Config));
850 Config.OptionCount = OptCount;
851 Config.OptionList = OptList;
852 Config.Dhcp4Callback = HttpBootDhcp4CallBack;
853 Config.CallbackContext = Private;
854 Config.DiscoverTryCount = HTTP_BOOT_DHCP_RETRIES;
855 Config.DiscoverTimeout = mHttpDhcpTimeout;
856
857 //
858 // Configure the DHCPv4 instance for HTTP boot.
859 //
860 Status = Dhcp4->Configure (Dhcp4, &Config);
861 if (EFI_ERROR (Status)) {
862 goto ON_EXIT;
863 }
864
865 //
866 // Initialize the record fields for DHCPv4 offer in private data.
867 //
868 Private->OfferNum = 0;
869 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
870 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
871
872 //
873 // Start DHCPv4 D.O.R.A. process to acquire IPv4 address.
874 //
875 Status = Dhcp4->Start (Dhcp4, NULL);
876 if (EFI_ERROR (Status)) {
877 goto ON_EXIT;
878 }
879
880 //
881 // Get the acquired IPv4 address and store them.
882 //
883 Status = Dhcp4->GetModeData (Dhcp4, &Mode);
884 if (EFI_ERROR (Status)) {
885 goto ON_EXIT;
886 }
887
888 ASSERT (Mode.State == Dhcp4Bound);
889 CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
890 CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
891 CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
892
893 Status = HttpBootRegisterIp4Gateway (Private);
894 if (EFI_ERROR (Status)) {
895 goto ON_EXIT;
896 }
897
898 AsciiPrint ("\n Station IP address is ");
899 HttpBootShowIp4Addr (&Private->StationIp.v4);
900 AsciiPrint ("\n");
901
902 ON_EXIT:
903 if (EFI_ERROR (Status)) {
904 Dhcp4->Stop (Dhcp4);
905 Dhcp4->Configure (Dhcp4, NULL);
906 } else {
907 ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
908 Dhcp4->Configure (Dhcp4, &Config);
909 }
910
911 return Status;
912 }