Program loading, build system for apps, and badly-made lib linking.

This commit is contained in:
xamidev
2024-09-14 22:39:06 +02:00
parent 87bb1d96fd
commit 03b712ace0
8 changed files with 109 additions and 4 deletions

View File

@@ -8,6 +8,7 @@
#include "../libc/string.h"
#include "initrd.h"
#include "system.h"
#include "kheap.h"
static unsigned int octal_to_int(const char* str, size_t size)
{
@@ -31,6 +32,18 @@ uint32_t tar_parse_size(const char* in)
return size;
}
uint32_t tar_get_size(tar_header_t* header)
{
uint32_t size = 0;
char* size_str = header->size;
for (int i=0; i<11 && size_str[i] != '\0'; i++)
{
size = size*8 + (size_str[i]-'0');
}
return size;
}
void tar_find_file(uint8_t *tar_start, const char* filename)
{
uint8_t *ptr = tar_start;
@@ -174,3 +187,52 @@ uint32_t tar_get_file_size(uint8_t* initrd, const char* filename)
}
return -1;
}
tar_header_t* tar_find(uint8_t* initrd, const char* filename)
{
tar_header_t* header = (tar_header_t*)initrd;
while (header->filename[0] != '\0')
{
if (strcmp(header->filename, filename) == 0)
{
return header;
}
uint32_t file_size = tar_get_size(header);
uint32_t file_blocks = (file_size + 511)/512;
header = (tar_header_t*) ((uintptr_t)header+(file_blocks+1)*512);
}
return NULL;
}
void* tar_get_file_content(tar_header_t* header)
{
return (void*) ((uintptr_t)header+512);
}
void* load_file_from_initrd(uint8_t* initrd, const char* filename)
{
tar_header_t* file = tar_find(initrd, filename);
if (file == NULL)
{
printf("'%s' not found\n", filename);
return NULL;
}
uint32_t file_size = tar_get_size(file);
void* file_data = malloc(file_size);
if (file_data == NULL)
{
printf("Malloc error for file '%s'\n", filename);
return NULL;
}
void* file_content = tar_get_file_content(file);
memcpy(file_data, file_content, file_size);
printf("Loaded '%s' at 0x%x, size=%u\n", filename, (unsigned int)file_data, file_size);
return file_data;
}

View File

@@ -35,5 +35,6 @@ void ls_initrd(uint8_t* initrd, int verbose);
void cat_initrd(uint8_t* initrd, const char* filename);
int tar_file_to_buffer(uint8_t* initrd, const char* filename, char* buffer);
uint32_t tar_get_file_size(uint8_t* initrd, const char* filename);
void* load_file_from_initrd(uint8_t* initrd, const char* filename);
#endif

View File

@@ -118,6 +118,16 @@ void kmain(multiboot2_info *mb_info)
printf("[debug] malloc test ptr1=0x%x, ptr2=0x%x\n", (unsigned int)ptr1, (unsigned int)ptr2);
free(ptr1); free(ptr2);
// usually the place where i do testing
void* binary_file = load_file_from_initrd((uint8_t*)initrd_addr, "./hello.bin");
if (binary_file == NULL)
{
printf("NOT LOADED...\n");
} else {
printf("LOADED!\n");
}
timer_install();
keyboard_install();
printf("[kernel] spawning shell...\n");

6
src/programs/hello.c Normal file
View File

@@ -0,0 +1,6 @@
#include "../libc/stdio.h"
void main()
{
printf("Hello, world, from a PROGRAM!\n");
}