From 3f365fc06b67f8842b2e155349110f7c5659768d Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Sat, 26 May 2012 22:46:56 +0200 Subject: load_code: Get rid of win32 specific code in favor SDL_LoadFunction & friends APIs. Refactor native/hosted implementation seperation while at it (no wrappers starting with _ anymore). Change-Id: If68ae89700443bb3be483c1cace3d6739409560a --- firmware/SOURCES | 2 +- firmware/export/load_code.h | 20 ++------ firmware/load_code.c | 104 --------------------------------------- firmware/target/hosted/lc-unix.c | 34 ++++++++++--- uisimulator/common/io.c | 64 +++++++++++++++++++++--- 5 files changed, 90 insertions(+), 134 deletions(-) diff --git a/firmware/SOURCES b/firmware/SOURCES index c2bd45996c..eb49e3e581 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES @@ -7,7 +7,6 @@ backlight.c buflib.c core_alloc.c general.c -load_code.c powermgmt.c #if (CONFIG_PLATFORM & PLATFORM_HOSTED) @@ -32,6 +31,7 @@ logf.c #endif /* ROCKBOX_HAS_LOGF */ kernel.c #if (CONFIG_PLATFORM & PLATFORM_NATIVE) +load_code.c #ifdef RB_PROFILE profile.c #endif /* RB_PROFILE */ diff --git a/firmware/export/load_code.h b/firmware/export/load_code.h index 6f8505aba7..cca577044e 100644 --- a/firmware/export/load_code.h +++ b/firmware/export/load_code.h @@ -25,10 +25,11 @@ #include "config.h" +extern void *lc_open(const char *filename, unsigned char *buf, size_t buf_size); + #if (CONFIG_PLATFORM & PLATFORM_NATIVE) #include "system.h" -extern void *lc_open(const char *filename, unsigned char *buf, size_t buf_size); /* header is always at the beginning of the blob, and handle actually points * to the start of the blob (the header is there) */ static inline void *lc_open_from_mem(void* addr, size_t blob_size) @@ -44,23 +45,10 @@ static inline void lc_close(void *handle) { (void)handle; } #elif (CONFIG_PLATFORM & PLATFORM_HOSTED) -/* don't call these directly for loading code - * they're to be wrapped by platform specific functions */ -#ifdef WIN32 -/* windows' LoadLibrary can only handle ucs2, no utf-8 */ -#define _lc_open_char wchar_t -#else -#define _lc_open_char char -#endif -extern void *_lc_open(const _lc_open_char *filename, unsigned char *buf, size_t buf_size); -extern void *_lc_get_header(void *handle); -extern void _lc_close(void *handle); - -extern void *lc_open(const char *filename, unsigned char *buf, size_t buf_size); -extern void *lc_open_from_mem(void *addr, size_t blob_size); +extern void *lc_open_from_mem(void* addr, size_t blob_size); extern void *lc_get_header(void *handle); extern void lc_close(void *handle); -extern const char* lc_last_error(void); + #endif /* this struct needs to be the first part of other headers diff --git a/firmware/load_code.c b/firmware/load_code.c index a76aca380d..d22d6bb3b2 100644 --- a/firmware/load_code.c +++ b/firmware/load_code.c @@ -25,8 +25,6 @@ #include "debug.h" #include "load_code.h" -#if (CONFIG_PLATFORM & PLATFORM_NATIVE) - /* load binary blob from disk to memory, returning a handle */ void * lc_open(const char *filename, unsigned char *buf, size_t buf_size) { @@ -97,105 +95,3 @@ error_fd: error: return NULL; } - -#elif (CONFIG_PLATFORM & PLATFORM_HOSTED) -/* libdl wrappers */ - - -#ifdef WIN32 -/* win32 */ -#include -#define dlopen(_x_, _y_) LoadLibraryW(_x_) -#define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_) -#define dlclose(_x_) FreeLibrary(_x_) -static inline char *_dlerror(void) -{ - static char err_buf[64]; - FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, - err_buf, sizeof(err_buf), NULL); - return err_buf; -} -#define dlerror _dlerror -#else -/* unix */ -#include -#endif -#include -#include "rbpaths.h" -#include "general.h" - -void * _lc_open(const _lc_open_char *filename, unsigned char *buf, size_t buf_size) -{ - (void)buf; - (void)buf_size; - return dlopen(filename, RTLD_NOW); -} - -void *lc_open_from_mem(void *addr, size_t blob_size) -{ -#ifndef SIMULATOR - (void)addr; - (void)blob_size; - /* we don't support loading code from memory on application builds, - * it doesn't make sense (since it means writing the blob to disk again and - * then falling back to load from disk) and requires the ability to write - * to an executable directory */ - return NULL; -#else - /* support it in the sim for the sake of simulating */ - int fd, i; - char temp_filename[MAX_PATH]; - - /* We have to create the dynamic link library file from ram so we - can simulate the codec loading. With voice and crossfade, - multiple codecs may be loaded at the same time, so we need - to find an unused filename */ - for (i = 0; i < 10; i++) - { - snprintf(temp_filename, sizeof(temp_filename), - ROCKBOX_DIR "/libtemp_binary_%d.dll", i); - fd = open(temp_filename, O_WRONLY|O_CREAT|O_TRUNC, 0700); - if (fd >= 0) - break; /* Created a file ok */ - } - - if (fd < 0) - { - DEBUGF("open failed\n"); - return NULL; - } - - if (write(fd, addr, blob_size) < (ssize_t)blob_size) - { - DEBUGF("Write failed\n"); - close(fd); - remove(temp_filename); - return NULL; - } - - close(fd); - return lc_open(temp_filename, NULL, 0); -#endif -} - - -void *_lc_get_header(void *handle) -{ - char *ret = dlsym(handle, "__header"); - if (ret == NULL) - ret = dlsym(handle, "___header"); - - return ret; -} - -void _lc_close(void *handle) -{ - if (handle) - dlclose(handle); -} - -const char *lc_last_error(void) -{ - return dlerror(); -} -#endif diff --git a/firmware/target/hosted/lc-unix.c b/firmware/target/hosted/lc-unix.c index 8a265de066..6e5f15ec99 100644 --- a/firmware/target/hosted/lc-unix.c +++ b/firmware/target/hosted/lc-unix.c @@ -20,24 +20,44 @@ ****************************************************************************/ #include /* size_t */ +#include +#include "debug.h" #include "load_code.h" -/* unix specific because WIN32 wants UCS instead of UTF-8, so filenames - * need to be converted */ - -/* plain wrappers , nothing to do */ void *lc_open(const char *filename, unsigned char *buf, size_t buf_size) { - return _lc_open(filename, buf, buf_size); + (void)buf; + (void)buf_size; + void *handle = dlopen(filename, RTLD_NOW); + if (handle == NULL) + { + DEBUGF("failed to load %s\n", filename); + DEBUGF("lc_open(%s): %s\n", filename, dlerror()); + } + return handle; } void *lc_get_header(void *handle) { - return _lc_get_header(handle); + char *ret = dlsym(handle, "__header"); + if (ret == NULL) + ret = dlsym(handle, "___header"); + + return ret; } void lc_close(void *handle) { - _lc_close(handle); + dlclose(handle); } +void *lc_open_from_mem(void *addr, size_t blob_size) +{ + (void)addr; + (void)blob_size; + /* we don't support loading code from memory on application builds, + * it doesn't make sense (since it means writing the blob to disk again and + * then falling back to load from disk) and requires the ability to write + * to an executable directory */ + return NULL; +} diff --git a/uisimulator/common/io.c b/uisimulator/common/io.c index 6f9f4b2945..fdacc59069 100644 --- a/uisimulator/common/io.c +++ b/uisimulator/common/io.c @@ -584,27 +584,79 @@ int sim_fsync(int fd) #ifndef __PCTOOL__ +#include void *lc_open(const char *filename, unsigned char *buf, size_t buf_size) { - const char *sim_path = get_sim_pathname(filename); - void *handle = _lc_open(UTF8_TO_OS(sim_path), buf, buf_size); - + (void)buf; + (void)buf_size; + void *handle = SDL_LoadObject(get_sim_pathname(filename)); if (handle == NULL) { DEBUGF("failed to load %s\n", filename); - DEBUGF("lc_open(%s): %s\n", filename, lc_last_error()); + DEBUGF("lc_open(%s): %s\n", filename, SDL_GetError()); } return handle; } void *lc_get_header(void *handle) { - return _lc_get_header(handle); + char *ret = SDL_LoadFunction(handle, "__header"); + if (ret == NULL) + ret = SDL_LoadFunction(handle, "___header"); + + return ret; } void lc_close(void *handle) { - _lc_close(handle); + SDL_UnloadObject(handle); +} + +void *lc_open_from_mem(void *addr, size_t blob_size) +{ +#ifndef SIMULATOR + (void)addr; + (void)blob_size; + /* we don't support loading code from memory on application builds, + * it doesn't make sense (since it means writing the blob to disk again and + * then falling back to load from disk) and requires the ability to write + * to an executable directory */ + return NULL; +#else + /* support it in the sim for the sake of simulating */ + int fd, i; + char temp_filename[MAX_PATH]; + + /* We have to create the dynamic link library file from ram so we + can simulate the codec loading. With voice and crossfade, + multiple codecs may be loaded at the same time, so we need + to find an unused filename */ + for (i = 0; i < 10; i++) + { + snprintf(temp_filename, sizeof(temp_filename), + ROCKBOX_DIR "/libtemp_binary_%d.dll", i); + fd = open(temp_filename, O_WRONLY|O_CREAT|O_TRUNC, 0700); + if (fd >= 0) + break; /* Created a file ok */ + } + + if (fd < 0) + { + DEBUGF("open failed\n"); + return NULL; + } + + if (write(fd, addr, blob_size) < (ssize_t)blob_size) + { + DEBUGF("Write failed\n"); + close(fd); + remove(temp_filename); + return NULL; + } + + close(fd); + return lc_open(temp_filename, NULL, 0); +#endif } #endif /* __PCTOOL__ */ -- cgit v1.2.3