fixed one bug when calculate the string length.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Mtftp4Dxe / Mtftp4Support.c
CommitLineData
772db4bb 1/** @file\r
2\r
3Copyright (c) 2006 - 2007, Intel Corporation\r
4All rights reserved. This program and the accompanying materials\r
5are licensed and made available under the terms and conditions of the BSD License\r
6which accompanies this distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12Module Name:\r
13\r
14 Mtftp4Support.c\r
15\r
16Abstract:\r
17\r
18 Support routines for Mtftp\r
19\r
20\r
21**/\r
22\r
23#include "Mtftp4Impl.h"\r
24\r
25\r
26/**\r
27 Allocate a MTFTP4 block range, then init it to the\r
28 range of [Start, End]\r
29\r
30 @param Start The start block number\r
31 @param End The last block number in the range\r
32\r
33 @return NULL if failed to allocate memory, otherwise the created block range.\r
34\r
35**/\r
36STATIC\r
37MTFTP4_BLOCK_RANGE *\r
38Mtftp4AllocateRange (\r
39 IN UINT16 Start,\r
40 IN UINT16 End\r
41 )\r
42{\r
43 MTFTP4_BLOCK_RANGE *Range;\r
44\r
45 Range = NetAllocatePool (sizeof (MTFTP4_BLOCK_RANGE));\r
46\r
47 if (Range == NULL) {\r
48 return NULL;\r
49 }\r
50\r
51 NetListInit (&Range->Link);\r
52 Range->Start = Start;\r
53 Range->End = End;\r
54\r
55 return Range;\r
56}\r
57\r
58\r
59/**\r
60 Initialize the block range for either RRQ or WRQ. RRQ and WRQ have\r
61 different requirements for Start and End. For example, during start\r
62 up, WRQ initializes its whole valid block range to [0, 0xffff]. This\r
63 is bacause the server will send us a ACK0 to inform us to start the\r
64 upload. When the client received ACK0, it will remove 0 from the range,\r
65 get the next block number, which is 1, then upload the BLOCK1. For RRQ\r
66 without option negotiation, the server will directly send us the BLOCK1\r
67 in response to the client's RRQ. When received BLOCK1, the client will\r
68 remove it from the block range and send an ACK. It also works if there\r
69 is option negotiation.\r
70\r
71 @param Head The block range head to initialize\r
72 @param Start The Start block number.\r
73 @param End The last block number.\r
74\r
75 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range\r
76 @retval EFI_SUCCESS The initial block range is created.\r
77\r
78**/\r
79EFI_STATUS\r
80Mtftp4InitBlockRange (\r
81 IN NET_LIST_ENTRY *Head,\r
82 IN UINT16 Start,\r
83 IN UINT16 End\r
84 )\r
85{\r
86 MTFTP4_BLOCK_RANGE *Range;\r
87\r
88 Range = Mtftp4AllocateRange (Start, End);\r
89\r
90 if (Range == NULL) {\r
91 return EFI_OUT_OF_RESOURCES;\r
92 }\r
93\r
94 NetListInsertTail (Head, &Range->Link);\r
95 return EFI_SUCCESS;\r
96}\r
97\r
98\r
99/**\r
100 Get the first valid block number on the range list.\r
101\r
102 @param Head The block range head\r
103\r
104 @return -1: if the block range is empty. Otherwise the first valid block number.\r
105\r
106**/\r
107INTN\r
108Mtftp4GetNextBlockNum (\r
109 IN NET_LIST_ENTRY *Head\r
110 )\r
111{\r
112 MTFTP4_BLOCK_RANGE *Range;\r
113\r
114 if (NetListIsEmpty (Head)) {\r
115 return -1;\r
116 }\r
117\r
118 Range = NET_LIST_HEAD (Head, MTFTP4_BLOCK_RANGE, Link);\r
119 return Range->Start;\r
120}\r
121\r
122\r
123/**\r
124 Set the last block number of the block range list. It will\r
125 remove all the blocks after the Last. MTFTP initialize the\r
126 block range to the maximum possible range, such as [0, 0xffff]\r
127 for WRQ. When it gets the last block number, it will call\r
128 this function to set the last block number.\r
129\r
130 @param Head The block range list\r
131 @param Last The last block number\r
132\r
133 @return None\r
134\r
135**/\r
136VOID\r
137Mtftp4SetLastBlockNum (\r
138 IN NET_LIST_ENTRY *Head,\r
139 IN UINT16 Last\r
140 )\r
141{\r
142 MTFTP4_BLOCK_RANGE *Range;\r
143\r
144 //\r
145 // Iterate from the tail to head to remove the block number\r
146 // after the last.\r
147 //\r
148 while (!NetListIsEmpty (Head)) {\r
149 Range = NET_LIST_TAIL (Head, MTFTP4_BLOCK_RANGE, Link);\r
150\r
151 if (Range->Start > Last) {\r
152 NetListRemoveEntry (&Range->Link);\r
153 NetFreePool (Range);\r
154 continue;\r
155 }\r
156\r
157 if (Range->End > Last) {\r
158 Range->End = Last;\r
159 }\r
160\r
161 return ;\r
162 }\r
163}\r
164\r
165\r
166/**\r
167 Remove the block number from the block range list.\r
168\r
169 @param Head The block range list to remove from\r
170 @param Num The block number to remove\r
171\r
172 @retval EFI_NOT_FOUND The block number isn't in the block range list\r
173 @retval EFI_SUCCESS The block number has been removed from the list\r
174 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource\r
175\r
176**/\r
177EFI_STATUS\r
178Mtftp4RemoveBlockNum (\r
179 IN NET_LIST_ENTRY *Head,\r
180 IN UINT16 Num\r
181 )\r
182{\r
183 MTFTP4_BLOCK_RANGE *Range;\r
184 MTFTP4_BLOCK_RANGE *NewRange;\r
185 NET_LIST_ENTRY *Entry;\r
186\r
187 NET_LIST_FOR_EACH (Entry, Head) {\r
188\r
189 //\r
190 // Each block represents a hole [Start, End] in the file,\r
191 // skip to the first range with End >= Num\r
192 //\r
193 Range = NET_LIST_USER_STRUCT (Entry, MTFTP4_BLOCK_RANGE, Link);\r
194\r
195 if (Range->End < Num) {\r
196 continue;\r
197 }\r
198\r
199 //\r
200 // There are three different cases for Start\r
201 // 1. (Start > Num) && (End >= Num):\r
202 // because all the holes before this one has the condition of\r
203 // End < Num, so this block number has been removed.\r
204 //\r
205 // 2. (Start == Num) && (End >= Num):\r
206 // Need to increase the Start by one, and if End == Num, this\r
207 // hole has been removed completely, remove it.\r
208 //\r
209 // 3. (Start < Num) && (End >= Num):\r
210 // if End == Num, only need to decrease the End by one because\r
211 // we have (Start < Num) && (Num == End), so (Start <= End - 1).\r
212 // if (End > Num), the hold is splited into two holes, with\r
213 // [Start, Num - 1] and [Num + 1, End].\r
214 //\r
215 if (Range->Start > Num) {\r
216 return EFI_NOT_FOUND;\r
217\r
218 } else if (Range->Start == Num) {\r
219 Range->Start++;\r
220\r
221 if (Range->Start > Range->End) {\r
222 NetListRemoveEntry (&Range->Link);\r
223 NetFreePool (Range);\r
224 }\r
225\r
226 return EFI_SUCCESS;\r
227\r
228 } else {\r
229 if (Range->End == Num) {\r
230 Range->End--;\r
231 } else {\r
687a2e5f 232 NewRange = Mtftp4AllocateRange ((UINT16) (Num + 1), (UINT16) Range->End);\r
772db4bb 233\r
234 if (NewRange == NULL) {\r
235 return EFI_OUT_OF_RESOURCES;\r
236 }\r
237\r
238 Range->End = Num - 1;\r
239 NetListInsertAfter (&Range->Link, &NewRange->Link);\r
240 }\r
241\r
242 return EFI_SUCCESS;\r
243 }\r
244 }\r
245\r
246 return EFI_NOT_FOUND;\r
247}\r
248\r
249\r
250/**\r
251 Build then transmit the request packet for the MTFTP session.\r
252\r
253 @param Instance The Mtftp session\r
254\r
255 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request\r
256 @retval EFI_SUCCESS The request is built and sent\r
257 @retval Others Failed to transmit the packet.\r
258\r
259**/\r
260EFI_STATUS\r
261Mtftp4SendRequest (\r
262 IN MTFTP4_PROTOCOL *Instance\r
263 )\r
264{\r
265 EFI_MTFTP4_PACKET *Packet;\r
266 EFI_MTFTP4_OPTION *Options;\r
267 EFI_MTFTP4_TOKEN *Token;\r
268 NET_BUF *Nbuf;\r
269 UINT8 *Mode;\r
270 UINT8 *Cur;\r
271 UINT32 Len;\r
272 UINTN Index;\r
687a2e5f 273 UINT32 Len1;\r
274 UINT32 Len2;\r
772db4bb 275\r
276 Token = Instance->Token;\r
277 Options = Token->OptionList;\r
278 Mode = Instance->Token->ModeStr;\r
279\r
280 if (Mode == NULL) {\r
67a58d0f 281 Mode = (UINT8 *) "octet";\r
772db4bb 282 }\r
283\r
284 //\r
285 // Compute the packet length\r
286 //\r
687a2e5f 287 Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Token->Filename);\r
288 Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Mode);\r
289 Len = (Len1 + Len2 + 4);\r
772db4bb 290\r
291 for (Index = 0; Index < Token->OptionCount; Index++) {\r
687a2e5f 292 Len1 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);\r
293 Len2 = (UINT32) AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);\r
294 Len += Len1 + Len2 + 2;\r
772db4bb 295 }\r
296\r
297 //\r
298 // Allocate a packet then copy the data over\r
299 //\r
300 if ((Nbuf = NetbufAlloc (Len)) == NULL) {\r
301 return EFI_OUT_OF_RESOURCES;\r
302 }\r
303\r
304 Packet = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);\r
305 Packet->OpCode = HTONS (Instance->Operation);\r
306 Cur = Packet->Rrq.Filename;\r
687a2e5f 307 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Token->Filename);\r
b7d3c631 308 Cur += AsciiStrLen ((CHAR8 *) Token->Filename) + 1;\r
687a2e5f 309 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Mode);\r
b7d3c631 310 Cur += AsciiStrLen ((CHAR8 *) Mode) + 1;\r
772db4bb 311\r
312 for (Index = 0; Index < Token->OptionCount; ++Index) {\r
b61439a7 313 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Options[Index].OptionStr);\r
b7d3c631 314 Cur += AsciiStrLen ((CHAR8 *) Options[Index].OptionStr) + 1;\r
b61439a7 315\r
316 Cur = (UINT8 *) AsciiStrCpy ((CHAR8 *) Cur, (CHAR8 *) Options[Index].ValueStr);\r
b7d3c631 317 Cur += AsciiStrLen ((CHAR8 *) (CHAR8 *) Options[Index].ValueStr) + 1;\r
772db4bb 318 }\r
319\r
320 return Mtftp4SendPacket (Instance, Nbuf);\r
321}\r
322\r
323\r
324/**\r
325 Build then send an error message\r
326\r
327 @param Instance The MTFTP session\r
328 @param ErrInfo The error code and message\r
329\r
330 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet\r
331 @retval EFI_SUCCESS The error packet is transmitted.\r
332 @retval Others Failed to transmit the packet.\r
333\r
334**/\r
335EFI_STATUS\r
336Mtftp4SendError (\r
337 IN MTFTP4_PROTOCOL *Instance,\r
338 IN UINT16 ErrCode,\r
339 IN UINT8* ErrInfo\r
340 )\r
341{\r
342 NET_BUF *Packet;\r
343 EFI_MTFTP4_PACKET *TftpError;\r
344 UINT32 Len;\r
345\r
687a2e5f 346 Len = (UINT32) (AsciiStrLen ((CHAR8 *) ErrInfo) + sizeof (EFI_MTFTP4_ERROR_HEADER));\r
772db4bb 347 Packet = NetbufAlloc (Len);\r
348\r
349 if (Packet == NULL) {\r
350 return EFI_OUT_OF_RESOURCES;\r
351 }\r
352\r
353 TftpError = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (Packet, Len, FALSE);\r
354 TftpError->OpCode = HTONS (EFI_MTFTP4_OPCODE_ERROR);\r
355 TftpError->Error.ErrorCode = HTONS (ErrCode);\r
356\r
687a2e5f 357 AsciiStrCpy ((CHAR8 *) TftpError->Error.ErrorMessage, (CHAR8 *) ErrInfo);\r
772db4bb 358\r
359 return Mtftp4SendPacket (Instance, Packet);\r
360}\r
361\r
362\r
363/**\r
364 The callback function called when the packet is transmitted.\r
365 It simply frees the packet.\r
366\r
367 @param Packet The transmitted (or failed to) packet\r
368 @param Points The local and remote UDP access point\r
369 @param IoStatus The result of the transmission\r
370 @param Context Opaque parameter to the callback\r
371\r
372 @return None\r
373\r
374**/\r
375STATIC\r
376VOID\r
377Mtftp4OnPacketSent (\r
378 NET_BUF *Packet,\r
379 UDP_POINTS *Points,\r
380 EFI_STATUS IoStatus,\r
381 VOID *Context\r
382 )\r
383{\r
384 NetbufFree (Packet);\r
385}\r
386\r
387\r
388/**\r
389 Set the timeout for the instance. User a longer time for\r
390 passive instances.\r
391\r
392 @param Instance The Mtftp session to set time out\r
393\r
394 @return None\r
395\r
396**/\r
397VOID\r
398Mtftp4SetTimeout (\r
399 IN MTFTP4_PROTOCOL *Instance\r
400 )\r
401{\r
402 if (Instance->Master) {\r
403 Instance->PacketToLive = Instance->Timeout;\r
404 } else {\r
405 Instance->PacketToLive = Instance->Timeout * 2;\r
406 }\r
407}\r
408\r
409\r
410/**\r
411 Send the packet for the instance. It will first save a reference to\r
412 the packet for later retransmission. then determine the destination\r
413 port, listen port for requests, and connected port for others. At last,\r
414 send the packet out.\r
415\r
416 @param Instance The Mtftp instance\r
417 @param Packet The packet to send\r
418\r
419 @retval EFI_SUCCESS The packet is sent out\r
420 @retval Others Failed to transmit the packet.\r
421\r
422**/\r
423EFI_STATUS\r
424Mtftp4SendPacket (\r
425 IN MTFTP4_PROTOCOL *Instance,\r
426 IN NET_BUF *Packet\r
427 )\r
428{\r
429 UDP_POINTS UdpPoint;\r
430 EFI_STATUS Status;\r
431 UINT16 OpCode;\r
687a2e5f 432 UINT16 Value;\r
772db4bb 433\r
434 //\r
435 // Save the packet for retransmission\r
436 //\r
437 if (Instance->LastPacket != NULL) {\r
438 NetbufFree (Instance->LastPacket);\r
439 }\r
440\r
441 Instance->LastPacket = Packet;\r
442\r
443 Instance->CurRetry = 0;\r
444 Mtftp4SetTimeout (Instance);\r
445\r
446 UdpPoint.LocalAddr = 0;\r
447 UdpPoint.LocalPort = 0;\r
448 UdpPoint.RemoteAddr = Instance->ServerIp;\r
449\r
450 //\r
451 // Send the requests to the listening port, other packets\r
452 // to the connected port\r
453 //\r
687a2e5f 454 Value = *((UINT16 *) NetbufGetByte (Packet, 0, NULL));\r
455 OpCode = NTOHS (Value);\r
772db4bb 456\r
457 if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) || (OpCode == EFI_MTFTP4_OPCODE_DIR) ||\r
458 (OpCode == EFI_MTFTP4_OPCODE_WRQ)) {\r
459 UdpPoint.RemotePort = Instance->ListeningPort;\r
460 } else {\r
461 UdpPoint.RemotePort = Instance->ConnectedPort;\r
462 }\r
463\r
464 NET_GET_REF (Packet);\r
465\r
466 Status = UdpIoSendDatagram (\r
467 Instance->UnicastPort,\r
468 Packet,\r
469 &UdpPoint,\r
c4a62a12 470 0,\r
772db4bb 471 Mtftp4OnPacketSent,\r
472 Instance\r
473 );\r
474\r
475 if (EFI_ERROR (Status)) {\r
476 NET_PUT_REF (Packet);\r
477 }\r
478\r
479 return Status;\r
480}\r
481\r
482\r
483/**\r
484 Retransmit the last packet for the instance\r
485\r
486 @param Instance The Mtftp instance\r
487\r
488 @retval EFI_SUCCESS The last packet is retransmitted.\r
489 @retval Others Failed to retransmit.\r
490\r
491**/\r
492EFI_STATUS\r
493Mtftp4Retransmit (\r
494 IN MTFTP4_PROTOCOL *Instance\r
495 )\r
496{\r
497 UDP_POINTS UdpPoint;\r
498 EFI_STATUS Status;\r
499 UINT16 OpCode;\r
687a2e5f 500 UINT16 Value;\r
772db4bb 501\r
502 ASSERT (Instance->LastPacket != NULL);\r
503\r
504 UdpPoint.LocalAddr = 0;\r
505 UdpPoint.LocalPort = 0;\r
506 UdpPoint.RemoteAddr = Instance->ServerIp;\r
507\r
508 //\r
509 // Set the requests to the listening port, other packets to the connected port\r
510 //\r
687a2e5f 511 Value = *(UINT16 *) NetbufGetByte (Instance->LastPacket, 0, NULL);\r
512 OpCode = NTOHS (Value);\r
772db4bb 513\r
514 if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) || (OpCode == EFI_MTFTP4_OPCODE_DIR) ||\r
515 (OpCode == EFI_MTFTP4_OPCODE_WRQ)) {\r
516 UdpPoint.RemotePort = Instance->ListeningPort;\r
517 } else {\r
518 UdpPoint.RemotePort = Instance->ConnectedPort;\r
519 }\r
520\r
521 NET_GET_REF (Instance->LastPacket);\r
522\r
523 Status = UdpIoSendDatagram (\r
524 Instance->UnicastPort,\r
525 Instance->LastPacket,\r
526 &UdpPoint,\r
c4a62a12 527 0,\r
772db4bb 528 Mtftp4OnPacketSent,\r
529 Instance\r
530 );\r
531\r
532 if (EFI_ERROR (Status)) {\r
533 NET_PUT_REF (Instance->LastPacket);\r
534 }\r
535\r
536 return Status;\r
537}\r
538\r
539\r
540/**\r
541 The timer ticking function for the Mtftp service instance.\r
542\r
543 @param Event The ticking event\r
544 @param Context The Mtftp service instance\r
545\r
546 @return None\r
547\r
548**/\r
549VOID\r
550EFIAPI\r
551Mtftp4OnTimerTick (\r
552 IN EFI_EVENT Event,\r
553 IN VOID *Context\r
554 )\r
555{\r
556 MTFTP4_SERVICE *MtftpSb;\r
557 NET_LIST_ENTRY *Entry;\r
558 NET_LIST_ENTRY *Next;\r
559 MTFTP4_PROTOCOL *Instance;\r
560 EFI_MTFTP4_TOKEN *Token;\r
561\r
562 MtftpSb = (MTFTP4_SERVICE *) Context;\r
563\r
564 //\r
565 // Iterate through all the children of the Mtftp service instance. Time\r
566 // out the packet. If maximum retries reached, clean the session up.\r
567 //\r
568 NET_LIST_FOR_EACH_SAFE (Entry, Next, &MtftpSb->Children) {\r
569 Instance = NET_LIST_USER_STRUCT (Entry, MTFTP4_PROTOCOL, Link);\r
570\r
571 if ((Instance->PacketToLive == 0) || (--Instance->PacketToLive > 0)) {\r
572 continue;\r
573 }\r
574\r
575 //\r
576 // Call the user's time out handler\r
577 //\r
578 Token = Instance->Token;\r
579\r
580 if ((Token->TimeoutCallback != NULL) &&\r
581 EFI_ERROR (Token->TimeoutCallback (&Instance->Mtftp4, Token))) {\r
582\r
583 Mtftp4SendError (\r
584 Instance,\r
585 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,\r
67a58d0f 586 (UINT8 *) "User aborted the transfer in time out"\r
772db4bb 587 );\r
588\r
589 Mtftp4CleanOperation (Instance, EFI_ABORTED);\r
590 continue;\r
591 }\r
592\r
593 //\r
594 // Retransmit the packet if haven't reach the maxmium retry count,\r
595 // otherwise exit the transfer.\r
596 //\r
597 if (++Instance->CurRetry < Instance->MaxRetry) {\r
598 Mtftp4Retransmit (Instance);\r
599 Mtftp4SetTimeout (Instance);\r
600 } else {\r
601 Mtftp4CleanOperation (Instance, EFI_TIMEOUT);\r
602 continue;\r
603 }\r
604 }\r
605}\r