diff options
| author | FIX94 <fix94.1@gmail.com> | 2016-04-10 14:44:53 +0200 | 
|---|---|---|
| committer | FIX94 <fix94.1@gmail.com> | 2016-04-10 14:44:53 +0200 | 
| commit | 42379a1cc476d481f5aa7a7b6e69f271f079fda3 (patch) | |
| tree | 7109daa4a6242a8852ccd3ec364dc5ad48b0418f /source | |
| parent | 2abebc4d066c038594dc667be210990c4c1dce30 (diff) | |
| download | gen3uploader-42379a1cc476d481f5aa7a7b6e69f271f079fda3.tar.gz gen3uploader-42379a1cc476d481f5aa7a7b6e69f271f079fda3.tar.bz2 gen3uploader-42379a1cc476d481f5aa7a7b6e69f271f079fda3.zip | |
general cleanup
Diffstat (limited to 'source')
| -rw-r--r-- | source/main.c | 114 | 
1 files changed, 52 insertions, 62 deletions
| diff --git a/source/main.c b/source/main.c index 9a1b01a..6c34b92 100644 --- a/source/main.c +++ b/source/main.c | |||
| @@ -23,7 +23,7 @@ void printmain() | |||
| 23 | { | 23 | { | 
| 24 | printf("\x1b[2J"); | 24 | printf("\x1b[2J"); | 
| 25 | printf("\x1b[37m"); | 25 | printf("\x1b[37m"); | 
| 26 | printf("GBA Link Cable Dumper v1.2 by FIX94\n"); | 26 | printf("GBA Link Cable Dumper v1.3 by FIX94\n"); | 
| 27 | printf("Save Support based on SendSave by Chishm\n"); | 27 | printf("Save Support based on SendSave by Chishm\n"); | 
| 28 | } | 28 | } | 
| 29 | 29 | ||
| @@ -149,6 +149,12 @@ void sendsafe(u32 msg) | |||
| 149 | SI_Transfer(1,cmdbuf,5,resbuf,1,transcb,0); | 149 | SI_Transfer(1,cmdbuf,5,resbuf,1,transcb,0); | 
| 150 | wait_for_transfer(); | 150 | wait_for_transfer(); | 
| 151 | } | 151 | } | 
| 152 | void sendsafe_wait(u32 msg) | ||
| 153 | { | ||
| 154 | sendsafe(msg); | ||
| 155 | //wait for GBA | ||
| 156 | usleep(5000); | ||
| 157 | } | ||
| 152 | u32 recvfast() | 158 | u32 recvfast() | 
| 153 | { | 159 | { | 
| 154 | cmdbuf[0]=0x14; //read | 160 | cmdbuf[0]=0x14; //read | 
| @@ -169,6 +175,30 @@ bool dirExists(const char *path) | |||
| 169 | } | 175 | } | 
| 170 | return false; | 176 | return false; | 
| 171 | } | 177 | } | 
| 178 | void createFile(const char *path, size_t size) | ||
| 179 | { | ||
| 180 | int fd = open(path, O_WRONLY|O_CREAT); | ||
| 181 | if(fd >= 0) | ||
| 182 | { | ||
| 183 | ftruncate(fd, size); | ||
| 184 | close(fd); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | void warnError(char *msg) | ||
| 188 | { | ||
| 189 | puts(msg); | ||
| 190 | VIDEO_WaitVSync(); | ||
| 191 | VIDEO_WaitVSync(); | ||
| 192 | sleep(2); | ||
| 193 | } | ||
| 194 | void fatalError(char *msg) | ||
| 195 | { | ||
| 196 | puts(msg); | ||
| 197 | VIDEO_WaitVSync(); | ||
| 198 | VIDEO_WaitVSync(); | ||
| 199 | sleep(5); | ||
| 200 | exit(0); | ||
| 201 | } | ||
| 172 | int main(int argc, char *argv[]) | 202 | int main(int argc, char *argv[]) | 
| 173 | { | 203 | { | 
| 174 | void *xfb = NULL; | 204 | void *xfb = NULL; | 
| @@ -195,21 +225,13 @@ int main(int argc, char *argv[]) | |||
| 195 | if(!fatInitDefault()) | 225 | if(!fatInitDefault()) | 
| 196 | { | 226 | { | 
| 197 | printmain(); | 227 | printmain(); | 
| 198 | printf("ERROR: No usable device found to write dumped files to!\n"); | 228 | fatalError("ERROR: No usable device found to write dumped files to!"); | 
| 199 | VIDEO_WaitVSync(); | ||
| 200 | VIDEO_WaitVSync(); | ||
| 201 | sleep(5); | ||
| 202 | exit(0); | ||
| 203 | } | 229 | } | 
| 204 | mkdir("/dumps", S_IREAD | S_IWRITE); | 230 | mkdir("/dumps", S_IREAD | S_IWRITE); | 
| 205 | if(!dirExists("/dumps")) | 231 | if(!dirExists("/dumps")) | 
| 206 | { | 232 | { | 
| 207 | printmain(); | 233 | printmain(); | 
| 208 | printf("ERROR: Could not create dumps folder, make sure you have a supported device connected!\n"); | 234 | fatalError("ERROR: Could not create dumps folder, make sure you have a supported device connected!"); | 
| 209 | VIDEO_WaitVSync(); | ||
| 210 | VIDEO_WaitVSync(); | ||
| 211 | sleep(5); | ||
| 212 | exit(0); | ||
| 213 | } | 235 | } | 
| 214 | int i; | 236 | int i; | 
| 215 | while(1) | 237 | while(1) | 
| @@ -260,7 +282,7 @@ int main(int argc, char *argv[]) | |||
| 260 | for(i = 0; i < 0xC0; i+=4) | 282 | for(i = 0; i < 0xC0; i+=4) | 
| 261 | { | 283 | { | 
| 262 | sendsafe(__builtin_bswap32(*(vu32*)(gba_mb_gba+i))); | 284 | sendsafe(__builtin_bswap32(*(vu32*)(gba_mb_gba+i))); | 
| 263 | if(!(resbuf[0]&0x2)) printf("Possible error %02x\n",resbuf[0]); | 285 | //if(!(resbuf[0]&0x2)) printf("Possible error %02x\n",resbuf[0]); | 
| 264 | } | 286 | } | 
| 265 | //printf("Header done! Sending ROM...\n"); | 287 | //printf("Header done! Sending ROM...\n"); | 
| 266 | for(i = 0xC0; i < sendsize; i+=4) | 288 | for(i = 0xC0; i < sendsize; i+=4) | 
| @@ -272,7 +294,7 @@ int main(int argc, char *argv[]) | |||
| 272 | enc^=((~(i+(0x20<<20)))+1); | 294 | enc^=((~(i+(0x20<<20)))+1); | 
| 273 | enc^=0x20796220; | 295 | enc^=0x20796220; | 
| 274 | sendsafe(enc); | 296 | sendsafe(enc); | 
| 275 | if(!(resbuf[0]&0x2)) printf("Possible error %02x\n",resbuf[0]); | 297 | //if(!(resbuf[0]&0x2)) printf("Possible error %02x\n",resbuf[0]); | 
| 276 | } | 298 | } | 
| 277 | fcrc |= (sendsize<<16); | 299 | fcrc |= (sendsize<<16); | 
| 278 | //printf("ROM done! CRC: %08x\n", fcrc); | 300 | //printf("ROM done! CRC: %08x\n", fcrc); | 
| @@ -305,17 +327,12 @@ int main(int argc, char *argv[]) | |||
| 305 | int gbasize = 0; | 327 | int gbasize = 0; | 
| 306 | while(gbasize == 0) | 328 | while(gbasize == 0) | 
| 307 | gbasize = __builtin_bswap32(recvsafe()); | 329 | gbasize = __builtin_bswap32(recvsafe()); | 
| 308 | sendsafe(0); //got gbasize | 330 | sendsafe_wait(0); //got gbasize | 
| 309 | usleep(5000); //wait for it to set next val | ||
| 310 | u32 savesize = __builtin_bswap32(recvsafe()); | 331 | u32 savesize = __builtin_bswap32(recvsafe()); | 
| 311 | sendsafe(0); //got savesize | 332 | sendsafe_wait(0); //got savesize | 
| 312 | usleep(5000); //wait for it to set next val | ||
| 313 | if(gbasize == -1) | 333 | if(gbasize == -1) | 
| 314 | { | 334 | { | 
| 315 | printf("ERROR: No (Valid) GBA Card inserted!\n"); | 335 | warnError("ERROR: No (Valid) GBA Card inserted!\n"); | 
| 316 | VIDEO_WaitVSync(); | ||
| 317 | VIDEO_WaitVSync(); | ||
| 318 | sleep(2); | ||
| 319 | continue; | 336 | continue; | 
| 320 | } | 337 | } | 
| 321 | //get rom header | 338 | //get rom header | 
| @@ -383,9 +400,7 @@ int main(int argc, char *argv[]) | |||
| 383 | { | 400 | { | 
| 384 | fclose(f); | 401 | fclose(f); | 
| 385 | command = 0; | 402 | command = 0; | 
| 386 | printf("ERROR: Game already dumped!\n"); | 403 | warnError("ERROR: Game already dumped!\n"); | 
| 387 | VIDEO_WaitVSync(); | ||
| 388 | sleep(2); | ||
| 389 | } | 404 | } | 
| 390 | } | 405 | } | 
| 391 | else if(command == 2) | 406 | else if(command == 2) | 
| @@ -395,9 +410,7 @@ int main(int argc, char *argv[]) | |||
| 395 | { | 410 | { | 
| 396 | fclose(f); | 411 | fclose(f); | 
| 397 | command = 0; | 412 | command = 0; | 
| 398 | printf("ERROR: Save already backed up!\n"); | 413 | warnError("ERROR: Save already backed up!\n"); | 
| 399 | VIDEO_WaitVSync(); | ||
| 400 | sleep(2); | ||
| 401 | } | 414 | } | 
| 402 | } | 415 | } | 
| 403 | else if(command == 3) | 416 | else if(command == 3) | 
| @@ -411,9 +424,7 @@ int main(int argc, char *argv[]) | |||
| 411 | if(readsize != savesize) | 424 | if(readsize != savesize) | 
| 412 | { | 425 | { | 
| 413 | command = 0; | 426 | command = 0; | 
| 414 | printf("ERROR: Save has the wrong size, aborting restore!\n"); | 427 | warnError("ERROR: Save has the wrong size, aborting restore!\n"); | 
| 415 | VIDEO_WaitVSync(); | ||
| 416 | sleep(2); | ||
| 417 | } | 428 | } | 
| 418 | else | 429 | else | 
| 419 | { | 430 | { | 
| @@ -425,34 +436,20 @@ int main(int argc, char *argv[]) | |||
| 425 | else | 436 | else | 
| 426 | { | 437 | { | 
| 427 | command = 0; | 438 | command = 0; | 
| 428 | printf("ERROR: No Save to restore!\n"); | 439 | warnError("ERROR: No Save to restore!\n"); | 
| 429 | VIDEO_WaitVSync(); | ||
| 430 | sleep(2); | ||
| 431 | } | 440 | } | 
| 432 | } | 441 | } | 
| 433 | sendsafe(command); | 442 | sendsafe_wait(command); | 
| 434 | usleep(5000); //wait for it to set next val | ||
| 435 | if(command == 0) | 443 | if(command == 0) | 
| 436 | continue; | 444 | continue; | 
| 437 | else if(command == 1) | 445 | else if(command == 1) | 
| 438 | { | 446 | { | 
| 439 | //create base file with size | 447 | //create base file with size | 
| 440 | printf("Creating file...\n"); | 448 | printf("Preparing file...\n"); | 
| 441 | int fd = open(gamename, O_WRONLY|O_CREAT); | 449 | createFile(gamename,gbasize); | 
| 442 | if(fd >= 0) | ||
| 443 | { | ||
| 444 | ftruncate(fd, gbasize); | ||
| 445 | close(fd); | ||
| 446 | } | ||
| 447 | FILE *f = fopen(gamename,"wb"); | 450 | FILE *f = fopen(gamename,"wb"); | 
| 448 | if(!f) | 451 | if(!f) | 
| 449 | { | 452 | fatalError("ERROR: Could not create file! Exit..."); | 
| 450 | printf("ERROR: Could not create file! Exit...\n"); | ||
| 451 | VIDEO_WaitVSync(); | ||
| 452 | VIDEO_WaitVSync(); | ||
| 453 | sleep(5); | ||
| 454 | exit(0); | ||
| 455 | } | ||
| 456 | printf("Dumping...\n"); | 453 | printf("Dumping...\n"); | 
| 457 | u32 bytes_read = 0; | 454 | u32 bytes_read = 0; | 
| 458 | while(gbasize > 0) | 455 | while(gbasize > 0) | 
| @@ -464,10 +461,7 @@ int main(int argc, char *argv[]) | |||
| 464 | *(vu32*)(testdump+j) = recvfast(); | 461 | *(vu32*)(testdump+j) = recvfast(); | 
| 465 | bytes_read+=4; | 462 | bytes_read+=4; | 
| 466 | if((bytes_read&0xFFFF) == 0) | 463 | if((bytes_read&0xFFFF) == 0) | 
| 467 | { | ||
| 468 | printf("\r%02.02f MB done",(float)(bytes_read/1024)/1024.f); | 464 | printf("\r%02.02f MB done",(float)(bytes_read/1024)/1024.f); | 
| 469 | VIDEO_WaitVSync(); | ||
| 470 | } | ||
| 471 | } | 465 | } | 
| 472 | fwrite(testdump,toread,1,f); | 466 | fwrite(testdump,toread,1,f); | 
| 473 | gbasize -= toread; | 467 | gbasize -= toread; | 
| @@ -479,22 +473,18 @@ int main(int argc, char *argv[]) | |||
| 479 | } | 473 | } | 
| 480 | else if(command == 2) | 474 | else if(command == 2) | 
| 481 | { | 475 | { | 
| 476 | //create base file with size | ||
| 477 | printf("Preparing file...\n"); | ||
| 478 | createFile(savename,savesize); | ||
| 482 | FILE *f = fopen(savename,"wb"); | 479 | FILE *f = fopen(savename,"wb"); | 
| 483 | if(!f) | 480 | if(!f) | 
| 484 | { | 481 | fatalError("ERROR: Could not create file! Exit..."); | 
| 485 | printf("ERROR: Could not create file! Exit...\n"); | ||
| 486 | VIDEO_WaitVSync(); | ||
| 487 | VIDEO_WaitVSync(); | ||
| 488 | sleep(5); | ||
| 489 | exit(0); | ||
| 490 | } | ||
| 491 | printf("Waiting for GBA\n"); | 482 | printf("Waiting for GBA\n"); | 
| 492 | VIDEO_WaitVSync(); | 483 | VIDEO_WaitVSync(); | 
| 493 | u32 readval = 0; | 484 | u32 readval = 0; | 
| 494 | while(readval != savesize) | 485 | while(readval != savesize) | 
| 495 | readval = __builtin_bswap32(recvsafe()); | 486 | readval = __builtin_bswap32(recvsafe()); | 
| 496 | sendsafe(0); //got savesize | 487 | sendsafe_wait(0); //got savesize | 
| 497 | usleep(5000); //wait for it to set next val | ||
| 498 | printf("Receiving...\n"); | 488 | printf("Receiving...\n"); | 
| 499 | for(i = 0; i < savesize; i+=4) | 489 | for(i = 0; i < savesize; i+=4) | 
| 500 | *(vu32*)(testdump+i) = recvsafe(); | 490 | *(vu32*)(testdump+i) = recvsafe(); | 
| @@ -517,7 +507,7 @@ int main(int argc, char *argv[]) | |||
| 517 | while(recvsafe() != 0) | 507 | while(recvsafe() != 0) | 
| 518 | VIDEO_WaitVSync(); | 508 | VIDEO_WaitVSync(); | 
| 519 | printf("Save restored!\n"); | 509 | printf("Save restored!\n"); | 
| 520 | sendsafe(0); | 510 | sendsafe_wait(0); | 
| 521 | sleep(5); | 511 | sleep(5); | 
| 522 | } | 512 | } | 
| 523 | } | 513 | } | 
