]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/lua/core.lua
lualoader: Intercept the 'autoboot' cli command
[FreeBSD/FreeBSD.git] / stand / lua / core.lua
1 --
2 -- Copyright (c) 2015 Pedro Souza <pedrosouza@freebsd.org>
3 -- All rights reserved.
4 --
5 -- Redistribution and use in source and binary forms, with or without
6 -- modification, are permitted provided that the following conditions
7 -- are met:
8 -- 1. Redistributions of source code must retain the above copyright
9 --    notice, this list of conditions and the following disclaimer.
10 -- 2. Redistributions in binary form must reproduce the above copyright
11 --    notice, this list of conditions and the following disclaimer in the
12 --    documentation and/or other materials provided with the distribution.
13 --
14 -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 -- ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 -- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 -- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 -- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 -- SUCH DAMAGE.
25 --
26 -- $FreeBSD$
27 --
28
29 local config = require('config');
30
31 local core = {};
32
33 local compose_loader_cmd = function(cmd_name, argstr)
34         if (argstr ~= nil) then
35                 cmd_name = cmd_name .. " " .. argstr;
36         end
37         return cmd_name;
38 end
39
40 -- Internal function
41 -- Parses arguments to boot and returns two values: kernel_name, argstr
42 -- Defaults to nil and "" respectively.
43 -- This will also parse arguments to autoboot, but the with_kernel argument
44 -- will need to be explicitly overwritten to false
45 local parse_boot_args = function(argv, with_kernel)
46         if (#argv == 0) then
47                 return nil, "";
48         end
49         if (with_kernel == nil) then
50                 with_kernel = true;
51         end
52         local kernel_name;
53         local argstr = "";
54
55         for k, v in ipairs(argv) do
56                 if (with_kernel) and (v:sub(1,1) ~= "-") then
57                         kernel_name = v;
58                 else
59                         argstr = argstr .. " " .. v;
60                 end
61         end
62         if (with_kernel) then
63                 return kernel_name, argstr;
64         else
65                 return argstr;
66         end
67 end
68
69 -- Globals
70 function boot(...)
71         local argv = {...};
72         local cmd_name = "";
73         cmd_name, argv = core.popFrontTable(argv);
74         local kernel, argstr = parse_boot_args(argv);
75         if (kernel ~= nil) then
76                 loader.perform("unload");
77                 config.selectkernel(kernel);
78         end
79         core.boot(argstr);
80 end
81
82 function autoboot(...)
83         local argv = {...}
84         local cmd_name = "";
85         cmd_name, argv = core.popFrontTable(argv);
86         local argstr = parse_boot_args(argv, false);
87         core.autoboot(argstr);
88 end
89
90 -- Module exports
91 -- Commonly appearing constants
92 core.KEY_BACKSPACE      = 8;
93 core.KEY_ENTER          = 13;
94 core.KEY_DELETE         = 127;
95
96 core.KEYSTR_ESCAPE      = "\027";
97
98 core.MENU_RETURN        = "return";
99 core.MENU_ENTRY         = "entry";
100 core.MENU_SEPARATOR     = "separator";
101 core.MENU_SUBMENU       = "submenu";
102 core.MENU_CAROUSEL_ENTRY        = "carousel_entry";
103
104 function core.setVerbose(b)
105         if (b == nil) then
106                 b = not core.verbose;
107         end
108
109         if (b == true) then
110                 loader.setenv("boot_verbose", "YES");
111         else
112                 loader.unsetenv("boot_verbose");
113         end
114         core.verbose = b;
115 end
116
117 function core.setSingleUser(b)
118         if (b == nil) then
119                 b = not core.su;
120         end
121
122         if (b == true) then
123                 loader.setenv("boot_single", "YES");
124         else
125                 loader.unsetenv("boot_single");
126         end
127         core.su = b;
128 end
129
130 function core.getACPIPresent(checkingSystemDefaults)
131         local c = loader.getenv("hint.acpi.0.rsdp");
132
133         if (c ~= nil) then
134                 if (checkingSystemDefaults == true) then
135                         return true;
136                 end
137                 -- Otherwise, respect disabled if it's set
138                 c = loader.getenv("hint.acpi.0.disabled");
139                 return (c == nil) or (tonumber(c) ~= 1);
140         end
141         return false;
142 end
143
144 function core.setACPI(b)
145         if (b == nil) then
146                 b = not core.acpi;
147         end
148
149         if (b == true) then
150                 loader.setenv("acpi_load", "YES");
151                 loader.setenv("hint.acpi.0.disabled", "0");
152                 loader.unsetenv("loader.acpi_disabled_by_user");
153         else
154                 loader.unsetenv("acpi_load");
155                 loader.setenv("hint.acpi.0.disabled", "1");
156                 loader.setenv("loader.acpi_disabled_by_user", "1");
157         end
158         core.acpi = b;
159 end
160
161 function core.setSafeMode(b)
162         if (b == nil) then
163                 b = not core.sm;
164         end
165         if (b == true) then
166                 loader.setenv("kern.smp.disabled", "1");
167                 loader.setenv("hw.ata.ata_dma", "0");
168                 loader.setenv("hw.ata.atapi_dma", "0");
169                 loader.setenv("hw.ata.wc", "0");
170                 loader.setenv("hw.eisa_slots", "0");
171                 loader.setenv("kern.eventtimer.periodic", "1");
172                 loader.setenv("kern.geom.part.check_integrity", "0");
173         else
174                 loader.unsetenv("kern.smp.disabled");
175                 loader.unsetenv("hw.ata.ata_dma");
176                 loader.unsetenv("hw.ata.atapi_dma");
177                 loader.unsetenv("hw.ata.wc");
178                 loader.unsetenv("hw.eisa_slots");
179                 loader.unsetenv("kern.eventtimer.periodic");
180                 loader.unsetenv("kern.geom.part.check_integrity");
181         end
182         core.sm = b;
183 end
184
185 function core.kernelList()
186         local k = loader.getenv("kernel");
187         local v = loader.getenv("kernels") or "";
188
189         local kernels = {};
190         local unique = {};
191         local i = 0;
192         if (k ~= nil) then
193                 i = i + 1;
194                 kernels[i] = k;
195                 unique[k] = true;
196         end
197
198         for n in v:gmatch("([^; ]+)[; ]?") do
199                 if (unique[n] == nil) then
200                         i = i + 1;
201                         kernels[i] = n;
202                         unique[n] = true;
203                 end
204         end
205
206         -- Automatically detect other bootable kernel directories using a
207         -- heuristic.  Any directory in /boot that contains an ordinary file
208         -- named "kernel" is considered eligible.
209         for file in lfs.dir("/boot") do
210                 local fname = "/boot/" .. file;
211
212                 if (file == "." or file == "..") then
213                         goto continue;
214                 end
215
216                 if (lfs.attributes(fname, "mode") ~= "directory") then
217                         goto continue;
218                 end
219
220                 if (lfs.attributes(fname .. "/kernel", "mode") ~= "file") then
221                         goto continue;
222                 end
223
224                 if (unique[file] == nil) then
225                         i = i + 1;
226                         kernels[i] = file;
227                         unique[file] = true;
228                 end
229
230                 ::continue::
231         end
232         return kernels;
233 end
234
235 function core.setDefaults()
236         core.setACPI(core.getACPIPresent(true));
237         core.setSafeMode(false);
238         core.setSingleUser(false);
239         core.setVerbose(false);
240 end
241
242 function core.autoboot(argstr)
243         config.loadelf();
244         loader.perform(compose_loader_cmd("autoboot", argstr));
245 end
246
247 function core.boot(argstr)
248         config.loadelf();
249         loader.perform(compose_loader_cmd("boot", argstr));
250 end
251
252 function core.isSingleUserBoot()
253         local single_user = loader.getenv("boot_single");
254         return single_user ~= nil and single_user:lower() == "yes";
255 end
256
257 function core.isSerialBoot()
258         local c = loader.getenv("console");
259
260         if (c ~= nil) then
261                 if (c:find("comconsole") ~= nil) then
262                         return true;
263                 end
264         end
265
266         local s = loader.getenv("boot_serial");
267         if (s ~= nil) then
268                 return true;
269         end
270
271         local m = loader.getenv("boot_multicons");
272         if (m ~= nil) then
273                 return true;
274         end
275         return false;
276 end
277
278 function core.isSystem386()
279         return (loader.machine_arch == "i386");
280 end
281
282 -- This may be a better candidate for a 'utility' module.
283 function core.shallowCopyTable(tbl)
284         local new_tbl = {};
285         for k, v in pairs(tbl) do
286                 if (type(v) == "table") then
287                         new_tbl[k] = core.shallowCopyTable(v);
288                 else
289                         new_tbl[k] = v;
290                 end
291         end
292         return new_tbl;
293 end
294
295 -- XXX This should go away if we get the table lib into shape for importing.
296 -- As of now, it requires some 'os' functions, so we'll implement this in lua
297 -- for our uses
298 function core.popFrontTable(tbl)
299         -- Shouldn't reasonably happen
300         if (#tbl == 0) then
301                 return nil, nil;
302         elseif (#tbl == 1) then
303                 return tbl[1], {};
304         end
305
306         local first_value = tbl[1];
307         local new_tbl = {};
308         -- This is not a cheap operation
309         for k, v in ipairs(tbl) do
310                 if (k > 1) then
311                         new_tbl[k - 1] = v;
312                 end
313         end
314
315         return first_value, new_tbl;
316 end
317
318 -- On i386, hint.acpi.0.rsdp will be set before we're loaded. On !i386, it will
319 -- generally be set upon execution of the kernel. Because of this, we can't (or
320 -- don't really want to) detect/disable ACPI on !i386 reliably. Just set it
321 -- enabled if we detect it and leave well enough alone if we don't.
322 if (core.isSystem386()) and (core.getACPIPresent(false)) then
323         core.setACPI(true);
324 end
325 return core;