Mtftp6Support.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /** @file
  2. Mtftp6 support functions implementation.
  3. Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "Mtftp6Impl.h"
  7. /**
  8. Allocate a MTFTP block range, then init it to the range of [Start, End].
  9. @param[in] Start The start block number.
  10. @param[in] End The last block number in the range.
  11. @return Range The range of the allocated block buffer.
  12. **/
  13. MTFTP6_BLOCK_RANGE *
  14. Mtftp6AllocateRange (
  15. IN UINT16 Start,
  16. IN UINT16 End
  17. )
  18. {
  19. MTFTP6_BLOCK_RANGE *Range;
  20. Range = AllocateZeroPool (sizeof (MTFTP6_BLOCK_RANGE));
  21. if (Range == NULL) {
  22. return NULL;
  23. }
  24. InitializeListHead (&Range->Link);
  25. Range->Start = Start;
  26. Range->End = End;
  27. Range->Bound = End;
  28. return Range;
  29. }
  30. /**
  31. Initialize the block range for either RRQ or WRQ. RRQ and WRQ have
  32. different requirements for Start and End. For example, during startup,
  33. WRQ initializes its whole valid block range to [0, 0xffff]. This
  34. is because the server will send an ACK0 to inform the user to start the
  35. upload. When the client receives an ACK0, it will remove 0 from the range,
  36. get the next block number, which is 1, then upload the BLOCK1. For RRQ
  37. without option negotiation, the server will directly send the BLOCK1
  38. in response to the client's RRQ. When received BLOCK1, the client will
  39. remove it from the block range and send an ACK. It also works if there
  40. is option negotiation.
  41. @param[in] Head The block range head to initialize.
  42. @param[in] Start The Start block number.
  43. @param[in] End The last block number.
  44. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range.
  45. @retval EFI_SUCCESS The initial block range is created.
  46. **/
  47. EFI_STATUS
  48. Mtftp6InitBlockRange (
  49. IN LIST_ENTRY *Head,
  50. IN UINT16 Start,
  51. IN UINT16 End
  52. )
  53. {
  54. MTFTP6_BLOCK_RANGE *Range;
  55. Range = Mtftp6AllocateRange (Start, End);
  56. if (Range == NULL) {
  57. return EFI_OUT_OF_RESOURCES;
  58. }
  59. InsertTailList (Head, &Range->Link);
  60. return EFI_SUCCESS;
  61. }
  62. /**
  63. Get the first valid block number on the range list.
  64. @param[in] Head The block range head.
  65. @retval ==-1 If the block range is empty.
  66. @retval >-1 The first valid block number.
  67. **/
  68. INTN
  69. Mtftp6GetNextBlockNum (
  70. IN LIST_ENTRY *Head
  71. )
  72. {
  73. MTFTP6_BLOCK_RANGE *Range;
  74. if (IsListEmpty (Head)) {
  75. return -1;
  76. }
  77. Range = NET_LIST_HEAD (Head, MTFTP6_BLOCK_RANGE, Link);
  78. return Range->Start;
  79. }
  80. /**
  81. Set the last block number of the block range list. It
  82. removes all the blocks after the Last. MTFTP initialize the
  83. block range to the maximum possible range, such as [0, 0xffff]
  84. for WRQ. When it gets the last block number, it calls
  85. this function to set the last block number.
  86. @param[in] Head The block range list.
  87. @param[in] Last The last block number.
  88. **/
  89. VOID
  90. Mtftp6SetLastBlockNum (
  91. IN LIST_ENTRY *Head,
  92. IN UINT16 Last
  93. )
  94. {
  95. MTFTP6_BLOCK_RANGE *Range;
  96. //
  97. // Iterate from the tail to head to remove the block number
  98. // after the last.
  99. //
  100. while (!IsListEmpty (Head)) {
  101. Range = NET_LIST_TAIL (Head, MTFTP6_BLOCK_RANGE, Link);
  102. if (Range->Start > Last) {
  103. RemoveEntryList (&Range->Link);
  104. FreePool (Range);
  105. continue;
  106. }
  107. if (Range->End > Last) {
  108. Range->End = Last;
  109. }
  110. return;
  111. }
  112. }
  113. /**
  114. Remove the block number from the block range list.
  115. @param[in] Head The block range list to remove from.
  116. @param[in] Num The block number to remove.
  117. @param[in] Completed Whether Num is the last block number.
  118. @param[out] BlockCounter The continuous block counter instead of the value after roll-over.
  119. @retval EFI_NOT_FOUND The block number isn't in the block range list.
  120. @retval EFI_SUCCESS The block number has been removed from the list.
  121. @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
  122. **/
  123. EFI_STATUS
  124. Mtftp6RemoveBlockNum (
  125. IN LIST_ENTRY *Head,
  126. IN UINT16 Num,
  127. IN BOOLEAN Completed,
  128. OUT UINT64 *BlockCounter
  129. )
  130. {
  131. MTFTP6_BLOCK_RANGE *Range;
  132. MTFTP6_BLOCK_RANGE *NewRange;
  133. LIST_ENTRY *Entry;
  134. NET_LIST_FOR_EACH (Entry, Head) {
  135. //
  136. // Each block represents a hole [Start, End] in the file,
  137. // skip to the first range with End >= Num
  138. //
  139. Range = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);
  140. if (Range->End < Num) {
  141. continue;
  142. }
  143. //
  144. // There are three different cases for Start
  145. // 1. (Start > Num) && (End >= Num):
  146. // because all the holes before this one has the condition of
  147. // End < Num, so this block number has been removed.
  148. //
  149. // 2. (Start == Num) && (End >= Num):
  150. // Need to increase the Start by one, and if End == Num, this
  151. // hole has been removed completely, remove it.
  152. //
  153. // 3. (Start < Num) && (End >= Num):
  154. // if End == Num, only need to decrease the End by one because
  155. // we have (Start < Num) && (Num == End), so (Start <= End - 1).
  156. // if (End > Num), the hold is split into two holes, with
  157. // [Start, Num - 1] and [Num + 1, End].
  158. //
  159. if (Range->Start > Num) {
  160. return EFI_NOT_FOUND;
  161. } else if (Range->Start == Num) {
  162. Range->Start++;
  163. //
  164. // Note that: RFC 1350 does not mention block counter roll-over,
  165. // but several TFTP hosts implement the roll-over be able to accept
  166. // transfers of unlimited size. There is no consensus, however, whether
  167. // the counter should wrap around to zero or to one. Many implementations
  168. // wrap to zero, because this is the simplest to implement. Here we choose
  169. // this solution.
  170. //
  171. *BlockCounter = Num;
  172. if (Range->Round > 0) {
  173. *BlockCounter += Range->Bound + MultU64x32 (Range->Round - 1, (UINT32)(Range->Bound + 1)) + 1;
  174. }
  175. if (Range->Start > Range->Bound) {
  176. Range->Start = 0;
  177. Range->Round++;
  178. }
  179. if ((Range->Start > Range->End) || Completed) {
  180. RemoveEntryList (&Range->Link);
  181. FreePool (Range);
  182. }
  183. return EFI_SUCCESS;
  184. } else {
  185. if (Range->End == Num) {
  186. Range->End--;
  187. } else {
  188. NewRange = Mtftp6AllocateRange ((UINT16)(Num + 1), (UINT16)Range->End);
  189. if (NewRange == NULL) {
  190. return EFI_OUT_OF_RESOURCES;
  191. }
  192. Range->End = Num - 1;
  193. NetListInsertAfter (&Range->Link, &NewRange->Link);
  194. }
  195. return EFI_SUCCESS;
  196. }
  197. }
  198. return EFI_NOT_FOUND;
  199. }
  200. /**
  201. Configure the opened Udp6 instance until the corresponding Ip6 instance
  202. has been configured.
  203. @param[in] UdpIo The pointer to the Udp6 Io.
  204. @param[in] UdpCfgData The pointer to the Udp6 configure data.
  205. @retval EFI_SUCCESS Configure the Udp6 instance successfully.
  206. @retval EFI_NO_MAPPING The corresponding Ip6 instance has not
  207. been configured yet.
  208. **/
  209. EFI_STATUS
  210. Mtftp6GetMapping (
  211. IN UDP_IO *UdpIo,
  212. IN EFI_UDP6_CONFIG_DATA *UdpCfgData
  213. )
  214. {
  215. EFI_IP6_MODE_DATA Ip6Mode;
  216. EFI_UDP6_PROTOCOL *Udp6;
  217. EFI_STATUS Status;
  218. EFI_EVENT Event;
  219. Event = NULL;
  220. Udp6 = UdpIo->Protocol.Udp6;
  221. //
  222. // Create a timer to check whether the Ip6 instance configured or not.
  223. //
  224. Status = gBS->CreateEvent (
  225. EVT_TIMER,
  226. TPL_CALLBACK,
  227. NULL,
  228. NULL,
  229. &Event
  230. );
  231. if (EFI_ERROR (Status)) {
  232. goto ON_EXIT;
  233. }
  234. Status = gBS->SetTimer (
  235. Event,
  236. TimerRelative,
  237. MTFTP6_GET_MAPPING_TIMEOUT * MTFTP6_TICK_PER_SECOND
  238. );
  239. if (EFI_ERROR (Status)) {
  240. goto ON_EXIT;
  241. }
  242. //
  243. // Check the Ip6 mode data till timeout.
  244. //
  245. while (EFI_ERROR (gBS->CheckEvent (Event))) {
  246. Udp6->Poll (Udp6);
  247. Status = Udp6->GetModeData (Udp6, NULL, &Ip6Mode, NULL, NULL);
  248. if (!EFI_ERROR (Status)) {
  249. if (Ip6Mode.AddressList != NULL) {
  250. FreePool (Ip6Mode.AddressList);
  251. }
  252. if (Ip6Mode.GroupTable != NULL) {
  253. FreePool (Ip6Mode.GroupTable);
  254. }
  255. if (Ip6Mode.RouteTable != NULL) {
  256. FreePool (Ip6Mode.RouteTable);
  257. }
  258. if (Ip6Mode.NeighborCache != NULL) {
  259. FreePool (Ip6Mode.NeighborCache);
  260. }
  261. if (Ip6Mode.PrefixTable != NULL) {
  262. FreePool (Ip6Mode.PrefixTable);
  263. }
  264. if (Ip6Mode.IcmpTypeList != NULL) {
  265. FreePool (Ip6Mode.IcmpTypeList);
  266. }
  267. if (Ip6Mode.IsConfigured) {
  268. //
  269. // Continue to configure the Udp6 instance.
  270. //
  271. Status = Udp6->Configure (Udp6, UdpCfgData);
  272. } else {
  273. Status = EFI_NO_MAPPING;
  274. }
  275. }
  276. }
  277. ON_EXIT:
  278. if (Event != NULL) {
  279. gBS->CloseEvent (Event);
  280. }
  281. return Status;
  282. }
  283. /**
  284. The dummy configure routine for create a new Udp6 Io.
  285. @param[in] UdpIo The pointer to the Udp6 Io.
  286. @param[in] Context The pointer to the context.
  287. @retval EFI_SUCCESS This value is always returned.
  288. **/
  289. EFI_STATUS
  290. EFIAPI
  291. Mtftp6ConfigDummyUdpIo (
  292. IN UDP_IO *UdpIo,
  293. IN VOID *Context
  294. )
  295. {
  296. return EFI_SUCCESS;
  297. }
  298. /**
  299. The configure routine for Mtftp6 instance to transmit/receive.
  300. @param[in] UdpIo The pointer to the Udp6 Io.
  301. @param[in] ServerIp The pointer to the server address.
  302. @param[in] ServerPort The pointer to the server port.
  303. @param[in] LocalIp The pointer to the local address.
  304. @param[in] LocalPort The pointer to the local port.
  305. @retval EFI_SUCCESS Configured the Udp6 Io for Mtftp6 successfully.
  306. @retval EFI_NO_MAPPING The corresponding Ip6 instance has not been
  307. configured yet.
  308. **/
  309. EFI_STATUS
  310. Mtftp6ConfigUdpIo (
  311. IN UDP_IO *UdpIo,
  312. IN EFI_IPv6_ADDRESS *ServerIp,
  313. IN UINT16 ServerPort,
  314. IN EFI_IPv6_ADDRESS *LocalIp,
  315. IN UINT16 LocalPort
  316. )
  317. {
  318. EFI_STATUS Status;
  319. EFI_UDP6_PROTOCOL *Udp6;
  320. EFI_UDP6_CONFIG_DATA *Udp6Cfg;
  321. Udp6 = UdpIo->Protocol.Udp6;
  322. Udp6Cfg = &(UdpIo->Config.Udp6);
  323. ZeroMem (Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA));
  324. //
  325. // Set the Udp6 Io configure data.
  326. //
  327. Udp6Cfg->AcceptPromiscuous = FALSE;
  328. Udp6Cfg->AcceptAnyPort = FALSE;
  329. Udp6Cfg->AllowDuplicatePort = FALSE;
  330. Udp6Cfg->TrafficClass = 0;
  331. Udp6Cfg->HopLimit = 128;
  332. Udp6Cfg->ReceiveTimeout = 0;
  333. Udp6Cfg->TransmitTimeout = 0;
  334. Udp6Cfg->StationPort = LocalPort;
  335. Udp6Cfg->RemotePort = ServerPort;
  336. CopyMem (
  337. &Udp6Cfg->StationAddress,
  338. LocalIp,
  339. sizeof (EFI_IPv6_ADDRESS)
  340. );
  341. CopyMem (
  342. &Udp6Cfg->RemoteAddress,
  343. ServerIp,
  344. sizeof (EFI_IPv6_ADDRESS)
  345. );
  346. //
  347. // Configure the Udp6 instance with current configure data.
  348. //
  349. Status = Udp6->Configure (Udp6, Udp6Cfg);
  350. if (Status == EFI_NO_MAPPING) {
  351. return Mtftp6GetMapping (UdpIo, Udp6Cfg);
  352. }
  353. return Status;
  354. }
  355. /**
  356. Build and transmit the request packet for the Mtftp6 instance.
  357. @param[in] Instance The pointer to the Mtftp6 instance.
  358. @param[in] Operation The operation code of this packet.
  359. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request.
  360. @retval EFI_SUCCESS The request is built and sent.
  361. @retval Others Failed to transmit the packet.
  362. **/
  363. EFI_STATUS
  364. Mtftp6SendRequest (
  365. IN MTFTP6_INSTANCE *Instance,
  366. IN UINT16 Operation
  367. )
  368. {
  369. EFI_MTFTP6_PACKET *Packet;
  370. EFI_MTFTP6_OPTION *Options;
  371. EFI_MTFTP6_TOKEN *Token;
  372. RETURN_STATUS Status;
  373. NET_BUF *Nbuf;
  374. UINT8 *Mode;
  375. UINT8 *Cur;
  376. UINTN Index;
  377. UINT32 BufferLength;
  378. UINTN FileNameLength;
  379. UINTN ModeLength;
  380. UINTN OptionStrLength;
  381. UINTN ValueStrLength;
  382. Token = Instance->Token;
  383. Options = Token->OptionList;
  384. Mode = Token->ModeStr;
  385. if (Mode == NULL) {
  386. Mode = (UINT8 *)"octet";
  387. }
  388. //
  389. // The header format of RRQ/WRQ packet is:
  390. //
  391. // 2 bytes string 1 byte string 1 byte
  392. // ------------------------------------------------
  393. // | Opcode | Filename | 0 | Mode | 0 |
  394. // ------------------------------------------------
  395. //
  396. // The common option format is:
  397. //
  398. // string 1 byte string 1 byte
  399. // ---------------------------------------
  400. // | OptionStr | 0 | ValueStr | 0 |
  401. // ---------------------------------------
  402. //
  403. //
  404. // Compute the size of new Mtftp6 packet.
  405. //
  406. FileNameLength = AsciiStrLen ((CHAR8 *)Token->Filename);
  407. ModeLength = AsciiStrLen ((CHAR8 *)Mode);
  408. BufferLength = (UINT32)FileNameLength + (UINT32)ModeLength + 4;
  409. for (Index = 0; Index < Token->OptionCount; Index++) {
  410. OptionStrLength = AsciiStrLen ((CHAR8 *)Options[Index].OptionStr);
  411. ValueStrLength = AsciiStrLen ((CHAR8 *)Options[Index].ValueStr);
  412. BufferLength += (UINT32)OptionStrLength + (UINT32)ValueStrLength + 2;
  413. }
  414. //
  415. // Allocate a packet then copy the data.
  416. //
  417. if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) {
  418. return EFI_OUT_OF_RESOURCES;
  419. }
  420. //
  421. // Copy the opcode, filename and mode into packet.
  422. //
  423. Packet = (EFI_MTFTP6_PACKET *)NetbufAllocSpace (Nbuf, BufferLength, FALSE);
  424. ASSERT (Packet != NULL);
  425. Packet->OpCode = HTONS (Operation);
  426. BufferLength -= sizeof (Packet->OpCode);
  427. Cur = Packet->Rrq.Filename;
  428. Status = AsciiStrCpyS ((CHAR8 *)Cur, BufferLength, (CHAR8 *)Token->Filename);
  429. ASSERT_EFI_ERROR (Status);
  430. BufferLength -= (UINT32)(FileNameLength + 1);
  431. Cur += FileNameLength + 1;
  432. Status = AsciiStrCpyS ((CHAR8 *)Cur, BufferLength, (CHAR8 *)Mode);
  433. ASSERT_EFI_ERROR (Status);
  434. BufferLength -= (UINT32)(ModeLength + 1);
  435. Cur += ModeLength + 1;
  436. //
  437. // Copy all the extension options into the packet.
  438. //
  439. for (Index = 0; Index < Token->OptionCount; ++Index) {
  440. OptionStrLength = AsciiStrLen ((CHAR8 *)Options[Index].OptionStr);
  441. ValueStrLength = AsciiStrLen ((CHAR8 *)Options[Index].ValueStr);
  442. Status = AsciiStrCpyS ((CHAR8 *)Cur, BufferLength, (CHAR8 *)Options[Index].OptionStr);
  443. ASSERT_EFI_ERROR (Status);
  444. BufferLength -= (UINT32)(OptionStrLength + 1);
  445. Cur += OptionStrLength + 1;
  446. Status = AsciiStrCpyS ((CHAR8 *)Cur, BufferLength, (CHAR8 *)Options[Index].ValueStr);
  447. ASSERT_EFI_ERROR (Status);
  448. BufferLength -= (UINT32)(ValueStrLength + 1);
  449. Cur += ValueStrLength + 1;
  450. }
  451. //
  452. // Save the packet buf for retransmit
  453. //
  454. if (Instance->LastPacket != NULL) {
  455. NetbufFree (Instance->LastPacket);
  456. }
  457. Instance->LastPacket = Nbuf;
  458. Instance->CurRetry = 0;
  459. return Mtftp6TransmitPacket (Instance, Nbuf);
  460. }
  461. /**
  462. Build and send an error packet.
  463. @param[in] Instance The pointer to the Mtftp6 instance.
  464. @param[in] ErrCode The error code in the packet.
  465. @param[in] ErrInfo The error message in the packet.
  466. @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet.
  467. @retval EFI_SUCCESS The error packet is transmitted.
  468. @retval Others Failed to transmit the packet.
  469. **/
  470. EFI_STATUS
  471. Mtftp6SendError (
  472. IN MTFTP6_INSTANCE *Instance,
  473. IN UINT16 ErrCode,
  474. IN UINT8 *ErrInfo
  475. )
  476. {
  477. NET_BUF *Nbuf;
  478. EFI_MTFTP6_PACKET *TftpError;
  479. UINT32 Len;
  480. //
  481. // Allocate a packet then copy the data.
  482. //
  483. Len = (UINT32)(AsciiStrLen ((CHAR8 *)ErrInfo) + sizeof (EFI_MTFTP6_ERROR_HEADER));
  484. Nbuf = NetbufAlloc (Len);
  485. if (Nbuf == NULL) {
  486. return EFI_OUT_OF_RESOURCES;
  487. }
  488. TftpError = (EFI_MTFTP6_PACKET *)NetbufAllocSpace (Nbuf, Len, FALSE);
  489. if (TftpError == NULL) {
  490. NetbufFree (Nbuf);
  491. return EFI_OUT_OF_RESOURCES;
  492. }
  493. TftpError->OpCode = HTONS (EFI_MTFTP6_OPCODE_ERROR);
  494. TftpError->Error.ErrorCode = HTONS (ErrCode);
  495. AsciiStrCpyS ((CHAR8 *)TftpError->Error.ErrorMessage, AsciiStrLen ((CHAR8 *)ErrInfo) + 1, (CHAR8 *)ErrInfo);
  496. //
  497. // Save the packet buf for retransmit
  498. //
  499. if (Instance->LastPacket != NULL) {
  500. NetbufFree (Instance->LastPacket);
  501. }
  502. Instance->LastPacket = Nbuf;
  503. Instance->CurRetry = 0;
  504. return Mtftp6TransmitPacket (Instance, Nbuf);
  505. }
  506. /**
  507. The callback function called when the packet is transmitted.
  508. @param[in] Packet The pointer to the packet.
  509. @param[in] UdpEpt The pointer to the Udp6 access point.
  510. @param[in] IoStatus The result of the transmission.
  511. @param[in] Context The pointer to the context.
  512. **/
  513. VOID
  514. EFIAPI
  515. Mtftp6OnPacketSent (
  516. IN NET_BUF *Packet,
  517. IN UDP_END_POINT *UdpEpt,
  518. IN EFI_STATUS IoStatus,
  519. IN VOID *Context
  520. )
  521. {
  522. NetbufFree (Packet);
  523. *(BOOLEAN *)Context = TRUE;
  524. }
  525. /**
  526. Send the packet for the Mtftp6 instance.
  527. @param[in] Instance The pointer to the Mtftp6 instance.
  528. @param[in] Packet The pointer to the packet to be sent.
  529. @retval EFI_SUCCESS The packet was sent out
  530. @retval Others Failed to transmit the packet.
  531. **/
  532. EFI_STATUS
  533. Mtftp6TransmitPacket (
  534. IN MTFTP6_INSTANCE *Instance,
  535. IN NET_BUF *Packet
  536. )
  537. {
  538. EFI_UDP6_PROTOCOL *Udp6;
  539. EFI_UDP6_CONFIG_DATA Udp6CfgData;
  540. EFI_STATUS Status;
  541. UINT16 *Temp;
  542. UINT16 Value;
  543. UINT16 OpCode;
  544. ZeroMem (&Udp6CfgData, sizeof (EFI_UDP6_CONFIG_DATA));
  545. Udp6 = Instance->UdpIo->Protocol.Udp6;
  546. //
  547. // Set the live time of the packet.
  548. //
  549. Instance->PacketToLive = Instance->IsMaster ? Instance->Timeout : (Instance->Timeout * 2);
  550. Temp = (UINT16 *)NetbufGetByte (Packet, 0, NULL);
  551. ASSERT (Temp != NULL);
  552. Value = *Temp;
  553. OpCode = NTOHS (Value);
  554. if ((OpCode == EFI_MTFTP6_OPCODE_RRQ) || (OpCode == EFI_MTFTP6_OPCODE_DIR) || (OpCode == EFI_MTFTP6_OPCODE_WRQ)) {
  555. //
  556. // For the Rrq, Dir, Wrq requests of the operation, configure the Udp6Io as
  557. // (serverip, 69, localip, localport) to send.
  558. // Usually local address and local port are both default as zero.
  559. //
  560. Status = Udp6->Configure (Udp6, NULL);
  561. if (EFI_ERROR (Status)) {
  562. return Status;
  563. }
  564. Status = Mtftp6ConfigUdpIo (
  565. Instance->UdpIo,
  566. &Instance->ServerIp,
  567. Instance->ServerCmdPort,
  568. &Instance->Config->StationIp,
  569. Instance->Config->LocalPort
  570. );
  571. if (EFI_ERROR (Status)) {
  572. return Status;
  573. }
  574. //
  575. // Get the current local address and port by get Udp6 mode data.
  576. //
  577. Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);
  578. if (EFI_ERROR (Status)) {
  579. return Status;
  580. }
  581. NET_GET_REF (Packet);
  582. Instance->IsTransmitted = FALSE;
  583. Status = UdpIoSendDatagram (
  584. Instance->UdpIo,
  585. Packet,
  586. NULL,
  587. NULL,
  588. Mtftp6OnPacketSent,
  589. &Instance->IsTransmitted
  590. );
  591. if (EFI_ERROR (Status)) {
  592. NET_PUT_REF (Packet);
  593. return Status;
  594. }
  595. //
  596. // Poll till the packet sent out from the ip6 queue.
  597. //
  598. gBS->RestoreTPL (Instance->OldTpl);
  599. while (!Instance->IsTransmitted) {
  600. Udp6->Poll (Udp6);
  601. }
  602. Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  603. //
  604. // For the subsequent exchange of such requests, reconfigure the Udp6Io as
  605. // (serverip, 0, localip, localport) to receive.
  606. // Currently local address and local port are specified by Udp6 mode data.
  607. //
  608. Status = Udp6->Configure (Udp6, NULL);
  609. if (EFI_ERROR (Status)) {
  610. return Status;
  611. }
  612. Status = Mtftp6ConfigUdpIo (
  613. Instance->UdpIo,
  614. &Instance->ServerIp,
  615. Instance->ServerDataPort,
  616. &Udp6CfgData.StationAddress,
  617. Udp6CfgData.StationPort
  618. );
  619. } else {
  620. //
  621. // For the data exchange, configure the Udp6Io as (serverip, dataport,
  622. // localip, localport) to send/receive.
  623. // Currently local address and local port are specified by Udp6 mode data.
  624. //
  625. Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);
  626. if (EFI_ERROR (Status)) {
  627. return Status;
  628. }
  629. if (Udp6CfgData.RemotePort != Instance->ServerDataPort) {
  630. Status = Udp6->Configure (Udp6, NULL);
  631. if (EFI_ERROR (Status)) {
  632. return Status;
  633. }
  634. Status = Mtftp6ConfigUdpIo (
  635. Instance->UdpIo,
  636. &Instance->ServerIp,
  637. Instance->ServerDataPort,
  638. &Udp6CfgData.StationAddress,
  639. Udp6CfgData.StationPort
  640. );
  641. if (EFI_ERROR (Status)) {
  642. return Status;
  643. }
  644. }
  645. NET_GET_REF (Packet);
  646. Instance->IsTransmitted = FALSE;
  647. Status = UdpIoSendDatagram (
  648. Instance->UdpIo,
  649. Packet,
  650. NULL,
  651. NULL,
  652. Mtftp6OnPacketSent,
  653. &Instance->IsTransmitted
  654. );
  655. if (EFI_ERROR (Status)) {
  656. NET_PUT_REF (Packet);
  657. }
  658. //
  659. // Poll till the packet sent out from the ip6 queue.
  660. //
  661. gBS->RestoreTPL (Instance->OldTpl);
  662. while (!Instance->IsTransmitted) {
  663. Udp6->Poll (Udp6);
  664. }
  665. Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  666. }
  667. return Status;
  668. }
  669. /**
  670. Check packet for GetInfo callback routine.
  671. GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect
  672. the first packet from server, then abort the session.
  673. @param[in] This The pointer to the Mtftp6 protocol.
  674. @param[in] Token The pointer to the Mtftp6 token.
  675. @param[in] PacketLen The length of the packet.
  676. @param[in] Packet The pointer to the received packet.
  677. @retval EFI_ABORTED Abort the Mtftp6 operation.
  678. **/
  679. EFI_STATUS
  680. EFIAPI
  681. Mtftp6CheckPacket (
  682. IN EFI_MTFTP6_PROTOCOL *This,
  683. IN EFI_MTFTP6_TOKEN *Token,
  684. IN UINT16 PacketLen,
  685. IN EFI_MTFTP6_PACKET *Packet
  686. )
  687. {
  688. MTFTP6_GETINFO_CONTEXT *Context;
  689. UINT16 OpCode;
  690. Context = (MTFTP6_GETINFO_CONTEXT *)Token->Context;
  691. OpCode = NTOHS (Packet->OpCode);
  692. //
  693. // Set the GetInfo's return status according to the OpCode.
  694. //
  695. switch (OpCode) {
  696. case EFI_MTFTP6_OPCODE_ERROR:
  697. Context->Status = EFI_TFTP_ERROR;
  698. break;
  699. case EFI_MTFTP6_OPCODE_OACK:
  700. Context->Status = EFI_SUCCESS;
  701. break;
  702. default:
  703. Context->Status = EFI_PROTOCOL_ERROR;
  704. }
  705. //
  706. // Allocate buffer then copy the packet over. Use gBS->AllocatePool
  707. // in case NetAllocatePool will implements something tricky.
  708. //
  709. *(Context->Packet) = AllocateZeroPool (PacketLen);
  710. if (*(Context->Packet) == NULL) {
  711. Context->Status = EFI_OUT_OF_RESOURCES;
  712. return EFI_ABORTED;
  713. }
  714. *(Context->PacketLen) = PacketLen;
  715. CopyMem (*(Context->Packet), Packet, PacketLen);
  716. return EFI_ABORTED;
  717. }
  718. /**
  719. Clean up the current Mtftp6 operation.
  720. @param[in] Instance The pointer to the Mtftp6 instance.
  721. @param[in] Result The result to be returned to the user.
  722. **/
  723. VOID
  724. Mtftp6OperationClean (
  725. IN MTFTP6_INSTANCE *Instance,
  726. IN EFI_STATUS Result
  727. )
  728. {
  729. LIST_ENTRY *Entry;
  730. LIST_ENTRY *Next;
  731. MTFTP6_BLOCK_RANGE *Block;
  732. //
  733. // Clean up the current token and event.
  734. //
  735. if (Instance->Token != NULL) {
  736. Instance->Token->Status = Result;
  737. if (Instance->Token->Event != NULL) {
  738. gBS->SignalEvent (Instance->Token->Event);
  739. }
  740. Instance->Token = NULL;
  741. }
  742. //
  743. // Clean up the corresponding Udp6Io.
  744. //
  745. if (Instance->UdpIo != NULL) {
  746. UdpIoCleanIo (Instance->UdpIo);
  747. }
  748. if (Instance->McastUdpIo != NULL) {
  749. gBS->CloseProtocol (
  750. Instance->McastUdpIo->UdpHandle,
  751. &gEfiUdp6ProtocolGuid,
  752. Instance->McastUdpIo->Image,
  753. Instance->Handle
  754. );
  755. UdpIoFreeIo (Instance->McastUdpIo);
  756. Instance->McastUdpIo = NULL;
  757. }
  758. //
  759. // Clean up the stored last packet.
  760. //
  761. if (Instance->LastPacket != NULL) {
  762. NetbufFree (Instance->LastPacket);
  763. Instance->LastPacket = NULL;
  764. }
  765. NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->BlkList) {
  766. Block = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);
  767. RemoveEntryList (Entry);
  768. FreePool (Block);
  769. }
  770. //
  771. // Reinitialize the corresponding fields of the Mtftp6 operation.
  772. //
  773. ZeroMem (&Instance->ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));
  774. ZeroMem (&Instance->ServerIp, sizeof (EFI_IPv6_ADDRESS));
  775. ZeroMem (&Instance->McastIp, sizeof (EFI_IPv6_ADDRESS));
  776. Instance->ServerCmdPort = 0;
  777. Instance->ServerDataPort = 0;
  778. Instance->McastPort = 0;
  779. Instance->BlkSize = 0;
  780. Instance->Operation = 0;
  781. Instance->WindowSize = 1;
  782. Instance->TotalBlock = 0;
  783. Instance->AckedBlock = 0;
  784. Instance->LastBlk = 0;
  785. Instance->PacketToLive = 0;
  786. Instance->MaxRetry = 0;
  787. Instance->CurRetry = 0;
  788. Instance->Timeout = 0;
  789. Instance->IsMaster = TRUE;
  790. }
  791. /**
  792. Start the Mtftp6 instance to perform the operation, such as read file,
  793. write file, and read directory.
  794. @param[in] This The MTFTP session.
  795. @param[in] Token The token than encapsules the user's request.
  796. @param[in] OpCode The operation to perform.
  797. @retval EFI_INVALID_PARAMETER Some of the parameters are invalid.
  798. @retval EFI_NOT_STARTED The MTFTP session hasn't been configured.
  799. @retval EFI_ALREADY_STARTED There is pending operation for the session.
  800. @retval EFI_SUCCESS The operation is successfully started.
  801. **/
  802. EFI_STATUS
  803. Mtftp6OperationStart (
  804. IN EFI_MTFTP6_PROTOCOL *This,
  805. IN EFI_MTFTP6_TOKEN *Token,
  806. IN UINT16 OpCode
  807. )
  808. {
  809. MTFTP6_INSTANCE *Instance;
  810. EFI_STATUS Status;
  811. if ((This == NULL) ||
  812. (Token == NULL) ||
  813. (Token->Filename == NULL) ||
  814. ((Token->OptionCount != 0) && (Token->OptionList == NULL)) ||
  815. ((Token->OverrideData != NULL) && !NetIp6IsValidUnicast (&Token->OverrideData->ServerIp))
  816. )
  817. {
  818. return EFI_INVALID_PARAMETER;
  819. }
  820. //
  821. // At least define one method to collect the data for download.
  822. //
  823. if (((OpCode == EFI_MTFTP6_OPCODE_RRQ) || (OpCode == EFI_MTFTP6_OPCODE_DIR)) &&
  824. (Token->Buffer == NULL) &&
  825. (Token->CheckPacket == NULL)
  826. )
  827. {
  828. return EFI_INVALID_PARAMETER;
  829. }
  830. //
  831. // At least define one method to provide the data for upload.
  832. //
  833. if ((OpCode == EFI_MTFTP6_OPCODE_WRQ) && (Token->Buffer == NULL) && (Token->PacketNeeded == NULL)) {
  834. return EFI_INVALID_PARAMETER;
  835. }
  836. Instance = MTFTP6_INSTANCE_FROM_THIS (This);
  837. if (Instance->Config == NULL) {
  838. return EFI_NOT_STARTED;
  839. }
  840. if (Instance->Token != NULL) {
  841. return EFI_ACCESS_DENIED;
  842. }
  843. Status = EFI_SUCCESS;
  844. Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  845. Instance->Operation = OpCode;
  846. //
  847. // Parse the extension options in the request packet.
  848. //
  849. if (Token->OptionCount != 0) {
  850. Status = Mtftp6ParseExtensionOption (
  851. Token->OptionList,
  852. Token->OptionCount,
  853. TRUE,
  854. Instance->Operation,
  855. &Instance->ExtInfo
  856. );
  857. if (EFI_ERROR (Status)) {
  858. goto ON_ERROR;
  859. }
  860. }
  861. //
  862. // Initialize runtime data from config data or override data.
  863. //
  864. Instance->Token = Token;
  865. Instance->ServerCmdPort = Instance->Config->InitialServerPort;
  866. Instance->ServerDataPort = 0;
  867. Instance->MaxRetry = Instance->Config->TryCount;
  868. Instance->Timeout = Instance->Config->TimeoutValue;
  869. Instance->IsMaster = TRUE;
  870. CopyMem (
  871. &Instance->ServerIp,
  872. &Instance->Config->ServerIp,
  873. sizeof (EFI_IPv6_ADDRESS)
  874. );
  875. if (Token->OverrideData != NULL) {
  876. Instance->ServerCmdPort = Token->OverrideData->ServerPort;
  877. Instance->MaxRetry = Token->OverrideData->TryCount;
  878. Instance->Timeout = Token->OverrideData->TimeoutValue;
  879. CopyMem (
  880. &Instance->ServerIp,
  881. &Token->OverrideData->ServerIp,
  882. sizeof (EFI_IPv6_ADDRESS)
  883. );
  884. }
  885. //
  886. // Set default value for undefined parameters.
  887. //
  888. if (Instance->ServerCmdPort == 0) {
  889. Instance->ServerCmdPort = MTFTP6_DEFAULT_SERVER_CMD_PORT;
  890. }
  891. if (Instance->BlkSize == 0) {
  892. Instance->BlkSize = MTFTP6_DEFAULT_BLK_SIZE;
  893. }
  894. if (Instance->WindowSize == 0) {
  895. Instance->WindowSize = MTFTP6_DEFAULT_WINDOWSIZE;
  896. }
  897. if (Instance->MaxRetry == 0) {
  898. Instance->MaxRetry = MTFTP6_DEFAULT_MAX_RETRY;
  899. }
  900. if (Instance->Timeout == 0) {
  901. Instance->Timeout = MTFTP6_DEFAULT_TIMEOUT;
  902. }
  903. Token->Status = EFI_NOT_READY;
  904. //
  905. // Switch the routines by the operation code.
  906. //
  907. switch (OpCode) {
  908. case EFI_MTFTP6_OPCODE_RRQ:
  909. Status = Mtftp6RrqStart (Instance, OpCode);
  910. break;
  911. case EFI_MTFTP6_OPCODE_DIR:
  912. Status = Mtftp6RrqStart (Instance, OpCode);
  913. break;
  914. case EFI_MTFTP6_OPCODE_WRQ:
  915. Status = Mtftp6WrqStart (Instance, OpCode);
  916. break;
  917. default:
  918. Status = EFI_DEVICE_ERROR;
  919. goto ON_ERROR;
  920. }
  921. if (EFI_ERROR (Status)) {
  922. goto ON_ERROR;
  923. }
  924. //
  925. // Return immediately for asynchronous or poll the instance for synchronous.
  926. //
  927. gBS->RestoreTPL (Instance->OldTpl);
  928. if (Token->Event == NULL) {
  929. while (Token->Status == EFI_NOT_READY) {
  930. This->Poll (This);
  931. }
  932. return Token->Status;
  933. }
  934. return EFI_SUCCESS;
  935. ON_ERROR:
  936. Mtftp6OperationClean (Instance, Status);
  937. gBS->RestoreTPL (Instance->OldTpl);
  938. return Status;
  939. }
  940. /**
  941. The timer ticking routine for the Mtftp6 instance.
  942. @param[in] Event The pointer to the ticking event.
  943. @param[in] Context The pointer to the context.
  944. **/
  945. VOID
  946. EFIAPI
  947. Mtftp6OnTimerTick (
  948. IN EFI_EVENT Event,
  949. IN VOID *Context
  950. )
  951. {
  952. MTFTP6_SERVICE *Service;
  953. MTFTP6_INSTANCE *Instance;
  954. LIST_ENTRY *Entry;
  955. LIST_ENTRY *Next;
  956. EFI_MTFTP6_TOKEN *Token;
  957. EFI_STATUS Status;
  958. Service = (MTFTP6_SERVICE *)Context;
  959. //
  960. // Iterate through all the children of the Mtftp service instance. Time
  961. // out the packet. If maximum retries reached, clean the session up.
  962. //
  963. NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Children) {
  964. Instance = NET_LIST_USER_STRUCT (Entry, MTFTP6_INSTANCE, Link);
  965. if (Instance->Token == NULL) {
  966. continue;
  967. }
  968. if (Instance->PacketToLive > 0) {
  969. Instance->PacketToLive--;
  970. continue;
  971. }
  972. Instance->CurRetry++;
  973. Token = Instance->Token;
  974. if (Token->TimeoutCallback != NULL) {
  975. //
  976. // Call the timeout callback routine if has.
  977. //
  978. Status = Token->TimeoutCallback (&Instance->Mtftp6, Token);
  979. if (EFI_ERROR (Status)) {
  980. Mtftp6SendError (
  981. Instance,
  982. EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,
  983. (UINT8 *)"User aborted the transfer in time out"
  984. );
  985. Mtftp6OperationClean (Instance, EFI_ABORTED);
  986. continue;
  987. }
  988. }
  989. //
  990. // Retransmit the packet if haven't reach the maximum retry count,
  991. // otherwise exit the transfer.
  992. //
  993. if (Instance->CurRetry < Instance->MaxRetry) {
  994. Mtftp6TransmitPacket (Instance, Instance->LastPacket);
  995. } else {
  996. Mtftp6OperationClean (Instance, EFI_TIMEOUT);
  997. continue;
  998. }
  999. }
  1000. }