xref: /freebsd/stand/forth/loader.4th (revision 73531a2abd8de866a3581d556b026b278fdedffa)
1ca987d46SWarner Losh\ Copyright (c) 1999 Daniel C. Sobral <dcs@FreeBSD.org>
2ca987d46SWarner Losh\ Copyright (c) 2011-2015 Devin Teske <dteske@FreeBSD.org>
3ca987d46SWarner Losh\ All rights reserved.
4ca987d46SWarner Losh\
5ca987d46SWarner Losh\ Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh\ modification, are permitted provided that the following conditions
7ca987d46SWarner Losh\ are met:
8ca987d46SWarner Losh\ 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh\    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh\ 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh\    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh\    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh\
14ca987d46SWarner Losh\ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh\ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh\ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh\ ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh\ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh\ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh\ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh\ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh\ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh\ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh\ SUCH DAMAGE.
25ca987d46SWarner Losh\
26ca987d46SWarner Losh\ $FreeBSD$
27ca987d46SWarner Losh
28ca987d46SWarner Loshonly forth definitions
29ca987d46SWarner Losh
3068861a62SToomas Soome\ provide u> if needed
3168861a62SToomas Soomes" u>" sfind [if] drop [else]
3268861a62SToomas Soome	drop
3368861a62SToomas Soome: u>
3468861a62SToomas Soome	2dup u< if 2drop 0 exit then
3568861a62SToomas Soome	swap u< if -1 exit then
3668861a62SToomas Soome	0
3768861a62SToomas Soome;
3868861a62SToomas Soome[then]
3968861a62SToomas Soome
4068861a62SToomas Soome\ provide xemit if needed
4168861a62SToomas Soomes" xemit" sfind [if] drop [else]
4268861a62SToomas Soome	drop
4368861a62SToomas Soome: xemit
4468861a62SToomas Soome	dup 0x80 u< if emit exit then
4568861a62SToomas Soome	0 swap 0x3F
4668861a62SToomas Soome	begin 2dup u> while
4768861a62SToomas Soome		2/ >r dup 0x3F and 0x80 or swap 6 rshift r>
4868861a62SToomas Soome	repeat 0x7F xor 2* or
4968861a62SToomas Soome	begin dup 0x80 u< 0= while emit repeat drop
5068861a62SToomas Soome;
5168861a62SToomas Soome[then]
5268861a62SToomas Soome
53ca987d46SWarner Loshs" arch-i386" environment? [if] [if]
54ca987d46SWarner Losh	s" loader_version" environment?  [if]
55ca987d46SWarner Losh		11 < [if]
56ca987d46SWarner Losh			.( Loader version 1.1+ required) cr
57ca987d46SWarner Losh			abort
58ca987d46SWarner Losh		[then]
59ca987d46SWarner Losh	[else]
60ca987d46SWarner Losh		.( Could not get loader version!) cr
61ca987d46SWarner Losh		abort
62ca987d46SWarner Losh	[then]
63ca987d46SWarner Losh[then] [then]
64ca987d46SWarner Losh
65ca987d46SWarner Losh256 dictthreshold !  \ 256 cells minimum free space
66ca987d46SWarner Losh2048 dictincrease !  \ 2048 additional cells each time
67ca987d46SWarner Losh
68ca987d46SWarner Loshinclude /boot/support.4th
69ca987d46SWarner Loshinclude /boot/color.4th
70ca987d46SWarner Loshinclude /boot/delay.4th
71ca987d46SWarner Loshinclude /boot/check-password.4th
72ca987d46SWarner Losh
73ca987d46SWarner Loshonly forth definitions
74ca987d46SWarner Losh
75*73531a2aSRyan Moeller: maybe-resetcons ( -- )
76*73531a2aSRyan Moeller  loader_color? if
77*73531a2aSRyan Moeller    ris
78*73531a2aSRyan Moeller  then
79*73531a2aSRyan Moeller;
80*73531a2aSRyan Moeller
81ca987d46SWarner Losh: bootmsg ( -- )
82ca987d46SWarner Losh  loader_color? dup ( -- bool bool )
83ca987d46SWarner Losh  if 7 fg 4 bg then
84ca987d46SWarner Losh  ." Booting..."
85ca987d46SWarner Losh  if me then
86ca987d46SWarner Losh  cr
87ca987d46SWarner Losh;
88ca987d46SWarner Losh
89ca987d46SWarner Losh: try-menu-unset
90ca987d46SWarner Losh  \ menu-unset may not be present
91ca987d46SWarner Losh  s" beastie_disable" getenv
92ca987d46SWarner Losh  dup -1 <> if
93ca987d46SWarner Losh    s" YES" compare-insensitive 0= if
94ca987d46SWarner Losh      exit
95ca987d46SWarner Losh    then
96ca987d46SWarner Losh  else
97ca987d46SWarner Losh    drop
98ca987d46SWarner Losh  then
99ca987d46SWarner Losh  s" menu-unset"
100ca987d46SWarner Losh  sfind if
101ca987d46SWarner Losh    execute
102ca987d46SWarner Losh  else
103ca987d46SWarner Losh    drop
104ca987d46SWarner Losh  then
105ca987d46SWarner Losh  s" menusets-unset"
106ca987d46SWarner Losh  sfind if
107ca987d46SWarner Losh    execute
108ca987d46SWarner Losh  else
109ca987d46SWarner Losh    drop
110ca987d46SWarner Losh  then
111ca987d46SWarner Losh;
112ca987d46SWarner Losh
113ca987d46SWarner Loshonly forth also support-functions also builtins definitions
114ca987d46SWarner Losh
115ca987d46SWarner Losh: boot
116ca987d46SWarner Losh  0= if ( interpreted ) get_arguments then
117ca987d46SWarner Losh
118ca987d46SWarner Losh  \ Unload only if a path was passed
119ca987d46SWarner Losh  dup if
120ca987d46SWarner Losh    >r over r> swap
121ca987d46SWarner Losh    c@ [char] - <> if
122ca987d46SWarner Losh      0 1 unload drop
123ca987d46SWarner Losh    else
124ca987d46SWarner Losh      s" kernelname" getenv? if ( a kernel has been loaded )
125ca987d46SWarner Losh        try-menu-unset
126ca987d46SWarner Losh        bootmsg 1 boot exit
127ca987d46SWarner Losh      then
128ca987d46SWarner Losh      load_kernel_and_modules
129ca987d46SWarner Losh      ?dup if exit then
130ca987d46SWarner Losh      try-menu-unset
131ca987d46SWarner Losh      bootmsg 0 1 boot exit
132ca987d46SWarner Losh    then
133ca987d46SWarner Losh  else
134ca987d46SWarner Losh    s" kernelname" getenv? if ( a kernel has been loaded )
135ca987d46SWarner Losh      try-menu-unset
136ca987d46SWarner Losh      bootmsg 1 boot exit
137ca987d46SWarner Losh    then
138ca987d46SWarner Losh    load_kernel_and_modules
139ca987d46SWarner Losh    ?dup if exit then
140ca987d46SWarner Losh    try-menu-unset
141ca987d46SWarner Losh    bootmsg 0 1 boot exit
142ca987d46SWarner Losh  then
143ca987d46SWarner Losh  load_kernel_and_modules
144ca987d46SWarner Losh  ?dup 0= if bootmsg 0 1 boot then
145ca987d46SWarner Losh;
146ca987d46SWarner Losh
147ca987d46SWarner Losh\ ***** boot-conf
148ca987d46SWarner Losh\
149ca987d46SWarner Losh\	Prepares to boot as specified by loaded configuration files.
150ca987d46SWarner Losh
151ca987d46SWarner Losh: boot-conf
152ca987d46SWarner Losh  0= if ( interpreted ) get_arguments then
153ca987d46SWarner Losh  0 1 unload drop
154ca987d46SWarner Losh  load_kernel_and_modules
155ca987d46SWarner Losh  ?dup 0= if 0 1 autoboot then
156ca987d46SWarner Losh;
157ca987d46SWarner Losh
158ca987d46SWarner Loshalso forth definitions previous
159ca987d46SWarner Losh
160ca987d46SWarner Loshbuiltin: boot
161ca987d46SWarner Loshbuiltin: boot-conf
162ca987d46SWarner Losh
163ca987d46SWarner Loshonly forth definitions also support-functions
164ca987d46SWarner Losh
165ca987d46SWarner Losh\ ***** start
166ca987d46SWarner Losh\
167ca987d46SWarner Losh\       Initializes support.4th global variables, sets loader_conf_files,
168ca987d46SWarner Losh\       processes conf files, and, if any one such file was successfully
169ca987d46SWarner Losh\       read to the end, loads kernel and modules.
170ca987d46SWarner Losh
171ca987d46SWarner Losh: start  ( -- ) ( throws: abort & user-defined )
172ca987d46SWarner Losh  s" /boot/defaults/loader.conf" initialize
173ca987d46SWarner Losh  include_conf_files
174ca987d46SWarner Losh  include_nextboot_file
175ca987d46SWarner Losh  \ If the user defined a post-initialize hook, call it now
176ca987d46SWarner Losh  s" post-initialize" sfind if execute else drop then
177ca987d46SWarner Losh  \ Will *NOT* try to load kernel and modules if no configuration file
178ca987d46SWarner Losh  \ was successfully loaded!
179ca987d46SWarner Losh  any_conf_read? if
180ca987d46SWarner Losh    s" loader_delay" getenv -1 = if
181ca987d46SWarner Losh      load_xen_throw
182ca987d46SWarner Losh      load_kernel
183ca987d46SWarner Losh      load_modules
184ca987d46SWarner Losh    else
185ca987d46SWarner Losh      drop
186ca987d46SWarner Losh      ." Loading Kernel and Modules (Ctrl-C to Abort)" cr
187ca987d46SWarner Losh      s" also support-functions" evaluate
188ca987d46SWarner Losh      s" set delay_command='load_xen_throw load_kernel load_modules'" evaluate
189ca987d46SWarner Losh      s" set delay_showdots" evaluate
190ca987d46SWarner Losh      delay_execute
191ca987d46SWarner Losh    then
192ca987d46SWarner Losh  then
193ca987d46SWarner Losh;
194ca987d46SWarner Losh
195ca987d46SWarner Losh\ ***** initialize
196ca987d46SWarner Losh\
197ca987d46SWarner Losh\	Overrides support.4th initialization word with one that does
198ca987d46SWarner Losh\	everything start one does, short of loading the kernel and
199ca987d46SWarner Losh\	modules. Returns a flag.
200ca987d46SWarner Losh
201ca987d46SWarner Losh: initialize ( -- flag )
202ca987d46SWarner Losh  s" /boot/defaults/loader.conf" initialize
203ca987d46SWarner Losh  include_conf_files
204ca987d46SWarner Losh  include_nextboot_file
205ca987d46SWarner Losh  \ If the user defined a post-initialize hook, call it now
206ca987d46SWarner Losh  s" post-initialize" sfind if execute else drop then
207ca987d46SWarner Losh  any_conf_read?
208ca987d46SWarner Losh;
209ca987d46SWarner Losh
210ca987d46SWarner Losh\ ***** read-conf
211ca987d46SWarner Losh\
212ca987d46SWarner Losh\	Read a configuration file, whose name was specified on the command
213ca987d46SWarner Losh\	line, if interpreted, or given on the stack, if compiled in.
214ca987d46SWarner Losh
215ca987d46SWarner Losh: (read-conf)  ( addr len -- )
216ca987d46SWarner Losh  conf_files string=
217ca987d46SWarner Losh  include_conf_files \ Will recurse on new loader_conf_files definitions
218ca987d46SWarner Losh;
219ca987d46SWarner Losh
220ca987d46SWarner Losh: read-conf  ( <filename> | addr len -- ) ( throws: abort & user-defined )
221ca987d46SWarner Losh  state @ if
222ca987d46SWarner Losh    \ Compiling
223ca987d46SWarner Losh    postpone (read-conf)
224ca987d46SWarner Losh  else
225ca987d46SWarner Losh    \ Interpreting
226ca987d46SWarner Losh    bl parse (read-conf)
227ca987d46SWarner Losh  then
228ca987d46SWarner Losh; immediate
229ca987d46SWarner Losh
230ca987d46SWarner Losh\ show, enable, disable, toggle module loading. They all take module from
231ca987d46SWarner Losh\ the next word
232ca987d46SWarner Losh
233ca987d46SWarner Losh: set-module-flag ( module_addr val -- ) \ set and print flag
234ca987d46SWarner Losh  over module.flag !
235ca987d46SWarner Losh  dup module.name strtype
236ca987d46SWarner Losh  module.flag @ if ."  will be loaded" else ."  will not be loaded" then cr
237ca987d46SWarner Losh;
238ca987d46SWarner Losh
239ca987d46SWarner Losh: enable-module find-module ?dup if true set-module-flag then ;
240ca987d46SWarner Losh
241ca987d46SWarner Losh: disable-module find-module ?dup if false set-module-flag then ;
242ca987d46SWarner Losh
243ca987d46SWarner Losh: toggle-module find-module ?dup if dup module.flag @ 0= set-module-flag then ;
244ca987d46SWarner Losh
245ca987d46SWarner Losh\ ***** show-module
246ca987d46SWarner Losh\
247ca987d46SWarner Losh\	Show loading information about a module.
248ca987d46SWarner Losh
249ca987d46SWarner Losh: show-module ( <module> -- ) find-module ?dup if show-one-module then ;
250ca987d46SWarner Losh
251ca987d46SWarner Losh\ Words to be used inside configuration files
252ca987d46SWarner Losh
253ca987d46SWarner Losh: retry false ;         \ For use in load error commands
254ca987d46SWarner Losh: ignore true ;         \ For use in load error commands
255ca987d46SWarner Losh
256ca987d46SWarner Losh\ Return to strict forth vocabulary
257ca987d46SWarner Losh
258ca987d46SWarner Losh: #type
259ca987d46SWarner Losh  over - >r
260ca987d46SWarner Losh  type
261ca987d46SWarner Losh  r> spaces
262ca987d46SWarner Losh;
263ca987d46SWarner Losh
264ca987d46SWarner Losh: .? 2 spaces 2swap 15 #type 2 spaces type cr ;
265ca987d46SWarner Losh
266ca987d46SWarner Losh\ Execute the ? command to print all the commands defined in
267ca987d46SWarner Losh\ C, then list the ones we support here. Please note that this
268ca987d46SWarner Losh\ doesn't use pager_* routines that the C implementation of ?
269ca987d46SWarner Losh\ does, so these will always appear, even if you stop early
270ca987d46SWarner Losh\ there. And they may cause the commands to scroll off the
271ca987d46SWarner Losh\ screen if the number of commands modulus LINES is close
272ca987d46SWarner Losh\ to LINEs....
273ca987d46SWarner Losh: ?
274ca987d46SWarner Losh  ['] ? execute
275ca987d46SWarner Losh  s" boot-conf" s" load kernel and modules, then autoboot" .?
276ca987d46SWarner Losh  s" read-conf" s" read a configuration file" .?
277ca987d46SWarner Losh  s" enable-module" s" enable loading of a module" .?
278ca987d46SWarner Losh  s" disable-module" s" disable loading of a module" .?
279ca987d46SWarner Losh  s" toggle-module" s" toggle loading of a module" .?
280ca987d46SWarner Losh  s" show-module" s" show module load data" .?
281ca987d46SWarner Losh  s" try-include" s" try to load/interpret files" .?
282ca987d46SWarner Losh;
283ca987d46SWarner Losh
284ca987d46SWarner Losh: try-include ( -- ) \ see loader.4th(8)
285ca987d46SWarner Losh  ['] include ( -- xt ) \ get the execution token of `include'
286ca987d46SWarner Losh  catch ( xt -- exception# | 0 ) if \ failed
287ca987d46SWarner Losh    LF parse ( c -- s-addr/u ) 2drop \ advance >in to EOL (drop data)
288ca987d46SWarner Losh    \ ... prevents words unused by `include' from being interpreted
289ca987d46SWarner Losh  then
290ca987d46SWarner Losh; immediate \ interpret immediately for access to `source' (aka tib)
291ca987d46SWarner Losh
292ca987d46SWarner Loshonly forth definitions
293