1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
// MIT License, Copyright (c) 2020 Marvin Borner
#include <assert.h>
#include <cpu.h>
#include <def.h>
#include <fs.h>
#include <ide.h>
#include <mem.h>
#include <print.h>
PROTECTED static u8 *ide_buf = NULL;
struct ata_data {
u8 drive;
};
CLEAR static void ide_select_drive(u8 bus, u8 drive)
{
if (bus == ATA_PRIMARY) {
if (drive == ATA_MASTER)
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xa0);
else
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xb0);
} else {
if (drive == ATA_MASTER)
outb(ATA_SECONDARY_IO + ATA_REG_HDDEVSEL, 0xa0);
else
outb(ATA_SECONDARY_IO + ATA_REG_HDDEVSEL, 0xb0);
}
}
CLEAR static u8 ide_find(u8 bus, u8 drive)
{
u16 io = bus == ATA_PRIMARY ? ATA_PRIMARY_IO : ATA_SECONDARY_IO;
ide_select_drive(bus, drive);
// Reset
outb(io + ATA_REG_SECCOUNT0, 0);
outb(io + ATA_REG_LBA0, 0);
outb(io + ATA_REG_LBA1, 0);
outb(io + ATA_REG_LBA2, 0);
// Identify
outb(io + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
u8 status = inb(io + ATA_REG_STATUS);
if (!status)
return 0;
while ((inb(io + ATA_REG_STATUS) & ATA_SR_BSY) != 0)
;
do {
status = inb(io + ATA_REG_STATUS);
if (status & ATA_SR_ERR)
return 0;
} while ((status & ATA_SR_DRQ) == 0);
for (int i = 0; i < BLOCK_COUNT; i++)
*(u16 *)(ide_buf + i * 2) = inw(io + ATA_REG_DATA);
return 1;
}
static void ide_delay(u16 io) // 400ns
{
for (int i = 0; i < 4; i++)
inb(io + ATA_REG_ALTSTATUS);
}
static void ide_poll(u16 io)
{
for (int i = 0; i < 4; i++)
inb(io + ATA_REG_ALTSTATUS);
u8 status;
do {
status = inb(io + ATA_REG_STATUS);
} while (status & ATA_SR_BSY);
do {
status = inb(io + ATA_REG_STATUS);
assert(!(status & ATA_SR_ERR))
} while (!(status & ATA_SR_DRQ));
}
static u8 ata_read_one(u8 *buf, u32 lba, struct device *dev)
{
u8 drive = ((struct ata_data *)dev->data)->drive;
u16 io = (drive & ATA_PRIMARY << 1) == ATA_PRIMARY ? ATA_PRIMARY_IO : ATA_SECONDARY_IO;
drive = (drive & ATA_SLAVE) == ATA_SLAVE ? ATA_SLAVE : ATA_MASTER;
u8 cmd = drive == ATA_MASTER ? 0xe0 : 0xf0;
outb(io + ATA_REG_HDDEVSEL, (cmd | (u8)((lba >> 24 & 0x0f))));
outb(io + 1, 0x00);
outb(io + ATA_REG_SECCOUNT0, 1);
outb(io + ATA_REG_LBA0, (u8)lba);
outb(io + ATA_REG_LBA1, (u8)(lba >> 8));
outb(io + ATA_REG_LBA2, (u8)(lba >> 16));
outb(io + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
ide_poll(io);
for (int i = 0; i < BLOCK_COUNT; i++) {
u16 data = inw(io + ATA_REG_DATA);
*(u16 *)(buf + i * 2) = data;
}
ide_delay(io);
return 1;
}
static res ata_read(void *buf, u32 lba, u32 sector_count, struct device *dev)
{
u8 *b = buf; // I love bytes, yk
for (u32 i = 0; i < sector_count; i++) {
ata_read_one(b, lba + i, dev);
b += SECTOR_SIZE;
}
return sector_count;
}
u8 ata_pm = 0, ata_ps = 0, ata_sm = 0, ata_ss = 0;
CLEAR static void ata_probe(void)
{
for (u8 i = 0; i < 4; i++) {
u32 bus = i < 2 ? ATA_PRIMARY : ATA_SECONDARY;
u32 drive = i % 2 ? ATA_MASTER : ATA_SLAVE;
if (!ide_find(bus, drive))
continue;
struct device *dev = zalloc(sizeof(*dev));
struct ata_data *data = malloc(sizeof(*data));
data->drive = (bus << 1) | drive;
char *str = malloc(40);
for (int j = 0; j < 40; j += 2) {
str[j] = ide_buf[ATA_IDENT_MODEL + j + 1];
str[j + 1] = ide_buf[ATA_IDENT_MODEL + j];
}
dev->name = str;
dev->type = DEV_BLOCK;
dev->read = ata_read;
device_add(dev);
if (vfs_mounted(dev, "/"))
continue;
// TODO: Check if ext2 first
struct vfs *vfs = zalloc(sizeof(*vfs));
vfs->type = VFS_EXT2;
vfs->read = ext2_read;
vfs->stat = ext2_stat;
vfs->perm = ext2_perm;
vfs->ready = ext2_ready;
dev->vfs = vfs;
dev->data = data;
vfs_mount(dev, "/");
}
}
CLEAR void ata_install(void)
{
ide_buf = zalloc(SECTOR_SIZE);
ata_probe();
}
|