2008-07-21 15:50:53 +08:00
|
|
|
/*
|
|
|
|
* FCode boot loader
|
|
|
|
*/
|
|
|
|
|
2010-03-15 01:19:58 +08:00
|
|
|
#include "config.h"
|
2010-03-14 22:21:02 +08:00
|
|
|
#include "kernel/kernel.h"
|
2010-03-14 23:05:53 +08:00
|
|
|
#include "libopenbios/bindings.h"
|
2010-03-15 00:09:44 +08:00
|
|
|
#include "libopenbios/sys_info.h"
|
2010-03-27 04:25:04 +08:00
|
|
|
#include "libc/diskio.h"
|
2008-07-21 15:50:53 +08:00
|
|
|
#include "boot.h"
|
|
|
|
#define printf printk
|
|
|
|
#define debug printk
|
|
|
|
|
2010-03-27 04:25:04 +08:00
|
|
|
static int fd;
|
|
|
|
|
2008-11-30 19:54:01 +08:00
|
|
|
int fcode_load(const char *filename)
|
2008-07-21 15:50:53 +08:00
|
|
|
{
|
|
|
|
int retval = -1;
|
|
|
|
uint8_t fcode_header[8];
|
|
|
|
unsigned long start, size;
|
|
|
|
unsigned int offset;
|
|
|
|
|
2010-03-27 05:17:32 +08:00
|
|
|
/* Mark the saved-program-state as invalid */
|
|
|
|
feval("0 state-valid !");
|
|
|
|
|
2010-03-27 04:25:04 +08:00
|
|
|
fd = open_io(filename);
|
|
|
|
if (!fd)
|
2008-07-21 15:50:53 +08:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (offset = 0; offset < 16 * 512; offset += 512) {
|
2010-03-27 04:25:04 +08:00
|
|
|
seek_io(fd, offset);
|
|
|
|
if (read_io(fd, &fcode_header, sizeof(fcode_header))
|
2008-07-21 15:50:53 +08:00
|
|
|
!= sizeof(fcode_header)) {
|
|
|
|
debug("Can't read FCode header from file %s\n", filename);
|
|
|
|
retval = LOADER_NOT_SUPPORT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
switch (fcode_header[0]) {
|
|
|
|
case 0xf0: // start0
|
|
|
|
case 0xf1: // start1
|
|
|
|
case 0xf2: // start2
|
|
|
|
case 0xf3: // start4
|
|
|
|
case 0xfd: // version1
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Not a bootable FCode image\n");
|
|
|
|
retval = LOADER_NOT_SUPPORT;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
found:
|
|
|
|
size = (fcode_header[4] << 24) | (fcode_header[5] << 16) |
|
|
|
|
(fcode_header[6] << 8) | fcode_header[7];
|
|
|
|
|
|
|
|
start = 0x4000;
|
|
|
|
|
|
|
|
printf("Loading FCode image...\n");
|
|
|
|
|
2010-03-27 04:25:04 +08:00
|
|
|
seek_io(fd, offset + sizeof(fcode_header));
|
2008-07-21 15:50:53 +08:00
|
|
|
|
2010-03-27 04:25:04 +08:00
|
|
|
if ((unsigned long)read_io(fd, (void *)start, size) != size) {
|
2008-07-21 15:50:53 +08:00
|
|
|
printf("Can't read file (size 0x%lx)\n", size);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Loaded %lu bytes\n", size);
|
|
|
|
debug("entry point is %#lx\n", start);
|
2010-03-27 05:17:32 +08:00
|
|
|
|
|
|
|
// Initialise saved-program-state
|
|
|
|
PUSH(start);
|
|
|
|
feval("saved-program-state >sps.entry !");
|
|
|
|
PUSH(size);
|
|
|
|
feval("saved-program-state >sps.file-size !");
|
|
|
|
feval("fcode saved-program-state >sps.file-type !");
|
|
|
|
|
2010-03-27 06:33:50 +08:00
|
|
|
feval("-1 state-valid !");
|
2008-07-21 15:50:53 +08:00
|
|
|
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
out:
|
2010-03-27 04:25:04 +08:00
|
|
|
close_io(fd);
|
2008-07-21 15:50:53 +08:00
|
|
|
return retval;
|
|
|
|
}
|