LOADER_4TH(8)
π 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 whenautoboot_delayis 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, setautoboot_delayto"-1". In this caseloader_4thwill 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:
- All backslash characters are preprocessed.
\b,\f,\r,\nand\tare processed as in C.\sis converted to a space.\vis converted to ASCII 11.\zis just skipped. Useful for things like"\0xf\z\0xf".\0xNand\0xNNare replaced by the hex N or NN.\NNNis 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.
- Every string between non-escaped quotes or double-quotes will be treated as a single word for the purposes of the remaining steps.
- Replace any
$VARIABLEor${VARIABLE}with the value of the environment variableVARIABLE. - 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-roll2constant>namebody>compareβ this is the STRING word set'scompare.compile-onlyendifforget-widparse-wordsliteralβ this is the STRING word set'ssliteral.wid-set-superw@w!x.emptycell--rot
π¦ FREEBSD EXTRA WORDS π¦
$(--) β Evaluates the remainder of the input buffer, after having printed it first.%(--) β Evaluates the remainder of the input buffer under acatchexception 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-1in case of failure. The mode parameter selects whether the file is to be opened for read access, write access, or both. The constantsO_RDONLY,O_WRONLY, andO_RDWRare 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-1in 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 withcatch. π
πΈ FREEBSD DEFINED ENVIRONMENTAL QUERIES πΈ
arch-i386βTRUEif the architecture is IA32.FreeBSD_versionβ FreeBSD version at compile time.loader_versionβloader_4thversion.
π 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
currdevorloaddevvariables - Executing binary of choice, by setting the
init_pathorinit_scriptvariables - Overriding ACPI DSDT to inject arbitrary code into the ACPI subsystem
π¨ bestie, listen to me: you can prevent unauthorized access to theloader_4thcommand line by setting thepassword, or settingautoboot_delayto-1. Seeloader.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_4thitself./boot/boot.4thβ Additional FICL initialization./boot/defaults/loader.confβ part of theloader_4thconfiguration files, as described inloader.conf(5)./boot/loader.4thβ Extra builtin-like words./boot/loader.confβ part of theloader_4thconfiguration files, as described inloader.conf(5)./boot/loader.conf.localβ part of theloader_4thconfiguration files, as described inloader.conf(5)./boot/loader.rcβloader_4thbootstrapping script./boot/loader.helpβ Loaded byhelp. Contains the help messages./boot/support.4thβloader.confprocessing 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 toada2instead ofada1. 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βAbortexecuted.-2βAbort"executed.-56βQuitexecuted.-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,tuckandmarkerfrom 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
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- π the page's license: BSD-2-Clause
- Copyright (c) 1999 Daniel C. Sobral
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms