organize device drivers into subdirectories
This commit is contained in:
1
kernel/device/storage/.gitignore
vendored
Normal file
1
kernel/device/storage/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.o
|
||||
465
kernel/device/storage/idedrv.c
Normal file
465
kernel/device/storage/idedrv.c
Normal file
@@ -0,0 +1,465 @@
|
||||
#include <amd64/apic.h>
|
||||
#include <amd64/intr_defs.h>
|
||||
#include <amd64/io.h>
|
||||
#include <device/device.h>
|
||||
#include <device/storage/idedrv.h>
|
||||
#include <device/storage/partitions.h>
|
||||
#include <devices.h>
|
||||
#include <irq/irq.h>
|
||||
#include <libk/list.h>
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <mm/malloc.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
#include <status.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
#define IDE_REG_DATA 0x00
|
||||
#define IDE_REG_ERROR 0x01
|
||||
#define IDE_REG_SECCOUNT 0x02
|
||||
#define IDE_REG_LBA0 0x03
|
||||
#define IDE_REG_LBA1 0x04
|
||||
#define IDE_REG_LBA2 0x05
|
||||
#define IDE_REG_DRIVE 0x06
|
||||
#define IDE_REG_STATUS 0x07
|
||||
#define IDE_REG_CMD 0x07
|
||||
|
||||
#define IDE_BSY 0x80
|
||||
#define IDE_DRDY 0x40
|
||||
#define IDE_DF 0x20
|
||||
#define IDE_ERR 0x01
|
||||
#define IDE_DRQ 0x08
|
||||
|
||||
#define IDE_CMD_READ28 0x20
|
||||
#define IDE_CMD_WRITE28 0x30
|
||||
#define IDE_CMD_READ48 0x24
|
||||
#define IDE_CMD_WRITE48 0x34
|
||||
#define IDE_CMD_FLUSH48 0xEA
|
||||
#define IDE_CMD_FLUSH28 0xE7
|
||||
#define IDE_CMD_IDENTIFY 0xEC
|
||||
|
||||
#define IDE_READ 1
|
||||
#define IDE_WRITE 2
|
||||
|
||||
static bool ide_wait (uint16_t io, uint32_t timeout, bool drq, bool errcheck) {
|
||||
uint32_t i = 0;
|
||||
uint8_t status;
|
||||
|
||||
while (1) {
|
||||
status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (!(status & IDE_BSY))
|
||||
break;
|
||||
|
||||
if (++i >= timeout)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (errcheck && (status & (IDE_ERR | IDE_DF)))
|
||||
return false;
|
||||
|
||||
if (!drq)
|
||||
return true;
|
||||
|
||||
i = 0;
|
||||
|
||||
while (1) {
|
||||
status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (status & (IDE_ERR | IDE_DF))
|
||||
return false;
|
||||
|
||||
if (status & IDE_DRQ)
|
||||
return true;
|
||||
|
||||
if (++i >= timeout)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma clang optimize off
|
||||
static void ide_delay (uint16_t ctrl) {
|
||||
inb (ctrl);
|
||||
inb (ctrl);
|
||||
inb (ctrl);
|
||||
inb (ctrl);
|
||||
}
|
||||
#pragma clang optimize on
|
||||
|
||||
static void ide_irq (void* arg, void* regs, bool user, struct reschedule_ctx* rctx) {
|
||||
(void)user, (void)regs, (void)rctx;
|
||||
|
||||
uint64_t fd;
|
||||
|
||||
struct idedrv* idedrv = arg;
|
||||
|
||||
spin_lock (&idedrv->device->lock, &fd);
|
||||
|
||||
struct idedrv_request* req = idedrv->current_req;
|
||||
|
||||
if (req == NULL) {
|
||||
(void)inb (idedrv->io + IDE_REG_STATUS);
|
||||
spin_unlock (&idedrv->device->lock, fd);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t status = inb (idedrv->io + IDE_REG_STATUS);
|
||||
|
||||
if ((status & (IDE_ERR | IDE_DF))) {
|
||||
atomic_store (&req->done, 1);
|
||||
idedrv->current_req = NULL;
|
||||
spin_unlock (&idedrv->device->lock, fd);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((status & IDE_DRQ) && (req->sector_done_count < req->sector_count)) {
|
||||
uint16_t* p = req->buffer + (req->sector_done_count * (idedrv->sector_size / 2));
|
||||
|
||||
if (req->type == IDE_READ)
|
||||
insw (idedrv->io + IDE_REG_DATA, p, idedrv->sector_size / 2);
|
||||
else
|
||||
outsw (idedrv->io + IDE_REG_DATA, p, idedrv->sector_size / 2);
|
||||
|
||||
req->sector_done_count++;
|
||||
}
|
||||
|
||||
if ((req->sector_done_count >= req->sector_count)) {
|
||||
atomic_store (&req->done, 1);
|
||||
idedrv->current_req = NULL;
|
||||
}
|
||||
|
||||
spin_unlock (&idedrv->device->lock, fd);
|
||||
}
|
||||
|
||||
void ide_probe (uint16_t io, uint16_t ctrl, uint8_t devno, struct ide_probe* probe) {
|
||||
probe->flags = 0;
|
||||
probe->sector_count = 0;
|
||||
probe->sector_size = 0;
|
||||
|
||||
uint16_t identify_buffer[256];
|
||||
|
||||
uint8_t status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (status == 0xFF)
|
||||
return;
|
||||
|
||||
outb (io + IDE_REG_DRIVE, 0xA0 | (devno << 4));
|
||||
ide_delay (ctrl);
|
||||
|
||||
outb (io + IDE_REG_SECCOUNT, 0);
|
||||
outb (io + IDE_REG_LBA0, 0);
|
||||
outb (io + IDE_REG_LBA1, 0);
|
||||
outb (io + IDE_REG_LBA2, 0);
|
||||
|
||||
outb (io + IDE_REG_CMD, IDE_CMD_IDENTIFY);
|
||||
|
||||
status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (status == 0)
|
||||
return;
|
||||
|
||||
if (!ide_wait (io, 90000000, true, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
insw (io + IDE_REG_DATA, identify_buffer, 256);
|
||||
|
||||
probe->flags |= IDE_PROBE_AVAIL;
|
||||
|
||||
if ((identify_buffer[106] & 0xC000) == 0x4000 && identify_buffer[106] & (1 << 12)) {
|
||||
uint32_t words_per_sector =
|
||||
(uint32_t)identify_buffer[117] | ((uint32_t)identify_buffer[118] << 16);
|
||||
probe->sector_size = (size_t)words_per_sector * 2;
|
||||
}
|
||||
|
||||
if ((identify_buffer[83] & (1 << 10)) != 0)
|
||||
probe->flags |= IDE_PROBE_LBA48;
|
||||
|
||||
if ((probe->flags & IDE_PROBE_LBA48)) {
|
||||
probe->sector_count =
|
||||
(size_t)((uint64_t)identify_buffer[100] | ((uint64_t)identify_buffer[101] << 16) |
|
||||
((uint64_t)identify_buffer[102] << 32) | ((uint64_t)identify_buffer[103] << 48));
|
||||
} else {
|
||||
probe->sector_count =
|
||||
(size_t)((uint64_t)identify_buffer[60] | ((uint64_t)identify_buffer[61] << 16));
|
||||
}
|
||||
|
||||
probe->io = io;
|
||||
probe->ctrl = ctrl;
|
||||
probe->devno = devno;
|
||||
|
||||
if (probe->sector_size == 0)
|
||||
probe->sector_size = 512;
|
||||
}
|
||||
|
||||
static void ide_prepare (struct idedrv* idedrv, size_t sector, uint16_t sector_count,
|
||||
bool irq_enable) {
|
||||
uint8_t ctrl = inb (idedrv->ctrl);
|
||||
if (irq_enable)
|
||||
ctrl &= ~0x02;
|
||||
else
|
||||
ctrl |= 0x02;
|
||||
outb (idedrv->ctrl, ctrl);
|
||||
|
||||
if (idedrv->lba48) {
|
||||
outb (idedrv->io + IDE_REG_DRIVE, 0x40 | (idedrv->devno << 4));
|
||||
ide_delay (idedrv->ctrl);
|
||||
|
||||
ide_wait (idedrv->io, 100000, false, false);
|
||||
|
||||
outb (idedrv->io + IDE_REG_SECCOUNT, (sector_count >> 8) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA0, (sector >> 24) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA1, (sector >> 32) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA2, (sector >> 40) & 0xFF);
|
||||
|
||||
outb (idedrv->io + IDE_REG_SECCOUNT, sector_count & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA0, sector & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA1, (sector >> 8) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA2, (sector >> 16) & 0xFF);
|
||||
} else {
|
||||
outb (idedrv->io + IDE_REG_DRIVE, 0xE0 | (idedrv->devno << 4) | ((sector >> 24) & 0x0F));
|
||||
ide_delay (idedrv->ctrl);
|
||||
|
||||
ide_wait (idedrv->io, 100000, false, false);
|
||||
|
||||
uint8_t count = (sector_count == 256) ? 0 : (uint8_t)sector_count;
|
||||
outb (idedrv->io + IDE_REG_SECCOUNT, count);
|
||||
outb (idedrv->io + IDE_REG_LBA0, sector & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA1, (sector >> 8) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA2, (sector >> 16) & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_INIT (idedrv_init) {
|
||||
struct idedrv_init* init = arg;
|
||||
|
||||
struct idedrv* idedrv = malloc (sizeof (*idedrv));
|
||||
|
||||
if (idedrv == NULL)
|
||||
return false;
|
||||
|
||||
idedrv->device = device;
|
||||
idedrv->lba48 = init->lba48;
|
||||
idedrv->sector_count = init->sector_count;
|
||||
idedrv->sector_size = init->sector_size;
|
||||
idedrv->io = init->io;
|
||||
idedrv->ctrl = init->ctrl;
|
||||
idedrv->devno = init->devno;
|
||||
idedrv->irq = init->irq;
|
||||
idedrv->current_req = NULL;
|
||||
idedrv->irqs_support = init->irqs_support;
|
||||
|
||||
device->udata = idedrv;
|
||||
|
||||
if (idedrv->irqs_support)
|
||||
irq_attach (&ide_irq, idedrv, idedrv->irq);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_FINI (idedrv_fini) {
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
if (idedrv->current_req != NULL) {
|
||||
free (idedrv->current_req);
|
||||
idedrv->current_req = NULL;
|
||||
}
|
||||
|
||||
if (idedrv->irqs_support)
|
||||
irq_detach (idedrv->irq);
|
||||
|
||||
free (idedrv);
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_read) {
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t sector = *(size_t*)a1;
|
||||
size_t sector_count = *(size_t*)a2;
|
||||
uint16_t* buffer = a3;
|
||||
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
if (sector + sector_count > idedrv->sector_count)
|
||||
return -ST_OOB_ERROR;
|
||||
|
||||
if (!ide_wait (idedrv->io, 100000, false, false))
|
||||
return -ST_XDRV_READ_ERROR;
|
||||
|
||||
if (idedrv->irqs_support) {
|
||||
struct idedrv_request* req = malloc (sizeof (*req));
|
||||
|
||||
if (req == NULL)
|
||||
return -ST_OOM_ERROR;
|
||||
|
||||
memset (req, 0, sizeof (*req));
|
||||
req->buffer = buffer;
|
||||
req->sector_count = sector_count;
|
||||
req->sector_done_count = 0;
|
||||
req->type = IDE_READ;
|
||||
|
||||
idedrv->current_req = req;
|
||||
|
||||
ide_prepare (idedrv, sector, sector_count, true);
|
||||
|
||||
uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
|
||||
spin_unlock (&device->lock, *lockflags);
|
||||
|
||||
while (!atomic_load (&req->done))
|
||||
spin_lock_relax ();
|
||||
|
||||
spin_lock (&device->lock, lockflags);
|
||||
|
||||
free (req);
|
||||
} else {
|
||||
ide_prepare (idedrv, sector, sector_count, false);
|
||||
|
||||
uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
|
||||
for (uint16_t s = 0; s < sector_count; s++) {
|
||||
if (!ide_wait (idedrv->io, 100000, true, true))
|
||||
return -ST_XDRV_READ_ERROR;
|
||||
|
||||
insw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)),
|
||||
idedrv->sector_size / 2);
|
||||
}
|
||||
}
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_write) {
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t sector = *(size_t*)a1;
|
||||
size_t sector_count = *(size_t*)a2;
|
||||
uint16_t* buffer = a3;
|
||||
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
if (sector + sector_count > idedrv->sector_count)
|
||||
return -ST_OOB_ERROR;
|
||||
|
||||
if (!ide_wait (idedrv->io, 100000, false, false))
|
||||
return -ST_XDRV_WRITE_ERROR;
|
||||
|
||||
if (idedrv->irqs_support) {
|
||||
struct idedrv_request* req = malloc (sizeof (*req));
|
||||
|
||||
if (req == NULL)
|
||||
return -ST_OOM_ERROR;
|
||||
|
||||
memset (req, 0, sizeof (*req));
|
||||
req->buffer = buffer;
|
||||
req->sector_count = sector_count;
|
||||
req->sector_done_count = 0;
|
||||
req->type = IDE_WRITE;
|
||||
|
||||
idedrv->current_req = req;
|
||||
|
||||
ide_prepare (idedrv, sector, sector_count, true);
|
||||
|
||||
uint8_t cmd = idedrv->lba48 ? IDE_CMD_WRITE48 : IDE_CMD_WRITE28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
|
||||
if (!ide_wait (idedrv->io, 100000, true, true)) {
|
||||
idedrv->current_req = NULL;
|
||||
free (req);
|
||||
return -ST_XDRV_WRITE_ERROR;
|
||||
}
|
||||
|
||||
outsw (idedrv->io + IDE_REG_DATA, buffer, idedrv->sector_size / 2);
|
||||
|
||||
req->sector_done_count = 1;
|
||||
|
||||
spin_unlock (&device->lock, *lockflags);
|
||||
|
||||
while (!atomic_load (&req->done))
|
||||
spin_lock_relax ();
|
||||
|
||||
spin_lock (&device->lock, lockflags);
|
||||
|
||||
free (req);
|
||||
} else {
|
||||
ide_prepare (idedrv, sector, sector_count, false);
|
||||
|
||||
uint8_t cmd = idedrv->lba48 ? IDE_CMD_WRITE48 : IDE_CMD_WRITE28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
|
||||
for (uint16_t s = 0; s < sector_count; s++) {
|
||||
if (!ide_wait (idedrv->io, 100000, true, true))
|
||||
return -ST_XDRV_WRITE_ERROR;
|
||||
|
||||
outsw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)),
|
||||
idedrv->sector_size / 2);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ctrl = inb (idedrv->ctrl);
|
||||
ctrl |= 0x02;
|
||||
outb (idedrv->ctrl, ctrl);
|
||||
|
||||
if (idedrv->lba48)
|
||||
outb (idedrv->io + IDE_REG_CMD, IDE_CMD_FLUSH48);
|
||||
else
|
||||
outb (idedrv->io + IDE_REG_CMD, IDE_CMD_FLUSH28);
|
||||
|
||||
uint8_t status;
|
||||
do {
|
||||
status = inb (idedrv->io + IDE_REG_STATUS);
|
||||
} while (status & IDE_BSY);
|
||||
|
||||
if (status & (IDE_ERR | IDE_DF))
|
||||
return -ST_XDRV_WRITE_ERROR;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_get_device_type) {
|
||||
(void)proc, (void)rctx, (void)device, (void)a2, (void)a3, (void)a4, (void)lockflags;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
int* device_type = (int*)a1;
|
||||
|
||||
*device_type = XDRV_TYPE_IDEDRV;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_get_sector_size) {
|
||||
(void)proc, (void)rctx, (void)a2, (void)a3, (void)a4, (void)lockflags;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* secsize = (size_t*)a1;
|
||||
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
*secsize = idedrv->sector_size;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_get_size) {
|
||||
(void)proc, (void)rctx, (void)a2, (void)a3, (void)a4, (void)lockflags;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* size = (size_t*)a1;
|
||||
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
*size = idedrv->sector_size * idedrv->sector_count;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
72
kernel/device/storage/idedrv.h
Normal file
72
kernel/device/storage/idedrv.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#ifndef _KERNEL_DEVICE_IDEDRV_H
|
||||
#define _KERNEL_DEVICE_IDEDRV_H
|
||||
|
||||
#include <device/def_device_op.h>
|
||||
#include <libk/list.h>
|
||||
#include <libk/std.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
#include <proc/suspension_q.h>
|
||||
|
||||
#define IDE_PROBE_AVAIL (1 << 0)
|
||||
#define IDE_PROBE_LBA48 (1 << 1)
|
||||
|
||||
struct device;
|
||||
|
||||
struct idedrv_init {
|
||||
bool lba48;
|
||||
size_t sector_count;
|
||||
size_t sector_size;
|
||||
uint16_t io, ctrl;
|
||||
uint8_t devno;
|
||||
uint8_t irq;
|
||||
bool irqs_support;
|
||||
};
|
||||
|
||||
struct idedrv_request {
|
||||
uint16_t* buffer;
|
||||
size_t sector_done_count;
|
||||
size_t sector_count;
|
||||
int type;
|
||||
atomic_int done;
|
||||
};
|
||||
|
||||
struct idedrv {
|
||||
struct device* device;
|
||||
bool lba48;
|
||||
size_t sector_count;
|
||||
size_t sector_size;
|
||||
uint16_t io, ctrl;
|
||||
uint8_t devno;
|
||||
uint8_t irq;
|
||||
struct idedrv_request* current_req;
|
||||
bool irqs_support;
|
||||
};
|
||||
|
||||
struct ide_probe {
|
||||
uint16_t flags;
|
||||
size_t sector_count;
|
||||
size_t sector_size;
|
||||
uint16_t io, ctrl;
|
||||
uint8_t devno;
|
||||
uint8_t irq;
|
||||
bool irqs_support;
|
||||
};
|
||||
|
||||
DEFINE_DEVICE_INIT (idedrv_init);
|
||||
|
||||
DEFINE_DEVICE_FINI (idedrv_fini);
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_read);
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_write);
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_get_device_type);
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_get_sector_size);
|
||||
|
||||
DEFINE_DEVICE_OP (idedrv_get_size);
|
||||
|
||||
void ide_probe (uint16_t io, uint16_t ctrl, uint8_t devno, struct ide_probe* probe);
|
||||
|
||||
#endif // _KERNEL_DEVICE_IDEDRV_H
|
||||
112
kernel/device/storage/partdrv.c
Normal file
112
kernel/device/storage/partdrv.c
Normal file
@@ -0,0 +1,112 @@
|
||||
#include <device/device.h>
|
||||
#include <device/storage/partdrv.h>
|
||||
#include <devices.h>
|
||||
#include <libk/std.h>
|
||||
#include <mm/malloc.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
#include <status.h>
|
||||
|
||||
DEFINE_DEVICE_INIT (partdrv_init) {
|
||||
struct partdrv_init* init = arg;
|
||||
|
||||
struct partdrv* partdrv = malloc (sizeof (*partdrv));
|
||||
|
||||
if (partdrv == NULL)
|
||||
return false;
|
||||
|
||||
partdrv->start_sector = init->start_sector;
|
||||
partdrv->super = init->super;
|
||||
partdrv->total_size = init->total_size;
|
||||
|
||||
device->udata = partdrv;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_FINI (partdrv_fini) {
|
||||
struct partdrv* partdrv = device->udata;
|
||||
|
||||
free (partdrv);
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_read) {
|
||||
uint64_t fs;
|
||||
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
struct partdrv* partdrv = device->udata;
|
||||
struct device* super = partdrv->super;
|
||||
|
||||
size_t sector = *(size_t*)a1 + partdrv->start_sector;
|
||||
size_t sector_count = *(size_t*)a2;
|
||||
uint8_t* buffer = a3;
|
||||
|
||||
spin_lock (&super->lock, &fs);
|
||||
int ret = device_op (super, XDRV_READ, proc, rctx, &fs, §or, §or_count, buffer);
|
||||
spin_unlock (&super->lock, fs);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_write) {
|
||||
uint64_t fs;
|
||||
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
struct partdrv* partdrv = device->udata;
|
||||
struct device* super = partdrv->super;
|
||||
|
||||
size_t sector = *(size_t*)a1 + partdrv->start_sector;
|
||||
size_t sector_count = *(size_t*)a2;
|
||||
uint8_t* buffer = a3;
|
||||
|
||||
spin_lock (&super->lock, &fs);
|
||||
int ret = device_op (super, XDRV_WRITE, proc, rctx, &fs, §or, §or_count, buffer);
|
||||
spin_unlock (&super->lock, fs);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_get_device_type) {
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
int* device_type = (int*)a1;
|
||||
|
||||
*device_type = XDRV_TYPE_PARTDRV;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_get_sector_size) {
|
||||
uint64_t fs;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* secsize = (size_t*)a1;
|
||||
|
||||
struct partdrv* partdrv = device->udata;
|
||||
|
||||
spin_lock (&partdrv->super->lock, &fs);
|
||||
device_op (partdrv->super, XDRV_GET_SECTOR_SIZE, proc, rctx, &fs, secsize);
|
||||
spin_unlock (&partdrv->super->lock, fs);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_get_size) {
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* size = (size_t*)a1;
|
||||
|
||||
struct partdrv* partdrv = device->udata;
|
||||
|
||||
*size = partdrv->total_size;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
37
kernel/device/storage/partdrv.h
Normal file
37
kernel/device/storage/partdrv.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef _KERNEL_DEVICE_PARTDRV_H
|
||||
#define _KERNEL_DEVICE_PARTDRV_H
|
||||
|
||||
#include <device/def_device_op.h>
|
||||
#include <libk/std.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
|
||||
struct device;
|
||||
|
||||
struct partdrv_init {
|
||||
struct device* super;
|
||||
size_t start_sector;
|
||||
size_t total_size;
|
||||
};
|
||||
|
||||
struct partdrv {
|
||||
struct device* super;
|
||||
size_t start_sector;
|
||||
size_t total_size;
|
||||
};
|
||||
|
||||
DEFINE_DEVICE_INIT (partdrv_init);
|
||||
|
||||
DEFINE_DEVICE_FINI (partdrv_fini);
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_read);
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_write);
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_get_device_type);
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_get_sector_size);
|
||||
|
||||
DEFINE_DEVICE_OP (partdrv_get_size);
|
||||
|
||||
#endif // _KERNEL_DEVICE_PARTDRV_H
|
||||
74
kernel/device/storage/partitions.c
Normal file
74
kernel/device/storage/partitions.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <device/device.h>
|
||||
#include <device/storage/partdrv.h>
|
||||
#include <device/storage/partitions.h>
|
||||
#include <devices.h>
|
||||
#include <libk/fieldsizeof.h>
|
||||
#include <libk/lengthof.h>
|
||||
#include <libk/printf.h>
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
#include <status.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
static int device_probe_partitions_dos (struct proc* proc, struct reschedule_ctx* rctx,
|
||||
struct device* device) {
|
||||
uint64_t fd;
|
||||
|
||||
struct dos_mbr mbr;
|
||||
memset (&mbr, 0, sizeof (mbr));
|
||||
size_t sector = 0;
|
||||
size_t sector_count = 1;
|
||||
size_t sector_size;
|
||||
|
||||
device_op_func_t ops[] = {
|
||||
[XDRV_GET_SIZE] = &partdrv_get_size,
|
||||
[XDRV_GET_SECTOR_SIZE] = &partdrv_get_sector_size,
|
||||
[XDRV_GET_DEVICE_TYPE] = &partdrv_get_device_type,
|
||||
[XDRV_READ] = &partdrv_read,
|
||||
[XDRV_WRITE] = &partdrv_write,
|
||||
};
|
||||
|
||||
spin_lock (&device->lock, &fd);
|
||||
|
||||
device_op (device, XDRV_GET_SECTOR_SIZE, proc, rctx, &fd, §or_size);
|
||||
|
||||
int ret = device_op (device, XDRV_READ, proc, rctx, &fd, §or, §or_count, &mbr);
|
||||
|
||||
if (ret < 0) {
|
||||
spin_unlock (&device->lock, fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!(mbr.valid_sign[0] == 0x55 && mbr.valid_sign[1] == 0xAA)) {
|
||||
spin_unlock (&device->lock, fd);
|
||||
return -ST_PARTITION_ERROR;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < lengthof (mbr.ptes); i++) {
|
||||
struct dos_pte* pte = &mbr.ptes[i];
|
||||
|
||||
struct partdrv_init init = {
|
||||
.start_sector = pte->start_lba,
|
||||
.total_size = pte->sector_count * sector_size,
|
||||
.super = device,
|
||||
};
|
||||
|
||||
char key[fieldsizeof (struct device, key)];
|
||||
memset (key, 0, sizeof (key));
|
||||
snprintf (key, sizeof (key), "%sp%zu", device->key, i);
|
||||
|
||||
device_create (DEVICE_TYPE_DRIVE, key, ops, lengthof (ops), &partdrv_init, &partdrv_fini, &init,
|
||||
proc, rctx);
|
||||
}
|
||||
|
||||
spin_unlock (&device->lock, fd);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int device_probe_partitions (struct proc* proc, struct reschedule_ctx* rctx,
|
||||
struct device* device) {
|
||||
return device_probe_partitions_dos (proc, rctx, device);
|
||||
}
|
||||
29
kernel/device/storage/partitions.h
Normal file
29
kernel/device/storage/partitions.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef _KERNEL_DEVICE_PARTITIONS_H
|
||||
#define _KERNEL_DEVICE_PARTITIONS_H
|
||||
|
||||
#include <aux/compiler.h>
|
||||
#include <device/device.h>
|
||||
#include <libk/std.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
|
||||
struct dos_pte {
|
||||
uint8_t drive_attrs;
|
||||
uint8_t chs_start_addr[3];
|
||||
uint8_t part_type;
|
||||
uint8_t chs_last_sect_addr[3];
|
||||
uint32_t start_lba;
|
||||
uint32_t sector_count;
|
||||
} PACKED;
|
||||
|
||||
struct dos_mbr {
|
||||
uint8_t boot_code[440];
|
||||
uint8_t signature[4];
|
||||
uint8_t resv[2];
|
||||
struct dos_pte ptes[4];
|
||||
uint8_t valid_sign[2];
|
||||
} PACKED;
|
||||
|
||||
int device_probe_partitions (struct proc* proc, struct reschedule_ctx* rctx, struct device* device);
|
||||
|
||||
#endif // _KERNEL_DEVICE_PARTITIONS_H
|
||||
116
kernel/device/storage/ramdrv.c
Normal file
116
kernel/device/storage/ramdrv.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <device/device.h>
|
||||
#include <device/storage/partitions.h>
|
||||
#include <device/storage/ramdrv.h>
|
||||
#include <devices.h>
|
||||
#include <libk/align.h>
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <mm/malloc.h>
|
||||
#include <status.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
DEFINE_DEVICE_INIT (ramdrv_init) {
|
||||
struct ramdrv_init* init = arg;
|
||||
|
||||
struct ramdrv* ramdrv = malloc (sizeof (*ramdrv));
|
||||
|
||||
if (ramdrv == NULL)
|
||||
return false;
|
||||
|
||||
ramdrv->sector_size = init->sector_size;
|
||||
ramdrv->total_size = align_up (init->total_size, ramdrv->sector_size);
|
||||
|
||||
ramdrv->buffer = malloc (ramdrv->total_size);
|
||||
|
||||
if (ramdrv->buffer == NULL) {
|
||||
free (ramdrv);
|
||||
return false;
|
||||
}
|
||||
|
||||
memset (ramdrv->buffer, 0, ramdrv->total_size);
|
||||
|
||||
if (init->buffer != NULL)
|
||||
memcpy (ramdrv->buffer, init->buffer, init->total_size);
|
||||
|
||||
device->udata = ramdrv;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_FINI (ramdrv_fini) {
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
|
||||
free (ramdrv->buffer);
|
||||
free (ramdrv);
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_get_device_type) {
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
int* device_type = (int*)a1;
|
||||
|
||||
*device_type = XDRV_TYPE_RAMDRV;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_get_size) {
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* size = (size_t*)a1;
|
||||
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
|
||||
*size = ramdrv->total_size;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_get_sector_size) {
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* secsize = (size_t*)a1;
|
||||
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
|
||||
*secsize = ramdrv->sector_size;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_read) {
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t sector = *(size_t*)a1;
|
||||
size_t sector_count = *(size_t*)a2;
|
||||
uint8_t* buffer = a3;
|
||||
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
size_t pos = sector * ramdrv->sector_size;
|
||||
size_t size = sector_count * ramdrv->sector_size;
|
||||
|
||||
memcpy (buffer, (void*)(((uintptr_t)ramdrv->buffer) + pos), size);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_write) {
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t sector = *(size_t*)a1;
|
||||
size_t sector_count = *(size_t*)a2;
|
||||
uint8_t* buffer = a3;
|
||||
|
||||
struct ramdrv* ramdrv = device->udata;
|
||||
size_t pos = sector * ramdrv->sector_size;
|
||||
size_t size = sector_count * ramdrv->sector_size;
|
||||
|
||||
memcpy ((void*)(((uintptr_t)ramdrv->buffer) + pos), buffer, size);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
38
kernel/device/storage/ramdrv.h
Normal file
38
kernel/device/storage/ramdrv.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef _KERNEL_DEVICE_RAMDRV_H
|
||||
#define _KERNEL_DEVICE_RAMDRV_H
|
||||
|
||||
#include <device/def_device_op.h>
|
||||
#include <libk/list.h>
|
||||
#include <libk/std.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
|
||||
struct device;
|
||||
|
||||
struct ramdrv_init {
|
||||
size_t total_size;
|
||||
size_t sector_size;
|
||||
uint8_t* buffer;
|
||||
};
|
||||
|
||||
struct ramdrv {
|
||||
size_t total_size;
|
||||
size_t sector_size;
|
||||
uint8_t* buffer;
|
||||
};
|
||||
|
||||
DEFINE_DEVICE_INIT (ramdrv_init);
|
||||
|
||||
DEFINE_DEVICE_FINI (ramdrv_fini);
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_read);
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_write);
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_get_device_type);
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_get_sector_size);
|
||||
|
||||
DEFINE_DEVICE_OP (ramdrv_get_size);
|
||||
|
||||
#endif // _KERNEL_DEVICE_RAMDRV_H
|
||||
9
kernel/device/storage/src.mk
Normal file
9
kernel/device/storage/src.mk
Normal file
@@ -0,0 +1,9 @@
|
||||
c += device/storage/partdrv.c \
|
||||
device/storage/ramdrv.c \
|
||||
device/storage/partitions.c \
|
||||
device/storage/idedrv.c
|
||||
|
||||
o += device/storage/partdrv.o \
|
||||
device/storage/ramdrv.o \
|
||||
device/storage/partitions.o \
|
||||
device/storage/idedrv.o
|
||||
Reference in New Issue
Block a user