CoolPotOS/driver/vdisk.c

144 lines
4.0 KiB
C
Raw Normal View History

2024-05-03 20:32:10 +08:00
/*
* Plants-OS VDiskDriver
* Copyright by min0911.
*/
#include "../include/vdisk.h"
#include "../include/printf.h"
2024-05-12 00:17:47 +08:00
#include "../include/fifo.h"
#include "../include/task.h"
2024-05-03 20:32:10 +08:00
int getReadyDisk();
2024-05-18 23:44:25 +08:00
vdisk vdisk_ctl[26];
2024-05-03 20:32:10 +08:00
int init_vdisk() {
2024-05-18 23:44:25 +08:00
for (int i = 0; i < 26; i++) {
2024-05-03 20:32:10 +08:00
vdisk_ctl[i].flag = 0; // 设置为未使用
}
}
int register_vdisk(vdisk vd) {
2024-05-18 23:44:25 +08:00
for (int i = 0; i < 26; i++) {
2024-05-03 20:32:10 +08:00
if (!vdisk_ctl[i].flag) {
vdisk_ctl[i] = vd; // 找到了!
return i + ('A'); // 注册成功返回drive
}
}
2024-05-18 23:44:25 +08:00
printk("[vdisk]not found\n");
2024-05-03 20:32:10 +08:00
return 0; // 注册失败
}
int logout_vdisk(char drive) {
int indx = drive - ('A');
2024-05-18 23:44:25 +08:00
if (indx > 26) {
2024-05-03 20:32:10 +08:00
return 0; // 失败
}
if (vdisk_ctl[indx].flag) {
vdisk_ctl[indx].flag = 0; // 设置为没有
return 1; // 成功
} else {
return 0; // 失败
}
}
int rw_vdisk(char drive, unsigned int lba, unsigned char *buffer,
unsigned int number, int read) {
int indx = drive - ('A');
2024-05-18 23:44:25 +08:00
if (indx > 26) {
2024-05-03 20:32:10 +08:00
return 0; // 失败
}
if (vdisk_ctl[indx].flag) {
if (read) {
vdisk_ctl[indx].Read(drive, buffer, number, lba);
} else {
vdisk_ctl[indx].Write(drive, buffer, number, lba);
}
return 1; // 成功
} else {
return 0; // 失败
}
}
2024-05-18 23:44:25 +08:00
bool have_vdisk(char drive) {
2024-05-03 20:32:10 +08:00
int indx = drive - 'A';
2024-05-18 23:44:25 +08:00
// printk("drive=%c\n",drive);
if (indx > 26) {
2024-05-03 20:32:10 +08:00
return 0; // 失败
}
if (vdisk_ctl[indx].flag) {
return 1; // 成功
} else {
return 0; // 失败
}
}
2024-05-18 23:44:25 +08:00
// 基于vdisk的通用读写
2024-05-12 00:17:47 +08:00
static unsigned char *drive_name[16] = {NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL};
static struct FIFO8 drive_fifo[16];
static unsigned char drive_buf[16][256];
bool SetDrive(unsigned char *name) {
for (int i = 0; i != 16; i++) {
if (drive_name[i] == NULL) {
drive_name[i] = name;
fifo8_init(&drive_fifo[i], 256, drive_buf[i]);
2024-05-18 23:44:25 +08:00
return true;
2024-05-12 00:17:47 +08:00
}
}
2024-05-18 23:44:25 +08:00
return false;
2024-05-12 00:17:47 +08:00
}
unsigned int GetDriveCode(unsigned char *name) {
for (int i = 0; i != 16; i++) {
if (strcmp((char *)drive_name[i], (char *)name) == 0) {
return i;
}
}
return 16;
}
bool DriveSemaphoreTake(unsigned int drive_code) {
2024-05-18 23:44:25 +08:00
return true;
2024-05-12 00:17:47 +08:00
}
void DriveSemaphoreGive(unsigned int drive_code) {
2024-05-18 23:44:25 +08:00
return;
}
void Disk_Read(unsigned int lba, unsigned int number, void *buffer,
char drive) {
if (have_vdisk(drive)) {
if (DriveSemaphoreTake(GetDriveCode((unsigned char *)"DISK_DRIVE"))) {
for (int i = 0; i < number; i += SECTORS_ONCE) {
int sectors = ((number - i) >= SECTORS_ONCE) ? SECTORS_ONCE : (number - i);
rw_vdisk(drive, lba + i, buffer + i * 512, sectors, 1);
}
DriveSemaphoreGive(GetDriveCode((unsigned char *)"DISK_DRIVE"));
}
2024-05-12 00:17:47 +08:00
}
2024-05-18 23:44:25 +08:00
}
unsigned int disk_Size(char drive) {
unsigned char drive1 = drive;
if (have_vdisk(drive1)) {
int indx = drive1 - 'A';
return vdisk_ctl[indx].size;
} else {
return 0;
}
return 0;
}
bool DiskReady(char drive) { return have_vdisk(drive); }
int getReadyDisk() { return 0; }
void Disk_Write(unsigned int lba, unsigned int number, void *buffer,
char drive) {
// printk("%d\n",lba);
if (have_vdisk(drive)) {
if (DriveSemaphoreTake(GetDriveCode((unsigned char *)"DISK_DRIVE"))) {
// printk("*buffer(%d %d) = %02x\n",lba,number,*(unsigned char *)buffer);
for (int i = 0; i < number; i += SECTORS_ONCE) {
int sectors = ((number - i) >= SECTORS_ONCE) ? SECTORS_ONCE : (number - i);
rw_vdisk(drive, lba + i, buffer + i * 512, sectors, 0);
}
DriveSemaphoreGive(GetDriveCode((unsigned char *)"DISK_DRIVE"));
}
2024-05-12 00:17:47 +08:00
}
}