Calibrate iPod Classic battery gauge a bit better
[kugel-rb.git] / firmware / load_code.c
blob59eb7ac0f78815bee35252787de3e6972f403f1b
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)
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);
34 ssize_t read_size;
35 struct lc_header hdr;
36 unsigned char *buf_end = buf+buf_size;
37 off_t copy_size;
39 if (fd < 0)
41 DEBUGF("Could not open file");
42 goto error;
45 #if NUM_CORES > 1
46 /* Make sure COP cache is flushed and invalidated before loading */
48 int my_core = switch_core(CURRENT_CORE ^ 1);
49 cpucache_commit_discard();
50 switch_core(my_core);
52 #endif
54 /* read the header to obtain the load address */
55 read_size = read(fd, &hdr, sizeof(hdr));
57 if (read_size < 0)
59 DEBUGF("Could not read from file");
60 goto error_fd;
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
65 * can be larger */
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");
71 goto error_fd;
74 /* go back to beginning to load the whole thing (incl. header) */
75 if (lseek(fd, 0, SEEK_SET) < 0)
77 DEBUGF("lseek failed");
78 goto error_fd;
81 /* the header has the addresses where the code is linked at */
82 read_size = read(fd, hdr.load_addr, copy_size);
83 close(fd);
85 if (read_size < 0)
87 DEBUGF("Could not read from file");
88 goto error;
91 /* commit dcache and discard icache */
92 cpucache_commit_discard();
93 /* return a pointer the header, reused by lc_get_header() */
94 return hdr.load_addr;
96 error_fd:
97 close(fd);
98 error:
99 return NULL;
102 #elif (CONFIG_PLATFORM & PLATFORM_HOSTED)
103 /* libdl wrappers */
106 #ifdef WIN32
107 /* win32 */
108 #include <windows.h>
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);
117 return err_buf;
119 #define dlerror _dlerror
120 #else
121 /* unix */
122 #include <dlfcn.h>
123 #endif
124 #include <stdio.h>
125 #include "rbpaths.h"
126 #include "general.h"
128 void * _lc_open(const _lc_open_char *filename, unsigned char *buf, size_t buf_size)
130 (void)buf;
131 (void)buf_size;
132 return dlopen(filename, RTLD_NOW);
135 void *lc_open_from_mem(void *addr, size_t blob_size)
137 #ifndef SIMULATOR
138 (void)addr;
139 (void)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 */
144 return NULL;
145 #else
146 /* support it in the sim for the sake of simulating */
147 int fd, i;
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);
159 if (fd >= 0)
160 break; /* Created a file ok */
163 if (fd < 0)
165 DEBUGF("open failed\n");
166 return NULL;
169 if (write(fd, addr, blob_size) < (ssize_t)blob_size)
171 DEBUGF("Write failed\n");
172 close(fd);
173 remove(temp_filename);
174 return NULL;
177 close(fd);
178 return lc_open(temp_filename, NULL, 0);
179 #endif
183 void *_lc_get_header(void *handle)
185 char *ret = dlsym(handle, "__header");
186 if (ret == NULL)
187 ret = dlsym(handle, "___header");
189 return ret;
192 void _lc_close(void *handle)
194 if (handle)
195 dlclose(handle);
198 const char *lc_last_error(void)
200 return dlerror();
202 #endif