2007-07-30 04:37:10 +02:00
|
|
|
/** @file
|
2009-01-16 07:56:18 +01:00
|
|
|
Support routines for Mtftp.
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2018-03-02 04:33:28 +01:00
|
|
|
Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
|
2019-04-04 01:05:13 +02:00
|
|
|
SPDX-License-Identifier: BSD-2-Clause-Patent
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "Mtftp4Impl.h"
|
|
|
|
|
|
|
|
/**
|
2009-01-16 07:56:18 +01:00
|
|
|
Allocate a MTFTP4 block range, then init it to the range of [Start, End]
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@param Start The start block number
|
|
|
|
@param End The last block number in the range
|
|
|
|
|
2009-01-16 07:56:18 +01:00
|
|
|
@return Pointer to the created block range, NULL if failed to allocate memory.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
MTFTP4_BLOCK_RANGE *
|
|
|
|
Mtftp4AllocateRange (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN UINT16 Start,
|
|
|
|
IN UINT16 End
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
MTFTP4_BLOCK_RANGE *Range;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
2010-06-29 03:26:28 +02:00
|
|
|
Range = AllocateZeroPool (sizeof (MTFTP4_BLOCK_RANGE));
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
if (Range == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
InitializeListHead (&Range->Link);
|
2021-12-05 23:54:07 +01:00
|
|
|
Range->Start = Start;
|
|
|
|
Range->End = End;
|
|
|
|
Range->Bound = End;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
return Range;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-27 15:08:52 +02:00
|
|
|
Initialize the block range for either RRQ or WRQ.
|
|
|
|
|
|
|
|
RRQ and WRQ have different requirements for Start and End.
|
|
|
|
For example, during start up, WRQ initializes its whole valid block range
|
2020-02-07 02:07:57 +01:00
|
|
|
to [0, 0xffff]. This is because the server will send us a ACK0 to inform us
|
2018-06-27 15:08:52 +02:00
|
|
|
to start the upload. When the client received ACK0, it will remove 0 from the
|
2009-01-16 07:56:18 +01:00
|
|
|
range, get the next block number, which is 1, then upload the BLOCK1. For RRQ
|
2018-06-27 15:08:52 +02:00
|
|
|
without option negotiation, the server will directly send us the BLOCK1 in
|
|
|
|
response to the client's RRQ. When received BLOCK1, the client will remove
|
|
|
|
it from the block range and send an ACK. It also works if there is option
|
2009-01-16 07:56:18 +01:00
|
|
|
negotiation.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@param Head The block range head to initialize
|
|
|
|
@param Start The Start block number.
|
|
|
|
@param End The last block number.
|
|
|
|
|
|
|
|
@retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range
|
|
|
|
@retval EFI_SUCCESS The initial block range is created.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
Mtftp4InitBlockRange (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN LIST_ENTRY *Head,
|
|
|
|
IN UINT16 Start,
|
|
|
|
IN UINT16 End
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
MTFTP4_BLOCK_RANGE *Range;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
Range = Mtftp4AllocateRange (Start, End);
|
|
|
|
|
|
|
|
if (Range == NULL) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
InsertTailList (Head, &Range->Link);
|
2007-07-30 04:37:10 +02:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Get the first valid block number on the range list.
|
|
|
|
|
|
|
|
@param Head The block range head
|
|
|
|
|
2018-06-27 15:08:52 +02:00
|
|
|
@return The first valid block number, -1 if the block range is empty.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
INTN
|
|
|
|
Mtftp4GetNextBlockNum (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN LIST_ENTRY *Head
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
MTFTP4_BLOCK_RANGE *Range;
|
|
|
|
|
2008-02-14 10:40:22 +01:00
|
|
|
if (IsListEmpty (Head)) {
|
2007-07-30 04:37:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Range = NET_LIST_HEAD (Head, MTFTP4_BLOCK_RANGE, Link);
|
|
|
|
return Range->Start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-27 15:08:52 +02:00
|
|
|
Set the last block number of the block range list.
|
|
|
|
|
2009-01-16 07:56:18 +01:00
|
|
|
It will remove all the blocks after the Last. MTFTP initialize the block range
|
2018-06-27 15:08:52 +02:00
|
|
|
to the maximum possible range, such as [0, 0xffff] for WRQ. When it gets the
|
2009-01-16 07:56:18 +01:00
|
|
|
last block number, it will call this function to set the last block number.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@param Head The block range list
|
|
|
|
@param Last The last block number
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
Mtftp4SetLastBlockNum (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN LIST_ENTRY *Head,
|
|
|
|
IN UINT16 Last
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
MTFTP4_BLOCK_RANGE *Range;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Iterate from the tail to head to remove the block number
|
|
|
|
// after the last.
|
|
|
|
//
|
2008-02-14 10:40:22 +01:00
|
|
|
while (!IsListEmpty (Head)) {
|
2007-07-30 04:37:10 +02:00
|
|
|
Range = NET_LIST_TAIL (Head, MTFTP4_BLOCK_RANGE, Link);
|
|
|
|
|
|
|
|
if (Range->Start > Last) {
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&Range->Link);
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (Range);
|
2007-07-30 04:37:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Range->End > Last) {
|
|
|
|
Range->End = Last;
|
|
|
|
}
|
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
return;
|
2007-07-30 04:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Remove the block number from the block range list.
|
|
|
|
|
|
|
|
@param Head The block range list to remove from
|
|
|
|
@param Num The block number to remove
|
2018-10-25 09:31:10 +02:00
|
|
|
@param Completed Whether Num is the last block number.
|
|
|
|
@param BlockCounter The continuous block counter instead of the value after roll-over.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@retval EFI_NOT_FOUND The block number isn't in the block range list
|
|
|
|
@retval EFI_SUCCESS The block number has been removed from the list
|
|
|
|
@retval EFI_OUT_OF_RESOURCES Failed to allocate resource
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
Mtftp4RemoveBlockNum (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN LIST_ENTRY *Head,
|
|
|
|
IN UINT16 Num,
|
|
|
|
IN BOOLEAN Completed,
|
|
|
|
OUT UINT64 *BlockCounter
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
MTFTP4_BLOCK_RANGE *Range;
|
|
|
|
MTFTP4_BLOCK_RANGE *NewRange;
|
|
|
|
LIST_ENTRY *Entry;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
NET_LIST_FOR_EACH (Entry, Head) {
|
|
|
|
//
|
|
|
|
// Each block represents a hole [Start, End] in the file,
|
|
|
|
// skip to the first range with End >= Num
|
|
|
|
//
|
|
|
|
Range = NET_LIST_USER_STRUCT (Entry, MTFTP4_BLOCK_RANGE, Link);
|
|
|
|
|
|
|
|
if (Range->End < Num) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// There are three different cases for Start
|
|
|
|
// 1. (Start > Num) && (End >= Num):
|
|
|
|
// because all the holes before this one has the condition of
|
|
|
|
// End < Num, so this block number has been removed.
|
|
|
|
//
|
|
|
|
// 2. (Start == Num) && (End >= Num):
|
|
|
|
// Need to increase the Start by one, and if End == Num, this
|
|
|
|
// hole has been removed completely, remove it.
|
|
|
|
//
|
|
|
|
// 3. (Start < Num) && (End >= Num):
|
|
|
|
// if End == Num, only need to decrease the End by one because
|
|
|
|
// we have (Start < Num) && (Num == End), so (Start <= End - 1).
|
2020-02-07 02:07:57 +01:00
|
|
|
// if (End > Num), the hold is split into two holes, with
|
2007-07-30 04:37:10 +02:00
|
|
|
// [Start, Num - 1] and [Num + 1, End].
|
|
|
|
//
|
|
|
|
if (Range->Start > Num) {
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
} else if (Range->Start == Num) {
|
|
|
|
Range->Start++;
|
|
|
|
|
2010-06-29 03:26:28 +02:00
|
|
|
//
|
2018-06-27 15:08:52 +02:00
|
|
|
// Note that: RFC 1350 does not mention block counter roll-over,
|
|
|
|
// but several TFTP hosts implement the roll-over be able to accept
|
|
|
|
// transfers of unlimited size. There is no consensus, however, whether
|
|
|
|
// the counter should wrap around to zero or to one. Many implementations
|
|
|
|
// wrap to zero, because this is the simplest to implement. Here we choose
|
2010-06-29 03:26:28 +02:00
|
|
|
// this solution.
|
|
|
|
//
|
2021-12-05 23:54:07 +01:00
|
|
|
*BlockCounter = Num;
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2010-06-29 03:26:28 +02:00
|
|
|
if (Range->Round > 0) {
|
2021-12-05 23:54:07 +01:00
|
|
|
*BlockCounter += Range->Bound + MultU64x32 ((UINTN)(Range->Round -1), (UINT32)(Range->Bound + 1)) + 1;
|
2018-09-14 09:47:20 +02:00
|
|
|
}
|
2010-06-29 03:26:28 +02:00
|
|
|
|
|
|
|
if (Range->Start > Range->Bound) {
|
2018-06-27 15:08:52 +02:00
|
|
|
Range->Start = 0;
|
2021-12-05 23:54:07 +01:00
|
|
|
Range->Round++;
|
2010-06-29 03:26:28 +02:00
|
|
|
}
|
|
|
|
|
2010-07-01 11:08:33 +02:00
|
|
|
if ((Range->Start > Range->End) || Completed) {
|
2008-02-14 10:40:22 +01:00
|
|
|
RemoveEntryList (&Range->Link);
|
2009-11-13 07:13:59 +01:00
|
|
|
FreePool (Range);
|
2007-07-30 04:37:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
} else {
|
|
|
|
if (Range->End == Num) {
|
|
|
|
Range->End--;
|
|
|
|
} else {
|
2021-12-05 23:54:07 +01:00
|
|
|
NewRange = Mtftp4AllocateRange ((UINT16)(Num + 1), (UINT16)Range->End);
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
if (NewRange == NULL) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
Range->End = Num - 1;
|
|
|
|
NetListInsertAfter (&Range->Link, &NewRange->Link);
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Build then transmit the request packet for the MTFTP session.
|
|
|
|
|
|
|
|
@param Instance The Mtftp session
|
|
|
|
|
|
|
|
@retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request
|
|
|
|
@retval EFI_SUCCESS The request is built and sent
|
|
|
|
@retval Others Failed to transmit the packet.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
Mtftp4SendRequest (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN MTFTP4_PROTOCOL *Instance
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
EFI_MTFTP4_PACKET *Packet;
|
|
|
|
EFI_MTFTP4_OPTION *Options;
|
|
|
|
EFI_MTFTP4_TOKEN *Token;
|
|
|
|
RETURN_STATUS Status;
|
|
|
|
NET_BUF *Nbuf;
|
|
|
|
UINT8 *Mode;
|
|
|
|
UINT8 *Cur;
|
|
|
|
UINTN Index;
|
|
|
|
UINT32 BufferLength;
|
|
|
|
UINTN FileNameLength;
|
|
|
|
UINTN ModeLength;
|
|
|
|
UINTN OptionStrLength;
|
|
|
|
UINTN ValueStrLength;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
Token = Instance->Token;
|
|
|
|
Options = Token->OptionList;
|
|
|
|
Mode = Instance->Token->ModeStr;
|
|
|
|
|
|
|
|
if (Mode == NULL) {
|
2021-12-05 23:54:07 +01:00
|
|
|
Mode = (UINT8 *)"octet";
|
2007-07-30 04:37:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Compute the packet length
|
|
|
|
//
|
2021-12-05 23:54:07 +01:00
|
|
|
FileNameLength = AsciiStrLen ((CHAR8 *)Token->Filename);
|
|
|
|
ModeLength = AsciiStrLen ((CHAR8 *)Mode);
|
|
|
|
BufferLength = (UINT32)FileNameLength + (UINT32)ModeLength + 4;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
for (Index = 0; Index < Token->OptionCount; Index++) {
|
2021-12-05 23:54:07 +01:00
|
|
|
OptionStrLength = AsciiStrLen ((CHAR8 *)Options[Index].OptionStr);
|
|
|
|
ValueStrLength = AsciiStrLen ((CHAR8 *)Options[Index].ValueStr);
|
|
|
|
BufferLength += (UINT32)OptionStrLength + (UINT32)ValueStrLength + 2;
|
2007-07-30 04:37:10 +02:00
|
|
|
}
|
2021-12-05 23:54:07 +01:00
|
|
|
|
2007-07-30 04:37:10 +02:00
|
|
|
//
|
|
|
|
// Allocate a packet then copy the data over
|
|
|
|
//
|
2015-07-10 08:57:22 +02:00
|
|
|
if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) {
|
2007-07-30 04:37:10 +02:00
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
Packet = (EFI_MTFTP4_PACKET *)NetbufAllocSpace (Nbuf, BufferLength, FALSE);
|
2009-06-29 11:19:25 +02:00
|
|
|
ASSERT (Packet != NULL);
|
|
|
|
|
2007-07-30 04:37:10 +02:00
|
|
|
Packet->OpCode = HTONS (Instance->Operation);
|
2015-07-10 08:57:22 +02:00
|
|
|
BufferLength -= sizeof (Packet->OpCode);
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
Cur = Packet->Rrq.Filename;
|
|
|
|
Status = AsciiStrCpyS ((CHAR8 *)Cur, BufferLength, (CHAR8 *)Token->Filename);
|
2015-07-10 08:57:22 +02:00
|
|
|
ASSERT_EFI_ERROR (Status);
|
2021-12-05 23:54:07 +01:00
|
|
|
BufferLength -= (UINT32)(FileNameLength + 1);
|
|
|
|
Cur += FileNameLength + 1;
|
|
|
|
Status = AsciiStrCpyS ((CHAR8 *)Cur, BufferLength, (CHAR8 *)Mode);
|
2015-07-10 08:57:22 +02:00
|
|
|
ASSERT_EFI_ERROR (Status);
|
2021-12-05 23:54:07 +01:00
|
|
|
BufferLength -= (UINT32)(ModeLength + 1);
|
|
|
|
Cur += ModeLength + 1;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
for (Index = 0; Index < Token->OptionCount; ++Index) {
|
2021-12-05 23:54:07 +01:00
|
|
|
OptionStrLength = AsciiStrLen ((CHAR8 *)Options[Index].OptionStr);
|
|
|
|
ValueStrLength = AsciiStrLen ((CHAR8 *)Options[Index].ValueStr);
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
Status = AsciiStrCpyS ((CHAR8 *)Cur, BufferLength, (CHAR8 *)Options[Index].OptionStr);
|
2015-07-10 08:57:22 +02:00
|
|
|
ASSERT_EFI_ERROR (Status);
|
2021-12-05 23:54:07 +01:00
|
|
|
BufferLength -= (UINT32)(OptionStrLength + 1);
|
|
|
|
Cur += OptionStrLength + 1;
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
Status = AsciiStrCpyS ((CHAR8 *)Cur, BufferLength, (CHAR8 *)Options[Index].ValueStr);
|
2015-07-10 08:57:22 +02:00
|
|
|
ASSERT_EFI_ERROR (Status);
|
2021-12-05 23:54:07 +01:00
|
|
|
BufferLength -= (UINT32)(ValueStrLength + 1);
|
|
|
|
Cur += ValueStrLength + 1;
|
2007-07-30 04:37:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Mtftp4SendPacket (Instance, Nbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-16 07:56:18 +01:00
|
|
|
Build then send an error message.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@param Instance The MTFTP session
|
2018-06-27 15:08:52 +02:00
|
|
|
@param ErrCode The error code
|
2009-01-16 07:56:18 +01:00
|
|
|
@param ErrInfo The error message
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
|
|
|
|
@retval EFI_SUCCESS The error packet is transmitted.
|
|
|
|
@retval Others Failed to transmit the packet.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
Mtftp4SendError (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN MTFTP4_PROTOCOL *Instance,
|
|
|
|
IN UINT16 ErrCode,
|
|
|
|
IN UINT8 *ErrInfo
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
NET_BUF *Packet;
|
|
|
|
EFI_MTFTP4_PACKET *TftpError;
|
|
|
|
UINT32 Len;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
Len = (UINT32)(AsciiStrLen ((CHAR8 *)ErrInfo) + sizeof (EFI_MTFTP4_ERROR_HEADER));
|
|
|
|
Packet = NetbufAlloc (Len);
|
2007-07-30 04:37:10 +02:00
|
|
|
if (Packet == NULL) {
|
|
|
|
return EFI_OUT_OF_RESOURCES;
|
|
|
|
}
|
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
TftpError = (EFI_MTFTP4_PACKET *)NetbufAllocSpace (Packet, Len, FALSE);
|
2009-06-29 11:19:25 +02:00
|
|
|
ASSERT (TftpError != NULL);
|
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
TftpError->OpCode = HTONS (EFI_MTFTP4_OPCODE_ERROR);
|
2007-07-30 04:37:10 +02:00
|
|
|
TftpError->Error.ErrorCode = HTONS (ErrCode);
|
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
AsciiStrCpyS ((CHAR8 *)TftpError->Error.ErrorMessage, Len, (CHAR8 *)ErrInfo);
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
return Mtftp4SendPacket (Instance, Packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
The callback function called when the packet is transmitted.
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2007-07-30 04:37:10 +02:00
|
|
|
It simply frees the packet.
|
|
|
|
|
|
|
|
@param Packet The transmitted (or failed to) packet
|
2009-10-30 06:11:38 +01:00
|
|
|
@param EndPoint The local and remote UDP access point
|
2007-07-30 04:37:10 +02:00
|
|
|
@param IoStatus The result of the transmission
|
|
|
|
@param Context Opaque parameter to the callback
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
2010-04-17 01:24:45 +02:00
|
|
|
EFIAPI
|
2007-07-30 04:37:10 +02:00
|
|
|
Mtftp4OnPacketSent (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN NET_BUF *Packet,
|
|
|
|
IN UDP_END_POINT *EndPoint,
|
|
|
|
IN EFI_STATUS IoStatus,
|
|
|
|
IN VOID *Context
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
NetbufFree (Packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-16 07:56:18 +01:00
|
|
|
Set the timeout for the instance. User a longer time for passive instances.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@param Instance The Mtftp session to set time out
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
Mtftp4SetTimeout (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN OUT MTFTP4_PROTOCOL *Instance
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
if (Instance->Master) {
|
|
|
|
Instance->PacketToLive = Instance->Timeout;
|
|
|
|
} else {
|
|
|
|
Instance->PacketToLive = Instance->Timeout * 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-27 15:08:52 +02:00
|
|
|
Send the packet for the instance.
|
|
|
|
|
|
|
|
It will first save a reference to the packet for later retransmission.
|
|
|
|
Then determine the destination port, listen port for requests, and connected
|
2009-01-16 07:56:18 +01:00
|
|
|
port for others. At last, send the packet out.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@param Instance The Mtftp instance
|
|
|
|
@param Packet The packet to send
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The packet is sent out
|
|
|
|
@retval Others Failed to transmit the packet.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
Mtftp4SendPacket (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN OUT MTFTP4_PROTOCOL *Instance,
|
|
|
|
IN OUT NET_BUF *Packet
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
UDP_END_POINT UdpPoint;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT16 OpCode;
|
|
|
|
UINT8 *Buffer;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Save the packet for retransmission
|
|
|
|
//
|
|
|
|
if (Instance->LastPacket != NULL) {
|
|
|
|
NetbufFree (Instance->LastPacket);
|
|
|
|
}
|
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
Instance->LastPacket = Packet;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
Instance->CurRetry = 0;
|
2007-07-30 04:37:10 +02:00
|
|
|
Mtftp4SetTimeout (Instance);
|
|
|
|
|
2009-10-30 06:11:38 +01:00
|
|
|
ZeroMem (&UdpPoint, sizeof (UdpPoint));
|
|
|
|
UdpPoint.RemoteAddr.Addr[0] = Instance->ServerIp;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Send the requests to the listening port, other packets
|
|
|
|
// to the connected port
|
|
|
|
//
|
2012-11-01 04:55:20 +01:00
|
|
|
Buffer = NetbufGetByte (Packet, 0, NULL);
|
|
|
|
ASSERT (Buffer != NULL);
|
|
|
|
OpCode = NTOHS (*(UINT16 *)Buffer);
|
2007-07-30 04:37:10 +02:00
|
|
|
|
2018-06-27 15:08:52 +02:00
|
|
|
if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) ||
|
2009-01-16 07:56:18 +01:00
|
|
|
(OpCode == EFI_MTFTP4_OPCODE_DIR) ||
|
2021-12-05 23:54:07 +01:00
|
|
|
(OpCode == EFI_MTFTP4_OPCODE_WRQ))
|
|
|
|
{
|
2007-07-30 04:37:10 +02:00
|
|
|
UdpPoint.RemotePort = Instance->ListeningPort;
|
|
|
|
} else {
|
|
|
|
UdpPoint.RemotePort = Instance->ConnectedPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
NET_GET_REF (Packet);
|
|
|
|
|
|
|
|
Status = UdpIoSendDatagram (
|
|
|
|
Instance->UnicastPort,
|
|
|
|
Packet,
|
|
|
|
&UdpPoint,
|
2009-10-30 06:11:38 +01:00
|
|
|
NULL,
|
2007-07-30 04:37:10 +02:00
|
|
|
Mtftp4OnPacketSent,
|
|
|
|
Instance
|
|
|
|
);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
NET_PUT_REF (Packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-01-16 07:56:18 +01:00
|
|
|
Retransmit the last packet for the instance.
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
@param Instance The Mtftp instance
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The last packet is retransmitted.
|
|
|
|
@retval Others Failed to retransmit.
|
|
|
|
|
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
Mtftp4Retransmit (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN MTFTP4_PROTOCOL *Instance
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
UDP_END_POINT UdpPoint;
|
|
|
|
EFI_STATUS Status;
|
|
|
|
UINT16 OpCode;
|
|
|
|
UINT8 *Buffer;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
ASSERT (Instance->LastPacket != NULL);
|
|
|
|
|
2009-10-30 06:11:38 +01:00
|
|
|
ZeroMem (&UdpPoint, sizeof (UdpPoint));
|
|
|
|
UdpPoint.RemoteAddr.Addr[0] = Instance->ServerIp;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Set the requests to the listening port, other packets to the connected port
|
|
|
|
//
|
2012-11-01 04:55:20 +01:00
|
|
|
Buffer = NetbufGetByte (Instance->LastPacket, 0, NULL);
|
|
|
|
ASSERT (Buffer != NULL);
|
2021-12-05 23:54:07 +01:00
|
|
|
OpCode = NTOHS (*(UINT16 *)Buffer);
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) || (OpCode == EFI_MTFTP4_OPCODE_DIR) ||
|
2021-12-05 23:54:07 +01:00
|
|
|
(OpCode == EFI_MTFTP4_OPCODE_WRQ))
|
|
|
|
{
|
2007-07-30 04:37:10 +02:00
|
|
|
UdpPoint.RemotePort = Instance->ListeningPort;
|
|
|
|
} else {
|
|
|
|
UdpPoint.RemotePort = Instance->ConnectedPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
NET_GET_REF (Instance->LastPacket);
|
|
|
|
|
|
|
|
Status = UdpIoSendDatagram (
|
|
|
|
Instance->UnicastPort,
|
|
|
|
Instance->LastPacket,
|
|
|
|
&UdpPoint,
|
2009-10-30 06:11:38 +01:00
|
|
|
NULL,
|
2007-07-30 04:37:10 +02:00
|
|
|
Mtftp4OnPacketSent,
|
|
|
|
Instance
|
|
|
|
);
|
|
|
|
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
NET_PUT_REF (Instance->LastPacket);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
2018-03-02 04:33:28 +01:00
|
|
|
/**
|
|
|
|
The timer ticking function in TPL_NOTIFY level for the Mtftp service instance.
|
|
|
|
|
|
|
|
@param Event The ticking event
|
|
|
|
@param Context The Mtftp service instance
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
Mtftp4OnTimerTickNotifyLevel (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN EFI_EVENT Event,
|
|
|
|
IN VOID *Context
|
2018-03-02 04:33:28 +01:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
MTFTP4_SERVICE *MtftpSb;
|
|
|
|
LIST_ENTRY *Entry;
|
|
|
|
LIST_ENTRY *Next;
|
|
|
|
MTFTP4_PROTOCOL *Instance;
|
2018-03-02 04:33:28 +01:00
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
MtftpSb = (MTFTP4_SERVICE *)Context;
|
2018-03-02 04:33:28 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Iterate through all the children of the Mtftp service instance. Time
|
|
|
|
// out the current packet transmit.
|
|
|
|
//
|
|
|
|
NET_LIST_FOR_EACH_SAFE (Entry, Next, &MtftpSb->Children) {
|
|
|
|
Instance = NET_LIST_USER_STRUCT (Entry, MTFTP4_PROTOCOL, Link);
|
|
|
|
if ((Instance->PacketToLive == 0) || (--Instance->PacketToLive > 0)) {
|
|
|
|
Instance->HasTimeout = FALSE;
|
|
|
|
} else {
|
|
|
|
Instance->HasTimeout = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-30 04:37:10 +02:00
|
|
|
/**
|
|
|
|
The timer ticking function for the Mtftp service instance.
|
|
|
|
|
|
|
|
@param Event The ticking event
|
|
|
|
@param Context The Mtftp service instance
|
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
EFIAPI
|
|
|
|
Mtftp4OnTimerTick (
|
2021-12-05 23:54:07 +01:00
|
|
|
IN EFI_EVENT Event,
|
|
|
|
IN VOID *Context
|
2007-07-30 04:37:10 +02:00
|
|
|
)
|
|
|
|
{
|
2021-12-05 23:54:07 +01:00
|
|
|
MTFTP4_SERVICE *MtftpSb;
|
|
|
|
LIST_ENTRY *Entry;
|
|
|
|
LIST_ENTRY *Next;
|
|
|
|
MTFTP4_PROTOCOL *Instance;
|
|
|
|
EFI_MTFTP4_TOKEN *Token;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
MtftpSb = (MTFTP4_SERVICE *)Context;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
//
|
2018-03-02 04:33:28 +01:00
|
|
|
// Iterate through all the children of the Mtftp service instance.
|
2007-07-30 04:37:10 +02:00
|
|
|
//
|
|
|
|
NET_LIST_FOR_EACH_SAFE (Entry, Next, &MtftpSb->Children) {
|
|
|
|
Instance = NET_LIST_USER_STRUCT (Entry, MTFTP4_PROTOCOL, Link);
|
2018-03-02 04:33:28 +01:00
|
|
|
if (!Instance->HasTimeout) {
|
2007-07-30 04:37:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
2018-06-27 15:08:52 +02:00
|
|
|
|
2018-03-02 04:33:28 +01:00
|
|
|
Instance->HasTimeout = FALSE;
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Call the user's time out handler
|
|
|
|
//
|
|
|
|
Token = Instance->Token;
|
|
|
|
|
2021-12-05 23:54:07 +01:00
|
|
|
if ((Token != NULL) && (Token->TimeoutCallback != NULL) &&
|
|
|
|
EFI_ERROR (Token->TimeoutCallback (&Instance->Mtftp4, Token)))
|
|
|
|
{
|
2007-07-30 04:37:10 +02:00
|
|
|
Mtftp4SendError (
|
2018-03-02 04:33:28 +01:00
|
|
|
Instance,
|
|
|
|
EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
|
2021-12-05 23:54:07 +01:00
|
|
|
(UINT8 *)"User aborted the transfer in time out"
|
2018-03-02 04:33:28 +01:00
|
|
|
);
|
2007-07-30 04:37:10 +02:00
|
|
|
|
|
|
|
Mtftp4CleanOperation (Instance, EFI_ABORTED);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2020-02-07 02:07:57 +01:00
|
|
|
// Retransmit the packet if haven't reach the maximum retry count,
|
2007-07-30 04:37:10 +02:00
|
|
|
// otherwise exit the transfer.
|
|
|
|
//
|
|
|
|
if (++Instance->CurRetry < Instance->MaxRetry) {
|
|
|
|
Mtftp4Retransmit (Instance);
|
|
|
|
Mtftp4SetTimeout (Instance);
|
|
|
|
} else {
|
|
|
|
Mtftp4CleanOperation (Instance, EFI_TIMEOUT);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|