]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg/VirtioFsDxe: add helper for formatting UEFI basenames
authorLaszlo Ersek <lersek@redhat.com>
Wed, 16 Dec 2020 21:11:06 +0000 (22:11 +0100)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 21 Dec 2020 17:16:23 +0000 (17:16 +0000)
The EFI_FILE_INFO structure, which is output by
EFI_FILE_PROTOCOL.GetInfo(), ends with a flexible CHAR16 array called
"FileName". Add the VirtioFsGetBasename() function, for determining the
required array size, and for filling the array as well.

Cc: Ard Biesheuvel <ard.biesheuvel@arm.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Philippe Mathieu-Daudé <philmd@redhat.com>
Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3097
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Message-Id: <20201216211125.19496-30-lersek@redhat.com>
Acked-by: Ard Biesheuvel <ard.biesheuvel@arm.com>
OvmfPkg/VirtioFsDxe/Helpers.c
OvmfPkg/VirtioFsDxe/VirtioFsDxe.h

index 0e4390b005996f85783052b8a4465fa05c5f1e0f..5c3e990add0417c829c16653e8593f2a60114b55 100644 (file)
@@ -1722,6 +1722,67 @@ ForgetNextDirNodeId:
   return Status;\r
 }\r
 \r
+/**\r
+  Format the last component of a canonical pathname into a caller-provided\r
+  CHAR16 array.\r
+\r
+  @param[in] Path              The canonical pathname (as defined in the\r
+                               description of VirtioFsAppendPath()) to format\r
+                               the last component of.\r
+\r
+  @param[out] Basename         If BasenameSize is zero on input, Basename may\r
+                               be NULL. Otherwise, Basename is allocated by the\r
+                               caller. On successful return, Basename contains\r
+                               the last component of Path, formatted as a\r
+                               NUL-terminated CHAR16 string. When Path is "/"\r
+                               on input, Basename is L"" on output.\r
+\r
+  @param[in,out] BasenameSize  On input, the number of bytes the caller\r
+                               provides in Basename. On output, regardless of\r
+                               return value, the number of bytes required for\r
+                               formatting Basename, including the terminating\r
+                               L'\0'.\r
+\r
+  @retval EFI_SUCCESS           Basename has been filled in.\r
+\r
+  @retval EFI_BUFFER_TOO_SMALL  BasenameSize was too small on input; Basename\r
+                                has not been modified.\r
+**/\r
+EFI_STATUS\r
+VirtioFsGetBasename (\r
+  IN     CHAR8  *Path,\r
+     OUT CHAR16 *Basename     OPTIONAL,\r
+  IN OUT UINTN  *BasenameSize\r
+  )\r
+{\r
+  UINTN AllocSize;\r
+  UINTN LastComponent;\r
+  UINTN Idx;\r
+  UINTN PathSize;\r
+\r
+  AllocSize = *BasenameSize;\r
+\r
+  LastComponent = MAX_UINTN;\r
+  for (Idx = 0; Path[Idx] != '\0'; Idx++) {\r
+    if (Path[Idx] == '/') {\r
+      LastComponent = Idx;\r
+    }\r
+  }\r
+  PathSize = Idx + 1;\r
+  ASSERT (LastComponent < MAX_UINTN);\r
+  LastComponent++;\r
+  *BasenameSize = (PathSize - LastComponent) * sizeof Basename[0];\r
+\r
+  if (*BasenameSize > AllocSize) {\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  for (Idx = LastComponent; Idx < PathSize; Idx++) {\r
+    Basename[Idx - LastComponent] = Path[Idx];\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
   Convert select fields of a VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE object to\r
   corresponding fields in EFI_FILE_INFO.\r
index 029c568b39c730a797405e6726f52e2ecc3353c6..d1b746c0d8cf4927394b3256f895759d6738e031 100644 (file)
@@ -234,6 +234,13 @@ VirtioFsLookupMostSpecificParentDir (
      OUT CHAR8     **LastComponent\r
   );\r
 \r
+EFI_STATUS\r
+VirtioFsGetBasename (\r
+  IN     CHAR8  *Path,\r
+     OUT CHAR16 *Basename     OPTIONAL,\r
+  IN OUT UINTN  *BasenameSize\r
+  );\r
+\r
 EFI_STATUS\r
 VirtioFsFuseAttrToEfiFileInfo (\r
   IN     VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE *FuseAttr,\r