]>
Commit | Line | Data |
---|---|---|
a3bcde70 HT |
1 | /** @file\r |
2 | Mtftp6 support functions implementation.\r | |
3 | \r | |
f75a7f56 | 4 | Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>\r |
a3bcde70 | 5 | \r |
ecf98fbc | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
a3bcde70 HT |
7 | \r |
8 | **/\r | |
9 | \r | |
10 | #include "Mtftp6Impl.h"\r | |
11 | \r | |
12 | \r | |
13 | /**\r | |
14 | Allocate a MTFTP block range, then init it to the range of [Start, End].\r | |
15 | \r | |
16 | @param[in] Start The start block number.\r | |
17 | @param[in] End The last block number in the range.\r | |
18 | \r | |
19 | @return Range The range of the allocated block buffer.\r | |
20 | \r | |
21 | **/\r | |
22 | MTFTP6_BLOCK_RANGE *\r | |
23 | Mtftp6AllocateRange (\r | |
24 | IN UINT16 Start,\r | |
25 | IN UINT16 End\r | |
26 | )\r | |
27 | {\r | |
28 | MTFTP6_BLOCK_RANGE *Range;\r | |
29 | \r | |
30 | Range = AllocateZeroPool (sizeof (MTFTP6_BLOCK_RANGE));\r | |
31 | \r | |
32 | if (Range == NULL) {\r | |
33 | return NULL;\r | |
34 | }\r | |
35 | \r | |
36 | InitializeListHead (&Range->Link);\r | |
37 | Range->Start = Start;\r | |
38 | Range->End = End;\r | |
39 | Range->Bound = End;\r | |
40 | \r | |
41 | return Range;\r | |
42 | }\r | |
43 | \r | |
44 | \r | |
45 | /**\r | |
46 | Initialize the block range for either RRQ or WRQ. RRQ and WRQ have\r | |
47 | different requirements for Start and End. For example, during startup,\r | |
48 | WRQ initializes its whole valid block range to [0, 0xffff]. This\r | |
49 | is bacause the server will send an ACK0 to inform the user to start the\r | |
50 | upload. When the client receives an ACK0, it will remove 0 from the range,\r | |
51 | get the next block number, which is 1, then upload the BLOCK1. For RRQ\r | |
52 | without option negotiation, the server will directly send the BLOCK1\r | |
53 | in response to the client's RRQ. When received BLOCK1, the client will\r | |
54 | remove it from the block range and send an ACK. It also works if there\r | |
55 | is option negotiation.\r | |
56 | \r | |
57 | @param[in] Head The block range head to initialize.\r | |
58 | @param[in] Start The Start block number.\r | |
59 | @param[in] End The last block number.\r | |
60 | \r | |
61 | @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for initial block range.\r | |
62 | @retval EFI_SUCCESS The initial block range is created.\r | |
63 | \r | |
64 | **/\r | |
65 | EFI_STATUS\r | |
66 | Mtftp6InitBlockRange (\r | |
67 | IN LIST_ENTRY *Head,\r | |
68 | IN UINT16 Start,\r | |
69 | IN UINT16 End\r | |
70 | )\r | |
71 | {\r | |
72 | MTFTP6_BLOCK_RANGE *Range;\r | |
73 | \r | |
74 | Range = Mtftp6AllocateRange (Start, End);\r | |
75 | \r | |
76 | if (Range == NULL) {\r | |
77 | return EFI_OUT_OF_RESOURCES;\r | |
78 | }\r | |
79 | \r | |
80 | InsertTailList (Head, &Range->Link);\r | |
81 | return EFI_SUCCESS;\r | |
82 | }\r | |
83 | \r | |
84 | \r | |
85 | /**\r | |
86 | Get the first valid block number on the range list.\r | |
87 | \r | |
88 | @param[in] Head The block range head.\r | |
89 | \r | |
90 | @retval ==-1 If the block range is empty.\r | |
91 | @retval >-1 The first valid block number.\r | |
92 | \r | |
93 | **/\r | |
94 | INTN\r | |
95 | Mtftp6GetNextBlockNum (\r | |
96 | IN LIST_ENTRY *Head\r | |
97 | )\r | |
98 | {\r | |
99 | MTFTP6_BLOCK_RANGE *Range;\r | |
100 | \r | |
101 | if (IsListEmpty (Head)) {\r | |
102 | return -1;\r | |
103 | }\r | |
104 | \r | |
105 | Range = NET_LIST_HEAD (Head, MTFTP6_BLOCK_RANGE, Link);\r | |
106 | return Range->Start;\r | |
107 | }\r | |
108 | \r | |
109 | \r | |
110 | /**\r | |
111 | Set the last block number of the block range list. It\r | |
112 | removes all the blocks after the Last. MTFTP initialize the\r | |
113 | block range to the maximum possible range, such as [0, 0xffff]\r | |
114 | for WRQ. When it gets the last block number, it calls\r | |
115 | this function to set the last block number.\r | |
116 | \r | |
117 | @param[in] Head The block range list.\r | |
118 | @param[in] Last The last block number.\r | |
119 | \r | |
120 | **/\r | |
121 | VOID\r | |
122 | Mtftp6SetLastBlockNum (\r | |
123 | IN LIST_ENTRY *Head,\r | |
124 | IN UINT16 Last\r | |
125 | )\r | |
126 | {\r | |
127 | MTFTP6_BLOCK_RANGE *Range;\r | |
128 | \r | |
129 | //\r | |
130 | // Iterate from the tail to head to remove the block number\r | |
131 | // after the last.\r | |
132 | //\r | |
133 | while (!IsListEmpty (Head)) {\r | |
134 | Range = NET_LIST_TAIL (Head, MTFTP6_BLOCK_RANGE, Link);\r | |
135 | \r | |
136 | if (Range->Start > Last) {\r | |
137 | RemoveEntryList (&Range->Link);\r | |
138 | FreePool (Range);\r | |
139 | continue;\r | |
140 | }\r | |
141 | \r | |
142 | if (Range->End > Last) {\r | |
143 | Range->End = Last;\r | |
144 | }\r | |
145 | return ;\r | |
146 | }\r | |
147 | }\r | |
148 | \r | |
149 | \r | |
150 | /**\r | |
151 | Remove the block number from the block range list.\r | |
152 | \r | |
153 | @param[in] Head The block range list to remove from.\r | |
154 | @param[in] Num The block number to remove.\r | |
2f6693c2 JW |
155 | @param[in] Completed Whether Num is the last block number.\r |
156 | @param[out] BlockCounter The continuous block counter instead of the value after roll-over.\r | |
a3bcde70 HT |
157 | \r |
158 | @retval EFI_NOT_FOUND The block number isn't in the block range list.\r | |
159 | @retval EFI_SUCCESS The block number has been removed from the list.\r | |
160 | @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.\r | |
161 | \r | |
162 | **/\r | |
163 | EFI_STATUS\r | |
164 | Mtftp6RemoveBlockNum (\r | |
165 | IN LIST_ENTRY *Head,\r | |
166 | IN UINT16 Num,\r | |
167 | IN BOOLEAN Completed,\r | |
2f6693c2 | 168 | OUT UINT64 *BlockCounter\r |
a3bcde70 HT |
169 | )\r |
170 | {\r | |
171 | MTFTP6_BLOCK_RANGE *Range;\r | |
172 | MTFTP6_BLOCK_RANGE *NewRange;\r | |
173 | LIST_ENTRY *Entry;\r | |
174 | \r | |
175 | NET_LIST_FOR_EACH (Entry, Head) {\r | |
176 | \r | |
177 | //\r | |
178 | // Each block represents a hole [Start, End] in the file,\r | |
179 | // skip to the first range with End >= Num\r | |
180 | //\r | |
181 | Range = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);\r | |
182 | \r | |
183 | if (Range->End < Num) {\r | |
184 | continue;\r | |
185 | }\r | |
186 | \r | |
187 | //\r | |
188 | // There are three different cases for Start\r | |
189 | // 1. (Start > Num) && (End >= Num):\r | |
190 | // because all the holes before this one has the condition of\r | |
191 | // End < Num, so this block number has been removed.\r | |
192 | //\r | |
193 | // 2. (Start == Num) && (End >= Num):\r | |
194 | // Need to increase the Start by one, and if End == Num, this\r | |
195 | // hole has been removed completely, remove it.\r | |
196 | //\r | |
197 | // 3. (Start < Num) && (End >= Num):\r | |
198 | // if End == Num, only need to decrease the End by one because\r | |
199 | // we have (Start < Num) && (Num == End), so (Start <= End - 1).\r | |
200 | // if (End > Num), the hold is splited into two holes, with\r | |
201 | // [Start, Num - 1] and [Num + 1, End].\r | |
202 | //\r | |
203 | if (Range->Start > Num) {\r | |
204 | return EFI_NOT_FOUND;\r | |
205 | \r | |
206 | } else if (Range->Start == Num) {\r | |
207 | Range->Start++;\r | |
208 | \r | |
209 | //\r | |
210 | // Note that: RFC 1350 does not mention block counter roll-over,\r | |
211 | // but several TFTP hosts implement the roll-over be able to accept\r | |
212 | // transfers of unlimited size. There is no consensus, however, whether\r | |
213 | // the counter should wrap around to zero or to one. Many implementations\r | |
214 | // wrap to zero, because this is the simplest to implement. Here we choose\r | |
215 | // this solution.\r | |
216 | //\r | |
2f6693c2 | 217 | *BlockCounter = Num;\r |
a3bcde70 HT |
218 | \r |
219 | if (Range->Round > 0) {\r | |
2f6693c2 | 220 | *BlockCounter += Range->Bound + MultU64x32 (Range->Round - 1, (UINT32)(Range->Bound + 1)) + 1;\r |
a3bcde70 HT |
221 | }\r |
222 | \r | |
223 | if (Range->Start > Range->Bound) {\r | |
224 | Range->Start = 0;\r | |
225 | Range->Round ++;\r | |
226 | }\r | |
227 | \r | |
228 | if ((Range->Start > Range->End) || Completed) {\r | |
229 | RemoveEntryList (&Range->Link);\r | |
230 | FreePool (Range);\r | |
231 | }\r | |
232 | \r | |
233 | return EFI_SUCCESS;\r | |
234 | \r | |
235 | } else {\r | |
236 | if (Range->End == Num) {\r | |
237 | Range->End--;\r | |
238 | } else {\r | |
239 | NewRange = Mtftp6AllocateRange ((UINT16) (Num + 1), (UINT16) Range->End);\r | |
240 | \r | |
241 | if (NewRange == NULL) {\r | |
242 | return EFI_OUT_OF_RESOURCES;\r | |
243 | }\r | |
244 | \r | |
245 | Range->End = Num - 1;\r | |
246 | NetListInsertAfter (&Range->Link, &NewRange->Link);\r | |
247 | }\r | |
248 | \r | |
249 | return EFI_SUCCESS;\r | |
250 | }\r | |
251 | }\r | |
252 | \r | |
253 | return EFI_NOT_FOUND;\r | |
254 | }\r | |
255 | \r | |
256 | \r | |
257 | /**\r | |
258 | Configure the opened Udp6 instance until the corresponding Ip6 instance\r | |
259 | has been configured.\r | |
260 | \r | |
261 | @param[in] UdpIo The pointer to the Udp6 Io.\r | |
262 | @param[in] UdpCfgData The pointer to the Udp6 configure data.\r | |
263 | \r | |
264 | @retval EFI_SUCCESS Configure the Udp6 instance successfully.\r | |
265 | @retval EFI_NO_MAPPING The corresponding Ip6 instance has not\r | |
266 | been configured yet.\r | |
267 | \r | |
268 | **/\r | |
269 | EFI_STATUS\r | |
270 | Mtftp6GetMapping (\r | |
271 | IN UDP_IO *UdpIo,\r | |
272 | IN EFI_UDP6_CONFIG_DATA *UdpCfgData\r | |
273 | )\r | |
274 | {\r | |
275 | EFI_IP6_MODE_DATA Ip6Mode;\r | |
276 | EFI_UDP6_PROTOCOL *Udp6;\r | |
277 | EFI_STATUS Status;\r | |
278 | EFI_EVENT Event;\r | |
279 | \r | |
280 | Event = NULL;\r | |
281 | Udp6 = UdpIo->Protocol.Udp6;\r | |
282 | \r | |
283 | //\r | |
284 | // Create a timer to check whether the Ip6 instance configured or not.\r | |
285 | //\r | |
286 | Status = gBS->CreateEvent (\r | |
287 | EVT_TIMER,\r | |
288 | TPL_CALLBACK,\r | |
289 | NULL,\r | |
290 | NULL,\r | |
291 | &Event\r | |
292 | );\r | |
293 | if (EFI_ERROR (Status)) {\r | |
294 | goto ON_EXIT;\r | |
295 | }\r | |
296 | \r | |
297 | Status = gBS->SetTimer (\r | |
298 | Event,\r | |
299 | TimerRelative,\r | |
300 | MTFTP6_GET_MAPPING_TIMEOUT * MTFTP6_TICK_PER_SECOND\r | |
301 | );\r | |
302 | if (EFI_ERROR (Status)) {\r | |
303 | goto ON_EXIT;\r | |
304 | }\r | |
305 | \r | |
306 | //\r | |
307 | // Check the Ip6 mode data till timeout.\r | |
308 | //\r | |
309 | while (EFI_ERROR (gBS->CheckEvent (Event))) {\r | |
310 | \r | |
311 | Udp6->Poll (Udp6);\r | |
312 | \r | |
313 | Status = Udp6->GetModeData (Udp6, NULL, &Ip6Mode, NULL, NULL);\r | |
314 | \r | |
315 | if (!EFI_ERROR (Status)) {\r | |
ce22514e ZL |
316 | if (Ip6Mode.AddressList != NULL) {\r |
317 | FreePool (Ip6Mode.AddressList);\r | |
318 | }\r | |
319 | \r | |
320 | if (Ip6Mode.GroupTable != NULL) {\r | |
321 | FreePool (Ip6Mode.GroupTable);\r | |
322 | }\r | |
323 | \r | |
324 | if (Ip6Mode.RouteTable != NULL) {\r | |
325 | FreePool (Ip6Mode.RouteTable);\r | |
326 | }\r | |
327 | \r | |
328 | if (Ip6Mode.NeighborCache != NULL) {\r | |
329 | FreePool (Ip6Mode.NeighborCache);\r | |
330 | }\r | |
331 | \r | |
332 | if (Ip6Mode.PrefixTable != NULL) {\r | |
333 | FreePool (Ip6Mode.PrefixTable);\r | |
334 | }\r | |
335 | \r | |
336 | if (Ip6Mode.IcmpTypeList != NULL) {\r | |
337 | FreePool (Ip6Mode.IcmpTypeList);\r | |
338 | }\r | |
a3bcde70 HT |
339 | \r |
340 | if (Ip6Mode.IsConfigured) {\r | |
341 | //\r | |
342 | // Continue to configure the Udp6 instance.\r | |
343 | //\r | |
344 | Status = Udp6->Configure (Udp6, UdpCfgData);\r | |
345 | } else {\r | |
346 | Status = EFI_NO_MAPPING;\r | |
347 | }\r | |
348 | }\r | |
349 | }\r | |
350 | \r | |
351 | ON_EXIT:\r | |
352 | \r | |
353 | if (Event != NULL) {\r | |
354 | gBS->CloseEvent (Event);\r | |
355 | }\r | |
356 | \r | |
357 | return Status;\r | |
358 | }\r | |
359 | \r | |
360 | \r | |
361 | /**\r | |
362 | The dummy configure routine for create a new Udp6 Io.\r | |
363 | \r | |
364 | @param[in] UdpIo The pointer to the Udp6 Io.\r | |
365 | @param[in] Context The pointer to the context.\r | |
366 | \r | |
367 | @retval EFI_SUCCESS This value is always returned.\r | |
368 | \r | |
369 | **/\r | |
370 | EFI_STATUS\r | |
371 | EFIAPI\r | |
372 | Mtftp6ConfigDummyUdpIo (\r | |
373 | IN UDP_IO *UdpIo,\r | |
374 | IN VOID *Context\r | |
375 | )\r | |
376 | {\r | |
377 | return EFI_SUCCESS;\r | |
378 | }\r | |
379 | \r | |
380 | \r | |
381 | /**\r | |
382 | The configure routine for Mtftp6 instance to transmit/receive.\r | |
383 | \r | |
384 | @param[in] UdpIo The pointer to the Udp6 Io.\r | |
385 | @param[in] ServerIp The pointer to the server address.\r | |
386 | @param[in] ServerPort The pointer to the server port.\r | |
387 | @param[in] LocalIp The pointer to the local address.\r | |
388 | @param[in] LocalPort The pointer to the local port.\r | |
389 | \r | |
390 | @retval EFI_SUCCESS Configured the Udp6 Io for Mtftp6 successfully.\r | |
391 | @retval EFI_NO_MAPPING The corresponding Ip6 instance has not been\r | |
392 | configured yet.\r | |
393 | \r | |
394 | **/\r | |
395 | EFI_STATUS\r | |
396 | Mtftp6ConfigUdpIo (\r | |
397 | IN UDP_IO *UdpIo,\r | |
398 | IN EFI_IPv6_ADDRESS *ServerIp,\r | |
399 | IN UINT16 ServerPort,\r | |
400 | IN EFI_IPv6_ADDRESS *LocalIp,\r | |
401 | IN UINT16 LocalPort\r | |
402 | )\r | |
403 | {\r | |
404 | EFI_STATUS Status;\r | |
405 | EFI_UDP6_PROTOCOL *Udp6;\r | |
406 | EFI_UDP6_CONFIG_DATA *Udp6Cfg;\r | |
407 | \r | |
408 | Udp6 = UdpIo->Protocol.Udp6;\r | |
409 | Udp6Cfg = &(UdpIo->Config.Udp6);\r | |
410 | \r | |
411 | ZeroMem (Udp6Cfg, sizeof (EFI_UDP6_CONFIG_DATA));\r | |
412 | \r | |
413 | //\r | |
414 | // Set the Udp6 Io configure data.\r | |
415 | //\r | |
416 | Udp6Cfg->AcceptPromiscuous = FALSE;\r | |
417 | Udp6Cfg->AcceptAnyPort = FALSE;\r | |
418 | Udp6Cfg->AllowDuplicatePort = FALSE;\r | |
419 | Udp6Cfg->TrafficClass = 0;\r | |
420 | Udp6Cfg->HopLimit = 128;\r | |
421 | Udp6Cfg->ReceiveTimeout = 0;\r | |
422 | Udp6Cfg->TransmitTimeout = 0;\r | |
423 | Udp6Cfg->StationPort = LocalPort;\r | |
424 | Udp6Cfg->RemotePort = ServerPort;\r | |
425 | \r | |
426 | CopyMem (\r | |
427 | &Udp6Cfg->StationAddress,\r | |
428 | LocalIp,\r | |
429 | sizeof (EFI_IPv6_ADDRESS)\r | |
430 | );\r | |
431 | \r | |
432 | CopyMem (\r | |
433 | &Udp6Cfg->RemoteAddress,\r | |
434 | ServerIp,\r | |
435 | sizeof (EFI_IPv6_ADDRESS)\r | |
436 | );\r | |
437 | \r | |
438 | //\r | |
439 | // Configure the Udp6 instance with current configure data.\r | |
440 | //\r | |
441 | Status = Udp6->Configure (Udp6, Udp6Cfg);\r | |
442 | \r | |
443 | if (Status == EFI_NO_MAPPING) {\r | |
444 | \r | |
445 | return Mtftp6GetMapping (UdpIo, Udp6Cfg);\r | |
446 | }\r | |
447 | \r | |
448 | return Status;\r | |
449 | }\r | |
450 | \r | |
451 | \r | |
452 | /**\r | |
453 | Build and transmit the request packet for the Mtftp6 instance.\r | |
454 | \r | |
455 | @param[in] Instance The pointer to the Mtftp6 instance.\r | |
456 | @param[in] Operation The operation code of this packet.\r | |
457 | \r | |
458 | @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the request.\r | |
459 | @retval EFI_SUCCESS The request is built and sent.\r | |
460 | @retval Others Failed to transmit the packet.\r | |
461 | \r | |
462 | **/\r | |
463 | EFI_STATUS\r | |
464 | Mtftp6SendRequest (\r | |
465 | IN MTFTP6_INSTANCE *Instance,\r | |
466 | IN UINT16 Operation\r | |
467 | )\r | |
468 | {\r | |
469 | EFI_MTFTP6_PACKET *Packet;\r | |
470 | EFI_MTFTP6_OPTION *Options;\r | |
471 | EFI_MTFTP6_TOKEN *Token;\r | |
7c6c4ac8 | 472 | RETURN_STATUS Status;\r |
a3bcde70 HT |
473 | NET_BUF *Nbuf;\r |
474 | UINT8 *Mode;\r | |
475 | UINT8 *Cur;\r | |
a3bcde70 | 476 | UINTN Index;\r |
7c6c4ac8 ZL |
477 | UINT32 BufferLength;\r |
478 | UINTN FileNameLength;\r | |
479 | UINTN ModeLength;\r | |
480 | UINTN OptionStrLength;\r | |
481 | UINTN ValueStrLength;\r | |
a3bcde70 HT |
482 | \r |
483 | Token = Instance->Token;\r | |
484 | Options = Token->OptionList;\r | |
485 | Mode = Token->ModeStr;\r | |
486 | \r | |
487 | if (Mode == NULL) {\r | |
488 | Mode = (UINT8 *) "octet";\r | |
489 | }\r | |
490 | \r | |
491 | //\r | |
492 | // The header format of RRQ/WRQ packet is:\r | |
493 | //\r | |
494 | // 2 bytes string 1 byte string 1 byte\r | |
495 | // ------------------------------------------------\r | |
496 | // | Opcode | Filename | 0 | Mode | 0 |\r | |
497 | // ------------------------------------------------\r | |
498 | //\r | |
499 | // The common option format is:\r | |
500 | //\r | |
501 | // string 1 byte string 1 byte\r | |
502 | // ---------------------------------------\r | |
503 | // | OptionStr | 0 | ValueStr | 0 |\r | |
504 | // ---------------------------------------\r | |
505 | //\r | |
506 | \r | |
507 | //\r | |
508 | // Compute the size of new Mtftp6 packet.\r | |
509 | //\r | |
7c6c4ac8 ZL |
510 | FileNameLength = AsciiStrLen ((CHAR8 *) Token->Filename);\r |
511 | ModeLength = AsciiStrLen ((CHAR8 *) Mode);\r | |
512 | BufferLength = (UINT32) FileNameLength + (UINT32) ModeLength + 4;\r | |
a3bcde70 HT |
513 | \r |
514 | for (Index = 0; Index < Token->OptionCount; Index++) {\r | |
7c6c4ac8 ZL |
515 | OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);\r |
516 | ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);\r | |
517 | BufferLength += (UINT32) OptionStrLength + (UINT32) ValueStrLength + 2;\r | |
a3bcde70 HT |
518 | }\r |
519 | \r | |
520 | //\r | |
521 | // Allocate a packet then copy the data.\r | |
522 | //\r | |
7c6c4ac8 | 523 | if ((Nbuf = NetbufAlloc (BufferLength)) == NULL) {\r |
a3bcde70 HT |
524 | return EFI_OUT_OF_RESOURCES;\r |
525 | }\r | |
526 | \r | |
527 | //\r | |
528 | // Copy the opcode, filename and mode into packet.\r | |
529 | //\r | |
7c6c4ac8 | 530 | Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, BufferLength, FALSE);\r |
a3bcde70 HT |
531 | ASSERT (Packet != NULL);\r |
532 | \r | |
533 | Packet->OpCode = HTONS (Operation);\r | |
7c6c4ac8 | 534 | BufferLength -= sizeof (Packet->OpCode);\r |
f75a7f56 | 535 | \r |
a3bcde70 | 536 | Cur = Packet->Rrq.Filename;\r |
7c6c4ac8 ZL |
537 | Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Token->Filename);\r |
538 | ASSERT_EFI_ERROR (Status);\r | |
539 | BufferLength -= (UINT32) (FileNameLength + 1);\r | |
540 | Cur += FileNameLength + 1;\r | |
541 | Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Mode);\r | |
542 | ASSERT_EFI_ERROR (Status);\r | |
543 | BufferLength -= (UINT32) (ModeLength + 1);\r | |
544 | Cur += ModeLength + 1;\r | |
a3bcde70 HT |
545 | \r |
546 | //\r | |
547 | // Copy all the extension options into the packet.\r | |
548 | //\r | |
549 | for (Index = 0; Index < Token->OptionCount; ++Index) {\r | |
7c6c4ac8 ZL |
550 | OptionStrLength = AsciiStrLen ((CHAR8 *) Options[Index].OptionStr);\r |
551 | ValueStrLength = AsciiStrLen ((CHAR8 *) Options[Index].ValueStr);\r | |
f75a7f56 | 552 | \r |
7c6c4ac8 ZL |
553 | Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].OptionStr);\r |
554 | ASSERT_EFI_ERROR (Status);\r | |
555 | BufferLength -= (UINT32) (OptionStrLength + 1);\r | |
556 | Cur += OptionStrLength + 1;\r | |
f75a7f56 | 557 | \r |
7c6c4ac8 ZL |
558 | Status = AsciiStrCpyS ((CHAR8 *) Cur, BufferLength, (CHAR8 *) Options[Index].ValueStr);\r |
559 | ASSERT_EFI_ERROR (Status);\r | |
560 | BufferLength -= (UINT32) (ValueStrLength + 1);\r | |
561 | Cur += ValueStrLength + 1;\r | |
f75a7f56 | 562 | \r |
a3bcde70 HT |
563 | }\r |
564 | \r | |
565 | //\r | |
566 | // Save the packet buf for retransmit\r | |
567 | //\r | |
568 | if (Instance->LastPacket != NULL) {\r | |
569 | NetbufFree (Instance->LastPacket);\r | |
570 | }\r | |
571 | \r | |
572 | Instance->LastPacket = Nbuf;\r | |
573 | Instance->CurRetry = 0;\r | |
574 | \r | |
575 | return Mtftp6TransmitPacket (Instance, Nbuf);\r | |
576 | }\r | |
577 | \r | |
578 | \r | |
579 | /**\r | |
580 | Build and send an error packet.\r | |
581 | \r | |
582 | @param[in] Instance The pointer to the Mtftp6 instance.\r | |
583 | @param[in] ErrCode The error code in the packet.\r | |
584 | @param[in] ErrInfo The error message in the packet.\r | |
585 | \r | |
586 | @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the error packet.\r | |
587 | @retval EFI_SUCCESS The error packet is transmitted.\r | |
588 | @retval Others Failed to transmit the packet.\r | |
589 | \r | |
590 | **/\r | |
591 | EFI_STATUS\r | |
592 | Mtftp6SendError (\r | |
593 | IN MTFTP6_INSTANCE *Instance,\r | |
594 | IN UINT16 ErrCode,\r | |
595 | IN UINT8* ErrInfo\r | |
596 | )\r | |
597 | {\r | |
598 | NET_BUF *Nbuf;\r | |
599 | EFI_MTFTP6_PACKET *TftpError;\r | |
600 | UINT32 Len;\r | |
601 | \r | |
602 | //\r | |
603 | // Allocate a packet then copy the data.\r | |
604 | //\r | |
605 | Len = (UINT32) (AsciiStrLen ((CHAR8 *) ErrInfo) + sizeof (EFI_MTFTP6_ERROR_HEADER));\r | |
606 | Nbuf = NetbufAlloc (Len);\r | |
607 | \r | |
608 | if (Nbuf == NULL) {\r | |
609 | return EFI_OUT_OF_RESOURCES;\r | |
610 | }\r | |
611 | \r | |
612 | TftpError = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (Nbuf, Len, FALSE);\r | |
613 | \r | |
614 | if (TftpError == NULL) {\r | |
615 | NetbufFree (Nbuf);\r | |
616 | return EFI_OUT_OF_RESOURCES;\r | |
617 | }\r | |
618 | \r | |
619 | TftpError->OpCode = HTONS (EFI_MTFTP6_OPCODE_ERROR);\r | |
620 | TftpError->Error.ErrorCode = HTONS (ErrCode);\r | |
621 | \r | |
b89ef371 | 622 | AsciiStrCpyS ((CHAR8 *) TftpError->Error.ErrorMessage, AsciiStrLen ((CHAR8 *) ErrInfo) + 1 , (CHAR8 *) ErrInfo);\r |
a3bcde70 HT |
623 | \r |
624 | //\r | |
625 | // Save the packet buf for retransmit\r | |
626 | //\r | |
627 | if (Instance->LastPacket != NULL) {\r | |
628 | NetbufFree (Instance->LastPacket);\r | |
629 | }\r | |
630 | \r | |
631 | Instance->LastPacket = Nbuf;\r | |
632 | Instance->CurRetry = 0;\r | |
633 | \r | |
634 | return Mtftp6TransmitPacket (Instance, Nbuf);\r | |
635 | }\r | |
636 | \r | |
637 | \r | |
638 | /**\r | |
639 | The callback function called when the packet is transmitted.\r | |
640 | \r | |
641 | @param[in] Packet The pointer to the packet.\r | |
642 | @param[in] UdpEpt The pointer to the Udp6 access point.\r | |
643 | @param[in] IoStatus The result of the transmission.\r | |
644 | @param[in] Context The pointer to the context.\r | |
645 | \r | |
646 | **/\r | |
647 | VOID\r | |
648 | EFIAPI\r | |
649 | Mtftp6OnPacketSent (\r | |
650 | IN NET_BUF *Packet,\r | |
651 | IN UDP_END_POINT *UdpEpt,\r | |
652 | IN EFI_STATUS IoStatus,\r | |
653 | IN VOID *Context\r | |
654 | )\r | |
655 | {\r | |
656 | NetbufFree (Packet);\r | |
657 | *(BOOLEAN *) Context = TRUE;\r | |
658 | }\r | |
659 | \r | |
660 | \r | |
661 | /**\r | |
662 | Send the packet for the Mtftp6 instance.\r | |
663 | \r | |
664 | @param[in] Instance The pointer to the Mtftp6 instance.\r | |
665 | @param[in] Packet The pointer to the packet to be sent.\r | |
666 | \r | |
667 | @retval EFI_SUCCESS The packet was sent out\r | |
668 | @retval Others Failed to transmit the packet.\r | |
669 | \r | |
670 | **/\r | |
671 | EFI_STATUS\r | |
672 | Mtftp6TransmitPacket (\r | |
673 | IN MTFTP6_INSTANCE *Instance,\r | |
674 | IN NET_BUF *Packet\r | |
675 | )\r | |
676 | {\r | |
677 | EFI_UDP6_PROTOCOL *Udp6;\r | |
678 | EFI_UDP6_CONFIG_DATA Udp6CfgData;\r | |
679 | EFI_STATUS Status;\r | |
680 | UINT16 *Temp;\r | |
681 | UINT16 Value;\r | |
682 | UINT16 OpCode;\r | |
683 | \r | |
684 | ZeroMem (&Udp6CfgData, sizeof(EFI_UDP6_CONFIG_DATA));\r | |
685 | Udp6 = Instance->UdpIo->Protocol.Udp6;\r | |
686 | \r | |
687 | //\r | |
688 | // Set the live time of the packet.\r | |
689 | //\r | |
690 | Instance->PacketToLive = Instance->IsMaster ? Instance->Timeout : (Instance->Timeout * 2);\r | |
691 | \r | |
692 | Temp = (UINT16 *) NetbufGetByte (Packet, 0, NULL);\r | |
693 | ASSERT (Temp != NULL);\r | |
694 | \r | |
695 | Value = *Temp;\r | |
696 | OpCode = NTOHS (Value);\r | |
697 | \r | |
698 | if (OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR || OpCode == EFI_MTFTP6_OPCODE_WRQ) {\r | |
699 | //\r | |
700 | // For the Rrq, Dir, Wrq requests of the operation, configure the Udp6Io as\r | |
701 | // (serverip, 69, localip, localport) to send.\r | |
702 | // Usually local address and local port are both default as zero.\r | |
703 | //\r | |
704 | Status = Udp6->Configure (Udp6, NULL);\r | |
705 | \r | |
706 | if (EFI_ERROR (Status)) {\r | |
707 | return Status;\r | |
708 | }\r | |
709 | \r | |
710 | Status = Mtftp6ConfigUdpIo (\r | |
711 | Instance->UdpIo,\r | |
712 | &Instance->ServerIp,\r | |
713 | Instance->ServerCmdPort,\r | |
714 | &Instance->Config->StationIp,\r | |
715 | Instance->Config->LocalPort\r | |
716 | );\r | |
717 | \r | |
718 | if (EFI_ERROR (Status)) {\r | |
719 | return Status;\r | |
720 | }\r | |
721 | \r | |
722 | //\r | |
723 | // Get the current local address and port by get Udp6 mode data.\r | |
724 | //\r | |
725 | Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);\r | |
726 | if (EFI_ERROR (Status)) {\r | |
727 | return Status;\r | |
728 | }\r | |
729 | \r | |
730 | NET_GET_REF (Packet);\r | |
731 | \r | |
732 | Instance->IsTransmitted = FALSE;\r | |
733 | \r | |
734 | Status = UdpIoSendDatagram (\r | |
735 | Instance->UdpIo,\r | |
736 | Packet,\r | |
737 | NULL,\r | |
738 | NULL,\r | |
739 | Mtftp6OnPacketSent,\r | |
740 | &Instance->IsTransmitted\r | |
741 | );\r | |
742 | \r | |
743 | if (EFI_ERROR (Status)) {\r | |
744 | NET_PUT_REF (Packet);\r | |
745 | return Status;\r | |
746 | }\r | |
747 | \r | |
748 | //\r | |
749 | // Poll till the packet sent out from the ip6 queue.\r | |
750 | //\r | |
751 | gBS->RestoreTPL (Instance->OldTpl);\r | |
752 | \r | |
753 | while (!Instance->IsTransmitted) {\r | |
754 | Udp6->Poll (Udp6);\r | |
755 | }\r | |
756 | \r | |
757 | Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r | |
758 | \r | |
759 | //\r | |
760 | // For the subsequent exchange of such requests, reconfigure the Udp6Io as\r | |
761 | // (serverip, 0, localip, localport) to receive.\r | |
762 | // Currently local address and local port are specified by Udp6 mode data.\r | |
763 | //\r | |
764 | Status = Udp6->Configure (Udp6, NULL);\r | |
765 | \r | |
766 | if (EFI_ERROR (Status)) {\r | |
767 | return Status;\r | |
768 | }\r | |
769 | \r | |
770 | Status = Mtftp6ConfigUdpIo (\r | |
771 | Instance->UdpIo,\r | |
772 | &Instance->ServerIp,\r | |
773 | Instance->ServerDataPort,\r | |
774 | &Udp6CfgData.StationAddress,\r | |
775 | Udp6CfgData.StationPort\r | |
776 | );\r | |
777 | } else {\r | |
778 | //\r | |
779 | // For the data exchange, configure the Udp6Io as (serverip, dataport,\r | |
780 | // localip, localport) to send/receive.\r | |
781 | // Currently local address and local port are specified by Udp6 mode data.\r | |
782 | //\r | |
783 | Status = Udp6->GetModeData (Udp6, &Udp6CfgData, NULL, NULL, NULL);\r | |
784 | if (EFI_ERROR (Status)) {\r | |
785 | return Status;\r | |
786 | }\r | |
787 | \r | |
788 | if (Udp6CfgData.RemotePort != Instance->ServerDataPort) {\r | |
789 | \r | |
790 | Status = Udp6->Configure (Udp6, NULL);\r | |
791 | \r | |
792 | if (EFI_ERROR (Status)) {\r | |
793 | return Status;\r | |
794 | }\r | |
795 | \r | |
796 | Status = Mtftp6ConfigUdpIo (\r | |
797 | Instance->UdpIo,\r | |
798 | &Instance->ServerIp,\r | |
799 | Instance->ServerDataPort,\r | |
800 | &Udp6CfgData.StationAddress,\r | |
801 | Udp6CfgData.StationPort\r | |
802 | );\r | |
803 | \r | |
804 | if (EFI_ERROR (Status)) {\r | |
805 | return Status;\r | |
806 | }\r | |
807 | }\r | |
808 | \r | |
809 | NET_GET_REF (Packet);\r | |
810 | \r | |
811 | Instance->IsTransmitted = FALSE;\r | |
812 | \r | |
813 | Status = UdpIoSendDatagram (\r | |
814 | Instance->UdpIo,\r | |
815 | Packet,\r | |
816 | NULL,\r | |
817 | NULL,\r | |
818 | Mtftp6OnPacketSent,\r | |
819 | &Instance->IsTransmitted\r | |
820 | );\r | |
821 | \r | |
822 | if (EFI_ERROR (Status)) {\r | |
823 | NET_PUT_REF (Packet);\r | |
824 | }\r | |
825 | \r | |
826 | //\r | |
827 | // Poll till the packet sent out from the ip6 queue.\r | |
828 | //\r | |
829 | gBS->RestoreTPL (Instance->OldTpl);\r | |
830 | \r | |
831 | while (!Instance->IsTransmitted) {\r | |
832 | Udp6->Poll (Udp6);\r | |
833 | }\r | |
834 | \r | |
835 | Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r | |
836 | }\r | |
837 | \r | |
838 | return Status;\r | |
839 | }\r | |
840 | \r | |
841 | \r | |
842 | /**\r | |
843 | Check packet for GetInfo callback routine.\r | |
844 | \r | |
845 | GetInfo is implemented with EfiMtftp6ReadFile. It's used to inspect\r | |
846 | the first packet from server, then abort the session.\r | |
847 | \r | |
848 | @param[in] This The pointer to the Mtftp6 protocol.\r | |
849 | @param[in] Token The pointer to the Mtftp6 token.\r | |
850 | @param[in] PacketLen The length of the packet.\r | |
851 | @param[in] Packet The pointer to the received packet.\r | |
852 | \r | |
853 | @retval EFI_ABORTED Abort the Mtftp6 operation.\r | |
854 | \r | |
855 | **/\r | |
856 | EFI_STATUS\r | |
857 | EFIAPI\r | |
858 | Mtftp6CheckPacket (\r | |
859 | IN EFI_MTFTP6_PROTOCOL *This,\r | |
860 | IN EFI_MTFTP6_TOKEN *Token,\r | |
861 | IN UINT16 PacketLen,\r | |
862 | IN EFI_MTFTP6_PACKET *Packet\r | |
863 | )\r | |
864 | {\r | |
865 | MTFTP6_GETINFO_CONTEXT *Context;\r | |
866 | UINT16 OpCode;\r | |
867 | \r | |
868 | Context = (MTFTP6_GETINFO_CONTEXT *) Token->Context;\r | |
869 | OpCode = NTOHS (Packet->OpCode);\r | |
870 | \r | |
871 | //\r | |
872 | // Set the GetInfo's return status according to the OpCode.\r | |
873 | //\r | |
874 | switch (OpCode) {\r | |
875 | case EFI_MTFTP6_OPCODE_ERROR:\r | |
876 | Context->Status = EFI_TFTP_ERROR;\r | |
877 | break;\r | |
878 | \r | |
879 | case EFI_MTFTP6_OPCODE_OACK:\r | |
880 | Context->Status = EFI_SUCCESS;\r | |
881 | break;\r | |
882 | \r | |
883 | default:\r | |
884 | Context->Status = EFI_PROTOCOL_ERROR;\r | |
885 | }\r | |
886 | \r | |
887 | //\r | |
888 | // Allocate buffer then copy the packet over. Use gBS->AllocatePool\r | |
889 | // in case NetAllocatePool will implements something tricky.\r | |
890 | //\r | |
891 | *(Context->Packet) = AllocateZeroPool (PacketLen);\r | |
892 | \r | |
893 | if (*(Context->Packet) == NULL) {\r | |
894 | Context->Status = EFI_OUT_OF_RESOURCES;\r | |
895 | return EFI_ABORTED;\r | |
896 | }\r | |
897 | \r | |
898 | *(Context->PacketLen) = PacketLen;\r | |
899 | CopyMem (*(Context->Packet), Packet, PacketLen);\r | |
900 | \r | |
901 | return EFI_ABORTED;\r | |
902 | }\r | |
903 | \r | |
904 | \r | |
905 | /**\r | |
906 | Clean up the current Mtftp6 operation.\r | |
907 | \r | |
908 | @param[in] Instance The pointer to the Mtftp6 instance.\r | |
909 | @param[in] Result The result to be returned to the user.\r | |
910 | \r | |
911 | **/\r | |
912 | VOID\r | |
913 | Mtftp6OperationClean (\r | |
914 | IN MTFTP6_INSTANCE *Instance,\r | |
915 | IN EFI_STATUS Result\r | |
916 | )\r | |
917 | {\r | |
918 | LIST_ENTRY *Entry;\r | |
919 | LIST_ENTRY *Next;\r | |
920 | MTFTP6_BLOCK_RANGE *Block;\r | |
921 | \r | |
922 | //\r | |
923 | // Clean up the current token and event.\r | |
924 | //\r | |
925 | if (Instance->Token != NULL) {\r | |
926 | Instance->Token->Status = Result;\r | |
927 | if (Instance->Token->Event != NULL) {\r | |
928 | gBS->SignalEvent (Instance->Token->Event);\r | |
929 | }\r | |
930 | Instance->Token = NULL;\r | |
931 | }\r | |
932 | \r | |
933 | //\r | |
934 | // Clean up the corresponding Udp6Io.\r | |
935 | //\r | |
936 | if (Instance->UdpIo != NULL) {\r | |
937 | UdpIoCleanIo (Instance->UdpIo);\r | |
938 | }\r | |
939 | \r | |
940 | if (Instance->McastUdpIo != NULL) {\r | |
216f7970 | 941 | gBS->CloseProtocol (\r |
942 | Instance->McastUdpIo->UdpHandle,\r | |
943 | &gEfiUdp6ProtocolGuid,\r | |
944 | Instance->McastUdpIo->Image,\r | |
945 | Instance->Handle\r | |
946 | );\r | |
a3bcde70 HT |
947 | UdpIoFreeIo (Instance->McastUdpIo);\r |
948 | Instance->McastUdpIo = NULL;\r | |
949 | }\r | |
950 | \r | |
951 | //\r | |
952 | // Clean up the stored last packet.\r | |
953 | //\r | |
954 | if (Instance->LastPacket != NULL) {\r | |
955 | NetbufFree (Instance->LastPacket);\r | |
956 | Instance->LastPacket = NULL;\r | |
957 | }\r | |
958 | \r | |
959 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &Instance->BlkList) {\r | |
960 | Block = NET_LIST_USER_STRUCT (Entry, MTFTP6_BLOCK_RANGE, Link);\r | |
961 | RemoveEntryList (Entry);\r | |
962 | FreePool (Block);\r | |
963 | }\r | |
964 | \r | |
965 | //\r | |
966 | // Reinitialize the corresponding fields of the Mtftp6 operation.\r | |
967 | //\r | |
968 | ZeroMem (&Instance->ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));\r | |
969 | ZeroMem (&Instance->ServerIp, sizeof (EFI_IPv6_ADDRESS));\r | |
970 | ZeroMem (&Instance->McastIp, sizeof (EFI_IPv6_ADDRESS));\r | |
971 | \r | |
972 | Instance->ServerCmdPort = 0;\r | |
973 | Instance->ServerDataPort = 0;\r | |
974 | Instance->McastPort = 0;\r | |
975 | Instance->BlkSize = 0;\r | |
f3427f12 JW |
976 | Instance->Operation = 0;\r |
977 | Instance->WindowSize = 1;\r | |
978 | Instance->TotalBlock = 0;\r | |
979 | Instance->AckedBlock = 0;\r | |
a3bcde70 HT |
980 | Instance->LastBlk = 0;\r |
981 | Instance->PacketToLive = 0;\r | |
982 | Instance->MaxRetry = 0;\r | |
983 | Instance->CurRetry = 0;\r | |
984 | Instance->Timeout = 0;\r | |
985 | Instance->IsMaster = TRUE;\r | |
986 | }\r | |
987 | \r | |
988 | \r | |
989 | /**\r | |
990 | Start the Mtftp6 instance to perform the operation, such as read file,\r | |
991 | write file, and read directory.\r | |
992 | \r | |
993 | @param[in] This The MTFTP session.\r | |
994 | @param[in] Token The token than encapsues the user's request.\r | |
995 | @param[in] OpCode The operation to perform.\r | |
996 | \r | |
997 | @retval EFI_INVALID_PARAMETER Some of the parameters are invalid.\r | |
998 | @retval EFI_NOT_STARTED The MTFTP session hasn't been configured.\r | |
999 | @retval EFI_ALREADY_STARTED There is pending operation for the session.\r | |
1000 | @retval EFI_SUCCESS The operation is successfully started.\r | |
1001 | \r | |
1002 | **/\r | |
1003 | EFI_STATUS\r | |
1004 | Mtftp6OperationStart (\r | |
1005 | IN EFI_MTFTP6_PROTOCOL *This,\r | |
1006 | IN EFI_MTFTP6_TOKEN *Token,\r | |
1007 | IN UINT16 OpCode\r | |
1008 | )\r | |
1009 | {\r | |
1010 | MTFTP6_INSTANCE *Instance;\r | |
1011 | EFI_STATUS Status;\r | |
1012 | \r | |
1013 | if (This == NULL ||\r | |
1014 | Token == NULL ||\r | |
1015 | Token->Filename == NULL ||\r | |
1016 | (Token->OptionCount != 0 && Token->OptionList == NULL) ||\r | |
1017 | (Token->OverrideData != NULL && !NetIp6IsValidUnicast (&Token->OverrideData->ServerIp))\r | |
1018 | ) {\r | |
1019 | return EFI_INVALID_PARAMETER;\r | |
1020 | }\r | |
1021 | \r | |
1022 | //\r | |
1023 | // At least define one method to collect the data for download.\r | |
1024 | //\r | |
1025 | if ((OpCode == EFI_MTFTP6_OPCODE_RRQ || OpCode == EFI_MTFTP6_OPCODE_DIR) &&\r | |
1026 | Token->Buffer == NULL &&\r | |
1027 | Token->CheckPacket == NULL\r | |
1028 | ) {\r | |
1029 | return EFI_INVALID_PARAMETER;\r | |
1030 | }\r | |
1031 | \r | |
1032 | //\r | |
1033 | // At least define one method to provide the data for upload.\r | |
1034 | //\r | |
1035 | if (OpCode == EFI_MTFTP6_OPCODE_WRQ && Token->Buffer == NULL && Token->PacketNeeded == NULL) {\r | |
1036 | return EFI_INVALID_PARAMETER;\r | |
1037 | }\r | |
1038 | \r | |
1039 | Instance = MTFTP6_INSTANCE_FROM_THIS (This);\r | |
1040 | \r | |
1041 | if (Instance->Config == NULL) {\r | |
1042 | return EFI_NOT_STARTED;\r | |
1043 | }\r | |
1044 | \r | |
1045 | if (Instance->Token != NULL) {\r | |
1046 | return EFI_ACCESS_DENIED;\r | |
1047 | }\r | |
1048 | \r | |
1049 | Status = EFI_SUCCESS;\r | |
1050 | Instance->OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r | |
1051 | \r | |
f3427f12 JW |
1052 | Instance->Operation = OpCode;\r |
1053 | \r | |
a3bcde70 HT |
1054 | //\r |
1055 | // Parse the extension options in the request packet.\r | |
1056 | //\r | |
1057 | if (Token->OptionCount != 0) {\r | |
1058 | \r | |
1059 | Status = Mtftp6ParseExtensionOption (\r | |
1060 | Token->OptionList,\r | |
1061 | Token->OptionCount,\r | |
1062 | TRUE,\r | |
f3427f12 | 1063 | Instance->Operation,\r |
a3bcde70 HT |
1064 | &Instance->ExtInfo\r |
1065 | );\r | |
1066 | \r | |
1067 | if (EFI_ERROR (Status)) {\r | |
1068 | goto ON_ERROR;\r | |
1069 | }\r | |
1070 | }\r | |
1071 | \r | |
1072 | //\r | |
1073 | // Initialize runtime data from config data or override data.\r | |
1074 | //\r | |
1075 | Instance->Token = Token;\r | |
1076 | Instance->ServerCmdPort = Instance->Config->InitialServerPort;\r | |
1077 | Instance->ServerDataPort = 0;\r | |
1078 | Instance->MaxRetry = Instance->Config->TryCount;\r | |
1079 | Instance->Timeout = Instance->Config->TimeoutValue;\r | |
1080 | Instance->IsMaster = TRUE;\r | |
1081 | \r | |
1082 | CopyMem (\r | |
1083 | &Instance->ServerIp,\r | |
1084 | &Instance->Config->ServerIp,\r | |
1085 | sizeof (EFI_IPv6_ADDRESS)\r | |
1086 | );\r | |
1087 | \r | |
1088 | if (Token->OverrideData != NULL) {\r | |
1089 | Instance->ServerCmdPort = Token->OverrideData->ServerPort;\r | |
1090 | Instance->MaxRetry = Token->OverrideData->TryCount;\r | |
1091 | Instance->Timeout = Token->OverrideData->TimeoutValue;\r | |
1092 | \r | |
1093 | CopyMem (\r | |
1094 | &Instance->ServerIp,\r | |
1095 | &Token->OverrideData->ServerIp,\r | |
1096 | sizeof (EFI_IPv6_ADDRESS)\r | |
1097 | );\r | |
1098 | }\r | |
1099 | \r | |
1100 | //\r | |
1101 | // Set default value for undefined parameters.\r | |
1102 | //\r | |
1103 | if (Instance->ServerCmdPort == 0) {\r | |
1104 | Instance->ServerCmdPort = MTFTP6_DEFAULT_SERVER_CMD_PORT;\r | |
1105 | }\r | |
1106 | if (Instance->BlkSize == 0) {\r | |
1107 | Instance->BlkSize = MTFTP6_DEFAULT_BLK_SIZE;\r | |
f3427f12 JW |
1108 | }\r |
1109 | if (Instance->WindowSize == 0) {\r | |
1110 | Instance->WindowSize = MTFTP6_DEFAULT_WINDOWSIZE;\r | |
a3bcde70 HT |
1111 | }\r |
1112 | if (Instance->MaxRetry == 0) {\r | |
1113 | Instance->MaxRetry = MTFTP6_DEFAULT_MAX_RETRY;\r | |
1114 | }\r | |
1115 | if (Instance->Timeout == 0) {\r | |
1116 | Instance->Timeout = MTFTP6_DEFAULT_TIMEOUT;\r | |
1117 | }\r | |
1118 | \r | |
1119 | Token->Status = EFI_NOT_READY;\r | |
1120 | \r | |
1121 | //\r | |
1122 | // Switch the routines by the operation code.\r | |
1123 | //\r | |
1124 | switch (OpCode) {\r | |
1125 | case EFI_MTFTP6_OPCODE_RRQ:\r | |
1126 | Status = Mtftp6RrqStart (Instance, OpCode);\r | |
1127 | break;\r | |
1128 | \r | |
1129 | case EFI_MTFTP6_OPCODE_DIR:\r | |
1130 | Status = Mtftp6RrqStart (Instance, OpCode);\r | |
1131 | break;\r | |
1132 | \r | |
1133 | case EFI_MTFTP6_OPCODE_WRQ:\r | |
1134 | Status = Mtftp6WrqStart (Instance, OpCode);\r | |
1135 | break;\r | |
1136 | \r | |
1137 | default:\r | |
1138 | Status = EFI_DEVICE_ERROR;\r | |
1139 | goto ON_ERROR;\r | |
1140 | }\r | |
1141 | \r | |
1142 | if (EFI_ERROR (Status)) {\r | |
1143 | goto ON_ERROR;\r | |
1144 | }\r | |
1145 | \r | |
1146 | //\r | |
1147 | // Return immediately for asynchronous or poll the instance for synchronous.\r | |
1148 | //\r | |
1149 | gBS->RestoreTPL (Instance->OldTpl);\r | |
1150 | \r | |
1151 | if (Token->Event == NULL) {\r | |
1152 | while (Token->Status == EFI_NOT_READY) {\r | |
1153 | This->Poll (This);\r | |
1154 | }\r | |
1155 | return Token->Status;\r | |
1156 | }\r | |
1157 | \r | |
1158 | return EFI_SUCCESS;\r | |
1159 | \r | |
1160 | ON_ERROR:\r | |
1161 | \r | |
1162 | Mtftp6OperationClean (Instance, Status);\r | |
1163 | gBS->RestoreTPL (Instance->OldTpl);\r | |
1164 | \r | |
1165 | return Status;\r | |
1166 | }\r | |
1167 | \r | |
1168 | \r | |
1169 | /**\r | |
1170 | The timer ticking routine for the Mtftp6 instance.\r | |
1171 | \r | |
1172 | @param[in] Event The pointer to the ticking event.\r | |
1173 | @param[in] Context The pointer to the context.\r | |
1174 | \r | |
1175 | **/\r | |
1176 | VOID\r | |
1177 | EFIAPI\r | |
1178 | Mtftp6OnTimerTick (\r | |
1179 | IN EFI_EVENT Event,\r | |
1180 | IN VOID *Context\r | |
1181 | )\r | |
1182 | {\r | |
1183 | MTFTP6_SERVICE *Service;\r | |
1184 | MTFTP6_INSTANCE *Instance;\r | |
1185 | LIST_ENTRY *Entry;\r | |
1186 | LIST_ENTRY *Next;\r | |
1187 | EFI_MTFTP6_TOKEN *Token;\r | |
1188 | EFI_STATUS Status;\r | |
1189 | \r | |
1190 | Service = (MTFTP6_SERVICE *) Context;\r | |
1191 | \r | |
1192 | //\r | |
1193 | // Iterate through all the children of the Mtftp service instance. Time\r | |
1194 | // out the packet. If maximum retries reached, clean the session up.\r | |
1195 | //\r | |
1196 | NET_LIST_FOR_EACH_SAFE (Entry, Next, &Service->Children) {\r | |
1197 | \r | |
1198 | Instance = NET_LIST_USER_STRUCT (Entry, MTFTP6_INSTANCE, Link);\r | |
1199 | \r | |
1200 | if (Instance->Token == NULL) {\r | |
1201 | continue;\r | |
1202 | }\r | |
1203 | \r | |
1204 | if (Instance->PacketToLive > 0) {\r | |
1205 | Instance->PacketToLive--;\r | |
1206 | continue;\r | |
1207 | }\r | |
1208 | \r | |
1209 | Instance->CurRetry++;\r | |
1210 | Token = Instance->Token;\r | |
1211 | \r | |
1212 | if (Token->TimeoutCallback != NULL) {\r | |
1213 | //\r | |
1214 | // Call the timeout callback routine if has.\r | |
1215 | //\r | |
1216 | Status = Token->TimeoutCallback (&Instance->Mtftp6, Token);\r | |
1217 | \r | |
1218 | if (EFI_ERROR (Status)) {\r | |
1219 | Mtftp6SendError (\r | |
1220 | Instance,\r | |
1221 | EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,\r | |
1222 | (UINT8 *) "User aborted the transfer in time out"\r | |
1223 | );\r | |
1224 | Mtftp6OperationClean (Instance, EFI_ABORTED);\r | |
1225 | continue;\r | |
1226 | }\r | |
1227 | }\r | |
1228 | \r | |
1229 | //\r | |
1230 | // Retransmit the packet if haven't reach the maxmium retry count,\r | |
1231 | // otherwise exit the transfer.\r | |
1232 | //\r | |
1233 | if (Instance->CurRetry < Instance->MaxRetry) {\r | |
1234 | Mtftp6TransmitPacket (Instance, Instance->LastPacket);\r | |
1235 | } else {\r | |
1236 | Mtftp6OperationClean (Instance, EFI_TIMEOUT);\r | |
1237 | continue;\r | |
1238 | }\r | |
1239 | }\r | |
1240 | }\r |