efifs.c revision 1.2.6.2 1 1.2.6.2 tron /* $NetBSD: efifs.c,v 1.2.6.2 2006/05/24 15:48:08 tron Exp $ */
2 1.2.6.2 tron
3 1.2.6.2 tron /*-
4 1.2.6.2 tron * Copyright (c) 2001 Doug Rabson
5 1.2.6.2 tron * All rights reserved.
6 1.2.6.2 tron *
7 1.2.6.2 tron * Redistribution and use in source and binary forms, with or without
8 1.2.6.2 tron * modification, are permitted provided that the following conditions
9 1.2.6.2 tron * are met:
10 1.2.6.2 tron * 1. Redistributions of source code must retain the above copyright
11 1.2.6.2 tron * notice, this list of conditions and the following disclaimer.
12 1.2.6.2 tron * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.6.2 tron * notice, this list of conditions and the following disclaimer in the
14 1.2.6.2 tron * documentation and/or other materials provided with the distribution.
15 1.2.6.2 tron *
16 1.2.6.2 tron * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.6.2 tron * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.6.2 tron * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.6.2 tron * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.6.2 tron * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.6.2 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.6.2 tron * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.6.2 tron * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.6.2 tron * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.6.2 tron * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.6.2 tron * SUCH DAMAGE.
27 1.2.6.2 tron *
28 1.2.6.2 tron * $FreeBSD: src/sys/boot/efi/libefi/efifs.c,v 1.8 2003/08/02 08:22:03 marcel Exp $
29 1.2.6.2 tron */
30 1.2.6.2 tron
31 1.2.6.2 tron #include <sys/time.h>
32 1.2.6.2 tron #include <sys/dirent.h>
33 1.2.6.2 tron #include <lib/libsa/stand.h>
34 1.2.6.2 tron #include <lib/libkern/libkern.h>
35 1.2.6.2 tron #include <machine/stdarg.h>
36 1.2.6.2 tron
37 1.2.6.2 tron #include <efi.h>
38 1.2.6.2 tron #include <efilib.h>
39 1.2.6.2 tron
40 1.2.6.2 tron #include <bootstrap.h>
41 1.2.6.2 tron
42 1.2.6.2 tron #include "efiboot.h"
43 1.2.6.2 tron
44 1.2.6.2 tron /* Perform I/O in blocks of size EFI_BLOCK_SIZE. */
45 1.2.6.2 tron #define EFI_BLOCK_SIZE (1024 * 1024)
46 1.2.6.2 tron
47 1.2.6.2 tron
48 1.2.6.2 tron int
49 1.2.6.2 tron efifs_open(const char *upath, struct open_file *f)
50 1.2.6.2 tron {
51 1.2.6.2 tron struct efi_devdesc *dev = f->f_devdata;
52 1.2.6.2 tron static EFI_GUID sfsid = SIMPLE_FILE_SYSTEM_PROTOCOL;
53 1.2.6.2 tron EFI_FILE_IO_INTERFACE *sfs;
54 1.2.6.2 tron EFI_FILE *root;
55 1.2.6.2 tron EFI_FILE *file;
56 1.2.6.2 tron EFI_STATUS status;
57 1.2.6.2 tron CHAR16 *cp;
58 1.2.6.2 tron CHAR16 *path;
59 1.2.6.2 tron
60 1.2.6.2 tron /*
61 1.2.6.2 tron * We cannot blindly assume that f->f_devdata points to a
62 1.2.6.2 tron * efi_devdesc structure. Before we dereference 'dev', make
63 1.2.6.2 tron * sure that the underlying device is ours.
64 1.2.6.2 tron */
65 1.2.6.2 tron if (f->f_dev != &devsw[0] || dev->d_handle == NULL)
66 1.2.6.2 tron return ENOENT;
67 1.2.6.2 tron
68 1.2.6.2 tron status = BS->HandleProtocol(dev->d_handle, &sfsid, (VOID **)&sfs);
69 1.2.6.2 tron if (EFI_ERROR(status))
70 1.2.6.2 tron return ENOENT;
71 1.2.6.2 tron
72 1.2.6.2 tron /*
73 1.2.6.2 tron * Find the root directory.
74 1.2.6.2 tron */
75 1.2.6.2 tron status = sfs->OpenVolume(sfs, &root);
76 1.2.6.2 tron
77 1.2.6.2 tron /*
78 1.2.6.2 tron * Convert path to CHAR16, skipping leading separators.
79 1.2.6.2 tron */
80 1.2.6.2 tron while (*upath == '/')
81 1.2.6.2 tron upath++;
82 1.2.6.2 tron if (!*upath) {
83 1.2.6.2 tron /* Opening the root directory, */
84 1.2.6.2 tron f->f_fsdata = root;
85 1.2.6.2 tron return 0;
86 1.2.6.2 tron }
87 1.2.6.2 tron cp = path = alloc((strlen(upath) + 1) * sizeof(CHAR16));
88 1.2.6.2 tron if (path == NULL)
89 1.2.6.2 tron return ENOMEM;
90 1.2.6.2 tron while (*upath) {
91 1.2.6.2 tron if (*upath == '/')
92 1.2.6.2 tron *cp = '\\';
93 1.2.6.2 tron else
94 1.2.6.2 tron *cp = *upath;
95 1.2.6.2 tron upath++;
96 1.2.6.2 tron cp++;
97 1.2.6.2 tron }
98 1.2.6.2 tron *cp++ = 0;
99 1.2.6.2 tron
100 1.2.6.2 tron /*
101 1.2.6.2 tron * Try to open it.
102 1.2.6.2 tron */
103 1.2.6.2 tron status = root->Open(root, &file, path, EFI_FILE_MODE_READ, 0);
104 1.2.6.2 tron free(path);
105 1.2.6.2 tron if (EFI_ERROR(status)) {
106 1.2.6.2 tron root->Close(root);
107 1.2.6.2 tron return ENOENT;
108 1.2.6.2 tron }
109 1.2.6.2 tron
110 1.2.6.2 tron root->Close(root);
111 1.2.6.2 tron f->f_fsdata = file;
112 1.2.6.2 tron return 0;
113 1.2.6.2 tron }
114 1.2.6.2 tron
115 1.2.6.2 tron int
116 1.2.6.2 tron efifs_close(struct open_file *f)
117 1.2.6.2 tron {
118 1.2.6.2 tron EFI_FILE *file = f->f_fsdata;
119 1.2.6.2 tron
120 1.2.6.2 tron file->Close(file);
121 1.2.6.2 tron return 0;
122 1.2.6.2 tron }
123 1.2.6.2 tron
124 1.2.6.2 tron int
125 1.2.6.2 tron efifs_read(struct open_file *f, void *buf, size_t size, size_t *resid)
126 1.2.6.2 tron {
127 1.2.6.2 tron EFI_FILE *file = f->f_fsdata;
128 1.2.6.2 tron EFI_STATUS status;
129 1.2.6.2 tron UINTN sz = size;
130 1.2.6.2 tron char *bufp;
131 1.2.6.2 tron
132 1.2.6.2 tron bufp = buf;
133 1.2.6.2 tron while (size > 0) {
134 1.2.6.2 tron sz = size;
135 1.2.6.2 tron if (sz > EFI_BLOCK_SIZE)
136 1.2.6.2 tron sz = EFI_BLOCK_SIZE;
137 1.2.6.2 tron status = file->Read(file, &sz, bufp);
138 1.2.6.2 tron
139 1.2.6.2 tron #if !defined(LIBSA_NO_TWIDDLE)
140 1.2.6.2 tron twiddle();
141 1.2.6.2 tron #endif
142 1.2.6.2 tron
143 1.2.6.2 tron if (EFI_ERROR(status))
144 1.2.6.2 tron return EIO;
145 1.2.6.2 tron if (sz == 0)
146 1.2.6.2 tron break;
147 1.2.6.2 tron size -= sz;
148 1.2.6.2 tron bufp += sz;
149 1.2.6.2 tron }
150 1.2.6.2 tron if (resid)
151 1.2.6.2 tron *resid = size;
152 1.2.6.2 tron return 0;
153 1.2.6.2 tron }
154 1.2.6.2 tron
155 1.2.6.2 tron int
156 1.2.6.2 tron efifs_write(struct open_file *f, void *buf, size_t size, size_t *resid)
157 1.2.6.2 tron {
158 1.2.6.2 tron EFI_FILE *file = f->f_fsdata;
159 1.2.6.2 tron EFI_STATUS status;
160 1.2.6.2 tron UINTN sz = size;
161 1.2.6.2 tron char *bufp;
162 1.2.6.2 tron
163 1.2.6.2 tron bufp = buf;
164 1.2.6.2 tron while (size > 0) {
165 1.2.6.2 tron sz = size;
166 1.2.6.2 tron if (sz > EFI_BLOCK_SIZE)
167 1.2.6.2 tron sz = EFI_BLOCK_SIZE;
168 1.2.6.2 tron status = file->Write(file, &sz, bufp);
169 1.2.6.2 tron
170 1.2.6.2 tron #if !defined(LIBSA_NO_TWIDDLE)
171 1.2.6.2 tron twiddle();
172 1.2.6.2 tron #endif
173 1.2.6.2 tron
174 1.2.6.2 tron if (EFI_ERROR(status))
175 1.2.6.2 tron return EIO;
176 1.2.6.2 tron if (sz == 0)
177 1.2.6.2 tron break;
178 1.2.6.2 tron size -= sz;
179 1.2.6.2 tron bufp += sz;
180 1.2.6.2 tron }
181 1.2.6.2 tron if (resid)
182 1.2.6.2 tron *resid = size;
183 1.2.6.2 tron return 0;
184 1.2.6.2 tron }
185 1.2.6.2 tron
186 1.2.6.2 tron off_t
187 1.2.6.2 tron efifs_seek(struct open_file *f, off_t offset, int where)
188 1.2.6.2 tron {
189 1.2.6.2 tron EFI_FILE *file = f->f_fsdata;
190 1.2.6.2 tron EFI_STATUS status;
191 1.2.6.2 tron UINT64 base;
192 1.2.6.2 tron UINTN sz;
193 1.2.6.2 tron static EFI_GUID infoid = EFI_FILE_INFO_ID;
194 1.2.6.2 tron EFI_FILE_INFO info;
195 1.2.6.2 tron
196 1.2.6.2 tron switch (where) {
197 1.2.6.2 tron case SEEK_SET:
198 1.2.6.2 tron base = 0;
199 1.2.6.2 tron break;
200 1.2.6.2 tron
201 1.2.6.2 tron case SEEK_CUR:
202 1.2.6.2 tron status = file->GetPosition(file, &base);
203 1.2.6.2 tron if (EFI_ERROR(status))
204 1.2.6.2 tron return -1;
205 1.2.6.2 tron break;
206 1.2.6.2 tron
207 1.2.6.2 tron case SEEK_END:
208 1.2.6.2 tron sz = sizeof(info);
209 1.2.6.2 tron status = file->GetInfo(file, &infoid, &sz, &info);
210 1.2.6.2 tron if (EFI_ERROR(status))
211 1.2.6.2 tron return -1;
212 1.2.6.2 tron base = info.FileSize;
213 1.2.6.2 tron break;
214 1.2.6.2 tron }
215 1.2.6.2 tron
216 1.2.6.2 tron status = file->SetPosition(file, base + offset);
217 1.2.6.2 tron if (EFI_ERROR(status))
218 1.2.6.2 tron return -1;
219 1.2.6.2 tron file->GetPosition(file, &base);
220 1.2.6.2 tron
221 1.2.6.2 tron return base;
222 1.2.6.2 tron }
223 1.2.6.2 tron
224 1.2.6.2 tron int
225 1.2.6.2 tron efifs_stat(struct open_file *f, struct stat *sb)
226 1.2.6.2 tron {
227 1.2.6.2 tron EFI_FILE *file = f->f_fsdata;
228 1.2.6.2 tron EFI_STATUS status;
229 1.2.6.2 tron char *buf;
230 1.2.6.2 tron UINTN sz;
231 1.2.6.2 tron static EFI_GUID infoid = EFI_FILE_INFO_ID;
232 1.2.6.2 tron EFI_FILE_INFO *info;
233 1.2.6.2 tron
234 1.2.6.2 tron bzero(sb, sizeof(*sb));
235 1.2.6.2 tron
236 1.2.6.2 tron buf = alloc(1024);
237 1.2.6.2 tron sz = 1024;
238 1.2.6.2 tron
239 1.2.6.2 tron status = file->GetInfo(file, &infoid, &sz, buf);
240 1.2.6.2 tron if (EFI_ERROR(status)) {
241 1.2.6.2 tron free(buf);
242 1.2.6.2 tron return -1;
243 1.2.6.2 tron }
244 1.2.6.2 tron
245 1.2.6.2 tron info = (EFI_FILE_INFO *) buf;
246 1.2.6.2 tron
247 1.2.6.2 tron if (info->Attribute & EFI_FILE_READ_ONLY)
248 1.2.6.2 tron sb->st_mode = S_IRUSR;
249 1.2.6.2 tron else
250 1.2.6.2 tron sb->st_mode = S_IRUSR | S_IWUSR;
251 1.2.6.2 tron if (info->Attribute & EFI_FILE_DIRECTORY)
252 1.2.6.2 tron sb->st_mode |= S_IFDIR;
253 1.2.6.2 tron else
254 1.2.6.2 tron sb->st_mode |= S_IFREG;
255 1.2.6.2 tron sb->st_size = info->FileSize;
256 1.2.6.2 tron
257 1.2.6.2 tron free(buf);
258 1.2.6.2 tron return 0;
259 1.2.6.2 tron }
260 1.2.6.2 tron
261 1.2.6.2 tron int
262 1.2.6.2 tron efifs_readdir(struct open_file *f, struct dirent *d)
263 1.2.6.2 tron {
264 1.2.6.2 tron EFI_FILE *file = f->f_fsdata;
265 1.2.6.2 tron EFI_STATUS status;
266 1.2.6.2 tron char *buf;
267 1.2.6.2 tron UINTN sz;
268 1.2.6.2 tron EFI_FILE_INFO *info;
269 1.2.6.2 tron int i;
270 1.2.6.2 tron
271 1.2.6.2 tron buf = alloc(1024);
272 1.2.6.2 tron sz = 1024;
273 1.2.6.2 tron
274 1.2.6.2 tron status = file->Read(file, &sz, buf);
275 1.2.6.2 tron if (EFI_ERROR(status) || sz < offsetof(EFI_FILE_INFO, FileName))
276 1.2.6.2 tron return ENOENT;
277 1.2.6.2 tron
278 1.2.6.2 tron info = (EFI_FILE_INFO *) buf;
279 1.2.6.2 tron
280 1.2.6.2 tron d->d_fileno = 0;
281 1.2.6.2 tron d->d_reclen = sizeof(*d);
282 1.2.6.2 tron if (info->Attribute & EFI_FILE_DIRECTORY)
283 1.2.6.2 tron d->d_type = DT_DIR;
284 1.2.6.2 tron else
285 1.2.6.2 tron d->d_type = DT_REG;
286 1.2.6.2 tron d->d_namlen = ((info->Size - offsetof(EFI_FILE_INFO, FileName))
287 1.2.6.2 tron / sizeof(CHAR16));
288 1.2.6.2 tron for (i = 0; i < d->d_namlen; i++)
289 1.2.6.2 tron d->d_name[i] = info->FileName[i];
290 1.2.6.2 tron d->d_name[i] = 0;
291 1.2.6.2 tron
292 1.2.6.2 tron free(buf);
293 1.2.6.2 tron return 0;
294 1.2.6.2 tron }
295 1.2.6.2 tron
296 1.2.6.2 tron static EFI_HANDLE *fs_handles;
297 1.2.6.2 tron UINTN fs_handle_count;
298 1.2.6.2 tron
299 1.2.6.2 tron int
300 1.2.6.2 tron efifs_get_unit(EFI_HANDLE h)
301 1.2.6.2 tron {
302 1.2.6.2 tron UINTN u;
303 1.2.6.2 tron
304 1.2.6.2 tron u = 0;
305 1.2.6.2 tron while (u < fs_handle_count && fs_handles[u] != h)
306 1.2.6.2 tron u++;
307 1.2.6.2 tron return ((u < fs_handle_count) ? u : -1);
308 1.2.6.2 tron }
309 1.2.6.2 tron
310 1.2.6.2 tron int
311 1.2.6.2 tron efifs_dev_init(void)
312 1.2.6.2 tron {
313 1.2.6.2 tron EFI_STATUS status;
314 1.2.6.2 tron UINTN sz;
315 1.2.6.2 tron static EFI_GUID sfsid = SIMPLE_FILE_SYSTEM_PROTOCOL;
316 1.2.6.2 tron
317 1.2.6.2 tron sz = 0;
318 1.2.6.2 tron status = BS->LocateHandle(ByProtocol, &sfsid, 0, &sz, 0);
319 1.2.6.2 tron if (status != EFI_BUFFER_TOO_SMALL)
320 1.2.6.2 tron return ENOENT;
321 1.2.6.2 tron fs_handles = (EFI_HANDLE *) alloc(sz);
322 1.2.6.2 tron status = BS->LocateHandle(ByProtocol, &sfsid, 0,
323 1.2.6.2 tron &sz, fs_handles);
324 1.2.6.2 tron if (EFI_ERROR(status)) {
325 1.2.6.2 tron free(fs_handles);
326 1.2.6.2 tron return ENOENT;
327 1.2.6.2 tron }
328 1.2.6.2 tron fs_handle_count = sz / sizeof(EFI_HANDLE);
329 1.2.6.2 tron
330 1.2.6.2 tron return 0;
331 1.2.6.2 tron }
332 1.2.6.2 tron
333 1.2.6.2 tron /*
334 1.2.6.2 tron * Print information about disks
335 1.2.6.2 tron */
336 1.2.6.2 tron void
337 1.2.6.2 tron efifs_dev_print(int verbose)
338 1.2.6.2 tron {
339 1.2.6.2 tron int i;
340 1.2.6.2 tron char line[80];
341 1.2.6.2 tron
342 1.2.6.2 tron for (i = 0; i < fs_handle_count; i++) {
343 1.2.6.2 tron sprintf(line, " fs%d: EFI filesystem", i);
344 1.2.6.2 tron pager_output(line);
345 1.2.6.2 tron /* XXX more detail? */
346 1.2.6.2 tron pager_output("\n");
347 1.2.6.2 tron }
348 1.2.6.2 tron }
349 1.2.6.2 tron
350 1.2.6.2 tron /*
351 1.2.6.2 tron * Attempt to open the disk described by (dev) for use by (f).
352 1.2.6.2 tron *
353 1.2.6.2 tron * Note that the philosophy here is "give them exactly what
354 1.2.6.2 tron * they ask for". This is necessary because being too "smart"
355 1.2.6.2 tron * about what the user might want leads to complications.
356 1.2.6.2 tron * (eg. given no slice or partition value, with a disk that is
357 1.2.6.2 tron * sliced - are they after the first BSD slice, or the DOS
358 1.2.6.2 tron * slice before it?)
359 1.2.6.2 tron */
360 1.2.6.2 tron int
361 1.2.6.2 tron efifs_dev_open(struct open_file *f, ...)
362 1.2.6.2 tron {
363 1.2.6.2 tron va_list args;
364 1.2.6.2 tron struct efi_devdesc *dev;
365 1.2.6.2 tron int unit;
366 1.2.6.2 tron
367 1.2.6.2 tron va_start(args, f);
368 1.2.6.2 tron dev = va_arg(args, struct efi_devdesc*);
369 1.2.6.2 tron va_end(args);
370 1.2.6.2 tron
371 1.2.6.2 tron unit = dev->d_kind.efidisk.unit;
372 1.2.6.2 tron if (unit < 0 || unit >= fs_handle_count) {
373 1.2.6.2 tron printf("attempt to open nonexistent EFI filesystem\n");
374 1.2.6.2 tron return(ENXIO);
375 1.2.6.2 tron }
376 1.2.6.2 tron
377 1.2.6.2 tron dev->d_handle = fs_handles[unit];
378 1.2.6.2 tron
379 1.2.6.2 tron return 0;
380 1.2.6.2 tron }
381 1.2.6.2 tron
382 1.2.6.2 tron int
383 1.2.6.2 tron efifs_dev_close(struct open_file *f)
384 1.2.6.2 tron {
385 1.2.6.2 tron
386 1.2.6.2 tron return 0;
387 1.2.6.2 tron }
388 1.2.6.2 tron
389 1.2.6.2 tron int
390 1.2.6.2 tron efifs_dev_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
391 1.2.6.2 tron {
392 1.2.6.2 tron return 0;
393 1.2.6.2 tron }
394 1.2.6.2 tron
395