summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2026-02-24 21:12:39 +0100
committerCarlos Maiolino <[email protected]>2026-02-24 21:12:39 +0100
commit150499dbea44a4ecf1689990ed915467a14abb02 (patch)
tree4821b1a6438a6b4728c26d5f4e3c745757f3987e
parentc96b289dc49c18ac36fb7e13242e5f855a56840e (diff)
block: Add a simple block layer
Signed-off-by: Carlos Maiolino <[email protected]>
-rw-r--r--Makefile7
-rw-r--r--src/block/block.c44
-rw-r--r--src/include/ata/ata.h6
-rw-r--r--src/include/block/block.h16
-rw-r--r--src/include/toxic/config.h1
-rw-r--r--src/kernel.c5
6 files changed, 76 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 365b7b9..2414134 100644
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,8 @@ KOBJ_FILES = $(KERNEL_ASM_OBJ) \
./build/mm/kernel_heap.o \
./build/mm/paging.asm.o \
./build/mm/paging.o \
- ./build/drivers/ata.o
+ ./build/drivers/ata.o \
+ ./build/block/block.o
BOOT_TGT =./bin/boot.bin
@@ -84,6 +85,9 @@ $(BUILD_DIR)/mm/paging.o: ./src/mm/paging.c
$(BUILD_DIR)/drivers/ata.o: ./src/drivers/ata.c
i686-elf-gcc $(INCLUDES) $(FLAGS) -std=gnu99 -c ./src/drivers/ata.c -o $(BUILD_DIR)/drivers/ata.o
+$(BUILD_DIR)/block/block.o: ./src/block/block.c
+ i686-elf-gcc $(INCLUDES) $(FLAGS) -std=gnu99 -c ./src/block/block.c -o $(BUILD_DIR)/block/block.o
+
clean:
rm -f $(BOOT_TGT)
rm -f bin/kernel.bin
@@ -97,4 +101,5 @@ clean:
rm -f build/io/io.asm.o
rm -f build/mm/*.o
rm -f build/drivers/*.o
+ rm -f build/block/*.o
rm -f bin/os.bin
diff --git a/src/block/block.c b/src/block/block.c
new file mode 100644
index 0000000..d4758f5
--- /dev/null
+++ b/src/block/block.c
@@ -0,0 +1,44 @@
+#include <toxic/config.h>
+#include <toxic/string.h>
+#include <block/block.h>
+#include <ata/ata.h>
+
+/*
+ * Only primary ATA disk is supported by now, just
+ * hardcode it here.
+ */
+struct bdev hda;
+
+/* Init block layer */
+void
+block_init()
+{
+ memset(&hda, 0, sizeof(struct bdev));
+ hda.id = TOXIC_HDA_0;
+ hda.sector_size = SECTOR_SIZE;
+}
+
+struct bdev *
+bdev_get(int id)
+{
+ /* Just a single bdev is supported */
+ if (id)
+ return NULL;
+
+ return &hda;
+}
+
+/* Read len bytes from bdev starting at addr into buf*/
+int
+bread(
+ struct bdev *bdev,
+ unsigned int addr,
+ unsigned int len,
+ void *buf)
+{
+ /*
+ * XXX: This should be a function call into the disk driver.
+ * i.e. bdev->read(), or something like that
+ */
+ return ata_read_sector(addr, len, buf);
+}
diff --git a/src/include/ata/ata.h b/src/include/ata/ata.h
new file mode 100644
index 0000000..2ea73bd
--- /dev/null
+++ b/src/include/ata/ata.h
@@ -0,0 +1,6 @@
+#ifndef ATA_H
+#define ATA_H
+
+int ata_read_sector(int lba, int total, void *buf);
+
+#endif /* ATA_H */
diff --git a/src/include/block/block.h b/src/include/block/block.h
new file mode 100644
index 0000000..eedff88
--- /dev/null
+++ b/src/include/block/block.h
@@ -0,0 +1,16 @@
+#ifndef BLOCK_H
+#define BLOCK_H
+
+#define TOXIC_HDA_0 0
+
+struct bdev {
+ unsigned int id;
+ unsigned int sector_size;
+};
+
+void block_init(void);
+struct bdev * bdev_get(int id);
+int bread( struct bdev *bdev, unsigned int addr,
+ unsigned int len, void *buf);
+
+#endif /* BLOCK_H */
diff --git a/src/include/toxic/config.h b/src/include/toxic/config.h
index 19d05a9..c1c1e7f 100644
--- a/src/include/toxic/config.h
+++ b/src/include/toxic/config.h
@@ -8,4 +8,5 @@
/* Kernel heap settings */
#define PAGE_SIZE (4096)
+#define SECTOR_SIZE (512)
#endif /* CONFIG_H */
diff --git a/src/kernel.c b/src/kernel.c
index d917bd8..373d65d 100644
--- a/src/kernel.c
+++ b/src/kernel.c
@@ -7,7 +7,7 @@
#include <toxic/io.h>
#include <mm/kernel_heap.h>
#include <mm/paging.h>
-#include <ata/ata.h>
+#include <block/block.h>
static struct page_directory *kernel_directory;
@@ -16,6 +16,7 @@ void start_kernel()
char buf[512] = {0};
init_display(2);
+ block_init();
vprintl("Hello World!!!\n");
vprintl("Testing it!!!\n");
@@ -51,5 +52,5 @@ void start_kernel()
kfree(ptr);
- ata_read_sector(0, 1, buf);
+ bread(bdev_get(0), 0, 1, buf);
}