2020-07-22 19:35:23 -05:00
|
|
|
#include <dbg.h>
|
|
|
|
#include <elf.h>
|
2019-05-03 08:34:22 -05:00
|
|
|
#include <grub/text_fb_info.h>
|
2020-07-22 19:35:23 -05:00
|
|
|
#include <initrd.h>
|
2019-06-22 16:11:44 -05:00
|
|
|
#include <ipc/vfs.h>
|
2019-06-27 17:00:23 -05:00
|
|
|
#include <memory.h>
|
2020-07-22 19:35:23 -05:00
|
|
|
#include <pthread.h>
|
2019-07-20 11:03:27 -05:00
|
|
|
#include <stdio.h>
|
2020-07-22 19:35:23 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <tasking.h>
|
2019-08-31 16:48:11 -05:00
|
|
|
#include <vfs.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 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;
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:07:46 -05:00
|
|
|
char load_proc(uint32_t datapos,char* initrd) {
|
2019-06-29 10:05:08 -05:00
|
|
|
int pos=0;
|
2019-06-22 10:31:33 -05:00
|
|
|
elf_header header;
|
|
|
|
pos=datapos;
|
2019-10-20 09:44:33 -05:00
|
|
|
char str[256];
|
|
|
|
int_to_ascii(pos,str);
|
|
|
|
serial_print("POS:");
|
|
|
|
serial_print(str);
|
|
|
|
serial_print("\n");
|
2019-06-22 10:31:33 -05:00
|
|
|
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
|
|
|
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);
|
|
|
|
}
|
2020-07-20 10:07:46 -05:00
|
|
|
createProcCr3((void*)header.entry,cr3);
|
2019-06-22 10:31:33 -05:00
|
|
|
}
|
2019-06-29 10:05:08 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:07:46 -05:00
|
|
|
// char load_proc_devfs(uint32_t datapos) {
|
|
|
|
// serial_print("load_proc_devfs\n");
|
2020-07-12 14:30:16 -05:00
|
|
|
// FILE* initrd=fopen("/dev/initrd","r");
|
|
|
|
// elf_header header;
|
|
|
|
// fseek(initrd,datapos,SEEK_SET);
|
|
|
|
// fread(&header,sizeof(elf_header),1,initrd);
|
|
|
|
// if (header.magic!=ELF_MAGIC) {
|
|
|
|
// serial_print("Bad magic number\n");
|
|
|
|
// return 0;
|
|
|
|
// } else {
|
|
|
|
// void* cr3=new_address_space();
|
|
|
|
// for (int i=0;i<header.pheader_ent_nm;i++) {
|
|
|
|
// elf_pheader pheader;
|
|
|
|
// fseek(initrd,(header.prog_hdr)+(header.pheader_ent_sz*i)+datapos,SEEK_SET);
|
|
|
|
// fread(&pheader,sizeof(elf_pheader),1,initrd);
|
|
|
|
// serial_print("pheader.memsz=");
|
|
|
|
// char str[256];
|
|
|
|
// hex_to_ascii(pheader.memsz,str);
|
|
|
|
// serial_print(str);
|
|
|
|
// serial_print("\n");
|
|
|
|
// char* ptr=alloc_memory(((pheader.memsz)/4096)+1);
|
|
|
|
// memset(ptr,0,pheader.memsz);
|
|
|
|
// if (pheader.filesz>0) {
|
|
|
|
// fseek(initrd,pheader.offset+datapos,SEEK_SET);
|
|
|
|
// fread(ptr,sizeof(char),pheader.filesz,initrd);
|
|
|
|
// }
|
|
|
|
// copy_data(cr3,ptr,pheader.memsz,(void*)pheader.vaddr);
|
|
|
|
// }
|
2020-07-20 10:07:46 -05:00
|
|
|
// createProcCr3((void*)header.entry,cr3);
|
2020-07-12 14:30:16 -05:00
|
|
|
// }
|
|
|
|
// return 1;
|
|
|
|
// }
|
2019-10-20 09:44:33 -05:00
|
|
|
|
2020-07-22 19:26:55 -05:00
|
|
|
void* thread_func(void* arg) {
|
2020-07-20 09:51:30 -05:00
|
|
|
for (;;) yield();
|
2020-07-22 19:26:55 -05:00
|
|
|
return NULL;
|
2020-07-20 09:51:30 -05:00
|
|
|
}
|
2019-10-20 09:44:33 -05:00
|
|
|
|
2019-08-26 13:07:01 -05:00
|
|
|
int main() {
|
2020-07-12 14:28:58 -05:00
|
|
|
serial_print("IN INIT\n");
|
2020-07-22 07:33:44 -05:00
|
|
|
pthread_t thread;
|
|
|
|
pthread_create(&thread,NULL,thread_func,NULL);
|
2020-07-20 10:07:46 -05:00
|
|
|
blockThread(THREAD_BLOCKED);
|
2020-07-20 09:51:30 -05:00
|
|
|
for (int i=0;i<5;i++) {
|
|
|
|
serial_print("YIELDING\n");
|
2020-07-12 14:28:58 -05:00
|
|
|
yield();
|
2020-07-20 09:51:30 -05:00
|
|
|
serial_print("YIELDED\n");
|
2020-07-12 14:28:58 -05:00
|
|
|
}
|
2020-07-20 09:51:30 -05:00
|
|
|
serial_print("EXITING\n");
|
|
|
|
exit(0);
|
2020-07-12 14:28:58 -05:00
|
|
|
// long size=initrd_sz();
|
|
|
|
// char* initrd=malloc(size);
|
|
|
|
// initrd_get(initrd);
|
|
|
|
// exit(0);
|
|
|
|
// uint32_t datapos=find_loc("vfs",initrd);
|
2020-07-20 10:07:46 -05:00
|
|
|
// load_proc(datapos,initrd);
|
2020-07-12 14:28:58 -05:00
|
|
|
// yield(); // Bochs fails here
|
|
|
|
// rescan_vfs();
|
|
|
|
// datapos=find_loc("devfs",initrd);
|
2020-07-20 10:07:46 -05:00
|
|
|
// load_proc(datapos,initrd);
|
2020-07-12 14:28:58 -05:00
|
|
|
// yieldToPID(3);
|
|
|
|
// datapos=find_loc("initrd_drv",initrd);
|
2020-07-20 10:07:46 -05:00
|
|
|
// load_proc(datapos,initrd);
|
2020-07-12 14:28:58 -05:00
|
|
|
// yieldToPID(4);
|
|
|
|
// mount("","devfs","/dev/");
|
|
|
|
// datapos=find_loc("vga_drv",initrd);
|
2020-07-20 10:07:46 -05:00
|
|
|
// serial_print("Making vga process\n");
|
|
|
|
// load_proc_devfs(datapos);
|
|
|
|
// serial_print("Made vga process\n");
|
2020-07-12 14:28:58 -05:00
|
|
|
// yieldToPID(5);
|
|
|
|
// FILE* file;
|
|
|
|
// do {
|
|
|
|
// file=fopen("/dev/vga","w");
|
|
|
|
// } while(file==NULL);
|
|
|
|
// do {
|
|
|
|
// file=fopen("/dev/vga","w");
|
|
|
|
// } while(file==NULL);
|
2020-07-09 11:28:57 -05:00
|
|
|
// datapos=find_loc("pci",initrd);
|
2020-07-20 10:07:46 -05:00
|
|
|
// load_proc(datapos,initrd);
|
2020-07-09 11:28:57 -05:00
|
|
|
// free(initrd);
|
|
|
|
// yieldToPID(4);
|
2020-07-12 14:28:58 -05:00
|
|
|
// fputs("FPUTS String\n",file);
|
2020-07-09 11:28:57 -05:00
|
|
|
// char str[3]={0,0,0};
|
|
|
|
// fgets(str,2,stdin);
|
|
|
|
// char str2[3]={0,0,0};
|
|
|
|
// fgets(str2,2,stdin);
|
|
|
|
// printf("Printf %s,%s\n",str,str2);
|
2019-03-17 18:04:50 -05:00
|
|
|
}
|