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