2019-05-15 19:26:40 +02:00
|
|
|
// The OS/K Team licences this file to you under the MIT license.
|
2019-05-15 21:56:42 +02:00
|
|
|
// See the LICENSE file in the project root for more information.
|
2019-05-15 19:26:40 +02:00
|
|
|
|
|
|
|
#include "arch.h"
|
|
|
|
|
2019-05-22 18:39:46 +02:00
|
|
|
#define FWPROGSIZE (1024 * 1024 * 1024)
|
|
|
|
static ssize_t fwsize;
|
|
|
|
static ushort *fwprog;
|
2019-05-15 19:26:40 +02:00
|
|
|
|
|
|
|
ushort bget(ctx_t *ctx)
|
|
|
|
{
|
2019-05-22 18:39:46 +02:00
|
|
|
if (ctx->r[RIP].val % 2) {
|
|
|
|
_except(ctx, E_ALI, "Misaligned RIP register: 0x%016lX",
|
|
|
|
ctx->r[RIP].val);
|
|
|
|
}
|
|
|
|
|
2019-05-16 21:42:23 +02:00
|
|
|
if (addr2real(ctx->r[RIP].val) >= ctx->mz) {
|
2019-05-16 18:45:00 +02:00
|
|
|
_except(ctx, E_ACC, "Executing out of memory: 0x%016lX",
|
|
|
|
ctx->r[RIP].val);
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
2019-05-29 16:57:22 +02:00
|
|
|
|
2019-05-16 10:03:01 +02:00
|
|
|
ushort c = ctx->mp[addr2real(ctx->r[RIP].val)];
|
|
|
|
|
2019-05-22 18:39:46 +02:00
|
|
|
ctx->r[RIP].val += 2;
|
2019-05-16 10:03:01 +02:00
|
|
|
|
|
|
|
return c;
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 21:42:23 +02:00
|
|
|
ushort dget(ctx_t *ctx)
|
|
|
|
{
|
|
|
|
static int i = 0;
|
|
|
|
|
2019-05-22 18:39:46 +02:00
|
|
|
if (i >= fwsize) {
|
2019-05-16 21:42:23 +02:00
|
|
|
log("Finished disassembling\n");
|
|
|
|
fclose(ctx->disf);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->r[RIP].val += 2;
|
|
|
|
return fwprog[i++];
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
2019-05-15 19:26:40 +02:00
|
|
|
{
|
2019-05-16 21:42:23 +02:00
|
|
|
ctx_t main_ctx;
|
2019-05-22 18:39:46 +02:00
|
|
|
FILE *fwfile;
|
2019-05-15 19:26:40 +02:00
|
|
|
|
|
|
|
main_ctx.r = arch_r;
|
|
|
|
main_ctx.i = arch_i;
|
2019-05-16 21:42:23 +02:00
|
|
|
|
2019-05-22 18:39:46 +02:00
|
|
|
if (argc < 2) {
|
|
|
|
log("Not enough arguments\n");
|
|
|
|
exit(-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
fwprog = malloc(FWPROGSIZE);
|
|
|
|
fwfile = fopen(argv[1], "rb");
|
|
|
|
|
|
|
|
if (!fwprog) {
|
|
|
|
log("Couldn't allocate firmware buffer\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fwfile) {
|
|
|
|
log("Couldn't open program file\n");
|
|
|
|
exit(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
fwsize = fread(fwprog, 1, FWPROGSIZE, fwfile);
|
|
|
|
|
|
|
|
//log("Loaded %lu bytes
|
|
|
|
|
|
|
|
if (fwsize < 2) {
|
|
|
|
log("Error while reading program file\n");
|
|
|
|
exit(-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(argc > 2 && !strcmp(argv[2], "-d"))) {
|
|
|
|
main_ctx.mp = malloc(MEMSIZE + 16);
|
2019-05-16 21:42:23 +02:00
|
|
|
main_ctx.mz = MEMSIZE;
|
|
|
|
|
|
|
|
main_ctx.get = bget;
|
|
|
|
main_ctx.disf = NULL;
|
|
|
|
|
|
|
|
main_ctx.r[RIP].val = MEMOFF;
|
2019-05-15 19:26:40 +02:00
|
|
|
|
2019-05-16 21:42:23 +02:00
|
|
|
if (main_ctx.mp == 0) {
|
|
|
|
log("Couldn't allocate RAM\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2019-05-22 18:39:46 +02:00
|
|
|
memcpy(&main_ctx.mp[addr2real(main_ctx.r[RIP].val)], fwprog, fwsize);
|
2019-05-16 21:42:23 +02:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
decode(&main_ctx);
|
|
|
|
}
|
2019-05-16 10:03:01 +02:00
|
|
|
}
|
2019-05-15 19:26:40 +02:00
|
|
|
|
2019-05-16 21:42:23 +02:00
|
|
|
// Disassembly
|
|
|
|
else {
|
|
|
|
main_ctx.disf = fopen("fwprog.dis", "w");
|
|
|
|
|
|
|
|
if (main_ctx.disf == NULL) {
|
|
|
|
log("Couldn't open fwprog.dis\n");
|
|
|
|
exit(-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
main_ctx.mp = NULL;
|
|
|
|
main_ctx.mz = 0;
|
|
|
|
|
|
|
|
main_ctx.get = dget;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
disasm(&main_ctx);
|
|
|
|
}
|
2019-05-15 19:26:40 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 21:42:23 +02:00
|
|
|
|
2019-05-15 19:26:40 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|