[292] | 1 | /** |
---|
[586] | 2 | * \file : reset_elf_loader.c |
---|
[292] | 3 | * \date : August 2012 |
---|
| 4 | * \author : Cesar Fuguet |
---|
| 5 | * |
---|
[586] | 6 | * This file defines an elf file loader which reads an executable .elf file |
---|
| 7 | * starting at a sector passed as argument on a disk and copy the different |
---|
[292] | 8 | * ELF program segments in the appropriate memory address using as information |
---|
[586] | 9 | * the virtual address read from the .elf file. |
---|
[292] | 10 | */ |
---|
| 11 | |
---|
[586] | 12 | #include <reset_ioc.h> |
---|
[292] | 13 | #include <elf-types.h> |
---|
[586] | 14 | #include <reset_tty.h> |
---|
| 15 | #include <reset_utils.h> |
---|
[292] | 16 | #include <defs.h> |
---|
| 17 | |
---|
[930] | 18 | extern int blk_buf_idx; |
---|
| 19 | |
---|
[701] | 20 | /////////////////////////////////////////////////////////////////////////////// |
---|
| 21 | void * reset_elf_loader(size_t lba) |
---|
| 22 | /////////////////////////////////////////////////////////////////////////////// |
---|
[586] | 23 | { |
---|
[701] | 24 | size_t file_offset = lba * BLOCK_SIZE; |
---|
[425] | 25 | |
---|
[701] | 26 | reset_puts("\n[RESET] Start reset_elf_loader at cycle "); |
---|
| 27 | reset_putd( proctime() ); |
---|
| 28 | reset_puts("\n"); |
---|
[570] | 29 | |
---|
[425] | 30 | /* |
---|
[930] | 31 | * Init the cache block index |
---|
| 32 | */ |
---|
| 33 | blk_buf_idx = -1; |
---|
| 34 | |
---|
| 35 | /* |
---|
[701] | 36 | * Load ELF HEADER |
---|
[292] | 37 | */ |
---|
[701] | 38 | Elf32_Ehdr elf_header; |
---|
| 39 | if (pread(file_offset, (void*)&elf_header, sizeof(Elf32_Ehdr), 0) < 0) { |
---|
| 40 | goto error; |
---|
| 41 | } |
---|
| 42 | check_elf_header(&elf_header); |
---|
[292] | 43 | |
---|
[701] | 44 | /* |
---|
| 45 | * Load ELF PROGRAM HEADER TABLE |
---|
| 46 | */ |
---|
[758] | 47 | Elf32_Phdr elf_pht[RESET_PHDR_ARRAY_SIZE]; |
---|
[701] | 48 | size_t phdr_nbyte = sizeof(Elf32_Phdr) * elf_header.e_phnum; |
---|
| 49 | size_t phdr_off = elf_header.e_phoff; |
---|
| 50 | if (pread(file_offset, (void*)&elf_pht, phdr_nbyte, phdr_off) < 0) { |
---|
| 51 | goto error; |
---|
| 52 | } |
---|
[292] | 53 | |
---|
| 54 | /* |
---|
[701] | 55 | * Search for loadable segments in the ELF file |
---|
[412] | 56 | */ |
---|
[701] | 57 | int pseg; |
---|
| 58 | for (pseg = 0; pseg < elf_header.e_phnum; pseg++) |
---|
[292] | 59 | { |
---|
[701] | 60 | if(elf_pht[pseg].p_type != PT_LOAD) continue; |
---|
[292] | 61 | |
---|
[586] | 62 | #if (RESET_DEBUG == 1) |
---|
[701] | 63 | reset_puts("[RESET DEBUG] Loadable segment found:\n"); |
---|
| 64 | reset_print_elf_phdr(&elf_pht[pseg]); |
---|
[412] | 65 | #endif |
---|
| 66 | |
---|
[701] | 67 | addr_t p_paddr = elf_pht[pseg].p_paddr; |
---|
| 68 | size_t p_filesz = elf_pht[pseg].p_filesz; |
---|
| 69 | size_t p_memsz = elf_pht[pseg].p_memsz; |
---|
| 70 | size_t p_offset = elf_pht[pseg].p_offset; |
---|
[292] | 71 | |
---|
[701] | 72 | /* |
---|
| 73 | * Copy program segment from ELF executable into corresponding physical |
---|
| 74 | * address |
---|
| 75 | */ |
---|
| 76 | if (pread(file_offset, (void*)p_paddr, p_filesz, p_offset) < 0) { |
---|
| 77 | goto error; |
---|
[292] | 78 | } |
---|
| 79 | |
---|
[701] | 80 | /* |
---|
| 81 | * Fill remaining bytes with zero (filesz < memsz) |
---|
| 82 | */ |
---|
| 83 | char* pseg_ptr = (char*)p_paddr; |
---|
| 84 | memset((void*)&pseg_ptr[p_filesz], 0, (p_memsz - p_filesz)); |
---|
[292] | 85 | |
---|
[701] | 86 | reset_puts("\n[RESET] Segment loaded : address = "); |
---|
| 87 | reset_putx(p_paddr); |
---|
| 88 | reset_puts(" / size = "); |
---|
| 89 | reset_putx(p_filesz); |
---|
| 90 | reset_puts("\n"); |
---|
[292] | 91 | } |
---|
| 92 | |
---|
[586] | 93 | reset_puts("\n[RESET] Complete reset_elf_loader at cycle "); |
---|
| 94 | reset_putd( proctime() ); |
---|
| 95 | reset_puts(" / boot entry = "); |
---|
[701] | 96 | reset_putx( (addr_t)(elf_header.e_entry) ); |
---|
[586] | 97 | reset_puts("\n"); |
---|
[425] | 98 | |
---|
[701] | 99 | return ((void *) elf_header.e_entry); |
---|
| 100 | |
---|
| 101 | error: |
---|
| 102 | reset_puts("\n[RESET ERROR] Error while loading ELF file"); |
---|
| 103 | reset_exit(); |
---|
| 104 | return 0; |
---|
[292] | 105 | } |
---|
[425] | 106 | |
---|
| 107 | // vim: tabstop=4 : softtabstop=4 : shiftwidth=4 : expandtab |
---|