2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
4 * Contributed by Fenghua Yu <fenghua.yu@intel.com>
5 * Contributed by Bibo Mao <bibo.mao@intel.com>
6 * Contributed by Chandramouli Narayanan <mouli@linux.intel.com>
8 * This file is part of the ELILO, the EFI Linux boot loader.
10 * ELILO is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * ELILO is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with ELILO; see the file COPYING. If not, write to the Free
22 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 * Please check out the elilo.txt for complete documentation on how
26 * to use this program.
39 #include "glue_localfs.h"
42 #include "glue_netfs.h"
46 #include "glue_ext2fs.h"
50 * table of device naming schemes.
51 * we will probe each one in sequential order and stop at first match.
53 extern devname_scheme_t simple_devname_scheme;
55 static devname_scheme_t *devname_scheme_tab[]={
56 &simple_devname_scheme,
62 * Once we are able to create Boot Services drivers we won't need
63 * this hack and we'll be able to load the driver from the boot manager
64 * or EFI shell. So for now we explicitely invoke the probe routine
65 * of each driver that we know about
68 typedef EFI_STATUS (fops_fixups_t)(EFI_HANDLE dev, device_t *d);
71 * List of filesystem we currently support
75 static fileops_fs_t *fs_tab[]={
88 static device_t *dev_tab;
89 static UINTN ndev, ndev_boot;
90 static device_t *boot_dev;
92 typedef struct _fdesc_t {
93 struct _fdesc_t *next; /* pointer to next free (when in free list) */
94 fileops_t *fops; /* pointer to filesystem-specific glue interface */
95 UINTN fh; /* file descriptor from lower level protocol */
98 #define FILEOPS_FD_MAX 16
100 static fdesc_t fd_tab[FILEOPS_FD_MAX];
101 static fdesc_t *free_fd;
103 static devname_scheme_t *current_devname_scheme;
105 #define FDESC_IDX(f) (fops_fd_t)(f-fd_tab)
106 #define FDESC_INVALID_FD(fd) (fd >= FILEOPS_FD_MAX || fd_tab[(fd)].fops == NULL)
107 #define FDESC_F(fd) (fd_tab+(fd))
114 if (free_fd == NULL) {
115 ERR_PRT((L"out of file descriptor"));
118 free_fd = free_fd->next;
127 ERR_PRT((L"invalid fd"));
129 fd->fops = NULL; /* mark as invalid */
135 glue_filesystem(EFI_GUID *proto, EFI_HANDLE dev, fops_fs_glue_t glue)
141 status = uefi_call_wrapper(BS->HandleProtocol, 3, dev, proto, &intf);
142 if (EFI_ERROR(status)) {
143 ERR_PRT((L"unable to locate %g: should not happen", proto));
144 return NULL; /* should not happen */
146 fops = (fileops_t *)alloc(sizeof(*fops), EfiLoaderData);
148 ERR_PRT((L"failed to allocate fileops"));
149 return NULL; /* XXX uninstall protocol */
151 Memset(fops, 0, sizeof(*fops));
155 /* initialize private interface now */
162 fops_split_path(CHAR16 *path, CHAR16 *dev)
165 UINTN len = FILEOPS_DEVNAME_MAXLEN;
166 # define CHAR_COLON L':'
168 p = StrChr(path, CHAR_COLON);
173 /* copy device part of the name */
174 for (d = dev ; path != p ;) {
175 if (--len == 0) return -1; /* too long */
183 find_device_byname(CHAR16 *dev)
186 for(i=0; i < ndev; i++) {
187 if (!StrCmp(dev, dev_tab[i].name)) return dev_tab+i;
194 * This function is used to walk through the device list and get names.
196 * - pidx = opaque cookie returned by previous call (must be zero for initial call)
197 * - type is the type of filesystem to look for, e.g., vfat or ext2fs. NULL means ANY
198 * - maxlen is the size in bytes of the returned string buffer
199 * - idx is the opaque cookie returned (can be reused by next call)
200 * - name a string buffer to hold the name of the next matching device
201 * - dev will receive the EFI_HANDLE corresponding to the device if not NULL
204 * - EFI_INVALID_PARAMETER: when NULL values are passed for required arguments.
205 * - EFI_NOT_FOUND: when reached the end of the list of invalid cookie
206 * - EFI_BUFFER_TOO_SMALL is device name does not fit into string buffer
207 * - EFI_SUCCESS otherwise
210 fops_get_next_device(UINTN pidx, CHAR16 *type, UINTN maxlen, UINTN *idx, CHAR16 *name, EFI_HANDLE *dev)
214 if (name == NULL || idx == NULL) return EFI_INVALID_PARAMETER;
216 for(i=pidx; i < ndev; i++) {
217 if (type == NULL || !StrCmp(dev_tab[i].fops->name, type)) goto found;
219 return EFI_NOT_FOUND;
221 if (StrLen(dev_tab[i].name) >= maxlen) {
222 *idx = pidx; /* will restart from same place! */
223 return EFI_BUFFER_TOO_SMALL;
225 StrCpy(name, dev_tab[i].name);
227 if (dev) *dev = dev_tab[i].dev;
233 fops_get_device_handle(CHAR16 *name, EFI_HANDLE *dev)
237 if (name == NULL || dev == NULL) return EFI_INVALID_PARAMETER;
239 for(i=0; i < ndev; i++) {
240 if (!StrCmp(dev_tab[i].name, name)) goto found;
242 return EFI_NOT_FOUND;
244 *dev = dev_tab[i].dev;
251 fops_open(CHAR16 *fullname, fops_fd_t *fd)
257 CHAR16 dev_name[FILEOPS_DEVNAME_MAXLEN];
261 if (fd == NULL || fullname == NULL || *fullname == CHAR_NULL || fops_split_path(fullname, dev_name) == -1)
262 return EFI_INVALID_PARAMETER;
264 DBG_PRT((L"fops_open(%s), dev:%s:", fullname ? fullname : L"(NULL)", dev_name));
267 if (dev_name[0] == CHAR_NULL) {
268 if (boot_dev == NULL) return EFI_INVALID_PARAMETER;
269 fops = boot_dev->fops;
271 if ((dev=find_device_byname(dev_name)) == NULL) return EFI_INVALID_PARAMETER;
273 name += StrLen(dev_name)+1;
278 if (fops == NULL) return EFI_INVALID_PARAMETER;
280 if ((f=fd_alloc()) == NULL) return EFI_OUT_OF_RESOURCES;
282 DBG_PRT((L"dev:%s: fullname:%s: name:%s: f=%d", dev_name, fullname, name, f - fd_tab));
284 status = fops->open(fops->intf, name, &f->fh);
285 if (EFI_ERROR(status)) goto error;
298 fops_read(fops_fd_t fd, VOID *buf, UINTN *size)
302 if (buf == NULL || FDESC_INVALID_FD(fd) || size == NULL) return EFI_INVALID_PARAMETER;
306 return f->fops->read(f->fops->intf, f->fh, buf, size);
310 fops_close(fops_fd_t fd)
315 if (FDESC_INVALID_FD(fd)) return EFI_INVALID_PARAMETER;
319 status = f->fops->close(f->fops->intf, f->fh);
327 fops_infosize(fops_fd_t fd, UINT64 *size)
331 if (FDESC_INVALID_FD(fd) || size == NULL) return EFI_INVALID_PARAMETER;
335 return f->fops->infosize(f->fops->intf, f->fh, size);
339 fops_seek(fops_fd_t fd, UINT64 newpos)
343 if (FDESC_INVALID_FD(fd)) return EFI_INVALID_PARAMETER;
347 return f->fops->seek(f->fops->intf, f->fh, newpos);
351 fops_setdefaults(struct config_file *defconf, CHAR16 *kname, UINTN maxlen, CHAR16 *devpath)
356 * The first default config file is architecture dependent. This is useful
357 * in case of network booting where the server is used for both types of
360 #if defined(CONFIG_ia64)
361 #define FILEOPS_ARCH_DEFAULT_CONFIG L"elilo-ia64.conf"
362 #elif defined (CONFIG_ia32)
363 #define FILEOPS_ARCH_DEFAULT_CONFIG L"elilo-ia32.conf"
364 #elif defined (CONFIG_x86_64)
365 #define FILEOPS_ARCH_DEFAULT_CONFIG L"elilo-x86_64.conf"
367 #error "You need to specfy your default arch config file"
371 * last resort config file. Common to all architectures
373 #define FILEOPS_DEFAULT_CONFIG L"elilo.conf"
375 #define FILEOPS_DEFAULT_KERNEL L"vmlinux"
378 if (defconf == NULL || kname == NULL) return EFI_INVALID_PARAMETER;
381 for (i=0; i<MAX_DEFAULT_CONFIGS; i++) {
382 defconf[i].fname[0] = CHAR_NULL;
385 if (boot_dev == NULL || boot_dev->fops == NULL) {
386 if (boot_dev == NULL)
387 Print(L"Warning boot device not recognized\n");
389 Print(L"Unknown filesystem on boot device\n");
391 Print(L"Using builtin defaults for kernel and config file\n");
393 StrnCpy(kname, FILEOPS_DEFAULT_KERNEL, maxlen-1);
396 boot_dev->fops->setdefaults(boot_dev->fops->intf, defconf, kname, maxlen, devpath);
398 i=0; while (i<MAX_DEFAULT_CONFIGS && defconf[i].fname[0] != CHAR_NULL) i += 1;
400 if ((i+3) >= MAX_DEFAULT_CONFIGS) {
401 Print(L"ERROR: i = %d, MAX_DEFAULT_CONFIGS is not large enough\n", i);
402 return EFI_INVALID_PARAMETER;
405 StrnCpy(defconf[i].fname, FILEOPS_ARCH_DEFAULT_CONFIG, maxlen-1);
406 StrnCpy(defconf[i+1].fname, FILEOPS_DEFAULT_CONFIG, maxlen-1);
409 VERB_PRT(3,Print(L"Default config filename list:\n"));
410 for (i=0; i<MAX_DEFAULT_CONFIGS; i++) {
411 if (defconf[i].fname[0] == CHAR_NULL) { break; }
412 VERB_PRT(3,Print(L"\t%s\n", defconf[i].fname));
420 fops_getdefault_path(CHAR16 *path, UINTN maxlen)
422 if (path == NULL || maxlen == 0) return EFI_INVALID_PARAMETER;
425 * if underlying filesystem implements the call, then we call
426 * otherwise we return an empty string
428 if (boot_dev->fops->getdefault_path)
429 return boot_dev->fops->getdefault_path(path, maxlen);
437 fops_bootdev_name(VOID)
439 return boot_dev ? boot_dev->name : L"not supported";
443 add_dev_tab(EFI_GUID *proto, EFI_HANDLE boot_handle, UINTN size, fops_fs_glue_t glue)
450 if (size == 0) return 0;
452 tab = (EFI_HANDLE *)alloc(size, EfiLoaderData);
454 ERR_PRT((L"failed to allocate handle table"));
458 Memset(tab, 0, size);
461 * get the actual device handles now
463 status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, proto, NULL, &size, tab);
464 if (status != EFI_SUCCESS) {
465 ERR_PRT((L"failed to get handles for proto %g size=%d: %r", proto, size, status));
469 size /= sizeof(EFI_HANDLE);
471 for(i=0; i < size; i++) {
472 dev_tab[idx].dev = tab[i];
473 dev_tab[idx].fops = glue_filesystem(proto, tab[i], glue);
475 if (tab[i] == boot_handle) boot_dev = dev_tab+idx;
477 /* count the ones we recognized */
478 if (dev_tab[idx].fops) ndev_boot++;
480 /* assign a generic name for now */
481 dev_tab[idx].name[0] = L'd';
482 dev_tab[idx].name[1] = L'e';
483 dev_tab[idx].name[2] = L'v';
484 dev_tab[idx].name[3] = L'0' + idx/100;
485 dev_tab[idx].name[4] = L'0' + (idx%100)/10;
486 dev_tab[idx].name[5] = L'0' + (idx%100) % 10;
487 dev_tab[idx].name[6] = CHAR_NULL;
490 if (elilo_opt.debug) {
495 dp = DevicePathFromHandle(dev_tab[idx].dev);
496 if (dp) str = DevicePathToStr(dp);
498 str2 = str == NULL ? L"Unknown" : str;
500 DBG_PRT((L"%s : %-8s : %s\n", dev_tab[idx].name,
501 (dev_tab[idx].fops ? dev_tab[idx].fops->name: L"N/A"), str2));
503 if (str) FreePool(str);
511 /* remember actual number of bootable devices */
518 probe_devname_schemes(device_t *dev_tab, INTN ndev)
520 devname_scheme_t **p;
522 for (p = devname_scheme_tab; *p ; p++) {
523 if ((*p)->install_scheme(dev_tab, ndev) == 0) goto found;
525 ERR_PRT((L"No devname schemes worked, using builtin\n"));
528 VERB_PRT(3, Print(L"devname scheme: %s\n", (*p)->name));
529 current_devname_scheme = *p;
534 find_filesystems(EFI_HANDLE boot_handle)
536 UINTN size, total = 0;
540 * 1st pass, figure out how big a table we need
542 for(fs = fs_tab; *fs; fs++) {
544 uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &(*fs)->proto, NULL, &size, NULL);
548 ERR_PRT((L"No useable filesystem found"));
551 total /= sizeof(EFI_HANDLE);
552 DBG_PRT((L"found %d filesystems", total));
554 dev_tab = (device_t *)alloc(total*sizeof(device_t), EfiLoaderData);
555 if (dev_tab == NULL) {
556 ERR_PRT((L"failed to allocate handle table"));
560 Memset(dev_tab, 0, total*sizeof(device_t));
563 * do a 2nd pass to initialize the table now
565 for(fs = fs_tab; *fs; fs++) {
568 uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &(*fs)->proto, NULL, &size, NULL);
569 if (size == 0) continue;
571 add_dev_tab(&(*fs)->proto, boot_handle, size, (*fs)->glue);
573 probe_devname_schemes(dev_tab, ndev);
583 for (i=0; i < FILEOPS_FD_MAX-1; i++) {
584 fd_tab[i].next = &fd_tab[i+1];
586 fd_tab[i].next = NULL;
593 * both functions will go away once we go with boottime drivers
596 install_filesystems(VOID)
600 for(fs = fs_tab; *fs; fs++) (*fs)->install();
606 uninstall_filesystems(VOID)
610 for(fs = fs_tab; *fs; fs++) (*fs)->uninstall();
616 init_devices(EFI_HANDLE boot_handle)
618 /* simulate driver installation */
619 install_filesystems();
622 * now let's do our part
626 return find_filesystems(boot_handle);
634 for(i=0; i < FILEOPS_FD_MAX; i++) {
640 * simulate driver removal
642 uninstall_filesystems();
654 for(idx=0; idx< ndev; idx++) {
657 dp = DevicePathFromHandle(dev_tab[idx].dev);
658 if (dp) str = DevicePathToStr(dp);
660 str2 = str == NULL ? L"Unknown" : str;
662 Print(L"%8s : %-6s : %s\n", dev_tab[idx].name,
663 (dev_tab[idx].fops ? dev_tab[idx].fops->name: L"N/A"), str2);
665 if (str) FreePool(str);
667 Print(L"%d devices available for booting\n", ndev_boot);
669 if (boot_dev == NULL) {
670 Print(L"boot device not detected\n");
672 Print(L"boot device %s: %s\n", boot_dev->name,
673 (boot_dev->fops ? boot_dev->fops->name : L"No file access"));