2 Functions implementation related with Mtftp for UefiPxeBc Driver.
4 Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "PxeBcImpl.h"
12 CHAR8
*mMtftpOptions
[PXE_MTFTP_OPTION_MAXIMUM_INDEX
] = {
21 This is a callback function when packets are received or transmitted in Mtftp driver.
23 A callback function that is provided by the caller to intercept
24 the EFI_MTFTP6_OPCODE_DATA or EFI_MTFTP6_OPCODE_DATA8 packets processed in the
25 EFI_MTFTP6_PROTOCOL.ReadFile() function, and alternatively to intercept
26 EFI_MTFTP6_OPCODE_OACK or EFI_MTFTP6_OPCODE_ERROR packets during a call to
27 EFI_MTFTP6_PROTOCOL.ReadFile(), WriteFile() or ReadDirectory().
29 @param[in] This Pointer to EFI_MTFTP6_PROTOCOL.
30 @param[in] Token Pointer to EFI_MTFTP6_TOKEN.
31 @param[in] PacketLen Length of EFI_MTFTP6_PACKET.
32 @param[in] Packet Pointer to EFI_MTFTP6_PACKET to be checked.
34 @retval EFI_SUCCESS The current operation succeeded.
35 @retval EFI_ABORTED Abort the current transfer process.
40 PxeBcMtftp6CheckPacket (
41 IN EFI_MTFTP6_PROTOCOL
*This
,
42 IN EFI_MTFTP6_TOKEN
*Token
,
44 IN EFI_MTFTP6_PACKET
*Packet
47 PXEBC_PRIVATE_DATA
*Private
;
48 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
*Callback
;
51 Private
= (PXEBC_PRIVATE_DATA
*)Token
->Context
;
52 Callback
= Private
->PxeBcCallback
;
55 if (Packet
->OpCode
== EFI_MTFTP6_OPCODE_ERROR
) {
57 // Store the tftp error message into mode data and set the received flag.
59 Private
->Mode
.TftpErrorReceived
= TRUE
;
60 Private
->Mode
.TftpError
.ErrorCode
= (UINT8
)Packet
->Error
.ErrorCode
;
62 Private
->Mode
.TftpError
.ErrorString
,
63 PXE_MTFTP_ERROR_STRING_LENGTH
,
64 (CHAR8
*)Packet
->Error
.ErrorMessage
,
65 PXE_MTFTP_ERROR_STRING_LENGTH
- 1
67 Private
->Mode
.TftpError
.ErrorString
[PXE_MTFTP_ERROR_STRING_LENGTH
- 1] = '\0';
70 if (Callback
!= NULL
) {
72 // Callback to user if has when received any tftp packet.
74 Status
= Callback
->Callback (
79 (EFI_PXE_BASE_CODE_PACKET
*)Packet
81 if (Status
!= EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
) {
83 // User wants to abort current process if not EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE.
88 // User wants to continue current process if EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE.
98 This function is to get the size of a file using Tftp.
100 @param[in] Private Pointer to PxeBc private data.
101 @param[in] Config Pointer to EFI_MTFTP6_CONFIG_DATA.
102 @param[in] Filename Pointer to boot file name.
103 @param[in] BlockSize Pointer to required block size.
104 @param[in] WindowSize Pointer to required window size.
105 @param[in, out] BufferSize Pointer to buffer size.
107 @retval EFI_SUCCESS Successfully obtained the size of file.
108 @retval EFI_NOT_FOUND Parse the tftp options failed.
109 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
110 @retval Others Has not obtained the size of the file.
114 PxeBcMtftp6GetFileSize (
115 IN PXEBC_PRIVATE_DATA
*Private
,
116 IN EFI_MTFTP6_CONFIG_DATA
*Config
,
119 IN UINTN
*WindowSize
,
120 IN OUT UINT64
*BufferSize
123 EFI_MTFTP6_PROTOCOL
*Mtftp6
;
124 EFI_MTFTP6_OPTION ReqOpt
[3];
125 EFI_MTFTP6_PACKET
*Packet
;
126 EFI_MTFTP6_OPTION
*Option
;
128 UINT8 OptBuf
[PXE_MTFTP_OPTBUF_MAXNUM_INDEX
];
134 Status
= EFI_DEVICE_ERROR
;
135 Mtftp6
= Private
->Mtftp6
;
139 OptBufSize
= PXE_MTFTP_OPTBUF_MAXNUM_INDEX
;
141 Config
->InitialServerPort
= PXEBC_BS_DOWNLOAD_PORT
;
143 Status
= Mtftp6
->Configure (Mtftp6
, Config
);
144 if (EFI_ERROR (Status
)) {
149 // Build the required options for get info.
151 ReqOpt
[0].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_TSIZE_INDEX
];
152 PxeBcUintnToAscDec (0, OptBuf
, OptBufSize
);
153 ReqOpt
[0].ValueStr
= OptBuf
;
155 if (BlockSize
!= NULL
) {
156 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_BLKSIZE_INDEX
];
157 ReqOpt
[OptCnt
].ValueStr
= (UINT8
*)(ReqOpt
[OptCnt
-1].ValueStr
+ AsciiStrLen ((CHAR8
*)ReqOpt
[OptCnt
-1].ValueStr
) + 1);
158 OptBufSize
-= (AsciiStrLen ((CHAR8
*)ReqOpt
[OptCnt
-1].ValueStr
) + 1);
159 PxeBcUintnToAscDec (*BlockSize
, ReqOpt
[OptCnt
].ValueStr
, OptBufSize
);
163 if (WindowSize
!= NULL
) {
164 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX
];
165 ReqOpt
[OptCnt
].ValueStr
= (UINT8
*)(ReqOpt
[OptCnt
-1].ValueStr
+ AsciiStrLen ((CHAR8
*)ReqOpt
[OptCnt
-1].ValueStr
) + 1);
166 OptBufSize
-= (AsciiStrLen ((CHAR8
*)ReqOpt
[OptCnt
-1].ValueStr
) + 1);
167 PxeBcUintnToAscDec (*WindowSize
, ReqOpt
[OptCnt
].ValueStr
, OptBufSize
);
171 Status
= Mtftp6
->GetInfo (
181 if (EFI_ERROR (Status
)) {
182 if (Status
== EFI_TFTP_ERROR
) {
184 // Store the tftp error message into mode data and set the received flag.
186 Private
->Mode
.TftpErrorReceived
= TRUE
;
187 Private
->Mode
.TftpError
.ErrorCode
= (UINT8
)Packet
->Error
.ErrorCode
;
189 Private
->Mode
.TftpError
.ErrorString
,
190 PXE_MTFTP_ERROR_STRING_LENGTH
,
191 (CHAR8
*)Packet
->Error
.ErrorMessage
,
192 PXE_MTFTP_ERROR_STRING_LENGTH
- 1
194 Private
->Mode
.TftpError
.ErrorString
[PXE_MTFTP_ERROR_STRING_LENGTH
- 1] = '\0';
201 // Parse the options in the reply packet.
204 Status
= Mtftp6
->ParseOptions (
211 if (EFI_ERROR (Status
)) {
216 // Parse out the value of "tsize" option.
218 Status
= EFI_NOT_FOUND
;
219 while (OptCnt
!= 0) {
220 if (AsciiStrnCmp ((CHAR8
*)Option
[OptCnt
- 1].OptionStr
, "tsize", 5) == 0) {
221 *BufferSize
= AsciiStrDecimalToUint64 ((CHAR8
*)(Option
[OptCnt
- 1].ValueStr
));
222 Status
= EFI_SUCCESS
;
231 if (Packet
!= NULL
) {
235 Mtftp6
->Configure (Mtftp6
, NULL
);
241 This function is to get data of a file using Tftp.
243 @param[in] Private Pointer to PxeBc private data.
244 @param[in] Config Pointer to EFI_MTFTP6_CONFIG_DATA.
245 @param[in] Filename Pointer to boot file name.
246 @param[in] BlockSize Pointer to required block size.
247 @param[in] WindowSize Pointer to required window size.
248 @param[in] BufferPtr Pointer to buffer.
249 @param[in, out] BufferSize Pointer to buffer size.
250 @param[in] DontUseBuffer Indicates whether with a receive buffer.
252 @retval EFI_SUCCESS Successfully read the data from the special file.
253 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
254 @retval Others Read data from file failed.
258 PxeBcMtftp6ReadFile (
259 IN PXEBC_PRIVATE_DATA
*Private
,
260 IN EFI_MTFTP6_CONFIG_DATA
*Config
,
263 IN UINTN
*WindowSize
,
265 IN OUT UINT64
*BufferSize
,
266 IN BOOLEAN DontUseBuffer
269 EFI_MTFTP6_PROTOCOL
*Mtftp6
;
270 EFI_MTFTP6_TOKEN Token
;
271 EFI_MTFTP6_OPTION ReqOpt
[2];
273 UINT8 BlksizeBuf
[10];
274 UINT8 WindowsizeBuf
[10];
277 Status
= EFI_DEVICE_ERROR
;
278 Mtftp6
= Private
->Mtftp6
;
280 Config
->InitialServerPort
= PXEBC_BS_DOWNLOAD_PORT
;
282 Status
= Mtftp6
->Configure (Mtftp6
, Config
);
283 if (EFI_ERROR (Status
)) {
287 if (BlockSize
!= NULL
) {
288 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_BLKSIZE_INDEX
];
289 ReqOpt
[OptCnt
].ValueStr
= BlksizeBuf
;
290 PxeBcUintnToAscDec (*BlockSize
, ReqOpt
[OptCnt
].ValueStr
, sizeof (BlksizeBuf
));
294 if (WindowSize
!= NULL
) {
295 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX
];
296 ReqOpt
[OptCnt
].ValueStr
= WindowsizeBuf
;
297 PxeBcUintnToAscDec (*WindowSize
, ReqOpt
[OptCnt
].ValueStr
, sizeof (WindowsizeBuf
));
302 Token
.OverrideData
= NULL
;
303 Token
.Filename
= Filename
;
304 Token
.ModeStr
= NULL
;
305 Token
.OptionCount
= OptCnt
;
306 Token
.OptionList
= ReqOpt
;
307 Token
.Context
= Private
;
310 Token
.BufferSize
= 0;
313 Token
.BufferSize
= *BufferSize
;
314 Token
.Buffer
= BufferPtr
;
317 Token
.CheckPacket
= PxeBcMtftp6CheckPacket
;
318 Token
.TimeoutCallback
= NULL
;
319 Token
.PacketNeeded
= NULL
;
321 Status
= Mtftp6
->ReadFile (Mtftp6
, &Token
);
323 // Get the real size of received buffer.
325 *BufferSize
= Token
.BufferSize
;
327 Mtftp6
->Configure (Mtftp6
, NULL
);
333 This function is used to write the data of a file using Tftp.
335 @param[in] Private Pointer to PxeBc private data.
336 @param[in] Config Pointer to EFI_MTFTP6_CONFIG_DATA.
337 @param[in] Filename Pointer to boot file name.
338 @param[in] Overwrite Indicate whether with overwrite attribute.
339 @param[in] BlockSize Pointer to required block size.
340 @param[in] BufferPtr Pointer to buffer.
341 @param[in, out] BufferSize Pointer to buffer size.
343 @retval EFI_SUCCESS Successfully wrote the data into a special file.
344 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
345 @retval other Write data into file failed.
349 PxeBcMtftp6WriteFile (
350 IN PXEBC_PRIVATE_DATA
*Private
,
351 IN EFI_MTFTP6_CONFIG_DATA
*Config
,
353 IN BOOLEAN Overwrite
,
356 IN OUT UINT64
*BufferSize
359 EFI_MTFTP6_PROTOCOL
*Mtftp6
;
360 EFI_MTFTP6_TOKEN Token
;
361 EFI_MTFTP6_OPTION ReqOpt
[1];
366 Status
= EFI_DEVICE_ERROR
;
367 Mtftp6
= Private
->Mtftp6
;
369 Config
->InitialServerPort
= PXEBC_BS_DOWNLOAD_PORT
;
371 Status
= Mtftp6
->Configure (Mtftp6
, Config
);
372 if (EFI_ERROR (Status
)) {
376 if (BlockSize
!= NULL
) {
377 ReqOpt
[0].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_BLKSIZE_INDEX
];
378 ReqOpt
[0].ValueStr
= OptBuf
;
379 PxeBcUintnToAscDec (*BlockSize
, ReqOpt
[0].ValueStr
, PXE_MTFTP_OPTBUF_MAXNUM_INDEX
);
384 Token
.OverrideData
= NULL
;
385 Token
.Filename
= Filename
;
386 Token
.ModeStr
= NULL
;
387 Token
.OptionCount
= OptCnt
;
388 Token
.OptionList
= ReqOpt
;
389 Token
.BufferSize
= *BufferSize
;
390 Token
.Buffer
= BufferPtr
;
391 Token
.CheckPacket
= PxeBcMtftp6CheckPacket
;
392 Token
.TimeoutCallback
= NULL
;
393 Token
.PacketNeeded
= NULL
;
395 Status
= Mtftp6
->WriteFile (Mtftp6
, &Token
);
397 // Get the real size of transmitted buffer.
399 *BufferSize
= Token
.BufferSize
;
401 Mtftp6
->Configure (Mtftp6
, NULL
);
407 This function is to read the data (file) from a directory using Tftp.
409 @param[in] Private Pointer to PxeBc private data.
410 @param[in] Config Pointer to EFI_MTFTP6_CONFIG_DATA.
411 @param[in] Filename Pointer to boot file name.
412 @param[in] BlockSize Pointer to required block size.
413 @param[in] WindowSize Pointer to required window size.
414 @param[in] BufferPtr Pointer to buffer.
415 @param[in, out] BufferSize Pointer to buffer size.
416 @param[in] DontUseBuffer Indicates whether to use a receive buffer.
418 @retval EFI_SUCCESS Successfully obtained the data from the file included in directory.
419 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
420 @retval Others Operation failed.
424 PxeBcMtftp6ReadDirectory (
425 IN PXEBC_PRIVATE_DATA
*Private
,
426 IN EFI_MTFTP6_CONFIG_DATA
*Config
,
429 IN UINTN
*WindowSize
,
431 IN OUT UINT64
*BufferSize
,
432 IN BOOLEAN DontUseBuffer
435 EFI_MTFTP6_PROTOCOL
*Mtftp6
;
436 EFI_MTFTP6_TOKEN Token
;
437 EFI_MTFTP6_OPTION ReqOpt
[2];
439 UINT8 BlksizeBuf
[10];
440 UINT8 WindowsizeBuf
[10];
443 Status
= EFI_DEVICE_ERROR
;
444 Mtftp6
= Private
->Mtftp6
;
446 Config
->InitialServerPort
= PXEBC_BS_DOWNLOAD_PORT
;
448 Status
= Mtftp6
->Configure (Mtftp6
, Config
);
449 if (EFI_ERROR (Status
)) {
453 if (BlockSize
!= NULL
) {
454 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_BLKSIZE_INDEX
];
455 ReqOpt
[OptCnt
].ValueStr
= BlksizeBuf
;
456 PxeBcUintnToAscDec (*BlockSize
, ReqOpt
[OptCnt
].ValueStr
, sizeof (BlksizeBuf
));
460 if (WindowSize
!= NULL
) {
461 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX
];
462 ReqOpt
[OptCnt
].ValueStr
= WindowsizeBuf
;
463 PxeBcUintnToAscDec (*WindowSize
, ReqOpt
[OptCnt
].ValueStr
, sizeof (WindowsizeBuf
));
468 Token
.OverrideData
= NULL
;
469 Token
.Filename
= Filename
;
470 Token
.ModeStr
= NULL
;
471 Token
.OptionCount
= OptCnt
;
472 Token
.OptionList
= ReqOpt
;
473 Token
.Context
= Private
;
476 Token
.BufferSize
= 0;
479 Token
.BufferSize
= *BufferSize
;
480 Token
.Buffer
= BufferPtr
;
483 Token
.CheckPacket
= PxeBcMtftp6CheckPacket
;
484 Token
.TimeoutCallback
= NULL
;
485 Token
.PacketNeeded
= NULL
;
487 Status
= Mtftp6
->ReadDirectory (Mtftp6
, &Token
);
489 // Get the real size of received buffer.
491 *BufferSize
= Token
.BufferSize
;
493 Mtftp6
->Configure (Mtftp6
, NULL
);
499 This is a callback function when packets are received or transmitted in Mtftp driver.
501 A callback function that is provided by the caller to intercept
502 the EFI_MTFTP6_OPCODE_DATA or EFI_MTFTP4_OPCODE_DATA8 packets processed in the
503 EFI_MTFTP4_PROTOCOL.ReadFile() function, and alternatively to intercept
504 EFI_MTFTP4_OPCODE_OACK or EFI_MTFTP4_OPCODE_ERROR packets during a call to
505 EFI_MTFTP4_PROTOCOL.ReadFile(), WriteFile() or ReadDirectory().
507 @param[in] This Pointer to EFI_MTFTP4_PROTOCOL.
508 @param[in] Token Pointer to EFI_MTFTP4_TOKEN.
509 @param[in] PacketLen Length of EFI_MTFTP4_PACKET.
510 @param[in] Packet Pointer to EFI_MTFTP4_PACKET to be checked.
512 @retval EFI_SUCCESS The current operation succeeded.
513 @retval EFI_ABORTED Abort the current transfer process.
518 PxeBcMtftp4CheckPacket (
519 IN EFI_MTFTP4_PROTOCOL
*This
,
520 IN EFI_MTFTP4_TOKEN
*Token
,
522 IN EFI_MTFTP4_PACKET
*Packet
525 PXEBC_PRIVATE_DATA
*Private
;
526 EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL
*Callback
;
529 Private
= (PXEBC_PRIVATE_DATA
*)Token
->Context
;
530 Callback
= Private
->PxeBcCallback
;
531 Status
= EFI_SUCCESS
;
533 if (Packet
->OpCode
== EFI_MTFTP4_OPCODE_ERROR
) {
535 // Store the tftp error message into mode data and set the received flag.
537 Private
->Mode
.TftpErrorReceived
= TRUE
;
538 Private
->Mode
.TftpError
.ErrorCode
= (UINT8
)Packet
->Error
.ErrorCode
;
540 Private
->Mode
.TftpError
.ErrorString
,
541 PXE_MTFTP_ERROR_STRING_LENGTH
,
542 (CHAR8
*)Packet
->Error
.ErrorMessage
,
543 PXE_MTFTP_ERROR_STRING_LENGTH
- 1
545 Private
->Mode
.TftpError
.ErrorString
[PXE_MTFTP_ERROR_STRING_LENGTH
- 1] = '\0';
548 if (Callback
!= NULL
) {
550 // Callback to user if has when received any tftp packet.
552 Status
= Callback
->Callback (
557 (EFI_PXE_BASE_CODE_PACKET
*)Packet
559 if (Status
!= EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE
) {
561 // User wants to abort current process if not EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE.
563 Status
= EFI_ABORTED
;
566 // User wants to continue current process if EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE.
568 Status
= EFI_SUCCESS
;
576 This function is to get size of a file using Tftp.
578 @param[in] Private Pointer to PxeBc private data.
579 @param[in] Config Pointer to EFI_MTFTP4_CONFIG_DATA.
580 @param[in] Filename Pointer to boot file name.
581 @param[in] BlockSize Pointer to required block size.
582 @param[in] WindowSize Pointer to required window size.
583 @param[in, out] BufferSize Pointer to buffer size.
585 @retval EFI_SUCCESS Successfully obtained the size of file.
586 @retval EFI_NOT_FOUND Parse the tftp options failed.
587 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
588 @retval Others Did not obtain the size of the file.
592 PxeBcMtftp4GetFileSize (
593 IN PXEBC_PRIVATE_DATA
*Private
,
594 IN EFI_MTFTP4_CONFIG_DATA
*Config
,
597 IN UINTN
*WindowSize
,
598 IN OUT UINT64
*BufferSize
601 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
602 EFI_MTFTP4_OPTION ReqOpt
[3];
603 EFI_MTFTP4_PACKET
*Packet
;
604 EFI_MTFTP4_OPTION
*Option
;
606 UINT8 OptBuf
[PXE_MTFTP_OPTBUF_MAXNUM_INDEX
];
612 Status
= EFI_DEVICE_ERROR
;
613 Mtftp4
= Private
->Mtftp4
;
617 OptBufSize
= PXE_MTFTP_OPTBUF_MAXNUM_INDEX
;
619 Config
->InitialServerPort
= PXEBC_BS_DOWNLOAD_PORT
;
621 Status
= Mtftp4
->Configure (Mtftp4
, Config
);
622 if (EFI_ERROR (Status
)) {
627 // Build the required options for get info.
629 ReqOpt
[0].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_TSIZE_INDEX
];
630 PxeBcUintnToAscDec (0, OptBuf
, OptBufSize
);
631 ReqOpt
[0].ValueStr
= OptBuf
;
633 if (BlockSize
!= NULL
) {
634 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_BLKSIZE_INDEX
];
635 ReqOpt
[OptCnt
].ValueStr
= (UINT8
*)(ReqOpt
[OptCnt
-1].ValueStr
+ AsciiStrLen ((CHAR8
*)ReqOpt
[OptCnt
-1].ValueStr
) + 1);
636 OptBufSize
-= (AsciiStrLen ((CHAR8
*)ReqOpt
[OptCnt
-1].ValueStr
) + 1);
637 PxeBcUintnToAscDec (*BlockSize
, ReqOpt
[OptCnt
].ValueStr
, OptBufSize
);
641 if (WindowSize
!= NULL
) {
642 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX
];
643 ReqOpt
[OptCnt
].ValueStr
= (UINT8
*)(ReqOpt
[OptCnt
-1].ValueStr
+ AsciiStrLen ((CHAR8
*)ReqOpt
[OptCnt
-1].ValueStr
) + 1);
644 OptBufSize
-= (AsciiStrLen ((CHAR8
*)ReqOpt
[OptCnt
-1].ValueStr
) + 1);
645 PxeBcUintnToAscDec (*WindowSize
, ReqOpt
[OptCnt
].ValueStr
, OptBufSize
);
649 Status
= Mtftp4
->GetInfo (
659 if (EFI_ERROR (Status
)) {
660 if (Status
== EFI_TFTP_ERROR
) {
662 // Store the tftp error message into mode data and set the received flag.
664 Private
->Mode
.TftpErrorReceived
= TRUE
;
665 Private
->Mode
.TftpError
.ErrorCode
= (UINT8
)Packet
->Error
.ErrorCode
;
667 Private
->Mode
.TftpError
.ErrorString
,
668 PXE_MTFTP_ERROR_STRING_LENGTH
,
669 (CHAR8
*)Packet
->Error
.ErrorMessage
,
670 PXE_MTFTP_ERROR_STRING_LENGTH
- 1
672 Private
->Mode
.TftpError
.ErrorString
[PXE_MTFTP_ERROR_STRING_LENGTH
- 1] = '\0';
679 // Parse the options in the reply packet.
682 Status
= Mtftp4
->ParseOptions (
689 if (EFI_ERROR (Status
)) {
694 // Parse out the value of "tsize" option.
696 Status
= EFI_NOT_FOUND
;
697 while (OptCnt
!= 0) {
698 if (AsciiStrnCmp ((CHAR8
*)Option
[OptCnt
- 1].OptionStr
, "tsize", 5) == 0) {
699 *BufferSize
= AsciiStrDecimalToUint64 ((CHAR8
*)(Option
[OptCnt
- 1].ValueStr
));
700 Status
= EFI_SUCCESS
;
709 if (Packet
!= NULL
) {
713 Mtftp4
->Configure (Mtftp4
, NULL
);
719 This function is to read the data of a file using Tftp.
721 @param[in] Private Pointer to PxeBc private data.
722 @param[in] Config Pointer to EFI_MTFTP4_CONFIG_DATA.
723 @param[in] Filename Pointer to boot file name.
724 @param[in] BlockSize Pointer to required block size.
725 @param[in] WindowSize Pointer to required window size.
726 @param[in] BufferPtr Pointer to buffer.
727 @param[in, out] BufferSize Pointer to buffer size.
728 @param[in] DontUseBuffer Indicates whether to use a receive buffer.
730 @retval EFI_SUCCESS Successfully read the data from the special file.
731 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
732 @retval Others Read data from file failed.
736 PxeBcMtftp4ReadFile (
737 IN PXEBC_PRIVATE_DATA
*Private
,
738 IN EFI_MTFTP4_CONFIG_DATA
*Config
,
741 IN UINTN
*WindowSize
,
743 IN OUT UINT64
*BufferSize
,
744 IN BOOLEAN DontUseBuffer
747 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
748 EFI_MTFTP4_TOKEN Token
;
749 EFI_MTFTP4_OPTION ReqOpt
[2];
751 UINT8 BlksizeBuf
[10];
752 UINT8 WindowsizeBuf
[10];
755 Status
= EFI_DEVICE_ERROR
;
756 Mtftp4
= Private
->Mtftp4
;
758 Config
->InitialServerPort
= PXEBC_BS_DOWNLOAD_PORT
;
760 Status
= Mtftp4
->Configure (Mtftp4
, Config
);
761 if (EFI_ERROR (Status
)) {
765 if (BlockSize
!= NULL
) {
766 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_BLKSIZE_INDEX
];
767 ReqOpt
[OptCnt
].ValueStr
= BlksizeBuf
;
768 PxeBcUintnToAscDec (*BlockSize
, ReqOpt
[OptCnt
].ValueStr
, sizeof (BlksizeBuf
));
772 if (WindowSize
!= NULL
) {
773 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX
];
774 ReqOpt
[OptCnt
].ValueStr
= WindowsizeBuf
;
775 PxeBcUintnToAscDec (*WindowSize
, ReqOpt
[OptCnt
].ValueStr
, sizeof (WindowsizeBuf
));
780 Token
.OverrideData
= NULL
;
781 Token
.Filename
= Filename
;
782 Token
.ModeStr
= NULL
;
783 Token
.OptionCount
= OptCnt
;
784 Token
.OptionList
= ReqOpt
;
785 Token
.Context
= Private
;
788 Token
.BufferSize
= 0;
791 Token
.BufferSize
= *BufferSize
;
792 Token
.Buffer
= BufferPtr
;
795 Token
.CheckPacket
= PxeBcMtftp4CheckPacket
;
796 Token
.TimeoutCallback
= NULL
;
797 Token
.PacketNeeded
= NULL
;
799 Status
= Mtftp4
->ReadFile (Mtftp4
, &Token
);
801 // Get the real size of received buffer.
803 *BufferSize
= Token
.BufferSize
;
805 Mtftp4
->Configure (Mtftp4
, NULL
);
811 This function is to write the data of a file using Tftp.
813 @param[in] Private Pointer to PxeBc private data.
814 @param[in] Config Pointer to EFI_MTFTP4_CONFIG_DATA.
815 @param[in] Filename Pointer to boot file name.
816 @param[in] Overwrite Indicates whether to use the overwrite attribute.
817 @param[in] BlockSize Pointer to required block size.
818 @param[in] BufferPtr Pointer to buffer.
819 @param[in, out] BufferSize Pointer to buffer size.
821 @retval EFI_SUCCESS Successfully write the data into the special file.
822 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
823 @retval other Write data into file failed.
827 PxeBcMtftp4WriteFile (
828 IN PXEBC_PRIVATE_DATA
*Private
,
829 IN EFI_MTFTP4_CONFIG_DATA
*Config
,
831 IN BOOLEAN Overwrite
,
834 IN OUT UINT64
*BufferSize
837 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
838 EFI_MTFTP4_TOKEN Token
;
839 EFI_MTFTP4_OPTION ReqOpt
[1];
844 Status
= EFI_DEVICE_ERROR
;
845 Mtftp4
= Private
->Mtftp4
;
847 Config
->InitialServerPort
= PXEBC_BS_DOWNLOAD_PORT
;
849 Status
= Mtftp4
->Configure (Mtftp4
, Config
);
850 if (EFI_ERROR (Status
)) {
854 if (BlockSize
!= NULL
) {
855 ReqOpt
[0].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_BLKSIZE_INDEX
];
856 ReqOpt
[0].ValueStr
= OptBuf
;
857 PxeBcUintnToAscDec (*BlockSize
, ReqOpt
[0].ValueStr
, PXE_MTFTP_OPTBUF_MAXNUM_INDEX
);
862 Token
.OverrideData
= NULL
;
863 Token
.Filename
= Filename
;
864 Token
.ModeStr
= NULL
;
865 Token
.OptionCount
= OptCnt
;
866 Token
.OptionList
= ReqOpt
;
867 Token
.BufferSize
= *BufferSize
;
868 Token
.Buffer
= BufferPtr
;
869 Token
.CheckPacket
= PxeBcMtftp4CheckPacket
;
870 Token
.TimeoutCallback
= NULL
;
871 Token
.PacketNeeded
= NULL
;
873 Status
= Mtftp4
->WriteFile (Mtftp4
, &Token
);
875 // Get the real size of transmitted buffer.
877 *BufferSize
= Token
.BufferSize
;
879 Mtftp4
->Configure (Mtftp4
, NULL
);
885 This function is to get data (file) from a directory using Tftp.
887 @param[in] Private Pointer to PxeBc private data.
888 @param[in] Config Pointer to EFI_MTFTP4_CONFIG_DATA.
889 @param[in] Filename Pointer to boot file name.
890 @param[in] BlockSize Pointer to required block size.
891 @param[in] WindowSize Pointer to required window size.
892 @param[in] BufferPtr Pointer to buffer.
893 @param[in, out] BufferSize Pointer to buffer size.
894 @param[in] DontUseBuffer Indicates whether to use a receive buffer.
896 @retval EFI_SUCCESS Successfully obtained the data from the file included in the directory.
897 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
898 @retval Others Operation failed.
902 PxeBcMtftp4ReadDirectory (
903 IN PXEBC_PRIVATE_DATA
*Private
,
904 IN EFI_MTFTP4_CONFIG_DATA
*Config
,
907 IN UINTN
*WindowSize
,
909 IN OUT UINT64
*BufferSize
,
910 IN BOOLEAN DontUseBuffer
913 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
914 EFI_MTFTP4_TOKEN Token
;
915 EFI_MTFTP4_OPTION ReqOpt
[2];
917 UINT8 BlksizeBuf
[10];
918 UINT8 WindowsizeBuf
[10];
921 Status
= EFI_DEVICE_ERROR
;
922 Mtftp4
= Private
->Mtftp4
;
924 Config
->InitialServerPort
= PXEBC_BS_DOWNLOAD_PORT
;
926 Status
= Mtftp4
->Configure (Mtftp4
, Config
);
927 if (EFI_ERROR (Status
)) {
931 if (BlockSize
!= NULL
) {
932 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_BLKSIZE_INDEX
];
933 ReqOpt
[OptCnt
].ValueStr
= BlksizeBuf
;
934 PxeBcUintnToAscDec (*BlockSize
, ReqOpt
[OptCnt
].ValueStr
, sizeof (BlksizeBuf
));
938 if (WindowSize
!= NULL
) {
939 ReqOpt
[OptCnt
].OptionStr
= (UINT8
*)mMtftpOptions
[PXE_MTFTP_OPTION_WINDOWSIZE_INDEX
];
940 ReqOpt
[OptCnt
].ValueStr
= WindowsizeBuf
;
941 PxeBcUintnToAscDec (*WindowSize
, ReqOpt
[OptCnt
].ValueStr
, sizeof (WindowsizeBuf
));
946 Token
.OverrideData
= NULL
;
947 Token
.Filename
= Filename
;
948 Token
.ModeStr
= NULL
;
949 Token
.OptionCount
= OptCnt
;
950 Token
.OptionList
= ReqOpt
;
951 Token
.Context
= Private
;
954 Token
.BufferSize
= 0;
957 Token
.BufferSize
= *BufferSize
;
958 Token
.Buffer
= BufferPtr
;
961 Token
.CheckPacket
= PxeBcMtftp4CheckPacket
;
962 Token
.TimeoutCallback
= NULL
;
963 Token
.PacketNeeded
= NULL
;
965 Status
= Mtftp4
->ReadDirectory (Mtftp4
, &Token
);
967 // Get the real size of received buffer.
969 *BufferSize
= Token
.BufferSize
;
971 Mtftp4
->Configure (Mtftp4
, NULL
);
977 This function is wrapper to get the file size using TFTP.
979 @param[in] Private Pointer to PxeBc private data.
980 @param[in] Config Pointer to configure data.
981 @param[in] Filename Pointer to boot file name.
982 @param[in] BlockSize Pointer to required block size.
983 @param[in] WindowSize Pointer to required window size.
984 @param[in, out] BufferSize Pointer to buffer size.
986 @retval EFI_SUCCESS Successfully obtained the size of file.
987 @retval EFI_NOT_FOUND Parse the tftp options failed.
988 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
989 @retval Others Did not obtain the size of the file.
993 PxeBcTftpGetFileSize (
994 IN PXEBC_PRIVATE_DATA
*Private
,
998 IN UINTN
*WindowSize
,
999 IN OUT UINT64
*BufferSize
1002 if (Private
->PxeBc
.Mode
->UsingIpv6
) {
1003 return PxeBcMtftp6GetFileSize (
1005 (EFI_MTFTP6_CONFIG_DATA
*)Config
,
1012 return PxeBcMtftp4GetFileSize (
1014 (EFI_MTFTP4_CONFIG_DATA
*)Config
,
1024 This function is a wrapper to get file using TFTP.
1026 @param[in] Private Pointer to PxeBc private data.
1027 @param[in] Config Pointer to config data.
1028 @param[in] Filename Pointer to boot file name.
1029 @param[in] BlockSize Pointer to required block size.
1030 @param[in] WindowSize Pointer to required window size.
1031 @param[in] BufferPtr Pointer to buffer.
1032 @param[in, out] BufferSize Pointer to buffer size.
1033 @param[in] DontUseBuffer Indicates whether to use a receive buffer.
1035 @retval EFI_SUCCESS Successfully read the data from the special file.
1036 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1037 @retval Others Read data from file failed.
1042 IN PXEBC_PRIVATE_DATA
*Private
,
1045 IN UINTN
*BlockSize
,
1046 IN UINTN
*WindowSize
,
1047 IN UINT8
*BufferPtr
,
1048 IN OUT UINT64
*BufferSize
,
1049 IN BOOLEAN DontUseBuffer
1052 if (Private
->PxeBc
.Mode
->UsingIpv6
) {
1053 return PxeBcMtftp6ReadFile (
1055 (EFI_MTFTP6_CONFIG_DATA
*)Config
,
1064 return PxeBcMtftp4ReadFile (
1066 (EFI_MTFTP4_CONFIG_DATA
*)Config
,
1078 This function is a wrapper to write file using TFTP.
1080 @param[in] Private Pointer to PxeBc private data.
1081 @param[in] Config Pointer to config data.
1082 @param[in] Filename Pointer to boot file name.
1083 @param[in] Overwrite Indicate whether with overwrite attribute.
1084 @param[in] BlockSize Pointer to required block size.
1085 @param[in] BufferPtr Pointer to buffer.
1086 @param[in, out] BufferSize Pointer to buffer size.
1088 @retval EFI_SUCCESS Successfully wrote the data into a special file.
1089 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1090 @retval other Write data into file failed.
1094 PxeBcTftpWriteFile (
1095 IN PXEBC_PRIVATE_DATA
*Private
,
1098 IN BOOLEAN Overwrite
,
1099 IN UINTN
*BlockSize
,
1100 IN UINT8
*BufferPtr
,
1101 IN OUT UINT64
*BufferSize
1104 if (Private
->PxeBc
.Mode
->UsingIpv6
) {
1105 return PxeBcMtftp6WriteFile (
1107 (EFI_MTFTP6_CONFIG_DATA
*)Config
,
1115 return PxeBcMtftp4WriteFile (
1117 (EFI_MTFTP4_CONFIG_DATA
*)Config
,
1128 This function is a wrapper to get the data (file) from a directory using TFTP.
1130 @param[in] Private Pointer to PxeBc private data.
1131 @param[in] Config Pointer to config data.
1132 @param[in] Filename Pointer to boot file name.
1133 @param[in] BlockSize Pointer to required block size.
1134 @param[in] WindowSize Pointer to required window size.
1135 @param[in] BufferPtr Pointer to buffer.
1136 @param[in, out] BufferSize Pointer to buffer size.
1137 @param[in] DontUseBuffer Indicatse whether to use a receive buffer.
1139 @retval EFI_SUCCESS Successfully obtained the data from the file included in the directory.
1140 @retval EFI_DEVICE_ERROR The network device encountered an error during this operation.
1141 @retval Others Operation failed.
1145 PxeBcTftpReadDirectory (
1146 IN PXEBC_PRIVATE_DATA
*Private
,
1149 IN UINTN
*BlockSize
,
1150 IN UINTN
*WindowSize
,
1151 IN UINT8
*BufferPtr
,
1152 IN OUT UINT64
*BufferSize
,
1153 IN BOOLEAN DontUseBuffer
1156 if (Private
->PxeBc
.Mode
->UsingIpv6
) {
1157 return PxeBcMtftp6ReadDirectory (
1159 (EFI_MTFTP6_CONFIG_DATA
*)Config
,
1168 return PxeBcMtftp4ReadDirectory (
1170 (EFI_MTFTP4_CONFIG_DATA
*)Config
,