2 Routines to process Wrq (upload).
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "Mtftp4Impl.h"
14 Build then send a MTFTP data packet for the MTFTP upload session.
16 @param Instance The MTFTP upload session.
17 @param BlockNum The block number to send.
19 @retval EFI_OUT_OF_RESOURCES Failed to build the packet.
20 @retval EFI_ABORTED The consumer of this child directs to abort the
21 transmission by return an error through PacketNeeded.
22 @retval EFI_SUCCESS The data is sent.
27 IN OUT MTFTP4_PROTOCOL
*Instance
,
31 EFI_MTFTP4_PACKET
*Packet
;
32 EFI_MTFTP4_TOKEN
*Token
;
40 // Allocate a buffer to hold the user data
42 UdpPacket
= NetbufAlloc (Instance
->BlkSize
+ MTFTP4_DATA_HEAD_LEN
);
44 if (UdpPacket
== NULL
) {
45 return EFI_OUT_OF_RESOURCES
;
48 Packet
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (UdpPacket
, MTFTP4_DATA_HEAD_LEN
, FALSE
);
49 ASSERT (Packet
!= NULL
);
51 Packet
->Data
.OpCode
= HTONS (EFI_MTFTP4_OPCODE_DATA
);
52 Packet
->Data
.Block
= HTONS (BlockNum
);
55 // Read the block from either the buffer or PacketNeeded callback
57 Token
= Instance
->Token
;
58 DataLen
= Instance
->BlkSize
;
60 if (Token
->Buffer
!= NULL
) {
61 Start
= MultU64x32 (BlockNum
- 1, Instance
->BlkSize
);
63 if (Token
->BufferSize
< Start
+ Instance
->BlkSize
) {
64 DataLen
= (UINT16
) (Token
->BufferSize
- Start
);
65 Instance
->LastBlock
= BlockNum
;
66 Mtftp4SetLastBlockNum (&Instance
->Blocks
, BlockNum
);
70 NetbufAllocSpace (UdpPacket
, DataLen
, FALSE
);
71 CopyMem (Packet
->Data
.Data
, (UINT8
*) Token
->Buffer
+ Start
, DataLen
);
76 // Get data from PacketNeeded
79 Status
= Token
->PacketNeeded (
86 if (EFI_ERROR (Status
) || (DataLen
> Instance
->BlkSize
)) {
87 if (DataBuf
!= NULL
) {
91 if (UdpPacket
!= NULL
) {
92 NetbufFree (UdpPacket
);
97 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
98 (UINT8
*) "User aborted the transfer"
104 if (DataLen
< Instance
->BlkSize
) {
105 Instance
->LastBlock
= BlockNum
;
106 Mtftp4SetLastBlockNum (&Instance
->Blocks
, BlockNum
);
110 NetbufAllocSpace (UdpPacket
, DataLen
, FALSE
);
111 CopyMem (Packet
->Data
.Data
, DataBuf
, DataLen
);
116 return Mtftp4SendPacket (Instance
, UdpPacket
);
121 Function to handle received ACK packet.
123 If the ACK number matches the expected block number, and there are more
124 data pending, send the next block. Otherwise tell the caller that we are done.
126 @param Instance The MTFTP upload session
127 @param Packet The MTFTP packet received
128 @param Len The packet length
129 @param Completed Return whether the upload has finished.
131 @retval EFI_SUCCESS The ACK is successfully processed.
132 @retval EFI_TFTP_ERROR The block number loops back.
133 @retval Others Failed to transmit the next data packet.
138 IN MTFTP4_PROTOCOL
*Instance
,
139 IN EFI_MTFTP4_PACKET
*Packet
,
141 OUT BOOLEAN
*Completed
149 AckNum
= NTOHS (Packet
->Ack
.Block
[0]);
150 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
152 ASSERT (Expected
>= 0);
155 // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp4WrqInput
158 if (Expected
!= AckNum
) {
163 // Remove the acked block number, if this is the last block number,
164 // tell the Mtftp4WrqInput to finish the transfer. This is the last
165 // block number if the block range are empty.
167 Mtftp4RemoveBlockNum (&Instance
->Blocks
, AckNum
, *Completed
, &BlockCounter
);
169 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
174 // The block range is empty. It may either because the the last
175 // block has been ACKed, or the sequence number just looped back,
176 // that is, there is more than 0xffff blocks.
178 if (Instance
->LastBlock
== AckNum
) {
179 ASSERT (Instance
->LastBlock
>= 1);
186 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
187 (UINT8
*) "Block number rolls back, not supported, try blksize option"
190 return EFI_TFTP_ERROR
;
194 return Mtftp4WrqSendBlock (Instance
, (UINT16
) Expected
);
199 Check whether the received OACK is valid.
201 The OACK is valid only if:
202 1. It only include options requested by us
203 2. It can only include a smaller block size
204 3. It can't change the proposed time out value.
205 4. Other requirements of the individal MTFTP options as required.
207 @param Reply The options included in the OACK
208 @param Request The options we requested
210 @retval TRUE The options included in OACK is valid.
211 @retval FALSE The options included in OACK is invalid.
216 IN MTFTP4_OPTION
*Reply
,
217 IN MTFTP4_OPTION
*Request
221 // It is invalid for server to return options we don't request
223 if ((Reply
->Exist
& ~Request
->Exist
) != 0) {
228 // Server can only specify a smaller block size to be used and
229 // return the timeout matches that requested.
231 if ((((Reply
->Exist
& MTFTP4_BLKSIZE_EXIST
) != 0) && (Reply
->BlkSize
> Request
->BlkSize
)) ||
232 (((Reply
->Exist
& MTFTP4_TIMEOUT_EXIST
) != 0) && (Reply
->Timeout
!= Request
->Timeout
))) {
241 Function to handle the MTFTP OACK packet.
243 It parses the packet's options, and update the internal states of the session.
245 @param Instance The MTFTP session
246 @param Packet The received OACK packet
247 @param Len The length of the packet
248 @param Completed Whether the transmisson has completed. NOT used by
251 @retval EFI_SUCCESS The OACK process is OK
252 @retval EFI_TFTP_ERROR Some error occured, and the session reset.
256 Mtftp4WrqHandleOack (
257 IN OUT MTFTP4_PROTOCOL
*Instance
,
258 IN EFI_MTFTP4_PACKET
*Packet
,
260 OUT BOOLEAN
*Completed
264 EFI_MTFTP4_PACKET Bogus
;
271 // Ignore the OACK if already started the upload
273 Expected
= Mtftp4GetNextBlockNum (&Instance
->Blocks
);
280 // Parse and validate the options from server
282 ZeroMem (&Reply
, sizeof (MTFTP4_OPTION
));
283 Status
= Mtftp4ParseOptionOack (Packet
, Len
, Instance
->Operation
, &Reply
);
285 if (EFI_ERROR (Status
) || !Mtftp4WrqOackValid (&Reply
, &Instance
->RequestOption
)) {
287 // Don't send a MTFTP error packet when out of resource, it can
288 // only make it worse.
290 if (Status
!= EFI_OUT_OF_RESOURCES
) {
293 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION
,
294 (UINT8
*) "Mal-formated OACK packet"
298 return EFI_TFTP_ERROR
;
301 if (Reply
.BlkSize
!= 0) {
302 Instance
->BlkSize
= Reply
.BlkSize
;
305 if (Reply
.Timeout
!= 0) {
306 Instance
->Timeout
= Reply
.Timeout
;
310 // Build a bogus ACK0 packet then pass it to the Mtftp4WrqHandleAck,
311 // which will start the transmission of the first data block.
313 Bogus
.Ack
.OpCode
= HTONS (EFI_MTFTP4_OPCODE_ACK
);
314 Bogus
.Ack
.Block
[0] = 0;
316 Status
= Mtftp4WrqHandleAck (
319 sizeof (EFI_MTFTP4_ACK_HEADER
),
328 The input process routine for MTFTP upload.
330 @param UdpPacket The received MTFTP packet.
331 @param EndPoint The local/remote access point
332 @param IoStatus The result of the packet receiving
333 @param Context Opaque parameter for the callback, which is the
339 IN NET_BUF
*UdpPacket
,
340 IN UDP_END_POINT
*EndPoint
,
341 IN EFI_STATUS IoStatus
,
345 MTFTP4_PROTOCOL
*Instance
;
346 EFI_MTFTP4_PACKET
*Packet
;
352 Instance
= (MTFTP4_PROTOCOL
*) Context
;
353 NET_CHECK_SIGNATURE (Instance
, MTFTP4_PROTOCOL_SIGNATURE
);
357 Status
= EFI_SUCCESS
;
359 if (EFI_ERROR (IoStatus
)) {
364 ASSERT (UdpPacket
!= NULL
);
366 if (UdpPacket
->TotalSize
< MTFTP4_OPCODE_LEN
) {
371 // Client send initial request to server's listening port. Server
372 // will select a UDP port to communicate with the client.
374 if (EndPoint
->RemotePort
!= Instance
->ConnectedPort
) {
375 if (Instance
->ConnectedPort
!= 0) {
378 Instance
->ConnectedPort
= EndPoint
->RemotePort
;
383 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
385 Len
= UdpPacket
->TotalSize
;
387 if (UdpPacket
->BlockOpNum
> 1) {
388 Packet
= AllocatePool (Len
);
390 if (Packet
== NULL
) {
391 Status
= EFI_OUT_OF_RESOURCES
;
395 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
398 Packet
= (EFI_MTFTP4_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
399 ASSERT (Packet
!= NULL
);
402 Opcode
= NTOHS (Packet
->OpCode
);
405 // Call the user's CheckPacket if provided. Abort the transmission
406 // if CheckPacket returns an EFI_ERROR code.
408 if ((Instance
->Token
->CheckPacket
!= NULL
) &&
409 ((Opcode
== EFI_MTFTP4_OPCODE_OACK
) || (Opcode
== EFI_MTFTP4_OPCODE_ERROR
))) {
411 Status
= Instance
->Token
->CheckPacket (
418 if (EFI_ERROR (Status
)) {
420 // Send an error message to the server to inform it
422 if (Opcode
!= EFI_MTFTP4_OPCODE_ERROR
) {
425 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
426 (UINT8
*) "User aborted the transfer"
430 Status
= EFI_ABORTED
;
436 case EFI_MTFTP4_OPCODE_ACK
:
437 if (Len
!= MTFTP4_OPCODE_LEN
+ MTFTP4_BLKNO_LEN
) {
441 Status
= Mtftp4WrqHandleAck (Instance
, Packet
, Len
, &Completed
);
444 case EFI_MTFTP4_OPCODE_OACK
:
445 if (Len
<= MTFTP4_OPCODE_LEN
) {
449 Status
= Mtftp4WrqHandleOack (Instance
, Packet
, Len
, &Completed
);
452 case EFI_MTFTP4_OPCODE_ERROR
:
453 Status
= EFI_TFTP_ERROR
;
462 // Free the resources, then if !EFI_ERROR (Status) and not completed,
463 // restart the receive, otherwise end the session.
465 if ((Packet
!= NULL
) && (UdpPacket
->BlockOpNum
> 1)) {
469 if (UdpPacket
!= NULL
) {
470 NetbufFree (UdpPacket
);
473 if (!EFI_ERROR (Status
) && !Completed
) {
474 Status
= UdpIoRecvDatagram (Instance
->UnicastPort
, Mtftp4WrqInput
, Instance
, 0);
478 // Status may have been updated by UdpIoRecvDatagram
480 if (EFI_ERROR (Status
) || Completed
) {
481 Mtftp4CleanOperation (Instance
, Status
);
488 Start the MTFTP session for upload.
490 It will first init some states, then send the WRQ request packet,
491 and start receiving the packet.
493 @param Instance The MTFTP session
494 @param Operation Redundant parameter, which is always
495 EFI_MTFTP4_OPCODE_WRQ here.
497 @retval EFI_SUCCESS The upload process has been started.
498 @retval Others Failed to start the upload.
503 IN MTFTP4_PROTOCOL
*Instance
,
510 // The valid block number range are [0, 0xffff]. For example:
511 // the client sends an WRQ request to the server, the server
512 // ACK with an ACK0 to let client start transfer the first
515 Status
= Mtftp4InitBlockRange (&Instance
->Blocks
, 0, 0xffff);
517 if (EFI_ERROR (Status
)) {
521 Status
= Mtftp4SendRequest (Instance
);
523 if (EFI_ERROR (Status
)) {
527 return UdpIoRecvDatagram (Instance
->UnicastPort
, Mtftp4WrqInput
, Instance
, 0);