2 Routines to process Rrq (download).
4 Copyright (c) 2006 - 2009, Intel Corporation<BR>
5 All rights reserved. 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
30 IN NET_BUF
*UdpPacket
,
31 IN UDP_END_POINT
*EndPoint
,
32 IN EFI_STATUS IoStatus
,
38 Start the MTFTP session to download.
40 It will first initialize some of the internal states then build and send a RRQ
41 reqeuest packet, at last, it will start receive for the downloading.
43 @param Instance The Mtftp session
44 @param Operation The MTFTP opcode, it may be a EFI_MTFTP4_OPCODE_RRQ
45 or EFI_MTFTP4_OPCODE_DIR.
47 @retval EFI_SUCCESS The mtftp download session is started.
48 @retval Others Failed to start downloading.
53 IN MTFTP4_PROTOCOL
*Instance
,
60 // The valid block number range are [1, 0xffff]. For example:
61 // the client sends an RRQ request to the server, the server
62 // transfers the DATA1 block. If option negoitation is ongoing,
63 // the server will send back an OACK, then client will send ACK0.
65 Status
= Mtftp4InitBlockRange (&Instance
->Blocks
, 1, 0xffff);
67 if (EFI_ERROR (Status
)) {
71 Status
= Mtftp4SendRequest (Instance
);
73 if (EFI_ERROR (Status
)) {
77 return UdpIoRecvDatagram (Instance
->UnicastPort
, Mtftp4RrqInput
, Instance
, 0);
82 Build and send a ACK packet for the download session.
84 @param Instance The Mtftp session
85 @param BlkNo The BlkNo to ack.
87 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet
88 @retval EFI_SUCCESS The ACK has been sent
89 @retval Others Failed to send the ACK.
94 IN MTFTP4_PROTOCOL
*Instance
,
98 EFI_MTFTP4_PACKET
*Ack
;
101 Packet
= NetbufAlloc (sizeof (EFI_MTFTP4_ACK_HEADER
));
102 if (Packet
== NULL
) {
103 return EFI_OUT_OF_RESOURCES
;
106 Ack
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (
108 sizeof (EFI_MTFTP4_ACK_HEADER
),
111 ASSERT (Ack
!= NULL
);
113 Ack
->Ack
.OpCode
= HTONS (EFI_MTFTP4_OPCODE_ACK
);
114 Ack
->Ack
.Block
[0] = HTONS (BlkNo
);
116 return Mtftp4SendPacket (Instance
, Packet
);
121 Deliver the received data block to the user, which can be saved
122 in the user provide buffer or through the CheckPacket callback.
124 @param Instance The Mtftp session
125 @param Packet The received data packet
126 @param Len The packet length
128 @retval EFI_SUCCESS The data is saved successfully
129 @retval EFI_ABORTED The user tells to abort by return an error through
131 @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small and buffer length is
132 updated to the actual buffer size needed.
137 IN OUT MTFTP4_PROTOCOL
*Instance
,
138 IN EFI_MTFTP4_PACKET
*Packet
,
142 EFI_MTFTP4_TOKEN
*Token
;
148 Token
= Instance
->Token
;
149 Block
= NTOHS (Packet
->Data
.Block
);
150 DataLen
= Len
- MTFTP4_DATA_HEAD_LEN
;
153 // This is the last block, save the block no
155 if (DataLen
< Instance
->BlkSize
) {
156 Instance
->LastBlock
= Block
;
157 Mtftp4SetLastBlockNum (&Instance
->Blocks
, Block
);
161 // Remove this block number from the file hole. If Mtftp4RemoveBlockNum
162 // returns EFI_NOT_FOUND, the block has been saved, don't save it again.
164 Status
= Mtftp4RemoveBlockNum (&Instance
->Blocks
, Block
);
166 if (Status
== EFI_NOT_FOUND
) {
168 } else if (EFI_ERROR (Status
)) {
172 if (Token
->CheckPacket
!= NULL
) {
173 Status
= Token
->CheckPacket (&Instance
->Mtftp4
, Token
, (UINT16
) Len
, Packet
);
175 if (EFI_ERROR (Status
)) {
178 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION
,
179 (UINT8
*) "User aborted download"
186 if (Token
->Buffer
!= NULL
) {
187 Start
= MultU64x32 (Block
- 1, Instance
->BlkSize
);
189 if (Start
+ DataLen
<= Token
->BufferSize
) {
190 CopyMem ((UINT8
*) Token
->Buffer
+ Start
, Packet
->Data
.Data
, DataLen
);
193 // Update the file size when received the last block
195 if (Instance
->LastBlock
== Block
) {
196 Token
->BufferSize
= Start
+ DataLen
;
199 } else if (Instance
->LastBlock
!= 0) {
201 // Don't save the data if the buffer is too small, return
202 // EFI_BUFFER_TOO_SMALL if received the last packet. This
203 // will give a accurate file length.
205 Token
->BufferSize
= Start
+ DataLen
;
209 EFI_MTFTP4_ERRORCODE_DISK_FULL
,
210 (UINT8
*) "User provided memory block is too small"
213 return EFI_BUFFER_TOO_SMALL
;
222 Function to process the received data packets.
224 It will save the block then send back an ACK if it is active.
226 @param Instance The downloading MTFTP session
227 @param Packet The packet received
228 @param Len The length of the packet
229 @param Multicast Whether this packet is multicast or unicast
230 @param Completed Return whether the download has completed
232 @retval EFI_SUCCESS The data packet is successfully processed
233 @retval EFI_ABORTED The download is aborted by the user
234 @retval EFI_BUFFER_TOO_SMALL The user provided buffer is too small
238 Mtftp4RrqHandleData (
239 IN MTFTP4_PROTOCOL
*Instance
,
240 IN EFI_MTFTP4_PACKET
*Packet
,
242 IN BOOLEAN Multicast
,
243 OUT BOOLEAN
*Completed
251 BlockNum
= NTOHS (Packet
->Data
.Block
);
252 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
254 ASSERT (Expected
>= 0);
257 // If we are active and received an unexpected packet, retransmit
258 // the last ACK then restart receiving. If we are passive, save
261 if (Instance
->Master
&& (Expected
!= BlockNum
)) {
262 Mtftp4Retransmit (Instance
);
266 Status
= Mtftp4RrqSaveBlock (Instance
, Packet
, Len
);
268 if (EFI_ERROR (Status
)) {
273 // Reset the passive client's timer whenever it received a
274 // valid data packet.
276 if (!Instance
->Master
) {
277 Mtftp4SetTimeout (Instance
);
281 // Check whether we have received all the blocks. Send the ACK if we
282 // are active (unicast client or master client for multicast download).
283 // If we have received all the blocks, send an ACK even if we are passive
284 // to tell the server that we are done.
286 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
288 if (Instance
->Master
|| (Expected
< 0)) {
291 // If we are passive client, then the just received Block maybe
292 // isn't the last block. We need to send an ACK to the last block
293 // to inform the server that we are done. If we are active client,
294 // the Block == Instance->LastBlock.
296 BlockNum
= Instance
->LastBlock
;
300 BlockNum
= (UINT16
) (Expected
- 1);
303 Mtftp4RrqSendAck (Instance
, BlockNum
);
311 Validate whether the options received in the server's OACK packet is valid.
313 The options are valid only if:
314 1. The server doesn't include options not requested by us
315 2. The server can only use smaller blksize than that is requested
316 3. The server can only use the same timeout as requested
317 4. The server doesn't change its multicast channel.
319 @param This The downloading Mtftp session
320 @param Reply The options in the OACK packet
321 @param Request The requested options
323 @retval TRUE The options in the OACK is OK.
324 @retval FALSE The options in the OACK is invalid.
329 IN MTFTP4_PROTOCOL
*This
,
330 IN MTFTP4_OPTION
*Reply
,
331 IN MTFTP4_OPTION
*Request
336 // It is invalid for server to return options we don't request
338 if ((Reply
->Exist
&~Request
->Exist
) != 0) {
343 // Server can only specify a smaller block size to be used and
344 // return the timeout matches that requested.
346 if ((((Reply
->Exist
& MTFTP4_BLKSIZE_EXIST
) != 0)&& (Reply
->BlkSize
> Request
->BlkSize
)) ||
347 (((Reply
->Exist
& MTFTP4_TIMEOUT_EXIST
) != 0) && (Reply
->Timeout
!= Request
->Timeout
))) {
352 // The server can send ",,master" to client to change its master
353 // setting. But if it use the specific multicast channel, it can't
354 // change the setting.
356 if (((Reply
->Exist
& MTFTP4_MCAST_EXIST
) != 0) && (This
->McastIp
!= 0)) {
357 if ((Reply
->McastIp
!= 0) && (Reply
->McastIp
!= This
->McastIp
)) {
361 if ((Reply
->McastPort
!= 0) && (Reply
->McastPort
!= This
->McastPort
)) {
371 Configure a UDP IO port to receive the multicast.
373 @param McastIo The UDP IO to configure
374 @param Context The opaque parameter to the function which is the
377 @retval EFI_SUCCESS The UDP child is successfully configured.
378 @retval Others Failed to configure the UDP child.
382 Mtftp4RrqConfigMcastPort (
387 MTFTP4_PROTOCOL
*Instance
;
388 EFI_MTFTP4_CONFIG_DATA
*Config
;
389 EFI_UDP4_CONFIG_DATA UdpConfig
;
390 EFI_IPv4_ADDRESS Group
;
394 Instance
= (MTFTP4_PROTOCOL
*) Context
;
395 Config
= &Instance
->Config
;
397 UdpConfig
.AcceptBroadcast
= FALSE
;
398 UdpConfig
.AcceptPromiscuous
= FALSE
;
399 UdpConfig
.AcceptAnyPort
= FALSE
;
400 UdpConfig
.AllowDuplicatePort
= FALSE
;
401 UdpConfig
.TypeOfService
= 0;
402 UdpConfig
.TimeToLive
= 64;
403 UdpConfig
.DoNotFragment
= FALSE
;
404 UdpConfig
.ReceiveTimeout
= 0;
405 UdpConfig
.TransmitTimeout
= 0;
406 UdpConfig
.UseDefaultAddress
= Config
->UseDefaultSetting
;
407 UdpConfig
.StationAddress
= Config
->StationIp
;
408 UdpConfig
.SubnetMask
= Config
->SubnetMask
;
409 UdpConfig
.StationPort
= Instance
->McastPort
;
410 UdpConfig
.RemotePort
= 0;
412 Ip
= HTONL (Instance
->ServerIp
);
413 CopyMem (&UdpConfig
.RemoteAddress
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
415 Status
= McastIo
->Protocol
.Udp4
->Configure (McastIo
->Protocol
.Udp4
, &UdpConfig
);
417 if (EFI_ERROR (Status
)) {
421 if (!Config
->UseDefaultSetting
&&
422 !EFI_IP4_EQUAL (&mZeroIp4Addr
, &Config
->GatewayIp
)) {
424 // The station IP address is manually configured and the Gateway IP is not 0.
425 // Add the default route for this UDP instance.
427 Status
= McastIo
->Protocol
.Udp4
->Routes (
428 McastIo
->Protocol
.Udp4
,
435 if (EFI_ERROR (Status
)) {
436 McastIo
->Protocol
.Udp4
->Configure (McastIo
->Protocol
.Udp4
, NULL
);
442 // join the multicast group
444 Ip
= HTONL (Instance
->McastIp
);
445 CopyMem (&Group
, &Ip
, sizeof (EFI_IPv4_ADDRESS
));
447 return McastIo
->Protocol
.Udp4
->Groups (McastIo
->Protocol
.Udp4
, TRUE
, &Group
);
452 Function to process the OACK.
454 It will first validate the OACK packet, then update the various negotiated parameters.
456 @param Instance The download MTFTP session
457 @param Packet The packet received
458 @param Len The packet length
459 @param Multicast Whether this packet is received as a multicast
460 @param Completed Returns whether the download has completed. NOT
461 used by this function.
463 @retval EFI_DEVICE_ERROR Failed to create/start a multicast UDP child
464 @retval EFI_TFTP_ERROR Some error happened during the process
465 @retval EFI_SUCCESS The OACK is successfully processed.
469 Mtftp4RrqHandleOack (
470 IN OUT MTFTP4_PROTOCOL
*Instance
,
471 IN EFI_MTFTP4_PACKET
*Packet
,
473 IN BOOLEAN Multicast
,
474 OUT BOOLEAN
*Completed
484 // If already started the master download, don't change the
485 // setting. Master download always succeeds.
487 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
488 ASSERT (Expected
!= -1);
490 if (Instance
->Master
&& (Expected
!= 1)) {
495 // Parse and validate the options from server
497 ZeroMem (&Reply
, sizeof (MTFTP4_OPTION
));
499 Status
= Mtftp4ParseOptionOack (Packet
, Len
, &Reply
);
501 if (EFI_ERROR (Status
) ||
502 !Mtftp4RrqOackValid (Instance
, &Reply
, &Instance
->RequestOption
)) {
504 // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.
506 if (Status
!= EFI_OUT_OF_RESOURCES
) {
509 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION
,
510 (UINT8
*) "Mal-formated OACK packet"
514 return EFI_TFTP_ERROR
;
517 if ((Reply
.Exist
& MTFTP4_MCAST_EXIST
) != 0) {
520 // Save the multicast info. Always update the Master, only update the
521 // multicast IP address, block size, timeoute at the first time. If IP
522 // address is updated, create a UDP child to receive the multicast.
524 Instance
->Master
= Reply
.Master
;
526 if (Instance
->McastIp
== 0) {
527 if ((Reply
.McastIp
== 0) || (Reply
.McastPort
== 0)) {
530 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION
,
531 (UINT8
*) "Illegal multicast setting"
534 return EFI_TFTP_ERROR
;
538 // Create a UDP child then start receive the multicast from it.
540 Instance
->McastIp
= Reply
.McastIp
;
541 Instance
->McastPort
= Reply
.McastPort
;
542 Instance
->McastUdpPort
= UdpIoCreateIo (
543 Instance
->Service
->Controller
,
544 Instance
->Service
->Image
,
545 Mtftp4RrqConfigMcastPort
,
550 if (Instance
->McastUdpPort
== NULL
) {
551 return EFI_DEVICE_ERROR
;
554 Status
= UdpIoRecvDatagram (Instance
->McastUdpPort
, Mtftp4RrqInput
, Instance
, 0);
556 if (EFI_ERROR (Status
)) {
559 EFI_MTFTP4_ERRORCODE_ACCESS_VIOLATION
,
560 (UINT8
*) "Failed to create socket to receive multicast packet"
567 // Update the parameters used.
569 if (Reply
.BlkSize
!= 0) {
570 Instance
->BlkSize
= Reply
.BlkSize
;
573 if (Reply
.Timeout
!= 0) {
574 Instance
->Timeout
= Reply
.Timeout
;
579 Instance
->Master
= TRUE
;
581 if (Reply
.BlkSize
!= 0) {
582 Instance
->BlkSize
= Reply
.BlkSize
;
585 if (Reply
.Timeout
!= 0) {
586 Instance
->Timeout
= Reply
.Timeout
;
591 // Send an ACK to (Expected - 1) which is 0 for unicast download,
592 // or tell the server we want to receive the Expected block.
594 return Mtftp4RrqSendAck (Instance
, (UINT16
) (Expected
- 1));
599 The packet process callback for MTFTP download.
601 @param UdpPacket The packet received
602 @param EndPoint The local/remote access point of the packet
603 @param IoStatus The status of the receiving
604 @param Context Opaque parameter, which is the MTFTP session
609 IN NET_BUF
*UdpPacket
,
610 IN UDP_END_POINT
*EndPoint
,
611 IN EFI_STATUS IoStatus
,
615 MTFTP4_PROTOCOL
*Instance
;
616 EFI_MTFTP4_PACKET
*Packet
;
623 Instance
= (MTFTP4_PROTOCOL
*) Context
;
624 NET_CHECK_SIGNATURE (Instance
, MTFTP4_PROTOCOL_SIGNATURE
);
626 Status
= EFI_SUCCESS
;
631 if (EFI_ERROR (IoStatus
)) {
636 ASSERT (UdpPacket
!= NULL
);
639 // Find the port this packet is from to restart receive correctly.
641 Multicast
= (BOOLEAN
) (EndPoint
->LocalAddr
.Addr
[0] == Instance
->McastIp
);
643 if (UdpPacket
->TotalSize
< MTFTP4_OPCODE_LEN
) {
648 // Client send initial request to server's listening port. Server
649 // will select a UDP port to communicate with the client. The server
650 // is required to use the same port as RemotePort to multicast the
653 if (EndPoint
->RemotePort
!= Instance
->ConnectedPort
) {
654 if (Instance
->ConnectedPort
!= 0) {
657 Instance
->ConnectedPort
= EndPoint
->RemotePort
;
662 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
664 Len
= UdpPacket
->TotalSize
;
666 if (UdpPacket
->BlockOpNum
> 1) {
667 Packet
= AllocatePool (Len
);
669 if (Packet
== NULL
) {
670 Status
= EFI_OUT_OF_RESOURCES
;
674 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
677 Packet
= (EFI_MTFTP4_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
680 Opcode
= NTOHS (Packet
->OpCode
);
683 // Call the user's CheckPacket if provided. Abort the transmission
684 // if CheckPacket returns an EFI_ERROR code.
686 if ((Instance
->Token
->CheckPacket
!= NULL
) &&
687 ((Opcode
== EFI_MTFTP4_OPCODE_OACK
) || (Opcode
== EFI_MTFTP4_OPCODE_ERROR
))) {
689 Status
= Instance
->Token
->CheckPacket (
696 if (EFI_ERROR (Status
)) {
698 // Send an error message to the server to inform it
700 if (Opcode
!= EFI_MTFTP4_OPCODE_ERROR
) {
703 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
704 (UINT8
*) "User aborted the transfer"
708 Status
= EFI_ABORTED
;
714 case EFI_MTFTP4_OPCODE_DATA
:
715 if ((Len
> (UINT32
) (MTFTP4_DATA_HEAD_LEN
+ Instance
->BlkSize
)) ||
716 (Len
< (UINT32
) MTFTP4_DATA_HEAD_LEN
)) {
720 Status
= Mtftp4RrqHandleData (Instance
, Packet
, Len
, Multicast
, &Completed
);
723 case EFI_MTFTP4_OPCODE_OACK
:
724 if (Multicast
|| (Len
<= MTFTP4_OPCODE_LEN
)) {
728 Status
= Mtftp4RrqHandleOack (Instance
, Packet
, Len
, Multicast
, &Completed
);
731 case EFI_MTFTP4_OPCODE_ERROR
:
732 Status
= EFI_TFTP_ERROR
;
742 // Free the resources, then if !EFI_ERROR (Status), restart the
743 // receive, otherwise end the session.
745 if ((Packet
!= NULL
) && (UdpPacket
->BlockOpNum
> 1)) {
749 if (UdpPacket
!= NULL
) {
750 NetbufFree (UdpPacket
);
753 if (!EFI_ERROR (Status
) && !Completed
) {
755 Status
= UdpIoRecvDatagram (Instance
->McastUdpPort
, Mtftp4RrqInput
, Instance
, 0);
757 Status
= UdpIoRecvDatagram (Instance
->UnicastPort
, Mtftp4RrqInput
, Instance
, 0);
761 if (EFI_ERROR (Status
) || Completed
) {
762 Mtftp4CleanOperation (Instance
, Status
);