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)
30 /* load binary blob from disk to memory, returning a handle */
31 void * lc_open(const char *filename
, unsigned char *buf
, size_t buf_size
)
33 int fd
= open(filename
, O_RDONLY
);
36 unsigned char *buf_end
= buf
+buf_size
;
41 DEBUGF("Could not open file");
46 /* Make sure COP cache is flushed and invalidated before loading */
48 int my_core
= switch_core(CURRENT_CORE
^ 1);
49 cpucache_commit_discard();
54 /* read the header to obtain the load address */
55 read_size
= read(fd
, &hdr
, sizeof(hdr
));
59 DEBUGF("Could not read from file");
63 /* hdr.end_addr points to the end of the bss section,
64 * but there might be idata/icode behind that so the bytes to copy
66 copy_size
= MAX(filesize(fd
), hdr
.end_addr
- hdr
.load_addr
);
68 if (hdr
.load_addr
< buf
|| (hdr
.load_addr
+copy_size
) > buf_end
)
70 DEBUGF("Binary doesn't fit into memory");
74 /* go back to beginning to load the whole thing (incl. header) */
75 if (lseek(fd
, 0, SEEK_SET
) < 0)
77 DEBUGF("lseek failed");
81 /* the header has the addresses where the code is linked at */
82 read_size
= read(fd
, hdr
.load_addr
, copy_size
);
87 DEBUGF("Could not read from file");
91 /* commit dcache and discard icache */
92 cpucache_commit_discard();
93 /* return a pointer the header, reused by lc_get_header() */
102 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
109 #define dlopen(_x_, _y_) LoadLibraryW(_x_)
110 #define dlsym(_x_, _y_) (void *)GetProcAddress(_x_, _y_)
111 #define dlclose(_x_) FreeLibrary(_x_)
112 static inline char *_dlerror(void)
114 static char err_buf
[64];
115 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, GetLastError(), 0,
116 err_buf
, sizeof(err_buf
), NULL
);
119 #define dlerror _dlerror
128 void * _lc_open(const _lc_open_char
*filename
, unsigned char *buf
, size_t buf_size
)
132 return dlopen(filename
, RTLD_NOW
);
135 void *lc_open_from_mem(void *addr
, size_t blob_size
)
140 /* we don't support loading code from memory on application builds,
141 * it doesn't make sense (since it means writing the blob to disk again and
142 * then falling back to load from disk) and requires the ability to write
143 * to an executable directory */
146 /* support it in the sim for the sake of simulating */
148 char temp_filename
[MAX_PATH
];
150 /* We have to create the dynamic link library file from ram so we
151 can simulate the codec loading. With voice and crossfade,
152 multiple codecs may be loaded at the same time, so we need
153 to find an unused filename */
154 for (i
= 0; i
< 10; i
++)
156 snprintf(temp_filename
, sizeof(temp_filename
),
157 ROCKBOX_DIR
"/libtemp_binary_%d.dll", i
);
158 fd
= open(temp_filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0700);
160 break; /* Created a file ok */
165 DEBUGF("open failed\n");
169 if (write(fd
, addr
, blob_size
) < (ssize_t
)blob_size
)
171 DEBUGF("Write failed\n");
173 remove(temp_filename
);
178 return lc_open(temp_filename
, NULL
, 0);
183 void *_lc_get_header(void *handle
)
185 char *ret
= dlsym(handle
, "__header");
187 ret
= dlsym(handle
, "___header");
192 void _lc_close(void *handle
)
198 const char *lc_last_error(void)