Skip to content

Commit

Permalink
Integrated filesystem.c into the gui and cleaned up mem.c
Browse files Browse the repository at this point in the history
  • Loading branch information
pruten committed Apr 12, 2014
1 parent e821728 commit ba8b9e8
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 267 deletions.
7 changes: 0 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@

CC = clang
CFLAGS = -O3 -arch i386 -arch x86_64 -Wno-deprecated-declarations
LFLAGS = -framework OpenGL -framework GLUT

all: shoebill

shoebill: make_core
# shoebill: make_core test.c
# $(CC) $(LFLAGS) $(CFLAGS) -L intermediates -l shoebill_core test.c -o shoebill

make_core:
$(MAKE) -C core -j 4

clean:
rm -rf intermediates
rm -f shoebill
78 changes: 55 additions & 23 deletions core/coff.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <assert.h>
#include "shoebill.h"
#include "coff.h"

Expand All @@ -40,25 +41,37 @@ void symb_inorder(rb_node *cur) {
symb_inorder(cur->right);
}

#define _coff_buf_seek(__len) ({ \
uint32_t _result = 1, _len=(__len); \
if (_len > buflen) _result = 0; \
else bufptr = _len; \
_result; \
})

#define _coff_buf_read(_dst, __len) ({\
uint32_t _result = 1, _len=(__len); \
if ((bufptr + _len) > buflen) \
_result = 0; \
else {\
memcpy((_dst), buf+bufptr, _len); \
bufptr += _len; \
} \
_result; \
})


// Given a path to a COFF binary, create a coff_file structure and return a pointer.
// God help you if you want to free it.
coff_file* coff_parser(const char *path)
coff_file* coff_parse(uint8_t *buf, uint32_t buflen)
{
FILE *f;
uint8_t rawhead[20], *ptr;
uint32_t i;
coff_file *cf = NULL;

// Open the file
f = fopen(path, "r");
if (!f) {
printf("coff_parser: I couldn't open that binary for reading (%s)\n", path);
goto fail;
}
uint32_t bufptr = 0;

// Pull out 20 bytes (the file header)
if (fread(rawhead, 20, 1, f) != 1) {
printf("coff_parser: error: this binary is missing its file header\n");
if (!_coff_buf_read(rawhead, 20)) {
printf("coff_parse: error: this binary is missing its file header\n");
goto fail;
}

Expand Down Expand Up @@ -86,7 +99,7 @@ coff_file* coff_parser(const char *path)
// pull out cf->opt_header bytes (a.out-format header, I guess?)
if (cf->opt_header_len > 0) {
uint8_t *opt = malloc(cf->opt_header_len);
if (fread(opt, cf->opt_header_len, 1, f) != 1) {
if (!_coff_buf_read(opt, cf->opt_header_len)) {
printf("coff_parse: I ran out of data pulling the optional header (%u bytes)\n", cf->opt_header_len);
free(opt);
goto fail;
Expand All @@ -99,7 +112,7 @@ coff_file* coff_parser(const char *path)
for (i=0; i<cf->num_sections; i++) {
// read the header
uint8_t rawsec[40];
if (fread(rawsec, 40, 1, f) != 1) {
if (!_coff_buf_read(rawsec, 40)) {
printf("coff_parse: I ran out of data pulling section #%u\n", i+1);
goto fail;
}
Expand Down Expand Up @@ -139,14 +152,14 @@ coff_file* coff_parser(const char *path)
}

// seek to the position in the binary that holds this section's raw data
if (fseek(f, cf->sections[i].data_ptr, SEEK_SET) != 0) {
if (!_coff_buf_seek(cf->sections[i].data_ptr)) {
printf("coff_parse: I couldn't seek to 0x%x in section %u\n", cf->sections[i].data_ptr, i+1);
goto fail;
}

// load the data and attach it to the section struct
data = malloc(cf->sections[i].sz); // FIXME: sz might not be a sane value
if (fread(data, cf->sections[i].sz, 1, f) != 1) {
if (!_coff_buf_read(data, cf->sections[i].sz)) {
printf("coff_parse: I couldn't fread section %u (%s)'s data (%u bytes)\n", i+1, cf->sections[i].name, cf->sections[i].sz);
free(data);
goto fail;
Expand All @@ -164,14 +177,14 @@ coff_file* coff_parser(const char *path)
cf->symbols = (coff_symbol*)calloc(sizeof(coff_symbol), cf->num_symbols);

// Seek to the symbol table
if (fseek(f, cf->symtab_offset, SEEK_SET) != 0) {
if (!_coff_buf_seek(cf->symtab_offset)) {
printf("coff_parse: I couldn't seek to symtab_offset, 0x%x\n", cf->symtab_offset);
goto fail;
}

for (i=0; i < cf->num_symbols; i++) {
uint8_t raw_symb[18];
if (fread(raw_symb, 18, 1, f) != 1) {
if (!_coff_buf_read(raw_symb, 18)) {
printf("coff_parse: I ran out of data pulling symbol #%u\n", i+1);
goto fail;
}
Expand All @@ -185,11 +198,11 @@ coff_file* coff_parser(const char *path)

// printf("Loading from external: base idx=0x%x, offset=%u, addr=0x%x\n", idx-offset, offset, idx);

if (fseek(f, idx, SEEK_SET) != 0) {
if (!_coff_buf_seek(idx)) {
printf("coff_parse: I ran out of data pulling symbol %u's name (idx=0x%x)\n", i+1, idx);
goto fail;
}
for (j=0; (fread(&tmp_name[j], 1, 1, f)==1) && tmp_name[j]; j++) {
for (j=0; (_coff_buf_read(&tmp_name[j], 1)) && tmp_name[j]; j++) {
if (j >= 255) {
// printf("coff_parse: this symbol's name is too long: %u\n", i+1);
goto fail;
Expand All @@ -198,10 +211,7 @@ coff_file* coff_parser(const char *path)
cf->symbols[i].name = malloc(j+1);
memcpy(cf->symbols[i].name, tmp_name, j);
cf->symbols[i].name[j] = 0;
fseek(f, cf->symtab_offset + (i+1)*18, SEEK_SET);
//printf("cf->symtab_offset = 0x%x, i=%u, (i+1)*18 = %u\n",
//cf->symtab_offset, i, (i+1)*18);
//printf("seeking back to 0x%x\n", cf->symtab_offset + (i+1)*18);
_coff_buf_seek(cf->symtab_offset + (i+1)*18);
}
else {
uint8_t tmp_name[9];
Expand Down Expand Up @@ -257,6 +267,28 @@ coff_file* coff_parser(const char *path)
return NULL;
}

coff_file* coff_parse_from_path(const char *path)
{
FILE *f = fopen(path, "r");
uint8_t *buf = malloc(1);
uint32_t i=0, tmp;
coff_file *coff;

do {
buf = realloc(buf, i + 128*1024);
assert(buf);
tmp = fread(buf+i, 1, 128*1024, f);
i += tmp;

// don't load more than 64mb - there are surely no 64MB A/UX kernels
} while ((tmp > 0) && (i < 64*1024*1024));


coff = coff_parse(buf, i);
free(buf);
return coff;
}

// dump some data about a coff_file structure
void print_coff_info(coff_file *coff)
{
Expand Down
3 changes: 2 additions & 1 deletion core/coff.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ typedef struct {
coff_symbol* coff_find_func(coff_file *coff, uint32_t addr);
coff_symbol* coff_find_symbol(coff_file *coff, const char *name);

coff_file* coff_parser(const char *path);
coff_file* coff_parse(uint8_t *buf, uint32_t buflen);
coff_file* coff_parse_from_path(const char *path);
uint32_t be2native (uint8_t **dat, uint32_t bytes);
void print_coff_info(coff_file *coff);

Expand Down
40 changes: 21 additions & 19 deletions core/core_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,8 +641,8 @@ uint32_t shoebill_initialize(shoebill_control_t *control)
uint32_t i, j, pc = 0xffffffff;
coff_file *coff = NULL;
scsi_device_t disks[8];
uint8_t *rom_data = NULL;
uint32_t rom_size = 0;
uint8_t *rom_data = NULL, *kernel_data = NULL;
uint32_t rom_size = 0, kernel_size;

memset(&disks[0], 0, 8 * sizeof(scsi_device_t));
memset(&shoe, 0, sizeof(global_shoebill_context_t));
Expand All @@ -657,16 +657,26 @@ uint32_t shoebill_initialize(shoebill_control_t *control)
else if (!_load_rom(control, &rom_data, &rom_size))
goto fail;

// Try to open the disk images
if (!_open_disk_images(control, disks))
goto fail;

// Try to load the A/UX kernel
if (control->aux_kernel_path == NULL) {
sprintf(control->error_msg, "No A/UX kernel specified\n");
goto fail;
}
coff = coff_parser(control->aux_kernel_path);
else if (!control->scsi_devices[0].path || strlen((char*)control->scsi_devices[0].path)==0) {
sprintf(control->error_msg, "The root A/UX disk needs to be at scsi ID 0\n");
goto fail;
}

// Load the kernel from the disk at scsi id #0
kernel_data = shoebill_extract_kernel((char*)control->scsi_devices[0].path,
control->aux_kernel_path,
control->error_msg,
&kernel_size);
if (!kernel_data)
goto fail;

coff = coff_parse(kernel_data, kernel_size);
free(kernel_data);

if (coff == NULL) {
sprintf(control->error_msg, "Can't open that A/UX kernel [%s]\n",
Expand All @@ -675,15 +685,13 @@ uint32_t shoebill_initialize(shoebill_control_t *control)
}
shoe.coff = coff;

// Try to open the disk images
if (!_open_disk_images(control, disks))
goto fail;

// Allocate and configure the rom and memory space

/*if (control->ram_size > (256 * 1024 * 1024)) {
// I think A/UX will go insane if you give it >256MB of memory
sprintf(control->error_msg, "%u bytes is too much memory\n", control->ram_size);
goto fail;
}
else */if (control->ram_size < (1024*1024)) {
if (control->ram_size < (1024*1024)) {
sprintf(control->error_msg, "%u bytes is too little ram\n", control->ram_size);
goto fail;
}
Expand Down Expand Up @@ -712,12 +720,6 @@ uint32_t shoebill_initialize(shoebill_control_t *control)
if (!_load_aux_kernel(control, coff, &pc))
goto fail;

// HACK:
// for (i=0; i<0x4000; i++) {
// uint8_t c = pget(AUX_LOMEM_OFFSET + i, 1);
// pset(i, 1, c);
// }

/*
* Load it all into the internal global shoebill state
* (Can't fail after this point)
Expand Down
2 changes: 1 addition & 1 deletion core/filesystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ static uint8_t ufs_load_cylinder_group(ufs_t *mount, uint32_t frag_offset, ufs_c
uint32_t numfrags = sizeof(ufs_cylinder_group_t) / mount->frag_size;
numfrags += ((sizeof(ufs_cylinder_group_t) % mount->frag_size) != 0);

uint8_t *buf = p_alloc(mount->pool, numfrags * mount->frag_size);
uint8_t *buf = p_alloc(mount->pool, (numfrags+1) * mount->frag_size);
uint32_t i;

for (i=0; i <= numfrags; i++)
Expand Down
Loading

0 comments on commit ba8b9e8

Please sign in to comment.