12 #include "cdi/storage.h" 13 #include "pedigree/kernel/processor/types.h" 14 #include "pedigree/kernel/utilities/utility.h" 20 void cdi_storage_driver_register(
struct cdi_storage_driver* driver);
22 void cdi_cpp_disk_register(
struct cdi_storage_device* device);
24 int cdi_storage_read(
struct cdi_storage_device* device, uint64_t pos,
size_t size,
void* dest);
25 int cdi_storage_write(
struct cdi_storage_device* device, uint64_t pos,
size_t size,
void* src);
31 void cdi_storage_driver_init(
struct cdi_storage_driver* driver)
33 cdi_driver_init(reinterpret_cast<struct cdi_driver*>(driver));
39 void cdi_storage_driver_destroy(
struct cdi_storage_driver* driver)
41 cdi_driver_destroy(reinterpret_cast<struct cdi_driver*>(driver));
47 void cdi_storage_driver_register(
struct cdi_storage_driver* driver)
49 static int initialized = 0;
51 cdi_driver_register(reinterpret_cast<struct cdi_driver*>(driver));
57 void cdi_storage_device_init(
struct cdi_storage_device* device)
59 cdi_cpp_disk_register(device);
69 int cdi_storage_read(
struct cdi_storage_device* device, uint64_t pos,
size_t size,
void* dest)
71 struct cdi_storage_driver* driver =
reinterpret_cast<struct cdi_storage_driver*
>(device->dev.driver);
72 size_t block_size = device->block_size;
74 uint64_t block_read_start = pos / block_size;
75 uint64_t block_read_end = (pos + size) / block_size;
77 uint64_t block_read_count = block_read_end - block_read_start;
80 if (((pos % block_size) == 0) && (((pos + size) % block_size) == 0)) {
82 return driver->read_blocks(device, block_read_start, block_read_count, dest);
86 uint8_t *buffer =
new uint8_t[block_read_count * block_size];
89 if (driver->read_blocks(device, block_read_start, block_read_count, buffer) != 0)
96 MemoryCopy(dest, buffer + (pos % block_size), size);
109 int cdi_storage_write(
struct cdi_storage_device* device, uint64_t pos,
110 size_t size,
void* src)
112 struct cdi_storage_driver* driver =
reinterpret_cast<struct cdi_storage_driver*
>(device->dev.driver);
114 size_t block_size = device->block_size;
115 uint64_t block_write_start = pos / block_size;
116 uint8_t *source =
reinterpret_cast<uint8_t*
>(src);
117 uint8_t *buffer =
new uint8_t[block_size];
124 offset = (pos % block_size);
126 tmp_size = block_size - offset;
127 tmp_size = (tmp_size > size ? size : tmp_size);
129 if (driver->read_blocks(device, block_write_start, 1, buffer) != 0) {
133 MemoryCopy(buffer + offset, source, tmp_size);
136 if (driver->write_blocks(device, block_write_start, 1, buffer) != 0) {
148 tmp_size = size / block_size;
151 if (driver->write_blocks(device, block_write_start, tmp_size, source)
157 size -= tmp_size * block_size;
158 source += tmp_size * block_size;
159 block_write_start += block_size;
165 if (driver->read_blocks(device, block_write_start, 1, buffer) != 0) {
169 MemoryCopy(buffer, source, size);
172 if (driver->write_blocks(device, block_write_start, 1, buffer) != 0) {