/** Function to handle received ACK packet. If the ACK number matches the expected block number, and there are more data pending, send the next block. Otherwise tell the caller that we are done. @param Instance The MTFTP upload session @param Packet The MTFTP packet received @param Len The packet length @param Completed Return whether the upload has finished. @retval EFI_SUCCESS The ACK is successfully processed. @retval EFI_TFTP_ERROR The block number loops back. @retval Others Failed to transmit the next data packet. **/ EFI_STATUS Mtftp4WrqHandleAck ( IN MTFTP4_PROTOCOL *Instance, IN EFI_MTFTP4_PACKET *Packet, IN UINT32 Len, OUT BOOLEAN *Completed ) { UINT16 AckNum; INTN Expected; UINT64 TotalBlock; *Completed = FALSE; AckNum = NTOHS (Packet->Ack.Block[0]); Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); ASSERT (Expected >= 0); // // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp4WrqInput // restart receive. // if (Expected != AckNum) { return EFI_SUCCESS; } // // Remove the acked block number, if this is the last block number, // tell the Mtftp4WrqInput to finish the transfer. This is the last // block number if the block range are empty.. // Mtftp4RemoveBlockNum (&Instance->Blocks, AckNum, *Completed,&TotalBlock); Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); if (Expected < 0) { // // The block range is empty. It may either because the the last // block has been ACKed, or the sequence number just looped back, // that is, there is more than 0xffff blocks. // if (Instance->LastBlock == AckNum) { ASSERT (Instance->LastBlock >= 1); *Completed = TRUE; return EFI_SUCCESS; } else { Mtftp4SendError ( Instance, EFI_MTFTP4_ERRORCODE_REQUEST_DENIED, (UINT8 *) "Block number rolls back, not supported, try blksize option" ); return EFI_TFTP_ERROR; } } return Mtftp4WrqSendBlock (Instance, (UINT16) Expected); }
/** The input process routine for MTFTP upload. @param UdpPacket The received MTFTP packet. @param EndPoint The local/remote access point @param IoStatus The result of the packet receiving @param Context Opaque parameter for the callback, which is the MTFTP session. **/ VOID EFIAPI Mtftp4WrqInput ( IN NET_BUF *UdpPacket, IN UDP_END_POINT *EndPoint, IN EFI_STATUS IoStatus, IN VOID *Context ) { MTFTP4_PROTOCOL *Instance; EFI_MTFTP4_PACKET *Packet; BOOLEAN Completed; EFI_STATUS Status; UINT32 Len; UINT16 Opcode; Instance = (MTFTP4_PROTOCOL *) Context; NET_CHECK_SIGNATURE (Instance, MTFTP4_PROTOCOL_SIGNATURE); Completed = FALSE; Packet = NULL; Status = EFI_SUCCESS; if (EFI_ERROR (IoStatus)) { Status = IoStatus; goto ON_EXIT; } ASSERT (UdpPacket != NULL); if (UdpPacket->TotalSize < MTFTP4_OPCODE_LEN) { goto ON_EXIT; } // // Client send initial request to server's listening port. Server // will select a UDP port to communicate with the client. // if (EndPoint->RemotePort != Instance->ConnectedPort) { if (Instance->ConnectedPort != 0) { goto ON_EXIT; } else { Instance->ConnectedPort = EndPoint->RemotePort; } } // // Copy the MTFTP packet to a continuous buffer if it isn't already so. // Len = UdpPacket->TotalSize; if (UdpPacket->BlockOpNum > 1) { Packet = AllocatePool (Len); if (Packet == NULL) { Status = EFI_OUT_OF_RESOURCES; goto ON_EXIT; } NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet); } else { Packet = (EFI_MTFTP4_PACKET *) NetbufGetByte (UdpPacket, 0, NULL); ASSERT (Packet != NULL); } Opcode = NTOHS (Packet->OpCode); // // Call the user's CheckPacket if provided. Abort the transmission // if CheckPacket returns an EFI_ERROR code. // if ((Instance->Token->CheckPacket != NULL) && ((Opcode == EFI_MTFTP4_OPCODE_OACK) || (Opcode == EFI_MTFTP4_OPCODE_ERROR))) { Status = Instance->Token->CheckPacket ( &Instance->Mtftp4, Instance->Token, (UINT16) Len, Packet ); if (EFI_ERROR (Status)) { // // Send an error message to the server to inform it // if (Opcode != EFI_MTFTP4_OPCODE_ERROR) { Mtftp4SendError ( Instance, EFI_MTFTP4_ERRORCODE_REQUEST_DENIED, (UINT8 *) "User aborted the transfer" ); } Status = EFI_ABORTED; goto ON_EXIT; } } switch (Opcode) { case EFI_MTFTP4_OPCODE_ACK: if (Len != MTFTP4_OPCODE_LEN + MTFTP4_BLKNO_LEN) { goto ON_EXIT; } Status = Mtftp4WrqHandleAck (Instance, Packet, Len, &Completed); break; case EFI_MTFTP4_OPCODE_OACK: if (Len <= MTFTP4_OPCODE_LEN) { goto ON_EXIT; } Status = Mtftp4WrqHandleOack (Instance, Packet, Len, &Completed); break; case EFI_MTFTP4_OPCODE_ERROR: Status = EFI_TFTP_ERROR; break; default: break; } ON_EXIT: // // Free the resources, then if !EFI_ERROR (Status) and not completed, // restart the receive, otherwise end the session. // if ((Packet != NULL) && (UdpPacket->BlockOpNum > 1)) { FreePool (Packet); } if (UdpPacket != NULL) { NetbufFree (UdpPacket); } if (!EFI_ERROR (Status) && !Completed) { Status = UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0); } // // Status may have been updated by UdpIoRecvDatagram // if (EFI_ERROR (Status) || Completed) { Mtftp4CleanOperation (Instance, Status); } }
/** Build then send a MTFTP data packet for the MTFTP upload session. @param Instance The MTFTP upload session. @param BlockNum The block number to send. @retval EFI_OUT_OF_RESOURCES Failed to build the packet. @retval EFI_ABORTED The consumer of this child directs to abort the transmission by return an error through PacketNeeded. @retval EFI_SUCCESS The data is sent. **/ EFI_STATUS Mtftp4WrqSendBlock ( IN OUT MTFTP4_PROTOCOL *Instance, IN UINT16 BlockNum ) { EFI_MTFTP4_PACKET *Packet; EFI_MTFTP4_TOKEN *Token; NET_BUF *UdpPacket; EFI_STATUS Status; UINT16 DataLen; UINT8 *DataBuf; UINT64 Start; // // Allocate a buffer to hold the user data // UdpPacket = NetbufAlloc (Instance->BlkSize + MTFTP4_DATA_HEAD_LEN); if (UdpPacket == NULL) { return EFI_OUT_OF_RESOURCES; } Packet = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (UdpPacket, MTFTP4_DATA_HEAD_LEN, FALSE); ASSERT (Packet != NULL); Packet->Data.OpCode = HTONS (EFI_MTFTP4_OPCODE_DATA); Packet->Data.Block = HTONS (BlockNum); // // Read the block from either the buffer or PacketNeeded callback // Token = Instance->Token; DataLen = Instance->BlkSize; if (Token->Buffer != NULL) { Start = MultU64x32 (BlockNum - 1, Instance->BlkSize); if (Token->BufferSize < Start + Instance->BlkSize) { DataLen = (UINT16) (Token->BufferSize - Start); Instance->LastBlock = BlockNum; Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum); } if (DataLen > 0) { NetbufAllocSpace (UdpPacket, DataLen, FALSE); CopyMem (Packet->Data.Data, (UINT8 *) Token->Buffer + Start, DataLen); } } else { // // Get data from PacketNeeded // DataBuf = NULL; Status = Token->PacketNeeded ( &Instance->Mtftp4, Token, &DataLen, (VOID **) &DataBuf ); if (EFI_ERROR (Status) || (DataLen > Instance->BlkSize)) { if (DataBuf != NULL) { FreePool (DataBuf); } if (UdpPacket != NULL) { NetbufFree (UdpPacket); } Mtftp4SendError ( Instance, EFI_MTFTP4_ERRORCODE_REQUEST_DENIED, (UINT8 *) "User aborted the transfer" ); return EFI_ABORTED; } if (DataLen < Instance->BlkSize) { Instance->LastBlock = BlockNum; Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum); } if (DataLen > 0) { NetbufAllocSpace (UdpPacket, DataLen, FALSE); CopyMem (Packet->Data.Data, DataBuf, DataLen); FreePool (DataBuf); } } return Mtftp4SendPacket (Instance, UdpPacket); }
/** Function to handle the MTFTP OACK packet. It parses the packet's options, and update the internal states of the session. @param Instance The MTFTP session @param Packet The received OACK packet @param Len The length of the packet @param Completed Whether the transmisson has completed. NOT used by this function. @retval EFI_SUCCESS The OACK process is OK @retval EFI_TFTP_ERROR Some error occured, and the session reset. **/ EFI_STATUS Mtftp4WrqHandleOack ( IN OUT MTFTP4_PROTOCOL *Instance, IN EFI_MTFTP4_PACKET *Packet, IN UINT32 Len, OUT BOOLEAN *Completed ) { MTFTP4_OPTION Reply; EFI_MTFTP4_PACKET Bogus; EFI_STATUS Status; INTN Expected; *Completed = FALSE; // // Ignore the OACK if already started the upload // Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); if (Expected != 0) { return EFI_SUCCESS; } // // Parse and validate the options from server // ZeroMem (&Reply, sizeof (MTFTP4_OPTION)); Status = Mtftp4ParseOptionOack (Packet, Len, &Reply); if (EFI_ERROR (Status) || !Mtftp4WrqOackValid (&Reply, &Instance->RequestOption)) { // // Don't send a MTFTP error packet when out of resource, it can // only make it worse. // if (Status != EFI_OUT_OF_RESOURCES) { Mtftp4SendError ( Instance, EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION, (UINT8 *) "Mal-formated OACK packet" ); } return EFI_TFTP_ERROR; } if (Reply.BlkSize != 0) { Instance->BlkSize = Reply.BlkSize; } if (Reply.Timeout != 0) { Instance->Timeout = Reply.Timeout; } // // Build a bogus ACK0 packet then pass it to the Mtftp4WrqHandleAck, // which will start the transmission of the first data block. // Bogus.Ack.OpCode = HTONS (EFI_MTFTP4_OPCODE_ACK); Bogus.Ack.Block[0] = 0; Status = Mtftp4WrqHandleAck ( Instance, &Bogus, sizeof (EFI_MTFTP4_ACK_HEADER), Completed ); return Status; }
/** Function to process the OACK. It will first validate the OACK packet, then update the various negotiated parameters. @param Instance The download MTFTP session @param Packet The packet received @param Len The packet length @param Multicast Whether this packet is received as a multicast @param Completed Returns whether the download has completed. NOT used by this function. @retval EFI_DEVICE_ERROR Failed to create/start a multicast UDP child @retval EFI_TFTP_ERROR Some error happened during the process @retval EFI_SUCCESS The OACK is successfully processed. **/ EFI_STATUS Mtftp4RrqHandleOack ( IN OUT MTFTP4_PROTOCOL *Instance, IN EFI_MTFTP4_PACKET *Packet, IN UINT32 Len, IN BOOLEAN Multicast, OUT BOOLEAN *Completed ) { MTFTP4_OPTION Reply; EFI_STATUS Status; INTN Expected; EFI_UDP4_PROTOCOL *Udp4; *Completed = FALSE; // // If already started the master download, don't change the // setting. Master download always succeeds. // Expected = Mtftp4GetNextBlockNum (&Instance->Blocks); ASSERT (Expected != -1); if (Instance->Master && (Expected != 1)) { return EFI_SUCCESS; } // // Parse and validate the options from server // ZeroMem (&Reply, sizeof (MTFTP4_OPTION)); Status = Mtftp4ParseOptionOack (Packet, Len, &Reply); if (EFI_ERROR (Status) || !Mtftp4RrqOackValid (Instance, &Reply, &Instance->RequestOption)) { // // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES. // if (Status != EFI_OUT_OF_RESOURCES) { Mtftp4SendError ( Instance, EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION, (UINT8 *) "Mal-formated OACK packet" ); } return EFI_TFTP_ERROR; } if ((Reply.Exist & MTFTP4_MCAST_EXIST) != 0) { // // Save the multicast info. Always update the Master, only update the // multicast IP address, block size, timeoute at the first time. If IP // address is updated, create a UDP child to receive the multicast. // Instance->Master = Reply.Master; if (Instance->McastIp == 0) { if ((Reply.McastIp == 0) || (Reply.McastPort == 0)) { Mtftp4SendError ( Instance, EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION, (UINT8 *) "Illegal multicast setting" ); return EFI_TFTP_ERROR; } // // Create a UDP child then start receive the multicast from it. // Instance->McastIp = Reply.McastIp; Instance->McastPort = Reply.McastPort; if (Instance->McastUdpPort == NULL) { Instance->McastUdpPort = UdpIoCreateIo ( Instance->Service->Controller, Instance->Service->Image, Mtftp4RrqConfigMcastPort, UDP_IO_UDP4_VERSION, Instance ); if (Instance->McastUdpPort != NULL) { Status = gBS->OpenProtocol ( Instance->McastUdpPort->UdpHandle, &gEfiUdp4ProtocolGuid, (VOID **) &Udp4, Instance->Service->Image, Instance->Handle, EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER ); if (EFI_ERROR (Status)) { UdpIoFreeIo (Instance->McastUdpPort); Instance->McastUdpPort = NULL; return EFI_DEVICE_ERROR; } } } if (Instance->McastUdpPort == NULL) { return EFI_DEVICE_ERROR; } Status = UdpIoRecvDatagram (Instance->McastUdpPort, Mtftp4RrqInput, Instance, 0); if (EFI_ERROR (Status)) { Mtftp4SendError ( Instance, EFI_MTFTP4_ERRORCODE_ACCESS_VIOLATION, (UINT8 *) "Failed to create socket to receive multicast packet" ); return Status; } // // Update the parameters used. // if (Reply.BlkSize != 0) { Instance->BlkSize = Reply.BlkSize; } if (Reply.Timeout != 0) { Instance->Timeout = Reply.Timeout; } } } else { Instance->Master = TRUE; if (Reply.BlkSize != 0) { Instance->BlkSize = Reply.BlkSize; } if (Reply.Timeout != 0) { Instance->Timeout = Reply.Timeout; } } // // Send an ACK to (Expected - 1) which is 0 for unicast download, // or tell the server we want to receive the Expected block. // return Mtftp4RrqSendAck (Instance, (UINT16) (Expected - 1)); }
/** Deliver the received data block to the user, which can be saved in the user provide buffer or through the CheckPacket callback. @param Instance The Mtftp session @param Packet The received data packet @param Len The packet length @retval EFI_SUCCESS The data is saved successfully @retval EFI_ABORTED The user tells to abort by return an error through CheckPacket @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small and buffer length is updated to the actual buffer size needed. **/ EFI_STATUS Mtftp4RrqSaveBlock ( IN OUT MTFTP4_PROTOCOL *Instance, IN EFI_MTFTP4_PACKET *Packet, IN UINT32 Len ) { EFI_MTFTP4_TOKEN *Token; EFI_STATUS Status; UINT16 Block; UINT64 Start; UINT32 DataLen; UINT64 TotalBlock; BOOLEAN Completed; Completed = FALSE; 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) { Completed = TRUE; 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. // Note that : For bigger files, allowing the block counter to roll over // to accept transfers of unlimited size. So TotalBlock is memorised as // continuous block counter. // Status = Mtftp4RemoveBlockNum (&Instance->Blocks, Block, Completed, &TotalBlock); 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, (UINT8 *) "User aborted download" ); return EFI_ABORTED; } } if (Token->Buffer != NULL) { Start = MultU64x32 (TotalBlock - 1, Instance->BlkSize); if (Start + DataLen <= Token->BufferSize) { CopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen); // // Update the file size when received the last block // if ((Instance->LastBlock == Block) && Completed) { 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, (UINT8 *) "User provided memory block is too small" ); return EFI_BUFFER_TOO_SMALL; } } return EFI_SUCCESS; }