]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c
1. Add EFI_COMPONENT_NAME2_PROTOCOL.GetControllerName() support.
[mirror_edk2.git] / NetworkPkg / Mtftp6Dxe / Mtftp6Rrq.c
CommitLineData
a3bcde70
HT
1/** @file\r
2 Mtftp6 Rrq process functions implementation.\r
3\r
7a49cd08 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 Build and send a ACK packet for download.\r
21\r
22 @param[in] Instance The pointer to the Mtftp6 instance.\r
23 @param[in] BlockNum The block number to be acked.\r
24\r
25 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet.\r
26 @retval EFI_SUCCESS The ACK has been sent.\r
27 @retval Others Failed to send the ACK.\r
28\r
29**/\r
30EFI_STATUS\r
31Mtftp6RrqSendAck (\r
32 IN MTFTP6_INSTANCE *Instance,\r
33 IN UINT16 BlockNum\r
34 )\r
35{\r
36 EFI_MTFTP6_PACKET *Ack;\r
37 NET_BUF *Packet;\r
38\r
39 //\r
40 // Allocate net buffer to create ack packet.\r
41 //\r
42 Packet = NetbufAlloc (sizeof (EFI_MTFTP6_ACK_HEADER));\r
43\r
44 if (Packet == NULL) {\r
45 return EFI_OUT_OF_RESOURCES;\r
46 }\r
47\r
48 Ack = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (\r
49 Packet,\r
50 sizeof (EFI_MTFTP6_ACK_HEADER),\r
51 FALSE\r
52 );\r
53 ASSERT (Ack != NULL);\r
54\r
55 Ack->Ack.OpCode = HTONS (EFI_MTFTP6_OPCODE_ACK);\r
56 Ack->Ack.Block[0] = HTONS (BlockNum);\r
57\r
58 //\r
59 // Reset current retry count of the instance.\r
60 //\r
61 Instance->CurRetry = 0;\r
62\r
63 return Mtftp6TransmitPacket (Instance, Packet);\r
64}\r
65\r
66\r
67/**\r
68 Deliver the received data block to the user, which can be saved\r
69 in the user provide buffer or through the CheckPacket callback.\r
70\r
71 @param[in] Instance The pointer to the Mtftp6 instance.\r
72 @param[in] Packet The pointer to the received packet.\r
73 @param[in] Len The packet length.\r
74 @param[out] UdpPacket The net buf of the received packet.\r
75\r
76 @retval EFI_SUCCESS The data was saved successfully.\r
77 @retval EFI_ABORTED The user tells to abort by return an error through\r
78 CheckPacket.\r
79 @retval EFI_BUFFER_TOO_SMALL The user's buffer is too small, and buffer length is\r
80 updated to the actual buffer size needed.\r
81\r
82**/\r
83EFI_STATUS\r
84Mtftp6RrqSaveBlock (\r
85 IN MTFTP6_INSTANCE *Instance,\r
86 IN EFI_MTFTP6_PACKET *Packet,\r
87 IN UINT32 Len,\r
88 OUT NET_BUF **UdpPacket\r
89 )\r
90{\r
91 EFI_MTFTP6_TOKEN *Token;\r
92 EFI_STATUS Status;\r
93 UINT16 Block;\r
94 UINT64 Start;\r
95 UINT32 DataLen;\r
96 UINT64 TotalBlock;\r
97 BOOLEAN Completed;\r
98\r
99 Completed = FALSE;\r
100 Token = Instance->Token;\r
101 Block = NTOHS (Packet->Data.Block);\r
102 DataLen = Len - MTFTP6_DATA_HEAD_LEN;\r
103\r
104 //\r
105 // This is the last block, save the block num\r
106 //\r
107 if (DataLen < Instance->BlkSize) {\r
76389e18 108 Completed = TRUE;\r
a3bcde70
HT
109 Instance->LastBlk = Block;\r
110 Mtftp6SetLastBlockNum (&Instance->BlkList, Block);\r
111 }\r
112\r
113 //\r
114 // Remove this block number from the file hole. If Mtftp6RemoveBlockNum\r
115 // returns EFI_NOT_FOUND, the block has been saved, don't save it again.\r
116 // Note that : For bigger files, allowing the block counter to roll over\r
117 // to accept transfers of unlimited size. So TotalBlock is memorised as\r
118 // continuous block counter.\r
119 //\r
120 Status = Mtftp6RemoveBlockNum (&Instance->BlkList, Block, Completed, &TotalBlock);\r
121\r
122 if (Status == EFI_NOT_FOUND) {\r
123 return EFI_SUCCESS;\r
124 } else if (EFI_ERROR (Status)) {\r
125 return Status;\r
126 }\r
127\r
128 if (Token->CheckPacket != NULL) {\r
129 //\r
130 // Callback to the check packet routine with the received packet.\r
131 //\r
132 Status = Token->CheckPacket (&Instance->Mtftp6, Token, (UINT16) Len, Packet);\r
133\r
134 if (EFI_ERROR (Status)) {\r
135 //\r
136 // Free the received packet before send new packet in ReceiveNotify,\r
137 // since the Udp6Io might need to be reconfigured.\r
138 //\r
139 NetbufFree (*UdpPacket);\r
140 *UdpPacket = NULL;\r
141 //\r
142 // Send the Mtftp6 error message if user aborted the current session.\r
143 //\r
144 Mtftp6SendError (\r
145 Instance,\r
146 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,\r
147 (UINT8 *) "User aborted download"\r
148 );\r
149\r
150 return EFI_ABORTED;\r
151 }\r
152 }\r
153\r
154 if (Token->Buffer != NULL) {\r
155\r
156 Start = MultU64x32 (TotalBlock - 1, Instance->BlkSize);\r
157 if (Start + DataLen <= Token->BufferSize) {\r
158 CopyMem ((UINT8 *) Token->Buffer + Start, Packet->Data.Data, DataLen);\r
159 //\r
160 // Update the file size when received the last block\r
161 //\r
162 if ((Instance->LastBlk == Block) && Completed) {\r
163 Token->BufferSize = Start + DataLen;\r
164 }\r
165 } else if (Instance->LastBlk != 0) {\r
166 //\r
167 // Don't save the data if the buffer is too small, return\r
168 // EFI_BUFFER_TOO_SMALL if received the last packet. This\r
169 // will give a accurate file length.\r
170 //\r
171 Token->BufferSize = Start + DataLen;\r
172\r
173 //\r
174 // Free the received packet before send new packet in ReceiveNotify,\r
175 // since the udpio might need to be reconfigured.\r
176 //\r
177 NetbufFree (*UdpPacket);\r
178 *UdpPacket = NULL;\r
179 //\r
180 // Send the Mtftp6 error message if no enough buffer.\r
181 //\r
182 Mtftp6SendError (\r
183 Instance,\r
184 EFI_MTFTP6_ERRORCODE_DISK_FULL,\r
185 (UINT8 *) "User provided memory block is too small"\r
186 );\r
187\r
188 return EFI_BUFFER_TOO_SMALL;\r
189 }\r
190 }\r
191\r
192 return EFI_SUCCESS;\r
193}\r
194\r
195\r
196/**\r
197 Process the received data packets. It will save the block\r
198 then send back an ACK if it is active.\r
199\r
200 @param[in] Instance The pointer to the Mtftp6 instance.\r
201 @param[in] Packet The pointer to the received packet.\r
202 @param[in] Len The length of the packet.\r
203 @param[out] UdpPacket The net buf of received packet.\r
204 @param[out] IsCompleted If TRUE, the download has been completed.\r
205 Otherwise, the download has not been completed.\r
206\r
207 @retval EFI_SUCCESS The data packet was successfully processed.\r
208 @retval EFI_ABORTED The download was aborted by the user.\r
209 @retval EFI_BUFFER_TOO_SMALL The user-provided buffer is too small.\r
210\r
211**/\r
212EFI_STATUS\r
213Mtftp6RrqHandleData (\r
214 IN MTFTP6_INSTANCE *Instance,\r
215 IN EFI_MTFTP6_PACKET *Packet,\r
216 IN UINT32 Len,\r
217 OUT NET_BUF **UdpPacket,\r
218 OUT BOOLEAN *IsCompleted\r
219 )\r
220{\r
221 EFI_STATUS Status;\r
222 UINT16 BlockNum;\r
223 INTN Expected;\r
224\r
225 *IsCompleted = FALSE;\r
226 BlockNum = NTOHS (Packet->Data.Block);\r
227 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);\r
228\r
229 ASSERT (Expected >= 0);\r
230\r
231 //\r
232 // If we are active and received an unexpected packet, retransmit\r
233 // the last ACK then restart receiving. If we are passive, save\r
234 // the block.\r
235 //\r
236 if (Instance->IsMaster && (Expected != BlockNum)) {\r
237 //\r
238 // Free the received packet before send new packet in ReceiveNotify,\r
239 // since the udpio might need to be reconfigured.\r
240 //\r
241 NetbufFree (*UdpPacket);\r
242 *UdpPacket = NULL;\r
243\r
244 Mtftp6TransmitPacket (Instance, Instance->LastPacket);\r
245 return EFI_SUCCESS;\r
246 }\r
247\r
248 Status = Mtftp6RrqSaveBlock (Instance, Packet, Len, UdpPacket);\r
249\r
250 if (EFI_ERROR (Status)) {\r
251 return Status;\r
252 }\r
253\r
254 //\r
255 // Reset the passive client's timer whenever it received a valid data packet.\r
256 //\r
257 if (!Instance->IsMaster) {\r
258 Instance->PacketToLive = Instance->Timeout * 2;\r
259 }\r
260\r
261 //\r
262 // Check whether we have received all the blocks. Send the ACK if we\r
263 // are active (unicast client or master client for multicast download).\r
264 // If we have received all the blocks, send an ACK even if we are passive\r
265 // to tell the server that we are done.\r
266 //\r
267 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);\r
268\r
269 if (Instance->IsMaster || Expected < 0) {\r
270 if (Expected < 0) {\r
271 //\r
272 // If we are passive client, then the just received Block maybe\r
273 // isn't the last block. We need to send an ACK to the last block\r
274 // to inform the server that we are done. If we are active client,\r
275 // the Block == Instance->LastBlock.\r
276 //\r
277 BlockNum = Instance->LastBlk;\r
278 *IsCompleted = TRUE;\r
279\r
280 } else {\r
281 BlockNum = (UINT16) (Expected - 1);\r
282 }\r
283 //\r
284 // Free the received packet before send new packet in ReceiveNotify,\r
285 // since the udpio might need to be reconfigured.\r
286 //\r
287 NetbufFree (*UdpPacket);\r
288 *UdpPacket = NULL;\r
289\r
290 Mtftp6RrqSendAck (Instance, BlockNum);\r
291 }\r
292\r
293 return EFI_SUCCESS;\r
294}\r
295\r
296\r
297/**\r
298 Validate whether the options received in the server's OACK packet is valid.\r
299 The options are valid only if:\r
300 1. The server doesn't include options not requested by us.\r
301 2. The server can only use smaller blksize than that is requested.\r
302 3. The server can only use the same timeout as requested.\r
303 4. The server doesn't change its multicast channel.\r
304\r
305 @param[in] Instance The pointer to the Mtftp6 instance.\r
306 @param[in] ReplyInfo The pointer to options information in reply packet.\r
307 @param[in] RequestInfo The pointer to requested options info.\r
308\r
309 @retval TRUE If the option in the OACK is valid.\r
310 @retval FALSE If the option is invalid.\r
311\r
312**/\r
313BOOLEAN\r
314Mtftp6RrqOackValid (\r
315 IN MTFTP6_INSTANCE *Instance,\r
316 IN MTFTP6_EXT_OPTION_INFO *ReplyInfo,\r
317 IN MTFTP6_EXT_OPTION_INFO *RequestInfo\r
318 )\r
319{\r
320 //\r
321 // It is invalid for server to return options we don't request\r
322 //\r
323 if ((ReplyInfo->BitMap & ~RequestInfo->BitMap) != 0) {\r
324 return FALSE;\r
325 }\r
326\r
327 //\r
328 // Server can only specify a smaller block size to be used and\r
329 // return the timeout matches that requested.\r
330 //\r
331 if ((((ReplyInfo->BitMap & MTFTP6_OPT_BLKSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) ||\r
332 (((ReplyInfo->BitMap & MTFTP6_OPT_TIMEOUT_BIT) != 0) && (ReplyInfo->Timeout != RequestInfo->Timeout))\r
333 ) {\r
334 return FALSE;\r
335 }\r
336\r
337 //\r
338 // The server can send ",,master" to client to change its master\r
339 // setting. But if it use the specific multicast channel, it can't\r
340 // change the setting.\r
341 //\r
342 if (((ReplyInfo->BitMap & MTFTP6_OPT_MCAST_BIT) != 0) && !NetIp6IsUnspecifiedAddr (&Instance->McastIp)) {\r
343\r
344 if (!NetIp6IsUnspecifiedAddr (&ReplyInfo->McastIp) && CompareMem (\r
345 &ReplyInfo->McastIp,\r
346 &Instance->McastIp,\r
347 sizeof (EFI_IPv6_ADDRESS)\r
348 ) != 0) {\r
349 return FALSE;\r
350 }\r
351\r
352 if ((ReplyInfo->McastPort != 0) && (ReplyInfo->McastPort != Instance->McastPort)) {\r
353 return FALSE;\r
354 }\r
355 }\r
356\r
357 return TRUE;\r
358}\r
359\r
360\r
361/**\r
362 Configure Udp6Io to receive a packet from a multicast address.\r
363\r
364 @param[in] McastIo The pointer to the mcast Udp6Io.\r
365 @param[in] Context The pointer to the context.\r
366\r
367 @retval EFI_SUCCESS The mcast Udp6Io was successfully configured.\r
368 @retval Others Failed to configure the Udp6Io.\r
369\r
370**/\r
371EFI_STATUS\r
372EFIAPI\r
373Mtftp6RrqConfigMcastUdpIo (\r
374 IN UDP_IO *McastIo,\r
375 IN VOID *Context\r
376 )\r
377{\r
378 EFI_STATUS Status;\r
379 EFI_UDP6_PROTOCOL *Udp6;\r
380 EFI_UDP6_CONFIG_DATA *Udp6Cfg;\r
381 EFI_IPv6_ADDRESS Group;\r
382 MTFTP6_INSTANCE *Instance;\r
383\r
384 Udp6 = McastIo->Protocol.Udp6;\r
385 Udp6Cfg = &(McastIo->Config.Udp6);\r
386 Instance = (MTFTP6_INSTANCE *) Context;\r
387\r
388 //\r
389 // Set the configure data for the mcast Udp6Io.\r
390 //\r
391 ZeroMem (Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA));\r
392\r
393 Udp6Cfg->AcceptPromiscuous = FALSE;\r
394 Udp6Cfg->AcceptAnyPort = FALSE;\r
395 Udp6Cfg->AllowDuplicatePort = FALSE;\r
396 Udp6Cfg->TrafficClass = 0;\r
397 Udp6Cfg->HopLimit = 128;\r
398 Udp6Cfg->ReceiveTimeout = 0;\r
399 Udp6Cfg->TransmitTimeout = 0;\r
400 Udp6Cfg->StationPort = Instance->McastPort;\r
401 Udp6Cfg->RemotePort = 0;\r
402\r
403 CopyMem (\r
404 &Udp6Cfg->RemoteAddress,\r
405 &Instance->ServerIp,\r
406 sizeof (EFI_IPv6_ADDRESS)\r
407 );\r
408\r
409 //\r
410 // Configure the mcast Udp6Io.\r
411 //\r
412 Status = Udp6->Configure (Udp6, Udp6Cfg);\r
413\r
414 if (EFI_ERROR (Status)) {\r
415 return Status;\r
416 }\r
417\r
418 //\r
419 // Join the multicast group\r
420 //\r
421 CopyMem (&Group, &Instance->McastIp, sizeof (EFI_IPv6_ADDRESS));\r
422\r
423 return Udp6->Groups (Udp6, TRUE, &Group);\r
424}\r
425\r
426\r
427/**\r
428 Process the OACK packet for Rrq.\r
429\r
430 @param[in] Instance The pointer to the Mtftp6 instance.\r
431 @param[in] Packet The pointer to the received packet.\r
432 @param[in] Len The length of the packet.\r
433 @param[out] UdpPacket The net buf of received packet.\r
434 @param[out] IsCompleted If TRUE, the download has been completed.\r
435 Otherwise, the download has not been completed.\r
436\r
437 @retval EFI_DEVICE_ERROR Failed to create/start a multicast Udp6 child.\r
438 @retval EFI_TFTP_ERROR An error happened during the process.\r
439 @retval EFI_SUCCESS The OACK packet successfully processed.\r
440\r
441**/\r
442EFI_STATUS\r
443Mtftp6RrqHandleOack (\r
444 IN MTFTP6_INSTANCE *Instance,\r
445 IN EFI_MTFTP6_PACKET *Packet,\r
446 IN UINT32 Len,\r
447 OUT NET_BUF **UdpPacket,\r
448 OUT BOOLEAN *IsCompleted\r
449 )\r
450{\r
451 EFI_MTFTP6_OPTION *Options;\r
452 UINT32 Count;\r
453 MTFTP6_EXT_OPTION_INFO ExtInfo;\r
454 EFI_STATUS Status;\r
455 INTN Expected;\r
216f7970 456 EFI_UDP6_PROTOCOL *Udp6;\r
a3bcde70
HT
457\r
458 *IsCompleted = FALSE;\r
459\r
460 //\r
461 // If already started the master download, don't change the\r
462 // setting. Master download always succeeds.\r
463 //\r
464 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);\r
465 ASSERT (Expected != -1);\r
466\r
467 if (Instance->IsMaster && Expected != 1) {\r
468 return EFI_SUCCESS;\r
469 }\r
470\r
471 ZeroMem (&ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));\r
472\r
473 //\r
474 // Parse the options in the packet.\r
475 //\r
476 Status = Mtftp6ParseStart (Packet, Len, &Count, &Options);\r
477\r
478 if (EFI_ERROR (Status)) {\r
479 return Status;\r
480 }\r
7a49cd08 481 ASSERT (Options != NULL);\r
a3bcde70
HT
482\r
483 //\r
484 // Parse the extensive options in the packet.\r
485 //\r
486 Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, &ExtInfo);\r
487\r
488 if (EFI_ERROR (Status) || !Mtftp6RrqOackValid (Instance, &ExtInfo, &Instance->ExtInfo)) {\r
489 //\r
490 // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.\r
491 //\r
492 if (Status != EFI_OUT_OF_RESOURCES) {\r
493 //\r
494 // Free the received packet before send new packet in ReceiveNotify,\r
495 // since the udpio might need to be reconfigured.\r
496 //\r
497 NetbufFree (*UdpPacket);\r
498 *UdpPacket = NULL;\r
499 //\r
500 // Send the Mtftp6 error message if invalid packet.\r
501 //\r
502 Mtftp6SendError (\r
503 Instance,\r
504 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,\r
505 (UINT8 *) "Mal-formated OACK packet"\r
506 );\r
507 }\r
508\r
509 return EFI_TFTP_ERROR;\r
510 }\r
511\r
512 if ((ExtInfo.BitMap & MTFTP6_OPT_MCAST_BIT) != 0) {\r
513\r
514 //\r
515 // Save the multicast info. Always update the Master, only update the\r
516 // multicast IP address, block size, timeoute at the first time. If IP\r
517 // address is updated, create a UDP child to receive the multicast.\r
518 //\r
519 Instance->IsMaster = ExtInfo.IsMaster;\r
520\r
521 if (NetIp6IsUnspecifiedAddr (&Instance->McastIp)) {\r
522 if (NetIp6IsUnspecifiedAddr (&ExtInfo.McastIp) || ExtInfo.McastPort == 0) {\r
523 //\r
524 // Free the received packet before send new packet in ReceiveNotify,\r
525 // since the udpio might need to be reconfigured.\r
526 //\r
527 NetbufFree (*UdpPacket);\r
528 *UdpPacket = NULL;\r
529 //\r
530 // Send the Mtftp6 error message if invalid multi-cast setting.\r
531 //\r
532 Mtftp6SendError (\r
533 Instance,\r
534 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,\r
535 (UINT8 *) "Illegal multicast setting"\r
536 );\r
537\r
538 return EFI_TFTP_ERROR;\r
539 }\r
540\r
541 //\r
542 // Create a UDP child then start receive the multicast from it.\r
543 //\r
544 CopyMem (\r
545 &Instance->McastIp,\r
546 &ExtInfo.McastIp,\r
547 sizeof (EFI_IP_ADDRESS)\r
548 );\r
549\r
550 Instance->McastPort = ExtInfo.McastPort;\r
75dce340 551 if (Instance->McastUdpIo == NULL) {\r
552 Instance->McastUdpIo = UdpIoCreateIo (\r
553 Instance->Service->Controller,\r
554 Instance->Service->Image,\r
555 Mtftp6RrqConfigMcastUdpIo,\r
556 UDP_IO_UDP6_VERSION,\r
557 Instance\r
558 );\r
216f7970 559 if (Instance->McastUdpIo != NULL) {\r
560 Status = gBS->OpenProtocol (\r
561 Instance->McastUdpIo->UdpHandle,\r
562 &gEfiUdp6ProtocolGuid,\r
563 (VOID **) &Udp6,\r
564 Instance->Service->Image,\r
565 Instance->Handle,\r
566 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER\r
567 );\r
568 if (EFI_ERROR (Status)) {\r
569 UdpIoFreeIo (Instance->McastUdpIo);\r
570 Instance->McastUdpIo = NULL;\r
571 return EFI_DEVICE_ERROR;\r
572 }\r
573 }\r
75dce340 574 }\r
a3bcde70
HT
575\r
576 if (Instance->McastUdpIo == NULL) {\r
577 return EFI_DEVICE_ERROR;\r
578 }\r
579\r
580 Status = UdpIoRecvDatagram (\r
581 Instance->McastUdpIo,\r
582 Mtftp6RrqInput,\r
583 Instance,\r
584 0\r
585 );\r
586\r
587 if (EFI_ERROR (Status)) {\r
588 //\r
589 // Free the received packet before send new packet in ReceiveNotify,\r
590 // since the udpio might need to be reconfigured.\r
591 //\r
592 NetbufFree (*UdpPacket);\r
593 *UdpPacket = NULL;\r
594 //\r
595 // Send the Mtftp6 error message if failed to create Udp6Io to receive.\r
596 //\r
597 Mtftp6SendError (\r
598 Instance,\r
599 EFI_MTFTP6_ERRORCODE_ACCESS_VIOLATION,\r
600 (UINT8 *) "Failed to create socket to receive multicast packet"\r
601 );\r
602\r
603 return Status;\r
604 }\r
605\r
606 //\r
607 // Update the parameters used.\r
608 //\r
609 if (ExtInfo.BlkSize != 0) {\r
610 Instance->BlkSize = ExtInfo.BlkSize;\r
611 }\r
612\r
613 if (ExtInfo.Timeout != 0) {\r
614 Instance->Timeout = ExtInfo.Timeout;\r
615 }\r
616 }\r
617\r
618 } else {\r
619\r
620 Instance->IsMaster = TRUE;\r
621\r
622 if (ExtInfo.BlkSize != 0) {\r
623 Instance->BlkSize = ExtInfo.BlkSize;\r
624 }\r
625\r
626 if (ExtInfo.Timeout != 0) {\r
627 Instance->Timeout = ExtInfo.Timeout;\r
628 }\r
629 }\r
630\r
631 //\r
632 // Free the received packet before send new packet in ReceiveNotify,\r
633 // since the udpio might need to be reconfigured.\r
634 //\r
635 NetbufFree (*UdpPacket);\r
636 *UdpPacket = NULL;\r
637 //\r
638 // Send an ACK to (Expected - 1) which is 0 for unicast download,\r
639 // or tell the server we want to receive the Expected block.\r
640 //\r
641 return Mtftp6RrqSendAck (Instance, (UINT16) (Expected - 1));\r
642}\r
643\r
644\r
645/**\r
646 The packet process callback for Mtftp6 download.\r
647\r
648 @param[in] UdpPacket The pointer to the packet received.\r
649 @param[in] UdpEpt The pointer to the Udp6 access point.\r
650 @param[in] IoStatus The status from Udp6 instance.\r
651 @param[in] Context The pointer to the context.\r
652\r
653**/\r
654VOID\r
655EFIAPI\r
656Mtftp6RrqInput (\r
657 IN NET_BUF *UdpPacket,\r
658 IN UDP_END_POINT *UdpEpt,\r
659 IN EFI_STATUS IoStatus,\r
660 IN VOID *Context\r
661 )\r
662{\r
663 MTFTP6_INSTANCE *Instance;\r
664 EFI_MTFTP6_PACKET *Packet;\r
665 BOOLEAN IsCompleted;\r
666 BOOLEAN IsMcast;\r
667 EFI_STATUS Status;\r
668 UINT16 Opcode;\r
669 UINT32 TotalNum;\r
670 UINT32 Len;\r
671\r
672 Instance = (MTFTP6_INSTANCE *) Context;\r
673\r
674 NET_CHECK_SIGNATURE (Instance, MTFTP6_INSTANCE_SIGNATURE);\r
675\r
676 Status = EFI_SUCCESS;\r
677 Packet = NULL;\r
678 IsCompleted = FALSE;\r
679 IsMcast = FALSE;\r
680 TotalNum = 0;\r
681\r
682 //\r
683 // Return error status if Udp6 instance failed to receive.\r
684 //\r
685 if (EFI_ERROR (IoStatus)) {\r
686 Status = IoStatus;\r
687 goto ON_EXIT;\r
688 }\r
689\r
690 ASSERT (UdpPacket != NULL);\r
691\r
692 if (UdpPacket->TotalSize < MTFTP6_OPCODE_LEN) {\r
693 goto ON_EXIT;\r
694 }\r
695\r
696 //\r
697 // Find the port this packet is from to restart receive correctly.\r
698 //\r
699 if (CompareMem (\r
700 Ip6Swap128 (&UdpEpt->LocalAddr.v6),\r
701 &Instance->McastIp,\r
702 sizeof (EFI_IPv6_ADDRESS)\r
703 ) == 0) {\r
704 IsMcast = TRUE;\r
705 } else {\r
706 IsMcast = FALSE;\r
707 }\r
708\r
709 //\r
710 // Client send initial request to server's listening port. Server\r
711 // will select a UDP port to communicate with the client. The server\r
712 // is required to use the same port as RemotePort to multicast the\r
713 // data.\r
714 //\r
715 if (UdpEpt->RemotePort != Instance->ServerDataPort) {\r
716 if (Instance->ServerDataPort != 0) {\r
717 goto ON_EXIT;\r
718 } else {\r
719 //\r
720 // For the subsequent exchange of requests, reconfigure the udpio as\r
721 // (serverip, serverport, localip, localport).\r
722 // Ususally, the client set serverport as 0 to receive and reset it\r
723 // once the first packet arrives to send ack.\r
724 //\r
725 Instance->ServerDataPort = UdpEpt->RemotePort;\r
726 }\r
727 }\r
728\r
729 //\r
730 // Copy the MTFTP packet to a continuous buffer if it isn't already so.\r
731 //\r
732 Len = UdpPacket->TotalSize;\r
733 TotalNum = UdpPacket->BlockOpNum;\r
734\r
735 if (TotalNum > 1) {\r
736 Packet = AllocateZeroPool (Len);\r
737\r
738 if (Packet == NULL) {\r
739 Status = EFI_OUT_OF_RESOURCES;\r
740 goto ON_EXIT;\r
741 }\r
742\r
743 NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet);\r
744\r
745 } else {\r
746 Packet = (EFI_MTFTP6_PACKET *) NetbufGetByte (UdpPacket, 0, NULL);\r
747 ASSERT (Packet != NULL);\r
748 }\r
749\r
750 Opcode = NTOHS (Packet->OpCode);\r
751\r
752 //\r
753 // Callback to the user's CheckPacket if provided. Abort the transmission\r
754 // if CheckPacket returns an EFI_ERROR code.\r
755 //\r
756 if ((Instance->Token->CheckPacket != NULL) &&\r
757 (Opcode == EFI_MTFTP6_OPCODE_OACK || Opcode == EFI_MTFTP6_OPCODE_ERROR)\r
758 ) {\r
759\r
760 Status = Instance->Token->CheckPacket (\r
761 &Instance->Mtftp6,\r
762 Instance->Token,\r
763 (UINT16) Len,\r
764 Packet\r
765 );\r
766\r
767 if (EFI_ERROR (Status)) {\r
768 //\r
769 // Send an error message to the server to inform it\r
770 //\r
771 if (Opcode != EFI_MTFTP6_OPCODE_ERROR) {\r
772 //\r
773 // Free the received packet before send new packet in ReceiveNotify,\r
774 // since the udpio might need to be reconfigured.\r
775 //\r
776 NetbufFree (UdpPacket);\r
777 UdpPacket = NULL;\r
778 //\r
779 // Send the Mtftp6 error message if user aborted the current session.\r
780 //\r
781 Mtftp6SendError (\r
782 Instance,\r
783 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,\r
784 (UINT8 *) "User aborted the transfer"\r
785 );\r
786 }\r
787\r
788 Status = EFI_ABORTED;\r
789 goto ON_EXIT;\r
790 }\r
791 }\r
792\r
793 //\r
794 // Switch the process routines by the operation code.\r
795 //\r
796 switch (Opcode) {\r
797 case EFI_MTFTP6_OPCODE_DATA:\r
798 if ((Len > (UINT32) (MTFTP6_DATA_HEAD_LEN + Instance->BlkSize)) || (Len < (UINT32) MTFTP6_DATA_HEAD_LEN)) {\r
799 goto ON_EXIT;\r
800 }\r
801 //\r
802 // Handle the data packet of Rrq.\r
803 //\r
804 Status = Mtftp6RrqHandleData (\r
805 Instance,\r
806 Packet,\r
807 Len,\r
808 &UdpPacket,\r
809 &IsCompleted\r
810 );\r
811 break;\r
812\r
813 case EFI_MTFTP6_OPCODE_OACK:\r
814 if (IsMcast || Len <= MTFTP6_OPCODE_LEN) {\r
815 goto ON_EXIT;\r
816 }\r
817 //\r
818 // Handle the Oack packet of Rrq.\r
819 //\r
820 Status = Mtftp6RrqHandleOack (\r
821 Instance,\r
822 Packet,\r
823 Len,\r
824 &UdpPacket,\r
825 &IsCompleted\r
826 );\r
827 break;\r
828\r
829 default:\r
830 //\r
831 // Drop and return eror if received error message.\r
832 //\r
833 Status = EFI_TFTP_ERROR;\r
834 break;\r
835 }\r
836\r
837ON_EXIT:\r
838 //\r
839 // Free the resources, then if !EFI_ERROR (Status), restart the\r
840 // receive, otherwise end the session.\r
841 //\r
842 if (Packet != NULL && TotalNum > 1) {\r
843 FreePool (Packet);\r
844 }\r
845 if (UdpPacket != NULL) {\r
846 NetbufFree (UdpPacket);\r
847 }\r
848 if (!EFI_ERROR (Status) && !IsCompleted) {\r
849 if (IsMcast) {\r
850 Status = UdpIoRecvDatagram (\r
851 Instance->McastUdpIo,\r
852 Mtftp6RrqInput,\r
853 Instance,\r
854 0\r
855 );\r
856 } else {\r
857 Status = UdpIoRecvDatagram (\r
858 Instance->UdpIo,\r
859 Mtftp6RrqInput,\r
860 Instance,\r
861 0\r
862 );\r
863 }\r
864 }\r
865 //\r
866 // Clean up the current session if failed to continue.\r
867 //\r
868 if (EFI_ERROR (Status) || IsCompleted) {\r
869 Mtftp6OperationClean (Instance, Status);\r
870 }\r
871}\r
872\r
873\r
874/**\r
875 Start the Mtftp6 instance to download. It first initializes some\r
876 of the internal states, then builds and sends an RRQ reqeuest packet.\r
877 Finally, it starts receive for the downloading.\r
878\r
879 @param[in] Instance The pointer to the Mtftp6 instance.\r
880 @param[in] Operation The operation code of current packet.\r
881\r
882 @retval EFI_SUCCESS The Mtftp6 is started to download.\r
883 @retval Others Failed to start to download.\r
884\r
885**/\r
886EFI_STATUS\r
887Mtftp6RrqStart (\r
888 IN MTFTP6_INSTANCE *Instance,\r
889 IN UINT16 Operation\r
890 )\r
891{\r
892 EFI_STATUS Status;\r
893\r
894 //\r
895 // The valid block number range are [1, 0xffff]. For example:\r
896 // the client sends an RRQ request to the server, the server\r
897 // transfers the DATA1 block. If option negoitation is ongoing,\r
898 // the server will send back an OACK, then client will send ACK0.\r
899 //\r
900 Status = Mtftp6InitBlockRange (&Instance->BlkList, 1, 0xffff);\r
901\r
902 if (EFI_ERROR (Status)) {\r
903 return Status;\r
904 }\r
905\r
906 Status = Mtftp6SendRequest (Instance, Operation);\r
907\r
908 if (EFI_ERROR (Status)) {\r
909 return Status;\r
910 }\r
911\r
912 return UdpIoRecvDatagram (\r
913 Instance->UdpIo,\r
914 Mtftp6RrqInput,\r
915 Instance,\r
916 0\r
917 );\r
918}\r
919\r