Forked from 那我就让时光倒流 / OSkernel_HIT_naw0
Source project has a limited visibility.
fat32.c 43.30 KiB
#include "include/param.h"
#include "include/types.h"
#include "include/riscv.h"
#include "include/spinlock.h"
#include "include/sleeplock.h"
#include "include/buf.h"
#include "include/proc.h"
#include "include/stat.h"
#include "include/fat32.h"
#include "include/string.h"
#include "include/printf.h"
#include "include/vm.h"
static struct {
    uint32  first_data_sec; // data所在的第一个扇区
    uint32  data_sec_cnt; // 数据扇区数
    uint32  data_clus_cnt; // 数据簇数
    uint32  byts_per_clus; // 每簇字节数
    struct {
        uint16  byts_per_sec;  // 扇区字节数
        uint8   sec_per_clus;  // 每簇扇区数
        uint16  rsvd_sec_cnt;  // 保留扇区数
        uint8   fat_cnt;  // fat数          
        uint32  hidd_sec;  // 隐藏扇区数         
        uint32  tot_sec;  // 总扇区数          
        uint32  fat_sz;   // 一个fat所占扇区数           
        uint32  root_clus; // 根目录簇号 
    } bpb; 
} fat; 
// 目录缓冲区
static struct entry_cache {
    struct spinlock lock;
    struct dirent entries[ENTRY_CACHE_NUM];
} ecache; 
// 根目录
static struct dirent root;  
//挂载设备的集合
//其中设备0表示主存的文件系统
 struct fstype dev_fat[8] ;
//表示寻找在挂载集合的下标
int  mount_num=0;
/**
 * 读取bpb 初始化根目录 初始化目录缓冲区 
 * Read the Boot Parameter Block.  
 * @return  0       if success  
 *          -1      if fail  
int fat32_init() 
    #ifdef DEBUG
    printf("[fat32_init] enter!\n");
    #endif
    struct buf *b = bread(0, 0);
    if (strncmp((char const*)(b->data + 82), "FAT32", 5)){
        panic("not FAT32 volume");
    // fat.bpb.byts_per_sec = *(uint16 *)(b->data + 11);
    memmove(&fat.bpb.byts_per_sec, b->data + 11, 2);            // avoid misaligned load on k210
    fat.bpb.sec_per_clus = *(b->data + 13);  
    fat.bpb.rsvd_sec_cnt = *(uint16 *)(b->data + 14);
    fat.bpb.fat_cnt = *(b->data + 16);
    fat.bpb.hidd_sec = *(uint32 *)(b->data + 28);
    fat.bpb.tot_sec = *(uint32 *)(b->data + 32);
    fat.bpb.fat_sz = *(uint32 *)(b->data + 36);
    fat.bpb.root_clus = *(uint32 *)(b->data + 44);
    fat.first_data_sec = fat.bpb.rsvd_sec_cnt + fat.bpb.fat_cnt * fat.bpb.fat_sz;