-
Notifications
You must be signed in to change notification settings - Fork 8
/
fdisk.c
1225 lines (1054 loc) · 38.4 KB
/
fdisk.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
/*
Extremely simplified FDISK + FORMAT utility for the MEGA65.
This program is designed to be compilable both for the MEGA65
using CC65, and also for UNIX-like operating systems for testing.
All hardware dependent features will be in fdisk_hal_mega65.c and
fdisk_hal_unix.c, respectively. I.e., this file contains only the
hardware independent logic.
This program gets the size of the SD card, and then calculates an
appropriate MBR, DOS Boot Sector, FS Information Sector, FATs and
root directory, and puts them in place.
Also creates the MEGA65 system partitions for
installed services, and for task switching.
XXX - Should initialise the configuration sector in the system partition,
so that on first use, you don't get the "CONFIGURATION INVALID" message
from the Hypervisor.
*/
#include <stdio.h>
#include <string.h>
#ifndef __CC65__
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include "fdisk_hal.h"
#include "fdisk_memory.h"
#include "fdisk_screen.h"
#include "fdisk_fat32.h"
#ifdef __CC65__
#include "ascii.h"
#endif
#include "dirtymock.h"
#ifdef __CC65__
#define xcopy(str, addr, len) lcopy((unsigned long)str, (unsigned long)addr, len);
#else
#define xcopy(str, addr, len) bcopy(str, addr, len);
#endif
unsigned char slot_magic[16] = { 0x4d, 0x45, 0x47, 0x41, 0x36, 0x35, 0x42, 0x49, 0x54, 0x53, 0x54, 0x52, 0x45, 0x41, 0x4d,
0x30 };
unsigned char have_rom = 0, have_sdfiles = 0;
uint8_t hardware_model_id = 0xff;
unsigned long slot_size = 8UL * 0x100000UL;
int format_disk(void);
void open_sdcard_and_retrieve_details(void);
#ifdef __CC65__
#define MAX_SLOT 8
#else
#define MAX_SLOT 1
#endif
typedef struct {
char version[32];
unsigned char file_count;
unsigned long file_offset;
} mega65slotT;
mega65slotT mega65slot[MAX_SLOT];
// When set, it enters batch mode
unsigned char dont_confirm = 0;
uint8_t sector_buffer[512];
void clear_sector_buffer(void)
{
#ifndef __CC65__DONTUSE
int i;
for (i = 0; i < 512; i++)
sector_buffer[i] = 0;
#else
lfill((uint32_t)sector_buffer, 0, 512);
#endif
}
/* Build a master boot record that has the single partition we need in
the correct place, and with the size of the partition set correctly.
*/
void build_mbr(const uint32_t sys_partition_start, const uint32_t sys_partition_sectors, const uint32_t fat_partition_start,
const uint32_t fat_partition_sectors)
{
clear_sector_buffer();
// Set disk signature (fixed value)
sector_buffer[0x1b8] = 0x83;
sector_buffer[0x1b9] = 0x7d;
sector_buffer[0x1ba] = 0xcb;
sector_buffer[0x1bb] = 0xa6;
// We have to put the FAT partition first for people running bitstreams from
// the microSD card, as otherwise the N4/N4DDR boards don't find the FAT partition
// MEGA65 System Partition entry
// sector_buffer[0x1ce]=0x00; // Not bootable by DOS
// sector_buffer[0x1cf]=0x00; // 3 bytes CHS starting point
// sector_buffer[0x1d0]=0x00;
// sector_buffer[0x1d1]=0x00;
sector_buffer[0x1d2] = 0x41; // Partition type (MEGA65 System Partition)
// sector_buffer[0x1d3]=0x00; // 3 bytes CHS end point - SHOULD CHANGE WITH DISK SIZE
// sector_buffer[0x1d4]=0x00;
// sector_buffer[0x1d5]=0x00;
// LBA starting sector of partition (usually @ 0x0800 = sector 2,048 = 1MB)
sector_buffer[0x1d6] = (sys_partition_start >> 0) & 0xff;
sector_buffer[0x1d7] = (sys_partition_start >> 8) & 0xff;
sector_buffer[0x1d8] = (sys_partition_start >> 16) & 0xff;
sector_buffer[0x1d9] = (sys_partition_start >> 24) & 0xff;
// LBA size of partition in sectors
sector_buffer[0x1da] = (sys_partition_sectors >> 0) & 0xff;
sector_buffer[0x1db] = (sys_partition_sectors >> 8) & 0xff;
sector_buffer[0x1dc] = (sys_partition_sectors >> 16) & 0xff;
sector_buffer[0x1dd] = (sys_partition_sectors >> 24) & 0xff;
// FAT32 Partition entry
// sector_buffer[0x1be]=0x00; // Not bootable by DOS
// sector_buffer[0x1bf]=0x00; // 3 bytes CHS starting point
// sector_buffer[0x1c0]=0x00;
// sector_buffer[0x1c1]=0x00;
sector_buffer[0x1c2] = 0x0c; // Partition type (VFAT32)
// sector_buffer[0x1c3]=0x00; // 3 bytes CHS end point - SHOULD CHANGE WITH DISK SIZE
// sector_buffer[0x1c4]=0x00;
// sector_buffer[0x1c5]=0x00;
// LBA starting sector of FAT32 partition
sector_buffer[0x1c6] = (fat_partition_start >> 0) & 0xff;
sector_buffer[0x1c7] = (fat_partition_start >> 8) & 0xff;
sector_buffer[0x1c8] = (fat_partition_start >> 16) & 0xff;
sector_buffer[0x1c9] = (fat_partition_start >> 24) & 0xff;
// LBA size of partition in sectors
sector_buffer[0x1ca] = (fat_partition_sectors >> 0) & 0xff;
sector_buffer[0x1cb] = (fat_partition_sectors >> 8) & 0xff;
sector_buffer[0x1cc] = (fat_partition_sectors >> 16) & 0xff;
sector_buffer[0x1cd] = (fat_partition_sectors >> 24) & 0xff;
// MBR signature
sector_buffer[0x1fe] = 0x55;
sector_buffer[0x1ff] = 0xaa;
}
uint8_t boot_bytes[258] = {
// Jump to boot code, required by most version of DOS
0xeb, 0x58, 0x90,
// OEM String: MEGA65r1
0x4d, 0x45, 0x47, 0x41, 0x36, 0x35, 0x72, 0x31,
// BIOS Parameter block. We patch certain
// values in here.
0x00, 0x02, // Sector size = 512 bytes
0x08, // Sectors per cluster
/* 0x0e */ 0x38, 0x02, // Number of reserved sectors (0x238 = 568)
/* 0x10 */ 0x02, // Number of FATs
0x00, 0x00, // Max directory entries for FAT12/16 (0 for FAT32)
/* offset 0x13 */ 0x00, 0x00, // Total logical sectors (0 for FAT32)
0xf8, // Disk type (0xF8 = hard disk)
0x00, 0x00, // Sectors per FAT for FAT12/16 (0 for FAT32)
/* offset 0x18 */ 0x00, 0x00, // Sectors per track (0 for LBA only)
0x00, 0x00, // Number of heads for CHS drives, zero for LBA
0x00, 0x00, 0x00, 0x00, // 32-bit Number of hidden sectors before partition. Should be 0 if logical sectors == 0
/* 0x20 */ 0x00, 0xe8, 0x0f, 0x00, // 32-bit total logical sectors
/* 0x24 */ 0xf8, 0x03, 0x00, 0x00, // Sectors per FAT
/* 0x28 */ 0x00, 0x00, // Drive description
/* 0x2a */ 0x00, 0x00, // Version 0.0
/* 0x2c */ 0x02, 0x00, 0x00, 0x00, // Number of first cluster
/* 0x30 */ 0x01, 0x00, // Logical sector of FS Information sector
/* 0x32 */ 0x06, 0x00, // Sector number of backup-copy of boot sector
/* 0x34 */ 0x00, 0x00, 0x00, 0x00, // Filler bytes
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Filler bytes
/* 0x40 */ 0x80, // Physical drive number
/* 0x41 */ 0x00, // FAT12/16 use only
/* 0x42 */ 0x29, // 0x29 == Extended Boot Signature
/* 0x43 */ 0x6d, 0x66, 0x62, 0x61, // Volume ID "mfba"
/* 0x47 */ 0x4d, 0x2e, 0x45, 0x2e, 0x47, // 11 byte volume label
0x2e, 0x41, 0x2e, 0x20, 0x36, 0x35,
/* 0x52 */ 0x46, 0x41, 0x54, 0x33, 0x32, 0x20, 0x20, 0x20, // "FAT32 "
// Boot loader code starts here
0x0e, 0x1f, 0xbe, 0x77, 0x7c, 0xac, 0x22, 0xc0, 0x74, 0x0b, 0x56, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 0x5e, 0xeb,
0xf0, 0x32, 0xe4, 0xcd, 0x16, 0xcd, 0x19, 0xeb, 0xfe,
// From here on is the non-bootable error message
// 0x82 - 0x69 =
0x4d, 0x45, 0x47, 0x41, 0x36, 0x35, 0x20,
// 9-character name of operating system
'H', 'Y', 'P', 'P', 'O', 'B', 'O', 'O', 'T',
0x20, 0x56, 0x30, 0x30, 0x2e, 0x31, 0x31, 0x0d, 0x0a, 0x0d, 0x3f, 0x4e, 0x4f, 0x20, 0x34, 0x35, 0x47, 0x53, 0x30, 0x32,
0x2c, 0x20, 0x34, 0x35, 0x31, 0x30, 0x2c, 0x20, 0x36, 0x35, 0x5b, 0x63, 0x65, 0x5d, 0x30, 0x32, 0x2c, 0x20, 0x36, 0x35,
0x31, 0x30, 0x20, 0x4f, 0x52, 0x20, 0x38, 0x35, 0x31, 0x30, 0x20, 0x50, 0x52, 0x4f, 0x43, 0x45, 0x53, 0x53, 0x4f, 0x52,
0x20, 0x20, 0x45, 0x52, 0x52, 0x4f, 0x52, 0x0d, 0x0a, 0x49, 0x4e, 0x53, 0x45, 0x52, 0x54, 0x20, 0x44, 0x49, 0x53, 0x4b,
0x20, 0x49, 0x4e, 0x20, 0x52, 0x45, 0x41, 0x4c, 0x20, 0x43, 0x4f, 0x4d, 0x50, 0x55, 0x54, 0x45, 0x52, 0x20, 0x41, 0x4e,
0x44, 0x20, 0x54, 0x52, 0x59, 0x20, 0x41, 0x47, 0x41, 0x49, 0x4e, 0x2e, 0x0a, 0x0a, 0x52, 0x45, 0x41, 0x44, 0x59, 0x2e,
0x0d, 0x0a
};
void build_dosbootsector(uint32_t data_sectors, uint32_t fs_sectors_per_fat)
{
uint16_t i;
clear_sector_buffer();
// Start with template, and then modify relevant fields */
xcopy(boot_bytes, sector_buffer, sizeof(boot_bytes));
// 0x20-0x23 = 32-bit number of data sectors in file system
for (i = 0; i < 4; i++)
sector_buffer[0x20 + i] = ((data_sectors) >> (i * 8)) & 0xff;
// 0x24-0x27 = 32-bit number of sectors per fat
for (i = 0; i < 4; i++)
sector_buffer[0x24 + i] = ((fs_sectors_per_fat) >> (i * 8)) & 0xff;
// 0x43-0x46 = 32-bit volume ID (random bytes)
// 0x47-0x51 = 11 byte volume string
// Boot sector signature
sector_buffer[510] = 0x55;
sector_buffer[511] = 0xaa;
}
void build_fs_information_sector(const uint32_t fs_clusters)
{
uint8_t i;
clear_sector_buffer();
sector_buffer[0] = 0x52;
sector_buffer[1] = 0x52;
sector_buffer[2] = 0x61;
sector_buffer[3] = 0x41;
sector_buffer[0x1e4] = 0x72;
sector_buffer[0x1e5] = 0x72;
sector_buffer[0x1e6] = 0x41;
sector_buffer[0x1e7] = 0x61;
// Last free cluster = (cluster count - 1)
#ifndef __CC65__
fprintf(stderr, "Writing fs_clusters (0x%x) as ", fs_clusters);
#endif
for (i = 0; i < 4; i++) {
// Number of free clusters
sector_buffer[0x1e8 + i] = ((fs_clusters - 3) >> (i * 8)) & 0xff;
#ifndef __CC65__
fprintf(stderr, "%02x ", sector_buffer[0x1e8 + i]);
#endif
}
#ifndef __CC65__
fprintf(stderr, "\n");
#endif
// First free cluster = 2
sector_buffer[0x1ec] = 0x02 + 1; // OSX newfs/fsck puts 3 here instead?
// Boot sector signature
sector_buffer[510] = 0x55;
sector_buffer[511] = 0xaa;
}
uint8_t fat_bytes[12] = { 0xf8, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x0f, 0xf8, 0xff, 0xff, 0x0f };
void build_empty_fat()
{
int i;
clear_sector_buffer();
for (i = 0; i < 12; i++)
sector_buffer[i] = fat_bytes[i];
}
uint8_t dir_bytes[15] = { 8, 0, 0, 0x53, 0xae, 0x93, 0x4a, 0x93, 0x4a, 0, 0, 0x53, 0xae, 0x93, 0x4a };
void build_root_dir(const uint8_t volume_name[11])
{
int i;
clear_sector_buffer();
for (i = 0; i < 11; i++)
sector_buffer[i] = volume_name[i];
for (i = 0; i < 15; i++)
sector_buffer[11 + i] = dir_bytes[i];
}
uint32_t sdcard_sectors;
uint32_t sys_partition_start, sys_partition_sectors;
uint32_t fat_partition_start, fat_partition_sectors;
uint32_t sys_partition_freeze_dir;
uint16_t freeze_dir_sectors;
uint32_t sys_partition_service_dir;
uint16_t service_dir_sectors;
// Calculate clusters for file system, and FAT size
uint32_t fs_clusters = 0;
uint32_t reserved_sectors = 568; // not sure why we use this value
uint32_t rootdir_sector = 0;
uint32_t fat_sectors = 0;
uint32_t fat1_sector = 0;
uint32_t fat2_sector = 0;
uint32_t fs_data_sectors = 0;
uint8_t sectors_per_cluster = 8; // 4KB clusters
uint8_t volume_name[11] = {'M', 'E', 'G', 'A', '6', '5', 'F', 'D', 'I', 'S', 'K'};
// Work out maximum number of clusters we can accommodate
uint32_t sectors_required;
uint32_t fat_available_sectors;
void sector_buffer_write_uint16(const uint16_t offset, const uint32_t value)
{
sector_buffer[offset + 0] = (value >> 0) & 0xff;
sector_buffer[offset + 1] = (value >> 8) & 0xff;
}
void sector_buffer_write_uint32(const uint16_t offset, const uint32_t value)
{
sector_buffer[offset + 0] = (value >> 0) & 0xff;
sector_buffer[offset + 1] = (value >> 8) & 0xff;
sector_buffer[offset + 2] = (value >> 16) & 0xff;
sector_buffer[offset + 3] = (value >> 24) & 0xff;
}
uint8_t sys_part_magic[] = { 'M', 'E', 'G', 'A', '6', '5', 'S', 'Y', 'S', '0', '0' };
void build_mega65_sys_sector(const uint32_t sys_partition_sectors)
{
/*
System partition has frozen program and system service areas, including
directories for each.
We work out how many of each we can have (equal number of each), based
on the size we require them to be.
The size of each is subject to change, so is left flexible. One thing
that is not resolved, is whether to allow including a D81 image in either.
For now, we will allow 384KB RAM (including 128K "ROM" area) + 32KB colour RAM
+ 32KB IO regs (including 4KB thumbnail).
That all up means 448KB per frozen program slot. We'll just make them 512KB for
good measure.
For some services at least, we intend to allow for a substantial part of
memory to be preserved, so we need to have a mechanism that indicates what
parts of which memory areas require preservation, and whether IO should be
preserved or not.
Simple apporach is KB start and end ranges for the four regions = 8 bytes.
This can go in the directory entries, which have 64 bytes for information.
We will also likely put the hardware/access permission flags in there
somewhere, too.
Anyway, so we need to divide the available space by (512KB + 128 bytes).
Two types of area means simplest approach with equal slots for both means
dividing space by (512KB + 128 bytes)*2= ~1025KB.
*/
uint16_t i;
uint32_t slot_size = 512UL * 1024UL / 512UL; // slot_size units is sectors
// Take 1MB from partition size, for reserved space when
// calculating what can fit.
uint32_t reserved_sectors = 1024UL * 1024UL / 512UL;
uint32_t slot_count = (sys_partition_sectors - reserved_sectors) / (slot_size * 2 + 1);
uint16_t dir_size;
// Limit number of freeze slots to 16 bit counters
if (slot_count >= 0xffff)
slot_count = 0xffff;
dir_size = 1 + (slot_count / 4);
freeze_dir_sectors = dir_size;
service_dir_sectors = dir_size;
// Freeze directory begins at 1MB
sys_partition_freeze_dir = reserved_sectors;
// System service directory begins after that
sys_partition_service_dir = sys_partition_freeze_dir + slot_size * slot_count;
#ifdef __CC65__
write_line(" Freeze and OS Service slots.", 0);
screen_decimal(screen_line_address - 79, slot_count);
#else
fprintf(stdout, " %5d Freeze and OS Service slots\n", slot_count);
#endif
// Clear sector
clear_sector_buffer();
// Write magic bytes
for (i = 0; i < 11; i++)
sector_buffer[i] = sys_part_magic[i];
// $010-$013 = Start of freeze program area
sector_buffer_write_uint32(0x10, 0);
// $014-$017 = Size of freeze program area
sector_buffer_write_uint32(0x14, slot_size * slot_count + dir_size);
// $018-$01b = Size of each freeze program slot
sector_buffer_write_uint32(0x18, slot_size);
// $01c-$01d = Number of freeze slots
sector_buffer_write_uint16(0x1c, slot_count);
// $01e-$01f = Number of sectors in freeze slot directory
sector_buffer_write_uint16(0x1e, dir_size);
// $020-$023 = Start of freeze program area
sector_buffer_write_uint32(0x20, slot_size * slot_count + dir_size);
// $024-$027 = Size of service program area
sector_buffer_write_uint32(0x24, slot_size * slot_count + dir_size);
// $028-$02b = Size of each service slot
sector_buffer_write_uint32(0x28, slot_size);
// $02c-$02d = Number of service slots
sector_buffer_write_uint16(0x2c, slot_count);
// $02e-$02f = Number of sectors in service slot directory
sector_buffer_write_uint16(0x2e, dir_size);
// Now make sector numbers relative to start of disk for later use
sys_partition_freeze_dir += sys_partition_start;
sys_partition_service_dir += sys_partition_start;
return;
}
void build_mega65_sys_config_sector(void)
{
/*
Create default valid system configuration sector
*/
// Clear sector
clear_sector_buffer();
// Structure version bytes
sector_buffer[0x000] = 0x01;
sector_buffer[0x001] = 0x01;
// As we keep the sector filled with zeros (excpect below values),
// the following defaults will result:
// PAL video mode with audio, default audio settings, CRT emulation off,
// SD card for floppies, 1351 mouse mode, force reset to onboarding,
// empty default disk image (results in MEGA65.D81 mounted as default disk
// if present)
// Generate a random MAC address if no valid one is already present
sector_buffer[0x006] = (get_random_byte() & 0xfe) | 0x02;
sector_buffer[0x007] = get_random_byte();
sector_buffer[0x008] = get_random_byte();
sector_buffer[0x009] = get_random_byte();
sector_buffer[0x00A] = get_random_byte();
sector_buffer[0x00B] = get_random_byte();
// Keep empty default disk image, this will default to auto-boot mega65.d81, anyway
#if 0
// Set name of default disk image
#ifdef __CC65__
lcopy((unsigned long)"mega65.d81", (unsigned long)§or_buffer[0x10], 10);
#else
bcopy("mega65.d81", §or_buffer[0x10], 10);
#endif
#endif
// DMAgic to new version (F011B) by default
sector_buffer[0x020] = 0x01;
// SID 8580 by default
sector_buffer[0x022] = 0x01;
return;
}
void show_partition_entry(const char i)
{
char j;
#ifdef __CC65__
char report[80] = "$$* : Start=%%%/%%/%%%% or $$$$$$$$ / End=%%%/%%/%%%% or $$$$$$$$";
#endif
int offset = 0x1be + (i << 4);
char active = sector_buffer[offset + 0];
char shead = sector_buffer[offset + 1];
char ssector = sector_buffer[offset + 2] & 0x1f;
int scylinder = ((sector_buffer[offset + 2] << 2) & 0x300) + sector_buffer[offset + 3];
char id = sector_buffer[offset + 4];
char ehead = sector_buffer[offset + 5];
char esector = sector_buffer[offset + 6] & 0x1f;
int ecylinder = ((sector_buffer[offset + 6] << 2) & 0x300) + sector_buffer[offset + 7];
uint32_t lba_start, lba_end;
for (j = 0; j < 4; j++)
((char *)&lba_start)[j] = sector_buffer[offset + 8 + j];
for (j = 0; j < 4; j++)
((char *)&lba_end)[j] = sector_buffer[offset + 12 + j];
#ifdef __CC65__
format_hex((int)report + 0, id, 2);
if (!(active & 0x80))
report[2] = ' '; // not active
format_decimal((int)report + 12, shead, 3);
format_decimal((int)report + 16, ssector, 2);
format_decimal((int)report + 19, scylinder, 4);
format_hex((int)report + 27, lba_start, 8);
format_decimal((int)report + 42, ehead, 3);
format_decimal((int)report + 46, esector, 2);
format_decimal((int)report + 49, ecylinder, 4);
format_hex((int)report + 57, lba_end, 8);
write_line(report, 2);
#else
printf("%02X%c : Start=%3d/%2d/%4d or %08X / End=%3d/%2d/%4d or %08X\n", id, active & 80 ? '*' : ' ', shead, ssector,
scylinder, lba_start, ehead, esector, ecylinder, lba_end);
#endif
}
void show_mbr(void)
{
char i;
sdcard_readsector(0);
write_line("", 0);
if ((sector_buffer[0x1fe] != 0x55) || (sector_buffer[0x1ff] != 0xAA))
write_line("Current partition table is invalid.", 2);
else {
write_line("Current partition table:", 2);
for (i = 0; i < 4; i++) {
show_partition_entry(i);
}
}
}
char buffer[80];
unsigned char file_count;
unsigned long file_offset, next_offset, file_len, first_sector;
char eightthree[8 + 3 + 1];
typedef struct {
int model_id;
uint8_t slot_mb;
} mega_models_t;
// clang-format off
mega_models_t mega_models[] = {
{ 0x01, 8 },
{ 0x02, 4 },
{ 0x03, 8 },
{ 0x04, 8 },
{ 0x05, 8 },
{ 0x06, 8 },
{ 0x21, 4 },
{ 0x22, 4 },
{ 0x40, 4 },
{ 0x41, 4 },
{ 0x42, 4 },
{ 0x60, 4},
{ 0x61, 8},
{ 0x62, 8},
{ 0xFD, 4 },
{ 0xFE, 8 },
{ 0x00, 0 }
};
// clang-format on
void scan_slots(void)
{
unsigned char i, j, k;
// for non CC65 compile (why? for what?) we stick to the default 8MB defined at top
#ifdef __CC65__
hardware_model_id = PEEK(0xD629);
for (i = 0; mega_models[i].model_id; i++)
if (hardware_model_id == mega_models[i].model_id)
slot_size = mega_models[i].slot_mb * 0x100000UL;
#endif
for (i = 0; i < MAX_SLOT; i++) {
mega65slot[i].version[0] = 0;
mega65slot[i].file_count = 0;
mega65slot[i].file_offset = 0;
flash_read512bytes(slot_size * (uint32_t)i);
#ifdef __CC65__
lcopy(0xffd6e00L, (long)sector_buffer, 512);
#endif
for (j = 0; j < 16; j++)
if (slot_magic[j] != sector_buffer[j])
break;
if (j < 16) continue;
for (j = 0; j < 6; j++)
if (slot_magic[j] != sector_buffer[16 + j]) // check MEGA65 slot
break;
if (j < 6) continue;
for (j = 0; j < 32; j++)
mega65slot[i].version[j] = sector_buffer[48 + j];
mega65slot[i].version[j] = 0;
for (j--; mega65slot[i].version[j] == ' ' && j > 0 ; j--);
mega65slot[i].file_count = sector_buffer[0x72];
#ifdef __CC65__
mega65slot[i].file_offset = i * slot_size + *(unsigned long *)§or_buffer[0x73];
#else
mega65slot[i].file_offset = i * slot_size + *(unsigned int *)§or_buffer[0x73];
#endif
// detect duplicate slots and hide them
if (i > 1) {
for (j = 0; j < i; j++) {
if (!mega65slot[j].file_count)
continue;
if (mega65slot[i].file_count != mega65slot[j].file_count)
continue;
for (k = 0; k < 32 && mega65slot[i].version[k] == mega65slot[j].version[k]; k++);
if (k < 32)
continue;
mega65slot[i].file_count = 0;
mega65slot[i].file_offset = 0;
break;
}
}
}
}
char populate_file_system(unsigned char slot)
{
unsigned char i, j, k;
char *pos;
if (!mega65slot[slot].version[0] || !mega65slot[slot].file_count)
return 1;
strcpy(buffer, "Using files embedded in slot @");
pos = strchr(buffer, '@');
*pos = 0x30 + slot;
write_line(buffer, 1);
file_offset = mega65slot[slot].file_offset;
file_count = mega65slot[slot].file_count;
write_line(" Files in Core, starting at $ .", 1);
#ifdef __CC65__
format_decimal(screen_line_address - 79, file_count, 2);
screen_hex(screen_line_address - 48, file_offset);
#endif
for (i = 0; i < file_count; i++) {
flash_read512bytes(file_offset);
#ifdef __CC65__
next_offset = slot * slot_size + *(unsigned long *)§or_buffer[0];
file_len = *(unsigned long *)§or_buffer[4];
#else
next_offset = slot * slot_size + *(unsigned int *)§or_buffer[0];
file_len = *(unsigned int *)§or_buffer[4];
#endif
write_line("Pre-populating file ", 1);
for (j = 0; sector_buffer[8 + j]; j++)
#ifdef __CC65__
lpoke(screen_line_address - 59 + j, sector_buffer[8 + j]);
#else
printf("%c", sector_buffer[8+j]);
printf("\n");
#endif
#ifdef __CC65__
recolour_last_line(8);
#endif
// Prepare "EIGHT THR" formatted DOS filename for fat32_create_contiguous_file
for (j = 0; j < 11; j++)
eightthree[j] = ' ';
eightthree[11] = 0;
k = 0;
for (j = 0; sector_buffer[8 + j]; j++) {
if (sector_buffer[8 + j] == '.')
k = 8;
else
eightthree[k++] = sector_buffer[8 + j];
if (k >= 11)
break;
}
if (!strcmp((char *)§or_buffer[8], "MEGA65.ROM"))
have_rom = 1;
// Skip header
file_offset += 4 + 4 + 32;
first_sector = fat32_create_contiguous_file(eightthree, file_len, fat_partition_start + rootdir_sector,
fat_partition_start + fat1_sector, fat_partition_start + fat2_sector);
if (first_sector) {
// Write out file sectors
unsigned long addr;
for (addr = 0; addr <= file_len; addr += 512) {
POKE(0xD020, PEEK(0xD020) + 1);
flash_read512bytes(file_offset + addr);
sdcard_writesector(first_sector++);
}
#ifdef __CC65__
recolour_last_line(1);
#endif
}
else {
write_line("!! Error writing file", 1);
#ifdef __CC65__
recolour_last_line(2);
#endif
}
file_offset = next_offset;
}
return 0;
}
#ifdef __CC65__
void main(void)
#else
int DIRTYMOCK(main)(int argc, char **argv)
#endif
{
unsigned char key = '0', cardSlot = 0, slotAvail = 0;
rescanSlots:
#ifdef __CC65__
mega65_fast();
setup_screen();
#endif
next_card:
slotAvail = 0;
sdcard_select(0);
sdcard_open();
// Memory map the SD card sector buffer on MEGA65
sdcard_map_sector_buffer();
write_line("Detecting SD card(s) (can take a while)", 1);
write_line("", 0);
write_line("SD Card 0 (Internal SD slot):", 1);
#ifdef __CC65__
recolour_last_line(0x2c);
#endif
sdcard_select(0);
if (sdcard_reset()) {
write_line("No card detected on bus 0", 2);
#ifdef __CC65__
recolour_last_line(8);
#endif
}
else {
sdcard_sectors = sdcard_getsize();
// Report speed of SD card
sdcard_readspeed_test();
// Show summary of current MBR
show_mbr();
slotAvail |= 1;
}
write_line("", 0);
write_line("SD Card 1 (External microSD slot):", 1);
#ifdef __CC65__
recolour_last_line(0x2c);
#endif
sdcard_select(1);
if (sdcard_reset()) {
write_line("No card detected on bus 1", 2);
#ifdef __CC65__
recolour_last_line(8);
#endif
}
else {
sdcard_sectors = sdcard_getsize();
// Report speed of SD card
sdcard_readspeed_test();
// Show summary of current MBR
show_mbr();
slotAvail |= 2;
}
write_line("", 0);
// Make user select SD card
POKE(0xd020, 6);
strcpy(buffer, "Please select SD card to modify or r to rescan (");
if (slotAvail&1)
strcat(buffer, "0/");
if (slotAvail&2)
strcat(buffer, "1/");
strcat(buffer, "r): ");
write_line(buffer, 1);
#ifdef __CC65__
recolour_last_line(7);
do {
key = mega65_getkey();
} while (key != 'r' && (!(slotAvail&1) || key != '0') && (!(slotAvail&2) || key != '1'));
if (key == 'r')
goto rescanSlots;
cardSlot = key & 1;
sdcard_select(cardSlot);
#else
if (key == 'r')
goto rescanSlots;
#endif
// Then make sure we have correct information for the selected card
open_sdcard_and_retrieve_details();
#ifndef __CC65__
printf("Type DELETE EVERYTHING to delete everything on %s SD.\n", cardSlot&1 ? "external" : "internal");
char line[1024];
fgets(line, 1024, stdin);
while (line[0] && line[strlen(line) - 1] == '\n')
line[strlen(line) - 1] = 0;
while (line[0] && line[strlen(line) - 1] == '\r')
line[strlen(line) - 1] = 0;
if (strcmp(line, "DELETE EVERYTHING")) {
fprintf(stderr, "String did not match -- aborting.\n");
exit(-1);
}
fprintf(stderr, "Creating File System with %u (0x%x) CLUSTERS, %d SECTORS PER FAT, %d RESERVED SECTORS.\r\n", fs_clusters,
fs_clusters, fat_sectors, reserved_sectors);
#else
write_line("", 0);
strcpy(buffer, "Format ");
strcat(buffer, cardSlot&1 ? "external" : "internal");
strcat(buffer, " Card with new partition table and FAT32 file system?");
write_line(buffer, 1);
recolour_last_line(7);
{
char col = 6;
int megs = (fat_partition_sectors + 1) / 2048;
screen_decimal(screen_line_address + 2, megs);
if (megs < 10000)
col = 5;
if (megs < 1000)
col = 4;
if (megs < 100)
col = 3;
if (megs < 10)
col = 2;
write_line("MiB VFAT32 Data Partition @ $$$$$$$$:", 2 + col);
screen_hex(screen_line_address - 80 + 28 + 2 + col, fat_partition_start);
}
write_line(" $ Clusters, Sectors/FAT, Reserved Sectors.", 0);
screen_hex(screen_line_address - 80 + 3, fs_clusters);
screen_decimal(screen_line_address - 80 + 22, fat_sectors);
screen_decimal(screen_line_address - 80 + 41, reserved_sectors);
{
char col = 6;
int megs = (sys_partition_sectors + 1) / 2048;
screen_decimal(2 + screen_line_address, megs);
if (megs < 10000)
col = 5;
if (megs < 1000)
col = 4;
if (megs < 100)
col = 3;
if (megs < 10)
col = 2;
write_line("MiB MEGA65 System Partition @ $$$$$$$$:", 2 + col);
screen_hex(screen_line_address - 80 + 30 + 2 + col, sys_partition_start);
}
// multisector_write_test();
while (1) {
unsigned char len;
if (!dont_confirm) {
write_line("", 0);
strcpy(buffer, "Type DELETE EVERYTHING to continue formatting the ");
strcat(buffer, cardSlot&1 ? "external" : "internal");
strcat(buffer, " SD");
write_line(buffer, 1);
recolour_last_line(2);
write_line("or type FIX MBR to re-write MBR:", 1);
recolour_last_line(2);
screen_line_address++;
len = read_line(buffer, 79);
screen_line_address--;
if (len) {
write_line(buffer, 1);
recolour_last_line(7);
}
}
if (!strcmp("FIX MBR", buffer)) {
build_mbr(sys_partition_start, sys_partition_sectors, fat_partition_start, fat_partition_sectors);
sdcard_writesector(0);
show_mbr();
write_line("MBR Re-written", 0);
while (1)
continue;
}
else if (!strcmp("FOLTERLOS MODUS BITTE", buffer)) {
// Delete cards REPEATEDLY
dont_confirm = 1;
break;
}
else if (strcmp("DELETE EVERYTHING", buffer) && strcmp("BATCH MODE", buffer)) {
write_line("Entered text does not match. Try again.", 1);
recolour_last_line(8);
}
else
// String matches -- so proceed
break;
}
#endif
if (format_disk() == 1)
goto next_card;
}
void open_sdcard_and_retrieve_details(void)
{
sdcard_open();
sdcard_sectors = sdcard_getsize();
sdcard_readspeed_test();
show_mbr();
// Calculate sectors for the system and FAT32 partitions.
// This is the size of the card, minus 2,048 (=0x0800) sectors.
// The system partition should be sized to be not more than 50% of
// the SD card, and probably doesn't need to be bigger than 2GB, which would
// allow 1GB for 1,024 1MB freeze images and 1,024 1MB service images.
// (note that freeze images might end up being a funny size to allow for all
// mem plus a D81 image to be saved. This is all to be determined.)
// Simple solution for now: Use 1/2 disk for system partition, or 2GiB, whichever
// is smaller.
sys_partition_sectors = (sdcard_sectors - 0x0800) >> 1;
if (sys_partition_sectors > (2 * 1024UL * (1024UL * 1024UL / 512UL)))
sys_partition_sectors = (2 * 1024UL * (1024UL * 1024UL / 512UL));
sys_partition_sectors &= 0xfffff800; // round down to nearest 1MB boundary
fat_partition_sectors = sdcard_sectors - 0x800 - sys_partition_sectors;
fat_available_sectors = fat_partition_sectors - reserved_sectors;
fs_clusters = fat_available_sectors / (sectors_per_cluster);
fat_sectors = fs_clusters / (512 / 4);
if (fs_clusters % (512 / 4))
fat_sectors++;
sectors_required = 2 * fat_sectors + ((fs_clusters - 2) * sectors_per_cluster);
while (sectors_required > fat_available_sectors) {
uint32_t excess_sectors = sectors_required - fat_available_sectors;
uint32_t delta = (excess_sectors / (1 + sectors_per_cluster));
if (delta < 1)
delta = 1;
#ifndef __CC65__
fprintf(
stderr, "%d clusters would take %d too many sectors.\r\n", fs_clusters, sectors_required - fat_available_sectors);
#endif
fs_clusters -= delta;
fat_sectors = fs_clusters / (512 / 4);
if (fs_clusters % (512 / 4))
fat_sectors++;
sectors_required = 2 * fat_sectors + ((fs_clusters - 2) * sectors_per_cluster);
}
#ifndef __CC65__
fprintf(stderr, "VFAT32 PARTITION HAS $%x SECTORS ($%x AVAILABLE)\r\n", fat_partition_sectors, fat_available_sectors);
#else
// Tell use how many sectors available for partition
write_line("", 0);
write_line("$ Sectors available for MEGA65 System partition.", 1);
screen_hex(screen_line_address - 78, sys_partition_sectors);
build_mega65_sys_sector(sys_partition_sectors);
write_line("$ Sectors available for VFAT32 partition.", 1);
screen_hex(screen_line_address - 78, fat_partition_sectors);
#endif
fat_partition_start = 0x00000800;
sys_partition_start = fat_partition_start + fat_partition_sectors;
fat1_sector = reserved_sectors;
fat2_sector = fat1_sector + fat_sectors;
rootdir_sector = fat2_sector + fat_sectors;
fs_data_sectors = fs_clusters * sectors_per_cluster;
}
int format_disk(void)
{
unsigned char key;
// MBR is always the first sector of a disk
#ifdef __CC65__
write_line("", 0);
write_line("Writing Partition Table / Master Boot Record...", 1);
#endif