source: rtems/c/src/lib/libbsp/arm/nds/libnds/source/common/gbfs.c @ 32b8506

4.104.115
Last change on this file since 32b8506 was 32b8506, checked in by Ralf Corsepius <ralf.corsepius@…>, on 11/29/09 at 14:53:02

Whitespace removal.

  • Property mode set to 100644
File size: 15.2 KB
Line 
1/*---------------------------------------------------------------------------------
2        $Id$
3
4        access object in a GBFS file
5
6        Copyright 2002-2005 Damian Yerrick
7
8        Additional code Dave Murphy
9
10        Permission is hereby granted, free of charge, to any person obtaining
11        a copy of this software and associated documentation files (the
12        "Software"), to deal in the Software without restriction, including
13        without limitation the rights to use, copy, modify, merge, publish,
14        distribute, sublicense, and/or sell copies of the Software, and to
15        permit persons to whom the Software is furnished to do so, subject to
16        the following conditions:
17
18        The above copyright notice and this permission notice shall be
19        included in all copies or substantial portions of the Software.
20
21        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22        EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23        OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24        NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
25        BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26        AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
27        OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28        IN THE SOFTWARE.
29
30        $Log$
31        Revision 1.1  2008/04/16 18:37:33  joel
32        2008-04-16      Matthieu Bucchianeri <mbucchia@gmail.com>
33
34                * ChangeLog, Makefile.am, README, bsp_specs, configure.ac,
35                mk_libnds.sh, patch.libnds, preinstall.am, block/block.c,
36                clock/clock.c, console/console.c, coproc/coproc.S, coproc/coproc.c,
37                coproc/coproc.ld, dswifi/dswifi_license.txt, dswifi/makefile,
38                dswifi/arm7/makefile, dswifi/arm7/source/wifi_arm7.c,
39                dswifi/arm7/source/wifi_arm7.h, dswifi/arm9/makefile,
40                dswifi/arm9/source/sgIP.c, dswifi/arm9/source/sgIP.h,
41                dswifi/arm9/source/sgIP_ARP.c, dswifi/arm9/source/sgIP_ARP.h,
42                dswifi/arm9/source/sgIP_Config.h, dswifi/arm9/source/sgIP_DHCP.c,
43                dswifi/arm9/source/sgIP_DHCP.h, dswifi/arm9/source/sgIP_DNS.c,
44                dswifi/arm9/source/sgIP_DNS.h, dswifi/arm9/source/sgIP_Hub.c,
45                dswifi/arm9/source/sgIP_Hub.h, dswifi/arm9/source/sgIP_ICMP.c,
46                dswifi/arm9/source/sgIP_ICMP.h, dswifi/arm9/source/sgIP_IP.c,
47                dswifi/arm9/source/sgIP_IP.h, dswifi/arm9/source/sgIP_TCP.c,
48                dswifi/arm9/source/sgIP_TCP.h, dswifi/arm9/source/sgIP_UDP.c,
49                dswifi/arm9/source/sgIP_UDP.h, dswifi/arm9/source/sgIP_memblock.c,
50                dswifi/arm9/source/sgIP_memblock.h,
51                dswifi/arm9/source/sgIP_sockets.c, dswifi/arm9/source/sgIP_sockets.h,
52                dswifi/arm9/source/wifi_arm9.c, dswifi/arm9/source/wifi_arm9.h,
53                dswifi/common/source/dsregs.h, dswifi/common/source/spinlock.h,
54                dswifi/common/source/spinlock.s, dswifi/common/source/wifi_shared.h,
55                dswifi/include/dswifi7.h, dswifi/include/dswifi9.h,
56                dswifi/include/dswifi_version.h, dswifi/include/netdb.h,
57                dswifi/include/sgIP_errno.h, dswifi/include/netinet/in.h, fb/fb.c,
58                fb/fb.h, include/bsp.h, include/my_ipc.h, include/tm27.h,
59                include/types.h, include/sys/iosupport.h, irq/irq.c, irq/irq.h,
60                libfat/gba/include/fat.h, libfat/include/fat.h,
61                libfat/nds/include/fat.h, libfat/source/bit_ops.h,
62                libfat/source/cache.c, libfat/source/cache.h, libfat/source/common.h,
63                libfat/source/directory.c, libfat/source/directory.h,
64                libfat/source/fatdir.c, libfat/source/fatdir.h,
65                libfat/source/fatfile.c, libfat/source/fatfile.h,
66                libfat/source/file_allocation_table.c,
67                libfat/source/file_allocation_table.h, libfat/source/filetime.c,
68                libfat/source/filetime.h, libfat/source/libfat.c,
69                libfat/source/mem_allocate.h, libfat/source/partition.c,
70                libfat/source/partition.h, libfat/source/disc_io/disc.c,
71                libfat/source/disc_io/disc.h, libfat/source/disc_io/disc_io.h,
72                libfat/source/disc_io/io_cf_common.c,
73                libfat/source/disc_io/io_cf_common.h,
74                libfat/source/disc_io/io_dldi.h, libfat/source/disc_io/io_dldi.s,
75                libfat/source/disc_io/io_efa2.c, libfat/source/disc_io/io_efa2.h,
76                libfat/source/disc_io/io_fcsr.c, libfat/source/disc_io/io_fcsr.h,
77                libfat/source/disc_io/io_m3_common.c,
78                libfat/source/disc_io/io_m3_common.h,
79                libfat/source/disc_io/io_m3cf.c, libfat/source/disc_io/io_m3cf.h,
80                libfat/source/disc_io/io_m3sd.c, libfat/source/disc_io/io_m3sd.h,
81                libfat/source/disc_io/io_mpcf.c, libfat/source/disc_io/io_mpcf.h,
82                libfat/source/disc_io/io_njsd.c, libfat/source/disc_io/io_njsd.h,
83                libfat/source/disc_io/io_nmmc.c, libfat/source/disc_io/io_nmmc.h,
84                libfat/source/disc_io/io_sc_common.c,
85                libfat/source/disc_io/io_sc_common.h,
86                libfat/source/disc_io/io_sccf.c, libfat/source/disc_io/io_sccf.h,
87                libfat/source/disc_io/io_scsd.c, libfat/source/disc_io/io_scsd.h,
88                libfat/source/disc_io/io_scsd_s.s,
89                libfat/source/disc_io/io_sd_common.c,
90                libfat/source/disc_io/io_sd_common.h, libnds/Makefile.arm7,
91                libnds/Makefile.arm9, libnds/libnds_license.txt,
92                libnds/basicARM7/source/defaultARM7.c,
93                libnds/include/default_font_bin.h, libnds/include/gbfs.h,
94                libnds/include/nds.h, libnds/include/nds/bios.h,
95                libnds/include/nds/card.h, libnds/include/nds/dma.h,
96                libnds/include/nds/interrupts.h, libnds/include/nds/ipc.h,
97                libnds/include/nds/jtypes.h, libnds/include/nds/memory.h,
98                libnds/include/nds/registers_alt.h, libnds/include/nds/reload.h,
99                libnds/include/nds/system.h, libnds/include/nds/timers.h,
100                libnds/include/nds/arm7/audio.h, libnds/include/nds/arm7/clock.h,
101                libnds/include/nds/arm7/serial.h, libnds/include/nds/arm7/touch.h,
102                libnds/include/nds/arm9/background.h,
103                libnds/include/nds/arm9/boxtest.h, libnds/include/nds/arm9/cache.h,
104                libnds/include/nds/arm9/console.h,
105                libnds/include/nds/arm9/exceptions.h,
106                libnds/include/nds/arm9/image.h, libnds/include/nds/arm9/input.h,
107                libnds/include/nds/arm9/math.h, libnds/include/nds/arm9/ndsmotion.h,
108                libnds/include/nds/arm9/pcx.h, libnds/include/nds/arm9/postest.h,
109                libnds/include/nds/arm9/rumble.h, libnds/include/nds/arm9/sound.h,
110                libnds/include/nds/arm9/sprite.h, libnds/include/nds/arm9/trig_lut.h,
111                libnds/include/nds/arm9/video.h, libnds/include/nds/arm9/videoGL.h,
112                libnds/source/arm7/audio.c, libnds/source/arm7/clock.c,
113                libnds/source/arm7/microphone.c, libnds/source/arm7/spi.c,
114                libnds/source/arm7/touch.c, libnds/source/arm7/userSettings.c,
115                libnds/source/arm9/COS.bin, libnds/source/arm9/COS.s,
116                libnds/source/arm9/SIN.bin, libnds/source/arm9/SIN.s,
117                libnds/source/arm9/TAN.bin, libnds/source/arm9/TAN.s,
118                libnds/source/arm9/boxtest.c, libnds/source/arm9/console.c,
119                libnds/source/arm9/dcache.s, libnds/source/arm9/default_font.bin,
120                libnds/source/arm9/default_font.s,
121                libnds/source/arm9/exceptionHandler.S,
122                libnds/source/arm9/exceptionHandler.s,
123                libnds/source/arm9/exceptions.c, libnds/source/arm9/gurumeditation.c,
124                libnds/source/arm9/icache.s, libnds/source/arm9/image.c,
125                libnds/source/arm9/initSystem.c, libnds/source/arm9/keys.c,
126                libnds/source/arm9/ndsmotion.c, libnds/source/arm9/pcx.c,
127                libnds/source/arm9/rumble.c, libnds/source/arm9/sound.c,
128                libnds/source/arm9/system.c, libnds/source/arm9/touch.c,
129                libnds/source/arm9/video.c, libnds/source/arm9/videoGL.c,
130                libnds/source/common/biosCalls.s, libnds/source/common/card.c,
131                libnds/source/common/gbfs.c,
132                libnds/source/common/interruptDispatcher.s,
133                libnds/source/common/interrupts.c, rtc/rtc.c, sound/sound.c,
134                sound/sound.h, start/start.S, startup/linkcmds, startup/start.c,
135                timer/timer.c, tools/Makefile.am, tools/bin2s, tools/bin2s.c,
136                tools/configure.ac, tools/runtest,
137                tools/ndstool/include/arm7_sha1_homebrew.h,
138                tools/ndstool/include/arm7_sha1_nintendo.h,
139                tools/ndstool/include/banner.h, tools/ndstool/include/bigint.h,
140                tools/ndstool/include/crc.h, tools/ndstool/include/default_icon.h,
141                tools/ndstool/include/encryption.h, tools/ndstool/include/header.h,
142                tools/ndstool/include/hook.h, tools/ndstool/include/little.h,
143                tools/ndstool/include/loadme.h, tools/ndstool/include/logo.h,
144                tools/ndstool/include/ndscreate.h,
145                tools/ndstool/include/ndsextract.h, tools/ndstool/include/ndstool.h,
146                tools/ndstool/include/ndstree.h, tools/ndstool/include/overlay.h,
147                tools/ndstool/include/passme.h, tools/ndstool/include/passme_sram.h,
148                tools/ndstool/include/passme_vhd1.h,
149                tools/ndstool/include/passme_vhd2.h, tools/ndstool/include/raster.h,
150                tools/ndstool/include/sha1.h, tools/ndstool/include/types.h,
151                tools/ndstool/source/arm7_sha1_homebrew.c,
152                tools/ndstool/source/arm7_sha1_nintendo.c,
153                tools/ndstool/source/banner.cpp, tools/ndstool/source/bigint.cpp,
154                tools/ndstool/source/compile_date.c, tools/ndstool/source/crc.cpp,
155                tools/ndstool/source/default_icon.c,
156                tools/ndstool/source/encryption.cpp, tools/ndstool/source/header.cpp,
157                tools/ndstool/source/hook.cpp, tools/ndstool/source/loadme.c,
158                tools/ndstool/source/logo.cpp, tools/ndstool/source/ndscodes.cpp,
159                tools/ndstool/source/ndscreate.cpp,
160                tools/ndstool/source/ndsextract.cpp,
161                tools/ndstool/source/ndstool.cpp, tools/ndstool/source/ndstree.cpp,
162                tools/ndstool/source/passme.cpp, tools/ndstool/source/passme_sram.c,
163                tools/ndstool/source/raster.cpp, tools/ndstool/source/sha1.cpp,
164                touchscreen/README.reco, touchscreen/parser.c, touchscreen/reco.c,
165                touchscreen/reco.h, touchscreen/touchscreen.c,
166                touchscreen/touchscreen.h, wifi/compat.c, wifi/compat.h, wifi/wifi.c:
167                New files.
168
169        Revision 1.5  2006/05/05 05:32:06  wntrmute
170        consistency formatting
171
172        Revision 1.4  2006/02/25 02:31:12  wntrmute
173        tabs, not spaces
174
175        Revision 1.3  2005/09/20 04:59:44  wntrmute
176        *** empty log message ***
177
178        Revision 1.2  2005/08/23 17:06:10  wntrmute
179        converted all endings to unix
180
181        Revision 1.1  2005/08/04 17:55:44  wntrmute
182        added gbfs
183
184
185---------------------------------------------------------------------------------*/
186
187
188/*---------------------------------------------------------------------------------
189        This code assumes a LITTLE ENDIAN target.  It'll need a boatload
190        of itohs and itohl calls if converted to run on Sega Genesis.  It
191        also assumes that the target uses 16-bit short and 32-bit longs.
192---------------------------------------------------------------------------------*/
193
194#include <stdlib.h>
195#include <string.h>
196#include <nds/jtypes.h>
197#include "gbfs.h"
198
199/*---------------------------------------------------------------------------------
200        change this to the end of your ROM,
201        or to 0x02040000 for gba multiboot
202        or to 0x24000000 for nds main ram
203---------------------------------------------------------------------------------*/
204
205static const u32 * GBFS_1ST_SEARCH_LIMIT = (const u32 *)0x02400000;
206static const u32 * GBFS_2ND_SEARCH_START = (const u32 *)0x08000000;
207static const u32 * GBFS_2ND_SEARCH_LIMIT = (const u32 *)0x0a000000;
208/*---------------------------------------------------------------------------------
209        a power of two, less than or equal to the argument passed to
210        padbin.  Increasing the stride makes find_first_gbfs_file()
211        faster at the cost of a slightly larger binary.
212---------------------------------------------------------------------------------*/
213static int GBFS_STRIDE = 256;
214
215/*---------------------------------------------------------------------------------
216        Set the search limits and stride for searching
217---------------------------------------------------------------------------------*/
218void gbfs_search_range(
219        u32 gbfs_1st_limit,
220        u32 gbfs_2nd_start, u32 gbfs_2nd_limit,
221        u32 gbfs_stride
222) {
223//---------------------------------------------------------------------------------
224        if ( NULL != gbfs_1st_limit ) GBFS_1ST_SEARCH_LIMIT = (u32 *)gbfs_1st_limit;
225        if ( NULL != gbfs_2nd_limit ) GBFS_2ND_SEARCH_LIMIT = (u32 *)gbfs_2nd_limit;
226        if ( NULL != gbfs_2nd_start ) GBFS_2ND_SEARCH_START = (u32 *)gbfs_2nd_start;
227        if ( NULL != gbfs_stride ) GBFS_STRIDE = gbfs_stride;
228
229}
230
231//---------------------------------------------------------------------------------
232const GBFS_FILE * find_first_gbfs_file(const void *start) {
233//---------------------------------------------------------------------------------
234        /* align the pointer */
235        const u32 *here = (const u32 *)
236                                                                                        ((unsigned long)start & (-GBFS_STRIDE));
237        const char rest_of_magic[] = "ightGBFS\r\n\x1a\n";
238
239        /* Linear-search first in multiboot space. */
240        while(here < GBFS_1ST_SEARCH_LIMIT)
241        {
242                /*      We have to keep the magic code in two pieces; otherwise,
243                        this function may find itself and think it's a GBFS file.
244                        This obviously won't work if your compiler stores this
245                        numeric literal just before the literal string, but Devkit
246                        Advance R4 and R5 seem to keep numeric constant pools
247                        separate enough from string pools for this to work.
248                */
249                if(*here == 0x456e6950) { /* ASCII code for little endian "PinE" */
250                        /*      We've matched the first four bytes.
251                                If the rest of the magic matches, then we've found a file. */
252                        if(!memcmp(here + 1, rest_of_magic, 12))
253                                return (const GBFS_FILE *)here;
254                }
255                here += GBFS_STRIDE / sizeof(here);
256 }
257
258        /* Now search in ROM space. */
259        if(here < GBFS_2ND_SEARCH_START)
260                here = GBFS_2ND_SEARCH_START;
261        while(here < GBFS_2ND_SEARCH_LIMIT) {
262                /* Search loop same as above. */
263                if(*here == 0x456e6950) {  /* ASCII code for little endian "PinE" */
264                        if(!memcmp(here + 1, rest_of_magic, 12))
265                                return (const GBFS_FILE *)here;
266                }
267                here += GBFS_STRIDE / sizeof(*here);
268        }
269        return 0;
270}
271
272
273//---------------------------------------------------------------------------------
274const void *skip_gbfs_file(const GBFS_FILE *file) {
275//---------------------------------------------------------------------------------
276        return ((char *)file + file->total_len);
277}
278
279
280//---------------------------------------------------------------------------------
281static int namecmp(const void *a, const void *b) {
282//---------------------------------------------------------------------------------
283        return memcmp(a, b, 24);
284}
285
286
287//---------------------------------------------------------------------------------
288const void *gbfs_get_obj(       const GBFS_FILE *file,
289                                                        const char *name,
290                                                        u32 *len) {
291//---------------------------------------------------------------------------------
292        char key[24] = {0};
293
294        const GBFS_ENTRY *dirbase = (const GBFS_ENTRY *)((const char *)file + file->dir_off);
295        size_t n_entries = file->dir_nmemb;
296        const GBFS_ENTRY *here;
297
298        strncpy(key, name, 24);
299
300        here = bsearch( key, dirbase,
301                                        n_entries, sizeof(GBFS_ENTRY),
302                                         namecmp);
303        if(!here)
304                return NULL;
305
306        if(len)
307                *len = here->len;
308        return (char *)file + here->data_offset;
309}
310
311
312//---------------------------------------------------------------------------------
313const void *gbfs_get_nth_obj(   const GBFS_FILE *file,
314                                                                size_t n,
315                                                                char *name,
316                                                                u32 *len)
317//---------------------------------------------------------------------------------
318{
319        const GBFS_ENTRY *dirbase = (const GBFS_ENTRY *)((const char *)file + file->dir_off);
320        size_t n_entries = file->dir_nmemb;
321        const GBFS_ENTRY *here = dirbase + n;
322
323        if(n >= n_entries)
324                return NULL;
325
326        if(name) {
327                strncpy(name, here->name, 24);
328                name[24] = 0;
329        }
330
331        if(len)
332                *len = here->len;
333
334        return (char *)file + here->data_offset;
335}
336
337
338//---------------------------------------------------------------------------------
339void *gbfs_copy_obj(    void *dst,
340                                                const GBFS_FILE *file,
341                                                const char *name) {
342//---------------------------------------------------------------------------------
343        u32 len;
344        const void *src = gbfs_get_obj(file, name, &len);
345
346        if(!src)
347                return NULL;
348
349        memcpy(dst, src, len);
350        return dst;
351}
352
353
354//---------------------------------------------------------------------------------
355size_t gbfs_count_objs(const GBFS_FILE *file) {
356//---------------------------------------------------------------------------------
357        return file ? file->dir_nmemb : 0;
358}
359
Note: See TracBrowser for help on using the repository browser.