🆕 读取fat32文件系统的基础信息

This commit is contained in:
fslongjin
2022-04-19 20:56:01 +08:00
parent 39dd802ff1
commit 979bb35599
10 changed files with 185 additions and 25 deletions

44
kernel/filesystem/MBR.h Normal file
View File

@ -0,0 +1,44 @@
/**
* @file MBR.h
* @author fslongjin (longjin@RinGoTek.cn)
* @brief MBR分区表
* @version 0.1
* @date 2022-04-19
*
* @copyright Copyright (c) 2022
*
*/
#pragma once
#include <common/glib.h>
/**
* @brief MBR硬盘分区表项的结构
*
*/
struct MBR_disk_partition_table_entry_t
{
uint8_t flags; // 引导标志符,标记此分区为活动分区
uint8_t starting_head; // 起始磁头号
uint16_t starting_sector : 6, // 起始扇区号
starting_cylinder : 10; // 起始柱面号
uint8_t type; // 分区类型ID
uint8_t ending_head; // 结束磁头号
uint16_t ending_sector:6, // 结束扇区号
ending_cylinder:10; // 结束柱面号
uint32_t starting_LBA; // 起始逻辑扇区
uint32_t total_sectors; // 分区占用的磁盘扇区数
}__attribute__((packed));
/**
* @brief MBR磁盘分区表结构体
*
*/
struct MBR_disk_partition_table_t
{
uint8_t reserved[446];
struct MBR_disk_partition_table_entry_t DPTE[4]; // 磁盘分区表项
uint16_t BS_TrailSig;
}__attribute__((packed));

View File

@ -0,0 +1,42 @@
#include "fat32.h"
#include <common/kprint.h>
#include <driver/disk/ahci/ahci.h>
/**
* @brief 读取指定磁盘上的第0个分区的fat32文件系统
*
* @param disk_num
*/
void fat32_FS_init(int disk_num)
{
int i;
unsigned char buf[512];
struct MBR_disk_partition_table_t DPT;
struct fat32_BootSector_t fat32_bootsector;
struct fat32_FSInfo_t fat32_fsinfo;
memset(buf, 0, 512);
ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, 0, 1, (uint64_t)&buf, 0, 0);
DPT = *(struct MBR_disk_partition_table_t *)buf;
// for(i = 0 ;i < 512 ; i++)
// color_printk(PURPLE,WHITE,"%02x",buf[i]);
printk_color(ORANGE, BLACK, "DPTE[0] start_LBA:%#018lx\ttype:%#018lx\n", DPT.DPTE[0].starting_LBA, DPT.DPTE[0].type);
memset(buf, 0, 512);
ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, DPT.DPTE[0].starting_LBA, 1, (uint64_t)&buf, 0, 0);
fat32_bootsector = *(struct fat32_BootSector_t *)buf;
// for(i = 0 ;i < 512 ; i++)
// printk_color(PURPLE,WHITE,"%02x",buf[i]);
printk_color(ORANGE, BLACK, "FAT32 Boot Sector\n\tBPB_FSInfo:%#018lx\n\tBPB_BkBootSec:%#018lx\n\tBPB_TotSec32:%#018lx\n", fat32_bootsector.BPB_FSInfo, fat32_bootsector.BPB_BkBootSec, fat32_bootsector.BPB_TotSec32);
memset(buf, 0, 512);
ahci_operation.transfer(ATA_CMD_READ_DMA_EXT, DPT.DPTE[0].starting_LBA+ fat32_bootsector.BPB_FSInfo, 1, (uint64_t)&buf, 0, 0);
fat32_fsinfo = *(struct fat32_FSInfo_t *)buf;
// for(i = 0 ;i < 512 ; i++)
// printk_color(PURPLE,WHITE,"%02x",buf[i]);
printk_color(ORANGE, BLACK, "FAT32 FSInfo\n\tFSI_LeadSig:%#018lx\n\tFSI_StrucSig:%#018lx\n\tFSI_Free_Count:%#018lx\n", fat32_fsinfo.FSI_LeadSig, fat32_fsinfo.FSI_StrucSig, fat32_fsinfo.FSI_Free_Count);
}

View File

@ -0,0 +1,77 @@
/**
* @file fat32.h
* @author fslongjin (longjin@RinGoTek.cn)
* @brief fat32文件系统
* @version 0.1
* @date 2022-04-19
*
* @copyright Copyright (c) 2022
*
*/
#pragma once
#include <filesystem/MBR.h>
/**
* @brief fat32文件系统引导扇区结构体
*
*/
struct fat32_BootSector_t
{
uint8_t BS_jmpBoot[3]; // 跳转指令
uint8_t BS_OEMName[8]; // 生产厂商名
uint16_t BPB_BytesPerSec; // 每扇区字节数
uint8_t BPB_SecPerClus; // 每簇扇区数
uint16_t BPB_RsvdSecCnt; // 保留扇区数
uint8_t BPB_NumFATs; // FAT表数量
uint16_t BPB_RootEntCnt; // 根目录文件数最大值
uint16_t BPB_TotSec16; // 16位扇区总数
uint8_t BPB_Media; // 介质描述符
uint16_t BPB_FATSz16; // FAT12/16每FAT扇区数
uint16_t BPB_SecPerTrk; // 每磁道扇区数
uint16_t BPB_NumHeads; // 磁头数
uint32_t BPB_HiddSec; // 隐藏扇区数
uint32_t BPB_TotSec32; // 32位扇区总数
uint32_t BPB_FATSz32; // FAT32每FAT扇区数
uint16_t BPB_ExtFlags; // 扩展标志
uint16_t BPB_FSVer; // 文件系统版本号
uint32_t BPB_RootClus; // 根目录起始簇号
uint16_t BPB_FSInfo; // FS info结构体的扇区号
uint16_t BPB_BkBootSec; // 引导扇区的备份扇区号
uint8_t BPB_Reserved0[12];
uint8_t BS_DrvNum; // int0x13的驱动器号
uint8_t BS_Reserved1;
uint8_t BS_BootSig; // 扩展引导标记
uint32_t BS_VolID; // 卷序列号
uint8_t BS_VolLab[11]; // 卷标
uint8_t BS_FilSysType[8]; // 文件系统类型
uint8_t BootCode[420]; // 引导代码、数据
uint16_t BS_TrailSig; // 结束标志0xAA55
} __attribute__((packed));
/**
* @brief fat32文件系统的FSInfo扇区结构体
*
*/
struct fat32_FSInfo_t
{
uint32_t FSI_LeadSig; // FS info扇区标志符 数值为0x41615252
uint8_t FSI_Reserved1[480]; // 保留使用全部置为0
uint32_t FSI_StrucSig; // 另一个标志符数值为0x61417272
uint32_t FSI_Free_Count; // 上一次记录的空闲簇数量,这是一个参考值
uint32_t FSI_Nxt_Free; // 空闲簇的起始搜索位置,这是为驱动程序提供的参考值
uint8_t FSI_Reserved2[12]; // 保留使用全部置为0
uint32_t FSI_TrailSig; // 结束标志数值为0xaa550000
} __attribute__((packed));
/**
* @brief 读取指定磁盘上的第0个分区的fat32文件系统
*
* @param disk_num
*/
void fat32_FS_init(int disk_num);