2019-04-26 10:03:16 -05:00
|
|
|
#include <grub/text_fb_info.h>
|
2019-04-27 09:51:32 -05:00
|
|
|
#include <stdlib.h>
|
2019-05-06 08:38:02 -05:00
|
|
|
#include <tasking.h>
|
2019-04-27 09:51:32 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <memory.h>
|
2019-04-27 15:03:31 -05:00
|
|
|
#include <grub/multiboot2.h>
|
2019-04-27 09:51:32 -05:00
|
|
|
#include <stdint.h>
|
2019-05-06 08:38:02 -05:00
|
|
|
#include "../cpu/cpu_init.h"
|
2019-05-22 14:18:29 -05:00
|
|
|
#include "vga_err.h"
|
2019-05-06 08:38:02 -05:00
|
|
|
#include "elf.h"
|
2019-04-27 09:51:32 -05:00
|
|
|
|
2019-05-23 17:38:26 -05:00
|
|
|
typedef struct {
|
|
|
|
char filename[100];
|
|
|
|
char mode[8];
|
|
|
|
char uid[8];
|
|
|
|
char gid[8];
|
|
|
|
char size[12];
|
|
|
|
char mtime[12];
|
|
|
|
char chksum[8];
|
|
|
|
char typeflag[1];
|
|
|
|
} tar_header;
|
|
|
|
|
2019-04-27 09:51:32 -05:00
|
|
|
static long initrd_sz;
|
|
|
|
static char* initrd;
|
|
|
|
typedef int (*func_ptr)();
|
2019-05-01 09:39:23 -05:00
|
|
|
static struct multiboot_boot_header_tag* tags;
|
2019-04-27 15:03:31 -05:00
|
|
|
|
2019-05-01 09:39:23 -05:00
|
|
|
static void read_initrd(struct multiboot_boot_header_tag* tags) {
|
|
|
|
struct multiboot_tag* tag=(struct multiboot_tag*)(tags+1);
|
|
|
|
while (tag->type!=0) {
|
|
|
|
switch (tag->type) {
|
|
|
|
case MULTIBOOT_TAG_TYPE_MODULE: {
|
|
|
|
struct multiboot_tag_module* mod=(struct multiboot_tag_module*) tag;
|
|
|
|
initrd=malloc(sizeof(char)*(mod->mod_end-mod->mod_start));
|
|
|
|
initrd_sz=mod->mod_end-mod->mod_start;
|
2019-05-05 07:27:57 -05:00
|
|
|
memcpy(initrd,(void*)(mod->mod_start+0xC0000000),mod->mod_end-mod->mod_start);
|
2019-05-01 09:39:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tag=(struct multiboot_tag*)((char*)tag+((tag->size+7)&0xFFFFFFF8));
|
|
|
|
}
|
|
|
|
}
|
2019-04-27 09:51:32 -05:00
|
|
|
|
2019-05-23 17:38:26 -05:00
|
|
|
uint32_t getsize(const char *in) {
|
|
|
|
uint32_t size=0;
|
|
|
|
uint32_t j;
|
|
|
|
uint32_t count=1;
|
|
|
|
for (j=11;j>0;j--,count*=8) {
|
|
|
|
size+=((in[j-1]-'0')*count);
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
2019-05-03 08:34:22 -05:00
|
|
|
|
|
|
|
void kmain(struct multiboot_boot_header_tag* hdr) {
|
|
|
|
tags=hdr;
|
|
|
|
cpu_init(tags);
|
2019-05-22 14:18:29 -05:00
|
|
|
vga_init((char*)0xC00B8000);
|
2019-05-01 09:39:23 -05:00
|
|
|
read_initrd(tags);
|
2019-05-03 08:34:22 -05:00
|
|
|
int pos=0;
|
|
|
|
uint32_t datapos;
|
2019-05-23 17:38:26 -05:00
|
|
|
tar_header tar_hdr;
|
|
|
|
for (int i=0;;i++) {
|
|
|
|
char* tar_hdr_ptr=(char*)&tar_hdr;
|
|
|
|
for (size_t i=0;i<sizeof(tar_hdr);i++) {
|
|
|
|
tar_hdr_ptr[i]=initrd[pos+i];
|
|
|
|
}
|
|
|
|
if (tar_hdr.filename[0]=='\0') break;
|
|
|
|
uint32_t size=getsize(tar_hdr.size);
|
|
|
|
pos+=512;
|
|
|
|
if (strcmp(tar_hdr.filename,"init")==0) {
|
|
|
|
datapos=pos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos+=size;
|
2019-04-30 17:11:26 -05:00
|
|
|
}
|
2019-05-01 09:39:23 -05:00
|
|
|
elf_header header;
|
2019-05-03 08:34:22 -05:00
|
|
|
pos=datapos;
|
|
|
|
char* hdr_ptr=(char*)&header;
|
2019-05-05 07:27:57 -05:00
|
|
|
for (size_t i=0;i<sizeof(elf_header);i++) {
|
2019-05-03 08:34:22 -05:00
|
|
|
hdr_ptr[i]=initrd[pos];
|
|
|
|
pos++;
|
|
|
|
}
|
2019-05-01 09:39:23 -05:00
|
|
|
if (header.magic!=ELF_MAGIC) {
|
2019-05-03 08:34:22 -05:00
|
|
|
vga_write_string("[INFO] Invalid magic number for prog.elf\n");
|
2019-05-01 09:39:23 -05:00
|
|
|
} else {
|
2019-05-06 08:24:57 -05:00
|
|
|
void* cr3=new_address_space();
|
2019-05-01 09:39:23 -05:00
|
|
|
for (int i=0;i<header.pheader_ent_nm;i++) {
|
|
|
|
elf_pheader pheader;
|
2019-05-03 08:34:22 -05:00
|
|
|
pos=(header.prog_hdr)+(header.pheader_ent_sz*i)+datapos;
|
|
|
|
char* phdr_ptr=(char*)&pheader;
|
2019-05-05 07:27:57 -05:00
|
|
|
for (size_t i=0;i<sizeof(elf_pheader);i++) {
|
2019-05-03 08:34:22 -05:00
|
|
|
phdr_ptr[i]=initrd[pos];
|
|
|
|
pos++;
|
|
|
|
}
|
2019-05-06 08:24:57 -05:00
|
|
|
char* ptr=alloc_memory(((pheader.memsz)/4096)+1);
|
|
|
|
memset(ptr,0,pheader.memsz);
|
2019-05-01 09:39:23 -05:00
|
|
|
if (pheader.filesz>0) {
|
2019-05-03 08:34:22 -05:00
|
|
|
pos=pheader.offset+datapos;
|
2019-05-05 07:27:57 -05:00
|
|
|
for (size_t i=0;i<pheader.filesz;i++) {
|
2019-05-06 08:24:57 -05:00
|
|
|
ptr[i]=initrd[pos];
|
2019-05-03 08:34:22 -05:00
|
|
|
pos++;
|
|
|
|
}
|
2019-05-01 09:39:23 -05:00
|
|
|
}
|
2019-05-06 08:38:02 -05:00
|
|
|
copy_data(cr3,ptr,pheader.memsz,(void*)pheader.vaddr);
|
2019-05-06 08:24:57 -05:00
|
|
|
}
|
2019-05-23 20:21:51 -05:00
|
|
|
char* initrd2=alloc_memory((initrd_sz/4096)+1);
|
|
|
|
memcpy(initrd2,initrd,initrd_sz);
|
2019-05-24 08:54:14 -05:00
|
|
|
initrd2=put_data(cr3,initrd2,initrd_sz);
|
|
|
|
createTaskCr3Param((void*)header.entry,cr3,(uint32_t)initrd2,initrd_sz);
|
2019-05-06 08:24:57 -05:00
|
|
|
for(;;) {
|
|
|
|
yield();
|
2019-05-01 09:39:23 -05:00
|
|
|
}
|
2019-04-30 17:11:26 -05:00
|
|
|
}
|
2019-02-09 12:52:45 -06:00
|
|
|
}
|