2 * Copyright (C) 2001-2003 Hewlett-Packard Co.
3 * Contributed by Stephane Eranian <eranian@hpl.hp.com>
4 * Copyright (C) 2006-2009 Intel Corporation
5 * Contributed by Fenghua Yu <fenghua.yu@intel.com>
6 * Contributed by Bibo Mao <bibo.mao@intel.com>
7 * Contributed by Chandramouli Narayanan <mouli@linux.intel.com>
9 * This file is part of the ELILO, the EFI Linux boot loader.
11 * ELILO is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * ELILO is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with ELILO; see the file COPYING. If not, write to the Free
23 * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 * Please check out the elilo.txt for complete documentation on how
27 * to use this program.
34 #include "fs/localfs.h"
37 * LocalFS is just a shim layer on top of the
38 * FileSystem Protocol which gives access to FAT32,16,12
40 #define FS_NAME L"vfat"
44 EFI_HANDLE dev; /* device we're attached to */
45 EFI_FILE_HANDLE volume; /* root of volume */
46 } localfs_priv_state_t;
48 #define LOCALFS_F2FD(f) ((UINTN)(f))
49 #define LOCALFS_FD2F(fd) ((EFI_FILE_HANDLE)(fd))
52 localfs_interface_t pub_intf;
54 localfs_interface_t pub_intf;
55 localfs_priv_state_t priv_data;
59 #define FS_PRIVATE(n) (&(((localfs_t *)n)->localfs_priv.priv_data))
62 static EFI_GUID LocalFsProtocol = LOCALFS_PROTOCOL;
72 static dev_tab_t *dev_tab; /* holds all devices we found */
73 static UINTN ndev; /* how many entries in dev_tab */
76 localfs_name(localfs_interface_t *this, CHAR16 *name, UINTN maxlen)
78 if (name == NULL || maxlen < 1) return EFI_INVALID_PARAMETER;
80 StrnCpy(name, FS_NAME, maxlen-1);
82 name[maxlen-1] = CHAR_NULL;
89 localfs_open(localfs_interface_t *this, CHAR16 *name, UINTN *fd)
91 localfs_priv_state_t *lfs;
95 if (this == NULL || name == NULL || fd == NULL) return EFI_INVALID_PARAMETER;
97 lfs = FS_PRIVATE(this);
99 DBG_PRT((L"localfs_open on %s\n", name));
101 status = uefi_call_wrapper(lfs->volume->Open, 5, lfs->volume, &fh, name, EFI_FILE_MODE_READ, 0);
102 if (status == EFI_SUCCESS) {
103 *fd = LOCALFS_F2FD(fh);
109 localfs_read(localfs_interface_t *this, UINTN fd, VOID *buf, UINTN *size)
111 localfs_priv_state_t *lfs;
113 if (this == NULL || fd == 0 || buf == NULL || size == NULL) return EFI_INVALID_PARAMETER;
115 lfs = FS_PRIVATE(this);
117 return uefi_call_wrapper(lfs->volume->Read, 3, LOCALFS_FD2F(fd), size, buf);
121 localfs_close(localfs_interface_t *this, UINTN fd)
123 localfs_priv_state_t *lfs;
125 if (this == NULL || fd == 0) return EFI_INVALID_PARAMETER;
127 lfs = FS_PRIVATE(this);
129 return uefi_call_wrapper(lfs->volume->Close, 1, LOCALFS_FD2F(fd));
133 localfs_infosize(localfs_interface_t *this, UINTN fd, UINT64 *sz)
135 localfs_priv_state_t *lfs;
138 if (this == NULL || fd == 0 || sz == NULL) return EFI_INVALID_PARAMETER;
140 lfs = FS_PRIVATE(this);
142 info = LibFileInfo(LOCALFS_FD2F(fd));
143 if (info == NULL) return EFI_UNSUPPORTED;
145 *sz = info->FileSize;
147 uefi_call_wrapper(BS->FreePool, 1, info);
153 localfs_seek(localfs_interface_t *this, UINTN fd, UINT64 newpos)
155 localfs_priv_state_t *lfs;
157 if (this == NULL || fd == 0) return EFI_INVALID_PARAMETER;
159 lfs = FS_PRIVATE(this);
161 return uefi_call_wrapper(lfs->volume->SetPosition, 2, LOCALFS_FD2F(fd), newpos);
165 localfs_init_state(localfs_t *localfs, EFI_HANDLE dev, EFI_FILE_HANDLE volume)
167 localfs_priv_state_t *lfs = FS_PRIVATE(localfs);
169 /* need to do some init here on localfs_intf */
170 Memset(localfs, 0, sizeof(*localfs));
172 localfs->pub_intf.localfs_name = localfs_name;
173 localfs->pub_intf.localfs_open = localfs_open;
174 localfs->pub_intf.localfs_read = localfs_read;
175 localfs->pub_intf.localfs_close = localfs_close;
176 localfs->pub_intf.localfs_infosize = localfs_infosize;
177 localfs->pub_intf.localfs_seek = localfs_seek;
180 lfs->volume = volume;
184 localfs_install_one(EFI_HANDLE dev, VOID **intf)
189 EFI_FILE_IO_INTERFACE *volume;
190 EFI_FILE_HANDLE volume_fh;
192 status = uefi_call_wrapper(BS->HandleProtocol, 3, dev, &LocalFsProtocol, (VOID **)&localfs);
193 if (status == EFI_SUCCESS) {
194 ERR_PRT((L"Warning: found existing %s protocol on device", FS_NAME));
198 status = uefi_call_wrapper(BS->HandleProtocol, 3, dev, &FileSystemProtocol, (VOID **)&volume);
199 if (EFI_ERROR(status)) return EFI_INVALID_PARAMETER;
201 status = uefi_call_wrapper(volume->OpenVolume, 2, volume, &volume_fh);
202 if (EFI_ERROR(status)) {
203 ERR_PRT((L"cannot open volume"));
207 localfs = (localfs_t *)alloc(sizeof(*localfs), EfiLoaderData);
208 if (localfs == NULL) {
209 ERR_PRT((L"failed to allocate %s", FS_NAME));
210 return EFI_OUT_OF_RESOURCES;
212 localfs_init_state(localfs, dev, volume_fh);
214 status = LibInstallProtocolInterfaces(&dev, &LocalFsProtocol, localfs, NULL);
215 if (EFI_ERROR(status)) {
216 ERR_PRT((L"Cannot install %s protocol: %r", FS_NAME, status));
221 if (intf) *intf = (VOID *)localfs;
224 { EFI_DEVICE_PATH *dp; CHAR16 *str;
225 dp = DevicePathFromHandle(dev);
226 str = DevicePathToStr(dp);
227 Print(L"attached %s to %s\n", FS_NAME, str);
228 uefi_call_wrapper(BS->FreePool, 1, str);
235 localfs_install(VOID)
242 uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &FileSystemProtocol, NULL, &size, NULL);
243 if (size == 0) return EFI_UNSUPPORTED; /* no device found, oh well */
245 DBG_PRT((L"size=%d", size));
247 dev_tab = (dev_tab_t *)alloc(size, EfiLoaderData);
248 if (dev_tab == NULL) {
249 ERR_PRT((L"failed to allocate handle table"));
250 return EFI_OUT_OF_RESOURCES;
252 status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &FileSystemProtocol, NULL, &size, (VOID **)dev_tab);
253 if (status != EFI_SUCCESS) {
254 ERR_PRT((L"failed to get handles: %r", status));
258 ndev = size / sizeof(EFI_HANDLE);
260 for(i=0; i < ndev; i++) {
262 localfs_install_one(dev_tab[i].dev, &intf);
263 /* override device handle with interface pointer */
264 dev_tab[i].intf = intf;
271 localfs_uninstall(VOID)
274 localfs_priv_state_t *lfs;
278 for(i=0; i < ndev; i++) {
279 if (dev_tab[i].intf == NULL) continue;
280 lfs = FS_PRIVATE(dev_tab[i].intf);
281 status = uefi_call_wrapper(BS->UninstallProtocolInterface, 3, lfs->dev, &LocalFsProtocol, dev_tab[i].intf);
282 if (EFI_ERROR(status)) {
283 ERR_PRT((L"Uninstall %s error: %r", FS_NAME, status));
287 { EFI_DEVICE_PATH *dp; CHAR16 *str;
288 dp = DevicePathFromHandle(lfs->dev);
289 str = DevicePathToStr(dp);
290 Print(L"uninstalled %s on %s\n", FS_NAME, str);
291 uefi_call_wrapper(BS->FreePool, 1, str);
293 free(dev_tab[i].intf);
295 if (dev_tab) free(dev_tab);