]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioFsDxe/VirtioFsDxe.h
OvmfPkg/VirtioFsDxe: split canon. path into last parent + last component
[mirror_edk2.git] / OvmfPkg / VirtioFsDxe / VirtioFsDxe.h
1 /** @file
2 Internal macro definitions, type definitions, and function declarations for
3 the Virtio Filesystem device driver.
4
5 Copyright (C) 2020, Red Hat, Inc.
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8 **/
9
10 #ifndef VIRTIO_FS_DXE_H_
11 #define VIRTIO_FS_DXE_H_
12
13 #include <Base.h> // SIGNATURE_64()
14 #include <Guid/FileInfo.h> // EFI_FILE_INFO
15 #include <IndustryStandard/VirtioFs.h> // VIRTIO_FS_TAG_BYTES
16 #include <Library/DebugLib.h> // CR()
17 #include <Protocol/SimpleFileSystem.h> // EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
18 #include <Protocol/VirtioDevice.h> // VIRTIO_DEVICE_PROTOCOL
19 #include <Uefi/UefiBaseType.h> // EFI_EVENT
20
21 #define VIRTIO_FS_SIG SIGNATURE_64 ('V', 'I', 'R', 'T', 'I', 'O', 'F', 'S')
22
23 #define VIRTIO_FS_FILE_SIG \
24 SIGNATURE_64 ('V', 'I', 'O', 'F', 'S', 'F', 'I', 'L')
25
26 //
27 // The following limit applies to two kinds of pathnames.
28 //
29 // - The length of a POSIX-style, canonical pathname *at rest* never exceeds
30 // VIRTIO_FS_MAX_PATHNAME_LENGTH. (Length is defined as the number of CHAR8
31 // elements in the canonical pathname, excluding the terminating '\0'.) This
32 // is an invariant that is ensured for canonical pathnames created, and that
33 // is assumed about canonical pathname inputs (which all originate
34 // internally).
35 //
36 // - If the length of a UEFI-style pathname *argument*, originating directly or
37 // indirectly from the EFI_FILE_PROTOCOL caller, exceeds
38 // VIRTIO_FS_MAX_PATHNAME_LENGTH, then the argument is rejected. (Length is
39 // defined as the number of CHAR16 elements in the UEFI-style pathname,
40 // excluding the terminating L'\0'.) This is a restriction that's checked on
41 // external UEFI-style pathname inputs.
42 //
43 // The limit is not expected to be a practical limitation; it's only supposed
44 // to prevent attempts at overflowing size calculations. For both kinds of
45 // pathnames, separate limits could be used; a common limit is used purely for
46 // simplicity.
47 //
48 #define VIRTIO_FS_MAX_PATHNAME_LENGTH ((UINTN)65535)
49
50 //
51 // Filesystem label encoded in UCS-2, transformed from the UTF-8 representation
52 // in "VIRTIO_FS_CONFIG.Tag", and NUL-terminated. Only the printable ASCII code
53 // points (U+0020 through U+007E) are supported.
54 //
55 typedef CHAR16 VIRTIO_FS_LABEL[VIRTIO_FS_TAG_BYTES + 1];
56
57 //
58 // Main context structure, expressing an EFI_SIMPLE_FILE_SYSTEM_PROTOCOL
59 // interface on top of the Virtio Filesystem device.
60 //
61 typedef struct {
62 //
63 // Parts of this structure are initialized / torn down in various functions
64 // at various call depths. The table to the right should make it easier to
65 // track them.
66 //
67 // field init function init depth
68 // ----------- ------------------ ----------
69 UINT64 Signature; // DriverBindingStart 0
70 VIRTIO_DEVICE_PROTOCOL *Virtio; // DriverBindingStart 0
71 VIRTIO_FS_LABEL Label; // VirtioFsInit 1
72 UINT16 QueueSize; // VirtioFsInit 1
73 VRING Ring; // VirtioRingInit 2
74 VOID *RingMap; // VirtioRingMap 2
75 UINT64 RequestId; // FuseInitSession 1
76 EFI_EVENT ExitBoot; // DriverBindingStart 0
77 LIST_ENTRY OpenFiles; // DriverBindingStart 0
78 EFI_SIMPLE_FILE_SYSTEM_PROTOCOL SimpleFs; // DriverBindingStart 0
79 } VIRTIO_FS;
80
81 #define VIRTIO_FS_FROM_SIMPLE_FS(SimpleFsReference) \
82 CR (SimpleFsReference, VIRTIO_FS, SimpleFs, VIRTIO_FS_SIG);
83
84 //
85 // Structure for describing a contiguous buffer, potentially mapped for Virtio
86 // transfer.
87 //
88 typedef struct {
89 //
90 // The following fields originate from the owner of the buffer.
91 //
92 VOID *Buffer;
93 UINTN Size;
94 //
95 // All of the fields below, until the end of the structure, are
96 // zero-initialized when the structure is initially validated.
97 //
98 // Mapped, MappedAddress and Mapping are updated when the buffer is mapped
99 // for VirtioOperationBusMasterRead or VirtioOperationBusMasterWrite. They
100 // are again updated when the buffer is unmapped.
101 //
102 BOOLEAN Mapped;
103 EFI_PHYSICAL_ADDRESS MappedAddress;
104 VOID *Mapping;
105 //
106 // Transferred is updated after VirtioFlush() returns successfully:
107 // - for VirtioOperationBusMasterRead, Transferred is set to Size;
108 // - for VirtioOperationBusMasterWrite, Transferred is calculated from the
109 // UsedLen output parameter of VirtioFlush().
110 //
111 UINTN Transferred;
112 } VIRTIO_FS_IO_VECTOR;
113
114 //
115 // Structure for describing a list of IO Vectors.
116 //
117 typedef struct {
118 //
119 // The following fields originate from the owner of the buffers.
120 //
121 VIRTIO_FS_IO_VECTOR *IoVec;
122 UINTN NumVec;
123 //
124 // TotalSize is calculated when the scatter-gather list is initially
125 // validated.
126 //
127 UINT32 TotalSize;
128 } VIRTIO_FS_SCATTER_GATHER_LIST;
129
130 //
131 // Private context structure that exposes EFI_FILE_PROTOCOL on top of an open
132 // FUSE file reference.
133 //
134 typedef struct {
135 UINT64 Signature;
136 EFI_FILE_PROTOCOL SimpleFile;
137 BOOLEAN IsDirectory;
138 BOOLEAN IsOpenForWriting;
139 VIRTIO_FS *OwnerFs;
140 LIST_ENTRY OpenFilesEntry;
141 CHAR8 *CanonicalPathname;
142 //
143 // In the FUSE wire protocol, every request except FUSE_INIT refers to a
144 // file, namely by the "VIRTIO_FS_FUSE_REQUEST.NodeId" field; that is, by the
145 // inode number of the file. However, some of the FUSE requests that we need
146 // for some of the EFI_FILE_PROTOCOL member functions require an open file
147 // handle *in addition* to the inode number. For simplicity, whenever a
148 // VIRTIO_FS_FILE object is created, primarily defined by its NodeId field,
149 // we also *open* the referenced file at once, and save the returned file
150 // handle in the FuseHandle field. This way, when an EFI_FILE_PROTOCOL member
151 // function must send a FUSE request that needs the file handle *in addition*
152 // to the inode number, FuseHandle will be at our disposal at once.
153 //
154 UINT64 NodeId;
155 UINT64 FuseHandle;
156 } VIRTIO_FS_FILE;
157
158 #define VIRTIO_FS_FILE_FROM_SIMPLE_FILE(SimpleFileReference) \
159 CR (SimpleFileReference, VIRTIO_FS_FILE, SimpleFile, VIRTIO_FS_FILE_SIG);
160
161 #define VIRTIO_FS_FILE_FROM_OPEN_FILES_ENTRY(OpenFilesEntryReference) \
162 CR (OpenFilesEntryReference, VIRTIO_FS_FILE, OpenFilesEntry, \
163 VIRTIO_FS_FILE_SIG);
164
165 //
166 // Initialization and helper routines for the Virtio Filesystem device.
167 //
168
169 EFI_STATUS
170 VirtioFsInit (
171 IN OUT VIRTIO_FS *VirtioFs
172 );
173
174 VOID
175 VirtioFsUninit (
176 IN OUT VIRTIO_FS *VirtioFs
177 );
178
179 VOID
180 EFIAPI
181 VirtioFsExitBoot (
182 IN EFI_EVENT ExitBootEvent,
183 IN VOID *VirtioFsAsVoid
184 );
185
186 EFI_STATUS
187 VirtioFsSgListsValidate (
188 IN VIRTIO_FS *VirtioFs,
189 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *RequestSgList,
190 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList OPTIONAL
191 );
192
193 EFI_STATUS
194 VirtioFsSgListsSubmit (
195 IN OUT VIRTIO_FS *VirtioFs,
196 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *RequestSgList,
197 IN OUT VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList OPTIONAL
198 );
199
200 EFI_STATUS
201 VirtioFsFuseNewRequest (
202 IN OUT VIRTIO_FS *VirtioFs,
203 OUT VIRTIO_FS_FUSE_REQUEST *Request,
204 IN UINT32 RequestSize,
205 IN VIRTIO_FS_FUSE_OPCODE Opcode,
206 IN UINT64 NodeId
207 );
208
209 EFI_STATUS
210 VirtioFsFuseCheckResponse (
211 IN VIRTIO_FS_SCATTER_GATHER_LIST *ResponseSgList,
212 IN UINT64 RequestId,
213 OUT UINTN *TailBufferFill
214 );
215
216 EFI_STATUS
217 VirtioFsErrnoToEfiStatus (
218 IN INT32 Errno
219 );
220
221 EFI_STATUS
222 VirtioFsAppendPath (
223 IN CHAR8 *LhsPath8,
224 IN CHAR16 *RhsPath16,
225 OUT CHAR8 **ResultPath8,
226 OUT BOOLEAN *RootEscape
227 );
228
229 EFI_STATUS
230 VirtioFsLookupMostSpecificParentDir (
231 IN OUT VIRTIO_FS *VirtioFs,
232 IN OUT CHAR8 *Path,
233 OUT UINT64 *DirNodeId,
234 OUT CHAR8 **LastComponent
235 );
236
237 EFI_STATUS
238 VirtioFsFuseAttrToEfiFileInfo (
239 IN VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE *FuseAttr,
240 OUT EFI_FILE_INFO *FileInfo
241 );
242
243 //
244 // Wrapper functions for FUSE commands (primitives).
245 //
246
247 EFI_STATUS
248 VirtioFsFuseLookup (
249 IN OUT VIRTIO_FS *VirtioFs,
250 IN UINT64 DirNodeId,
251 IN CHAR8 *Name,
252 OUT UINT64 *NodeId,
253 OUT VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE *FuseAttr
254 );
255
256 EFI_STATUS
257 VirtioFsFuseForget (
258 IN OUT VIRTIO_FS *VirtioFs,
259 IN UINT64 NodeId
260 );
261
262 EFI_STATUS
263 VirtioFsFuseMkDir (
264 IN OUT VIRTIO_FS *VirtioFs,
265 IN UINT64 ParentNodeId,
266 IN CHAR8 *Name,
267 OUT UINT64 *NodeId
268 );
269
270 EFI_STATUS
271 VirtioFsFuseOpen (
272 IN OUT VIRTIO_FS *VirtioFs,
273 IN UINT64 NodeId,
274 IN BOOLEAN ReadWrite,
275 OUT UINT64 *FuseHandle
276 );
277
278 EFI_STATUS
279 VirtioFsFuseReleaseFileOrDir (
280 IN OUT VIRTIO_FS *VirtioFs,
281 IN UINT64 NodeId,
282 IN UINT64 FuseHandle,
283 IN BOOLEAN IsDir
284 );
285
286 EFI_STATUS
287 VirtioFsFuseFsyncFileOrDir (
288 IN OUT VIRTIO_FS *VirtioFs,
289 IN UINT64 NodeId,
290 IN UINT64 FuseHandle,
291 IN BOOLEAN IsDir
292 );
293
294 EFI_STATUS
295 VirtioFsFuseFlush (
296 IN OUT VIRTIO_FS *VirtioFs,
297 IN UINT64 NodeId,
298 IN UINT64 FuseHandle
299 );
300
301 EFI_STATUS
302 VirtioFsFuseInitSession (
303 IN OUT VIRTIO_FS *VirtioFs
304 );
305
306 EFI_STATUS
307 VirtioFsFuseOpenDir (
308 IN OUT VIRTIO_FS *VirtioFs,
309 IN UINT64 NodeId,
310 OUT UINT64 *FuseHandle
311 );
312
313 EFI_STATUS
314 VirtioFsFuseOpenOrCreate (
315 IN OUT VIRTIO_FS *VirtioFs,
316 IN UINT64 ParentNodeId,
317 IN CHAR8 *Name,
318 OUT UINT64 *NodeId,
319 OUT UINT64 *FuseHandle
320 );
321
322 //
323 // EFI_SIMPLE_FILE_SYSTEM_PROTOCOL member functions for the Virtio Filesystem
324 // driver.
325 //
326
327 EFI_STATUS
328 EFIAPI
329 VirtioFsOpenVolume (
330 IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *This,
331 OUT EFI_FILE_PROTOCOL **Root
332 );
333
334 //
335 // EFI_FILE_PROTOCOL member functions for the Virtio Filesystem driver.
336 //
337
338 EFI_STATUS
339 EFIAPI
340 VirtioFsSimpleFileClose (
341 IN EFI_FILE_PROTOCOL *This
342 );
343
344 EFI_STATUS
345 EFIAPI
346 VirtioFsSimpleFileDelete (
347 IN EFI_FILE_PROTOCOL *This
348 );
349
350 EFI_STATUS
351 EFIAPI
352 VirtioFsSimpleFileFlush (
353 IN EFI_FILE_PROTOCOL *This
354 );
355
356 EFI_STATUS
357 EFIAPI
358 VirtioFsSimpleFileGetInfo (
359 IN EFI_FILE_PROTOCOL *This,
360 IN EFI_GUID *InformationType,
361 IN OUT UINTN *BufferSize,
362 OUT VOID *Buffer
363 );
364
365 EFI_STATUS
366 EFIAPI
367 VirtioFsSimpleFileGetPosition (
368 IN EFI_FILE_PROTOCOL *This,
369 OUT UINT64 *Position
370 );
371
372 EFI_STATUS
373 EFIAPI
374 VirtioFsSimpleFileOpen (
375 IN EFI_FILE_PROTOCOL *This,
376 OUT EFI_FILE_PROTOCOL **NewHandle,
377 IN CHAR16 *FileName,
378 IN UINT64 OpenMode,
379 IN UINT64 Attributes
380 );
381
382 EFI_STATUS
383 EFIAPI
384 VirtioFsSimpleFileRead (
385 IN EFI_FILE_PROTOCOL *This,
386 IN OUT UINTN *BufferSize,
387 OUT VOID *Buffer
388 );
389
390 EFI_STATUS
391 EFIAPI
392 VirtioFsSimpleFileSetInfo (
393 IN EFI_FILE_PROTOCOL *This,
394 IN EFI_GUID *InformationType,
395 IN UINTN BufferSize,
396 IN VOID *Buffer
397 );
398
399 EFI_STATUS
400 EFIAPI
401 VirtioFsSimpleFileSetPosition (
402 IN EFI_FILE_PROTOCOL *This,
403 IN UINT64 Position
404 );
405
406 EFI_STATUS
407 EFIAPI
408 VirtioFsSimpleFileWrite (
409 IN EFI_FILE_PROTOCOL *This,
410 IN OUT UINTN *BufferSize,
411 IN VOID *Buffer
412 );
413
414 #endif // VIRTIO_FS_DXE_H_