1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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
);
37 DEBUGF("Could not open file");
42 /* Make sure COP cache is flushed and invalidated before loading */
44 int my_core
= switch_core(CURRENT_CORE
^ 1);
45 cpucache_invalidate();
50 read_size
= read(fd
, buf
, buf_size
);
52 cpucache_invalidate();
56 DEBUGF("Could not read from file");
62 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
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
);
79 #define dlerror _dlerror
89 void * _lc_open(const char *filename
, char *buf
, size_t buf_size
)
93 void* dl_handle
= dlopen(filename
, RTLD_NOW
);
97 void *lc_open_from_mem(void *addr
, size_t blob_size
)
100 char temp_filename
[MAX_PATH
];
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
++)
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)
113 snprintf(temp_filename
, sizeof(temp_filename
),
114 "/data/data/org.rockbox/app_rockbox/libtemp_binary_%d.so", i
);
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
);
121 fd
= open(temp_filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, 0766);
123 break; /* Created a file ok */
126 DEBUGF("Creating %s\n", temp_filename
);
129 DEBUGF("open failed\n");
133 if (write(fd
, addr
, blob_size
) < (ssize_t
)blob_size
)
135 DEBUGF("Write failed\n");
137 remove(temp_filename
);
142 return lc_open(temp_filename
, NULL
, 0);
146 void *_lc_get_header(void *handle
)
148 char *ret
= dlsym(handle
, "__header");
150 ret
= dlsym(handle
, "___header");
155 void _lc_close(void *handle
)
161 const char *lc_last_error(void)