diff --git a/stand/kboot/arch/amd64/Makefile.inc b/stand/kboot/arch/amd64/Makefile.inc index e2f5b9038ee6..fdd40fe9bc5a 100644 --- a/stand/kboot/arch/amd64/Makefile.inc +++ b/stand/kboot/arch/amd64/Makefile.inc @@ -1,5 +1,5 @@ -SRCS+= host_syscall.S amd64_tramp.S elf64_freebsd.c load_addr.c +SRCS+= host_syscall.S amd64_tramp.S elf64_freebsd.c load_addr.c fdt_arch.c CFLAGS+= -I${SYSDIR}/contrib/dev/acpica/include LDFLAGS= -nostdlib -static -T ${.CURDIR}/arch/${MACHINE_ARCH}/ldscript.amd64 diff --git a/stand/kboot/arch/amd64/fdt_arch.c b/stand/kboot/arch/amd64/fdt_arch.c new file mode 100644 index 000000000000..eefb76b1f92c --- /dev/null +++ b/stand/kboot/arch/amd64/fdt_arch.c @@ -0,0 +1,32 @@ +/*- + * Copyright (c) 2022 Netflix, Inc + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "kboot.h" + +void +fdt_arch_fixups(void *fdtp) +{ + /* amd64 doesn't pass in a FDT, so nothing to fix */ +} diff --git a/stand/kboot/arch/powerpc64/Makefile.inc b/stand/kboot/arch/powerpc64/Makefile.inc index 4c863553397c..c29e69970505 100644 --- a/stand/kboot/arch/powerpc64/Makefile.inc +++ b/stand/kboot/arch/powerpc64/Makefile.inc @@ -1,8 +1,8 @@ CFLAGS+= -mcpu=powerpc64 -SRCS+= ppc64_elf_freebsd.c host_syscall.S kerneltramp.S load_addr.c +SRCS+= ppc64_elf_freebsd.c host_syscall.S kerneltramp.S load_addr.c fdt_arch.c SRCS+= ucmpdi2.c LDFLAGS= -nostdlib -static -T ${.CURDIR}/arch/${MACHINE_ARCH}/ldscript.powerpc MK_PIE= no diff --git a/stand/kboot/kbootfdt.c b/stand/kboot/arch/powerpc64/fdt_arch.c similarity index 59% copy from stand/kboot/kbootfdt.c copy to stand/kboot/arch/powerpc64/fdt_arch.c index e4e8e7cfbf04..dc5f0fbd8fb6 100644 --- a/stand/kboot/kbootfdt.c +++ b/stand/kboot/arch/powerpc64/fdt_arch.c @@ -1,182 +1,89 @@ /*- * Copyright (C) 2014 Nathan Whitehorn * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -__FBSDID("$FreeBSD$"); - #include #include #include -#include "bootstrap.h" -#include "host_syscall.h" - -static void -add_node_to_fdt(void *buffer, const char *path, int fdt_offset) -{ - int child_offset, fd, pfd, error, dentsize; - char subpath[512]; - void *propbuf; - ssize_t proplen; - - char dents[2048]; - struct host_dirent64 *dent; - int d_type; - - fd = host_open(path, O_RDONLY, 0); - while (1) { - dentsize = host_getdents64(fd, dents, sizeof(dents)); - if (dentsize <= 0) - break; - for (dent = (struct host_dirent64 *)dents; - (char *)dent < dents + dentsize; - dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) { - sprintf(subpath, "%s/%s", path, dent->d_name); - if (strcmp(dent->d_name, ".") == 0 || - strcmp(dent->d_name, "..") == 0) - continue; - d_type = dent->d_type; - if (d_type == 4 /* DT_DIR */) { - child_offset = fdt_add_subnode(buffer, fdt_offset, - dent->d_name); - if (child_offset < 0) { - printf("Error %d adding node %s/%s, skipping\n", - child_offset, path, dent->d_name); - continue; - } - add_node_to_fdt(buffer, subpath, child_offset); - } else { - propbuf = malloc(1024); - proplen = 0; - pfd = host_open(subpath, O_RDONLY, 0); - if (pfd > 0) { - proplen = host_read(pfd, propbuf, 1024); - host_close(pfd); - } - error = fdt_setprop(buffer, fdt_offset, dent->d_name, - propbuf, proplen); - free(propbuf); - if (error) - printf("Error %d adding property %s to " - "node %d\n", error, dent->d_name, - fdt_offset); - } - } - } - - host_close(fd); -} +#include "kboot.h" /* Fix up wrong values added to the device tree by prom_init() in Linux */ -static void -fdt_linux_fixups(void *fdtp) +void +fdt_arch_fixups(void *fdtp) { int offset, len; const void *prop; /* * Remove /memory/available properties, which reflect long-gone OF * state */ offset = fdt_path_offset(fdtp, "/memory@0"); if (offset > 0) fdt_delprop(fdtp, offset, "available"); /* * Add reservations for OPAL and RTAS state if present */ offset = fdt_path_offset(fdtp, "/ibm,opal"); if (offset > 0) { const uint64_t *base, *size; base = fdt_getprop(fdtp, offset, "opal-base-address", &len); size = fdt_getprop(fdtp, offset, "opal-runtime-size", &len); if (base != NULL && size != NULL) fdt_add_mem_rsv(fdtp, fdt64_to_cpu(*base), fdt64_to_cpu(*size)); } offset = fdt_path_offset(fdtp, "/rtas"); if (offset > 0) { const uint32_t *base, *size; base = fdt_getprop(fdtp, offset, "linux,rtas-base", &len); size = fdt_getprop(fdtp, offset, "rtas-size", &len); if (base != NULL && size != NULL) fdt_add_mem_rsv(fdtp, fdt32_to_cpu(*base), fdt32_to_cpu(*size)); } /* * Patch up /chosen nodes so that the stored handles mean something, * where possible. */ offset = fdt_path_offset(fdtp, "/chosen"); if (offset > 0) { fdt_delprop(fdtp, offset, "cpu"); /* This node not meaningful */ offset = fdt_path_offset(fdtp, "/chosen"); prop = fdt_getprop(fdtp, offset, "linux,stdout-package", &len); if (prop != NULL) { fdt_setprop(fdtp, offset, "stdout", prop, len); offset = fdt_path_offset(fdtp, "/chosen"); fdt_setprop(fdtp, offset, "stdin", prop, len); } } } - -int -fdt_platform_load_dtb(void) -{ - void *buffer; - size_t buflen = 409600; - - buffer = malloc(buflen); - fdt_create_empty_tree(buffer, buflen); - add_node_to_fdt(buffer, "/proc/device-tree", - fdt_path_offset(buffer, "/")); - fdt_linux_fixups(buffer); - - fdt_pack(buffer); - - fdt_load_dtb_addr(buffer); - free(buffer); - - return (0); -} - -void -fdt_platform_load_overlays(void) -{ - -} - -void -fdt_platform_fixups(void) -{ - -} - diff --git a/stand/kboot/kboot.h b/stand/kboot/kboot.h index 679d645d8ff0..81bd18faa893 100644 --- a/stand/kboot/kboot.h +++ b/stand/kboot/kboot.h @@ -1,16 +1,20 @@ /*- * Copyright (c) 2022, Netflix, Inc. * * SPDX-License-Identifier: BSD-2-Clause */ #ifndef KBOOT_H #define KBOOT_H #define DEVT_HOSTDISK 1234 void do_init(void); + +/* Per-platform fdt fixup */ +void fdt_arch_fixups(void *fdtp); + uint64_t kboot_get_phys_load_segment(void); uint8_t kboot_get_kernel_machine_bits(void); #endif /* KBOOT_H */ diff --git a/stand/kboot/kbootfdt.c b/stand/kboot/kbootfdt.c index e4e8e7cfbf04..df9506af6773 100644 --- a/stand/kboot/kbootfdt.c +++ b/stand/kboot/kbootfdt.c @@ -1,182 +1,123 @@ /*- * Copyright (C) 2014 Nathan Whitehorn * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include "bootstrap.h" #include "host_syscall.h" +#include "kboot.h" static void add_node_to_fdt(void *buffer, const char *path, int fdt_offset) { int child_offset, fd, pfd, error, dentsize; char subpath[512]; void *propbuf; ssize_t proplen; char dents[2048]; struct host_dirent64 *dent; int d_type; fd = host_open(path, O_RDONLY, 0); while (1) { dentsize = host_getdents64(fd, dents, sizeof(dents)); if (dentsize <= 0) break; for (dent = (struct host_dirent64 *)dents; (char *)dent < dents + dentsize; dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) { sprintf(subpath, "%s/%s", path, dent->d_name); if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; d_type = dent->d_type; if (d_type == 4 /* DT_DIR */) { child_offset = fdt_add_subnode(buffer, fdt_offset, dent->d_name); if (child_offset < 0) { printf("Error %d adding node %s/%s, skipping\n", child_offset, path, dent->d_name); continue; } add_node_to_fdt(buffer, subpath, child_offset); } else { propbuf = malloc(1024); proplen = 0; pfd = host_open(subpath, O_RDONLY, 0); if (pfd > 0) { proplen = host_read(pfd, propbuf, 1024); host_close(pfd); } error = fdt_setprop(buffer, fdt_offset, dent->d_name, propbuf, proplen); free(propbuf); if (error) printf("Error %d adding property %s to " "node %d\n", error, dent->d_name, fdt_offset); } } } host_close(fd); } -/* Fix up wrong values added to the device tree by prom_init() in Linux */ - -static void -fdt_linux_fixups(void *fdtp) -{ - int offset, len; - const void *prop; - - /* - * Remove /memory/available properties, which reflect long-gone OF - * state - */ - - offset = fdt_path_offset(fdtp, "/memory@0"); - if (offset > 0) - fdt_delprop(fdtp, offset, "available"); - - /* - * Add reservations for OPAL and RTAS state if present - */ - - offset = fdt_path_offset(fdtp, "/ibm,opal"); - if (offset > 0) { - const uint64_t *base, *size; - base = fdt_getprop(fdtp, offset, "opal-base-address", - &len); - size = fdt_getprop(fdtp, offset, "opal-runtime-size", - &len); - if (base != NULL && size != NULL) - fdt_add_mem_rsv(fdtp, fdt64_to_cpu(*base), - fdt64_to_cpu(*size)); - } - offset = fdt_path_offset(fdtp, "/rtas"); - if (offset > 0) { - const uint32_t *base, *size; - base = fdt_getprop(fdtp, offset, "linux,rtas-base", &len); - size = fdt_getprop(fdtp, offset, "rtas-size", &len); - if (base != NULL && size != NULL) - fdt_add_mem_rsv(fdtp, fdt32_to_cpu(*base), - fdt32_to_cpu(*size)); - } - - /* - * Patch up /chosen nodes so that the stored handles mean something, - * where possible. - */ - offset = fdt_path_offset(fdtp, "/chosen"); - if (offset > 0) { - fdt_delprop(fdtp, offset, "cpu"); /* This node not meaningful */ - - offset = fdt_path_offset(fdtp, "/chosen"); - prop = fdt_getprop(fdtp, offset, "linux,stdout-package", &len); - if (prop != NULL) { - fdt_setprop(fdtp, offset, "stdout", prop, len); - offset = fdt_path_offset(fdtp, "/chosen"); - fdt_setprop(fdtp, offset, "stdin", prop, len); - } - } -} - int fdt_platform_load_dtb(void) { void *buffer; size_t buflen = 409600; buffer = malloc(buflen); fdt_create_empty_tree(buffer, buflen); add_node_to_fdt(buffer, "/proc/device-tree", fdt_path_offset(buffer, "/")); - fdt_linux_fixups(buffer); + fdt_arch_fixups(buffer); fdt_pack(buffer); fdt_load_dtb_addr(buffer); free(buffer); return (0); } void fdt_platform_load_overlays(void) { } void fdt_platform_fixups(void) { }