1 ()                                                                          ()
   2 
   3 
   4 
   5  Copyright (c) 2006-2015 Devin Teske <dteske@FreeBSD.org>  All rights
   6 reserved.   Redistribution and use in source and binary forms, with or without
   7  modification, are permitted provided that the following conditions  are met:
   8 1. Redistributions of source code must retain the above copyright     notice,
   9 this list of conditions and the following disclaimer.   2. Redistributions in
  10 binary form must reproduce the above copyright     notice, this list of
  11 conditions and the following disclaimer in the     documentation and/or other
  12 materials provided with the distribution.   THIS SOFTWARE IS PROVIDED BY THE
  13 AUTHOR AND CONTRIBUTORS ``AS IS'' AND  ANY EXPRESS OR IMPLIED WARRANTIES,
  14 INCLUDING, BUT NOT LIMITED TO, THE  IMPLIED WARRANTIES OF MERCHANTABILITY AND
  15 FITNESS FOR A PARTICULAR PURPOSE  ARE DISCLAIMED.  IN NO EVENT SHALL THE
  16 AUTHOR OR CONTRIBUTORS BE LIABLE  FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18 PROCUREMENT OF SUBSTITUTE GOODS  OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  19 OR BUSINESS INTERRUPTION)  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  20 WHETHER IN CONTRACT, STRICT  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  21 OTHERWISE) ARISING IN ANY WAY  OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22 ADVISED OF THE POSSIBILITY OF  SUCH DAMAGE.   Copyright 2015 Toomas Soome
  23 <tsoome@me.com>    Copyright 2019 OmniOS Community Edition (OmniOSce)
  24 Association.   Copyright 2019 Joyent, Inc.
  25 
  26 marker task-menu-commands.4th
  27 
  28 include /boot/forth/menusets.4th
  29 
  30 only forth definitions
  31 
  32 variable osconsole_state variable acpi_state variable kernel_state variable
  33 root_state variable kmdb_state variable drop_into_kmdb_state 0 kmdb_state !  0
  34 drop_into_kmdb_state !  0 osconsole_state !  0 acpi_state !  0 kernel_state !
  35 0 root_state !
  36 
  37 also menu-namespace also menu-command-helpers
  38 
  39  Boot
  40 
  41 : init_boot ( N -- N )   dup  s" smartos" getenv? if        s" set
  42 menu_keycode[N]=98"  base command to execute      else           s"
  43 boot_single" getenv -1 <> if              drop ( n n c-addr -- n n )  unused
  44                toggle_menuitem ( n n -- n n )               s" set
  45 menu_keycode[N]=115"  base command to execute          else                s"
  46 set menu_keycode[N]=98"  base command to execute       then      then      17
  47 +c!  replace 'N' with ASCII numeral     evaluate ;
  48 
  49  Alternate Boot
  50 
  51 : init_altboot ( N -- N )     dup  s" smartos" getenv? if        s" set
  52 menu_keycode[N]=114"  base command to execute     else           s"
  53 boot_single" getenv -1 <> if              drop ( n c-addr -- n )  unused
  54                toggle_menuitem ( n -- n )              s" set
  55 menu_keycode[N]=109"  base command to execute          else                s"
  56 set menu_keycode[N]=115"  base command to execute           then      then
  57      17 +c!  replace 'N' with ASCII numeral  evaluate ;
  58 
  59 : altboot ( N -- NOTREACHED )      s" smartos" getenv? if        s" alt-boot-
  60 args" getenv dup -1 <> if         s" boot-args" setenv ( c-addr/u -- )
  61           then           ." NoInstall/Recovery mode boot. login/pw: root/root"
  62 cr   else           s" boot_single" 2dup getenv -1 <> if        drop (
  63 c-addr/u c-addr -- c-addr/u )  unused             unsetenv ( c-addr/u -- )
  64           else                2drop ( c-addr/u -- )  unused                s"
  65 set boot_single=YES" evaluate           then      then      0 boot ( state --
  66 ) ;
  67 
  68  Single User Mode
  69 
  70 : singleuser_enabled? ( -- flag )  s" boot_single" getenv -1 <>   dup if
  71           swap drop ( c-addr flag -- flag )  then ;
  72 
  73 : singleuser_enable ( -- )    s" set boot_single=YES" evaluate ;
  74 
  75 : singleuser_disable ( -- )   s" boot_single" unsetenv ;
  76 
  77 : init_singleuser ( N -- N )  singleuser_enabled? if        toggle_menuitem (
  78 n -- n )  then ;
  79 
  80 : toggle_singleuser ( N -- N TRUE )     toggle_menuitem     menu-redraw
  81 
  82       Now we're going to make the change effective
  83 
  84      dup toggle_stateN @ 0= if          singleuser_disable  else
  85           singleuser_enable   then
  86 
  87      TRUE  loop menu again ;
  88 
  89  Verbose Boot
  90 
  91 : verbose_enabled? ( -- flag )     s" boot_verbose" getenv -1 <> dup if
  92           swap drop ( c-addr flag -- flag )  then ;
  93 
  94 : verbose_enable ( -- )  s" set boot_verbose=YES" evaluate ;
  95 
  96 : verbose_disable ( -- )      s" boot_verbose" unsetenv ;
  97 
  98 : init_verbose ( N -- N )     verbose_enabled? if           toggle_menuitem (
  99 n -- n )  then ;
 100 
 101 : toggle_verbose ( N -- N TRUE )   toggle_menuitem     menu-redraw
 102 
 103       Now we're going to make the change effective
 104 
 105      dup toggle_stateN @ 0= if          verbose_disable     else
 106           verbose_enable      then
 107 
 108      TRUE  loop menu again ;
 109 
 110  kmdb
 111 
 112 : kmdb_enabled? ( -- flag )   s" boot_kmdb" getenv -1 <> dup if         swap
 113 drop ( c-addr flag -- flag )  then ;
 114 
 115 : kmdb_enable ( -- )     s" set boot_kmdb=YES" evaluate ;
 116 
 117 : kmdb_disable ( -- )    s" boot_kmdb" unsetenv   s" boot_drop_into_kmdb"
 118 unsetenv ;
 119 
 120 : init_kmdb ( N -- N )   dup kmdb_state !          store entry number for
 121 kmdb+drop_into_kmdb      kmdb_enabled? if         toggle_menuitem ( n -- n )
 122      then ;
 123 
 124 : toggle_kmdb ( N -- N TRUE )      toggle_menuitem     dup toggle_stateN @ 0=
 125 if ( kmdb is not set )        drop_into_kmdb_state @ if ( drop_into_kmdb is
 126 set? )              drop_into_kmdb_state @ toggle_stateN @ if ( drop_into_kmdb
 127 is enabled? )                 drop_into_kmdb_state @ toggle_menuitem drop
 128                then           then      then      menu-redraw
 129 
 130       Now we're going to make the change effective
 131 
 132      dup toggle_stateN @ 0= if          kmdb_disable   else
 133           kmdb_enable    then
 134 
 135      TRUE  loop menu again ;
 136 
 137  drop into kmdb
 138 
 139 : drop_into_kmdb_disable ( -- )    s" boot_drop_into_kmdb" unsetenv ;
 140 
 141 : drop_into_kmdb_enabled? ( -- flag )    -d is only allowed with -k   s"
 142 boot_drop_into_kmdb" getenv -1 <> kmdb_enabled?   and dup if       swap drop (
 143 c-addr flag -- flag )    else           drop_into_kmdb_disable         make
 144 sure env is not set      then ;
 145 
 146 : drop_into_kmdb_enable ( -- )     kmdb_enable    s" set
 147 boot_drop_into_kmdb=YES" evaluate ;
 148 
 149 : init_drop_into_kmdb ( N -- N )   dup drop_into_kmdb_state !          store
 150 entry number for kmdb    kmdb_enabled? drop_into_kmdb_enabled? and if
 151           toggle_menuitem ( n -- n )    then ;
 152 
 153 : toggle_drop_into_kmdb ( N -- N TRUE )      toggle_menuitem     kmdb_enabled?
 154 0= if          kmdb_state @ toggle_menuitem drop  then      menu-redraw
 155 
 156       Now we're going to make the change effective
 157 
 158      dup toggle_stateN @ 0= if          drop_into_kmdb_disable   else
 159           drop_into_kmdb_enable    then
 160 
 161      TRUE  loop menu again ;
 162 
 163  Reconfiguration boot
 164 
 165 : reconfigure_enabled? ( -- flag )      s" boot_reconfigure" getenv -1 <> dup
 166 if        swap drop ( c-addr flag -- flag )  then ;
 167 
 168 : reconfigure_enable ( -- )   s" set boot_reconfigure=YES" evaluate ;
 169 
 170 : reconfigure_disable ( -- )  s" boot_reconfigure" unsetenv ;
 171 
 172 : init_reconfigure ( N -- N )      reconfigure_enabled? if
 173           toggle_menuitem ( n -- n )    then ;
 174 
 175 : toggle_reconfigure ( N -- N TRUE )    toggle_menuitem     menu-redraw
 176 
 177       Now we're going to make the change effective
 178 
 179      dup toggle_stateN @ 0= if          reconfigure_disable      else
 180           reconfigure_enable  then
 181 
 182      TRUE  loop menu again ;
 183 
 184  Escape to Prompt
 185 
 186 : goto_prompt ( N -- N FALSE )
 187 
 188      s" set autoboot_delay=NO" evaluate
 189 
 190      cr   ." To get back to the menu, type `menu' and press ENTER" cr      ."
 191 or type `boot' and press ENTER to start illumos." cr   cr
 192 
 193      FALSE  exit the menu ;
 194 
 195  Cyclestate (used by osconsole/acpi/kernel/root below)
 196 
 197 : init_cyclestate ( N K -- N )     over cycle_stateN ( n k -- n k addr )
 198      begin          tuck @  ( n k addr -- n addr k c )           over <> ( n
 199 addr k c -- n addr k 0|-1 )   while          rot ( n addr k -- addr k n )
 200           cycle_menuitem           swap rot ( addr k n -- n k addr )  repeat
 201      2drop ( n k addr -- n ) ;
 202 
 203  OS Console  getenv os_console, if not set getenv console, if not set, default
 204 to "text"  allowed serial consoles: ttya .. ttyd  if new console will be added
 205 (graphics?), this section needs to be updated : init_osconsole ( N -- N )  s"
 206 os_console" getenv dup -1 = if          drop           s" console" getenv dup
 207 -1 = if             drop 0          default to text         then      then
 208                     ( n c-addr/u | n 0 )
 209 
 210      dup 0<> if                 ( n c-addr/u )           2dup s" ttyd" compare
 211 0= if               2drop 4        else 2dup s" ttyc" compare 0= if
 212                2drop 3        else 2dup s" ttyb" compare 0= if
 213                2drop 2        else 2dup s" ttya" compare 0= if
 214                2drop 1        else                2drop 0         anything
 215 else defaults to text         then then then then      then
 216      osconsole_state !  ;
 217 
 218 : activate_osconsole ( N -- N )    dup cycle_stateN @  ( n -- n n2 )  dup
 219 osconsole_state !   ( n n2 -- n n2 )   copy for re-initialization
 220 
 221      case      0 of s" text" endof      1 of s" ttya" endof      2 of s" ttyb"
 222 endof     3 of s" ttyc" endof      4 of s" ttyd" endof      dup s" unknown
 223 state: " type . cr  endcase   s" os_console" setenv ;
 224 
 225 : cycle_osconsole ( N -- N TRUE )  cycle_menuitem  cycle cycle_stateN to next
 226 value     activate_osconsole   apply current cycle_stateN   menu-redraw
 227       redraw menu   TRUE       loop menu again ;
 228 
 229  ACPI : init_acpi ( N -- N )  s" acpi-user-options" getenv dup -1 <> if
 230           evaluate        use ?number parse step
 231 
 232            translate option to cycle state        case           1 of 1
 233 acpi_state ! endof       2 of 2 acpi_state ! endof          4 of 3 acpi_state
 234 ! endof        8 of 4 acpi_state ! endof          0 acpi_state !
 235           endcase   else           drop      then ;
 236 
 237 : activate_acpi ( N -- N )    dup cycle_stateN @  ( n -- n n2 )  dup
 238 acpi_state !   ( n n2 -- n n2 )   copy for re-initialization
 239 
 240       if N == 0, it's default, just unset env.    dup 0= if           drop
 241           s" acpi-user-options" unsetenv     else           case           1
 242 of s" 1" endof           2 of s" 2" endof         3 of s" 4" endof         4
 243 of s" 8" endof           endcase        s" acpi-user-options" setenv  then ;
 244 
 245 : cycle_acpi ( N -- N TRUE )  cycle_menuitem  cycle cycle_stateN to next value
 246      activate_acpi   apply current cycle_stateN   menu-redraw     redraw menu
 247      TRUE       loop menu again ;
 248 
 249  Kernel
 250 
 251 : init_kernel ( N -- N )      kernel_state @  ( n -- n k )  init_cyclestate (
 252 n k -- n ) ;
 253 
 254 : activate_kernel ( N -- N )  dup cycle_stateN @  ( n -- n n2 )  dup
 255 kernel_state ! ( n n2 -- n n2 )   copy for re-initialization     48 +
 256                ( n n2 -- n n2' )  kernel_state to ASCII num
 257 
 258      s" set kernel=${kernel_prefix}${kernel[N]}${kernel_suffix}"      36 +c!
 259           ( n n2 c-addr/u -- n c-addr/u )  'N' to ASCII num      evaluate  ( n
 260 c-addr/u -- n )  sets $kernel to full kernel-path ;
 261 
 262 : cycle_kernel ( N -- N TRUE )     cycle_menuitem  cycle cycle_stateN to next
 263 value     activate_kernel  apply current cycle_stateN  menu-redraw     redraw
 264 menu      TRUE       loop menu again ;
 265 
 266  Root
 267 
 268 : init_root ( N -- N )   root_state @    ( n -- n k )  init_cyclestate ( n k
 269 -- n ) ;
 270 
 271 : activate_root ( N -- N )    dup cycle_stateN @  ( n -- n n2 )  dup
 272 root_state !   ( n n2 -- n n2 )   copy for re-initialization     48 +
 273                ( n n2 -- n n2' )  root_state to ASCII num
 274 
 275      s" set root=${root_prefix}${root[N]}${root_suffix}"    30 +c!         ( n
 276 n2 c-addr/u -- n c-addr/u )  'N' to ASCII num     evaluate  ( n c-addr/u -- n
 277 )  sets $root to full kernel-path ;
 278 
 279 : cycle_root ( N -- N TRUE )  cycle_menuitem  cycle cycle_stateN to next value
 280      activate_root   apply current cycle_stateN   menu-redraw     redraw menu
 281      TRUE       loop menu again ;
 282 
 283  Menusets
 284 
 285 : goto_menu ( N M -- N TRUE )      menu-unset     menuset-loadsetnum ( n m --
 286 n )  menu-redraw    TRUE  Loop menu again ;
 287 
 288  Defaults
 289 
 290 : unset_boot_options     0 acpi_state !      s" acpi-user-options" unsetenv
 291      s" boot-args" unsetenv   s" boot_ask" unsetenv    singleuser_disable
 292      verbose_disable     kmdb_disable        \[u00A0]disables drop_into_kmdb
 293 as well   reconfigure_disable ;
 294 
 295 : set_default_boot_options ( N -- N TRUE )   unset_boot_options  2 goto_menu ;
 296 
 297  Set boot environment defaults
 298 
 299 
 300 : init_bootenv ( -- )    s" set
 301 menu_caption[1]=${bemenu_current}${zfs_be_active}" evaluate      s" set
 302 ansi_caption[1]=${beansi_current}${zfs_be_active}" evaluate      s" set
 303 menu_caption[2]=${bemenu_bootfs}${currdev}" evaluate   s" set
 304 ansi_caption[2]=${beansi_bootfs}${currdev}" evaluate   s" set
 305 menu_caption[3]=${bemenu_page}${zfs_be_currpage}${bemenu_pageof}${zfs_be_pages}"
 306 evaluate  s" set
 307 ansi_caption[3]=${beansi_page}${zfs_be_currpage}${bemenu_pageof}${zfs_be_pages}"
 308 evaluate ;
 309 
 310  Redraw the entire screen. A long BE name can corrupt the menu
 311 
 312 : be_draw_screen    clear           Clear the screen (in screen.4th)
 313      print_version   print version string (bottom-right; see version.4th)
 314      draw-beastie    Draw FreeBSD logo at right (in beastie.4th)      draw-
 315 brand      Draw brand.4th logo at top (in brand.4th)   menu-init  Initialize
 316 menu and draw bounding box (in menu.4th) ;
 317 
 318  Select a boot environment
 319 
 320 : set_bootenv ( N -- N TRUE )      dup s" bootenv_root[E]" 13 +c! getenv   s"
 321 currdev" getenv compare 0= if           s" zfs_be_active" getenv type ."  is
 322 already active"     else           dup s" set currdev=${bootenv_root[E]}" 27
 323 +c! evaluate        dup s" bootenvmenu_caption[E]" 20 +c! getenv           s"
 324 zfs_be_active" setenv         ." Activating " s" currdev" getenv type cr
 325           s" unload" evaluate           free-module-options
 326           unset_boot_options       s" /boot/defaults/loader.conf" read-conf
 327           s" /boot/loader.conf" read-conf         s" /boot/loader.conf.local"
 328 read-conf           init_bootenv
 329 
 330           s" 1" s" zfs_be_currpage" setenv        s" be-set-page" evaluate
 331      then
 332 
 333      500 ms               sleep so user can see the message
 334      be_draw_screen      menu-redraw    TRUE ;
 335 
 336  Chainload this entry. Normally we do not return, in case of error  from chain
 337 load, we continue with normal menu code.
 338 
 339 : set_be_chain ( N -- no return | N TRUE )   dup s" chain ${bootenv_root[E]}"
 340 21 +c! evaluate catch drop
 341 
 342      menu-redraw    TRUE ;
 343 
 344  Switch to the next page of boot environments
 345 
 346 : set_be_page ( N -- N TRUE )      s" zfs_be_currpage" getenv dup -1 = if
 347           drop s" 1"     else           s2n       1+         increment the
 348 page number         dup       s" zfs_be_pages" getenv       s2n       >      if
 349 drop 1 then         n2s  then
 350 
 351      s" zfs_be_currpage" setenv    s" be-set-page" evaluate      3 goto_menu ;
 352 
 353 only forth definitions
 354 
 355 
 356 
 357                                 August 28, 2019                             ()