dev: implement reading from block devices
reading from block devices is done using the block cache (bcache). This cache stores sectors from a block device in pages of memory marked as 'cached', which will allow them to be reclaimed when memory pressure is high (TODO). while block device drivers implement callbacks allowing reading/writing at block-granularity, the device subsystem uses the block cache to implement reading/writing at byte-granularity in a driver-agnostic way. block drivers can disable the block cache for their devices, but this will require that any clients communicate with the devices at block-granularity. also added an offset parameter to device and object read/write functions/callbacks.
This commit is contained in:
22
init/main.c
22
init/main.c
@@ -29,7 +29,7 @@ void print_kernel_banner(void)
|
||||
static void hang(void)
|
||||
{
|
||||
while (1) {
|
||||
printk("tick");
|
||||
//printk("tick");
|
||||
milli_sleep(2000);
|
||||
}
|
||||
}
|
||||
@@ -110,5 +110,25 @@ void kernel_init(uintptr_t arg)
|
||||
tty_connect_foreground_input_device(cast_to_device(kbd));
|
||||
}
|
||||
|
||||
struct object *disk;
|
||||
status = object_get("/dev/block/disk0", &disk);
|
||||
if (status == KERN_OK) {
|
||||
unsigned char buf[32] = {0};
|
||||
struct device *disk_dev = cast_to_device(disk);
|
||||
size_t nread = 0;
|
||||
|
||||
status = device_read(disk_dev, buf, 1, 32, &nread, 0);
|
||||
if (status == KERN_OK) {
|
||||
printk("read %zu bytes from /dev/block/disk0:", nread);
|
||||
for (int i = 0; i < sizeof buf; i++) {
|
||||
printk("%02xh", buf[i]);
|
||||
}
|
||||
} else {
|
||||
printk("failed to read from block device (%s)", kern_status_string(status));
|
||||
}
|
||||
} else {
|
||||
printk("cannot open block device (%s)", kern_status_string(status));
|
||||
}
|
||||
|
||||
hang();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user