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