2006-04-27 00:39:16 +08:00
|
|
|
/* tag: forth source loader
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Stefan Reinauer
|
|
|
|
*
|
|
|
|
* See the file "COPYING" for further information about
|
|
|
|
* the copyright and warranty status of this work.
|
|
|
|
*/
|
|
|
|
|
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"
|
2010-03-27 19:53:59 +08:00
|
|
|
#include "libopenbios/forth_load.h"
|
2006-04-27 00:39:16 +08:00
|
|
|
#define printk printk
|
|
|
|
#define debug printk
|
|
|
|
|
2010-03-27 04:25:04 +08:00
|
|
|
static int fd;
|
2010-03-27 19:53:59 +08:00
|
|
|
static char *forthtext=NULL;
|
2010-03-29 04:55:10 +08:00
|
|
|
|
|
|
|
int is_forth(char *forth)
|
|
|
|
{
|
|
|
|
return (forth[0] == '\\' && forth[1] == ' ');
|
|
|
|
}
|
|
|
|
|
2010-06-27 21:06:38 +08:00
|
|
|
int forth_load(ihandle_t dev)
|
2006-04-27 00:39:16 +08:00
|
|
|
{
|
|
|
|
char magic[2];
|
|
|
|
unsigned long forthsize;
|
|
|
|
int retval = -1;
|
|
|
|
|
2010-03-27 05:17:32 +08:00
|
|
|
/* Mark the saved-program-state as invalid */
|
|
|
|
feval("0 state-valid !");
|
|
|
|
|
2010-06-27 21:06:38 +08:00
|
|
|
fd = open_ih(dev);
|
2010-08-07 20:49:52 +08:00
|
|
|
if (fd == -1) {
|
2006-04-27 00:39:16 +08:00
|
|
|
goto out;
|
2010-08-07 20:49:52 +08:00
|
|
|
}
|
2006-04-27 00:39:16 +08:00
|
|
|
|
2010-03-27 04:25:04 +08:00
|
|
|
if (read_io(fd, magic, 2) != 2) {
|
2006-04-27 00:39:16 +08:00
|
|
|
debug("Can't read magic header\n");
|
|
|
|
retval = LOADER_NOT_SUPPORT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-03-29 04:55:10 +08:00
|
|
|
if (!is_forth(magic)) {
|
2006-04-27 00:39:16 +08:00
|
|
|
debug("No forth source image\n");
|
|
|
|
retval = LOADER_NOT_SUPPORT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-03-27 04:25:04 +08:00
|
|
|
/* Calculate the file size by seeking to the end of the file */
|
|
|
|
seek_io(fd, -1);
|
|
|
|
forthsize = tell(fd);
|
2006-04-27 00:39:16 +08:00
|
|
|
forthtext = malloc(forthsize+1);
|
2010-03-27 04:25:04 +08:00
|
|
|
seek_io(fd, 0);
|
2008-07-08 02:35:51 +08:00
|
|
|
|
2006-04-27 00:39:16 +08:00
|
|
|
printk("Loading forth source ...");
|
2010-03-27 19:53:59 +08:00
|
|
|
if ((size_t)read_io(fd, forthtext, forthsize) != forthsize) {
|
2006-04-27 00:39:16 +08:00
|
|
|
printk("Can't read forth text\n");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
forthtext[forthsize]=0;
|
|
|
|
printk("ok\n");
|
|
|
|
|
2010-03-27 05:17:32 +08:00
|
|
|
// Initialise saved-program-state
|
|
|
|
PUSH((ucell)forthtext);
|
2016-08-26 22:49:38 +08:00
|
|
|
feval("load-state >ls.entry !");
|
2010-03-27 05:17:32 +08:00
|
|
|
PUSH((ucell)forthsize);
|
2016-08-26 22:49:38 +08:00
|
|
|
feval("load-state >ls.file-size !");
|
|
|
|
feval("forth load-state >ls.file-type !");
|
2010-03-27 05:17:32 +08:00
|
|
|
|
|
|
|
feval("-1 state-valid !");
|
|
|
|
|
2006-04-27 00:39:16 +08:00
|
|
|
retval=0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
//if (forthtext)
|
|
|
|
// free(forthtext);
|
|
|
|
return retval;
|
|
|
|
}
|
2010-03-29 07:47:36 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
forth_init_program(void)
|
|
|
|
{
|
|
|
|
// Currently not implemented
|
|
|
|
feval("0 state-valid !");
|
|
|
|
}
|