2 Routines to process Rrq (download).
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
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.
16 #include "Mtftp4Impl.h"
20 The packet process callback for MTFTP download.
22 @param UdpPacket The packet received
23 @param EndPoint The local/remote access point of the packet
24 @param IoStatus The status of the receiving
25 @param Context Opaque parameter, which is the MTFTP session
31 IN NET_BUF
*UdpPacket
,
32 IN UDP_END_POINT
*EndPoint
,
33 IN EFI_STATUS IoStatus
,
39 Start the MTFTP session to download.
41 It will first initialize some of the internal states then build and send a RRQ
42 reqeuest packet, at last, it will start receive for the downloading.
44 @param Instance The Mtftp session
45 @param Operation The MTFTP opcode, it may be a EFI_MTFTP4_OPCODE_RRQ
46 or EFI_MTFTP4_OPCODE_DIR.
48 @retval EFI_SUCCESS The mtftp download session is started.
49 @retval Others Failed to start downloading.
54 IN MTFTP4_PROTOCOL
*Instance
,
61 // The valid block number range are [1, 0xffff]. For example:
62 // the client sends an RRQ request to the server, the server
63 // transfers the DATA1 block. If option negoitation is ongoing,
64 // the server will send back an OACK, then client will send ACK0.
66 Status
= Mtftp4InitBlockRange (&Instance
->Blocks
, 1, 0xffff);
68 if (EFI_ERROR (Status
)) {
72 Status
= Mtftp4SendRequest (Instance
);
74 if (EFI_ERROR (Status
)) {
78 return UdpIoRecvDatagram (Instance
->UnicastPort
, Mtftp4RrqInput
, Instance
, 0);
83 Build and send a ACK packet for the download session.
85 @param Instance The Mtftp session
86 @param BlkNo The BlkNo to ack.
88 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet
89 @retval EFI_SUCCESS The ACK has been sent
90 @retval Others Failed to send the ACK.
95 IN MTFTP4_PROTOCOL
*Instance
,
99 EFI_MTFTP4_PACKET
*Ack
;
102 Packet
= NetbufAlloc (sizeof (EFI_MTFTP4_ACK_HEADER
));
103 if (Packet
== NULL
) {
104 return EFI_OUT_OF_RESOURCES
;
107 Ack
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (
109 sizeof (EFI_MTFTP4_ACK_HEADER
),
112 ASSERT (Ack
!= NULL
);
114 Ack
->Ack
.OpCode
= HTONS (EFI_MTFTP4_OPCODE_ACK
);
115 Ack
->Ack
.Block
[0] = HTONS (BlkNo
);
117 return Mtftp4SendPacket (Instance
, Packet
);
122 Deliver the received data block to the user, which can be saved
123 in the user provide buffer or through the CheckPacket callback.
125 @param Instance The Mtftp session
126 @param Packet The received data packet
127 @param Len The packet length
129 @retval EFI_SUCCESS The data is saved successfully
130 @retval EFI_ABORTED The user tells to abort by return an error through
132 @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small and buffer length is
133 updated to the actual buffer size needed.
138 IN OUT MTFTP4_PROTOCOL
*Instance
,
139 IN EFI_MTFTP4_PACKET
*Packet
,
143 EFI_MTFTP4_TOKEN
*Token
;
152 Token
= Instance
->Token
;
153 Block
= NTOHS (Packet
->Data
.Block
);
154 DataLen
= Len
- MTFTP4_DATA_HEAD_LEN
;
157 // This is the last block, save the block no
159 if (DataLen
< Instance
->BlkSize
) {
161 Instance
->LastBlock
= Block
;
162 Mtftp4SetLastBlockNum (&Instance
->Blocks
, Block
);
166 // Remove this block number from the file hole. If Mtftp4RemoveBlockNum
167 // returns EFI_NOT_FOUND, the block has been saved, don't save it again.
168 // Note that : For bigger files, allowing the block counter to roll over
169 // to accept transfers of unlimited size. So TotalBlock is memorised as
170 // continuous block counter.
172 Status
= Mtftp4RemoveBlockNum (&Instance
->Blocks
, Block
, Completed
, &TotalBlock
);
174 if (Status
== EFI_NOT_FOUND
) {
176 } else if (EFI_ERROR (Status
)) {
180 if (Token
->CheckPacket
!= NULL
) {
181 Status
= Token
->CheckPacket (&Instance
->Mtftp4
, Token
, (UINT16
) Len
, Packet
);
183 if (EFI_ERROR (Status
)) {
186 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION
,
187 (UINT8
*) "User aborted download"
194 if (Token
->Buffer
!= NULL
) {
195 Start
= MultU64x32 (TotalBlock
- 1, Instance
->BlkSize
);
197 if (Start
+ DataLen
<= Token
->BufferSize
) {
198 CopyMem ((UINT8
*) Token
->Buffer
+ Start
, Packet
->Data
.Data
, DataLen
);
201 // Update the file size when received the last block
203 if ((Instance
->LastBlock
== Block
) && Completed
) {
204 Token
->BufferSize
= Start
+ DataLen
;
207 } else if (Instance
->LastBlock
!= 0) {
209 // Don't save the data if the buffer is too small, return
210 // EFI_BUFFER_TOO_SMALL if received the last packet. This
211 // will give a accurate file length.
213 Token
->BufferSize
= Start
+ DataLen
;
217 EFI_MTFTP4_ERRORCODE_DISK_FULL
,
218 (UINT8
*) "User provided memory block is too small"
221 return EFI_BUFFER_TOO_SMALL
;
230 Function to process the received data packets.
232 It will save the block then send back an ACK if it is active.
234 @param Instance The downloading MTFTP session
235 @param Packet The packet received
236 @param Len The length of the packet
237 @param Multicast Whether this packet is multicast or unicast
238 @param Completed Return whether the download has completed
240 @retval EFI_SUCCESS The data packet is successfully processed
241 @retval EFI_ABORTED The download is aborted by the user
242 @retval EFI_BUFFER_TOO_SMALL The user provided buffer is too small
246 Mtftp4RrqHandleData (
247 IN MTFTP4_PROTOCOL
*Instance
,
248 IN EFI_MTFTP4_PACKET
*Packet
,
250 IN BOOLEAN Multicast
,
251 OUT BOOLEAN
*Completed
259 BlockNum
= NTOHS (Packet
->Data
.Block
);
260 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
262 ASSERT (Expected
>= 0);
265 // If we are active and received an unexpected packet, retransmit
266 // the last ACK then restart receiving. If we are passive, save
269 if (Instance
->Master
&& (Expected
!= BlockNum
)) {
270 Mtftp4Retransmit (Instance
);
274 Status
= Mtftp4RrqSaveBlock (Instance
, Packet
, Len
);
276 if (EFI_ERROR (Status
)) {
281 // Reset the passive client's timer whenever it received a
282 // valid data packet.
284 if (!Instance
->Master
) {
285 Mtftp4SetTimeout (Instance
);
289 // Check whether we have received all the blocks. Send the ACK if we
290 // are active (unicast client or master client for multicast download).
291 // If we have received all the blocks, send an ACK even if we are passive
292 // to tell the server that we are done.
294 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
296 if (Instance
->Master
|| (Expected
< 0)) {
299 // If we are passive client, then the just received Block maybe
300 // isn't the last block. We need to send an ACK to the last block
301 // to inform the server that we are done. If we are active client,
302 // the Block == Instance->LastBlock.
304 BlockNum
= Instance
->LastBlock
;
308 BlockNum
= (UINT16
) (Expected
- 1);
311 Mtftp4RrqSendAck (Instance
, BlockNum
);
319 Validate whether the options received in the server's OACK packet is valid.
321 The options are valid only if:
322 1. The server doesn't include options not requested by us
323 2. The server can only use smaller blksize than that is requested
324 3. The server can only use the same timeout as requested
325 4. The server doesn't change its multicast channel.
327 @param This The downloading Mtftp session
328 @param Reply The options in the OACK packet
329 @param Request The requested options
331 @retval TRUE The options in the OACK is OK.
332 @retval FALSE The options in the OACK is invalid.
337 IN MTFTP4_PROTOCOL
*This
,
338 IN MTFTP4_OPTION
*Reply
,
339 IN MTFTP4_OPTION
*Request
344 // It is invalid for server to return options we don't request
346 if ((Reply
->Exist
&~Request
->Exist
) != 0) {
351 // Server can only specify a smaller block size to be used and
352 // return the timeout matches that requested.
354 if ((((Reply
->Exist
& MTFTP4_BLKSIZE_EXIST
) != 0)&& (Reply
->BlkSize
> Request
->BlkSize
)) ||
355 (((Reply
->Exist
& MTFTP4_TIMEOUT_EXIST
) != 0) && (Reply
->Timeout
!= Request
->Timeout
))) {
360 // The server can send ",,master" to client to change its master
361 // setting. But if it use the specific multicast channel, it can't
362 // change the setting.
364 if (((Reply
->Exist
& MTFTP4_MCAST_EXIST
) != 0) && (This
->McastIp
!= 0)) {
365 if ((Reply
->McastIp
!= 0) && (Reply
->McastIp
!= This
->McastIp
)) {
369 if ((Reply
->McastPort
!= 0) && (Reply
->McastPort
!= This
->McastPort
)) {
379 Configure a UDP IO port to receive the multicast.
381 @param McastIo The UDP IO to configure
382 @param Context The opaque parameter to the function which is the
385 @retval EFI_SUCCESS The UDP child is successfully configured.
386 @retval Others Failed to configure the UDP child.
391 Mtftp4RrqConfigMcastPort (
396 MTFTP4_PROTOCOL
*Instance
;
397 EFI_MTFTP4_CONFIG_DATA
*Config
;
398 EFI_UDP4_CONFIG_DATA UdpConfig
;
399 EFI_IPv4_ADDRESS Group
;
403 Instance
= (MTFTP4_PROTOCOL
*) Context
;
404 Config
= &Instance
->Config
;
406 UdpConfig
.AcceptBroadcast
= FALSE
;
407 UdpConfig
.AcceptPromiscuous
= FALSE
;
408 UdpConfig
.AcceptAnyPort
= FALSE
;
409 UdpConfig
.AllowDuplicatePort
= FALSE
;
410 UdpConfig
.TypeOfService
= 0;
411 UdpConfig
.TimeToLive
= 64;
412 UdpConfig
.DoNotFragment
= FALSE
;
413 UdpConfig
.ReceiveTimeout
= 0;
414 UdpConfig
.TransmitTimeout
= 0;
415 UdpConfig
.UseDefaultAddress
= Config
->UseDefaultSetting
;
416 UdpConfig
.StationAddress
= Config
->StationIp
;
417 UdpConfig
.SubnetMask
= Config
->SubnetMask
;
418 UdpConfig
.StationPort
= Instance
->McastPort
;
419 UdpConfig
.RemotePort
= 0;
421 Ip
= HTONL (Instance
->ServerIp
);
422 CopyMem (&UdpConfig
.RemoteAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
424 Status
= McastIo
->Protocol
.Udp4
->Configure (McastIo
->Protocol
.Udp4
, &UdpConfig
);
426 if (EFI_ERROR (Status
)) {
430 if (!Config
->UseDefaultSetting
&&
431 !EFI_IP4_EQUAL (&mZeroIp4Addr
, &Config
->GatewayIp
)) {
433 // The station IP address is manually configured and the Gateway IP is not 0.
434 // Add the default route for this UDP instance.
436 Status
= McastIo
->Protocol
.Udp4
->Routes (
437 McastIo
->Protocol
.Udp4
,
444 if (EFI_ERROR (Status
)) {
445 McastIo
->Protocol
.Udp4
->Configure (McastIo
->Protocol
.Udp4
, NULL
);
451 // join the multicast group
453 Ip
= HTONL (Instance
->McastIp
);
454 CopyMem (&Group
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
456 return McastIo
->Protocol
.Udp4
->Groups (McastIo
->Protocol
.Udp4
, TRUE
, &Group
);
461 Function to process the OACK.
463 It will first validate the OACK packet, then update the various negotiated parameters.
465 @param Instance The download MTFTP session
466 @param Packet The packet received
467 @param Len The packet length
468 @param Multicast Whether this packet is received as a multicast
469 @param Completed Returns whether the download has completed. NOT
470 used by this function.
472 @retval EFI_DEVICE_ERROR Failed to create/start a multicast UDP child
473 @retval EFI_TFTP_ERROR Some error happened during the process
474 @retval EFI_SUCCESS The OACK is successfully processed.
478 Mtftp4RrqHandleOack (
479 IN OUT MTFTP4_PROTOCOL
*Instance
,
480 IN EFI_MTFTP4_PACKET
*Packet
,
482 IN BOOLEAN Multicast
,
483 OUT BOOLEAN
*Completed
489 EFI_UDP4_PROTOCOL
*Udp4
;
494 // If already started the master download, don't change the
495 // setting. Master download always succeeds.
497 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
498 ASSERT (Expected
!= -1);
500 if (Instance
->Master
&& (Expected
!= 1)) {
505 // Parse and validate the options from server
507 ZeroMem (&Reply
, sizeof (MTFTP4_OPTION
));
509 Status
= Mtftp4ParseOptionOack (Packet
, Len
, &Reply
);
511 if (EFI_ERROR (Status
) ||
512 !Mtftp4RrqOackValid (Instance
, &Reply
, &Instance
->RequestOption
)) {
514 // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.
516 if (Status
!= EFI_OUT_OF_RESOURCES
) {
519 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION
,
520 (UINT8
*) "Mal-formated OACK packet"
524 return EFI_TFTP_ERROR
;
527 if ((Reply
.Exist
& MTFTP4_MCAST_EXIST
) != 0) {
530 // Save the multicast info. Always update the Master, only update the
531 // multicast IP address, block size, timeoute at the first time. If IP
532 // address is updated, create a UDP child to receive the multicast.
534 Instance
->Master
= Reply
.Master
;
536 if (Instance
->McastIp
== 0) {
537 if ((Reply
.McastIp
== 0) || (Reply
.McastPort
== 0)) {
540 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION
,
541 (UINT8
*) "Illegal multicast setting"
544 return EFI_TFTP_ERROR
;
548 // Create a UDP child then start receive the multicast from it.
550 Instance
->McastIp
= Reply
.McastIp
;
551 Instance
->McastPort
= Reply
.McastPort
;
552 if (Instance
->McastUdpPort
== NULL
) {
553 Instance
->McastUdpPort
= UdpIoCreateIo (
554 Instance
->Service
->Controller
,
555 Instance
->Service
->Image
,
556 Mtftp4RrqConfigMcastPort
,
560 if (Instance
->McastUdpPort
!= NULL
) {
561 Status
= gBS
->OpenProtocol (
562 Instance
->McastUdpPort
->UdpHandle
,
563 &gEfiUdp4ProtocolGuid
,
565 Instance
->Service
->Image
,
567 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
569 if (EFI_ERROR (Status
)) {
570 UdpIoFreeIo (Instance
->McastUdpPort
);
571 Instance
->McastUdpPort
= NULL
;
572 return EFI_DEVICE_ERROR
;
578 if (Instance
->McastUdpPort
== NULL
) {
579 return EFI_DEVICE_ERROR
;
582 Status
= UdpIoRecvDatagram (Instance
->McastUdpPort
, Mtftp4RrqInput
, Instance
, 0);
584 if (EFI_ERROR (Status
)) {
587 EFI_MTFTP4_ERRORCODE_ACCESS_VIOLATION
,
588 (UINT8
*) "Failed to create socket to receive multicast packet"
595 // Update the parameters used.
597 if (Reply
.BlkSize
!= 0) {
598 Instance
->BlkSize
= Reply
.BlkSize
;
601 if (Reply
.Timeout
!= 0) {
602 Instance
->Timeout
= Reply
.Timeout
;
607 Instance
->Master
= TRUE
;
609 if (Reply
.BlkSize
!= 0) {
610 Instance
->BlkSize
= Reply
.BlkSize
;
613 if (Reply
.Timeout
!= 0) {
614 Instance
->Timeout
= Reply
.Timeout
;
619 // Send an ACK to (Expected - 1) which is 0 for unicast download,
620 // or tell the server we want to receive the Expected block.
622 return Mtftp4RrqSendAck (Instance
, (UINT16
) (Expected
- 1));
627 The packet process callback for MTFTP download.
629 @param UdpPacket The packet received
630 @param EndPoint The local/remote access point of the packet
631 @param IoStatus The status of the receiving
632 @param Context Opaque parameter, which is the MTFTP session
638 IN NET_BUF
*UdpPacket
,
639 IN UDP_END_POINT
*EndPoint
,
640 IN EFI_STATUS IoStatus
,
644 MTFTP4_PROTOCOL
*Instance
;
645 EFI_MTFTP4_PACKET
*Packet
;
652 Instance
= (MTFTP4_PROTOCOL
*) Context
;
653 NET_CHECK_SIGNATURE (Instance
, MTFTP4_PROTOCOL_SIGNATURE
);
655 Status
= EFI_SUCCESS
;
660 if (EFI_ERROR (IoStatus
)) {
665 ASSERT (UdpPacket
!= NULL
);
668 // Find the port this packet is from to restart receive correctly.
670 Multicast
= (BOOLEAN
) (EndPoint
->LocalAddr
.Addr
[0] == Instance
->McastIp
);
672 if (UdpPacket
->TotalSize
< MTFTP4_OPCODE_LEN
) {
677 // Client send initial request to server's listening port. Server
678 // will select a UDP port to communicate with the client. The server
679 // is required to use the same port as RemotePort to multicast the
682 if (EndPoint
->RemotePort
!= Instance
->ConnectedPort
) {
683 if (Instance
->ConnectedPort
!= 0) {
686 Instance
->ConnectedPort
= EndPoint
->RemotePort
;
691 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
693 Len
= UdpPacket
->TotalSize
;
695 if (UdpPacket
->BlockOpNum
> 1) {
696 Packet
= AllocatePool (Len
);
698 if (Packet
== NULL
) {
699 Status
= EFI_OUT_OF_RESOURCES
;
703 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
706 Packet
= (EFI_MTFTP4_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
709 Opcode
= NTOHS (Packet
->OpCode
);
712 // Call the user's CheckPacket if provided. Abort the transmission
713 // if CheckPacket returns an EFI_ERROR code.
715 if ((Instance
->Token
->CheckPacket
!= NULL
) &&
716 ((Opcode
== EFI_MTFTP4_OPCODE_OACK
) || (Opcode
== EFI_MTFTP4_OPCODE_ERROR
))) {
718 Status
= Instance
->Token
->CheckPacket (
725 if (EFI_ERROR (Status
)) {
727 // Send an error message to the server to inform it
729 if (Opcode
!= EFI_MTFTP4_OPCODE_ERROR
) {
732 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
733 (UINT8
*) "User aborted the transfer"
737 Status
= EFI_ABORTED
;
743 case EFI_MTFTP4_OPCODE_DATA
:
744 if ((Len
> (UINT32
) (MTFTP4_DATA_HEAD_LEN
+ Instance
->BlkSize
)) ||
745 (Len
< (UINT32
) MTFTP4_DATA_HEAD_LEN
)) {
749 Status
= Mtftp4RrqHandleData (Instance
, Packet
, Len
, Multicast
, &Completed
);
752 case EFI_MTFTP4_OPCODE_OACK
:
753 if (Multicast
|| (Len
<= MTFTP4_OPCODE_LEN
)) {
757 Status
= Mtftp4RrqHandleOack (Instance
, Packet
, Len
, Multicast
, &Completed
);
760 case EFI_MTFTP4_OPCODE_ERROR
:
761 Status
= EFI_TFTP_ERROR
;
771 // Free the resources, then if !EFI_ERROR (Status), restart the
772 // receive, otherwise end the session.
774 if ((Packet
!= NULL
) && (UdpPacket
->BlockOpNum
> 1)) {
778 if (UdpPacket
!= NULL
) {
779 NetbufFree (UdpPacket
);
782 if (!EFI_ERROR (Status
) && !Completed
) {
784 Status
= UdpIoRecvDatagram (Instance
->McastUdpPort
, Mtftp4RrqInput
, Instance
, 0);
786 Status
= UdpIoRecvDatagram (Instance
->UnicastPort
, Mtftp4RrqInput
, Instance
, 0);
790 if (EFI_ERROR (Status
) || Completed
) {
791 Mtftp4CleanOperation (Instance
, Status
);