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

91 lines
1.7 KiB

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <bitops.h>
#include <flash.h>
#include <macros.h>
#include <mufs.h>
#include "block.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 fs;
struct mufs_super super;
uint8_t log2_bits_per_block = dev->log2_block_size + ilog2(8);
size_t nbitmap_size;
uint32_t block;
fs.dev = dev;
fs.nblocks = flash_get_capacity(dev) >> dev->log2_block_size;
nbitmap_size = align_up(fs.nblocks, log2_bits_per_block) >>
log2_bits_per_block;
flash_erase(dev, 1, nbitmap_size);
for (block = 0; block < 1 + nbitmap_size; ++block)
mufs_mark_block(&fs, block, 1);
memcpy(super.magic, "mufs", 4);
super.nblocks = fs.nblocks;
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 flash_sync(dev);
}