-
Notifications
You must be signed in to change notification settings - Fork 2
/
bios.lua
355 lines (339 loc) · 8.55 KB
/
bios.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
--Outdated version! Please get the latest version from the dedicated repo!
--BLOCK MESA BIOS
--Used for all Block Mesa bootable computers
--inject code stolen from https://pastebin.com/yzfDMjwf
local oldPull = os.pullEvent
local oldRequire = _ENV.require
local oldSettingsGet = settings.get
local oldSettingsSet = settings.set
local fsOpen = fs.open
local oldDebug = _G.debug
_G.os.pullEvent = os.pullEventRaw
_G.os.pullEventOld = oldPull
--internal flag things
local version = "1.30"
local isDiskBooted = false
local baseDirectory = ""
local directory = "/"
local driveLetter = "C"
local whiteColor = 0x00FF00
local blackColor = 0x000000
local function setColors()
term.redirect(term.native())
term.setPaletteColour(colors.white, whiteColor)
term.setPaletteColour(colors.red, whiteColor)
term.setPaletteColour(colors.yellow, whiteColor)
term.setPaletteColour(colors.orange, whiteColor)
term.setPaletteColour(colors.lime, whiteColor)
term.setPaletteColour(colors.green, whiteColor)
term.setPaletteColour(colors.blue, whiteColor)
term.setPaletteColour(colors.cyan, whiteColor)
term.setPaletteColour(colors.black, blackColor)
term.setPaletteColour(colors.gray, blackColor)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
end
local function resolvePath(path)
local matches = {}
for i in path:gmatch("[^/]+") do
table.insert(matches,i)
end
local result1 = {}
local lastIndex = 1
for i,v in pairs(matches) do
if v ~= "." then
if v== ".." then
result1[lastIndex] = nil
lastIndex = lastIndex-1
else
lastIndex = lastIndex + 1
result1[lastIndex] = v
end
end
end
local result = {}
for i,v in pairs(result1) do
table.insert(result,v)
end
local final = "/"
for i,v in pairs(result) do
if i ~= 1 then
final = final .. "/"
end
final = final..v
end
return final
end
local function setupTerm()
setColors()
term.setCursorBlink(false)
term.clear()
term.setCursorPos(1,1)
print("BLOCK MESA BIOS v"..version)
end
setupTerm()
local function enterSetup()
--For a future update
end
if not oldSettingsGet("bm-bios.firstBoot") then
oldSettingsSet("bios.use_multishell",false)
oldSettingsSet("shell.allow_disk_startup",false)
oldSettingsSet("bm-bios.firstBoot",true)
settings.save()
print("Rebooting...")
os.reboot()
end
local bootIntoSetup = settings.get("bm-bios.bootToSetup")
if bootIntoSetup == nil or bootIntoSetup then
enterSetup()
end
local notAllowed = {
["/startup.lua"] = true,
["/startup"] = true,
["/startup.lua/"] = true,
["/startup/"] = true,
["/.settings"] = true,
["/.settings/"] = true,
}
local biosUrl = "https://raw.githubusercontent.com/BlockMesa/BM-BIOS/main/"
local updateUrl = "https://raw.githubusercontent.com/BlockMesa/BM-DOS/main/"
local bios = {
getBootedDrive = function()
return baseDirectory
end,
isDiskBooted = function()
return isDiskBooted
end,
getDir = function()
return directory
end,
setDir = function(dir)
--shell.setDir(dir)
directory = dir
end,
getDrive = function()
return driveLetter
end,
setDrive = function(a)
driveLetter = a
end,
updateFile = function(file,url)
if oldSettingsGet("bm-bios.secureboot") and (string.sub(url,1,56) ~= updateUrl and string.sub(url,1,57) ~= biosUrl) then
print("Trust check failed")
return
end
local result, reason = http.get({url = url, binary = true}) --make names better
if not result then
print(("Failed to update %s from %s (%s)"):format(file, url, reason)) --include more detail
return
end
a1 = fsOpen(file,"wb")
a1.write(result.readAll())
a1.close()
result.close()
end,
protect = function(path)
notAllowed[path] = true
return
end,
require = oldRequire,
fixColorScheme = setColors,
resolvePath = resolvePath,
settingsGet = oldSettingsGet,
settingsSet = oldSettingsSet,
}
local function boot(prefix)
print("Booting from drive "..driveLetter)
baseDirectory = prefix
directory = prefix
local success, response = pcall(os.run,{bios=bios},prefix..".BOOT")
if not success then
print(response)
while true do os.sleep() end
end
end
local function findBootableDevice()
if fs.exists("disk") and fs.exists("/disk/.BOOT") then
bios.setDrive("A")
isDiskBooted = true
boot("/disk/")
elseif fs.exists("/.BOOT") then
bios.setDrive("C")
boot("/")
else
print("NO BOOT DEVICE FOUND!")
while true do os.sleep() end
end
end
local blankFunction = function(...)
return
end
local function overides()
--misc overides
_G.debug = {
debug = blankFunction,
gethook = blankFunction,
getinfo = blankFunction,
getlocal = blankFunction,
getmetatable = blankFunction,
getregistry = blankFunction,
getupvalue = blankFunction,
getuservalue = blankFunction,
sethook = blankFunction,
setmetatable = blankFunction,
setupvalue = blankFunction,
setuservalue = blankFunction,
traceback = blankFunction,
upvalueid = blankFunction,
upvaluejoin = blankFunction
}
_G.settings.set = function(key,newKey)
key = string.lower(key)
newKey = string.lower(newKey)
if key == "bm-bios.passphrase" or key == "bm-bios.secureboot" then
error("Permissions error!")
else
return oldSettingsSet(key,newKey)
end
end
_G.settings.get = function(key)
key = string.lower(key)
if key == "bm-bios.passphrase" or key == "bm-bios.secureboot" then
error("Permissions error!")
else
return oldSettingsGet(key)
end
end
_G.settings.save = function()
local t = {}
for i,v in pairs(settings.getNames()) do
t[v] = oldSettingsGet(v)
end
local new = textutils.serialise(t)
local a = fsOpen("/.settings","w")
a.write(new)
a.close()
return true
end
local oldFs = {}
local fakeFs ={}
local oldIo = {}
local fakeIo = {}
--IO library
oldIo.open = io.open
function fakeIo.open(oldPath,a)
local path = resolvePath(oldPath)
if notAllowed[string.lower(path)] then
return nil
end
return oldIo.open(path,a)
end
_G.io.open = fakeIo.open
oldIo.output = io.output
function fakeIo.output(oldPath)
if type(oldPath) == "string" then
local path = resolvePath(oldPath)
if notAllowed[string.lower(path)] then
return nil
end
return oldIo.output(path)
end
end
_G.io.output = fakeIo.output
oldIo.input = io.input
function fakeIo.input(oldPath)
if type(oldPath) == "string" then
local path = resolvePath(oldPath)
if notAllowed[string.lower(path)] then
return nil
end
return oldIo.input(path)
end
end
_G.io.input = fakeIo.input
oldIo.lines = io.lines
function fakeIo.lines(oldPath)
local path = resolvePath(oldPath)
if notAllowed[string.lower(path)] then
return nil
end
return oldIo.lines(path)
end
_G.io.lines = fakeIo.lines
--FS library
oldFs.open = fs.open
function fakeFs.open(oldPath,a)
local path = resolvePath(oldPath)
if notAllowed[string.lower(path)] then
return nil
end
return oldFs.open(path,a)
end
_G.fs.open = fakeFs.open
oldFs.delete = fs.delete
function fakeFs.delete(oldPath)
local path = resolvePath(oldPath)
if notAllowed[string.lower(path)] then
return nil
end
return oldFs.delete(path)
end
_G.fs.delete = fakeFs.delete
oldFs.copy = fs.copy
function fakeFs.copy(oldPath,oldpath1)
local path = resolvePath(oldPath)
local path1 = resolvePath(oldPath1)
if notAllowed[string.lower(path)] or notAllowed[string.lower(path1)] then
return nil
end
return oldFs.copy(path)
end
_G.fs.copy = fakeFs.copy
oldFs.move = fs.move
function fakeFs.move(oldPath,oldpath1)
local path = resolvePath(oldPath)
local path1 = resolvePath(oldPath1)
if notAllowed[string.lower(path)] or notAllowed[string.lower(path1)] then
return nil
end
return oldFs.move(path)
end
_G.fs.move = fakeFs.move
oldFs.makeDir = fs.makeDir
function fakeFs.makeDir(oldPath)
local path = resolvePath(oldPath)
if notAllowed[string.lower(path)] then
return nil
end
return oldFs.makeDir(path)
end
_G.fs.makeDir = fakeFs.makeDir
oldFs.exists = fs.exists
function fakeFs.exists(oldPath)
local path = resolvePath(oldPath)
if notAllowed[string.lower(path)] then
return false
end
return oldFs.exists(path)
end
_G.fs.exists = fakeFs.exists
end
local oldErr = printError
local function overwrite()
_G.printError = oldErr
_G.os.pullEvent = oldPull
_G['rednet'] = nil
setupTerm()
if oldSettingsGet("bm-bios.secureboot") then
overides()
end
local success, err = pcall(findBootableDevice)
if not success then
print(err)
print("Press any key to continue.")
os.pullEvent("key")
end
end
_G.printError = overwrite
_G.os.pullEvent = nil
--os.queueEvent("key")