]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Drivers/AndroidFastbootTransportTcpDxe/FastbootTransportTcp.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / EmbeddedPkg / Drivers / AndroidFastbootTransportTcpDxe / FastbootTransportTcp.c
1 /** @file
2 #
3 # Copyright (c) 2014, ARM Ltd. All rights reserved.<BR>
4 #
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7 #
8 #**/
9
10 #include <Protocol/AndroidFastbootTransport.h>
11 #include <Protocol/Dhcp4.h>
12 #include <Protocol/Tcp4.h>
13 #include <Protocol/ServiceBinding.h>
14 #include <Protocol/SimpleTextOut.h>
15
16 #include <Library/BaseLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/DebugLib.h>
19 #include <Library/MemoryAllocationLib.h>
20 #include <Library/PrintLib.h>
21 #include <Library/UefiBootServicesTableLib.h>
22 #include <Library/UefiDriverEntryPoint.h>
23 #include <Library/UefiRuntimeServicesTableLib.h>
24
25 #define IP4_ADDR_TO_STRING(IpAddr, IpAddrString) UnicodeSPrint ( \
26 IpAddrString, \
27 16 * 2, \
28 L"%d.%d.%d.%d", \
29 IpAddr.Addr[0], \
30 IpAddr.Addr[1], \
31 IpAddr.Addr[2], \
32 IpAddr.Addr[3] \
33 );
34
35 // Fastboot says max packet size is 512, but FASTBOOT_TRANSPORT_PROTOCOL
36 // doesn't place a limit on the size of buffers returned by Receive.
37 // (This isn't actually a packet size - it's just the size of the buffers we
38 // pass to the TCP driver to fill with received data.)
39 // We can achieve much better performance by doing this in larger chunks.
40 #define RX_FRAGMENT_SIZE 2048
41
42 STATIC EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *mTextOut;
43
44 STATIC EFI_TCP4_PROTOCOL *mTcpConnection;
45 STATIC EFI_TCP4_PROTOCOL *mTcpListener;
46
47 STATIC EFI_EVENT mReceiveEvent;
48
49 STATIC EFI_SERVICE_BINDING_PROTOCOL *mTcpServiceBinding;
50 STATIC EFI_HANDLE mTcpHandle = NULL;
51
52 // We only ever use one IO token for receive and one for transmit. To save
53 // repeatedly allocating and freeing, just allocate statically and re-use.
54 #define NUM_RX_TOKENS 16
55 #define TOKEN_NEXT(Index) (((Index) + 1) % NUM_RX_TOKENS)
56
57 STATIC UINTN mNextSubmitIndex;
58 STATIC UINTN mNextReceiveIndex;
59 STATIC EFI_TCP4_IO_TOKEN mReceiveToken[NUM_RX_TOKENS];
60 STATIC EFI_TCP4_RECEIVE_DATA mRxData[NUM_RX_TOKENS];
61 STATIC EFI_TCP4_IO_TOKEN mTransmitToken;
62 STATIC EFI_TCP4_TRANSMIT_DATA mTxData;
63 // We also reuse the accept token
64 STATIC EFI_TCP4_LISTEN_TOKEN mAcceptToken;
65 // .. and the close token
66 STATIC EFI_TCP4_CLOSE_TOKEN mCloseToken;
67
68 // List type for queued received packets
69 typedef struct _FASTBOOT_TCP_PACKET_LIST {
70 LIST_ENTRY Link;
71 VOID *Buffer;
72 UINTN BufferSize;
73 } FASTBOOT_TCP_PACKET_LIST;
74
75 STATIC LIST_ENTRY mPacketListHead;
76
77 STATIC
78 VOID
79 EFIAPI
80 DataReceived (
81 IN EFI_EVENT Event,
82 IN VOID *Context
83 );
84
85 /*
86 Helper function to set up a receive IO token and call Tcp->Receive
87 */
88 STATIC
89 EFI_STATUS
90 SubmitRecieveToken (
91 VOID
92 )
93 {
94 EFI_STATUS Status;
95 VOID *FragmentBuffer;
96
97 Status = EFI_SUCCESS;
98
99 FragmentBuffer = AllocatePool (RX_FRAGMENT_SIZE);
100 ASSERT (FragmentBuffer != NULL);
101 if (FragmentBuffer == NULL) {
102 DEBUG ((DEBUG_ERROR, "TCP Fastboot out of resources"));
103 return EFI_OUT_OF_RESOURCES;
104 }
105
106 mRxData[mNextSubmitIndex].DataLength = RX_FRAGMENT_SIZE;
107 mRxData[mNextSubmitIndex].FragmentTable[0].FragmentLength = RX_FRAGMENT_SIZE;
108 mRxData[mNextSubmitIndex].FragmentTable[0].FragmentBuffer = FragmentBuffer;
109
110 Status = mTcpConnection->Receive (mTcpConnection, &mReceiveToken[mNextSubmitIndex]);
111 if (EFI_ERROR (Status)) {
112 DEBUG ((DEBUG_ERROR, "TCP Receive: %r\n", Status));
113 FreePool (FragmentBuffer);
114 }
115
116 mNextSubmitIndex = TOKEN_NEXT (mNextSubmitIndex);
117 return Status;
118 }
119
120 /*
121 Event notify function for when we have closed our TCP connection.
122 We can now start listening for another connection.
123 */
124 STATIC
125 VOID
126 ConnectionClosed (
127 IN EFI_EVENT Event,
128 IN VOID *Context
129 )
130 {
131 EFI_STATUS Status;
132
133 // Possible bug in EDK2 TCP4 driver: closing a connection doesn't remove its
134 // PCB from the list of live connections. Subsequent attempts to Configure()
135 // a TCP instance with the same local port will fail with INVALID_PARAMETER.
136 // Calling Configure with NULL is a workaround for this issue.
137 Status = mTcpConnection->Configure (mTcpConnection, NULL);
138
139 mTcpConnection = NULL;
140
141 Status = mTcpListener->Accept (mTcpListener, &mAcceptToken);
142 if (EFI_ERROR (Status)) {
143 DEBUG ((DEBUG_ERROR, "TCP Accept: %r\n", Status));
144 }
145 }
146
147 STATIC
148 VOID
149 CloseReceiveEvents (
150 VOID
151 )
152 {
153 UINTN Index;
154
155 for (Index = 0; Index < NUM_RX_TOKENS; Index++) {
156 gBS->CloseEvent (mReceiveToken[Index].CompletionToken.Event);
157 }
158 }
159
160 /*
161 Event notify function to be called when we receive TCP data.
162 */
163 STATIC
164 VOID
165 EFIAPI
166 DataReceived (
167 IN EFI_EVENT Event,
168 IN VOID *Context
169 )
170 {
171 EFI_STATUS Status;
172 FASTBOOT_TCP_PACKET_LIST *NewEntry;
173 EFI_TCP4_IO_TOKEN *ReceiveToken;
174
175 ReceiveToken = &mReceiveToken[mNextReceiveIndex];
176
177 Status = ReceiveToken->CompletionToken.Status;
178
179 if (Status == EFI_CONNECTION_FIN) {
180 //
181 // Remote host closed connection. Close our end.
182 //
183
184 CloseReceiveEvents ();
185
186 Status = mTcpConnection->Close (mTcpConnection, &mCloseToken);
187 ASSERT_EFI_ERROR (Status);
188
189 return;
190 }
191
192 //
193 // Add an element to the receive queue
194 //
195
196 NewEntry = AllocatePool (sizeof (FASTBOOT_TCP_PACKET_LIST));
197 if (NewEntry == NULL) {
198 DEBUG ((DEBUG_ERROR, "TCP Fastboot: Out of resources\n"));
199 return;
200 }
201
202 mNextReceiveIndex = TOKEN_NEXT (mNextReceiveIndex);
203
204 if (!EFI_ERROR (Status)) {
205 NewEntry->Buffer
206 = ReceiveToken->Packet.RxData->FragmentTable[0].FragmentBuffer;
207 NewEntry->BufferSize
208 = ReceiveToken->Packet.RxData->FragmentTable[0].FragmentLength;
209
210 // Prepare to receive more data
211 SubmitRecieveToken ();
212 } else {
213 // Fatal receive error. Put an entry with NULL in the queue, signifying
214 // to return EFI_DEVICE_ERROR from TcpFastbootTransportReceive.
215 NewEntry->Buffer = NULL;
216 NewEntry->BufferSize = 0;
217
218 DEBUG ((DEBUG_ERROR, "\nTCP Fastboot Receive error: %r\n", Status));
219 }
220
221 InsertTailList (&mPacketListHead, &NewEntry->Link);
222
223 Status = gBS->SignalEvent (mReceiveEvent);
224 ASSERT_EFI_ERROR (Status);
225 }
226
227 /*
228 Event notify function to be called when we accept an incoming TCP connection.
229 */
230 STATIC
231 VOID
232 EFIAPI
233 ConnectionAccepted (
234 IN EFI_EVENT Event,
235 IN VOID *Context
236 )
237 {
238 EFI_TCP4_LISTEN_TOKEN *AcceptToken;
239 EFI_STATUS Status;
240 UINTN Index;
241
242 AcceptToken = (EFI_TCP4_LISTEN_TOKEN *)Context;
243 Status = AcceptToken->CompletionToken.Status;
244
245 if (EFI_ERROR (Status)) {
246 DEBUG ((DEBUG_ERROR, "TCP Fastboot: Connection Error: %r\n", Status));
247 return;
248 }
249
250 DEBUG ((DEBUG_ERROR, "TCP Fastboot: Connection Received.\n"));
251
252 //
253 // Accepting a new TCP connection creates a new instance of the TCP protocol.
254 // Open it and prepare to receive on it.
255 //
256
257 Status = gBS->OpenProtocol (
258 AcceptToken->NewChildHandle,
259 &gEfiTcp4ProtocolGuid,
260 (VOID **)&mTcpConnection,
261 gImageHandle,
262 NULL,
263 EFI_OPEN_PROTOCOL_GET_PROTOCOL
264 );
265 if (EFI_ERROR (Status)) {
266 DEBUG ((DEBUG_ERROR, "Open TCP Connection: %r\n", Status));
267 return;
268 }
269
270 mNextSubmitIndex = 0;
271 mNextReceiveIndex = 0;
272
273 for (Index = 0; Index < NUM_RX_TOKENS; Index++) {
274 Status = gBS->CreateEvent (
275 EVT_NOTIFY_SIGNAL,
276 TPL_CALLBACK,
277 DataReceived,
278 NULL,
279 &(mReceiveToken[Index].CompletionToken.Event)
280 );
281 ASSERT_EFI_ERROR (Status);
282 }
283
284 for (Index = 0; Index < NUM_RX_TOKENS; Index++) {
285 SubmitRecieveToken ();
286 }
287 }
288
289 /*
290 Set up TCP Fastboot transport: Configure the network device via DHCP then
291 start waiting for a TCP connection on the Fastboot port.
292 */
293 EFI_STATUS
294 TcpFastbootTransportStart (
295 EFI_EVENT ReceiveEvent
296 )
297 {
298 EFI_STATUS Status;
299 EFI_HANDLE NetDeviceHandle;
300 EFI_HANDLE *HandleBuffer;
301 EFI_IP4_MODE_DATA Ip4ModeData;
302 UINTN NumHandles;
303 CHAR16 IpAddrString[16];
304 UINTN Index;
305
306 EFI_TCP4_CONFIG_DATA TcpConfigData = {
307 0x00, // IPv4 Type of Service
308 255, // IPv4 Time to Live
309 { // AccessPoint:
310 TRUE, // Use default address
311 {
312 { 0, 0, 0, 0 }
313 }, // IP Address (ignored - use default)
314 {
315 { 0, 0, 0, 0 }
316 }, // Subnet mask (ignored - use default)
317 FixedPcdGet32 (PcdAndroidFastbootTcpPort), // Station port
318 {
319 { 0, 0, 0, 0 }
320 }, // Remote address: accept any
321 0, // Remote Port: accept any
322 FALSE // ActiveFlag: be a "server"
323 },
324 NULL // Default advanced TCP options
325 };
326
327 mReceiveEvent = ReceiveEvent;
328 InitializeListHead (&mPacketListHead);
329
330 mTextOut->OutputString (mTextOut, L"Initialising TCP Fastboot transport...\r\n");
331
332 //
333 // Open a passive TCP instance
334 //
335
336 Status = gBS->LocateHandleBuffer (
337 ByProtocol,
338 &gEfiTcp4ServiceBindingProtocolGuid,
339 NULL,
340 &NumHandles,
341 &HandleBuffer
342 );
343 if (EFI_ERROR (Status)) {
344 DEBUG ((DEBUG_ERROR, "Find TCP Service Binding: %r\n", Status));
345 return Status;
346 }
347
348 // We just use the first network device
349 NetDeviceHandle = HandleBuffer[0];
350
351 Status = gBS->OpenProtocol (
352 NetDeviceHandle,
353 &gEfiTcp4ServiceBindingProtocolGuid,
354 (VOID **)&mTcpServiceBinding,
355 gImageHandle,
356 NULL,
357 EFI_OPEN_PROTOCOL_GET_PROTOCOL
358 );
359 if (EFI_ERROR (Status)) {
360 DEBUG ((DEBUG_ERROR, "Open TCP Service Binding: %r\n", Status));
361 return Status;
362 }
363
364 Status = mTcpServiceBinding->CreateChild (mTcpServiceBinding, &mTcpHandle);
365 if (EFI_ERROR (Status)) {
366 DEBUG ((DEBUG_ERROR, "TCP ServiceBinding Create: %r\n", Status));
367 return Status;
368 }
369
370 Status = gBS->OpenProtocol (
371 mTcpHandle,
372 &gEfiTcp4ProtocolGuid,
373 (VOID **)&mTcpListener,
374 gImageHandle,
375 NULL,
376 EFI_OPEN_PROTOCOL_GET_PROTOCOL
377 );
378 if (EFI_ERROR (Status)) {
379 DEBUG ((DEBUG_ERROR, "Open TCP Protocol: %r\n", Status));
380 }
381
382 //
383 // Set up re-usable tokens
384 //
385
386 for (Index = 0; Index < NUM_RX_TOKENS; Index++) {
387 mRxData[Index].UrgentFlag = FALSE;
388 mRxData[Index].FragmentCount = 1;
389 mReceiveToken[Index].Packet.RxData = &mRxData[Index];
390 }
391
392 mTxData.Push = TRUE;
393 mTxData.Urgent = FALSE;
394 mTxData.FragmentCount = 1;
395 mTransmitToken.Packet.TxData = &mTxData;
396
397 Status = gBS->CreateEvent (
398 EVT_NOTIFY_SIGNAL,
399 TPL_CALLBACK,
400 ConnectionAccepted,
401 &mAcceptToken,
402 &mAcceptToken.CompletionToken.Event
403 );
404 ASSERT_EFI_ERROR (Status);
405
406 Status = gBS->CreateEvent (
407 EVT_NOTIFY_SIGNAL,
408 TPL_CALLBACK,
409 ConnectionClosed,
410 &mCloseToken,
411 &mCloseToken.CompletionToken.Event
412 );
413 ASSERT_EFI_ERROR (Status);
414
415 //
416 // Configure the TCP instance
417 //
418
419 Status = mTcpListener->Configure (mTcpListener, &TcpConfigData);
420 if (Status == EFI_NO_MAPPING) {
421 // Wait until the IP configuration process (probably DHCP) has finished
422 do {
423 Status = mTcpListener->GetModeData (
424 mTcpListener,
425 NULL,
426 NULL,
427 &Ip4ModeData,
428 NULL,
429 NULL
430 );
431 ASSERT_EFI_ERROR (Status);
432 } while (!Ip4ModeData.IsConfigured);
433
434 Status = mTcpListener->Configure (mTcpListener, &TcpConfigData);
435 } else if (EFI_ERROR (Status)) {
436 DEBUG ((DEBUG_ERROR, "TCP Configure: %r\n", Status));
437 return Status;
438 }
439
440 //
441 // Tell the user our address and hostname
442 //
443 IP4_ADDR_TO_STRING (Ip4ModeData.ConfigData.StationAddress, IpAddrString);
444
445 mTextOut->OutputString (mTextOut, L"TCP Fastboot transport configured.");
446 mTextOut->OutputString (mTextOut, L"\r\nIP address: ");
447 mTextOut->OutputString (mTextOut, IpAddrString);
448 mTextOut->OutputString (mTextOut, L"\r\n");
449
450 //
451 // Start listening for a connection
452 //
453
454 Status = mTcpListener->Accept (mTcpListener, &mAcceptToken);
455 if (EFI_ERROR (Status)) {
456 DEBUG ((DEBUG_ERROR, "TCP Accept: %r\n", Status));
457 return Status;
458 }
459
460 mTextOut->OutputString (mTextOut, L"TCP Fastboot transport initialised.\r\n");
461
462 FreePool (HandleBuffer);
463
464 return EFI_SUCCESS;
465 }
466
467 EFI_STATUS
468 TcpFastbootTransportStop (
469 VOID
470 )
471 {
472 EFI_TCP4_CLOSE_TOKEN CloseToken;
473 EFI_STATUS Status;
474 UINTN EventIndex;
475 FASTBOOT_TCP_PACKET_LIST *Entry;
476 FASTBOOT_TCP_PACKET_LIST *NextEntry;
477
478 // Close any existing TCP connection, blocking until it's done.
479 if (mTcpConnection != NULL) {
480 CloseReceiveEvents ();
481
482 CloseToken.AbortOnClose = FALSE;
483
484 Status = gBS->CreateEvent (0, 0, NULL, NULL, &CloseToken.CompletionToken.Event);
485 ASSERT_EFI_ERROR (Status);
486
487 Status = mTcpConnection->Close (mTcpConnection, &CloseToken);
488 ASSERT_EFI_ERROR (Status);
489
490 Status = gBS->WaitForEvent (
491 1,
492 &CloseToken.CompletionToken.Event,
493 &EventIndex
494 );
495 ASSERT_EFI_ERROR (Status);
496
497 ASSERT_EFI_ERROR (CloseToken.CompletionToken.Status);
498
499 // Possible bug in EDK2 TCP4 driver: closing a connection doesn't remove its
500 // PCB from the list of live connections. Subsequent attempts to Configure()
501 // a TCP instance with the same local port will fail with INVALID_PARAMETER.
502 // Calling Configure with NULL is a workaround for this issue.
503 Status = mTcpConnection->Configure (mTcpConnection, NULL);
504 ASSERT_EFI_ERROR (Status);
505 }
506
507 gBS->CloseEvent (mAcceptToken.CompletionToken.Event);
508
509 // Stop listening for connections.
510 // Ideally we would do this with Cancel, but it isn't implemented by EDK2.
511 // So we just "reset this TCPv4 instance brutally".
512 Status = mTcpListener->Configure (mTcpListener, NULL);
513 ASSERT_EFI_ERROR (Status);
514
515 Status = mTcpServiceBinding->DestroyChild (mTcpServiceBinding, mTcpHandle);
516
517 // Free any data the user didn't pick up
518 Entry = (FASTBOOT_TCP_PACKET_LIST *)GetFirstNode (&mPacketListHead);
519 while (!IsNull (&mPacketListHead, &Entry->Link)) {
520 NextEntry = (FASTBOOT_TCP_PACKET_LIST *)GetNextNode (&mPacketListHead, &Entry->Link);
521
522 RemoveEntryList (&Entry->Link);
523 if (Entry->Buffer) {
524 FreePool (Entry->Buffer);
525 }
526
527 FreePool (Entry);
528
529 Entry = NextEntry;
530 }
531
532 return EFI_SUCCESS;
533 }
534
535 /*
536 Event notify function for when data has been sent. Free resources and report
537 errors.
538 Context should point to the transmit IO token passed to
539 TcpConnection->Transmit.
540 */
541 STATIC
542 VOID
543 DataSent (
544 EFI_EVENT Event,
545 VOID *Context
546 )
547 {
548 EFI_STATUS Status;
549
550 Status = mTransmitToken.CompletionToken.Status;
551 if (EFI_ERROR (Status)) {
552 DEBUG ((DEBUG_ERROR, "TCP Fastboot transmit result: %r\n", Status));
553 gBS->SignalEvent (*(EFI_EVENT *)Context);
554 }
555
556 FreePool (mTransmitToken.Packet.TxData->FragmentTable[0].FragmentBuffer);
557 }
558
559 EFI_STATUS
560 TcpFastbootTransportSend (
561 IN UINTN BufferSize,
562 IN CONST VOID *Buffer,
563 IN EFI_EVENT *FatalErrorEvent
564 )
565 {
566 EFI_STATUS Status;
567
568 if (BufferSize > 512) {
569 return EFI_INVALID_PARAMETER;
570 }
571
572 //
573 // Build transmit IO token
574 //
575
576 // Create an event so we are notified when a transmission is complete.
577 // We use this to free resources and report errors.
578 Status = gBS->CreateEvent (
579 EVT_NOTIFY_SIGNAL,
580 TPL_CALLBACK,
581 DataSent,
582 FatalErrorEvent,
583 &mTransmitToken.CompletionToken.Event
584 );
585 ASSERT_EFI_ERROR (Status);
586
587 mTxData.DataLength = BufferSize;
588
589 mTxData.FragmentTable[0].FragmentLength = BufferSize;
590 mTxData.FragmentTable[0].FragmentBuffer = AllocateCopyPool (
591 BufferSize,
592 Buffer
593 );
594
595 Status = mTcpConnection->Transmit (mTcpConnection, &mTransmitToken);
596 if (EFI_ERROR (Status)) {
597 DEBUG ((DEBUG_ERROR, "TCP Transmit: %r\n", Status));
598 return Status;
599 }
600
601 return EFI_SUCCESS;
602 }
603
604 EFI_STATUS
605 TcpFastbootTransportReceive (
606 OUT UINTN *BufferSize,
607 OUT VOID **Buffer
608 )
609 {
610 FASTBOOT_TCP_PACKET_LIST *Entry;
611
612 if (IsListEmpty (&mPacketListHead)) {
613 return EFI_NOT_READY;
614 }
615
616 Entry = (FASTBOOT_TCP_PACKET_LIST *)GetFirstNode (&mPacketListHead);
617
618 if (Entry->Buffer == NULL) {
619 // There was an error receiving this packet.
620 return EFI_DEVICE_ERROR;
621 }
622
623 *Buffer = Entry->Buffer;
624 *BufferSize = Entry->BufferSize;
625
626 RemoveEntryList (&Entry->Link);
627 FreePool (Entry);
628
629 return EFI_SUCCESS;
630 }
631
632 FASTBOOT_TRANSPORT_PROTOCOL mTransportProtocol = {
633 TcpFastbootTransportStart,
634 TcpFastbootTransportStop,
635 TcpFastbootTransportSend,
636 TcpFastbootTransportReceive
637 };
638
639 EFI_STATUS
640 TcpFastbootTransportEntryPoint (
641 IN EFI_HANDLE ImageHandle,
642 IN EFI_SYSTEM_TABLE *SystemTable
643 )
644 {
645 EFI_STATUS Status;
646
647 Status = gBS->LocateProtocol (
648 &gEfiSimpleTextOutProtocolGuid,
649 NULL,
650 (VOID **)&mTextOut
651 );
652 if (EFI_ERROR (Status)) {
653 DEBUG ((DEBUG_ERROR, "Fastboot: Open Text Output Protocol: %r\n", Status));
654 return Status;
655 }
656
657 Status = gBS->InstallProtocolInterface (
658 &ImageHandle,
659 &gAndroidFastbootTransportProtocolGuid,
660 EFI_NATIVE_INTERFACE,
661 &mTransportProtocol
662 );
663 if (EFI_ERROR (Status)) {
664 DEBUG ((DEBUG_ERROR, "Fastboot: Install transport Protocol: %r\n", Status));
665 }
666
667 return Status;
668 }