start adding disk things

This commit is contained in:
2025-03-15 16:03:16 +03:00
parent fe1264d55d
commit a7a7121812
6 changed files with 181 additions and 2 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
bin/
myos.bin
boot.iso
+12 -1
View File
@@ -1,5 +1,5 @@
# Define the object files
OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/gdt.o
OBJS = bin/boot.o bin/kernel.o bin/idt.o bin/isr.o bin/screen.o bin/utils.o bin/string.o bin/stdio.o bin/gdt.o bin/disk.o
# Define the compiler and assembler
CC = gcc -D__is_katauos
@@ -18,6 +18,13 @@ OUTPUT = myos.bin
all: $(OUTPUT)
image:
cp myos.bin grubshit/boot/
grub-mkrescue -o boot.iso grubshit
test:
qemu-system-i386 -hda boot.iso -m 512M
$(OUTPUT): $(OBJS)
$(CC) -T $(LDSCRIPT) -o $@ $(OBJS) $(LDFLAGS)
@@ -48,8 +55,12 @@ bin/stdio.o: src/stdio.c
bin/gdt.o: src/gdt.c
$(CC) $(CFLAGS) -c $< -o $@
bin/disk.o: src/disk.c
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf bin/
rm -f $(OUTPUT)
rm boot.iso
.PHONY: all clean
+8
View File
@@ -0,0 +1,8 @@
set default=0
set timeout=5
menuentry "katauOS" {
multiboot /boot/myos.bin
# set gfxpayload=1280x720x32
boot
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef DISK_H
#define DISK_H
#include <stdint.h>
typedef struct {
unsigned short cylinders;
unsigned char heads;
unsigned char sectors;
} disk_info;
void get_hdd_geometry(uint32_t* cylinders, uint16_t* heads, uint32_t* sectors);
void get_disk_info(disk_info *info);
int ide_check_disk_present();
#endif
+133
View File
@@ -0,0 +1,133 @@
#include "../include/disk.h"
#include "../include/utils.h"
#include "../include/stdio.h"
// Define the IDE registers
#define IDE_CMD 0x1F7
#define IDE_DATA 0x1F0
#define IDE_SEC_COUNT 0x1F2
#define IDE_SEC_NUM 0x1F3
#define IDE_CYL_LOW 0x1F4
#define IDE_CYL_HIGH 0x1F5
#define IDE_HEAD 0x1F6
#define IDE_STATUS 0x1F7
#define IDE_COMMAND 0x1F7
#define STATUS_ERR 0x01
#define STATUS_BUSY 0x80
#define STATUS_DRQ 0x08
// Define the IDE commands
#define IDE_CMD_IDENTIFY 0xEC
void ide_send_command(unsigned char command) {
__asm__ __volatile__("outb %0, %1" : : "a" (command), "d" (IDE_COMMAND));
}
unsigned short ide_read_data() {
unsigned short data;
//__asm__ __volatile__("inw %1, %0" : "=a" (data) : "d" (IDE_DATA));
__asm__ __volatile__("inw %w1, %0" : "=a" (data) : "Nd" (IDE_DATA));
return data;
}
unsigned char ide_read_status() {
unsigned char status;
__asm__ __volatile__("inb %1, %0" : "=a" (status) : "d" (IDE_STATUS));
return status;
}
uint16_t inw(uint16_t port) {
uint16_t value;
__asm__ __volatile__("inw %w1, %w0" : "=a" (value) : "Nd" (port));
return value;
}
//https://wiki.osdev.org/ATA_PIO_Mode#IDENTIFY_command
void get_disk_info(disk_info *info) {
outportb(0x1F6, 0xA0);//select master drive, 0xA0 - master, 0xB0 - slave
//set sectorcount, LBAlo, LBAmid, LBAhi IO ports to 0
outportb(0x1F2, 0x0);
outportb(0x1F3, 0x0);
outportb(0x1F4, 0x0);
outportb(0x1F5, 0x0);
// Send the identify command
ide_send_command(IDE_CMD_IDENTIFY);//0xEC
uint8_t huy = inportb(0x1F7);
printf("huy is %X\n", huy);
if(huy == 0)
return;
// Wait for the data to be ready
while (ide_read_status() & STATUS_BUSY);
uint8_t lbaMID, lbaHI;
lbaMID = inportb(0x1F4);
lbaHI = inportb(0x1F5);
//TODO: FIX THIS FUCKERY
if(lbaMID != 0 || lbaHI != 0)
{
printf("ERROR! lbaMID: %X, lbaHI: %X\n", lbaMID, lbaHI);
return;
}
//while(!(ide_read_status() & STATUS_DRQ));//wait for the DRQ bit to be set
while(1)
{
uint8_t status = ide_read_status();
if(status & STATUS_DRQ || status & STATUS_ERR)
break;
}
// Read the data
unsigned short data[256];
for (int i = 0; i < 256; i++) {
data[i] = ide_read_data();
}
// Extract the disk information
info->cylinders = (data[1] >> 8) | (data[2] << 8);
info->heads = data[3];
info->sectors = data[6];
}
// Function to check if a disk is present
int ide_check_disk_present() {
// Send the identify command
ide_send_command(IDE_CMD_IDENTIFY);
// Wait for 1 second
for (int i = 0; i < 1000000; i++);
// Check the status register
unsigned char status;
__asm__ __volatile__("inb %1, %0" : "=a" (status) : "d" (IDE_STATUS));
// If the device is not busy and there's no error, it's likely a disk is present
if ((status & 0x80) == 0 && (status & 0x01) == 0) {
return 1; // Disk present
}
// If the device is busy, it might be a disk, so let's try again
if (status & 0x80) {
// Wait for the device to become ready
while ((ide_read_status() & 0x80)!= 0);
// Check the status register again
__asm__ __volatile__("inb %1, %0" : "=a" (status) : "d" (IDE_STATUS));
// If the device is not busy and there's no error, it's likely a disk is present
if ((status & 0x80) == 0 && (status & 0x01) == 0) {
return 1; // Disk present
}
}
return 0; // No disk present
}
+11
View File
@@ -3,6 +3,9 @@
#include "../include/screen.h"
#include "../include/stdio.h"
#include "../include/utils.h"
#include "../include/disk.h"
#include <stdint.h>
void kmain()
{
@@ -15,6 +18,14 @@ void kmain()
char yooo[256] = "heheh";
printf("test string: %s\n", yooo);
printf("checking for disk...\n");
int disk_present = ide_check_disk_present();
printf("we got the result, it's %X\n", disk_present);
disk_info *info;
get_disk_info(info);
printf("CYL HEAD SECT: %X %X %X\n", info->cylinders, info->heads, info->sectors);
while(1)
{
if(inportb(0x64) & 0x1)