// Attempt to load a module file. // If file found and is a valid module, then load into memory, relocate and link to CHDK core // Returns memory address of module if successful, 0 if failure. flat_hdr* module_preload(const char *path, const char *name, _version_t ver) { // Allocate buffer and open file int module_fd = b_open(path); if (module_fd <= 0) { moduleload_error(name, "open error"); return 0; } // Read module header only to get size info flat_hdr flat; b_read(module_fd, (char*)&flat, sizeof(flat)); // TODO - compare loaded with requested size // Error message char *msg = 0; // Pointer to memory allocated to load module flat_hdr* flat_buf = 0; // Check version and magic number - make sure it is a CHDK module file if ((flat.rev == FLAT_VERSION) && (flat.magic == FLAT_MAGIC_NUMBER)) { // Allocate module memory, and load module code msg = load_module_file(module_fd, name, flat.reloc_start, flat.bss_size, &flat_buf); if (msg == 0) { // Module info checks ModuleInfo *mod_info = flat_buf->_module_info = (ModuleInfo*)((unsigned int)flat_buf+flat_buf->_module_info_offset); // Validate version requirements msg = validate(mod_info, ver); if (msg == 0) { // Make relocations msg = link_module(module_fd, flat_buf); } } } else msg = "bad magicnum"; // Close file b_close(module_fd); // If any error found, free module memory and display error if (msg) { if (flat_buf) free(flat_buf); moduleload_error(name, msg); return 0; } // TODO these could be changed to operate on affected address ranges only // after relocating but before attempting to execute loaded code // clean data cache to ensure code is in main memory dcache_clean_all(); // then flush instruction cache to ensure no addresses containing new code are cached icache_flush_all(); // Return module memory address return flat_buf; }
struct flat_hdr* module_preload(const char *name, _version_t ver) { module_fd = -1; module_filename = name; flat_buf = 0; buf_load = 0; char path[60]; struct flat_hdr flat; int size_flat; flat_module_path_make(path,module_filename); module_fd = open( path, O_RDONLY, 0777 ); if ( module_fd <=0 ) { moduleload_error("file not found",0); return 0; } // @tsv TODO - compare loaded with requested b_read( module_fd, (char*)&flat, sizeof(flat) ); if ( flat.rev!=FLAT_VERSION || memcmp( flat.magic, FLAT_MAGIC_NUMBER, 4) ) { moduleload_error("bad magicnum", 0); return 0; } size_flat = flat.reloc_start; flat_buf = (struct flat_hdr*)malloc( size_flat ); if ( !flat_buf ) { moduleload_error("malloc",0); return 0; } module_log_load(module_filename,flat_buf); if ( 0!= lseek(module_fd, 0, SEEK_SET) ) { moduleload_error("read",0); return 0; } if ( size_flat != b_read(module_fd, (char*)flat_buf, size_flat) ) { moduleload_error("read",0); return 0; } // Module info checks struct ModuleInfo *mod_info = flat_buf->_module_info = (struct ModuleInfo*)((unsigned int)flat_buf+flat_buf->_module_info_offset); if ( mod_info->magicnum != MODULEINFO_V1_MAGICNUM || mod_info->sizeof_struct != sizeof(struct ModuleInfo) ) { moduleload_error("Malformed module info", 0 ); return 0; } if ( mod_info->chdk_required_branch && mod_info->chdk_required_branch != CURRENT_CHDK_BRANCH ) { moduleload_error("require different CHDK branch",0 ); return 0; } if ( mod_info->chdk_required_ver > CHDK_BUILD_NUM) { moduleload_error("require CHDK%05d", mod_info->chdk_required_ver); return 0; } if ( mod_info->chdk_required_platfid && mod_info->chdk_required_platfid != conf.platformid ) { moduleload_error("require platfid %d", mod_info->chdk_required_platfid); return 0; } if ( !API_VERSION_MATCH_REQUIREMENT( mod_info->module_version, ver ) ) { moduleload_error("incorrect module version", 0); return 0; } if ( !API_VERSION_MATCH_REQUIREMENT( conf.api_version, mod_info->conf_ver ) ) { moduleload_error("incorrect CONF version", 0); return 0; } if ( !API_VERSION_MATCH_REQUIREMENT( camera_screen.api_version, mod_info->cam_screen_ver ) ) { moduleload_error("incorrect CAM SCREEN version", 0); return 0; } if ( !API_VERSION_MATCH_REQUIREMENT( camera_sensor.api_version, mod_info->cam_sensor_ver ) ) { moduleload_error("incorrect CAM SENSOR version", 0); return 0; } if ( !API_VERSION_MATCH_REQUIREMENT( camera_info.api_version, mod_info->cam_info_ver ) ) { moduleload_error("incorrect CAM INFO version", 0); return 0; } // Make relocations int reloc_size = flat.import_start - flat.reloc_start; int reloc_count = reloc_size/sizeof(uint32_t); int import_size = flat.file_size - flat.import_start; int import_count = import_size/sizeof(uint32_t); if (!alloc_reloc_buf(reloc_size, import_size)) return 0; if ( !module_do_action( "reloc", flat.reloc_start, reloc_count, reloc_size, module_do_relocations ) ) return 0; if ( !module_do_action( "export", flat.import_start, import_count, import_size, module_do_imports ) ) return 0; b_close( module_fd ); module_fd = -1; // TODO these could be changed to operate on affected address ranges only // after relocating but before attempting to execute loaded code // clean data cache to ensure code is in main memory dcache_clean_all(); // then flush instruction cache to ensure no addresses containing new code are cached icache_flush_all(); return flat_buf; }
void cache_sync_instructions(void) { dcache_clean_all(); /* includes trailing DSB (in assembly) */ icache_invalidate_all(); /* includes leading DSB and trailing ISB */ }