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