static void erasearea_flash(void *start, size_t size) { #if USE_BLOCKWRITING rom_erase(size, (off_t)(uintptr_t)start); #endif flash_setup(); while (size > ROM_ERASE_UNIT_SIZE) { flash_clear(start); size -= ROM_ERASE_UNIT_SIZE; start = (char*) start + ROM_ERASE_UNIT_SIZE; } flash_done(); }
void load_hex_to_rom(const char *path) { void *entry; HexStat stat = {0, 0xFFFFFFFF}; RomHeader header; int x, y; int fd; printf("Erasing ROM... "); rom_erase(); while(rom_status() & 0x1); terminal_set_fg(TERMINAL_COLOR_LIGHT_GREEN); printf("done.\n"); terminal_set_fg(TERMINAL_COLOR_LIGHT_GRAY); printf("Writing to ROM... "); terminal_get_pos(&x, &y); fd = fat_open(path, O_RDONLY); if(hexload(get_byte, fd, _write_rom_byte, &stat, &entry) < 0) { terminal_set_fg(TERMINAL_COLOR_RED); printf("Invalid hex file"); terminal_set_fg(TERMINAL_COLOR_LIGHT_GRAY); fat_close(fd); return; } fat_close(fd); header.magic = 0xDEADBEEF; header.entry = (uint32_t) entry; header.size = stat.size; header.dest_addr = LLRAM_BASE | stat.offset; header.flash_offset = stat.offset; rom_write(ROM_SIZE - 512, (void *) &header, sizeof(header)); terminal_set_pos(x, y); terminal_set_fg(TERMINAL_COLOR_LIGHT_GREEN); printf("done.\n"); terminal_set_fg(TERMINAL_COLOR_LIGHT_GRAY); printf("Press any key..."); }
/*---------------------------------------------------------------------------*/ int elfloader_load(off_t eepromaddr) { struct cle_info h; int ret; void (*elfloader_init)(void); elfloader_unknown[0] = 0; /* The ELF header is located at the start of the buffer. */ ret = cle_read_info(&h, xmem_pread, eepromaddr); if(ret != ELFLOADER_OK) { memcpy(elfloader_unknown, h.name, sizeof(elfloader_unknown)); elfloader_unknown[sizeof(elfloader_unknown) - 1] = 0; return ret; } if(datamemory != NULL) { free(datamemory); } /* We are making semi-permanent allocations, first compact heap! */ /* malloc_compact(); */ datamemory = malloc(IMAX(h.textsize, h.datasize + h.bsssize)); if(datamemory == NULL) { return ELFLOADER_DATA_TO_LARGE; /* XXX or text to large */ } h.data = datamemory; h.bss = datamemory + h.datasize; h.text = TEXTMEMORY; PRINTF("elfloader: copy text segment to RAM %p %p\n", h.data, h.data + h.textsize); ret = xmem_pread(datamemory, h.textsize, eepromaddr + h.textoff); assert(ret > 0); if(h.textrelasize > 0) { PRINTF("elfloader: relocate text in RAM\n"); ret = cle_relocate(&h, xmem_pread, eepromaddr, datamemory, h.textrelaoff, h.textrelasize); if(ret != ELFLOADER_OK) { memcpy(elfloader_unknown, h.name, sizeof(elfloader_unknown)); elfloader_unknown[sizeof(elfloader_unknown) - 1] = 0; return ret; } } PRINTF("elfloader: copy text segment to ROM 0x%lx 0x%lx\n", (unsigned long)h.text, (unsigned long)h.text + h.textsize); ret = rom_erase((h.textsize+ROM_ERASE_UNIT_SIZE) & ~(ROM_ERASE_UNIT_SIZE-1), h.text); assert(ret > 0); ret = rom_pwrite(datamemory, h.textsize, h.text); assert(ret > 0); PRINTF("elfloader: copy data segment to RAM %p %p\n", h.data, h.data + h.datasize); ret = xmem_pread(datamemory, h.datasize, eepromaddr + h.dataoff); assert(ret >= h.datasize); if(h.datarelasize > 0) { PRINTF("elfloader: relocate data segment\n"); ret = cle_relocate(&h, xmem_pread, eepromaddr, datamemory, h.datarelaoff, h.datarelasize); if(ret != ELFLOADER_OK) { memcpy(elfloader_unknown, h.name, sizeof(elfloader_unknown)); elfloader_unknown[sizeof(elfloader_unknown) - 1] = 0; return ret; } } PRINTF("elfloader: zero bss %p %p\n", h.bss, h.bss + h.bsssize); memset(h.bss, 0, h.bsssize); /* Find _init, _fini, and loaded_process. */ elfloader_loaded_process = cle_lookup(&h, xmem_pread, eepromaddr, "autostart_processes"); elfloader_fini = cle_lookup(&h, xmem_pread, eepromaddr, "_fini"); elfloader_init = cle_lookup(&h, xmem_pread, eepromaddr, "_init"); if(elfloader_init != NULL) { PRINTF("init=%p fini=%p\n", elfloader_init, elfloader_fini); (*elfloader_init)(); elfloader_loaded_process = NULL; return ELFLOADER_OK; } if(elfloader_loaded_process != NULL) { PRINTF("elfloader: launch program\n"); process_start(elfloader_loaded_process, NULL); elfloader_fini = NULL; return ELFLOADER_OK; } else { return ELFLOADER_NO_STARTPOINT; } }