-
Notifications
You must be signed in to change notification settings - Fork 0
/
libgbfs.c
169 lines (125 loc) · 4.44 KB
/
libgbfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* Modifed by Justin Armstrong Aug 2002 <[email protected]>
*
* changes from Damian Yerrick's original 20020404 release:
* - max pathname is now 128 chars
* - renamed GBFS_FILE to GBFS_ARCHIVE
* see readme for more details
*/
/* libgbfs.c
access object in a GBFS file
Copyright 2002 Damian Yerrick
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
/* This code assumes a LITTLE ENDIAN target. It'll need a boatload
of itohs and itohl calls if converted to run on Sega Genesis. It
also assumes that the target uses 16-bit short and 32-bit longs.
*/
#include "util.h"
#include "kernel.h"
#include "semaphore.h"
#include "gbfs.h"
static bool s_inited = FALSE;
static struct semaphore_t s_fs_sem;
/* change this to the end of your ROM, or to 0x02040000 for multiboot */
#define GBFS_SEARCH_LIMIT ((const u32 *)0x08800000)
static void
_init_gbfs() {
s_inited = TRUE;
semaphore_create(&s_fs_sem, 1);
}
const GBFS_ARCHIVE *find_first_gbfs_archive(const void *start)
{
/* align the pointer */
const u32 *here = (const u32 *)
((unsigned long)start & 0xfffffffc);
const char rest_of_magic[] = "ightGBFS\r\n\032\n";
if (!s_inited)
_init_gbfs();
semaphore_wait(&s_fs_sem);
/* while we haven't yet reached the end of the ROM space */
while(here < GBFS_SEARCH_LIMIT)
{
/* We have to keep the magic code in two pieces; otherwise,
this function will find itself and think it's a GBFS file.
This obviously won't work if your compiler stores this
numeric literal just before the literal string, but Devkit
Advance seems to keep numeric constant pools separate enough
from string pools for this to work.
*/
if(*here == 0x456e6950) /* ASCII code for "PinE" */
{
/* we're already after here;
if the rest of the magic matches, then we're through */
if(!memcmp(here + 1, rest_of_magic, 12)) {
semaphore_signal(&s_fs_sem);
return (const GBFS_ARCHIVE *)here;
}
}
++here;
}
semaphore_signal(&s_fs_sem);
return 0;
}
const void *skip_gbfs_archive(const GBFS_ARCHIVE *file)
{
return ((char *)file + file->total_len);
}
static int namecmp(const void *a, const void *b)
{
return strncmp(a, b, MAX_GBFS_PATH);
}
const void *gbfs_get_obj(const GBFS_ARCHIVE *file,
const char *name,
u32 *len)
{
char key[MAX_GBFS_PATH] = {0};
void* data;
semaphore_wait(&s_fs_sem);
GBFS_ENTRY *dirbase = (GBFS_ENTRY *)((char *)file + file->dir_off);
size_t n_entries = file->dir_nmemb;
GBFS_ENTRY *here;
//printf("gbfs_get_obj: %s\r\n",name);
strncpy(key, name, MAX_GBFS_PATH);
here = bsearch(key, dirbase,
n_entries, sizeof(GBFS_ENTRY),
namecmp);
if(!here) {
semaphore_signal(&s_fs_sem);
return NULL;
}
if(len)
*len = here->len;
data = (char *)file + sizeof(GBFS_ARCHIVE) + (sizeof(GBFS_ENTRY)*n_entries) + here->data_offset;
semaphore_signal(&s_fs_sem);
return data;
}
void *gbfs_copy_obj(void *dst,
const GBFS_ARCHIVE *file,
const char *name)
{
u32 len;
const void *src;
semaphore_wait(&s_fs_sem);
src = gbfs_get_obj(file, name, &len);
if(!src)
return NULL;
memcpy(dst, src, len);
semaphore_signal(&s_fs_sem);
return dst;
}