summaryrefslogtreecommitdiff
path: root/firmware/load_code.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/load_code.c')
-rw-r--r--firmware/load_code.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/firmware/load_code.c b/firmware/load_code.c
new file mode 100644
index 0000000000..9e8e71f9af
--- /dev/null
+++ b/firmware/load_code.c
@@ -0,0 +1,165 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2010 by Thomas Martitz
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "config.h"
23#include "system.h"
24#include "file.h"
25#include "debug.h"
26#include "load_code.h"
27
28#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
29/* load binary blob from disk to memory, returning a handle */
30void * lc_open(const char *filename, char *buf, size_t buf_size)
31{
32 int fd = open(filename, O_RDONLY);
33 ssize_t read_size;
34
35 if (fd < 0)
36 {
37 DEBUGF("Could not open file");
38 return NULL;
39 }
40
41#if NUM_CORES > 1
42 /* Make sure COP cache is flushed and invalidated before loading */
43 {
44 int my_core = switch_core(CURRENT_CORE ^ 1);
45 cpucache_invalidate();
46 switch_core(my_core);
47 }
48#endif
49
50 read_size = read(fd, buf, buf_size);
51 close(fd);
52 cpucache_invalidate();
53
54 if (read_size < 0)
55 {
56 DEBUGF("Could not read from file");
57 return NULL;
58 }
59 return buf;
60}
61
62#elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
63/* libdl wrappers */
64
65
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_)
72static inline char *_dlerror(void)
73{
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;
78}
79#define dlerror _dlerror
80#else
81/* unix */
82#include <dlfcn.h>
83#define O_BINARY 0
84#endif
85#include <stdio.h>
86#include "rbpaths.h"
87#include "general.h"
88
89void * _lc_open(const char *filename, char *buf, size_t buf_size)
90{
91 (void)buf;
92 (void)buf_size;
93 void* dl_handle = dlopen(filename, RTLD_NOW);
94 return dl_handle;
95}
96
97void *lc_open_from_mem(void *addr, size_t blob_size)
98{
99 int fd, i;
100 char temp_filename[MAX_PATH];
101
102 /* We have to create the dynamic link library file from ram so we
103 can simulate the codec loading. With voice and crossfade,
104 multiple codecs may be loaded at the same time, so we need
105 to find an unused filename */
106 for (i = 0; i < 10; i++)
107 {
108#if (CONFIG_PLATFORM & PLATFORM_ANDROID)
109 /* we need that path fixed, since get_user_file_path()
110 * gives us the folder on the sdcard where we cannot load libraries
111 * from (no exec permissions)
112 */
113 snprintf(temp_filename, sizeof(temp_filename),
114 "/data/data/org.rockbox/app_rockbox/libtemp_binary_%d.so", i);
115#else
116 char name[MAX_PATH];
117 const char *_name = get_user_file_path(ROCKBOX_DIR, NEED_WRITE, name, sizeof(name));
118 snprintf(temp_filename, sizeof(temp_filename),
119 "%slibtemp_binary_%d.dll", _name, i);
120#endif
121 fd = open(temp_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0766);
122 if (fd >= 0)
123 break; /* Created a file ok */
124 }
125
126 DEBUGF("Creating %s\n", temp_filename);
127 if (fd < 0)
128 {
129 DEBUGF("open failed\n");
130 return NULL;
131 }
132
133 if (write(fd, addr, blob_size) < (ssize_t)blob_size)
134 {
135 DEBUGF("Write failed\n");
136 close(fd);
137 remove(temp_filename);
138 return NULL;
139 }
140
141 close(fd);
142 return lc_open(temp_filename, NULL, 0);
143}
144
145
146void *_lc_get_header(void *handle)
147{
148 char *ret = dlsym(handle, "__header");
149 if (ret == NULL)
150 ret = dlsym(handle, "___header");
151
152 return ret;
153}
154
155void _lc_close(void *handle)
156{
157 if (handle)
158 dlclose(handle);
159}
160
161const char *lc_last_error(void)
162{
163 return dlerror();
164}
165#endif