2 Mtftp6 Wrq process functions implementation.
4 Copyright (c) 2009 - 2014, 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
;
301 // Ignore the OACK if already started the upload
303 Expected
= Mtftp6GetNextBlockNum (&Instance
->BlkList
);
310 // Parse and validate the options from server
312 ZeroMem (&ExtInfo
, sizeof (MTFTP6_EXT_OPTION_INFO
));
314 Status
= Mtftp6ParseStart (Packet
, Len
, &Count
, &Options
);
316 if (EFI_ERROR (Status
)) {
319 ASSERT (Options
!= NULL
);
321 Status
= Mtftp6ParseExtensionOption (Options
, Count
, FALSE
, &ExtInfo
);
323 if (EFI_ERROR(Status
) || !Mtftp6WrqOackValid (&ExtInfo
, &Instance
->ExtInfo
)) {
325 // Don't send a MTFTP error packet when out of resource, it can
326 // only make it worse.
328 if (Status
!= EFI_OUT_OF_RESOURCES
) {
330 // Free the received packet before send new packet in ReceiveNotify,
331 // since the udpio might need to be reconfigured.
333 NetbufFree (*UdpPacket
);
336 // Send the Mtftp6 error message if invalid Oack packet received.
340 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION
,
341 (UINT8
*) "Mal-formated OACK packet"
345 return EFI_TFTP_ERROR
;
348 if (ExtInfo
.BlkSize
!= 0) {
349 Instance
->BlkSize
= ExtInfo
.BlkSize
;
352 if (ExtInfo
.Timeout
!= 0) {
353 Instance
->Timeout
= ExtInfo
.Timeout
;
357 // Build a bogus ACK0 packet then pass it to the Mtftp6WrqHandleAck,
358 // which will start the transmission of the first data block.
360 Dummy
.Ack
.OpCode
= HTONS (EFI_MTFTP6_OPCODE_ACK
);
361 Dummy
.Ack
.Block
[0] = 0;
363 return Mtftp6WrqHandleAck (
366 sizeof (EFI_MTFTP6_ACK_HEADER
),
374 The packet process callback for Mtftp6 upload.
376 @param[in] UdpPacket The pointer to the packet received.
377 @param[in] UdpEpt The pointer to the Udp6 access point.
378 @param[in] IoStatus The status from Udp6 instance.
379 @param[in] Context The pointer to the context.
385 IN NET_BUF
*UdpPacket
,
386 IN UDP_END_POINT
*UdpEpt
,
387 IN EFI_STATUS IoStatus
,
391 MTFTP6_INSTANCE
*Instance
;
392 EFI_MTFTP6_PACKET
*Packet
;
399 Instance
= (MTFTP6_INSTANCE
*) Context
;
401 NET_CHECK_SIGNATURE (Instance
, MTFTP6_INSTANCE_SIGNATURE
);
405 Status
= EFI_SUCCESS
;
409 // Return error status if Udp6 instance failed to receive.
411 if (EFI_ERROR (IoStatus
)) {
416 ASSERT (UdpPacket
!= NULL
);
418 if (UdpPacket
->TotalSize
< MTFTP6_OPCODE_LEN
) {
423 // Client send initial request to server's listening port. Server
424 // will select a UDP port to communicate with the client.
426 if (UdpEpt
->RemotePort
!= Instance
->ServerDataPort
) {
427 if (Instance
->ServerDataPort
!= 0) {
430 Instance
->ServerDataPort
= UdpEpt
->RemotePort
;
435 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
437 Len
= UdpPacket
->TotalSize
;
438 TotalNum
= UdpPacket
->BlockOpNum
;
441 Packet
= AllocateZeroPool (Len
);
443 if (Packet
== NULL
) {
444 Status
= EFI_OUT_OF_RESOURCES
;
448 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
451 Packet
= (EFI_MTFTP6_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
452 ASSERT (Packet
!= NULL
);
455 Opcode
= NTOHS (Packet
->OpCode
);
458 // Callback to the user's CheckPacket if provided. Abort the transmission
459 // if CheckPacket returns an EFI_ERROR code.
461 if (Instance
->Token
->CheckPacket
!= NULL
&&
462 (Opcode
== EFI_MTFTP6_OPCODE_OACK
|| Opcode
== EFI_MTFTP6_OPCODE_ERROR
)
465 Status
= Instance
->Token
->CheckPacket (
472 if (EFI_ERROR (Status
)) {
474 // Send an error message to the server to inform it
476 if (Opcode
!= EFI_MTFTP6_OPCODE_ERROR
) {
478 // Free the received packet before send new packet in ReceiveNotify,
479 // since the udpio might need to be reconfigured.
481 NetbufFree (UdpPacket
);
484 // Send the Mtftp6 error message if user aborted the current session.
488 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED
,
489 (UINT8
*) "User aborted the transfer"
493 Status
= EFI_ABORTED
;
499 // Switch the process routines by the operation code.
502 case EFI_MTFTP6_OPCODE_ACK
:
503 if (Len
!= MTFTP6_OPCODE_LEN
+ MTFTP6_BLKNO_LEN
) {
507 // Handle the Ack packet of Wrq.
509 Status
= Mtftp6WrqHandleAck (Instance
, Packet
, Len
, &UdpPacket
, &IsCompleted
);
512 case EFI_MTFTP6_OPCODE_OACK
:
513 if (Len
<= MTFTP6_OPCODE_LEN
) {
517 // Handle the Oack packet of Wrq.
519 Status
= Mtftp6WrqHandleOack (Instance
, Packet
, Len
, &UdpPacket
, &IsCompleted
);
524 // Drop and return eror if received error message.
526 Status
= EFI_TFTP_ERROR
;
532 // Free the resources, then if !EFI_ERROR (Status) and not completed,
533 // restart the receive, otherwise end the session.
535 if (Packet
!= NULL
&& TotalNum
> 1) {
539 if (UdpPacket
!= NULL
) {
540 NetbufFree (UdpPacket
);
543 if (!EFI_ERROR (Status
) && !IsCompleted
) {
544 Status
= UdpIoRecvDatagram (
552 // Clean up the current session if failed to continue.
554 if (EFI_ERROR (Status
) || IsCompleted
) {
555 Mtftp6OperationClean (Instance
, Status
);
561 Start the Mtftp6 instance to upload. It will first init some states,
562 then send the WRQ request packet, and start to receive the packet.
564 @param[in] Instance The pointer to the Mtftp6 instance.
565 @param[in] Operation The operation code of the current packet.
567 @retval EFI_SUCCESS The Mtftp6 was started to upload.
568 @retval Others Failed to start to upload.
573 IN MTFTP6_INSTANCE
*Instance
,
580 // The valid block number range are [0, 0xffff]. For example:
581 // the client sends an WRQ request to the server, the server
582 // ACK with an ACK0 to let client start transfer the first
585 Status
= Mtftp6InitBlockRange (&Instance
->BlkList
, 0, 0xffff);
587 if (EFI_ERROR (Status
)) {
591 Status
= Mtftp6SendRequest (Instance
, Operation
);
593 if (EFI_ERROR (Status
)) {
597 return UdpIoRecvDatagram (