mtftp4rrq.c

来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 778 行 · 第 1/2 页

C
778
字号
/*++

Copyright (c) 2006, Intel Corporation                                                         
All rights reserved. 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        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 

Module Name:

  Mtftp4Rrq.c
 
Abstract:

  Routines to process Rrq (download)

--*/

#include "Mtftp4Impl.h"

VOID
Mtftp4RrqInput (
  IN NET_BUF                *UdpPacket,
  IN UDP_POINTS             *Points,
  IN EFI_STATUS             IoStatus,
  IN VOID                   *Context
  );

EFI_STATUS
Mtftp4RrqStart (
  IN MTFTP4_PROTOCOL        *Instance,
  IN UINT16                 Operation
  )
/*++

Routine Description:

  Start the MTFTP session to download. It will first initialize some
  of the internal states then build and send a RRQ reqeuest packet, at
  last, it will start receive for the downloading.

Arguments:

  Instance  - The Mtftp session
  Operation - The MTFTP opcode, it may be a EFI_MTFTP4_OPCODE_RRQ or
              EFI_MTFTP4_OPCODE_DIR.

Returns:

  EFI_SUCCESS - The mtftp download session is started.
  Others      - Failed to start downloading.

--*/
{
  EFI_STATUS                Status;

  //
  // The valid block number range are [1, 0xffff]. For example:
  // the client sends an RRQ request to the server, the server
  // transfers the DATA1 block. If option negoitation is ongoing,
  // the server will send back an OACK, then client will send ACK0.
  //
  Status = Mtftp4InitBlockRange (&Instance->Blocks, 1, 0xffff);

  if (EFI_ERROR (Status)) {
    return Status;
  }

  Status = Mtftp4SendRequest (Instance);

  if (EFI_ERROR (Status)) {
    return Status;
  }

  return UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4RrqInput, Instance, 0);
}

EFI_STATUS
Mtftp4RrqSendAck (
  IN MTFTP4_PROTOCOL        *Instance,
  IN UINT16                 BlkNo
  )
/*++

Routine Description:

  Build and send a ACK packet for the download session.

Arguments:

  Instance  - The Mtftp session
  BlkNo     - The BlkNo to ack.

Returns:

  EFI_OUT_OF_RESOURCES - Failed to allocate memory for the packet
  EFI_SUCCESS          - The ACK has been sent
  Others               - Failed to send the ACK.

--*/
{
  EFI_MTFTP4_PACKET         *Ack;
  NET_BUF                   *Packet;

  Packet = NetbufAlloc (sizeof (EFI_MTFTP4_ACK_HEADER));

  if (Packet == NULL) {
    return EFI_OUT_OF_RESOURCES;
  }

  Ack = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (
                                Packet,
                                sizeof (EFI_MTFTP4_ACK_HEADER),
                                FALSE
                                );

  Ack->Ack.OpCode   = HTONS (EFI_MTFTP4_OPCODE_ACK);
  Ack->Ack.Block[0] = HTONS (BlkNo);

  return Mtftp4SendPacket (Instance, Packet);
}

EFI_STATUS
Mtftp4RrqSaveBlock (
  IN MTFTP4_PROTOCOL        *Instance,
  IN EFI_MTFTP4_PACKET      *Packet,
  IN UINT32                 Len
  )
/*++

Routine Description:

  Deliver the received data block to the user, which can be saved 
  in the user provide buffer or through the CheckPacket callback.

Arguments:

  Instance  - The Mtftp session
  Packet    - The received data packet
  Len       - The packet length

Returns:

  EFI_SUCCESS          - The data is saved successfully
  EFI_ABORTED          - The user tells to abort by return an error 
                         through CheckPacket
  EFI_BUFFER_TOO_SMALL - The user's buffer is too small and buffer length is 
                         updated to the actual buffer size needed.
--*/
{
  EFI_MTFTP4_TOKEN          *Token;
  EFI_STATUS                Status;
  UINT16                    Block;
  UINT64                    Start;
  UINT32                    DataLen;

  Token   = Instance->Token;
  Block   = NTOHS (Packet->Data.Block);
  DataLen = Len - MTFTP4_DATA_HEAD_LEN;

  //
  // This is the last block, save the block no
  //
  if (DataLen < Instance->BlkSize) {
    Instance->LastBlock = Block;
    Mtftp4SetLastBlockNum (&Instance->Blocks, Block);
  } 
  
  //
  // Remove this block number from the file hole. If Mtftp4RemoveBlockNum
  // returns EFI_NOT_FOUND, the block has been saved, don't save it again.
  //
  Status = Mtftp4RemoveBlockNum (&Instance->Blocks, Block);

  if (Status == EFI_NOT_FOUND) {
    return EFI_SUCCESS;
  } else if (EFI_ERROR (Status)) {
    return Status;
  }

  if (Token->CheckPacket != NULL) {
    Status = Token->CheckPacket (&Instance->Mtftp4, Token, (UINT16) Len, Packet);

    if (EFI_ERROR (Status)) {
      Mtftp4SendError (
        Instance, 
        EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION,
        "User aborted download"
        );
      
      return EFI_ABORTED;
    }
  }

  if (Token->Buffer != NULL) {
    Start = MultU64x32 (Block - 1, Instance->BlkSize);

    if (Start + DataLen <= Token->BufferSize) {
      NetCopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen);

      //
      // Update the file size when received the last block
      //
      if (Instance->LastBlock == Block) {
        Token->BufferSize = Start + DataLen;
      }

    } else if (Instance->LastBlock != 0) {
      //
      // Don't save the data if the buffer is too small, return
      // EFI_BUFFER_TOO_SMALL if received the last packet. This
      // will give a accurate file length. 
      //
      Token->BufferSize = Start + DataLen;

      Mtftp4SendError (
        Instance, 
        EFI_MTFTP4_ERRORCODE_DISK_FULL, 
        "User provided memory block is too small"
        );

      return EFI_BUFFER_TOO_SMALL;
    }
  }

  return EFI_SUCCESS;
}

EFI_STATUS
Mtftp4RrqHandleData (
  IN  MTFTP4_PROTOCOL       *Instance,
  IN  EFI_MTFTP4_PACKET     *Packet,
  IN  UINT32                Len,
  IN  BOOLEAN               Multicast,
  OUT BOOLEAN               *Completed
  )
/*++

Routine Description:

  Function to process the received data packets. It will save the block
  then send back an ACK if it is active. 

Arguments:

  Instance  - The downloading MTFTP session
  Packet    - The packet received 
  Len       - The length of the packet
  Multicast - Whether this packet is multicast or unicast
  Completed - Return whether the download has completed

Returns:

  EFI_SUCCESS          - The data packet is successfully processed
  EFI_ABORTED          - The download is aborted by the user
  EFI_BUFFER_TOO_SMALL - The user provided buffer is too small

--*/
{
  EFI_STATUS                Status;
  UINT16                    BlockNum;
  INTN                      Expected;

  *Completed  = FALSE;
  BlockNum    = NTOHS (Packet->Data.Block);
  Expected    = Mtftp4GetNextBlockNum (&Instance->Blocks);

  ASSERT (Expected >= 0);

  //
  // If we are active and received an unexpected packet, retransmit
  // the last ACK then restart receiving. If we are passive, save 
  // the block.
  //
  if (Instance->Master && (Expected != BlockNum)) {
    Mtftp4Retransmit (Instance);
    return EFI_SUCCESS;
  }

  Status = Mtftp4RrqSaveBlock (Instance, Packet, Len);

  if (EFI_ERROR (Status)) {
    return Status;
  }

  //
  // Reset the passive client's timer whenever it received a 
  // valid data packet. 
  //
  if (!Instance->Master) {
    Mtftp4SetTimeout (Instance);
  }
  
  //
  // Check whether we have received all the blocks. Send the ACK if we
  // are active (unicast client or master client for multicast download).
  // If we have received all the blocks, send an ACK even if we are passive
  // to tell the server that we are done.
  //
  Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);

  if (Instance->Master || (Expected < 0)) {  
    if (Expected < 0) {
      //
      // If we are passive client, then the just received Block maybe 
      // isn't the last block. We need to send an ACK to the last block
      // to inform the server that we are done. If we are active client,
      // the Block == Instance->LastBlock.
      //
      BlockNum   = Instance->LastBlock;
      *Completed = TRUE;
      
    } else {
      BlockNum = (UINT16) (Expected - 1);
    }
    
    Mtftp4RrqSendAck (Instance, BlockNum);
  }

  return EFI_SUCCESS;
}

BOOLEAN
Mtftp4RrqOackValid (
  IN MTFTP4_PROTOCOL        *This,
  IN MTFTP4_OPTION          *Reply,
  IN MTFTP4_OPTION          *Request
  )
/*++

Routine Description:

  Validate whether the options received in the server's OACK packet is valid.
  The options are valid only if:
    1. The server doesn't include options not requested by us
    2. The server can only use smaller blksize than that is requested
    3. The server can only use the same timeout as requested
    4. The server doesn't change its multicast channel.
    

Arguments:

  This    - The downloading Mtftp session
  Reply   - The options in the OACK packet
  Request - The requested options

Returns:

  TRUE if the options in the OACK is OK, otherwise FALSE.

--*/
{

  //
  // It is invalid for server to return options we don't request
  //
  if ((Reply->Exist &~Request->Exist) != 0) {
    return FALSE;
  }
  
  //
  // Server can only specify a smaller block size to be used and
  // return the timeout matches that requested.
  //
  if (((Reply->Exist & MTFTP4_BLKSIZE_EXIST) && (Reply->BlkSize > Request->BlkSize)) ||
      ((Reply->Exist & MTFTP4_TIMEOUT_EXIST) && (Reply->Timeout != Request->Timeout))) {
    return FALSE;
  }

  //
  // The server can send ",,master" to client to change its master
  // setting. But if it use the specific multicast channel, it can't 
  // change the setting.
  //
  if ((Reply->Exist & MTFTP4_MCAST_EXIST) && (This->McastIp != 0)) {
    if ((Reply->McastIp != 0) && (Reply->McastIp != This->McastIp)) {
      return FALSE;
    } 

    if ((Reply->McastPort != 0) && (Reply->McastPort != This->McastPort)) {
      return FALSE;
    }
  }

  return TRUE;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?