]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Impl.c
Fix various 'EFIAPI' inconsistencies found while building MdeModulePkg.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Mtftp4Dxe / Mtftp4Impl.c
1 /** @file
2 Interface routine for Mtftp4.
3
4 Copyright (c) 2006 - 2007, 2009, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php<BR>
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "Mtftp4Impl.h"
17
18
19 /**
20 Clean up the MTFTP session to get ready for new operation.
21
22 @param Instance The MTFTP session to clean up
23 @param Result The result to return to the caller who initiated
24 the operation.
25 **/
26 VOID
27 Mtftp4CleanOperation (
28 IN OUT MTFTP4_PROTOCOL *Instance,
29 IN EFI_STATUS Result
30 )
31 {
32 LIST_ENTRY *Entry;
33 LIST_ENTRY *Next;
34 MTFTP4_BLOCK_RANGE *Block;
35 EFI_MTFTP4_TOKEN *Token;
36
37 //
38 // Free various resources.
39 //
40 Token = Instance->Token;
41
42 if (Token != NULL) {
43 Token->Status = Result;
44
45 if (Token->Event != NULL) {
46 gBS->SignalEvent (Token->Event);
47 }
48
49 Instance->Token = NULL;
50 }
51
52 ASSERT (Instance->UnicastPort != NULL);
53 UdpIoCleanPort (Instance->UnicastPort);
54
55 if (Instance->LastPacket != NULL) {
56 NetbufFree (Instance->LastPacket);
57 Instance->LastPacket = NULL;
58 }
59
60 if (Instance->McastUdpPort != NULL) {
61 UdpIoFreePort (Instance->McastUdpPort);
62 Instance->McastUdpPort = NULL;
63 }
64
65 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->Blocks) {
66 Block = NET_LIST_USER_STRUCT (Entry, MTFTP4_BLOCK_RANGE, Link);
67 RemoveEntryList (Entry);
68 gBS->FreePool (Block);
69 }
70
71 ZeroMem (&Instance->RequestOption, sizeof (MTFTP4_OPTION));
72
73 Instance->Operation = 0;
74
75 Instance->BlkSize = MTFTP4_DEFAULT_BLKSIZE;
76 Instance->LastBlock = 0;
77 Instance->ServerIp = 0;
78 Instance->ListeningPort = 0;
79 Instance->ConnectedPort = 0;
80 Instance->Gateway = 0;
81 Instance->PacketToLive = 0;
82 Instance->MaxRetry = 0;
83 Instance->CurRetry = 0;
84 Instance->Timeout = 0;
85 Instance->McastIp = 0;
86 Instance->McastPort = 0;
87 Instance->Master = TRUE;
88 }
89
90
91 /**
92 Check packet for GetInfo.
93
94 GetInfo is implemented with EfiMtftp4ReadFile. It use Mtftp4GetInfoCheckPacket
95 to inspect the first packet from server, then abort the session.
96
97 @param This The MTFTP4 protocol instance
98 @param Token The user's token
99 @param PacketLen The length of the packet
100 @param Packet The received packet.
101
102 @retval EFI_ABORTED Abort the ReadFile operation and return.
103
104 **/
105 EFI_STATUS
106 EFIAPI
107 Mtftp4GetInfoCheckPacket (
108 IN EFI_MTFTP4_PROTOCOL *This,
109 IN EFI_MTFTP4_TOKEN *Token,
110 IN UINT16 PacketLen,
111 IN EFI_MTFTP4_PACKET *Packet
112 )
113 {
114 MTFTP4_PROTOCOL *Instance;
115 MTFTP4_GETINFO_STATE *State;
116 EFI_STATUS Status;
117 UINT16 OpCode;
118
119 Instance = MTFTP4_PROTOCOL_FROM_THIS (This);
120 State = &Instance->GetInfoState;
121 OpCode = NTOHS (Packet->OpCode);
122
123 //
124 // Set the GetInfo's return status according to the OpCode.
125 //
126 switch (OpCode) {
127 case EFI_MTFTP4_OPCODE_ERROR:
128 State->Status = EFI_TFTP_ERROR;
129 break;
130
131 case EFI_MTFTP4_OPCODE_OACK:
132 State->Status = EFI_SUCCESS;
133 break;
134
135 default:
136 State->Status = EFI_PROTOCOL_ERROR;
137 }
138
139 //
140 // Allocate buffer then copy the packet over. Use gBS->AllocatePool
141 // in case AllocatePool will implements something tricky.
142 //
143 Status = gBS->AllocatePool (EfiBootServicesData, PacketLen, (VOID **) State->Packet);
144
145 if (EFI_ERROR (Status)) {
146 State->Status = EFI_OUT_OF_RESOURCES;
147 return EFI_ABORTED;
148 }
149
150 *(State->PacketLen) = PacketLen;
151 CopyMem (*(State->Packet), Packet, PacketLen);
152
153 return EFI_ABORTED;
154 }
155
156
157 /**
158 Check whether the override data is valid.
159
160 It will first validate whether the server is a valid unicast. If a gateway
161 is provided in the Override, it also check that it is a unicast on the
162 connected network.
163
164 @param Instance The MTFTP instance
165 @param Override The override data to validate.
166
167 @retval TRUE The override data is valid
168 @retval FALSE The override data is invalid
169
170 **/
171 BOOLEAN
172 Mtftp4OverrideValid (
173 IN MTFTP4_PROTOCOL *Instance,
174 IN EFI_MTFTP4_OVERRIDE_DATA *Override
175 )
176 {
177 EFI_MTFTP4_CONFIG_DATA *Config;
178 IP4_ADDR Ip;
179 IP4_ADDR Netmask;
180 IP4_ADDR Gateway;
181
182 CopyMem (&Ip, &Override->ServerIp, sizeof (IP4_ADDR));
183 if (!Ip4IsUnicast (NTOHL (Ip), 0)) {
184 return FALSE;
185 }
186
187 Config = &Instance->Config;
188
189 CopyMem (&Gateway, &Override->GatewayIp, sizeof (IP4_ADDR));
190 Gateway = NTOHL (Gateway);
191
192 if (!Config->UseDefaultSetting && (Gateway != 0)) {
193 CopyMem (&Netmask, &Config->SubnetMask, sizeof (IP4_ADDR));
194 CopyMem (&Ip, &Config->StationIp, sizeof (IP4_ADDR));
195
196 Netmask = NTOHL (Netmask);
197 Ip = NTOHL (Ip);
198
199 if (!Ip4IsUnicast (Gateway, Netmask) || !IP4_NET_EQUAL (Gateway, Ip, Netmask)) {
200 return FALSE;
201 }
202 }
203
204 return TRUE;
205 }
206
207
208 /**
209 Poll the UDP to get the IP4 default address, which may be retrieved
210 by DHCP.
211
212 The default time out value is 5 seconds. If IP has retrieved the default address,
213 the UDP is reconfigured.
214
215 @param Instance The Mtftp instance
216 @param UdpPort The UDP port to poll
217 @param UdpCfgData The UDP configure data to reconfigure the UDP
218 port.
219
220 @retval TRUE The default address is retrieved and UDP is reconfigured.
221 @retval FALSE Some error occured.
222
223 **/
224 BOOLEAN
225 Mtftp4GetMapping (
226 IN MTFTP4_PROTOCOL *Instance,
227 IN UDP_IO_PORT *UdpPort,
228 IN EFI_UDP4_CONFIG_DATA *UdpCfgData
229 )
230 {
231 MTFTP4_SERVICE *Service;
232 EFI_IP4_MODE_DATA Ip4Mode;
233 EFI_UDP4_PROTOCOL *Udp;
234 EFI_STATUS Status;
235
236 ASSERT (Instance->Config.UseDefaultSetting);
237
238 Service = Instance->Service;
239 Udp = UdpPort->Udp;
240
241 Status = gBS->SetTimer (
242 Service->TimerToGetMap,
243 TimerRelative,
244 MTFTP4_TIME_TO_GETMAP * TICKS_PER_SECOND
245 );
246 if (EFI_ERROR (Status)) {
247 return FALSE;
248 }
249
250 while (!EFI_ERROR (gBS->CheckEvent (Service->TimerToGetMap))) {
251 Udp->Poll (Udp);
252
253 if (!EFI_ERROR (Udp->GetModeData (Udp, NULL, &Ip4Mode, NULL, NULL)) &&
254 Ip4Mode.IsConfigured) {
255
256 Udp->Configure (Udp, NULL);
257 return (BOOLEAN) (Udp->Configure (Udp, UdpCfgData) == EFI_SUCCESS);
258 }
259 }
260
261 return FALSE;
262 }
263
264
265 /**
266 Configure the UDP port for unicast receiving.
267
268 @param UdpIo The UDP port
269 @param Instance The MTFTP session
270
271 @retval EFI_SUCCESS The UDP port is successfully configured for the
272 session to unicast receive.
273
274 **/
275 EFI_STATUS
276 Mtftp4ConfigUnicastPort (
277 IN UDP_IO_PORT *UdpIo,
278 IN MTFTP4_PROTOCOL *Instance
279 )
280 {
281 EFI_MTFTP4_CONFIG_DATA *Config;
282 EFI_UDP4_CONFIG_DATA UdpConfig;
283 EFI_STATUS Status;
284 IP4_ADDR Ip;
285
286 Config = &Instance->Config;
287
288 UdpConfig.AcceptBroadcast = FALSE;
289 UdpConfig.AcceptPromiscuous = FALSE;
290 UdpConfig.AcceptAnyPort = FALSE;
291 UdpConfig.AllowDuplicatePort = FALSE;
292 UdpConfig.TypeOfService = 0;
293 UdpConfig.TimeToLive = 64;
294 UdpConfig.DoNotFragment = FALSE;
295 UdpConfig.ReceiveTimeout = 0;
296 UdpConfig.TransmitTimeout = 0;
297 UdpConfig.UseDefaultAddress = Config->UseDefaultSetting;
298 UdpConfig.StationAddress = Config->StationIp;
299 UdpConfig.SubnetMask = Config->SubnetMask;
300 UdpConfig.StationPort = 0;
301 UdpConfig.RemotePort = 0;
302
303 Ip = HTONL (Instance->ServerIp);
304 CopyMem (&UdpConfig.RemoteAddress, &Ip, sizeof (EFI_IPv4_ADDRESS));
305
306 Status = UdpIo->Udp->Configure (UdpIo->Udp, &UdpConfig);
307
308 if ((Status == EFI_NO_MAPPING) && Mtftp4GetMapping (Instance, UdpIo, &UdpConfig)) {
309 return EFI_SUCCESS;
310 }
311
312 if (!Config->UseDefaultSetting && !EFI_IP4_EQUAL (&mZeroIp4Addr, &Config->GatewayIp)) {
313 //
314 // The station IP address is manually configured and the Gateway IP is not 0.
315 // Add the default route for this UDP instance.
316 //
317 Status = UdpIo->Udp->Routes (UdpIo->Udp, FALSE, &mZeroIp4Addr, &mZeroIp4Addr, &Config->GatewayIp);
318 if (EFI_ERROR (Status)) {
319 UdpIo->Udp->Configure (UdpIo->Udp, NULL);
320 }
321 }
322 return Status;
323 }
324
325
326 /**
327 Start the MTFTP session to do the operation, such as read file,
328 write file, and read directory.
329
330 @param This The MTFTP session
331 @param Token The token than encapsues the user's request.
332 @param Operation The operation to do
333
334 @retval EFI_INVALID_PARAMETER Some of the parameters are invalid.
335 @retval EFI_NOT_STARTED The MTFTP session hasn't been configured.
336 @retval EFI_ALREADY_STARTED There is pending operation for the session.
337 @retval EFI_SUCCESS The operation is successfully started.
338
339 **/
340 EFI_STATUS
341 Mtftp4Start (
342 IN EFI_MTFTP4_PROTOCOL *This,
343 IN EFI_MTFTP4_TOKEN *Token,
344 IN UINT16 Operation
345 )
346 {
347 MTFTP4_PROTOCOL *Instance;
348 EFI_MTFTP4_OVERRIDE_DATA *Override;
349 EFI_MTFTP4_CONFIG_DATA *Config;
350 EFI_TPL OldTpl;
351 EFI_STATUS Status;
352
353 //
354 // Validate the parameters
355 //
356 if ((This == NULL) || (Token == NULL) || (Token->Filename == NULL) ||
357 ((Token->OptionCount != 0) && (Token->OptionList == NULL))) {
358 return EFI_INVALID_PARAMETER;
359 }
360
361 //
362 // User must provide at least one method to collect the data for download.
363 //
364 if (((Operation == EFI_MTFTP4_OPCODE_RRQ) || (Operation == EFI_MTFTP4_OPCODE_DIR)) &&
365 ((Token->Buffer == NULL) && (Token->CheckPacket == NULL))) {
366 return EFI_INVALID_PARAMETER;
367 }
368
369 //
370 // User must provide at least one method to provide the data for upload.
371 //
372 if ((Operation == EFI_MTFTP4_OPCODE_WRQ) &&
373 ((Token->Buffer == NULL) && (Token->PacketNeeded == NULL))) {
374 return EFI_INVALID_PARAMETER;
375 }
376
377 Instance = MTFTP4_PROTOCOL_FROM_THIS (This);
378
379 Status = EFI_SUCCESS;
380 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
381
382 if (Instance->State != MTFTP4_STATE_CONFIGED) {
383 Status = EFI_NOT_STARTED;
384 }
385
386 if (Instance->Operation != 0) {
387 Status = EFI_ACCESS_DENIED;
388 }
389
390 if (EFI_ERROR (Status)) {
391 gBS->RestoreTPL (OldTpl);
392 return Status;
393 }
394
395 //
396 // Set the Operation now to prevent the application start other
397 // operations.
398 //
399 Instance->Operation = Operation;
400 Override = Token->OverrideData;
401
402 if ((Override != NULL) && !Mtftp4OverrideValid (Instance, Override)) {
403 Status = EFI_INVALID_PARAMETER;
404 goto ON_ERROR;
405 }
406
407 if (Token->OptionCount != 0) {
408 Status = Mtftp4ParseOption (
409 Token->OptionList,
410 Token->OptionCount,
411 TRUE,
412 &Instance->RequestOption
413 );
414
415 if (EFI_ERROR (Status)) {
416 goto ON_ERROR;
417 }
418 }
419
420 //
421 // Set the operation parameters from the configuration or override data.
422 //
423 Config = &Instance->Config;
424 Instance->Token = Token;
425 Instance->BlkSize = MTFTP4_DEFAULT_BLKSIZE;
426
427 CopyMem (&Instance->ServerIp, &Config->ServerIp, sizeof (IP4_ADDR));
428 Instance->ServerIp = NTOHL (Instance->ServerIp);
429
430 Instance->ListeningPort = Config->InitialServerPort;
431 Instance->ConnectedPort = 0;
432
433 CopyMem (&Instance->Gateway, &Config->GatewayIp, sizeof (IP4_ADDR));
434 Instance->Gateway = NTOHL (Instance->Gateway);
435
436 Instance->MaxRetry = Config->TryCount;
437 Instance->Timeout = Config->TimeoutValue;
438 Instance->Master = TRUE;
439
440 if (Override != NULL) {
441 CopyMem (&Instance->ServerIp, &Override->ServerIp, sizeof (IP4_ADDR));
442 CopyMem (&Instance->Gateway, &Override->GatewayIp, sizeof (IP4_ADDR));
443
444 Instance->ServerIp = NTOHL (Instance->ServerIp);
445 Instance->Gateway = NTOHL (Instance->Gateway);
446
447 Instance->ListeningPort = Override->ServerPort;
448 Instance->MaxRetry = Override->TryCount;
449 Instance->Timeout = Override->TimeoutValue;
450 }
451
452 if (Instance->ListeningPort == 0) {
453 Instance->ListeningPort = MTFTP4_DEFAULT_SERVER_PORT;
454 }
455
456 if (Instance->MaxRetry == 0) {
457 Instance->MaxRetry = MTFTP4_DEFAULT_RETRY;
458 }
459
460 if (Instance->Timeout == 0) {
461 Instance->Timeout = MTFTP4_DEFAULT_TIMEOUT;
462 }
463
464 //
465 // Config the unicast UDP child to send initial request
466 //
467 Status = Mtftp4ConfigUnicastPort (Instance->UnicastPort, Instance);
468
469 if (EFI_ERROR (Status)) {
470 goto ON_ERROR;
471 }
472
473 //
474 // Set initial status.
475 //
476 Token->Status = EFI_NOT_READY;
477
478 //
479 // Build and send an initial requests
480 //
481 if (Operation == EFI_MTFTP4_OPCODE_WRQ) {
482 Status = Mtftp4WrqStart (Instance, Operation);
483 } else {
484 Status = Mtftp4RrqStart (Instance, Operation);
485 }
486
487 gBS->RestoreTPL (OldTpl);
488
489 if (EFI_ERROR (Status)) {
490 goto ON_ERROR;
491 }
492
493 if (Token->Event != NULL) {
494 return EFI_SUCCESS;
495 }
496
497 //
498 // Return immediately for asynchronous operation or poll the
499 // instance for synchronous operation.
500 //
501 while (Token->Status == EFI_NOT_READY) {
502 This->Poll (This);
503 }
504
505 return Token->Status;
506
507 ON_ERROR:
508 Mtftp4CleanOperation (Instance, Status);
509 gBS->RestoreTPL (OldTpl);
510
511 return Status;
512 }
513
514
515 /**
516 Reads the current operational settings.
517
518 The GetModeData()function reads the current operational settings of this
519 EFI MTFTPv4 Protocol driver instance.
520
521 @param This Pointer to the EFI_MTFTP4_PROTOCOL instance.
522 @param ModeData Pointer to storage for the EFI MTFTPv4 Protocol
523 driver mode data.
524
525 @retval EFI_SUCCESS The configuration data was successfully returned.
526 @retval EFI_OUT_OF_RESOURCES The required mode data could not be allocated.
527 @retval EFI_INVALID_PARAMETER This is NULL or ModeData is NULL.
528
529 **/
530 EFI_STATUS
531 EFIAPI
532 EfiMtftp4GetModeData (
533 IN EFI_MTFTP4_PROTOCOL *This,
534 OUT EFI_MTFTP4_MODE_DATA *ModeData
535 )
536 {
537 MTFTP4_PROTOCOL *Instance;
538 EFI_TPL OldTpl;
539
540 if ((This == NULL) || (ModeData == NULL)) {
541 return EFI_INVALID_PARAMETER;
542 }
543
544 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
545
546 Instance = MTFTP4_PROTOCOL_FROM_THIS (This);
547 CopyMem(&ModeData->ConfigData, &Instance->Config, sizeof (Instance->Config));
548 ModeData->SupportedOptionCount = MTFTP4_SUPPORTED_OPTIONS;
549 ModeData->SupportedOptoins = (UINT8 **) mMtftp4SupportedOptions;
550 ModeData->UnsupportedOptionCount = 0;
551 ModeData->UnsupportedOptoins = NULL;
552
553 gBS->RestoreTPL (OldTpl);
554
555 return EFI_SUCCESS;
556 }
557
558
559
560 /**
561 Initializes, changes, or resets the default operational setting for this
562 EFI MTFTPv4 Protocol driver instance.
563
564 The Configure() function is used to set and change the configuration data for
565 this EFI MTFTPv4 Protocol driver instance. The configuration data can be reset
566 to startup defaults by calling Configure() with MtftpConfigData set to NULL.
567 Whenever the instance is reset, any pending operation is aborted. By changing
568 the EFI MTFTPv4 Protocol driver instance configuration data, the client can
569 connect to different MTFTPv4 servers. The configuration parameters in
570 MtftpConfigData are used as the default parameters in later MTFTPv4 operations
571 and can be overridden in later operations.
572
573 @param This Pointer to the EFI_MTFTP4_PROTOCOL instance
574 @param ConfigData MtftpConfigDataPointer to the configuration data
575 structure
576
577 @retval EFI_SUCCESS The EFI MTFTPv4 Protocol driver was configured
578 successfully.
579 @retval EFI_INVALID_PARAMETER One or more following conditions are TRUE:
580 1.This is NULL.
581 2.MtftpConfigData.UseDefaultSetting is FALSE and
582 MtftpConfigData.StationIp is not a valid IPv4
583 unicast address.
584 3.MtftpCofigData.UseDefaultSetting is FALSE and
585 MtftpConfigData.SubnetMask is invalid.
586 4.MtftpCofigData.ServerIp is not a valid IPv4
587 unicast address.
588 5.MtftpConfigData.UseDefaultSetting is FALSE and
589 MtftpConfigData.GatewayIp is not a valid IPv4
590 unicast address or is not in the same subnet
591 with station address.
592 @retval EFI_ACCESS_DENIED The EFI configuration could not be changed at this
593 time because there is one MTFTP background operation
594 in progress.
595 @retval EFI_NO_MAPPING When using a default address, configuration
596 (DHCP, BOOTP, RARP, etc.) has not finished yet.
597 @retval EFI_UNSUPPORTED A configuration protocol (DHCP, BOOTP, RARP, etc.)
598 could not be located when clients choose to use
599 the default address settings.
600 @retval EFI_OUT_OF_RESOURCES The EFI MTFTPv4 Protocol driver instance data could
601 not be allocated.
602 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
603 The EFI MTFTPv4 Protocol driver instance is not
604 configured.
605
606 **/
607 EFI_STATUS
608 EFIAPI
609 EfiMtftp4Configure (
610 IN EFI_MTFTP4_PROTOCOL *This,
611 IN EFI_MTFTP4_CONFIG_DATA *ConfigData
612 )
613 {
614 MTFTP4_PROTOCOL *Instance;
615 EFI_TPL OldTpl;
616 IP4_ADDR Ip;
617 IP4_ADDR Netmask;
618 IP4_ADDR Gateway;
619 IP4_ADDR ServerIp;
620
621 if (This == NULL) {
622 return EFI_INVALID_PARAMETER;
623 }
624
625 Instance = MTFTP4_PROTOCOL_FROM_THIS (This);
626
627 if (ConfigData == NULL) {
628 //
629 // Reset the operation if ConfigData is NULL
630 //
631 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
632
633 Mtftp4CleanOperation (Instance, EFI_ABORTED);
634 ZeroMem (&Instance->Config, sizeof (EFI_MTFTP4_CONFIG_DATA));
635 Instance->State = MTFTP4_STATE_UNCONFIGED;
636
637 gBS->RestoreTPL (OldTpl);
638
639 } else {
640 //
641 // Configure the parameters for new operation.
642 //
643 CopyMem (&Ip, &ConfigData->StationIp, sizeof (IP4_ADDR));
644 CopyMem (&Netmask, &ConfigData->SubnetMask, sizeof (IP4_ADDR));
645 CopyMem (&Gateway, &ConfigData->GatewayIp, sizeof (IP4_ADDR));
646 CopyMem (&ServerIp, &ConfigData->ServerIp, sizeof (IP4_ADDR));
647
648 Ip = NTOHL (Ip);
649 Netmask = NTOHL (Netmask);
650 Gateway = NTOHL (Gateway);
651 ServerIp = NTOHL (ServerIp);
652
653 if (!Ip4IsUnicast (ServerIp, 0)) {
654 return EFI_INVALID_PARAMETER;
655 }
656
657 if (!ConfigData->UseDefaultSetting &&
658 ((!IP4_IS_VALID_NETMASK (Netmask) || !Ip4IsUnicast (Ip, Netmask)))) {
659
660 return EFI_INVALID_PARAMETER;
661 }
662
663 if ((Gateway != 0) &&
664 (!IP4_NET_EQUAL (Gateway, Ip, Netmask) || !Ip4IsUnicast (Gateway, Netmask))) {
665
666 return EFI_INVALID_PARAMETER;
667 }
668
669 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
670
671 if ((Instance->State == MTFTP4_STATE_CONFIGED) && (Instance->Operation != 0)) {
672 gBS->RestoreTPL (OldTpl);
673 return EFI_ACCESS_DENIED;
674 }
675
676 CopyMem(&Instance->Config, ConfigData, sizeof (*ConfigData));;
677 Instance->State = MTFTP4_STATE_CONFIGED;
678
679 gBS->RestoreTPL (OldTpl);
680 }
681
682 return EFI_SUCCESS;
683 }
684
685
686
687 /**
688 Parses the options in an MTFTPv4 OACK packet.
689
690 The ParseOptions() function parses the option fields in an MTFTPv4 OACK packet
691 and returns the number of options that were found and optionally a list of
692 pointers to the options in the packet.
693 If one or more of the option fields are not valid, then EFI_PROTOCOL_ERROR is
694 returned and *OptionCount and *OptionList stop at the last valid option.
695 The OptionList is allocated by this function, and caller should free it when used.
696
697 @param This Pointer to the EFI_MTFTP4_PROTOCOL instance.
698 @param PacketLen Length of the OACK packet to be parsed.
699 @param Packet Pointer to the OACK packet to be parsed.
700 @param OptionCount Pointer to the number of options in following OptionList.
701 @param OptionList Pointer to EFI_MTFTP4_OPTION storage. Call the
702 EFI Boot Service FreePool() to release theOptionList
703 if the options in this OptionList are not needed
704 any more
705
706 @retval EFI_SUCCESS The OACK packet was valid and the OptionCount and
707 OptionList parameters have been updated.
708 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
709 1.PacketLen is 0.
710 2.Packet is NULL or Packet is not a valid MTFTPv4 packet.
711 3.OptionCount is NULL.
712 @retval EFI_NOT_FOUND No options were found in the OACK packet.
713 @retval EFI_OUT_OF_RESOURCES Storage for the OptionList array cannot be allocated.
714 @retval EFI_PROTOCOL_ERROR One or more of the option fields is invalid.
715
716 **/
717 EFI_STATUS
718 EFIAPI
719 EfiMtftp4ParseOptions (
720 IN EFI_MTFTP4_PROTOCOL *This,
721 IN UINT32 PacketLen,
722 IN EFI_MTFTP4_PACKET *Packet,
723 OUT UINT32 *OptionCount,
724 OUT EFI_MTFTP4_OPTION **OptionList OPTIONAL
725 )
726 {
727 EFI_STATUS Status;
728
729 if ((This == NULL) || (PacketLen < MTFTP4_OPCODE_LEN) ||
730 (Packet == NULL) || (OptionCount == NULL)) {
731
732 return EFI_INVALID_PARAMETER;
733 }
734
735 Status = Mtftp4ExtractOptions (Packet, PacketLen, OptionCount, OptionList);
736
737 if (EFI_ERROR (Status)) {
738 return Status;
739 }
740
741 if (*OptionCount == 0) {
742 return EFI_NOT_FOUND;
743 }
744
745 return EFI_SUCCESS;
746 }
747
748
749 /**
750 Downloads a file from an MTFTPv4 server.
751
752 The ReadFile() function is used to initialize and start an MTFTPv4 download
753 process and optionally wait for completion. When the download operation completes,
754 whether successfully or not, the Token.Status field is updated by the EFI MTFTPv4
755 Protocol driver and then Token.Event is signaled (if it is not NULL).
756 Data can be downloaded from the MTFTPv4 server into either of the following locations:
757 1.A fixed buffer that is pointed to by Token.Buffer
758 2.A download service function that is pointed to by Token.CheckPacket
759 If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket
760 will be called first. If the call is successful, the packet will be stored in
761 Token.Buffer.
762
763 @param This Pointer to the EFI_MTFTP4_PROTOCOL instance
764 @param Token Pointer to the token structure to provide the
765 parameters that are used in this operation.
766
767 @retval EFI_SUCCESS The data file has been transferred successfully.
768 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
769 @retval EFI_BUFFER_TOO_SMALL BufferSize is not large enough to hold the downloaded
770 data in downloading process.
771 @retval EFI_ABORTED Current operation is aborted by user.
772 @retval EFI_ICMP_ERROR An ICMP ERROR packet was received.
773 @retval EFI_TIMEOUT No responses were received from the MTFTPv4 server.
774 @retval EFI_TFTP_ERROR An MTFTPv4 ERROR packet was received.
775 @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred.
776
777 **/
778 EFI_STATUS
779 EFIAPI
780 EfiMtftp4ReadFile (
781 IN EFI_MTFTP4_PROTOCOL *This,
782 IN EFI_MTFTP4_TOKEN *Token
783 )
784 {
785 return Mtftp4Start (This, Token, EFI_MTFTP4_OPCODE_RRQ);
786 }
787
788
789 /**
790 Sends a data file to an MTFTPv4 server. May be unsupported in some EFI implementations
791
792 The WriteFile() function is used to initialize an uploading operation with the
793 given option list and optionally wait for completion. If one or more of the
794 options is not supported by the server, the unsupported options are ignored and
795 a standard TFTP process starts instead. When the upload process completes,
796 whether successfully or not, Token.Event is signaled, and the EFI MTFTPv4 Protocol
797 driver updates Token.Status.
798 The caller can supply the data to be uploaded in the following two modes:
799 1.Through the user-provided buffer
800 2.Through a callback function
801 With the user-provided buffer, the Token.BufferSize field indicates the length
802 of the buffer, and the driver will upload the data in the buffer. With an
803 EFI_MTFTP4_PACKET_NEEDED callback function, the driver will call this callback
804 function to get more data from the user to upload. See the definition of
805 EFI_MTFTP4_PACKET_NEEDED for more information. These two modes cannot be used at
806 the same time. The callback function will be ignored if the user provides the buffer.
807
808 @param This Pointer to the EFI_MTFTP4_PROTOCOL instance.
809 @param Token Pointer to the token structure to provide the
810 parameters that are used in this function
811
812 @retval EFI_SUCCESS The upload session has started.
813 @retval EFI_UNSUPPORTED The operation is not supported by this implementation.
814 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
815 1. This is NULL.
816 2. Token is NULL.
817 3. Token.Filename is NULL.
818 4. Token.OptionCount is not zero and
819 Token.OptionList is NULL.
820 5. One or more options in Token.OptionList have wrong
821 format.
822 6. Token.Buffer and Token.PacketNeeded are both
823 NULL.
824 7. One or more IPv4 addresses in Token.OverrideData
825 are not valid unicast IPv4 addresses if
826 Token.OverrideData is not NULL.
827 @retval EFI_UNSUPPORTED One or more options in the Token.OptionList are in the
828 unsupported list of structure EFI_MTFTP4_MODE_DATA.
829 @retval EFI_NOT_STARTED The EFI MTFTPv4 Protocol driver has not been started.
830 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,
831 BOOTP, RARP, etc.) is not finished yet.
832 @retval EFI_ALREADY_STARTED This Token is already being used in another MTFTPv4
833 session.
834 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
835 @retval EFI_ACCESS_DENIED The previous operation has not completed yet.
836 @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred.
837
838 **/
839 EFI_STATUS
840 EFIAPI
841 EfiMtftp4WriteFile (
842 IN EFI_MTFTP4_PROTOCOL *This,
843 IN EFI_MTFTP4_TOKEN *Token
844 )
845 {
846 return Mtftp4Start (This, Token, EFI_MTFTP4_OPCODE_WRQ);
847 }
848
849
850 /**
851 Downloads a data file "directory" from an MTFTPv4 server.
852 May be unsupported in some EFI implementations
853
854 The ReadDirectory() function is used to return a list of files on the MTFTPv4
855 server that are logically (or operationally) related to Token.Filename. The
856 directory request packet that is sent to the server is built with the option
857 list that was provided by caller, if present.
858 The file information that the server returns is put into either of the following
859 locations:
860 1.A fixed buffer that is pointed to by Token.Buffer
861 2.A download service function that is pointed to by Token.CheckPacket
862 If both Token.Buffer and Token.CheckPacket are used, then Token.CheckPacket will
863 be called first. If the call is successful, the packet will be stored in Token.Buffer.
864 The returned directory listing in the Token.Buffer or EFI_MTFTP4_PACKET consists
865 of a list of two or three variable-length ASCII strings, each terminated by a
866 null character, for each file in the directory. If the multicast option is involved,
867 the first field of each directory entry is the static multicast IP address and
868 UDP port number that is associated with the file name. The format of the field
869 is ip:ip:ip:ip:port. If the multicast option is not involved, this field and its
870 terminating null character are not present.
871 The next field of each directory entry is the file name and the last field is
872 the file information string. The information string contains the file size and
873 the create/modify timestamp. The format of the information string is filesize
874 yyyy-mm-dd hh:mm:ss:ffff. The timestamp is Coordinated Universal Time
875 (UTC; also known as Greenwich Mean Time [GMT]).
876 The only difference between ReadFile and ReadDirectory is the opcode used.
877
878 @param This Pointer to the EFI_MTFTP4_PROTOCOL instance
879 @param Token Pointer to the token structure to provide the
880 parameters that are used in this function
881
882 @retval EFI_SUCCESS The MTFTPv4 related file "directory" has been downloaded.
883 @retval EFI_UNSUPPORTED The operation is not supported by this implementation.
884 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
885 1. This is NULL.
886 2. Token is NULL.
887 3. Token.Filename is NULL.
888 4. Token.OptionCount is not zero and
889 Token.OptionList is NULL.
890 5. One or more options in Token.OptionList have wrong
891 format.
892 6. Token.Buffer and Token.PacketNeeded are both
893 NULL.
894 7. One or more IPv4 addresses in Token.OverrideData
895 are not valid unicast IPv4 addresses if
896 Token.OverrideData is not NULL.
897 @retval EFI_UNSUPPORTED One or more options in the Token.OptionList are in the
898 unsupported list of structure EFI_MTFTP4_MODE_DATA.
899 @retval EFI_NOT_STARTED The EFI MTFTPv4 Protocol driver has not been started.
900 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,
901 BOOTP, RARP, etc.) is not finished yet.
902 @retval EFI_ALREADY_STARTED This Token is already being used in another MTFTPv4
903 session.
904 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
905 @retval EFI_ACCESS_DENIED The previous operation has not completed yet.
906 @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred.
907
908 **/
909 EFI_STATUS
910 EFIAPI
911 EfiMtftp4ReadDirectory (
912 IN EFI_MTFTP4_PROTOCOL *This,
913 IN EFI_MTFTP4_TOKEN *Token
914 )
915 {
916 return Mtftp4Start (This, Token, EFI_MTFTP4_OPCODE_DIR);
917 }
918
919
920 /**
921 Gets information about a file from an MTFTPv4 server.
922
923 The GetInfo() function assembles an MTFTPv4 request packet with options;
924 sends it to the MTFTPv4 server; and may return an MTFTPv4 OACK, MTFTPv4 ERROR,
925 or ICMP ERROR packet. Retries occur only if no response packets are received
926 from the MTFTPv4 server before the timeout expires.
927 It is implemented with EfiMtftp4ReadFile: build a token, then pass it to
928 EfiMtftp4ReadFile. In its check packet callback abort the opertions.
929
930 @param This Pointer to the EFI_MTFTP4_PROTOCOL instance
931 @param OverrideData Data that is used to override the existing
932 parameters. If NULL, the default parameters that
933 were set in the EFI_MTFTP4_PROTOCOL.Configure()
934 function are used
935 @param Filename Pointer to ASCIIZ file name string
936 @param ModeStr Pointer to ASCIIZ mode string. If NULL, "octet"
937 will be used
938 @param OptionCount Number of option/value string pairs in OptionList
939 @param OptionList Pointer to array of option/value string pairs.
940 Ignored if OptionCount is zero
941 @param PacketLength The number of bytes in the returned packet
942 @param Packet PacketThe pointer to the received packet. This
943 buffer must be freed by the caller.
944
945 @retval EFI_SUCCESS An MTFTPv4 OACK packet was received and is in
946 the Buffer.
947 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
948 1.This is NULL.
949 2.Filename is NULL.
950 3.OptionCount is not zero and OptionList is NULL.
951 4.One or more options in OptionList have wrong format.
952 5.PacketLength is NULL.
953 6.One or more IPv4 addresses in OverrideData are
954 not valid unicast IPv4 addresses if OverrideData
955 is not NULL.
956 @retval EFI_UNSUPPORTED One or more options in the OptionList are in the
957 unsupported list of structure EFI_MTFTP4_MODE_DATA
958 @retval EFI_NOT_STARTED The EFI MTFTPv4 Protocol driver has not been started.
959 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,
960 BOOTP, RARP, etc.) has not finished yet.
961 @retval EFI_ACCESS_DENIED The previous operation has not completed yet.
962 @retval EFI_OUT_OF_RESOURCES Required system resources could not be allocated.
963 @retval EFI_TFTP_ERROR An MTFTPv4 ERROR packet was received and is in
964 the Buffer.
965 @retval EFI_ICMP_ERROR An ICMP ERROR packet was received and the Packet
966 is set to NULL.
967 @retval EFI_PROTOCOL_ERROR An unexpected MTFTPv4 packet was received and is
968 in the Buffer.
969 @retval EFI_TIMEOUT No responses were received from the MTFTPv4 server.
970 @retval EFI_DEVICE_ERROR An unexpected network error or system error occurred.
971
972 **/
973 EFI_STATUS
974 EFIAPI
975 EfiMtftp4GetInfo (
976 IN EFI_MTFTP4_PROTOCOL *This,
977 IN EFI_MTFTP4_OVERRIDE_DATA *OverrideData OPTIONAL,
978 IN UINT8 *Filename,
979 IN UINT8 *ModeStr OPTIONAL,
980 IN UINT8 OptionCount,
981 IN EFI_MTFTP4_OPTION *OptionList OPTIONAL,
982 OUT UINT32 *PacketLength,
983 OUT EFI_MTFTP4_PACKET **Packet OPTIONAL
984 )
985 {
986 EFI_MTFTP4_TOKEN Token;
987 MTFTP4_PROTOCOL *Instance;
988 MTFTP4_GETINFO_STATE *State;
989 EFI_STATUS Status;
990
991 if ((This == NULL) || (Filename == NULL) || (PacketLength == NULL) ||
992 ((OptionCount != 0) && (OptionList == NULL))) {
993 return EFI_INVALID_PARAMETER;
994 }
995
996 if (Packet != NULL) {
997 *Packet = NULL;
998 }
999
1000 *PacketLength = 0;
1001 Instance = MTFTP4_PROTOCOL_FROM_THIS (This);
1002 State = &Instance->GetInfoState;
1003 State->Packet = Packet;
1004 State->PacketLen = PacketLength;
1005 State->Status = EFI_SUCCESS;
1006
1007 //
1008 // Fill in the Token to issue an synchronous ReadFile operation
1009 //
1010 Token.Status = EFI_SUCCESS;
1011 Token.Event = NULL;
1012 Token.OverrideData = OverrideData;
1013 Token.Filename = Filename;
1014 Token.ModeStr = ModeStr;
1015 Token.OptionCount = OptionCount;
1016 Token.OptionList = OptionList;
1017 Token.BufferSize = 0;
1018 Token.Buffer = NULL;
1019 Token.CheckPacket = Mtftp4GetInfoCheckPacket;
1020 Token.TimeoutCallback = NULL;
1021 Token.PacketNeeded = NULL;
1022
1023 Status = EfiMtftp4ReadFile (This, &Token);
1024
1025 if (EFI_ABORTED == Status) {
1026 return State->Status;
1027 }
1028
1029 return Status;
1030 }
1031
1032 /**
1033 Polls for incoming data packets and processes outgoing data packets.
1034
1035 The Poll() function can be used by network drivers and applications to increase
1036 the rate that data packets are moved between the communications device and the
1037 transmit and receive queues.
1038 In some systems, the periodic timer event in the managed network driver may not
1039 poll the underlying communications device fast enough to transmit and/or receive
1040 all data packets without missing incoming packets or dropping outgoing packets.
1041 Drivers and applications that are experiencing packet loss should try calling
1042 the Poll() function more often.
1043
1044 @param This Pointer to the EFI_MTFTP4_PROTOCOL instance
1045
1046 @retval EFI_SUCCESS Incoming or outgoing data was processed.
1047 @retval EFI_NOT_STARTED This EFI MTFTPv4 Protocol instance has not been started.
1048 @retval EFI_NO_MAPPING When using a default address, configuration (DHCP,
1049 BOOTP, RARP, etc.) is not finished yet.
1050 @retval EFI_INVALID_PARAMETER This is NULL.
1051 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred.
1052 @retval EFI_TIMEOUT Data was dropped out of the transmit and/or receive
1053 queue. Consider increasing the polling rate.
1054
1055 **/
1056 EFI_STATUS
1057 EFIAPI
1058 EfiMtftp4Poll (
1059 IN EFI_MTFTP4_PROTOCOL *This
1060 )
1061 {
1062 MTFTP4_PROTOCOL *Instance;
1063 EFI_UDP4_PROTOCOL *Udp;
1064
1065 if (This == NULL) {
1066 return EFI_INVALID_PARAMETER;
1067 }
1068
1069 Instance = MTFTP4_PROTOCOL_FROM_THIS (This);
1070
1071 if (Instance->State == MTFTP4_STATE_UNCONFIGED) {
1072 return EFI_NOT_STARTED;
1073 } else if (Instance->State == MTFTP4_STATE_DESTORY) {
1074 return EFI_DEVICE_ERROR;
1075 }
1076
1077 Udp = Instance->UnicastPort->Udp;
1078 return Udp->Poll (Udp);
1079 }
1080
1081 EFI_MTFTP4_PROTOCOL gMtftp4ProtocolTemplate = {
1082 EfiMtftp4GetModeData,
1083 EfiMtftp4Configure,
1084 EfiMtftp4GetInfo,
1085 EfiMtftp4ParseOptions,
1086 EfiMtftp4ReadFile,
1087 EfiMtftp4WriteFile,
1088 EfiMtftp4ReadDirectory,
1089 EfiMtftp4Poll
1090 };