Skip to content

Commit

Permalink
增加文件系统
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyi1212 committed Jan 17, 2024
1 parent 766858b commit 3abecc6
Show file tree
Hide file tree
Showing 13 changed files with 560 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ def linker(): #交叉编译链接
if a != 0:
exit(-1)

os.system("qemu-system-i386 -kernel isodir\\sys\\kernel.elf")
os.system("qemu-system-i386 -kernel isodir\\sys\\kernel.elf -drive format=qcow2,file=cpos.qcow2")

Binary file added cpos.qcow2
Binary file not shown.
73 changes: 73 additions & 0 deletions driver/disk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "../include/disk.h"
#include "../include/io.h"

void wait_disk_ready()
{
while (1)
{
int data = io_in8(0x1f7);
if ((data & 0x88) == 0x08) // 第3位为1表示硬盘控制器已准备好数据传输,第7位为1表示硬盘忙。
{
return;
}
}
}

void select_sector(int lba)
{
io_out8(0x1f2, 1);
io_out8(0x1f3, lba);
io_out8(0x1f4, lba >> 8);
io_out8(0x1f5, lba >> 16);
io_out8(0x1f6, (((lba >> 24) & 0x0f) | 0xe0));
}

void read_disk_one_sector(int lba, unsigned int memory_addrress)
{
while (io_in8(0x1f7) & 0x80)
;
select_sector(lba);
io_out8(0x1f7, 0x20);
wait_disk_ready();
for (int i = 0; i < 256; i++)
{
short data = (short)io_in16(0x1f0);
*((short *)memory_addrress) = data;
memory_addrress += 2;
}
}

void write_disk_one_sertor(int lba, unsigned int memory_addrress)
{
while (io_in8(0x1f7) & 0x80)
;
select_sector(lba);
io_out8(0x1f7, 0x30);
wait_disk_ready();
for (int i = 0; i < 256; i++)
{
short data = *((short *)memory_addrress);
io_out16(0x1f0, data);
memory_addrress += 2;
}
}

void read_disk(int lba, int sector_count, unsigned int memory_addrress)
{
for (int i = 0; i < sector_count; i++)
{
read_disk_one_sector(lba, memory_addrress);
lba++;
memory_addrress += 512;
}
}

void write_disk(int lba, int sector_count, unsigned int memory_addrress)
{
for (int i = 0; i < sector_count; i++)
{
write_disk_one_sertor(lba, memory_addrress);
lba++;
memory_addrress += 512;
}
}
11 changes: 11 additions & 0 deletions include/disk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef CPOS_DISK_H
#define CPOS_DISK_H

void wait_disk_ready(); //不适用于SATA硬盘
void select_sector(int lba);
void read_disk_one_sector(int lba, unsigned int memory_addrress);
void write_disk_one_sertor(int lba, unsigned int memory_addrress);
void read_disk(int lba, int sector_count, unsigned int memory_addrress);
void write_disk(int lba, int sector_count, unsigned int memory_addrress);

#endif
48 changes: 48 additions & 0 deletions include/fat16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#ifndef CPOS_FAT16_H
#define CPOS_FAT16_H

#define SECTOR_SIZE 512
#define FAT1_SECTORS 32 //FAT1占用扇区数
#define ROOT_DIR_SECTORS 32 //根目录占用扇区数
#define SECTOR_NUM_OF_FAT1_START 1 //FAT1起始扇区号
#define SECTOR_NUM_OF_ROOT_DIR_START 33 //根目录起始扇区号
#define SECTOR_NUM_OF_DATA_START 65 //数据区起始扇区号,对应簇号为2。
#define SECTOR_CLUSTER_BALANCE SECTOR_NUM_OF_DATA_START - 2 //簇号加上该值正好对应扇区号。
#define MAX_FILE_NUM 16 //最大文件数

// FAT16目录项结构(32B);
struct File {
// 文件名 如果第一个字节为0xe5,代表这个文件已经被删除;如果第一个字节为0x00,代表这一段不包含任何文件名信息。
unsigned char name[8];
unsigned char ext[3]; // 扩展名
// 属性:bit0只读文件,bit1隐藏文件,bit2系统文件,bit3非文件信息(比如磁盘名称),bit4目录,bit5文件。
unsigned char type; // 0x20 文件 | 0x10 目录
unsigned char reserve[10]; // 保留
unsigned short time; // 最后一次写入时间
unsigned short date; // 最后一次写入日期
unsigned short clustno; // 起始簇号
unsigned int size; // 文件大小
};

void read_root_dir_sector1(struct File *root_entries);
void save_root_dir_sector1(struct File *root_entries);
void read_one_cluster(unsigned short clustno, unsigned int memory_addrress);
int read_root_dir(struct File *file_infos);
void get_fat1(unsigned short *fat1);
void save_fat1(unsigned short *fat1);
void get_file_all_clustnos(unsigned short first_clustno, unsigned short *clustnos);
void get_file_all_clustnos(unsigned short first_clustno, unsigned short *clustnos);
void read_file(struct File *file, void *file_addr);
void check_name_or_ext(char *str, int len);
void check_name_and_ext(char *name, char *ext);
void analyse_fullname(char *fullname, char *name, char *ext);
int find_file(char *name, char *ext, struct File *const file);
struct File *create_file(char *fullname);
struct File *create_dir(char *fullname);
struct File *open_file(char *fullname);
void alter_file_name(struct File *file, char *new_fullname);
void alter_dir_entry(struct File *file);
void save_file(struct File *file, char *content);
void delete_file(struct File *file);

#endif
2 changes: 1 addition & 1 deletion include/multiboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdint.h>

#define VERSION "0.0.1"
#define VERSION "0.0.2"
#define LOADER_SAVE_GRAPHICS_ADDRESS 0xB8000 // loader中保存显存地址的地址
#define VARTUAL_GRAPHICS_ADDRESS 0xe0000000

Expand Down
3 changes: 3 additions & 0 deletions include/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ void setup_shell();

//内置命令
void end_echo(int argc,char **argv);
void cmd_ls();
void cmd_cat(int argc,char **argv);
void cmd_read(int argc,char **argv);

#endif
Binary file modified isodir/sys/kernel.elf
Binary file not shown.
Loading

0 comments on commit 3abecc6

Please sign in to comment.