-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
890 lines (780 loc) · 21.9 KB
/
main.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
/*
MIT License
Copyright (c) 2021 Christopher Gelatt
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.
*/
#include "AmigaROMUtil.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void print_help(void);
int print_rom_info(const char* encryption_key_path, const char* rom_input_path);
int split_rom(const bool swap, const bool unswap, const bool unconditional_swap, const char* encryption_key_path, const bool correct_checksum, const char* rom_high_path, const char* rom_low_path, const char* rom_input_path);
int merge_rom(const bool swap, const bool unswap, const bool unconditional_swap, const bool encrypt_rom, const char* encryption_key_path, const bool correct_checksum, const char* rom_high_path, const char* rom_low_path, const char* rom_output_path);
int swap_rom(const bool swap_state, const bool unconditional_swap, const bool encrypt_rom, const char* encryption_key_path, const bool correct_checksum, const char* rom_input_path, const char* rom_output_path);
int crypt_rom(const bool encryption_state, const char* encryption_key_path, const char* rom_input_path, const char* rom_output_path);
int checksum_rom(const bool correct_checksum, const char* encryption_key_path, const char* rom_input_path, const char* rom_output_path);
int main(int argc, char** argv)
{
char* rom_input_path = NULL;
char* rom_output_path = NULL;
char* rom_high_path = NULL;
char* rom_low_path = NULL;
char* encryption_key_path = NULL;
bool rom_info = false;
bool split = false;
bool merge = false;
bool swap = false;
bool unswap = false;
bool unconditional_swap = false;
bool validate_checksum = false;
bool correct_checksum = false;
bool encrypt_rom = false;
bool decrypt_rom = false;
int c;
int operation_result = 0;
while((c = getopt(argc, argv, "i:o:a:b:k:fsgpunvcedh")) != -1)
{
switch(c)
{
case 'i':
rom_input_path = strdup(optarg);
break;
case 'o':
rom_output_path = strdup(optarg);
break;
case 'a':
rom_high_path = strdup(optarg);
break;
case 'b':
rom_low_path = strdup(optarg);
break;
case 'k':
encryption_key_path = strdup(optarg);
break;
case 'f':
rom_info = true;
break;
case 's':
split = true;
break;
case 'g':
merge = true;
break;
case 'p':
swap = true;
break;
case 'u':
unswap = true;
break;
case 'n':
unconditional_swap = true;
break;
case 'v':
validate_checksum = true;
break;
case 'c':
correct_checksum = true;
break;
case 'e':
encrypt_rom = true;
break;
case 'd':
decrypt_rom = true;
break;
case 'h':
default:
print_help();
exit(0);
}
}
if(unconditional_swap)
{
swap = true;
unswap = false;
}
if(!rom_info && !split && !merge && !swap && !unswap && !encrypt_rom && !decrypt_rom && !validate_checksum && !correct_checksum)
{
print_help();
exit(1);
}
if(rom_info && !rom_input_path)
{
print_help();
exit(1);
}
if(split && merge)
{
print_help();
exit(1);
}
if(encrypt_rom && decrypt_rom)
{
print_help();
exit(1);
}
if(split && (!rom_input_path || !rom_high_path || !rom_low_path))
{
print_help();
exit(1);
}
if(merge && (!rom_output_path || !rom_high_path || !rom_low_path))
{
print_help();
exit(1);
}
if(swap && (!rom_input_path || !rom_output_path))
{
print_help();
exit(1);
}
if(unswap && (!rom_input_path || !rom_output_path))
{
print_help();
exit(1);
}
if((encrypt_rom || decrypt_rom || correct_checksum) && (!rom_input_path || !rom_output_path))
{
print_help();
exit(1);
}
if(validate_checksum && (!rom_input_path && !merge))
{
print_help();
exit(1);
}
if((encrypt_rom || decrypt_rom) && !encryption_key_path)
{
print_help();
exit(1);
}
if(rom_info)
{
operation_result = print_rom_info(encryption_key_path, rom_input_path);
}
else if(split)
{
operation_result = split_rom(swap, unswap, unconditional_swap, encryption_key_path, correct_checksum, rom_high_path, rom_low_path, rom_input_path);
}
else if(merge)
{
operation_result = merge_rom(swap, unswap, unconditional_swap, encrypt_rom, encryption_key_path, correct_checksum, rom_high_path, rom_low_path, rom_output_path);
}
else if(swap)
{
operation_result = swap_rom(true, unconditional_swap, encrypt_rom, encryption_key_path, correct_checksum, rom_input_path, rom_output_path);
}
else if(unswap)
{
operation_result = swap_rom(false, unconditional_swap, encrypt_rom, encryption_key_path, correct_checksum, rom_input_path, rom_output_path);
}
else if(encrypt_rom)
{
operation_result = crypt_rom(true, encryption_key_path, rom_input_path, rom_output_path);
}
else if(decrypt_rom)
{
operation_result = crypt_rom(false, encryption_key_path, rom_input_path, rom_output_path);
}
else if(correct_checksum)
{
operation_result = checksum_rom(true, encryption_key_path, rom_input_path, rom_output_path);
}
else if(validate_checksum)
{
operation_result = checksum_rom(false, encryption_key_path, rom_input_path, NULL);
}
free(rom_high_path);
free(rom_low_path);
free(rom_input_path);
free(rom_output_path);
free(encryption_key_path);
exit(operation_result);
}
void print_help(void)
{
printf("Usage: AmigaROMUtil [options]\n");
printf("Options:\n");
printf(" -i FILE Path to input ROM (except for merging)\n");
printf(" -o FILE Path to output ROM (except for splitting)\n");
printf(" -a FILE Path to High ROM for merging or splitting\n");
printf(" -b FILE Path to Low ROM for merging or splitting\n");
printf(" -k FILE Path to ROM encryption/decryption key\n");
printf(" -f Print ROM info and quit (requires -i)\n");
printf(" -s Split ROM (requires -i, -a, -b)\n");
printf(" -g Merge ROM (requires -a, -b, -o)\n");
printf(" -p Byte swap ROM for burning to an IC (requires -i, -o)\n");
printf(" -u Unbyte swap ROM for use with emulators (requires -i, -o)\n");
printf(" -n Unconditional byte swap for ROMs which are unknown to this program\n");
printf(" -v Validate checksum (requires -i)\n");
printf(" -c Correct checksum (requires -i, -o)\n");
printf(" -e Encrypt ROM (requires -i, -o, -k)\n");
printf(" -d Decrypt ROM (requires -i, -o, -k)\n");
printf(" -h Display this information\n");
printf("\n");
printf("Notes:\n");
printf("-n implies -u and not -n, and will override those if set\n");
printf("-s and -m, -p and -u, -e and -d are each mutually exclusive\n");
return;
}
int print_rom_info(const char* encryption_key_path, const char* rom_input_path)
{
char *info_string = NULL;
ParsedAmigaROMData input_rom = GetInitializedAmigaROM();
info_string = (char *)malloc(4096);
if(!info_string)
{
return 1;
}
input_rom = ReadAmigaROM(rom_input_path, encryption_key_path);
PrintAmigaROMInfo(&input_rom, info_string, 4096);
printf("%s\n", info_string);
free(info_string);
return 0;
}
int split_rom(const bool swap, const bool unswap, const bool unconditional_swap, const char* encryption_key_path, const bool correct_checksum, const char* rom_high_path, const char* rom_low_path, const char* rom_input_path)
{
ParsedAmigaROMData high_rom = GetInitializedAmigaROM();
ParsedAmigaROMData input_rom = GetInitializedAmigaROM();
ParsedAmigaROMData low_rom = GetInitializedAmigaROM();
input_rom = ReadAmigaROM(rom_input_path, encryption_key_path);
if(!input_rom.parsed_rom)
{
if(input_rom.is_encrypted && !input_rom.can_decrypt)
{
printf("ERROR: Source ROM is encrypted. Please provide a valid key to decrypt.");
}
else
{
printf("ERROR: Unable to load source ROM at: %s\n", rom_high_path);
printf("Is it a valid ROM?\n");
}
if(input_rom.rom_data)
{
DestroyInitializedAmigaROM(&input_rom);
}
return 1;
}
if(input_rom.version == NULL)
{
printf("WARNING: Unknown source ROM loaded.\n");
}
else
{
printf("Detected source ROM: %s\n", input_rom.version);
}
if(input_rom.type != 'M')
{
printf("ROM type: %c\n", input_rom.type);
printf("WARNING: ROM is not detected as a known merged ROM.\n");
}
if(input_rom.has_valid_checksum)
{
printf("Source ROM checksum is valid.\n");
}
else
{
if(correct_checksum)
{
if(CorrectAmigaROMChecksum(&input_rom))
{
printf("Corrected source ROM checksum.\n");
}
else
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to correct source ROM checksum.\n");
return 1;
}
}
else
{
printf("WARNING: Source ROM checksum is invalid.\n");
}
}
if((swap || unswap) && ((input_rom.type != 'U' || unconditional_swap) || SetAmigaROMByteSwap(&input_rom, swap, unswap, unconditional_swap) == 0))
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to perform conditional swap operation. Aborting.\n");
return 1;
}
if(!SplitAmigaROM(&input_rom, &high_rom, &low_rom))
{
DestroyInitializedAmigaROM(&input_rom);
if(high_rom.rom_data)
{
DestroyInitializedAmigaROM(&high_rom);
}
if(low_rom.rom_data)
{
DestroyInitializedAmigaROM(&low_rom);
}
printf("ERROR: ROM split operation failed. Aborting.\n");
return 1;
}
if(!WriteAmigaROM(&high_rom, rom_high_path))
{
DestroyInitializedAmigaROM(&input_rom);
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: Unable to write High ROM to disk. Aborting.\n");
return 1;
}
if(!WriteAmigaROM(&low_rom, rom_low_path))
{
DestroyInitializedAmigaROM(&input_rom);
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: Unable to write Low ROM to disk. Aborting.\n");
return 1;
}
DestroyInitializedAmigaROM(&input_rom);
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("Successfully wrote High and Low ROMs.\n");
return 0;
}
int merge_rom(const bool swap, const bool unswap, const bool unconditional_swap, const bool encrypt_rom, const char* encryption_key_path, const bool correct_checksum, const char* rom_high_path, const char* rom_low_path, const char* rom_output_path)
{
ParsedAmigaROMData high_rom = GetInitializedAmigaROM();
ParsedAmigaROMData low_rom = GetInitializedAmigaROM();
ParsedAmigaROMData output_rom = GetInitializedAmigaROM();
high_rom = ReadAmigaROM(rom_high_path, encryption_key_path);
if(!high_rom.parsed_rom)
{
if(high_rom.is_encrypted && !high_rom.can_decrypt)
{
printf("ERROR: High ROM is encrypted. Please provide a valid key to decrypt.");
}
else
{
printf("ERROR: Unable to load High ROM at: %s\n", rom_high_path);
printf("Is it a valid ROM?\n");
}
if(high_rom.rom_data)
{
DestroyInitializedAmigaROM(&high_rom);
}
return 1;
}
low_rom = ReadAmigaROM(rom_low_path, encryption_key_path);
if(!low_rom.parsed_rom)
{
if(low_rom.is_encrypted && !low_rom.can_decrypt)
{
printf("ERROR: Low ROM is encrypted. Please provide a valid key to decrypt.");
}
else
{
printf("ERROR: Unable to load Low ROM at: %s\n", rom_low_path);
printf("Is it a valid ROM?\n");
}
if(low_rom.rom_data)
{
DestroyInitializedAmigaROM(&low_rom);
}
DestroyInitializedAmigaROM(&high_rom);
return 1;
}
if(high_rom.rom_size != low_rom.rom_size)
{
printf("ERROR: High and Low ROMs must be the same size to merge.\n");
return 1;
}
if(high_rom.version == NULL)
{
printf("WARNING: Unknown High ROM loaded.\n");
}
else
{
printf("Detected High ROM: %s\n", high_rom.version);
}
if(low_rom.version == NULL)
{
printf("WARNING: Unknown Low ROM loaded.\n");
}
else
{
printf("Detected Low ROM: %s\n", low_rom.version);
}
if(high_rom.version != NULL && low_rom.version == NULL)
{
printf("WARNING: Known High ROM and unknown Low ROM detected.\n");
}
else if(low_rom.version != NULL && high_rom.version == NULL)
{
printf("WARNING: Known Low ROM and unknown High ROM detected.\n");
}
else if((high_rom.version != NULL && low_rom.version != NULL) && (strlen(high_rom.version) != strlen(low_rom.version) || strncmp(high_rom.version, low_rom.version, strlen(high_rom.version)) != 0))
{
printf("WARNING: High and Low ROMs are not from the same set.\n");
}
if(high_rom.type != 'A')
{
printf("WARNING: High ROM is not detected as a known High ROM.\n");
}
if(low_rom.type != 'B')
{
printf("WARNING: Low ROM is not detected as a known Low ROM.\n");
}
if((swap || unswap) && ((high_rom.type != 'U' || unconditional_swap) && SetAmigaROMByteSwap(&high_rom, swap, unswap, unconditional_swap) == 0))
{
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: Unable to perform conditional swap operation for High ROM. Aborting.\n");
return 1;
}
if((swap || unswap) && ((low_rom.type != 'U' || unconditional_swap) && SetAmigaROMByteSwap(&low_rom, swap, unswap, unconditional_swap) == 0))
{
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: Unable to perform conditional swap operation for Low ROM. Aborting.\n");
return 1;
}
if(!MergeAmigaROM(&high_rom, &low_rom, &output_rom))
{
if(output_rom.rom_data)
{
DestroyInitializedAmigaROM(&output_rom);
}
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: ROM merge operation failed. Aborting.\n");
return 1;
}
ParseAmigaROMData(&output_rom, encryption_key_path);
if(output_rom.has_valid_checksum)
{
printf("Merged ROM checksum is valid.\n");
}
else
{
if(correct_checksum)
{
if(CorrectAmigaROMChecksum(&output_rom))
{
printf("Corrected merged ROM checksum.\n");
}
else
{
DestroyInitializedAmigaROM(&output_rom);
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: Unable to correct merged ROM checksum.\n");
return 1;
}
}
else
{
printf("WARNING: Merged ROM checksum is invalid.\n");
}
}
if(encrypt_rom)
{
if(encryption_key_path == NULL)
{
DestroyInitializedAmigaROM(&output_rom);
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: Encrypting a ROM requires specifying an encryption key.\n");
return 1;
}
if(high_rom.is_encrypted)
{
printf("INFO: Encrypting merged ROM with the same key as the High ROM.\n");
}
if(low_rom.is_encrypted)
{
printf("INFO: Encrypting merged ROM with the same key as the Low ROM.\n");
}
if(!CryptAmigaROM(&output_rom, true, encryption_key_path))
{
DestroyInitializedAmigaROM(&output_rom);
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: Unable to access ROM encryption key.\n");
return 1;
}
else
{
printf("Encrypted ROM.\n");
}
}
if(!WriteAmigaROM(&output_rom, rom_output_path))
{
DestroyInitializedAmigaROM(&output_rom);
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("ERROR: Unable to write merged ROM to disk. Aborting.");
return 1;
}
DestroyInitializedAmigaROM(&output_rom);
DestroyInitializedAmigaROM(&high_rom);
DestroyInitializedAmigaROM(&low_rom);
printf("Successfully wrote merged ROM.\n");
return 0;
}
int swap_rom(const bool swap_state, const bool unconditional_swap, const bool encrypt_rom, const char* encryption_key_path, const bool correct_checksum, const char* rom_input_path, const char* rom_output_path)
{
size_t encrypted_input_rom_size = 0;
ParsedAmigaROMData input_rom = GetInitializedAmigaROM();
input_rom = ReadAmigaROM(rom_input_path, encryption_key_path);
if(!input_rom.parsed_rom)
{
if(input_rom.is_encrypted && !input_rom.can_decrypt)
{
printf("ERROR: Source ROM is encrypted. Please provide a valid key to decrypt.");
}
else
{
printf("ERROR: Unable to load source ROM at: %s\n", rom_input_path);
printf("Is it a valid ROM?\n");
}
if(input_rom.rom_data != NULL)
{
DestroyInitializedAmigaROM(&input_rom);
}
return 1;
}
if(input_rom.version == NULL)
{
printf("WARNING: Unknown source ROM loaded.\n");
}
else
{
printf("Detected source ROM: %s\n", input_rom.version);
}
if(SetAmigaROMByteSwap(&input_rom, swap_state, !swap_state, unconditional_swap) == 0)
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to perform conditional swap operation for ROM. Aborting.\n");
return 1;
}
if(input_rom.has_valid_checksum)
{
printf("ROM checksum is valid.\n");
}
else
{
if(correct_checksum)
{
if(CorrectAmigaROMChecksum(&input_rom))
{
printf("Corrected ROM checksum.\n");
}
else
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to correct checksum for ROM. Aborting.\n");
return 1;
}
}
else
{
printf("WARNING: ROM checksum is invalid.\n");
}
}
if(encrypt_rom)
{
if(encryption_key_path == NULL)
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Encrypting a ROM requires specifying an encryption key.\n");
return 1;
}
if(input_rom.is_encrypted)
{
printf("INFO: Encrypting swapped ROM with the same key as the source ROM.\n");
}
if(!CryptAmigaROM(&input_rom, true, encryption_key_path))
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to access ROM encryption key.\n");
return 1;
}
else
{
printf("Encrypted ROM.\n");
input_rom.rom_size = encrypted_input_rom_size;
}
}
if(!WriteAmigaROM(&input_rom, rom_output_path))
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to write swapped ROM to disk.\n");
return 1;
}
DestroyInitializedAmigaROM(&input_rom);
if(swap_state)
{
printf("Successfully wrote swapped ROM.\n");
}
else
{
printf("Successfully wrote unswapped ROM.\n");
}
return 0;
}
int crypt_rom(const bool encryption_state, const char* encryption_key_path, const char* rom_input_path, const char* rom_output_path)
{
size_t encrypted_input_rom_size = 0;
ParsedAmigaROMData input_rom = GetInitializedAmigaROM();
input_rom = ReadAmigaROM(rom_input_path, encryption_key_path);
if(!input_rom.parsed_rom)
{
if(input_rom.is_encrypted && !input_rom.can_decrypt)
{
printf("ERROR: Source ROM is encrypted. Please provide a valid key to decrypt.");
}
else
{
printf("ERROR: Unable to load source ROM at: %s\n", rom_input_path);
printf("Is it a valid ROM?\n");
}
if(input_rom.rom_data != NULL)
{
DestroyInitializedAmigaROM(&input_rom);
}
return 1;
}
if(input_rom.version == NULL)
{
printf("WARNING: Unknown source ROM loaded.\n");
}
else
{
printf("Detected source ROM: %s\n", input_rom.version);
}
if(input_rom.is_encrypted && input_rom.successfully_decrypted && !encryption_state)
{
{
printf("Decrypted ROM.\n");
}
}
if(encryption_state)
{
if(input_rom.is_encrypted)
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Encrypting with the same key used to decrypt the ROM.\n");
return 1;
}
if(!CryptAmigaROM(&input_rom, true, encryption_key_path))
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to access ROM encryption key.\n");
return 1;
}
else
{
printf("Encrypted ROM.\n");
input_rom.rom_size = encrypted_input_rom_size;
}
}
if(!WriteAmigaROM(&input_rom, rom_output_path))
{
DestroyInitializedAmigaROM(&input_rom);
if(encryption_state)
{
printf("ERROR: Unable to write encrypted ROM to disk.\n");
}
else
{
printf("ERROR: Unable to write decrypted ROM to disk.\n");
}
return 1;
}
DestroyInitializedAmigaROM(&input_rom);
if(encryption_state)
{
printf("Successfully wrote encrypted ROM.\n");
}
else
{
printf("Successfully wrote decrypted ROM.\n");
}
return 0;
}
int checksum_rom(const bool correct_checksum, const char* encryption_key_path, const char* rom_input_path, const char* rom_output_path)
{
ParsedAmigaROMData input_rom;
input_rom = ReadAmigaROM(rom_input_path, encryption_key_path);
if(!input_rom.parsed_rom)
{
if(input_rom.is_encrypted && !input_rom.can_decrypt)
{
printf("ERROR: Source ROM is encrypted. Please provide a valid key to decrypt.");
}
else
{
printf("ERROR: Unable to load source ROM at: %s\n", rom_input_path);
printf("Is it a valid ROM?\n");
}
if(input_rom.rom_data != NULL)
{
DestroyInitializedAmigaROM(&input_rom);
}
return 1;
}
if(input_rom.version == NULL)
{
printf("WARNING: Unknown source ROM loaded.\n");
}
else
{
printf("Detected source ROM: %s\n", input_rom.version);
}
if(input_rom.has_valid_checksum)
{
printf("ROM checksum is valid.\n");
}
else
{
if(correct_checksum)
{
if(CorrectAmigaROMChecksum(&input_rom))
{
printf("Corrected ROM checksum.\n");
if(!WriteAmigaROM(&input_rom, rom_output_path))
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to write corrected ROM to disk.\n");
return 1;
}
printf("Successfully wrote corrected ROM.\n");
}
else
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: Unable to correct ROM checksum.\n");
return 1;
}
}
else
{
DestroyInitializedAmigaROM(&input_rom);
printf("ERROR: ROM checksum is invalid.\n");
return 1;
}
}
DestroyInitializedAmigaROM(&input_rom);
return 0;
}