Skip to content
Snippets Groups Projects
Commit c245b210 authored by Byron Lathi's avatar Byron Lathi
Browse files

done for tonight. need to read elf bytewise

yeah, I guess it was just a coincidence that the segment was aligned.

turns out that I need to do what I thought in the first place.
parent 86498adf
No related branches found
No related tags found
No related merge requests found
......@@ -9,9 +9,7 @@ int main(int argc, char** argv)
int i;
for (i = 0; i < argc; i++) {
sys_write(1, "Arg: ", 4);
sys_write(1, argv[i], strlen(argv[i]));
sys_write(1, "\n", 1);
printf("Arg%d: %s\n", i, argv[i]);
}
uint8_t test[64];
......
......@@ -35,6 +35,34 @@ uint32_t ext2_get_inode(uint32_t inode_num, inode_t* buf)
return 0;
}
uint32_t ext2_read_sectors(uint32_t inode_num, uint32_t start, uint32_t count, uint16_t* buf)
{
if (!buf)
return -1;
inode_t inode;
ext2_get_inode(inode_num, &inode);
uint32_t sectors_read = 0;
// step 1: determine which block.
uint32_t block = start / 2;
// step 2: which sector in that block do we want?
uint32_t sector = start % 2;
printf("start %d block %d sector %d\n", start, block, sector);
// read the first (maybe misaligned) sector(s)
int i;
for (i = sector; i < 2 && sectors_read < count; i++) {
if (!inode.block_pointer[block])
break;
printf("Reading from inode %x block %x sector %x into %#x\n", inode_num, block, i, buf + sectors_read * 256);
ata_read_sectors(ATA_MASTER, ext2_base + inode.block_pointer[block] * 2 + i, 1, buf + sectors_read * 256);
sectors_read++;
}
}
uint32_t ext2_read_data(uint32_t inode_num, uint32_t count, uint16_t* buf)
{
if (!buf)
......
......@@ -122,6 +122,7 @@ typedef struct dir_entry {
uint32_t ext2_init(uint32_t base);
uint32_t ext2_get_inode(uint32_t inode_num, inode_t* buf);
uint32_t ext2_read_sectors(uint32_t inode_num, uint32_t start, uint32_t count, uint16_t* buf);
uint32_t ext2_read_data(uint32_t inode_num, uint32_t count, uint16_t* buf);
uint32_t ext2_find_inode_num(const int8_t* filename);
uint32_t ext2_find_inode(const int8_t* filename, inode_t* inode);
......
......@@ -10,6 +10,8 @@
#define PCB_MASK 0xffffe000
#define divhi(x, y) (x/y + (x % y != 0))
// TEMPORARY!!!
static uint32_t pid = 0;
......@@ -162,10 +164,34 @@ int32_t _sys_exec(uint8_t* cmd)
printf("arg%d: %s\n", i, argv[i]);
}
elf_header_t* header = (elf_header_t*)data.data8;
elf_header_t* e_header = (elf_header_t*)data.data8;
printf("Program header count: %d\n", e_header->e_phnum);
printf("Program header size: %x\n", e_header->e_phentsize);
printf("Program header offset: %x\n", e_header->e_phoff);
program_header_t* p_header[e_header->e_phnum];
for (i = 0; i < e_header->e_phnum; i++) {
p_header[i] = (program_header_t*)(data.data8 + e_header->e_phoff + i * e_header->e_phentsize);
}
map_large(EXEC_BASE_ADDR, _8MB + pid * _4MB);
ext2_read_data(parsed_inode, 0, (uint16_t*)EXEC_LOAD_ADDR);
// what if you...
// read the program header to see how many bytes you needed to allocate and where
// wrote an ext2 read where you could specifiy a starting point in the inode and bytes to read
for (i = 0; i < e_header->e_phnum; i++) {
printf("Program header %d type %x flags %x", i, p_header[i]->p_type, p_header[i]->p_flags);
printf(" offset %x vaddr %#x paddr %#x size %x\n",
p_header[i]->p_offset, p_header[i]->p_vaddr, p_header[i]->p_paddr, p_header[i]->p_filesz);
// so there are the two program segments that we need to load.
}
ext2_read_sectors(parsed_inode, p_header[1]->p_offset/512, divhi(p_header[1]->p_memsz, 512), p_header[1]->p_vaddr);
*USER_ARGC = argc;
......@@ -190,7 +216,7 @@ int32_t _sys_exec(uint8_t* cmd)
iret \n\
pop %2"
: "=m"(pcb->parent_ebp), "=m"(pcb->parent_esp), "=m"(status)
: "r"(header->e_entry)
: "r"(e_header->e_entry)
: "%eax"
);
......
......@@ -71,6 +71,36 @@ typedef struct elf_header{
uint16_t e_shstrndx;
} elf_header_t;
#define PT_NULL 0
#define PT_LOAD 1
#define PT_DYNAMIC 2
#define PT_INTERP 3
#define PT_NOTE 4
#define PT_SHLIB 5
#define PT_PHDR 6
#define PT_TLS 7
#define PT_LOOS 0x60000000
#define PT_HIOS 0x6fffffff
#define PT_LOPROC 0x70000000
#define PT_HIPROC 0x7fffffff
#define PF_X 1
#define PF_W 2
#define PF_R 4
#define PF_MASKOS 0x0ff00000
#define PF_MASKPROC 0xf0000000
typedef struct program_header {
uint32_t p_type;
uint32_t p_offset;
uint32_t p_vaddr;
uint32_t p_paddr;
uint32_t p_filesz;
uint32_t p_memsz;
uint32_t p_flags;
uint32_t p_align;
} program_header_t;
#define MAX_FD 8
typedef struct pcb {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment