Use the AMS_LOWMEM define to indicate memory size as the .lds files do in config...
[kugel-rb.git] / apps / plugins / lib / overlay.c
blob44bca1d98963af3bc949d541da0327815d05c4f9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Overlay loader
12 * Copyright (C) 2006 Jens Arnold
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #ifndef SIMULATOR
25 #include "plugin.h"
26 #include "overlay.h"
28 /* load and run a plugin linked as an overlay.
30 arguments:
31 rb = pointer to plugin api, also passed on to the overlay
32 parameter = plugin parameter, passed on to the overlay
33 filename = overlay file name, absolute path as usual
34 name = overlay display name
36 result:
37 return value from the overlay
39 As long as a large plugin to be overlayed doesn't use the audiobuffer
40 itself, no adjustments in the plugin source code are necessary to make
41 it work as an overlay, it only needs an adapted linker script.
43 If the overlayed plugin *does* use the audiobuffer itself, it needs
44 to make sure not to overwrite itself.
46 The linker script for the overlay should use a base address towards the
47 end of the audiobuffer, just low enough to make the overlay fit. */
49 enum plugin_status run_overlay(const void* parameter,
50 unsigned char *filename, unsigned char *name)
52 int fd, readsize;
53 ssize_t audiobuf_size;
54 unsigned char *audiobuf;
55 static struct plugin_header header;
57 fd = rb->open(filename, O_RDONLY);
58 if (fd < 0)
60 rb->splashf(2*HZ, "Can't open %s", filename);
61 return PLUGIN_ERROR;
63 readsize = rb->read(fd, &header, sizeof(header));
64 rb->close(fd);
65 /* Close for now. Less code than doing it in all error checks.
66 * Would need to seek back anyway. */
68 if (readsize != sizeof(header))
70 rb->splashf(2*HZ, "Reading %s overlay failed.", name);
71 return PLUGIN_ERROR;
73 if (header.magic != PLUGIN_MAGIC || header.target_id != TARGET_ID)
75 rb->splashf(2*HZ, "%s overlay: Incompatible model.", name);
76 return PLUGIN_ERROR;
78 if (header.api_version != PLUGIN_API_VERSION)
80 rb->splashf(2*HZ, "%s overlay: Incompatible version.", name);
81 return PLUGIN_ERROR;
84 audiobuf = rb->plugin_get_audio_buffer((size_t *)&audiobuf_size);
85 if (header.load_addr < audiobuf ||
86 header.end_addr > audiobuf + audiobuf_size)
88 rb->splashf(2*HZ, "%s overlay doesn't fit into memory.", name);
89 return PLUGIN_ERROR;
92 fd = rb->open(filename, O_RDONLY);
93 if (fd < 0)
95 rb->splashf(2*HZ, "Can't open %s", filename);
96 return PLUGIN_ERROR;
98 readsize = rb->read(fd, header.load_addr, header.end_addr - header.load_addr);
99 rb->close(fd);
101 if (readsize < 0)
103 rb->splashf(2*HZ, "Reading %s overlay failed.", name);
104 return PLUGIN_ERROR;
106 /* Zero out bss area */
107 rb->memset(header.load_addr + readsize, 0,
108 header.end_addr - (header.load_addr + readsize));
110 *(header.api) = rb;
111 return header.entry_point(parameter);
113 #endif