-
Notifications
You must be signed in to change notification settings - Fork 0
/
.BIOS
348 lines (334 loc) · 8.31 KB
/
.BIOS
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
--BLOCK MESA BIOS
--Used for all Block Mesa bootable computers
--inject code stolen from https://pastebin.com/yzfDMjwf
local oldPull = os.pullEvent
if settngs.get("bm-bios.noTerminate", true) then
_G.os.pullEvent=coroutine.yield
end
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
local function hang()
while true do
os.pullEventRaw("free_lunch")
end
end
--internal flag things
local version = "3.00"
local isDiskBooted = false
local baseDirectory = ""
local directory = "/"
local bootMessage = ""
local whiteColor = 0x00FF00
local blackColor = 0x000000
local function setColors()
for i=1,15 do
local color = i^2
term.setPaletteColor(color,term.nativePaletteColor(color))
end
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()
term.redirect(term.native())
setColors()
term.setCursorBlink(false)
term.clear()
term.setCursorPos(1,1)
print("BLOCK MESA BIOS v"..version)
term.blit("Now in color!","e140db0e14dbe","fffffffffffff")
term.setTextColor(colors.white)
term.setCursorPos(1,4)
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", true) --Default to open setup
if bootIntoSetup then
enterSetup()
end
local notAllowed = {
["/startup.lua"] = true,
["/startup"] = true,
["/startup.lua/"] = true,
["/startup/"] = true,
["/.settings"] = true,
["/.settings/"] = true,
}
local githubUrl = "https://raw.githubusercontent.com/BlockMesa/"
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,
updateFile = function(file,url)
if oldSettingsGet("bm-bios.secureboot") and string.sub(url,1,44) ~= githubUrl 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
--Lemmmy ban avoidance
local a1 = fsOpen(file,"wb")
a1.write(result.readAll())
a1.close()
result.close()
end,
protect = function(path)
notAllowed[path] = true
end,
fixColorScheme = setColors,
resolvePath = resolvePath,
settingsGet = oldSettingsGet,
settingsSet = oldSettingsSet,
}
local function boot(prefix)
print(bootMessage)
baseDirectory = prefix
directory = prefix
local success, response = pcall(os.run,{bios=bios},prefix..".BOOT")
if not success then
print(response)
hang()
end
end
local function findBootableDevice()
if fs.exists("disk") and fs.exists("/disk/.BOOT") then
bootMessage = "Booting from external drive"
isDiskBooted = true
boot("/disk/")
elseif fs.exists("/.BOOT") then
bootMessage = "Booting from internal disk"
boot("/")
else
print("NO BOOT DEVICE FOUND!")
hang()
end
end
local blankFunction = function()
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)] and (a == "w" or a == "wb" or a == "w+" or a == "wb+" or a == "w+b" or a == "rb+" or a == "r+" or a == "r+b") 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)] and (a == "w" or a == "wb" or a == "w+" or a == "wb+" or a == "w+b" or a == "rb+" or a == "r+" or a == "r+b") 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)
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
if compat and compat.isCapy64 then
overwrite()
end
--os.queueEvent("key")
hang()