]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpBootDxe/HttpBootClient.c
dd835c4f833a75e64dfe860c070e0fffa50f099c
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootClient.c
1 /** @file
2 Implementation of the boot file download function.
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 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 Release all the resource of a cache item.
461
462 @param[in] Cache The pointer to the cache item.
463
464 **/
465 VOID
466 HttpBootFreeCache (
467 IN HTTP_BOOT_CACHE_CONTENT *Cache
468 )
469 {
470 UINTN Index;
471 LIST_ENTRY *Entry;
472 LIST_ENTRY *NextEntry;
473 HTTP_BOOT_ENTITY_DATA *EntityData;
474
475 if (Cache != NULL) {
476 //
477 // Free the request data
478 //
479 if (Cache->RequestData != NULL) {
480 if (Cache->RequestData->Url != NULL) {
481 FreePool (Cache->RequestData->Url);
482 }
483 FreePool (Cache->RequestData);
484 }
485
486 //
487 // Free the response header
488 //
489 if (Cache->ResponseData != NULL) {
490 if (Cache->ResponseData->Headers != NULL) {
491 for (Index = 0; Index < Cache->ResponseData->HeaderCount; Index++) {
492 FreePool (Cache->ResponseData->Headers[Index].FieldName);
493 FreePool (Cache->ResponseData->Headers[Index].FieldValue);
494 }
495 FreePool (Cache->ResponseData->Headers);
496 }
497 }
498
499 //
500 // Free the response body
501 //
502 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Cache->EntityDataList) {
503 EntityData = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_ENTITY_DATA, Link);
504 if (EntityData->Block != NULL) {
505 FreePool (EntityData->Block);
506 }
507 RemoveEntryList (&EntityData->Link);
508 FreePool (EntityData);
509 }
510
511 FreePool (Cache);
512 }
513 }
514
515 /**
516 Clean up all cached data.
517
518 @param[in] Private The pointer to the driver's private data.
519
520 **/
521 VOID
522 HttpBootFreeCacheList (
523 IN HTTP_BOOT_PRIVATE_DATA *Private
524 )
525 {
526 LIST_ENTRY *Entry;
527 LIST_ENTRY *NextEntry;
528 HTTP_BOOT_CACHE_CONTENT *Cache;
529
530 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->CacheList) {
531 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);
532 RemoveEntryList (&Cache->Link);
533 HttpBootFreeCache (Cache);
534 }
535 }
536
537 /**
538 Get the file content from cached data.
539
540 @param[in] Private The pointer to the driver's private data.
541 @param[in] Uri Uri of the file to be retrieved from cache.
542 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
543 code of EFI_SUCCESS, the amount of data transferred to
544 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
545 the size of Buffer required to retrieve the requested file.
546 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
547 then the size of the requested file is returned in
548 BufferSize.
549
550 @retval EFI_SUCCESS Successfully created.
551 @retval Others Failed to create HttpIo.
552
553 **/
554 EFI_STATUS
555 HttpBootGetFileFromCache (
556 IN HTTP_BOOT_PRIVATE_DATA *Private,
557 IN CHAR16 *Uri,
558 IN OUT UINTN *BufferSize,
559 OUT UINT8 *Buffer
560 )
561 {
562 LIST_ENTRY *Entry;
563 LIST_ENTRY *Entry2;
564 HTTP_BOOT_CACHE_CONTENT *Cache;
565 HTTP_BOOT_ENTITY_DATA *EntityData;
566 UINTN CopyedSize;
567
568 if (Uri == NULL || BufferSize == 0 || Buffer == NULL) {
569 return EFI_INVALID_PARAMETER;
570 }
571
572 //
573 // Search file in the cache list, the cache entry will be released upon a successful
574 // match.
575 //
576 NET_LIST_FOR_EACH (Entry, &Private->CacheList) {
577 Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);
578 //
579 // Compare the URI to see whether we already have a cache for this file.
580 //
581 if ((Cache->RequestData != NULL) &&
582 (Cache->RequestData->Url != NULL) &&
583 (StrCmp (Uri, Cache->RequestData->Url) == 0))
584 {
585 //
586 // Hit cache, check buffer size.
587 //
588 if (*BufferSize < Cache->EntityLength) {
589 *BufferSize = Cache->EntityLength;
590 return EFI_BUFFER_TOO_SMALL;
591 }
592
593 //
594 // Fill data to buffer.
595 //
596 CopyedSize = 0;
597 NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {
598 EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);
599 if (*BufferSize > CopyedSize) {
600 CopyMem (
601 Buffer + CopyedSize,
602 EntityData->DataStart,
603 MIN (EntityData->DataLength, *BufferSize - CopyedSize)
604 );
605 CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);
606 }
607 }
608 *BufferSize = CopyedSize;
609
610 //
611 // On success, free the cached data to release the memory resource.
612 //
613 RemoveEntryList (&Cache->Link);
614 HttpBootFreeCache (Cache);
615 return EFI_SUCCESS;
616 }
617 }
618
619 return EFI_NOT_FOUND;
620 }
621
622 /**
623 A callback function to intercept events during message parser.
624
625 This function will be invoked during HttpParseMessageBody() with various events type. An error
626 return status of the callback function will cause the HttpParseMessageBody() aborted.
627
628 @param[in] EventType Event type of this callback call.
629 @param[in] Data A pointer to data buffer.
630 @param[in] Length Length in bytes of the Data.
631 @param[in] Context Callback context set by HttpInitMsgParser().
632
633 @retval EFI_SUCCESS Continue to parser the message body.
634 @retval Others Abort the parse.
635
636 **/
637 EFI_STATUS
638 EFIAPI
639 HttpBootGetBootFileCallback (
640 IN HTTP_BODY_PARSE_EVENT EventType,
641 IN CHAR8 *Data,
642 IN UINTN Length,
643 IN VOID *Context
644 )
645 {
646 HTTP_BOOT_CALLBACK_DATA *CallbackData;
647 HTTP_BOOT_ENTITY_DATA *NewEntityData;
648
649 //
650 // We only care about the entity data.
651 //
652 if (EventType != BodyParseEventOnData) {
653 return EFI_SUCCESS;
654 }
655
656 CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context;
657 //
658 // Copy data if caller has provided a buffer.
659 //
660 if (CallbackData->BufferSize > CallbackData->CopyedSize) {
661 CopyMem (
662 CallbackData->Buffer + CallbackData->CopyedSize,
663 Data,
664 MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize)
665 );
666 CallbackData->CopyedSize += MIN (Length, CallbackData->BufferSize - CallbackData->CopyedSize);
667 }
668
669 //
670 // The caller doesn't provide a buffer, save the block into cache list.
671 //
672 if (CallbackData->Cache != NULL) {
673 NewEntityData = AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA));
674 if (NewEntityData == NULL) {
675 return EFI_OUT_OF_RESOURCES;
676 }
677 if (CallbackData->NewBlock) {
678 NewEntityData->Block = CallbackData->Block;
679 CallbackData->Block = NULL;
680 }
681 NewEntityData->DataLength = Length;
682 NewEntityData->DataStart = (UINT8*) Data;
683 InsertTailList (&CallbackData->Cache->EntityDataList, &NewEntityData->Link);
684 }
685 return EFI_SUCCESS;
686 }
687
688 /**
689 This function download the boot file by using UEFI HTTP protocol.
690
691 @param[in] Private The pointer to the driver's private data.
692 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
693 the caller only want to know the size of the requested file.
694 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
695 code of EFI_SUCCESS, the amount of data transferred to
696 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
697 the size of Buffer required to retrieve the requested file.
698 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
699 then the size of the requested file is returned in
700 BufferSize.
701
702 @retval EFI_SUCCESS The file was loaded.
703 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
704 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
705 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
706 BufferSize has been updated with the size needed to complete
707 the request.
708 @retval Others Unexpected error happened.
709
710 **/
711 EFI_STATUS
712 HttpBootGetBootFile (
713 IN HTTP_BOOT_PRIVATE_DATA *Private,
714 IN BOOLEAN HeaderOnly,
715 IN OUT UINTN *BufferSize,
716 OUT UINT8 *Buffer
717 )
718 {
719 EFI_STATUS Status;
720 EFI_HTTP_STATUS_CODE StatusCode;
721 CHAR8 *HostName;
722 EFI_HTTP_REQUEST_DATA *RequestData;
723 HTTP_IO_RESPONSE_DATA *ResponseData;
724 HTTP_IO_RESPONSE_DATA ResponseBody;
725 HTTP_IO *HttpIo;
726 HTTP_IO_HEADER *HttpIoHeader;
727 VOID *Parser;
728 HTTP_BOOT_CALLBACK_DATA Context;
729 UINTN ContentLength;
730 HTTP_BOOT_CACHE_CONTENT *Cache;
731 UINT8 *Block;
732 CHAR16 *Url;
733 BOOLEAN IdentityMode;
734 UINTN ReceivedSize;
735
736 ASSERT (Private != NULL);
737 ASSERT (Private->HttpCreated);
738
739 if (BufferSize == NULL) {
740 return EFI_INVALID_PARAMETER;
741 }
742
743 if (*BufferSize != 0 && Buffer == NULL) {
744 return EFI_INVALID_PARAMETER;
745 }
746
747 //
748 // First, check whether we already cached the requested Uri.
749 //
750 Url = AllocatePool ((AsciiStrLen (Private->BootFileUri) + 1) * sizeof (CHAR16));
751 if (Url == NULL) {
752 return EFI_OUT_OF_RESOURCES;
753 }
754 AsciiStrToUnicodeStr (Private->BootFileUri, Url);
755 if (!HeaderOnly) {
756 Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer);
757 if (Status != EFI_NOT_FOUND) {
758 FreePool (Url);
759 return Status;
760 }
761 }
762
763 //
764 // Not found in cache, try to download it through HTTP.
765 //
766
767 //
768 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
769 //
770 Cache = NULL;
771 if ((!HeaderOnly) && (*BufferSize == 0)) {
772 Cache = AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT));
773 if (Cache == NULL) {
774 Status = EFI_OUT_OF_RESOURCES;
775 goto ERROR_1;
776 }
777 InitializeListHead (&Cache->EntityDataList);
778 }
779
780 //
781 // 2. Send HTTP request message.
782 //
783
784 //
785 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
786 // Host
787 // Accept
788 // User-Agent
789 //
790 HttpIoHeader = HttpBootCreateHeader (3);
791 if (HttpIoHeader == NULL) {
792 Status = EFI_OUT_OF_RESOURCES;
793 goto ERROR_2;
794 }
795
796 //
797 // Add HTTP header field 1: Host
798 //
799 HostName = NULL;
800 Status = HttpUrlGetHostName (
801 Private->BootFileUri,
802 Private->BootFileUriParser,
803 &HostName
804 );
805 if (EFI_ERROR (Status)) {
806 goto ERROR_3;
807 }
808 Status = HttpBootSetHeader (
809 HttpIoHeader,
810 HTTP_FIELD_NAME_HOST,
811 HostName
812 );
813 FreePool (HostName);
814 if (EFI_ERROR (Status)) {
815 goto ERROR_3;
816 }
817
818 //
819 // Add HTTP header field 2: Accept
820 //
821 Status = HttpBootSetHeader (
822 HttpIoHeader,
823 HTTP_FIELD_NAME_ACCEPT,
824 "*/*"
825 );
826 if (EFI_ERROR (Status)) {
827 goto ERROR_3;
828 }
829
830 //
831 // Add HTTP header field 3: User-Agent
832 //
833 Status = HttpBootSetHeader (
834 HttpIoHeader,
835 HTTP_FIELD_NAME_USER_AGENT,
836 HTTP_USER_AGENT_EFI_HTTP_BOOT
837 );
838 if (EFI_ERROR (Status)) {
839 goto ERROR_3;
840 }
841
842 //
843 // 2.2 Build the rest of HTTP request info.
844 //
845 RequestData = AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA));
846 if (RequestData == NULL) {
847 Status = EFI_OUT_OF_RESOURCES;
848 goto ERROR_3;
849 }
850 RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;
851 RequestData->Url = Url;
852 if (RequestData->Url == NULL) {
853 Status = EFI_OUT_OF_RESOURCES;
854 goto ERROR_4;
855 }
856 AsciiStrToUnicodeStr (Private->BootFileUri, RequestData->Url);
857
858 //
859 // 2.3 Record the request info in a temp cache item.
860 //
861 if (Cache != NULL) {
862 Cache->RequestData = RequestData;
863 }
864
865 //
866 // 2.4 Send out the request to HTTP server.
867 //
868 HttpIo = &Private->HttpIo;
869 Status = HttpIoSendRequest (
870 HttpIo,
871 RequestData,
872 HttpIoHeader->HeaderCount,
873 HttpIoHeader->Headers,
874 0,
875 NULL
876 );
877 if (EFI_ERROR (Status)) {
878 goto ERROR_4;
879 }
880
881 //
882 // 3. Receive HTTP response message.
883 //
884
885 //
886 // 3.1 First step, use zero BodyLength to only receive the response headers.
887 //
888 ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA));
889 if (ResponseData == NULL) {
890 Status = EFI_OUT_OF_RESOURCES;
891 goto ERROR_4;
892 }
893 Status = HttpIoRecvResponse (
894 &Private->HttpIo,
895 TRUE,
896 ResponseData
897 );
898 if (EFI_ERROR (Status) || EFI_ERROR (ResponseData->Status)) {
899 if (EFI_ERROR (ResponseData->Status)) {
900 StatusCode = HttpIo->RspToken.Message->Data.Response->StatusCode;
901 HttpBootPrintErrorMessage (StatusCode);
902 Status = ResponseData->Status;
903 }
904 goto ERROR_5;
905 }
906
907 //
908 // 3.2 Cache the response header.
909 //
910 if (Cache != NULL) {
911 Cache->ResponseData = ResponseData;
912 }
913
914 //
915 // 3.3 Init a message-body parser from the header information.
916 //
917 Parser = NULL;
918 Context.NewBlock = FALSE;
919 Context.Block = NULL;
920 Context.CopyedSize = 0;
921 Context.Buffer = Buffer;
922 Context.BufferSize = *BufferSize;
923 Context.Cache = Cache;
924 Status = HttpInitMsgParser (
925 HeaderOnly? HttpMethodHead : HttpMethodGet,
926 ResponseData->Response.StatusCode,
927 ResponseData->HeaderCount,
928 ResponseData->Headers,
929 HttpBootGetBootFileCallback,
930 (VOID*) &Context,
931 &Parser
932 );
933 if (EFI_ERROR (Status)) {
934 goto ERROR_6;
935 }
936
937 //
938 // 3.4 Continue to receive and parse message-body if needed.
939 //
940 Block = NULL;
941 if (!HeaderOnly) {
942 //
943 // 3.4.1, check whether we are in identity transfer-coding.
944 //
945 ContentLength = 0;
946 Status = HttpGetEntityLength (Parser, &ContentLength);
947 if (!EFI_ERROR (Status)) {
948 IdentityMode = TRUE;
949 } else {
950 IdentityMode = FALSE;
951 }
952
953 //
954 // 3.4.2, start the message-body download, the identity and chunked transfer-coding
955 // is handled in different path here.
956 //
957 ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESPONSE_DATA));
958 if (IdentityMode) {
959 //
960 // In identity transfer-coding there is no need to parse the message body,
961 // just download the message body to the user provided buffer directly.
962 //
963 ReceivedSize = 0;
964 while (ReceivedSize < ContentLength) {
965 ResponseBody.Body = (CHAR8*) Buffer + ReceivedSize;
966 ResponseBody.BodyLength = *BufferSize - ReceivedSize;
967 Status = HttpIoRecvResponse (
968 &Private->HttpIo,
969 FALSE,
970 &ResponseBody
971 );
972 if (EFI_ERROR (Status)) {
973 goto ERROR_6;
974 }
975 ReceivedSize += ResponseBody.BodyLength;
976 }
977 } else {
978 //
979 // In "chunked" transfer-coding mode, so we need to parse the received
980 // data to get the real entity content.
981 //
982 Block = NULL;
983 while (!HttpIsMessageComplete (Parser)) {
984 //
985 // Allocate a buffer in Block to hold the message-body.
986 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().
987 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before
988 // every HttpIoRecvResponse().
989 //
990 if (Block == NULL || Context.BufferSize == 0) {
991 Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);
992 if (Block == NULL) {
993 Status = EFI_OUT_OF_RESOURCES;
994 goto ERROR_6;
995 }
996 Context.NewBlock = TRUE;
997 Context.Block = Block;
998 } else {
999 Context.NewBlock = FALSE;
1000 }
1001
1002 ResponseBody.Body = (CHAR8*) Block;
1003 ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;
1004 Status = HttpIoRecvResponse (
1005 &Private->HttpIo,
1006 FALSE,
1007 &ResponseBody
1008 );
1009 if (EFI_ERROR (Status)) {
1010 goto ERROR_6;
1011 }
1012
1013 //
1014 // Parse the new received block of the message-body, the block will be saved in cache.
1015 //
1016 Status = HttpParseMessageBody (
1017 Parser,
1018 ResponseBody.BodyLength,
1019 ResponseBody.Body
1020 );
1021 if (EFI_ERROR (Status)) {
1022 goto ERROR_6;
1023 }
1024 }
1025 }
1026 }
1027
1028 //
1029 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.
1030 //
1031 Status = HttpGetEntityLength (Parser, &ContentLength);
1032 if (EFI_ERROR (Status)) {
1033 goto ERROR_6;
1034 }
1035
1036 if (*BufferSize < ContentLength) {
1037 Status = EFI_BUFFER_TOO_SMALL;
1038 }
1039 *BufferSize = ContentLength;
1040
1041 //
1042 // 4. Save the cache item to driver's cache list and return.
1043 //
1044 if (Cache != NULL) {
1045 Cache->EntityLength = ContentLength;
1046 InsertTailList (&Private->CacheList, &Cache->Link);
1047 }
1048
1049 if (Parser != NULL) {
1050 HttpFreeMsgParser (Parser);
1051 }
1052
1053 return EFI_SUCCESS;
1054
1055 ERROR_6:
1056 if (Parser != NULL) {
1057 HttpFreeMsgParser (Parser);
1058 }
1059 if (Context.Block != NULL) {
1060 FreePool (Context.Block);
1061 }
1062 HttpBootFreeCache (Cache);
1063
1064 ERROR_5:
1065 if (ResponseData != NULL) {
1066 FreePool (ResponseData);
1067 }
1068 ERROR_4:
1069 if (RequestData != NULL) {
1070 FreePool (RequestData);
1071 }
1072 ERROR_3:
1073 HttpBootFreeHeader (HttpIoHeader);
1074 ERROR_2:
1075 if (Cache != NULL) {
1076 FreePool (Cache);
1077 }
1078 ERROR_1:
1079 if (Url != NULL) {
1080 FreePool (Url);
1081 }
1082
1083 return Status;
1084 }