2019-05-24 11:52:13 -05:00
|
|
|
#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>
|
2019-06-22 16:11:44 -05:00
|
|
|
#include <ipc/vfs.h>
|
2019-06-22 10:31:33 -05:00
|
|
|
#include <elf.h>
|
2019-06-27 17:00:23 -05:00
|
|
|
#include <mailboxes.h>
|
|
|
|
#include <memory.h>
|
|
|
|
#include <tasking.h>
|
|
|
|
#include <stdlib.h>
|
2019-07-20 11:03:27 -05:00
|
|
|
#include <stdio.h>
|
2019-05-01 09:39:23 -05:00
|
|
|
|
2019-05-24 08:54:14 -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;
|
|
|
|
}
|
|
|
|
|
2019-06-29 09:55:02 -05:00
|
|
|
void display_msg(vfs_message* vfs_msg) {
|
|
|
|
vga_write_string("Message of type ");
|
|
|
|
char str[256];
|
|
|
|
str[0]='\0';
|
|
|
|
int_to_ascii(vfs_msg->type,str);
|
|
|
|
vga_write_string(str);
|
|
|
|
vga_write_string("\n");
|
|
|
|
vga_write_string("ID ");
|
|
|
|
str[0]='\0';
|
|
|
|
int_to_ascii(vfs_msg->id,str);
|
|
|
|
vga_write_string(str);
|
|
|
|
vga_write_string("\n");
|
|
|
|
vga_write_string("Mode ");
|
|
|
|
vga_write_string(&vfs_msg->mode[0]);
|
|
|
|
vga_write_string("\n");
|
|
|
|
vga_write_string("FD ");
|
|
|
|
str[0]='\0';
|
|
|
|
int_to_ascii(vfs_msg->fd,str);
|
|
|
|
vga_write_string(str);
|
|
|
|
vga_write_string("\n");
|
|
|
|
vga_write_string("Path ");
|
|
|
|
vga_write_string(&vfs_msg->path[0]);
|
|
|
|
vga_write_string("\n");
|
|
|
|
vga_write_string("Flags ");
|
|
|
|
str[0]='\0';
|
|
|
|
hex_to_ascii(vfs_msg->flags,str);
|
|
|
|
vga_write_string(str);
|
|
|
|
vga_write_string("\n");
|
|
|
|
}
|
|
|
|
|
2019-06-29 10:05:08 -05:00
|
|
|
uint32_t find_loc(char* name,char* initrd) {
|
|
|
|
uint32_t pos=0;
|
2019-06-22 10:31:33 -05:00
|
|
|
tar_header tar_hdr;
|
2019-05-24 08:54:14 -05:00
|
|
|
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-05-24 08:54:14 -05:00
|
|
|
}
|
2019-06-22 10:31:33 -05:00
|
|
|
if (tar_hdr.filename[0]=='\0') break;
|
|
|
|
uint32_t size=getsize(tar_hdr.size);
|
2019-05-24 08:54:14 -05:00
|
|
|
pos+=512;
|
2019-06-29 10:05:08 -05:00
|
|
|
if (strcmp(tar_hdr.filename,name)==0) {
|
|
|
|
return pos;
|
2019-06-22 10:31:33 -05:00
|
|
|
break;
|
2019-05-24 08:54:14 -05:00
|
|
|
}
|
|
|
|
pos+=size;
|
2019-05-24 11:52:13 -05:00
|
|
|
if (pos%512!=0) {
|
|
|
|
pos+=512-(pos%512);
|
|
|
|
}
|
2019-05-24 08:54:14 -05:00
|
|
|
}
|
2019-06-29 10:05:08 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char load_task(uint32_t datapos,char* initrd) {
|
|
|
|
int pos=0;
|
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) {
|
2019-06-29 10:05:08 -05:00
|
|
|
vga_write_string("[INFO] Invalid magic number\n");
|
|
|
|
return 0;
|
2019-06-22 10:31:33 -05:00
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
createTaskCr3((void*)header.entry,cr3);
|
|
|
|
}
|
2019-06-29 10:05:08 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vfs_message* make_msg(vfs_message_type type, char* mode, char* path) {
|
|
|
|
static uint32_t id=0;
|
|
|
|
vfs_message* msg_data=malloc(sizeof(vfs_message));
|
|
|
|
msg_data->type=type;
|
|
|
|
msg_data->id=id;
|
|
|
|
id++;
|
|
|
|
strcpy(&msg_data->mode[0],mode);
|
|
|
|
strcpy(&msg_data->path[0],path);
|
|
|
|
return msg_data;
|
|
|
|
}
|
|
|
|
|
2019-07-01 15:13:47 -05:00
|
|
|
void test_vfs(char* path,uint32_t box,uint32_t fs_box) {
|
|
|
|
vga_write_string("Sending test message\n");
|
|
|
|
vfs_message* msg_data=make_msg(VFS_OPEN,"r",path);
|
2019-06-29 10:05:08 -05:00
|
|
|
Message msg;
|
|
|
|
msg.from=box;
|
|
|
|
msg.to=1;
|
|
|
|
msg.msg=msg_data;
|
|
|
|
msg.size=sizeof(vfs_message);
|
|
|
|
mailbox_send_msg(&msg);
|
|
|
|
free(msg.msg);
|
|
|
|
yield();
|
2019-07-01 15:13:47 -05:00
|
|
|
vga_write_string("Getting fs_box message\n");
|
2019-06-29 10:05:08 -05:00
|
|
|
yield();
|
|
|
|
vga_write_string("Getting message\n");
|
|
|
|
msg.msg=malloc(sizeof(vfs_message));
|
|
|
|
mailbox_get_msg(box,&msg,sizeof(vfs_message));
|
2019-07-01 15:13:47 -05:00
|
|
|
if (msg.from==0) {
|
|
|
|
vga_write_string("No message\n");
|
|
|
|
} else {
|
|
|
|
vfs_message* vfs_msg=(vfs_message*)msg.msg;
|
|
|
|
display_msg(vfs_msg);
|
|
|
|
}
|
|
|
|
free(msg.msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(char* initrd, uint32_t initrd_sz) {
|
|
|
|
text_fb_info info;
|
|
|
|
info.address=map_phys((void*)0xB8000,10);
|
|
|
|
info.width=80;
|
|
|
|
info.height=25;
|
|
|
|
vga_init(info);
|
2019-07-20 11:03:27 -05:00
|
|
|
vga_write_string("INIT\n");
|
|
|
|
uint32_t datapos=find_loc("vfs",initrd);
|
2019-07-13 10:18:41 -05:00
|
|
|
load_task(datapos,initrd);
|
2019-08-04 10:58:29 -05:00
|
|
|
yield(); // Bochs fails here
|
|
|
|
datapos=find_loc("fsdrv",initrd);
|
|
|
|
load_task(datapos,initrd);
|
2019-08-04 13:14:35 -05:00
|
|
|
yieldToPID(3);
|
2019-08-04 10:58:29 -05:00
|
|
|
FILE* file;
|
|
|
|
do {
|
|
|
|
vga_write_string("CALLING FOPEN\n");
|
|
|
|
file=fopen("/dev/sda","w");
|
|
|
|
vga_write_string("FOPEN RETURNED\n");
|
|
|
|
} while(file==NULL);
|
|
|
|
vga_write_string("CALLING FPUTS\n");
|
|
|
|
fputs("FPUTS String",file);
|
|
|
|
vga_write_string("FPUTS RETURNED\n");
|
2019-03-17 18:04:50 -05:00
|
|
|
}
|