PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/stand/forth/loader.4th

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