VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/Firmware/NetworkPkg/Mtftp6Dxe/Mtftp6Wrq.c@ 48674

Last change on this file since 48674 was 48674, checked in by vboxsync, 11 years ago

EFI: Export newly imported tinaocore UEFI sources to OSE.

  • Property svn:eol-style set to native
File size: 16.1 KB
Line 
1/** @file
2 Mtftp6 Wrq process functions implementation.
3
4 Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14**/
15
16#include "Mtftp6Impl.h"
17
18
19
20/**
21 Build and send a Mtftp6 data packet for upload.
22
23 @param[in] Instance The pointer to the Mtftp6 instance.
24 @param[in] BlockNum The block num to be sent.
25
26 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for the packet.
27 @retval EFI_SUCCESS The data packet was sent.
28 @retval EFI_ABORTED The user aborted this process.
29
30**/
31EFI_STATUS
32Mtftp6WrqSendBlock (
33 IN MTFTP6_INSTANCE *Instance,
34 IN UINT16 BlockNum
35 )
36{
37 EFI_MTFTP6_PACKET *Packet;
38 EFI_MTFTP6_TOKEN *Token;
39 NET_BUF *UdpPacket;
40 EFI_STATUS Status;
41 UINT16 DataLen;
42 UINT8 *DataBuf;
43 UINT64 Start;
44
45 //
46 // Allocate net buffer to create data packet.
47 //
48 UdpPacket = NetbufAlloc (Instance->BlkSize + MTFTP6_DATA_HEAD_LEN);
49
50 if (UdpPacket == NULL) {
51 return EFI_OUT_OF_RESOURCES;
52 }
53
54 Packet = (EFI_MTFTP6_PACKET *) NetbufAllocSpace (
55 UdpPacket,
56 MTFTP6_DATA_HEAD_LEN,
57 FALSE
58 );
59 ASSERT (Packet != NULL);
60
61 Packet->Data.OpCode = HTONS (EFI_MTFTP6_OPCODE_DATA);
62 Packet->Data.Block = HTONS (BlockNum);
63
64 //
65 // Read the block from either the buffer or PacketNeeded callback
66 //
67 Token = Instance->Token;
68 DataLen = Instance->BlkSize;
69
70 if (Token->Buffer != NULL) {
71 Start = MultU64x32 (BlockNum - 1, Instance->BlkSize);
72
73 if (Token->BufferSize < Start + Instance->BlkSize) {
74 DataLen = (UINT16) (Token->BufferSize - Start);
75 Instance->LastBlk = BlockNum;
76 Mtftp6SetLastBlockNum (&Instance->BlkList, BlockNum);
77 }
78
79 if (DataLen > 0) {
80 NetbufAllocSpace (UdpPacket, DataLen, FALSE);
81 CopyMem (Packet->Data.Data, (UINT8 *) Token->Buffer + Start, DataLen);
82 }
83
84 } else {
85 //
86 // Get data from PacketNeeded
87 //
88 DataBuf = NULL;
89 Status = Token->PacketNeeded (&Instance->Mtftp6, Token, &DataLen, (VOID*) &DataBuf);
90
91 if (EFI_ERROR (Status) || (DataLen > Instance->BlkSize)) {
92 if (DataBuf != NULL) {
93 gBS->FreePool (DataBuf);
94 }
95 //
96 // The received packet has already been freed.
97 //
98 Mtftp6SendError (
99 Instance,
100 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,
101 (UINT8 *) "User aborted the transfer"
102 );
103
104 return EFI_ABORTED;
105 }
106
107 if (DataLen < Instance->BlkSize) {
108 Instance->LastBlk = BlockNum;
109 Mtftp6SetLastBlockNum (&Instance->BlkList, BlockNum);
110 }
111
112 if (DataLen > 0) {
113 NetbufAllocSpace (UdpPacket, DataLen, FALSE);
114 CopyMem (Packet->Data.Data, DataBuf, DataLen);
115 gBS->FreePool (DataBuf);
116 }
117 }
118
119 //
120 // Reset current retry count of the instance.
121 //
122 Instance->CurRetry = 0;
123
124 return Mtftp6TransmitPacket (Instance, UdpPacket);
125}
126
127
128/**
129 Function to handle received ACK packet. If the ACK number matches the
130 expected block number, with more data pending, send the next
131 block. Otherwise, tell the caller that we are done.
132
133 @param[in] Instance The pointer to the Mtftp6 instance.
134 @param[in] Packet The pointer to the received packet.
135 @param[in] Len The length of the packet.
136 @param[out] UdpPacket The net buf of received packet.
137 @param[out] IsCompleted If TRUE, the upload has been completed.
138 Otherwise, the upload has not been completed.
139
140 @retval EFI_SUCCESS The ACK packet successfully processed.
141 @retval EFI_TFTP_ERROR The block number loops back.
142 @retval Others Failed to transmit the next data packet.
143
144**/
145EFI_STATUS
146Mtftp6WrqHandleAck (
147 IN MTFTP6_INSTANCE *Instance,
148 IN EFI_MTFTP6_PACKET *Packet,
149 IN UINT32 Len,
150 OUT NET_BUF **UdpPacket,
151 OUT BOOLEAN *IsCompleted
152 )
153{
154 UINT16 AckNum;
155 INTN Expected;
156 UINT64 TotalBlock;
157
158 *IsCompleted = FALSE;
159 AckNum = NTOHS (Packet->Ack.Block[0]);
160 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);
161
162 ASSERT (Expected >= 0);
163
164 //
165 // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp6WrqInput
166 // restart receive.
167 //
168 if (Expected != AckNum) {
169 return EFI_SUCCESS;
170 }
171
172 //
173 // Remove the acked block number, if this is the last block number,
174 // tell the Mtftp6WrqInput to finish the transfer. This is the last
175 // block number if the block range are empty..
176 //
177 Mtftp6RemoveBlockNum (&Instance->BlkList, AckNum, *IsCompleted, &TotalBlock);
178
179 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);
180
181 if (Expected < 0) {
182 //
183 // The block range is empty. It may either because the the last
184 // block has been ACKed, or the sequence number just looped back,
185 // that is, there is more than 0xffff blocks.
186 //
187 if (Instance->LastBlk == AckNum) {
188 ASSERT (Instance->LastBlk >= 1);
189 *IsCompleted = TRUE;
190 return EFI_SUCCESS;
191
192 } else {
193 //
194 // Free the received packet before send new packet in ReceiveNotify,
195 // since the udpio might need to be reconfigured.
196 //
197 NetbufFree (*UdpPacket);
198 *UdpPacket = NULL;
199 //
200 // Send the Mtftp6 error message if block number rolls back.
201 //
202 Mtftp6SendError (
203 Instance,
204 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,
205 (UINT8 *) "Block number rolls back, not supported, try blksize option"
206 );
207
208 return EFI_TFTP_ERROR;
209 }
210 }
211
212 //
213 // Free the receive buffer before send new packet since it might need
214 // reconfigure udpio.
215 //
216 NetbufFree (*UdpPacket);
217 *UdpPacket = NULL;
218
219 return Mtftp6WrqSendBlock (Instance, (UINT16) Expected);
220}
221
222
223/**
224 Check whether the received OACK is valid. The OACK is valid
225 only if:
226 1. It only include options requested by us.
227 2. It can only include a smaller block size.
228 3. It can't change the proposed time out value.
229 4. Other requirements of the individal MTFTP6 options as required.
230
231 @param[in] ReplyInfo The pointer to options information in reply packet.
232 @param[in] RequestInfo The pointer to requested options information.
233
234 @retval TRUE If the option in OACK is valid.
235 @retval FALSE If the option is invalid.
236
237**/
238BOOLEAN
239Mtftp6WrqOackValid (
240 IN MTFTP6_EXT_OPTION_INFO *ReplyInfo,
241 IN MTFTP6_EXT_OPTION_INFO *RequestInfo
242 )
243{
244 //
245 // It is invalid for server to return options we don't request
246 //
247 if ((ReplyInfo->BitMap & ~RequestInfo->BitMap) != 0) {
248 return FALSE;
249 }
250
251 //
252 // Server can only specify a smaller block size to be used and
253 // return the timeout matches that requested.
254 //
255 if ((((ReplyInfo->BitMap & MTFTP6_OPT_BLKSIZE_BIT) != 0) && (ReplyInfo->BlkSize > RequestInfo->BlkSize)) ||
256 (((ReplyInfo->BitMap & MTFTP6_OPT_TIMEOUT_BIT) != 0) && (ReplyInfo->Timeout != RequestInfo->Timeout))
257 ) {
258
259 return FALSE;
260 }
261
262 return TRUE;
263}
264
265
266/**
267 Process the OACK packet for Wrq.
268
269 @param[in] Instance The pointer to the Mtftp6 instance.
270 @param[in] Packet The pointer to the received packet.
271 @param[in] Len The length of the packet.
272 @param[out] UdpPacket The net buf of received packet.
273 @param[out] IsCompleted If TRUE, the upload has been completed.
274 Otherwise, the upload has not been completed.
275
276 @retval EFI_SUCCESS The OACK packet successfully processed.
277 @retval EFI_TFTP_ERROR An TFTP communication error happened.
278 @retval Others Failed to process the OACK packet.
279
280**/
281EFI_STATUS
282Mtftp6WrqHandleOack (
283 IN MTFTP6_INSTANCE *Instance,
284 IN EFI_MTFTP6_PACKET *Packet,
285 IN UINT32 Len,
286 OUT NET_BUF **UdpPacket,
287 OUT BOOLEAN *IsCompleted
288 )
289{
290 EFI_MTFTP6_OPTION *Options;
291 UINT32 Count;
292 MTFTP6_EXT_OPTION_INFO ExtInfo;
293 EFI_MTFTP6_PACKET Dummy;
294 EFI_STATUS Status;
295 INTN Expected;
296
297 *IsCompleted = FALSE;
298
299 //
300 // Ignore the OACK if already started the upload
301 //
302 Expected = Mtftp6GetNextBlockNum (&Instance->BlkList);
303
304 if (Expected != 0) {
305 return EFI_SUCCESS;
306 }
307
308 //
309 // Parse and validate the options from server
310 //
311 ZeroMem (&ExtInfo, sizeof (MTFTP6_EXT_OPTION_INFO));
312
313 Status = Mtftp6ParseStart (Packet, Len, &Count, &Options);
314
315 if (EFI_ERROR (Status)) {
316 return Status;
317 }
318 ASSERT (Options != NULL);
319
320 Status = Mtftp6ParseExtensionOption (Options, Count, FALSE, &ExtInfo);
321
322 if (EFI_ERROR(Status) || !Mtftp6WrqOackValid (&ExtInfo, &Instance->ExtInfo)) {
323 //
324 // Don't send a MTFTP error packet when out of resource, it can
325 // only make it worse.
326 //
327 if (Status != EFI_OUT_OF_RESOURCES) {
328 //
329 // Free the received packet before send new packet in ReceiveNotify,
330 // since the udpio might need to be reconfigured.
331 //
332 NetbufFree (*UdpPacket);
333 *UdpPacket = NULL;
334 //
335 // Send the Mtftp6 error message if invalid Oack packet received.
336 //
337 Mtftp6SendError (
338 Instance,
339 EFI_MTFTP6_ERRORCODE_ILLEGAL_OPERATION,
340 (UINT8 *) "Mal-formated OACK packet"
341 );
342 }
343
344 return EFI_TFTP_ERROR;
345 }
346
347 if (ExtInfo.BlkSize != 0) {
348 Instance->BlkSize = ExtInfo.BlkSize;
349 }
350
351 if (ExtInfo.Timeout != 0) {
352 Instance->Timeout = ExtInfo.Timeout;
353 }
354
355 //
356 // Build a bogus ACK0 packet then pass it to the Mtftp6WrqHandleAck,
357 // which will start the transmission of the first data block.
358 //
359 Dummy.Ack.OpCode = HTONS (EFI_MTFTP6_OPCODE_ACK);
360 Dummy.Ack.Block[0] = 0;
361
362 return Mtftp6WrqHandleAck (
363 Instance,
364 &Dummy,
365 sizeof (EFI_MTFTP6_ACK_HEADER),
366 UdpPacket,
367 IsCompleted
368 );
369}
370
371
372/**
373 The packet process callback for Mtftp6 upload.
374
375 @param[in] UdpPacket The pointer to the packet received.
376 @param[in] UdpEpt The pointer to the Udp6 access point.
377 @param[in] IoStatus The status from Udp6 instance.
378 @param[in] Context The pointer to the context.
379
380**/
381VOID
382EFIAPI
383Mtftp6WrqInput (
384 IN NET_BUF *UdpPacket,
385 IN UDP_END_POINT *UdpEpt,
386 IN EFI_STATUS IoStatus,
387 IN VOID *Context
388 )
389{
390 MTFTP6_INSTANCE *Instance;
391 EFI_MTFTP6_PACKET *Packet;
392 BOOLEAN IsCompleted;
393 EFI_STATUS Status;
394 UINT32 TotalNum;
395 UINT32 Len;
396 UINT16 Opcode;
397
398 Instance = (MTFTP6_INSTANCE *) Context;
399
400 NET_CHECK_SIGNATURE (Instance, MTFTP6_INSTANCE_SIGNATURE);
401
402 IsCompleted = FALSE;
403 Packet = NULL;
404 Status = EFI_SUCCESS;
405 TotalNum = 0;
406
407 //
408 // Return error status if Udp6 instance failed to receive.
409 //
410 if (EFI_ERROR (IoStatus)) {
411 Status = IoStatus;
412 goto ON_EXIT;
413 }
414
415 ASSERT (UdpPacket != NULL);
416
417 if (UdpPacket->TotalSize < MTFTP6_OPCODE_LEN) {
418 goto ON_EXIT;
419 }
420
421 //
422 // Client send initial request to server's listening port. Server
423 // will select a UDP port to communicate with the client.
424 //
425 if (UdpEpt->RemotePort != Instance->ServerDataPort) {
426 if (Instance->ServerDataPort != 0) {
427 goto ON_EXIT;
428 } else {
429 Instance->ServerDataPort = UdpEpt->RemotePort;
430 }
431 }
432
433 //
434 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
435 //
436 Len = UdpPacket->TotalSize;
437 TotalNum = UdpPacket->BlockOpNum;
438
439 if (TotalNum > 1) {
440 Packet = AllocateZeroPool (Len);
441
442 if (Packet == NULL) {
443 Status = EFI_OUT_OF_RESOURCES;
444 goto ON_EXIT;
445 }
446
447 NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet);
448
449 } else {
450 Packet = (EFI_MTFTP6_PACKET *) NetbufGetByte (UdpPacket, 0, NULL);
451 ASSERT (Packet != NULL);
452 }
453
454 Opcode = NTOHS (Packet->OpCode);
455
456 //
457 // Callback to the user's CheckPacket if provided. Abort the transmission
458 // if CheckPacket returns an EFI_ERROR code.
459 //
460 if (Instance->Token->CheckPacket != NULL &&
461 (Opcode == EFI_MTFTP6_OPCODE_OACK || Opcode == EFI_MTFTP6_OPCODE_ERROR)
462 ) {
463
464 Status = Instance->Token->CheckPacket (
465 &Instance->Mtftp6,
466 Instance->Token,
467 (UINT16) Len,
468 Packet
469 );
470
471 if (EFI_ERROR (Status)) {
472 //
473 // Send an error message to the server to inform it
474 //
475 if (Opcode != EFI_MTFTP6_OPCODE_ERROR) {
476 //
477 // Free the received packet before send new packet in ReceiveNotify,
478 // since the udpio might need to be reconfigured.
479 //
480 NetbufFree (UdpPacket);
481 UdpPacket = NULL;
482 //
483 // Send the Mtftp6 error message if user aborted the current session.
484 //
485 Mtftp6SendError (
486 Instance,
487 EFI_MTFTP6_ERRORCODE_REQUEST_DENIED,
488 (UINT8 *) "User aborted the transfer"
489 );
490 }
491
492 Status = EFI_ABORTED;
493 goto ON_EXIT;
494 }
495 }
496
497 //
498 // Switch the process routines by the operation code.
499 //
500 switch (Opcode) {
501 case EFI_MTFTP6_OPCODE_ACK:
502 if (Len != MTFTP6_OPCODE_LEN + MTFTP6_BLKNO_LEN) {
503 goto ON_EXIT;
504 }
505 //
506 // Handle the Ack packet of Wrq.
507 //
508 Status = Mtftp6WrqHandleAck (Instance, Packet, Len, &UdpPacket, &IsCompleted);
509 break;
510
511 case EFI_MTFTP6_OPCODE_OACK:
512 if (Len <= MTFTP6_OPCODE_LEN) {
513 goto ON_EXIT;
514 }
515 //
516 // Handle the Oack packet of Wrq.
517 //
518 Status = Mtftp6WrqHandleOack (Instance, Packet, Len, &UdpPacket, &IsCompleted);
519 break;
520
521 default:
522 //
523 // Drop and return eror if received error message.
524 //
525 Status = EFI_TFTP_ERROR;
526 break;
527 }
528
529ON_EXIT:
530 //
531 // Free the resources, then if !EFI_ERROR (Status) and not completed,
532 // restart the receive, otherwise end the session.
533 //
534 if (Packet != NULL && TotalNum > 1) {
535 FreePool (Packet);
536 }
537
538 if (UdpPacket != NULL) {
539 NetbufFree (UdpPacket);
540 }
541
542 if (!EFI_ERROR (Status) && !IsCompleted) {
543 Status = UdpIoRecvDatagram (
544 Instance->UdpIo,
545 Mtftp6WrqInput,
546 Instance,
547 0
548 );
549 }
550 //
551 // Clean up the current session if failed to continue.
552 //
553 if (EFI_ERROR (Status) || IsCompleted) {
554 Mtftp6OperationClean (Instance, Status);
555 }
556}
557
558
559/**
560 Start the Mtftp6 instance to upload. It will first init some states,
561 then send the WRQ request packet, and start to receive the packet.
562
563 @param[in] Instance The pointer to the Mtftp6 instance.
564 @param[in] Operation The operation code of the current packet.
565
566 @retval EFI_SUCCESS The Mtftp6 was started to upload.
567 @retval Others Failed to start to upload.
568
569**/
570EFI_STATUS
571Mtftp6WrqStart (
572 IN MTFTP6_INSTANCE *Instance,
573 IN UINT16 Operation
574 )
575{
576 EFI_STATUS Status;
577
578 //
579 // The valid block number range are [0, 0xffff]. For example:
580 // the client sends an WRQ request to the server, the server
581 // ACK with an ACK0 to let client start transfer the first
582 // packet.
583 //
584 Status = Mtftp6InitBlockRange (&Instance->BlkList, 0, 0xffff);
585
586 if (EFI_ERROR (Status)) {
587 return Status;
588 }
589
590 Status = Mtftp6SendRequest (Instance, Operation);
591
592 if (EFI_ERROR (Status)) {
593 return Status;
594 }
595
596 return UdpIoRecvDatagram (
597 Instance->UdpIo,
598 Mtftp6WrqInput,
599 Instance,
600 0
601 );
602}
603
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette