summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/VirtioFsDxe/SimpleFsGetInfo.c
blob: c8be1d502202d8a449956a70288b80ac8e537afc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/** @file
  EFI_FILE_PROTOCOL.GetInfo() member function for the Virtio Filesystem driver.

  Copyright (C) 2020, Red Hat, Inc.

  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <Guid/FileSystemInfo.h>            // gEfiFileSystemInfoGuid
#include <Guid/FileSystemVolumeLabelInfo.h> // gEfiFileSystemVolumeLabelInfo...
#include <Library/BaseLib.h>                // StrSize()
#include <Library/BaseMemoryLib.h>          // CompareGuid()

#include "VirtioFsDxe.h"

/**
  Provide EFI_FILE_INFO about this particular file.
**/
STATIC
EFI_STATUS
GetFileInfo (
  IN     EFI_FILE_PROTOCOL *This,
  IN OUT UINTN             *BufferSize,
     OUT VOID              *Buffer
  )
{
  VIRTIO_FS_FILE                     *VirtioFsFile;
  VIRTIO_FS                          *VirtioFs;
  UINTN                              AllocSize;
  UINTN                              BasenameSize;
  EFI_STATUS                         Status;
  EFI_FILE_INFO                      *FileInfo;
  VIRTIO_FS_FUSE_ATTRIBUTES_RESPONSE FuseAttr;

  VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);
  VirtioFs     = VirtioFsFile->OwnerFs;

  AllocSize = *BufferSize;

  //
  // Calculate the needed size.
  //
  BasenameSize = 0;
  Status = VirtioFsGetBasename (VirtioFsFile->CanonicalPathname, NULL,
             &BasenameSize);
  ASSERT (Status == EFI_BUFFER_TOO_SMALL);
  *BufferSize = OFFSET_OF (EFI_FILE_INFO, FileName) + BasenameSize;

  if (*BufferSize > AllocSize) {
    return EFI_BUFFER_TOO_SMALL;
  }

  //
  // Set the structure size, and store the basename.
  //
  FileInfo = Buffer;
  FileInfo->Size = *BufferSize;
  Status = VirtioFsGetBasename (VirtioFsFile->CanonicalPathname,
             FileInfo->FileName, &BasenameSize);
  ASSERT_EFI_ERROR (Status);

  //
  // Fetch the file attributes, and convert them into the caller's buffer.
  //
  Status = VirtioFsFuseGetAttr (VirtioFs, VirtioFsFile->NodeId, &FuseAttr);
  if (!EFI_ERROR (Status)) {
    Status = VirtioFsFuseAttrToEfiFileInfo (&FuseAttr, FileInfo);
  }
  return (Status == EFI_BUFFER_TOO_SMALL) ? EFI_DEVICE_ERROR : Status;
}

/**
  Provide EFI_FILE_SYSTEM_INFO about the filesystem this file lives on.
**/
STATIC
EFI_STATUS
GetFileSystemInfo (
  IN     EFI_FILE_PROTOCOL *This,
  IN OUT UINTN             *BufferSize,
     OUT VOID              *Buffer
  )
{
  VIRTIO_FS_FILE                 *VirtioFsFile;
  VIRTIO_FS                      *VirtioFs;
  UINTN                          AllocSize;
  UINTN                          LabelSize;
  EFI_STATUS                     Status;
  VIRTIO_FS_FUSE_STATFS_RESPONSE FilesysAttr;
  UINT64                         MaxBlocks;
  EFI_FILE_SYSTEM_INFO           *FilesysInfo;

  VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);
  VirtioFs     = VirtioFsFile->OwnerFs;

  AllocSize = *BufferSize;

  //
  // Calculate the needed size.
  //
  LabelSize = StrSize (VirtioFs->Label);
  *BufferSize = OFFSET_OF (EFI_FILE_SYSTEM_INFO, VolumeLabel) + LabelSize;

  if (*BufferSize > AllocSize) {
    return EFI_BUFFER_TOO_SMALL;
  }

  //
  // Fetch the filesystem attributes.
  //
  Status = VirtioFsFuseStatFs (VirtioFs, VirtioFsFile->NodeId, &FilesysAttr);
  if (EFI_ERROR (Status)) {
    return (Status == EFI_BUFFER_TOO_SMALL) ? EFI_DEVICE_ERROR : Status;
  }
  //
  // Sanity checks...
  //
  if (FilesysAttr.Frsize != FilesysAttr.Bsize) {
    return EFI_UNSUPPORTED;
  }
  if (FilesysAttr.Frsize == 0 || FilesysAttr.Blocks == 0 ||
      FilesysAttr.Bavail > FilesysAttr.Blocks) {
    return EFI_DEVICE_ERROR;
  }
  MaxBlocks = DivU64x32 (MAX_UINT64, FilesysAttr.Frsize);
  if (FilesysAttr.Blocks > MaxBlocks || FilesysAttr.Bavail > MaxBlocks) {
    return EFI_DEVICE_ERROR;
  }

  //
  // Fill in EFI_FILE_SYSTEM_INFO.
  //
  FilesysInfo             = Buffer;
  FilesysInfo->Size       = *BufferSize;
  FilesysInfo->ReadOnly   = FALSE;
  FilesysInfo->VolumeSize = MultU64x32 (FilesysAttr.Blocks,
                              FilesysAttr.Frsize);
  FilesysInfo->FreeSpace  = MultU64x32 (FilesysAttr.Bavail,
                              FilesysAttr.Frsize);
  FilesysInfo->BlockSize  = FilesysAttr.Frsize;
  CopyMem (FilesysInfo->VolumeLabel, VirtioFs->Label, LabelSize);

  return EFI_SUCCESS;
}

/**
  Return the filesystem label as EFI_FILE_SYSTEM_VOLUME_LABEL.
**/
STATIC
EFI_STATUS
GetFileSystemVolumeLabelInfo (
  IN     EFI_FILE_PROTOCOL *This,
  IN OUT UINTN             *BufferSize,
     OUT VOID              *Buffer
  )
{
  VIRTIO_FS_FILE               *VirtioFsFile;
  VIRTIO_FS                    *VirtioFs;
  UINTN                        AllocSize;
  UINTN                        LabelSize;
  EFI_FILE_SYSTEM_VOLUME_LABEL *FilesysVolumeLabel;

  VirtioFsFile = VIRTIO_FS_FILE_FROM_SIMPLE_FILE (This);
  VirtioFs     = VirtioFsFile->OwnerFs;

  AllocSize = *BufferSize;

  //
  // Calculate the needed size.
  //
  LabelSize = StrSize (VirtioFs->Label);
  *BufferSize = (OFFSET_OF (EFI_FILE_SYSTEM_VOLUME_LABEL, VolumeLabel) +
                 LabelSize);

  if (*BufferSize > AllocSize) {
    return EFI_BUFFER_TOO_SMALL;
  }

  //
  // Store the label.
  //
  FilesysVolumeLabel = Buffer;
  CopyMem (FilesysVolumeLabel->VolumeLabel, VirtioFs->Label, LabelSize);

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
VirtioFsSimpleFileGetInfo (
  IN     EFI_FILE_PROTOCOL *This,
  IN     EFI_GUID          *InformationType,
  IN OUT UINTN             *BufferSize,
     OUT VOID              *Buffer
  )
{
  if (CompareGuid (InformationType, &gEfiFileInfoGuid)) {
    return GetFileInfo (This, BufferSize, Buffer);
  }

  if (CompareGuid (InformationType, &gEfiFileSystemInfoGuid)) {
    return GetFileSystemInfo (This, BufferSize, Buffer);
  }

  if (CompareGuid (InformationType, &gEfiFileSystemVolumeLabelInfoIdGuid)) {
    return GetFileSystemVolumeLabelInfo (This, BufferSize, Buffer);
  }

  return EFI_UNSUPPORTED;
}