Source code for the Trusted Boot Module.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tbm-mcu/source/shell/mufs.c

211 lines
4.0 KiB

#include <ctype.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <flash.h>
#include <ftl.h>
#include <macros.h>
#include <shell.h>
#include <fs/mufs.h>
static void do_mufs_mount(const char *s);
static void do_mufs_format(const char *s);
static void do_mufs_mkdir(const char *s);
static void do_mufs_rmdir(const char *s);
static void do_mufs_stat(const char *stat);
static void do_mufs_cat(const char *s);
static void do_mufs_append(const char *s);
static void do_mufs_mv(const char *s);
static void do_mufs_rm(const char *s);
static void do_mufs_ls(const char *s);
static struct cmd mufs_cmds[] = {
{ "mount", do_mufs_mount },
{ "format", do_mufs_format },
{ "mkdir", do_mufs_mkdir },
{ "rmdir", do_mufs_rmdir },
{ "ls", do_mufs_ls },
{ "stat", do_mufs_stat },
{ "cat", do_mufs_cat },
{ "append", do_mufs_append },
{ "mv", do_mufs_mv, },
{ "rm", do_mufs_rm, },
{ NULL, NULL },
};
extern struct flash_dev *flash;
struct mufs mufs;
static void do_mufs_mount(const char *s)
{
(void)s;
if (!flash) {
fprintf(stderr, "error: no flash device probed.\n");
return;
}
if (mufs_mount(&mufs, flash) < 0) {
fprintf(stderr, "error: unable to mount the filesystem.\n");
return;
}
printf("log2_nentries=%u\n", mufs.log2_nentries);
printf("nentries=%u\n", UINT32_C(1) << mufs.log2_nentries);
}
static void do_mufs_format(const char *s)
{
(void)s;
if (!flash) {
fprintf(stderr, "error: no flash device probed.\n");
return;
}
if (mufs_format(flash) < 0) {
fprintf(stderr, "error: unable to format the flash device.\n");
return;
}
}
static void do_mufs_mkdir(const char *s)
{
if (mufs_mkdir(&mufs, s) < 0) {
fprintf(stderr, "error: unable to create the directory\n");
return;
}
}
static void do_mufs_rmdir(const char *s)
{
if (mufs_rmdir(&mufs, s) < 0) {
fprintf(stderr, "error: unable to remove the directory\n");
return;
}
}
static void do_mufs_stat(const char *s)
{
struct mufs_stat stat;
if (mufs_stat(&mufs, s, &stat) < 0) {
fprintf(stderr, "error: unable to stat the file\n");
return;
}
switch (stat.type) {
case MUFS_DIR: printf(" type: directory\n"); break;
case MUFS_FILE: printf(" type: file\n"); break;
default: return;
}
printf(" file size: %u bytes\n", stat.file_size);
}
static void do_mufs_cat(const char *s)
{
char data[256];
struct mufs_file *file;
if (!(file = mufs_open(&mufs, s, MUFS_READ))) {
fprintf(stderr, "error: unable to open the file\n");
return;
}
while (mufs_read(file, data, sizeof data) != 0) {
fwrite(data, sizeof *data, sizeof data, stdout);
}
mufs_close(file);
}
static void do_mufs_append(const char *s)
{
struct mufs_file *file;
char *path, *line;
if (!(path = malloc(strlen(s) + 2)))
return;
strcpy(path, s);
if (!(line = strchr(path, ' '))) {
fprintf(stderr, "usage: append <path> <line>\n");
goto err_free_path;
}
*line++ = '\0';
if (!(file = mufs_open(&mufs, path, MUFS_WRITE | MUFS_APPEND))) {
fprintf(stderr, "error: unable to open the file\n");
goto err_free_path;
}
strcpy(line + strlen(line), "\n");
mufs_write(file, line, strlen(line));
mufs_close(file);
err_free_path:
free(path);
}
static void do_mufs_mv(const char *s)
{
char *old, *new;
if (!(old = strdup(s)))
return;
if (!(new = strchr(old, ' '))) {
fprintf(stderr, "usage: mv <old> <new>\n");
goto err_free_old;
}
*new++ = '\0';
if (mufs_rename(&mufs, old, new) < 0) {
fprintf(stderr, "error: unable to move the file\n");
goto err_free_old;
}
err_free_old:
free(old);
}
static void do_mufs_rm(const char *s)
{
if (mufs_unlink(&mufs, s) < 0) {
fprintf(stderr, "error: unable to remove the file\n");
return;
}
}
static void do_mufs_ls(const char *s)
{
struct mufs_dirent ent;
struct mufs_dir *dir;
if (!(dir = mufs_opendir(&mufs, s))) {
fprintf(stderr, "error: unable to open the directory\n");
return;
}
while (mufs_readdir(dir, &ent) == 0) {
printf("%s\n", ent.path);
}
mufs_closedir(dir);
}
void do_mufs_cmd(const char *line)
{
cmd_exec(mufs_cmds, line);
}