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