]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Mtftp6Dxe/Mtftp6Support.c
NetworkPkg: Refine casting expression result to bigger size
[mirror_edk2.git] / NetworkPkg / Mtftp6Dxe / Mtftp6Support.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Mtftp6 support functions implementation.\r
3\r
ce5c3273 4 Copyright (c) 2009 - 2017, 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
ce5c3273 226 *TotalBlock += Range->Bound + MultU64x32 (Range->Round - 1, (UINT32)(Range->Bound + 1)) + 1;\r
a3bcde70
HT
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
ce22514e
ZL
322 if (Ip6Mode.AddressList != NULL) {\r
323 FreePool (Ip6Mode.AddressList);\r
324 }\r
325\r
326 if (Ip6Mode.GroupTable != NULL) {\r
327 FreePool (Ip6Mode.GroupTable);\r
328 }\r
329\r
330 if (Ip6Mode.RouteTable != NULL) {\r
331 FreePool (Ip6Mode.RouteTable);\r
332 }\r
333\r
334 if (Ip6Mode.NeighborCache != NULL) {\r
335 FreePool (Ip6Mode.NeighborCache);\r
336 }\r
337\r
338 if (Ip6Mode.PrefixTable != NULL) {\r
339 FreePool (Ip6Mode.PrefixTable);\r
340 }\r
341\r
342 if (Ip6Mode.IcmpTypeList != NULL) {\r
343 FreePool (Ip6Mode.IcmpTypeList);\r
344 }\r
a3bcde70
HT
345\r
346 if (Ip6Mode.IsConfigured) {\r
347 //\r
348 // Continue to configure the Udp6 instance.\r
349 //\r
350 Status = Udp6->Configure (Udp6, UdpCfgData);\r
351 } else {\r
352 Status = EFI_NO_MAPPING;\r
353 }\r
354 }\r
355 }\r
356\r
357ON_EXIT:\r
358\r
359 if (Event != NULL) {\r
360 gBS->CloseEvent (Event);\r
361 }\r
362\r
363 return Status;\r
364}\r
365\r
366\r
367/**\r
368 The dummy configure routine for create a new Udp6 Io.\r
369\r
370 @param[in] UdpIo The pointer to the Udp6 Io.\r
371 @param[in] Context The pointer to the context.\r
372\r
373 @retval EFI_SUCCESS This value is always returned.\r
374\r
375**/\r
376EFI_STATUS\r
377EFIAPI\r
378Mtftp6ConfigDummyUdpIo (\r
379 IN UDP_IO *UdpIo,\r
380 IN VOID *Context\r
381 )\r
382{\r
383 return EFI_SUCCESS;\r
384}\r
385\r
386\r
387/**\r
388 The configure routine for Mtftp6 instance to transmit/receive.\r
389\r
390 @param[in] UdpIo The pointer to the Udp6 Io.\r
391 @param[in] ServerIp The pointer to the server address.\r
392 @param[in] ServerPort The pointer to the server port.\r
393 @param[in] LocalIp The pointer to the local address.\r
394 @param[in] LocalPort The pointer to the local port.\r
395\r
396 @retval EFI_SUCCESS Configured the Udp6 Io for Mtftp6 successfully.\r
397 @retval EFI_NO_MAPPING The corresponding Ip6 instance has not been\r
398 configured yet.\r
399\r
400**/\r
401EFI_STATUS\r
402Mtftp6ConfigUdpIo (\r
403 IN UDP_IO *UdpIo,\r
404 IN EFI_IPv6_ADDRESS *ServerIp,\r
405 IN UINT16 ServerPort,\r
406 IN EFI_IPv6_ADDRESS *LocalIp,\r
407 IN UINT16 LocalPort\r
408 )\r
409{\r
410 EFI_STATUS Status;\r
411 EFI_UDP6_PROTOCOL *Udp6;\r
412 EFI_UDP6_CONFIG_DATA *Udp6Cfg;\r
413\r
414 Udp6 = UdpIo->Protocol.Udp6;\r
415 Udp6Cfg = &(UdpIo->Config.Udp6);\r
416\r
417 ZeroMem (Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA));\r
418\r
419 //\r
420 // Set the Udp6 Io configure data.\r
421 //\r
422 Udp6Cfg->AcceptPromiscuous = FALSE;\r
423 Udp6Cfg->AcceptAnyPort = FALSE;\r
424 Udp6Cfg->AllowDuplicatePort = FALSE;\r
425 Udp6Cfg->TrafficClass = 0;\r
426 Udp6Cfg->HopLimit = 128;\r
427 Udp6Cfg->ReceiveTimeout = 0;\r
428 Udp6Cfg->TransmitTimeout = 0;\r
429 Udp6Cfg->StationPort = LocalPort;\r
430 Udp6Cfg->RemotePort = ServerPort;\r
431\r
432 CopyMem (\r
433 &Udp6Cfg->StationAddress,\r
434 LocalIp,\r
435 sizeof (EFI_IPv6_ADDRESS)\r
436 );\r
437\r
438 CopyMem (\r
439 &Udp6Cfg->RemoteAddress,\r
440 ServerIp,\r
441 sizeof (EFI_IPv6_ADDRESS)\r
442 );\r
443\r
444 //\r
445 // Configure the Udp6 instance with current configure data.\r
446 //\r
447 Status = Udp6->Configure (Udp6, Udp6Cfg);\r
448\r
449 if (Status == EFI_NO_MAPPING) {\r
450\r
451 return Mtftp6GetMapping (UdpIo, Udp6Cfg);\r
452 }\r
453\r
454 return Status;\r
455}\r
456\r
457\r
458/**\r
459 Build and transmit the request packet for the Mtftp6 instance.\r
460\r
461 @param[in] Instance The pointer to the Mtftp6 instance.\r
462 @param[in] Operation The operation code of this packet.\r
463\r
464 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request.\r
465 @retval EFI_SUCCESS The request is built and sent.\r
466 @retval Others Failed to transmit the packet.\r
467\r
468**/\r
469EFI_STATUS\r
470Mtftp6SendRequest (\r
471 IN MTFTP6_INSTANCE *Instance,\r
472 IN UINT16 Operation\r
473 )\r
474{\r
475 EFI_MTFTP6_PACKET *Packet;\r
476 EFI_MTFTP6_OPTION *Options;\r
477 EFI_MTFTP6_TOKEN *Token;\r
7c6c4ac8 478 RETURN_STATUS Status;\r
a3bcde70
HT
479 NET_BUF *Nbuf;\r
480 UINT8 *Mode;\r
481 UINT8 *Cur;\r
a3bcde70 482 UINTN Index;\r
7c6c4ac8
ZL
483 UINT32 BufferLength;\r
484 UINTN FileNameLength;\r
485 UINTN ModeLength;\r
486 UINTN OptionStrLength;\r
487 UINTN ValueStrLength;\r
a3bcde70
HT
488\r
489 Token = Instance->Token;\r
490 Options = Token->OptionList;\r
491 Mode = Token->ModeStr;\r
492\r
493 if (Mode == NULL) {\r
494 Mode = (UINT8 *) "octet";\r
495 }\r
496\r
497 //\r
498 // The header format of RRQ/WRQ packet is:\r
499 //\r
500 // 2 bytes string 1 byte string 1 byte\r
501 // ------------------------------------------------\r
502 // | Opcode | Filename | 0 | Mode | 0 |\r
503 // ------------------------------------------------\r
504 //\r
505 // The common option format is:\r
506 //\r
507 // string 1 byte string 1 byte\r
508 // ---------------------------------------\r
509 // | OptionStr | 0 | ValueStr | 0 |\r
510 // ---------------------------------------\r
511 //\r
512\r
513 //\r
514 // Compute the size of new Mtftp6 packet.\r
515 //\r
7c6c4ac8
ZL
516 FileNameLength = AsciiStrLen ((CHAR8 *) Token->Filename);\r
517 ModeLength = AsciiStrLen ((CHAR8 *) Mode);\r
518 BufferLength = (UINT32) FileNameLength + (UINT32) ModeLength + 4;\r
a3bcde70
HT
519\r
520 for (Index = 0; Index < Token->OptionCount; Index++) {\r
7c6c4ac8
ZL
521 OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);\r
522 ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);\r
523 BufferLength += (UINT32) OptionStrLength + (UINT32) ValueStrLength + 2;\r
a3bcde70
HT
524 }\r
525\r
526 //\r
527 // Allocate a packet then copy the data.\r
528 //\r
7c6c4ac8 529 if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) {\r
a3bcde70
HT
530 return EFI_OUT_OF_RESOURCES;\r
531 }\r
532\r
533 //\r
534 // Copy the opcode, filename and mode into packet.\r
535 //\r
7c6c4ac8 536 Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, BufferLength, FALSE);\r
a3bcde70
HT
537 ASSERT (Packet != NULL);\r
538\r
539 Packet->OpCode = HTONS (Operation);\r
7c6c4ac8
ZL
540 BufferLength -= sizeof (Packet->OpCode);\r
541 \r
a3bcde70 542 Cur = Packet->Rrq.Filename;\r
7c6c4ac8
ZL
543 Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Token->Filename);\r
544 ASSERT_EFI_ERROR (Status);\r
545 BufferLength -= (UINT32) (FileNameLength + 1);\r
546 Cur += FileNameLength + 1;\r
547 Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Mode);\r
548 ASSERT_EFI_ERROR (Status);\r
549 BufferLength -= (UINT32) (ModeLength + 1);\r
550 Cur += ModeLength + 1;\r
a3bcde70
HT
551\r
552 //\r
553 // Copy all the extension options into the packet.\r
554 //\r
555 for (Index = 0; Index < Token->OptionCount; ++Index) {\r
7c6c4ac8
ZL
556 OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);\r
557 ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);\r
558 \r
559 Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].OptionStr);\r
560 ASSERT_EFI_ERROR (Status);\r
561 BufferLength -= (UINT32) (OptionStrLength + 1);\r
562 Cur += OptionStrLength + 1;\r
563 \r
564 Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].ValueStr);\r
565 ASSERT_EFI_ERROR (Status);\r
566 BufferLength -= (UINT32) (ValueStrLength + 1);\r
567 Cur += ValueStrLength + 1;\r
568 \r
a3bcde70
HT
569 }\r
570\r
571 //\r
572 // Save the packet buf for retransmit\r
573 //\r
574 if (Instance->LastPacket != NULL) {\r
575 NetbufFree (Instance->LastPacket);\r
576 }\r
577\r
578 Instance->LastPacket = Nbuf;\r
579 Instance->CurRetry = 0;\r
580\r
581 return Mtftp6TransmitPacket (Instance, Nbuf);\r
582}\r
583\r
584\r
585/**\r
586 Build and send an error packet.\r
587\r
588 @param[in] Instance The pointer to the Mtftp6 instance.\r
589 @param[in] ErrCode The error code in the packet.\r
590 @param[in] ErrInfo The error message in the packet.\r
591\r
592 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet.\r
593 @retval EFI_SUCCESS The error packet is transmitted.\r
594 @retval Others Failed to transmit the packet.\r
595\r
596**/\r
597EFI_STATUS\r
598Mtftp6SendError (\r
599 IN MTFTP6_INSTANCE *Instance,\r
600 IN UINT16 ErrCode,\r
601 IN UINT8* ErrInfo\r
602 )\r
603{\r
604 NET_BUF *Nbuf;\r
605 EFI_MTFTP6_PACKET *TftpError;\r
606 UINT32 Len;\r
607\r
608 //\r
609 // Allocate a packet then copy the data.\r
610 //\r
611 Len = (UINT32) (AsciiStrLen ((CHAR8 *) ErrInfo) + sizeof (EFI_MTFTP6_ERROR_HEADER));\r
612 Nbuf = NetbufAlloc (Len);\r
613\r
614 if (Nbuf == NULL) {\r
615 return EFI_OUT_OF_RESOURCES;\r
616 }\r
617\r
618 TftpError = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);\r
619\r
620 if (TftpError == NULL) {\r
621 NetbufFree (Nbuf);\r
622 return EFI_OUT_OF_RESOURCES;\r
623 }\r
624\r
625 TftpError->OpCode = HTONS (EFI_MTFTP6_OPCODE_ERROR);\r
626 TftpError->Error.ErrorCode = HTONS (ErrCode);\r
627\r
b89ef371 628 AsciiStrCpyS ((CHAR8 *) TftpError->Error.ErrorMessage, AsciiStrLen ((CHAR8 *) ErrInfo) + 1 , (CHAR8 *) ErrInfo);\r
a3bcde70
HT
629\r
630 //\r
631 // Save the packet buf for retransmit\r
632 //\r
633 if (Instance->LastPacket != NULL) {\r
634 NetbufFree (Instance->LastPacket);\r
635 }\r
636\r
637 Instance->LastPacket = Nbuf;\r
638 Instance->CurRetry = 0;\r
639\r
640 return Mtftp6TransmitPacket (Instance, Nbuf);\r
641}\r
642\r
643\r
644/**\r
645 The callback function called when the packet is transmitted.\r
646\r
647 @param[in] Packet The pointer to the packet.\r
648 @param[in] UdpEpt The pointer to the Udp6 access point.\r
649 @param[in] IoStatus The result of the transmission.\r
650 @param[in] Context The pointer to the context.\r
651\r
652**/\r
653VOID\r
654EFIAPI\r
655Mtftp6OnPacketSent (\r
656 IN NET_BUF *Packet,\r
657 IN UDP_END_POINT *UdpEpt,\r
658 IN EFI_STATUS IoStatus,\r
659 IN VOID *Context\r
660 )\r
661{\r
662 NetbufFree (Packet);\r
663 *(BOOLEAN *) Context = TRUE;\r
664}\r
665\r
666\r
667/**\r
668 Send the packet for the Mtftp6 instance.\r
669\r
670 @param[in] Instance The pointer to the Mtftp6 instance.\r
671 @param[in] Packet The pointer to the packet to be sent.\r
672\r
673 @retval EFI_SUCCESS The packet was sent out\r
674 @retval Others Failed to transmit the packet.\r
675\r
676**/\r
677EFI_STATUS\r
678Mtftp6TransmitPacket (\r
679 IN MTFTP6_INSTANCE *Instance,\r
680 IN NET_BUF *Packet\r
681 )\r
682{\r
683 EFI_UDP6_PROTOCOL *Udp6;\r
684 EFI_UDP6_CONFIG_DATA Udp6CfgData;\r
685 EFI_STATUS Status;\r
686 UINT16 *Temp;\r
687 UINT16 Value;\r
688 UINT16 OpCode;\r
689\r
690 ZeroMem (&Udp6CfgData, sizeof(EFI_UDP6_CONFIG_DATA));\r
691 Udp6 = Instance->UdpIo->Protocol.Udp6;\r
692\r
693 //\r
694 // Set the live time of the packet.\r
695 //\r
696 Instance->PacketToLive = Instance->IsMaster ? Instance->Timeout : (Instance->Timeout * 2);\r
697\r
698 Temp = (UINT16 *) NetbufGetByte (Packet, 0, NULL);\r
699 ASSERT (Temp != NULL);\r
700\r
701 Value = *Temp;\r
702 OpCode = NTOHS (Value);\r
703\r
704 if (OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR || OpCode == EFI_MTFTP6_OPCODE_WRQ) {\r
705 //\r
706 // For the Rrq, Dir, Wrq requests of the operation, configure the Udp6Io as\r
707 // (serverip, 69, localip, localport) to send.\r
708 // Usually local address and local port are both default as zero.\r
709 //\r
710 Status = Udp6->Configure (Udp6, NULL);\r
711\r
712 if (EFI_ERROR (Status)) {\r
713 return Status;\r
714 }\r
715\r
716 Status = Mtftp6ConfigUdpIo (\r
717 Instance->UdpIo,\r
718 &Instance->ServerIp,\r
719 Instance->ServerCmdPort,\r
720 &Instance->Config->StationIp,\r
721 Instance->Config->LocalPort\r
722 );\r
723\r
724 if (EFI_ERROR (Status)) {\r
725 return Status;\r
726 }\r
727\r
728 //\r
729 // Get the current local address and port by get Udp6 mode data.\r
730 //\r
731 Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);\r
732 if (EFI_ERROR (Status)) {\r
733 return Status;\r
734 }\r
735\r
736 NET_GET_REF (Packet);\r
737\r
738 Instance->IsTransmitted = FALSE;\r
739\r
740 Status = UdpIoSendDatagram (\r
741 Instance->UdpIo,\r
742 Packet,\r
743 NULL,\r
744 NULL,\r
745 Mtftp6OnPacketSent,\r
746 &Instance->IsTransmitted\r
747 );\r
748\r
749 if (EFI_ERROR (Status)) {\r
750 NET_PUT_REF (Packet);\r
751 return Status;\r
752 }\r
753\r
754 //\r
755 // Poll till the packet sent out from the ip6 queue.\r
756 //\r
757 gBS->RestoreTPL (Instance->OldTpl);\r
758\r
759 while (!Instance->IsTransmitted) {\r
760 Udp6->Poll (Udp6);\r
761 }\r
762\r
763 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
764\r
765 //\r
766 // For the subsequent exchange of such requests, reconfigure the Udp6Io as\r
767 // (serverip, 0, localip, localport) to receive.\r
768 // Currently local address and local port are specified by Udp6 mode data.\r
769 //\r
770 Status = Udp6->Configure (Udp6, NULL);\r
771\r
772 if (EFI_ERROR (Status)) {\r
773 return Status;\r
774 }\r
775\r
776 Status = Mtftp6ConfigUdpIo (\r
777 Instance->UdpIo,\r
778 &Instance->ServerIp,\r
779 Instance->ServerDataPort,\r
780 &Udp6CfgData.StationAddress,\r
781 Udp6CfgData.StationPort\r
782 );\r
783 } else {\r
784 //\r
785 // For the data exchange, configure the Udp6Io as (serverip, dataport,\r
786 // localip, localport) to send/receive.\r
787 // Currently local address and local port are specified by Udp6 mode data.\r
788 //\r
789 Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);\r
790 if (EFI_ERROR (Status)) {\r
791 return Status;\r
792 }\r
793\r
794 if (Udp6CfgData.RemotePort != Instance->ServerDataPort) {\r
795\r
796 Status = Udp6->Configure (Udp6, NULL);\r
797\r
798 if (EFI_ERROR (Status)) {\r
799 return Status;\r
800 }\r
801\r
802 Status = Mtftp6ConfigUdpIo (\r
803 Instance->UdpIo,\r
804 &Instance->ServerIp,\r
805 Instance->ServerDataPort,\r
806 &Udp6CfgData.StationAddress,\r
807 Udp6CfgData.StationPort\r
808 );\r
809\r
810 if (EFI_ERROR (Status)) {\r
811 return Status;\r
812 }\r
813 }\r
814\r
815 NET_GET_REF (Packet);\r
816\r
817 Instance->IsTransmitted = FALSE;\r
818\r
819 Status = UdpIoSendDatagram (\r
820 Instance->UdpIo,\r
821 Packet,\r
822 NULL,\r
823 NULL,\r
824 Mtftp6OnPacketSent,\r
825 &Instance->IsTransmitted\r
826 );\r
827\r
828 if (EFI_ERROR (Status)) {\r
829 NET_PUT_REF (Packet);\r
830 }\r
831\r
832 //\r
833 // Poll till the packet sent out from the ip6 queue.\r
834 //\r
835 gBS->RestoreTPL (Instance->OldTpl);\r
836\r
837 while (!Instance->IsTransmitted) {\r
838 Udp6->Poll (Udp6);\r
839 }\r
840\r
841 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
842 }\r
843\r
844 return Status;\r
845}\r
846\r
847\r
848/**\r
849 Check packet for GetInfo callback routine.\r
850\r
851 GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect\r
852 the first packet from server, then abort the session.\r
853\r
854 @param[in] This The pointer to the Mtftp6 protocol.\r
855 @param[in] Token The pointer to the Mtftp6 token.\r
856 @param[in] PacketLen The length of the packet.\r
857 @param[in] Packet The pointer to the received packet.\r
858\r
859 @retval EFI_ABORTED Abort the Mtftp6 operation.\r
860\r
861**/\r
862EFI_STATUS\r
863EFIAPI\r
864Mtftp6CheckPacket (\r
865 IN EFI_MTFTP6_PROTOCOL *This,\r
866 IN EFI_MTFTP6_TOKEN *Token,\r
867 IN UINT16 PacketLen,\r
868 IN EFI_MTFTP6_PACKET *Packet\r
869 )\r
870{\r
871 MTFTP6_GETINFO_CONTEXT *Context;\r
872 UINT16 OpCode;\r
873\r
874 Context = (MTFTP6_GETINFO_CONTEXT *) Token->Context;\r
875 OpCode = NTOHS (Packet->OpCode);\r
876\r
877 //\r
878 // Set the GetInfo's return status according to the OpCode.\r
879 //\r
880 switch (OpCode) {\r
881 case EFI_MTFTP6_OPCODE_ERROR:\r
882 Context->Status = EFI_TFTP_ERROR;\r
883 break;\r
884\r
885 case EFI_MTFTP6_OPCODE_OACK:\r
886 Context->Status = EFI_SUCCESS;\r
887 break;\r
888\r
889 default:\r
890 Context->Status = EFI_PROTOCOL_ERROR;\r
891 }\r
892\r
893 //\r
894 // Allocate buffer then copy the packet over. Use gBS->AllocatePool\r
895 // in case NetAllocatePool will implements something tricky.\r
896 //\r
897 *(Context->Packet) = AllocateZeroPool (PacketLen);\r
898\r
899 if (*(Context->Packet) == NULL) {\r
900 Context->Status = EFI_OUT_OF_RESOURCES;\r
901 return EFI_ABORTED;\r
902 }\r
903\r
904 *(Context->PacketLen) = PacketLen;\r
905 CopyMem (*(Context->Packet), Packet, PacketLen);\r
906\r
907 return EFI_ABORTED;\r
908}\r
909\r
910\r
911/**\r
912 Clean up the current Mtftp6 operation.\r
913\r
914 @param[in] Instance The pointer to the Mtftp6 instance.\r
915 @param[in] Result The result to be returned to the user.\r
916\r
917**/\r
918VOID\r
919Mtftp6OperationClean (\r
920 IN MTFTP6_INSTANCE *Instance,\r
921 IN EFI_STATUS Result\r
922 )\r
923{\r
924 LIST_ENTRY *Entry;\r
925 LIST_ENTRY *Next;\r
926 MTFTP6_BLOCK_RANGE *Block;\r
927\r
928 //\r
929 // Clean up the current token and event.\r
930 //\r
931 if (Instance->Token != NULL) {\r
932 Instance->Token->Status = Result;\r
933 if (Instance->Token->Event != NULL) {\r
934 gBS->SignalEvent (Instance->Token->Event);\r
935 }\r
936 Instance->Token = NULL;\r
937 }\r
938\r
939 //\r
940 // Clean up the corresponding Udp6Io.\r
941 //\r
942 if (Instance->UdpIo != NULL) {\r
943 UdpIoCleanIo (Instance->UdpIo);\r
944 }\r
945\r
946 if (Instance->McastUdpIo != NULL) {\r
216f7970 947 gBS->CloseProtocol (\r
948 Instance->McastUdpIo->UdpHandle,\r
949 &gEfiUdp6ProtocolGuid,\r
950 Instance->McastUdpIo->Image,\r
951 Instance->Handle\r
952 );\r
a3bcde70
HT
953 UdpIoFreeIo (Instance->McastUdpIo);\r
954 Instance->McastUdpIo = NULL;\r
955 }\r
956\r
957 //\r
958 // Clean up the stored last packet.\r
959 //\r
960 if (Instance->LastPacket != NULL) {\r
961 NetbufFree (Instance->LastPacket);\r
962 Instance->LastPacket = NULL;\r
963 }\r
964\r
965 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->BlkList) {\r
966 Block = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);\r
967 RemoveEntryList (Entry);\r
968 FreePool (Block);\r
969 }\r
970\r
971 //\r
972 // Reinitialize the corresponding fields of the Mtftp6 operation.\r
973 //\r
974 ZeroMem (&Instance->ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));\r
975 ZeroMem (&Instance->ServerIp, sizeof (EFI_IPv6_ADDRESS));\r
976 ZeroMem (&Instance->McastIp, sizeof (EFI_IPv6_ADDRESS));\r
977\r
978 Instance->ServerCmdPort = 0;\r
979 Instance->ServerDataPort = 0;\r
980 Instance->McastPort = 0;\r
981 Instance->BlkSize = 0;\r
982 Instance->LastBlk = 0;\r
983 Instance->PacketToLive = 0;\r
984 Instance->MaxRetry = 0;\r
985 Instance->CurRetry = 0;\r
986 Instance->Timeout = 0;\r
987 Instance->IsMaster = TRUE;\r
988}\r
989\r
990\r
991/**\r
992 Start the Mtftp6 instance to perform the operation, such as read file,\r
993 write file, and read directory.\r
994\r
995 @param[in] This The MTFTP session.\r
996 @param[in] Token The token than encapsues the user's request.\r
997 @param[in] OpCode The operation to perform.\r
998\r
999 @retval EFI_INVALID_PARAMETER Some of the parameters are invalid.\r
1000 @retval EFI_NOT_STARTED The MTFTP session hasn't been configured.\r
1001 @retval EFI_ALREADY_STARTED There is pending operation for the session.\r
1002 @retval EFI_SUCCESS The operation is successfully started.\r
1003\r
1004**/\r
1005EFI_STATUS\r
1006Mtftp6OperationStart (\r
1007 IN EFI_MTFTP6_PROTOCOL *This,\r
1008 IN EFI_MTFTP6_TOKEN *Token,\r
1009 IN UINT16 OpCode\r
1010 )\r
1011{\r
1012 MTFTP6_INSTANCE *Instance;\r
1013 EFI_STATUS Status;\r
1014\r
1015 if (This == NULL ||\r
1016 Token == NULL ||\r
1017 Token->Filename == NULL ||\r
1018 (Token->OptionCount != 0 && Token->OptionList == NULL) ||\r
1019 (Token->OverrideData != NULL && !NetIp6IsValidUnicast (&Token->OverrideData->ServerIp))\r
1020 ) {\r
1021 return EFI_INVALID_PARAMETER;\r
1022 }\r
1023\r
1024 //\r
1025 // At least define one method to collect the data for download.\r
1026 //\r
1027 if ((OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR) &&\r
1028 Token->Buffer == NULL &&\r
1029 Token->CheckPacket == NULL\r
1030 ) {\r
1031 return EFI_INVALID_PARAMETER;\r
1032 }\r
1033\r
1034 //\r
1035 // At least define one method to provide the data for upload.\r
1036 //\r
1037 if (OpCode == EFI_MTFTP6_OPCODE_WRQ && Token->Buffer == NULL && Token->PacketNeeded == NULL) {\r
1038 return EFI_INVALID_PARAMETER;\r
1039 }\r
1040\r
1041 Instance = MTFTP6_INSTANCE_FROM_THIS (This);\r
1042\r
1043 if (Instance->Config == NULL) {\r
1044 return EFI_NOT_STARTED;\r
1045 }\r
1046\r
1047 if (Instance->Token != NULL) {\r
1048 return EFI_ACCESS_DENIED;\r
1049 }\r
1050\r
1051 Status = EFI_SUCCESS;\r
1052 Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1053\r
1054 //\r
1055 // Parse the extension options in the request packet.\r
1056 //\r
1057 if (Token->OptionCount != 0) {\r
1058\r
1059 Status = Mtftp6ParseExtensionOption (\r
1060 Token->OptionList,\r
1061 Token->OptionCount,\r
1062 TRUE,\r
1063 &Instance->ExtInfo\r
1064 );\r
1065\r
1066 if (EFI_ERROR (Status)) {\r
1067 goto ON_ERROR;\r
1068 }\r
1069 }\r
1070\r
1071 //\r
1072 // Initialize runtime data from config data or override data.\r
1073 //\r
1074 Instance->Token = Token;\r
1075 Instance->ServerCmdPort = Instance->Config->InitialServerPort;\r
1076 Instance->ServerDataPort = 0;\r
1077 Instance->MaxRetry = Instance->Config->TryCount;\r
1078 Instance->Timeout = Instance->Config->TimeoutValue;\r
1079 Instance->IsMaster = TRUE;\r
1080\r
1081 CopyMem (\r
1082 &Instance->ServerIp,\r
1083 &Instance->Config->ServerIp,\r
1084 sizeof (EFI_IPv6_ADDRESS)\r
1085 );\r
1086\r
1087 if (Token->OverrideData != NULL) {\r
1088 Instance->ServerCmdPort = Token->OverrideData->ServerPort;\r
1089 Instance->MaxRetry = Token->OverrideData->TryCount;\r
1090 Instance->Timeout = Token->OverrideData->TimeoutValue;\r
1091\r
1092 CopyMem (\r
1093 &Instance->ServerIp,\r
1094 &Token->OverrideData->ServerIp,\r
1095 sizeof (EFI_IPv6_ADDRESS)\r
1096 );\r
1097 }\r
1098\r
1099 //\r
1100 // Set default value for undefined parameters.\r
1101 //\r
1102 if (Instance->ServerCmdPort == 0) {\r
1103 Instance->ServerCmdPort = MTFTP6_DEFAULT_SERVER_CMD_PORT;\r
1104 }\r
1105 if (Instance->BlkSize == 0) {\r
1106 Instance->BlkSize = MTFTP6_DEFAULT_BLK_SIZE;\r
1107 }\r
1108 if (Instance->MaxRetry == 0) {\r
1109 Instance->MaxRetry = MTFTP6_DEFAULT_MAX_RETRY;\r
1110 }\r
1111 if (Instance->Timeout == 0) {\r
1112 Instance->Timeout = MTFTP6_DEFAULT_TIMEOUT;\r
1113 }\r
1114\r
1115 Token->Status = EFI_NOT_READY;\r
1116\r
1117 //\r
1118 // Switch the routines by the operation code.\r
1119 //\r
1120 switch (OpCode) {\r
1121 case EFI_MTFTP6_OPCODE_RRQ:\r
1122 Status = Mtftp6RrqStart (Instance, OpCode);\r
1123 break;\r
1124\r
1125 case EFI_MTFTP6_OPCODE_DIR:\r
1126 Status = Mtftp6RrqStart (Instance, OpCode);\r
1127 break;\r
1128\r
1129 case EFI_MTFTP6_OPCODE_WRQ:\r
1130 Status = Mtftp6WrqStart (Instance, OpCode);\r
1131 break;\r
1132\r
1133 default:\r
1134 Status = EFI_DEVICE_ERROR;\r
1135 goto ON_ERROR;\r
1136 }\r
1137\r
1138 if (EFI_ERROR (Status)) {\r
1139 goto ON_ERROR;\r
1140 }\r
1141\r
1142 //\r
1143 // Return immediately for asynchronous or poll the instance for synchronous.\r
1144 //\r
1145 gBS->RestoreTPL (Instance->OldTpl);\r
1146\r
1147 if (Token->Event == NULL) {\r
1148 while (Token->Status == EFI_NOT_READY) {\r
1149 This->Poll (This);\r
1150 }\r
1151 return Token->Status;\r
1152 }\r
1153\r
1154 return EFI_SUCCESS;\r
1155\r
1156ON_ERROR:\r
1157\r
1158 Mtftp6OperationClean (Instance, Status);\r
1159 gBS->RestoreTPL (Instance->OldTpl);\r
1160\r
1161 return Status;\r
1162}\r
1163\r
1164\r
1165/**\r
1166 The timer ticking routine for the Mtftp6 instance.\r
1167\r
1168 @param[in] Event The pointer to the ticking event.\r
1169 @param[in] Context The pointer to the context.\r
1170\r
1171**/\r
1172VOID\r
1173EFIAPI\r
1174Mtftp6OnTimerTick (\r
1175 IN EFI_EVENT Event,\r
1176 IN VOID *Context\r
1177 )\r
1178{\r
1179 MTFTP6_SERVICE *Service;\r
1180 MTFTP6_INSTANCE *Instance;\r
1181 LIST_ENTRY *Entry;\r
1182 LIST_ENTRY *Next;\r
1183 EFI_MTFTP6_TOKEN *Token;\r
1184 EFI_STATUS Status;\r
1185\r
1186 Service = (MTFTP6_SERVICE *) Context;\r
1187\r
1188 //\r
1189 // Iterate through all the children of the Mtftp service instance. Time\r
1190 // out the packet. If maximum retries reached, clean the session up.\r
1191 //\r
1192 NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Children) {\r
1193\r
1194 Instance = NET_LIST_USER_STRUCT (Entry, MTFTP6_INSTANCE, Link);\r
1195\r
1196 if (Instance->Token == NULL) {\r
1197 continue;\r
1198 }\r
1199\r
1200 if (Instance->PacketToLive > 0) {\r
1201 Instance->PacketToLive--;\r
1202 continue;\r
1203 }\r
1204\r
1205 Instance->CurRetry++;\r
1206 Token = Instance->Token;\r
1207\r
1208 if (Token->TimeoutCallback != NULL) {\r
1209 //\r
1210 // Call the timeout callback routine if has.\r
1211 //\r
1212 Status = Token->TimeoutCallback (&Instance->Mtftp6, Token);\r
1213\r
1214 if (EFI_ERROR (Status)) {\r
1215 Mtftp6SendError (\r
1216 Instance,\r
1217 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,\r
1218 (UINT8 *) "User aborted the transfer in time out"\r
1219 );\r
1220 Mtftp6OperationClean (Instance, EFI_ABORTED);\r
1221 continue;\r
1222 }\r
1223 }\r
1224\r
1225 //\r
1226 // Retransmit the packet if haven't reach the maxmium retry count,\r
1227 // otherwise exit the transfer.\r
1228 //\r
1229 if (Instance->CurRetry < Instance->MaxRetry) {\r
1230 Mtftp6TransmitPacket (Instance, Instance->LastPacket);\r
1231 } else {\r
1232 Mtftp6OperationClean (Instance, EFI_TIMEOUT);\r
1233 continue;\r
1234 }\r
1235 }\r
1236}\r