]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpBootDxe/HttpBootClient.c
b81b03c96070400db493e148f53ebfcb1e4cb2a4
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootClient.c
1 /** @file
2 Implementation of the boot file download function.
3
4 Copyright (c) 2015, 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 Update the IP and URL device path node to include the boot resource information.
19
20 @param[in] Private The pointer to the driver's private data.
21
22 @retval EFI_SUCCESS Device patch successfully updated.
23 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
24 @retval Others Unexpected error happened.
25
26 **/
27 EFI_STATUS
28 HttpBootUpdateDevicePath (
29 IN HTTP_BOOT_PRIVATE_DATA *Private
30 )
31 {
32 EFI_DEV_PATH *Node;
33 EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath;
34 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
35 UINTN Length;
36 EFI_STATUS Status;
37
38 TmpDevicePath = NULL;
39
40 //
41 // Update the IP node with DHCP assigned information.
42 //
43 if (!Private->UsingIpv6) {
44 Node = AllocateZeroPool (sizeof (IPv4_DEVICE_PATH));
45 if (Node == NULL) {
46 return EFI_OUT_OF_RESOURCES;
47 }
48 Node->Ipv4.Header.Type = MESSAGING_DEVICE_PATH;
49 Node->Ipv4.Header.SubType = MSG_IPv4_DP;
50 SetDevicePathNodeLength (Node, sizeof (IPv4_DEVICE_PATH));
51 CopyMem (&Node->Ipv4.LocalIpAddress, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));
52 Node->Ipv4.RemotePort = Private->Port;
53 Node->Ipv4.Protocol = EFI_IP_PROTO_TCP;
54 Node->Ipv4.StaticIpAddress = FALSE;
55 CopyMem (&Node->Ipv4.GatewayIpAddress, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));
56 CopyMem (&Node->Ipv4.SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));
57 } else {
58 Node = AllocateZeroPool (sizeof (IPv6_DEVICE_PATH));
59 if (Node == NULL) {
60 return EFI_OUT_OF_RESOURCES;
61 }
62 Node->Ipv6.Header.Type = MESSAGING_DEVICE_PATH;
63 Node->Ipv6.Header.SubType = MSG_IPv6_DP;
64 SetDevicePathNodeLength (Node, sizeof (IPv6_DEVICE_PATH));
65 Node->Ipv6.PrefixLength = IP6_PREFIX_LENGTH;
66 Node->Ipv6.RemotePort = Private->Port;
67 Node->Ipv6.Protocol = EFI_IP_PROTO_TCP;
68 Node->Ipv6.IpAddressOrigin = 0;
69 CopyMem (&Node->Ipv6.LocalIpAddress, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));
70 CopyMem (&Node->Ipv6.RemoteIpAddress, &Private->ServerIp.v6, sizeof (EFI_IPv6_ADDRESS));
71 CopyMem (&Node->Ipv6.GatewayIpAddress, &Private->GatewayIp.v6, sizeof (EFI_IPv6_ADDRESS));
72 }
73
74 TmpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);
75 FreePool (Node);
76 if (TmpDevicePath == NULL) {
77 return EFI_OUT_OF_RESOURCES;
78 }
79
80 //
81 // Update the URI node with the boot file URI.
82 //
83 Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + AsciiStrSize (Private->BootFileUri);
84 Node = AllocatePool (Length);
85 if (Node == NULL) {
86 FreePool (TmpDevicePath);
87 return EFI_OUT_OF_RESOURCES;
88 }
89 Node->DevPath.Type = MESSAGING_DEVICE_PATH;
90 Node->DevPath.SubType = MSG_URI_DP;
91 SetDevicePathNodeLength (Node, Length);
92 CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL), Private->BootFileUri, AsciiStrSize (Private->BootFileUri));
93
94 NewDevicePath = AppendDevicePathNode (TmpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);
95 FreePool (Node);
96 FreePool (TmpDevicePath);
97 if (NewDevicePath == NULL) {
98 return EFI_OUT_OF_RESOURCES;
99 }
100
101 if (!Private->UsingIpv6) {
102 //
103 // Reinstall the device path protocol of the child handle.
104 //
105 Status = gBS->ReinstallProtocolInterface (
106 Private->Ip4Nic->Controller,
107 &gEfiDevicePathProtocolGuid,
108 Private->Ip4Nic->DevicePath,
109 NewDevicePath
110 );
111 if (EFI_ERROR (Status)) {
112 return Status;
113 }
114
115 FreePool (Private->Ip4Nic->DevicePath);
116 Private->Ip4Nic->DevicePath = NewDevicePath;
117 } else {
118 //
119 // Reinstall the device path protocol of the child handle.
120 //
121 Status = gBS->ReinstallProtocolInterface (
122 Private->Ip6Nic->Controller,
123 &gEfiDevicePathProtocolGuid,
124 Private->Ip6Nic->DevicePath,
125 NewDevicePath
126 );
127 if (EFI_ERROR (Status)) {
128 return Status;
129 }
130 FreePool (Private->Ip6Nic->DevicePath);
131 Private->Ip6Nic->DevicePath = NewDevicePath;
132 }
133
134 return EFI_SUCCESS;
135 }
136
137 /**
138 Parse the boot file URI information from the selected Dhcp4 offer packet.
139
140 @param[in] Private The pointer to the driver's private data.
141
142 @retval EFI_SUCCESS Successfully parsed out all the boot information.
143 @retval Others Failed to parse out the boot information.
144
145 **/
146 EFI_STATUS
147 HttpBootDhcp4ExtractUriInfo (
148 IN HTTP_BOOT_PRIVATE_DATA *Private
149 )
150 {
151 HTTP_BOOT_DHCP4_PACKET_CACHE *SelectOffer;
152 HTTP_BOOT_DHCP4_PACKET_CACHE *HttpOffer;
153 UINT32 SelectIndex;
154 UINT32 ProxyIndex;
155 EFI_DHCP4_PACKET_OPTION *Option;
156 EFI_STATUS Status;
157
158 ASSERT (Private != NULL);
159 ASSERT (Private->SelectIndex != 0);
160 SelectIndex = Private->SelectIndex - 1;
161 ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM);
162
163 Status = EFI_SUCCESS;
164
165 //
166 // SelectOffer contains the IP address configuration and name server configuration.
167 // HttpOffer contains the boot file URL.
168 //
169 SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp4;
170 if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) || (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {
171 HttpOffer = SelectOffer;
172 } else {
173 ASSERT (Private->SelectProxyType != HttpOfferTypeMax);
174 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];
175 HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp4;
176 }
177
178 //
179 // Configure the default DNS server if server assigned.
180 //
181 if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || (SelectOffer->OfferType == HttpOfferTypeDhcpDns)) {
182 Option = SelectOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];
183 ASSERT (Option != NULL);
184 Status = HttpBootRegisterIp4Dns (
185 Private,
186 Option->Length,
187 Option->Data
188 );
189 if (EFI_ERROR (Status)) {
190 return Status;
191 }
192 }
193
194 //
195 // Extract the port from URL, and use default HTTP port 80 if not provided.
196 //
197 Status = HttpUrlGetPort (
198 (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
199 HttpOffer->UriParser,
200 &Private->Port
201 );
202 if (EFI_ERROR (Status) || Private->Port == 0) {
203 Private->Port = 80;
204 }
205
206 //
207 // Record the URI of boot file from the selected HTTP offer.
208 //
209 Private->BootFileUriParser = HttpOffer->UriParser;
210 Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data;
211
212
213 //
214 // All boot informations are valid here.
215 //
216 AsciiPrint ("\n URI: %a", Private->BootFileUri);
217
218 //
219 // Update the device path to include the IP and boot URI information.
220 //
221 Status = HttpBootUpdateDevicePath (Private);
222
223 return Status;
224 }
225
226 /**
227 Parse the boot file URI information from the selected Dhcp6 offer packet.
228
229 @param[in] Private The pointer to the driver's private data.
230
231 @retval EFI_SUCCESS Successfully parsed out all the boot information.
232 @retval Others Failed to parse out the boot information.
233
234 **/
235 EFI_STATUS
236 HttpBootDhcp6ExtractUriInfo (
237 IN HTTP_BOOT_PRIVATE_DATA *Private
238 )
239 {
240 HTTP_BOOT_DHCP6_PACKET_CACHE *SelectOffer;
241 HTTP_BOOT_DHCP6_PACKET_CACHE *HttpOffer;
242 UINT32 SelectIndex;
243 UINT32 ProxyIndex;
244 EFI_DHCP6_PACKET_OPTION *Option;
245 EFI_IPv6_ADDRESS IpAddr;
246 CHAR8 *HostName;
247 CHAR16 *HostNameStr;
248 EFI_STATUS Status;
249
250 ASSERT (Private != NULL);
251 ASSERT (Private->SelectIndex != 0);
252 SelectIndex = Private->SelectIndex - 1;
253 ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM);
254
255 Status = EFI_SUCCESS;
256 HostName = NULL;
257 //
258 // SelectOffer contains the IP address configuration and name server configuration.
259 // HttpOffer contains the boot file URL.
260 //
261 SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp6;
262 if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) || (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {
263 HttpOffer = SelectOffer;
264 } else {
265 ASSERT (Private->SelectProxyType != HttpOfferTypeMax);
266 ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];
267 HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp6;
268 }
269
270 //
271 // Set the Local station address to IP layer.
272 //
273 Status = HttpBootSetIp6Address (Private);
274 if (EFI_ERROR (Status)) {
275 return Status;
276 }
277
278 //
279 // Configure the default DNS server if server assigned.
280 //
281 if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || (SelectOffer->OfferType == HttpOfferTypeDhcpDns)) {
282 Option = SelectOffer->OptList[HTTP_BOOT_DHCP6_IDX_DNS_SERVER];
283 ASSERT (Option != NULL);
284 Status = HttpBootSetIp6Dns (
285 Private,
286 HTONS (Option->OpLen),
287 Option->Data
288 );
289 if (EFI_ERROR (Status)) {
290 return Status;
291 }
292 }
293
294 //
295 // Extract the HTTP server Ip frome URL. This is used to Check route table
296 // whether can send message to HTTP Server Ip through the GateWay.
297 //
298 Status = HttpUrlGetIp6 (
299 (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data,
300 HttpOffer->UriParser,
301 &IpAddr
302 );
303
304 if (EFI_ERROR (Status)) {
305 //
306 // The Http server address is expressed by Name Ip, so perform DNS resolution
307 //
308 Status = HttpUrlGetHostName (
309 (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data,
310 HttpOffer->UriParser,
311 &HostName
312 );
313 if (EFI_ERROR (Status)) {
314 return Status;
315 }
316
317 HostNameStr = AllocateZeroPool ((AsciiStrLen (HostName) + 1) * sizeof (CHAR16));
318 if (HostNameStr == NULL) {
319 Status = EFI_OUT_OF_RESOURCES;
320 goto Error;
321 }
322
323 AsciiStrToUnicodeStr (HostName, HostNameStr);
324 Status = HttpBootDns (Private, HostNameStr, &IpAddr);
325 FreePool (HostNameStr);
326 if (EFI_ERROR (Status)) {
327 goto Error;
328 }
329 }
330
331 CopyMem (&Private->ServerIp.v6, &IpAddr, sizeof (EFI_IPv6_ADDRESS));
332
333 //
334 // register the IPv6 gateway address to the network device.
335 //
336 Status = HttpBootSetIp6Gateway (Private);
337 if (EFI_ERROR (Status)) {
338 return Status;
339 }
340
341 //
342 // Extract the port from URL, and use default HTTP port 80 if not provided.
343 //
344 Status = HttpUrlGetPort (
345 (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data,
346 HttpOffer->UriParser,
347 &Private->Port
348 );
349 if (EFI_ERROR (Status) || Private->Port == 0) {
350 Private->Port = 80;
351 }
352
353 //
354 // Record the URI of boot file from the selected HTTP offer.
355 //
356 Private->BootFileUriParser = HttpOffer->UriParser;
357 Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data;
358
359
360 //
361 // All boot informations are valid here.
362 //
363 AsciiPrint ("\n URI: %a", Private->BootFileUri);
364 //
365 // Update the device path to include the IP and boot URI information.
366 //
367 Status = HttpBootUpdateDevicePath (Private);
368
369 Error:
370
371 if (HostName != NULL) {
372 FreePool (HostName);
373 }
374
375 return Status;
376 }
377
378
379 /**
380 Discover all the boot information for boot file.
381
382 @param[in, out] Private The pointer to the driver's private data.
383
384 @retval EFI_SUCCESS Successfully obtained all the boot information .
385 @retval Others Failed to retrieve the boot information.
386
387 **/
388 EFI_STATUS
389 HttpBootDiscoverBootInfo (
390 IN OUT HTTP_BOOT_PRIVATE_DATA *Private
391 )
392 {
393 EFI_STATUS Status;
394
395 //
396 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
397 // other Http boot information.
398 //
399 Status = HttpBootDhcp (Private);
400 if (EFI_ERROR (Status)) {
401 return Status;
402 }
403
404 if (!Private->UsingIpv6) {
405 Status = HttpBootDhcp4ExtractUriInfo (Private);
406 } else {
407 Status = HttpBootDhcp6ExtractUriInfo (Private);
408 }
409
410 return Status;
411 }
412
413 /**
414 Create a HttpIo instance for the file download.
415
416 @param[in] Private The pointer to the driver's private data.
417
418 @retval EFI_SUCCESS Successfully created.
419 @retval Others Failed to create HttpIo.
420
421 **/
422 EFI_STATUS
423 HttpBootCreateHttpIo (
424 IN HTTP_BOOT_PRIVATE_DATA *Private
425 )
426 {
427 HTTP_IO_CONFIG_DATA ConfigData;
428 EFI_STATUS Status;
429
430 ASSERT (Private != NULL);
431
432 ZeroMem (&ConfigData, sizeof (HTTP_IO_CONFIG_DATA));
433 if (!Private->UsingIpv6) {
434 ConfigData.Config4.HttpVersion = HttpVersion11;
435 ConfigData.Config4.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;
436 IP4_COPY_ADDRESS (&ConfigData.Config4.LocalIp, &Private->StationIp.v4);
437 IP4_COPY_ADDRESS (&ConfigData.Config4.SubnetMask, &Private->SubnetMask.v4);
438 } else {
439 ConfigData.Config6.HttpVersion = HttpVersion11;
440 ConfigData.Config6.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;
441 IP6_COPY_ADDRESS (&ConfigData.Config6.LocalIp, &Private->StationIp.v6);
442 }
443
444 Status = HttpIoCreateIo (
445 Private->Image,
446 Private->Controller,
447 Private->UsingIpv6 ? IP_VERSION_6 : IP_VERSION_4,
448 &ConfigData,
449 &Private->HttpIo
450 );
451 if (EFI_ERROR (Status)) {
452 return Status;
453 }
454
455 Private->HttpCreated = TRUE;
456 return EFI_SUCCESS;
457 }
458
459 /**
460 Get the file content from cached data.
461
462 @param[in] Private The pointer to the driver's private data.
463 @param[in] Uri Uri of the file to be retrieved from cache.
464 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
465 code of EFI_SUCCESS, the amount of data transferred to
466 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
467 the size of Buffer required to retrieve the requested file.
468 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
469 then the size of the requested file is returned in
470 BufferSize.
471
472 @retval EFI_SUCCESS Successfully created.
473 @retval Others Failed to create HttpIo.
474
475 **/
476 EFI_STATUS
477 HttpBootGetFileFromCache (
478 IN HTTP_BOOT_PRIVATE_DATA *Private,
479 IN CHAR16 *Uri,
480 IN OUT UINTN *BufferSize,
481 OUT UINT8 *Buffer
482 )
483 {
484 LIST_ENTRY *Entry;
485 LIST_ENTRY *Entry2;
486 HTTP_BOOT_CACHE_CONTENT *Cache;
487 HTTP_BOOT_ENTITY_DATA *EntityData;
488 UINTN CopyedSize;
489
490 if (Uri == NULL || BufferSize == 0 || Buffer == NULL) {
491 return EFI_INVALID_PARAMETER;
492 }
493
494 NET_LIST_FOR_EACH (Entry, &Private->CacheList) {
495 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);
496 //
497 // Compare the URI to see whether we already have a cache for this file.
498 //
499 if ((Cache->RequestData != NULL) &&
500 (Cache->RequestData->Url != NULL) &&
501 (StrCmp (Uri, Cache->RequestData->Url) == 0))
502 {
503 //
504 // Hit cache, check buffer size.
505 //
506 if (*BufferSize < Cache->EntityLength) {
507 *BufferSize = Cache->EntityLength;
508 return EFI_BUFFER_TOO_SMALL;
509 }
510
511 //
512 // Fill data to buffer.
513 //
514 CopyedSize = 0;
515 NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {
516 EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);
517 if (*BufferSize > CopyedSize) {
518 CopyMem (
519 Buffer + CopyedSize,
520 EntityData->DataStart,
521 MIN (EntityData->DataLength, *BufferSize - CopyedSize)
522 );
523 CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);
524 }
525 }
526 *BufferSize = CopyedSize;
527 return EFI_SUCCESS;
528 }
529 }
530
531 return EFI_NOT_FOUND;
532 }
533
534 /**
535 Release all the resource of a cache item.
536
537 @param[in] Cache The pointer to the cache item.
538
539 **/
540 VOID
541 HttpBootFreeCache (
542 IN HTTP_BOOT_CACHE_CONTENT *Cache
543 )
544 {
545 UINTN Index;
546 LIST_ENTRY *Entry;
547 LIST_ENTRY *NextEntry;
548 HTTP_BOOT_ENTITY_DATA *EntityData;
549
550 if (Cache != NULL) {
551 //
552 // Free the request data
553 //
554 if (Cache->RequestData != NULL) {
555 if (Cache->RequestData->Url != NULL) {
556 FreePool (Cache->RequestData->Url);
557 }
558 FreePool (Cache->RequestData);
559 }
560
561 //
562 // Free the response header
563 //
564 if (Cache->ResponseData != NULL) {
565 if (Cache->ResponseData->Headers != NULL) {
566 for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) {
567 FreePool (Cache->ResponseData->Headers[Index].FieldName);
568 FreePool (Cache->ResponseData->Headers[Index].FieldValue);
569 }
570 FreePool (Cache->ResponseData->Headers);
571 }
572 }
573
574 //
575 // Free the response body
576 //
577 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) {
578 EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link);
579 if (EntityData->Block != NULL) {
580 FreePool (EntityData->Block);
581 }
582 RemoveEntryList (&EntityData->Link);
583 FreePool (EntityData);
584 }
585
586 FreePool (Cache);
587 }
588 }
589
590 /**
591 Clean up all cached data.
592
593 @param[in] Private The pointer to the driver's private data.
594
595 **/
596 VOID
597 HttpBootFreeCacheList (
598 IN HTTP_BOOT_PRIVATE_DATA *Private
599 )
600 {
601 LIST_ENTRY *Entry;
602 LIST_ENTRY *NextEntry;
603 HTTP_BOOT_CACHE_CONTENT *Cache;
604
605 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->CacheList) {
606 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);
607 RemoveEntryList (&Cache->Link);
608 HttpBootFreeCache (Cache);
609 }
610 }
611
612 /**
613 A callback function to intercept events during message parser.
614
615 This function will be invoked during HttpParseMessageBody() with various events type. An error
616 return status of the callback function will cause the HttpParseMessageBody() aborted.
617
618 @param[in] EventType Event type of this callback call.
619 @param[in] Data A pointer to data buffer.
620 @param[in] Length Length in bytes of the Data.
621 @param[in] Context Callback context set by HttpInitMsgParser().
622
623 @retval EFI_SUCCESS Continue to parser the message body.
624 @retval Others Abort the parse.
625
626 **/
627 EFI_STATUS
628 EFIAPI
629 HttpBootGetBootFileCallback (
630 IN HTTP_BODY_PARSE_EVENT EventType,
631 IN CHAR8 *Data,
632 IN UINTN Length,
633 IN VOID *Context
634 )
635 {
636 HTTP_BOOT_CALLBACK_DATA *CallbackData;
637 HTTP_BOOT_ENTITY_DATA *NewEntityData;
638
639 //
640 // We only care about the entity data.
641 //
642 if (EventType != BodyParseEventOnData) {
643 return EFI_SUCCESS;
644 }
645
646 CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context;
647 //
648 // Copy data if caller has provided a buffer.
649 //
650 if (CallbackData->BufferSize > CallbackData->CopyedSize) {
651 CopyMem (
652 CallbackData->Buffer + CallbackData->CopyedSize,
653 Data,
654 MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize)
655 );
656 CallbackData->CopyedSize += MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize);
657 }
658
659 //
660 // The caller doesn't provide a buffer, save the block into cache list.
661 //
662 if (CallbackData->Cache != NULL) {
663 NewEntityData = AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA));
664 if (NewEntityData == NULL) {
665 return EFI_OUT_OF_RESOURCES;
666 }
667 if (CallbackData->NewBlock) {
668 NewEntityData->Block = CallbackData->Block;
669 CallbackData->Block = NULL;
670 }
671 NewEntityData->DataLength = Length;
672 NewEntityData->DataStart = (UINT8*) Data;
673 InsertTailList (&CallbackData->Cache->EntityDataList, &NewEntityData->Link);
674 }
675 return EFI_SUCCESS;
676 }
677
678 /**
679 This function download the boot file by using UEFI HTTP protocol.
680
681 @param[in] Private The pointer to the driver's private data.
682 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
683 the caller only want to know the size of the requested file.
684 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
685 code of EFI_SUCCESS, the amount of data transferred to
686 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
687 the size of Buffer required to retrieve the requested file.
688 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
689 then the size of the requested file is returned in
690 BufferSize.
691
692 @retval EFI_SUCCESS The file was loaded.
693 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
694 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
695 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
696 BufferSize has been updated with the size needed to complete
697 the request.
698 @retval Others Unexpected error happened.
699
700 **/
701 EFI_STATUS
702 HttpBootGetBootFile (
703 IN HTTP_BOOT_PRIVATE_DATA *Private,
704 IN BOOLEAN HeaderOnly,
705 IN OUT UINTN *BufferSize,
706 OUT UINT8 *Buffer
707 )
708 {
709 EFI_STATUS Status;
710 CHAR8 *HostName;
711 EFI_HTTP_REQUEST_DATA *RequestData;
712 HTTP_IO_RESOPNSE_DATA *ResponseData;
713 HTTP_IO_RESOPNSE_DATA ResponseBody;
714 HTTP_IO *HttpIo;
715 HTTP_IO_HEADER *HttpIoHeader;
716 VOID *Parser;
717 HTTP_BOOT_CALLBACK_DATA Context;
718 UINTN ContentLength;
719 HTTP_BOOT_CACHE_CONTENT *Cache;
720 UINT8 *Block;
721 CHAR16 *Url;
722
723 ASSERT (Private != NULL);
724 ASSERT (Private->HttpCreated);
725
726 if (BufferSize == NULL) {
727 return EFI_INVALID_PARAMETER;
728 }
729
730 if (*BufferSize != 0 && Buffer == NULL) {
731 return EFI_INVALID_PARAMETER;
732 }
733
734 //
735 // First, check whether we already cached the requested Uri.
736 //
737 Url = AllocatePool ((AsciiStrLen (Private->BootFileUri) + 1) * sizeof (CHAR16));
738 if (Url == NULL) {
739 return EFI_OUT_OF_RESOURCES;
740 }
741 AsciiStrToUnicodeStr (Private->BootFileUri, Url);
742 if (!HeaderOnly) {
743 Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer);
744 if (Status != EFI_NOT_FOUND) {
745 FreePool (Url);
746 return Status;
747 }
748 }
749
750 //
751 // Not found in cache, try to download it through HTTP.
752 //
753
754 //
755 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
756 //
757 Cache = NULL;
758 if ((!HeaderOnly) && (*BufferSize == 0)) {
759 Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT));
760 if (Cache == NULL) {
761 Status = EFI_OUT_OF_RESOURCES;
762 goto ERROR_1;
763 }
764 InitializeListHead (&Cache->EntityDataList);
765 }
766
767 //
768 // 2. Send HTTP request message.
769 //
770
771 //
772 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
773 // Host
774 // Accept
775 // User-Agent
776 //
777 HttpIoHeader = HttpBootCreateHeader (3);
778 if (HttpIoHeader == NULL) {
779 Status = EFI_OUT_OF_RESOURCES;
780 goto ERROR_2;
781 }
782
783 //
784 // Add HTTP header field 1: Host
785 //
786 HostName = NULL;
787 Status = HttpUrlGetHostName (
788 Private->BootFileUri,
789 Private->BootFileUriParser,
790 &HostName
791 );
792 if (EFI_ERROR (Status)) {
793 goto ERROR_3;
794 }
795 Status = HttpBootSetHeader (
796 HttpIoHeader,
797 HTTP_FIELD_NAME_HOST,
798 HostName
799 );
800 FreePool (HostName);
801 if (EFI_ERROR (Status)) {
802 goto ERROR_3;
803 }
804
805 //
806 // Add HTTP header field 2: Accept
807 //
808 Status = HttpBootSetHeader (
809 HttpIoHeader,
810 HTTP_FIELD_NAME_ACCEPT,
811 "*/*"
812 );
813 if (EFI_ERROR (Status)) {
814 goto ERROR_3;
815 }
816
817 //
818 // Add HTTP header field 3: User-Agent
819 //
820 Status = HttpBootSetHeader (
821 HttpIoHeader,
822 HTTP_FIELD_NAME_USER_AGENT,
823 HTTP_USER_AGENT_EFI_HTTP_BOOT
824 );
825 if (EFI_ERROR (Status)) {
826 goto ERROR_3;
827 }
828
829 //
830 // 2.2 Build the rest of HTTP request info.
831 //
832 RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA));
833 if (RequestData == NULL) {
834 Status = EFI_OUT_OF_RESOURCES;
835 goto ERROR_3;
836 }
837 RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;
838 RequestData->Url = Url;
839 if (RequestData->Url == NULL) {
840 Status = EFI_OUT_OF_RESOURCES;
841 goto ERROR_4;
842 }
843 AsciiStrToUnicodeStr (Private->BootFileUri, RequestData->Url);
844
845 //
846 // 2.3 Record the request info in a temp cache item.
847 //
848 if (Cache != NULL) {
849 Cache->RequestData = RequestData;
850 }
851
852 //
853 // 2.4 Send out the request to HTTP server.
854 //
855 HttpIo = &Private->HttpIo;
856 Status = HttpIoSendRequest (
857 HttpIo,
858 RequestData,
859 HttpIoHeader->HeaderCount,
860 HttpIoHeader->Headers,
861 0,
862 NULL
863 );
864 if (EFI_ERROR (Status)) {
865 goto ERROR_4;
866 }
867
868 //
869 // 3. Receive HTTP response message.
870 //
871
872 //
873 // 3.1 First step, use zero BodyLength to only receive the response headers.
874 //
875 ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESOPNSE_DATA));
876 if (ResponseData == NULL) {
877 Status = EFI_OUT_OF_RESOURCES;
878 goto ERROR_4;
879 }
880 Status = HttpIoRecvResponse (
881 &Private->HttpIo,
882 TRUE,
883 ResponseData
884 );
885 if (EFI_ERROR (Status)) {
886 goto ERROR_5;
887 }
888
889 //
890 // 3.2 Cache the response header.
891 //
892 if (Cache != NULL) {
893 Cache->ResponseData = ResponseData;
894 }
895
896 //
897 // 3.3 Init a message-body parser from the header information.
898 //
899 Parser = NULL;
900 Context.NewBlock = FALSE;
901 Context.Block = NULL;
902 Context.CopyedSize = 0;
903 Context.Buffer = Buffer;
904 Context.BufferSize = *BufferSize;
905 Context.Cache = Cache;
906 Status = HttpInitMsgParser (
907 HeaderOnly? HttpMethodHead : HttpMethodGet,
908 ResponseData->Response.StatusCode,
909 ResponseData->HeaderCount,
910 ResponseData->Headers,
911 HttpBootGetBootFileCallback,
912 (VOID*) &Context,
913 &Parser
914 );
915 if (EFI_ERROR (Status)) {
916 goto ERROR_6;
917 }
918
919 //
920 // 3.4 Continue to receive and parse message-body if needed.
921 //
922 Block = NULL;
923 if (!HeaderOnly) {
924 ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESOPNSE_DATA));
925 while (!HttpIsMessageComplete (Parser)) {
926 //
927 // Allocate a block to hold the message-body, if caller doesn't provide
928 // a buffer, the block will be cached and we will allocate a new one here.
929 //
930 if (Block == NULL || Context.BufferSize == 0) {
931 Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);
932 if (Block == NULL) {
933 Status = EFI_OUT_OF_RESOURCES;
934 goto ERROR_6;
935 }
936 Context.NewBlock = TRUE;
937 Context.Block = Block;
938 } else {
939 Context.NewBlock = FALSE;
940 }
941
942 ResponseBody.Body = (CHAR8*) Block;
943 ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;
944 Status = HttpIoRecvResponse (
945 &Private->HttpIo,
946 FALSE,
947 &ResponseBody
948 );
949 if (EFI_ERROR (Status)) {
950 goto ERROR_6;
951 }
952
953 //
954 // Parse the new received block of the message-body, the block will be saved in cache.
955 //
956 Status = HttpParseMessageBody (
957 Parser,
958 ResponseBody.BodyLength,
959 ResponseBody.Body
960 );
961 if (EFI_ERROR (Status)) {
962 goto ERROR_6;
963 }
964 }
965 }
966
967 //
968 // 3.5 Message-body receive & parse is completed, get the file size.
969 //
970 Status = HttpGetEntityLength (Parser, &ContentLength);
971 if (EFI_ERROR (Status)) {
972 goto ERROR_6;
973 }
974
975 if (*BufferSize < ContentLength) {
976 Status = EFI_BUFFER_TOO_SMALL;
977 }
978 *BufferSize = ContentLength;
979
980 //
981 // 4. Save the cache item to driver's cache list and return.
982 //
983 if (Cache != NULL) {
984 Cache->EntityLength = ContentLength;
985 InsertTailList (&Private->CacheList, &Cache->Link);
986 }
987
988 if (Parser != NULL) {
989 HttpFreeMsgParser (Parser);
990 }
991
992 return EFI_SUCCESS;
993
994 ERROR_6:
995 if (Parser != NULL) {
996 HttpFreeMsgParser (Parser);
997 }
998 if (Context.Block != NULL) {
999 FreePool (Context.Block);
1000 }
1001 HttpBootFreeCache (Cache);
1002
1003 ERROR_5:
1004 if (ResponseData != NULL) {
1005 FreePool (ResponseData);
1006 }
1007 ERROR_4:
1008 if (RequestData != NULL) {
1009 FreePool (RequestData);
1010 }
1011 ERROR_3:
1012 HttpBootFreeHeader (HttpIoHeader);
1013 ERROR_2:
1014 if (Cache != NULL) {
1015 FreePool (Cache);
1016 }
1017 ERROR_1:
1018 if (Url != NULL) {
1019 FreePool (Url);
1020 }
1021
1022 return Status;
1023 }