Skip to content

Commit

Permalink
Implemented low level disk functionality in C
Browse files Browse the repository at this point in the history
  • Loading branch information
nibblebits committed Sep 16, 2020
1 parent 4d9f9a6 commit 7d35fb9
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Makefile
@@ -1,4 +1,4 @@
FILES = ./build/kernel.asm.o ./build/kernel.o ./build/idt/idt.asm.o ./build/idt/idt.o ./build/memory/memory.o ./build/io/io.asm.o ./build/memory/heap/heap.o ./build/memory/heap/kheap.o ./build/memory/paging/paging.o ./build/memory/paging/paging.asm.o
FILES = ./build/kernel.asm.o ./build/kernel.o ./build/disk/disk.o ./build/idt/idt.asm.o ./build/idt/idt.o ./build/memory/memory.o ./build/io/io.asm.o ./build/memory/heap/heap.o ./build/memory/heap/kheap.o ./build/memory/paging/paging.o ./build/memory/paging/paging.asm.o
INCLUDES = -I./src
FLAGS = -g -ffreestanding -falign-jumps -falign-functions -falign-labels -falign-loops -fstrength-reduce -fomit-frame-pointer -finline-functions -Wno-unused-function -fno-builtin -Werror -Wno-unused-label -Wno-cpp -Wno-unused-parameter -nostdlib -nostartfiles -nodefaultlibs -Wall -O0 -Iinc

Expand Down Expand Up @@ -45,6 +45,8 @@ all: ./bin/boot.bin ./bin/kernel.bin
./build/memory/paging/paging.asm.o: ./src/memory/paging/paging.asm
nasm -f elf -g ./src/memory/paging/paging.asm -o ./build/memory/paging/paging.asm.o

./build/disk/disk.o: ./src/disk/disk.c
i686-elf-gcc $(INCLUDES) -I./src/disk $(FLAGS) -std=gnu99 -c ./src/disk/disk.c -o ./build/disk/disk.o

clean:
rm -rf ./bin/boot.bin
Expand Down
Empty file added build/disk/.keep
Empty file.
30 changes: 30 additions & 0 deletions src/disk/disk.c
@@ -0,0 +1,30 @@
#include "io/io.h"
int disk_read_sector(int lba, int total, void* buf)
{
outb(0x1F6, (lba >> 24) | 0xE0);
outb(0x1F2, total);
outb(0x1F3, (unsigned char)(lba & 0xff));
outb(0x1F4, (unsigned char)(lba >> 8));
outb(0x1F5, (unsigned char)(lba >> 16));
outb(0x1F7, 0x20);

unsigned short* ptr = (unsigned short*) buf;
for (int b = 0; b < total; b++)
{
// Wait for the buffer to be ready
char c = insb(0x1F7);
while(!(c & 0x08))
{
c = insb(0x1F7);
}

// Copy from hard disk to memory
for (int i = 0; i < 256; i++)
{
*ptr = insw(0x1F0);
ptr++;
}

}
return 0;
}
6 changes: 6 additions & 0 deletions src/disk/disk.h
@@ -0,0 +1,6 @@
#ifndef DISK_H
#define DISK_H

int disk_read_sector(int lba, int total, void* buf);

#endif
2 changes: 1 addition & 1 deletion src/io/io.asm
@@ -1,7 +1,7 @@
section .asm

global insb
global insws
global insw
global outb
global outw

Expand Down
12 changes: 3 additions & 9 deletions src/kernel.c
Expand Up @@ -4,6 +4,7 @@
#include "idt/idt.h"
#include "memory/heap/kheap.h"
#include "memory/paging/paging.h"
#include "disk/disk.h"

uint16_t* video_mem = 0;
uint16_t terminal_row = 0;
Expand Down Expand Up @@ -90,19 +91,12 @@ void kernel_main()
// Switch to kernel paging chunk
paging_switch(paging_4gb_chunk_get_directory(kernel_chunk));

char* ptr = kzalloc(4096);
paging_set(paging_4gb_chunk_get_directory(kernel_chunk), (void*)0x1000, (uint32_t)ptr | PAGING_ACCESS_FROM_ALL | PAGING_IS_PRESENT | PAGING_IS_WRITEABLE);

// Enable paging
enable_paging();

char* ptr2 = (char*) 0x1000;
ptr2[0] = 'A';
ptr2[1] = 'B';
print(ptr2);

print(ptr);

char buf[512];
disk_read_sector(0, 1, buf);

// Enable the system interrupts
enable_interrupts();
Expand Down

0 comments on commit 7d35fb9

Please sign in to comment.