LOADER_4TH(8)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸ’… loader_4th(8) β€” like, the final boss of kernel bootstrapping, bestie

okay so real talk, this is LOADER_4TH(8) from the System Manager's Manual, and it's genuinely the glow-up finale of the whole boot story. buckle up πŸŽ€


✨ NAME ✨

loader_4th β€” the final stage of kernel bootstrapping, no cap πŸ‘‘


🌸 DESCRIPTION 🌸

So the program literally called loader_4th is the final stage of FreeBSD's kernel bootstrapping process. On IA32 (i386) architectures it's a BTX client. It's linked statically to libsa(3) and usually lives in the directory /boot. iconic behavior honestly πŸ’…

It hands you a scripting language β€” like, a whole vibe β€” that you can use to automate tasks, do pre-configuration, or assist in recovery procedures. This scripting language is roughly split into two main components:

  • The smaller one is a set of commands designed for direct use by the casual user, called "builtin commands" for historical reasons. The whole drive behind these is user-friendliness 🫢
  • The bigger component is an ANS Forth compatible Forth interpreter based on FICL, by John Sadler. main character energy fr fr πŸ”₯

During initialization, loader_4th will probe for a console and set the console variable β€” or set it to serial console ("comconsole") if the previous boot stage used that. If multiple consoles are selected, they'll be listed separated by spaces. Then devices are probed, currdev and loaddev are set, and LINES is set to 24. Next, FICL is initialized, the builtin words are added to its vocabulary, and /boot/boot.4th is processed if it exists. πŸ‘€ No disk switching is possible while that file is being read. The inner interpreter loader_4th will use with FICL is then set to interpret, which is FICL's default. After that, /boot/loader.rc is processed if available. These files are processed through the include command, which reads all of them into memory before processing them, making disk changes possible. slay.

At this point, if an autoboot has not been tried, and if autoboot_delay is not set to "NO" (not case sensitive), then an autoboot will be tried. If the system gets past this point, prompt will be set and loader_4th will engage interactive mode.

⚠️ bestie, important: historically, even when autoboot_delay is set to "0" the user will be able to interrupt the autoboot process by pressing some key on the console while kernel and modules are being loaded. In some cases such behaviour may be undesirable β€” to prevent it, set autoboot_delay to "-1". In this case loader_4th will engage interactive mode only if autoboot has failed. πŸ’€

πŸ¦‹ BUILTIN COMMANDS πŸ¦‹

In loader_4th, builtin commands take parameters from the command line. Presently, the only way to call them from a script is by using evaluate on a string. If an error condition occurs, an exception will be generated, which can be intercepted using ANS Forth exception handling words. If it's not intercepted, an error message will be displayed and the interpreter's state will be reset β€” emptying the stack and restoring interpreting mode. The commands are described in the loader_simp(8) "BUILTIN COMMANDS" section. go read it, iykyk πŸ“–

πŸ’œ BUILTIN ENVIRONMENT VARIABLES πŸ’œ

The environment variables common to all interpreters are described in the loader_simp(8) "BUILTIN ENVIRONMENT VARIABLES" section.

πŸŽ€ BUILTIN PARSER πŸŽ€

When a builtin command is executed, the rest of the line is taken by it as arguments, and it's processed by a special parser which is not used for regular Forth commands. cute, right? it has rules:

  1. All backslash characters are preprocessed.
    • \b, \f, \r, \n and \t are processed as in C.
    • \s is converted to a space.
    • \v is converted to ASCII 11.
    • \z is just skipped. Useful for things like "\0xf\z\0xf".
    • \0xN and \0xNN are replaced by the hex N or NN.
    • \NNN is replaced by the octal NNN ASCII character.
    • \", \' and \$ will escape these characters, preventing them from receiving special treatment in Step 2, described below.
    • \\ will be replaced with a single \ .
    • In any other occurrence, backslash will just be removed.
  2. Every string between non-escaped quotes or double-quotes will be treated as a single word for the purposes of the remaining steps.
  3. Replace any $VARIABLE or ${VARIABLE} with the value of the environment variable VARIABLE.
  4. Space-delimited arguments are passed to the called builtin command. Spaces can also be escaped through the use of \\ .
πŸ™ˆ An exception to this parsing rule exists, and is described in "BUILTINS AND FORTH".

🌈 BUILTINS AND FORTH 🌈

All builtin words are state-smart, immediate words. If interpreted, they behave exactly as described previously. If they're compiled, though, they extract their arguments from the stack instead of the command line. plot twist 😭

If compiled, the builtin words expect to find, at execution time, the following parameters on the stack:

addrN lenN ... addr2 len2 addr1 len1 N

where addrX lenX are strings which will compose the command line that will be parsed into the builtin's arguments. Internally, these strings are concatenated in from 1 to N, with a space put between each one.

If no arguments are passed, a 0 must be passed β€” even if the builtin accepts no arguments. do not forget, bestie, fr fr.

While this behavior has benefits, it has its trade-offs. If the execution token of a builtin is acquired (through ' or [']), and then passed to catch or execute, the builtin behavior will depend on the system state at the time catch or execute is processed! This is particularly annoying for programs that want or need to handle exceptions. In this case, the use of a proxy is recommended. For example:

: (boot) boot;

πŸ”₯ FICL πŸ”₯

FICL is a Forth interpreter written in C, in the form of a forth virtual machine library that can be called by C functions and vice versa. πŸ’…

In loader_4th, each line read interactively is then fed to FICL, which may call loader_4th back to execute the builtin words. The builtin include will also feed FICL, one line at a time.

The words available to FICL can be classified into four groups:

  • The ANS Forth standard words
  • Extra FICL words
  • Extra FreeBSD words
  • The builtin commands

(the latter were already described, bestie). The ANS Forth standard words are listed in the "STANDARDS" section. The words falling in the two other groups are described in the following subsections. ✨

⭐ FICL EXTRA WORDS ⭐

okay here's the whole squad, no descriptions needed, they just exist:

  • .env
  • .ver
  • -roll
  • 2constant
  • >name
  • body>
  • compare β€” this is the STRING word set's compare.
  • compile-only
  • endif
  • forget-wid
  • parse-word
  • sliteral β€” this is the STRING word set's sliteral.
  • wid-set-super
  • w@
  • w!
  • x.
  • empty
  • cell-
  • -rot

πŸ¦‹ FREEBSD EXTRA WORDS πŸ¦‹

  • $ (--) β€” Evaluates the remainder of the input buffer, after having printed it first.
  • % (--) β€” Evaluates the remainder of the input buffer under a catch exception guard.
  • .# β€” Works like . but without outputting a trailing space.
  • fclose (fd --) β€” Closes a file.
  • fkey (fd -- char) β€” Reads a single character from a file.
  • fload (fd --) β€” Processes a file fd.
  • fopen (addr len mode -- fd) β€” Opens a file. Returns a file descriptor, or -1 in case of failure. The mode parameter selects whether the file is to be opened for read access, write access, or both. The constants O_RDONLY, O_WRONLY, and O_RDWR are defined in /boot/support.4th, indicating read only, write only, and read-write access, respectively.
  • fread (fd addr len -- len') β€” Tries to read len bytes from file fd into buffer addr. Returns the actual number of bytes read, or -1 in case of error or end of file.
  • heap? (-- cells) β€” Return the space remaining in the dictionary heap, in cells. This is not related to the heap used by dynamic memory allocation words. (don't mix them up πŸ™ˆ)
  • inb (port -- char) β€” Reads a byte from a port.
  • key (-- char) β€” Reads a single character from the console.
  • key? (-- flag) β€” Returns true if there is a character available to be read from the console.
  • ms (u --) β€” Waits u microseconds.
  • outb (port char --) β€” Writes a byte to a port.
  • seconds (-- u) β€” Returns the number of seconds since midnight.
  • tib> (-- addr len) β€” Returns the remainder of the input buffer as a string on the stack.
  • trace! (flag --) β€” Activates or deactivates tracing. Does not work with catch. πŸ’€

🌸 FREEBSD DEFINED ENVIRONMENTAL QUERIES 🌸

  • arch-i386 β€” TRUE if the architecture is IA32.
  • FreeBSD_version β€” FreeBSD version at compile time.
  • loader_version β€” loader_4th version.

πŸ‘‘ SECURITY πŸ‘‘

Access to the loader_4th command line provides several ways of compromising system security, including, but not limited to:

  • Booting from removable storage, by setting the currdev or loaddev variables
  • Executing binary of choice, by setting the init_path or init_script variables
  • Overriding ACPI DSDT to inject arbitrary code into the ACPI subsystem
🚨 bestie, listen to me: you can prevent unauthorized access to the loader_4th command line by setting the password, or setting autoboot_delay to -1. See loader.conf(5) for details. In order for this to be effective, one should also configure the firmware (BIOS or UEFI) to prevent booting from unauthorized devices. two locks, not one πŸ”

πŸ’– MD πŸ’–

Memory disk (MD) can be used when the loader_4th was compiled with MD_IMAGE_SIZE. The size of the memory disk is determined by MD_IMAGE_SIZE. If MD is available, a file system can be embedded into the loader_4th with /sys/tools/embed_mfs.sh. Then, MD will be probed and be set to currdev during initialization.

Currently, MD is only supported in loader.efi(8). just that one, bestie 🫢

πŸŽ€ FILES πŸŽ€

  • /boot/loader β€” loader_4th itself.
  • /boot/boot.4th β€” Additional FICL initialization.
  • /boot/defaults/loader.conf β€” part of the loader_4th configuration files, as described in loader.conf(5).
  • /boot/loader.4th β€” Extra builtin-like words.
  • /boot/loader.conf β€” part of the loader_4th configuration files, as described in loader.conf(5).
  • /boot/loader.conf.local β€” part of the loader_4th configuration files, as described in loader.conf(5).
  • /boot/loader.rc β€” loader_4th bootstrapping script.
  • /boot/loader.help β€” Loaded by help. Contains the help messages.
  • /boot/support.4th β€” loader.conf processing words.
  • /usr/share/examples/bootforth/ β€” Assorted examples.

✨ EXAMPLES ✨

okay bestie, examples time β€” these are the little rituals you can run, and yes they're giving main character energy πŸ’…

  • Boot in single user mode β€” literally just this:
boot -s
  • Load the kernel, a splash screen, and then autoboot in five seconds. Notice that a kernel must be loaded before any other load command is attempted. No cap, kernel first or it's a no from the loader:
load kernel
load splash_bmp
load -t splash_image_data /boot/chuckrulez.bmp
autoboot 5
  • Set the disk unit of the root device to 2, and then boot. This would be needed in a system with two IDE disks, with the second IDE disk hardwired to ada2 instead of ada1. Very specific, very real, very "trust me bestie" 🫢
set root_disk_unit=2
boot /boot/kernel/kernel
  • Set the default device used for loading a kernel from a ZFS filesystem β€” this is the ZFS girlies' moment:
set currdev=zfs:tank/ROOT/knowngood:

πŸ’€ ERRORS πŸ’€

The following values are thrown by loader_4th β€” collect them like PokΓ©mon, but make it error codes 😭

  • 100 β€” Any type of error in the processing of a builtin.
  • -1 β€” Abort executed.
  • -2 β€” Abort" executed.
  • -56 β€” Quit executed.
  • -256 β€” Out of interpreting text.
  • -257 β€” Need more text to succeed -- will finish on next run.
  • -258 β€” Bye executed.
  • -259 β€” Unspecified error.

πŸ‘€ SEE ALSO πŸ‘€

go read these next, bestie, they're lowkey essential: libsa(3), loader.conf(5), tuning(7), boot(8), and btxld(8) πŸ¦‹


πŸ¦‹ STANDARDS πŸ¦‹

For the purposes of ANS Forth compliance, loader is an ANS Forth System with Environmental Restrictions, Providing:

  • .(, :noname, ?do, parse, pick, roll, refill, to, value, \, false, true, <>, 0<>, compile,, ,, erase, nip, tuck and marker from the Core Extensions word set.
  • the Exception Extensions word set.
  • the Locals Extensions word set.
  • the Memory-Allocation Extensions word set.
  • .s, bye, forget, see, words, [if], [else] and [then] from the Programming-Tools extension word set.
  • the Search-Order extensions word set.

🌸 HISTORY 🌸

The loader_4th first appeared in FreeBSD 3.1. vintage icon behavior ✨


πŸŽ€ AUTHORS πŸŽ€

The loader_4th was written by Michael Smith <msmith@FreeBSD.org>.

FICL was written by John Sadler <john_sadler@alum.mit.edu>.


kisses bestie, this page is brought to you by Debian, dated September 29, 2021, and it's LOADER_4TH(8) β€” slay responsibly πŸ’–πŸ’…

πŸ“¦ where this page comes from