xref: /freebsd/stand/lua/menu.lua (revision e15abd1f164933eae0c5ddcc6cfebe5fbff1a85c)
1088b4f5fSWarner Losh--
2088b4f5fSWarner Losh-- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
321d5bcbeSKyle Evans-- Copyright (C) 2018 Kyle Evans <kevans@FreeBSD.org>
4088b4f5fSWarner Losh-- All rights reserved.
5088b4f5fSWarner Losh--
6088b4f5fSWarner Losh-- Redistribution and use in source and binary forms, with or without
7088b4f5fSWarner Losh-- modification, are permitted provided that the following conditions
8088b4f5fSWarner Losh-- are met:
9088b4f5fSWarner Losh-- 1. Redistributions of source code must retain the above copyright
10088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer.
11088b4f5fSWarner Losh-- 2. Redistributions in binary form must reproduce the above copyright
12088b4f5fSWarner Losh--    notice, this list of conditions and the following disclaimer in the
13088b4f5fSWarner Losh--    documentation and/or other materials provided with the distribution.
14088b4f5fSWarner Losh--
15088b4f5fSWarner Losh-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16088b4f5fSWarner Losh-- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17088b4f5fSWarner Losh-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18088b4f5fSWarner Losh-- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19088b4f5fSWarner Losh-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20088b4f5fSWarner Losh-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21088b4f5fSWarner Losh-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22088b4f5fSWarner Losh-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23088b4f5fSWarner Losh-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24088b4f5fSWarner Losh-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25088b4f5fSWarner Losh-- SUCH DAMAGE.
26088b4f5fSWarner Losh--
27088b4f5fSWarner Losh-- $FreeBSD$
28088b4f5fSWarner Losh--
29088b4f5fSWarner Losh
30088b4f5fSWarner Losh
31088b4f5fSWarner Loshlocal core = require("core");
32088b4f5fSWarner Loshlocal color = require("color");
33088b4f5fSWarner Loshlocal config = require("config");
34088b4f5fSWarner Loshlocal screen = require("screen");
35088b4f5fSWarner Loshlocal drawer = require("drawer");
36088b4f5fSWarner Losh
37c8518398SKyle Evanslocal menu = {};
38c8518398SKyle Evans
39088b4f5fSWarner Loshlocal skip;
40088b4f5fSWarner Loshlocal run;
41088b4f5fSWarner Loshlocal autoboot;
42ada26c4aSKyle Evanslocal carousel_choices = {};
43088b4f5fSWarner Losh
44*e15abd1fSKyle Evanslocal OnOff = function(str, b)
45*e15abd1fSKyle Evans	if (b) then
46*e15abd1fSKyle Evans		return str .. color.escapef(color.GREEN) .. "On" ..
47*e15abd1fSKyle Evans		    color.escapef(color.WHITE);
48*e15abd1fSKyle Evans	else
49*e15abd1fSKyle Evans		return str .. color.escapef(color.RED) .. "off" ..
50*e15abd1fSKyle Evans		    color.escapef(color.WHITE);
51*e15abd1fSKyle Evans	end
52*e15abd1fSKyle Evansend
53*e15abd1fSKyle Evans
54*e15abd1fSKyle Evans
558d415029SKyle Evansmenu.handlers = {
568d415029SKyle Evans	-- Menu handlers take the current menu and selected entry as parameters,
578d415029SKyle Evans	-- and should return a boolean indicating whether execution should
588d415029SKyle Evans	-- continue or not. The return value may be omitted if this entry should
598d415029SKyle Evans	-- have no bearing on whether we continue or not, indicating that we
608d415029SKyle Evans	-- should just continue after execution.
618d415029SKyle Evans	[core.MENU_ENTRY] = function(current_menu, entry)
628d415029SKyle Evans		-- run function
638d415029SKyle Evans		entry.func();
648d415029SKyle Evans	end,
658d415029SKyle Evans	[core.MENU_CAROUSEL_ENTRY] = function(current_menu, entry)
668d415029SKyle Evans		-- carousel (rotating) functionality
678d415029SKyle Evans		local carid = entry.carousel_id;
688d415029SKyle Evans		local caridx = menu.getCarouselIndex(carid);
698d415029SKyle Evans		local choices = entry.items();
708d415029SKyle Evans
718d415029SKyle Evans		if (#choices > 0) then
728d415029SKyle Evans			caridx = (caridx % #choices) + 1;
738d415029SKyle Evans			menu.setCarouselIndex(carid, caridx);
748d415029SKyle Evans			entry.func(caridx, choices[caridx], choices);
758d415029SKyle Evans		end
768d415029SKyle Evans	end,
778d415029SKyle Evans	[core.MENU_SUBMENU] = function(current_menu, entry)
788d415029SKyle Evans		-- recurse
798d415029SKyle Evans		return menu.run(entry.submenu());
808d415029SKyle Evans	end,
818d415029SKyle Evans	[core.MENU_RETURN] = function(current_menu, entry)
828d415029SKyle Evans		-- allow entry to have a function/side effect
838d415029SKyle Evans		if (entry.func ~= nil) then
848d415029SKyle Evans			entry.func();
858d415029SKyle Evans		end
868d415029SKyle Evans		return false;
878d415029SKyle Evans	end,
888d415029SKyle Evans};
89280e990bSKyle Evans-- loader menu tree is rooted at menu.welcome
90088b4f5fSWarner Losh
91088b4f5fSWarner Loshmenu.boot_options = {
92d8757746SKyle Evans	entries = {
93088b4f5fSWarner Losh		-- return to welcome menu
94088b4f5fSWarner Losh		{
95a7cf0562SKyle Evans			entry_type = core.MENU_RETURN,
96088b4f5fSWarner Losh			name = function()
9757099121SKyle Evans				return "Back to main menu" ..
9857099121SKyle Evans				    color.highlight(" [Backspace]");
991666dfc0SKyle Evans			end
100088b4f5fSWarner Losh		},
101088b4f5fSWarner Losh
102088b4f5fSWarner Losh		-- load defaults
103088b4f5fSWarner Losh		{
104a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
105088b4f5fSWarner Losh			name = function()
10657099121SKyle Evans				return "Load System " .. color.highlight("D") ..
10757099121SKyle Evans				    "efaults";
108088b4f5fSWarner Losh			end,
109088b4f5fSWarner Losh			func = function()
11024a1bd54SKyle Evans				core.setDefaults();
111088b4f5fSWarner Losh			end,
112088b4f5fSWarner Losh			alias = {"d", "D"}
113088b4f5fSWarner Losh		},
114088b4f5fSWarner Losh
115088b4f5fSWarner Losh		{
116a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
117088b4f5fSWarner Losh			name = function()
118088b4f5fSWarner Losh				return "";
119088b4f5fSWarner Losh			end
120088b4f5fSWarner Losh		},
121088b4f5fSWarner Losh
122088b4f5fSWarner Losh		{
123a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
124088b4f5fSWarner Losh			name = function()
125088b4f5fSWarner Losh				return "Boot Options:";
126088b4f5fSWarner Losh			end
127088b4f5fSWarner Losh		},
128088b4f5fSWarner Losh
129088b4f5fSWarner Losh		-- acpi
130088b4f5fSWarner Losh		{
131a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
132088b4f5fSWarner Losh			name = function()
133fd2b19b3SKyle Evans				return OnOff(color.highlight("A") ..
134fd2b19b3SKyle Evans				    "CPI       :", core.acpi);
135088b4f5fSWarner Losh			end,
136088b4f5fSWarner Losh			func = function()
137088b4f5fSWarner Losh				core.setACPI();
138088b4f5fSWarner Losh			end,
139088b4f5fSWarner Losh			alias = {"a", "A"}
140088b4f5fSWarner Losh		},
141088b4f5fSWarner Losh		-- safe mode
142088b4f5fSWarner Losh		{
143a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
144088b4f5fSWarner Losh			name = function()
14557099121SKyle Evans				return OnOff("Safe " .. color.highlight("M") ..
14657099121SKyle Evans				    "ode  :", core.sm);
147088b4f5fSWarner Losh			end,
148088b4f5fSWarner Losh			func = function()
149088b4f5fSWarner Losh				core.setSafeMode();
150088b4f5fSWarner Losh			end,
151088b4f5fSWarner Losh			alias = {"m", "M"}
152088b4f5fSWarner Losh		},
153088b4f5fSWarner Losh		-- single user
154088b4f5fSWarner Losh		{
155a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
156088b4f5fSWarner Losh			name = function()
157fd2b19b3SKyle Evans				return OnOff(color.highlight("S") ..
158fd2b19b3SKyle Evans				    "ingle user:", core.su);
159088b4f5fSWarner Losh			end,
160088b4f5fSWarner Losh			func = function()
161088b4f5fSWarner Losh				core.setSingleUser();
162088b4f5fSWarner Losh			end,
163088b4f5fSWarner Losh			alias = {"s", "S"}
164088b4f5fSWarner Losh		},
165088b4f5fSWarner Losh		-- verbose boot
166088b4f5fSWarner Losh		{
167a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
168088b4f5fSWarner Losh			name = function()
169fd2b19b3SKyle Evans				return OnOff(color.highlight("V") ..
170fd2b19b3SKyle Evans				    "erbose    :", core.verbose);
171088b4f5fSWarner Losh			end,
172088b4f5fSWarner Losh			func = function()
173088b4f5fSWarner Losh				core.setVerbose();
174088b4f5fSWarner Losh			end,
175088b4f5fSWarner Losh			alias = {"v", "V"}
176088b4f5fSWarner Losh		},
177d8757746SKyle Evans	},
178088b4f5fSWarner Losh};
179088b4f5fSWarner Losh
180088b4f5fSWarner Loshmenu.welcome = {
181303253e5SKyle Evans	entries = function()
182303253e5SKyle Evans		local menu_entries = menu.welcome.all_entries;
183303253e5SKyle Evans		-- Swap the first two menu items on single user boot
184303253e5SKyle Evans		if (core.isSingleUserBoot()) then
1859a0904b0SKyle Evans			-- We'll cache the swapped menu, for performance
1869a0904b0SKyle Evans			if (menu.welcome.swapped_menu ~= nil) then
1879a0904b0SKyle Evans				return menu.welcome.swapped_menu;
1889a0904b0SKyle Evans			end
1895c1b5165SKyle Evans			-- Shallow copy the table
1905c1b5165SKyle Evans			menu_entries = core.shallowCopyTable(menu_entries);
1915c1b5165SKyle Evans
1929a0904b0SKyle Evans			-- Swap the first two menu entries
1934b6da14cSKyle Evans			menu_entries[1], menu_entries[2] =
1944b6da14cSKyle Evans			    menu_entries[2], menu_entries[1];
195303253e5SKyle Evans
1969a0904b0SKyle Evans			-- Then set their names to their alternate names
1979a0904b0SKyle Evans			menu_entries[1].name, menu_entries[2].name =
1989a0904b0SKyle Evans			    menu_entries[1].alternate_name,
1999a0904b0SKyle Evans			    menu_entries[2].alternate_name;
2009a0904b0SKyle Evans			menu.welcome.swapped_menu = menu_entries;
201303253e5SKyle Evans		end
202303253e5SKyle Evans		return menu_entries;
203303253e5SKyle Evans	end,
204303253e5SKyle Evans	all_entries = {
205088b4f5fSWarner Losh		-- boot multi user
206088b4f5fSWarner Losh		{
207a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
208088b4f5fSWarner Losh			name = function()
209fd2b19b3SKyle Evans				return color.highlight("B") ..
210fd2b19b3SKyle Evans				    "oot Multi user " ..
21157099121SKyle Evans				    color.highlight("[Enter]");
212088b4f5fSWarner Losh			end,
2135c1b5165SKyle Evans			-- Not a standard menu entry function!
2145c1b5165SKyle Evans			alternate_name = function()
2155c1b5165SKyle Evans				return color.highlight("B") ..
2165c1b5165SKyle Evans				    "oot Multi user";
2175c1b5165SKyle Evans			end,
218088b4f5fSWarner Losh			func = function()
219088b4f5fSWarner Losh				core.setSingleUser(false);
220088b4f5fSWarner Losh				core.boot();
221088b4f5fSWarner Losh			end,
222b458bf0dSKyle Evans			alias = {"b", "B"}
223088b4f5fSWarner Losh		},
224088b4f5fSWarner Losh
225088b4f5fSWarner Losh		-- boot single user
226088b4f5fSWarner Losh		{
227a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
228088b4f5fSWarner Losh			name = function()
229fd2b19b3SKyle Evans				return "Boot " .. color.highlight("S") ..
230fd2b19b3SKyle Evans				    "ingle user";
231088b4f5fSWarner Losh			end,
2325c1b5165SKyle Evans			-- Not a standard menu entry function!
2335c1b5165SKyle Evans			alternate_name = function()
2345c1b5165SKyle Evans				return "Boot " .. color.highlight("S") ..
2355c1b5165SKyle Evans				    "ingle user " .. color.highlight("[Enter]");
2365c1b5165SKyle Evans			end,
237088b4f5fSWarner Losh			func = function()
238088b4f5fSWarner Losh				core.setSingleUser(true);
239088b4f5fSWarner Losh				core.boot();
240088b4f5fSWarner Losh			end,
241088b4f5fSWarner Losh			alias = {"s", "S"}
242088b4f5fSWarner Losh		},
243088b4f5fSWarner Losh
244088b4f5fSWarner Losh		-- escape to interpreter
245088b4f5fSWarner Losh		{
246a7cf0562SKyle Evans			entry_type = core.MENU_RETURN,
247088b4f5fSWarner Losh			name = function()
248fd2b19b3SKyle Evans				return color.highlight("Esc") ..
249fd2b19b3SKyle Evans				    "ape to loader prompt";
250088b4f5fSWarner Losh			end,
251ef625845SKyle Evans			func = function()
25224a1bd54SKyle Evans				loader.setenv("autoboot_delay", "NO");
253ef625845SKyle Evans			end,
25439006570SKyle Evans			alias = {core.KEYSTR_ESCAPE}
255088b4f5fSWarner Losh		},
256088b4f5fSWarner Losh
257088b4f5fSWarner Losh		-- reboot
258088b4f5fSWarner Losh		{
259a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
260088b4f5fSWarner Losh			name = function()
261088b4f5fSWarner Losh				return color.highlight("R") .. "eboot";
262088b4f5fSWarner Losh			end,
263088b4f5fSWarner Losh			func = function()
264088b4f5fSWarner Losh				loader.perform("reboot");
265088b4f5fSWarner Losh			end,
266088b4f5fSWarner Losh			alias = {"r", "R"}
267088b4f5fSWarner Losh		},
268088b4f5fSWarner Losh
269088b4f5fSWarner Losh
270088b4f5fSWarner Losh		{
271a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
272088b4f5fSWarner Losh			name = function()
273088b4f5fSWarner Losh				return "";
274088b4f5fSWarner Losh			end
275088b4f5fSWarner Losh		},
276088b4f5fSWarner Losh
277088b4f5fSWarner Losh		{
278a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
279088b4f5fSWarner Losh			name = function()
280088b4f5fSWarner Losh				return "Options:";
281088b4f5fSWarner Losh			end
282088b4f5fSWarner Losh		},
283088b4f5fSWarner Losh
284088b4f5fSWarner Losh		-- kernel options
285088b4f5fSWarner Losh		{
286a7cf0562SKyle Evans			entry_type = core.MENU_CAROUSEL_ENTRY,
287ada26c4aSKyle Evans			carousel_id = "kernel",
288ada26c4aSKyle Evans			items = core.kernelList,
289ada26c4aSKyle Evans			name = function(idx, choice, all_choices)
29024a1bd54SKyle Evans				if (#all_choices == 0) then
291b1b1f2b8SKyle Evans					return "Kernel: ";
292088b4f5fSWarner Losh				end
293b1b1f2b8SKyle Evans
294bcf48a15SKyle Evans				local is_default = (idx == 1);
295bcf48a15SKyle Evans				local kernel_name = "";
296bcf48a15SKyle Evans				local name_color;
29724a1bd54SKyle Evans				if (is_default) then
298bcf48a15SKyle Evans					name_color = color.escapef(color.GREEN);
299bcf48a15SKyle Evans					kernel_name = "default/";
300bcf48a15SKyle Evans				else
301bcf48a15SKyle Evans					name_color = color.escapef(color.BLUE);
302b1b1f2b8SKyle Evans				end
303fd2b19b3SKyle Evans				kernel_name = kernel_name .. name_color ..
304fd2b19b3SKyle Evans				    choice .. color.default();
305fd2b19b3SKyle Evans				return color.highlight("K") .. "ernel: " ..
306fd2b19b3SKyle Evans				    kernel_name .. " (" .. idx .. " of " ..
307fd2b19b3SKyle Evans				    #all_choices .. ")";
308088b4f5fSWarner Losh			end,
3095d1e2f83SKyle Evans			func = function(idx, choice, all_choices)
310fa4a2394SKyle Evans				config.selectkernel(choice);
311088b4f5fSWarner Losh			end,
312088b4f5fSWarner Losh			alias = {"k", "K"}
313088b4f5fSWarner Losh		},
314088b4f5fSWarner Losh
315088b4f5fSWarner Losh		-- boot options
316088b4f5fSWarner Losh		{
317a7cf0562SKyle Evans			entry_type = core.MENU_SUBMENU,
318088b4f5fSWarner Losh			name = function()
319fd2b19b3SKyle Evans				return "Boot " .. color.highlight("O") ..
320fd2b19b3SKyle Evans				    "ptions";
321088b4f5fSWarner Losh			end,
322088b4f5fSWarner Losh			submenu = function()
323088b4f5fSWarner Losh				return menu.boot_options;
324088b4f5fSWarner Losh			end,
325088b4f5fSWarner Losh			alias = {"o", "O"}
326d8757746SKyle Evans		},
327d8757746SKyle Evans	},
328088b4f5fSWarner Losh};
329088b4f5fSWarner Losh
330ada26c4aSKyle Evans-- The first item in every carousel is always the default item.
331ada26c4aSKyle Evansfunction menu.getCarouselIndex(id)
332ada26c4aSKyle Evans	local val = carousel_choices[id];
333ada26c4aSKyle Evans	if (val == nil) then
334ada26c4aSKyle Evans		return 1;
335ada26c4aSKyle Evans	end
336ada26c4aSKyle Evans	return val;
337ada26c4aSKyle Evansend
338ada26c4aSKyle Evans
339ada26c4aSKyle Evansfunction menu.setCarouselIndex(id, idx)
340ada26c4aSKyle Evans	carousel_choices[id] = idx;
341ada26c4aSKyle Evansend
342ada26c4aSKyle Evans
343088b4f5fSWarner Loshfunction menu.run(m)
344088b4f5fSWarner Losh
345088b4f5fSWarner Losh	if (menu.skip()) then
346088b4f5fSWarner Losh		core.autoboot();
347088b4f5fSWarner Losh		return false;
348088b4f5fSWarner Losh	end
349088b4f5fSWarner Losh
350088b4f5fSWarner Losh	if (m == nil) then
351088b4f5fSWarner Losh		m = menu.welcome;
352088b4f5fSWarner Losh	end
353088b4f5fSWarner Losh
354088b4f5fSWarner Losh	-- redraw screen
355088b4f5fSWarner Losh	screen.clear();
356088b4f5fSWarner Losh	screen.defcursor();
357088b4f5fSWarner Losh	local alias_table = drawer.drawscreen(m);
358088b4f5fSWarner Losh
3593a0a07d0SKyle Evans	menu.autoboot();
360088b4f5fSWarner Losh
361088b4f5fSWarner Losh	cont = true;
36224a1bd54SKyle Evans	while (cont) do
363abc4f7e7SKyle Evans		local key = io.getchar();
364088b4f5fSWarner Losh
365b458bf0dSKyle Evans		-- Special key behaviors
3661504bce3SKyle Evans		if ((key == core.KEY_BACKSPACE) or (key == core.KEY_DELETE)) and
3671504bce3SKyle Evans		    (m ~= menu.welcome) then
3681f5696c7SKyle Evans			break;
369fe672a15SKyle Evans		elseif (key == core.KEY_ENTER) then
370b458bf0dSKyle Evans			core.boot();
371b458bf0dSKyle Evans			-- Should not return
372abc4f7e7SKyle Evans		end
373abc4f7e7SKyle Evans
374abc4f7e7SKyle Evans		key = string.char(key)
375088b4f5fSWarner Losh		-- check to see if key is an alias
376088b4f5fSWarner Losh		local sel_entry = nil;
377088b4f5fSWarner Losh		for k, v in pairs(alias_table) do
378088b4f5fSWarner Losh			if (key == k) then
379088b4f5fSWarner Losh				sel_entry = v;
380088b4f5fSWarner Losh			end
381088b4f5fSWarner Losh		end
382088b4f5fSWarner Losh
383088b4f5fSWarner Losh		-- if we have an alias do the assigned action:
384088b4f5fSWarner Losh		if (sel_entry ~= nil) then
3858d415029SKyle Evans			-- Get menu handler
3868d415029SKyle Evans			local handler = menu.handlers[sel_entry.entry_type];
3878d415029SKyle Evans			if (handler ~= nil) then
3888d415029SKyle Evans				-- The handler's return value indicates whether
3898d415029SKyle Evans				-- we need to exit this menu. An omitted return
3908d415029SKyle Evans				-- value means "continue" by default.
3918d415029SKyle Evans				cont = handler(m, sel_entry);
3928d415029SKyle Evans				if (cont == nil) then
3938d415029SKyle Evans					cont = true;
394aefcaa7eSKyle Evans				end
395088b4f5fSWarner Losh			end
396088b4f5fSWarner Losh			-- if we got an alias key the screen is out of date:
397088b4f5fSWarner Losh			screen.clear();
398088b4f5fSWarner Losh			screen.defcursor();
399088b4f5fSWarner Losh			alias_table = drawer.drawscreen(m);
400088b4f5fSWarner Losh		end
401088b4f5fSWarner Losh	end
402088b4f5fSWarner Losh
403088b4f5fSWarner Losh	if (m == menu.welcome) then
404088b4f5fSWarner Losh		screen.defcursor();
405088b4f5fSWarner Losh		print("Exiting menu!");
406fa4a2394SKyle Evans		config.loadelf();
407088b4f5fSWarner Losh		return false;
408088b4f5fSWarner Losh	end
409088b4f5fSWarner Losh
410088b4f5fSWarner Losh	return true;
411088b4f5fSWarner Loshend
412088b4f5fSWarner Losh
413088b4f5fSWarner Loshfunction menu.skip()
414b140d14bSKyle Evans	if (core.isSerialBoot()) then
415088b4f5fSWarner Losh		return true;
416088b4f5fSWarner Losh	end
417088b4f5fSWarner Losh	local c = string.lower(loader.getenv("console") or "");
41824a1bd54SKyle Evans	if ((c:match("^efi[ ;]") or c:match("[ ;]efi[ ;]")) ~= nil) then
419088b4f5fSWarner Losh		return true;
420088b4f5fSWarner Losh	end
421088b4f5fSWarner Losh
422088b4f5fSWarner Losh	c = string.lower(loader.getenv("beastie_disable") or "");
423088b4f5fSWarner Losh	print("beastie_disable", c);
424088b4f5fSWarner Losh	return c == "yes";
425088b4f5fSWarner Loshend
426088b4f5fSWarner Losh
427088b4f5fSWarner Loshfunction menu.autoboot()
42824a1bd54SKyle Evans	if (menu.already_autoboot == true) then
429088b4f5fSWarner Losh		return;
430088b4f5fSWarner Losh	end
431088b4f5fSWarner Losh	menu.already_autoboot = true;
432088b4f5fSWarner Losh
433088b4f5fSWarner Losh	local ab = loader.getenv("autoboot_delay");
434702b460dSKyle Evans	if (ab ~= nil) and (ab:lower() == "no") then
435702b460dSKyle Evans		return;
436702b460dSKyle Evans	elseif (tonumber(ab) == -1) then
437088b4f5fSWarner Losh		core.boot();
438088b4f5fSWarner Losh	end
439088b4f5fSWarner Losh	ab = tonumber(ab) or 10;
440088b4f5fSWarner Losh
441088b4f5fSWarner Losh	local x = loader.getenv("loader_menu_timeout_x") or 5;
442088b4f5fSWarner Losh	local y = loader.getenv("loader_menu_timeout_y") or 22;
443088b4f5fSWarner Losh
444088b4f5fSWarner Losh	local endtime = loader.time() + ab;
445088b4f5fSWarner Losh	local time;
446088b4f5fSWarner Losh
447088b4f5fSWarner Losh	repeat
448088b4f5fSWarner Losh		time = endtime - loader.time();
449088b4f5fSWarner Losh		screen.setcursor(x, y);
45057099121SKyle Evans		print("Autoboot in " .. time ..
45157099121SKyle Evans		    " seconds, hit [Enter] to boot" ..
45257099121SKyle Evans		    " or any other key to stop     ");
453088b4f5fSWarner Losh		screen.defcursor();
45424a1bd54SKyle Evans		if (io.ischar()) then
455088b4f5fSWarner Losh			local ch = io.getchar();
45624a1bd54SKyle Evans			if (ch == core.KEY_ENTER) then
457088b4f5fSWarner Losh				break;
458088b4f5fSWarner Losh			else
459088b4f5fSWarner Losh				-- erase autoboot msg
460088b4f5fSWarner Losh				screen.setcursor(0, y);
461088b4f5fSWarner Losh				print("                                        "
462088b4f5fSWarner Losh				    .. "                                        ");
463088b4f5fSWarner Losh				screen.defcursor();
464088b4f5fSWarner Losh				return;
465088b4f5fSWarner Losh			end
466088b4f5fSWarner Losh		end
467088b4f5fSWarner Losh
468088b4f5fSWarner Losh		loader.delay(50000);
4691f5696c7SKyle Evans	until time <= 0;
470088b4f5fSWarner Losh	core.boot();
471088b4f5fSWarner Losh
472088b4f5fSWarner Loshend
473088b4f5fSWarner Losh
47424a1bd54SKyle Evansreturn menu;
475