2 Routines to process Wrq (upload).
4 Copyright (c) 2006, 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.
15 #include "Mtftp4Impl.h"
20 Build then send a MTFTP data packet for the MTFTP upload session.
22 @param Instance The MTFTP upload session.
23 @param BlockNum The block number to send.
25 @retval EFI_OUT_OF_RESOURCES Failed to build the packet.
26 @retval EFI_ABORTED The consumer of this child directs to abort the
27 transmission by return an error through PacketNeeded.
28 @retval EFI_SUCCESS The data is sent.
33 IN OUT MTFTP4_PROTOCOL
*Instance
,
37 EFI_MTFTP4_PACKET
*Packet
;
38 EFI_MTFTP4_TOKEN
*Token
;
46 // Allocate a buffer to hold the user data
48 UdpPacket
= NetbufAlloc (Instance
->BlkSize
+ MTFTP4_DATA_HEAD_LEN
);
50 if (UdpPacket
== NULL
) {
51 return EFI_OUT_OF_RESOURCES
;
54 Packet
= (EFI_MTFTP4_PACKET
*) NetbufAllocSpace (UdpPacket
, MTFTP4_DATA_HEAD_LEN
, FALSE
);
56 Packet
->Data
.OpCode
= HTONS (EFI_MTFTP4_OPCODE_DATA
);
57 Packet
->Data
.Block
= HTONS (BlockNum
);
60 // Read the block from either the buffer or PacketNeeded callback
62 Token
= Instance
->Token
;
63 DataLen
= Instance
->BlkSize
;
65 if (Token
->Buffer
!= NULL
) {
66 Start
= MultU64x32 (BlockNum
- 1, Instance
->BlkSize
);
68 if (Token
->BufferSize
< Start
+ Instance
->BlkSize
) {
69 DataLen
= (UINT16
) (Token
->BufferSize
- Start
);
70 Instance
->LastBlock
= BlockNum
;
71 Mtftp4SetLastBlockNum (&Instance
->Blocks
, BlockNum
);
75 NetbufAllocSpace (UdpPacket
, DataLen
, FALSE
);
76 CopyMem (Packet
->Data
.Data
, (UINT8
*) Token
->Buffer
+ Start
, DataLen
);
81 // Get data from PacketNeeded
84 Status
= Token
->PacketNeeded (
91 if (EFI_ERROR (Status
) || (DataLen
> Instance
->BlkSize
)) {
92 if (DataBuf
!= NULL
) {
93 gBS
->FreePool (DataBuf
);
98 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
99 (UINT8
*) "User aborted the transfer"
105 if (DataLen
< Instance
->BlkSize
) {
106 Instance
->LastBlock
= BlockNum
;
107 Mtftp4SetLastBlockNum (&Instance
->Blocks
, BlockNum
);
111 NetbufAllocSpace (UdpPacket
, DataLen
, FALSE
);
112 CopyMem (Packet
->Data
.Data
, DataBuf
, DataLen
);
113 gBS
->FreePool (DataBuf
);
117 return Mtftp4SendPacket (Instance
, UdpPacket
);
122 Function to handle received ACK packet.
124 If the ACK number matches the expected block number, and there are more
125 data pending, send the next block. Otherwise tell the caller that we are done.
127 @param Instance The MTFTP upload session
128 @param Packet The MTFTP packet received
129 @param Len The packet length
130 @param Completed Return whether the upload has finished.
132 @retval EFI_SUCCESS The ACK is successfully processed.
133 @retval EFI_TFTP_ERROR The block number loops back.
134 @retval Others Failed to transmit the next data packet.
139 IN MTFTP4_PROTOCOL
*Instance
,
140 IN EFI_MTFTP4_PACKET
*Packet
,
142 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
);
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
, &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 Points 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
338 IN NET_BUF
*UdpPacket
,
339 IN UDP_POINTS
*Points
,
340 IN EFI_STATUS IoStatus
,
344 MTFTP4_PROTOCOL
*Instance
;
345 EFI_MTFTP4_PACKET
*Packet
;
351 Instance
= (MTFTP4_PROTOCOL
*) Context
;
352 NET_CHECK_SIGNATURE (Instance
, MTFTP4_PROTOCOL_SIGNATURE
);
356 Status
= EFI_SUCCESS
;
358 if (EFI_ERROR (IoStatus
)) {
363 ASSERT (UdpPacket
!= NULL
);
365 if (UdpPacket
->TotalSize
< MTFTP4_OPCODE_LEN
) {
370 // Client send initial request to server's listening port. Server
371 // will select a UDP port to communicate with the client.
373 if (Points
->RemotePort
!= Instance
->ConnectedPort
) {
374 if (Instance
->ConnectedPort
!= 0) {
377 Instance
->ConnectedPort
= Points
->RemotePort
;
382 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
384 Len
= UdpPacket
->TotalSize
;
386 if (UdpPacket
->BlockOpNum
> 1) {
387 Packet
= AllocatePool (Len
);
389 if (Packet
== NULL
) {
390 Status
= EFI_OUT_OF_RESOURCES
;
394 NetbufCopy (UdpPacket
, 0, Len
, (UINT8
*) Packet
);
397 Packet
= (EFI_MTFTP4_PACKET
*) NetbufGetByte (UdpPacket
, 0, NULL
);
400 Opcode
= NTOHS (Packet
->OpCode
);
403 // Call the user's CheckPacket if provided. Abort the transmission
404 // if CheckPacket returns an EFI_ERROR code.
406 if ((Instance
->Token
->CheckPacket
!= NULL
) &&
407 ((Opcode
== EFI_MTFTP4_OPCODE_OACK
) || (Opcode
== EFI_MTFTP4_OPCODE_ERROR
))) {
409 Status
= Instance
->Token
->CheckPacket (
416 if (EFI_ERROR (Status
)) {
418 // Send an error message to the server to inform it
420 if (Opcode
!= EFI_MTFTP4_OPCODE_ERROR
) {
423 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED
,
424 (UINT8
*) "User aborted the transfer"
428 Status
= EFI_ABORTED
;
434 case EFI_MTFTP4_OPCODE_ACK
:
435 if (Len
!= MTFTP4_OPCODE_LEN
+ MTFTP4_BLKNO_LEN
) {
439 Status
= Mtftp4WrqHandleAck (Instance
, Packet
, Len
, &Completed
);
442 case EFI_MTFTP4_OPCODE_OACK
:
443 if (Len
<= MTFTP4_OPCODE_LEN
) {
447 Status
= Mtftp4WrqHandleOack (Instance
, Packet
, Len
, &Completed
);
450 case EFI_MTFTP4_OPCODE_ERROR
:
451 Status
= EFI_TFTP_ERROR
;
460 // Free the resources, then if !EFI_ERROR (Status) and not completed,
461 // restart the receive, otherwise end the session.
463 if ((Packet
!= NULL
) && (UdpPacket
->BlockOpNum
> 1)) {
464 gBS
->FreePool (Packet
);
467 if (UdpPacket
!= NULL
) {
468 NetbufFree (UdpPacket
);
471 if (!EFI_ERROR (Status
) && !Completed
) {
472 Status
= UdpIoRecvDatagram (Instance
->UnicastPort
, Mtftp4WrqInput
, Instance
, 0);
476 // Status may have been updated by UdpIoRecvDatagram
478 if (EFI_ERROR (Status
) || Completed
) {
479 Mtftp4CleanOperation (Instance
, Status
);
486 Start the MTFTP session for upload.
488 It will first init some states, then send the WRQ request packet,
489 and start receiving the packet.
491 @param Instance The MTFTP session
492 @param Operation Redundant parameter, which is always
493 EFI_MTFTP4_OPCODE_WRQ here.
495 @retval EFI_SUCCESS The upload process has been started.
496 @retval Others Failed to start the upload.
501 IN MTFTP4_PROTOCOL
*Instance
,
508 // The valid block number range are [0, 0xffff]. For example:
509 // the client sends an WRQ request to the server, the server
510 // ACK with an ACK0 to let client start transfer the first
513 Status
= Mtftp4InitBlockRange (&Instance
->Blocks
, 0, 0xffff);
515 if (EFI_ERROR (Status
)) {
519 Status
= Mtftp4SendRequest (Instance
);
521 if (EFI_ERROR (Status
)) {
525 return UdpIoRecvDatagram (Instance
->UnicastPort
, Mtftp4WrqInput
, Instance
, 0);