Skip to content

Commit

Permalink
Directory listing, kheap and pmm
Browse files Browse the repository at this point in the history
Kheap and pmm, plus we can list directories now! As well, some bugfixes with the ext2 filesystem inode read.
  • Loading branch information
xing1357 committed Aug 1, 2022
1 parent 3b428a7 commit a150e89
Show file tree
Hide file tree
Showing 57 changed files with 1,191 additions and 212 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ext2.img
commit.txt
39 changes: 36 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ MKDIR= mkdir -p
CP = cp -f
DEFINES=

# qemu
QEMU= qemu-system-i386
QEMU_FLAGS= -cdrom out/SimpleOS.iso -drive file=ext2.img,format=raw

# assembler flags
ASM_FLAGS = -f elf32
# compiler flags
Expand All @@ -36,16 +40,19 @@ TARGET_ISO=$(OUT)/SimpleOS.iso
ISO_DIR=$(OUT)/isodir

OBJECTS=$(ASM_OBJ)/entry.o $(ASM_OBJ)/load_gdt.o\
$(ASM_OBJ)/load_idt.o $(ASM_OBJ)/exception.o $(ASM_OBJ)/irq.o\
$(ASM_OBJ)/load_idt.o $(ASM_OBJ)/exception.o $(ASM_OBJ)/irq.o $(ASM_OBJ)/load_tss.o\
$(OBJ)/io_ports.o $(OBJ)/vga.o\
$(OBJ)/string.o $(OBJ)/console.o\
$(OBJ)/gdt.o $(OBJ)/idt.o $(OBJ)/isr.o $(OBJ)/8259_pic.o\
$(OBJ)/keyboard.o $(OBJ)/mouse.o\
$(OBJ)/kernel.o\
$(OBJ)/gui.o\
$(OBJ)/hd.o\
$(OBJ)/ext2.o

$(OBJ)/ext2.o\
$(OBJ)/ide.o\
$(OBJ)/tss.o\
$(OBJ)/kheap.o\
$(OBJ)/pmm.o

all: $(OBJECTS)
@printf "[ linking... ]\n"
Expand Down Expand Up @@ -84,6 +91,11 @@ $(ASM_OBJ)/irq.o : $(ASM_SRC)/irq.asm
$(ASM) $(ASM_FLAGS) $(ASM_SRC)/irq.asm -o $(ASM_OBJ)/irq.o
@printf "\n"

$(ASM_OBJ)/load_tss.o : $(ASM_SRC)/load_tss.asm
@printf "[ $(ASM_SRC)/irq.asm ]\n"
$(ASM) $(ASM_FLAGS) $(ASM_SRC)/load_tss.asm -o $(ASM_OBJ)/load_tss.o
@printf "\n"

$(OBJ)/io_ports.o : $(SRC)/io_ports.c
@printf "[ $(SRC)/io_ports.c ]\n"
$(CC) $(CC_FLAGS) -c $(SRC)/io_ports.c -o $(OBJ)/io_ports.o
Expand Down Expand Up @@ -154,7 +166,28 @@ $(OBJ)/ext2.o : $(SRC)/ext2.c
$(CC) $(CC_FLAGS) -c $(SRC)/ext2.c -o $(OBJ)/ext2.o
@printf "\n"

$(OBJ)/ide.o : $(SRC)/ide.c
@printf "[ $(SRC)/ide.c ]\n"
$(CC) $(CC_FLAGS) -c $(SRC)/ide.c -o $(OBJ)/ide.o
@printf "\n"

$(OBJ)/tss.o : $(SRC)/tss.c
@printf "[ $(SRC)/tss.c ]\n"
$(CC) $(CC_FLAGS) -c $(SRC)/tss.c -o $(OBJ)/tss.o
@printf "\n"

$(OBJ)/kheap.o : $(SRC)/kheap.c
@printf "[ $(SRC)/kheap.c ]\n"
$(CC) $(CC_FLAGS) -c $(SRC)/kheap.c -o $(OBJ)/kheap.o
@printf "\n"

$(OBJ)/pmm.o : $(SRC)/pmm.c
@printf "[ $(SRC)/pmm.c ]\n"
$(CC) $(CC_FLAGS) -c $(SRC)/pmm.c -o $(OBJ)/pmm.o
@printf "\n"

run:
$(QEMU) $(QEMU_FLAGS)

clean:
rm -f $(OBJ)/*.o
Expand Down
4 changes: 0 additions & 4 deletions config/grub.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
set timeout=0
set default=0

menuentry "SimpleOS" {
multiboot /boot/SimpleOS.bin
boot
}
10 changes: 10 additions & 0 deletions include/ext2.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ typedef struct ext2_inode
unsigned char os_specific[12];
} ext2_inode;

typedef struct ext2_dirent
{
uint32 inode;
uint32 dirent_size;
uint32 name_len;
uint32 prev_name_len;
uint32 type;
char* name;
} ext2_dirent;

ext2_superblock sb;
ext2_bgdt bgdt;

Expand Down
8 changes: 0 additions & 8 deletions include/hd.h

This file was deleted.

34 changes: 33 additions & 1 deletion include/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,38 @@ extern uint8 __kernel_rodata_section_end;
extern uint8 __kernel_bss_section_start;
extern uint8 __kernel_bss_section_end;

#endif

typedef struct {
struct {
uint32 k_start_addr;
uint32 k_end_addr;
uint32 k_len;
uint32 text_start_addr;
uint32 text_end_addr;
uint32 text_len;
uint32 data_start_addr;
uint32 data_end_addr;
uint32 data_len;
uint32 rodata_start_addr;
uint32 rodata_end_addr;
uint32 rodata_len;
uint32 bss_start_addr;
uint32 bss_end_addr;
uint32 bss_len;
} kernel;

struct {
uint32 total_memory;
} system;

struct {
uint32 start_addr;
uint32 end_addr;
uint32 size;
} available;
} KERNEL_MEMORY_MAP;

extern KERNEL_MEMORY_MAP g_kmap;

#endif

55 changes: 55 additions & 0 deletions include/kheap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef KHEAP_H
#define KHEAP_H

#include "types.h"

// a singly linked list heap block
typedef struct _kheap_block {
struct {
uint32 size; // memory size
uint8 is_free; // block is free or not
} metadata;
struct _kheap_block *next;
void *data; // data pointer
} __attribute__((packed)) KHEAP_BLOCK;

/**
* initialize heap and set total memory size
*/
int kheap_init(void *start_addr, void *end_addr);

/**
* increase the heap memory by size & get its address
*/
void *kbrk(int size);

/**
* print list of allocated blocks
*/
void kheap_print_blocks();

/**
* allocate given size if list is null
* otherwise try some memory allocation algorithm like best fit etc
* to find best block to allocate
* # Need to work on internal/external segmentaion problem
*/
void *kmalloc(int size);

/**
* allocate memory n * size & zeroing out
*/
void *kcalloc(int n, int size);

/**
* allocate a new block of memory
* copy previous block data & set free the previous block
*/
void *krealloc(void *ptr, int size);

/**
* set free the block
*/
void kfree(void *addr);

#endif
118 changes: 93 additions & 25 deletions include/multiboot.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,112 @@ Licensed under MIT ( https://github.com/xing1357/SimpleOS/blob/main/LICENSE )

#include "types.h"

#define MULTIBOOT_FLAG_MEM 0x001
#define MULTIBOOT_FLAG_DEVICE 0x002
#define MULTIBOOT_FLAG_CMDLINE 0x004
#define MULTIBOOT_FLAG_MODS 0x008
#define MULTIBOOT_FLAG_AOUT 0x010
#define MULTIBOOT_FLAG_ELF 0x020
#define MULTIBOOT_FLAG_MMAP 0x040
#define MULTIBOOT_FLAG_CONFIG 0x080
#define MULTIBOOT_FLAG_LOADER 0x100
#define MULTIBOOT_FLAG_APM 0x200
#define MULTIBOOT_FLAG_VBE 0x400

struct multiboot
{
#define MULTIBOOT_MAGIC_HEADER 0x1BADB002
#define MULTIBOOT_BOOTLOADER_MAGIC 0x2BADB002

/* The Multiboot header. */
typedef struct {
uint32 magic;
uint32 flags;
uint32 mem_lower;
uint32 mem_upper;
uint32 boot_device;
uint32 cmdline;
uint32 mods_count;
uint32 mods_addr;
uint32 checksum;
uint32 header_addr;
uint32 load_addr;
uint32 load_end_addr;
uint32 bss_end_addr;
uint32 entry_addr;
} MULTIBOOT_HEADER;

/* The symbol table for a.out. */
typedef struct {
uint32 tabsize;
uint32 strsize;
uint32 addr;
uint32 reserved;
} AOUT_SYMBOL_TABLE;

/* The section header table for ELF. */
typedef struct {
uint32 num;
uint32 size;
uint32 addr;
uint32 shndx;
} ELF_SECTION_HEADER_TABLE;

typedef struct {
/* required, defined in entry.asm */
uint32 flags;

/* available low-high memory from BIOS, present if flags[0] is set(MEMINFO in entry.asm) */
uint32 mem_low;
uint32 mem_high;

/* "root" partition, present if flags[1] is set(BOOTDEVICE in entry.asm) */
uint32 boot_device;

/* kernel command line, present if flags[2] is set(CMDLINE in entry.asm) */
uint32 cmdline;

/* no of modules loaded, present if flags[3] is set(MODULECOUNT in entry.asm) */
uint32 modules_count;
uint32 modules_addr;

/* symbol table info, present if flags[4] & flags[5] is set(SYMT in entry.asm) */
union {
AOUT_SYMBOL_TABLE aout_sym;
ELF_SECTION_HEADER_TABLE elf_sec;
} u;

/* memory mapping, present if flags[6] is set(MEMMAP in entry.asm) */
uint32 mmap_length;
uint32 mmap_addr;

/* drive info, present if flags[7] is set(DRIVE in entry.asm) */
uint32 drives_length;
uint32 drives_addr;

/* ROM configuration table, present if flags[8] is set(CONFIGT in entry.asm) */
uint32 config_table;

/* boot loader name, present if flags[9] is set(BOOTLDNAME in entry.asm) */
uint32 boot_loader_name;

/* Advanced Power Management(APM) table, present if flags[10] is set(APMT in entry.asm) */
uint32 apm_table;

/* video info, present if flags[11] is set(VIDEO in entry.asm) */
uint32 vbe_control_info;
uint32 vbe_mode_info;
uint32 vbe_mode;
uint32 vbe_interface_seg;
uint32 vbe_interface_len;
} __attribute__((packed));
uint16 vbe_mode;
uint16 vbe_interface_seg;
uint16 vbe_interface_off;
uint16 vbe_interface_len;

/* video framebufer info, present if flags[12] is set(VIDEO_FRAMEBUF in entry.asm) */
uint64 framebuffer_addr;
uint32 framebuffer_pitch;
uint32 framebuffer_width;
uint32 framebuffer_height;
uint8 framebuffer_bpp;
uint8 framebuffer_type; // indexed = 0, RGB = 1, EGA = 2

} MULTIBOOT_INFO;

typedef struct multiboot_header multiboot_header_t;

typedef enum {
MULTIBOOT_MEMORY_AVAILABLE = 1,
MULTIBOOT_MEMORY_RESERVED,
MULTIBOOT_MEMORY_ACPI_RECLAIMABLE,
MULTIBOOT_MEMORY_NVS,
MULTIBOOT_MEMORY_BADRAM
} MULTIBOOT_MEMORY_TYPE;

typedef struct {
uint32 size;
uint32 addr_low;
uint32 addr_high;
uint32 len_low;
uint32 len_high;
MULTIBOOT_MEMORY_TYPE type;
} MULTIBOOT_MEMORY_MAP;

#endif
Loading

0 comments on commit a150e89

Please sign in to comment.