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 ****************************************************************************/
29 * EXIT_MAGIC magic, because 0 cannot be used due to setjmp()
32 #define EXIT_MAGIC 0x0CDEBABE
34 extern enum plugin_status
plugin_start(const void*);
35 extern unsigned char plugin_bss_start
[];
36 extern unsigned char plugin_end_addr
[];
38 static jmp_buf __exit_env
;
39 /* only 1 atexit handler for now, chain in the exit handler if you need more */
40 static void (*atexit_handler
)(void);
42 int rb_atexit(void (*fn
)(void))
51 { /* jump back in time to before starting the plugin */
52 longjmp(__exit_env
, status
!= 0 ? status
: EXIT_MAGIC
);
55 void _exit(int status
)
56 { /* don't call exit handler */
57 atexit_handler
= NULL
;
61 enum plugin_status
plugin__start(const void *param
)
64 enum plugin_status ret
;
66 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
68 /* IRAM must be copied before clearing the BSS ! */
69 #ifdef PLUGIN_USE_IRAM
70 extern char iramcopy
[], iramstart
[], iramend
[], iedata
[], iend
[];
71 size_t iram_size
= iramend
- iramstart
;
72 size_t ibss_size
= iend
- iedata
;
73 if (iram_size
> 0 || ibss_size
> 0)
75 /* We need to stop audio playback in order to use codec IRAM */
77 rb
->memcpy(iramstart
, iramcopy
, iram_size
);
78 rb
->memset(iedata
, 0, ibss_size
);
79 #ifdef HAVE_CPUCACHE_INVALIDATE
80 /* make the icache (if it exists) up to date with the new code */
81 rb
->cpucache_invalidate();
82 #endif /* HAVE_CPUCACHE_INVALIDATE */
84 /* barrier to prevent reordering iram copy and BSS clearing,
85 * because the BSS segment alias the IRAM copy.
87 asm volatile ("" ::: "memory");
89 #endif /* PLUGIN_USE_IRAM */
91 /* zero out the bss section */
92 rb
->memset(plugin_bss_start
, 0, plugin_end_addr
- plugin_bss_start
);
94 #ifdef HAVE_CPUCACHE_INVALIDATE
95 /* Some parts of bss may be used via a no-cache alias (at least
96 * portalplayer has this). If we don't clear the cache, those aliases
98 rb
->cpucache_invalidate();
99 #endif /* HAVE_CPUCACHE_INVALIDATE */
102 /* we come back here if exit() was called or the plugin returned normally */
103 exit_ret
= setjmp(__exit_env
);
105 { /* start the plugin */
106 ret
= plugin_start(param
);
109 { /* plugin exit via exit() */
110 if (exit_ret
== EXIT_MAGIC
)
111 { /* exit(EXIT_SUCCESS) */
114 else if (exit_ret
< INTERNAL_PLUGIN_RETVAL_START
)
115 { /* exit(EXIT_FAILURE) */
119 { /* exit(PLUGIN_XXX) */
120 ret
= (enum plugin_status
)exit_ret
;
124 /* before finishing, call the exit handler if there was one */
125 if (atexit_handler
!= NULL
)
131 static void cleanup_wrapper(void *param
)
138 void exit_on_usb(int button
)
139 { /* the default handler will call the exit handler before
140 * showing the usb screen; after that we don't want the exit handler
141 * to be called a second time, hence _exit()
143 * if not usb, then the handler will only be called if powering off
144 * if poweroff, the plugin doesn't want to run any further so exit as well*/
145 long result
= rb
->default_event_handler_ex(button
, cleanup_wrapper
, NULL
);
146 if (result
== SYS_USB_CONNECTED
)
147 _exit(PLUGIN_USB_CONNECTED
);
148 else if (result
== SYS_POWEROFF
)
149 _exit(PLUGIN_POWEROFF
);