Browse Source

partitions

master
Mathieu Serandour 1 year ago
parent
commit
1aad0f2831
  1. 59
      kernel/entry.c
  2. 0
      kernel/fs/fs.h
  3. 107
      kernel/fs/gpt.c
  4. 13
      kernel/fs/gpt.h

59
kernel/entry.c

@ -16,6 +16,8 @@
#include "drivers/pcie/pcie.h"
#include "drivers/pcie/scan.h"
#include "fs/gpt.h"
#include "memory/gdt.h"
#include "memory/physical_allocator.h"
#include "memory/paging.h"
@ -95,11 +97,11 @@ extern const char _binary_bootmessage_txt;
static void read_modules(unsigned module_count,
struct stivale2_module* modules) {
const struct stivale2_module* modules) {
for(unsigned i = 0; i < module_count; i++) {
struct stivale2_module* module = &modules[i];
const struct stivale2_module* module = &modules[i];
if(!strcmp(module->string, "kernel.symbols")) {
stacktrace_file(module->begin);
stacktrace_file((void*)module->begin);
}
}
}
@ -160,14 +162,14 @@ void _start(struct stivale2_struct *stivale2_struct) {
const struct stivale2_struct_tag_framebuffer* framebuffer_tag;
const struct stivale2_struct_tag_rsdp* rsdp_tag_ptr;
const struct stivale2_struct_tag_modules* modules_tag;
//const struct stivale2_struct_tag_boot_volume* boot_volume_tag;
const struct stivale2_struct_tag_boot_volume* boot_volume_tag;
term_str_tag = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_TERMINAL_ID);
memmap_tag = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_MEMMAP_ID);
framebuffer_tag = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID);
rsdp_tag_ptr = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_RSDP_ID);
modules_tag = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_MODULES_ID);
//boot_volume_tag = stivale2_get_tag(stivale2_struct, 0x9b4358364c19ee62);
boot_volume_tag = stivale2_get_tag(stivale2_struct, STIVALE2_STRUCT_TAG_BOOT_VOLUME_ID);
// term_str_tag == NULL is not a blocking
@ -184,7 +186,7 @@ void _start(struct stivale2_struct *stivale2_struct) {
}
else
log_warn("no stivale2 modules found");
// the default terminal handler does nothing
// print all logging messages
set_logging_level(LOG_LEVEL_DEBUG);
@ -197,7 +199,6 @@ void _start(struct stivale2_struct *stivale2_struct) {
set_backend_print_fun(empty_terminal_handler);
append_paging_initialization();
//set_terminal_handler(empty_terminal_handler);
@ -207,6 +208,7 @@ void _start(struct stivale2_struct *stivale2_struct) {
// drivers
atshutdown(remove_all_drivers);
atshutdown(free_all_devices);
atshutdown(gpt_cleanup);
video_init(framebuffer_tag);
@ -214,10 +216,6 @@ void _start(struct stivale2_struct *stivale2_struct) {
set_backend_print_fun(print_fun);
// first initialize our terminal
// we cannot use stivale2 terminal
// after loading our gdt
// so we need to load our gdt after our
// terminal is successfully installed
init_gdt_table();
@ -232,19 +230,54 @@ void _start(struct stivale2_struct *stivale2_struct) {
hpet_init();
apic_setup_clock();
pcie_init();
pic_init();
ps2kb_init();
ps2kb_set_event_callback(kbhandler);
disk_part_t* part;// = find_partition(*(GUID*)&boot_volume_tag->part_guid);
if(part) {
}
else {
log_warn(
"cannot find main partition! (boot volume GUID: {%8x-%4x-%4x-%2x%2x-%2x%2x%2x%2x%2x%2x})",
boot_volume_tag->part_guid.a,
boot_volume_tag->part_guid.b,
boot_volume_tag->part_guid.c,
boot_volume_tag->part_guid.d[0],
boot_volume_tag->part_guid.d[1],
boot_volume_tag->part_guid.d[2],
boot_volume_tag->part_guid.d[3],
boot_volume_tag->part_guid.d[4],
boot_volume_tag->part_guid.d[5],
boot_volume_tag->part_guid.d[6],
boot_volume_tag->part_guid.d[7]
);
part = search_partition("Bincows");
if(part) {
log_warn(
"Bincows partition found on device %s (partition %u). "
"This partition will be mounted as the main partition.",
part->interface->driver->device->name,
part->id);
}
else {
panic("no Bincows partition found.\n");
}
}
//printf("issou");
//printf("issou");
//log_info("%x allocated heap blocks", get_n_allocation());
for(;;) {
asm volatile("hlt");

0
kernel/fs/fs.h

107
kernel/fs/gpt.c

@ -8,6 +8,13 @@
#include "gpt.h"
// return 0 if equals, non zero else
inline int guidcmp(GUID const a, GUID const b) {
return ((a.high ^ b.high) | (a.low ^ b.low));
}
struct gpt_partition_descriptor {
GUID type_guid;
GUID partition_guid;
@ -17,18 +24,66 @@ struct gpt_partition_descriptor {
uint16_t name[];
} __attribute__((packed));
static partition_t* partitions = NULL;
static unsigned n_partitions = 0;
static void register_partition(partition_t p) {
static void register_partition(partition_t* p) {
unsigned last = n_partitions++;
partitions = realloc(partitions, n_partitions * sizeof(partition_t));
partitions[last] = p;
partitions[last] = *p;
log_info(
"GPT Partition %s\n"
"LBA begin: %lx\n"
"LBA end: %lx\n"
"attributes: %lx\n"
"type: %x\n"
"guid: %lx-%lx\n",
partitions[last].name,
partitions[last].begin,
partitions[last].end,
partitions[last].attributes,
partitions[last].type,
partitions[last].guid.low,
partitions[last].guid.high
);
}
partition_t* find_partition(GUID guid) {
for(unsigned i = 0; i < n_partitions; i++) {
if(!guidcmp(partitions[i].guid, guid))
return &partitions[i];
}
return NULL;
}
partition_t* search_partition(const char* name) {
for(unsigned i = 0; i < n_partitions; i++)
if(!strcmp(partitions[i].name, name))
return &partitions[i];
return NULL;
}
void gpt_cleanup(void) {
if(partitions != NULL)
free(partitions);
n_partitions = 0;
}
static GUID makeGUID(
uint32_t a, uint16_t b,
uint16_t c, uint16_t d,
@ -39,16 +94,6 @@ static GUID makeGUID(
};
}
int issouguidcmp(GUID const a, GUID const b) {
return ((a.high ^ b.high) | (a.low ^ b.low));
}
// return 0 if equals, non zero else
inline int guidcmp(GUID const a, GUID const b) {
log_info("issou %lx, %lx", a.high , b.high);
return ((a.high ^ b.high) | (a.low ^ b.low));
}
uint32_t get_type(GUID guid) {
@ -86,6 +131,7 @@ int issouguidcmp(GUID const a, GUID const b) {
void gpt_scan(const struct storage_interface* sti) {
void* buffer = malloc(1 << sti->lbashift);
// LBA 1: Partition Table Header
@ -106,15 +152,6 @@ void gpt_scan(const struct storage_interface* sti) {
unsigned partition_entry_size = *(uint32_t*)(buffer+0x54);
log_warn(
"n_partitions=%lx, partition_entry_array_lba=%lx, "
"partition_entry_size=%x",
n_partitions,
partition_entry_array_lba,
partition_entry_size
);
unsigned size = (partition_entry_size * n_partitions);
buffer = realloc(buffer, size);
@ -137,42 +174,20 @@ void gpt_scan(const struct storage_interface* sti) {
entry->type_guid.high == 0)
continue;
log_warn(
"typeguid: %lx-%lx\n",
entry->type_guid.low,
entry->type_guid.high);
partition_t p = (partition_t) {
.begin = entry->begin,
.end = entry->end,
.attributes = entry->attributes,
.type = get_type(entry->type_guid),
.id = i,
.partition_guid = entry->partition_guid,
.guid = entry->partition_guid,
.interface = sti,
};
utf16le2ascii(p.name, entry->name, 35);
log_info(
"GPT Partition %s\n"
"LBA begin: %lx\n"
"LBA end: %lx\n"
"attributes: %lx\n"
"type: %x\n"
"typeguid: %lx-%lx\n",
p.name,
entry->begin,
entry->end,
entry->attributes,
p.type,
entry->type_guid.low,
entry->type_guid.high
);
register_partition(p);
register_partition(&p);
}
free(buffer);

13
kernel/fs/gpt.h

@ -5,22 +5,22 @@
typedef struct {
uint64_t low, high;
} GUID;
} __attribute__((packed)) GUID;
typedef struct partition {
typedef struct disk_part {
const struct storage_interface* interface;
uint32_t type;
GUID partition_guid;
uint32_t id;
GUID guid;
uint64_t begin;
uint64_t end;
uint64_t attributes;
// null terminated
char name[36];
} partition_t;
} disk_part_t;
#define PARTITION_UNKNOWNED 0
@ -39,7 +39,8 @@ typedef struct partition {
void gpt_scan(const struct storage_interface* sti);
partition_t* find_partition(GUID guid);
disk_part_t* find_partition(GUID guid);
disk_part_t* search_partition(const char* name);
// release partition information memory
void gpt_cleanup(void);

Loading…
Cancel
Save