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

/stand/forth/menu-commands.4th

https://bitbucket.org/freebsd/freebsd-base
Forth | 418 lines | 342 code | 76 blank | 0 comment | 2 complexity | 498681dd57abf5fd4c54a642cacb4e5a MD5 | raw file
  1. \ Copyright (c) 2006-2015 Devin Teske <dteske@FreeBSD.org>
  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. \ $FreeBSD$
  26. marker task-menu-commands.4th
  27. include /boot/menusets.4th
  28. only forth definitions
  29. variable kernel_state
  30. variable root_state
  31. 0 kernel_state !
  32. 0 root_state !
  33. also menu-namespace also menu-command-helpers
  34. \
  35. \ Boot
  36. \
  37. : init_boot ( N -- N )
  38. dup
  39. s" boot_single" getenv -1 <> if
  40. drop ( n n c-addr -- n n ) \ unused
  41. toggle_menuitem ( n n -- n n )
  42. s" set menu_keycode[N]=115" \ base command to execute
  43. else
  44. s" set menu_keycode[N]=98" \ base command to execute
  45. then
  46. 17 +c! \ replace 'N' with ASCII numeral
  47. evaluate
  48. ;
  49. \
  50. \ Alternate Boot
  51. \
  52. : init_altboot ( N -- N )
  53. dup
  54. s" boot_single" getenv -1 <> if
  55. drop ( n c-addr -- n ) \ unused
  56. toggle_menuitem ( n -- n )
  57. s" set menu_keycode[N]=109" \ base command to execute
  58. else
  59. s" set menu_keycode[N]=115" \ base command to execute
  60. then
  61. 17 +c! \ replace 'N' with ASCII numeral
  62. evaluate
  63. ;
  64. : altboot ( N -- NOTREACHED )
  65. s" boot_single" 2dup getenv -1 <> if
  66. drop ( c-addr/u c-addr -- c-addr/u ) \ unused
  67. unsetenv ( c-addr/u -- )
  68. else
  69. 2drop ( c-addr/u -- ) \ unused
  70. s" set boot_single=YES" evaluate
  71. then
  72. 0 boot ( state -- )
  73. ;
  74. \
  75. \ ACPI
  76. \
  77. : acpi_enable ( -- )
  78. s" set acpi_load=YES" evaluate \ XXX deprecated but harmless
  79. s" set hint.acpi.0.disabled=0" evaluate
  80. s" loader.acpi_disabled_by_user" unsetenv
  81. ;
  82. : acpi_disable ( -- )
  83. s" acpi_load" unsetenv \ XXX deprecated but harmless
  84. s" set hint.acpi.0.disabled=1" evaluate
  85. s" set loader.acpi_disabled_by_user=1" evaluate
  86. ;
  87. : toggle_acpi ( N -- N TRUE )
  88. \ Make changes effective _before_ calling menu-redraw
  89. acpienabled? if
  90. acpi_disable
  91. else
  92. acpi_enable
  93. then
  94. menu-redraw
  95. TRUE \ loop menu again
  96. ;
  97. \
  98. \ Safe Mode
  99. \
  100. : safemode_enabled? ( -- flag )
  101. s" kern.smp.disabled" getenv -1 <> dup if
  102. swap drop ( c-addr flag -- flag )
  103. then
  104. ;
  105. : safemode_enable ( -- )
  106. s" set kern.smp.disabled=1" evaluate
  107. s" set hw.ata.ata_dma=0" evaluate
  108. s" set hw.ata.atapi_dma=0" evaluate
  109. s" set hw.ata.wc=0" evaluate
  110. s" set hw.eisa_slots=0" evaluate
  111. s" set kern.eventtimer.periodic=1" evaluate
  112. s" set kern.geom.part.check_integrity=0" evaluate
  113. ;
  114. : safemode_disable ( -- )
  115. s" kern.smp.disabled" unsetenv
  116. s" hw.ata.ata_dma" unsetenv
  117. s" hw.ata.atapi_dma" unsetenv
  118. s" hw.ata.wc" unsetenv
  119. s" hw.eisa_slots" unsetenv
  120. s" kern.eventtimer.periodic" unsetenv
  121. s" kern.geom.part.check_integrity" unsetenv
  122. ;
  123. : init_safemode ( N -- N )
  124. safemode_enabled? if
  125. toggle_menuitem ( n -- n )
  126. then
  127. ;
  128. : toggle_safemode ( N -- N TRUE )
  129. toggle_menuitem
  130. \ Now we're going to make the change effective
  131. dup toggle_stateN @ 0= if
  132. safemode_disable
  133. else
  134. safemode_enable
  135. then
  136. menu-redraw
  137. TRUE \ loop menu again
  138. ;
  139. \
  140. \ Single User Mode
  141. \
  142. : singleuser_enabled? ( -- flag )
  143. s" boot_single" getenv -1 <> dup if
  144. swap drop ( c-addr flag -- flag )
  145. then
  146. ;
  147. : singleuser_enable ( -- )
  148. s" set boot_single=YES" evaluate
  149. ;
  150. : singleuser_disable ( -- )
  151. s" boot_single" unsetenv
  152. ;
  153. : init_singleuser ( N -- N )
  154. singleuser_enabled? if
  155. toggle_menuitem ( n -- n )
  156. then
  157. ;
  158. : toggle_singleuser ( N -- N TRUE )
  159. toggle_menuitem
  160. menu-redraw
  161. \ Now we're going to make the change effective
  162. dup toggle_stateN @ 0= if
  163. singleuser_disable
  164. else
  165. singleuser_enable
  166. then
  167. TRUE \ loop menu again
  168. ;
  169. \
  170. \ Verbose Boot
  171. \
  172. : verbose_enabled? ( -- flag )
  173. s" boot_verbose" getenv -1 <> dup if
  174. swap drop ( c-addr flag -- flag )
  175. then
  176. ;
  177. : verbose_enable ( -- )
  178. s" set boot_verbose=YES" evaluate
  179. ;
  180. : verbose_disable ( -- )
  181. s" boot_verbose" unsetenv
  182. ;
  183. : init_verbose ( N -- N )
  184. verbose_enabled? if
  185. toggle_menuitem ( n -- n )
  186. then
  187. ;
  188. : toggle_verbose ( N -- N TRUE )
  189. toggle_menuitem
  190. menu-redraw
  191. \ Now we're going to make the change effective
  192. dup toggle_stateN @ 0= if
  193. verbose_disable
  194. else
  195. verbose_enable
  196. then
  197. TRUE \ loop menu again
  198. ;
  199. \
  200. \ Escape to Prompt
  201. \
  202. : goto_prompt ( N -- N FALSE )
  203. s" set autoboot_delay=NO" evaluate
  204. cr
  205. ." To get back to the menu, type `menu' and press ENTER" cr
  206. ." or type `boot' and press ENTER to start FreeBSD." cr
  207. cr
  208. FALSE \ exit the menu
  209. ;
  210. \
  211. \ Cyclestate (used by kernel/root below)
  212. \
  213. : init_cyclestate ( N K -- N )
  214. over cycle_stateN ( n k -- n k addr )
  215. begin
  216. tuck @ ( n k addr -- n addr k c )
  217. over <> ( n addr k c -- n addr k 0|-1 )
  218. while
  219. rot ( n addr k -- addr k n )
  220. cycle_menuitem
  221. swap rot ( addr k n -- n k addr )
  222. repeat
  223. 2drop ( n k addr -- n )
  224. ;
  225. \
  226. \ Kernel
  227. \
  228. : init_kernel ( N -- N )
  229. kernel_state @ ( n -- n k )
  230. init_cyclestate ( n k -- n )
  231. ;
  232. : activate_kernel ( N -- N )
  233. dup cycle_stateN @ ( n -- n n2 )
  234. dup kernel_state ! ( n n2 -- n n2 ) \ copy for re-initialization
  235. 48 + ( n n2 -- n n2' ) \ kernel_state to ASCII num
  236. s" set kernel=${kernel_prefix}${kernel[N]}${kernel_suffix}"
  237. 36 +c! ( n n2 c-addr/u -- n c-addr/u ) \ 'N' to ASCII num
  238. evaluate ( n c-addr/u -- n ) \ sets $kernel to full kernel-path
  239. ;
  240. : cycle_kernel ( N -- N TRUE )
  241. cycle_menuitem \ cycle cycle_stateN to next value
  242. activate_kernel \ apply current cycle_stateN
  243. menu-redraw \ redraw menu
  244. TRUE \ loop menu again
  245. ;
  246. \
  247. \ Root
  248. \
  249. : init_root ( N -- N )
  250. root_state @ ( n -- n k )
  251. init_cyclestate ( n k -- n )
  252. ;
  253. : activate_root ( N -- N )
  254. dup cycle_stateN @ ( n -- n n2 )
  255. dup root_state ! ( n n2 -- n n2 ) \ copy for re-initialization
  256. 48 + ( n n2 -- n n2' ) \ root_state to ASCII num
  257. s" set root=${root_prefix}${root[N]}${root_suffix}"
  258. 30 +c! ( n n2 c-addr/u -- n c-addr/u ) \ 'N' to ASCII num
  259. evaluate ( n c-addr/u -- n ) \ sets $root to full kernel-path
  260. ;
  261. : cycle_root ( N -- N TRUE )
  262. cycle_menuitem \ cycle cycle_stateN to next value
  263. activate_root \ apply current cycle_stateN
  264. menu-redraw \ redraw menu
  265. TRUE \ loop menu again
  266. ;
  267. \
  268. \ Menusets
  269. \
  270. : goto_menu ( N M -- N TRUE )
  271. menu-unset
  272. menuset-loadsetnum ( n m -- n )
  273. menu-redraw
  274. TRUE \ Loop menu again
  275. ;
  276. \
  277. \ Defaults
  278. \
  279. : set_default_boot_options ( N -- N TRUE )
  280. acpi_enable
  281. safemode_disable
  282. singleuser_disable
  283. verbose_disable
  284. 2 goto_menu
  285. ;
  286. \
  287. \ Set boot environment defaults
  288. \
  289. : init_bootenv ( -- )
  290. s" set menu_caption[1]=${bemenu_current}${vfs.root.mountfrom}" evaluate
  291. s" set ansi_caption[1]=${beansi_current}${vfs.root.mountfrom}" evaluate
  292. s" set menu_caption[2]=${bemenu_bootfs}${zfs_be_active}" evaluate
  293. s" set ansi_caption[2]=${beansi_bootfs}${zfs_be_active}" evaluate
  294. s" set menu_caption[3]=${bemenu_page}${zfs_be_currpage}${bemenu_pageof}${zfs_be_pages}" evaluate
  295. s" set ansi_caption[3]=${beansi_page}${zfs_be_currpage}${bemenu_pageof}${zfs_be_pages}" evaluate
  296. ;
  297. \
  298. \ Redraw the entire screen. A long BE name can corrupt the menu
  299. \
  300. : be_draw_screen
  301. clear \ Clear the screen (in screen.4th)
  302. print_version \ print version string (bottom-right; see version.4th)
  303. draw-beastie \ Draw FreeBSD logo at right (in beastie.4th)
  304. draw-brand \ Draw brand.4th logo at top (in brand.4th)
  305. menu-init \ Initialize menu and draw bounding box (in menu.4th)
  306. ;
  307. \
  308. \ Select a boot environment
  309. \
  310. : set_bootenv ( N -- N TRUE )
  311. dup s" set vfs.root.mountfrom=${bootenv_root[E]}" 38 +c! evaluate
  312. s" set currdev=${vfs.root.mountfrom}:" evaluate
  313. s" unload" evaluate
  314. free-module-options
  315. s" /boot/defaults/loader.conf" read-conf
  316. s" /boot/loader.conf" read-conf
  317. s" /boot/loader.conf.local" read-conf
  318. init_bootenv
  319. be_draw_screen
  320. menu-redraw
  321. TRUE
  322. ;
  323. \
  324. \ Switch to the next page of boot environments
  325. \
  326. : set_be_page ( N -- N TRUE )
  327. s" zfs_be_currpage" getenv dup -1 = if
  328. drop s" 1"
  329. else
  330. 0 s>d 2swap
  331. >number ( ud caddr/u -- ud' caddr'/u' ) \ convert string to numbers
  332. 2drop \ drop the string
  333. 1 um/mod ( ud u1 -- u2 u3 ) \ convert double ud' to single u3' and remainder u2
  334. swap drop ( ud2 u3 -- u3 ) \ drop the remainder u2
  335. 1+ \ increment the page number
  336. s>d <# #s #> \ convert back to a string
  337. then
  338. s" zfs_be_currpage" setenv
  339. s" reloadbe" evaluate
  340. 3 goto_menu
  341. ;
  342. only forth definitions