/** @file
Mtftp6 support functions implementation.
Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "Mtftp6Impl.h"
/**
Allocate a MTFTP block range, then init it to the range of [Start, End].
@param[in] Start The start block number.
@param[in] End The last block number in the range.
@return Range The range of the allocated block buffer.
**/
)
{
return NULL;
}
return Range;
}
/**
Initialize the block range for either RRQ or WRQ. RRQ and WRQ have
different requirements for Start and End. For example, during startup,
WRQ initializes its whole valid block range to [0, 0xffff]. This
is bacause the server will send an ACK0 to inform the user to start the
upload. When the client receives an ACK0, it will remove 0 from the range,
get the next block number, which is 1, then upload the BLOCK1. For RRQ
without option negotiation, the server will directly send 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 negotiation.
@param[in] Head The block range head to initialize.
@param[in] Start The Start block number.
@param[in] 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.
**/
)
{
return EFI_OUT_OF_RESOURCES;
}
return EFI_SUCCESS;
}
/**
Get the first valid block number on the range list.
@param[in] Head The block range head.
@retval ==-1 If the block range is empty.
@retval >-1 The first valid block number.
**/
)
{
if (IsListEmpty (Head)) {
return -1;
}
}
/**
Set the last block number of the block range list. It
removes all the blocks after the Last. MTFTP initialize the
block range to the maximum possible range, such as [0, 0xffff]
for WRQ. When it gets the last block number, it calls
this function to set the last block number.
@param[in] Head The block range list.
@param[in] Last The last block number.
**/
)
{
//
// Iterate from the tail to head to remove the block number
// after the last.
//
while (!IsListEmpty (Head)) {
continue;
}
}
return ;
}
}
/**
Remove the block number from the block range list.
@param[in] Head The block range list to remove from.
@param[in] Num The block number to remove.
@param[in] Completed Whether Num is the last block number
@param[out] TotalBlock The continuous block number in all
@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 resources.
**/
)
{
//
// Each block represents a hole [Start, End] in the file,
// skip to the first range with 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).
// if (End > Num), the hold is splited into two holes, with
// [Start, Num - 1] and [Num + 1, End].
//
return EFI_NOT_FOUND;
//
// 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
// this solution.
//
*TotalBlock = Num;
*TotalBlock += Range->Bound + MultU64x32 ((UINT64) (Range->Round -1), (UINT32)(Range->Bound + 1)) + 1;
}
}
}
return EFI_SUCCESS;
} else {
} else {
return EFI_OUT_OF_RESOURCES;
}
}
return EFI_SUCCESS;
}
}
return EFI_NOT_FOUND;
}
/**
Configure the opened Udp6 instance until the corresponding Ip6 instance
has been configured.
@param[in] UdpIo The pointer to the Udp6 Io.
@param[in] UdpCfgData The pointer to the Udp6 configure data.
@retval EFI_SUCCESS Configure the Udp6 instance successfully.
@retval EFI_NO_MAPPING The corresponding Ip6 instance has not
been configured yet.
**/
)
{
//
// Create a timer to check whether the Ip6 instance configured or not.
//
NULL,
NULL,
);
goto ON_EXIT;
}
);
goto ON_EXIT;
}
//
// Check the Ip6 mode data till timeout.
//
if (Ip6Mode.IsConfigured) {
//
// Continue to configure the Udp6 instance.
//
} else {
}
}
}
}
return Status;
}
/**
The dummy configure routine for create a new Udp6 Io.
@param[in] UdpIo The pointer to the Udp6 Io.
@param[in] Context The pointer to the context.
@retval EFI_SUCCESS This value is always returned.
**/
)
{
return EFI_SUCCESS;
}
/**
@param[in] UdpIo The pointer to the Udp6 Io.
@param[in] ServerIp The pointer to the server address.
@param[in] ServerPort The pointer to the server port.
@param[in] LocalIp The pointer to the local address.
@param[in] LocalPort The pointer to the local port.
@retval EFI_SUCCESS Configured the Udp6 Io for Mtftp6 successfully.
@retval EFI_NO_MAPPING The corresponding Ip6 instance has not been
configured yet.
**/
)
{
//
// Set the Udp6 Io configure data.
//
Udp6Cfg->TrafficClass = 0;
Udp6Cfg->ReceiveTimeout = 0;
Udp6Cfg->TransmitTimeout = 0;
CopyMem (
sizeof (EFI_IPv6_ADDRESS)
);
CopyMem (
sizeof (EFI_IPv6_ADDRESS)
);
//
// Configure the Udp6 instance with current configure data.
//
if (Status == EFI_NO_MAPPING) {
}
return Status;
}
/**
Build and transmit the request packet for the Mtftp6 instance.
@param[in] Instance The pointer to the Mtftp6 instance.
@param[in] Operation The operation code of this packet.
@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.
**/
)
{
}
//
//
// 2 bytes string 1 byte string 1 byte
// ------------------------------------------------
// | Opcode | Filename | 0 | Mode | 0 |
// ------------------------------------------------
//
// The common option format is:
//
// string 1 byte string 1 byte
// ---------------------------------------
// | OptionStr | 0 | ValueStr | 0 |
// ---------------------------------------
//
//
// Compute the size of new Mtftp6 packet.
//
}
//
// Allocate a packet then copy the data.
//
return EFI_OUT_OF_RESOURCES;
}
//
// Copy the opcode, filename and mode into packet.
//
//
// Copy all the extension options into the packet.
//
}
//
// Save the packet buf for retransmit
//
}
}
/**
Build and send an error packet.
@param[in] Instance The pointer to the Mtftp6 instance.
@param[in] ErrCode The error code in the packet.
@param[in] ErrInfo The error message in the packet.
@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.
**/
)
{
//
// Allocate a packet then copy the data.
//
return EFI_OUT_OF_RESOURCES;
}
NetbufFree (Nbuf);
return EFI_OUT_OF_RESOURCES;
}
//
// Save the packet buf for retransmit
//
}
}
/**
The callback function called when the packet is transmitted.
@param[in] Packet The pointer to the packet.
@param[in] UdpEpt The pointer to the Udp6 access point.
@param[in] IoStatus The result of the transmission.
@param[in] Context The pointer to the context.
**/
)
{
NetbufFree (Packet);
}
/**
Send the packet for the Mtftp6 instance.
@param[in] Instance The pointer to the Mtftp6 instance.
@param[in] Packet The pointer to the packet to be sent.
@retval EFI_SUCCESS The packet was sent out
@retval Others Failed to transmit the packet.
**/
)
{
//
// Set the live time of the packet.
//
if (OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR || OpCode == EFI_MTFTP6_OPCODE_WRQ) {
//
// For the Rrq, Dir, Wrq requests of the operation, configure the Udp6Io as
// (serverip, 69, localip, localport) to send.
// Usually local address and local port are both default as zero.
//
return Status;
}
);
return Status;
}
//
// Get the current local address and port by get Udp6 mode data.
//
return Status;
}
NULL,
NULL,
);
return Status;
}
//
// Poll till the packet sent out from the ip6 queue.
//
while (!Instance->IsTransmitted) {
}
//
// For the subsequent exchange of such requests, reconfigure the Udp6Io as
// (serverip, 0, localip, localport) to receive.
// Currently local address and local port are specified by Udp6 mode data.
//
return Status;
}
);
} else {
//
// For the data exchange, configure the Udp6Io as (serverip, dataport,
// Currently local address and local port are specified by Udp6 mode data.
//
return Status;
}
return Status;
}
);
return Status;
}
}
NULL,
NULL,
);
}
//
// Poll till the packet sent out from the ip6 queue.
//
while (!Instance->IsTransmitted) {
}
}
return Status;
}
/**
Check packet for GetInfo callback routine.
GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect
the first packet from server, then abort the session.
@param[in] This The pointer to the Mtftp6 protocol.
@param[in] Token The pointer to the Mtftp6 token.
@param[in] PacketLen The length of the packet.
@param[in] Packet The pointer to the received packet.
@retval EFI_ABORTED Abort the Mtftp6 operation.
**/
)
{
//
// Set the GetInfo's return status according to the OpCode.
//
switch (OpCode) {
case EFI_MTFTP6_OPCODE_ERROR:
break;
case EFI_MTFTP6_OPCODE_OACK:
break;
default:
}
//
// Allocate buffer then copy the packet over. Use gBS->AllocatePool
// in case NetAllocatePool will implements something tricky.
//
return EFI_ABORTED;
}
return EFI_ABORTED;
}
/**
Clean up the current Mtftp6 operation.
@param[in] Instance The pointer to the Mtftp6 instance.
@param[in] Result The result to be returned to the user.
**/
)
{
//
// Clean up the current token and event.
//
}
}
//
// Clean up the corresponding Udp6Io.
//
}
}
//
// Clean up the stored last packet.
//
}
}
//
// Reinitialize the corresponding fields of the Mtftp6 operation.
//
Instance->ServerCmdPort = 0;
Instance->ServerDataPort = 0;
Instance->PacketToLive = 0;
}
/**
Start the Mtftp6 instance to perform the operation, such as read file,
write file, and read directory.
@param[in] This The MTFTP session.
@param[in] Token The token than encapsues the user's request.
@param[in] OpCode The operation to perform.
@retval EFI_INVALID_PARAMETER Some of the parameters are invalid.
@retval EFI_NOT_STARTED The MTFTP session hasn't been configured.
@retval EFI_ALREADY_STARTED There is pending operation for the session.
@retval EFI_SUCCESS The operation is successfully started.
**/
)
{
) {
return EFI_INVALID_PARAMETER;
}
//
// At least define one method to collect the data for download.
//
) {
return EFI_INVALID_PARAMETER;
}
//
// At least define one method to provide the data for upload.
//
return EFI_INVALID_PARAMETER;
}
return EFI_NOT_STARTED;
}
return EFI_ACCESS_DENIED;
}
//
// Parse the extension options in the request packet.
//
if (Token->OptionCount != 0) {
TRUE,
);
goto ON_ERROR;
}
}
//
// Initialize runtime data from config data or override data.
//
Instance->ServerDataPort = 0;
CopyMem (
sizeof (EFI_IPv6_ADDRESS)
);
CopyMem (
sizeof (EFI_IPv6_ADDRESS)
);
}
//
// Set default value for undefined parameters.
//
if (Instance->ServerCmdPort == 0) {
}
}
}
}
//
// Switch the routines by the operation code.
//
switch (OpCode) {
case EFI_MTFTP6_OPCODE_RRQ:
break;
case EFI_MTFTP6_OPCODE_DIR:
break;
case EFI_MTFTP6_OPCODE_WRQ:
break;
default:
goto ON_ERROR;
}
goto ON_ERROR;
}
//
// Return immediately for asynchronous or poll the instance for synchronous.
//
}
}
return EFI_SUCCESS;
return Status;
}
/**
The timer ticking routine for the Mtftp6 instance.
@param[in] Event The pointer to the ticking event.
@param[in] Context The pointer to the context.
**/
)
{
//
// Iterate through all the children of the Mtftp service instance. Time
// out the packet. If maximum retries reached, clean the session up.
//
continue;
}
if (Instance->PacketToLive > 0) {
Instance->PacketToLive--;
continue;
}
//
// Call the timeout callback routine if has.
//
(UINT8 *) "User aborted the transfer in time out"
);
continue;
}
}
//
// Retransmit the packet if haven't reach the maxmium retry count,
// otherwise exit the transfer.
//
} else {
continue;
}
}
}