xref: /freebsd/stand/lua/menu.lua (revision 277f38abffc6a8160b5044128b5b2c620fbb970c)
1088b4f5fSWarner Losh--
272e39d71SKyle Evans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
372e39d71SKyle Evans--
4088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
5e12ff891SKyle Evans-- Copyright (c) 2018 Kyle Evans <kevans@FreeBSD.org>
6088b4f5fSWarner Losh-- All rights reserved.
7088b4f5fSWarner Losh--
8088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
9088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
10088b4f5fSWarner Losh-- are met:
11088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
12088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
13088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
14088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
15088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
16088b4f5fSWarner Losh--
17088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27088b4f5fSWarner Losh-- SUCH DAMAGE.
28088b4f5fSWarner Losh--
29088b4f5fSWarner Losh-- $FreeBSD$
30088b4f5fSWarner Losh--
31088b4f5fSWarner Losh
3276c75816SKyle Evanslocal cli = require("cli")
33aedd6be5SKyle Evanslocal core = require("core")
34aedd6be5SKyle Evanslocal color = require("color")
35aedd6be5SKyle Evanslocal config = require("config")
36aedd6be5SKyle Evanslocal screen = require("screen")
37aedd6be5SKyle Evanslocal drawer = require("drawer")
38088b4f5fSWarner Losh
39aedd6be5SKyle Evanslocal menu = {}
40c8518398SKyle Evans
41beafe961SKyle Evanslocal drawn_menu
4243f7d9d1SKyle Evanslocal return_menu_entry = {
4343f7d9d1SKyle Evans	entry_type = core.MENU_RETURN,
4443f7d9d1SKyle Evans	name = "Back to main menu" .. color.highlight(" [Backspace]"),
4543f7d9d1SKyle Evans}
46ca16d83fSKyle Evans
4704af4229SKyle Evanslocal function OnOff(str, value)
4804af4229SKyle Evans	if value then
498ce1744fSKyle Evans		return str .. color.escapefg(color.GREEN) .. "On" ..
506dd078dfSToomas Soome		    color.resetfg()
51e15abd1fSKyle Evans	else
528ce1744fSKyle Evans		return str .. color.escapefg(color.RED) .. "off" ..
536dd078dfSToomas Soome		    color.resetfg()
54e15abd1fSKyle Evans	end
55e15abd1fSKyle Evansend
56e15abd1fSKyle Evans
579ed6f9efSKyle Evanslocal function bootenvSet(env)
587efc058fSKyle Evans	loader.setenv("vfs.root.mountfrom", env)
597efc058fSKyle Evans	loader.setenv("currdev", env .. ":")
607efc058fSKyle Evans	config.reload()
617efc058fSKyle Evansend
627efc058fSKyle Evans
63b5746545SKyle Evans-- Module exports
648d415029SKyle Evansmenu.handlers = {
658d415029SKyle Evans	-- Menu handlers take the current menu and selected entry as parameters,
668d415029SKyle Evans	-- and should return a boolean indicating whether execution should
678d415029SKyle Evans	-- continue or not. The return value may be omitted if this entry should
688d415029SKyle Evans	-- have no bearing on whether we continue or not, indicating that we
698d415029SKyle Evans	-- should just continue after execution.
70e2df27e3SKyle Evans	[core.MENU_ENTRY] = function(_, entry)
718d415029SKyle Evans		-- run function
72aedd6be5SKyle Evans		entry.func()
738d415029SKyle Evans	end,
74e2df27e3SKyle Evans	[core.MENU_CAROUSEL_ENTRY] = function(_, entry)
758d415029SKyle Evans		-- carousel (rotating) functionality
76aedd6be5SKyle Evans		local carid = entry.carousel_id
77aedd6be5SKyle Evans		local caridx = config.getCarouselIndex(carid)
784f437f9eSKyle Evans		local choices = entry.items
794f437f9eSKyle Evans		if type(choices) == "function" then
804f437f9eSKyle Evans			choices = choices()
814f437f9eSKyle Evans		end
829f71d421SKyle Evans		if #choices > 0 then
83aedd6be5SKyle Evans			caridx = (caridx % #choices) + 1
84aedd6be5SKyle Evans			config.setCarouselIndex(carid, caridx)
85aedd6be5SKyle Evans			entry.func(caridx, choices[caridx], choices)
868d415029SKyle Evans		end
878d415029SKyle Evans	end,
88e2df27e3SKyle Evans	[core.MENU_SUBMENU] = function(_, entry)
89da9ab827SKyle Evans		menu.process(entry.submenu)
908d415029SKyle Evans	end,
91e2df27e3SKyle Evans	[core.MENU_RETURN] = function(_, entry)
928d415029SKyle Evans		-- allow entry to have a function/side effect
939f71d421SKyle Evans		if entry.func ~= nil then
94aedd6be5SKyle Evans			entry.func()
958d415029SKyle Evans		end
96aedd6be5SKyle Evans		return false
978d415029SKyle Evans	end,
98aedd6be5SKyle Evans}
99280e990bSKyle Evans-- loader menu tree is rooted at menu.welcome
100088b4f5fSWarner Losh
1017efc058fSKyle Evansmenu.boot_environments = {
1027efc058fSKyle Evans	entries = {
1037efc058fSKyle Evans		-- return to welcome menu
10443f7d9d1SKyle Evans		return_menu_entry,
1057efc058fSKyle Evans		{
1067efc058fSKyle Evans			entry_type = core.MENU_CAROUSEL_ENTRY,
1077efc058fSKyle Evans			carousel_id = "be_active",
1087efc058fSKyle Evans			items = core.bootenvList,
1097efc058fSKyle Evans			name = function(idx, choice, all_choices)
1107efc058fSKyle Evans				if #all_choices == 0 then
1117efc058fSKyle Evans					return "Active: "
1127efc058fSKyle Evans				end
1137efc058fSKyle Evans
1147efc058fSKyle Evans				local is_default = (idx == 1)
1157efc058fSKyle Evans				local bootenv_name = ""
1167efc058fSKyle Evans				local name_color
1177efc058fSKyle Evans				if is_default then
1188ce1744fSKyle Evans					name_color = color.escapefg(color.GREEN)
1197efc058fSKyle Evans				else
1208ce1744fSKyle Evans					name_color = color.escapefg(color.BLUE)
1217efc058fSKyle Evans				end
1227efc058fSKyle Evans				bootenv_name = bootenv_name .. name_color ..
1238ce1744fSKyle Evans				    choice .. color.resetfg()
1247efc058fSKyle Evans				return color.highlight("A").."ctive: " ..
1257efc058fSKyle Evans				    bootenv_name .. " (" .. idx .. " of " ..
1267efc058fSKyle Evans				    #all_choices .. ")"
1277efc058fSKyle Evans			end,
128e2df27e3SKyle Evans			func = function(_, choice, _)
1297efc058fSKyle Evans				bootenvSet(choice)
1307efc058fSKyle Evans			end,
1317efc058fSKyle Evans			alias = {"a", "A"},
1327efc058fSKyle Evans		},
1337efc058fSKyle Evans		{
1347efc058fSKyle Evans			entry_type = core.MENU_ENTRY,
135*277f38abSMariusz Zaborski			visible = function()
136*277f38abSMariusz Zaborski				return core.isRewinded() == false
137*277f38abSMariusz Zaborski			end,
1387efc058fSKyle Evans			name = function()
1397efc058fSKyle Evans				return color.highlight("b") .. "ootfs: " ..
1407efc058fSKyle Evans				    core.bootenvDefault()
1417efc058fSKyle Evans			end,
1427efc058fSKyle Evans			func = function()
1437efc058fSKyle Evans				-- Reset active boot environment to the default
1447efc058fSKyle Evans				config.setCarouselIndex("be_active", 1)
1457efc058fSKyle Evans				bootenvSet(core.bootenvDefault())
1467efc058fSKyle Evans			end,
1477efc058fSKyle Evans			alias = {"b", "B"},
1487efc058fSKyle Evans		},
1497efc058fSKyle Evans	},
1507efc058fSKyle Evans}
1517efc058fSKyle Evans
152088b4f5fSWarner Loshmenu.boot_options = {
153d8757746SKyle Evans	entries = {
154088b4f5fSWarner Losh		-- return to welcome menu
15543f7d9d1SKyle Evans		return_menu_entry,
156088b4f5fSWarner Losh		-- load defaults
157088b4f5fSWarner Losh		{
158a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
159a51f9f0cSKyle Evans			name = "Load System " .. color.highlight("D") ..
160a51f9f0cSKyle Evans			    "efaults",
161a51f9f0cSKyle Evans			func = core.setDefaults,
1623cd5547bSKyle Evans			alias = {"d", "D"},
163088b4f5fSWarner Losh		},
164088b4f5fSWarner Losh		{
165a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
166088b4f5fSWarner Losh		},
167088b4f5fSWarner Losh		{
168a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
169a51f9f0cSKyle Evans			name = "Boot Options:",
170088b4f5fSWarner Losh		},
171088b4f5fSWarner Losh		-- acpi
172088b4f5fSWarner Losh		{
173a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
174c1ab36f5SKyle Evans			visible = core.isSystem386,
175088b4f5fSWarner Losh			name = function()
176fd2b19b3SKyle Evans				return OnOff(color.highlight("A") ..
177aedd6be5SKyle Evans				    "CPI       :", core.acpi)
178088b4f5fSWarner Losh			end,
179a51f9f0cSKyle Evans			func = core.setACPI,
1803cd5547bSKyle Evans			alias = {"a", "A"},
181088b4f5fSWarner Losh		},
182088b4f5fSWarner Losh		-- safe mode
183088b4f5fSWarner Losh		{
184a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
185088b4f5fSWarner Losh			name = function()
18657099121SKyle Evans				return OnOff("Safe " .. color.highlight("M") ..
187aedd6be5SKyle Evans				    "ode  :", core.sm)
188088b4f5fSWarner Losh			end,
189a51f9f0cSKyle Evans			func = core.setSafeMode,
1903cd5547bSKyle Evans			alias = {"m", "M"},
191088b4f5fSWarner Losh		},
192088b4f5fSWarner Losh		-- single user
193088b4f5fSWarner Losh		{
194a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
195088b4f5fSWarner Losh			name = function()
196fd2b19b3SKyle Evans				return OnOff(color.highlight("S") ..
197aedd6be5SKyle Evans				    "ingle user:", core.su)
198088b4f5fSWarner Losh			end,
199a51f9f0cSKyle Evans			func = core.setSingleUser,
2003cd5547bSKyle Evans			alias = {"s", "S"},
201088b4f5fSWarner Losh		},
202088b4f5fSWarner Losh		-- verbose boot
203088b4f5fSWarner Losh		{
204a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
205088b4f5fSWarner Losh			name = function()
206fd2b19b3SKyle Evans				return OnOff(color.highlight("V") ..
207aedd6be5SKyle Evans				    "erbose    :", core.verbose)
208088b4f5fSWarner Losh			end,
209a51f9f0cSKyle Evans			func = core.setVerbose,
2103cd5547bSKyle Evans			alias = {"v", "V"},
211088b4f5fSWarner Losh		},
212d8757746SKyle Evans	},
213aedd6be5SKyle Evans}
214088b4f5fSWarner Losh
215088b4f5fSWarner Loshmenu.welcome = {
216303253e5SKyle Evans	entries = function()
217aedd6be5SKyle Evans		local menu_entries = menu.welcome.all_entries
218d2187b39SRyan Moeller		local multi_user = menu_entries.multi_user
219d2187b39SRyan Moeller		local single_user = menu_entries.single_user
220d2187b39SRyan Moeller		local boot_entry_1, boot_entry_2
2219f71d421SKyle Evans		if core.isSingleUserBoot() then
222d2187b39SRyan Moeller			-- Swap the first two menu items on single user boot.
223d2187b39SRyan Moeller			-- We'll cache the alternate entries for performance.
224d2187b39SRyan Moeller			local alts = menu_entries.alts
225d2187b39SRyan Moeller			if alts == nil then
226d2187b39SRyan Moeller				single_user = core.deepCopyTable(single_user)
227d2187b39SRyan Moeller				multi_user = core.deepCopyTable(multi_user)
228d2187b39SRyan Moeller				single_user.name = single_user.alternate_name
229d2187b39SRyan Moeller				multi_user.name = multi_user.alternate_name
230d2187b39SRyan Moeller				menu_entries.alts = {
231d2187b39SRyan Moeller					single_user = single_user,
232d2187b39SRyan Moeller					multi_user = multi_user,
233d2187b39SRyan Moeller				}
234d2187b39SRyan Moeller			else
235d2187b39SRyan Moeller				single_user = alts.single_user
236d2187b39SRyan Moeller				multi_user = alts.multi_user
2379a0904b0SKyle Evans			end
238d2187b39SRyan Moeller			boot_entry_1, boot_entry_2 = single_user, multi_user
239d2187b39SRyan Moeller		else
240d2187b39SRyan Moeller			boot_entry_1, boot_entry_2 = multi_user, single_user
241303253e5SKyle Evans		end
242d2187b39SRyan Moeller		return {
243d2187b39SRyan Moeller			boot_entry_1,
244d2187b39SRyan Moeller			boot_entry_2,
245d2187b39SRyan Moeller			menu_entries.prompt,
246d2187b39SRyan Moeller			menu_entries.reboot,
247d2187b39SRyan Moeller			{
248d2187b39SRyan Moeller				entry_type = core.MENU_SEPARATOR,
249d2187b39SRyan Moeller			},
250d2187b39SRyan Moeller			{
251d2187b39SRyan Moeller				entry_type = core.MENU_SEPARATOR,
252d2187b39SRyan Moeller				name = "Options:",
253d2187b39SRyan Moeller			},
254d2187b39SRyan Moeller			menu_entries.kernel_options,
255d2187b39SRyan Moeller			menu_entries.boot_options,
256*277f38abSMariusz Zaborski			menu_entries.zpool_checkpoints,
257d2187b39SRyan Moeller			menu_entries.boot_envs,
258d2187b39SRyan Moeller			menu_entries.chainload,
259d2187b39SRyan Moeller		}
260303253e5SKyle Evans	end,
261303253e5SKyle Evans	all_entries = {
262d2187b39SRyan Moeller		multi_user = {
263a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
264a51f9f0cSKyle Evans			name = color.highlight("B") .. "oot Multi user " ..
265a51f9f0cSKyle Evans			    color.highlight("[Enter]"),
2665c1b5165SKyle Evans			-- Not a standard menu entry function!
267a51f9f0cSKyle Evans			alternate_name = color.highlight("B") ..
268a51f9f0cSKyle Evans			    "oot Multi user",
269088b4f5fSWarner Losh			func = function()
270aedd6be5SKyle Evans				core.setSingleUser(false)
271aedd6be5SKyle Evans				core.boot()
272088b4f5fSWarner Losh			end,
2733cd5547bSKyle Evans			alias = {"b", "B"},
274088b4f5fSWarner Losh		},
275d2187b39SRyan Moeller		single_user = {
276a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
277a51f9f0cSKyle Evans			name = "Boot " .. color.highlight("S") .. "ingle user",
2785c1b5165SKyle Evans			-- Not a standard menu entry function!
279a51f9f0cSKyle Evans			alternate_name = "Boot " .. color.highlight("S") ..
280a51f9f0cSKyle Evans			    "ingle user " .. color.highlight("[Enter]"),
281088b4f5fSWarner Losh			func = function()
282aedd6be5SKyle Evans				core.setSingleUser(true)
283aedd6be5SKyle Evans				core.boot()
284088b4f5fSWarner Losh			end,
2853cd5547bSKyle Evans			alias = {"s", "S"},
286088b4f5fSWarner Losh		},
287d2187b39SRyan Moeller		prompt = {
288a7cf0562SKyle Evans			entry_type = core.MENU_RETURN,
289a51f9f0cSKyle Evans			name = color.highlight("Esc") .. "ape to loader prompt",
290ef625845SKyle Evans			func = function()
291aedd6be5SKyle Evans				loader.setenv("autoboot_delay", "NO")
292ef625845SKyle Evans			end,
2933cd5547bSKyle Evans			alias = {core.KEYSTR_ESCAPE},
294088b4f5fSWarner Losh		},
295d2187b39SRyan Moeller		reboot = {
296a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
297a51f9f0cSKyle Evans			name = color.highlight("R") .. "eboot",
298088b4f5fSWarner Losh			func = function()
299aedd6be5SKyle Evans				loader.perform("reboot")
300088b4f5fSWarner Losh			end,
3013cd5547bSKyle Evans			alias = {"r", "R"},
302088b4f5fSWarner Losh		},
303d2187b39SRyan Moeller		kernel_options = {
304a7cf0562SKyle Evans			entry_type = core.MENU_CAROUSEL_ENTRY,
305ada26c4aSKyle Evans			carousel_id = "kernel",
306ada26c4aSKyle Evans			items = core.kernelList,
307ada26c4aSKyle Evans			name = function(idx, choice, all_choices)
3089f71d421SKyle Evans				if #all_choices == 0 then
309aedd6be5SKyle Evans					return "Kernel: "
310088b4f5fSWarner Losh				end
311b1b1f2b8SKyle Evans
312aedd6be5SKyle Evans				local is_default = (idx == 1)
313aedd6be5SKyle Evans				local kernel_name = ""
314aedd6be5SKyle Evans				local name_color
3159f71d421SKyle Evans				if is_default then
3168ce1744fSKyle Evans					name_color = color.escapefg(color.GREEN)
317aedd6be5SKyle Evans					kernel_name = "default/"
318bcf48a15SKyle Evans				else
3198ce1744fSKyle Evans					name_color = color.escapefg(color.BLUE)
320b1b1f2b8SKyle Evans				end
321fd2b19b3SKyle Evans				kernel_name = kernel_name .. name_color ..
3228ce1744fSKyle Evans				    choice .. color.resetfg()
323fd2b19b3SKyle Evans				return color.highlight("K") .. "ernel: " ..
324fd2b19b3SKyle Evans				    kernel_name .. " (" .. idx .. " of " ..
325aedd6be5SKyle Evans				    #all_choices .. ")"
326088b4f5fSWarner Losh			end,
327e2df27e3SKyle Evans			func = function(_, choice, _)
328e414851fSKyle Evans				if loader.getenv("kernelname") ~= nil then
329e414851fSKyle Evans					loader.perform("unload")
330e414851fSKyle Evans				end
331322a2dddSKyle Evans				config.selectKernel(choice)
332088b4f5fSWarner Losh			end,
3333cd5547bSKyle Evans			alias = {"k", "K"},
334088b4f5fSWarner Losh		},
335d2187b39SRyan Moeller		boot_options = {
336a7cf0562SKyle Evans			entry_type = core.MENU_SUBMENU,
337a51f9f0cSKyle Evans			name = "Boot " .. color.highlight("O") .. "ptions",
3389a28f948SKyle Evans			submenu = menu.boot_options,
3393cd5547bSKyle Evans			alias = {"o", "O"},
340d8757746SKyle Evans		},
341*277f38abSMariusz Zaborski		zpool_checkpoints = {
342*277f38abSMariusz Zaborski			entry_type = core.MENU_ENTRY,
343*277f38abSMariusz Zaborski			name = function()
344*277f38abSMariusz Zaborski				rewind = "No"
345*277f38abSMariusz Zaborski				if core.isRewinded() then
346*277f38abSMariusz Zaborski					rewind = "Yes"
347*277f38abSMariusz Zaborski				end
348*277f38abSMariusz Zaborski				return "Rewind ZFS " .. color.highlight("C") ..
349*277f38abSMariusz Zaborski					"heckpoint: " .. rewind
350*277f38abSMariusz Zaborski			end,
351*277f38abSMariusz Zaborski			func = function()
352*277f38abSMariusz Zaborski				core.changeRewindCheckpoint()
353*277f38abSMariusz Zaborski				if core.isRewinded() then
354*277f38abSMariusz Zaborski					bootenvSet(
355*277f38abSMariusz Zaborski					    core.bootenvDefaultRewinded())
356*277f38abSMariusz Zaborski				else
357*277f38abSMariusz Zaborski					bootenvSet(core.bootenvDefault())
358*277f38abSMariusz Zaborski				end
359*277f38abSMariusz Zaborski				config.setCarouselIndex("be_active", 1)
360*277f38abSMariusz Zaborski			end,
361*277f38abSMariusz Zaborski			visible = function()
362*277f38abSMariusz Zaborski				return core.isZFSBoot() and
363*277f38abSMariusz Zaborski				    core.isCheckpointed()
364*277f38abSMariusz Zaborski			end,
365*277f38abSMariusz Zaborski			alias = {"c", "C"},
366*277f38abSMariusz Zaborski		},
367d2187b39SRyan Moeller		boot_envs = {
3687efc058fSKyle Evans			entry_type = core.MENU_SUBMENU,
3697efc058fSKyle Evans			visible = function()
3707efc058fSKyle Evans				return core.isZFSBoot() and
3717efc058fSKyle Evans				    #core.bootenvList() > 1
3727efc058fSKyle Evans			end,
3737efc058fSKyle Evans			name = "Boot " .. color.highlight("E") .. "nvironments",
3747efc058fSKyle Evans			submenu = menu.boot_environments,
3757efc058fSKyle Evans			alias = {"e", "E"},
3767efc058fSKyle Evans		},
377d2187b39SRyan Moeller		chainload = {
378bdf12807SKyle Evans			entry_type = core.MENU_ENTRY,
379bdf12807SKyle Evans			name = function()
380bdf12807SKyle Evans				return 'Chain' .. color.highlight("L") ..
381bdf12807SKyle Evans				    "oad " .. loader.getenv('chain_disk')
382bdf12807SKyle Evans			end,
383bdf12807SKyle Evans			func = function()
384bdf12807SKyle Evans				loader.perform("chain " ..
385bdf12807SKyle Evans				    loader.getenv('chain_disk'))
386bdf12807SKyle Evans			end,
387bdf12807SKyle Evans			visible = function()
388bdf12807SKyle Evans				return loader.getenv('chain_disk') ~= nil
389bdf12807SKyle Evans			end,
390bdf12807SKyle Evans			alias = {"l", "L"},
391bdf12807SKyle Evans		},
392d8757746SKyle Evans	},
393aedd6be5SKyle Evans}
394088b4f5fSWarner Losh
39520a81676SKyle Evansmenu.default = menu.welcome
39628384160SKyle Evans-- current_alias_table will be used to keep our alias table consistent across
39728384160SKyle Evans-- screen redraws, instead of relying on whatever triggered the redraw to update
39828384160SKyle Evans-- the local alias_table in menu.process.
39928384160SKyle Evansmenu.current_alias_table = {}
40020a81676SKyle Evans
4012a11b810SKyle Evansfunction menu.draw(menudef)
402beafe961SKyle Evans	-- Clear the screen, reset the cursor, then draw
403aedd6be5SKyle Evans	screen.clear()
4042a11b810SKyle Evans	menu.current_alias_table = drawer.drawscreen(menudef)
4052a11b810SKyle Evans	drawn_menu = menudef
406decacd91SKyle Evans	screen.defcursor()
40728384160SKyle Evansend
40828384160SKyle Evans
409ca16d83fSKyle Evans-- 'keypress' allows the caller to indicate that a key has been pressed that we
410ca16d83fSKyle Evans-- should process as our initial input.
4112a11b810SKyle Evansfunction menu.process(menudef, keypress)
4122a11b810SKyle Evans	assert(menudef ~= nil)
41328384160SKyle Evans
4142a11b810SKyle Evans	if drawn_menu ~= menudef then
4152a11b810SKyle Evans		menu.draw(menudef)
4167dcffa90SKyle Evans	end
417ca16d83fSKyle Evans
418da9ab827SKyle Evans	while true do
419ca16d83fSKyle Evans		local key = keypress or io.getchar()
420ca16d83fSKyle Evans		keypress = nil
421088b4f5fSWarner Losh
422b458bf0dSKyle Evans		-- Special key behaviors
4239f71d421SKyle Evans		if (key == core.KEY_BACKSPACE or key == core.KEY_DELETE) and
4242a11b810SKyle Evans		    menudef ~= menu.default then
425aedd6be5SKyle Evans			break
4269f71d421SKyle Evans		elseif key == core.KEY_ENTER then
427aedd6be5SKyle Evans			core.boot()
428041929aaSKyle Evans			-- Should not return.  If it does, escape menu handling
429041929aaSKyle Evans			-- and drop to loader prompt.
430041929aaSKyle Evans			return false
431abc4f7e7SKyle Evans		end
432abc4f7e7SKyle Evans
433abc4f7e7SKyle Evans		key = string.char(key)
434088b4f5fSWarner Losh		-- check to see if key is an alias
435aedd6be5SKyle Evans		local sel_entry = nil
43628384160SKyle Evans		for k, v in pairs(menu.current_alias_table) do
4379f71d421SKyle Evans			if key == k then
438aedd6be5SKyle Evans				sel_entry = v
43928384160SKyle Evans				break
440088b4f5fSWarner Losh			end
441088b4f5fSWarner Losh		end
442088b4f5fSWarner Losh
443088b4f5fSWarner Losh		-- if we have an alias do the assigned action:
4449f71d421SKyle Evans		if sel_entry ~= nil then
445aedd6be5SKyle Evans			local handler = menu.handlers[sel_entry.entry_type]
4462a11b810SKyle Evans			assert(handler ~= nil)
447da9ab827SKyle Evans			-- The handler's return value indicates if we
448da9ab827SKyle Evans			-- need to exit this menu.  An omitted or true
449da9ab827SKyle Evans			-- return value means to continue.
4502a11b810SKyle Evans			if handler(menudef, sel_entry) == false then
451da9ab827SKyle Evans				return
452aefcaa7eSKyle Evans			end
45328384160SKyle Evans			-- If we got an alias key the screen is out of date...
45428384160SKyle Evans			-- redraw it.
4552a11b810SKyle Evans			menu.draw(menudef)
456088b4f5fSWarner Losh		end
457088b4f5fSWarner Losh	end
458088b4f5fSWarner Loshend
459088b4f5fSWarner Losh
460da9ab827SKyle Evansfunction menu.run()
461041929aaSKyle Evans	local autoboot_key
462c84dbc53SKyle Evans	local delay = loader.getenv("autoboot_delay")
463c84dbc53SKyle Evans
464c84dbc53SKyle Evans	if delay ~= nil and delay:lower() == "no" then
465c84dbc53SKyle Evans		delay = nil
466c84dbc53SKyle Evans	else
467c84dbc53SKyle Evans		delay = tonumber(delay) or 10
468c84dbc53SKyle Evans	end
469c84dbc53SKyle Evans
470c84dbc53SKyle Evans	if delay == -1 then
471c84dbc53SKyle Evans		core.boot()
472c84dbc53SKyle Evans		return
473c84dbc53SKyle Evans	end
474c84dbc53SKyle Evans
475beafe961SKyle Evans	menu.draw(menu.default)
476c84dbc53SKyle Evans
477041929aaSKyle Evans	if delay ~= nil then
478041929aaSKyle Evans		autoboot_key = menu.autoboot(delay)
479041929aaSKyle Evans
480041929aaSKyle Evans		-- autoboot_key should return the key pressed.  It will only
481041929aaSKyle Evans		-- return nil if we hit the timeout and executed the timeout
482041929aaSKyle Evans		-- command.  Bail out.
483041929aaSKyle Evans		if autoboot_key == nil then
484041929aaSKyle Evans			return
485041929aaSKyle Evans		end
486041929aaSKyle Evans	end
487ca16d83fSKyle Evans
488ca16d83fSKyle Evans	menu.process(menu.default, autoboot_key)
489beafe961SKyle Evans	drawn_menu = nil
490da9ab827SKyle Evans
491da9ab827SKyle Evans	screen.defcursor()
492da9ab827SKyle Evans	print("Exiting menu!")
493088b4f5fSWarner Loshend
494088b4f5fSWarner Losh
495c84dbc53SKyle Evansfunction menu.autoboot(delay)
4961495c98fSKyle Evans	local x = loader.getenv("loader_menu_timeout_x") or 4
4971495c98fSKyle Evans	local y = loader.getenv("loader_menu_timeout_y") or 23
498c84dbc53SKyle Evans	local endtime = loader.time() + delay
499aedd6be5SKyle Evans	local time
5003244729fSKyle Evans	local last
501088b4f5fSWarner Losh	repeat
502aedd6be5SKyle Evans		time = endtime - loader.time()
5033244729fSKyle Evans		if last == nil or last ~= time then
5043244729fSKyle Evans			last = time
505aedd6be5SKyle Evans			screen.setcursor(x, y)
50657099121SKyle Evans			print("Autoboot in " .. time ..
50757099121SKyle Evans			    " seconds, hit [Enter] to boot" ..
508aedd6be5SKyle Evans			    " or any other key to stop     ")
509aedd6be5SKyle Evans			screen.defcursor()
5103244729fSKyle Evans		end
5119f71d421SKyle Evans		if io.ischar() then
512aedd6be5SKyle Evans			local ch = io.getchar()
5139f71d421SKyle Evans			if ch == core.KEY_ENTER then
514aedd6be5SKyle Evans				break
515088b4f5fSWarner Losh			else
516088b4f5fSWarner Losh				-- erase autoboot msg
517aedd6be5SKyle Evans				screen.setcursor(0, y)
51866964bbcSKyle Evans				print(string.rep(" ", 80))
519aedd6be5SKyle Evans				screen.defcursor()
52012b95c84SKyle Evans				return ch
521088b4f5fSWarner Losh			end
522088b4f5fSWarner Losh		end
523088b4f5fSWarner Losh
524aedd6be5SKyle Evans		loader.delay(50000)
525aedd6be5SKyle Evans	until time <= 0
526088b4f5fSWarner Losh
527a76f8a5bSKyle Evans	local cmd = loader.getenv("menu_timeout_command") or "boot"
528e9c3ceb1SKyle Evans	cli_execute_unparsed(cmd)
529041929aaSKyle Evans	return nil
530088b4f5fSWarner Loshend
531088b4f5fSWarner Losh
53276c75816SKyle Evans-- CLI commands
5338f7f3d08SKyle Evansfunction cli.menu()
53476c75816SKyle Evans	menu.run()
53576c75816SKyle Evansend
53676c75816SKyle Evans
537aedd6be5SKyle Evansreturn menu
538