forked from TnA-Plastic/FreeMcBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osdname.c
214 lines (173 loc) · 5.78 KB
/
osdname.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
_____ ___ ____
____| | ____| PS2 Open Source Project
| ___| |____
---------------------------------------------------------------------------
Copyright (C) 2008 - Neme & jimmikaelkael (www.psx-scene.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the Free McBoot License.
This program and any related documentation is provided "as is"
WITHOUT ANY WARRANTIES, either express or implied, including, but not
limited to, implied warranties of fitness for a particular purpose. The
entire risk arising out of use or performance of the software remains
with you.
In no event shall the author be liable for any damages whatsoever
(including, without limitation, damages to your hardware or equipment,
environmental damage, loss of health, or any kind of pecuniary loss)
arising out of the use of or inability to use this software or
documentation, even if the author has been advised of the possibility of
such damages.
You should have received a copy of the Free McBoot License along with
this program; if not, please report at psx-scene :
http://psx-scene.com/forums/freevast/
---------------------------------------------------------------------------
osdname.c
Unpacks rom0:osdsys and tries to find osdxxx strings.
*/
#include <tamtypes.h>
#include <kernel.h>
#include <stdio.h>
#include <string.h>
//#include <sysclib.h>
u8 *Get_OSD_Name(void);
typedef struct {
int size;
u8 *addr;
} FileInfo_t;
u8 *find_osd_string(u8 *buf, u32 bufsize);
u8 *find_osdpack_start(u8 *osdsys_addr);
int load_file(const char *path, FileInfo_t *finf);
u32 OSDUnpack(u8 *pack_start, u8 *unpack_start);
u8 *Get_OSD_Name(void)
{
FileInfo_t osdsys;
u8 *pack_start, *unpack_start = NULL, *ret = NULL;
u32 unpacked_size;
if (!load_file("rom0:OSDSYS", &osdsys))
return NULL;
if ((pack_start = find_osdpack_start(osdsys.addr))) {
unpacked_size = *(u32*)pack_start;
printf("\tUnpacked size: %d\n", unpacked_size);
if ((unpack_start = malloc(unpacked_size * 2))) {
printf("\tUnpacking...\n");
OSDUnpack(pack_start, unpack_start);
printf("\tChecking unpacked OSDSYS...\n");
ret = find_osd_string(unpack_start, unpacked_size);
}
}
if (!ret) {
printf("\tChecking raw OSDSYS...\n");
ret = find_osd_string(osdsys.addr, osdsys.size);
}
if (unpack_start)
free(unpack_start);
free(osdsys.addr);
return ret;
}
/**********************************************************************************/
u8 *find_osd_string(u8 *buf, u32 bufsize)
{
u32 i, j;
const u8 *s, *p;
static u8 result[16];
static const u8 *osdstrings[] = { // add more strings if needed
"osdmain.elf",
"osdmain.sys",
"osdsys.elf",
"osd###.elf",
"osd###.sys",
"\n"
};
for (i = 0; *osdstrings[i] != '\n'; i++) {
for (j = 0; j < bufsize; j++) {
s = osdstrings[i];
for (p = buf + j; *s && (*s == *p || *s == '#'); s++, p++);
if (!*s) {
printf("\tString found: %s\n", osdstrings[i]);
strncpy(result, buf + j, strlen(osdstrings[i]));
return (result); // found!
}
}
}
return NULL;
}
/**********************************************************************************/
u8 *find_osdpack_start(u8 *osdsys_addr)
{
int i;
u16 section_hdr_num = *(u16*)&osdsys_addr[48];
u32 section_hdr_offset = *(u32*)&osdsys_addr[32];
u32 attrib;
// printf("\tSection header num: %d\n", section_hdr_num);
// printf("\tSection header offs: %08x\n", section_hdr_offset);
for (i = 0; i < section_hdr_num; i++) {
attrib = *(u32*)&osdsys_addr[section_hdr_offset + 8];
if (attrib == 3)
return (osdsys_addr + *(u32*)&osdsys_addr[section_hdr_offset + 16]);
section_hdr_offset += 40;
}
return NULL;
}
/**********************************************************************************/
int load_file(const char *path, FileInfo_t *finf)
{
int fd;
if ((fd = fioOpen(path, O_RDONLY)) < 0) {
printf("\tERROR: Failed to open file '%s'\n", path);
return 0;
}
finf->size = fioLseek(fd, 0, SEEK_END);
printf("\tFile '%s' size: %d\n", path, finf->size);
fioLseek(fd, 0, SEEK_SET);
// alloc x2 for safety
if (!(finf->addr = malloc(finf->size * 2))) {
printf("\tERROR: Failed to malloc %d bytes for '%s'\n", finf->size, path);
fioClose(fd);
return 0;
}
fioRead(fd, finf->addr, finf->size);
fioClose(fd);
return 1;
}
/**********************************************************************************/
u32 OSDUnpack(u8 *pack_start, u8 *unpack_start)
{
int hash = 0;
u32 unpacked_size, shift_amount = 0, offset_mask = 0, count, offset, numbytes;
u8 *pack_ptr, *src, *dest;
u8 c1, c2;
unpacked_size = *(u32*)pack_start;
pack_ptr = pack_start + 4;
dest = unpack_start;
count = 0;
for (;;) {
if (count == 0) {
count = 30;
hash = *(pack_ptr++) << 8;
hash = (hash | *(pack_ptr++)) << 8;
hash = (hash | *(pack_ptr++)) << 8;
hash = hash | *(pack_ptr++);
offset_mask = 0x3fff >> (hash & 3);
shift_amount = 14 - (hash & 3);
}
c1 = *(pack_ptr++);
if (hash >= 0) // sign bit set?
*(dest++) = c1;
else {
c2 = *(pack_ptr++);
offset = (((c1 << 8) | c2) & offset_mask) + 1;
numbytes = (((c1 << 8) | c2) >> shift_amount) + 2;
src = dest - offset;
*(dest++) = *(src++);
while (numbytes != 0) {
*(dest++) = *(src++);
numbytes--;
}
}
count--;
if (dest - unpack_start >= unpacked_size)
break;
hash <<= 1;
}
return unpacked_size;
}