Skip to content

Commit

Permalink
[sandbox]sync sandbox from x64 to arm
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjunjiang committed Mar 30, 2015
1 parent f7725d1 commit 18c79cc
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 35 deletions.
171 changes: 171 additions & 0 deletions src/arch/arm32/mach-sandbox/driver/sandbox-app.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* drivers/sandbox-app.c
*
* Copyright(c) 2007-2015 Jianjun Jiang <[email protected]>
* Official site: http://xboot.org
* Mobile phone: +86-18665388956
* QQ: 8192542
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/

#include <block/block.h>
#include <sandbox.h>

struct application_t
{
/* The application name */
char * name;

/* The start of application */
void * start;

/* The end of application */
void * end;

/* Busy or not */
bool_t busy;
};

static int application_open(struct block_t * blk)
{
struct application_t * app = (struct application_t *)(blk->priv);

if(app->busy == TRUE)
return -1;

app->busy = TRUE;
return 0;
}

static ssize_t application_read(struct block_t * blk, u8_t * buf, size_t blkno, size_t blkcnt)
{
struct application_t * app = (struct application_t *)(blk->priv);
u8_t * p = (u8_t *)(app->start);
loff_t offset = get_block_offset(blk, blkno);
size_t size = get_block_size(blk) * blkcnt;

if(offset < 0)
return 0;

if(size < 0)
return 0;

memcpy((void *)buf, (const void *)(p + offset), size);
return blkcnt;
}

static ssize_t application_write(struct block_t * blk, const u8_t * buf, size_t blkno, size_t blkcnt)
{
return 0;
}

static int application_close(struct block_t * blk)
{
struct application_t * app = (struct application_t *)(blk->priv);

app->busy = FALSE;
return 0;
}

static bool_t register_application(const char * name, void * start, void * end)
{
struct block_t * blk;
struct application_t * app;
size_t size;

if(!name)
return FALSE;

size = (size_t)(end - start);
size = (size + SZ_512) / SZ_512;
if(size <= 0)
return FALSE;

blk = malloc(sizeof(struct block_t));
if(!blk)
return FALSE;

app = malloc(sizeof(struct application_t));
if(!app)
{
free(blk);
return FALSE;
}

app->name = (char *)name;
app->start = start;
app->end = end;
app->busy = FALSE;

blk->name = app->name;
blk->blksz = SZ_512;
blk->blkcnt = size;
blk->open = application_open;
blk->read = application_read;
blk->write = application_write;
blk->close = application_close;
blk->priv = app;

if(!register_block(blk))
{
free(app);
free(blk);
return FALSE;
}

return TRUE;
}

static bool_t unregister_application(const char * name)
{
struct block_t * blk;
struct application_t * app;

if(!name)
return FALSE;

blk = search_block(name);
if(!blk)
return FALSE;

app = (struct application_t *)(blk->priv);
if(!unregister_block(blk))
return FALSE;

free(app);
free(blk);
return TRUE;
}

static __init void application_init(void)
{
struct sandbox_t * sandbox = sandbox_get();

if(sandbox->application.size > 0)
register_application("application", (void *)(sandbox->application.buffer), (void *)(sandbox->application.buffer + sandbox->application.size));
}

static __exit void application_exit(void)
{
struct sandbox_t * sandbox = sandbox_get();

if(sandbox->application.size > 0)
unregister_application("application");
}

device_initcall(application_init);
device_exitcall(application_exit);
2 changes: 1 addition & 1 deletion src/arch/arm32/mach-sandbox/libsandbox/sandbox-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int sandbox_file_close(int fd)
return ret < 0 ? 0 : 1;
}

int sandbox_sysfs_file_exist(const char * path)
int sandbox_file_exist(const char * path)
{
if(access(path, F_OK) == 0)
return 0;
Expand Down
88 changes: 61 additions & 27 deletions src/arch/arm32/mach-sandbox/libsandbox/sandbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <SDL.h>
#include <sandbox.h>

static const char default_json_config[] =
static char default_config[] =
"{"
"\"console\": {"
"\"in\" : \"stdio\","
Expand All @@ -27,56 +27,69 @@ static const char default_json_config[] =
"\"led\": [null]"
"}";

static struct sandbox_config_t __sandbox_config = {
.json = default_json_config,
.application = NULL,
};
static struct sandbox_t __sandbox;

static char * json_config_file(const char * filename)
static size_t read_file_to_memory(const char * filename, char ** buffer)
{
ssize_t len;
size_t len;
char * buf;
int fd;

if(!filename || !buffer)
return 0;

if(sandbox_file_exist(filename) != 0)
return 0;

fd = sandbox_file_open(filename);
if(fd <= 0)
return NULL;
return 0;

len = sandbox_file_length(fd);
if(len <= 0)
return NULL;
return 0;

buf = malloc(len + 1);
if(!buf)
return NULL;
return 0;
memset(buf, 0, len + 1);

sandbox_file_seek(fd, 0);
sandbox_file_read(fd, buf, len);
return buf;
sandbox_file_close(fd);

*buffer = buf;
return len;
}

static void print_usage(void)
{
printf(
"Usage: xboot [OPTIONS] <filename>\n"
"Usage: xboot [OPTIONS] <application>\n"
"Options:\n"
" --help Print help information\n"
" --config <json> Start xboot with a specified config file with json format\n"
" --config <FILE> Start xboot with a specified config file using json format\n"
);
exit(0);
}

struct sandbox_config_t * sandbox_get_config(void)
struct sandbox_t * sandbox_get(void)
{
return &__sandbox_config;
return &__sandbox;
}

void sandbox_init(int argc, char * argv[])
{
char * jsonfile = "xboot.json";
char * cfgfile = "xboot.json";
char * appfile = 0;
int i, idx = 0;
char * json;
char * buf;
size_t len;

/*
* Clear to zero for __sandbox
*/
memset(&__sandbox, 0, sizeof(struct sandbox_t));

for(i = 1; i < argc; i++)
{
Expand All @@ -86,27 +99,45 @@ void sandbox_init(int argc, char * argv[])
}
else if(!strcmp(argv[i], "--config") && (argc > i + 1))
{
if(sandbox_sysfs_file_exist(argv[++i]) == 0)
jsonfile = argv[i];
if(sandbox_file_exist(argv[++i]) == 0)
cfgfile = argv[i];
else
print_usage();
}
else
{
if((idx == 0) && (sandbox_sysfs_file_exist(argv[i]) == 0))
__sandbox_config.application = argv[i];
if((idx == 0) && (sandbox_file_exist(argv[i]) == 0))
appfile = argv[i];
else
print_usage();
idx++;
}
}

/*
* Read config file with json format
* Read config file
*/
json = json_config_file(jsonfile);
if(json)
__sandbox_config.json = json;
len = read_file_to_memory(cfgfile, &buf);
if(len > 0)
{
__sandbox.config.buffer = buf;
__sandbox.config.size = len;
}
else
{
__sandbox.config.buffer = default_config;
__sandbox.config.size = strlen(default_config);
}

/*
* Read application file
*/
len = read_file_to_memory(appfile, &buf);
if(len > 0)
{
__sandbox.application.buffer = buf;
__sandbox.application.size = len;
}

/*
* Initial sandbox system
Expand All @@ -117,8 +148,11 @@ void sandbox_init(int argc, char * argv[])

void sandbox_exit(void)
{
if(__sandbox_config.json != default_json_config)
free((char *)__sandbox_config.json);
if((__sandbox.config.size > 0) && (__sandbox.config.buffer != default_config))
free(__sandbox.config.buffer);

if(__sandbox.application.size > 0)
free(__sandbox.application.buffer);

/*
* Deinitial sandbox system
Expand Down
16 changes: 11 additions & 5 deletions src/arch/arm32/mach-sandbox/libsandbox/sandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
/*
* Sandbox interface
*/
struct sandbox_config_t {
const char * json;
const char * application;
struct sandbox_t {
struct {
char * buffer;
size_t size;
} config;
struct {
char * buffer;
size_t size;
} application;
};
struct sandbox_config_t * sandbox_get_config(void);
struct sandbox_t * sandbox_get(void);
void sandbox_init(int argc, char * argv[]);
void sandbox_exit(void);

Expand All @@ -27,7 +33,7 @@ ssize_t sandbox_console_write(void * buf, size_t count);
*/
int sandbox_file_open(const char * path);
int sandbox_file_close(int fd);
int sandbox_sysfs_file_exist(const char * path);
int sandbox_file_exist(const char * path);
ssize_t sandbox_file_read(int fd, void * buf, size_t count);
ssize_t sandbox_file_read_nonblock(int fd, void * buf, size_t count);
ssize_t sandbox_file_write(int fd, const void * buf, size_t count);
Expand Down
4 changes: 2 additions & 2 deletions src/arch/arm32/mach-sandbox/resource/res-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ static void json_resource_led(json_value * value)

static __init void resource_json_init(void)
{
struct sandbox_config_t * cfg = sandbox_get_config();
json_value * value = json_parse(cfg->json, strlen(cfg->json));
struct sandbox_t * sandbox = sandbox_get();
json_value * value = json_parse(sandbox->config.buffer, sandbox->config.size);
int i;

if(value && (value->type == json_object))
Expand Down

0 comments on commit 18c79cc

Please sign in to comment.