os/init/main.c

131 lines
3.4 KiB
C
Raw Normal View History

#include <string.h>
2019-05-05 07:31:13 -05:00
#include "vga.h"
2019-05-03 08:34:22 -05:00
#include <grub/text_fb_info.h>
#include <ipc/vfs.h>
2019-06-22 10:31:33 -05:00
#include <elf.h>
2019-05-01 09:39:23 -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;
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;
}
int main(char* initrd, uint32_t initrd_sz) {
2019-05-03 08:34:22 -05:00
text_fb_info info;
2019-05-23 17:08:03 -05:00
info.address=map_phys(0xB8000,10);
2019-05-03 08:34:22 -05:00
info.width=80;
info.height=25;
vga_init(info);
vga_write_string("INIT VGA\n");
int pos=0;
2019-06-22 10:31:33 -05:00
uint32_t datapos;
tar_header tar_hdr;
for (int i=0;;i++) {
2019-06-22 10:31:33 -05:00
char* tar_hdr_ptr=(char*)&tar_hdr;
for (size_t i=0;i<sizeof(tar_hdr);i++) {
tar_hdr_ptr[i]=initrd[pos+i];
}
2019-06-22 10:31:33 -05:00
if (tar_hdr.filename[0]=='\0') break;
uint32_t size=getsize(tar_hdr.size);
pos+=512;
2019-06-22 10:31:33 -05:00
if (strcmp(tar_hdr.filename,"vfs")==0) {
vga_write_string("VFS found, loading\n");
datapos=pos;
break;
} else {
vga_write_string("VFS not found\n");
// for(;;);
}
pos+=size;
if (pos%512!=0) {
pos+=512-(pos%512);
}
}
2019-06-22 10:31:33 -05:00
elf_header header;
pos=datapos;
char* hdr_ptr=(char*)&header;
for (size_t i=0;i<sizeof(elf_header);i++) {
hdr_ptr[i]=initrd[pos];
pos++;
}
if (header.magic!=ELF_MAGIC) {
vga_write_string("[INFO] Invalid magic number for vfs\n");
} else {
void* cr3=new_address_space();
for (int i=0;i<header.pheader_ent_nm;i++) {
elf_pheader pheader;
pos=(header.prog_hdr)+(header.pheader_ent_sz*i)+datapos;
char* phdr_ptr=(char*)&pheader;
for (size_t i=0;i<sizeof(elf_pheader);i++) {
phdr_ptr[i]=initrd[pos];
pos++;
}
char* ptr=alloc_memory(((pheader.memsz)/4096)+1);
memset(ptr,0,pheader.memsz);
if (pheader.filesz>0) {
pos=pheader.offset+datapos;
for (size_t i=0;i<pheader.filesz;i++) {
ptr[i]=initrd[pos];
pos++;
}
}
copy_data(cr3,ptr,pheader.memsz,(void*)pheader.vaddr);
}
vga_write_string("Loaded VFS into memory, creating task\n");
createTaskCr3((void*)header.entry,cr3);
2019-06-22 11:11:12 -05:00
vga_write_string("Created VFS task, sending test message\n");
vfs_message* msg=malloc(sizeof(vfs_message)+strlen("/dev/sda")+1);
msg->type=VFS_OPEN;
msg->id=1;
msg->mode[0]='r';
msg->mode[1]='\0';
strcpy(&msg->path,"/dev/sda");
send_msg(2,msg,sizeof(vfs_message)+strlen("/dev/sda")+1);
free(msg);
2019-06-22 11:11:12 -05:00
vga_write_string("Sent test message, yielding to task\n");
2019-06-22 10:31:33 -05:00
yield();
2019-06-22 11:11:12 -05:00
vga_write_string("Yielded and got control, getting message\n");
2019-06-22 15:29:02 -05:00
int sender;
int size;
msg=get_msg(&sender,&size);
vga_write_string("Message of type ");
2019-06-22 15:29:02 -05:00
char str[256];
str[0]='\0';
int_to_ascii(msg->type,str);
2019-06-22 15:29:02 -05:00
vga_write_string(str);
vga_write_string("\n");
vga_write_string("ID ");
str[0]='\0';
int_to_ascii(msg->id,str);
vga_write_string(str);
vga_write_string("\n");
vga_write_string("Mode ");
vga_write_string(&msg->mode);
vga_write_string("\n");
vga_write_string("FD ");
str[0]='\0';
int_to_ascii(msg->fd,str);
vga_write_string(str);
vga_write_string("\n");
vga_write_string("Path ");
vga_write_string(&msg->path);
2019-06-22 11:11:12 -05:00
vga_write_string("\n");
2019-06-22 10:31:33 -05:00
}
for(;;);
2019-03-17 18:04:50 -05:00
}