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/fs/mufs/super.c

77 lines
1.3 KiB

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <bitops.h>
#include <flash.h>
#include <macros.h>
#include <fs/mufs.h>
#include "dir.h"
struct mufs_super {
char magic[4];
uint32_t nblocks;
struct mufs_dtree root;
} __attribute__((packed));
struct mufs *mufs_mount(struct flash_dev *dev)
{
struct mufs_super super;
struct mufs *fs;
if (!dev)
return NULL;
if (flash_read(dev, 0, &super, sizeof super) == 0)
return NULL;
if (memcmp(super.magic, "mufs", 4) != 0)
return NULL;
if (!(fs = malloc(sizeof *fs)))
return NULL;
fs->dev = dev;
fs->nblocks = super.nblocks;
fs->log2_nentries = fs->dev->log2_block_size - ilog2(sizeof(uint32_t));
fs->root.fs = fs;
fs->root.va = offsetof(struct mufs_super, root);
fs->root.file_size = super.root.file_size;
fs->root.root = super.root.root;
fs->root.depth = super.root.depth;
return fs;
}
void mufs_unmount(struct mufs *fs)
{
if (!fs)
return;
if (fs->dev)
flash_release(fs->dev);
free(fs);
}
int mufs_format(struct flash_dev *dev)
{
struct mufs_super super;
memcpy(super.magic, "mufs", 4);
super.nblocks = flash_get_capacity(dev) >> dev->log2_block_size;
super.root.file_size = 0;
super.root.root = 0;
super.root.depth = 0;
if (flash_write(dev, 0, &super, sizeof super) == 0)
return -1;
return 0;
}