public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ard Biesheuvel" <ardb@kernel.org>
To: Gerd Hoffmann <kraxel@redhat.com>
Cc: devel@edk2.groups.io, Jiewen Yao <jiewen.yao@intel.com>,
	 Jordan Justen <jordan.l.justen@intel.com>,
	Leif Lindholm <quic_llindhol@quicinc.com>,
	 Pawel Polawski <ppolawsk@redhat.com>,
	Sami Mujawar <sami.mujawar@arm.com>,
	 Oliver Steffen <osteffen@redhat.com>,
	Ard Biesheuvel <ardb+tianocore@kernel.org>
Subject: Re: [PATCH 4/5] ArmVirt/PlatformBootManagerLib: set up virtio serial as console
Date: Thu, 1 Jun 2023 10:13:10 +0200	[thread overview]
Message-ID: <CAMj1kXEkvNS7UYLsg1m2_eDKkPH2Od4-VE2=P1pRKuQLbzdO+A@mail.gmail.com> (raw)
In-Reply-To: <20230512142306.1323983-5-kraxel@redhat.com>

On Fri, 12 May 2023 at 16:23, Gerd Hoffmann <kraxel@redhat.com> wrote:
>
> In case a virtio serial device is found in the system register the first
> console port as EFI console, by updating ConIn, ConOut and ErrOut.
>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  .../PlatformBootManagerLib/PlatformBm.c       | 163 ++++++++++++++++++
>  1 file changed, 163 insertions(+)
>
> diff --git a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
> index ed38c42a43ee..7010d73c1388 100644
> --- a/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
> +++ b/ArmVirtPkg/Library/PlatformBootManagerLib/PlatformBm.c
> @@ -312,6 +312,21 @@ IsVirtioRng (
>    return IsVirtio (Handle, ReportText, VIRTIO_SUBSYSTEM_ENTROPY_SOURCE);
>  }
>
> +/**
> +  This FILTER_FUNCTION checks if a handle corresponds to a Virtio serial device at
> +  the VIRTIO_DEVICE_PROTOCOL level.
> +**/
> +STATIC
> +BOOLEAN
> +EFIAPI
> +IsVirtioSerial (
> +  IN EFI_HANDLE    Handle,
> +  IN CONST CHAR16  *ReportText
> +  )
> +{
> +  return IsVirtio (Handle, ReportText, VIRTIO_SUBSYSTEM_CONSOLE);
> +}
> +
>  /**
>    This function checks if a handle corresponds to the Virtio Device ID given
>    at the EFI_PCI_IO_PROTOCOL level.
> @@ -446,6 +461,21 @@ IsVirtioPciRng (
>    return IsVirtioPci (Handle, ReportText, VIRTIO_SUBSYSTEM_ENTROPY_SOURCE);
>  }
>
> +/**
> +  This FILTER_FUNCTION checks if a handle corresponds to a Virtio serial device at
> +  the EFI_PCI_IO_PROTOCOL level.
> +**/
> +STATIC
> +BOOLEAN
> +EFIAPI
> +IsVirtioPciSerial (
> +  IN EFI_HANDLE    Handle,
> +  IN CONST CHAR16  *ReportText
> +  )
> +{
> +  return IsVirtioPci (Handle, ReportText, VIRTIO_SUBSYSTEM_CONSOLE);
> +}
> +
>  /**
>    This CALLBACK_FUNCTION attempts to connect a handle non-recursively, asking
>    the matching driver to produce all first-level child handles.
> @@ -534,6 +564,133 @@ AddOutput (
>      ));
>  }
>
> +/**
> +  This CALLBACK_FUNCTION retrieves the EFI_DEVICE_PATH_PROTOCOL from
> +  the handle, appends serial, uart and terminal nodes, finally updates
> +  ConIn, ConOut and ErrOut.
> +**/
> +STATIC
> +VOID
> +EFIAPI
> +SetupVirtioSerial (
> +  IN EFI_HANDLE    Handle,
> +  IN CONST CHAR16  *ReportText
> +  )
> +{
> +  STATIC ACPI_HID_DEVICE_PATH  SerialNode = {
> +    {
> +      ACPI_DEVICE_PATH,
> +      ACPI_DP,
> +      {
> +        (UINT8)(sizeof (ACPI_HID_DEVICE_PATH)),
> +        (UINT8)((sizeof (ACPI_HID_DEVICE_PATH)) >> 8)
> +      },
> +    },
> +    EISA_PNP_ID (0x0501),
> +    0
> +  };
> +
> +  STATIC UART_DEVICE_PATH  UartNode = {
> +    {
> +      MESSAGING_DEVICE_PATH,
> +      MSG_UART_DP,
> +      {
> +        (UINT8)(sizeof (UART_DEVICE_PATH)),
> +        (UINT8)((sizeof (UART_DEVICE_PATH)) >> 8)
> +      },
> +    },
> +    0,
> +    115200,
> +    8,
> +    1,
> +    1
> +  };
> +
> +  STATIC VENDOR_DEVICE_PATH  TerminalNode = {
> +    {
> +      MESSAGING_DEVICE_PATH,
> +      MSG_VENDOR_DP,
> +      {
> +        (UINT8)(sizeof (VENDOR_DEVICE_PATH)),
> +        (UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
> +      },
> +    },
> +    DEVICE_PATH_MESSAGING_VT_UTF8
> +  };
> +

Please make these STATIC CONST

> +  EFI_STATUS                Status;
> +  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
> +
> +  DevicePath = DevicePathFromHandle (Handle);
> +
> +  if (DevicePath == NULL) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "%a: %s: handle %p: device path not found\n",
> +      __func__,
> +      ReportText,
> +      Handle
> +      ));
> +    return;
> +  }
> +
> +  DevicePath = AppendDevicePathNode (
> +                 DevicePath,
> +                 (EFI_DEVICE_PATH_PROTOCOL *)&SerialNode

You can use &SerialNode.Header and drop the cast (same below)

> +                 );
> +  DevicePath = AppendDevicePathNode (
> +                 DevicePath,
> +                 (EFI_DEVICE_PATH_PROTOCOL *)&UartNode
> +                 );
> +  DevicePath = AppendDevicePathNode (
> +                 DevicePath,
> +                 (EFI_DEVICE_PATH_PROTOCOL *)&TerminalNode
> +                 );
> +

This leaks two copies of DevicePath - better to assign to a temp
variable and FreePool() it between calls.

DevicePath = DevicePathFromHandle()
DevicePath = AppendDevicePathNode(DevicePath)
TempDp = AppendDevicePathNode(DevicePath)
Free(DevicePath)
DevicePath = AppendDevicePathNode(TempDp)
Free(TempDp)

and then free DevicePath at the end too.


> +  Status = EfiBootManagerUpdateConsoleVariable (ConIn, DevicePath, NULL);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "%a: %s: adding to ConIn: %r\n",
> +      __func__,
> +      ReportText,
> +      Status
> +      ));
> +    return;
> +  }
> +
> +  Status = EfiBootManagerUpdateConsoleVariable (ConOut, DevicePath, NULL);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "%a: %s: adding to ConOut: %r\n",
> +      __func__,
> +      ReportText,
> +      Status
> +      ));
> +    return;
> +  }
> +
> +  Status = EfiBootManagerUpdateConsoleVariable (ErrOut, DevicePath, NULL);
> +  if (EFI_ERROR (Status)) {
> +    DEBUG ((
> +      DEBUG_ERROR,
> +      "%a: %s: adding to ErrOut: %r\n",
> +      __func__,
> +      ReportText,
> +      Status
> +      ));
> +    return;
> +  }
> +
> +  DEBUG ((
> +    DEBUG_VERBOSE,
> +    "%a: %s: added to ConIn, ConOut and ErrOut\n",
> +    __func__,
> +    ReportText
> +    ));
> +}
> +
>  STATIC
>  VOID
>  PlatformRegisterFvBootOption (
> @@ -932,6 +1089,12 @@ PlatformBootManagerBeforeConsole (
>    // instances on Virtio PCI RNG devices.
>    //
>    FilterAndProcess (&gEfiPciIoProtocolGuid, IsVirtioPciRng, Connect);
> +
> +  //
> +  // Register Virtio serial devices as console.
> +  //
> +  FilterAndProcess (&gVirtioDeviceProtocolGuid, IsVirtioSerial, SetupVirtioSerial);
> +  FilterAndProcess (&gEfiPciIoProtocolGuid, IsVirtioPciSerial, SetupVirtioSerial);
>  }
>
>  /**
> --
> 2.40.1
>

  reply	other threads:[~2023-06-01  8:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-12 14:23 [PATCH 0/5] ArmVirt: add VirtioSerialDxe Gerd Hoffmann
2023-05-12 14:23 ` [PATCH 1/5] ArmVirt: add VirtioSerialDxe to ArmVirtQemu builds Gerd Hoffmann
2023-06-01  7:58   ` [edk2-devel] " Ard Biesheuvel
2023-05-12 14:23 ` [PATCH 2/5] ArmVirt/PlatformBootManagerLib: factor out IsVirtio() Gerd Hoffmann
2023-06-01  7:57   ` Ard Biesheuvel
2023-05-12 14:23 ` [PATCH 3/5] ArmVirt/PlatformBootManagerLib: factor out IsVirtioPci() Gerd Hoffmann
2023-06-01  8:00   ` Ard Biesheuvel
2023-05-12 14:23 ` [PATCH 4/5] ArmVirt/PlatformBootManagerLib: set up virtio serial as console Gerd Hoffmann
2023-06-01  8:13   ` Ard Biesheuvel [this message]
2023-05-12 14:23 ` [PATCH 5/5] OvmfPkg/VirtioSerialDxe: use TPL_NOTIFY Gerd Hoffmann
2023-06-01  7:56   ` Ard Biesheuvel
2023-06-01  9:07     ` Ard Biesheuvel
2023-06-01  6:17 ` [PATCH 0/5] ArmVirt: add VirtioSerialDxe Gerd Hoffmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAMj1kXEkvNS7UYLsg1m2_eDKkPH2Od4-VE2=P1pRKuQLbzdO+A@mail.gmail.com' \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox