Commit Graph

146 Commits

Author SHA1 Message Date
Ian Lepore
e77f4eb2a0 Restore the ability to open a raw disk or partition in loader(8).
The disk_open() function searches for "the best partition" when slice and
partition information is not provided as part of the device name.  As of
r345477 the slice and partition fields of a disk_devdesc are initialized to
D_SLICEWILD and D_PARTWILD; in the past they were initialized to -1, which
was sometimes interpreted as meaning 'wildcard' and sometimes as 'open the
raw partition' depending on the context.  So as an unintended side effect of
r345477 it became basically impossible to ever open a disk or partition
without doing the 'best partition' search.  One visible effect of that was
the inability to open the raw disk to read the partition table correctly in
zfs_probe_dev(), leading to failures to find the zfs pool unless it was on
the first partition.

Now instead of always initializing slice and partition to wildcards, the
disk_parsedev() function initializes them based on the presence of a
path/file name following the device.  If there is any path or filename
following the ':' that ends the device name, then slice and partition are
initialized to D_SLICEWILD and D_PARTWILD.  If there is nothing after the
':' then it is considered to be a request to open the raw device or
partition itself (not a file stored within it), and the fields are
initialized to D_SLICENONE and D_PARTNONE.

With this change in place, all the tests in src/tools/boot are succesful
again, including the recently-added cases of booting from a zfs pool on
a partition other than slice 1 of the device.

PR:		236981
2019-04-25 15:09:21 +00:00
Toomas Soome
ed79202682 loader: add file_remove() function to undo file_insert_tail().
346002 did miss the fact that we do not only undo the loadaddr, but also
we need to remove the inserted module. Implement file_remove() to do the job.

MFC after:	1w
2019-04-07 13:36:17 +00:00
Toomas Soome
d7125bb1e2 loader: mod_loadkld() error: we previously assumed 'last_file' could be null
The last_file variable is used to reset the loadaddr variable back to original
value; however, it is possible the last_file is NULL, so we can not blindly
trust it. But then again, we can just save the original loadaddr and use
the saved value for recovery.

MFC after:	1w
2019-04-07 12:20:17 +00:00
Toomas Soome
d28c594669 loader: file_addmetadata() should check for memory allocation
malloc() can return NULL.

MFC after:	1w
2019-04-07 12:10:19 +00:00
Toomas Soome
1d28f39d20 loader: remove pointer checks before free() in module.c
free() does check for NULL argument, remove duplicate checks.

MFC after:	1w
2019-04-07 11:55:11 +00:00
Toomas Soome
db7599e9e5 loader: file_addmodule should check for memory allocation
strdup() can return NULL.

MFC after:	1w
2019-04-07 11:50:41 +00:00
Toomas Soome
cef2742e20 loader: malloc+bzero is calloc
Replace malloc+bzero in module.c with calloc.

MFC after:	1w
2019-04-07 11:48:41 +00:00
Marcin Wojtas
b0fefb25c5 Create kernel module to parse Veriexec manifest based on envs
The current approach of injecting manifest into mac_veriexec is to
verify the integrity of it in userspace (veriexec (8)) and pass its
entries into kernel using a char device (/dev/veriexec).
This requires verifying root partition integrity in loader,
for example by using memory disk and checking its hash.
Otherwise if rootfs is compromised an attacker could inject their own data.

This patch introduces an option to parse manifest in kernel based on envs.
The loader sets manifest path and digest.
EVENTHANDLER is used to launch the module right after the rootfs is mounted.
It has to be done this way, since one might want to verify integrity of the init file.
This means that manifest is required to be present on the root partition.
Note that the envs have to be set right before boot to make sure that no one can spoof them.

Submitted by: Kornel Duleba <mindal@semihalf.com>
Reviewed by: sjg
Obtained from: Semihalf
Sponsored by: Stormshield
Differential Revision: https://reviews.freebsd.org/D19281
2019-04-03 03:57:37 +00:00
Ian Lepore
14243f8de7 Distinguish between "no partition" and "choose best partition" with a constant.
The values of the d_slice and d_partition fields of a disk_devdesc have a
few values with special meanings in the disk_open() routine. Through various
evolutions of the loader code over time, a d_partition value of -1 has
meant both "use the first ufs partition found in the bsd label" and "don't
open a bsd partition at all, open the raw slice."

This defines a new special value of -2 to mean open the raw slice, and it
gives symbolic names to all the special values used in d_slice and
d_partition, and adjusts all existing uses of those fields to use the new
constants.

The phab review for this timed out without being accepted, but I'm still
citing it below because there is useful commentary there.

Differential Revision:	https://reviews.freebsd.org/D19262
2019-03-24 18:51:52 +00:00
Ed Maste
fe6ee08134 loader: fix loading of kernels with . in path
The loader indended to search the kernel file name (only) for . but
instead searched the entire path, so paths like
"boot/test.elfv2/kernel" would not work.

Submitted by:	alfredo.junior_eldorado.org.br
Reviewed by:	kevans
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D19658
2019-03-20 16:24:47 +00:00
Marcin Wojtas
c42e554dc9 Add missing boot.4th verification
During initialization of the forth interpreter
the loader looks for "/boot/boot.4th"
and executes any code found there.
That file was loaded bypassing verification.
Add a call to verify_file to change that.

Submitted by: Kornel Duleba <mindal@semihalf.com>
Reviewed by: sjg
Obtained from: Semihalf
Sponsored by: Stormshield
2019-03-19 02:45:32 +00:00
Kyle Evans
7325df02c5 stand: Improve some debugging experience
Some of these files using <FOO>_DEBUG defined a DEBUG() macro to serve as a
debug-printf. -DDEBUG is useful to enable some debugging output across
multiple ELF/common parts, so switch the DEBUG-as-printf macros over to
something more like DPRINTF that is more commonly used for this kind of
thing and less likely to conflict.

userboot/elf64_freebsd debugging also assumed %llx for uint64; use PRIx64
instead.

MFC after:	1 week
2019-03-12 16:21:39 +00:00
Emmanuel Vadot
73a41ff81c stand/common/module: Apply style(9) 2019-03-07 18:57:43 +00:00
Simon J. Gerraty
fd1d7567d8 Add -d flag to load command
When doing load -t manifest -d increments debug level

Reviewed by:	stevek
2019-03-04 19:50:59 +00:00
Simon J. Gerraty
8df8b2d3e5 Enable veriexec for loader
This relies on libbearssl and libsecureboot
to verify files read by loader in a maner equivalent
to how mac_veriexec

Note: disabled by default.
Use is initially expected to be by embeded vendors

Reviewed by:	emaste, imp
Sponsored by:	Juniper Networks
Differential Revision:	D16336
2019-02-26 06:22:10 +00:00
Toomas Soome
63f582e0f5 loader: ptable_close() should check its argument
If the passed in table is NULL, just return.
2019-02-18 20:29:19 +00:00
Ian Lepore
7827bb1c2c Garbage collection no-longer-used constant. 2019-02-17 23:48:51 +00:00
Ian Lepore
5b777dbfa5 Make lsdev -v output line up in neat columns by using a fixed width for
the size field and a tab between the partition type and the size.

Changes this

  disk devices:
        disk0 (MMC)
        disk0s1: DOS/Windows            49MB
        disk0s2: FreeBSD                14GB
        disk0s2a: FreeBSD UFS         14GB
        disk0s2b: Unknown             2048KB
        disk0s2d: FreeBSD UFS         2040KB

to this

  disk devices:
        disk0 (MMC)
        disk0s1: DOS/Windows      49MB
        disk0s2: FreeBSD          14GB
        disk0s2a: FreeBSD UFS     14GB
        disk0s2b: Unknown       2048KB
        disk0s2d: FreeBSD UFS   2040KB
2019-02-17 23:46:11 +00:00
Ian Lepore
71c96def1c Use a couple local variables to avoid repetitive long expressions that
cause line-wrapping.
2019-02-17 23:38:17 +00:00
Ian Lepore
4d02caf7cb Restore loader(8)'s ability for lsdev to show partitions within a bsd slice.
I'm pretty sure this used to work at one time, perhaps long ago.  It has
been failing recently because if you call disk_open() with dev->d_partition
set to -1 when d_slice refers to a bsd slice, it assumes you want it to
open the first partition within that slice.  When you then pass that open
dev instance to ptable_open(), it tries to read the start of the 'a'
partition and decides there is no recognizable partition type there.

This restores the old functionality by resetting d_offset to the start
of the raw slice after disk_open() returns.  For good measure, d_partition
is also set back to -1, although that doesn't currently affect anything.

I would have preferred to make disk_open() avoid such rude assumptions and
if you ask for partition -1 you get the raw slice.  But the commit history
shows that someone already did that once (r239058), and had to revert it
(r239232), so I didn't even try to go down that road.
2019-02-17 23:32:09 +00:00
Kyle Evans
e49f2c66d1 stand: dev_net: correct net_open's interpretation of params
net_open previously casted the first vararg to a char * and this was
half-OK: at first, it is passed to netif_open, which would cast it back to
the struct devdesc * that it really is and use it properly. It is then
strdup()d and used as the netdev_name, which is objectively wrong.

Correct it so that the first vararg is properly casted to a struct devdesc *
and the netdev_name gets set properly to make it more clear at a glance that
it's not doing something horribly wrong.

Reported by:	mmel
Reviewed by:	imp, mmel, tsoome
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D19206
2019-02-15 18:28:51 +00:00
Maxim Sobolev
d92fb78458 When handling CMD_CRIT error set command_errmsg to NULL after we dump it out,
so that it does not result in error message printed twice.

OK load doodoo
can't find 'doodoo'
can't find 'doodoo'
OK

MFC after:	2 weeks
2018-11-30 02:14:41 +00:00
Maxim Sobolev
55d5c94943 The libstand's panic() appends its own '\n' to the message, so that users of the API
don't need to supply one.

MFC after:	2 weeks
2018-11-29 18:37:48 +00:00
Toomas Soome
13dcff6bd8 loader: command_bcache() should print unsigned values
All bcache counters are unsigned.
2018-11-29 14:21:01 +00:00
Maxim Sobolev
6c81fe160c Nuke out buffer overflow safety marker code, it duplicates similar code in
the malloc()/free() as well as having potential of softening the handling
in case error is detected down to a mere warning as compared to hard panic
in free().

Submitted by:	tsoome
Differential Revision:	https://reviews.freebsd.org/D18299
2018-11-23 22:36:56 +00:00
Toomas Soome
a3fd276be0 loader: ptable_open() check for ptable_cd9660read result is wrong
The ptable_*read() functions return NULL on read errors (and partition table
closed as an side effect). The ptable_open must check the return value and
act properly.

PR:		232483
Reported by:	lev
Reviewed by:	lev,cem
Differential Revision:	https://reviews.freebsd.org/D17890
2018-11-07 21:36:52 +00:00
Toomas Soome
53e3fbee09 loader: always set media size from partition.
The disk access is validated by using partition table definitions, therefore
we have no need for if statements, just set the disk size.

Of course the partition table itself may be incorrect/inconsistent, but if
so, we are in trouble anyhow.

Differential Revision:	https://reviews.freebsd.org/D17822
2018-11-07 11:14:22 +00:00
Kyle Evans
d3d381b2b1 userboot: handle guest interpreter mismatches more intelligently
The switch to lualoader creates a problem with userboot: the host is
inclined to build userboot with Lua, but the host userboot's interpreter
must match what's available on the guest. For almost all FreeBSD guests in
the wild, Lua is not yet available and a Lua-based userboot will fail.

This revision updates userboot protocol to version 5, which adds a
swap_interpreter callback to request a different interpreter, and tries to
determine the proper interpreter to be used based on how the guest
/boot/loader is compiled. This is still a bit of a guess, but it's likely
the best possible guess we can make in order to get it right. The
interpreter is now embedded in the resulting executable, so we can open
/boot/loader on the guest and hunt that down to derive the interpreter it
was built with.

Using -l with bhyveload will not allow an intepreter swap, even if the
loader specified happens to be a userboot with the wrong interpreter. We'll
simply complain about the mismatch and bail out.

For legacy guests without the interpreter marker, we assume they're 4th.
For new guests with the interpreter marker, we'll read it and swap over
to the proper interpreter if it doesn't match what the userboot we're using
was compiled with.

Both flavors of userboot are installed by default, userboot_4th.so and
userboot_lua.so. This fixes the build WITHOUT_FORTH as a coincidence, which
was broken by userboot being forced to 4th.

Reviewed by:	imp, jhb, araujo (earlier version)
Approved by:	re (gjb)
Differential Revision:	https://reviews.freebsd.org/D16945
2018-09-01 02:23:45 +00:00
Kyle Evans
2ac6dfb0c2 lualoader: Print error messages from command failures at the prompt
Previously lualoader would remain silent, rather than printing
command_errmsg or noting that a command had failed or was not found.

Approved by:	re (gjb)
2018-08-31 15:02:53 +00:00
Warner Losh
060679ef9c Collapse zfsloader functionality back down into loader.
We no longer really need a separate zfsloader. It was useful when we
were first supporting ZFS and had limited ability to properly boot off
of ZFS without the special boot loader. Now that the boot loader has
matured, go the way loader.efi pioneered and just build one
binary. Change the name of the loader to load in the secondary boot
blocks to be just /boot/loader. Provide a symbolic link from zfsloader
to loader so people who have not upgraded their boot blocks are not
affected. This has the happy benefit of making coexistence easier as
well (fewer binaries in the matrix).

Discussed with: allanjude@, kevans@
RelNotes: Yes
Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D16361
2018-07-20 05:17:37 +00:00
Ian Lepore
c1418270b2 Extend loader(8) geli support to all architectures and all disk-like devices.
This moves the bulk of the geli support from lib386/biosdisk.c into a new
geli/gelidev.c which implements a devsw-type device whose dv_strategy()
function handles geli decryption. Support for all arches comes from moving
the taste-and-attach code to the devopen() function in libsa.

After opening any DEVT_DISK device, devopen() calls the new function
geli_probe_and_attach(), which will "attach" the geli code to the open_file
struct by creating a geli_devdesc instance to replace the disk_devdesc
instance in the open_file. That routes all IO for the device through the
geli code.

A new public geli_add_key() function is added, to allow arch/vendor-specific
code to add keys obtained from custom hardware or other sources.

With these changes, geli support will be compiled into all variations of
loader(8) on all arches because the default is WITH_LOADER_GELI.

Relnotes:	yes
Sponsored by:	Microchip Technology Inc
Differential Revision:	https://reviews.freebsd.org/D15743
2018-07-13 17:50:25 +00:00
Warner Losh
c96ac12e5c Transition to boot_env_to_howto and boot_howto_to_env in the boot
loader.

Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D16205
2018-07-13 16:43:29 +00:00
Warner Losh
4569e91328 Eliminate boot loader copies of boot arg parsing.
Eliminate 4 of the copies of the arg parsing in /boot/laoder
by using boot_parse_cmdline.

Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D16205
2018-07-13 16:43:23 +00:00
Warner Losh
007b82d7ab Stop using ../zfs/libzfs.h but instead use libzfs.h.
While ../zfs/libzfs.h mostly works, there are a few situations where
it does not. Eliminate the problem by using plain libzfs.h, like we do
for ufs support. This fixes the weird cases, and is easier to
understand. It also follows the general style convetion of avoiding
../ in #includes.
2018-07-08 07:42:58 +00:00
Allan Jude
436dbc4262 stand/common/disk.c: dev->d_offset still needs to be set to 0
With r335868, I thought this was no longer necessary. I was wrong.

Reported by:	ian
Sponsored by:	Klara Systems
2018-07-03 05:53:27 +00:00
Allan Jude
32c52b4847 stand/common/disk.c: Read partition table relative to the start of the disk
If a disk is of an oddball size, like the 200mb + 512b used in rootgen.sh,
when disk_open() is called on a GELI encrypted partition, attempts to read
the partition table fail, as they pass through the decryption process which
turns the already plaintext data into jibberish.

When reading the partition table, always pass a slice and partition setting
of -1, and an offset of 0. Setting the slice to -1 prevents a false
positive when checking the slice against the cache of GELI encrypted
slices.

Reviewed by:	imp, ian
Sponsored by:	Klara Systems
Differential Revision:	https://reviews.freebsd.org/D15847
2018-07-02 18:19:08 +00:00
Allan Jude
d48c0a685d stand/common/disk.c: Update debug printf
This was missed in r330809 because it is compiled out by default

Sponsored by:	Klara Systems
2018-06-17 19:31:35 +00:00
Warner Losh
5f88ee4479 bootprog_info is generated in vers.c. Move it's definition to
bootstrap.h and remove all the redundant copies.

Sponsored by: Netflix
2018-06-14 06:41:33 +00:00
Kyle Evans
3db6d179e8 stand: One more trivial consolidation (setting environment from howto) 2018-06-09 15:52:29 +00:00
Kyle Evans
28ee318d9c stand: Fix build after r334882
Not sure how this was not caught in Universe.
2018-06-09 15:28:37 +00:00
Kyle Evans
593e2c6ece stand: Consolidate checking for boot flags driven by environment vars
e.g. boot_mute, boot_single, boot_verbose, and friends; we checked for these
in multiple places, consolidate into common/ and allow a setting of "NO" for
any of these to turn them off. This allows systems with multiple
loader.conf(5) or loader.conf(5) overlay systems to easily turn off
variables in later processed files by setting it to NO.

Reported by:	Nick Wolff @ iXsystems
Reviewed by:	imp
2018-06-09 15:10:39 +00:00
Ian Lepore
9f6fd8397c Remove comments and assertions that are no longer valid after r330809.
r330809 replaced duplication of devdesc struct fields with an embedded copy
of the devdesc struct, to avoid fragility.  That means all the scattered
comments indicating that structs must match are no longer valid.  Likewise
asserts that attempted to mitigate some of the old fragility.

Reviewed by:	imp@
2018-06-05 22:13:45 +00:00
Dimitry Andric
073193ed51 Fix build of stand with base gcc
* Make autoboot() a static function in stand/common/boot.c, so it does
  not shadow local variables in gptboot.c and zfsboot.c.
* Remove -Winline from the Makefiles for gptboot, gptzfsboot and
  zfsboot, as gcc will always fail to inline some functions, and there
  is nothing we can do about it.
* For gcc <= 4.2.1, silence -Wuninitialized for isoboot, as it produces
  a false positive warning.
* Remove deprecated and unnecessary -mcpu=i386 flag from stand/defs.mk,
  as there is already a -march=i386 flag further in the file.

Reviewed by:	imp
MFC after:	3 days
Differential Revision: https://reviews.freebsd.org/D15628
2018-05-31 14:38:13 +00:00
Marius Strobl
dd5dbb3199 - Unbreak booting sparc64 kernels after the metadata unification in
r329190; sparc64 kernels are always 64-bit but with that revision
  in place, the loader was treating them as 32-bit ones.
- In order to reduce the likelihood of this kind of breakage in the
  future, #ifdef out md_load() on sparc64 and make md_load_dual() -
  which is currently local to metadata.c anyway - static.
- Make md_getboothowto() - also local to metadata.c - static.
- Get rid of the unused DTB pointer on sparc64.
2018-05-21 01:20:19 +00:00
Warner Losh
09001b5c78 Revert r333365
Even though we don't use it, it appears something else requires it to
be != 0 to work. This breaks tftp boot in loader.efi, so revert until
that can be sorted out.
2018-05-10 20:27:12 +00:00
Warner Losh
a1947307e7 We don't use f_devdata, so don't set it. Should that need to change
later, we can. This leaves ZFS as the only irregular f_devdata
user in the tree.
2018-05-08 16:16:56 +00:00
Warner Losh
e4eef18848 Use M. Warner Losh everywhere on my copyrights.
Remove 'All Rights Reserved' where I can.
2018-05-01 16:29:22 +00:00
Toomas Soome
b67d407ac7 loader: cstyle cleanup of command.c
just clean it up. no functional changes intended.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D15087
2018-04-16 14:10:19 +00:00
Toomas Soome
6d68f8acf4 loader: make sure we do not return garbage from help_getnext
Since we do free subtopic and desc in help_getnext(), we need to set them also
NULL, so we make sure we dont get double free().

Approved by:	bapt
Differential Revision:	https://reviews.freebsd.org/D15082
2018-04-16 12:46:14 +00:00
Toomas Soome
746dddb134 loader: command_errmsg should be const
Use const char * for command_errmsg.
2018-04-16 08:41:44 +00:00
Toomas Soome
faa5306390 loader: make sure we use snprintf() in commands.c
Safeguard against memory corruptions.
2018-04-16 08:15:50 +00:00
Toomas Soome
5276f60430 loader: provide values in help_getnext()
With r328289 we attempt to make sure we free the resources allocated in
help_getnext(), however, it is possible that we get no resources allocated
and help_getnext() will return early.

Make sure we have pointers set to NULL early in help_getnext().

Reported by:	Andy Fiddaman
2018-04-16 07:26:23 +00:00
Benno Rice
48990fce8e Add an ISO9660 "partition table" type to loader.
When booted via isoboot(8) loader will be handed a disk that simply contains
an ISO9660 image. Currently this confuses it greatly. Teach it how to spot
that it's in this situation and that ISO9660 has one "partition" covering
the whole disk.

Reviewed by:	imp
Sponsored by:	iXsystems, Inc.
Differential Revision:	https://reviews.freebsd.org/D14915
2018-04-05 19:45:30 +00:00
Conrad Meyer
70be5ca734 Apply r228478 (CTASSERT => _Static_assert()) to stand bootstrap.h
Reported by:	GCC (it doesn't like the unused array)
Sponsored by:	Dell EMC Isilon
2018-03-21 23:46:26 +00:00
Warner Losh
56e53cb8ef Prefer uintXX_t to u_intXX_t
A foolish consistency is the hobgoblin of little minds, adored by
little statesmen and philosophers and divines. With consistency a
great soul has simply nothing to do. -- Ralph Waldo Emerson
2018-03-13 16:33:00 +00:00
Warner Losh
de04d704a9 Use the actual struct devdesc at the start of all *_devdesc structs
The current system is fragile and requires very careful layout of all
*_devdesc structures. It also makes it hard to change the base
devdesc. Take a page from CAM and put the 'header' in all the derived
classes and adjust the code to match.

For OFW, move the iHandle h_handle out of a slot conflicting with
d_opendata. Due to quirks in the alignment rules, this worked.
However changing the code to use d_opendata storage now that it's a
pointer is hard, so just have a separate field for it.

All other cleanups were to make the *_devdesc structures match where
they'd taken some liberties that were none-the-less compatible enough
to work.
2018-03-12 21:39:49 +00:00
Warner Losh
b3a2aad11e Minor cosmetic changes.
Make sure { on the same line as struct for all struct *devdesc.  Move
some type definitions to next to the dv_type define, since that's what
sets the d_type.
2018-03-12 21:39:27 +00:00
Kyle Evans
bef6cd493c stand/interp_lua: correct errorr => error 2018-03-11 04:10:18 +00:00
Ian Lepore
7feea4075b Fix module loading on arm after the metadata.c unification in r329190.
Arm modules need an additional address fixup not needed by other platforms.
2018-02-28 21:51:51 +00:00
Ed Maste
6b6324985a load_elf.c: Use consistent indentation
As noted in D14267 load_elf.c has a variety of indentation styles.  Move
to standard 8 column hard tab indents, 4 space second level indents.
Also includes some whitespace cleanups found by clang-format.
2018-02-21 19:42:54 +00:00
Warner Losh
4784aef9f5 Consolidate three copies of ZFS commands into a central location.
There's no reason to have multiple copies of lszfs and
reloadbe. Consolidate them into one location. Also ldi_get_size is the
same everywhere (except sparc64). Make it the same everywhere as the
common definition is more general and will work on spar64.
2018-02-21 15:57:36 +00:00
Conrad Meyer
f91f3926e0 loader.lua: Expose errno table to lua
Reviewed by:	kevans
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D14454
2018-02-20 19:39:48 +00:00
Kyle Evans
afad05b233 lualoader: Add ability to intercept cli commands
If we failed to execute the input line as pure lua, run the command through
parse for consistent argument parsing. Pass the parsed arguments through to
a global "cli_execute" written in Lua, which is expected to either handle it
or pass it back through to interp_builtin_cmd (via loader.command).

lua-handled cli commands will then exist as globals in whatever module they
most belong in, and invocations at the loader prompt will magically dispatch
to them if they exist.

Reviewed by:	imp
Differential Revision:	https://reviews.freebsd.org/D14450
2018-02-20 17:46:50 +00:00
Conrad Meyer
77d4be50c9 Lua loader: Add barebones "lfs" module
Add a Lua FileSystem module, an emulation of a subset of the permissively
licensed (MIT) Lua library of the same name[0], to our loader's Lua
environment.

[0]: https://github.com/keplerproject/luafilesystem/

Reviewed by:	kevans
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D14418
2018-02-18 01:15:25 +00:00
Conrad Meyer
6771d4a815 interp_lua: Register io/loader with regular Lua module system
Reviewed by:	kevans
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D14421
2018-02-18 01:13:58 +00:00
Kyle Evans
4daa199d7b stand: Fix ubldr after r329190
metadata load files were consolidated in r329190, and these relocation fixup
bits were inadvertently dropped in the process. Re-add them to fix boot with
ubldr.

Glanced over by:	jhibbits
X-MFC-With:	r329190
2018-02-15 15:01:07 +00:00
Justin Hibbits
d3e1307bf5 Unify metadata load files for arm, mips, powerpc, sparc64
Summary:
All metadata.c files are very similar, with only trivial changes.  Unify them
into a single common file, with minor special-casing where needed.

Reviewed By: imp
Differential Revision: https://reviews.freebsd.org/D13978
2018-02-13 03:44:50 +00:00
Warner Losh
7cafeaa1fd Add Lua as a scripting langauge to /boot/loader
liblua glues the lua run time into the boot loader. It implements all
the runtime routines that lua expects. In addition, it has a few
standard 'C' headers that nueter various aspects of the LUA build that
are too specific to lua to be in libsa. Many refinements from the
original code to improve implementation and the number of included lua
libraries. Use int64_t for lua_Number. Have "/boot/lua" be the default
module path. Numerous cleanups from the original GSoC project,
including hacking libsa to allow lua to be built with only one change
outside luaconf.h.

Add the final bit of lua glue to bring in liblua and plug into the
multiple interpreter framework, previously committed.

Add LOADER_LUA option, currently off by default.

Presently, this is an experimental option. One must opt-in to using
this by defining WITH_LOADER_LUA and WITHOUT_FORTH. It's been
lightly tested, so keep a backup copy of your old loader handy.
The menu code, coming in the next commit, hasn't been exhaustively
tested. A LUA boot loader is 60k larger than a FORTH one, which is
80k larger than a no-interpreter one. Subtle changes in size
may tip things past some subtle limit (the binary is ~430k now
when built with LUA). A future version may offer coexistance.

Bump FreeBSD version to 1200058 to mark the milestone.

Pedro Souza's 2014 Summer of Code project. Rui Paulo, Pedro Arthur,
Zakary Nafziger and Wojciech A. Koszek also contributed. Warner Losh
reworked it extensively into its current form.

Obtained from: https://wiki.freebsd.org/SummerOfCode2014/LuaLoader
Sponsored by: Google Summer of Code
Relnotes: Yes
MFC After: 1 month
Differential Review: https://reviews.freebsd.org/D14295
2018-02-12 15:31:53 +00:00
Roger Pau Monné
12363d8ea3 loader: fix endianness conversion
r328536 broke symbol loading on amd64 at least (and probably other
arches). r328826 contained the problem to ppc only by adding
pre-processors guards.

Fix this properly by moving the endianness conversion to separate
helper functions, and make the conversion more robust by using sizeof
instead of having to manually code the size of each field.

Finally list the fields in each structure in a macro in order to avoid
code repetition.

Sponsored by:		Citrix Systems R&D
Reviewed by:		kib emaste wma
Differential revision:	https://reviews.freebsd.org/D14267
2018-02-09 10:20:16 +00:00
Warner Losh
86411ec1d7 Set script.lang in the environment to either 'forth' or 'simple' to
reflect what scripting language was compiled into the loader. I
anticipate that being able to find this out quickly from the OK prompt
will be useful in troubleshooting in the future.
2018-02-09 00:36:55 +00:00
Warner Losh
16bb6523bc Move to tabs for indentation and to 8-space notches, per style(9).
4 space indentation with a mix of tabs and spaces is a hassle. Update
to project-standard hard-tabs with 8-space indentation in these files.
This matches the new code coming in better as well.
2018-02-08 17:07:27 +00:00
Warner Losh
3a4a3639d2 Move simple interpreter 'perform' into interp.c and call it
interp_builtin_cmd().
2018-02-07 23:27:38 +00:00
Warner Losh
44eebfff73 Fix indentation to FreeBSD standard for interp files 2018-02-07 23:27:25 +00:00
John Baldwin
15746ef43a Ignore relocation tables for non-memory-resident sections.
As a followup to r328101, ignore relocation tables for ELF object
sections that are not memory resident.  For modules loaded by the
loader, ignore relocation tables whose associated section was not
loaded by the loader (sh_addr is zero).  For modules loaded at runtime
via kldload(2), ignore relocation tables whose associated section is
not marked with SHF_ALLOC.

Reported by:	Mori Hiroki <yamori813@yahoo.co.jp>, adrian
Tested on:	mips, mips64
MFC after:	1 month
Sponsored by:	DARPA / AFRL
2018-02-05 23:35:33 +00:00
Ed Maste
5cca46a92c Make cross-endian loader changes apply only to powerpc
The cross-endian loader change in r328536 (review D12422) broke symbol
loading on (at least) amd64 kernels.  Temporarily paper over the issue
by restricting the cross-endian support to only powerpc, until a proper
fix arrives.

Submitted by:	royger
2018-02-03 01:23:48 +00:00
Warner Losh
74bb0b0e9a Retire pnp.4th and the code needed only for 4th words used here.
This has never been installed. It was added to the tree disconnected
to the build in FreeBSD 5 (17 years ago) and has never been used as
far as I can tell. The desired improvements never really happened
(despite a couple minor cleanups along the way). It's relevance is
long past, so better to retire it.
2018-02-02 15:01:33 +00:00
Warner Losh
79a6a17abd Break out the interpreters (simple and forth) w/o ifdefs. This is
akin to what Pedro Souza and Wojciech Koszek did in the lua GSoC with
interp.h, interp_simple.c and changes to interp.c and interp_forth.c,
but completely redone from scratch.

This effectively restores the spirit of r326712 (my first attempt to
bring in Pedro's and Wojciech's work) updated for new requirements
that had silently broke their original work.  This change also differs
by using fixed function names instead of function pointers to simply
things. Only one interpreter at a time may be compiled in.

Also of note: we take a mutable string, pass it in via a const char *
pointer into intrp_forth's interp_run(). We then cast away the const
to pass into ficlExec since ficl would require extensive changes to
properly const-poison. See Sections 6.5.2.5 and 6.7.3 of C11 standard
noting it's only UB if you modify a const object through a non-const
pointer, but not char [] -> const char * -> char * as here.
2018-01-31 22:46:05 +00:00
Sean Bruno
71e9130584 Add missing non-POWERPC case to give the scr value something non-zero.
This fixes the instant reboot of netbooting after r328536 on x86 systems.

Reviewed by:	peter
Sponsored by:	Limelight Networks
2018-01-30 20:00:12 +00:00
Wojciech Macek
8de1ad0b9b loader: support for mixed-endianness ELF/loader and POWER8
On POWER8 with current petitpoot, the loader.kboot might be
run as little-endian application. The FreeBSD kernel is
always big-endian, so the load_elf_* routines must be aware
of proper endianness of all fields.

Submitted by:          Wojciech Macek <wma@semihalf.com>
Obtained from:         Semihalf
Sponsored by:          IBM, QCM Technologies
Differential revision: https://reviews.freebsd.org/D12422
2018-01-29 09:24:28 +00:00
Warner Losh
f9a20b8fc1 There's no tainted data here, tag it as such to avoid the slew of
false positives. The files the boot loader reads are assumed to be
good.

CID: 1006663,1006665,1265013, 1265014 (possibly more)
Sponsored by: Netflix
2018-01-23 18:01:40 +00:00
Warner Losh
4baa8d7ebd On malloc failure, be sure to close the include file that triggered
it.

CID: 1007775
Sponsored by: Netflix
2018-01-23 18:01:32 +00:00
Warner Losh
74ecc44117 Don't leak memory when displaying help.
Right now, we'll leak memory when we display a help topic because we
don't free t, s, d that we've just used when breaking out of the loop.
NB: coverity just reported t, but s and d also leak.

CID: 1007776
2018-01-23 18:01:27 +00:00
Warner Losh
1065f77afb Fix some resource leaks.
Always free dev and fstyp before strduping new values to assign to
them. Free them at the end of the loop. This keeps them from leaking
for mal-formed /etc/fstab lines.

CID: 1007777, 1007778, 1007779
Sponsored by: Netflix
2018-01-23 18:01:17 +00:00
John Baldwin
58c4aee0d7 Require the SHF_ALLOC flag for program sections from kernel object modules.
ELF object files can contain program sections which are not supposed
to be loaded into memory (e.g. .comment).  Normally the static linker
uses these flags to decide which sections are allocated to loadable
program segments in ELF binaries and shared objects (including kernels
on all architectures and kernel modules on architectures other than
amd64).

Mapping ELF object files (such as amd64 kernel modules) into memory
directly is a bit of a grey area.  ELF object files are intended to be
used as inputs to the static linker.  As a result, there is not a
standardized definition for what the memory layout of an ELF object
should be (none of the section headers have valid virtual memory
addresses for example).

The kernel and loader were not checking the SHF_ALLOC flag but loading
any program sections with certain types such as SHT_PROGBITS.  As a
result, the kernel and loader would load into RAM some sections that
weren't marked with SHF_ALLOC such as .comment that are not loaded
into RAM for kernel modules on other architectures (which are
implemented as ELF shared objects).  Aside from possibly requiring
slightly more RAM to hold a kernel module this does not affect runtime
correctness as the kernel relocates symbols based on the layout it
uses.

Debuggers such as gdb and lldb do not extract symbol tables from a
running process or kernel.  Instead, they replicate the memory layout
of ELF executables and shared objects and use that to construct their
own symbol tables.  For executables and shared objects this works
fine.  For ELF objects the current logic in kgdb (and probably lldb
based on a simple reading) assumes that only sections with SHF_ALLOC
are memory resident when constructing a memory layout.  If the
debugger constructs a different memory layout than the kernel, then it
will compute different addresses for symbols causing symbols in the
debugger to appear to have the wrong values (though the kernel itself
is working fine).  The current port of mdb does not check SHF_ALLOC as
it replicates the kernel's logic in its existing kernel support.

The bfd linker sorts the sections in ELF object files such that all of
the allocated sections (sections with SHF_ALLOCATED) are placed first
followed by unallocated sections.  As a result, when kgdb composed a
memory layout using only the allocated sections, this layout happened
to match the layout used by the kernel and loader.  The lld linker
does not sort the sections in ELF object files and mixed allocated and
unallocated sections.  This resulted in kgdb composing a different
memory layout than the kernel and loader.

We could either patch kgdb (and possibly in the future lldb) to use
custom handling when generating memory layouts for kernel modules that
are ELF objects, or we could change the kernel and loader to check
SHF_ALLOCATED.  I chose the latter as I feel we shouldn't be loading
things into RAM that the module won't use.  This should mostly be a
NOP when linking with bfd but will allow the existing kgdb to work
with amd64 kernel modules linked with lld.

Note that we only require SHF_ALLOC for "program" sections for types
like SHT_PROGBITS and SHT_NOBITS.  Other section types such as symbol
tables, string tables, and relocations must also be loaded and are not
marked with SHF_ALLOC.

Reported by:	np
Reviewed by:	kib, emaste
MFC after:	1 month
Sponsored by:	Chelsio Communications
Differential Revision:	https://reviews.freebsd.org/D13926
2018-01-17 22:51:59 +00:00
Konstantin Belousov
85f794e126 Skip IRELATIVE relocations when loader processes ELF files.
ifuncs can be only called in the (early boot) kernel environment, so
postpone resolving until early stage of the kernel boot.  This commit
is performed in advance to make loaders on most machines updated
before ifuncs appear in the kernels.

Reviewed by:	emaste, jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D13838
2018-01-11 13:57:30 +00:00
Warner Losh
6bc860372d Interact is always called with NULL. Simplify code a little by
removing this argument, and expanding when rc is NULL. This
effectively completes the back out of custom scripts for tftp booted
loaders from r269153 that was started in r292344 with the new path
tricks that obsoleted it.

Submitted by: Netflix
2017-12-19 04:05:55 +00:00
Warner Losh
0ff3f28b2d Simplify things a little. The RETURN macro isn't required. It's only
used once, inside an #ifdef where it would be defined to be return.

Sponsored by: Netflix
2017-12-19 04:05:43 +00:00
Warner Losh
ba25195ebf Revert r326792, r326784, r326772, r326712
Something subtle is creating problems for disk access on ubldr. Back
it out unti that can be sorted out.

Sponsored by: Netflix
2017-12-12 22:06:22 +00:00
Warner Losh
2ccd1dd900 Revert part of 362772. It was causing problems for includes and making
the menus disappear.

Sponsored by: Netflix
2017-12-11 23:15:43 +00:00
Warner Losh
c83457486b Fix regression with lua import
Don't print when we can't find a file. Copy it instead to the error
buffer. Higher level routines determine if it's appropriate to print
the error message.

Also, remove dead code (labeled bogusly lost functionality) since we
never used that functionality. Remove unused arg from interact() too.

Sponsored by: Netflix
2017-12-11 16:18:05 +00:00
Warner Losh
fb5af39a82 Create interp class.
Create an interp class. Use it to separate out the different types of
interpreters: forth and simple with function pointers rather than
via #ifdefs.

Obtained from: lua boot loader project
    (via https://bsdimp@github.com/bsdimp/freebsd.git lua-bootloader)
Sponsored by: Netflix
2017-12-08 19:57:16 +00:00
Warner Losh
bd04a914bc Const poison a couple of interfaces.
Obtained from: lua boot project
Sponsored by: Netflix
2017-12-08 19:57:11 +00:00
Warner Losh
f38658e140 Prefer stdint.h to inttypes.h since the added prototypes form the
latter aren't used. Prefer sys/link_elf.h to link.h so we're only
dependent on the kernel tree. The default installation of link.h just
includes this file, and any benefit from that is outweighed by the
hassle it causes. This reduces the footprint of files needed from the
system includes (or sysroot in buildworld).

Sponsored by: Netflix
2017-12-05 21:38:04 +00:00
Warner Losh
db71174436 Fix theoretical integer overflow issues. If the product here is
greater than 2^31-1, then the result will be huge. This is unlikely,
as we don't support that many sections, but out of an abundace of
caution cast to size_t so the multiplication won't overflow
mysteriously when size_t is larger than 32-bits. The resulting code
may be a smidge larger, but this isn't super-space critical code.

CID: 1194216, 1194217, 1194222, 1194223, 1265018, 1265019,1265020,
     1265021
Sponsored by: Netflix
2017-11-24 05:00:25 +00:00
Toomas Soome
a9abb85906 net_parse_rootpath() has no parameters
Add void for parameter list.
2017-11-22 10:04:09 +00:00
Warner Losh
ca987d4641 Move sys/boot to stand. Fix all references to new location
Sponsored by:	Netflix
2017-11-14 23:02:19 +00:00