os/init/main.c

130 lines
3.0 KiB
C
Raw Permalink Normal View History

2020-07-22 19:35:23 -05:00
#include <dbg.h>
#include <elf.h>
#include <initrd.h>
#include <memory.h>
2020-07-22 19:35:23 -05:00
#include <pthread.h>
#include <stdio.h>
2020-07-22 19:35:23 -05:00
#include <stdlib.h>
#include <string.h>
#include <tasking.h>
2020-08-02 14:37:23 -05:00
#include <rpc.h>
#include <serdes.h>
2020-08-23 14:05:38 -05:00
#include <spawn.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;
2020-07-23 11:50:23 -05:00
size_t getsize(const char *in) {
size_t size=0;
size_t j;
size_t count=1;
for (j=11;j>0;j--,count*=8) {
size+=((in[j-1]-'0')*count);
}
return size;
}
2020-07-23 11:50:23 -05:00
size_t find_loc(char* name,char* initrd) {
size_t pos=0;
2019-06-22 10:31:33 -05:00
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;
2020-07-23 11:50:23 -05:00
size_t size=getsize(tar_hdr.size);
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;
}
pos+=size;
if (pos%512!=0) {
pos+=512-(pos%512);
}
}
2019-06-29 10:05:08 -05:00
return 0;
}
2020-07-23 11:50:23 -05:00
char load_proc(size_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;
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* address_space=new_address_space();
2019-06-22 10:31:33 -05:00
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(address_space,ptr,pheader.memsz,(void*)pheader.vaddr);
2019-06-22 10:31:33 -05:00
}
create_proc((void*)header.entry,address_space,NULL,NULL);
2019-06-22 10:31:33 -05:00
}
2019-06-29 10:05:08 -05:00
return 1;
}
int main() {
2020-08-02 14:37:23 -05:00
long size=initrd_sz();
char* initrd=malloc(size);
initrd_get(initrd);
size_t datapos=find_loc("vfs",initrd);
load_proc(datapos,initrd);
while(rpc_is_init(2)==0);
datapos=find_loc("devfs",initrd);
2020-08-02 14:37:23 -05:00
load_proc(datapos,initrd);
while(rpc_is_init(3)==0);
int err=mount("","devfs","/dev");
if (err) {
serial_print("Failed to mount devfs\n");
exit(1);
}
datapos=find_loc("initrd_drv",initrd);
load_proc(datapos,initrd);
while(rpc_is_init(4)==0);
2020-08-23 14:05:38 -05:00
datapos=find_loc("tar_fs",initrd);
load_proc(datapos,initrd);
while(rpc_is_init(5)==0);
2020-08-23 14:05:38 -05:00
serial_print("Mounting initrd\n");
mount("/dev/initrd","tarfs","/initrd");
posix_spawn(NULL,"/initrd/vga_drv",NULL,NULL,NULL,NULL);
while(rpc_is_init(6)==0);
stdout=fopen("/dev/vga","w");
if (!stdout) {
2020-09-06 08:11:12 -05:00
serial_print("Could not open the VGA device file!\n");
exit(1);
}
2021-02-28 13:46:33 -06:00
posix_spawn(NULL,"/initrd/ps2",NULL,NULL,NULL,NULL);
// posix_spawn(NULL,"/initrd/pci",NULL,NULL,NULL,NULL);
for(;;);
2019-03-17 18:04:50 -05:00
}