From bf5f6e3ea7d1a60098678d3d1fb08a8d1e99767e Mon Sep 17 00:00:00 2001 From: Robbe Van Herck Date: Fri, 3 Jan 2020 15:14:29 +0100 Subject: [PATCH] Implement SFS Superblock --- docs/bootloader.md | 15 +++++++------ helpers/make_bin.c | 54 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 15 deletions(-) diff --git a/docs/bootloader.md b/docs/bootloader.md index 5efc1e2..402cc1c 100644 --- a/docs/bootloader.md +++ b/docs/bootloader.md @@ -29,13 +29,14 @@ ## Hard Drive layout -| start | end | use | -|--------|--------|--------------------------------------------| -| 0x0000 | 0x01ff | Bootloader code (including SFS superblock) | -| 0x0200 | 0x81ff | SFS reserved area (kernel ELF file) | -| 0x8200 | 0x???? | SFS data area | -| 0x???? | 0x???? | SFS free area | -| 0x???? | end | SFS index area | +| start | end | use | +|------------|------------|--------------------------------------------| +| 0x00000000 | 0x000001ff | Bootloader code (including SFS superblock) | +| 0x00000200 | 0x000081ff | SFS reserved area (kernel ELF file) | +| 0x00008200 | 0x400081ff | SFS data area (1GiB, 1048576 SFS blocks) | +| 0x40008200 | 0x5fffffff | SFS free area | +| 0x60000000 | 0x7fffffff | SFS index area | +| 0x80000000 | end | Unused | ## Known issues diff --git a/helpers/make_bin.c b/helpers/make_bin.c index 99249f1..1907bad 100644 --- a/helpers/make_bin.c +++ b/helpers/make_bin.c @@ -1,27 +1,65 @@ #include #include #include +#include #define KERNEL_SECTORS 64 +#define KiB 1024 +#define MiB (1024 * KiB) +#define GiB (1024 * MiB) + +#define DISK_SIZE (1 * GiB) + +#define BLOCK_SIZE_ARG 1 +#define BLOCK_SIZE (1 << (BLOCK_SIZE_ARG + 7)) + +#define RESERVED_SIZE (32 * KiB) +#define NUM_RESERVED_BLOCKS (RESERVED_SIZE / BLOCK_SIZE) + +#define DATA_SIZE (1 * GiB) +#define NUM_DATA_BLOCKS (DATA_SIZE / BLOCK_SIZE) + +#define INDEX_SIZE (128 * MiB) + int main(int argc, char** argv) { FILE* result_file = fopen("./target/boot.bin", "w+b"); FILE* bootloader_file = fopen("./target/bootloader/bootloader.bin", "rb"); // ------- BOOTLOADER + SFS HEADER ------- - char* buffer = calloc(512, sizeof(char)); + uint8_t* buffer = calloc(512, sizeof(uint8_t)); int i = 0; - int curr_char; + int curr_byte; - while ((curr_char = fgetc(bootloader_file)) != EOF) { - buffer[i] = (char) curr_char; + while ((curr_byte = fgetc(bootloader_file)) != EOF) { + buffer[i] = (uint8_t) curr_byte; i++; } - // TODO edit SFS header + // SFS Superblock + time_t current_time = time(NULL); + uint64_t sfs_timestamp = (uint64_t) current_time * 65536; - buffer[0x194] = 0xff; + (*(uint64_t*) (buffer + 0x194)) = sfs_timestamp; + (*(uint64_t*) (buffer + 0x19C)) = NUM_DATA_BLOCKS; + (*(uint64_t*) (buffer + 0x1A4)) = INDEX_SIZE; + (*(uint32_t*) (buffer + 0x1AC)) = 0x10534653; // SFS magic + version + (*(uint64_t*) (buffer + 0x1B0)) = ((uint64_t) DISK_SIZE) / BLOCK_SIZE; + + // The superblock may be bigger than one block + if (BLOCK_SIZE < 512) { + (*(uint32_t*) (buffer + 0x1B8)) = NUM_RESERVED_BLOCKS + 2; + } else { + (*(uint32_t*) (buffer + 0x1B8)) = NUM_RESERVED_BLOCKS + 1; + } + buffer[0x1BC] = BLOCK_SIZE_ARG; + + uint8_t sum = 0; + for (int i = 0x1AC; i < 0x1BD; i++) { + sum += buffer[i]; + } + buffer[0x1BD] = -sum; for (int i = 0; i < 512; i++) { @@ -37,8 +75,8 @@ int main(int argc, char** argv) { int bytes_written = 0; - while ((curr_char = fgetc(kernel_file)) != EOF) { - fputc((char) curr_char, result_file); + while ((curr_byte = fgetc(kernel_file)) != EOF) { + fputc((char) curr_byte, result_file); bytes_written++; }