1 #include "include/usbld.h"
2 #include "include/lang.h"
3 #include "include/util.h"
4 #include "include/iosupport.h"
5 #include "include/system.h"
6 #include "include/supportbase.h"
7 #include "include/ioman.h"
9 /// internal linked list used to populate the list from directory listing
11 base_game_info_t gameinfo
;
12 struct game_list_t
*next
;
15 int sbIsSameSize(const char* prefix
, int prevSize
) {
18 snprintf(path
, 255, "%sul.cfg", prefix
);
20 int fd
= openFile(path
, O_RDONLY
);
22 size
= getFileSize(fd
);
26 return size
== prevSize
;
29 static int isValidIsoName(char *name
)
31 // SCUS_XXX.XX.ABCDEFGHIJKLMNOP.iso
33 // Minimum is 17 char, GameID (11) + "." (1) + filename (1 min.) + ".iso" (4)
34 int size
= strlen(name
);
35 if ((size
>= 17) && (name
[4] == '_') && (name
[8] == '.') && (name
[11] == '.') && (stricmp(&name
[size
- 4], ".iso") == 0)) {
37 if (size
<= ISO_GAME_NAME_MAX
)
44 static int scanForISO(char* path
, char type
, struct game_list_t
** glist
) {
45 int fd
, size
, count
= 0;
48 if ((fd
= fioDopen(path
)) > 0) {
49 while (fioDread(fd
, &record
) > 0) {
50 if ((size
= isValidIsoName(record
.name
)) > 0) {
51 struct game_list_t
*next
= (struct game_list_t
*)malloc(sizeof(struct game_list_t
));
56 base_game_info_t
*game
= &(*glist
)->gameinfo
;
58 strncpy(game
->name
, &record
.name
[GAME_STARTUP_MAX
], size
);
59 game
->name
[size
] = '\0';
60 strncpy(game
->startup
, record
.name
, GAME_STARTUP_MAX
- 1);
61 game
->startup
[GAME_STARTUP_MAX
- 1] = '\0';
65 game
->sizeMB
= (record
.stat
.size
>> 20) | (record
.stat
.hisize
<< 12);
76 void sbReadList(base_game_info_t
**list
, const char* prefix
, int *fsize
, int* gamecount
) {
86 // temporary storage for the game names
87 struct game_list_t
*dlist_head
= NULL
;
89 // count iso games in "cd" directory
90 snprintf(path
, 255, "%sCD", prefix
);
91 count
+= scanForISO(path
, 0x12, &dlist_head
);
93 // count iso games in "dvd" directory
94 snprintf(path
, 255, "%sDVD", prefix
);
95 count
+= scanForISO(path
, 0x14, &dlist_head
);
98 // count and process games in ul.cfg
99 snprintf(path
, 255, "%sul.cfg", prefix
);
100 fd
= openFile(path
, O_RDONLY
);
104 size
= getFileSize(fd
);
106 count
+= size
/ 0x040;
109 *list
= (base_game_info_t
*)malloc(sizeof(base_game_info_t
) * count
);
112 fioRead(fd
, buffer
, 0x40);
114 base_game_info_t
*g
= &(*list
)[id
++];
116 // to ensure no leaks happen, we copy manually and pad the strings
117 memcpy(g
->name
, buffer
, UL_GAME_NAME_MAX
);
118 g
->name
[UL_GAME_NAME_MAX
] = '\0';
119 memcpy(g
->startup
, &buffer
[UL_GAME_NAME_MAX
+ 3], GAME_STARTUP_MAX
);
120 g
->startup
[GAME_STARTUP_MAX
] = '\0';
121 memcpy(&g
->parts
, &buffer
[47], 1);
122 memcpy(&g
->media
, &buffer
[48], 1);
131 *list
= (base_game_info_t
*)malloc(sizeof(base_game_info_t
) * count
);
133 // copy the dlist into the list
134 while ((id
< count
) && dlist_head
) {
135 // copy one game, advance
136 struct game_list_t
*cur
= dlist_head
;
137 dlist_head
= dlist_head
->next
;
139 memcpy(&(*list
)[id
++], &cur
->gameinfo
, sizeof(base_game_info_t
));
146 int sbPrepare(base_game_info_t
* game
, int mode
, char* isoname
, int size_cdvdman
, void** cdvdman_irx
, int* patchindex
) {
149 unsigned int compatmask
= configGetCompatibility(game
->startup
, mode
, NULL
);
152 configGetDiscIDBinary(game
->startup
, gameid
);
155 strcpy(isoname
, game
->startup
);
157 char gamename
[UL_GAME_NAME_MAX
+ 1];
158 memset(gamename
, 0, UL_GAME_NAME_MAX
+ 1);
159 strncpy(gamename
, game
->name
, UL_GAME_NAME_MAX
);
160 sprintf(isoname
,"ul.%08X.%s", USBA_crc32(gamename
), game
->startup
);
163 for (i
= 0; i
< size_cdvdman
; i
++) {
164 if (!strcmp((const char*)((u32
)cdvdman_irx
+ i
),"###### GAMESETTINGS ######")) {
169 memcpy((void*)((u32
)cdvdman_irx
+ i
), isoname
, strlen(isoname
) + 1);
170 memcpy((void*)((u32
)cdvdman_irx
+ i
+ 33), &game
->parts
, 1);
171 memcpy((void*)((u32
)cdvdman_irx
+ i
+ 34), &game
->media
, 1);
172 if (compatmask
& COMPAT_MODE_2
) {
173 u32 alt_read_mode
= 1;
174 memcpy((void*)((u32
)cdvdman_irx
+ i
+ 35), &alt_read_mode
, 1);
176 if (compatmask
& COMPAT_MODE_5
) {
178 memcpy((void*)((u32
)cdvdman_irx
+ i
+ 36), &no_dvddl
, 4);
180 if (compatmask
& COMPAT_MODE_4
) {
182 memcpy((void*)((u32
)cdvdman_irx
+ i
+ 40), &no_pss
, 4);
187 for (i
= 0; i
< size_cdvdman
; i
++) {
188 if (!strcmp((const char*)((u32
)cdvdman_irx
+ i
),"B00BS")) {
193 memcpy((void*)((u32
)cdvdman_irx
+ i
), &gameid
, 5);
197 int size_cdvdfsv_irx
;
199 sysGetCDVDFSV(&cdvdfsv_irx
, &size_cdvdfsv_irx
);
200 u32
*p
= (u32
*)cdvdfsv_irx
;
201 for (i
= 0; i
< (size_cdvdfsv_irx
>> 2); i
++) {
202 if (*p
== 0xC0DEC0DE) {
203 if (compatmask
& COMPAT_MODE_7
)
215 static void sbRebuildULCfg(base_game_info_t
**list
, const char* prefix
, int gamecount
, int excludeID
) {
217 snprintf(path
, 255, "%sul.cfg", prefix
);
219 file_buffer_t
* fileBuffer
= openFileBuffer(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0, 4096);
223 base_game_info_t
* game
;
225 memset(buffer
, 0, 0x40);
226 buffer
[32] = 0x75; // u
227 buffer
[33] = 0x6C; // l
228 buffer
[34] = 0x2E; // .
229 buffer
[53] = 0x08; // just to be compatible with original ul.cfg
231 for (i
= 0; i
< gamecount
; i
++) {
234 if (!game
->isISO
&& (i
!= excludeID
)) {
235 memset(buffer
, 0, UL_GAME_NAME_MAX
);
236 memset(&buffer
[UL_GAME_NAME_MAX
+ 3], 0, GAME_STARTUP_MAX
);
238 memcpy(buffer
, game
->name
, UL_GAME_NAME_MAX
);
239 memcpy(&buffer
[UL_GAME_NAME_MAX
+ 3], game
->startup
, GAME_STARTUP_MAX
);
240 buffer
[47] = game
->parts
;
241 buffer
[48] = game
->media
;
243 writeFileBuffer(fileBuffer
, buffer
, 0x40);
247 closeFileBuffer(fileBuffer
);
251 void sbDelete(base_game_info_t
**list
, const char* prefix
, const char* sep
, int gamecount
, int id
) {
253 base_game_info_t
* game
= &(*list
)[id
];
256 if (game
->media
== 0x12)
257 snprintf(path
, 255, "%sCD%s%s.%s.iso", prefix
, sep
, game
->startup
, game
->name
);
259 snprintf(path
, 255, "%sDVD%s%s.%s.iso", prefix
, sep
, game
->startup
, game
->name
);
262 char *pathStr
= "%sul.%08X.%s.%02x";
263 unsigned int crc
= USBA_crc32(game
->name
);
266 snprintf(path
, 255, pathStr
, prefix
, crc
, game
->startup
, i
++);
268 } while(i
< game
->parts
);
270 sbRebuildULCfg(list
, prefix
, gamecount
, id
);
274 void sbRename(base_game_info_t
**list
, const char* prefix
, const char* sep
, int gamecount
, int id
, char* newname
) {
275 char oldpath
[255], newpath
[255];
276 base_game_info_t
* game
= &(*list
)[id
];
279 if (game
->media
== 0x12) {
280 snprintf(oldpath
, 255, "%sCD%s%s.%s.iso", prefix
, sep
, game
->startup
, game
->name
);
281 snprintf(newpath
, 255, "%sCD%s%s.%s.iso", prefix
, sep
, game
->startup
, newname
);
283 snprintf(oldpath
, 255, "%sDVD%s%s.%s.iso", prefix
, sep
, game
->startup
, game
->name
);
284 snprintf(newpath
, 255, "%sDVD%s%s.%s.iso", prefix
, sep
, game
->startup
, newname
);
286 fileXioRename(oldpath
, newpath
);
288 memset(game
->name
, 0, UL_GAME_NAME_MAX
);
289 memcpy(game
->name
, newname
, UL_GAME_NAME_MAX
);
291 char *pathStr
= "%sul.%08X.%s.%02x";
292 unsigned int oldcrc
= USBA_crc32(game
->name
);
293 unsigned int newcrc
= USBA_crc32(newname
);
296 snprintf(oldpath
, 255, pathStr
, prefix
, oldcrc
, game
->startup
, i
);
297 snprintf(newpath
, 255, pathStr
, prefix
, newcrc
, game
->startup
, i
++);
298 fileXioRename(oldpath
, newpath
);
299 } while(i
< game
->parts
);
301 sbRebuildULCfg(list
, prefix
, gamecount
, -1);
305 config_set_t
* sbPopulateConfig(base_game_info_t
* game
, const char* prefix
, const char* sep
) {
307 snprintf(path
, 255, "%sCFG%s%s.cfg", prefix
, sep
, game
->startup
);
308 config_set_t
* config
= configAlloc(0, NULL
, path
);
311 configSetStr(config
, "#Name", game
->name
);
312 if (game
->sizeMB
!= -1)
313 configSetInt(config
, "#Size", game
->sizeMB
);
315 configSetStr(config
, "#Format", "ISO");
317 configSetStr(config
, "#Format", "UL");
319 if (game
->media
== 0x12)
320 configSetStr(config
, "#Media", "CD");
322 configSetStr(config
, "#Media", "DVD");
324 configSetStr(config
, "#Startup", game
->startup
);