2 Internal macro definitions, type definitions, and function declarations for
3 the Virtio Filesystem device driver.
5 Copyright (C) 2020, Red Hat, Inc.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #ifndef VIRTIO_FS_DXE_H_
11 #define VIRTIO_FS_DXE_H_
13 #include <Base.h> // SIGNATURE_64()
14 #include <IndustryStandard/VirtioFs.h> // VIRTIO_FS_TAG_BYTES
15 #include <Library/DebugLib.h> // CR()
16 #include <Protocol/SimpleFileSystem.h> // EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
17 #include <Protocol/VirtioDevice.h> // VIRTIO_DEVICE_PROTOCOL
18 #include <Uefi/UefiBaseType.h> // EFI_EVENT
20 #define VIRTIO_FS_SIG SIGNATURE_64 ('V', 'I', 'R', 'T', 'I', 'O', 'F', 'S')
22 #define VIRTIO_FS_FILE_SIG \
23 SIGNATURE_64 ('V', 'I', 'O', 'F', 'S', 'F', 'I', 'L')
26 // The following limit applies to two kinds of pathnames.
28 // - The length of a POSIX-style, canonical pathname *at rest* never exceeds
29 // VIRTIO_FS_MAX_PATHNAME_LENGTH. (Length is defined as the number of CHAR8
30 // elements in the canonical pathname, excluding the terminating '\0'.) This
31 // is an invariant that is ensured for canonical pathnames created, and that
32 // is assumed about canonical pathname inputs (which all originate
35 // - If the length of a UEFI-style pathname *argument*, originating directly or
36 // indirectly from the EFI_FILE_PROTOCOL caller, exceeds
37 // VIRTIO_FS_MAX_PATHNAME_LENGTH, then the argument is rejected. (Length is
38 // defined as the number of CHAR16 elements in the UEFI-style pathname,
39 // excluding the terminating L'\0'.) This is a restriction that's checked on
40 // external UEFI-style pathname inputs.
42 // The limit is not expected to be a practical limitation; it's only supposed
43 // to prevent attempts at overflowing size calculations. For both kinds of
44 // pathnames, separate limits could be used; a common limit is used purely for
47 #define VIRTIO_FS_MAX_PATHNAME_LENGTH ((UINTN)65535)
50 // Filesystem label encoded in UCS-2, transformed from the UTF-8 representation
51 // in "VIRTIO_FS_CONFIG.Tag", and NUL-terminated. Only the printable ASCII code
52 // points (U+0020 through U+007E) are supported.
54 typedef CHAR16 VIRTIO_FS_LABEL
[VIRTIO_FS_TAG_BYTES
+ 1];
57 // Main context structure, expressing an EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
58 // interface on top of the Virtio Filesystem device.
62 // Parts of this structure are initialized / torn down in various functions
63 // at various call depths. The table to the right should make it easier to
66 // field init function init depth
67 // ----------- ------------------ ----------
68 UINT64 Signature
; // DriverBindingStart 0
69 VIRTIO_DEVICE_PROTOCOL
*Virtio
; // DriverBindingStart 0
70 VIRTIO_FS_LABEL Label
; // VirtioFsInit 1
71 UINT16 QueueSize
; // VirtioFsInit 1
72 VRING Ring
; // VirtioRingInit 2
73 VOID
*RingMap
; // VirtioRingMap 2
74 UINT64 RequestId
; // FuseInitSession 1
75 EFI_EVENT ExitBoot
; // DriverBindingStart 0
76 LIST_ENTRY OpenFiles
; // DriverBindingStart 0
77 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL SimpleFs
; // DriverBindingStart 0
80 #define VIRTIO_FS_FROM_SIMPLE_FS(SimpleFsReference) \
81 CR (SimpleFsReference, VIRTIO_FS, SimpleFs, VIRTIO_FS_SIG);
84 // Structure for describing a contiguous buffer, potentially mapped for Virtio
89 // The following fields originate from the owner of the buffer.
94 // All of the fields below, until the end of the structure, are
95 // zero-initialized when the structure is initially validated.
97 // Mapped, MappedAddress and Mapping are updated when the buffer is mapped
98 // for VirtioOperationBusMasterRead or VirtioOperationBusMasterWrite. They
99 // are again updated when the buffer is unmapped.
102 EFI_PHYSICAL_ADDRESS MappedAddress
;
105 // Transferred is updated after VirtioFlush() returns successfully:
106 // - for VirtioOperationBusMasterRead, Transferred is set to Size;
107 // - for VirtioOperationBusMasterWrite, Transferred is calculated from the
108 // UsedLen output parameter of VirtioFlush().
111 } VIRTIO_FS_IO_VECTOR
;
114 // Structure for describing a list of IO Vectors.
118 // The following fields originate from the owner of the buffers.
120 VIRTIO_FS_IO_VECTOR
*IoVec
;
123 // TotalSize is calculated when the scatter-gather list is initially
127 } VIRTIO_FS_SCATTER_GATHER_LIST
;
130 // Private context structure that exposes EFI_FILE_PROTOCOL on top of an open
131 // FUSE file reference.
135 EFI_FILE_PROTOCOL SimpleFile
;
137 BOOLEAN IsOpenForWriting
;
139 LIST_ENTRY OpenFilesEntry
;
140 CHAR8
*CanonicalPathname
;
142 // In the FUSE wire protocol, every request except FUSE_INIT refers to a
143 // file, namely by the "VIRTIO_FS_FUSE_REQUEST.NodeId" field; that is, by the
144 // inode number of the file. However, some of the FUSE requests that we need
145 // for some of the EFI_FILE_PROTOCOL member functions require an open file
146 // handle *in addition* to the inode number. For simplicity, whenever a
147 // VIRTIO_FS_FILE object is created, primarily defined by its NodeId field,
148 // we also *open* the referenced file at once, and save the returned file
149 // handle in the FuseHandle field. This way, when an EFI_FILE_PROTOCOL member
150 // function must send a FUSE request that needs the file handle *in addition*
151 // to the inode number, FuseHandle will be at our disposal at once.
157 #define VIRTIO_FS_FILE_FROM_SIMPLE_FILE(SimpleFileReference) \
158 CR (SimpleFileReference, VIRTIO_FS_FILE, SimpleFile, VIRTIO_FS_FILE_SIG);
160 #define VIRTIO_FS_FILE_FROM_OPEN_FILES_ENTRY(OpenFilesEntryReference) \
161 CR (OpenFilesEntryReference, VIRTIO_FS_FILE, OpenFilesEntry, \
165 // Initialization and helper routines for the Virtio Filesystem device.
170 IN OUT VIRTIO_FS
*VirtioFs
175 IN OUT VIRTIO_FS
*VirtioFs
181 IN EFI_EVENT ExitBootEvent
,
182 IN VOID
*VirtioFsAsVoid
186 VirtioFsSgListsValidate (
187 IN VIRTIO_FS
*VirtioFs
,
188 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST
*RequestSgList
,
189 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST
*ResponseSgList OPTIONAL
193 VirtioFsSgListsSubmit (
194 IN OUT VIRTIO_FS
*VirtioFs
,
195 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST
*RequestSgList
,
196 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST
*ResponseSgList OPTIONAL
200 VirtioFsFuseNewRequest (
201 IN OUT VIRTIO_FS
*VirtioFs
,
202 OUT VIRTIO_FS_FUSE_REQUEST
*Request
,
203 IN UINT32 RequestSize
,
204 IN VIRTIO_FS_FUSE_OPCODE Opcode
,
209 VirtioFsFuseCheckResponse (
210 IN VIRTIO_FS_SCATTER_GATHER_LIST
*ResponseSgList
,
212 OUT UINTN
*TailBufferFill
216 VirtioFsErrnoToEfiStatus (
223 IN CHAR16
*RhsPath16
,
224 OUT CHAR8
**ResultPath8
,
225 OUT BOOLEAN
*RootEscape
229 // Wrapper functions for FUSE commands (primitives).
234 IN OUT VIRTIO_FS
*VirtioFs
,
240 IN OUT VIRTIO_FS
*VirtioFs
,
241 IN UINT64 ParentNodeId
,
248 IN OUT VIRTIO_FS
*VirtioFs
,
250 IN BOOLEAN ReadWrite
,
251 OUT UINT64
*FuseHandle
255 VirtioFsFuseReleaseFileOrDir (
256 IN OUT VIRTIO_FS
*VirtioFs
,
258 IN UINT64 FuseHandle
,
263 VirtioFsFuseFsyncFileOrDir (
264 IN OUT VIRTIO_FS
*VirtioFs
,
266 IN UINT64 FuseHandle
,
272 IN OUT VIRTIO_FS
*VirtioFs
,
278 VirtioFsFuseInitSession (
279 IN OUT VIRTIO_FS
*VirtioFs
283 VirtioFsFuseOpenDir (
284 IN OUT VIRTIO_FS
*VirtioFs
,
286 OUT UINT64
*FuseHandle
290 VirtioFsFuseOpenOrCreate (
291 IN OUT VIRTIO_FS
*VirtioFs
,
292 IN UINT64 ParentNodeId
,
295 OUT UINT64
*FuseHandle
299 // EFI_SIMPLE_FILE_SYSTEM_PROTOCOL member functions for the Virtio Filesystem
306 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
*This
,
307 OUT EFI_FILE_PROTOCOL
**Root
311 // EFI_FILE_PROTOCOL member functions for the Virtio Filesystem driver.
316 VirtioFsSimpleFileClose (
317 IN EFI_FILE_PROTOCOL
*This
322 VirtioFsSimpleFileDelete (
323 IN EFI_FILE_PROTOCOL
*This
328 VirtioFsSimpleFileFlush (
329 IN EFI_FILE_PROTOCOL
*This
334 VirtioFsSimpleFileGetInfo (
335 IN EFI_FILE_PROTOCOL
*This
,
336 IN EFI_GUID
*InformationType
,
337 IN OUT UINTN
*BufferSize
,
343 VirtioFsSimpleFileGetPosition (
344 IN EFI_FILE_PROTOCOL
*This
,
350 VirtioFsSimpleFileOpen (
351 IN EFI_FILE_PROTOCOL
*This
,
352 OUT EFI_FILE_PROTOCOL
**NewHandle
,
360 VirtioFsSimpleFileRead (
361 IN EFI_FILE_PROTOCOL
*This
,
362 IN OUT UINTN
*BufferSize
,
368 VirtioFsSimpleFileSetInfo (
369 IN EFI_FILE_PROTOCOL
*This
,
370 IN EFI_GUID
*InformationType
,
377 VirtioFsSimpleFileSetPosition (
378 IN EFI_FILE_PROTOCOL
*This
,
384 VirtioFsSimpleFileWrite (
385 IN EFI_FILE_PROTOCOL
*This
,
386 IN OUT UINTN
*BufferSize
,
390 #endif // VIRTIO_FS_DXE_H_