Prepare new maemo release
[maemo-rb.git] / apps / plugins / plugin_crt0.c
blob680bb0723df40b09d76bce1b5c162083421c7cbe
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 ****************************************************************************/
23 #include "plugin.h"
24 #include <setjmp.h>
26 PLUGIN_HEADER
29 * EXIT_MAGIC magic, because 0 cannot be used due to setjmp()
30 * must be > 0
31 */
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))
44 if (atexit_handler)
45 return -1;
46 atexit_handler = fn;
47 return 0;
50 void exit(int status)
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;
58 exit(status);
61 enum plugin_status plugin__start(const void *param)
63 int exit_ret;
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 */
76 rb->audio_stop();
77 rb->memcpy(iramstart, iramcopy, iram_size);
78 rb->memset(iedata, 0, ibss_size);
79 /* make the icache (if it exists) up to date with the new code */
80 rb->commit_discard_idcache();
82 /* barrier to prevent reordering iram copy and BSS clearing,
83 * because the BSS segment alias the IRAM copy.
85 asm volatile ("" ::: "memory");
87 #endif /* PLUGIN_USE_IRAM */
89 /* zero out the bss section */
90 rb->memset(plugin_bss_start, 0, plugin_end_addr - plugin_bss_start);
92 /* Some parts of bss may be used via a no-cache alias (at least
93 * portalplayer has this). If we don't clear the cache, those aliases
94 * may read garbage */
95 rb->commit_dcache();
96 #endif
98 /* we come back here if exit() was called or the plugin returned normally */
99 exit_ret = setjmp(__exit_env);
100 if (exit_ret == 0)
101 { /* start the plugin */
102 ret = plugin_start(param);
104 else
105 { /* plugin exit via exit() */
106 if (exit_ret == EXIT_MAGIC)
107 { /* exit(EXIT_SUCCESS) */
108 ret = PLUGIN_OK;
110 else if (exit_ret < INTERNAL_PLUGIN_RETVAL_START)
111 { /* exit(EXIT_FAILURE) */
112 ret = PLUGIN_ERROR;
114 else
115 { /* exit(PLUGIN_XXX) */
116 ret = (enum plugin_status)exit_ret;
120 /* before finishing, call the exit handler if there was one */
121 if (atexit_handler != NULL)
122 atexit_handler();
124 return ret;
127 static void cleanup_wrapper(void *param)
129 (void)param;
130 if (atexit_handler)
131 atexit_handler();
134 void exit_on_usb(int button)
135 { /* the default handler will call the exit handler before
136 * showing the usb screen; after that we don't want the exit handler
137 * to be called a second time, hence _exit()
139 * if not usb, then the handler will only be called if powering off
140 * if poweroff, the plugin doesn't want to run any further so exit as well*/
141 long result = rb->default_event_handler_ex(button, cleanup_wrapper, NULL);
142 if (result == SYS_USB_CONNECTED)
143 _exit(PLUGIN_USB_CONNECTED);
144 else if (result == SYS_POWEROFF)
145 _exit(PLUGIN_POWEROFF);