VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/Mtftp4Dxe/Mtftp4Wrq.c@ 80721

Last change on this file since 80721 was 80721, checked in by vboxsync, 5 years ago

Devices/EFI/FirmwareNew: Start upgrade process to edk2-stable201908 (compiles on Windows and works to some extent), bugref:4643

  • Property svn:eol-style set to native
File size: 13.5 KB
Line 
1/** @file
2 Routines to process Wrq (upload).
3
4Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5SPDX-License-Identifier: BSD-2-Clause-Patent
6
7**/
8
9#include "Mtftp4Impl.h"
10
11
12
13/**
14 Build then send a MTFTP data packet for the MTFTP upload session.
15
16 @param Instance The MTFTP upload session.
17 @param BlockNum The block number to send.
18
19 @retval EFI_OUT_OF_RESOURCES Failed to build the packet.
20 @retval EFI_ABORTED The consumer of this child directs to abort the
21 transmission by return an error through PacketNeeded.
22 @retval EFI_SUCCESS The data is sent.
23
24**/
25EFI_STATUS
26Mtftp4WrqSendBlock (
27 IN OUT MTFTP4_PROTOCOL *Instance,
28 IN UINT16 BlockNum
29 )
30{
31 EFI_MTFTP4_PACKET *Packet;
32 EFI_MTFTP4_TOKEN *Token;
33 NET_BUF *UdpPacket;
34 EFI_STATUS Status;
35 UINT16 DataLen;
36 UINT8 *DataBuf;
37 UINT64 Start;
38
39 //
40 // Allocate a buffer to hold the user data
41 //
42 UdpPacket = NetbufAlloc (Instance->BlkSize + MTFTP4_DATA_HEAD_LEN);
43
44 if (UdpPacket == NULL) {
45 return EFI_OUT_OF_RESOURCES;
46 }
47
48 Packet = (EFI_MTFTP4_PACKET *) NetbufAllocSpace (UdpPacket, MTFTP4_DATA_HEAD_LEN, FALSE);
49 ASSERT (Packet != NULL);
50
51 Packet->Data.OpCode = HTONS (EFI_MTFTP4_OPCODE_DATA);
52 Packet->Data.Block = HTONS (BlockNum);
53
54 //
55 // Read the block from either the buffer or PacketNeeded callback
56 //
57 Token = Instance->Token;
58 DataLen = Instance->BlkSize;
59
60 if (Token->Buffer != NULL) {
61 Start = MultU64x32 (BlockNum - 1, Instance->BlkSize);
62
63 if (Token->BufferSize < Start + Instance->BlkSize) {
64 DataLen = (UINT16) (Token->BufferSize - Start);
65 Instance->LastBlock = BlockNum;
66 Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum);
67 }
68
69 if (DataLen > 0) {
70 NetbufAllocSpace (UdpPacket, DataLen, FALSE);
71 CopyMem (Packet->Data.Data, (UINT8 *) Token->Buffer + Start, DataLen);
72 }
73
74 } else {
75 //
76 // Get data from PacketNeeded
77 //
78 DataBuf = NULL;
79 Status = Token->PacketNeeded (
80 &Instance->Mtftp4,
81 Token,
82 &DataLen,
83 (VOID **) &DataBuf
84 );
85
86 if (EFI_ERROR (Status) || (DataLen > Instance->BlkSize)) {
87 if (DataBuf != NULL) {
88 FreePool (DataBuf);
89 }
90
91 if (UdpPacket != NULL) {
92 NetbufFree (UdpPacket);
93 }
94
95 Mtftp4SendError (
96 Instance,
97 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
98 (UINT8 *) "User aborted the transfer"
99 );
100
101 return EFI_ABORTED;
102 }
103
104 if (DataLen < Instance->BlkSize) {
105 Instance->LastBlock = BlockNum;
106 Mtftp4SetLastBlockNum (&Instance->Blocks, BlockNum);
107 }
108
109 if (DataLen > 0) {
110 NetbufAllocSpace (UdpPacket, DataLen, FALSE);
111 CopyMem (Packet->Data.Data, DataBuf, DataLen);
112 FreePool (DataBuf);
113 }
114 }
115
116 return Mtftp4SendPacket (Instance, UdpPacket);
117}
118
119
120/**
121 Function to handle received ACK packet.
122
123 If the ACK number matches the expected block number, and there are more
124 data pending, send the next block. Otherwise tell the caller that we are done.
125
126 @param Instance The MTFTP upload session
127 @param Packet The MTFTP packet received
128 @param Len The packet length
129 @param Completed Return whether the upload has finished.
130
131 @retval EFI_SUCCESS The ACK is successfully processed.
132 @retval EFI_TFTP_ERROR The block number loops back.
133 @retval Others Failed to transmit the next data packet.
134
135**/
136EFI_STATUS
137Mtftp4WrqHandleAck (
138 IN MTFTP4_PROTOCOL *Instance,
139 IN EFI_MTFTP4_PACKET *Packet,
140 IN UINT32 Len,
141 OUT BOOLEAN *Completed
142 )
143{
144 UINT16 AckNum;
145 INTN Expected;
146 UINT64 BlockCounter;
147
148 *Completed = FALSE;
149 AckNum = NTOHS (Packet->Ack.Block[0]);
150 Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
151
152 ASSERT (Expected >= 0);
153
154 //
155 // Get an unwanted ACK, return EFI_SUCCESS to let Mtftp4WrqInput
156 // restart receive.
157 //
158 if (Expected != AckNum) {
159 return EFI_SUCCESS;
160 }
161
162 //
163 // Remove the acked block number, if this is the last block number,
164 // tell the Mtftp4WrqInput to finish the transfer. This is the last
165 // block number if the block range are empty.
166 //
167 Mtftp4RemoveBlockNum (&Instance->Blocks, AckNum, *Completed, &BlockCounter);
168
169 Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
170
171 if (Expected < 0) {
172
173 //
174 // The block range is empty. It may either because the the last
175 // block has been ACKed, or the sequence number just looped back,
176 // that is, there is more than 0xffff blocks.
177 //
178 if (Instance->LastBlock == AckNum) {
179 ASSERT (Instance->LastBlock >= 1);
180 *Completed = TRUE;
181 return EFI_SUCCESS;
182
183 } else {
184 Mtftp4SendError (
185 Instance,
186 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
187 (UINT8 *) "Block number rolls back, not supported, try blksize option"
188 );
189
190 return EFI_TFTP_ERROR;
191 }
192 }
193
194 return Mtftp4WrqSendBlock (Instance, (UINT16) Expected);
195}
196
197
198/**
199 Check whether the received OACK is valid.
200
201 The OACK is valid only if:
202 1. It only include options requested by us
203 2. It can only include a smaller block size
204 3. It can't change the proposed time out value.
205 4. Other requirements of the individal MTFTP options as required.
206
207 @param Reply The options included in the OACK
208 @param Request The options we requested
209
210 @retval TRUE The options included in OACK is valid.
211 @retval FALSE The options included in OACK is invalid.
212
213**/
214BOOLEAN
215Mtftp4WrqOackValid (
216 IN MTFTP4_OPTION *Reply,
217 IN MTFTP4_OPTION *Request
218 )
219{
220 //
221 // It is invalid for server to return options we don't request
222 //
223 if ((Reply->Exist & ~Request->Exist) != 0) {
224 return FALSE;
225 }
226
227 //
228 // Server can only specify a smaller block size to be used and
229 // return the timeout matches that requested.
230 //
231 if ((((Reply->Exist & MTFTP4_BLKSIZE_EXIST) != 0) && (Reply->BlkSize > Request->BlkSize)) ||
232 (((Reply->Exist & MTFTP4_TIMEOUT_EXIST) != 0) && (Reply->Timeout != Request->Timeout))) {
233 return FALSE;
234 }
235
236 return TRUE;
237}
238
239
240/**
241 Function to handle the MTFTP OACK packet.
242
243 It parses the packet's options, and update the internal states of the session.
244
245 @param Instance The MTFTP session
246 @param Packet The received OACK packet
247 @param Len The length of the packet
248 @param Completed Whether the transmisson has completed. NOT used by
249 this function.
250
251 @retval EFI_SUCCESS The OACK process is OK
252 @retval EFI_TFTP_ERROR Some error occured, and the session reset.
253
254**/
255EFI_STATUS
256Mtftp4WrqHandleOack (
257 IN OUT MTFTP4_PROTOCOL *Instance,
258 IN EFI_MTFTP4_PACKET *Packet,
259 IN UINT32 Len,
260 OUT BOOLEAN *Completed
261 )
262{
263 MTFTP4_OPTION Reply;
264 EFI_MTFTP4_PACKET Bogus;
265 EFI_STATUS Status;
266 INTN Expected;
267
268 *Completed = FALSE;
269
270 //
271 // Ignore the OACK if already started the upload
272 //
273 Expected = Mtftp4GetNextBlockNum (&Instance->Blocks);
274
275 if (Expected != 0) {
276 return EFI_SUCCESS;
277 }
278
279 //
280 // Parse and validate the options from server
281 //
282 ZeroMem (&Reply, sizeof (MTFTP4_OPTION));
283 Status = Mtftp4ParseOptionOack (Packet, Len, Instance->Operation, &Reply);
284
285 if (EFI_ERROR (Status) || !Mtftp4WrqOackValid (&Reply, &Instance->RequestOption)) {
286 //
287 // Don't send a MTFTP error packet when out of resource, it can
288 // only make it worse.
289 //
290 if (Status != EFI_OUT_OF_RESOURCES) {
291 Mtftp4SendError (
292 Instance,
293 EFI_MTFTP4_ERRORCODE_ILLEGAL_OPERATION,
294 (UINT8 *) "Mal-formated OACK packet"
295 );
296 }
297
298 return EFI_TFTP_ERROR;
299 }
300
301 if (Reply.BlkSize != 0) {
302 Instance->BlkSize = Reply.BlkSize;
303 }
304
305 if (Reply.Timeout != 0) {
306 Instance->Timeout = Reply.Timeout;
307 }
308
309 //
310 // Build a bogus ACK0 packet then pass it to the Mtftp4WrqHandleAck,
311 // which will start the transmission of the first data block.
312 //
313 Bogus.Ack.OpCode = HTONS (EFI_MTFTP4_OPCODE_ACK);
314 Bogus.Ack.Block[0] = 0;
315
316 Status = Mtftp4WrqHandleAck (
317 Instance,
318 &Bogus,
319 sizeof (EFI_MTFTP4_ACK_HEADER),
320 Completed
321 );
322
323 return Status;
324}
325
326
327/**
328 The input process routine for MTFTP upload.
329
330 @param UdpPacket The received MTFTP packet.
331 @param EndPoint The local/remote access point
332 @param IoStatus The result of the packet receiving
333 @param Context Opaque parameter for the callback, which is the
334 MTFTP session.
335**/
336VOID
337EFIAPI
338Mtftp4WrqInput (
339 IN NET_BUF *UdpPacket,
340 IN UDP_END_POINT *EndPoint,
341 IN EFI_STATUS IoStatus,
342 IN VOID *Context
343 )
344{
345 MTFTP4_PROTOCOL *Instance;
346 EFI_MTFTP4_PACKET *Packet;
347 BOOLEAN Completed;
348 EFI_STATUS Status;
349 UINT32 Len;
350 UINT16 Opcode;
351
352 Instance = (MTFTP4_PROTOCOL *) Context;
353 NET_CHECK_SIGNATURE (Instance, MTFTP4_PROTOCOL_SIGNATURE);
354
355 Completed = FALSE;
356 Packet = NULL;
357 Status = EFI_SUCCESS;
358
359 if (EFI_ERROR (IoStatus)) {
360 Status = IoStatus;
361 goto ON_EXIT;
362 }
363
364 ASSERT (UdpPacket != NULL);
365
366 if (UdpPacket->TotalSize < MTFTP4_OPCODE_LEN) {
367 goto ON_EXIT;
368 }
369
370 //
371 // Client send initial request to server's listening port. Server
372 // will select a UDP port to communicate with the client.
373 //
374 if (EndPoint->RemotePort != Instance->ConnectedPort) {
375 if (Instance->ConnectedPort != 0) {
376 goto ON_EXIT;
377 } else {
378 Instance->ConnectedPort = EndPoint->RemotePort;
379 }
380 }
381
382 //
383 // Copy the MTFTP packet to a continuous buffer if it isn't already so.
384 //
385 Len = UdpPacket->TotalSize;
386
387 if (UdpPacket->BlockOpNum > 1) {
388 Packet = AllocatePool (Len);
389
390 if (Packet == NULL) {
391 Status = EFI_OUT_OF_RESOURCES;
392 goto ON_EXIT;
393 }
394
395 NetbufCopy (UdpPacket, 0, Len, (UINT8 *) Packet);
396
397 } else {
398 Packet = (EFI_MTFTP4_PACKET *) NetbufGetByte (UdpPacket, 0, NULL);
399 ASSERT (Packet != NULL);
400 }
401
402 Opcode = NTOHS (Packet->OpCode);
403
404 //
405 // Call the user's CheckPacket if provided. Abort the transmission
406 // if CheckPacket returns an EFI_ERROR code.
407 //
408 if ((Instance->Token->CheckPacket != NULL) &&
409 ((Opcode == EFI_MTFTP4_OPCODE_OACK) || (Opcode == EFI_MTFTP4_OPCODE_ERROR))) {
410
411 Status = Instance->Token->CheckPacket (
412 &Instance->Mtftp4,
413 Instance->Token,
414 (UINT16) Len,
415 Packet
416 );
417
418 if (EFI_ERROR (Status)) {
419 //
420 // Send an error message to the server to inform it
421 //
422 if (Opcode != EFI_MTFTP4_OPCODE_ERROR) {
423 Mtftp4SendError (
424 Instance,
425 EFI_MTFTP4_ERRORCODE_REQUEST_DENIED,
426 (UINT8 *) "User aborted the transfer"
427 );
428 }
429
430 Status = EFI_ABORTED;
431 goto ON_EXIT;
432 }
433 }
434
435 switch (Opcode) {
436 case EFI_MTFTP4_OPCODE_ACK:
437 if (Len != MTFTP4_OPCODE_LEN + MTFTP4_BLKNO_LEN) {
438 goto ON_EXIT;
439 }
440
441 Status = Mtftp4WrqHandleAck (Instance, Packet, Len, &Completed);
442 break;
443
444 case EFI_MTFTP4_OPCODE_OACK:
445 if (Len <= MTFTP4_OPCODE_LEN) {
446 goto ON_EXIT;
447 }
448
449 Status = Mtftp4WrqHandleOack (Instance, Packet, Len, &Completed);
450 break;
451
452 case EFI_MTFTP4_OPCODE_ERROR:
453 Status = EFI_TFTP_ERROR;
454 break;
455
456 default:
457 break;
458 }
459
460ON_EXIT:
461 //
462 // Free the resources, then if !EFI_ERROR (Status) and not completed,
463 // restart the receive, otherwise end the session.
464 //
465 if ((Packet != NULL) && (UdpPacket->BlockOpNum > 1)) {
466 FreePool (Packet);
467 }
468
469 if (UdpPacket != NULL) {
470 NetbufFree (UdpPacket);
471 }
472
473 if (!EFI_ERROR (Status) && !Completed) {
474 Status = UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0);
475 }
476
477 //
478 // Status may have been updated by UdpIoRecvDatagram
479 //
480 if (EFI_ERROR (Status) || Completed) {
481 Mtftp4CleanOperation (Instance, Status);
482 }
483}
484
485
486
487/**
488 Start the MTFTP session for upload.
489
490 It will first init some states, then send the WRQ request packet,
491 and start receiving the packet.
492
493 @param Instance The MTFTP session
494 @param Operation Redundant parameter, which is always
495 EFI_MTFTP4_OPCODE_WRQ here.
496
497 @retval EFI_SUCCESS The upload process has been started.
498 @retval Others Failed to start the upload.
499
500**/
501EFI_STATUS
502Mtftp4WrqStart (
503 IN MTFTP4_PROTOCOL *Instance,
504 IN UINT16 Operation
505 )
506{
507 EFI_STATUS Status;
508
509 //
510 // The valid block number range are [0, 0xffff]. For example:
511 // the client sends an WRQ request to the server, the server
512 // ACK with an ACK0 to let client start transfer the first
513 // packet.
514 //
515 Status = Mtftp4InitBlockRange (&Instance->Blocks, 0, 0xffff);
516
517 if (EFI_ERROR (Status)) {
518 return Status;
519 }
520
521 Status = Mtftp4SendRequest (Instance);
522
523 if (EFI_ERROR (Status)) {
524 return Status;
525 }
526
527 return UdpIoRecvDatagram (Instance->UnicastPort, Mtftp4WrqInput, Instance, 0);
528}
529
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