xref: /freebsd/stand/lua/menu.lua (revision 1495c98f0b3172b53e79debf7b9d6049dbc9227c)
1088b4f5fSWarner Losh--
272e39d71SKyle Evans-- SPDX-License-Identifier: BSD-2-Clause-FreeBSD
372e39d71SKyle Evans--
4088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
521d5bcbeSKyle 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
32088b4f5fSWarner Losh
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
49e15abd1fSKyle Evans		return str .. color.escapef(color.GREEN) .. "On" ..
50aedd6be5SKyle Evans		    color.escapef(color.WHITE)
51e15abd1fSKyle Evans	else
52e15abd1fSKyle Evans		return str .. color.escapef(color.RED) .. "off" ..
53aedd6be5SKyle Evans		    color.escapef(color.WHITE)
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
1187efc058fSKyle Evans					name_color = color.escapef(color.GREEN)
1197efc058fSKyle Evans				else
1207efc058fSKyle Evans					name_color = color.escapef(color.BLUE)
1217efc058fSKyle Evans				end
1227efc058fSKyle Evans				bootenv_name = bootenv_name .. name_color ..
1237efc058fSKyle Evans				    choice .. color.default()
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,
1357efc058fSKyle Evans			name = function()
1367efc058fSKyle Evans				return color.highlight("b") .. "ootfs: " ..
1377efc058fSKyle Evans				    core.bootenvDefault()
1387efc058fSKyle Evans			end,
1397efc058fSKyle Evans			func = function()
1407efc058fSKyle Evans				-- Reset active boot environment to the default
1417efc058fSKyle Evans				config.setCarouselIndex("be_active", 1)
1427efc058fSKyle Evans				bootenvSet(core.bootenvDefault())
1437efc058fSKyle Evans			end,
1447efc058fSKyle Evans			alias = {"b", "B"},
1457efc058fSKyle Evans		},
1467efc058fSKyle Evans	},
1477efc058fSKyle Evans}
1487efc058fSKyle Evans
149088b4f5fSWarner Loshmenu.boot_options = {
150d8757746SKyle Evans	entries = {
151088b4f5fSWarner Losh		-- return to welcome menu
15243f7d9d1SKyle Evans		return_menu_entry,
153088b4f5fSWarner Losh		-- load defaults
154088b4f5fSWarner Losh		{
155a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
156a51f9f0cSKyle Evans			name = "Load System " .. color.highlight("D") ..
157a51f9f0cSKyle Evans			    "efaults",
158a51f9f0cSKyle Evans			func = core.setDefaults,
1593cd5547bSKyle Evans			alias = {"d", "D"},
160088b4f5fSWarner Losh		},
161088b4f5fSWarner Losh		{
162a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
163088b4f5fSWarner Losh		},
164088b4f5fSWarner Losh		{
165a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
166a51f9f0cSKyle Evans			name = "Boot Options:",
167088b4f5fSWarner Losh		},
168088b4f5fSWarner Losh		-- acpi
169088b4f5fSWarner Losh		{
170a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
171c1ab36f5SKyle Evans			visible = core.isSystem386,
172088b4f5fSWarner Losh			name = function()
173fd2b19b3SKyle Evans				return OnOff(color.highlight("A") ..
174aedd6be5SKyle Evans				    "CPI       :", core.acpi)
175088b4f5fSWarner Losh			end,
176a51f9f0cSKyle Evans			func = core.setACPI,
1773cd5547bSKyle Evans			alias = {"a", "A"},
178088b4f5fSWarner Losh		},
179088b4f5fSWarner Losh		-- safe mode
180088b4f5fSWarner Losh		{
181a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
182088b4f5fSWarner Losh			name = function()
18357099121SKyle Evans				return OnOff("Safe " .. color.highlight("M") ..
184aedd6be5SKyle Evans				    "ode  :", core.sm)
185088b4f5fSWarner Losh			end,
186a51f9f0cSKyle Evans			func = core.setSafeMode,
1873cd5547bSKyle Evans			alias = {"m", "M"},
188088b4f5fSWarner Losh		},
189088b4f5fSWarner Losh		-- single user
190088b4f5fSWarner Losh		{
191a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
192088b4f5fSWarner Losh			name = function()
193fd2b19b3SKyle Evans				return OnOff(color.highlight("S") ..
194aedd6be5SKyle Evans				    "ingle user:", core.su)
195088b4f5fSWarner Losh			end,
196a51f9f0cSKyle Evans			func = core.setSingleUser,
1973cd5547bSKyle Evans			alias = {"s", "S"},
198088b4f5fSWarner Losh		},
199088b4f5fSWarner Losh		-- verbose boot
200088b4f5fSWarner Losh		{
201a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
202088b4f5fSWarner Losh			name = function()
203fd2b19b3SKyle Evans				return OnOff(color.highlight("V") ..
204aedd6be5SKyle Evans				    "erbose    :", core.verbose)
205088b4f5fSWarner Losh			end,
206a51f9f0cSKyle Evans			func = core.setVerbose,
2073cd5547bSKyle Evans			alias = {"v", "V"},
208088b4f5fSWarner Losh		},
209d8757746SKyle Evans	},
210aedd6be5SKyle Evans}
211088b4f5fSWarner Losh
212088b4f5fSWarner Loshmenu.welcome = {
213303253e5SKyle Evans	entries = function()
214aedd6be5SKyle Evans		local menu_entries = menu.welcome.all_entries
215303253e5SKyle Evans		-- Swap the first two menu items on single user boot
2169f71d421SKyle Evans		if core.isSingleUserBoot() then
2179a0904b0SKyle Evans			-- We'll cache the swapped menu, for performance
2189f71d421SKyle Evans			if menu.welcome.swapped_menu ~= nil then
219aedd6be5SKyle Evans				return menu.welcome.swapped_menu
2209a0904b0SKyle Evans			end
2215c1b5165SKyle Evans			-- Shallow copy the table
222ee4e69f1SKyle Evans			menu_entries = core.deepCopyTable(menu_entries)
2235c1b5165SKyle Evans
2249a0904b0SKyle Evans			-- Swap the first two menu entries
2254b6da14cSKyle Evans			menu_entries[1], menu_entries[2] =
226aedd6be5SKyle Evans			    menu_entries[2], menu_entries[1]
227303253e5SKyle Evans
2289a0904b0SKyle Evans			-- Then set their names to their alternate names
2299a0904b0SKyle Evans			menu_entries[1].name, menu_entries[2].name =
2309a0904b0SKyle Evans			    menu_entries[1].alternate_name,
231aedd6be5SKyle Evans			    menu_entries[2].alternate_name
232aedd6be5SKyle Evans			menu.welcome.swapped_menu = menu_entries
233303253e5SKyle Evans		end
234aedd6be5SKyle Evans		return menu_entries
235303253e5SKyle Evans	end,
236303253e5SKyle Evans	all_entries = {
237088b4f5fSWarner Losh		-- boot multi user
238088b4f5fSWarner Losh		{
239a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
240a51f9f0cSKyle Evans			name = color.highlight("B") .. "oot Multi user " ..
241a51f9f0cSKyle Evans			    color.highlight("[Enter]"),
2425c1b5165SKyle Evans			-- Not a standard menu entry function!
243a51f9f0cSKyle Evans			alternate_name = color.highlight("B") ..
244a51f9f0cSKyle Evans			    "oot Multi user",
245088b4f5fSWarner Losh			func = function()
246aedd6be5SKyle Evans				core.setSingleUser(false)
247aedd6be5SKyle Evans				core.boot()
248088b4f5fSWarner Losh			end,
2493cd5547bSKyle Evans			alias = {"b", "B"},
250088b4f5fSWarner Losh		},
251088b4f5fSWarner Losh		-- boot single user
252088b4f5fSWarner Losh		{
253a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
254a51f9f0cSKyle Evans			name = "Boot " .. color.highlight("S") .. "ingle user",
2555c1b5165SKyle Evans			-- Not a standard menu entry function!
256a51f9f0cSKyle Evans			alternate_name = "Boot " .. color.highlight("S") ..
257a51f9f0cSKyle Evans			    "ingle user " .. color.highlight("[Enter]"),
258088b4f5fSWarner Losh			func = function()
259aedd6be5SKyle Evans				core.setSingleUser(true)
260aedd6be5SKyle Evans				core.boot()
261088b4f5fSWarner Losh			end,
2623cd5547bSKyle Evans			alias = {"s", "S"},
263088b4f5fSWarner Losh		},
264088b4f5fSWarner Losh		-- escape to interpreter
265088b4f5fSWarner Losh		{
266a7cf0562SKyle Evans			entry_type = core.MENU_RETURN,
267a51f9f0cSKyle Evans			name = color.highlight("Esc") .. "ape to loader prompt",
268ef625845SKyle Evans			func = function()
269aedd6be5SKyle Evans				loader.setenv("autoboot_delay", "NO")
270ef625845SKyle Evans			end,
2713cd5547bSKyle Evans			alias = {core.KEYSTR_ESCAPE},
272088b4f5fSWarner Losh		},
273088b4f5fSWarner Losh		-- reboot
274088b4f5fSWarner Losh		{
275a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
276a51f9f0cSKyle Evans			name = color.highlight("R") .. "eboot",
277088b4f5fSWarner Losh			func = function()
278aedd6be5SKyle Evans				loader.perform("reboot")
279088b4f5fSWarner Losh			end,
2803cd5547bSKyle Evans			alias = {"r", "R"},
281088b4f5fSWarner Losh		},
282088b4f5fSWarner Losh		{
283a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
284088b4f5fSWarner Losh		},
285088b4f5fSWarner Losh		{
286a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
287a51f9f0cSKyle Evans			name = "Options:",
288088b4f5fSWarner Losh		},
289088b4f5fSWarner Losh		-- kernel options
290088b4f5fSWarner Losh		{
291a7cf0562SKyle Evans			entry_type = core.MENU_CAROUSEL_ENTRY,
292ada26c4aSKyle Evans			carousel_id = "kernel",
293ada26c4aSKyle Evans			items = core.kernelList,
294ada26c4aSKyle Evans			name = function(idx, choice, all_choices)
2959f71d421SKyle Evans				if #all_choices == 0 then
296aedd6be5SKyle Evans					return "Kernel: "
297088b4f5fSWarner Losh				end
298b1b1f2b8SKyle Evans
299aedd6be5SKyle Evans				local is_default = (idx == 1)
300aedd6be5SKyle Evans				local kernel_name = ""
301aedd6be5SKyle Evans				local name_color
3029f71d421SKyle Evans				if is_default then
303aedd6be5SKyle Evans					name_color = color.escapef(color.GREEN)
304aedd6be5SKyle Evans					kernel_name = "default/"
305bcf48a15SKyle Evans				else
306aedd6be5SKyle Evans					name_color = color.escapef(color.BLUE)
307b1b1f2b8SKyle Evans				end
308fd2b19b3SKyle Evans				kernel_name = kernel_name .. name_color ..
309aedd6be5SKyle Evans				    choice .. color.default()
310fd2b19b3SKyle Evans				return color.highlight("K") .. "ernel: " ..
311fd2b19b3SKyle Evans				    kernel_name .. " (" .. idx .. " of " ..
312aedd6be5SKyle Evans				    #all_choices .. ")"
313088b4f5fSWarner Losh			end,
314e2df27e3SKyle Evans			func = function(_, choice, _)
315322a2dddSKyle Evans				config.selectKernel(choice)
316088b4f5fSWarner Losh			end,
3173cd5547bSKyle Evans			alias = {"k", "K"},
318088b4f5fSWarner Losh		},
319088b4f5fSWarner Losh		-- boot options
320088b4f5fSWarner Losh		{
321a7cf0562SKyle Evans			entry_type = core.MENU_SUBMENU,
322a51f9f0cSKyle Evans			name = "Boot " .. color.highlight("O") .. "ptions",
3239a28f948SKyle Evans			submenu = menu.boot_options,
3243cd5547bSKyle Evans			alias = {"o", "O"},
325d8757746SKyle Evans		},
3267efc058fSKyle Evans		-- boot environments
3277efc058fSKyle Evans		{
3287efc058fSKyle Evans			entry_type = core.MENU_SUBMENU,
3297efc058fSKyle Evans			visible = function()
3307efc058fSKyle Evans				return core.isZFSBoot() and
3317efc058fSKyle Evans				    #core.bootenvList() > 1
3327efc058fSKyle Evans			end,
3337efc058fSKyle Evans			name = "Boot " .. color.highlight("E") .. "nvironments",
3347efc058fSKyle Evans			submenu = menu.boot_environments,
3357efc058fSKyle Evans			alias = {"e", "E"},
3367efc058fSKyle Evans		},
337d8757746SKyle Evans	},
338aedd6be5SKyle Evans}
339088b4f5fSWarner Losh
34020a81676SKyle Evansmenu.default = menu.welcome
34128384160SKyle Evans-- current_alias_table will be used to keep our alias table consistent across
34228384160SKyle Evans-- screen redraws, instead of relying on whatever triggered the redraw to update
34328384160SKyle Evans-- the local alias_table in menu.process.
34428384160SKyle Evansmenu.current_alias_table = {}
34520a81676SKyle Evans
3462a11b810SKyle Evansfunction menu.draw(menudef)
347beafe961SKyle Evans	-- Clear the screen, reset the cursor, then draw
348aedd6be5SKyle Evans	screen.clear()
3492a11b810SKyle Evans	menu.current_alias_table = drawer.drawscreen(menudef)
3502a11b810SKyle Evans	drawn_menu = menudef
351decacd91SKyle Evans	screen.defcursor()
35228384160SKyle Evansend
35328384160SKyle Evans
354ca16d83fSKyle Evans-- 'keypress' allows the caller to indicate that a key has been pressed that we
355ca16d83fSKyle Evans-- should process as our initial input.
3562a11b810SKyle Evansfunction menu.process(menudef, keypress)
3572a11b810SKyle Evans	assert(menudef ~= nil)
35828384160SKyle Evans
3592a11b810SKyle Evans	if drawn_menu ~= menudef then
3602a11b810SKyle Evans		menu.draw(menudef)
3617dcffa90SKyle Evans	end
362ca16d83fSKyle Evans
363da9ab827SKyle Evans	while true do
364ca16d83fSKyle Evans		local key = keypress or io.getchar()
365ca16d83fSKyle Evans		keypress = nil
366088b4f5fSWarner Losh
367b458bf0dSKyle Evans		-- Special key behaviors
3689f71d421SKyle Evans		if (key == core.KEY_BACKSPACE or key == core.KEY_DELETE) and
3692a11b810SKyle Evans		    menudef ~= menu.default then
370aedd6be5SKyle Evans			break
3719f71d421SKyle Evans		elseif key == core.KEY_ENTER then
372aedd6be5SKyle Evans			core.boot()
373b458bf0dSKyle Evans			-- Should not return
374abc4f7e7SKyle Evans		end
375abc4f7e7SKyle Evans
376abc4f7e7SKyle Evans		key = string.char(key)
377088b4f5fSWarner Losh		-- check to see if key is an alias
378aedd6be5SKyle Evans		local sel_entry = nil
37928384160SKyle Evans		for k, v in pairs(menu.current_alias_table) do
3809f71d421SKyle Evans			if key == k then
381aedd6be5SKyle Evans				sel_entry = v
38228384160SKyle Evans				break
383088b4f5fSWarner Losh			end
384088b4f5fSWarner Losh		end
385088b4f5fSWarner Losh
386088b4f5fSWarner Losh		-- if we have an alias do the assigned action:
3879f71d421SKyle Evans		if sel_entry ~= nil then
388aedd6be5SKyle Evans			local handler = menu.handlers[sel_entry.entry_type]
3892a11b810SKyle Evans			assert(handler ~= nil)
390da9ab827SKyle Evans			-- The handler's return value indicates if we
391da9ab827SKyle Evans			-- need to exit this menu.  An omitted or true
392da9ab827SKyle Evans			-- return value means to continue.
3932a11b810SKyle Evans			if handler(menudef, sel_entry) == false then
394da9ab827SKyle Evans				return
395aefcaa7eSKyle Evans			end
39628384160SKyle Evans			-- If we got an alias key the screen is out of date...
39728384160SKyle Evans			-- redraw it.
3982a11b810SKyle Evans			menu.draw(menudef)
399088b4f5fSWarner Losh		end
400088b4f5fSWarner Losh	end
401088b4f5fSWarner Loshend
402088b4f5fSWarner Losh
403da9ab827SKyle Evansfunction menu.run()
404beafe961SKyle Evans	menu.draw(menu.default)
405ca16d83fSKyle Evans	local autoboot_key = menu.autoboot()
406ca16d83fSKyle Evans
407ca16d83fSKyle Evans	menu.process(menu.default, autoboot_key)
408beafe961SKyle Evans	drawn_menu = nil
409da9ab827SKyle Evans
410da9ab827SKyle Evans	screen.defcursor()
411da9ab827SKyle Evans	print("Exiting menu!")
412088b4f5fSWarner Loshend
413088b4f5fSWarner Losh
414088b4f5fSWarner Loshfunction menu.autoboot()
415aedd6be5SKyle Evans	local ab = loader.getenv("autoboot_delay")
4169f71d421SKyle Evans	if ab ~= nil and ab:lower() == "no" then
41712b95c84SKyle Evans		return nil
4189f71d421SKyle Evans	elseif tonumber(ab) == -1 then
419aedd6be5SKyle Evans		core.boot()
420088b4f5fSWarner Losh	end
421aedd6be5SKyle Evans	ab = tonumber(ab) or 10
422088b4f5fSWarner Losh
423*1495c98fSKyle Evans	local x = loader.getenv("loader_menu_timeout_x") or 4
424*1495c98fSKyle Evans	local y = loader.getenv("loader_menu_timeout_y") or 23
425088b4f5fSWarner Losh
426aedd6be5SKyle Evans	local endtime = loader.time() + ab
427aedd6be5SKyle Evans	local time
428088b4f5fSWarner Losh
429088b4f5fSWarner Losh	repeat
430aedd6be5SKyle Evans		time = endtime - loader.time()
431aedd6be5SKyle Evans		screen.setcursor(x, y)
43257099121SKyle Evans		print("Autoboot in " .. time ..
43357099121SKyle Evans		    " seconds, hit [Enter] to boot" ..
434aedd6be5SKyle Evans		    " or any other key to stop     ")
435aedd6be5SKyle Evans		screen.defcursor()
4369f71d421SKyle Evans		if io.ischar() then
437aedd6be5SKyle Evans			local ch = io.getchar()
4389f71d421SKyle Evans			if ch == core.KEY_ENTER then
439aedd6be5SKyle Evans				break
440088b4f5fSWarner Losh			else
441088b4f5fSWarner Losh				-- erase autoboot msg
442aedd6be5SKyle Evans				screen.setcursor(0, y)
44366964bbcSKyle Evans				print(string.rep(" ", 80))
444aedd6be5SKyle Evans				screen.defcursor()
44512b95c84SKyle Evans				return ch
446088b4f5fSWarner Losh			end
447088b4f5fSWarner Losh		end
448088b4f5fSWarner Losh
449aedd6be5SKyle Evans		loader.delay(50000)
450aedd6be5SKyle Evans	until time <= 0
451088b4f5fSWarner Losh
452a76f8a5bSKyle Evans	local cmd = loader.getenv("menu_timeout_command") or "boot"
453a76f8a5bSKyle Evans	loader.interpret(cmd)
454088b4f5fSWarner Loshend
455088b4f5fSWarner Losh
456aedd6be5SKyle Evansreturn menu
457