-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboot.lua
300 lines (259 loc) · 7.24 KB
/
boot.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
--[[
The MIT License (MIT)
Copyright (c) 2014-2015 the TARDIX team
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]
--[[
Set up an environment for the kernel,
dynamic link it into memory,
run it.
]]
term.setCursorPos(1,1)
term.clear()
local function split(inputstr, sep)
sep = sep or "%s"
local t={} ; i=1
for str in string.gmatch(inputstr, '([^'..sep..']+)') do
t[i] = str
i = i + 1
end
return t
end
local _cmdlin1 = {...}
_G.kernelcmd = {}
for k, v in pairs(_cmdlin1) do
if #split(v, '=') == 2 then
local i, j = unpack(split(v, '='))
if #split(j, ',') >= 2 then
local x = split(j, ',')
kernelcmd[i] = x
else
kernelcmd[i] = j
end
else
kernelcmd[v] = true
end
end
function string.randomize(template)
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb)
return string.format('%x', v)
end)
end
function table.size(tab)
local ret = 0
for k, v in pairs(tab) do ret = ret + 1 end
return ret
end
function table.from(tab, start)
local ret = {}
for i = start, #tab do
ret[#ret + 1] = tab[i]
end
return ret
end
function table.filter(tab, fun)
local ret = {}
for k, v in pairs(tab) do
if fun(k, v) == true then
ret[k] = v
end
end
return ret
end
function table.foreach(tab, fun)
local ret = {}
for k, v in pairs(tab) do
ret[k] = fun(k, v)
end
return ret
end
----------------------------------------------------------------------------------------------------------
_G.kRoot = kernelcmd['kernel_root']
local ok, err = loadfile(fs.combine(kRoot, '/core/kmessage.lua'))
if not ok then
printError(err)
while true do
coroutine.yield()
end
end
_G.kmsg = ok()
kmsg.post('core', 'tardix kernel attempting initialization now')
if fs.exists(fs.combine(kRoot, '/core/lib')) then
for k, v in pairs(fs.list(fs.combine(kRoot, '/core/lib'))) do
if not fs.isDir(fs.combine(fs.combine(kRoot, '/core/lib'), v)) then
local ok, err = loadfile(fs.combine(fs.combine(kRoot, '/core/lib'), v))
kmsg.post('core', 'loaded library %s.', v)
if not ok then
printError(err)
else
_G[({v:gsub('.lua', '')})[1]] = ok()
end
end
end
end
if fs.exists(fs.combine(kRoot, '/core/lib/init')) then
_G.init = {}
for k, v in pairs(fs.list(fs.combine(kRoot, '/core/lib/init'))) do
local ok, err = loadfile(fs.combine(fs.combine(kRoot, '/core/lib/init'), v))
if not ok then
printError(err)
else
kmsg.post('core', 'loaded initialization library %s', v)
_G.init[({v:gsub('.lua', '')})[1]] = ok()
end
end
end
if fs.exists(fs.combine(kRoot, '/core/mod')) then
if not kernelcmd['nomods'] and module and fs.exists(fs.combine(kRoot, '/core/mod')) then
for k, v in pairs(fs.list(fs.combine(kRoot, '/core/mod'))) do
loadfile(fs.combine(fs.combine(kRoot, '/core/mod'), v))()
end
module.probeAll('load')
elseif kernelcmd['nomods'] and kernelcmd['loadmods'] and module and fs.exists(fs.combine(kRoot, '/core/mod')) then
for k, v in pairs(fs.list(fs.combine(kRoot, '/core/mod'))) do
loadfile(fs.combine(fs.combine(kRoot, '/core/mod'), v))()
end
for k, v in pairs(kernelcmd['loadmods']) do
module.probe(v, 'load')
end
end
end
function kreq(path)
return loadfile(fs.combine(kRoot, path))()
end
kmsg.post('init', 'searching for init')
if run.exec then
if kernelcmd['initrfs'] then
local initf = init.initrfs.loadinitrfs(kernelcmd['initrfs'])
local inits = {
'/init',
'/sbin/init',
'/bin/init',
'/lib/init',
'/usr/init',
'/usr/sbin/init',
'/usr/bin/init',
'/usr/lib/init',
'/init.lua',
'/sbin/init.lua',
'/bin/init.lua',
'/lib/init.lua',
'/usr/init.lua',
'/usr/sbin/init.lua',
'/usr/bin/init.lua',
'/usr/lib/init.lua',
}
for i = 1, #inits do
if initf.files and initf.files[inits[i]] then
kmsg.post('init', 'found init %s in a initrfs %s', kernelcmd['initrfs'], inits[i])
run.spawn(init.initrfs.loadfileFrom(initf, inits[i]))
break
end
end
else
local inits = {
'/init',
'/sbin/init',
'/bin/init',
'/lib/init',
'/usr/init',
'/usr/sbin/init',
'/usr/bin/init',
'/usr/lib/init',
'/init.lua',
'/sbin/init.lua',
'/bin/init.lua',
'/lib/init.lua',
'/usr/init.lua',
'/usr/sbin/init.lua',
'/usr/bin/init.lua',
'/usr/lib/init.lua',
}
for i = 1, #inits do
if fs.exists(inits[i]) then
kmsg.post('init', 'found init %s in root', inits[i])
run.spawn(inits[i])
break
end
end
end
else
local inits = {
'/init',
'/sbin/init',
'/bin/init',
'/lib/init',
'/usr/init',
'/usr/sbin/init',
'/usr/bin/init',
'/usr/lib/init',
'/init.lua',
'/sbin/init.lua',
'/bin/init.lua',
'/lib/init.lua',
'/usr/init.lua',
'/usr/sbin/init.lua',
'/usr/bin/init.lua',
'/usr/lib/init.lua',
}
for i = 1, #inits do
if fs.exists(inits[i]) then
kmsg.post('init', 'found init %s, initialized sequencially', inits[i])
dofile(inits[i])
break
end
end
end
_G.kthread = {
['hans'] = {}
}
function kthread.addFunctions(tab)
assert(type(tab) == 'table', 'kthread.addFunctions expects a table of functions')
for k, v in pairs(tab) do
assert(type(v) == 'function', 'kthread.addFunctions expects a table of functions')
table.insert(kthread.hans, v)
end
end
function kthread.getHandlers()
return kthread.hans
end
function kthread.addFile(file)
local ok, err = loadfile(file)
if not ok then
error(err)
end
kthread.addFunctions(run.dailin.link(ok))
end
kmsg.post('kthread', 'starting kernel thread')
run.exec(fs.combine(kRoot, '/core/kthread.lua'))
if fs.exists(fs.combine(kRoot, '/core/events')) then
for k, v in pairs(fs.list(fs.combine(kRoot, '/core/events'))) do
kmsg.post('kthread', 'added event handler %s', v)
kthread.addFile(fs.combine(fs.combine(kRoot,'/core/events'), v))
end
end
kmsg.post('core', 'completed initialization; starting main loop.')
while true do
if threading then
local data = {coroutine.yield()}
if data[1] == 'terminate' then
os.reboot()
end
threading.scheduler:update(unpack(data))
end
end