45 #include "loader/elfloader_compat.h"
46 #include "loader/cle.h"
48 #include "lib/malloc.h"
53 #include "lib/assert.h"
56 #define PRINTF(...) do {} while (0)
58 #define PRINTF(...) printf(__VA_ARGS__)
61 struct process *elfloader_loaded_process;
62 void (*elfloader_fini)(void);
64 #define IMAX(a, b) (((a) > (b)) ? (a) : (b))
66 unsigned char *datamemory;
69 extern int __data_load_end;
70 #define TEXTMEMORY (((cle_addr)(&__data_load_end) + ROM_ERASE_UNIT_SIZE) \
71 & ~(ROM_ERASE_UNIT_SIZE - 1))
73 #include <sys/unistd.h>
75 (cle_addr)(((uintptr_t)(&_etext + 1) \
76 + (uintptr_t)&_edata - (uintptr_t)&__data_start \
77 + ROM_ERASE_UNIT_SIZE) \
78 & ~(ROM_ERASE_UNIT_SIZE - 1))
95 ret = cle_read_info(&h, xmem_pread, eepromaddr);
103 if(datamemory !=
NULL) {
109 datamemory = malloc(IMAX(h.textsize, h.datasize + h.bsssize));
110 if(datamemory ==
NULL) {
111 return ELFLOADER_DATA_TO_LARGE;
115 h.bss = datamemory + h.datasize;
118 PRINTF(
"elfloader: copy text segment to RAM %p %p\n",
119 h.data, h.data + h.textsize);
120 ret = xmem_pread(datamemory, h.textsize, eepromaddr + h.textoff);
122 if(h.textrelasize > 0) {
123 PRINTF(
"elfloader: relocate text in RAM\n");
124 ret = cle_relocate(&h,
128 h.textrelaoff, h.textrelasize);
135 PRINTF(
"elfloader: copy text segment to ROM 0x%lx 0x%lx\n",
136 (
unsigned long)h.text,
137 (
unsigned long)h.text + h.textsize);
139 ret = rom_erase((h.textsize+ROM_ERASE_UNIT_SIZE) & ~(ROM_ERASE_UNIT_SIZE-1),
142 ret = rom_pwrite(datamemory, h.textsize, h.text);
145 PRINTF(
"elfloader: copy data segment to RAM %p %p\n",
146 h.data, h.data + h.datasize);
147 ret = xmem_pread(datamemory, h.datasize, eepromaddr + h.dataoff);
148 assert(ret >= h.datasize);
149 if(h.datarelasize > 0) {
150 PRINTF(
"elfloader: relocate data segment\n");
151 ret = cle_relocate(&h,
155 h.datarelaoff, h.datarelasize);
163 PRINTF(
"elfloader: zero bss %p %p\n", h.bss, h.bss + h.bsssize);
164 memset(h.bss, 0, h.bsssize);
167 elfloader_loaded_process = cle_lookup(&h, xmem_pread, eepromaddr,
168 "autostart_processes");
169 elfloader_fini = cle_lookup(&h, xmem_pread, eepromaddr,
"_fini");
175 elfloader_loaded_process =
NULL;
179 if(elfloader_loaded_process !=
NULL) {
180 PRINTF(
"elfloader: launch program\n");
182 elfloader_fini =
NULL;
190 elfloader_unload(
void)
192 if(elfloader_fini !=
NULL) {
194 elfloader_fini =
NULL;
195 }
else if(elfloader_loaded_process !=
NULL) {
197 elfloader_loaded_process =
NULL;
199 if(datamemory !=
NULL) {