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