Commit Graph

156 Commits

Author SHA1 Message Date
Andrew Turner
8e82c5e28d stand/kboot: Fix the linker script OUTPUT_FORMAT
ld.bfd doesn't understand elf64-aarch64 but does have
elf64-littleaarch64. Switch to this so we can link kboot with it.

While here switch to the single format version. We are unlikely to
support booting from a big-endian Linux.

Reviewed by:	imp, emaste
Sponsored by:	Arm Ltd
Differential Revision:	https://reviews.freebsd.org/D45258
2024-05-22 08:17:52 +00:00
Warner Losh
f989ebd4de kboot: Use C99 initialiers for hostconsole.
Sponsored by:		Netflix
2024-05-19 22:05:41 -06:00
Warner Losh
0111f09ca6 kboot: Initialize hostfs_root sooner (and remove kboot.conf)
Move the initialization of hostfs_root to be a bit sooner. While it
doesn't matter for the default case, we may want to use hostfs files
sooner.

Also, while we're here, remove kboot.conf. It duplicates the command
line and has proven difficult to use. It will be replaced by an early
script that can influence the state of the boot loader before we select
a device to boot from (including strongly suggesting which one to boot
from).

Sponsored by:		Netflix
2024-05-19 22:05:41 -06:00
Warner Losh
1d7bdae9ca kboot: Move console, acpi and smbios init
Move the console probing to as early as possible. There's no real
support for anything but hostcons, and setting it up early will show
other error messages.

ACPI and SMBIOS probing can be done just after we have the console, so
move it there. This allows other parts of the early code to use info
from that, as well as overriding and env vars set by these things on the
command line (smbios data may be wrong during initial development phases
as the automated way to populate per-board data may not be established,
etc).

Sponsored by:		Netflix
2024-05-19 22:05:40 -06:00
Warner Losh
462af7676b kboot: kbootfdt: fix error handling
If we are able to open /sys/firmware/fdt, but aren't able to read it,
fall back to /proc/device-tree. Remove comment that's not really true,
it turns out.

Sponsored by:		Netflix
2024-03-11 15:21:52 -06:00
Warner Losh
d75524b3fe kboot: Use is_linux_error to check mmap return error
Rather than checking against the (incorrect) -511, use the
is_linux_error() function to check to see if host_mmap failed.

Sponsored by:		Netflix
2024-03-11 15:21:52 -06:00
Warner Losh
a9cd3b675e kboot: Print UEFI memory map
If we can read the UEFI memory map, go ahead and print the memory map.
While the kernel prints this with bootverbose, having it at this stage
is useful for debugging other problems.

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D44287
2024-03-11 15:21:52 -06:00
Warner Losh
d650c3efb6 kboot: hostfs -- check for llseek failure correctly
The host_* syscalls are all raw Linux system calls, not the POSIX
wrappers that glibc / musl create. So we have to ranage change the
return value of host_llseek correctly to use the negative value hack
that all Linux system calls use.

This fixes a false positive error detection when we do something like
lseek(fd, 0xf1234567, ...); This returns 0xf1234567, which is a negative
value which used to trigger the error path.  Instead, we check using the
is_linux_error() and store the return value in a long. Translate that
errno to a host errno and set the global errno to that and return
-1. lseek can't otherwise return a negative number, since it's the
offset after seeking into the file, which by definition is positive.

This kept the 'read the UEFI memory map out of physical memory' from
working on aarch64 (whose boot loader falls back to reading it since
there are restrictive kernel options that can also prevent it), since
the physical address the memory map was at on my platform was like
0xfa008018.

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D44286
2024-03-11 15:21:51 -06:00
Warner Losh
8b1925f29c kboot: Avoid UB in signed shift
offset is signed. Copy it to the unsigned res before shifting. This
avoids any possible undefined behavior for right shifting signed
numbers. No functional change intended (and the code generated is the
nearly same for aarch64).

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D44285
2024-03-11 15:21:51 -06:00
Warner Losh
3ae18fdfbc kboot: Create function for error checking.
Linux has the convention of returning -ERRNO to flag errors from its
system calls. Sometimes other negative values are returned that are
success...  However, only values -1 to -4096 (inclusive) are really
errors. The rest are either truncated values that only look negative (so
use long instead of int), or are things like addresses or legal unsigned
file offsets or similar that are successful returns. Filter out the
latter.

Sponsored by:		Netflix
2024-03-11 15:21:51 -06:00
Warner Losh
ec6cbe468f loader/kboot: simplify linker set inclusion a little
Linker set sections are included by default. No need to do so
explicitly.  These were bogusly copied from the efi ldscripts. They were
there due to a workaround introduced in 2002 by peter@ for a gcc
upgrade, but whatever bugs necessitated it were filed by 2010 when
rpaulo@ imported the i386 support (though they were copied even though
the objcopy retained them correctly, the gcc bug having been
fixed). They've never been needed.

Sponsored by:		Netflix
Reviewed by:		tsoome, kevans
Differential Revision:	https://reviews.freebsd.org/D44060
2024-02-24 17:56:56 -07:00
Warner Losh
acb474c084 kboot: Centralize ldscript addition
Make the pattern for ldscripts always be
arch/$MACHINE_ARCH/$MACHINE_ARCH.ldscript so we can add it from a
central Makefile. This also moves from ldscript.arch to arch.ldscript to
match the loader's new convention.

Sponsored by:		Netflix
Reviewed by:		tsoome, kevans
Differential Revision:	https://reviews.freebsd.org/D44057
2024-02-24 17:56:23 -07:00
Warner Losh
3e15b01d69 libsa: Remove redundant sys/cdefs.h
Sponsored by:		Netflix
2024-02-22 09:31:57 -07:00
Warner Losh
b3e76e3d9e kboot: Implement write support for hostdisk
Don't assume that strategy is only called for read. Check the passed
flag for F_READ or F_WRITE and fail if it is neither. Open the disks for
writing and call host_read/host_write depending on that flag.

Sponsored by:		Netflix
Reviewed by:		kevans, gallatin
Differential Revision:	https://reviews.freebsd.org/D44016
2024-02-22 09:31:55 -07:00
Warner Losh
65ee8f90b7 kboot: Fix zfs bootonce protocol
This wasn't updated when the other copies were updated. Make it
identical to efi code. We should likely refactor this (with userboot),
but they are all not quite identical.

Sponsored by:		Netflix
2024-02-20 20:36:00 -07:00
Warner Losh
e34fd722ca kboot: Add our own lua bindings
Create a small wrapper around the new flua hash module so we can use it
here too. There's no 4th bindings, nor will they be created.

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D43874
2024-02-15 20:59:23 -07:00
Warner Losh
23d9b5c9fe loader: Remove gfx_fb_stub.c, it's no longer needed
Now that we draw in the gfx bindings for all our interpreters only when
graphics support is compiled in, we can eliminate this from all the
loaders that don't have graphics support.

Sponsored by:		Netflix
Reviewed by:		kevans, jhb
Differential Revision:	https://reviews.freebsd.org/D43905
2024-02-15 20:59:23 -07:00
Warner Losh
8f7327dcee kboot: update copyright on these files.
host_syscalls.c: I've written, so put Netflix copyright on. It's
possible in the confusion that Nathan wrote the host_gettimeofday
implementation.

syscall_nr: These files likely can't enjoy copyright protection since
they are just facts (the per-arch Linux system calls), so add a note
they are in the public domain.

Sponsored by:		Netflix
2024-01-28 13:04:32 -07:00
Warner Losh
2b7918f13c kboot: Assert copyright here
According to git blame I've 95%+ rewritten this file. Update copyright to
reflect that, but give nod to Nathan for the original I started with.

Sponsored by:		Netflix
2024-01-28 13:04:32 -07:00
Warner Losh
36ef238cb6 kboot: Move termios to libkboot
Sponsored by:		Netflix
2024-01-28 13:04:31 -07:00
Warner Losh
2e3f49888e kboot: Move system calls to libkboot
Sponsored by:		Netflix
2024-01-28 13:04:31 -07:00
Warner Losh
099335814d kboot: Move syscall stubs to libkboot
Sponsored by:		Netflix
2024-01-28 13:04:31 -07:00
Warner Losh
4aabaa1890 kboot: Cleanup libkern reference
For aarch64 and amd64, we don't pull in anything from libkern, so we
don't need it in our path.  However, powerpc needs ucmpdi2 from libkern,
so bring it into libkboot's build and omit it from loader.kboot.

Sponsored by:		Netflix
2024-01-28 13:04:31 -07:00
Warner Losh
a03411e847 kboot: Move _start out of kboot and into libkboot
Move the startup code from kboot/kboot to kboot/libkboot and add the
necessary infrastructure for it to build. move start_arch.h, a private
header for libkboot, over as well.

Sponsored by:		Netflix
2024-01-28 13:04:31 -07:00
Warner Losh
091c255b5b kboot: Move to kboot/kboot
In anticipation of separating the library elements from the kboot
prorgam elements, move kboot down a level into a subdirectory. There
will be libkboot and include directories in subsequent commits,
mirroring other subsystems like i386 and efi.

Sponsored by:		Netflix
2024-01-28 13:04:31 -07:00
Jose Luis Duran
ed76a9506a stand: Fix typo (triple S)
Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/955
2023-12-27 20:24:31 -07:00
Warner Losh
7c43148a97 stand: Automated cleanup of cdefs and other formatting
Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by:		Netflix
2023-11-26 22:24:00 -07:00
Mark Johnston
b247ff70e8 stand: Rename LIBFDT to LIBSAFDT
Preemptively address a collision with LIBFDT (to be added in the future)
from src.libnames.mk, which gets included via bsd.progs.mk.  No
functional change intended.

Reviewed by:	imp
MFC after:	1 week
Sponsored by:	Innovate UK
Differential Revision:	https://reviews.freebsd.org/D42486
2023-11-07 09:57:32 -05:00
John Baldwin
f53355131f Trim various $FreeBSD$
Approved by:	markj (cddl/contrib changes)
Reviewed by:	imp, emaste
Differential Revision:	https://reviews.freebsd.org/D41961
2023-10-10 10:34:43 -07:00
Warner Losh
d0b2dbfa0e Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
2023-08-16 11:55:03 -06:00
Warner Losh
1d386b48a5 Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:42 -06:00
Warner Losh
2a63c3be15 Remove $FreeBSD$: one-line .c comment pattern
Remove /^/[*/]\s*\$FreeBSD\$.*\n/
2023-08-16 11:54:29 -06:00
Warner Losh
42b388439b Remove $FreeBSD$: one-line .h pattern
Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
2023-08-16 11:54:23 -06:00
Warner Losh
b3e7694832 Remove $FreeBSD$: two-line .h pattern
Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
2023-08-16 11:54:16 -06:00
Gordon Bergling
34db0134a6 kboot: Fix a typo in a source code comment
- s/descriptoin/description/

MFC after:	3 days
2023-08-02 11:35:50 +02:00
VexedUXR
6f6213ec90 Add comments for memory size
Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/753
2023-06-12 08:57:20 -06:00
Warner Losh
a083d08676 kboot: Add smbios support
Add support for getting smbios from /sys/firmware/efi/systab, if
any. Add ptov mapping that uses mmap on /dev/mem to do the mapping with
64k pages (usually we only need 1 or two mappings).

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D39792
2023-05-01 15:12:29 -06:00
Warner Losh
facd0edbb2 kboot: Fix an off by one error
Fix an off-by-one error that would mean we'd get stuck on the newline if
ACPI= wasn't first.

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D39817
2023-05-01 15:12:24 -06:00
Warner Losh
83eabc64ef kboot: Add HOST_MAP_FAILED define
Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D39790
2023-05-01 15:11:32 -06:00
Warner Losh
5328f9034f stand/kboot: Simplify
There's plenty of stack in kboot, so use it here rather than the
malloc/free dance.

Sponsored by:		Netflix
Reviewed by:		tsoome, kevans
Differential Revision:	https://reviews.freebsd.org/D39416
2023-05-01 15:02:53 -06:00
Warner Losh
7f3c360646 Parse /kboot.conf
If there's a kboot.conf, prase it after the command line args are
parsed. It's not always easy to get all the right command line args
depending on the environment. Allow an escape hatch. While we can't do
everything one might like in this file, we can do enough.

Sponsored by:		Netflix
2023-03-13 20:45:50 -06:00
Warner Losh
7b4299eb4e kboot: Fix hostdisk_override
We were assuming that hostdisk_override was both a directory and a
file, which is not going to work very well. It's supposed to be a
single file, so recode it as such. Simplify erorr handling a little as
well and fix a return type-mismatch that doesn't matter for the
generated code (return NULL is the same as return false in this
context)

Sponsored by: Netflix
2023-03-02 11:12:10 -07:00
Warner Losh
35b4acad2f kboot: Use MIN instead of min
MIN works for any type, while min() is only for integers. So we were
rounding down to 0 since that's the size of 4GB truncated to an int.

Sponsored by: Netflix
2023-03-02 11:12:10 -07:00
Warner Losh
3a616b10d8 kboot: Better default boot device
Provide a better message when we can't find a boot device.

Sponsored by: Netflix
2023-03-02 11:12:09 -07:00
Warner Losh
6c47abb63b kboot: Hack for running on FreeBSD host
When we're running on a FreeBSD host, we can't open /proc/iomem.  So,
for now, just assume that we have 32GB of ram starting at 4GB.

Sponsored by: Netflix
2023-03-02 11:12:09 -07:00
Mitchell Horne
8859960436 loader: always install help files
Address two issues with current help file logic:

The existing condition prevents the common help file from being
installed when there are no additional help files defined. This results
in no loader.help on EFI platforms, for example.

Second, due to the fact that we build and install multiple loader types,
each successive install will clobber the previous loader.help. The
result is that we could lose type-specific commands, or possibly list
them in loaders that do not have such commands.

Instead, give each loader type a uniquely named help file. The EFI
loader will look for /boot/loader.help.efi, userboot will look for
/boot/loader.help.userboot, etc. The interpreter variant has no effect
on which help file is loaded.

This leaves the old /boot/loader.help unused.

Some credit for the final approach goes to Mathieu <sigsys@gmail.com>
for their version of the fix in https://reviews.freebsd.org/D22951.

PR:		267134
Reported by:	Daniel O'Connor <darius@dons.net.au>
Reviewed by:	imp
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D28591
2023-02-03 16:35:06 -04:00
Warner Losh
335e3daaf0 kboot: Keep track of what's used in the segment
Keep track of how much is used in the segment as we allocate it to the
application. Set memsz to 0 first, and increment it as used. Adjust the
bufsz before we call kexec so the kernel copies the right amount (it's
an error for bufsz to be bigger than memsz, so we set them == when we
retrieve the segment). Make sure we round to the page size, otherwise
kexec_load gets cranky.

Sponsored by:		Netflix
Reviewed by:		tsoome
Differential Revision:	https://reviews.freebsd.org/D38315
2023-02-03 08:41:41 -07:00
Warner Losh
db8d0c0cd9 kboot: Allocate a really big first segment
Allocate a huge segment for the first kexec_load segments. We limit the
lessor of:
	allocation to the size of the remaining memory segment
	45% of available memory
	95% of the memory we can allocate

This allows us to have really large RAM disks. We likely need to limit
this to the amount we actually used, though, since this can be a lot of
memory.

We have to do this complicated calculation for a few reasons: First, we
need 2 copies of the loaded kernel in the memory: The kernel can copy
everything to a temporary buffer. Next, malloc (via mmap) is limited to
a certain amount due to over commit, so we have to not allocate all we
can (only most of what we can).

Sponsored by:		Netflix
Reviewed by:		tsoome
Differential Revision:	https://reviews.freebsd.org/D38314
2023-02-03 08:41:41 -07:00
Warner Losh
1d3a7e849b kboot: Remove externs
kboot_get_phys_load_segment is defined in kboot.h, so remove them from
the .c files.

Sponsored by:		Netflix
Reviewed by:		tsoome
Differential Revision:	https://reviews.freebsd.org/D38310
2023-02-03 08:41:41 -07:00
Warner Losh
045fa2801a kboot: Try to read UEFI memory from physical memory on aarch64
Try to open /dev/mem to read in the UEFI memory map. If we can't, then
we'll read it in the trampoline.

Retain reading in /proc/iomem to find reserved areas in Linux. We need
to know them for good places to put the kernel. These are not reflected
in the UEFI memory map. However, we should not adjust the UEFI memory
map since these reserved areas of the Linux kernel are free to be used
once we enter the kexec trampoline...

Sponsored by:		Netflix
Reviewed by:		tsoome, kevans, andrew
Differential Revision:	https://reviews.freebsd.org/D38264
2023-02-03 08:41:41 -07:00