Mtftp4Impl.c 40 KB

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