Revert r27972 to fix FS#11610 (but in a way android builds still work).
[maemo-rb.git] / firmware / load_code.c
blob75bac8b2ac302e34f83e9d7553085d858dafe4e6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 by Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "system.h"
24 #include "file.h"
25 #include "debug.h"
26 #include "load_code.h"
28 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
29 /* load binary blob from disk to memory, returning a handle */
30 void * lc_open(const char *filename, char *buf, size_t buf_size)
32 int fd = open(filename, O_RDONLY);
33 ssize_t read_size;
35 if (fd < 0)
37 DEBUGF("Could not open file");
38 return NULL;
41 #if NUM_CORES > 1
42 /* Make sure COP cache is flushed and invalidated before loading */
44 int my_core = switch_core(CURRENT_CORE ^ 1);
45 cpucache_invalidate();
46 switch_core(my_core);
48 #endif
50 read_size = read(fd, buf, buf_size);
51 close(fd);
52 cpucache_invalidate();
54 if (read_size < 0)
56 DEBUGF("Could not read from file");
57 return NULL;
59 return buf;
62 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
63 /* libdl wrappers */
66 #ifdef WIN32
67 /* win32 */
68 #include <windows.h>
69 #define dlopen(_x_, _y_) LoadLibraryW(_x_)
70 #define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_)
71 #define dlclose(_x_) FreeLibrary(_x_)
72 static inline char *_dlerror(void)
74 static char err_buf[64];
75 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
76 err_buf, sizeof(err_buf), NULL);
77 return err_buf;
79 #define dlerror _dlerror
80 #else
81 /* unix */
82 #include <dlfcn.h>
83 #endif
84 #include <stdio.h>
85 #include "rbpaths.h"
86 #include "general.h"
88 void * _lc_open(const _lc_open_char *filename, char *buf, size_t buf_size)
90 (void)buf;
91 (void)buf_size;
92 void* dl_handle = dlopen(filename, RTLD_NOW);
93 return dl_handle;
96 void *lc_open_from_mem(void *addr, size_t blob_size)
98 int fd, i;
99 char temp_filename[MAX_PATH];
101 /* We have to create the dynamic link library file from ram so we
102 can simulate the codec loading. With voice and crossfade,
103 multiple codecs may be loaded at the same time, so we need
104 to find an unused filename */
105 for (i = 0; i < 10; i++)
107 #if (CONFIG_PLATFORM & PLATFORM_ANDROID)
108 /* we need that path fixed, since get_user_file_path()
109 * gives us the folder on the sdcard where we cannot load libraries
110 * from (no exec permissions)
112 snprintf(temp_filename, sizeof(temp_filename),
113 "/data/data/org.rockbox/app_rockbox/libtemp_binary_%d.so", i);
114 #else
115 char name[MAX_PATH];
116 const char *_name = get_user_file_path(ROCKBOX_DIR, NEED_WRITE, name, sizeof(name));
117 snprintf(temp_filename, sizeof(temp_filename),
118 "%s/libtemp_binary_%d.dll", _name, i);
119 #endif
120 fd = open(temp_filename, O_WRONLY|O_CREAT|O_TRUNC, 0700);
121 if (fd >= 0)
122 break; /* Created a file ok */
125 if (fd < 0)
127 DEBUGF("open failed\n");
128 return NULL;
131 if (write(fd, addr, blob_size) < (ssize_t)blob_size)
133 DEBUGF("Write failed\n");
134 close(fd);
135 remove(temp_filename);
136 return NULL;
139 close(fd);
140 return lc_open(temp_filename, NULL, 0);
144 void *_lc_get_header(void *handle)
146 char *ret = dlsym(handle, "__header");
147 if (ret == NULL)
148 ret = dlsym(handle, "___header");
150 return ret;
153 void _lc_close(void *handle)
155 if (handle)
156 dlclose(handle);
159 const char *lc_last_error(void)
161 return dlerror();
163 #endif