view src/shell.c @ 99:2a0aa3efc228

The shell script will now theroretically load the program into memory, will not run it. The shell script loads a program into memory and displays the address but as of now does not jump to it (though it does display the address it would jump to) Added a memset function Added a python script (credit for its writing to Jeff) that sets up a filesystem
author Jonathan Pevarnek <pevarnj@gmail.com>
date Sat, 14 May 2011 19:40:18 -0400
parents 28c230f0700e
children d8f21e4a75e3
line wrap: on
line source

#include <std.h>
#include <string.h>
#include <stdio.h>
#include <error.h>
#include <fs.h>
#include <die.h>
#include <elf.h>

void start(u64 __memsize)
{
	init_all(__memsize);
	if(isError(init_fs(0x100, __memsize))) die();
	char buffer[1024];
	ErrCode err;
	while(1) {
		sPrint("$ ");
		sGet(buffer, 78);
		u32 fid;
		if(isError(err = lookupFile(buffer, &fid))) {
			if(errCode(err) == NOTFILE) sPrint("ERROR: command not found\n");
			continue;
		} else {
			u32 fileSize;
			if(isError(getFileSize(fid, &fileSize)));
			void *data = malloc(fileSize);
			if(!data) continue;
			if(isError(getFileData(fid, data))) goto loopCont;
			Elf64_Ehdr *eHdr = data;
			if(eHdr->e_ident[EI_MAG0] != ELFMAG0 || eHdr->e_ident[EI_MAG1] != ELFMAG1 ||
					eHdr->e_ident[EI_MAG2] != ELFMAG2 || eHdr->e_ident[EI_MAG3] != ELFMAG3)
				goto loopCont;
			if(eHdr->e_ident[EI_CLASS] != ELFCLASS64) goto loopCont;
			int i;
			for(i = 0; i < eHdr->e_phnum; i++) {
				Elf64_Phdr *pHdr = ((Elf64_Phdr*)(data + eHdr->e_phoff) + i);
				if(pHdr->p_type == PT_LOAD) {
					void *fromLoc = data + pHdr->p_offset;
					void *toLoc = (void*)pHdr->p_vaddr;
					memcpy(toLoc, fromLoc, pHdr->p_filesz);
					size_t diff = pHdr->p_memsz - pHdr->p_filesz;
					if(diff) memset(toLoc + pHdr->p_filesz, 0, diff);
				}
			}
			sprintf(buffer, "Going to memory address %i\n", eHdr->e_entry);
			sPrint(buffer);
		loopCont:
			free(data);
		}
	}

//END:
	sPrint("DONE\n");
	die();
}