]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/Mtftp6Dxe/Mtftp6Rrq.c
1. Fix a bug in PXE driver that the PXE boot do not restart if a new boot option...
[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
456\r
457 *IsCompleted = FALSE;\r
458\r
459 //\r
460 // If already started the master download, don't change the\r
461 // setting. Master download always succeeds.\r
462 //\r
463 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);\r
464 ASSERT (Expected != -1);\r
465\r
466 if (Instance->IsMaster && Expected != 1) {\r
467 return EFI_SUCCESS;\r
468 }\r
469\r
470 ZeroMem (&ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));\r
471\r
472 //\r
473 // Parse the options in the packet.\r
474 //\r
475 Status = Mtftp6ParseStart (Packet, Len, &Count, &Options);\r
476\r
477 if (EFI_ERROR (Status)) {\r
478 return Status;\r
479 }\r
7a49cd08 480 ASSERT (Options != NULL);\r
a3bcde70
HT
481\r
482 //\r
483 // Parse the extensive options in the packet.\r
484 //\r
485 Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, &ExtInfo);\r
486\r
487 if (EFI_ERROR (Status) || !Mtftp6RrqOackValid (Instance, &ExtInfo, &Instance->ExtInfo)) {\r
488 //\r
489 // Don't send an ERROR packet if the error is EFI_OUT_OF_RESOURCES.\r
490 //\r
491 if (Status != EFI_OUT_OF_RESOURCES) {\r
492 //\r
493 // Free the received packet before send new packet in ReceiveNotify,\r
494 // since the udpio might need to be reconfigured.\r
495 //\r
496 NetbufFree (*UdpPacket);\r
497 *UdpPacket = NULL;\r
498 //\r
499 // Send the Mtftp6 error message if invalid packet.\r
500 //\r
501 Mtftp6SendError (\r
502 Instance,\r
503 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,\r
504 (UINT8 *) "Mal-formated OACK packet"\r
505 );\r
506 }\r
507\r
508 return EFI_TFTP_ERROR;\r
509 }\r
510\r
511 if ((ExtInfo.BitMap & MTFTP6_OPT_MCAST_BIT) != 0) {\r
512\r
513 //\r
514 // Save the multicast info. Always update the Master, only update the\r
515 // multicast IP address, block size, timeoute at the first time. If IP\r
516 // address is updated, create a UDP child to receive the multicast.\r
517 //\r
518 Instance->IsMaster = ExtInfo.IsMaster;\r
519\r
520 if (NetIp6IsUnspecifiedAddr (&Instance->McastIp)) {\r
521 if (NetIp6IsUnspecifiedAddr (&ExtInfo.McastIp) || ExtInfo.McastPort == 0) {\r
522 //\r
523 // Free the received packet before send new packet in ReceiveNotify,\r
524 // since the udpio might need to be reconfigured.\r
525 //\r
526 NetbufFree (*UdpPacket);\r
527 *UdpPacket = NULL;\r
528 //\r
529 // Send the Mtftp6 error message if invalid multi-cast setting.\r
530 //\r
531 Mtftp6SendError (\r
532 Instance,\r
533 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,\r
534 (UINT8 *) "Illegal multicast setting"\r
535 );\r
536\r
537 return EFI_TFTP_ERROR;\r
538 }\r
539\r
540 //\r
541 // Create a UDP child then start receive the multicast from it.\r
542 //\r
543 CopyMem (\r
544 &Instance->McastIp,\r
545 &ExtInfo.McastIp,\r
546 sizeof (EFI_IP_ADDRESS)\r
547 );\r
548\r
549 Instance->McastPort = ExtInfo.McastPort;\r
75dce340 550 if (Instance->McastUdpIo == NULL) {\r
551 Instance->McastUdpIo = UdpIoCreateIo (\r
552 Instance->Service->Controller,\r
553 Instance->Service->Image,\r
554 Mtftp6RrqConfigMcastUdpIo,\r
555 UDP_IO_UDP6_VERSION,\r
556 Instance\r
557 );\r
558 }\r
a3bcde70
HT
559\r
560 if (Instance->McastUdpIo == NULL) {\r
561 return EFI_DEVICE_ERROR;\r
562 }\r
563\r
564 Status = UdpIoRecvDatagram (\r
565 Instance->McastUdpIo,\r
566 Mtftp6RrqInput,\r
567 Instance,\r
568 0\r
569 );\r
570\r
571 if (EFI_ERROR (Status)) {\r
572 //\r
573 // Free the received packet before send new packet in ReceiveNotify,\r
574 // since the udpio might need to be reconfigured.\r
575 //\r
576 NetbufFree (*UdpPacket);\r
577 *UdpPacket = NULL;\r
578 //\r
579 // Send the Mtftp6 error message if failed to create Udp6Io to receive.\r
580 //\r
581 Mtftp6SendError (\r
582 Instance,\r
583 EFI_MTFTP6_ERRORCODE_ACCESS_VIOLATION,\r
584 (UINT8 *) "Failed to create socket to receive multicast packet"\r
585 );\r
586\r
587 return Status;\r
588 }\r
589\r
590 //\r
591 // Update the parameters used.\r
592 //\r
593 if (ExtInfo.BlkSize != 0) {\r
594 Instance->BlkSize = ExtInfo.BlkSize;\r
595 }\r
596\r
597 if (ExtInfo.Timeout != 0) {\r
598 Instance->Timeout = ExtInfo.Timeout;\r
599 }\r
600 }\r
601\r
602 } else {\r
603\r
604 Instance->IsMaster = TRUE;\r
605\r
606 if (ExtInfo.BlkSize != 0) {\r
607 Instance->BlkSize = ExtInfo.BlkSize;\r
608 }\r
609\r
610 if (ExtInfo.Timeout != 0) {\r
611 Instance->Timeout = ExtInfo.Timeout;\r
612 }\r
613 }\r
614\r
615 //\r
616 // Free the received packet before send new packet in ReceiveNotify,\r
617 // since the udpio might need to be reconfigured.\r
618 //\r
619 NetbufFree (*UdpPacket);\r
620 *UdpPacket = NULL;\r
621 //\r
622 // Send an ACK to (Expected - 1) which is 0 for unicast download,\r
623 // or tell the server we want to receive the Expected block.\r
624 //\r
625 return Mtftp6RrqSendAck (Instance, (UINT16) (Expected - 1));\r
626}\r
627\r
628\r
629/**\r
630 The packet process callback for Mtftp6 download.\r
631\r
632 @param[in] UdpPacket The pointer to the packet received.\r
633 @param[in] UdpEpt The pointer to the Udp6 access point.\r
634 @param[in] IoStatus The status from Udp6 instance.\r
635 @param[in] Context The pointer to the context.\r
636\r
637**/\r
638VOID\r
639EFIAPI\r
640Mtftp6RrqInput (\r
641 IN NET_BUF *UdpPacket,\r
642 IN UDP_END_POINT *UdpEpt,\r
643 IN EFI_STATUS IoStatus,\r
644 IN VOID *Context\r
645 )\r
646{\r
647 MTFTP6_INSTANCE *Instance;\r
648 EFI_MTFTP6_PACKET *Packet;\r
649 BOOLEAN IsCompleted;\r
650 BOOLEAN IsMcast;\r
651 EFI_STATUS Status;\r
652 UINT16 Opcode;\r
653 UINT32 TotalNum;\r
654 UINT32 Len;\r
655\r
656 Instance = (MTFTP6_INSTANCE *) Context;\r
657\r
658 NET_CHECK_SIGNATURE (Instance, MTFTP6_INSTANCE_SIGNATURE);\r
659\r
660 Status = EFI_SUCCESS;\r
661 Packet = NULL;\r
662 IsCompleted = FALSE;\r
663 IsMcast = FALSE;\r
664 TotalNum = 0;\r
665\r
666 //\r
667 // Return error status if Udp6 instance failed to receive.\r
668 //\r
669 if (EFI_ERROR (IoStatus)) {\r
670 Status = IoStatus;\r
671 goto ON_EXIT;\r
672 }\r
673\r
674 ASSERT (UdpPacket != NULL);\r
675\r
676 if (UdpPacket->TotalSize < MTFTP6_OPCODE_LEN) {\r
677 goto ON_EXIT;\r
678 }\r
679\r
680 //\r
681 // Find the port this packet is from to restart receive correctly.\r
682 //\r
683 if (CompareMem (\r
684 Ip6Swap128 (&UdpEpt->LocalAddr.v6),\r
685 &Instance->McastIp,\r
686 sizeof (EFI_IPv6_ADDRESS)\r
687 ) == 0) {\r
688 IsMcast = TRUE;\r
689 } else {\r
690 IsMcast = FALSE;\r
691 }\r
692\r
693 //\r
694 // Client send initial request to server's listening port. Server\r
695 // will select a UDP port to communicate with the client. The server\r
696 // is required to use the same port as RemotePort to multicast the\r
697 // data.\r
698 //\r
699 if (UdpEpt->RemotePort != Instance->ServerDataPort) {\r
700 if (Instance->ServerDataPort != 0) {\r
701 goto ON_EXIT;\r
702 } else {\r
703 //\r
704 // For the subsequent exchange of requests, reconfigure the udpio as\r
705 // (serverip, serverport, localip, localport).\r
706 // Ususally, the client set serverport as 0 to receive and reset it\r
707 // once the first packet arrives to send ack.\r
708 //\r
709 Instance->ServerDataPort = UdpEpt->RemotePort;\r
710 }\r
711 }\r
712\r
713 //\r
714 // Copy the MTFTP packet to a continuous buffer if it isn't already so.\r
715 //\r
716 Len = UdpPacket->TotalSize;\r
717 TotalNum = UdpPacket->BlockOpNum;\r
718\r
719 if (TotalNum > 1) {\r
720 Packet = AllocateZeroPool (Len);\r
721\r
722 if (Packet == NULL) {\r
723 Status = EFI_OUT_OF_RESOURCES;\r
724 goto ON_EXIT;\r
725 }\r
726\r
727 NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet);\r
728\r
729 } else {\r
730 Packet = (EFI_MTFTP6_PACKET *) NetbufGetByte (UdpPacket, 0, NULL);\r
731 ASSERT (Packet != NULL);\r
732 }\r
733\r
734 Opcode = NTOHS (Packet->OpCode);\r
735\r
736 //\r
737 // Callback to the user's CheckPacket if provided. Abort the transmission\r
738 // if CheckPacket returns an EFI_ERROR code.\r
739 //\r
740 if ((Instance->Token->CheckPacket != NULL) &&\r
741 (Opcode == EFI_MTFTP6_OPCODE_OACK || Opcode == EFI_MTFTP6_OPCODE_ERROR)\r
742 ) {\r
743\r
744 Status = Instance->Token->CheckPacket (\r
745 &Instance->Mtftp6,\r
746 Instance->Token,\r
747 (UINT16) Len,\r
748 Packet\r
749 );\r
750\r
751 if (EFI_ERROR (Status)) {\r
752 //\r
753 // Send an error message to the server to inform it\r
754 //\r
755 if (Opcode != EFI_MTFTP6_OPCODE_ERROR) {\r
756 //\r
757 // Free the received packet before send new packet in ReceiveNotify,\r
758 // since the udpio might need to be reconfigured.\r
759 //\r
760 NetbufFree (UdpPacket);\r
761 UdpPacket = NULL;\r
762 //\r
763 // Send the Mtftp6 error message if user aborted the current session.\r
764 //\r
765 Mtftp6SendError (\r
766 Instance,\r
767 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,\r
768 (UINT8 *) "User aborted the transfer"\r
769 );\r
770 }\r
771\r
772 Status = EFI_ABORTED;\r
773 goto ON_EXIT;\r
774 }\r
775 }\r
776\r
777 //\r
778 // Switch the process routines by the operation code.\r
779 //\r
780 switch (Opcode) {\r
781 case EFI_MTFTP6_OPCODE_DATA:\r
782 if ((Len > (UINT32) (MTFTP6_DATA_HEAD_LEN + Instance->BlkSize)) || (Len < (UINT32) MTFTP6_DATA_HEAD_LEN)) {\r
783 goto ON_EXIT;\r
784 }\r
785 //\r
786 // Handle the data packet of Rrq.\r
787 //\r
788 Status = Mtftp6RrqHandleData (\r
789 Instance,\r
790 Packet,\r
791 Len,\r
792 &UdpPacket,\r
793 &IsCompleted\r
794 );\r
795 break;\r
796\r
797 case EFI_MTFTP6_OPCODE_OACK:\r
798 if (IsMcast || Len <= MTFTP6_OPCODE_LEN) {\r
799 goto ON_EXIT;\r
800 }\r
801 //\r
802 // Handle the Oack packet of Rrq.\r
803 //\r
804 Status = Mtftp6RrqHandleOack (\r
805 Instance,\r
806 Packet,\r
807 Len,\r
808 &UdpPacket,\r
809 &IsCompleted\r
810 );\r
811 break;\r
812\r
813 default:\r
814 //\r
815 // Drop and return eror if received error message.\r
816 //\r
817 Status = EFI_TFTP_ERROR;\r
818 break;\r
819 }\r
820\r
821ON_EXIT:\r
822 //\r
823 // Free the resources, then if !EFI_ERROR (Status), restart the\r
824 // receive, otherwise end the session.\r
825 //\r
826 if (Packet != NULL && TotalNum > 1) {\r
827 FreePool (Packet);\r
828 }\r
829 if (UdpPacket != NULL) {\r
830 NetbufFree (UdpPacket);\r
831 }\r
832 if (!EFI_ERROR (Status) && !IsCompleted) {\r
833 if (IsMcast) {\r
834 Status = UdpIoRecvDatagram (\r
835 Instance->McastUdpIo,\r
836 Mtftp6RrqInput,\r
837 Instance,\r
838 0\r
839 );\r
840 } else {\r
841 Status = UdpIoRecvDatagram (\r
842 Instance->UdpIo,\r
843 Mtftp6RrqInput,\r
844 Instance,\r
845 0\r
846 );\r
847 }\r
848 }\r
849 //\r
850 // Clean up the current session if failed to continue.\r
851 //\r
852 if (EFI_ERROR (Status) || IsCompleted) {\r
853 Mtftp6OperationClean (Instance, Status);\r
854 }\r
855}\r
856\r
857\r
858/**\r
859 Start the Mtftp6 instance to download. It first initializes some\r
860 of the internal states, then builds and sends an RRQ reqeuest packet.\r
861 Finally, it starts receive for the downloading.\r
862\r
863 @param[in] Instance The pointer to the Mtftp6 instance.\r
864 @param[in] Operation The operation code of current packet.\r
865\r
866 @retval EFI_SUCCESS The Mtftp6 is started to download.\r
867 @retval Others Failed to start to download.\r
868\r
869**/\r
870EFI_STATUS\r
871Mtftp6RrqStart (\r
872 IN MTFTP6_INSTANCE *Instance,\r
873 IN UINT16 Operation\r
874 )\r
875{\r
876 EFI_STATUS Status;\r
877\r
878 //\r
879 // The valid block number range are [1, 0xffff]. For example:\r
880 // the client sends an RRQ request to the server, the server\r
881 // transfers the DATA1 block. If option negoitation is ongoing,\r
882 // the server will send back an OACK, then client will send ACK0.\r
883 //\r
884 Status = Mtftp6InitBlockRange (&Instance->BlkList, 1, 0xffff);\r
885\r
886 if (EFI_ERROR (Status)) {\r
887 return Status;\r
888 }\r
889\r
890 Status = Mtftp6SendRequest (Instance, Operation);\r
891\r
892 if (EFI_ERROR (Status)) {\r
893 return Status;\r
894 }\r
895\r
896 return UdpIoRecvDatagram (\r
897 Instance->UdpIo,\r
898 Mtftp6RrqInput,\r
899 Instance,\r
900 0\r
901 );\r
902}\r
903\r