From 08a9d7442e333cb25dbcc2ee4e539ac6ff93c041 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Mon, 8 Nov 2021 22:33:15 +0100 Subject: [PATCH] Add user file size before the file As a quick fix before FS support --- Makefile | 3 ++- core/main.c | 28 +++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 5d88075..7d472df 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,8 @@ disk.img: disk.sfdisk userspace sfdisk $@ < $< # Before having filesystem support, just dump the user prog into the first partition strip userspace/user -o userspace/user.strip - dd if=userspace/user.strip of=disk.img seek=2048 bs=512 + $(eval file_size:=$(shell du -b userspace/user.strip|awk '{print $$1}' | xargs printf "%016d" )) \ + sed '1s/^/$(file_size)/' userspace/user.strip | dd of=disk.img seek=2048 bs=512 # NASM without preprocessing %.o:%.asm diff --git a/core/main.c b/core/main.c index e90fa95..d296eaa 100644 --- a/core/main.c +++ b/core/main.c @@ -215,6 +215,8 @@ static uaddr_t loadElfProg(const char *prog) return elf_hdr->e_entry; } +#define FILE_HEADER_SIZE 16 +#define FILE_MAX_SIZE 64 void loadUserSpace() { struct ata_partition *part = ATAGetPartition(0); @@ -224,13 +226,33 @@ void loadUserSpace() return; } - char *buf = malloc(26 * 512); + char *buf = malloc(FILE_MAX_SIZE * 512); if (buf == NULL) { printf("ENOMEM\n"); return; } - if (ATAReadPartitionSector(part, 0, 26, buf)) { + if (ATAReadPartitionSector(part, 0, 1, buf)) { + printf("Fail to read from disk\n"); + return; + } + + int sectorToRead; + { + char size[FILE_HEADER_SIZE + 1]; + memcpy(size, buf, FILE_HEADER_SIZE); + size[FILE_HEADER_SIZE] = '\0'; + int sizeInt = atoi(size); + sectorToRead = DIV_ROUND_UP(sizeInt, DISK_SECTOR_SIZE) - 1; + } + + if (sectorToRead > FILE_MAX_SIZE - 1) { + printf("File too long"); + return; + } + + if (ATAReadPartitionSector(part, 1, sectorToRead, + buf + DISK_SECTOR_SIZE)) { printf("Fail to read from disk\n"); return; } @@ -238,7 +260,7 @@ void loadUserSpace() struct process *proc = processCreate("UserSpace"); threadChangeCurrentContext(processGetMMUContext(proc)); - uaddr_t prog = loadElfProg(buf); + uaddr_t prog = loadElfProg(buf + FILE_HEADER_SIZE); if (prog == (uaddr_t)NULL) { free(buf); return;