summaryrefslogtreecommitdiff
path: root/src/kernel/arch/amd64/driver/pata.c
blob: 99c31020910279761b52bea7607f80b2cd52529f (plain)
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
#include <camellia/errno.h>
#include <camellia/fsutil.h>
#include <kernel/arch/amd64/ata.h>
#include <kernel/arch/amd64/driver/driver.h>
#include <kernel/arch/amd64/driver/util.h>
#include <kernel/panic.h>
#include <kernel/proc.h>
#include <kernel/vfs/request.h>
#include <shared/mem.h>

static const int root_id = 100;

static void accept(VfsReq *req);
void pata_init(void) {
	ata_init();
	vfs_root_register("/ata", accept);
}

static void accept(VfsReq *req) {
	int ret;
	long id = (long __force)req->id;
	char wbuf[4096];
	size_t len;
	switch (req->type) {
		case VFSOP_OPEN:
			     if (reqpathcmp(req, "/"))  ret = root_id;
			else if (reqpathcmp(req, "/0")) ret = 0;
			else if (reqpathcmp(req, "/1")) ret = 1;
			else if (reqpathcmp(req, "/2")) ret = 2;
			else if (reqpathcmp(req, "/3")) ret = 3;
			else ret = -ENOENT;
			// TODO don't allow opening nonexistent drives
			vfsreq_finish_short(req, ret);
			break;

		case VFSOP_READ:
			if (id == root_id) {
				// TODO use dirbuild
				char list[8] = {};
				size_t len = 0;
				for (int i = 0; i < 4; i++) {
					if (ata_available(i)) {
						list[len] = '0' + i;
						len += 2;
					}
				}
				vfsreq_finish_short(req, req_readcopy(req, list, len));
				break;
			}
			fs_normslice(&req->offset, &req->output.len, ata_size(id), false);
			len = min(req->output.len, sizeof wbuf);
			ata_read(id, wbuf, len, req->offset);
			pcpy_to(req->caller, req->output.buf, wbuf, len);
			vfsreq_finish_short(req, len);
			break;

		case VFSOP_WRITE:
			if (id == root_id) {
				vfsreq_finish_short(req, -EACCES);
				break;
			}
			fs_normslice(&req->offset, &req->input.len, ata_size(id), false);
			len = min(req->input.len, sizeof wbuf);
			if (len != 0) {
				len = pcpy_from(req->caller, wbuf, req->input.buf, len);
				ata_write(id, wbuf, len, req->offset);
			}
			vfsreq_finish_short(req, len);
			break;

		case VFSOP_GETSIZE:
			if (id == root_id) {
				panic_unimplemented();
			}
			vfsreq_finish_short(req, ata_size(id));
			break;

		default:
			vfsreq_finish_short(req, -ENOSYS);
			break;
	}
}