2 -- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
5 -- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6 -- All rights reserved.
8 -- Redistribution and use in source and binary forms, with or without
9 -- modification, are permitted provided that the following conditions
11 -- 1. Redistributions of source code must retain the above copyright
12 -- notice, this list of conditions and the following disclaimer.
13 -- 2. Redistributions in binary form must reproduce the above copyright
14 -- notice, this list of conditions and the following disclaimer in the
15 -- documentation and/or other materials provided with the distribution.
17 -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 -- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 -- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 -- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 -- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 local cli = require("cli")
33 local core = require("core")
34 local color = require("color")
35 local config = require("config")
36 local screen = require("screen")
37 local drawer = require("drawer")
42 local return_menu_entry = {
43 entry_type = core.MENU_RETURN,
44 name = "Back to main menu" .. color.highlight(" [Backspace]"),
47 local function OnOff(str, value)
49 return str .. color.escapefg(color.GREEN) .. "On" ..
52 return str .. color.escapefg(color.RED) .. "off" ..
57 local function bootenvSet(env)
58 loader.setenv("vfs.root.mountfrom", env)
59 loader.setenv("currdev", env .. ":")
65 -- Menu handlers take the current menu and selected entry as parameters,
66 -- and should return a boolean indicating whether execution should
67 -- continue or not. The return value may be omitted if this entry should
68 -- have no bearing on whether we continue or not, indicating that we
69 -- should just continue after execution.
70 [core.MENU_ENTRY] = function(_, entry)
74 [core.MENU_CAROUSEL_ENTRY] = function(_, entry)
75 -- carousel (rotating) functionality
76 local carid = entry.carousel_id
77 local caridx = config.getCarouselIndex(carid)
78 local choices = entry.items
79 if type(choices) == "function" then
83 caridx = (caridx % #choices) + 1
84 config.setCarouselIndex(carid, caridx)
85 entry.func(caridx, choices[caridx], choices)
88 [core.MENU_SUBMENU] = function(_, entry)
89 menu.process(entry.submenu)
91 [core.MENU_RETURN] = function(_, entry)
92 -- allow entry to have a function/side effect
93 if entry.func ~= nil then
99 -- loader menu tree is rooted at menu.welcome
101 menu.boot_environments = {
103 -- return to welcome menu
106 entry_type = core.MENU_CAROUSEL_ENTRY,
107 carousel_id = "be_active",
108 items = core.bootenvList,
109 name = function(idx, choice, all_choices)
110 if #all_choices == 0 then
114 local is_default = (idx == 1)
115 local bootenv_name = ""
118 name_color = color.escapefg(color.GREEN)
120 name_color = color.escapefg(color.BLUE)
122 bootenv_name = bootenv_name .. name_color ..
123 choice .. color.resetfg()
124 return color.highlight("A").."ctive: " ..
125 bootenv_name .. " (" .. idx .. " of " ..
128 func = function(_, choice, _)
134 entry_type = core.MENU_ENTRY,
136 return core.isRewinded() == false
139 return color.highlight("b") .. "ootfs: " ..
140 core.bootenvDefault()
143 -- Reset active boot environment to the default
144 config.setCarouselIndex("be_active", 1)
145 bootenvSet(core.bootenvDefault())
152 menu.boot_options = {
154 -- return to welcome menu
158 entry_type = core.MENU_ENTRY,
159 name = "Load System " .. color.highlight("D") ..
161 func = core.setDefaults,
165 entry_type = core.MENU_SEPARATOR,
168 entry_type = core.MENU_SEPARATOR,
169 name = "Boot Options:",
173 entry_type = core.MENU_ENTRY,
174 visible = core.isSystem386,
176 return OnOff(color.highlight("A") ..
184 entry_type = core.MENU_ENTRY,
186 return OnOff("Safe " .. color.highlight("M") ..
189 func = core.setSafeMode,
194 entry_type = core.MENU_ENTRY,
196 return OnOff(color.highlight("S") ..
197 "ingle user:", core.su)
199 func = core.setSingleUser,
204 entry_type = core.MENU_ENTRY,
206 return OnOff(color.highlight("V") ..
207 "erbose :", core.verbose)
209 func = core.setVerbose,
217 local menu_entries = menu.welcome.all_entries
218 local multi_user = menu_entries.multi_user
219 local single_user = menu_entries.single_user
220 local boot_entry_1, boot_entry_2
221 if core.isSingleUserBoot() then
222 -- Swap the first two menu items on single user boot.
223 -- We'll cache the alternate entries for performance.
224 local alts = menu_entries.alts
226 single_user = core.deepCopyTable(single_user)
227 multi_user = core.deepCopyTable(multi_user)
228 single_user.name = single_user.alternate_name
229 multi_user.name = multi_user.alternate_name
230 menu_entries.alts = {
231 single_user = single_user,
232 multi_user = multi_user,
235 single_user = alts.single_user
236 multi_user = alts.multi_user
238 boot_entry_1, boot_entry_2 = single_user, multi_user
240 boot_entry_1, boot_entry_2 = multi_user, single_user
248 entry_type = core.MENU_SEPARATOR,
251 entry_type = core.MENU_SEPARATOR,
254 menu_entries.kernel_options,
255 menu_entries.boot_options,
256 menu_entries.zpool_checkpoints,
257 menu_entries.boot_envs,
258 menu_entries.chainload,
263 entry_type = core.MENU_ENTRY,
264 name = color.highlight("B") .. "oot Multi user " ..
265 color.highlight("[Enter]"),
266 -- Not a standard menu entry function!
267 alternate_name = color.highlight("B") ..
270 core.setSingleUser(false)
276 entry_type = core.MENU_ENTRY,
277 name = "Boot " .. color.highlight("S") .. "ingle user",
278 -- Not a standard menu entry function!
279 alternate_name = "Boot " .. color.highlight("S") ..
280 "ingle user " .. color.highlight("[Enter]"),
282 core.setSingleUser(true)
288 entry_type = core.MENU_RETURN,
289 name = color.highlight("Esc") .. "ape to loader prompt",
291 loader.setenv("autoboot_delay", "NO")
293 alias = {core.KEYSTR_ESCAPE},
296 entry_type = core.MENU_ENTRY,
297 name = color.highlight("R") .. "eboot",
299 loader.perform("reboot")
304 entry_type = core.MENU_CAROUSEL_ENTRY,
305 carousel_id = "kernel",
306 items = core.kernelList,
307 name = function(idx, choice, all_choices)
308 if #all_choices == 0 then
312 local is_default = (idx == 1)
313 local kernel_name = ""
316 name_color = color.escapefg(color.GREEN)
317 kernel_name = "default/"
319 name_color = color.escapefg(color.BLUE)
321 kernel_name = kernel_name .. name_color ..
322 choice .. color.resetfg()
323 return color.highlight("K") .. "ernel: " ..
324 kernel_name .. " (" .. idx .. " of " ..
327 func = function(_, choice, _)
328 if loader.getenv("kernelname") ~= nil then
329 loader.perform("unload")
331 config.selectKernel(choice)
336 entry_type = core.MENU_SUBMENU,
337 name = "Boot " .. color.highlight("O") .. "ptions",
338 submenu = menu.boot_options,
341 zpool_checkpoints = {
342 entry_type = core.MENU_ENTRY,
345 if core.isRewinded() then
348 return "Rewind ZFS " .. color.highlight("C") ..
349 "heckpoint: " .. rewind
352 core.changeRewindCheckpoint()
353 if core.isRewinded() then
355 core.bootenvDefaultRewinded())
357 bootenvSet(core.bootenvDefault())
359 config.setCarouselIndex("be_active", 1)
362 return core.isZFSBoot() and
363 core.isCheckpointed()
368 entry_type = core.MENU_SUBMENU,
370 return core.isZFSBoot() and
371 #core.bootenvList() > 1
373 name = "Boot " .. color.highlight("E") .. "nvironments",
374 submenu = menu.boot_environments,
378 entry_type = core.MENU_ENTRY,
380 return 'Chain' .. color.highlight("L") ..
381 "oad " .. loader.getenv('chain_disk')
384 loader.perform("chain " ..
385 loader.getenv('chain_disk'))
388 return loader.getenv('chain_disk') ~= nil
395 menu.default = menu.welcome
396 -- current_alias_table will be used to keep our alias table consistent across
397 -- screen redraws, instead of relying on whatever triggered the redraw to update
398 -- the local alias_table in menu.process.
399 menu.current_alias_table = {}
401 function menu.draw(menudef)
402 -- Clear the screen, reset the cursor, then draw
404 menu.current_alias_table = drawer.drawscreen(menudef)
409 -- 'keypress' allows the caller to indicate that a key has been pressed that we
410 -- should process as our initial input.
411 function menu.process(menudef, keypress)
412 assert(menudef ~= nil)
414 if drawn_menu ~= menudef then
419 local key = keypress or io.getchar()
422 -- Special key behaviors
423 if (key == core.KEY_BACKSPACE or key == core.KEY_DELETE) and
424 menudef ~= menu.default then
426 elseif key == core.KEY_ENTER then
428 -- Should not return. If it does, escape menu handling
429 -- and drop to loader prompt.
433 key = string.char(key)
434 -- check to see if key is an alias
435 local sel_entry = nil
436 for k, v in pairs(menu.current_alias_table) do
443 -- if we have an alias do the assigned action:
444 if sel_entry ~= nil then
445 local handler = menu.handlers[sel_entry.entry_type]
446 assert(handler ~= nil)
447 -- The handler's return value indicates if we
448 -- need to exit this menu. An omitted or true
449 -- return value means to continue.
450 if handler(menudef, sel_entry) == false then
453 -- If we got an alias key the screen is out of date...
462 local delay = loader.getenv("autoboot_delay")
464 if delay ~= nil and delay:lower() == "no" then
467 delay = tonumber(delay) or 10
475 menu.draw(menu.default)
478 autoboot_key = menu.autoboot(delay)
480 -- autoboot_key should return the key pressed. It will only
481 -- return nil if we hit the timeout and executed the timeout
482 -- command. Bail out.
483 if autoboot_key == nil then
488 menu.process(menu.default, autoboot_key)
492 print("Exiting menu!")
495 function menu.autoboot(delay)
496 local x = loader.getenv("loader_menu_timeout_x") or 4
497 local y = loader.getenv("loader_menu_timeout_y") or 23
498 local endtime = loader.time() + delay
502 time = endtime - loader.time()
503 if last == nil or last ~= time then
505 screen.setcursor(x, y)
506 print("Autoboot in " .. time ..
507 " seconds, hit [Enter] to boot" ..
508 " or any other key to stop ")
512 local ch = io.getchar()
513 if ch == core.KEY_ENTER then
516 -- erase autoboot msg
517 screen.setcursor(0, y)
518 print(string.rep(" ", 80))
527 local cmd = loader.getenv("menu_timeout_command") or "boot"
528 cli_execute_unparsed(cmd)