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