]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Network/Mtftp4Dxe/Mtftp4Support.c
af61c0bf6978266a92b9175454f68bf093dffa50
[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 (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
274 Token = Instance->Token;
275 Options = Token->OptionList;
276 Mode = Instance->Token->ModeStr;
277
278 if (Mode == NULL) {
279 Mode = "octet";
280 }
281
282 //
283 // Compute the packet length
284 //
285 Len = (UINT32) (AsciiStrLen (Token->Filename) + AsciiStrLen (Mode) + 4);
286
287 for (Index = 0; Index < Token->OptionCount; Index++) {
288 Len += (UINT32) (AsciiStrLen (Options[Index].OptionStr) +
289 AsciiStrLen (Options[Index].ValueStr) + 2);
290 }
291
292 //
293 // Allocate a packet then copy the data over
294 //
295 if ((Nbuf = NetbufAlloc (Len)) == NULL) {
296 return EFI_OUT_OF_RESOURCES;
297 }
298
299 Packet = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);
300 Packet->OpCode = HTONS (Instance->Operation);
301 Cur = Packet->Rrq.Filename;
302 Cur = AsciiStrCpy (Cur, Token->Filename);
303 Cur = AsciiStrCpy (Cur, Mode);
304
305 for (Index = 0; Index < Token->OptionCount; ++Index) {
306 Cur = AsciiStrCpy (Cur, Options[Index].OptionStr);
307 Cur = AsciiStrCpy (Cur, Options[Index].ValueStr);
308 }
309
310 return Mtftp4SendPacket (Instance, Nbuf);
311 }
312
313
314 /**
315 Build then send an error message
316
317 @param Instance The MTFTP session
318 @param ErrInfo The error code and message
319
320 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet
321 @retval EFI_SUCCESS The error packet is transmitted.
322 @retval Others Failed to transmit the packet.
323
324 **/
325 EFI_STATUS
326 Mtftp4SendError (
327 IN MTFTP4_PROTOCOL *Instance,
328 IN UINT16 ErrCode,
329 IN UINT8* ErrInfo
330 )
331 {
332 NET_BUF *Packet;
333 EFI_MTFTP4_PACKET *TftpError;
334 UINT32 Len;
335
336 Len = (UINT32) (AsciiStrLen (ErrInfo) + sizeof (EFI_MTFTP4_ERROR_HEADER));
337 Packet = NetbufAlloc (Len);
338
339 if (Packet == NULL) {
340 return EFI_OUT_OF_RESOURCES;
341 }
342
343 TftpError = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (Packet, Len, FALSE);
344 TftpError->OpCode = HTONS (EFI_MTFTP4_OPCODE_ERROR);
345 TftpError->Error.ErrorCode = HTONS (ErrCode);
346
347 AsciiStrCpy (TftpError->Error.ErrorMessage, ErrInfo);
348
349 return Mtftp4SendPacket (Instance, Packet);
350 }
351
352
353 /**
354 The callback function called when the packet is transmitted.
355 It simply frees the packet.
356
357 @param Packet The transmitted (or failed to) packet
358 @param Points The local and remote UDP access point
359 @param IoStatus The result of the transmission
360 @param Context Opaque parameter to the callback
361
362 @return None
363
364 **/
365 STATIC
366 VOID
367 Mtftp4OnPacketSent (
368 NET_BUF *Packet,
369 UDP_POINTS *Points,
370 EFI_STATUS IoStatus,
371 VOID *Context
372 )
373 {
374 NetbufFree (Packet);
375 }
376
377
378 /**
379 Set the timeout for the instance. User a longer time for
380 passive instances.
381
382 @param Instance The Mtftp session to set time out
383
384 @return None
385
386 **/
387 VOID
388 Mtftp4SetTimeout (
389 IN MTFTP4_PROTOCOL *Instance
390 )
391 {
392 if (Instance->Master) {
393 Instance->PacketToLive = Instance->Timeout;
394 } else {
395 Instance->PacketToLive = Instance->Timeout * 2;
396 }
397 }
398
399
400 /**
401 Send the packet for the instance. It will first save a reference to
402 the packet for later retransmission. then determine the destination
403 port, listen port for requests, and connected port for others. At last,
404 send the packet out.
405
406 @param Instance The Mtftp instance
407 @param Packet The packet to send
408
409 @retval EFI_SUCCESS The packet is sent out
410 @retval Others Failed to transmit the packet.
411
412 **/
413 EFI_STATUS
414 Mtftp4SendPacket (
415 IN MTFTP4_PROTOCOL *Instance,
416 IN NET_BUF *Packet
417 )
418 {
419 UDP_POINTS UdpPoint;
420 EFI_STATUS Status;
421 UINT16 OpCode;
422
423 //
424 // Save the packet for retransmission
425 //
426 if (Instance->LastPacket != NULL) {
427 NetbufFree (Instance->LastPacket);
428 }
429
430 Instance->LastPacket = Packet;
431
432 Instance->CurRetry = 0;
433 Mtftp4SetTimeout (Instance);
434
435 UdpPoint.LocalAddr = 0;
436 UdpPoint.LocalPort = 0;
437 UdpPoint.RemoteAddr = Instance->ServerIp;
438
439 //
440 // Send the requests to the listening port, other packets
441 // to the connected port
442 //
443 OpCode = NTOHS (*((UINT16 *) NetbufGetByte (Packet, 0, NULL)));
444
445 if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) || (OpCode == EFI_MTFTP4_OPCODE_DIR) ||
446 (OpCode == EFI_MTFTP4_OPCODE_WRQ)) {
447 UdpPoint.RemotePort = Instance->ListeningPort;
448 } else {
449 UdpPoint.RemotePort = Instance->ConnectedPort;
450 }
451
452 NET_GET_REF (Packet);
453
454 Status = UdpIoSendDatagram (
455 Instance->UnicastPort,
456 Packet,
457 &UdpPoint,
458 Instance->Gateway,
459 Mtftp4OnPacketSent,
460 Instance
461 );
462
463 if (EFI_ERROR (Status)) {
464 NET_PUT_REF (Packet);
465 }
466
467 return Status;
468 }
469
470
471 /**
472 Retransmit the last packet for the instance
473
474 @param Instance The Mtftp instance
475
476 @retval EFI_SUCCESS The last packet is retransmitted.
477 @retval Others Failed to retransmit.
478
479 **/
480 EFI_STATUS
481 Mtftp4Retransmit (
482 IN MTFTP4_PROTOCOL *Instance
483 )
484 {
485 UDP_POINTS UdpPoint;
486 EFI_STATUS Status;
487 UINT16 OpCode;
488
489 ASSERT (Instance->LastPacket != NULL);
490
491 UdpPoint.LocalAddr = 0;
492 UdpPoint.LocalPort = 0;
493 UdpPoint.RemoteAddr = Instance->ServerIp;
494
495 //
496 // Set the requests to the listening port, other packets to the connected port
497 //
498 OpCode = NTOHS (*(UINT16 *) NetbufGetByte (Instance->LastPacket, 0, NULL));
499
500 if ((OpCode == EFI_MTFTP4_OPCODE_RRQ) || (OpCode == EFI_MTFTP4_OPCODE_DIR) ||
501 (OpCode == EFI_MTFTP4_OPCODE_WRQ)) {
502 UdpPoint.RemotePort = Instance->ListeningPort;
503 } else {
504 UdpPoint.RemotePort = Instance->ConnectedPort;
505 }
506
507 NET_GET_REF (Instance->LastPacket);
508
509 Status = UdpIoSendDatagram (
510 Instance->UnicastPort,
511 Instance->LastPacket,
512 &UdpPoint,
513 Instance->Gateway,
514 Mtftp4OnPacketSent,
515 Instance
516 );
517
518 if (EFI_ERROR (Status)) {
519 NET_PUT_REF (Instance->LastPacket);
520 }
521
522 return Status;
523 }
524
525
526 /**
527 The timer ticking function for the Mtftp service instance.
528
529 @param Event The ticking event
530 @param Context The Mtftp service instance
531
532 @return None
533
534 **/
535 VOID
536 EFIAPI
537 Mtftp4OnTimerTick (
538 IN EFI_EVENT Event,
539 IN VOID *Context
540 )
541 {
542 MTFTP4_SERVICE *MtftpSb;
543 NET_LIST_ENTRY *Entry;
544 NET_LIST_ENTRY *Next;
545 MTFTP4_PROTOCOL *Instance;
546 EFI_MTFTP4_TOKEN *Token;
547
548 MtftpSb = (MTFTP4_SERVICE *) Context;
549
550 //
551 // Iterate through all the children of the Mtftp service instance. Time
552 // out the packet. If maximum retries reached, clean the session up.
553 //
554 NET_LIST_FOR_EACH_SAFE (Entry, Next, &MtftpSb->Children) {
555 Instance = NET_LIST_USER_STRUCT (Entry, MTFTP4_PROTOCOL, Link);
556
557 if ((Instance->PacketToLive == 0) || (--Instance->PacketToLive > 0)) {
558 continue;
559 }
560
561 //
562 // Call the user's time out handler
563 //
564 Token = Instance->Token;
565
566 if ((Token->TimeoutCallback != NULL) &&
567 EFI_ERROR (Token->TimeoutCallback (&Instance->Mtftp4, Token))) {
568
569 Mtftp4SendError (
570 Instance,
571 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
572 "User aborted the transfer in time out"
573 );
574
575 Mtftp4CleanOperation (Instance, EFI_ABORTED);
576 continue;
577 }
578
579 //
580 // Retransmit the packet if haven't reach the maxmium retry count,
581 // otherwise exit the transfer.
582 //
583 if (++Instance->CurRetry < Instance->MaxRetry) {
584 Mtftp4Retransmit (Instance);
585 Mtftp4SetTimeout (Instance);
586 } else {
587 Mtftp4CleanOperation (Instance, EFI_TIMEOUT);
588 continue;
589 }
590 }
591 }