2 Mtftp6 Wrq process functions implementation.
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "Mtftp6Impl.h"
21 Build and send a Mtftp6 data packet for upload.
23 @param[in] Instance The pointer to the Mtftp6 instance.
24 @param[in] BlockNum The block num to be sent.
26 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet.
27 @retval EFI_SUCCESS The data packet was sent.
28 @retval EFI_ABORTED The user aborted this process.
33 IN MTFTP6_INSTANCE
*Instance
,
37 EFI_MTFTP6_PACKET
*Packet
;
38 EFI_MTFTP6_TOKEN
*Token
;
46 // Allocate net buffer to create data packet.
48 UdpPacket
= NetbufAlloc (Instance
->BlkSize
+ MTFTP6_DATA_HEAD_LEN
);
50 if (UdpPacket
== NULL
) {
51 return EFI_OUT_OF_RESOURCES
;
54 Packet
= (EFI_MTFTP6_PACKET
*) NetbufAllocSpace (
59 ASSERT (Packet
!= NULL
);
61 Packet
->Data
.OpCode
= HTONS (EFI_MTFTP6_OPCODE_DATA
);
62 Packet
->Data
.Block
= HTONS (BlockNum
);
65 // Read the block from either the buffer or PacketNeeded callback
67 Token
= Instance
->Token
;
68 DataLen
= Instance
->BlkSize
;
70 if (Token
->Buffer
!= NULL
) {
71 Start
= MultU64x32 (BlockNum
- 1, Instance
->BlkSize
);
73 if (Token
->BufferSize
< Start
+ Instance
->BlkSize
) {
74 DataLen
= (UINT16
) (Token
->BufferSize
- Start
);
75 Instance
->LastBlk
= BlockNum
;
76 Mtftp6SetLastBlockNum (&Instance
->BlkList
, BlockNum
);
80 NetbufAllocSpace (UdpPacket
, DataLen
, FALSE
);
81 CopyMem (Packet
->Data
.Data
, (UINT8
*) Token
->Buffer
+ Start
, DataLen
);
86 // Get data from PacketNeeded
89 Status
= Token
->PacketNeeded (&Instance
->Mtftp6
, Token
, &DataLen
, (VOID
*) &DataBuf
);
91 if (EFI_ERROR (Status
) || (DataLen
> Instance
->BlkSize
)) {
92 if (DataBuf
!= NULL
) {
93 gBS
->FreePool (DataBuf
);
96 // The received packet has already been freed.
100 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
101 (UINT8
*) "User aborted the transfer"
107 if (DataLen
< Instance
->BlkSize
) {
108 Instance
->LastBlk
= BlockNum
;
109 Mtftp6SetLastBlockNum (&Instance
->BlkList
, BlockNum
);
113 NetbufAllocSpace (UdpPacket
, DataLen
, FALSE
);
114 CopyMem (Packet
->Data
.Data
, DataBuf
, DataLen
);
115 gBS
->FreePool (DataBuf
);
120 // Reset current retry count of the instance.
122 Instance
->CurRetry
= 0;
124 return Mtftp6TransmitPacket (Instance
, UdpPacket
);
129 Function to handle received ACK packet. If the ACK number matches the
130 expected block number, with more data pending, send the next
131 block. Otherwise, tell the caller that we are done.
133 @param[in] Instance The pointer to the Mtftp6 instance.
134 @param[in] Packet The pointer to the received packet.
135 @param[in] Len The length of the packet.
136 @param[out] UdpPacket The net buf of received packet.
137 @param[out] IsCompleted If TRUE, the upload has been completed.
138 Otherwise, the upload has not been completed.
140 @retval EFI_SUCCESS The ACK packet successfully processed.
141 @retval EFI_TFTP_ERROR The block number loops back.
142 @retval Others Failed to transmit the next data packet.
147 IN MTFTP6_INSTANCE
*Instance
,
148 IN EFI_MTFTP6_PACKET
*Packet
,
150 OUT NET_BUF
**UdpPacket
,
151 OUT BOOLEAN
*IsCompleted
158 *IsCompleted
= FALSE
;
159 AckNum
= NTOHS (Packet
->Ack
.Block
[0]);
160 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
162 ASSERT (Expected
>= 0);
165 // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp6WrqInput
168 if (Expected
!= AckNum
) {
173 // Remove the acked block number, if this is the last block number,
174 // tell the Mtftp6WrqInput to finish the transfer. This is the last
175 // block number if the block range are empty..
177 Mtftp6RemoveBlockNum (&Instance
->BlkList
, AckNum
, *IsCompleted
, &TotalBlock
);
179 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
183 // The block range is empty. It may either because the the last
184 // block has been ACKed, or the sequence number just looped back,
185 // that is, there is more than 0xffff blocks.
187 if (Instance
->LastBlk
== AckNum
) {
188 ASSERT (Instance
->LastBlk
>= 1);
194 // Free the received packet before send new packet in ReceiveNotify,
195 // since the udpio might need to be reconfigured.
197 NetbufFree (*UdpPacket
);
200 // Send the Mtftp6 error message if block number rolls back.
204 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
205 (UINT8
*) "Block number rolls back, not supported, try blksize option"
208 return EFI_TFTP_ERROR
;
213 // Free the receive buffer before send new packet since it might need
214 // reconfigure udpio.
216 NetbufFree (*UdpPacket
);
219 return Mtftp6WrqSendBlock (Instance
, (UINT16
) Expected
);
224 Check whether the received OACK is valid. The OACK is valid
226 1. It only include options requested by us.
227 2. It can only include a smaller block size.
228 3. It can't change the proposed time out value.
229 4. Other requirements of the individal MTFTP6 options as required.
231 @param[in] ReplyInfo The pointer to options information in reply packet.
232 @param[in] RequestInfo The pointer to requested options information.
234 @retval TRUE If the option in OACK is valid.
235 @retval FALSE If the option is invalid.
240 IN MTFTP6_EXT_OPTION_INFO
*ReplyInfo
,
241 IN MTFTP6_EXT_OPTION_INFO
*RequestInfo
245 // It is invalid for server to return options we don't request
247 if ((ReplyInfo
->BitMap
& ~RequestInfo
->BitMap
) != 0) {
252 // Server can only specify a smaller block size to be used and
253 // return the timeout matches that requested.
255 if ((((ReplyInfo
->BitMap
& MTFTP6_OPT_BLKSIZE_BIT
) != 0) && (ReplyInfo
->BlkSize
> RequestInfo
->BlkSize
)) ||
256 (((ReplyInfo
->BitMap
& MTFTP6_OPT_TIMEOUT_BIT
) != 0) && (ReplyInfo
->Timeout
!= RequestInfo
->Timeout
))
267 Process the OACK packet for Wrq.
269 @param[in] Instance The pointer to the Mtftp6 instance.
270 @param[in] Packet The pointer to the received packet.
271 @param[in] Len The length of the packet.
272 @param[out] UdpPacket The net buf of received packet.
273 @param[out] IsCompleted If TRUE, the upload has been completed.
274 Otherwise, the upload has not been completed.
276 @retval EFI_SUCCESS The OACK packet successfully processed.
277 @retval EFI_TFTP_ERROR An TFTP communication error happened.
278 @retval Others Failed to process the OACK packet.
282 Mtftp6WrqHandleOack (
283 IN MTFTP6_INSTANCE
*Instance
,
284 IN EFI_MTFTP6_PACKET
*Packet
,
286 OUT NET_BUF
**UdpPacket
,
287 OUT BOOLEAN
*IsCompleted
290 EFI_MTFTP6_OPTION
*Options
;
292 MTFTP6_EXT_OPTION_INFO ExtInfo
;
293 EFI_MTFTP6_PACKET Dummy
;
297 *IsCompleted
= FALSE
;
300 // Ignore the OACK if already started the upload
302 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
309 // Parse and validate the options from server
311 ZeroMem (&ExtInfo
, sizeof (MTFTP6_EXT_OPTION_INFO
));
313 Status
= Mtftp6ParseStart (Packet
, Len
, &Count
, &Options
);
315 if (EFI_ERROR (Status
)) {
318 ASSERT (Options
!= NULL
);
320 Status
= Mtftp6ParseExtensionOption (Options
, Count
, FALSE
, &ExtInfo
);
322 if (EFI_ERROR(Status
) || !Mtftp6WrqOackValid (&ExtInfo
, &Instance
->ExtInfo
)) {
324 // Don't send a MTFTP error packet when out of resource, it can
325 // only make it worse.
327 if (Status
!= EFI_OUT_OF_RESOURCES
) {
329 // Free the received packet before send new packet in ReceiveNotify,
330 // since the udpio might need to be reconfigured.
332 NetbufFree (*UdpPacket
);
335 // Send the Mtftp6 error message if invalid Oack packet received.
339 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
340 (UINT8
*) "Mal-formated OACK packet"
344 return EFI_TFTP_ERROR
;
347 if (ExtInfo
.BlkSize
!= 0) {
348 Instance
->BlkSize
= ExtInfo
.BlkSize
;
351 if (ExtInfo
.Timeout
!= 0) {
352 Instance
->Timeout
= ExtInfo
.Timeout
;
356 // Build a bogus ACK0 packet then pass it to the Mtftp6WrqHandleAck,
357 // which will start the transmission of the first data block.
359 Dummy
.Ack
.OpCode
= HTONS (EFI_MTFTP6_OPCODE_ACK
);
360 Dummy
.Ack
.Block
[0] = 0;
362 return Mtftp6WrqHandleAck (
365 sizeof (EFI_MTFTP6_ACK_HEADER
),
373 The packet process callback for Mtftp6 upload.
375 @param[in] UdpPacket The pointer to the packet received.
376 @param[in] UdpEpt The pointer to the Udp6 access point.
377 @param[in] IoStatus The status from Udp6 instance.
378 @param[in] Context The pointer to the context.
384 IN NET_BUF
*UdpPacket
,
385 IN UDP_END_POINT
*UdpEpt
,
386 IN EFI_STATUS IoStatus
,
390 MTFTP6_INSTANCE
*Instance
;
391 EFI_MTFTP6_PACKET
*Packet
;
398 Instance
= (MTFTP6_INSTANCE
*) Context
;
400 NET_CHECK_SIGNATURE (Instance
, MTFTP6_INSTANCE_SIGNATURE
);
404 Status
= EFI_SUCCESS
;
408 // Return error status if Udp6 instance failed to receive.
410 if (EFI_ERROR (IoStatus
)) {
415 ASSERT (UdpPacket
!= NULL
);
417 if (UdpPacket
->TotalSize
< MTFTP6_OPCODE_LEN
) {
422 // Client send initial request to server's listening port. Server
423 // will select a UDP port to communicate with the client.
425 if (UdpEpt
->RemotePort
!= Instance
->ServerDataPort
) {
426 if (Instance
->ServerDataPort
!= 0) {
429 Instance
->ServerDataPort
= UdpEpt
->RemotePort
;
434 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
436 Len
= UdpPacket
->TotalSize
;
437 TotalNum
= UdpPacket
->BlockOpNum
;
440 Packet
= AllocateZeroPool (Len
);
442 if (Packet
== NULL
) {
443 Status
= EFI_OUT_OF_RESOURCES
;
447 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
450 Packet
= (EFI_MTFTP6_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
451 ASSERT (Packet
!= NULL
);
454 Opcode
= NTOHS (Packet
->OpCode
);
457 // Callback to the user's CheckPacket if provided. Abort the transmission
458 // if CheckPacket returns an EFI_ERROR code.
460 if (Instance
->Token
->CheckPacket
!= NULL
&&
461 (Opcode
== EFI_MTFTP6_OPCODE_OACK
|| Opcode
== EFI_MTFTP6_OPCODE_ERROR
)
464 Status
= Instance
->Token
->CheckPacket (
471 if (EFI_ERROR (Status
)) {
473 // Send an error message to the server to inform it
475 if (Opcode
!= EFI_MTFTP6_OPCODE_ERROR
) {
477 // Free the received packet before send new packet in ReceiveNotify,
478 // since the udpio might need to be reconfigured.
480 NetbufFree (UdpPacket
);
483 // Send the Mtftp6 error message if user aborted the current session.
487 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
488 (UINT8
*) "User aborted the transfer"
492 Status
= EFI_ABORTED
;
498 // Switch the process routines by the operation code.
501 case EFI_MTFTP6_OPCODE_ACK
:
502 if (Len
!= MTFTP6_OPCODE_LEN
+ MTFTP6_BLKNO_LEN
) {
506 // Handle the Ack packet of Wrq.
508 Status
= Mtftp6WrqHandleAck (Instance
, Packet
, Len
, &UdpPacket
, &IsCompleted
);
511 case EFI_MTFTP6_OPCODE_OACK
:
512 if (Len
<= MTFTP6_OPCODE_LEN
) {
516 // Handle the Oack packet of Wrq.
518 Status
= Mtftp6WrqHandleOack (Instance
, Packet
, Len
, &UdpPacket
, &IsCompleted
);
523 // Drop and return eror if received error message.
525 Status
= EFI_TFTP_ERROR
;
531 // Free the resources, then if !EFI_ERROR (Status) and not completed,
532 // restart the receive, otherwise end the session.
534 if (Packet
!= NULL
&& TotalNum
> 1) {
538 if (UdpPacket
!= NULL
) {
539 NetbufFree (UdpPacket
);
542 if (!EFI_ERROR (Status
) && !IsCompleted
) {
543 Status
= UdpIoRecvDatagram (
551 // Clean up the current session if failed to continue.
553 if (EFI_ERROR (Status
) || IsCompleted
) {
554 Mtftp6OperationClean (Instance
, Status
);
560 Start the Mtftp6 instance to upload. It will first init some states,
561 then send the WRQ request packet, and start to receive the packet.
563 @param[in] Instance The pointer to the Mtftp6 instance.
564 @param[in] Operation The operation code of the current packet.
566 @retval EFI_SUCCESS The Mtftp6 was started to upload.
567 @retval Others Failed to start to upload.
572 IN MTFTP6_INSTANCE
*Instance
,
579 // The valid block number range are [0, 0xffff]. For example:
580 // the client sends an WRQ request to the server, the server
581 // ACK with an ACK0 to let client start transfer the first
584 Status
= Mtftp6InitBlockRange (&Instance
->BlkList
, 0, 0xffff);
586 if (EFI_ERROR (Status
)) {
590 Status
= Mtftp6SendRequest (Instance
, Operation
);
592 if (EFI_ERROR (Status
)) {
596 return UdpIoRecvDatagram (