This repository was archived by the owner on Jun 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbios.lua
More file actions
321 lines (268 loc) · 8.41 KB
/
bios.lua
File metadata and controls
321 lines (268 loc) · 8.41 KB
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
local stringsMain, stringsChangeLabel, stringKeyDown, stringsFilesystem, colorsTitle, colorsBackground, colorsText, colorsSelectionBackground, colorsSelectionText, componentProxy, componentList, pullSignal, uptime, tableInsert, mathMax, mathMin, mathHuge, mathFloor = "MineOS EFI", "Change label", "key_down", "filesystem", 0x2D2D2D, 0xE1E1E1, 0x878787, 0x878787, 0xE1E1E1, component.proxy, component.list, computer.pullSignal, computer.uptime, table.insert, math.max, math.min, math.huge, math.floor
local eeprom, gpu, internetAddress = componentProxy(componentList("eeprom")()), componentProxy(componentList("gpu")()), componentList("internet")()
gpu.bind(componentList("screen")(), true)
local shutdown, gpuSet, gpuSetBackground, gpuSetForeground, gpuFill, eepromSetData, eepromGetData, screenWidth, screenHeight = computer.shutdown, gpu.set, gpu.setBackground, gpu.setForeground, gpu.fill, eeprom.setData, eeprom.getData, gpu.getResolution()
local OSList, rectangle, centrizedText, menuElement =
{
{
"/OS.lua",
function()
end
},
{
"/init.lua",
function()
computer.getBootAddress, computer.setBootAddress = eepromGetData, eepromSetData
end
}
},
function(x, y, width, height, color)
gpuSetBackground(color)
gpuFill(x, y, width, height, " ")
end,
function(y, foreground, text)
local x = mathFloor(screenWidth / 2 - #text / 2)
gpuSetForeground(foreground)
gpuSet(x, y, text)
end,
function(text, callback, breakLoop)
return {
s = text,
c = callback,
b = breakLoop
}
end
local function title(y, titleText)
y = mathFloor(screenHeight / 2 - y / 2)
rectangle(1, 1, screenWidth, screenHeight, colorsBackground)
centrizedText(y, colorsTitle, titleText)
return y + 2
end
local function status(titleText, statusText, needWait)
local lines = {}
for line in statusText:gmatch("[^\r\n]+") do
lines[#lines + 1] = line:gsub("\t", " ")
end
local y = title(#lines, titleText)
for i = 1, #lines do
centrizedText(y, colorsText, lines[i])
y = y + 1
end
if needWait then
repeat
needWait = pullSignal()
until needWait == stringKeyDown or needWait == "touch"
end
end
local function executeString(...)
local result, reason = load(...)
if result then
result, reason = xpcall(result, debug.traceback)
if result then
return
end
end
status(stringsMain, reason, 1)
end
local boot, menuBack, menu, input =
function(proxy)
for i = 1, #OSList do
if proxy.exists(OSList[i][1]) then
status(stringsMain, "Booting from " .. (proxy.getLabel() or proxy.address))
-- Updating current EEPROM boot address if it's differs from given proxy address
if eepromGetData() ~= proxy.address then
eepromSetData(proxy.address)
end
-- Running OS pre-boot function
OSList[i][2]()
-- Reading boot file
local handle, data, chunk, success, reason = proxy.open(OSList[i][1], "rb"), ""
repeat
chunk = proxy.read(handle, mathHuge)
data = data .. (chunk or "")
until not chunk
proxy.close(handle)
-- Running boot file
executeString(data, "=" .. OSList[i][1])
return 1
end
end
end,
function(f)
return menuElement("Back", f, 1)
end,
function(titleText, elements)
local selectedElement, maxLength = 1, 0
for i = 1, #elements do
maxLength = math.max(maxLength, #elements[i].s)
end
while 1 do
local y, x, eventData = title(#elements + 2, titleText)
for i = 1, #elements do
x = mathFloor(screenWidth / 2 - #elements[i].s / 2)
if i == selectedElement then
rectangle(mathFloor(screenWidth / 2 - maxLength / 2) - 2, y, maxLength + 4, 1, colorsSelectionBackground)
gpuSetForeground(colorsSelectionText)
gpuSet(x, y, elements[i].s)
gpuSetBackground(colorsBackground)
else
gpuSetForeground(colorsText)
gpuSet(x, y, elements[i].s)
end
y = y + 1
end
eventData = {pullSignal()}
if eventData[1] == stringKeyDown then
if eventData[4] == 200 and selectedElement > 1 then
selectedElement = selectedElement - 1
elseif eventData[4] == 208 and selectedElement < #elements then
selectedElement = selectedElement + 1
elseif eventData[4] == 28 then
if elements[selectedElement].c then
elements[selectedElement].c()
end
if elements[selectedElement].b then
return
end
end
end
end
end,
function(y, prefix)
local text, state, eblo, eventData, char = "", true
while 1 do
eblo = prefix .. text
gpuFill(1, y, screenWidth, 1, " ")
gpuSetForeground(colorsText)
gpuSet(mathFloor(screenWidth / 2 - #eblo / 2), y, eblo .. (state and "█" or ""))
eventData = {pullSignal(0.5)}
if eventData[1] == stringKeyDown then
if eventData[4] == 28 then
return text
elseif eventData[4] == 14 then
text = text:sub(1, -2)
else
char = unicode.char(eventData[3])
if char:match("^[%w%d%p%s]+") then
text = text .. char
end
end
state = true
elseif eventData[1] == "clipboard" then
text = text .. eventData[3]
elseif not eventData[1] then
state = not state
end
end
end
status(stringsMain, "Hold Alt to show boot options")
local deadline, eventData = uptime() + 1
while uptime() < deadline do
eventData = {pullSignal(deadline - uptime())}
if eventData[1] == stringKeyDown and eventData[4] == 56 then
local utilities = {
menuElement("Disk management", function()
local restrict, filesystems, filesystemOptions =
function(text, limit)
if #text < limit then
text = text .. string.rep(" ", limit - #text)
else
text = text:sub(1, limit)
end
return text .. " "
end,
{menuBack()}
local function updateFilesystems()
for i = 2, #filesystems do
table.remove(filesystems, 1)
end
for address in componentList(stringsFilesystem) do
local proxy = componentProxy(address)
local label, isReadOnly, filesystemOptions =
proxy.getLabel() or "Unnamed",
proxy.isReadOnly(),
{
menuElement("Set as bootable medium", function()
eepromSetData(address)
updateFilesystems()
end, 1)
}
if not isReadOnly then
tableInsert(filesystemOptions, menuElement(stringsChangeLabel, function()
proxy.setLabel(input(title(2, stringsChangeLabel), "Enter new name: "))
updateFilesystems()
end, 1))
tableInsert(filesystemOptions, menuElement("Format", function()
status(stringsMain, "Formatting filesystem " .. address)
for _, file in ipairs(proxy.list("/")) do
proxy.remove(file)
end
updateFilesystems()
end, 1))
end
tableInsert(filesystemOptions, menuBack())
tableInsert(filesystems, 1,
menuElement(
(address == eepromGetData() and "> " or " ") ..
restrict(label, 12) ..
restrict(proxy.spaceTotal() > 1048576 and "HDD" or proxy.spaceTotal() > 65536 and "FDD" or "SYS", 3) ..
restrict(isReadOnly and "R" or "R/W", 3) ..
restrict(string.format("%.1f", proxy.spaceUsed() / proxy.spaceTotal() * 100) .. "%", 6) ..
address:sub(1, 7) .. "…",
function()
menu(label .. " (" .. address .. ")", filesystemOptions)
end
)
)
end
end
updateFilesystems()
menu("Select filesystem", filesystems)
end),
menuElement("Shutdown", function()
shutdown()
end),
menuBack()
}
if internetAddress then
tableInsert(utilities, 2, menuElement("Clean install (deletes all files)", function()
local handle, data, result, reason = componentProxy(internetAddress).request("https://raw.githubusercontent.com/IgorTimofeev/MineOS/master/Installer/Main.lua"), ""
if handle then
status(stringsMain, "Downloading recovery script")
while 1 do
result, reason = handle.read(mathHuge)
if result then
data = data .. result
else
handle.close()
if reason then
status(stringsMain, reason, 1)
else
executeString(data, "=string")
end
break
end
end
else
status(stringsMain, "invalid URL-address", 1)
end
end))
end
menu(stringsMain, utilities)
end
end
local proxy = componentProxy(eepromGetData())
if not (proxy and boot(proxy)) then
for address in componentList(stringsFilesystem) do
proxy = componentProxy(address)
if boot(proxy) then
break
else
proxy = nil
end
end
if not proxy then
status(stringsMain, "No bootable mediums found", 1)
end
end
shutdown()