Skip to content

Commit

Permalink
fix(lab6): imporve lab6
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyCrane committed Dec 9, 2024
1 parent 968df92 commit 335677a
Show file tree
Hide file tree
Showing 35 changed files with 1,019 additions and 1,204 deletions.
740 changes: 351 additions & 389 deletions docs/lab6.md

Large diffs are not rendered by default.

59 changes: 0 additions & 59 deletions src/lab6/Makefile

This file was deleted.

10 changes: 0 additions & 10 deletions src/lab6/arch/riscv/Makefile

This file was deleted.

19 changes: 0 additions & 19 deletions src/lab6/arch/riscv/include/sbi.h

This file was deleted.

Binary file added src/lab6/disk.img.zip
Binary file not shown.
6 changes: 2 additions & 4 deletions src/lab6/fs/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
# ASM_SRC = $(filter-out ramdisk.S, $(sort $(wildcard *.S)))
C_SRC = $(sort $(wildcard *.c))
OBJ = $(patsubst %.S,%.o,$(ASM_SRC)) $(patsubst %.c,%.o,$(C_SRC))
OBJ = $(patsubst %.c,%.o,$(C_SRC))

all:$(OBJ)

%.o:%.c
${GCC} ${CFLAG} -c $<

clean:
$(shell rm *.o 2>/dev/null)
$(shell rm *.o 2>/dev/null)
72 changes: 10 additions & 62 deletions src/lab6/fs/fat32.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include <fat32.h>
#include <printk.h>
#include <virtio.h>
#include <string.h>
#include <mbr.h>
#include <mm.h>
#include "fat32.h"
#include "printk.h"
#include "virtio.h"
#include "string.h"
#include "mbr.h"
#include "mm.h"

struct fat32_bpb fat32_header;

struct fat32_volume fat32_volume;

uint8_t fat32_buf[VIRTIO_BLK_SECTOR_SIZE];
Expand All @@ -30,9 +29,6 @@ void fat32_init(uint64_t lba, uint64_t size) {
fat32_volume.sec_per_cluster = 0/* to calculate */;
fat32_volume.first_data_sec = 0/* to calculate */;
fat32_volume.fat_sz = 0/* to calculate */;

virtio_blk_read_sector(fat32_volume.first_data_sec, fat32_buf); // Get the root directory
struct fat32_dir_entry *dir_entry = (struct fat32_dir_entry *)fat32_buf;
}

int is_fat32(uint64_t lba) {
Expand All @@ -43,7 +39,7 @@ int is_fat32(uint64_t lba) {
return 1;
}

int next_slash(const char* path) {
int next_slash(const char* path) { // util function to be used in fat32_open_file
int i = 0;
while (path[i] != '\0' && path[i] != '/') {
i++;
Expand All @@ -54,7 +50,7 @@ int next_slash(const char* path) {
return i;
}

void to_upper_case(char *str) {
void to_upper_case(char *str) { // util function to be used in fat32_open_file
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] -= 32;
Expand Down Expand Up @@ -87,60 +83,12 @@ uint64_t fat32_table_sector_of_cluster(uint32_t cluster) {
return fat32_volume.first_fat_sec + cluster / (VIRTIO_BLK_SECTOR_SIZE / sizeof(uint32_t));
}

int64_t fat32_extend_filesz(struct file* file, uint64_t new_size) {
uint64_t sector = cluster_to_sector(file->fat32_file.dir.cluster) + file->fat32_file.dir.index / FAT32_ENTRY_PER_SECTOR;

virtio_blk_read_sector(sector, fat32_table_buf);
uint32_t index = file->fat32_file.dir.index % FAT32_ENTRY_PER_SECTOR;
uint32_t original_file_len = ((struct fat32_dir_entry *)fat32_table_buf)[index].size;
((struct fat32_dir_entry *)fat32_table_buf)[index].size = new_size;

virtio_blk_write_sector(sector, fat32_table_buf);

uint32_t clusters_required = new_size / (fat32_volume.sec_per_cluster * VIRTIO_BLK_SECTOR_SIZE);
uint32_t clusters_original = original_file_len / (fat32_volume.sec_per_cluster * VIRTIO_BLK_SECTOR_SIZE);
uint32_t new_clusters = clusters_required - clusters_original;

uint32_t cluster = file->fat32_file.cluster;
while (1) {
uint32_t next_cluster_number = next_cluster(cluster);
if (next_cluster_number >= 0x0ffffff8) {
break;
}
cluster = next_cluster_number;
}

for (int i = 0; i < new_clusters; i++) {
uint32_t cluster_to_append;
for (int j = 2; j < fat32_volume.fat_sz * VIRTIO_BLK_SECTOR_SIZE / sizeof(uint32_t); j++) {
if (next_cluster(j) == 0) {
cluster_to_append = j;
break;
}
}
uint64_t fat_sector = fat32_table_sector_of_cluster(cluster);
virtio_blk_read_sector(fat_sector, fat32_table_buf);
uint32_t index_in_sector = cluster * 4 % VIRTIO_BLK_SECTOR_SIZE;
*(uint32_t*)(fat32_table_buf + index_in_sector) = cluster_to_append;
virtio_blk_write_sector(fat_sector, fat32_table_buf);
cluster = cluster_to_append;
}

uint64_t fat_sector = fat32_table_sector_of_cluster(cluster);
virtio_blk_read_sector(fat_sector, fat32_table_buf);
uint32_t index_in_sector = cluster * 4 % VIRTIO_BLK_SECTOR_SIZE;
*(uint32_t*)(fat32_table_buf + index_in_sector) = 0x0fffffff;
virtio_blk_write_sector(fat_sector, fat32_table_buf);

return 0;
}

int64_t fat32_read(struct file* file, void* buf, uint64_t len) {
return 0;
/* todo: read content to buf, and return read length */
return 0;
}

int64_t fat32_write(struct file* file, const void* buf, uint64_t len) {
return 0;
/* todo: fat32_write */
return 0;
}
7 changes: 0 additions & 7 deletions src/lab6/fs/fs.S

This file was deleted.

46 changes: 46 additions & 0 deletions src/lab6/fs/fs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "fs.h"
#include "vfs.h"
#include "mm.h"
#include "string.h"
#include "printk.h"
#include "fat32.h"

struct files_struct *file_init() {
// todo: alloc pages for files_struct, and initialize stdin, stdout, stderr
struct files_struct *ret = NULL;
return ret;
}

uint32_t get_fs_type(const char *filename) {
uint32_t ret;
if (memcmp(filename, "/fat32/", 7) == 0) {
ret = FS_TYPE_FAT32;
} else if (memcmp(filename, "/ext2/", 6) == 0) {
ret = FS_TYPE_EXT2;
} else {
ret = -1;
}
return ret;
}

int32_t file_open(struct file* file, const char* path, int flags) {
file->opened = 1;
file->perms = flags;
file->cfo = 0;
file->fs_type = get_fs_type(path);
memcpy(file->path, path, strlen(path) + 1);

if (file->fs_type == FS_TYPE_FAT32) {
file->lseek = fat32_lseek;
file->write = fat32_write;
file->read = fat32_read;
file->fat32_file = fat32_open_file(path);
// todo: check if fat32_file is valid (i.e. successfully opened) and return
} else if (file->fs_type == FS_TYPE_EXT2) {
printk(RED "Unsupport ext2\n" CLEAR);
return -1;
} else {
printk(RED "Unknown fs type: %s\n" CLEAR, path);
return -1;
}
}
13 changes: 5 additions & 8 deletions src/lab6/fs/mbr.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <mbr.h>
#include <virtio.h>
#include <fat32.h>
#include "mbr.h"
#include "virtio.h"
#include "fat32.h"

uint8_t mbr_buf[VIRTIO_BLK_SECTOR_SIZE];

struct partition_info partitions[MBR_MAX_PARTITIONS];

void mbr_init() {
Expand All @@ -12,8 +11,6 @@ void mbr_init() {
for (int i = 0; i < 4; i++) {
if (mbr->partition_table[i].type == 0x83) {
uint32_t lba = mbr->partition_table[i].lba_first_sector;
// virtio_blk_read_sector(lba, mbr_buf);
// printk("[S] Detect partition%d offset: %d, len:%d, type:%x\n", i+1, lba, mbr->partition_table[i].sector_count, mbr->partition_table[i].type);
partition_init(i + 1, lba, mbr->partition_table[i].sector_count);
}
}
Expand All @@ -22,6 +19,6 @@ void mbr_init() {
void partition_init(int partion_number, uint64_t start_lba, uint64_t sector_count) {
if (is_fat32(start_lba)) {
fat32_init(start_lba, sector_count);
printk("[S] fat32 partition init done!\n", partion_number);
printk("...fat32 partition #%d init done!\n", partion_number);
}
}
}
Loading

0 comments on commit 335677a

Please sign in to comment.