-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua.c
More file actions
550 lines (494 loc) · 15.4 KB
/
lua.c
File metadata and controls
550 lines (494 loc) · 15.4 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#include <lua.h>
#include <lapi.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdio.h>
#include <stdlib.h>
extern void printk(char* s);
extern void printki(long n);
extern void printkid(long n);
extern void* kmalloc(long len);
extern unsigned char rdiskb(unsigned long offset);
extern unsigned short rdiskw(unsigned long offset);
extern unsigned int rdiski(unsigned long offset);
extern unsigned long rdiskl(unsigned long offset);
extern unsigned char inb(unsigned short port);
extern unsigned short inw(unsigned short port);
extern unsigned int ind(unsigned short port);
extern void outb(unsigned short port, unsigned char val);
extern void outw(unsigned short port, unsigned short val);
extern void outd(unsigned short port, unsigned int val);
extern void irq_mask(unsigned char irq, unsigned char mask);
extern void io_wait();
static int lmalloc(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) malloc(lua_tounsigned(L, 1)));
return 1;
}
static int lcalloc(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) calloc(lua_tounsigned(L, 1), lua_tounsigned(L, 2)));
return 1;
}
static int lfree(lua_State *L)
{
free(lua_tounsigned(L, 1));
return 0;
}
static int lkmalloc(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) kmalloc(lua_tounsigned(L, 1)));
return 1;
}
static int lpeekb(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) *((unsigned char*)lua_tounsigned(L, 1)));
return 1;
}
static int lpeekw(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) *((unsigned short*)lua_tounsigned(L, 1)));
return 1;
}
static int lpeeki(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) *((unsigned int*)lua_tounsigned(L, 1)));
return 1;
}
static int lpeekl(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) *((unsigned long*)lua_tounsigned(L, 1)));
return 1;
}
static int lpokeb(lua_State *L)
{
*((unsigned char*)lua_tounsigned(L, 1)) = (unsigned char) lua_tounsigned(L, 2);
return 0;
}
static int lpokew(lua_State *L)
{
*((unsigned short*)lua_tounsigned(L, 1)) = (unsigned short) lua_tounsigned(L, 2);
return 0;
}
static int lpokei(lua_State *L)
{
*((unsigned int*)lua_tounsigned(L, 1)) = (unsigned int) lua_tounsigned(L, 2);
return 0;
}
static int lpokel(lua_State *L)
{
*((unsigned long*)lua_tounsigned(L, 1)) = (unsigned long) lua_tounsigned(L, 2);
return 0;
}
static int linb(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) inb((unsigned short) lua_tounsigned(L, 1)));
return 1;
}
static int linw(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) inw((unsigned short) lua_tounsigned(L, 1)));
return 1;
}
static int lini(lua_State *L)
{
lua_pushunsigned(L, (lua_Unsigned) ind((unsigned short) lua_tounsigned(L, 1)));
return 1;
}
static int loutb(lua_State *L)
{
outb((unsigned short) lua_tounsigned(L, 1), (unsigned char) lua_tounsigned(L, 2));
return 0;
}
static int loutw(lua_State *L)
{
outw((unsigned short) lua_tounsigned(L, 1), (unsigned short) lua_tounsigned(L, 2));
return 0;
}
static int louti(lua_State *L)
{
outd((unsigned short) lua_tounsigned(L, 1), (unsigned int) lua_tounsigned(L, 2));
return 0;
}
static int lrdiskb(lua_State *L)
{
int n = lua_gettop(L);
if (lua_gettop(L) < 1 || !lua_isinteger(L, n) || !lua_isnumber(L, n)) {
lua_pushstring(L, "lrdiskb incorrect argument");
lua_error(L);
}
lua_pushunsigned(L, (lua_Unsigned) rdiskb(lua_tounsigned(L, n)));
return 1;
}
static int lrdiskw(lua_State *L)
{
int n = lua_gettop(L);
if (lua_gettop(L) < 1 || !lua_isinteger(L, n) || !lua_isnumber(L, n)) {
lua_pushstring(L, "lrdiskw incorrect argument");
lua_error(L);
}
lua_pushunsigned(L, (lua_Unsigned) rdiskw(lua_tounsigned(L, n)));
return 1;
}
static int lrdiski(lua_State *L)
{
int n = lua_gettop(L);
if (lua_gettop(L) < 1 || !lua_isinteger(L, n) || !lua_isnumber(L, n)) {
lua_pushstring(L, "lrdiski incorrect argument");
lua_error(L);
}
lua_pushunsigned(L, (lua_Unsigned) rdiski(lua_tounsigned(L, n)));
return 1;
}
static int lrdiskl(lua_State *L)
{
int n = lua_gettop(L);
if (lua_gettop(L) < 1 || !lua_isinteger(L, n) || !lua_isnumber(L, n)) {
lua_pushstring(L, "lrdiskl incorrect argument");
lua_error(L);
}
lua_pushunsigned(L, (lua_Unsigned) rdiskl(lua_tounsigned(L, n)));
return 1;
}
static int lirqmask(lua_State *L)
{
irq_mask((unsigned char) lua_tounsigned(L, 1), (unsigned char) lua_tounsigned(L, 2));
return 0;
}
static int liowait(lua_State *L)
{
io_wait();
return 0;
}
lua_State *globalL;
void call_lua_int(long inte)
{
lua_State *L = globalL;
lua_getglobal(L, "call_lua_int_2");
lua_pushinteger(L, (lua_Integer) inte);
lua_call(L, 1, 0);
}
void reportlua(lua_State *L)
{
const char* c = lua_tostring(L, lua_gettop(L));
int i;
int j = 0;
char prefix[256];
for (i = 0; i < 255; i++) {
prefix[i] = c[i];
if (c[i] == ':') {
j++;
if (j == 4) {
i++;
break;
}
}
}
if (j == 4) {
prefix[i] = 0;
c += i;
printk(prefix);
printk("\n ");
}
printk(c);
printk("\n");
//printk(lua_tostring(L, lua_gettop(L)));
}
#define LBUFFER_LIMIT 65536
char lbuffer[LBUFFER_LIMIT];
int lbufferptr = 0;
static int clearlbuffer(lua_State *L)
{
lbufferptr = 0;
return 0;
}
static int finishlbuffer(lua_State *L)
{
lua_pushlstring(L, lbuffer, lua_tounsigned(L, 1));
return 1;
}
static int addlbuffer(lua_State *L)
{
long rloc = lua_tounsigned(L, 1);
long sectors_per_cluster = lua_tounsigned(L, 2);
long* lp = (long*)lbuffer;
int i;
for (i = 0; i < (sectors_per_cluster * 512 / 8); i++) {
lp[lbufferptr + i] = rdiskl(rloc);
rloc += 8;
}
lbufferptr += sectors_per_cluster * 512 / 8;
if (lbufferptr >= LBUFFER_LIMIT) {
printk("!!! Need to extend LBUFFER_LIMIT.\n");
printk("!!! Lua file is too long; cannot continue.\n");
while (1) {}
}
return 0;
}
static const luaL_Reg bclib[] = {
{"rdiskb", lrdiskb},
{"rdiskw", lrdiskw},
{"rdiski", lrdiski},
{"rdiskl", lrdiskl},
{"clearlbuffer", clearlbuffer},
{"addlbuffer", addlbuffer},
{"finishlbuffer", finishlbuffer},
{"inb", linb},
{"inw", linw},
{"ini", lini},
{"outb", loutb},
{"outw", loutw},
{"outi", louti},
{"peekb", lpeekb},
{"peekw", lpeekw},
{"peeki", lpeeki},
{"peekl", lpeekl},
{"pokeb", lpokeb},
{"pokew", lpokew},
{"pokei", lpokei},
{"pokel", lpokel},
{"kmalloc", lkmalloc},
{"irqmask", lirqmask},
{"iowait", liowait},
{"malloc", lmalloc},
{"calloc", lcalloc},
{"free", lfree},
{NULL, NULL}
};
LUAMOD_API int luaopen_bc (lua_State *L) {
luaL_newlib(L, bclib);
return 1;
}
void open_private_libs(lua_State *L) {
luaL_requiref(L, "bc", luaopen_bc, 1);
lua_pop(L, 1);
}
// TEMP HACK
extern void irq1();
extern void set_int(unsigned char inte, void* funptr);
void lua_stuff()
{
// TEMP TESTING HACK
set_int(0x21, irq1);
//printk("lua_stuff() starting\n");
lua_State *l;
l = luaL_newstate();
// shh
globalL = l;
luaL_openlibs(l);
open_private_libs(l);
const char* lcode =
"-- lua.c lua\n"
"function is_fat(id)\n"
" return (id == 1) or (id == 4) or (id == 6) or (id == 0xb) or (id == 0xc) or (id == 0xe) or (id == 0x11) or (id == 0x14) or (id == 0x16) or (id == 0x1b) or (id == 0x1c) or (id == 0x1e)\n"
"end\n"
"\n"
"function fat_investigate(lba)\n"
" -- print('base lba is ' .. lba)\n"
" local base = lba * 512\n"
" local total_sectors_16 = bc.rdiskw(base + 19)\n"
" local bytes_per_sector = bc.rdiskw(base + 11)\n"
" local sectors_per_cluster = bc.rdiskb(base + 0xd)\n"
" local reserved_sector_count = bc.rdiskw(base + 14)\n"
" -- print('reserved_sector_count: ' .. reserved_sector_count)\n"
" local table_count = bc.rdiskb(base + 16)\n"
" local table_size_16 = bc.rdiskw(base + 22)\n"
" local root_entry_count = bc.rdiskw(base + 17)\n"
" -- print('root_entry_count: ' .. root_entry_count)\n"
" local num_fats = bc.rdiskb(base + 0x10)\n"
" local sectors_per_fat = bc.rdiskw(base + 0x16)\n"
" -- print('num_fats: ' .. num_fats)\n"
" -- print('sectors_per_fat: ' .. sectors_per_fat)\n"
" local root_dir_sectors = ((root_entry_count * 32) + (bytes_per_sector - 1)) / bytes_per_sector\n"
" local first_data_sector = reserved_sector_count + (table_count * table_size_16)\n"
" local data_sectors = total_sectors_16 - (reserved_sector_count + (table_count * table_size_16) + root_dir_sectors)\n"
" local total_clusters = data_sectors / bc.rdiskb(base + 13)\n"
" local fat_region = lba + reserved_sector_count\n"
" local root_directory_region = fat_region + num_fats * sectors_per_fat\n"
" -- temp hack - fix when we get division back!\n"
" local data_region = root_directory_region + 32\n"
" local fat12 = false\n"
" local fat16 = false\n"
" local fat32 = false\n"
" if total_clusters < 4085 then\n"
" fat12 = true\n"
" elseif total_clusters < 65525 then\n"
" fat16 = true\n"
" else\n"
" fat32 = true\n"
" end\n"
" -- print('bytes per fat sector is ' .. bytes_per_sector)\n"
" -- print('sectors per cluster is ' .. sectors_per_cluster)\n"
" if fat12 then print('FAT12') end\n"
" if fat16 then print('FAT16') end\n"
" if fat32 then print('FAT32') end\n"
" if (fat12 or fat32) then\n"
" print('TODO: fat12/fat32. sorry! (shouldn\\'t be much work, though.)')\n"
" else\n"
" -- print('first_data_sector: ' .. first_data_sector)\n"
" -- local sb = 512 * (first_data_sector + (root_entry_count * 32 / bytes_per_sector))\n"
" -- local sb = 512 * (first_data_sector + 32)\n"
" local sb = 512 * root_directory_region\n"
" -- print(root_entry_count * 32)\n"
" -- print(bytes_per_sector)\n"
" -- print(16384.0 / 512.0)\n"
" -- print((root_entry_count * 32) / bytes_per_sector)\n"
" -- print((first_data_sector + (root_entry_count * 32 / bytes_per_sector)))\n"
" -- print(sb)\n"
" local inc = 0\n"
" local j = 0\n"
" local foundlbc = false\n"
" -- print('TODO: interrogate FAT in case directory is long')\n"
" while (inc < (root_entry_count * 32)) do\n"
" -- print('j is ' .. j)\n"
" -- print('sb is ' .. sb)\n"
" -- print(' and inc is ' .. inc)\n"
" -- print('sb+inc == ' .. (sb + inc))\n"
" if ((bc.rdiskb(sb + inc) == 0) or (bc.rdiskb(sb + inc) == 0xe5)) then\n"
" -- print('no entry; continuing')\n"
" else\n"
" local a = ''\n"
" local b = ''\n"
" local attrib = bc.rdiskb(sb + inc + 11)\n"
" if ((attrib ~= 0xf) and (attrib ~= 0x8)) then\n"
" for i = 0,10 do\n"
" -- print('i stuff')\n"
" local c = bc.rdiskb(sb + inc + i)\n"
" if i == 8 then\n"
" a = a .. '.'\n"
" end\n"
" if ((c ~= 0) and (c ~= 0x20) and (c ~= 10)) then\n"
" a = a .. string.char(c)\n"
" b = b .. c .. ' '\n"
" end\n"
" end\n"
" -- is this the folder we're looking for?\n"
" if ((a == 'LBECAUSE.') and (bit64.band(bc.rdiskb(sb + inc + 11), 0x10) > 0)) then\n"
" foundlbc = true\n"
" -- print('found LBECAUSE folder. this is the disk!')\n"
" -- print('given cluster is ' .. bc.rdiskw(sb + inc + 26))\n"
" --local lbecause_entry_sector = 512 * (sectors_per_cluster * bc.rdiskw(sb + inc + 26))\n"
" local lbecause_entry_sector = data_region + (sectors_per_cluster * (bc.rdiskw(sb + inc + 26) - 2))\n"
" -- temp hack\n"
" --sb = lbecause_entry_sector\n"
" --sb = 501824\n"
" -- print('TODO: interrogate FAT in case subdirectory \\'lbecause\\' is long')\n"
" local sb2 = 512 * lbecause_entry_sector\n"
" local inc2 = 0\n"
" local lua_files = {}\n"
" while (inc2 < 512) do\n"
" if (bc.rdiskb(sb2 + inc2) == 0) then\n"
" break\n"
" else\n"
" local att = bc.rdiskb(sb2 + inc2 + 11)\n"
" local a = ''\n"
" if (att ~= 0xf) then\n"
" for i = 0,10 do\n"
" local c = bc.rdiskb(sb2 + inc2 + i)\n"
" if i == 8 then\n"
" a = a .. '.'\n"
" end\n"
" if ((c ~= 0) and (c ~= 0x20) and (c ~= 10)) then\n"
" a = a .. string.char(c)\n"
" end\n"
" end\n"
" if string.sub(a, -4) == '.LUA' then\n"
" local size = bc.rdiski(sb2 + inc2 + 0x1c)\n"
" --print('reading lua file ' .. a .. ' (' .. size .. ' bytes)')\n"
" local cont = bc.rdiskw(sb2 + inc2 + 0x1a)\n"
" bc.clearlbuffer()\n"
" while ((cont > 0x0002) and (cont < 0xfff8)) do\n"
" local d = data_region + (sectors_per_cluster * (cont - 2))\n"
" -- read file part\n"
" bc.addlbuffer(512 * d, sectors_per_cluster)\n"
" --print(a .. ': [' .. string.format('%x', cont) .. '] ' .. d)\n"
" cont = bc.rdiskw(512 * fat_region + 2 * cont)\n"
" end\n"
" local e = {a, bc.finishlbuffer(size)}\n"
" if a == 'RUN.LUA' then\n"
" table.insert(lua_files, e)\n"
" else\n"
" table.insert(lua_files, 1, e)\n"
" end\n"
" -- print(e[2])\n"
" end\n"
" end\n"
" end\n"
" inc2 = inc2 + 32\n"
" end\n"
" print('* starting Lua *')\n"
" for k,v in pairs(lua_files) do\n"
" --print(v[1])\n"
" assert(loadstring('-- ' .. v[1] .. '\\n' .. v[2]))()\n"
" end\n"
" print('Done.')\n"
" break\n"
" end\n"
" end\n"
" --print(a)\n"
" -- print(b)\n"
" end\n"
" inc = inc + 32\n"
" j = j + 1\n"
" end\n"
" if not foundlbc then print('Well, I failed to find LBECAUSE on a fat16 part.') end\n"
" end\n"
" return\n"
"end\n"
"\n"
"print('TODO: check bus 0 slave disk, bus 1 master & slave')\n"
"print('TODO: extended partitions')\n"
"print('TODO: verify this is in fact an mbr')\n"
"print('TODO: floppies, cds')\n"
"-- print('is partition 1 bootable? ' .. (bc.rdiskb(446) == 0x80 and 'yes' or 'no'))\n"
"-- print('partition 1 start lba is ' .. bc.rdiski(446 + 8))\n"
"-- print('partition 1 total sectors is ' .. bc.rdiski(446 + 12))\n"
"print('partition 1 part type ' .. bc.rdiskb(446 + 4))\n"
"print('partition 2 part type ' .. bc.rdiskb(462 + 4))\n"
"print('partition 3 part type ' .. bc.rdiskb(478 + 4))\n"
"print('partition 4 part type ' .. bc.rdiskb(494 + 4))\n"
"if is_fat(bc.rdiskb(446 + 4)) then\n"
" print('investigating partition 1')\n"
" fat_investigate(bc.rdiski(446 + 8))\n"
"end\n"
"if is_fat(bc.rdiskb(462 + 4)) then\n"
" print('investigating partition 2')\n"
" fat_investigate(bc.rdiski(462 + 8))\n"
"end\n"
"if is_fat(bc.rdiskb(478 + 4)) then\n"
" print('investigating partition 3')\n"
" fat_investigate(bc.rdiski(478 + 8))\n"
"end\n"
"if is_fat(bc.rdiskb(494 + 4)) then\n"
" print('investigating partition 4')\n"
" fat_investigate(bc.rdiski(494 + 8))\n"
"end\n"
;
int lsv = luaL_loadstring(l, lcode);
if (lsv != LUA_OK) {
if (lsv == LUA_ERRSYNTAX) {
printk("LUA_ERRSYNTAX\n");
} else if (lsv == LUA_ERRMEM) {
printk("LUA_ERRMEM\n");
} else if (lsv == LUA_ERRGCMM) {
printk("LUA_ERRGCMM\n");
} else {
printki(lsv); printk("\n");
}
reportlua(l);
return;
}
int pcv = lua_pcall(l, 0, LUA_MULTRET, 0);
if (pcv != LUA_OK) {
if (pcv == LUA_ERRRUN) {
printk("LUA_ERRRUN\n");
reportlua(l);
} else if (pcv == LUA_ERRMEM) {
printk("LUA_ERRMEM\n");
} else if (pcv == LUA_ERRGCMM) {
printk("LUA_ERRGCMM\n");
} else {
printki(pcv); printk("\n");
}
}
}