* don't display a share connection error when listing share
[open-ps2-loader.git] / src / supportbase.c
blob248420d8b1f4a333a47485a17a811c1c0508af04
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
10 struct game_list_t {
11 base_game_info_t gameinfo;
12 struct game_list_t *next;
15 int sbIsSameSize(const char* prefix, int prevSize) {
16 int size = -1;
17 char path[255];
18 snprintf(path, 255, "%sul.cfg", prefix);
20 int fd = openFile(path, O_RDONLY);
21 if (fd >= 0) {
22 size = getFileSize(fd);
23 fioClose(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)) {
36 size -= 16;
37 if (size <= ISO_GAME_NAME_MAX)
38 return size;
41 return 0;
44 static int scanForISO(char* path, char type, struct game_list_t** glist) {
45 int fd, size, count = 0;
46 fio_dirent_t record;
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));
53 next->next = *glist;
54 *glist = next;
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';
62 game->parts = 0x01;
63 game->media = type;
64 game->isISO = 1;
65 game->sizeMB = (record.stat.size >> 20) | (record.stat.hisize << 12);
67 count++;
70 fioDclose(fd);
73 return count;
76 void sbReadList(base_game_info_t **list, const char* prefix, int *fsize, int* gamecount) {
77 int fd, size, id = 0;
78 size_t count = 0;
79 char path[255];
81 free(*list);
82 *list = NULL;
83 *fsize = -1;
84 *gamecount = 0;
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);
101 if(fd >= 0) {
102 char buffer[0x040];
104 size = getFileSize(fd);
105 *fsize = size;
106 count += size / 0x040;
108 if (count > 0) {
109 *list = (base_game_info_t*)malloc(sizeof(base_game_info_t) * count);
111 while (size > 0) {
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);
123 g->isISO = 0;
124 g->sizeMB = -1;
125 size -= 0x40;
128 fioClose(fd);
130 else if (count > 0)
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));
140 free(cur);
143 *gamecount = count;
146 int sbPrepare(base_game_info_t* game, config_set_t* configSet, char* isoname, int size_cdvdman, void** cdvdman_irx, int* patchindex) {
147 int i;
149 unsigned int compatmask = 0;
150 configGetInt(configSet, CONFIG_ITEM_COMPAT, &compatmask);
152 char gameid[5];
153 configGetDiscIDBinary(configSet, gameid);
155 if (game->isISO)
156 strcpy(isoname, game->startup);
157 else {
158 char gamename[UL_GAME_NAME_MAX + 1];
159 memset(gamename, 0, UL_GAME_NAME_MAX + 1);
160 strncpy(gamename, game->name, UL_GAME_NAME_MAX);
161 sprintf(isoname,"ul.%08X.%s", USBA_crc32(gamename), game->startup);
164 for (i = 0; i < size_cdvdman; i++) {
165 if (!strcmp((const char*)((u32)cdvdman_irx + i),"###### GAMESETTINGS ######")) {
166 break;
170 memcpy((void*)((u32)cdvdman_irx + i), isoname, strlen(isoname) + 1);
171 memcpy((void*)((u32)cdvdman_irx + i + 33), &game->parts, 1);
172 memcpy((void*)((u32)cdvdman_irx + i + 34), &game->media, 1);
173 if (compatmask & COMPAT_MODE_2) {
174 u32 alt_read_mode = 1;
175 memcpy((void*)((u32)cdvdman_irx + i + 35), &alt_read_mode, 1);
177 if (compatmask & COMPAT_MODE_5) {
178 u32 no_dvddl = 1;
179 memcpy((void*)((u32)cdvdman_irx + i + 36), &no_dvddl, 4);
181 if (compatmask & COMPAT_MODE_4) {
182 u32 no_pss = 1;
183 memcpy((void*)((u32)cdvdman_irx + i + 40), &no_pss, 4);
186 *patchindex = i;
188 for (i = 0; i < size_cdvdman; i++) {
189 if (!strcmp((const char*)((u32)cdvdman_irx + i),"B00BS")) {
190 break;
193 // game id
194 memcpy((void*)((u32)cdvdman_irx + i), &gameid, 5);
196 // patches cdvdfsv
197 void *cdvdfsv_irx;
198 int size_cdvdfsv_irx;
200 sysGetCDVDFSV(&cdvdfsv_irx, &size_cdvdfsv_irx);
201 u32 *p = (u32 *)cdvdfsv_irx;
202 for (i = 0; i < (size_cdvdfsv_irx >> 2); i++) {
203 if (*p == 0xC0DEC0DE) {
204 if (compatmask & COMPAT_MODE_7)
205 *p = 1;
206 else
207 *p = 0;
208 break;
210 p++;
213 return compatmask;
216 static void sbRebuildULCfg(base_game_info_t **list, const char* prefix, int gamecount, int excludeID) {
217 char path[255];
218 snprintf(path, 255, "%sul.cfg", prefix);
220 file_buffer_t* fileBuffer = openFileBuffer(path, O_WRONLY | O_CREAT | O_TRUNC, 0, 4096);
221 if (fileBuffer) {
222 int i;
223 char buffer[0x40];
224 base_game_info_t* game;
226 memset(buffer, 0, 0x40);
227 buffer[32] = 0x75; // u
228 buffer[33] = 0x6C; // l
229 buffer[34] = 0x2E; // .
230 buffer[53] = 0x08; // just to be compatible with original ul.cfg
232 for (i = 0; i < gamecount; i++) {
233 game = &(*list)[i];
235 if (!game->isISO && (i != excludeID)) {
236 memset(buffer, 0, UL_GAME_NAME_MAX);
237 memset(&buffer[UL_GAME_NAME_MAX + 3], 0, GAME_STARTUP_MAX);
239 memcpy(buffer, game->name, UL_GAME_NAME_MAX);
240 memcpy(&buffer[UL_GAME_NAME_MAX + 3], game->startup, GAME_STARTUP_MAX);
241 buffer[47] = game->parts;
242 buffer[48] = game->media;
244 writeFileBuffer(fileBuffer, buffer, 0x40);
248 closeFileBuffer(fileBuffer);
252 void sbDelete(base_game_info_t **list, const char* prefix, const char* sep, int gamecount, int id) {
253 char path[255];
254 base_game_info_t* game = &(*list)[id];
256 if (game->isISO) {
257 if (game->media == 0x12)
258 snprintf(path, 255, "%sCD%s%s.%s.iso", prefix, sep, game->startup, game->name);
259 else
260 snprintf(path, 255, "%sDVD%s%s.%s.iso", prefix, sep, game->startup, game->name);
261 fileXioRemove(path);
262 } else {
263 char *pathStr = "%sul.%08X.%s.%02x";
264 unsigned int crc = USBA_crc32(game->name);
265 int i = 0;
266 do {
267 snprintf(path, 255, pathStr, prefix, crc, game->startup, i++);
268 fileXioRemove(path);
269 } while(i < game->parts);
271 sbRebuildULCfg(list, prefix, gamecount, id);
275 void sbRename(base_game_info_t **list, const char* prefix, const char* sep, int gamecount, int id, char* newname) {
276 char oldpath[255], newpath[255];
277 base_game_info_t* game = &(*list)[id];
279 if (game->isISO) {
280 if (game->media == 0x12) {
281 snprintf(oldpath, 255, "%sCD%s%s.%s.iso", prefix, sep, game->startup, game->name);
282 snprintf(newpath, 255, "%sCD%s%s.%s.iso", prefix, sep, game->startup, newname);
283 } else {
284 snprintf(oldpath, 255, "%sDVD%s%s.%s.iso", prefix, sep, game->startup, game->name);
285 snprintf(newpath, 255, "%sDVD%s%s.%s.iso", prefix, sep, game->startup, newname);
287 fileXioRename(oldpath, newpath);
288 } else {
289 memset(game->name, 0, UL_GAME_NAME_MAX);
290 memcpy(game->name, newname, UL_GAME_NAME_MAX);
292 char *pathStr = "%sul.%08X.%s.%02x";
293 unsigned int oldcrc = USBA_crc32(game->name);
294 unsigned int newcrc = USBA_crc32(newname);
295 int i = 0;
296 do {
297 snprintf(oldpath, 255, pathStr, prefix, oldcrc, game->startup, i);
298 snprintf(newpath, 255, pathStr, prefix, newcrc, game->startup, i++);
299 fileXioRename(oldpath, newpath);
300 } while(i < game->parts);
302 sbRebuildULCfg(list, prefix, gamecount, -1);
306 config_set_t* sbPopulateConfig(base_game_info_t* game, const char* prefix, const char* sep) {
307 char path[255];
308 snprintf(path, 255, "%sCFG%s%s.cfg", prefix, sep, game->startup);
309 config_set_t* config = configAlloc(0, NULL, path);
310 configRead(config);
312 configSetStr(config, CONFIG_ITEM_NAME, game->name);
313 if (game->sizeMB != -1)
314 configSetInt(config, CONFIG_ITEM_SIZE, game->sizeMB);
315 if (game->isISO)
316 configSetStr(config, CONFIG_ITEM_FORMAT, "ISO");
317 else
318 configSetStr(config, CONFIG_ITEM_FORMAT, "UL");
320 if (game->media == 0x12)
321 configSetStr(config, CONFIG_ITEM_MEDIA, "CD");
322 else
323 configSetStr(config, CONFIG_ITEM_MEDIA, "DVD");
325 configSetStr(config, CONFIG_ITEM_STARTUP, game->startup);
327 return config;