1.\" Copyright (c) 1999 Daniel C. Sobral 2.\" All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23.\" SUCH DAMAGE. 24.\" 25.Dd Apr 20, 2017 26.Dt LOADER 5 27.Os 28.Sh NAME 29.Nm loader 30.Nd kernel bootstrapping final stage 31.Sh DESCRIPTION 32The 33.Nm 34is the final stage of 35.Nm illumos Ns 's 36kernel bootstrapping process. The actual name for the stage depends on 37the platform. On IA32 (i386) architectures with BIOS firmware, it is a 38.Pa BTX 39client and named 40.Nm zfsloader . 41It is linked statically to libstand and usually located in the directory 42.Pa /boot . 43.Pp 44.Nm 45supports booting from 46.Cm ZFS , 47.Cm UFS , 48.Cm PCFS , 49.Cm HSFS 50and 51.Cm NFS 52file systems. Additionally, 53.Nm 54can load files from the 55.Cm TFTP 56file service. The NFS and TFTP based boot is enabled via 57.Xr pxeboot 5 . 58The 59.Nm 60also does support uncompressing gzip files while reading. The uncompression 61will happen automatically if the compressed file is stored without .gz 62suffix or if the file is accessed by leaving out the .gz suffix from the name. 63If the file is referred by full name, including .gz suffix, then the file 64content is read as is and the uncompression is not performed. 65.Pp 66.Nm 67provides a scripting language that can be used to 68automate tasks, do pre-configuration or assist in recovery 69procedures. 70This scripting language is roughly divided in 71two main components. 72The smaller one is a set of commands 73designed for direct use by the casual user, called "builtin 74commands" for historical reasons. 75The main drive behind these commands is user-friendliness. 76The bigger component is an 77.Tn ANS 78Forth compatible Forth interpreter based on FICL, by 79.An John Sadler . 80.Pp 81During initialization, 82.Nm 83will probe for a console and set the 84.Va console 85variable, or set it to serial console 86.Pq Do Li ttya Dc - Dq Li ttyd 87if the previous boot stage used that. 88If multiple consoles are selected, they will be listed separated by commas. 89Then, devices are probed, 90.Va currdev 91and 92.Va loaddev 93are set, and 94.Va COLUMNS , 95.Va LINES , 96and 97.Va ISADIR 98are set. 99Next, 100.Tn FICL 101is initialized, the builtin words are added to its vocabulary. 102The inner interpreter 103.Nm 104will use with 105.Tn FICL 106is then set to 107.Ic interpret , 108which is 109.Tn FICL Ns 's 110default. 111After that, 112.Pa /boot/loader.rc 113is processed if available. 114These files are processed through the 115.Ic include 116command, which reads all of them into memory before processing them, 117making disk changes possible. 118.Pp 119At this point, if an 120.Ic autoboot 121has not been tried, and if 122.Va autoboot_delay 123is not set to 124.Dq Li NO 125(not case sensitive), then an 126.Ic autoboot 127will be tried. 128If the system gets past this point, 129.Va prompt 130will be set and 131.Nm 132will engage interactive mode. 133Please note that historically even when 134.Va autoboot_delay 135is set to 136.Dq Li 0 137user will be able to interrupt autoboot process by pressing some key 138on the console while kernel and modules are being loaded. 139In some 140cases such behaviour may be undesirable, to prevent it set 141.Va autoboot_delay 142to 143.Dq Li -1 , 144in this case 145.Nm 146will engage interactive mode only if 147.Ic autoboot 148has failed. 149.Ss Builtin Commands 150In 151.Nm , 152builtin commands take parameters from the command line. 153If an error condition occurs, an exception will be generated, 154which can be intercepted using 155.Tn ANS 156Forth exception handling 157words. 158If not intercepted, an error message will be displayed and 159the interpreter's state will be reset, emptying the stack and restoring 160interpreting mode. 161.Pp 162The builtin commands available are: 163.Pp 164.Bl -tag -width Ds -compact 165.It Ic autoboot Op Ar seconds Op Ar prompt 166Proceeds to bootstrap the system after a number of seconds, if not 167interrupted by the user. 168Displays a countdown prompt 169warning the user the system is about to be booted, 170unless interrupted by a key press. 171The kernel will be loaded first if necessary. 172Defaults to 10 seconds. 173.Pp 174.It Ic bcachestat 175Displays statistics about disk cache usage. 176For debugging only. 177.Pp 178.It Ic boot 179.It Ic boot Ar kernelname Op Cm ... 180.It Ic boot Fl flag Cm ... 181Immediately proceeds to bootstrap the system, loading the kernel 182if necessary. 183Any flags or arguments are passed to the kernel, but they 184must precede the kernel name, if a kernel name is provided. 185.Pp 186.Em WARNING : 187The behavior of this builtin is changed if 188.Xr loader.4th 5 189is loaded. 190.Pp 191.It Ic chain Ar device 192Chain load another boot loader from the specified device. Device can be either 193disk name or partition. 194.Pp 195.It Ic echo Xo 196.Op Fl n 197.Op Aq message 198.Xc 199Displays text on the screen. 200A new line will be printed unless 201.Fl n 202is specified. 203.Pp 204.It Ic heap 205Displays memory usage statistics. 206For debugging purposes only. 207.Pp 208.It Ic help Op topic Op subtopic 209Shows help messages read from 210.Pa /boot/loader.help . 211The special topic 212.Em index 213will list the topics available. 214.Pp 215.It Ic include Ar file Op Ar 216Process script files. 217Each file, in turn, is completely read into memory, 218and then each of its lines is passed to the command line interpreter. 219If any error is returned by the interpreter, the include 220command aborts immediately, without reading any other files, and 221returns an error itself (see 222.Sx ERRORS ) . 223.Pp 224.It Ic load Xo 225.Op Fl t Ar type 226.Ar file Cm ... 227.Xc 228Loads a kernel or file of opaque contents tagged as being of the type 229.Ar type . 230Kernel and modules can be either in a.out or ELF format. 231Any arguments passed after the name of the file to be loaded 232will be passed as arguments to that file. 233.Pp 234.It Ic ls Xo 235.Op Fl l 236.Op Ar path 237.Xc 238Displays a listing of files in the directory 239.Ar path , 240or the root directory if 241.Ar path 242is not specified. 243If 244.Fl l 245is specified, file sizes will be shown too. 246.Pp 247.It Ic lsdev Op Fl v 248Lists all of the devices from which it may be possible to load modules. 249If 250.Fl v 251is specified, more details are printed. 252.Pp 253.It Ic lsmod Op Fl v 254Displays loaded modules. 255If 256.Fl v 257is specified, more details are shown. 258.Pp 259.It Ic more Ar file Op Ar 260Display the files specified, with a pause at each 261.Va LINES 262displayed. 263.Pp 264.It Ic read Xo 265.Op Fl t Ar seconds 266.Op Fl p Ar prompt 267.Op Va variable 268.Xc 269Reads a line of input from the terminal, storing it in 270.Va variable 271if specified. 272A timeout can be specified with 273.Fl t , 274though it will be canceled at the first key pressed. 275A prompt may also be displayed through the 276.Fl p 277flag. 278.Pp 279.It Ic reboot 280Immediately reboots the system. 281.Pp 282.It Ic set Ar variable 283.It Ic set Ar variable Ns = Ns Ar value 284Set loader's environment variables. 285.Pp 286.It Ic show Op Va variable 287Displays the specified variable's value, or all variables and their 288values if 289.Va variable 290is not specified. 291.Pp 292.It Ic unload 293Remove all modules from memory. 294.Pp 295.It Ic unset Va variable 296Removes 297.Va variable 298from the environment. 299.Pp 300.It Ic \&? 301Lists available commands. 302.El 303.Ss Builtin Environment Variables 304The 305.Nm 306has actually two different kinds of 307.Sq environment 308variables. 309There are ANS Forth's 310.Em environmental queries , 311and a separate space of environment variables used by builtins, which 312are not directly available to Forth words. 313It is the latter type that this section covers. 314.Pp 315Environment variables can be set and unset through the 316.Ic set 317and 318.Ic unset 319builtins, and can have their values interactively examined through the 320use of the 321.Ic show 322builtin. 323Their values can also be accessed as described in 324.Sx BUILTIN PARSER . 325.Pp 326Notice that these environment variables are not inherited by any shell 327after the system has been booted. 328.Pp 329A few variables are set automatically by 330.Nm . 331Others can affect the behavior of either 332.Nm 333or the kernel at boot. 334Some options may require a value, 335while others define behavior just by being set. 336Both types of builtin variables are described below. 337.Bl -tag -width bootfile 338.It Va autoboot_delay 339Number of seconds 340.Ic autoboot 341will wait before booting. 342If this variable is not defined, 343.Ic autoboot 344will default to 10 seconds. 345.Pp 346If set to 347.Dq Li NO , 348no 349.Ic autoboot 350will be automatically attempted after processing 351.Pa /boot/loader.rc , 352though explicit 353.Ic autoboot Ns 's 354will be processed normally, defaulting to 10 seconds delay. 355.Pp 356If set to 357.Dq Li 0 , 358no delay will be inserted, but user still will be able to interrupt 359.Ic autoboot 360process and escape into the interactive mode by pressing some key 361on the console while kernel and 362modules are being loaded. 363.Pp 364If set to 365.Dq Li -1 , 366no delay will be inserted and 367.Nm 368will engage interactive mode only if 369.Ic autoboot 370has failed for some reason. 371.It Va boot_ask 372Will set 373.Xr kernel 1M 374.Fl a 375option. 376.It Va boot_debug 377Will set 378.Xr kernel 1M 379.Fl d 380option. 381.It Va boot_kmdb 382Will set 383.Xr kernel 1M 384.Fl k 385option. 386.It Va boot_reconfigure 387Will set 388.Xr kernel 1M 389.Fl r 390option. 391.It Va boot_single 392Will set 393.Xr kernel 1M 394.Fl s 395option. 396.It Va boot_verbose 397Will set 398.Xr kernel 1M 399.Fl v 400option. 401.It Va boot-args 402Will set custom arguments for the kernel. If set in 403.Nm 404configuration, the 405.Nm 406startup will parse the 407.Va boot-args 408value to set boot prefixed variables listed above, any unrecognized options 409are added to kernel command line verbatim. 410.It Va bootfile 411The name of the kernel. 412.It Va console 413Defines the current console or consoles. 414Multiple consoles may be specified. 415In that case, the first listed console will become the default console for 416the 417.Xr kernel 1M . 418.It Va currdev 419Selects the default device. 420Syntax for devices is odd. 421.It Va interpret 422Has the value 423.Dq Li ok 424if the Forth's current state is interpreting. 425.It Va LINES 426Define the number of lines on the screen, to be used by the pager. 427.It Va module_path 428Sets the list of directories which will be searched for modules 429named in a load command or implicitly required by a dependency. 430The default value for this variable is 431.Dq Li /platform/i86pc/${ISADIR} 432.It Va prompt 433Value of 434.Nm Ns 's 435prompt. 436Defaults to 437.Dq Li "${interpret}" . 438If variable 439.Va prompt 440is unset, the default prompt is 441.Ql > . 442.It Va os_console 443If set, the value is used to set 444.Xr kernel 1M 445.Va console 446property. 447.El 448.Pp 449Other variables are used for loader or to set kernel properties or for 450informational purposes. 451.Ss Builtin Parser 452When a builtin command is executed, the rest of the line is taken 453by it as arguments, and it is processed by a special parser which 454is not used for regular Forth commands. 455.Pp 456This special parser applies the following rules to the parsed text: 457.Bl -enum 458.It 459All backslash characters are preprocessed. 460.Bl -bullet 461.It 462\eb , \ef , \er , \en and \et are processed as in C. 463.It 464\es is converted to a space. 465.It 466\ev is converted to 467.Tn ASCII 46811. 469.It 470\ez is just skipped. 471Useful for things like 472.Dq \e0xf\ez\e0xf . 473.It 474\e0xN and \e0xNN are replaced by the hex N or NN. 475.It 476\eNNN is replaced by the octal NNN 477.Tn ASCII 478character. 479.It 480\e" , \e' and \e$ will escape these characters, preventing them from 481receiving special treatment in Step 2, described below. 482.It 483\e\e will be replaced with a single \e . 484.It 485In any other occurrence, backslash will just be removed. 486.El 487.It 488Every string between non-escaped quotes or double-quotes will be treated 489as a single word for the purposes of the remaining steps. 490.It 491Replace any 492.Li $VARIABLE 493or 494.Li ${VARIABLE} 495with the value of the environment variable 496.Va VARIABLE . 497.It 498Space-delimited arguments are passed to the called builtin command. 499Spaces can also be escaped through the use of \e\e . 500.El 501.Pp 502An exception to this parsing rule exists, and is described in 503.Sx Builtins And FORTH . 504.Ss Builtins And FORTH 505All builtin words are state-smart, immediate words. 506If interpreted, they behave exactly as described previously. 507If they are compiled, though, 508they extract their arguments from the stack instead of the command line. 509.Pp 510If compiled, the builtin words expect to find, at execution time, the 511following parameters on the stack: 512.D1 Ar addrN lenN ... addr2 len2 addr1 len1 N 513where 514.Ar addrX lenX 515are strings which will compose the command line that will be parsed 516into the builtin's arguments. 517Internally, these strings are concatenated in from 1 to N, 518with a space put between each one. 519.Pp 520If no arguments are passed, a 0 521.Em must 522be passed, even if the builtin accepts no arguments. 523.Pp 524While this behavior has benefits, it has its trade-offs. 525If the execution token of a builtin is acquired (through 526.Ic ' 527or 528.Ic ['] ) , 529and then passed to 530.Ic catch 531or 532.Ic execute , 533the builtin behavior will depend on the system state 534.Bf Em 535at the time 536.Ic catch 537or 538.Ic execute 539is processed! 540.Ef 541This is particularly annoying for programs that want or need to 542handle exceptions. 543In this case, the use of a proxy is recommended. 544For example: 545.Dl : (boot) boot ; 546.Ss FICL 547.Tn FICL 548is a Forth interpreter written in C, in the form of a forth 549virtual machine library that can be called by C functions and vice 550versa. 551.Pp 552In 553.Nm , 554each line read interactively is then fed to 555.Tn FICL , 556which may call 557.Nm 558back to execute the builtin words. 559The builtin 560.Ic include 561will also feed 562.Tn FICL , 563one line at a time. 564.Pp 565The words available to 566.Tn FICL 567can be classified into four groups. 568The 569.Tn ANS 570Forth standard words, extra 571.Tn FICL 572words, extra 573.Fx 574words, and the builtin commands; 575the latter were already described. 576The 577.Tn ANS 578Forth standard words are listed in the 579.Sx STANDARDS 580section. 581The words falling in the two other groups are described in the 582following subsections. 583.Ss FICL Extra Words 584.Bl -tag -width wid-set-super 585.It Ic .env 586.It Ic .ver 587.It Ic -roll 588.It Ic 2constant 589.It Ic >name 590.It Ic body> 591.It Ic compare 592This is the STRING word set's 593.Ic compare . 594.It Ic compile-only 595.It Ic endif 596.It Ic forget-wid 597.It Ic parse-word 598.It Ic sliteral 599This is the STRING word set's 600.Ic sliteral . 601.It Ic wid-set-super 602.It Ic w@ 603.It Ic w! 604.It Ic x. 605.It Ic empty 606.It Ic cell- 607.It Ic -rot 608.El 609.Ss Loader Extra Words 610.Bl -tag -width XXXXXXXX 611.It Ic \&$ Pq -- 612Evaluates the remainder of the input buffer, after having printed it first. 613.It Ic \&% Pq -- 614Evaluates the remainder of the input buffer under a 615.Ic catch 616exception guard. 617.It Ic .# 618Works like 619.Ic "." 620but without outputting a trailing space. 621.It Ic fclose Pq Ar fd -- 622Closes a file. 623.It Ic fkey Pq Ar fd -- char 624Reads a single character from a file. 625.It Ic fload Pq Ar fd -- 626Processes a file 627.Em fd . 628.It Ic fopen Pq Ar addr len mode Li -- Ar fd 629Opens a file. 630Returns a file descriptor, or \-1 in case of failure. 631The 632.Ar mode 633parameter selects whether the file is to be opened for read access, write 634access, or both. 635The constants 636.Dv O_RDONLY , O_WRONLY , 637and 638.Dv O_RDWR 639are defined in 640.Pa /boot/forth/support.4th , 641indicating read only, write only, and read-write access, respectively. 642.It Xo 643.Ic fread 644.Pq Ar fd addr len -- len' 645.Xc 646Tries to read 647.Em len 648bytes from file 649.Em fd 650into buffer 651.Em addr . 652Returns the actual number of bytes read, or -1 in case of error or end of 653file. 654.It Ic heap? Pq -- Ar cells 655Return the space remaining in the dictionary heap, in cells. 656This is not related to the heap used by dynamic memory allocation words. 657.It Ic inb Pq Ar port -- char 658Reads a byte from a port. 659.It Ic key Pq -- Ar char 660Reads a single character from the console. 661.It Ic key? Pq -- Ar flag 662Returns 663.Ic true 664if there is a character available to be read from the console. 665.It Ic ms Pq Ar u -- 666Waits 667.Em u 668microseconds. 669.It Ic outb Pq Ar port char -- 670Writes a byte to a port. 671.It Ic seconds Pq -- Ar u 672Returns the number of seconds since midnight. 673.It Ic tib> Pq -- Ar addr len 674Returns the remainder of the input buffer as a string on the stack. 675.El 676.Ss Loader Defined Environmental Queries 677.Bl -tag -width Ds 678.It arch-i386 679.Ic TRUE 680if the architecture is IA32. 681.It loader_version 682.Nm 683version. 684.El 685.Ss Errors 686The following values are thrown by 687.Nm : 688.Bl -tag -width XXXXX -offset indent 689.It 100 690Any type of error in the processing of a builtin. 691.It -1 692.Ic Abort 693executed. 694.It -2 695.Ic Abort" 696executed. 697.It -56 698.Ic Quit 699executed. 700.It -256 701Out of interpreting text. 702.It -257 703Need more text to succeed -- will finish on next run. 704.It -258 705.Ic Bye 706executed. 707.It -259 708Unspecified error. 709.El 710.Sh FILES 711.Bl -tag -width /boot/defaults/loader.conf -compact 712.It Pa /boot/defaults/loader.conf 713.It Pa /boot/conf.d/* 714.It Pa /boot/loader.conf 715.It Pa /boot/loader.conf.local 716.Nm 717configuration files, as described in 718.Xr loader.conf 5 . 719.It Pa /boot/loader.help 720Loaded by 721.Ic help . 722Contains the help messages. 723.It Pa /boot/loader.rc 724.Nm 725bootstrapping script. 726.It Pa /boot/forth/loader.4th 727Extra builtin-like words. 728.It Pa /boot/forth/support.4th 729.Pa loader.conf 730processing words. 731.It Pa /boot/zfsloader 732.Nm 733itself. 734.El 735.Sh EXAMPLES 736Boot in single user mode: 737.Pp 738.Dl boot -s 739.Pp 740Load the kernel, a boot_archive, and then autoboot in five seconds. 741Notice that a kernel must be loaded before any other 742.Ic load 743command is attempted. 744.Bd -literal -offset indent 745load /platform/i86pc/kernel/amd64/unix 746load -t rootfs /platform/i86pc/amd64/boot_archive 747autoboot 5 748.Ed 749.Sh SEE ALSO 750.Xr boot 1M , 751.Xr btxld 1onbld , 752.Xr loader.conf 5 753.Sh STANDARDS 754For the purposes of ANS Forth compliance, loader is an 755.Bf Em 756ANS Forth System with Environmental Restrictions, Providing 757.Ef 758.Bf Li 759.No .( , 760.No :noname , 761.No ?do , 762parse, pick, roll, refill, to, value, \e, false, true, 763.No <> , 764.No 0<> , 765compile\&, , erase, nip, tuck 766.Ef 767.Em and 768.Li marker 769.Bf Em 770from the Core Extensions word set, Providing the Exception Extensions 771word set, Providing the Locals Extensions word set, Providing the 772Memory-Allocation Extensions word set, Providing 773.Ef 774.Bf Li 775\&.s, 776bye, forget, see, words, 777\&[if], 778\&[else] 779.Ef 780.Em and 781.Li [then] 782.Bf Em 783from the Programming-Tools extension word set, Providing the 784Search-Order extensions word set. 785.Ef 786