-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathmempak.c
1127 lines (986 loc) · 29.6 KB
/
mempak.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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file mempak.c
* @brief Controller Pak Filesystem Routine
* @ingroup controllerpak
*/
#include <string.h>
#include "regsinternal.h"
#include <unistd.h>
#include "controller.h"
#include "mempak.h"
/**
* @name Inode values
* @{
*/
/** @brief This block is empty */
#define BLOCK_EMPTY 0x03
/** @brief This is the last block in the note */
#define BLOCK_LAST 0x01
/** @brief First valid block that can contain user data */
#define BLOCK_VALID_FIRST 0x05
/** @brief Last valid block that can contain user data */
#define BLOCK_VALID_LAST 0x7F
/** @} */
int read_mempak_sector( int controller, int sector, uint8_t *sector_data )
{
if( sector < 0 || sector >= 128 ) { return -1; }
if( sector_data == 0 ) { return -1; }
/* Sectors are 256 bytes, a Controller Pak reads 32 bytes at a time */
for( int i = 0; i < 8; i++ )
{
if( read_mempak_address( controller, (sector * MEMPAK_BLOCK_SIZE) + (i * 32), sector_data + (i * 32) ) )
{
/* Failed to read a block */
return -2;
}
}
return 0;
}
int write_mempak_sector( int controller, int sector, uint8_t *sector_data )
{
if( sector < 0 || sector >= 128 ) { return -1; }
if( sector_data == 0 ) { return -1; }
/* Sectors are 256 bytes, a Controller Pak writes 32 bytes at a time */
for( int i = 0; i < 8; i++ )
{
if( write_mempak_address( controller, (sector * MEMPAK_BLOCK_SIZE) + (i * 32), sector_data + (i * 32) ) )
{
/* Failed to read a block */
return -2;
}
}
return 0;
}
/**
* @brief Calculate the checksum of a header
*
* @param[in] block
* A block at the start of a header
*
* @return The 16 bit checksum over the header
*/
static uint16_t __get_header_checksum( uint16_t *block )
{
uint32_t sum = 0;
for ( int i = 0; i < 14; i++ )
{
sum += *(block++);
}
return sum & 0xFFFF;
}
/**
* @brief Check a Controller Pak header for validity
*
* @param[in] sector
* A sector containing a Controller Pak header
*
* @retval 0 if the header is valid
* @retval -1 if the header is invalid
*/
static int __validate_header( uint8_t *sector )
{
uint16_t checksum = 0;
uint8_t current_block = 0x0;
if( !sector ) { return -1; }
/* Header is first sector */
if( memcmp( §or[0x20], §or[0x60], 16 ) != 0 ) { return -1; }
if( memcmp( §or[0x80], §or[0xC0], 16 ) != 0 ) { return -1; }
if( memcmp( §or[0x20], §or[0x80], 16 ) != 0 ) { return -1; }
/* Check 4 checksums of copied header data */
current_block = 0x20;
checksum = __get_header_checksum((uint16_t *)§or[current_block]);
if( (checksum != *(uint16_t *)(§or[current_block + 0x1C])) || (checksum != 0xFFF2 - *(uint16_t *)(§or[current_block + 0x1E])) ) { return -1; }
current_block = 0x60;
checksum = __get_header_checksum((uint16_t *)§or[current_block]);
if( (checksum != *(uint16_t *)(§or[current_block + 0x1C])) || (checksum != 0xFFF2 - *(uint16_t *)(§or[current_block + 0x1E])) ) { return -1; }
current_block = 0x80;
checksum = __get_header_checksum((uint16_t *)§or[current_block]);
if( (checksum != *(uint16_t *)(§or[current_block + 0x1C])) || (checksum != 0xFFF2 - *(uint16_t *)(§or[current_block + 0x1E])) ) { return -1; }
current_block = 0xC0;
checksum = __get_header_checksum((uint16_t *)§or[current_block]);
if( (checksum != *(uint16_t *)(§or[current_block + 0x1C])) || (checksum != 0xFFF2 - *(uint16_t *)(§or[current_block + 0x1E])) ) { return -1; }
return 0;
}
/**
* @brief Calculate the checksum over a TOC sector
*
* @param[in] sector
* A sector containing a TOC
*
* @return The 8 bit checksum over the TOC
*/
static uint8_t __get_toc_checksum( uint8_t *sector )
{
uint32_t sum = 0;
/* Rudimentary checksum */
for( int i = 5; i < 128; i++ )
{
sum += sector[(i << 1) + 1];
}
return sum & 0xFF;
}
/**
* @brief Check a Controller Pak TOC sector for validity
*
* @param[in] sector
* A sector containing a TOC
*
* @retval 0 if the TOC is valid
* @retval -1 if the TOC is invalid
*/
static int __validate_toc( uint8_t *sector )
{
/* True checksum is sum % 256 */
if( __get_toc_checksum( sector ) == sector[1] )
{
return 0;
}
else
{
return -1;
}
return -1;
}
/**
* @brief Convert a Controller Pak character to ASCII
*
* @param[in] c
* A character read from a Controller Pak entry title
*
* @return ASCII equivalent of character read
*/
static char __n64_to_ascii( char c )
{
/* Miscelaneous chart */
switch( c )
{
case 0x00:
return 0;
case 0x0F:
return ' ';
case 0x34:
return '!';
case 0x35:
return '\"';
case 0x36:
return '#';
case 0x37:
return '`';
case 0x38:
return '*';
case 0x39:
return '+';
case 0x3A:
return ',';
case 0x3B:
return '-';
case 0x3C:
return '.';
case 0x3D:
return '/';
case 0x3E:
return ':';
case 0x3F:
return '=';
case 0x40:
return '?';
case 0x41:
return '@';
}
/* Numbers */
if( c >= 0x10 && c <= 0x19 )
{
return '0' + (c - 0x10);
}
/* Uppercase ASCII */
if( c >= 0x1A && c <= 0x33 )
{
return 'A' + (c - 0x1A);
}
/* Default to space for unprintables */
return ' ';
}
/**
* @brief Convert an ASCII character to a Controller Pak character
*
* If the character passed in is one that the Controller Pak doesn't support, this
* function will default to a space.
*
* @param[in] c
* An ASCII character
*
* @return A Controller Pak character equivalent to the ASCII character passed in
*/
static char __ascii_to_n64( char c )
{
/* Miscelaneous chart */
switch( c )
{
case 0:
return 0x00;
case ' ':
return 0x0F;
case '!':
return 0x34;
case '\"':
return 0x35;
case '#':
return 0x36;
case '`':
return 0x37;
case '*':
return 0x38;
case '+':
return 0x39;
case ',':
return 0x3A;
case '-':
return 0x3B;
case '.':
return 0x3C;
case '/':
return 0x3D;
case ':':
return 0x3E;
case '=':
return 0x3F;
case '?':
return 0x40;
case '@':
return 0x41;
}
/* Numbers */
if( c >= '0' && c <= '9' )
{
return 0x10 + (c - '0');
}
/* Uppercase ASCII */
if( c >= 'A' && c <= 'Z' )
{
return 0x1A + (c - 'A');
}
/* Default to space for unprintables */
return 0x0F;
}
/**
* @brief Check a region read from a Controller Pak entry for validity
*
* @param[in] region
* A region read from a Controller Pak entry
*
* @retval 0 if the region is valid
* @retval -1 if the region is invalid
*/
static int __validate_region( uint8_t region )
{
switch( region )
{
case 0x00:
case 0x37:
case 0x41:
case 0x44:
case 0x45:
case 0x46:
case 0x49:
case 0x4A:
case 0x50:
case 0x53:
case 0x55:
case 0x58:
case 0x59:
/* Acceptible region */
return 0;
}
/* Invalid region */
return -3;
}
/**
* @brief Parse a note structure from a TOC
*
* Given a note block read from a Controller Pak TOC, parse and return a structure
* representing the data.
*
* @param[in] tnote
* 32 bytes read from a Controller Pak TOC
* @param[out] note
* Parsed note structure
*
* @retval 0 note was parsed properly
* @retval -1 parameters were invalid
* @retval -2 note inode out of bounds
* @retval -3 note region invalid
*/
static int __read_note( uint8_t *tnote, entry_structure_t *note )
{
if( !tnote || !note ) { return -1; }
/* Easy stuff */
note->vendor = (tnote[0] << 16) | (tnote[1] << 8) | (tnote[2]);
note->region = tnote[3];
/* Important stuff */
note->game_id = (tnote[4] << 8) | (tnote[5]);
note->inode = (tnote[6] << 8) | (tnote[7]);
/* Don't know number of blocks */
note->blocks = 0;
note->valid = 0;
note->entry_id = 255;
/* Translate n64 to ascii */
memset( note->name, 0, sizeof( note->name ) );
for( int i = 0; i < 16; i++ )
{
note->name[i] = __n64_to_ascii( tnote[0x10 + i] );
}
/* Find the last position */
for( int i = 0; i < 17; i++ )
{
if( note->name[i] == 0 )
{
/* Here it is! */
note->name[i] = '.';
note->name[i+1] = __n64_to_ascii( tnote[0xC] );
break;
}
}
/* Validate entries */
if( note->inode < BLOCK_VALID_FIRST || note->inode > BLOCK_VALID_LAST )
{
/* Invalid inode */
return -2;
}
if( __validate_region( note->region ) )
{
/* Invalid region */
return -3;
}
/* Checks out */
note->valid = 1;
return 0;
}
/**
* @brief Create a note structure for a Controller Pak TOC
*
* Given a valid note structure, format it for writing to a Controller Pak TOC
*
* @param[in] note
* Valid note structure to convert
* @param[out] out_note
* 32 bytes ready to be written to a Controller Pak TOC
*
* @retval 0 if the note was converted properly
* @retval -1 if the parameters were invalid
*/
static int __write_note( entry_structure_t *note, uint8_t *out_note )
{
char tname[19];
if( !out_note || !note ) { return -1; }
/* Start with baseline */
memset( out_note, 0, 32 );
/* Easy stuff */
out_note[0] = (note->vendor >> 16) & 0xFF;
out_note[1] = (note->vendor >> 8) & 0xFF;
out_note[2] = note->vendor & 0xFF;
out_note[3] = note->region;
/* Important stuff */
out_note[4] = (note->game_id >> 8) & 0xFF;
out_note[5] = note->game_id & 0xFF;
out_note[6] = (note->inode >> 8) & 0xFF;
out_note[7] = note->inode & 0xFF;
/* Fields that generally stay the same on official saves */
out_note[8] = 0x02;
out_note[9] = 0x03;
/* Translate ascii to n64 */
memcpy( tname, note->name, sizeof( note->name ) );
for( int i = 18; i >= 0; i-- )
{
if( tname[i] == '.' )
{
/* Found extension */
out_note[0xC] = __ascii_to_n64( tname[i+1] );
/* Erase these as they have been taken care of */
tname[i] = 0;
tname[i+1] = 0;
}
}
for( int i = 0; i < 16; i++ )
{
out_note[0x10 + i] = __ascii_to_n64( tname[i] );
}
return 0;
}
/**
* @brief Return number of pages a note occupies
*
* Given a starting inode and a TOC sector, walk the linked list for a note
* and return the number of pages/blocks/sectors a note occupies.
*
* @param[in] sector
* A TOC sector
* @param[in] inode
* A starting inode
*
* @retval -2 The file contained free blocks
* @retval -3 The filesystem was invalid
* @return The number of blocks in a note
*/
static int __get_num_pages( uint8_t *sector, int inode )
{
if( inode < BLOCK_VALID_FIRST || inode > BLOCK_VALID_LAST ) { return -1; }
int tally = 0;
int last = inode;
int rcount = 0;
/* If we go over this, something is wrong */
while( rcount < 123 )
{
switch( sector[(last << 1) + 1] )
{
case BLOCK_LAST:
/* Last block */
return tally + 1;
case BLOCK_EMPTY:
/* Error, can't have free blocks! */
return -2;
default:
last = sector[(last << 1) + 1];
tally++;
/* Failed to point to valid next block */
if( last < BLOCK_VALID_FIRST || last > BLOCK_VALID_LAST ) { return -3; }
break;
}
rcount++;
}
/* Invalid filesystem */
return -3;
}
/**
* @brief Get number of free blocks on a Controller Pak
*
* @param[in] sector
* A valid TOC block to examine
*
* @return The number of free blocks
*/
static int __get_free_space( uint8_t *sector )
{
int space = 0;
for( int i = BLOCK_VALID_FIRST; i <= BLOCK_VALID_LAST; i++ )
{
if( sector[(i << 1) + 1] == BLOCK_EMPTY )
{
space++;
}
}
return space;
}
/**
* @brief Get the inode of the n'th block in a note
*
* @param[in] sector
* A valid TOC sector
* @param[in] inode
* The starting inode of the note
* @param[in] block
* The block offset (starting from 0) to retrieve
*
* @retval -2 if there were free blocks in the file
* @retval -3 if the filesystem was invalid
* @return The inode of the n'th block
*/
static int __get_note_block( uint8_t *sector, int inode, int block )
{
if( inode < BLOCK_VALID_FIRST || inode > BLOCK_VALID_LAST ) { return -1; }
if( block < 0 || block > 123 ) { return -1; }
int tally = block + 1;
int last = inode;
/* Only going through until we hit the requested node */
while( tally > 0 )
{
/* Count down to zero, when zero is hit, this is the node we want */
tally--;
if( !tally )
{
return last;
}
else
{
switch( sector[(last << 1) + 1] )
{
case BLOCK_LAST:
/* Last block, couldn't find block number */
return -2;
case BLOCK_EMPTY:
/* Error, can't have free blocks! */
return -2;
default:
last = sector[(last << 1) + 1];
/* Failed to point to valid next block */
if( last < 5 || last >= 128 ) { return -3; }
break;
}
}
}
/* Invalid filesystem */
return -3;
}
/**
* @brief Retrieve the sector number of the first valid TOC found
*
* @param[in] controller
* The controller (0-3) to inspect for a valid TOC
*
* @retval -2 the Controller Pak was not inserted or was bad
* @retval -3 the Controller Pak was unformatted or the header was invalid
* @retval 1 the first sector has a valid TOC
* @retval 2 the second sector has a valid TOC
*/
static int __get_valid_toc( int controller )
{
/* We will need only one sector at a time */
uint8_t data[MEMPAK_BLOCK_SIZE];
/* First check to see that the header block is valid */
if( read_mempak_sector( controller, 0, data ) )
{
/* Couldn't read header */
return -2;
}
if( __validate_header( data ) )
{
/* Header is invalid or unformatted */
return -3;
}
/* Try to read the first TOC */
if( read_mempak_sector( controller, 1, data ) )
{
/* Couldn't read header */
return -2;
}
if( __validate_toc( data ) )
{
/* First TOC is bad. Maybe the second works? */
if( read_mempak_sector( controller, 2, data ) )
{
/* Couldn't read header */
return -2;
}
if( __validate_toc( data ) )
{
/* Second TOC is bad, nothing good on this memcard */
return -3;
}
else
{
/* Found a good TOC! */
return 2;
}
}
else
{
/* Found a good TOC! */
return 1;
}
}
int validate_mempak( int controller )
{
int toc = __get_valid_toc( controller );
if( toc == 1 || toc == 2 )
{
/* Found a valid TOC */
return 0;
}
else
{
/* Pass on return code */
return toc;
}
}
int get_mempak_entry( int controller, int entry, entry_structure_t *entry_data )
{
uint8_t data[MEMPAK_BLOCK_SIZE];
int toc;
if( entry < 0 || entry > 15 ) { return -1; }
if( entry_data == 0 ) { return -1; }
/* Make sure Controller Pak is valid */
if( (toc = __get_valid_toc( controller )) <= 0 )
{
/* Bad Controller Pak or was removed, return */
return -2;
}
/* Entries are spread across two sectors, but we can luckly grab just one
with a single Controller Pak read */
if( read_mempak_address( controller, (3 * MEMPAK_BLOCK_SIZE) + (entry * 32), data ) )
{
/* Couldn't read note database */
return -2;
}
if( __read_note( data, entry_data ) )
{
/* Note is most likely empty, don't bother getting length */
return 0;
}
/* Grab the TOC sector */
if( read_mempak_sector( controller, toc, data ) )
{
/* Couldn't read TOC */
return -2;
}
/* Get the length of the entry */
int blocks = __get_num_pages( data, entry_data->inode );
if( blocks > 0 )
{
/* Valid entry */
entry_data->blocks = blocks;
entry_data->entry_id = entry;
return 0;
}
else
{
/* Invalid TOC */
entry_data->valid = 0;
return 0;
}
}
int get_mempak_free_space( int controller )
{
uint8_t data[MEMPAK_BLOCK_SIZE];
int toc;
/* Make sure Controller Pak is valid */
if( (toc = __get_valid_toc( controller )) <= 0 )
{
/* Bad Controller Pak or was removed, return */
return -2;
}
/* Grab the valid TOC to get free space */
if( read_mempak_sector( controller, toc, data ) )
{
/* Couldn't read TOC */
return -2;
}
return __get_free_space( data );
}
int format_mempak( int controller )
{
/* Many mempak dumps exist online for users of emulated games to get
saves that have all unlocks. Every beginning sector on all these
was the same, so this is the data I use to initialize the first
sector */
uint8_t sector[MEMPAK_BLOCK_SIZE] = { 0x81,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
0xff,0xff,0xff,0xff,0x05,0x1a,0x5f,0x13,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x01,0xff,0x66,0x25,0x99,0xcd,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0x05,0x1a,0x5f,0x13,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x01,0xff,0x66,0x25,0x99,0xcd,
0xff,0xff,0xff,0xff,0x05,0x1a,0x5f,0x13,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x01,0xff,0x66,0x25,0x99,0xcd,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0x05,0x1a,0x5f,0x13,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0x01,0xff,0x66,0x25,0x99,0xcd,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
if( write_mempak_sector( controller, 0, sector ) )
{
/* Couldn't write initial sector */
return -2;
}
/* Write out entry sectors, which can safely be zero */
memset( sector, 0x0, MEMPAK_BLOCK_SIZE );
if( write_mempak_sector( controller, 3, sector ) ||
write_mempak_sector( controller, 4, sector ) )
{
/* Couldn't write entry sectors */
return -2;
}
/* Go through, insert 'empty sector' marking on all entries */
for( int i = 0; i < 128; i++ )
{
sector[(i << 1) + 1] = BLOCK_EMPTY;
}
/* Fix the checksum */
sector[1] = __get_toc_checksum( sector );
/* Write out */
if( write_mempak_sector( controller, 1, sector ) ||
write_mempak_sector( controller, 2, sector ) )
{
/* Couldn't write TOC sectors */
return -2;
}
return 0;
}
int read_mempak_entry_data( int controller, entry_structure_t *entry, uint8_t *data )
{
int toc;
uint8_t tocdata[MEMPAK_BLOCK_SIZE];
/* Some serious sanity checking */
if( entry == 0 || data == 0 ) { return -1; }
if( entry->valid == 0 ) { return -1; }
if( entry->blocks == 0 || entry->blocks > 123 ) { return -1; }
if( entry->inode < BLOCK_VALID_FIRST || entry->inode > BLOCK_VALID_LAST ) { return -1; }
/* Grab the TOC sector so we can get to the individual blocks the data comprises of */
if( (toc = __get_valid_toc( controller )) <= 0 )
{
/* Bad Controller Pak or was removed, return */
return -2;
}
/* Grab the valid TOC to get free space */
if( read_mempak_sector( controller, toc, tocdata ) )
{
/* Couldn't read TOC */
return -2;
}
/* Now loop through blocks and grab each one */
for( int i = 0; i < entry->blocks; i++ )
{
int block = __get_note_block( tocdata, entry->inode, i );
if( read_mempak_sector( controller, block, data + (i * MEMPAK_BLOCK_SIZE) ) )
{
/* Couldn't read a sector */
return -3;
}
}
/* Fetched all blocks successfully */
return 0;
}
/**
* @brief Write associated data to a Controller Pak entry
*
* Given a Controller Pak entry structure with a valid region, name and block count, writes the
* entry and associated data to the Controller Pak. This function will not overwrite any existing
* user data. To update an existing entry, use #delete_mempak_entry followed by
* #write_mempak_entry_data with the same entry structure.
*
* @param[in] controller
* The controller (0-3) to write the entry and data to
* @param[in] entry
* The entry structure containing a region, name and block count
* @param[in] data
* The associated data to write to to the created entry
*
* @retval 0 if the entry was created and written successfully
* @retval -1 if the parameters were invalid or the note has no length
* @retval -2 if the Controller Pak wasn't present or was bad
* @retval -3 if there was an error writing to the Controller Pak
* @retval -4 if there wasn't enough space to store the note
* @retval -5 if there is no room in the TOC to add a new entry
*/
int write_mempak_entry_data( int controller, entry_structure_t *entry, uint8_t *data )
{
uint8_t sector[MEMPAK_BLOCK_SIZE];
uint8_t tmp_data[32];
int toc;
/* Sanity checking on input data */
if( !entry || !data ) { return -1; }
if( entry->blocks < 1 ) { return -1; }
if( __validate_region( entry->region ) ) { return -1; }
if( strlen( entry->name ) == 0 ) { return -1; }
/* Grab valid TOC */
if( (toc = __get_valid_toc( controller )) <= 0 )
{
/* Bad Controller Pak or was removed, return */
return -2;
}
/* Grab the valid TOC to get free space */
if( read_mempak_sector( controller, toc, sector ) )
{
/* Couldn't read TOC */
return -2;
}
/* Verify that we have enough free space */
if( __get_free_space( sector ) < entry->blocks )
{
/* Not enough space for note */
return -4;
}
/* Find blocks in TOC to allocate */
int tally = entry->blocks;
uint8_t last = BLOCK_LAST;
for( int i = BLOCK_VALID_FIRST; i <= BLOCK_VALID_LAST; i++ )
{
if( sector[(i << 1) + 1] == BLOCK_EMPTY )
{
/* We can use this block */
tally--;
/* Point this towards the next block */
sector[(i << 1) + 1] = last;
/* This block is now the last block */
last = i;
}
/* If we found all blocks, we can exit out early */
if( !tally ) { break; }
}
if( tally > 0 )
{
/* Even though we had free space, couldn't get all blocks? */
return -4;
}
else
{
/* Last now contains our inode */
entry->inode = last;
entry->valid = 0;
entry->vendor = 0;
/* A value observed in most games */
entry->game_id = 0x4535;
}
/* Loop through allocated blocks and write data to sectors */
for( int i = 0; i < entry->blocks; i++ )
{
int block = __get_note_block( sector, entry->inode, i );
if( write_mempak_sector( controller, block, data + (i * MEMPAK_BLOCK_SIZE) ) )
{
/* Couldn't write a sector */
return -3;
}
}
/* Find an empty entry to store to */
for( int i = 0; i < 16; i++ )
{
entry_structure_t tmp_entry;
if( read_mempak_address( controller, (3 * MEMPAK_BLOCK_SIZE) + (i * 32), tmp_data ) )
{
/* Couldn't read note database */
return -2;
}
/* See if we can write to this note */
__read_note( tmp_data, &tmp_entry );
if( tmp_entry.valid == 0 )
{
entry->entry_id = i;
entry->valid = 1;
break;
}
}
if( entry->valid == 0 )
{
/* Couldn't find an entry */
return -5;
}
/* Update CRC on newly updated TOC */
sector[1] = __get_toc_checksum( sector );
/* Write back to alternate TOC first before erasing the known valid one */
if( write_mempak_sector( controller, ( toc == 1 ) ? 2 : 1, sector ) )
{
/* Failed to write alternate TOC */
return -2;
}