Mercurial > hg > Members > taiki > EFIKernel
view boot/bootx64.c @ 18:bd4c99e700e8
add close and read, seek
author | taiki |
---|---|
date | Thu, 07 Feb 2013 06:11:26 +0900 |
parents | 2013da6b3211 |
children | 2fbe46f63d4c |
line wrap: on
line source
/* * This program is EFI boot loader. * load Mach-O kernel, and locate on memory, and jump kernel. * * Author: Taiki TAIRA. */ #include "bootx64.h" #include "mach-o/mach_o.h" INTN efi_error(CHAR16* error_massage, EFI_STATUS status) { Print(L"%s", error_massage); if (status < 0) return ERROR; return SUCCESS; } EFI_STATUS open(CHAR16 *name, EFI_FILE_HANDLE *fd, fs_t *fs) { EFI_STATUS status; if (name == NULL || fd == NULL) return EFI_INVALID_PARAMETER; status = uefi_call_wrapper(fs->volume->Open, 5, fs->volume, &fd, name, EFI_FILE_MODE_READ, (UINT64)0); return status; } EFI_STATUS close(fs_t *fs, EFI_FILE_HANDLE *fd) { if (fs == NULL || fd == NULL) return EFI_INVALID_PARAMETER; return uefi_call_wrapper(fs->volume->Close, 1, fd); } EFI_STATUS read(fs_t *fs, EFI_FILE_HANDLE *fd, VOID *buf, UINTN *size) { if (buf == NULL || fd == NULL || size == NULL) return EFI_INVALID_PARAMETER; return uefi_call_wrapper(fs->volume->Read, 3, fd, size, buf); } EFI_STATUS seek(fs_t *fs, EFI_FILE_HANDLE fd, UINTN newpos) { if (newpos <= 0 || fd == NULL || fs == NULL) return EFI_INVALID_PARAMETER; return uefi_call_wrapper(fs->volume->SetPosition, 2, fd, newpos); } EFI_STATUS config_fs_one(EFI_HANDLE dev, VOID *fs) { EFI_STATUS status; EFI_FILE_IO_INTERFACE *volume; EFI_FILE_HANDLE volume_fh; EFI_GUID LocalFsProtocol = LOCALFS_PROTOCOL; status = uefi_call_wrapper(BS->HandleProtocol, 3, dev, &FileSystemProtocol, (VOID **)&volume); if (EFI_ERROR(status)) return EFI_INVALID_PARAMETER; /* alloc */ status = uefi_call_wrapper(volume->OpenVolume, 2, volume, &volume_fh); if (EFI_ERROR(status)) { Print(L"Can not open volume.\n"); return status; } Print(L"Open volume.\n"); fs_t *fs_tmp = (fs_t *)fs; SetMem(fs, sizeof(fs_t), 0); fs_tmp->dev = dev; fs_tmp->volume = volume_fh; status = LibInstallProtocolInterfaces(&dev, &LocalFsProtocol, fs, NULL); if (EFI_ERROR(status)) return status; /* free */ return EFI_SUCCESS; } EFI_STATUS config_fs(EFI_HANDLE boot_handle, fs_t *fs, dev_tab_t *boot_dev) { UINTN size = 0; UINTN i; EFI_GUID *proto = NULL; Print(L"configure filesystems for all volume. \n"); uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &FileSystemProtocol, NULL, &size, NULL); if (size == 0) return EFI_UNSUPPORTED; /* alloc */ dev_tab_t *dev_tab = NULL; // all devices EFI_STATUS status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &FileSystemProtocol, NULL, &size, (VOID **)dev_tab); if (status != EFI_SUCCESS) { efi_error(L"can't get handler.\n", status); /* free */ return status; } UINTN ndev = size / sizeof(EFI_HANDLE); for (i = 0; i < ndev; i++) { VOID *fs = NULL; config_fs_one(dev_tab[i].dev, &fs); dev_tab[i].fs = fs; } status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, proto, NULL, &size, NULL); if (EFI_ERROR(status)) { Print(L"No useable filesystem found.\n"); return status; } /* alloc */ SetMem(proto, sizeof(EFI_HANDLE), 0); uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, &proto, NULL, &size, NULL); EFI_HANDLE *tab = NULL; SetMem(tab, size, 0); status = uefi_call_wrapper(BS->LocateHandle, 5, ByProtocol, proto, NULL, &size, tab); if (status != EFI_SUCCESS) { Print(L"faild to get handles\n"); } size /= sizeof(EFI_HANDLE); UINTN idx = 0; for (i=0; i<size; i++) { dev_tab[idx].dev = tab[i]; if (tab[i] == boot_handle) boot_dev = dev_tab + idx; idx++; } return EFI_SUCCESS; } EFI_STATUS load(fs_t *fs, EFI_FILE_HANDLE *fd, CHAR16 *kname) { VOID *buf = NULL; UINTN size; read(fs ,fd, buf, &size); //seek(fs ,fd); return EFI_SUCCESS; } static inline void start_kernel() { } /* start cbc kernel boot loader. */ EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *system_table) { InitializeLib(image, system_table); EFI_LOADED_IMAGE *info; CHAR16 *kname = L"kernel"; Print(L"Boot start. %s\n", kname); uefi_call_wrapper(BS->SetWatchdogTimer, 4, 0, 0x0, 0, NULL); Print(L"Set watchdog timer.\n"); EFI_STATUS status = uefi_call_wrapper(BS->HandleProtocol, 3, image, &LoadedImageProtocol, (VOID **) &info); if (EFI_ERROR(status)) { Print(L"Load error.\n"); return EFI_LOAD_ERROR; } fs_t fs; dev_tab_t boot_dev; status = config_fs(info->DeviceHandle, &fs, &boot_dev); EFI_FILE_HANDLE fd; open(kname, &fd, &fs); load(&fs, &fd, kname); close(&fs, &fd); UINTN cookie = 0; uefi_call_wrapper(BS->ExitBootServices, 2, image, cookie); start_kernel(); status = EFI_SUCCESS; return status; }