From adcda5689472a9f67cff5a5e8bec75eb8b990913 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Thu, 23 Mar 2017 15:18:13 +0000 Subject: [PATCH] fs: add initial code to create the partition table --- Makefile | 1 + include/fs/part.h | 3 +++ source/fs/part.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 include/fs/part.h create mode 100644 source/fs/part.c diff --git a/Makefile b/Makefile index ab74411..f63e21e 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ obj-y += source/shell/flash.o obj-y += source/core/flash.o obj-y += source/fs/block.o +obj-y += source/fs/part.o obj = $(addprefix $(BUILD)/, $(obj-y)) diff --git a/include/fs/part.h b/include/fs/part.h new file mode 100644 index 0000000..77e6870 --- /dev/null +++ b/include/fs/part.h @@ -0,0 +1,3 @@ +#pragma once + +int part_create(struct flash_dev *dev); diff --git a/source/fs/part.c b/source/fs/part.c new file mode 100644 index 0000000..9fa51b5 --- /dev/null +++ b/source/fs/part.c @@ -0,0 +1,40 @@ +#include +#include +#include + +#include +#include + +#include +#include + +struct part_hdr { + char magic[8]; + char rsv[24]; +} __attribute__((packed)); + +struct part_entry { + char label[16]; + uint32_t start, end; + uint32_t flags; + uint32_t rsv; +} __attribute__((packed)); + +#define PART_MAGIC "FL-PART\x7f" + +int part_create(struct flash_dev *dev) +{ + struct part_hdr hdr; + + if (block_is_erased(dev, 0, 4 * KIB) != 0 && + flash_erase(dev, 0, 4 * KIB) != 0) + return -1; + + memset(&hdr, 0, sizeof hdr); + memcpy(hdr.magic, PART_MAGIC, sizeof hdr.magic); + + if (flash_write(dev, 0, &hdr, sizeof hdr) != 0) + return -1; + + return 0; +}