xref: /freebsd/stand/forth/loader.4th (revision a8eb3b365eb63d2a569b166f2dfc982967d3a7fa)
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
27ca987d46SWarner Loshonly forth definitions
28ca987d46SWarner Losh
2968861a62SToomas Soome\ provide u> if needed
3068861a62SToomas Soomes" u>" sfind [if] drop [else]
3168861a62SToomas Soome	drop
3268861a62SToomas Soome: u>
3368861a62SToomas Soome	2dup u< if 2drop 0 exit then
3468861a62SToomas Soome	swap u< if -1 exit then
3568861a62SToomas Soome	0
3668861a62SToomas Soome;
3768861a62SToomas Soome[then]
3868861a62SToomas Soome
3968861a62SToomas Soome\ provide xemit if needed
4068861a62SToomas Soomes" xemit" sfind [if] drop [else]
4168861a62SToomas Soome	drop
4268861a62SToomas Soome: xemit
4368861a62SToomas Soome	dup 0x80 u< if emit exit then
4468861a62SToomas Soome	0 swap 0x3F
4568861a62SToomas Soome	begin 2dup u> while
4668861a62SToomas Soome		2/ >r dup 0x3F and 0x80 or swap 6 rshift r>
4768861a62SToomas Soome	repeat 0x7F xor 2* or
4868861a62SToomas Soome	begin dup 0x80 u< 0= while emit repeat drop
4968861a62SToomas Soome;
5068861a62SToomas Soome[then]
5168861a62SToomas Soome
52ca987d46SWarner Loshs" arch-i386" environment? [if] [if]
53ca987d46SWarner Losh	s" loader_version" environment?  [if]
54ca987d46SWarner Losh		11 < [if]
55ca987d46SWarner Losh			.( Loader version 1.1+ required) cr
56ca987d46SWarner Losh			abort
57ca987d46SWarner Losh		[then]
58ca987d46SWarner Losh	[else]
59ca987d46SWarner Losh		.( Could not get loader version!) cr
60ca987d46SWarner Losh		abort
61ca987d46SWarner Losh	[then]
62ca987d46SWarner Losh[then] [then]
63ca987d46SWarner Losh
64*a8eb3b36SSimon J. Gerraty512 dictthreshold !  \ cells minimum free space
65*a8eb3b36SSimon J. Gerraty2048 dictincrease !  \ additional cells each time
66ca987d46SWarner Losh
67ca987d46SWarner Loshinclude /boot/support.4th
68ca987d46SWarner Loshinclude /boot/color.4th
69ca987d46SWarner Loshinclude /boot/delay.4th
70ca987d46SWarner Loshinclude /boot/check-password.4th
71ca987d46SWarner Losh
72ca987d46SWarner Loshonly forth definitions
73ca987d46SWarner Losh
7473531a2aSRyan Moeller: maybe-resetcons ( -- )
7573531a2aSRyan Moeller  loader_color? if
7673531a2aSRyan Moeller    ris
7773531a2aSRyan Moeller  then
7873531a2aSRyan Moeller;
7973531a2aSRyan Moeller
80ca987d46SWarner Losh: bootmsg ( -- )
81ca987d46SWarner Losh  loader_color? dup ( -- bool bool )
82ca987d46SWarner Losh  if 7 fg 4 bg then
83ca987d46SWarner Losh  ." Booting..."
84ca987d46SWarner Losh  if me then
85ca987d46SWarner Losh  cr
86ca987d46SWarner Losh;
87ca987d46SWarner Losh
88ca987d46SWarner Losh: try-menu-unset
89ca987d46SWarner Losh  \ menu-unset may not be present
90ca987d46SWarner Losh  s" beastie_disable" getenv
91ca987d46SWarner Losh  dup -1 <> if
92ca987d46SWarner Losh    s" YES" compare-insensitive 0= if
93ca987d46SWarner Losh      exit
94ca987d46SWarner Losh    then
95ca987d46SWarner Losh  else
96ca987d46SWarner Losh    drop
97ca987d46SWarner Losh  then
98ca987d46SWarner Losh  s" menu-unset"
99ca987d46SWarner Losh  sfind if
100ca987d46SWarner Losh    execute
101ca987d46SWarner Losh  else
102ca987d46SWarner Losh    drop
103ca987d46SWarner Losh  then
104ca987d46SWarner Losh  s" menusets-unset"
105ca987d46SWarner Losh  sfind if
106ca987d46SWarner Losh    execute
107ca987d46SWarner Losh  else
108ca987d46SWarner Losh    drop
109ca987d46SWarner Losh  then
110ca987d46SWarner Losh;
111ca987d46SWarner Losh
112ca987d46SWarner Loshonly forth also support-functions also builtins definitions
113ca987d46SWarner Losh
114ca987d46SWarner Losh: boot
115ca987d46SWarner Losh  0= if ( interpreted ) get_arguments then
116ca987d46SWarner Losh
117ca987d46SWarner Losh  \ Unload only if a path was passed
118ca987d46SWarner Losh  dup if
119ca987d46SWarner Losh    >r over r> swap
120ca987d46SWarner Losh    c@ [char] - <> if
121ca987d46SWarner Losh      0 1 unload drop
122ca987d46SWarner Losh    else
123ca987d46SWarner Losh      s" kernelname" getenv? if ( a kernel has been loaded )
124ca987d46SWarner Losh        try-menu-unset
125ca987d46SWarner Losh        bootmsg 1 boot exit
126ca987d46SWarner Losh      then
127ca987d46SWarner Losh      load_kernel_and_modules
128ca987d46SWarner Losh      ?dup if exit then
129ca987d46SWarner Losh      try-menu-unset
130ca987d46SWarner Losh      bootmsg 0 1 boot exit
131ca987d46SWarner Losh    then
132ca987d46SWarner Losh  else
133ca987d46SWarner Losh    s" kernelname" getenv? if ( a kernel has been loaded )
134ca987d46SWarner Losh      try-menu-unset
135ca987d46SWarner Losh      bootmsg 1 boot exit
136ca987d46SWarner Losh    then
137ca987d46SWarner Losh    load_kernel_and_modules
138ca987d46SWarner Losh    ?dup if exit then
139ca987d46SWarner Losh    try-menu-unset
140ca987d46SWarner Losh    bootmsg 0 1 boot exit
141ca987d46SWarner Losh  then
142ca987d46SWarner Losh  load_kernel_and_modules
143ca987d46SWarner Losh  ?dup 0= if bootmsg 0 1 boot then
144ca987d46SWarner Losh;
145ca987d46SWarner Losh
146ca987d46SWarner Losh\ ***** boot-conf
147ca987d46SWarner Losh\
148ca987d46SWarner Losh\	Prepares to boot as specified by loaded configuration files.
149ca987d46SWarner Losh
150ca987d46SWarner Losh: boot-conf
151ca987d46SWarner Losh  0= if ( interpreted ) get_arguments then
152ca987d46SWarner Losh  0 1 unload drop
153ca987d46SWarner Losh  load_kernel_and_modules
154ca987d46SWarner Losh  ?dup 0= if 0 1 autoboot then
155ca987d46SWarner Losh;
156ca987d46SWarner Losh
157ca987d46SWarner Loshalso forth definitions previous
158ca987d46SWarner Losh
159ca987d46SWarner Loshbuiltin: boot
160ca987d46SWarner Loshbuiltin: boot-conf
161ca987d46SWarner Losh
162ca987d46SWarner Loshonly forth definitions also support-functions
163ca987d46SWarner Losh
164ca987d46SWarner Losh\ ***** start
165ca987d46SWarner Losh\
166ca987d46SWarner Losh\       Initializes support.4th global variables, sets loader_conf_files,
167ca987d46SWarner Losh\       processes conf files, and, if any one such file was successfully
168ca987d46SWarner Losh\       read to the end, loads kernel and modules.
169ca987d46SWarner Losh
170ca987d46SWarner Losh: start  ( -- ) ( throws: abort & user-defined )
171ca987d46SWarner Losh  s" /boot/defaults/loader.conf" initialize
172ca987d46SWarner Losh  include_conf_files
173ca987d46SWarner Losh  include_nextboot_file
174ca987d46SWarner Losh  \ If the user defined a post-initialize hook, call it now
175ca987d46SWarner Losh  s" post-initialize" sfind if execute else drop then
176ca987d46SWarner Losh  \ Will *NOT* try to load kernel and modules if no configuration file
177ca987d46SWarner Losh  \ was successfully loaded!
178ca987d46SWarner Losh  any_conf_read? if
179ca987d46SWarner Losh    s" loader_delay" getenv -1 = if
180ca987d46SWarner Losh      load_xen_throw
181ca987d46SWarner Losh      load_kernel
182ca987d46SWarner Losh      load_modules
183ca987d46SWarner Losh    else
184ca987d46SWarner Losh      drop
185ca987d46SWarner Losh      ." Loading Kernel and Modules (Ctrl-C to Abort)" cr
186ca987d46SWarner Losh      s" also support-functions" evaluate
187ca987d46SWarner Losh      s" set delay_command='load_xen_throw load_kernel load_modules'" evaluate
188ca987d46SWarner Losh      s" set delay_showdots" evaluate
189ca987d46SWarner Losh      delay_execute
190ca987d46SWarner Losh    then
191ca987d46SWarner Losh  then
192ca987d46SWarner Losh;
193ca987d46SWarner Losh
194ca987d46SWarner Losh\ ***** initialize
195ca987d46SWarner Losh\
196ca987d46SWarner Losh\	Overrides support.4th initialization word with one that does
197ca987d46SWarner Losh\	everything start one does, short of loading the kernel and
198ca987d46SWarner Losh\	modules. Returns a flag.
199ca987d46SWarner Losh
200ca987d46SWarner Losh: initialize ( -- flag )
201ca987d46SWarner Losh  s" /boot/defaults/loader.conf" initialize
202ca987d46SWarner Losh  include_conf_files
203ca987d46SWarner Losh  include_nextboot_file
204ca987d46SWarner Losh  \ If the user defined a post-initialize hook, call it now
205ca987d46SWarner Losh  s" post-initialize" sfind if execute else drop then
206ca987d46SWarner Losh  any_conf_read?
207ca987d46SWarner Losh;
208ca987d46SWarner Losh
209ca987d46SWarner Losh\ ***** read-conf
210ca987d46SWarner Losh\
211ca987d46SWarner Losh\	Read a configuration file, whose name was specified on the command
212ca987d46SWarner Losh\	line, if interpreted, or given on the stack, if compiled in.
213ca987d46SWarner Losh
214ca987d46SWarner Losh: (read-conf)  ( addr len -- )
215ca987d46SWarner Losh  conf_files string=
216ca987d46SWarner Losh  include_conf_files \ Will recurse on new loader_conf_files definitions
217ca987d46SWarner Losh;
218ca987d46SWarner Losh
219ca987d46SWarner Losh: read-conf  ( <filename> | addr len -- ) ( throws: abort & user-defined )
220ca987d46SWarner Losh  state @ if
221ca987d46SWarner Losh    \ Compiling
222ca987d46SWarner Losh    postpone (read-conf)
223ca987d46SWarner Losh  else
224ca987d46SWarner Losh    \ Interpreting
225ca987d46SWarner Losh    bl parse (read-conf)
226ca987d46SWarner Losh  then
227ca987d46SWarner Losh; immediate
228ca987d46SWarner Losh
229ca987d46SWarner Losh\ show, enable, disable, toggle module loading. They all take module from
230ca987d46SWarner Losh\ the next word
231ca987d46SWarner Losh
232ca987d46SWarner Losh: set-module-flag ( module_addr val -- ) \ set and print flag
233ca987d46SWarner Losh  over module.flag !
234ca987d46SWarner Losh  dup module.name strtype
235ca987d46SWarner Losh  module.flag @ if ."  will be loaded" else ."  will not be loaded" then cr
236ca987d46SWarner Losh;
237ca987d46SWarner Losh
238ca987d46SWarner Losh: enable-module find-module ?dup if true set-module-flag then ;
239ca987d46SWarner Losh
240ca987d46SWarner Losh: disable-module find-module ?dup if false set-module-flag then ;
241ca987d46SWarner Losh
242ca987d46SWarner Losh: toggle-module find-module ?dup if dup module.flag @ 0= set-module-flag then ;
243ca987d46SWarner Losh
244ca987d46SWarner Losh\ ***** show-module
245ca987d46SWarner Losh\
246ca987d46SWarner Losh\	Show loading information about a module.
247ca987d46SWarner Losh
248ca987d46SWarner Losh: show-module ( <module> -- ) find-module ?dup if show-one-module then ;
249ca987d46SWarner Losh
250ca987d46SWarner Losh\ Words to be used inside configuration files
251ca987d46SWarner Losh
252ca987d46SWarner Losh: retry false ;         \ For use in load error commands
253ca987d46SWarner Losh: ignore true ;         \ For use in load error commands
254ca987d46SWarner Losh
255ca987d46SWarner Losh\ Return to strict forth vocabulary
256ca987d46SWarner Losh
257ca987d46SWarner Losh: #type
258ca987d46SWarner Losh  over - >r
259ca987d46SWarner Losh  type
260ca987d46SWarner Losh  r> spaces
261ca987d46SWarner Losh;
262ca987d46SWarner Losh
263ca987d46SWarner Losh: .? 2 spaces 2swap 15 #type 2 spaces type cr ;
264ca987d46SWarner Losh
265ca987d46SWarner Losh\ Execute the ? command to print all the commands defined in
266ca987d46SWarner Losh\ C, then list the ones we support here. Please note that this
267ca987d46SWarner Losh\ doesn't use pager_* routines that the C implementation of ?
268ca987d46SWarner Losh\ does, so these will always appear, even if you stop early
269ca987d46SWarner Losh\ there. And they may cause the commands to scroll off the
270ca987d46SWarner Losh\ screen if the number of commands modulus LINES is close
271ca987d46SWarner Losh\ to LINEs....
272ca987d46SWarner Losh: ?
273ca987d46SWarner Losh  ['] ? execute
274ca987d46SWarner Losh  s" boot-conf" s" load kernel and modules, then autoboot" .?
275ca987d46SWarner Losh  s" read-conf" s" read a configuration file" .?
276ca987d46SWarner Losh  s" enable-module" s" enable loading of a module" .?
277ca987d46SWarner Losh  s" disable-module" s" disable loading of a module" .?
278ca987d46SWarner Losh  s" toggle-module" s" toggle loading of a module" .?
279ca987d46SWarner Losh  s" show-module" s" show module load data" .?
280ca987d46SWarner Losh  s" try-include" s" try to load/interpret files" .?
281ca987d46SWarner Losh;
282ca987d46SWarner Losh
283ca987d46SWarner Losh: try-include ( -- ) \ see loader.4th(8)
284ca987d46SWarner Losh  ['] include ( -- xt ) \ get the execution token of `include'
285ca987d46SWarner Losh  catch ( xt -- exception# | 0 ) if \ failed
286ca987d46SWarner Losh    LF parse ( c -- s-addr/u ) 2drop \ advance >in to EOL (drop data)
287ca987d46SWarner Losh    \ ... prevents words unused by `include' from being interpreted
288ca987d46SWarner Losh  then
289ca987d46SWarner Losh; immediate \ interpret immediately for access to `source' (aka tib)
290ca987d46SWarner Losh
291ca987d46SWarner Loshonly forth definitions
292