From f1364bb785575b152e1362ce66e0ead87c8ea23b Mon Sep 17 00:00:00 2001 From: krw Date: Fri, 25 Jun 2021 17:49:49 +0000 Subject: [PATCH] 1) Finish eliminating all uses of EFI_CALL() used in the tree, allowing for the removal of eficall.h files. 2) Allow booting from 4k-byte sector devices. 3) Don't leak memory after successfull i/o. The end result is that riscv64 efidev.c and efipxe.c are identical to the arm64/armv7 versions, efirng.c is identical to the amd64/arm64 versions and efiboot.c has only the arm64 -> riscv64 changes. ok kettenis@ --- sys/arch/riscv64/stand/efiboot/efiboot.c | 40 +++++++++--------- sys/arch/riscv64/stand/efiboot/efidev.c | 43 ++++++++++++------- sys/arch/riscv64/stand/efiboot/efipxe.c | 53 +++++++++++------------- sys/arch/riscv64/stand/efiboot/efirng.c | 7 ++-- 4 files changed, 74 insertions(+), 69 deletions(-) diff --git a/sys/arch/riscv64/stand/efiboot/efiboot.c b/sys/arch/riscv64/stand/efiboot/efiboot.c index 6564837fca7..564cece9a4a 100644 --- a/sys/arch/riscv64/stand/efiboot/efiboot.c +++ b/sys/arch/riscv64/stand/efiboot/efiboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: efiboot.c,v 1.1 2021/04/28 19:01:00 drahn Exp $ */ +/* $OpenBSD: efiboot.c,v 1.2 2021/06/25 17:49:49 krw Exp $ */ /* * Copyright (c) 2015 YASUOKA Masahiko @@ -41,7 +41,6 @@ #include "efidev.h" #include "efiboot.h" -#include "eficall.h" #include "fdt.h" EFI_SYSTEM_TABLE *ST; @@ -84,13 +83,13 @@ efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab) IH = image; /* disable reset by watchdog after 5 minutes */ - EFI_CALL(BS->SetWatchdogTimer, 0, 0, 0, NULL); + BS->SetWatchdogTimer(0, 0, 0, NULL); - status = EFI_CALL(BS->HandleProtocol, image, &imgp_guid, + status = BS->HandleProtocol(image, &imgp_guid, (void **)&imgp); if (status == EFI_SUCCESS) - status = EFI_CALL(BS->HandleProtocol, imgp->DeviceHandle, - &devp_guid, (void **)&dp); + status = BS->HandleProtocol(imgp->DeviceHandle, &devp_guid, + (void **)&dp); if (status == EFI_SUCCESS) efi_bootdp = dp; @@ -210,7 +209,7 @@ efi_heap_init(void) { EFI_STATUS status; - status = EFI_CALL(BS->AllocatePages, AllocateAnyPages, EfiLoaderData, + status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, EFI_SIZE_TO_PAGES(heapsiz), &heap); if (status != EFI_SUCCESS) panic("BS->AllocatePages()"); @@ -234,11 +233,11 @@ efi_diskprobe(void) TAILQ_INIT(&disklist); sz = 0; - status = EFI_CALL(BS->LocateHandle, ByProtocol, &blkio_guid, 0, &sz, 0); + status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, 0); if (status == EFI_BUFFER_TOO_SMALL) { handles = alloc(sz); - status = EFI_CALL(BS->LocateHandle, ByProtocol, &blkio_guid, - 0, &sz, handles); + status = BS->LocateHandle(ByProtocol, &blkio_guid, 0, &sz, + handles); } if (handles == NULL || EFI_ERROR(status)) return; @@ -256,7 +255,7 @@ efi_diskprobe(void) depth = 1; for (i = 0; i < sz / sizeof(EFI_HANDLE); i++) { - status = EFI_CALL(BS->HandleProtocol, handles[i], &blkio_guid, + status = BS->HandleProtocol(handles[i], &blkio_guid, (void **)&blkio); if (EFI_ERROR(status)) panic("BS->HandleProtocol() returns %d", status); @@ -269,7 +268,7 @@ efi_diskprobe(void) if (efi_bootdp == NULL || depth == -1 || bootdev != 0) goto next; - status = EFI_CALL(BS->HandleProtocol, handles[i], &devp_guid, + status = BS->HandleProtocol(handles[i], &devp_guid, (void **)&dp); if (EFI_ERROR(status)) goto next; @@ -382,7 +381,7 @@ efi_framebuffer(void) } } - status = EFI_CALL(BS->LocateProtocol, &gop_guid, NULL, (void **)&gop); + status = BS->LocateProtocol(&gop_guid, NULL, (void **)&gop); if (status != EFI_SUCCESS) return; @@ -624,7 +623,7 @@ efi_cleanup(void) for (retry = 1; retry >= 0; retry--) { efi_memprobe_internal(); /* sync the current map */ efi_updatefdt(); - status = EFI_CALL(BS->ExitBootServices, IH, mmap_key); + status = BS->ExitBootServices(IH, mmap_key); if (status == EFI_SUCCESS) break; if (retry == 0) @@ -882,12 +881,11 @@ efi_memprobe_internal(void) free(mmap, mmap_ndesc * mmap_descsiz); siz = 0; - status = EFI_CALL(BS->GetMemoryMap, &siz, NULL, &mapkey, &mmsiz, - &mmver); + status = BS->GetMemoryMap(&siz, NULL, &mapkey, &mmsiz, &mmver); if (status != EFI_BUFFER_TOO_SMALL) panic("cannot get the size of memory map"); mm = alloc(siz); - status = EFI_CALL(BS->GetMemoryMap, &siz, mm, &mapkey, &mmsiz, &mmver); + status = BS->GetMemoryMap(&siz, mm, &mapkey, &mmsiz, &mmver); if (status != EFI_SUCCESS) panic("cannot get the memory map"); n = siz / mmsiz; @@ -932,8 +930,8 @@ efi_memprobe_find(UINTN pages, UINTN align, EFI_PHYSICAL_ADDRESS *addr) if (paddr & (align - 1)) continue; - if (EFI_CALL(BS->AllocatePages, AllocateAddress, - EfiLoaderData, pages, &paddr) == EFI_SUCCESS) { + if (BS->AllocatePages(AllocateAddress, EfiLoaderData, + pages, &paddr) == EFI_SUCCESS) { *addr = paddr; return EFI_SUCCESS; } @@ -996,7 +994,7 @@ Xdtb_efi(void) int Xexit_efi(void) { - EFI_CALL(BS->Exit, IH, 0, 0, NULL); + BS->Exit(IH, 0, 0, NULL); for (;;) continue; return (0); @@ -1005,6 +1003,6 @@ Xexit_efi(void) int Xpoweroff_efi(void) { - EFI_CALL(RS->ResetSystem, EfiResetShutdown, EFI_SUCCESS, 0, NULL); + RS->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL); return (0); } diff --git a/sys/arch/riscv64/stand/efiboot/efidev.c b/sys/arch/riscv64/stand/efiboot/efidev.c index ac14ab5c3e3..5258249343c 100644 --- a/sys/arch/riscv64/stand/efiboot/efidev.c +++ b/sys/arch/riscv64/stand/efiboot/efidev.c @@ -1,4 +1,4 @@ -/* $OpenBSD: efidev.c,v 1.2 2021/06/02 22:44:27 krw Exp $ */ +/* $OpenBSD: efidev.c,v 1.3 2021/06/25 17:49:49 krw Exp $ */ /* * Copyright (c) 2015 YASUOKA Masahiko @@ -36,7 +36,6 @@ #include "libsa.h" #include -#include "eficall.h" extern EFI_BOOT_SERVICES *BS; @@ -75,43 +74,57 @@ efid_init(struct diskinfo *dip, void *handle) static EFI_STATUS efid_io(int rw, efi_diskinfo_t ed, u_int off, int nsect, void *buf) { - EFI_STATUS status = EFI_SUCCESS; + u_int blks, start, end; EFI_PHYSICAL_ADDRESS addr; + EFI_STATUS status; caddr_t data; + size_t size; - if (ed->blkio->Media->BlockSize != DEV_BSIZE) + /* block count of the intrinsic block size in DEV_BSIZE */ + blks = EFI_BLKSPERSEC(ed); + if (blks == 0) + /* block size < 512. HP Stream 13 actually has such a disk. */ return (EFI_UNSUPPORTED); + start = off / blks; + end = (off + nsect + blks - 1) / blks; + size = (end - start) * ed->blkio->Media->BlockSize; + status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, - EFI_SIZE_TO_PAGES(nsect * DEV_BSIZE), &addr); + EFI_SIZE_TO_PAGES(size), &addr); if (EFI_ERROR(status)) goto on_eio; data = (caddr_t)(uintptr_t)addr; switch (rw) { case F_READ: - status = EFI_CALL(ed->blkio->ReadBlocks, - ed->blkio, ed->mediaid, off, - nsect * DEV_BSIZE, data); + status = ed->blkio->ReadBlocks(ed->blkio, ed->mediaid, start, + size, data); if (EFI_ERROR(status)) goto on_eio; - memcpy(buf, data, nsect * DEV_BSIZE); + memcpy(buf, data + DEV_BSIZE * (off - start * blks), + DEV_BSIZE * nsect); break; case F_WRITE: if (ed->blkio->Media->ReadOnly) goto on_eio; - memcpy(data, buf, nsect * DEV_BSIZE); - status = EFI_CALL(ed->blkio->WriteBlocks, - ed->blkio, ed->mediaid, off, - nsect * DEV_BSIZE, data); + if (off % blks != 0 || nsect % blks != 0) { + status = ed->blkio->ReadBlocks(ed->blkio, ed->mediaid, + start, size, data); + if (EFI_ERROR(status)) + goto on_eio; + } + memcpy(data + DEV_BSIZE * (off - start * blks), buf, + DEV_BSIZE * nsect); + status = ed->blkio->WriteBlocks(ed->blkio, ed->mediaid, start, + size, data); if (EFI_ERROR(status)) goto on_eio; break; } - return (EFI_SUCCESS); on_eio: - BS->FreePages(addr, EFI_SIZE_TO_PAGES(nsect * DEV_BSIZE)); + BS->FreePages(addr, EFI_SIZE_TO_PAGES(size)); return (status); } diff --git a/sys/arch/riscv64/stand/efiboot/efipxe.c b/sys/arch/riscv64/stand/efiboot/efipxe.c index 7fe706e8167..2ac2a2feb73 100644 --- a/sys/arch/riscv64/stand/efiboot/efipxe.c +++ b/sys/arch/riscv64/stand/efiboot/efipxe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: efipxe.c,v 1.1 2021/04/28 19:01:00 drahn Exp $ */ +/* $OpenBSD: efipxe.c,v 1.2 2021/06/25 17:49:49 krw Exp $ */ /* * Copyright (c) 2017 Patrick Wildt * @@ -35,7 +35,6 @@ #include #include -#include "eficall.h" #include "efiboot.h" #include "disk.h" @@ -83,7 +82,7 @@ efi_pxeprobe(void) if (efi_bootdp == NULL) return; - status = EFI_CALL(BS->LocateHandleBuffer, ByProtocol, &pxe_guid, NULL, + status = BS->LocateHandleBuffer(ByProtocol, &pxe_guid, NULL, &nhandles, &handles); if (status != EFI_SUCCESS) return; @@ -91,8 +90,8 @@ efi_pxeprobe(void) for (i = 0; i < nhandles; i++) { EFI_PXE_BASE_CODE_DHCPV4_PACKET *dhcp; - status = EFI_CALL(BS->HandleProtocol, handles[i], - &devp_guid, (void **)&dp0); + status = BS->HandleProtocol(handles[i], &devp_guid, + (void **)&dp0); if (status != EFI_SUCCESS) continue; @@ -100,12 +99,12 @@ efi_pxeprobe(void) if (depth == -1 || efi_device_path_ncmp(efi_bootdp, dp0, depth)) continue; - status = EFI_CALL(BS->HandleProtocol, handles[i], &net_guid, + status = BS->HandleProtocol(handles[i], &net_guid, (void **)&net); if (status != EFI_SUCCESS) continue; - status = EFI_CALL(BS->HandleProtocol, handles[i], &pxe_guid, + status = BS->HandleProtocol(handles[i], &pxe_guid, (void **)&pxe); if (status != EFI_SUCCESS) continue; @@ -114,8 +113,8 @@ efi_pxeprobe(void) continue; if (pxe->Mtftp != NULL) { - status = EFI_CALL(pxe->Mtftp, NULL, 0, NULL, - FALSE, NULL, NULL, NULL, NULL, NULL, FALSE); + status = pxe->Mtftp(NULL, 0, NULL, FALSE, NULL, NULL, + NULL, NULL, NULL, FALSE); if (status != EFI_UNSUPPORTED) use_mtftp = 1; } @@ -165,8 +164,8 @@ mtftp_open(char *path, struct open_file *f) memset(tftpfile, 0, sizeof(*tftpfile)); memcpy(&dstip, &servip, sizeof(servip)); - status = EFI_CALL(PXE->Mtftp, PXE, EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE, - NULL, FALSE, &size, NULL, &dstip, path, NULL, FALSE); + status = PXE->Mtftp(PXE, EFI_PXE_BASE_CODE_TFTP_GET_FILE_SIZE, NULL, + FALSE, &size, NULL, &dstip, path, NULL, FALSE); if (status != EFI_SUCCESS) { free(tftpfile, sizeof(*tftpfile)); return ENOENT; @@ -176,7 +175,7 @@ mtftp_open(char *path, struct open_file *f) if (tftpfile->inbufsize == 0) goto out; - status = EFI_CALL(BS->AllocatePages, AllocateAnyPages, EfiLoaderData, + status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, EFI_SIZE_TO_PAGES(tftpfile->inbufsize), &addr); if (status != EFI_SUCCESS) { free(tftpfile, sizeof(*tftpfile)); @@ -184,7 +183,7 @@ mtftp_open(char *path, struct open_file *f) } tftpfile->inbuf = (unsigned char *)((paddr_t)addr); - status = EFI_CALL(PXE->Mtftp, PXE, EFI_PXE_BASE_CODE_TFTP_READ_FILE, + status = PXE->Mtftp(PXE, EFI_PXE_BASE_CODE_TFTP_READ_FILE, tftpfile->inbuf, FALSE, &size, NULL, &dstip, path, NULL, FALSE); if (status != EFI_SUCCESS) { free(tftpfile, sizeof(*tftpfile)); @@ -201,7 +200,7 @@ mtftp_close(struct open_file *f) struct mtftp_handle *tftpfile = f->f_fsdata; if (tftpfile->inbuf != NULL) - EFI_CALL(BS->FreePages, (paddr_t)tftpfile->inbuf, + BS->FreePages((paddr_t)tftpfile->inbuf, EFI_SIZE_TO_PAGES(tftpfile->inbufsize)); free(tftpfile, sizeof(*tftpfile)); return 0; @@ -328,14 +327,13 @@ tftpopen(struct open_file *f, ...) return 1; if (!use_mtftp) { - status = EFI_CALL(BS->AllocatePages, AllocateAnyPages, - EfiLoaderData, EFI_SIZE_TO_PAGES(RECV_SIZE), &txbuf); + status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, + EFI_SIZE_TO_PAGES(RECV_SIZE), &txbuf); if (status != EFI_SUCCESS) return ENOMEM; if ((tftpdev_sock = netif_open("efinet")) < 0) { - EFI_CALL(BS->FreePages, txbuf, - EFI_SIZE_TO_PAGES(RECV_SIZE)); + BS->FreePages(txbuf, EFI_SIZE_TO_PAGES(RECV_SIZE)); return ENXIO; } @@ -352,7 +350,7 @@ tftpclose(struct open_file *f) if (!use_mtftp) { ret = netif_close(*(int *)f->f_devdata); - EFI_CALL(BS->FreePages, txbuf, EFI_SIZE_TO_PAGES(RECV_SIZE)); + BS->FreePages(txbuf, EFI_SIZE_TO_PAGES(RECV_SIZE)); txbuf = 0; } @@ -417,19 +415,18 @@ efinet_init(struct iodesc *desc, void *v) return; if (net->Mode->State == EfiSimpleNetworkStopped) { - status = EFI_CALL(net->Start, net); + status = net->Start(net); if (status != EFI_SUCCESS) return; } if (net->Mode->State != EfiSimpleNetworkInitialized) { - status = EFI_CALL(net->Initialize, net, 0, 0); + status = net->Initialize(net, 0, 0); if (status != EFI_SUCCESS) return; } - EFI_CALL(net->ReceiveFilters, net, - EFI_SIMPLE_NETWORK_RECEIVE_UNICAST | + net->ReceiveFilters(net, EFI_SIMPLE_NETWORK_RECEIVE_UNICAST | EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST, 0, FALSE, 0, NULL); @@ -461,8 +458,7 @@ efinet_get(struct iodesc *desc, void *pkt, size_t len, time_t tmo) status = EFI_NOT_READY; while ((getsecs() - t) < tmo) { pktsz = bufsz; - status = EFI_CALL(net->Receive, net, NULL, &pktsz, ptr, - NULL, NULL, NULL); + status = net->Receive(net, NULL, &pktsz, ptr, NULL, NULL, NULL); if (status == EFI_SUCCESS) break; if (status != EFI_NOT_READY) @@ -493,14 +489,13 @@ efinet_put(struct iodesc *desc, void *pkt, size_t len) goto out; memcpy((void *)txbuf, pkt, len); - status = EFI_CALL(net->Transmit, net, 0, len, (void *)txbuf, - NULL, NULL, NULL); + status = net->Transmit(net, 0, len, (void *)txbuf, NULL, NULL, NULL); if (status != EFI_SUCCESS) goto out; buf = NULL; while (status == EFI_SUCCESS) { - status = EFI_CALL(net->GetStatus, net, NULL, &buf); + status = net->GetStatus(net, NULL, &buf); if (buf) break; } @@ -520,5 +515,5 @@ efinet_end(struct netif *nif) if (net == NULL) return; - EFI_CALL(net->Shutdown, net); + net->Shutdown(net); } diff --git a/sys/arch/riscv64/stand/efiboot/efirng.c b/sys/arch/riscv64/stand/efiboot/efirng.c index 1662e44cd39..03bf3e7008c 100644 --- a/sys/arch/riscv64/stand/efiboot/efirng.c +++ b/sys/arch/riscv64/stand/efiboot/efirng.c @@ -1,4 +1,4 @@ -/* $OpenBSD: efirng.c,v 1.1 2021/04/28 19:01:00 drahn Exp $ */ +/* $OpenBSD: efirng.c,v 1.2 2021/06/25 17:49:49 krw Exp $ */ /* * Copyright (c) 2018 Mark Kettenis @@ -21,7 +21,6 @@ #include #include -#include "eficall.h" #include "libsa.h" extern EFI_BOOT_SERVICES *BS; @@ -68,13 +67,13 @@ fwrandom(char *buf, size_t buflen) size_t i; int ret = 0; - status = EFI_CALL(BS->LocateProtocol, &rng_guid, NULL, (void **)&rng); + status = BS->LocateProtocol(&rng_guid, NULL, (void **)&rng); if (rng == NULL || EFI_ERROR(status)) return -1; random = alloc(buflen); - status = EFI_CALL(rng->GetRNG, rng, NULL, buflen, random); + status = rng->GetRNG(rng, NULL, buflen, random); if (EFI_ERROR(status)) { printf("RNG GetRNG() failed (%d)\n", status); ret = -1; -- 2.20.1