diff --git a/README.md b/README.md index 27ed227d7..0f625fed2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ | Github Account | Student ID | Name | |----------------|------------|---------------| -| hellcwang | 310554049 | Chia-Wei Wang | +| | 310554049 | | ## Requirements diff --git a/lab8/Makefile b/lab8/Makefile index 21eb07cc9..8dff3e3da 100644 --- a/lab8/Makefile +++ b/lab8/Makefile @@ -20,7 +20,7 @@ all: clean kernel8.img initramfs.cpio #initramfs.cpio: initramfs # cd initramfs; find . -print -depth | cpio -H newc -o > $@; mv $@ .. -initramfs.cpio: vfs1.img +initramfs.cpio: vfs2.img ls $^ | cpio -H newc -o > $@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.S diff --git a/lab8/config.txt b/lab8/config.txt index 10286b5bb..cb0a7cd94 100644 --- a/lab8/config.txt +++ b/lab8/config.txt @@ -1 +1,2 @@ -initramfs initramfs.cpio 0x8000000 +initramfs ramfs.cpi 0x8000000 +device_tree=dt.dtb diff --git a/lab8/include/fatfs.h b/lab8/include/fatfs.h index e255e395c..5941e4b59 100644 --- a/lab8/include/fatfs.h +++ b/lab8/include/fatfs.h @@ -14,24 +14,24 @@ typedef struct { int size; size_t Eof; void *dirs; - void *data; + uint32_t data; // The sector of the data } FsAttr; // For the directory typedef struct { - char name[8]; // SFN care tof the first byte - char ext[3]; // Extension pad with " " - uint8_t attr; // File attribute Normal -> 0 - uint8_t attr2; // default: 0 - uint8_t createTimeMs; // ms of the createTime - uint16_t createTime; // 15:13 -> Hours, 10:5 -> Min, 4:0 -> seconds - uint16_t createTimeDate; // 15:13 -> Year, 10:5 -> Mon, 4:0 -> Day + char name[8]; // SFN care tof the first byte + char ext[3]; // Extension pad with " " + uint8_t attr; // File attribute Normal -> 0 + uint8_t attr2; // default: 0 + uint8_t createTimeMs; // ms of the createTime + uint16_t createTime; // 15:13 -> Hours, 10:5 -> Min, 4:0 -> seconds + uint16_t createTimeDate; // 15:13 -> Year, 10:5 -> Mon, 4:0 -> Day uint16_t ownerID; - uint16_t highAddr; // High two bytes of first cluster + uint16_t highAddr; // High two bytes of first cluster uint16_t modifiedTime; uint16_t modifiedDate; - uint16_t lowAddr; // Low two bytes of first cluster - uint32_t size; // Total size of file + uint16_t lowAddr; // Low two bytes of first cluster + uint32_t size; // Total size of file } Entry; int fatfs_init(struct filesystem *fs, struct mount *m); @@ -49,6 +49,6 @@ int fatfs_read(struct file *f, void *buf, size_t len); int fatfs_write(struct file *f, const void *buf, size_t len); int fatfs_open(struct vnode *, struct file **target); int fatfs_close(struct file *f); - +void fatfs_sync(); #endif diff --git a/lab8/include/framefs.h b/lab8/include/framefs.h index 3817a5cb4..9bd3beeb6 100644 --- a/lab8/include/framefs.h +++ b/lab8/include/framefs.h @@ -7,11 +7,11 @@ #include "uart.h" #include "vfs.h" -struct framebuffer_info{ - unsigned int width; - unsigned int height; - unsigned int pitch; - unsigned int isrgb; +struct framebuffer_info { + unsigned int width; + unsigned int height; + unsigned int pitch; + unsigned int isrgb; }; int framefs_init(struct filesystem *fs, struct mount *m); diff --git a/lab8/include/ramfs.h b/lab8/include/ramfs.h index 1bae30cbb..366ed420c 100644 --- a/lab8/include/ramfs.h +++ b/lab8/include/ramfs.h @@ -31,5 +31,4 @@ int ramfs_write(struct file *f, const void *buf, size_t len); int ramfs_open(struct vnode *, struct file **target); int ramfs_close(struct file *f); - #endif diff --git a/lab8/include/sd.h b/lab8/include/sd.h index 2ac43e9d9..0535b35b9 100644 --- a/lab8/include/sd.h +++ b/lab8/include/sd.h @@ -1,7 +1,7 @@ #ifndef SD_H #define SD_H -void readblock(int, void*); -void writeblock(int, void*); +void readblock(int, void *); +void writeblock(int, void *); void sd_init(); #endif diff --git a/lab8/include/syscall.h b/lab8/include/syscall.h index 3eee10674..1cfe7e34d 100644 --- a/lab8/include/syscall.h +++ b/lab8/include/syscall.h @@ -40,6 +40,7 @@ int sys_mount(const char *src, const char *target, const char *filesystem, unsigned long, const void *); int sys_chdir(const char *path); long sys_lseek64(int fd, long offset, int whence); -int sys_ioctl(int fd, unsigned long request, void* fb_info); +int sys_ioctl(int fd, unsigned long request, void *fb_info); +void sys_sync(); #endif // SYSCALL_H diff --git a/lab8/include/vfs.h b/lab8/include/vfs.h index 041c49032..6d68f810b 100644 --- a/lab8/include/vfs.h +++ b/lab8/include/vfs.h @@ -12,19 +12,19 @@ typedef enum { /* THis is the INODE*/ struct vnode { - struct vnode *parent; // Parent DIR - struct mount *mount; // The mount node of the FS + struct vnode *parent; // Parent DIR + struct mount *mount; // The mount node of the FS struct vnode_operations *v_ops; // Vnode operations - struct file_operations *f_ops; // File operations - char name[16]; // Name of the node. - FsTy type; // Type of the vnode - void *internal; // The for each file defined + struct file_operations *f_ops; // File operations + char name[16]; // Name of the node. + FsTy type; // Type of the vnode + void *internal; // The for each file defined }; /* Mount Node of the FS*/ struct mount { - struct vnode *root; // The root of this fs - struct filesysytem *system; // Point to the file system + struct vnode *root; // The root of this fs + struct filesysytem *system; // Point to the file system }; /* Opening a file need to do following things. * 1. Allocat a file strucure. @@ -32,12 +32,13 @@ struct mount { * descriptor table for the process. */ struct file { - struct vnode *vnode; // The original vnode of file - size_t f_pos; // Current position of the file - size_t Eof; // End of the FILE + struct vnode *vnode; // The original vnode of file + size_t f_pos; // Current position of the file + size_t Eof; // End of the FILE struct file_operations *f_ops; int flags; - void *data; // The data of this file + void *data; // The data of this file + int dirty; }; /* This structure contain the filesystem name such as Ext3 @@ -66,6 +67,12 @@ struct file_operations { int (*close)(struct file *); }; +typedef struct FIle_list { + struct FIle_list *prev; + struct FIle_list *next; + struct file *f; +} file_list; + // struct mount* rootfs; /* From linux vfs.rst It sys. @@ -87,7 +94,6 @@ int vfs_mount(const char *target, const char *filesystem); */ int vfs_lookup(const char *pathname, struct vnode **target, struct vnode *root); - #define O_CREAT 0b0100 #define SEEK_SET 0 diff --git a/lab8/src/fatfs.c b/lab8/src/fatfs.c index 6cf72a418..53867fcdc 100644 --- a/lab8/src/fatfs.c +++ b/lab8/src/fatfs.c @@ -1,6 +1,5 @@ #include "fatfs.h" #include "heap.h" -#include "initrd.h" #include "mem.h" #include "str.h" #include "uart.h" @@ -13,57 +12,100 @@ extern struct vnode *fsRoot; static int BPB; static uint64_t partition_start; static uint64_t reserved_blocks; -static uint64_t start_dirs; +static uint64_t root_dir; +static uint64_t fat_start; +static uint64_t data_start; +static uint64_t sector_per_fat; +static uint64_t free_sector; +static file_list *dirty_files = NULL; + +static void add_list(file_list *xxx, struct file *f) { + if (dirty_files == NULL) { + dirty_files = malloc(sizeof(file_list)); + dirty_files->f = f; + dirty_files->prev = NULL; + dirty_files->next = NULL; + //uart_puth(dirty_files); + return; + } + while (dirty_files->next != NULL) { + dirty_files = dirty_files->next; + } + dirty_files->next = malloc(sizeof(file_list)); + dirty_files->next->prev = dirty_files; + + dirty_files = dirty_files->next; + dirty_files->f = f; + dirty_files->next = NULL; + while (dirty_files->prev != NULL) + dirty_files = dirty_files->prev; + return; +} /************************************************************** - * This function will initialize the FS from CPIO archive + * This function will initialize the FS from Fat (Sd card) * * @root: The root of this file system. *************************************************************/ int fatfs_initFsCpio(struct vnode *root) { - void *f = initrd_getLo(); // get the location of cpio struct vnode *target = NULL; + char fat_buf[512] = {0}; + char dir_buf[512] = {0}; char buf[16] = {0}; + Entry *e = NULL; uint64_t size; - while (1) { - char *fname = initrd_getName(f); - char *fdata = initrd_getData(f); - int fsize = initrd_getSize(f); - int fmode = initrd_getMode(f); - // uart_puts(fname); - // End - if (strcmp(fname, "TRAILER!!!") == 0) + readblock(data_start, dir_buf); + struct vnode *dir_node = root; + for (int i = 0; i < 512; i += sizeof(Entry)) { + e = dir_buf + i; + memset(buf, 0, sizeof(buf)); + int i, k = 0; + if (e->name[0] == 0) break; - struct vnode *dir_node = root; - while (fname != NULL) { - memset(buf, 0, 16); - fname = getFileName(buf, fname); - - // The Directory must be the directory type - ((FsAttr *)(dir_node->internal))->type = DIRTYPE; - if (*buf != 0) { - // Find if the dir is exist - dir_node->v_ops->lookup(dir_node, &target, buf); - // If not exist, just create it - if (target == NULL) - dir_node->v_ops->create(dir_node, &target, buf); + // Get the file name + for (i = 0; i < 8; i++) { + buf[i] = e->name[i]; + if (e->name[i] == ' ') { + buf[i] = '.'; + break; } - dir_node = target; - ((FsAttr *)(target->internal))->data = fdata; - ((FsAttr *)(target->internal))->Eof = fsize; - target = NULL; } - f = initrd_jumpNext(f); + if (buf[i] == '.') + i++; + else { + buf[i] = '.'; + i++; + } + for (int j = i; j < i + 3; j++) { + buf[j] = e->ext[k++]; + if (buf[j] == ' ') { + buf[j] = '\0'; + break; + } + } + //uart_puts(buf); + dir_node->v_ops->create(dir_node, &target, buf); + uint32_t tmp; + tmp = e->highAddr << 16; + tmp += e->lowAddr; + ((FsAttr *)(target->internal))->data = tmp; + ; + //uart_puth(tmp); + ((FsAttr *)(target->internal))->Eof = e->size; + //uart_puth(e->size); + //uart_puts("\n"); + target = NULL; } + return 0; } /************************************************************** - * Get the initialize function of the Ramfs + * Get the initialize function of the Fatfs *************************************************************/ struct filesystem *getFatFs(void) { struct filesystem *fs = malloc(sizeof(struct filesystem)); - fs->name = "Ramfs"; + fs->name = "Fatfs"; fs->setup_mount = fatfs_init; return fs; } @@ -131,6 +173,7 @@ int fatfs_lookup(struct vnode *dir, struct vnode **target, const char *name) { * Create implementation. which will create a new Vnode. ***************************************************************/ int fatfs_create(struct vnode *dir, struct vnode **target, const char *name) { + uart_puts(name); FsAttr *fs = (FsAttr *)dir->internal; if (fs->type != DIRTYPE) { uart_puts("U should add file only in DIR\n"); @@ -138,7 +181,7 @@ int fatfs_create(struct vnode *dir, struct vnode **target, const char *name) { } if (fs->dirs == NULL) - fs->dirs = (struct vnode **)malloc(sizeof(struct vnode *) * 16); + fs->dirs = (struct vnode **)malloc(sizeof(struct vnode *) * 32); struct vnode **c = (struct vnode **)fs->dirs; *target = (struct vnode *)malloc(sizeof(struct vnode)); memset(*target, 0, sizeof(struct vnode)); @@ -159,7 +202,8 @@ int fatfs_create(struct vnode *dir, struct vnode **target, const char *name) { } cfs->type = NORMAL; cfs->size = 0; - cfs->data = pmalloc(0); + cfs->data = free_sector; + free_sector++; // Update parent links c[(fs->size)++] = (*target); return 0; @@ -192,7 +236,57 @@ int fatfs_init(struct filesystem *fs, struct mount *m) { sd_init(); // Read The MBR readblock(0, buf); + // Get the first partition LBA (Little Endian on multiple byte) + for (int i = 0x1be + 0x0B; i >= 0x1be + 0x08; i--) { + partition_start <<= 8; + partition_start += buf[i]; + } + uart_puth(partition_start); + uart_puts("\n"); + + // Get the fs information of the FAT file system. + readblock(partition_start, buf); + + // Get reserved blocks in the FAT + for (int i = 0x0E; i < 0x0E + 1; i++) { + reserved_blocks <<= 4; + reserved_blocks += buf[i]; + } + int fat_nums = buf[16]; + for (int i = 36 + 3; i >= 36; i--) { + sector_per_fat <<= 8; + sector_per_fat += buf[i]; + } + for (int i = 47; i >= 44; i--) { + root_dir <<= 8; + root_dir += buf[i]; + } + uart_puts("root block: "); + uart_puth(root_dir); + + // Get the next free block in the FAT + readblock(partition_start + 1, buf); + for (int i = 495; i >= 492; i--) { + free_sector <<= 8; + free_sector += buf[i]; + } + uart_puts("Next free FAT: "); + uart_puth(free_sector); + // Jump to the FAT table + fat_start = partition_start + reserved_blocks; + readblock(fat_start, buf); + data_start = partition_start + reserved_blocks + sector_per_fat * fat_nums; + // RootDir + readblock(data_start, buf); + uart_puts("\nRoot location: "); + uart_puth(data_start * 512); + uart_puts("\n"); + for (int i = 0; i < 512; i += sizeof(Entry)) { + Entry *e = buf + i; + uart_puts(e->name); + uart_puts("\n"); + } // Check if the data already exist if (root->internal == NULL) { root->internal = (FsAttr *)malloc(sizeof(FsAttr)); @@ -212,10 +306,9 @@ int fatfs_init(struct filesystem *fs, struct mount *m) { attr->name = name; attr->type = DIRTYPE; attr->size = 0; - // Preserve the created dirs - if (attr->dirs == NULL) { - attr->dirs = (void *)smalloc(8 * sizeof(void *)); - } + // Use the new dirs + attr->dirs = (void *)smalloc(32 * sizeof(void *)); + fatfs_initFsCpio(root); return 0; } @@ -236,11 +329,14 @@ int fatfs_open(struct vnode *v, struct file **target) { if (*target == NULL) { *target = (struct file *)malloc(sizeof(struct file)); } + char *buf = (char *)pmalloc(0); (*target)->vnode = v; (*target)->f_pos = 0; (*target)->f_ops = v->f_ops; (*target)->Eof = ((FsAttr *)(v->internal))->Eof; - (*target)->data = ((FsAttr *)(v->internal))->data; + readblock(((FsAttr *)(v->internal))->data - 2 + data_start, buf); + (*target)->data = buf; + (*target)->dirty = 0; return 0; } @@ -250,13 +346,14 @@ int fatfs_open(struct vnode *v, struct file **target) { int fatfs_write(struct file *f, const void *buf, size_t len) { const char *c = (const char *)buf; char *data = (char *)f->data; + f->dirty = 1; if (f->data == NULL) return 1; for (size_t i = f->f_pos; i < len; i++) { *(data + (f->f_pos)) = *c++; (f->f_pos)++; } - // Update the EOF + // Update the EOF if (f->f_pos > f->Eof) f->Eof = f->f_pos; return f->f_pos; @@ -285,5 +382,85 @@ int fatfs_read(struct file *f, void *buf, size_t len) { *************************************************************/ int fatfs_close(struct file *f) { ((FsAttr *)(f->vnode->internal))->Eof = f->Eof; + + if (f->dirty != 0) + add_list(dirty_files, f); + + return 0; +} + +void fatfs_sync() { + + // Update the dircetory entry + struct vnode *target = NULL; + char fat_buf[512] = {0}; + char dir_buf[512] = {0}; + int k = 0; + readblock(fat_start, fat_buf); + Entry *e = NULL; + uint64_t size; + int i = 0; + while (dirty_files != NULL) { + readblock(data_start + k, dir_buf); + struct file *f = dirty_files->f; + char *buf = ((FsAttr *)(f->vnode->internal))->name; + // Update DIr + for (i; i < 512; i += sizeof(Entry)) { + e = (Entry *)(dir_buf + i); + int ii, k = 0; + if (dir_buf[i] != 0 && dir_buf[i] != 'A') + continue; + + // write the file name + for (ii = 0; ii < 8; ii++) { + e->name[ii] = buf[ii]; + if (e->name[ii] == '.') { + e->name[ii] = ' '; + break; + } + } + for (int j = ii; j < 8; j++) + e->name[j] = ' '; + ii++; + for (int j = 0; j < 3; j++) { + e->ext[j] = buf[ii++]; + } + e->highAddr = (((FsAttr *)(f->vnode->internal))->data >> 16) & (65535); + e->lowAddr = ((FsAttr *)(f->vnode->internal))->data & 65535; + e->size = f->Eof; + dir_buf[i + 11] = 0; + dir_buf[i + 12] = 0; + i += 32; + break; + } + if (i == 552) { + k = fat_buf[2]; + root_dir = fat_buf[2] / 512; + continue; + } + // Write back file content + writeblock(data_start + ((FsAttr *)(f->vnode->internal))->data - 2, + f->data); + dirty_files = dirty_files->next; + writeblock(data_start + k, dir_buf); + } + + //ramfs_dump(fsRoot, 0); + // Write back DIR + char tmp_buf[512] = {0}; + readblock(data_start, tmp_buf); + writeblock(data_start, dir_buf); + /* + for (int i = 0; i < 512; i++) { + if (tmp_buf[i] == dir_buf[i]) + continue; + uart_puti(i); + uart_puth(tmp_buf[i]); + uart_puts(" "); + if (i % 16 == 0) + uart_puts("\n"); + } + */ + return 0; } diff --git a/lab8/src/framefs.c b/lab8/src/framefs.c index 5029c235c..5803abbda 100644 --- a/lab8/src/framefs.c +++ b/lab8/src/framefs.c @@ -121,7 +121,7 @@ int framefs_write(struct file *f, const void *buf, size_t len) { char *d = f->data; uart_puts(" \b"); // Delay, if No this line will break. for (i = f->f_pos; i < f->f_pos + len; i++) { - *(d + i ) = *c ++ ; + *(d + i) = *c++; count++; } f->f_pos += len; diff --git a/lab8/src/interrupt.c b/lab8/src/interrupt.c index 1ca8a7ae9..601732e51 100644 --- a/lab8/src/interrupt.c +++ b/lab8/src/interrupt.c @@ -379,6 +379,9 @@ void low_syn_handler(Trap_frame *trap_frame) { case 19: regs[0] = sys_ioctl(regs[0], regs[1], regs[2]); break; + case 20: + sys_sync(); + break; default: uart_puts("***UNKNOWN INT***\n"); } diff --git a/lab8/src/main.c b/lab8/src/main.c index c8a5ef4de..d6e0e4a66 100644 --- a/lab8/src/main.c +++ b/lab8/src/main.c @@ -75,11 +75,17 @@ int main(void *dtb_location) { test->mount = frameM; ffs->setup_mount(test, frameM); - ramfs_dump(fsRoot, 0); - // FAT - sd_init(); + struct filesystem *fatfs = getFatFs(); + register_filesystem(fatfs); + vfs_mkdir("/boot", NULL); + vfs_lookup("/boot", &test, NULL); + struct mount *fatM = (struct mount *)malloc(sizeof(struct mount)); + fatM->root = test; + test->mount = fatM; + fatfs->setup_mount(test, fatM); + ramfs_dump(fsRoot, 0); core_timer_enable(); terminal_run_thread(); diff --git a/lab8/src/ramfs.c b/lab8/src/ramfs.c index bf1043bc7..f4cdef6d9 100644 --- a/lab8/src/ramfs.c +++ b/lab8/src/ramfs.c @@ -245,7 +245,7 @@ int ramfs_write(struct file *f, const void *buf, size_t len) { *(data + (f->f_pos)) = *c++; (f->f_pos)++; } - // Update the EOF + // Update the EOF if (f->f_pos > f->Eof) f->Eof = f->f_pos; return f->f_pos; diff --git a/lab8/src/sd.c b/lab8/src/sd.c index a35381707..0f545ba72 100644 --- a/lab8/src/sd.c +++ b/lab8/src/sd.c @@ -10,15 +10,15 @@ #define SEND_RELATIVE_ADDR 3 #define SELECT_CARD 7 #define SEND_IF_COND 8 - #define VOLTAGE_CHECK_PATTERN 0x1aa +#define VOLTAGE_CHECK_PATTERN 0x1aa #define STOP_TRANSMISSION 12 #define SET_BLOCKLEN 16 #define READ_SINGLE_BLOCK 17 #define WRITE_SINGLE_BLOCK 24 #define SD_APP_OP_COND 41 - #define SDCARD_3_3V (1 << 21) - #define SDCARD_ISHCS (1 << 30) - #define SDCARD_READY (1 << 31) +#define SDCARD_3_3V (1 << 21) +#define SDCARD_ISHCS (1 << 30) +#define SDCARD_READY (1 << 31) #define APP_CMD 55 // gpio @@ -31,45 +31,45 @@ // sdhost #define SDHOST_BASE (MMIO_BASE + 0x202000) #define SDHOST_CMD (SDHOST_BASE + 0) - #define SDHOST_READ 0x40 - #define SDHOST_WRITE 0x80 - #define SDHOST_LONG_RESPONSE 0x200 - #define SDHOST_NO_REPONSE 0x400 - #define SDHOST_BUSY 0x800 - #define SDHOST_NEW_CMD 0x8000 -#define SDHOST_ARG (SDHOST_BASE + 0x4) +#define SDHOST_READ 0x40 +#define SDHOST_WRITE 0x80 +#define SDHOST_LONG_RESPONSE 0x200 +#define SDHOST_NO_REPONSE 0x400 +#define SDHOST_BUSY 0x800 +#define SDHOST_NEW_CMD 0x8000 +#define SDHOST_ARG (SDHOST_BASE + 0x4) #define SDHOST_TOUT (SDHOST_BASE + 0x8) - #define SDHOST_TOUT_DEFAULT 0xf00000 +#define SDHOST_TOUT_DEFAULT 0xf00000 #define SDHOST_CDIV (SDHOST_BASE + 0xc) - #define SDHOST_CDIV_MAXDIV 0x7ff - #define SDHOST_CDIV_DEFAULT 0x148 +#define SDHOST_CDIV_MAXDIV 0x7ff +#define SDHOST_CDIV_DEFAULT 0x148 #define SDHOST_RESP0 (SDHOST_BASE + 0x10) #define SDHOST_RESP1 (SDHOST_BASE + 0x14) #define SDHOST_RESP2 (SDHOST_BASE + 0x18) #define SDHOST_RESP3 (SDHOST_BASE + 0x1c) #define SDHOST_HSTS (SDHOST_BASE + 0x20) - #define SDHOST_HSTS_MASK (0x7f8) - #define SDHOST_HSTS_ERR_MASK (0xf8) - #define SDHOST_HSTS_DATA (1 << 0) +#define SDHOST_HSTS_MASK (0x7f8) +#define SDHOST_HSTS_ERR_MASK (0xf8) +#define SDHOST_HSTS_DATA (1 << 0) #define SDHOST_PWR (SDHOST_BASE + 0x30) #define SDHOST_DBG (SDHOST_BASE + 0x34) - #define SDHOST_DBG_FSM_DATA 1 - #define SDHOST_DBG_FSM_MASK 0xf - #define SDHOST_DBG_MASK (0x1f << 14 | 0x1f << 9) - #define SDHOST_DBG_FIFO (0x4 << 14 | 0x4 << 9) +#define SDHOST_DBG_FSM_DATA 1 +#define SDHOST_DBG_FSM_MASK 0xf +#define SDHOST_DBG_MASK (0x1f << 14 | 0x1f << 9) +#define SDHOST_DBG_FIFO (0x4 << 14 | 0x4 << 9) #define SDHOST_CFG (SDHOST_BASE + 0x38) - #define SDHOST_CFG_DATA_EN (1 << 4) - #define SDHOST_CFG_SLOW (1 << 3) - #define SDHOST_CFG_INTBUS (1 << 1) +#define SDHOST_CFG_DATA_EN (1 << 4) +#define SDHOST_CFG_SLOW (1 << 3) +#define SDHOST_CFG_INTBUS (1 << 1) #define SDHOST_SIZE (SDHOST_BASE + 0x3c) #define SDHOST_DATA (SDHOST_BASE + 0x40) #define SDHOST_CNT (SDHOST_BASE + 0x50) // helper -#define set(io_addr, val) \ +#define set(io_addr, val) \ asm volatile("str %w1, [%0]" ::"r"(io_addr), "r"(val) : "memory"); -#define get(io_addr, val) \ +#define get(io_addr, val) \ asm volatile("ldr %w0, [%1]" : "=r"(val) : "r"(io_addr) : "memory"); static inline void delay(unsigned long tick) { @@ -78,7 +78,7 @@ static inline void delay(unsigned long tick) { } } -static int is_hcs; // high capcacity(SDHC) +static int is_hcs; // high capcacity(SDHC) static void pin_setup() { set(GPIO_GPFSEL4, 0x24000000); @@ -187,13 +187,13 @@ static void wait_finish() { } while ((dbg & SDHOST_DBG_FSM_MASK) != SDHOST_HSTS_DATA); } -void readblock(int block_idx, void* buf) { - unsigned int* buf_u = (unsigned int*)buf; +void readblock(int block_idx, void *buf) { + unsigned int *buf_u = (unsigned int *)buf; int succ = 0; if (!is_hcs) { block_idx <<= 9; } - do{ + do { set_block(512, 1); sd_cmd(READ_SINGLE_BLOCK | SDHOST_READ, block_idx); for (int i = 0; i < 128; ++i) { @@ -208,17 +208,17 @@ void readblock(int block_idx, void* buf) { } else { succ = 1; } - } while(!succ); + } while (!succ); wait_finish(); } -void writeblock(int block_idx, void* buf) { - unsigned int* buf_u = (unsigned int*)buf; +void writeblock(int block_idx, void *buf) { + unsigned int *buf_u = (unsigned int *)buf; int succ = 0; if (!is_hcs) { block_idx <<= 9; } - do{ + do { set_block(512, 1); sd_cmd(WRITE_SINGLE_BLOCK | SDHOST_WRITE, block_idx); for (int i = 0; i < 128; ++i) { @@ -233,7 +233,7 @@ void writeblock(int block_idx, void* buf) { } else { succ = 1; } - } while(!succ); + } while (!succ); wait_finish(); } diff --git a/lab8/src/syscall.c b/lab8/src/syscall.c index 9b48e3fc9..c3d144256 100644 --- a/lab8/src/syscall.c +++ b/lab8/src/syscall.c @@ -1,4 +1,5 @@ #include "syscall.h" +#include "fatfs.h" #include "initrd.h" #include "loader.h" #include "mailbox.h" @@ -270,7 +271,6 @@ void posix_kill(int pid, int sig) { } int sys_open(const char *pathname, int flags) { - // uart_puts("sys_open\n"); Thread *t = get_current(); struct vnode *cur = t->curDir; char *path; @@ -397,6 +397,12 @@ int sys_ioctl(int fd, unsigned long request, void *fb_info) { return 0; } +void sys_sync() { + fatfs_sync(); + //uart_puts("sys_sync\n"); + return; +} + //============================================================ // Test Functions. void fork_test() {