-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xiaoyi1212
committed
Jan 17, 2024
1 parent
766858b
commit 3abecc6
Showing
13 changed files
with
560 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Oops, something went wrong.