feat: simple device abstraction layer
[lunaix-os.git] / lunaix-os / kernel / block.c
1 #include <hal/ahci/hba.h>
2 #include <klibc/stdio.h>
3 #include <klibc/string.h>
4 #include <lib/crc.h>
5 #include <lunaix/block.h>
6 #include <lunaix/fs/twifs.h>
7 #include <lunaix/mm/cake.h>
8 #include <lunaix/mm/valloc.h>
9 #include <lunaix/syslog.h>
10
11 #include <lunaix/spike.h>
12
13 #define BLOCK_EREAD 1
14 #define BLOCK_ESIG 2
15 #define BLOCK_ECRC 3
16 #define BLOCK_EFULL 3
17
18 LOG_MODULE("BLOCK")
19
20 #define MAX_DEV 32
21
22 struct cake_pile* lbd_pile;
23 struct block_dev** dev_registry;
24
25 int free_slot = 0;
26
27 int
28 __block_mount_partitions(struct hba_device* hd_dev);
29
30 int
31 __block_register(struct block_dev* dev);
32
33 void
34 block_init()
35 {
36     lbd_pile = cake_new_pile("block_dev", sizeof(struct block_dev), 1, 0);
37     dev_registry = vcalloc(sizeof(struct block_dev*), MAX_DEV);
38     free_slot = 0;
39 }
40
41 int
42 __block_read(struct device* dev,
43              void* buf,
44              unsigned int offset,
45              unsigned int len)
46 {
47     int errno;
48     struct block_dev* bdev = (struct block_dev*)dev->underlay;
49     size_t acc_size = 0, rd_size = 0, bsize = bdev->hd_dev->block_size,
50            rd_block = 0;
51     void* block = valloc(bsize);
52
53     while (acc_size < len) {
54         if (!bdev->hd_dev->ops.read_buffer(
55               bdev->hd_dev, offset + rd_block, block, bsize)) {
56             errno = ENXIO;
57             goto error;
58         }
59         rd_size = MIN(len - acc_size, bsize);
60         memcpy(buf + acc_size, block, rd_size);
61         acc_size += rd_size;
62         rd_block++;
63     }
64
65     vfree(block);
66     return rd_block;
67
68 error:
69     vfree(block);
70     return errno;
71 }
72
73 int
74 __block_write(struct device* dev,
75               void* buf,
76               unsigned int offset,
77               unsigned int len)
78 {
79     int errno;
80     struct block_dev* bdev = (struct block_dev*)dev->underlay;
81     size_t acc_size = 0, wr_size = 0, bsize = bdev->hd_dev->block_size,
82            wr_block = 0;
83     void* block = valloc(bsize);
84
85     while (acc_size < len) {
86         wr_size = MIN(len - acc_size, bsize);
87         memcpy(block, buf + acc_size, wr_size);
88         if (!bdev->hd_dev->ops.write_buffer(
89               bdev->hd_dev, offset + wr_block, block, bsize)) {
90             errno = ENXIO;
91             break;
92         }
93         acc_size += wr_size;
94         wr_block++;
95     }
96
97     vfree(block);
98     return wr_block;
99
100 error:
101     vfree(block);
102     return errno;
103 }
104
105 int
106 block_mount_disk(struct hba_device* hd_dev)
107 {
108     int errno = 0;
109     struct block_dev* bdev = cake_grab(lbd_pile);
110     strncpy(bdev->name, hd_dev->model, PARTITION_NAME_SIZE);
111     bdev->hd_dev = hd_dev;
112     bdev->base_lba = 0;
113     bdev->end_lba = hd_dev->max_lba;
114     if (!__block_register(bdev)) {
115         errno = BLOCK_EFULL;
116         goto error;
117     }
118
119     return errno;
120
121 error:
122     kprintf(KERROR "Fail to mount hd: %s[%s] (%x)\n",
123             hd_dev->model,
124             hd_dev->serial_num,
125             -errno);
126 }
127
128 int
129 __block_register(struct block_dev* bdev)
130 {
131     if (free_slot >= MAX_DEV) {
132         return 0;
133     }
134
135     struct device* dev = device_add(NULL, bdev, "sd%c", 'a' + free_slot);
136     dev->write = __block_write;
137     dev->read = __block_read;
138
139     bdev->dev = dev;
140     dev_registry[free_slot++] = bdev;
141     return 1;
142 }