2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
5 * This file is part of the ELILO, the EFI Linux boot loader.
7 * ELILO is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * ELILO is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with ELILO; see the file COPYING. If not, write to the Free
19 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 * Please check out the elilo.txt for complete documentation on how
23 * to use this program.
36 #include "glue_localfs.h"
39 #include "glue_netfs.h"
43 #include "glue_ext2fs.h"
47 * table of device naming schemes.
48 * we will probe each one in sequential order and stop at first match.
50 extern devname_scheme_t simple_devname_scheme;
52 static devname_scheme_t *devname_scheme_tab[]={
53 &simple_devname_scheme,
59 * Once we are able to create Boot Services drivers we won't need
60 * this hack and we'll be able to load the driver from the boot manager
61 * or EFI shell. So for now we explicitely invoke the probe routine
62 * of each driver that we know about
65 typedef EFI_STATUS (fops_fixups_t)(EFI_HANDLE dev, device_t *d);
68 * List of filesystem we currently support
72 static fileops_fs_t *fs_tab[]={
85 static device_t *dev_tab;
86 static UINTN ndev, ndev_boot;
87 static device_t *boot_dev;
89 typedef struct _fdesc_t {
90 struct _fdesc_t *next; /* pointer to next free (when in free list) */
91 fileops_t *fops; /* pointer to filesystem-specific glue interface */
92 UINTN fh; /* file descriptor from lower level protocol */
95 #define FILEOPS_FD_MAX 16
97 static fdesc_t fd_tab[FILEOPS_FD_MAX];
98 static fdesc_t *free_fd;
100 static devname_scheme_t *current_devname_scheme;
102 #define FDESC_IDX(f) (fops_fd_t)(f-fd_tab)
103 #define FDESC_INVALID_FD(fd) (fd >= FILEOPS_FD_MAX || fd_tab[(fd)].fops == NULL)
104 #define FDESC_F(fd) (fd_tab+(fd))
111 if (free_fd == NULL) {
112 ERR_PRT((L"out of file descriptor"));
115 free_fd = free_fd->next;
124 ERR_PRT((L"invalid fd"));
126 fd->fops = NULL; /* mark as invalid */
132 glue_filesystem(EFI_GUID *proto, EFI_HANDLE dev, fops_fs_glue_t glue)
138 status = BS->HandleProtocol(dev, proto, &intf);
139 if (EFI_ERROR(status)) {
140 ERR_PRT((L"unable to locate %g: should not happen", proto));
141 return NULL; /* should not happen */
143 fops = (fileops_t *)alloc(sizeof(*fops), EfiLoaderData);
145 ERR_PRT((L"failed to allocate fileops"));
146 return NULL; /* XXX uninstall protocol */
148 Memset(fops, 0, sizeof(*fops));
152 /* initialize private interface now */
159 fops_split_path(CHAR16 *path, CHAR16 *dev)
162 UINTN len = FILEOPS_DEVNAME_MAXLEN;
163 # define CHAR_COLON L':'
165 p = StrChr(path, CHAR_COLON);
170 /* copy device part of the name */
171 for (d = dev ; path != p ;) {
172 if (--len == 0) return -1; /* too long */
180 find_device_byname(CHAR16 *dev)
183 for(i=0; i < ndev; i++) {
184 if (!StrCmp(dev, dev_tab[i].name)) return dev_tab+i;
191 * This function is used to walk through the device list and get names.
193 * - pidx = opaque cookie returned by previous call (must be zero for initial call)
194 * - type is the type of filesystem to look for, e.g., vfat or ext2fs. NULL means ANY
195 * - maxlen is the size in bytes of the returned string buffer
196 * - idx is the opaque cookie returned (can be reused by next call)
197 * - name a string buffer to hold the name of the next matching device
198 * - dev will receive the EFI_HANDLE corresponding to the device if not NULL
201 * - EFI_INVALID_PARAMETER: when NULL values are passed for required arguments.
202 * - EFI_NOT_FOUND: when reached the end of the list of invalid cookie
203 * - EFI_BUFFER_TOO_SMALL is device name does not fit into string buffer
204 * - EFI_SUCCESS otherwise
207 fops_get_next_device(UINTN pidx, CHAR16 *type, UINTN maxlen, UINTN *idx, CHAR16 *name, EFI_HANDLE *dev)
211 if (name == NULL || idx == NULL) return EFI_INVALID_PARAMETER;
213 for(i=pidx; i < ndev; i++) {
214 if (type == NULL || !StrCmp(dev_tab[i].fops->name, type)) goto found;
216 return EFI_NOT_FOUND;
218 if (StrLen(dev_tab[i].name) >= maxlen) {
219 *idx = pidx; /* will restart from same place! */
220 return EFI_BUFFER_TOO_SMALL;
222 StrCpy(name, dev_tab[i].name);
224 if (dev) *dev = dev_tab[i].dev;
230 fops_get_device_handle(CHAR16 *name, EFI_HANDLE *dev)
234 if (name == NULL || dev == NULL) return EFI_INVALID_PARAMETER;
236 for(i=0; i < ndev; i++) {
237 if (!StrCmp(dev_tab[i].name, name)) goto found;
239 return EFI_NOT_FOUND;
241 *dev = dev_tab[i].dev;
248 fops_open(CHAR16 *fullname, fops_fd_t *fd)
254 CHAR16 dev_name[FILEOPS_DEVNAME_MAXLEN];
258 if (fd == NULL || fullname == NULL || *fullname == CHAR_NULL || fops_split_path(fullname, dev_name) == -1)
259 return EFI_INVALID_PARAMETER;
261 DBG_PRT((L"fops_open(%s), dev:%s:", fullname ? fullname : L"(NULL)", dev_name));
264 if (dev_name[0] == CHAR_NULL) {
265 if (boot_dev == NULL) return EFI_INVALID_PARAMETER;
266 fops = boot_dev->fops;
268 if ((dev=find_device_byname(dev_name)) == NULL) return EFI_INVALID_PARAMETER;
270 name += StrLen(dev_name)+1;
275 if (fops == NULL) return EFI_INVALID_PARAMETER;
277 if ((f=fd_alloc()) == NULL) return EFI_OUT_OF_RESOURCES;
279 DBG_PRT((L"dev:%s: fullname:%s: name:%s: f=%d", dev_name, fullname, name, f - fd_tab));
281 status = fops->open(fops->intf, name, &f->fh);
282 if (EFI_ERROR(status)) goto error;
295 fops_read(fops_fd_t fd, VOID *buf, UINTN *size)
299 if (buf == NULL || FDESC_INVALID_FD(fd) || size == NULL) return EFI_INVALID_PARAMETER;
303 return f->fops->read(f->fops->intf, f->fh, buf, size);
307 fops_close(fops_fd_t fd)
312 if (FDESC_INVALID_FD(fd)) return EFI_INVALID_PARAMETER;
316 status = f->fops->close(f->fops->intf, f->fh);
324 fops_infosize(fops_fd_t fd, UINT64 *size)
328 if (FDESC_INVALID_FD(fd) || size == NULL) return EFI_INVALID_PARAMETER;
332 return f->fops->infosize(f->fops->intf, f->fh, size);
336 fops_seek(fops_fd_t fd, UINT64 newpos)
340 if (FDESC_INVALID_FD(fd)) return EFI_INVALID_PARAMETER;
344 return f->fops->seek(f->fops->intf, f->fh, newpos);
348 fops_setdefaults(struct config_file *defconf, CHAR16 *kname, UINTN maxlen, CHAR16 *devpath)
353 * The first default config file is architecture dependent. This is useful
354 * in case of network booting where the server is used for both types of
357 #if defined(CONFIG_ia64)
358 #define FILEOPS_ARCH_DEFAULT_CONFIG L"elilo-ia64.conf"
359 #elif defined (CONFIG_ia32)
360 #define FILEOPS_ARCH_DEFAULT_CONFIG L"elilo-ia32.conf"
362 #error "You need to specfy your default arch config file"
366 * last resort config file. Common to all architectures
368 #define FILEOPS_DEFAULT_CONFIG L"elilo.conf"
370 #define FILEOPS_DEFAULT_KERNEL L"vmlinux"
373 if (defconf == NULL || kname == NULL) return EFI_INVALID_PARAMETER;
376 for (i=0; i<MAX_DEFAULT_CONFIGS; i++) {
377 defconf[i].fname[0] = CHAR_NULL;
380 if (boot_dev == NULL || boot_dev->fops == NULL) {
381 if (boot_dev == NULL)
382 Print(L"Warning boot device not recognized\n");
384 Print(L"Unknown filesystem on boot device\n");
386 Print(L"Using builtin defaults for kernel and config file\n");
388 StrnCpy(kname, FILEOPS_DEFAULT_KERNEL, maxlen-1);
391 boot_dev->fops->setdefaults(boot_dev->fops->intf, defconf, kname, maxlen, devpath);
393 i=0; while (i<MAX_DEFAULT_CONFIGS && defconf[i].fname[0] != CHAR_NULL) i += 1;
395 if ((i+3) >= MAX_DEFAULT_CONFIGS) {
396 Print(L"ERROR: i = %d, MAX_DEFAULT_CONFIGS is not large enough\n", i);
397 return EFI_INVALID_PARAMETER;
400 StrnCpy(defconf[i].fname, FILEOPS_ARCH_DEFAULT_CONFIG, maxlen-1);
401 StrnCpy(defconf[i+1].fname, FILEOPS_DEFAULT_CONFIG, maxlen-1);
404 VERB_PRT(3,Print(L"Default config filename list:\n"));
405 for (i=0; i<MAX_DEFAULT_CONFIGS; i++) {
406 if (defconf[i].fname[0] == CHAR_NULL) { break; }
407 VERB_PRT(3,Print(L"\t%s\n", defconf[i].fname));
415 fops_getdefault_path(CHAR16 *path, UINTN maxlen)
417 if (path == NULL || maxlen == 0) return EFI_INVALID_PARAMETER;
420 * if underlying filesystem implements the call, then we call
421 * otherwise we return an empty string
423 if (boot_dev->fops->getdefault_path)
424 return boot_dev->fops->getdefault_path(path, maxlen);
432 fops_bootdev_name(VOID)
434 return boot_dev ? boot_dev->name : L"not supported";
438 add_dev_tab(EFI_GUID *proto, EFI_HANDLE boot_handle, UINTN size, fops_fs_glue_t glue)
445 if (size == 0) return 0;
447 tab = (EFI_HANDLE *)alloc(size, EfiLoaderData);
449 ERR_PRT((L"failed to allocate handle table"));
453 Memset(tab, 0, size);
456 * get the actual device handles now
458 status = BS->LocateHandle(ByProtocol, proto, NULL, &size, tab);
459 if (status != EFI_SUCCESS) {
460 ERR_PRT((L"failed to get handles for proto %g size=%d: %r", proto, size, status));
464 size /= sizeof(EFI_HANDLE);
466 for(i=0; i < size; i++) {
467 dev_tab[idx].dev = tab[i];
468 dev_tab[idx].fops = glue_filesystem(proto, tab[i], glue);
470 if (tab[i] == boot_handle) boot_dev = dev_tab+idx;
472 /* count the ones we recognized */
473 if (dev_tab[idx].fops) ndev_boot++;
475 /* assign a generic name for now */
476 dev_tab[idx].name[0] = L'd';
477 dev_tab[idx].name[1] = L'e';
478 dev_tab[idx].name[2] = L'v';
479 dev_tab[idx].name[3] = L'0' + idx/100;
480 dev_tab[idx].name[4] = L'0' + (idx%100)/10;
481 dev_tab[idx].name[5] = L'0' + (idx%100) % 10;
482 dev_tab[idx].name[6] = CHAR_NULL;
485 if (elilo_opt.debug) {
490 dp = DevicePathFromHandle(dev_tab[idx].dev);
491 if (dp) str = DevicePathToStr(dp);
493 str2 = str == NULL ? L"Unknown" : str;
495 DBG_PRT((L"%s : %-8s : %s\n", dev_tab[idx].name,
496 (dev_tab[idx].fops ? dev_tab[idx].fops->name: L"N/A"), str2));
498 if (str) FreePool(str);
506 /* remember actual number of bootable devices */
513 probe_devname_schemes(device_t *dev_tab, INTN ndev)
515 devname_scheme_t **p;
517 for (p = devname_scheme_tab; *p ; p++) {
518 if ((*p)->install_scheme(dev_tab, ndev) == 0) goto found;
520 ERR_PRT((L"No devname schemes worked, using builtin\n"));
523 VERB_PRT(3, Print(L"devname scheme: %s\n", (*p)->name));
524 current_devname_scheme = *p;
529 find_filesystems(EFI_HANDLE boot_handle)
531 UINTN size, total = 0;
535 * 1st pass, figure out how big a table we need
537 for(fs = fs_tab; *fs; fs++) {
539 BS->LocateHandle(ByProtocol, &(*fs)->proto, NULL, &size, NULL);
543 ERR_PRT((L"No useable filesystem found"));
546 total /= sizeof(EFI_HANDLE);
547 DBG_PRT((L"found %d filesystems", total));
549 dev_tab = (device_t *)alloc(total*sizeof(device_t), EfiLoaderData);
550 if (dev_tab == NULL) {
551 ERR_PRT((L"failed to allocate handle table"));
555 Memset(dev_tab, 0, total*sizeof(device_t));
558 * do a 2nd pass to initialize the table now
560 for(fs = fs_tab; *fs; fs++) {
563 BS->LocateHandle(ByProtocol, &(*fs)->proto, NULL, &size, NULL);
564 if (size == 0) continue;
566 add_dev_tab(&(*fs)->proto, boot_handle, size, (*fs)->glue);
568 probe_devname_schemes(dev_tab, ndev);
578 for (i=0; i < FILEOPS_FD_MAX-1; i++) {
579 fd_tab[i].next = &fd_tab[i+1];
581 fd_tab[i].next = NULL;
588 * both functions will go away once we go with boottime drivers
591 install_filesystems(VOID)
595 for(fs = fs_tab; *fs; fs++) (*fs)->install();
601 uninstall_filesystems(VOID)
605 for(fs = fs_tab; *fs; fs++) (*fs)->uninstall();
611 init_devices(EFI_HANDLE boot_handle)
613 /* simulate driver installation */
614 install_filesystems();
617 * now let's do our part
621 return find_filesystems(boot_handle);
629 for(i=0; i < FILEOPS_FD_MAX; i++) {
635 * simulate driver removal
637 uninstall_filesystems();
649 for(idx=0; idx< ndev; idx++) {
652 dp = DevicePathFromHandle(dev_tab[idx].dev);
653 if (dp) str = DevicePathToStr(dp);
655 str2 = str == NULL ? L"Unknown" : str;
657 Print(L"%8s : %-6s : %s\n", dev_tab[idx].name,
658 (dev_tab[idx].fops ? dev_tab[idx].fops->name: L"N/A"), str2);
660 if (str) FreePool(str);
662 Print(L"%d devices available for booting\n", ndev_boot);
664 if (boot_dev == NULL) {
665 Print(L"boot device not detected\n");
667 Print(L"boot device %s: %s\n", boot_dev->name,
668 (boot_dev->fops ? boot_dev->fops->name : L"No file access"));