xref: /freebsd/stand/lua/menu.lua (revision 0eac99f76ec31270f902cc2a0ff5ae4b5b606a65)
1088b4f5fSWarner Losh--
24d846d26SWarner Losh-- SPDX-License-Identifier: BSD-2-Clause
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
3076c75816SKyle Evanslocal cli = require("cli")
31aedd6be5SKyle Evanslocal core = require("core")
32aedd6be5SKyle Evanslocal color = require("color")
33aedd6be5SKyle Evanslocal config = require("config")
34aedd6be5SKyle Evanslocal screen = require("screen")
35aedd6be5SKyle Evanslocal drawer = require("drawer")
36088b4f5fSWarner Losh
37aedd6be5SKyle Evanslocal menu = {}
38c8518398SKyle Evans
39beafe961SKyle Evanslocal drawn_menu
4043f7d9d1SKyle Evanslocal return_menu_entry = {
4143f7d9d1SKyle Evans	entry_type = core.MENU_RETURN,
4243f7d9d1SKyle Evans	name = "Back to main menu" .. color.highlight(" [Backspace]"),
4343f7d9d1SKyle Evans}
44ca16d83fSKyle Evans
4504af4229SKyle Evanslocal function OnOff(str, value)
4604af4229SKyle Evans	if value then
478ce1744fSKyle Evans		return str .. color.escapefg(color.GREEN) .. "On" ..
486dd078dfSToomas Soome		    color.resetfg()
49e15abd1fSKyle Evans	else
508ce1744fSKyle Evans		return str .. color.escapefg(color.RED) .. "off" ..
516dd078dfSToomas Soome		    color.resetfg()
52e15abd1fSKyle Evans	end
53e15abd1fSKyle Evansend
54e15abd1fSKyle Evans
559ed6f9efSKyle Evanslocal function bootenvSet(env)
567efc058fSKyle Evans	loader.setenv("vfs.root.mountfrom", env)
577efc058fSKyle Evans	loader.setenv("currdev", env .. ":")
587efc058fSKyle Evans	config.reload()
59d7584aa0SCyrus Rahman	if loader.getenv("kernelname") ~= nil then
60d7584aa0SCyrus Rahman		loader.perform("unload")
61d7584aa0SCyrus Rahman	end
627efc058fSKyle Evansend
637efc058fSKyle Evans
649b17aa27SJessica Clarkelocal function multiUserPrompt()
659b17aa27SJessica Clarke	return loader.getenv("loader_menu_multi_user_prompt") or "Multi user"
669b17aa27SJessica Clarkeend
679b17aa27SJessica Clarke
68b5746545SKyle Evans-- Module exports
698d415029SKyle Evansmenu.handlers = {
708d415029SKyle Evans	-- Menu handlers take the current menu and selected entry as parameters,
718d415029SKyle Evans	-- and should return a boolean indicating whether execution should
728d415029SKyle Evans	-- continue or not. The return value may be omitted if this entry should
738d415029SKyle Evans	-- have no bearing on whether we continue or not, indicating that we
748d415029SKyle Evans	-- should just continue after execution.
75e2df27e3SKyle Evans	[core.MENU_ENTRY] = function(_, entry)
768d415029SKyle Evans		-- run function
77aedd6be5SKyle Evans		entry.func()
788d415029SKyle Evans	end,
79e2df27e3SKyle Evans	[core.MENU_CAROUSEL_ENTRY] = function(_, entry)
808d415029SKyle Evans		-- carousel (rotating) functionality
81aedd6be5SKyle Evans		local carid = entry.carousel_id
82aedd6be5SKyle Evans		local caridx = config.getCarouselIndex(carid)
834f437f9eSKyle Evans		local choices = entry.items
844f437f9eSKyle Evans		if type(choices) == "function" then
854f437f9eSKyle Evans			choices = choices()
864f437f9eSKyle Evans		end
879f71d421SKyle Evans		if #choices > 0 then
88aedd6be5SKyle Evans			caridx = (caridx % #choices) + 1
89aedd6be5SKyle Evans			config.setCarouselIndex(carid, caridx)
90aedd6be5SKyle Evans			entry.func(caridx, choices[caridx], choices)
918d415029SKyle Evans		end
928d415029SKyle Evans	end,
93e2df27e3SKyle Evans	[core.MENU_SUBMENU] = function(_, entry)
94da9ab827SKyle Evans		menu.process(entry.submenu)
958d415029SKyle Evans	end,
96e2df27e3SKyle Evans	[core.MENU_RETURN] = function(_, entry)
978d415029SKyle Evans		-- allow entry to have a function/side effect
989f71d421SKyle Evans		if entry.func ~= nil then
99aedd6be5SKyle Evans			entry.func()
1008d415029SKyle Evans		end
101aedd6be5SKyle Evans		return false
1028d415029SKyle Evans	end,
103aedd6be5SKyle Evans}
104280e990bSKyle Evans-- loader menu tree is rooted at menu.welcome
105088b4f5fSWarner Losh
1067efc058fSKyle Evansmenu.boot_environments = {
1077efc058fSKyle Evans	entries = {
1087efc058fSKyle Evans		-- return to welcome menu
10943f7d9d1SKyle Evans		return_menu_entry,
1107efc058fSKyle Evans		{
1117efc058fSKyle Evans			entry_type = core.MENU_CAROUSEL_ENTRY,
1127efc058fSKyle Evans			carousel_id = "be_active",
1137efc058fSKyle Evans			items = core.bootenvList,
1147efc058fSKyle Evans			name = function(idx, choice, all_choices)
1157efc058fSKyle Evans				if #all_choices == 0 then
1167efc058fSKyle Evans					return "Active: "
1177efc058fSKyle Evans				end
1187efc058fSKyle Evans
1197efc058fSKyle Evans				local is_default = (idx == 1)
1207efc058fSKyle Evans				local bootenv_name = ""
1217efc058fSKyle Evans				local name_color
1227efc058fSKyle Evans				if is_default then
1238ce1744fSKyle Evans					name_color = color.escapefg(color.GREEN)
1247efc058fSKyle Evans				else
1258ce1744fSKyle Evans					name_color = color.escapefg(color.BLUE)
1267efc058fSKyle Evans				end
1277efc058fSKyle Evans				bootenv_name = bootenv_name .. name_color ..
1288ce1744fSKyle Evans				    choice .. color.resetfg()
1297efc058fSKyle Evans				return color.highlight("A").."ctive: " ..
1307efc058fSKyle Evans				    bootenv_name .. " (" .. idx .. " of " ..
1317efc058fSKyle Evans				    #all_choices .. ")"
1327efc058fSKyle Evans			end,
133e2df27e3SKyle Evans			func = function(_, choice, _)
1347efc058fSKyle Evans				bootenvSet(choice)
1357efc058fSKyle Evans			end,
1367efc058fSKyle Evans			alias = {"a", "A"},
1377efc058fSKyle Evans		},
1387efc058fSKyle Evans		{
1397efc058fSKyle Evans			entry_type = core.MENU_ENTRY,
140277f38abSMariusz Zaborski			visible = function()
141277f38abSMariusz Zaborski				return core.isRewinded() == false
142277f38abSMariusz Zaborski			end,
1437efc058fSKyle Evans			name = function()
1447efc058fSKyle Evans				return color.highlight("b") .. "ootfs: " ..
1457efc058fSKyle Evans				    core.bootenvDefault()
1467efc058fSKyle Evans			end,
1477efc058fSKyle Evans			func = function()
1487efc058fSKyle Evans				-- Reset active boot environment to the default
1497efc058fSKyle Evans				config.setCarouselIndex("be_active", 1)
1507efc058fSKyle Evans				bootenvSet(core.bootenvDefault())
1517efc058fSKyle Evans			end,
1527efc058fSKyle Evans			alias = {"b", "B"},
1537efc058fSKyle Evans		},
1547efc058fSKyle Evans	},
1557efc058fSKyle Evans}
1567efc058fSKyle Evans
157088b4f5fSWarner Loshmenu.boot_options = {
158d8757746SKyle Evans	entries = {
159088b4f5fSWarner Losh		-- return to welcome menu
16043f7d9d1SKyle Evans		return_menu_entry,
161088b4f5fSWarner Losh		-- load defaults
162088b4f5fSWarner Losh		{
163a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
164a51f9f0cSKyle Evans			name = "Load System " .. color.highlight("D") ..
165a51f9f0cSKyle Evans			    "efaults",
166a51f9f0cSKyle Evans			func = core.setDefaults,
1673cd5547bSKyle Evans			alias = {"d", "D"},
168088b4f5fSWarner Losh		},
169088b4f5fSWarner Losh		{
170a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
171088b4f5fSWarner Losh		},
172088b4f5fSWarner Losh		{
173a7cf0562SKyle Evans			entry_type = core.MENU_SEPARATOR,
174a51f9f0cSKyle Evans			name = "Boot Options:",
175088b4f5fSWarner Losh		},
176088b4f5fSWarner Losh		-- acpi
177088b4f5fSWarner Losh		{
178a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
179e0f3dc82SR. Christian McDonald			visible = core.hasACPI,
180088b4f5fSWarner Losh			name = function()
181fd2b19b3SKyle Evans				return OnOff(color.highlight("A") ..
182aedd6be5SKyle Evans				    "CPI       :", core.acpi)
183088b4f5fSWarner Losh			end,
184a51f9f0cSKyle Evans			func = core.setACPI,
1853cd5547bSKyle Evans			alias = {"a", "A"},
186088b4f5fSWarner Losh		},
187088b4f5fSWarner Losh		-- safe mode
188088b4f5fSWarner Losh		{
189a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
190088b4f5fSWarner Losh			name = function()
19157099121SKyle Evans				return OnOff("Safe " .. color.highlight("M") ..
192aedd6be5SKyle Evans				    "ode  :", core.sm)
193088b4f5fSWarner Losh			end,
194a51f9f0cSKyle Evans			func = core.setSafeMode,
1953cd5547bSKyle Evans			alias = {"m", "M"},
196088b4f5fSWarner Losh		},
197088b4f5fSWarner Losh		-- single user
198088b4f5fSWarner Losh		{
199a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
200088b4f5fSWarner Losh			name = function()
201fd2b19b3SKyle Evans				return OnOff(color.highlight("S") ..
202aedd6be5SKyle Evans				    "ingle user:", core.su)
203088b4f5fSWarner Losh			end,
204a51f9f0cSKyle Evans			func = core.setSingleUser,
2053cd5547bSKyle Evans			alias = {"s", "S"},
206088b4f5fSWarner Losh		},
207088b4f5fSWarner Losh		-- verbose boot
208088b4f5fSWarner Losh		{
209a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
210088b4f5fSWarner Losh			name = function()
211fd2b19b3SKyle Evans				return OnOff(color.highlight("V") ..
212aedd6be5SKyle Evans				    "erbose    :", core.verbose)
213088b4f5fSWarner Losh			end,
214a51f9f0cSKyle Evans			func = core.setVerbose,
2153cd5547bSKyle Evans			alias = {"v", "V"},
216088b4f5fSWarner Losh		},
217d8757746SKyle Evans	},
218aedd6be5SKyle Evans}
219088b4f5fSWarner Losh
220088b4f5fSWarner Loshmenu.welcome = {
221303253e5SKyle Evans	entries = function()
222aedd6be5SKyle Evans		local menu_entries = menu.welcome.all_entries
223d2187b39SRyan Moeller		local multi_user = menu_entries.multi_user
224d2187b39SRyan Moeller		local single_user = menu_entries.single_user
225d2187b39SRyan Moeller		local boot_entry_1, boot_entry_2
2269f71d421SKyle Evans		if core.isSingleUserBoot() then
227d2187b39SRyan Moeller			-- Swap the first two menu items on single user boot.
228d2187b39SRyan Moeller			-- We'll cache the alternate entries for performance.
229d2187b39SRyan Moeller			local alts = menu_entries.alts
230d2187b39SRyan Moeller			if alts == nil then
231d2187b39SRyan Moeller				single_user = core.deepCopyTable(single_user)
232d2187b39SRyan Moeller				multi_user = core.deepCopyTable(multi_user)
233d2187b39SRyan Moeller				single_user.name = single_user.alternate_name
234d2187b39SRyan Moeller				multi_user.name = multi_user.alternate_name
235d2187b39SRyan Moeller				menu_entries.alts = {
236d2187b39SRyan Moeller					single_user = single_user,
237d2187b39SRyan Moeller					multi_user = multi_user,
238d2187b39SRyan Moeller				}
239d2187b39SRyan Moeller			else
240d2187b39SRyan Moeller				single_user = alts.single_user
241d2187b39SRyan Moeller				multi_user = alts.multi_user
2429a0904b0SKyle Evans			end
243d2187b39SRyan Moeller			boot_entry_1, boot_entry_2 = single_user, multi_user
244d2187b39SRyan Moeller		else
245d2187b39SRyan Moeller			boot_entry_1, boot_entry_2 = multi_user, single_user
246303253e5SKyle Evans		end
247d2187b39SRyan Moeller		return {
248*0eac99f7SWarner Losh			loader_needs_upgrade,
249d2187b39SRyan Moeller			boot_entry_1,
250d2187b39SRyan Moeller			boot_entry_2,
251d2187b39SRyan Moeller			menu_entries.prompt,
252d2187b39SRyan Moeller			menu_entries.reboot,
2538f3b3610SWarner Losh			menu_entries.console,
254d2187b39SRyan Moeller			{
255d2187b39SRyan Moeller				entry_type = core.MENU_SEPARATOR,
256d2187b39SRyan Moeller			},
257d2187b39SRyan Moeller			{
258d2187b39SRyan Moeller				entry_type = core.MENU_SEPARATOR,
259d2187b39SRyan Moeller				name = "Options:",
260d2187b39SRyan Moeller			},
261d2187b39SRyan Moeller			menu_entries.kernel_options,
262d2187b39SRyan Moeller			menu_entries.boot_options,
263277f38abSMariusz Zaborski			menu_entries.zpool_checkpoints,
264d2187b39SRyan Moeller			menu_entries.boot_envs,
265d2187b39SRyan Moeller			menu_entries.chainload,
266e7ccd5b4SWarner Losh			menu_entries.vendor,
267d2187b39SRyan Moeller		}
268303253e5SKyle Evans	end,
269303253e5SKyle Evans	all_entries = {
270d2187b39SRyan Moeller		multi_user = {
271a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
2729b17aa27SJessica Clarke			name = function()
2739b17aa27SJessica Clarke				return color.highlight("B") .. "oot " ..
2749b17aa27SJessica Clarke				    multiUserPrompt() .. " " ..
2759b17aa27SJessica Clarke				    color.highlight("[Enter]")
2769b17aa27SJessica Clarke			end,
2775c1b5165SKyle Evans			-- Not a standard menu entry function!
2789b17aa27SJessica Clarke			alternate_name = function()
2799b17aa27SJessica Clarke				return color.highlight("B") .. "oot " ..
2809b17aa27SJessica Clarke				    multiUserPrompt()
2819b17aa27SJessica Clarke			end,
282088b4f5fSWarner Losh			func = function()
283aedd6be5SKyle Evans				core.setSingleUser(false)
284aedd6be5SKyle Evans				core.boot()
285088b4f5fSWarner Losh			end,
2863cd5547bSKyle Evans			alias = {"b", "B"},
287088b4f5fSWarner Losh		},
288d2187b39SRyan Moeller		single_user = {
289a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
290a51f9f0cSKyle Evans			name = "Boot " .. color.highlight("S") .. "ingle user",
2915c1b5165SKyle Evans			-- Not a standard menu entry function!
292a51f9f0cSKyle Evans			alternate_name = "Boot " .. color.highlight("S") ..
293a51f9f0cSKyle Evans			    "ingle user " .. color.highlight("[Enter]"),
294088b4f5fSWarner Losh			func = function()
295aedd6be5SKyle Evans				core.setSingleUser(true)
296aedd6be5SKyle Evans				core.boot()
297088b4f5fSWarner Losh			end,
2983cd5547bSKyle Evans			alias = {"s", "S"},
299088b4f5fSWarner Losh		},
3008f3b3610SWarner Losh		console = {
3018f3b3610SWarner Losh			entry_type = core.MENU_ENTRY,
3028f3b3610SWarner Losh			name = function()
3038f3b3610SWarner Losh				return color.highlight("C") .. "ons: " .. core.getConsoleName()
3048f3b3610SWarner Losh			end,
3058f3b3610SWarner Losh			func = function()
3068f3b3610SWarner Losh				core.nextConsoleChoice()
3078f3b3610SWarner Losh			end,
3088f3b3610SWarner Losh			alias = {"c", "C"},
3098f3b3610SWarner Losh		},
310d2187b39SRyan Moeller		prompt = {
311a7cf0562SKyle Evans			entry_type = core.MENU_RETURN,
312a51f9f0cSKyle Evans			name = color.highlight("Esc") .. "ape to loader prompt",
313ef625845SKyle Evans			func = function()
314aedd6be5SKyle Evans				loader.setenv("autoboot_delay", "NO")
315ef625845SKyle Evans			end,
3163cd5547bSKyle Evans			alias = {core.KEYSTR_ESCAPE},
317088b4f5fSWarner Losh		},
318d2187b39SRyan Moeller		reboot = {
319a7cf0562SKyle Evans			entry_type = core.MENU_ENTRY,
320a51f9f0cSKyle Evans			name = color.highlight("R") .. "eboot",
321088b4f5fSWarner Losh			func = function()
322aedd6be5SKyle Evans				loader.perform("reboot")
323088b4f5fSWarner Losh			end,
3243cd5547bSKyle Evans			alias = {"r", "R"},
325088b4f5fSWarner Losh		},
326d2187b39SRyan Moeller		kernel_options = {
327a7cf0562SKyle Evans			entry_type = core.MENU_CAROUSEL_ENTRY,
328ada26c4aSKyle Evans			carousel_id = "kernel",
329ada26c4aSKyle Evans			items = core.kernelList,
330ada26c4aSKyle Evans			name = function(idx, choice, all_choices)
3319f71d421SKyle Evans				if #all_choices == 0 then
332aedd6be5SKyle Evans					return "Kernel: "
333088b4f5fSWarner Losh				end
334b1b1f2b8SKyle Evans
335aedd6be5SKyle Evans				local is_default = (idx == 1)
336aedd6be5SKyle Evans				local kernel_name = ""
337aedd6be5SKyle Evans				local name_color
3389f71d421SKyle Evans				if is_default then
3398ce1744fSKyle Evans					name_color = color.escapefg(color.GREEN)
340aedd6be5SKyle Evans					kernel_name = "default/"
341bcf48a15SKyle Evans				else
3428ce1744fSKyle Evans					name_color = color.escapefg(color.BLUE)
343b1b1f2b8SKyle Evans				end
344fd2b19b3SKyle Evans				kernel_name = kernel_name .. name_color ..
3458ce1744fSKyle Evans				    choice .. color.resetfg()
346fd2b19b3SKyle Evans				return color.highlight("K") .. "ernel: " ..
347fd2b19b3SKyle Evans				    kernel_name .. " (" .. idx .. " of " ..
348aedd6be5SKyle Evans				    #all_choices .. ")"
349088b4f5fSWarner Losh			end,
350e2df27e3SKyle Evans			func = function(_, choice, _)
351e414851fSKyle Evans				if loader.getenv("kernelname") ~= nil then
352e414851fSKyle Evans					loader.perform("unload")
353e414851fSKyle Evans				end
354322a2dddSKyle Evans				config.selectKernel(choice)
355088b4f5fSWarner Losh			end,
3563cd5547bSKyle Evans			alias = {"k", "K"},
357088b4f5fSWarner Losh		},
358d2187b39SRyan Moeller		boot_options = {
359a7cf0562SKyle Evans			entry_type = core.MENU_SUBMENU,
360a51f9f0cSKyle Evans			name = "Boot " .. color.highlight("O") .. "ptions",
3619a28f948SKyle Evans			submenu = menu.boot_options,
3623cd5547bSKyle Evans			alias = {"o", "O"},
363d8757746SKyle Evans		},
364277f38abSMariusz Zaborski		zpool_checkpoints = {
365277f38abSMariusz Zaborski			entry_type = core.MENU_ENTRY,
366277f38abSMariusz Zaborski			name = function()
36794510c29SKyle Evans				local rewind = "No"
368277f38abSMariusz Zaborski				if core.isRewinded() then
369277f38abSMariusz Zaborski					rewind = "Yes"
370277f38abSMariusz Zaborski				end
371277f38abSMariusz Zaborski				return "Rewind ZFS " .. color.highlight("C") ..
372277f38abSMariusz Zaborski					"heckpoint: " .. rewind
373277f38abSMariusz Zaborski			end,
374277f38abSMariusz Zaborski			func = function()
375277f38abSMariusz Zaborski				core.changeRewindCheckpoint()
376277f38abSMariusz Zaborski				if core.isRewinded() then
377277f38abSMariusz Zaborski					bootenvSet(
378277f38abSMariusz Zaborski					    core.bootenvDefaultRewinded())
379277f38abSMariusz Zaborski				else
380277f38abSMariusz Zaborski					bootenvSet(core.bootenvDefault())
381277f38abSMariusz Zaborski				end
382277f38abSMariusz Zaborski				config.setCarouselIndex("be_active", 1)
383277f38abSMariusz Zaborski			end,
384277f38abSMariusz Zaborski			visible = function()
385277f38abSMariusz Zaborski				return core.isZFSBoot() and
386277f38abSMariusz Zaborski				    core.isCheckpointed()
387277f38abSMariusz Zaborski			end,
388277f38abSMariusz Zaborski			alias = {"c", "C"},
389277f38abSMariusz Zaborski		},
390d2187b39SRyan Moeller		boot_envs = {
3917efc058fSKyle Evans			entry_type = core.MENU_SUBMENU,
3927efc058fSKyle Evans			visible = function()
3937efc058fSKyle Evans				return core.isZFSBoot() and
3947efc058fSKyle Evans				    #core.bootenvList() > 1
3957efc058fSKyle Evans			end,
3967efc058fSKyle Evans			name = "Boot " .. color.highlight("E") .. "nvironments",
3977efc058fSKyle Evans			submenu = menu.boot_environments,
3987efc058fSKyle Evans			alias = {"e", "E"},
3997efc058fSKyle Evans		},
400d2187b39SRyan Moeller		chainload = {
401bdf12807SKyle Evans			entry_type = core.MENU_ENTRY,
402bdf12807SKyle Evans			name = function()
403bdf12807SKyle Evans				return 'Chain' .. color.highlight("L") ..
404bdf12807SKyle Evans				    "oad " .. loader.getenv('chain_disk')
405bdf12807SKyle Evans			end,
406bdf12807SKyle Evans			func = function()
407bdf12807SKyle Evans				loader.perform("chain " ..
408bdf12807SKyle Evans				    loader.getenv('chain_disk'))
409bdf12807SKyle Evans			end,
410bdf12807SKyle Evans			visible = function()
411bdf12807SKyle Evans				return loader.getenv('chain_disk') ~= nil
412bdf12807SKyle Evans			end,
413bdf12807SKyle Evans			alias = {"l", "L"},
414bdf12807SKyle Evans		},
415*0eac99f7SWarner Losh		loader_needs_upgrade = {
416*0eac99f7SWarner Losh			entry_type = core.MENU_SEPARATOR,
417*0eac99f7SWarner Losh			name = function()
418*0eac99f7SWarner Losh				return "Loader requires updating"
419*0eac99f7SWarner Losh			end
420*0eac99f7SWarner Losh			visible = function()
421*0eac99f7SWarner Losh				return core.loaderTooOld()
422*0eac99f7SWarner Losh			end
423*0eac99f7SWarner Losh		},
424e7ccd5b4SWarner Losh		vendor = {
425e7ccd5b4SWarner Losh			entry_type = core.MENU_ENTRY,
426556e66b7SWarner Losh			visible = function()
427556e66b7SWarner Losh				return false
428556e66b7SWarner Losh			end
429e7ccd5b4SWarner Losh		},
430d8757746SKyle Evans	},
431aedd6be5SKyle Evans}
432088b4f5fSWarner Losh
43320a81676SKyle Evansmenu.default = menu.welcome
43428384160SKyle Evans-- current_alias_table will be used to keep our alias table consistent across
43528384160SKyle Evans-- screen redraws, instead of relying on whatever triggered the redraw to update
43628384160SKyle Evans-- the local alias_table in menu.process.
43728384160SKyle Evansmenu.current_alias_table = {}
43820a81676SKyle Evans
4392a11b810SKyle Evansfunction menu.draw(menudef)
440beafe961SKyle Evans	-- Clear the screen, reset the cursor, then draw
441aedd6be5SKyle Evans	screen.clear()
4422a11b810SKyle Evans	menu.current_alias_table = drawer.drawscreen(menudef)
4432a11b810SKyle Evans	drawn_menu = menudef
444decacd91SKyle Evans	screen.defcursor()
44528384160SKyle Evansend
44628384160SKyle Evans
447ca16d83fSKyle Evans-- 'keypress' allows the caller to indicate that a key has been pressed that we
448ca16d83fSKyle Evans-- should process as our initial input.
4492a11b810SKyle Evansfunction menu.process(menudef, keypress)
4502a11b810SKyle Evans	assert(menudef ~= nil)
45128384160SKyle Evans
4522a11b810SKyle Evans	if drawn_menu ~= menudef then
4532a11b810SKyle Evans		menu.draw(menudef)
4547dcffa90SKyle Evans	end
455ca16d83fSKyle Evans
456da9ab827SKyle Evans	while true do
457ca16d83fSKyle Evans		local key = keypress or io.getchar()
458ca16d83fSKyle Evans		keypress = nil
459088b4f5fSWarner Losh
460b458bf0dSKyle Evans		-- Special key behaviors
4619f71d421SKyle Evans		if (key == core.KEY_BACKSPACE or key == core.KEY_DELETE) and
4622a11b810SKyle Evans		    menudef ~= menu.default then
463aedd6be5SKyle Evans			break
4649f71d421SKyle Evans		elseif key == core.KEY_ENTER then
465aedd6be5SKyle Evans			core.boot()
466041929aaSKyle Evans			-- Should not return.  If it does, escape menu handling
467041929aaSKyle Evans			-- and drop to loader prompt.
468041929aaSKyle Evans			return false
469abc4f7e7SKyle Evans		end
470abc4f7e7SKyle Evans
471abc4f7e7SKyle Evans		key = string.char(key)
472088b4f5fSWarner Losh		-- check to see if key is an alias
473aedd6be5SKyle Evans		local sel_entry = nil
47428384160SKyle Evans		for k, v in pairs(menu.current_alias_table) do
4759f71d421SKyle Evans			if key == k then
476aedd6be5SKyle Evans				sel_entry = v
47728384160SKyle Evans				break
478088b4f5fSWarner Losh			end
479088b4f5fSWarner Losh		end
480088b4f5fSWarner Losh
481088b4f5fSWarner Losh		-- if we have an alias do the assigned action:
4829f71d421SKyle Evans		if sel_entry ~= nil then
483aedd6be5SKyle Evans			local handler = menu.handlers[sel_entry.entry_type]
4842a11b810SKyle Evans			assert(handler ~= nil)
485da9ab827SKyle Evans			-- The handler's return value indicates if we
486da9ab827SKyle Evans			-- need to exit this menu.  An omitted or true
487da9ab827SKyle Evans			-- return value means to continue.
4882a11b810SKyle Evans			if handler(menudef, sel_entry) == false then
489da9ab827SKyle Evans				return
490aefcaa7eSKyle Evans			end
49128384160SKyle Evans			-- If we got an alias key the screen is out of date...
49228384160SKyle Evans			-- redraw it.
4932a11b810SKyle Evans			menu.draw(menudef)
494088b4f5fSWarner Losh		end
495088b4f5fSWarner Losh	end
496088b4f5fSWarner Loshend
497088b4f5fSWarner Losh
498da9ab827SKyle Evansfunction menu.run()
499041929aaSKyle Evans	local autoboot_key
500c84dbc53SKyle Evans	local delay = loader.getenv("autoboot_delay")
501c84dbc53SKyle Evans
502c84dbc53SKyle Evans	if delay ~= nil and delay:lower() == "no" then
503c84dbc53SKyle Evans		delay = nil
504c84dbc53SKyle Evans	else
505c84dbc53SKyle Evans		delay = tonumber(delay) or 10
506c84dbc53SKyle Evans	end
507c84dbc53SKyle Evans
508c84dbc53SKyle Evans	if delay == -1 then
509c84dbc53SKyle Evans		core.boot()
510c84dbc53SKyle Evans		return
511c84dbc53SKyle Evans	end
512c84dbc53SKyle Evans
513beafe961SKyle Evans	menu.draw(menu.default)
514c84dbc53SKyle Evans
515041929aaSKyle Evans	if delay ~= nil then
516041929aaSKyle Evans		autoboot_key = menu.autoboot(delay)
517041929aaSKyle Evans
518041929aaSKyle Evans		-- autoboot_key should return the key pressed.  It will only
519041929aaSKyle Evans		-- return nil if we hit the timeout and executed the timeout
520041929aaSKyle Evans		-- command.  Bail out.
521041929aaSKyle Evans		if autoboot_key == nil then
522041929aaSKyle Evans			return
523041929aaSKyle Evans		end
524041929aaSKyle Evans	end
525ca16d83fSKyle Evans
526ca16d83fSKyle Evans	menu.process(menu.default, autoboot_key)
527beafe961SKyle Evans	drawn_menu = nil
528da9ab827SKyle Evans
529da9ab827SKyle Evans	screen.defcursor()
530da9ab827SKyle Evans	print("Exiting menu!")
531088b4f5fSWarner Loshend
532088b4f5fSWarner Losh
533c84dbc53SKyle Evansfunction menu.autoboot(delay)
5341495c98fSKyle Evans	local x = loader.getenv("loader_menu_timeout_x") or 4
5351495c98fSKyle Evans	local y = loader.getenv("loader_menu_timeout_y") or 23
536c84dbc53SKyle Evans	local endtime = loader.time() + delay
537aedd6be5SKyle Evans	local time
5383244729fSKyle Evans	local last
539088b4f5fSWarner Losh	repeat
540aedd6be5SKyle Evans		time = endtime - loader.time()
5413244729fSKyle Evans		if last == nil or last ~= time then
5423244729fSKyle Evans			last = time
543aedd6be5SKyle Evans			screen.setcursor(x, y)
54457099121SKyle Evans			print("Autoboot in " .. time ..
545ed107537SKyle Evans			    " seconds. [Space] to pause ")
546aedd6be5SKyle Evans			screen.defcursor()
5473244729fSKyle Evans		end
5489f71d421SKyle Evans		if io.ischar() then
549aedd6be5SKyle Evans			local ch = io.getchar()
5509f71d421SKyle Evans			if ch == core.KEY_ENTER then
551aedd6be5SKyle Evans				break
552088b4f5fSWarner Losh			else
553088b4f5fSWarner Losh				-- erase autoboot msg
554aedd6be5SKyle Evans				screen.setcursor(0, y)
55566964bbcSKyle Evans				print(string.rep(" ", 80))
556aedd6be5SKyle Evans				screen.defcursor()
55712b95c84SKyle Evans				return ch
558088b4f5fSWarner Losh			end
559088b4f5fSWarner Losh		end
560088b4f5fSWarner Losh
561aedd6be5SKyle Evans		loader.delay(50000)
562aedd6be5SKyle Evans	until time <= 0
563088b4f5fSWarner Losh
564a76f8a5bSKyle Evans	local cmd = loader.getenv("menu_timeout_command") or "boot"
565e9c3ceb1SKyle Evans	cli_execute_unparsed(cmd)
566041929aaSKyle Evans	return nil
567088b4f5fSWarner Loshend
568088b4f5fSWarner Losh
56976c75816SKyle Evans-- CLI commands
5708f7f3d08SKyle Evansfunction cli.menu()
57176c75816SKyle Evans	menu.run()
57276c75816SKyle Evansend
57376c75816SKyle Evans
574aedd6be5SKyle Evansreturn menu
575