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