1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 Daniel Stenberg
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 ****************************************************************************/
30 #include "ata_idle_notify.h"
32 #define HAVE_STATVFS (!defined(WIN32))
33 #define HAVE_LSTAT (!defined(WIN32))
36 #include <sys/statvfs.h>
47 #include "dir-win32.h"
51 #ifdef HAVE_SDL_THREADS
52 #include "thread-sdl.h"
54 #define sim_thread_unlock() NULL
55 #define sim_thread_lock(a)
60 #include "ata.h" /* for IF_MV2 et al. */
62 #include "load_code.h"
64 /* keep this in sync with file.h! */
65 #undef MAX_PATH /* this avoids problems when building simulator */
67 #define MAX_OPEN_FILES 11
69 /* Windows (and potentially other OSes) distinguish binary and text files.
70 * Define a dummy for the others. */
75 /* Unicode compatibility for win32 */
76 #if defined __MINGW32__
77 /* Rockbox unicode functions */
78 extern const unsigned char* utf8decode(const unsigned char *utf8
,
80 extern unsigned char* utf8encode(unsigned long ucs
, unsigned char *utf8
);
82 /* Static buffers for the conversion results. This isn't thread safe,
83 * but it's sufficient for rockbox. */
84 static unsigned char convbuf1
[3*MAX_PATH
];
85 static unsigned char convbuf2
[3*MAX_PATH
];
87 static wchar_t* utf8_to_ucs2(const unsigned char *utf8
, void *buffer
)
89 wchar_t *ucs
= buffer
;
92 utf8
= utf8decode(utf8
, ucs
++);
97 static unsigned char *ucs2_to_utf8(const wchar_t *ucs
, unsigned char *buffer
)
99 unsigned char *utf8
= buffer
;
102 utf8
= utf8encode(*ucs
++, utf8
);
108 #define UTF8_TO_OS(a) utf8_to_ucs2(a,convbuf1)
109 #define OS_TO_UTF8(a) ucs2_to_utf8(a,convbuf1)
111 #define DIRENT_T struct _wdirent
112 #define STAT_T struct _stat
113 extern int _wmkdir(const wchar_t*);
114 extern int _wrmdir(const wchar_t*);
115 #define MKDIR(a,b) (_wmkdir)(UTF8_TO_OS(a))
116 #define RMDIR(a) (_wrmdir)(UTF8_TO_OS(a))
117 #define OPENDIR(a) (_wopendir)(UTF8_TO_OS(a))
118 #define READDIR(a) (_wreaddir)(a)
119 #define CLOSEDIR(a) (_wclosedir)(a)
120 #define STAT(a,b) (_wstat)(UTF8_TO_OS(a),b)
121 /* empty variable parameter list doesn't work for variadic macros,
122 * so pretend the second parameter is variable too */
123 #define OPEN(a,...) (_wopen)(UTF8_TO_OS(a), __VA_ARGS__)
124 #define CLOSE(a) (close)(a)
125 #define REMOVE(a) (_wremove)(UTF8_TO_OS(a))
126 #define RENAME(a,b) (_wrename)(UTF8_TO_OS(a),utf8_to_ucs2(b,convbuf2))
128 #else /* !__MINGW32__ */
130 #define UTF8_TO_OS(a) (a)
131 #define OS_TO_UTF8(a) (a)
133 #define DIRENT_T struct dirent
134 #define STAT_T struct stat
135 #define MKDIR(a,b) (mkdir)(a,b)
136 #define RMDIR(a) (rmdir)(a)
137 #define OPENDIR(a) (opendir)(a)
138 #define READDIR(a) (readdir)(a)
139 #define CLOSEDIR(a) (closedir)(a)
140 #define STAT(a,b) (stat)(a,b)
141 /* empty variable parameter list doesn't work for variadic macros,
142 * so pretend the second parameter is variable too */
143 #define OPEN(a, ...) (open)(a, __VA_ARGS__)
144 #define CLOSE(x) (close)(x)
145 #define REMOVE(a) (remove)(a)
146 #define RENAME(a,b) (rename)(a,b)
148 #endif /* !__MINGW32__ */
152 int dircache_get_entry_id(const char *filename
);
153 void dircache_add_file(const char *name
, long startcluster
);
154 void dircache_remove(const char *name
);
155 void dircache_rename(const char *oldname
, const char *newname
);
159 #define SIMULATOR_DEFAULT_ROOT "simdisk"
160 extern const char *sim_root_dir
;
162 static int num_openfiles
= 0;
168 unsigned short wrtdate
;
169 unsigned short wrttime
;
173 unsigned char d_name
[MAX_PATH
];
179 void *dir
; /* actually a DIR* dir */
188 typedef struct mydir MYDIR
;
190 static unsigned int rockbox2sim(int opt
)
193 /* this shouldn't be needed since we use the host's versions */
194 int newopt
= O_BINARY
;
213 /** Simulator I/O engine routines **/
214 #define IO_YIELD_THRESHOLD 512
224 struct mutex sim_mutex
; /* Rockbox mutex */
225 int cmd
; /* The command to perform */
226 int ready
; /* I/O ready flag - 1= ready */
227 int fd
; /* The file to read/write */
228 void *buf
; /* The buffer to read/write */
229 size_t count
; /* Number of bytes to read/write */
230 size_t accum
; /* Acculated bytes transferred */
233 static struct sim_io io
;
237 /* Initialize the rockbox kernel objects on a rockbox thread */
238 mutex_init(&io
.sim_mutex
);
243 int ata_spinup_time(void)
248 static ssize_t
io_trigger_and_wait(enum io_dir cmd
)
250 void *mythread
= NULL
;
253 if (io
.count
> IO_YIELD_THRESHOLD
||
254 (io
.accum
+= io
.count
) >= IO_YIELD_THRESHOLD
)
256 /* Allow other rockbox threads to run */
258 mythread
= sim_thread_unlock();
264 result
= read(io
.fd
, io
.buf
, io
.count
);
267 result
= write(io
.fd
, io
.buf
, io
.count
);
274 call_storage_idle_notifys(false);
276 /* Regain our status as current */
277 if (mythread
!= NULL
)
279 sim_thread_lock(mythread
);
285 #if !defined(APPLICATION)
286 static const char *get_sim_pathname(const char *name
)
288 static char buffer
[MAX_PATH
]; /* sufficiently big */
292 snprintf(buffer
, sizeof(buffer
), "%s%s",
293 sim_root_dir
!= NULL
? sim_root_dir
: SIMULATOR_DEFAULT_ROOT
, name
);
296 fprintf(stderr
, "WARNING, bad file name lacks slash: %s\n", name
);
300 #define get_sim_pathname(name) name
303 MYDIR
*sim_opendir(const char *name
)
306 dir
= (DIR_T
*) OPENDIR(get_sim_pathname(name
));
310 MYDIR
*my
= (MYDIR
*)malloc(sizeof(MYDIR
));
312 my
->name
= (char *)malloc(strlen(name
)+1);
313 strcpy(my
->name
, name
);
317 /* failed open, return NULL */
322 static inline struct tm
* localtime_r (const time_t *clock
, struct tm
*result
) {
323 if (!clock
|| !result
) return NULL
;
324 memcpy(result
,localtime(clock
),sizeof(*result
));
329 struct sim_dirent
*sim_readdir(MYDIR
*dir
)
331 char buffer
[MAX_PATH
]; /* sufficiently big */
332 static struct sim_dirent secret
;
340 x11
= READDIR(dir
->dir
);
343 return (struct sim_dirent
*)0;
345 strcpy((char *)secret
.d_name
, OS_TO_UTF8(x11
->d_name
));
347 /* build file name */
348 snprintf(buffer
, sizeof(buffer
), "%s/%s",
349 get_sim_pathname(dir
->name
), secret
.d_name
);
351 if (STAT(buffer
, &s
)) /* get info */
354 /* File size larger than 2 GB? */
355 if (errno
== EOVERFLOW
)
357 DEBUGF("stat() overflow for %s. Skipping\n", buffer
);
365 #define ATTR_DIRECTORY 0x10
367 secret
.info
.attribute
= 0;
369 if (S_ISDIR(s
.st_mode
))
370 secret
.info
.attribute
= ATTR_DIRECTORY
;
372 secret
.info
.size
= s
.st_size
;
374 if (localtime_r(&(s
.st_mtime
), &tm
) == NULL
)
376 secret
.info
.wrtdate
= ((tm
.tm_year
- 80) << 9) |
377 ((tm
.tm_mon
+ 1) << 5) |
379 secret
.info
.wrttime
= (tm
.tm_hour
<< 11) |
384 #define ATTR_LINK 0x80
385 if (!lstat(buffer
, &s
) && S_ISLNK(s
.st_mode
))
387 secret
.info
.attribute
|= ATTR_LINK
;
394 void sim_closedir(MYDIR
*dir
)
402 int sim_open(const char *name
, int o
, ...)
404 int opts
= rockbox2sim(o
);
406 if (num_openfiles
>= MAX_OPEN_FILES
)
413 mode_t mode
= va_arg(ap
, unsigned int);
414 ret
= OPEN(get_sim_pathname(name
), opts
, mode
);
416 if (ret
>= 0 && (dircache_get_entry_id(name
) < 0))
417 dircache_add_file(name
, 0);
422 ret
= OPEN(get_sim_pathname(name
), opts
);
429 int sim_close(int fd
)
438 int sim_creat(const char *name
, mode_t mode
)
440 int ret
= OPEN(get_sim_pathname(name
),
441 O_BINARY
| O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
443 if (ret
>= 0 && (dircache_get_entry_id(name
) < 0))
444 dircache_add_file(name
, 0);
449 ssize_t
sim_read(int fd
, void *buf
, size_t count
)
453 mutex_lock(&io
.sim_mutex
);
455 /* Setup parameters */
460 result
= io_trigger_and_wait(IO_READ
);
462 mutex_unlock(&io
.sim_mutex
);
467 ssize_t
sim_write(int fd
, const void *buf
, size_t count
)
471 mutex_lock(&io
.sim_mutex
);
477 result
= io_trigger_and_wait(IO_WRITE
);
479 mutex_unlock(&io
.sim_mutex
);
484 int sim_mkdir(const char *name
)
486 return MKDIR(get_sim_pathname(name
), 0777);
489 int sim_rmdir(const char *name
)
491 return RMDIR(get_sim_pathname(name
));
494 int sim_remove(const char *name
)
496 int ret
= REMOVE(get_sim_pathname(name
));
499 dircache_remove(name
);
504 int sim_rename(const char *oldname
, const char *newname
)
506 char sim_old
[MAX_PATH
];
507 char sim_new
[MAX_PATH
];
509 dircache_rename(oldname
, newname
);
511 // This is needed as get_sim_pathname() has a static buffer
512 strncpy(sim_old
, get_sim_pathname(oldname
), MAX_PATH
);
513 strncpy(sim_new
, get_sim_pathname(newname
), MAX_PATH
);
514 return RENAME(sim_old
, sim_new
);
517 /* rockbox off_t may be different from system off_t */
518 long sim_lseek(int fildes
, long offset
, int whence
)
520 return lseek(fildes
, offset
, whence
);
523 long filesize(int fd
)
526 return _filelength(fd
);
530 if (!fstat(fd
, &buf
))
537 void fat_size(IF_MV2(int volume
,) unsigned long* size
, unsigned long* free
)
539 #ifdef HAVE_MULTIVOLUME
541 /* debugf("io.c: fat_size(volume=%d); simulator only supports volume 0\n",volume); */
550 long secperclus
, bytespersec
, free_clusters
, num_clusters
;
552 if (GetDiskFreeSpace(NULL
, &secperclus
, &bytespersec
, &free_clusters
,
555 *size
= num_clusters
* secperclus
/ 2 * (bytespersec
/ 512);
557 *free
= free_clusters
* secperclus
/ 2 * (bytespersec
/ 512);
562 if (!statvfs(".", &vfs
)) {
563 DEBUGF("statvfs: frsize=%d blocks=%ld free=%ld\n",
564 (int)vfs
.f_frsize
, (long)vfs
.f_blocks
, (long)vfs
.f_bfree
);
566 *size
= vfs
.f_blocks
/ 2 * (vfs
.f_frsize
/ 512);
568 *free
= vfs
.f_bfree
/ 2 * (vfs
.f_frsize
/ 512);
579 int sim_fsync(int fd
)
590 #include <SDL_loadso.h>
591 void *lc_open(const char *filename
, unsigned char *buf
, size_t buf_size
)
595 void *handle
= SDL_LoadObject(get_sim_pathname(filename
));
598 DEBUGF("failed to load %s\n", filename
);
599 DEBUGF("lc_open(%s): %s\n", filename
, SDL_GetError());
604 void *lc_get_header(void *handle
)
606 char *ret
= SDL_LoadFunction(handle
, "__header");
608 ret
= SDL_LoadFunction(handle
, "___header");
613 void lc_close(void *handle
)
615 SDL_UnloadObject(handle
);
618 void *lc_open_from_mem(void *addr
, size_t blob_size
)
623 /* we don't support loading code from memory on application builds,
624 * it doesn't make sense (since it means writing the blob to disk again and
625 * then falling back to load from disk) and requires the ability to write
626 * to an executable directory */
629 /* support it in the sim for the sake of simulating */
631 char temp_filename
[MAX_PATH
];
633 /* We have to create the dynamic link library file from ram so we
634 can simulate the codec loading. With voice and crossfade,
635 multiple codecs may be loaded at the same time, so we need
636 to find an unused filename */
637 for (i
= 0; i
< 10; i
++)
639 snprintf(temp_filename
, sizeof(temp_filename
),
640 ROCKBOX_DIR
"/libtemp_binary_%d.dll", i
);
641 fd
= open(temp_filename
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0700);
643 break; /* Created a file ok */
648 DEBUGF("open failed\n");
652 if (write(fd
, addr
, blob_size
) < (ssize_t
)blob_size
)
654 DEBUGF("Write failed\n");
656 remove(temp_filename
);
661 return lc_open(temp_filename
, NULL
, 0);
665 #endif /* __PCTOOL__ */
667 /* rockbox off_t may be different from system off_t */
668 int sim_ftruncate(int fd
, long length
)
671 return _chsize(fd
, length
);
673 return ftruncate(fd
, length
);