]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
1. Add EFI_COMPONENT_NAME2_PROTOCOL.GetControllerName() support.
[mirror_edk2.git] / NetworkPkg / Mtftp6Dxe / Mtftp6Support.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Mtftp6 support functions implementation.\r
3\r
216f7970 4 Copyright (c) 2009 - 2012, 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
515 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Token->Filename);\r
516 Cur += AsciiStrLen ((CHAR8 *) Token->Filename) + 1;\r
517 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Mode);\r
518 Cur += AsciiStrLen ((CHAR8 *) Mode) + 1;\r
519\r
520 //\r
521 // Copy all the extension options into the packet.\r
522 //\r
523 for (Index = 0; Index < Token->OptionCount; ++Index) {\r
524 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Options[Index].OptionStr);\r
525 Cur += AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1;\r
526 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Options[Index].ValueStr);\r
527 Cur += AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1;\r
528 }\r
529\r
530 //\r
531 // Save the packet buf for retransmit\r
532 //\r
533 if (Instance->LastPacket != NULL) {\r
534 NetbufFree (Instance->LastPacket);\r
535 }\r
536\r
537 Instance->LastPacket = Nbuf;\r
538 Instance->CurRetry = 0;\r
539\r
540 return Mtftp6TransmitPacket (Instance, Nbuf);\r
541}\r
542\r
543\r
544/**\r
545 Build and send an error packet.\r
546\r
547 @param[in] Instance The pointer to the Mtftp6 instance.\r
548 @param[in] ErrCode The error code in the packet.\r
549 @param[in] ErrInfo The error message in the packet.\r
550\r
551 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet.\r
552 @retval EFI_SUCCESS The error packet is transmitted.\r
553 @retval Others Failed to transmit the packet.\r
554\r
555**/\r
556EFI_STATUS\r
557Mtftp6SendError (\r
558 IN MTFTP6_INSTANCE *Instance,\r
559 IN UINT16 ErrCode,\r
560 IN UINT8* ErrInfo\r
561 )\r
562{\r
563 NET_BUF *Nbuf;\r
564 EFI_MTFTP6_PACKET *TftpError;\r
565 UINT32 Len;\r
566\r
567 //\r
568 // Allocate a packet then copy the data.\r
569 //\r
570 Len = (UINT32) (AsciiStrLen ((CHAR8 *) ErrInfo) + sizeof (EFI_MTFTP6_ERROR_HEADER));\r
571 Nbuf = NetbufAlloc (Len);\r
572\r
573 if (Nbuf == NULL) {\r
574 return EFI_OUT_OF_RESOURCES;\r
575 }\r
576\r
577 TftpError = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);\r
578\r
579 if (TftpError == NULL) {\r
580 NetbufFree (Nbuf);\r
581 return EFI_OUT_OF_RESOURCES;\r
582 }\r
583\r
584 TftpError->OpCode = HTONS (EFI_MTFTP6_OPCODE_ERROR);\r
585 TftpError->Error.ErrorCode = HTONS (ErrCode);\r
586\r
587 AsciiStrCpy ((CHAR8 *) TftpError->Error.ErrorMessage, (CHAR8 *) ErrInfo);\r
588\r
589 //\r
590 // Save the packet buf for retransmit\r
591 //\r
592 if (Instance->LastPacket != NULL) {\r
593 NetbufFree (Instance->LastPacket);\r
594 }\r
595\r
596 Instance->LastPacket = Nbuf;\r
597 Instance->CurRetry = 0;\r
598\r
599 return Mtftp6TransmitPacket (Instance, Nbuf);\r
600}\r
601\r
602\r
603/**\r
604 The callback function called when the packet is transmitted.\r
605\r
606 @param[in] Packet The pointer to the packet.\r
607 @param[in] UdpEpt The pointer to the Udp6 access point.\r
608 @param[in] IoStatus The result of the transmission.\r
609 @param[in] Context The pointer to the context.\r
610\r
611**/\r
612VOID\r
613EFIAPI\r
614Mtftp6OnPacketSent (\r
615 IN NET_BUF *Packet,\r
616 IN UDP_END_POINT *UdpEpt,\r
617 IN EFI_STATUS IoStatus,\r
618 IN VOID *Context\r
619 )\r
620{\r
621 NetbufFree (Packet);\r
622 *(BOOLEAN *) Context = TRUE;\r
623}\r
624\r
625\r
626/**\r
627 Send the packet for the Mtftp6 instance.\r
628\r
629 @param[in] Instance The pointer to the Mtftp6 instance.\r
630 @param[in] Packet The pointer to the packet to be sent.\r
631\r
632 @retval EFI_SUCCESS The packet was sent out\r
633 @retval Others Failed to transmit the packet.\r
634\r
635**/\r
636EFI_STATUS\r
637Mtftp6TransmitPacket (\r
638 IN MTFTP6_INSTANCE *Instance,\r
639 IN NET_BUF *Packet\r
640 )\r
641{\r
642 EFI_UDP6_PROTOCOL *Udp6;\r
643 EFI_UDP6_CONFIG_DATA Udp6CfgData;\r
644 EFI_STATUS Status;\r
645 UINT16 *Temp;\r
646 UINT16 Value;\r
647 UINT16 OpCode;\r
648\r
649 ZeroMem (&Udp6CfgData, sizeof(EFI_UDP6_CONFIG_DATA));\r
650 Udp6 = Instance->UdpIo->Protocol.Udp6;\r
651\r
652 //\r
653 // Set the live time of the packet.\r
654 //\r
655 Instance->PacketToLive = Instance->IsMaster ? Instance->Timeout : (Instance->Timeout * 2);\r
656\r
657 Temp = (UINT16 *) NetbufGetByte (Packet, 0, NULL);\r
658 ASSERT (Temp != NULL);\r
659\r
660 Value = *Temp;\r
661 OpCode = NTOHS (Value);\r
662\r
663 if (OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR || OpCode == EFI_MTFTP6_OPCODE_WRQ) {\r
664 //\r
665 // For the Rrq, Dir, Wrq requests of the operation, configure the Udp6Io as\r
666 // (serverip, 69, localip, localport) to send.\r
667 // Usually local address and local port are both default as zero.\r
668 //\r
669 Status = Udp6->Configure (Udp6, NULL);\r
670\r
671 if (EFI_ERROR (Status)) {\r
672 return Status;\r
673 }\r
674\r
675 Status = Mtftp6ConfigUdpIo (\r
676 Instance->UdpIo,\r
677 &Instance->ServerIp,\r
678 Instance->ServerCmdPort,\r
679 &Instance->Config->StationIp,\r
680 Instance->Config->LocalPort\r
681 );\r
682\r
683 if (EFI_ERROR (Status)) {\r
684 return Status;\r
685 }\r
686\r
687 //\r
688 // Get the current local address and port by get Udp6 mode data.\r
689 //\r
690 Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);\r
691 if (EFI_ERROR (Status)) {\r
692 return Status;\r
693 }\r
694\r
695 NET_GET_REF (Packet);\r
696\r
697 Instance->IsTransmitted = FALSE;\r
698\r
699 Status = UdpIoSendDatagram (\r
700 Instance->UdpIo,\r
701 Packet,\r
702 NULL,\r
703 NULL,\r
704 Mtftp6OnPacketSent,\r
705 &Instance->IsTransmitted\r
706 );\r
707\r
708 if (EFI_ERROR (Status)) {\r
709 NET_PUT_REF (Packet);\r
710 return Status;\r
711 }\r
712\r
713 //\r
714 // Poll till the packet sent out from the ip6 queue.\r
715 //\r
716 gBS->RestoreTPL (Instance->OldTpl);\r
717\r
718 while (!Instance->IsTransmitted) {\r
719 Udp6->Poll (Udp6);\r
720 }\r
721\r
722 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
723\r
724 //\r
725 // For the subsequent exchange of such requests, reconfigure the Udp6Io as\r
726 // (serverip, 0, localip, localport) to receive.\r
727 // Currently local address and local port are specified by Udp6 mode data.\r
728 //\r
729 Status = Udp6->Configure (Udp6, NULL);\r
730\r
731 if (EFI_ERROR (Status)) {\r
732 return Status;\r
733 }\r
734\r
735 Status = Mtftp6ConfigUdpIo (\r
736 Instance->UdpIo,\r
737 &Instance->ServerIp,\r
738 Instance->ServerDataPort,\r
739 &Udp6CfgData.StationAddress,\r
740 Udp6CfgData.StationPort\r
741 );\r
742 } else {\r
743 //\r
744 // For the data exchange, configure the Udp6Io as (serverip, dataport,\r
745 // localip, localport) to send/receive.\r
746 // Currently local address and local port are specified by Udp6 mode data.\r
747 //\r
748 Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);\r
749 if (EFI_ERROR (Status)) {\r
750 return Status;\r
751 }\r
752\r
753 if (Udp6CfgData.RemotePort != Instance->ServerDataPort) {\r
754\r
755 Status = Udp6->Configure (Udp6, NULL);\r
756\r
757 if (EFI_ERROR (Status)) {\r
758 return Status;\r
759 }\r
760\r
761 Status = Mtftp6ConfigUdpIo (\r
762 Instance->UdpIo,\r
763 &Instance->ServerIp,\r
764 Instance->ServerDataPort,\r
765 &Udp6CfgData.StationAddress,\r
766 Udp6CfgData.StationPort\r
767 );\r
768\r
769 if (EFI_ERROR (Status)) {\r
770 return Status;\r
771 }\r
772 }\r
773\r
774 NET_GET_REF (Packet);\r
775\r
776 Instance->IsTransmitted = FALSE;\r
777\r
778 Status = UdpIoSendDatagram (\r
779 Instance->UdpIo,\r
780 Packet,\r
781 NULL,\r
782 NULL,\r
783 Mtftp6OnPacketSent,\r
784 &Instance->IsTransmitted\r
785 );\r
786\r
787 if (EFI_ERROR (Status)) {\r
788 NET_PUT_REF (Packet);\r
789 }\r
790\r
791 //\r
792 // Poll till the packet sent out from the ip6 queue.\r
793 //\r
794 gBS->RestoreTPL (Instance->OldTpl);\r
795\r
796 while (!Instance->IsTransmitted) {\r
797 Udp6->Poll (Udp6);\r
798 }\r
799\r
800 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
801 }\r
802\r
803 return Status;\r
804}\r
805\r
806\r
807/**\r
808 Check packet for GetInfo callback routine.\r
809\r
810 GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect\r
811 the first packet from server, then abort the session.\r
812\r
813 @param[in] This The pointer to the Mtftp6 protocol.\r
814 @param[in] Token The pointer to the Mtftp6 token.\r
815 @param[in] PacketLen The length of the packet.\r
816 @param[in] Packet The pointer to the received packet.\r
817\r
818 @retval EFI_ABORTED Abort the Mtftp6 operation.\r
819\r
820**/\r
821EFI_STATUS\r
822EFIAPI\r
823Mtftp6CheckPacket (\r
824 IN EFI_MTFTP6_PROTOCOL *This,\r
825 IN EFI_MTFTP6_TOKEN *Token,\r
826 IN UINT16 PacketLen,\r
827 IN EFI_MTFTP6_PACKET *Packet\r
828 )\r
829{\r
830 MTFTP6_GETINFO_CONTEXT *Context;\r
831 UINT16 OpCode;\r
832\r
833 Context = (MTFTP6_GETINFO_CONTEXT *) Token->Context;\r
834 OpCode = NTOHS (Packet->OpCode);\r
835\r
836 //\r
837 // Set the GetInfo's return status according to the OpCode.\r
838 //\r
839 switch (OpCode) {\r
840 case EFI_MTFTP6_OPCODE_ERROR:\r
841 Context->Status = EFI_TFTP_ERROR;\r
842 break;\r
843\r
844 case EFI_MTFTP6_OPCODE_OACK:\r
845 Context->Status = EFI_SUCCESS;\r
846 break;\r
847\r
848 default:\r
849 Context->Status = EFI_PROTOCOL_ERROR;\r
850 }\r
851\r
852 //\r
853 // Allocate buffer then copy the packet over. Use gBS->AllocatePool\r
854 // in case NetAllocatePool will implements something tricky.\r
855 //\r
856 *(Context->Packet) = AllocateZeroPool (PacketLen);\r
857\r
858 if (*(Context->Packet) == NULL) {\r
859 Context->Status = EFI_OUT_OF_RESOURCES;\r
860 return EFI_ABORTED;\r
861 }\r
862\r
863 *(Context->PacketLen) = PacketLen;\r
864 CopyMem (*(Context->Packet), Packet, PacketLen);\r
865\r
866 return EFI_ABORTED;\r
867}\r
868\r
869\r
870/**\r
871 Clean up the current Mtftp6 operation.\r
872\r
873 @param[in] Instance The pointer to the Mtftp6 instance.\r
874 @param[in] Result The result to be returned to the user.\r
875\r
876**/\r
877VOID\r
878Mtftp6OperationClean (\r
879 IN MTFTP6_INSTANCE *Instance,\r
880 IN EFI_STATUS Result\r
881 )\r
882{\r
883 LIST_ENTRY *Entry;\r
884 LIST_ENTRY *Next;\r
885 MTFTP6_BLOCK_RANGE *Block;\r
886\r
887 //\r
888 // Clean up the current token and event.\r
889 //\r
890 if (Instance->Token != NULL) {\r
891 Instance->Token->Status = Result;\r
892 if (Instance->Token->Event != NULL) {\r
893 gBS->SignalEvent (Instance->Token->Event);\r
894 }\r
895 Instance->Token = NULL;\r
896 }\r
897\r
898 //\r
899 // Clean up the corresponding Udp6Io.\r
900 //\r
901 if (Instance->UdpIo != NULL) {\r
902 UdpIoCleanIo (Instance->UdpIo);\r
903 }\r
904\r
905 if (Instance->McastUdpIo != NULL) {\r
216f7970 906 gBS->CloseProtocol (\r
907 Instance->McastUdpIo->UdpHandle,\r
908 &gEfiUdp6ProtocolGuid,\r
909 Instance->McastUdpIo->Image,\r
910 Instance->Handle\r
911 );\r
a3bcde70
HT
912 UdpIoFreeIo (Instance->McastUdpIo);\r
913 Instance->McastUdpIo = NULL;\r
914 }\r
915\r
916 //\r
917 // Clean up the stored last packet.\r
918 //\r
919 if (Instance->LastPacket != NULL) {\r
920 NetbufFree (Instance->LastPacket);\r
921 Instance->LastPacket = NULL;\r
922 }\r
923\r
924 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->BlkList) {\r
925 Block = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);\r
926 RemoveEntryList (Entry);\r
927 FreePool (Block);\r
928 }\r
929\r
930 //\r
931 // Reinitialize the corresponding fields of the Mtftp6 operation.\r
932 //\r
933 ZeroMem (&Instance->ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));\r
934 ZeroMem (&Instance->ServerIp, sizeof (EFI_IPv6_ADDRESS));\r
935 ZeroMem (&Instance->McastIp, sizeof (EFI_IPv6_ADDRESS));\r
936\r
937 Instance->ServerCmdPort = 0;\r
938 Instance->ServerDataPort = 0;\r
939 Instance->McastPort = 0;\r
940 Instance->BlkSize = 0;\r
941 Instance->LastBlk = 0;\r
942 Instance->PacketToLive = 0;\r
943 Instance->MaxRetry = 0;\r
944 Instance->CurRetry = 0;\r
945 Instance->Timeout = 0;\r
946 Instance->IsMaster = TRUE;\r
947}\r
948\r
949\r
950/**\r
951 Start the Mtftp6 instance to perform the operation, such as read file,\r
952 write file, and read directory.\r
953\r
954 @param[in] This The MTFTP session.\r
955 @param[in] Token The token than encapsues the user's request.\r
956 @param[in] OpCode The operation to perform.\r
957\r
958 @retval EFI_INVALID_PARAMETER Some of the parameters are invalid.\r
959 @retval EFI_NOT_STARTED The MTFTP session hasn't been configured.\r
960 @retval EFI_ALREADY_STARTED There is pending operation for the session.\r
961 @retval EFI_SUCCESS The operation is successfully started.\r
962\r
963**/\r
964EFI_STATUS\r
965Mtftp6OperationStart (\r
966 IN EFI_MTFTP6_PROTOCOL *This,\r
967 IN EFI_MTFTP6_TOKEN *Token,\r
968 IN UINT16 OpCode\r
969 )\r
970{\r
971 MTFTP6_INSTANCE *Instance;\r
972 EFI_STATUS Status;\r
973\r
974 if (This == NULL ||\r
975 Token == NULL ||\r
976 Token->Filename == NULL ||\r
977 (Token->OptionCount != 0 && Token->OptionList == NULL) ||\r
978 (Token->OverrideData != NULL && !NetIp6IsValidUnicast (&Token->OverrideData->ServerIp))\r
979 ) {\r
980 return EFI_INVALID_PARAMETER;\r
981 }\r
982\r
983 //\r
984 // At least define one method to collect the data for download.\r
985 //\r
986 if ((OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR) &&\r
987 Token->Buffer == NULL &&\r
988 Token->CheckPacket == NULL\r
989 ) {\r
990 return EFI_INVALID_PARAMETER;\r
991 }\r
992\r
993 //\r
994 // At least define one method to provide the data for upload.\r
995 //\r
996 if (OpCode == EFI_MTFTP6_OPCODE_WRQ && Token->Buffer == NULL && Token->PacketNeeded == NULL) {\r
997 return EFI_INVALID_PARAMETER;\r
998 }\r
999\r
1000 Instance = MTFTP6_INSTANCE_FROM_THIS (This);\r
1001\r
1002 if (Instance->Config == NULL) {\r
1003 return EFI_NOT_STARTED;\r
1004 }\r
1005\r
1006 if (Instance->Token != NULL) {\r
1007 return EFI_ACCESS_DENIED;\r
1008 }\r
1009\r
1010 Status = EFI_SUCCESS;\r
1011 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1012\r
1013 //\r
1014 // Parse the extension options in the request packet.\r
1015 //\r
1016 if (Token->OptionCount != 0) {\r
1017\r
1018 Status = Mtftp6ParseExtensionOption (\r
1019 Token->OptionList,\r
1020 Token->OptionCount,\r
1021 TRUE,\r
1022 &Instance->ExtInfo\r
1023 );\r
1024\r
1025 if (EFI_ERROR (Status)) {\r
1026 goto ON_ERROR;\r
1027 }\r
1028 }\r
1029\r
1030 //\r
1031 // Initialize runtime data from config data or override data.\r
1032 //\r
1033 Instance->Token = Token;\r
1034 Instance->ServerCmdPort = Instance->Config->InitialServerPort;\r
1035 Instance->ServerDataPort = 0;\r
1036 Instance->MaxRetry = Instance->Config->TryCount;\r
1037 Instance->Timeout = Instance->Config->TimeoutValue;\r
1038 Instance->IsMaster = TRUE;\r
1039\r
1040 CopyMem (\r
1041 &Instance->ServerIp,\r
1042 &Instance->Config->ServerIp,\r
1043 sizeof (EFI_IPv6_ADDRESS)\r
1044 );\r
1045\r
1046 if (Token->OverrideData != NULL) {\r
1047 Instance->ServerCmdPort = Token->OverrideData->ServerPort;\r
1048 Instance->MaxRetry = Token->OverrideData->TryCount;\r
1049 Instance->Timeout = Token->OverrideData->TimeoutValue;\r
1050\r
1051 CopyMem (\r
1052 &Instance->ServerIp,\r
1053 &Token->OverrideData->ServerIp,\r
1054 sizeof (EFI_IPv6_ADDRESS)\r
1055 );\r
1056 }\r
1057\r
1058 //\r
1059 // Set default value for undefined parameters.\r
1060 //\r
1061 if (Instance->ServerCmdPort == 0) {\r
1062 Instance->ServerCmdPort = MTFTP6_DEFAULT_SERVER_CMD_PORT;\r
1063 }\r
1064 if (Instance->BlkSize == 0) {\r
1065 Instance->BlkSize = MTFTP6_DEFAULT_BLK_SIZE;\r
1066 }\r
1067 if (Instance->MaxRetry == 0) {\r
1068 Instance->MaxRetry = MTFTP6_DEFAULT_MAX_RETRY;\r
1069 }\r
1070 if (Instance->Timeout == 0) {\r
1071 Instance->Timeout = MTFTP6_DEFAULT_TIMEOUT;\r
1072 }\r
1073\r
1074 Token->Status = EFI_NOT_READY;\r
1075\r
1076 //\r
1077 // Switch the routines by the operation code.\r
1078 //\r
1079 switch (OpCode) {\r
1080 case EFI_MTFTP6_OPCODE_RRQ:\r
1081 Status = Mtftp6RrqStart (Instance, OpCode);\r
1082 break;\r
1083\r
1084 case EFI_MTFTP6_OPCODE_DIR:\r
1085 Status = Mtftp6RrqStart (Instance, OpCode);\r
1086 break;\r
1087\r
1088 case EFI_MTFTP6_OPCODE_WRQ:\r
1089 Status = Mtftp6WrqStart (Instance, OpCode);\r
1090 break;\r
1091\r
1092 default:\r
1093 Status = EFI_DEVICE_ERROR;\r
1094 goto ON_ERROR;\r
1095 }\r
1096\r
1097 if (EFI_ERROR (Status)) {\r
1098 goto ON_ERROR;\r
1099 }\r
1100\r
1101 //\r
1102 // Return immediately for asynchronous or poll the instance for synchronous.\r
1103 //\r
1104 gBS->RestoreTPL (Instance->OldTpl);\r
1105\r
1106 if (Token->Event == NULL) {\r
1107 while (Token->Status == EFI_NOT_READY) {\r
1108 This->Poll (This);\r
1109 }\r
1110 return Token->Status;\r
1111 }\r
1112\r
1113 return EFI_SUCCESS;\r
1114\r
1115ON_ERROR:\r
1116\r
1117 Mtftp6OperationClean (Instance, Status);\r
1118 gBS->RestoreTPL (Instance->OldTpl);\r
1119\r
1120 return Status;\r
1121}\r
1122\r
1123\r
1124/**\r
1125 The timer ticking routine for the Mtftp6 instance.\r
1126\r
1127 @param[in] Event The pointer to the ticking event.\r
1128 @param[in] Context The pointer to the context.\r
1129\r
1130**/\r
1131VOID\r
1132EFIAPI\r
1133Mtftp6OnTimerTick (\r
1134 IN EFI_EVENT Event,\r
1135 IN VOID *Context\r
1136 )\r
1137{\r
1138 MTFTP6_SERVICE *Service;\r
1139 MTFTP6_INSTANCE *Instance;\r
1140 LIST_ENTRY *Entry;\r
1141 LIST_ENTRY *Next;\r
1142 EFI_MTFTP6_TOKEN *Token;\r
1143 EFI_STATUS Status;\r
1144\r
1145 Service = (MTFTP6_SERVICE *) Context;\r
1146\r
1147 //\r
1148 // Iterate through all the children of the Mtftp service instance. Time\r
1149 // out the packet. If maximum retries reached, clean the session up.\r
1150 //\r
1151 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Children) {\r
1152\r
1153 Instance = NET_LIST_USER_STRUCT (Entry, MTFTP6_INSTANCE, Link);\r
1154\r
1155 if (Instance->Token == NULL) {\r
1156 continue;\r
1157 }\r
1158\r
1159 if (Instance->PacketToLive > 0) {\r
1160 Instance->PacketToLive--;\r
1161 continue;\r
1162 }\r
1163\r
1164 Instance->CurRetry++;\r
1165 Token = Instance->Token;\r
1166\r
1167 if (Token->TimeoutCallback != NULL) {\r
1168 //\r
1169 // Call the timeout callback routine if has.\r
1170 //\r
1171 Status = Token->TimeoutCallback (&Instance->Mtftp6, Token);\r
1172\r
1173 if (EFI_ERROR (Status)) {\r
1174 Mtftp6SendError (\r
1175 Instance,\r
1176 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,\r
1177 (UINT8 *) "User aborted the transfer in time out"\r
1178 );\r
1179 Mtftp6OperationClean (Instance, EFI_ABORTED);\r
1180 continue;\r
1181 }\r
1182 }\r
1183\r
1184 //\r
1185 // Retransmit the packet if haven't reach the maxmium retry count,\r
1186 // otherwise exit the transfer.\r
1187 //\r
1188 if (Instance->CurRetry < Instance->MaxRetry) {\r
1189 Mtftp6TransmitPacket (Instance, Instance->LastPacket);\r
1190 } else {\r
1191 Mtftp6OperationClean (Instance, EFI_TIMEOUT);\r
1192 continue;\r
1193 }\r
1194 }\r
1195}\r