-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a filesystem which is backed by the host's filesystem. It is modeled off of sandboxfs, which has very similar aims. Semihosting doesn't support listing directories (except with SYS_SYSTEM), so neither do we. it's possible to optimize a bit for the common case of reading a whole file by omitting a call to smh_seek, but this is left as a future optimization. Signed-off-by: Sean Anderson <[email protected]>
- Loading branch information
1 parent
8e1c9fe
commit f676b45
Showing
6 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// SPDX-License-Identifier: GPL-2.0+ | ||
/* | ||
* Copyright (C) 2022, Sean Anderson <[email protected]> | ||
* Copyright (c) 2012, Google Inc. | ||
*/ | ||
|
||
#include <common.h> | ||
#include <fs.h> | ||
#include <malloc.h> | ||
#include <os.h> | ||
#include <semihosting.h> | ||
#include <semihostingfs.h> | ||
|
||
int smh_fs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info) | ||
{ | ||
/* | ||
* Only accept a NULL struct blk_desc for the semihosting, which is when | ||
* hostfs interface is used | ||
*/ | ||
return !!rbdd; | ||
} | ||
|
||
static int smh_fs_read_at(const char *filename, loff_t pos, void *buffer, | ||
loff_t maxsize, loff_t *actread) | ||
{ | ||
long fd, size, ret; | ||
|
||
fd = smh_open(filename, MODE_READ | MODE_BINARY); | ||
if (fd < 0) | ||
return fd; | ||
ret = smh_seek(fd, pos); | ||
if (ret < 0) { | ||
smh_close(fd); | ||
return ret; | ||
} | ||
if (!maxsize) { | ||
size = smh_flen(fd); | ||
if (ret < 0) { | ||
smh_close(fd); | ||
return size; | ||
} | ||
|
||
maxsize = size; | ||
} | ||
|
||
size = smh_read(fd, buffer, maxsize); | ||
smh_close(fd); | ||
if (size < 0) | ||
return size; | ||
|
||
*actread = size; | ||
return 0; | ||
} | ||
|
||
static int smh_fs_write_at(const char *filename, loff_t pos, void *buffer, | ||
loff_t towrite, loff_t *actwrite) | ||
{ | ||
long fd, size, ret; | ||
|
||
fd = smh_open(filename, MODE_READ | MODE_BINARY | MODE_PLUS); | ||
if (fd < 0) | ||
return fd; | ||
ret = smh_seek(fd, pos); | ||
if (ret < 0) { | ||
smh_close(fd); | ||
return ret; | ||
} | ||
|
||
ret = smh_write(fd, buffer, towrite, &size); | ||
smh_close(fd); | ||
*actwrite = size; | ||
return ret; | ||
} | ||
|
||
int smh_fs_size(const char *filename, loff_t *result) | ||
{ | ||
long fd, size; | ||
|
||
fd = smh_open(filename, MODE_READ | MODE_BINARY); | ||
if (fd < 0) | ||
return fd; | ||
|
||
size = smh_flen(fd); | ||
smh_close(fd); | ||
|
||
if (size < 0) | ||
return size; | ||
|
||
*result = size; | ||
return 0; | ||
} | ||
|
||
int smh_fs_read(const char *filename, void *buf, loff_t offset, loff_t len, | ||
loff_t *actread) | ||
{ | ||
int ret; | ||
|
||
ret = smh_fs_read_at(filename, offset, buf, len, actread); | ||
if (ret) | ||
printf("** Unable to read file %s **\n", filename); | ||
|
||
return ret; | ||
} | ||
|
||
int smh_fs_write(const char *filename, void *buf, loff_t offset, | ||
loff_t len, loff_t *actwrite) | ||
{ | ||
int ret; | ||
|
||
ret = smh_fs_write_at(filename, offset, buf, len, actwrite); | ||
if (ret) | ||
printf("** Unable to write file %s **\n", filename); | ||
|
||
return ret; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* SPDX-License-Identifier: GPL-2.0+ */ | ||
/* | ||
* Copyright (C) 2022, Sean Anderson <[email protected]> | ||
* Copyright (c) 2012, Google Inc. | ||
*/ | ||
|
||
#ifndef __SEMIHOSTING_FS__ | ||
#define __SEMIHOSTING_FS__ | ||
|
||
struct blk_desc; | ||
struct disk_partition; | ||
|
||
int smh_fs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info); | ||
void smh_fs_close(void); | ||
int smh_fs_size(const char *filename, loff_t *size); | ||
int smh_fs_read(const char *filename, void *buf, loff_t offset, loff_t len, | ||
loff_t *actread); | ||
int smh_fs_write(const char *filename, void *buf, loff_t offset, | ||
loff_t len, loff_t *actwrite); | ||
|
||
#endif |