]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpBootDxe/HttpBootDhcp4.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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 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,
145 sizeof (HTTP_BOOT_DHCP4_OPTION_CLID)
146 );
147 HttpBootUintnToAscDecWithFormat (
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
164 /**
165 Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.
166
167 @param[in] Buffer Pointer to the option buffer.
168 @param[in] Length Length of the option buffer.
169 @param[in] OptTag Tag of the required option.
170
171 @retval NULL Failed to find the required option.
172 @retval Others The position of the required option.
173
174 **/
175 EFI_DHCP4_PACKET_OPTION *
176 HttpBootParseDhcp4Options (
177 IN UINT8 *Buffer,
178 IN UINT32 Length,
179 IN UINT8 OptTag
180 )
181 {
182 EFI_DHCP4_PACKET_OPTION *Option;
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
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 //
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] -
368 OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[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
433 /**
434 Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.
435
436 @param[in] Private Pointer to HTTP boot driver private data.
437 @param[in] RcvdOffer Pointer to the received offer packet.
438
439 @retval EFI_SUCCESS Cache and parse the packet successfully.
440 @retval Others Operation failed.
441 **/
442 EFI_STATUS
443 HttpBootCacheDhcp4Offer (
444 IN HTTP_BOOT_PRIVATE_DATA *Private,
445 IN EFI_DHCP4_PACKET *RcvdOffer
446 )
447 {
448 HTTP_BOOT_DHCP4_PACKET_CACHE *Cache4;
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
485 /**
486 Select an DHCPv4 or DHCP6 offer, and record SelectIndex and SelectProxyType.
487
488 @param[in] Private Pointer to HTTP boot driver private data.
489
490 **/
491 VOID
492 HttpBootSelectDhcpOffer (
493 IN HTTP_BOOT_PRIVATE_DATA *Private
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
557 /**
558 EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
559 to intercept events that occurred in the configuration process.
560
561 @param[in] This Pointer to the EFI DHCPv4 Protocol.
562 @param[in] Context Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().
563 @param[in] CurrentState The current operational state of the EFI DHCPv4 Protocol driver.
564 @param[in] Dhcp4Event The event that occurs in the current state, which usually means a
565 state transition.
566 @param[in] Packet The DHCPv4 packet that is going to be sent or already received.
567 @param[out] NewPacket The packet that is used to replace the above Packet.
568
569 @retval EFI_SUCCESS Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
570 @retval EFI_NOT_READY Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
571 driver will continue to wait for more DHCPOFFER packets until the
572 retry timeout expires.
573 @retval EFI_ABORTED Tells the EFI DHCPv4 Protocol driver to abort the current process
574 and return to the Dhcp4Init or Dhcp4InitReboot state.
575
576 **/
577 EFI_STATUS
578 EFIAPI
579 HttpBootDhcp4CallBack (
580 IN EFI_DHCP4_PROTOCOL *This,
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,
625 HttpBootDhcp4,
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
679 /**
680 This function will register the IPv4 gateway address to the network device.
681
682 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
683
684 @retval EFI_SUCCESS The new IP configuration has been configured successfully.
685 @retval Others Failed to configure the address.
686
687 **/
688 EFI_STATUS
689 HttpBootRegisterIp4Gateway (
690 IN HTTP_BOOT_PRIVATE_DATA *Private
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,
706 Ip4Config2DataTypeGateway,
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
718 /**
719 This function will register the default DNS addresses to the network device.
720
721 @param[in] Private The pointer to HTTP_BOOT_PRIVATE_DATA.
722 @param[in] DataLength Size of the buffer pointed to by DnsServerData in bytes.
723 @param[in] DnsServerData Point a list of DNS server address in an array
724 of EFI_IPv4_ADDRESS instances.
725
726 @retval EFI_SUCCESS The DNS configuration has been configured successfully.
727 @retval Others Failed to configure the address.
728
729 **/
730 EFI_STATUS
731 HttpBootRegisterIp4Dns (
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,
745 Ip4Config2DataTypeDnsServer,
746 DataLength,
747 DnsServerData
748 );
749 }
750
751 /**
752 This function will switch the IP4 configuration policy to Static.
753
754 @param[in] Private Pointer to HTTP boot driver private data.
755
756 @retval EFI_SUCCESS The policy is already configured to static.
757 @retval Others Other error as indicated..
758
759 **/
760 EFI_STATUS
761 HttpBootSetIp4Policy (
762 IN HTTP_BOOT_PRIVATE_DATA *Private
763 )
764 {
765 EFI_IP4_CONFIG2_POLICY Policy;
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,
775 Ip4Config2DataTypePolicy,
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,
787 Ip4Config2DataTypePolicy,
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
799 /**
800 Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other Http boot information.
801
802 @param[in] Private Pointer to HTTP boot driver private data.
803
804 @retval EFI_SUCCESS The D.O.R.A process successfully finished.
805 @retval Others Failed to finish the D.O.R.A process.
806
807 **/
808 EFI_STATUS
809 HttpBootDhcp4Dora (
810 IN HTTP_BOOT_PRIVATE_DATA *Private
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];
817 EFI_DHCP4_CONFIG_DATA Config;
818 EFI_STATUS Status;
819 EFI_DHCP4_MODE_DATA Mode;
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;
838 Config.Dhcp4Callback = HttpBootDhcp4CallBack;
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
888 ON_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 }