2 Mtftp6 Wrq process functions implementation.
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "Mtftp6Impl.h"
15 Build and send a Mtftp6 data packet for upload.
17 @param[in] Instance The pointer to the Mtftp6 instance.
18 @param[in] BlockNum The block num to be sent.
20 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet.
21 @retval EFI_SUCCESS The data packet was sent.
22 @retval EFI_ABORTED The user aborted this process.
27 IN MTFTP6_INSTANCE
*Instance
,
31 EFI_MTFTP6_PACKET
*Packet
;
32 EFI_MTFTP6_TOKEN
*Token
;
40 // Allocate net buffer to create data packet.
42 UdpPacket
= NetbufAlloc (Instance
->BlkSize
+ MTFTP6_DATA_HEAD_LEN
);
44 if (UdpPacket
== NULL
) {
45 return EFI_OUT_OF_RESOURCES
;
48 Packet
= (EFI_MTFTP6_PACKET
*) NetbufAllocSpace (
53 ASSERT (Packet
!= NULL
);
55 Packet
->Data
.OpCode
= HTONS (EFI_MTFTP6_OPCODE_DATA
);
56 Packet
->Data
.Block
= HTONS (BlockNum
);
59 // Read the block from either the buffer or PacketNeeded callback
61 Token
= Instance
->Token
;
62 DataLen
= Instance
->BlkSize
;
64 if (Token
->Buffer
!= NULL
) {
65 Start
= MultU64x32 (BlockNum
- 1, Instance
->BlkSize
);
67 if (Token
->BufferSize
< Start
+ Instance
->BlkSize
) {
68 DataLen
= (UINT16
) (Token
->BufferSize
- Start
);
69 Instance
->LastBlk
= BlockNum
;
70 Mtftp6SetLastBlockNum (&Instance
->BlkList
, BlockNum
);
74 NetbufAllocSpace (UdpPacket
, DataLen
, FALSE
);
75 CopyMem (Packet
->Data
.Data
, (UINT8
*) Token
->Buffer
+ Start
, DataLen
);
80 // Get data from PacketNeeded
83 Status
= Token
->PacketNeeded (&Instance
->Mtftp6
, Token
, &DataLen
, (VOID
*) &DataBuf
);
85 if (EFI_ERROR (Status
) || (DataLen
> Instance
->BlkSize
)) {
86 if (DataBuf
!= NULL
) {
87 gBS
->FreePool (DataBuf
);
90 // The received packet has already been freed.
94 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
95 (UINT8
*) "User aborted the transfer"
101 if (DataLen
< Instance
->BlkSize
) {
102 Instance
->LastBlk
= BlockNum
;
103 Mtftp6SetLastBlockNum (&Instance
->BlkList
, BlockNum
);
107 NetbufAllocSpace (UdpPacket
, DataLen
, FALSE
);
108 CopyMem (Packet
->Data
.Data
, DataBuf
, DataLen
);
109 gBS
->FreePool (DataBuf
);
114 // Reset current retry count of the instance.
116 Instance
->CurRetry
= 0;
118 return Mtftp6TransmitPacket (Instance
, UdpPacket
);
123 Function to handle received ACK packet. If the ACK number matches the
124 expected block number, with more data pending, send the next
125 block. Otherwise, tell the caller that we are done.
127 @param[in] Instance The pointer to the Mtftp6 instance.
128 @param[in] Packet The pointer to the received packet.
129 @param[in] Len The length of the packet.
130 @param[out] UdpPacket The net buf of received packet.
131 @param[out] IsCompleted If TRUE, the upload has been completed.
132 Otherwise, the upload has not been completed.
134 @retval EFI_SUCCESS The ACK packet successfully processed.
135 @retval EFI_TFTP_ERROR The block number loops back.
136 @retval Others Failed to transmit the next data packet.
141 IN MTFTP6_INSTANCE
*Instance
,
142 IN EFI_MTFTP6_PACKET
*Packet
,
144 OUT NET_BUF
**UdpPacket
,
145 OUT BOOLEAN
*IsCompleted
152 *IsCompleted
= FALSE
;
153 AckNum
= NTOHS (Packet
->Ack
.Block
[0]);
154 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
156 ASSERT (Expected
>= 0);
159 // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp6WrqInput
162 if (Expected
!= AckNum
) {
167 // Remove the acked block number, if this is the last block number,
168 // tell the Mtftp6WrqInput to finish the transfer. This is the last
169 // block number if the block range are empty.
171 Mtftp6RemoveBlockNum (&Instance
->BlkList
, AckNum
, *IsCompleted
, &BlockCounter
);
173 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
177 // The block range is empty. It may either because the the last
178 // block has been ACKed, or the sequence number just looped back,
179 // that is, there is more than 0xffff blocks.
181 if (Instance
->LastBlk
== AckNum
) {
182 ASSERT (Instance
->LastBlk
>= 1);
188 // Free the received packet before send new packet in ReceiveNotify,
189 // since the udpio might need to be reconfigured.
191 NetbufFree (*UdpPacket
);
194 // Send the Mtftp6 error message if block number rolls back.
198 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
199 (UINT8
*) "Block number rolls back, not supported, try blksize option"
202 return EFI_TFTP_ERROR
;
207 // Free the receive buffer before send new packet since it might need
208 // reconfigure udpio.
210 NetbufFree (*UdpPacket
);
213 return Mtftp6WrqSendBlock (Instance
, (UINT16
) Expected
);
218 Check whether the received OACK is valid. The OACK is valid
220 1. It only include options requested by us.
221 2. It can only include a smaller block size.
222 3. It can't change the proposed time out value.
223 4. Other requirements of the individal MTFTP6 options as required.
225 @param[in] ReplyInfo The pointer to options information in reply packet.
226 @param[in] RequestInfo The pointer to requested options information.
228 @retval TRUE If the option in OACK is valid.
229 @retval FALSE If the option is invalid.
234 IN MTFTP6_EXT_OPTION_INFO
*ReplyInfo
,
235 IN MTFTP6_EXT_OPTION_INFO
*RequestInfo
239 // It is invalid for server to return options we don't request
241 if ((ReplyInfo
->BitMap
& ~RequestInfo
->BitMap
) != 0) {
246 // Server can only specify a smaller block size to be used and
247 // return the timeout matches that requested.
249 if ((((ReplyInfo
->BitMap
& MTFTP6_OPT_BLKSIZE_BIT
) != 0) && (ReplyInfo
->BlkSize
> RequestInfo
->BlkSize
)) ||
250 (((ReplyInfo
->BitMap
& MTFTP6_OPT_TIMEOUT_BIT
) != 0) && (ReplyInfo
->Timeout
!= RequestInfo
->Timeout
))
261 Process the OACK packet for Wrq.
263 @param[in] Instance The pointer to the Mtftp6 instance.
264 @param[in] Packet The pointer to the received packet.
265 @param[in] Len The length of the packet.
266 @param[out] UdpPacket The net buf of received packet.
267 @param[out] IsCompleted If TRUE, the upload has been completed.
268 Otherwise, the upload has not been completed.
270 @retval EFI_SUCCESS The OACK packet successfully processed.
271 @retval EFI_TFTP_ERROR An TFTP communication error happened.
272 @retval Others Failed to process the OACK packet.
276 Mtftp6WrqHandleOack (
277 IN MTFTP6_INSTANCE
*Instance
,
278 IN EFI_MTFTP6_PACKET
*Packet
,
280 OUT NET_BUF
**UdpPacket
,
281 OUT BOOLEAN
*IsCompleted
284 EFI_MTFTP6_OPTION
*Options
;
286 MTFTP6_EXT_OPTION_INFO ExtInfo
;
287 EFI_MTFTP6_PACKET Dummy
;
291 *IsCompleted
= FALSE
;
295 // Ignore the OACK if already started the upload
297 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
304 // Parse and validate the options from server
306 ZeroMem (&ExtInfo
, sizeof (MTFTP6_EXT_OPTION_INFO
));
308 Status
= Mtftp6ParseStart (Packet
, Len
, &Count
, &Options
);
310 if (EFI_ERROR (Status
)) {
313 ASSERT (Options
!= NULL
);
315 Status
= Mtftp6ParseExtensionOption (Options
, Count
, FALSE
, Instance
->Operation
, &ExtInfo
);
317 if (EFI_ERROR(Status
) || !Mtftp6WrqOackValid (&ExtInfo
, &Instance
->ExtInfo
)) {
319 // Don't send a MTFTP error packet when out of resource, it can
320 // only make it worse.
322 if (Status
!= EFI_OUT_OF_RESOURCES
) {
324 // Free the received packet before send new packet in ReceiveNotify,
325 // since the udpio might need to be reconfigured.
327 NetbufFree (*UdpPacket
);
330 // Send the Mtftp6 error message if invalid Oack packet received.
334 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
335 (UINT8
*) "Malformatted OACK packet"
339 return EFI_TFTP_ERROR
;
342 if (ExtInfo
.BlkSize
!= 0) {
343 Instance
->BlkSize
= ExtInfo
.BlkSize
;
346 if (ExtInfo
.Timeout
!= 0) {
347 Instance
->Timeout
= ExtInfo
.Timeout
;
351 // Build a bogus ACK0 packet then pass it to the Mtftp6WrqHandleAck,
352 // which will start the transmission of the first data block.
354 Dummy
.Ack
.OpCode
= HTONS (EFI_MTFTP6_OPCODE_ACK
);
355 Dummy
.Ack
.Block
[0] = 0;
357 return Mtftp6WrqHandleAck (
360 sizeof (EFI_MTFTP6_ACK_HEADER
),
368 The packet process callback for Mtftp6 upload.
370 @param[in] UdpPacket The pointer to the packet received.
371 @param[in] UdpEpt The pointer to the Udp6 access point.
372 @param[in] IoStatus The status from Udp6 instance.
373 @param[in] Context The pointer to the context.
379 IN NET_BUF
*UdpPacket
,
380 IN UDP_END_POINT
*UdpEpt
,
381 IN EFI_STATUS IoStatus
,
385 MTFTP6_INSTANCE
*Instance
;
386 EFI_MTFTP6_PACKET
*Packet
;
393 Instance
= (MTFTP6_INSTANCE
*) Context
;
395 NET_CHECK_SIGNATURE (Instance
, MTFTP6_INSTANCE_SIGNATURE
);
399 Status
= EFI_SUCCESS
;
403 // Return error status if Udp6 instance failed to receive.
405 if (EFI_ERROR (IoStatus
)) {
410 ASSERT (UdpPacket
!= NULL
);
412 if (UdpPacket
->TotalSize
< MTFTP6_OPCODE_LEN
) {
417 // Client send initial request to server's listening port. Server
418 // will select a UDP port to communicate with the client.
420 if (UdpEpt
->RemotePort
!= Instance
->ServerDataPort
) {
421 if (Instance
->ServerDataPort
!= 0) {
424 Instance
->ServerDataPort
= UdpEpt
->RemotePort
;
429 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
431 Len
= UdpPacket
->TotalSize
;
432 TotalNum
= UdpPacket
->BlockOpNum
;
435 Packet
= AllocateZeroPool (Len
);
437 if (Packet
== NULL
) {
438 Status
= EFI_OUT_OF_RESOURCES
;
442 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
445 Packet
= (EFI_MTFTP6_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
446 ASSERT (Packet
!= NULL
);
449 Opcode
= NTOHS (Packet
->OpCode
);
452 // Callback to the user's CheckPacket if provided. Abort the transmission
453 // if CheckPacket returns an EFI_ERROR code.
455 if (Instance
->Token
->CheckPacket
!= NULL
&&
456 (Opcode
== EFI_MTFTP6_OPCODE_OACK
|| Opcode
== EFI_MTFTP6_OPCODE_ERROR
)
459 Status
= Instance
->Token
->CheckPacket (
466 if (EFI_ERROR (Status
)) {
468 // Send an error message to the server to inform it
470 if (Opcode
!= EFI_MTFTP6_OPCODE_ERROR
) {
472 // Free the received packet before send new packet in ReceiveNotify,
473 // since the udpio might need to be reconfigured.
475 NetbufFree (UdpPacket
);
478 // Send the Mtftp6 error message if user aborted the current session.
482 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
483 (UINT8
*) "User aborted the transfer"
487 Status
= EFI_ABORTED
;
493 // Switch the process routines by the operation code.
496 case EFI_MTFTP6_OPCODE_ACK
:
497 if (Len
!= MTFTP6_OPCODE_LEN
+ MTFTP6_BLKNO_LEN
) {
501 // Handle the Ack packet of Wrq.
503 Status
= Mtftp6WrqHandleAck (Instance
, Packet
, Len
, &UdpPacket
, &IsCompleted
);
506 case EFI_MTFTP6_OPCODE_OACK
:
507 if (Len
<= MTFTP6_OPCODE_LEN
) {
511 // Handle the Oack packet of Wrq.
513 Status
= Mtftp6WrqHandleOack (Instance
, Packet
, Len
, &UdpPacket
, &IsCompleted
);
518 // Drop and return eror if received error message.
520 Status
= EFI_TFTP_ERROR
;
526 // Free the resources, then if !EFI_ERROR (Status) and not completed,
527 // restart the receive, otherwise end the session.
529 if (Packet
!= NULL
&& TotalNum
> 1) {
533 if (UdpPacket
!= NULL
) {
534 NetbufFree (UdpPacket
);
537 if (!EFI_ERROR (Status
) && !IsCompleted
) {
538 Status
= UdpIoRecvDatagram (
546 // Clean up the current session if failed to continue.
548 if (EFI_ERROR (Status
) || IsCompleted
) {
549 Mtftp6OperationClean (Instance
, Status
);
555 Start the Mtftp6 instance to upload. It will first init some states,
556 then send the WRQ request packet, and start to receive the packet.
558 @param[in] Instance The pointer to the Mtftp6 instance.
559 @param[in] Operation The operation code of the current packet.
561 @retval EFI_SUCCESS The Mtftp6 was started to upload.
562 @retval Others Failed to start to upload.
567 IN MTFTP6_INSTANCE
*Instance
,
574 // The valid block number range are [0, 0xffff]. For example:
575 // the client sends an WRQ request to the server, the server
576 // ACK with an ACK0 to let client start transfer the first
579 Status
= Mtftp6InitBlockRange (&Instance
->BlkList
, 0, 0xffff);
581 if (EFI_ERROR (Status
)) {
585 Status
= Mtftp6SendRequest (Instance
, Operation
);
587 if (EFI_ERROR (Status
)) {
591 return UdpIoRecvDatagram (