2 * Copyright (c) 2001 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/boot/efi/libefi/efifs.c,v 1.8 2003/08/02 08:22:03 marcel Exp $
27 * $DragonFly: src/sys/boot/efi/libefi/efifs.c,v 1.1 2003/11/10 06:08:33 dillon Exp $
30 #include <sys/param.h>
40 /* Perform I/O in blocks of size EFI_BLOCK_SIZE. */
41 #define EFI_BLOCK_SIZE (1024 * 1024)
44 efifs_open(const char *upath, struct open_file *f)
46 struct efi_devdesc *dev = f->f_devdata;
47 static EFI_GUID sfsid = SIMPLE_FILE_SYSTEM_PROTOCOL;
48 EFI_FILE_IO_INTERFACE *sfs;
56 * We cannot blindly assume that f->f_devdata points to a
57 * efi_devdesc structure. Before we dereference 'dev', make
58 * sure that the underlying device is ours.
60 if (f->f_dev != &efifs_dev || dev->d_handle == NULL)
63 status = BS->HandleProtocol(dev->d_handle, &sfsid, (VOID **)&sfs);
64 if (EFI_ERROR(status))
68 * Find the root directory.
70 status = sfs->OpenVolume(sfs, &root);
73 * Convert path to CHAR16, skipping leading separators.
78 /* Opening the root directory, */
82 cp = path = malloc((strlen(upath) + 1) * sizeof(CHAR16));
98 status = root->Open(root, &file, path, EFI_FILE_MODE_READ, 0);
100 if (EFI_ERROR(status)) {
111 efifs_close(struct open_file *f)
113 EFI_FILE *file = f->f_fsdata;
120 efifs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
122 EFI_FILE *file = f->f_fsdata;
130 if (sz > EFI_BLOCK_SIZE)
132 status = file->Read(file, &sz, bufp);
134 if (EFI_ERROR(status))
147 efifs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
149 EFI_FILE *file = f->f_fsdata;
157 if (sz > EFI_BLOCK_SIZE)
159 status = file->Write(file, &sz, bufp);
161 if (EFI_ERROR(status))
174 efifs_seek(struct open_file *f, off_t offset, int where)
176 EFI_FILE *file = f->f_fsdata;
180 static EFI_GUID infoid = EFI_FILE_INFO_ID;
189 status = file->GetPosition(file, &base);
190 if (EFI_ERROR(status))
196 status = file->GetInfo(file, &infoid, &sz, &info);
197 if (EFI_ERROR(status))
199 base = info.FileSize;
203 status = file->SetPosition(file, base + offset);
204 if (EFI_ERROR(status))
206 file->GetPosition(file, &base);
212 efifs_stat(struct open_file *f, struct stat *sb)
214 EFI_FILE *file = f->f_fsdata;
218 static EFI_GUID infoid = EFI_FILE_INFO_ID;
221 bzero(sb, sizeof(*sb));
226 status = file->GetInfo(file, &infoid, &sz, buf);
227 if (EFI_ERROR(status)) {
232 info = (EFI_FILE_INFO *) buf;
234 if (info->Attribute & EFI_FILE_READ_ONLY)
235 sb->st_mode = S_IRUSR;
237 sb->st_mode = S_IRUSR | S_IWUSR;
238 if (info->Attribute & EFI_FILE_DIRECTORY)
239 sb->st_mode |= S_IFDIR;
241 sb->st_mode |= S_IFREG;
242 sb->st_size = info->FileSize;
249 efifs_readdir(struct open_file *f, struct dirent *d)
251 EFI_FILE *file = f->f_fsdata;
261 status = file->Read(file, &sz, buf);
262 if (EFI_ERROR(status) || sz < offsetof(EFI_FILE_INFO, FileName))
265 info = (EFI_FILE_INFO *) buf;
268 d->d_reclen = sizeof(*d);
269 if (info->Attribute & EFI_FILE_DIRECTORY)
273 d->d_namlen = ((info->Size - offsetof(EFI_FILE_INFO, FileName))
275 for (i = 0; i < d->d_namlen; i++)
276 d->d_name[i] = info->FileName[i];
283 struct fs_ops efi_fsops = {
294 static EFI_HANDLE *fs_handles;
295 UINTN fs_handle_count;
298 efifs_get_unit(EFI_HANDLE h)
303 while (u < fs_handle_count && fs_handles[u] != h)
305 return ((u < fs_handle_count) ? u : -1);
313 static EFI_GUID sfsid = SIMPLE_FILE_SYSTEM_PROTOCOL;
316 status = BS->LocateHandle(ByProtocol, &sfsid, 0, &sz, 0);
317 if (status != EFI_BUFFER_TOO_SMALL)
319 fs_handles = (EFI_HANDLE *) malloc(sz);
320 status = BS->LocateHandle(ByProtocol, &sfsid, 0,
322 if (EFI_ERROR(status)) {
326 fs_handle_count = sz / sizeof(EFI_HANDLE);
332 * Print information about disks
335 efifs_dev_print(int verbose)
340 for (i = 0; i < fs_handle_count; i++) {
341 sprintf(line, " fs%d: EFI filesystem", i);
343 /* XXX more detail? */
349 * Attempt to open the disk described by (dev) for use by (f).
351 * Note that the philosophy here is "give them exactly what
352 * they ask for". This is necessary because being too "smart"
353 * about what the user might want leads to complications.
354 * (eg. given no slice or partition value, with a disk that is
355 * sliced - are they after the first BSD slice, or the DOS
359 efifs_dev_open(struct open_file *f, ...)
362 struct efi_devdesc *dev;
366 dev = va_arg(args, struct efi_devdesc*);
369 unit = dev->d_kind.efidisk.unit;
370 if (unit < 0 || unit >= fs_handle_count) {
371 printf("attempt to open nonexistent EFI filesystem\n");
375 dev->d_handle = fs_handles[unit];
381 efifs_dev_close(struct open_file *f)
388 efifs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
393 struct devsw efifs_dev = {