-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig_file.c
834 lines (666 loc) · 21.5 KB
/
config_file.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
/*
//
// Copyright (C) 2009-2023 Jean-François DEL NERO
//
// This file is part of the HxCFloppyEmulator file selector.
//
// HxCFloppyEmulator file selector may be used and distributed without restriction
// provided that this copyright statement is not removed from the file and that any
// derivative work contains the original copyright notice and the associated
// disclaimer.
//
// HxCFloppyEmulator file selector is free software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// HxCFloppyEmulator file selector is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with HxCFloppyEmulator file selector; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "keysfunc_defs.h"
#include "cfg_file.h"
#ifdef CORTEX_FW_SUPPORT
#include "cortex_cfg_file.h"
#endif
#include "conf.h"
#include "ui_context.h"
#include "gui_utils.h"
#include "hxcfeda.h"
#include "hal.h"
#include "fat_opts.h"
#include "fat_misc.h"
#include "fat_defs.h"
#include "fat_filelib.h"
#include "fectrl.h"
#include "media_access.h"
#include "errors_def.h"
#ifdef TEST_FLOPPY_IO
static unsigned char tbuffer2[8*1024];
static unsigned char tbuffer[16*1024];
#endif
extern disk_in_drive_v2 disks_slots[MAX_NUMBER_OF_SLOT];
unsigned char cfgfile_header[512];
FL_FILE * cfg_file_handle;
void setcfg_backgroundcolor(int color)
{
cfgfile * cfgfile_ptr;
cfgfile_ptr=(cfgfile * )cfgfile_header;
cfgfile_ptr->background_color = color;
}
int getcfg_backgroundcolor()
{
cfgfile * cfgfile_ptr;
cfgfile_ptr=(cfgfile * )cfgfile_header;
return cfgfile_ptr->background_color;
}
void setcfg_fontselection(int font_id)
{
cfgfile * cfgfile_ptr;
cfgfile_ptr=(cfgfile * )cfgfile_header;
cfgfile_ptr->font_selection = font_id;
}
int getcfg_fontselection()
{
cfgfile * cfgfile_ptr;
cfgfile_ptr=(cfgfile * )cfgfile_header;
return cfgfile_ptr->font_selection;
}
#ifdef TEST_FLOPPY_IO
void test_access(ui_context * ctx, int i)
{
int file_cfg_size,j,k;
hxc_printf_box(ctx,"Cycle %d",i);
for(j=0;j<4*1024;j++)
{
tbuffer2[j] = j + i;
}
for(j=0;j<4*1024;j++)
{
tbuffer2[(4*1024)+j] = ((j + i) ^ 0xA5);
}
cfg_file_handle = fl_fopen("/TEST.BIN", "r");
if (cfg_file_handle)
{
fl_fseek( cfg_file_handle, 0, SEEK_END );
file_cfg_size = fl_ftell( cfg_file_handle );
fl_fseek( cfg_file_handle, 0, SEEK_SET );
if (fl_fswrite((unsigned char*)&tbuffer2, 16,0, cfg_file_handle) != 1)
{
#ifdef DEBUG
dbg_printf("fl_fswrite error : header %d !\n",0);
#endif
}
fl_fclose(cfg_file_handle);
}
cfg_file_handle = fl_fopen("/HXCSDFE.CFG", "r");
if (cfg_file_handle)
{
fl_fclose(cfg_file_handle);
}
memset(tbuffer,0,8*1024);
cfg_file_handle = fl_fopen("/TEST.BIN", "r");
if (cfg_file_handle)
{
fl_fseek( cfg_file_handle, 0, SEEK_END );
file_cfg_size = fl_ftell( cfg_file_handle );
fl_fseek( cfg_file_handle, 0, SEEK_SET );
if (fl_fsread((unsigned char*)&tbuffer, 16,0, cfg_file_handle) != 1)
{
#ifdef DEBUG
dbg_printf("fl_fswrite error : header %d !\n",0);
#endif
}
for(j=0;j<8*1024;j++)
{
if( tbuffer2[j] != tbuffer[j] )
{
hxc_printf_box(ctx,"Cycle %d - ERROR [%.4X] %.2X != %.2X",i,j,tbuffer2[j],tbuffer[j]);
for(;;);
}
}
fl_fclose(cfg_file_handle);
}
/* for(j=0;j<8*1024;j++)
{
if( tbuffer2[j] != tbuffer[j] )
hxc_printf_box(ctx,"Cycle %d - ERROR [%.4X] %.2X != %.2X",i,j,tbuffer2[j],tbuffer[j]);
//buffer2[j] = j + i;
}*/
}
#endif
int read_hxc_cfg_file(ui_context * ctx,unsigned char * cfgfile_header)
{
int ret;
unsigned short number_of_slots;
unsigned short i,d,slot_offset;
short sector_offset,last_sector_offset;
int file_cfg_size;
unsigned char temp_sector[512];
disk_in_drive * disk;
cfgfile * cfgfile_ptr;
#ifdef DEBUG
dbg_printf("enter read_cfg_file\n");
#endif
memset((void*)&disks_slots,0,sizeof(disks_slots));
ret = ERR_NO_ERROR;
cfg_file_handle = fl_fopen("/HXCSDFE.CFG", "r");
if (cfg_file_handle)
{
fl_fseek( cfg_file_handle, 0, SEEK_END );
file_cfg_size = fl_ftell( cfg_file_handle );
fl_fseek( cfg_file_handle, 0, SEEK_SET );
#ifdef DEBUG
dbg_printf("config size file : %d\n", file_cfg_size);
#endif
cfgfile_ptr=(cfgfile * )cfgfile_header;
fl_fread(cfgfile_header, 1, 512 , cfg_file_handle);
if( !strncmp(cfgfile_ptr->signature,"HXCFECFGV",9) )
{
switch(cfgfile_ptr->signature[9])
{
case '1':
#ifdef DEBUG
dbg_printf("V1 config file format\n");
#endif
ctx->cfg_file_format_version = 1;
ctx->number_of_drive = 2;
ctx->config_file_number_max_of_slot = ( ( file_cfg_size - 0x400 ) / (64 * 2) ) - 1;
if( ctx->config_file_number_max_of_slot > ( MAX_NUMBER_OF_SLOT / 2 ) )
ctx->config_file_number_max_of_slot = ( MAX_NUMBER_OF_SLOT / 2 );
number_of_slots = cfgfile_ptr->number_of_slot;
if( number_of_slots > ( MAX_NUMBER_OF_SLOT / 2 ) )
number_of_slots = ( MAX_NUMBER_OF_SLOT / 2 ) - 1;
if( number_of_slots > ctx->config_file_number_max_of_slot )
number_of_slots = (unsigned short) (ctx->config_file_number_max_of_slot - 1);
ctx->number_of_slots = number_of_slots;
fl_fseek(cfg_file_handle , 1024 , SEEK_SET);
memset(ctx->change_map,0,512);
memset(ctx->slot_map,0,512);
fl_fread(temp_sector, 1, 512 , cfg_file_handle);
i=1;
while(i<number_of_slots)
{
if(!(i&3))
{
fl_fread(temp_sector, 1, 512 , cfg_file_handle);
}
disk = (disk_in_drive *)&temp_sector[(i&3)*128];
disks_slots[i*2].attributes = disk->DirEnt.attributes;
disks_slots[i*2].firstCluster = disk->DirEnt.firstCluster;
disks_slots[i*2].size = disk->DirEnt.size;
memcpy(&disks_slots[i*2].name, disk->DirEnt.longName,17);
disks_slots[i*2].name[17] = 0;
memcpy(&disks_slots[i*2].type,&disk->DirEnt.name[8],3);
disk = (disk_in_drive *)&temp_sector[((i&3)*128)+64];
disks_slots[(i*2)+1].attributes = disk->DirEnt.attributes;
disks_slots[(i*2)+1].firstCluster = disk->DirEnt.firstCluster;
disks_slots[(i*2)+1].size = disk->DirEnt.size;
memcpy(&disks_slots[(i*2)+1].name, disk->DirEnt.longName,17);
disks_slots[(i*2)+1].name[17] = 0;
memcpy(&disks_slots[(i*2)+1].type,&disk->DirEnt.name[8],3);
ctx->slot_map[i>>3] |= (0x80 >> (i&7));
ctx->change_map[i>>3] |= (0x80 >> (i&7));
i++;
};
break;
case '2':
if( ctx->firmware_main_version < 3 )
{
#ifdef DEBUG
dbg_printf("Firmware not supporting this config file !\n");
#endif
ctx->cfg_file_format_version = 0;
ret = -ERR_CONFIG_FILE_VERSION;
return ret;
}
#ifdef DEBUG
dbg_printf("V2 config file format\n");
#endif
ctx->cfg_file_format_version = 2;
ctx->number_of_drive = ENDIAN_32BIT( cfgfile_ptr->number_of_drive_per_slot );
ctx->slots_position = ENDIAN_32BIT( cfgfile_ptr->slots_position );
ctx->config_file_number_max_of_slot = ENDIAN_32BIT( cfgfile_ptr->max_slot_number );
if( ctx->config_file_number_max_of_slot > (MAX_NUMBER_OF_SLOT / ctx->number_of_drive) )
ctx->config_file_number_max_of_slot = (MAX_NUMBER_OF_SLOT / ctx->number_of_drive);
memset(ctx->change_map,0,512);
number_of_slots = (unsigned short) ENDIAN_32BIT( cfgfile_ptr->max_slot_number );
fl_fseek(cfg_file_handle , 512 * ENDIAN_32BIT(cfgfile_ptr->slots_map_position) , SEEK_SET);
fl_fread(ctx->slot_map, 1, 512 , cfg_file_handle);
last_sector_offset = -1;
i = 0;
do
{
if( ctx->slot_map[i>>3] & (0x80 >> (i&7)) )
{
sector_offset = (short) ( ( i * 64 * ctx->number_of_drive ) / 512 );
if(last_sector_offset != sector_offset )
{
if( sector_offset - last_sector_offset != 1 )
{
fl_fseek(cfg_file_handle , 512 * (sector_offset + ENDIAN_32BIT(cfgfile_ptr->slots_position) ), SEEK_SET);
}
fl_fread(temp_sector, 1, 512 , cfg_file_handle);
last_sector_offset = sector_offset;
}
d = 0;
while( d < ctx->number_of_drive )
{
slot_offset = (unsigned short) ( ( i * 64 * ctx->number_of_drive ) + ( 64 * d ) ) % 512;
if( ( (i*ctx->number_of_drive) + d ) < MAX_NUMBER_OF_SLOT )
{
memcpy( &disks_slots[ (i*ctx->number_of_drive) + d ],
&temp_sector[slot_offset],
sizeof(disk_in_drive_v2));
}
d++;
}
}
i++;
}while(i<number_of_slots);
break;
default:
#ifdef DEBUG
dbg_printf("Unknown config file version !\n");
#endif
ctx->cfg_file_format_version = 0;
ret = -ERR_CONFIG_FILE_VERSION;
break;
}
}
else
{
ret = -ERR_CONFIG_FILE_SIGN;
}
}
else
{
ret = -ERR_CONFIG_FILE_ACCESS;
}
#ifdef DEBUG
dbg_printf("leave read_cfg_file : %d\n",ret);
#endif
return ret;
}
#ifdef CORTEX_FW_SUPPORT
int read_cortex_cfg_file(ui_context * ctx,unsigned char * cfgfile_header)
{
int ret;
unsigned short number_of_slots;
unsigned short i;
unsigned char temp_sector[512];
Cortex_disk_in_drive * cortex_disk;
Cortex_cfgfile * cfgfile_ptr;
memset((void*)&disks_slots,0,sizeof(disks_slots));
memset(ctx->change_map,0,512);
memset(ctx->slot_map,0,512);
ret = ERR_NO_ERROR;
cfg_file_handle = fl_fopen("/SELECTOR.ADF", "r");
if (cfg_file_handle)
{
cfgfile_ptr = (Cortex_cfgfile * )cfgfile_header;
fl_fseek(cfg_file_handle , 15*(512*11*2) , SEEK_SET);
fl_fread(cfgfile_header, 1, 512 , cfg_file_handle);
ctx->config_file_number_max_of_slot = MAX_NUMBER_OF_SLOT;
ctx->number_of_drive = 1;
number_of_slots = ENDIAN_16BIT(cfgfile_ptr->number_of_slot);
if( number_of_slots > ctx->config_file_number_max_of_slot )
number_of_slots = (unsigned short) (ctx->config_file_number_max_of_slot - 1);
ctx->number_of_slots = number_of_slots;
fl_fseek(cfg_file_handle , (15*(512*11*2))+(2*512) , SEEK_SET);
i=1;
fl_fread(temp_sector, 1, 512 , cfg_file_handle);
do
{
if(!(i&3))
{
fl_fread(temp_sector, 1, 512 , cfg_file_handle);
}
cortex_disk = (Cortex_disk_in_drive *)&temp_sector[(i&3)*128];
disks_slots[i].attributes = cortex_disk->DirEnt.attributes;
disks_slots[i].firstCluster = cortex_disk->DirEnt.firstCluster;
disks_slots[i].size = cortex_disk->DirEnt.size;
memcpy(&disks_slots[i].name, cortex_disk->DirEnt.longName,41);
disks_slots[i].name[41] = 0;
memcpy(&disks_slots[i].type,&cortex_disk->DirEnt.name[8],3);
ctx->slot_map[i>>3] |= (0x80 >> (i&7));
i++;
}while(i<number_of_slots);
fl_fclose(cfg_file_handle);
return ret;
}
else
{
return -ERR_CONFIG_FILE_ACCESS;
}
}
#endif
int read_cfg_file(ui_context * ctx,unsigned char * cfgfile_header)
{
if( ctx->firmware_type == HXC_LEGACY_FIRMWARE || ctx->firmware_type == HXC_CLONE_FIRMWARE )
{
hxc_printf_box(ctx,"Reading HXCSDFE.CFG ...");
return read_hxc_cfg_file(ctx,cfgfile_header);
}
else
{
#ifdef CORTEX_FW_SUPPORT
if(ctx->firmware_type == CORTEX_FIRMWARE)
{
hxc_printf_box(ctx,"Reading SELECTOR.ADF ...");
return read_cortex_cfg_file(ctx,cfgfile_header);
}
#endif
}
return -ERR_BAD_DRIVE_ID;
}
int save_hxc_cfg_file(ui_context * ctx,unsigned char * sdfecfg_file, int pre_selected_slot)
{
int ret;
unsigned char number_of_slot,slot_index;
unsigned short i,j,sect_nb;
cfgfile * cfgfile_ptr;
uint32_t floppyselectorindex;
disk_in_drive * disk;
unsigned char temp_buf[512];
#ifdef DEBUG
dbg_printf("enter save_cfg_file\n");
#endif
ret = ERR_NO_ERROR;
if (cfg_file_handle)
{
switch(ctx->cfg_file_format_version)
{
case 1:
#ifdef DEBUG
dbg_printf("V1 config file format\n");
#endif
cfgfile_ptr = (cfgfile * )sdfecfg_file;
number_of_slot = 1;
if(pre_selected_slot>=0)
slot_index = pre_selected_slot;
else
slot_index = 1;
floppyselectorindex=128; // Fisrt slot offset
memset( temp_buf,0,512); // Clear the sector
sect_nb=2; // Slots Sector offset
i = 1;
do
{
if( ctx->slot_map[i>>3] & (0x80 >> (i&7)) ) // Valid slot found
{
// Drive A
disk = (disk_in_drive *)&temp_buf[floppyselectorindex];
memset(disk,0,sizeof(disk_in_drive));
disk->DirEnt.attributes = disks_slots[i*2].attributes;
disk->DirEnt.firstCluster = disks_slots[i*2].firstCluster;
disk->DirEnt.size = disks_slots[i*2].size;
memset(disk->DirEnt.longName,0,17);
j = 0;
while( j<16 && disks_slots[i*2].name[j] )
{
disk->DirEnt.longName[j] = disks_slots[i*2].name[j];
j++;
}
memset(disk->DirEnt.name,' ',12);
disk->DirEnt.name[11] = 0;
j = 0;
while( j<8 && disks_slots[i*2].name[j] && disks_slots[i*2].name[j]!='.')
{
disk->DirEnt.name[j] = disks_slots[i*2].name[j];
j++;
}
memcpy(&disk->DirEnt.name[8],&disks_slots[i*2].type,3);
// Drive B
disk = (disk_in_drive *)&temp_buf[floppyselectorindex + 64];
memset(disk,0,sizeof(disk_in_drive));
disk->DirEnt.attributes = disks_slots[(i*2)+1].attributes;
disk->DirEnt.firstCluster = disks_slots[(i*2)+1].firstCluster;
disk->DirEnt.size = disks_slots[(i*2)+1].size;
memset(disk->DirEnt.longName,0,17);
j = 0;
while( j<16 && disks_slots[(i*2)+1].name[j] )
{
disk->DirEnt.longName[j] = disks_slots[(i*2)+1].name[j];
j++;
}
memset(disk->DirEnt.name,' ',12);
disk->DirEnt.name[11] = 0;
j = 0;
while( j<8 && disks_slots[(i*2)+1].name[j] && disks_slots[(i*2)+1].name[j]!='.')
{
disk->DirEnt.name[j] = disks_slots[(i*2)+1].name[j];
j++;
}
memcpy(&disk->DirEnt.name[8],&disks_slots[(i*2)+1].type,3);
//Next slot...
number_of_slot++;
floppyselectorindex=(floppyselectorindex+128)&0x1FF;
if(!(number_of_slot&0x3)) // Need to change to the next sector
{
// Save the sector
if (fl_fswrite((unsigned char*)temp_buf, 1,sect_nb, cfg_file_handle) != 1)
{
return -ERR_WRITE_FILE_ACCESS;
}
// Next sector
sect_nb++;
memset( temp_buf,0,512); // Clear the next sector
}
}
i++;
}while(i<ctx->config_file_number_max_of_slot);
if(number_of_slot&0x3)
{
if (fl_fswrite((unsigned char*)temp_buf, 1,sect_nb, cfg_file_handle) != 1)
{
return -ERR_WRITE_FILE_ACCESS;
}
}
if( slot_index >= number_of_slot )
{
slot_index = number_of_slot - 1;
}
fl_fseek(cfg_file_handle , 0 , SEEK_SET);
// Update the file header
cfgfile_ptr->number_of_slot = number_of_slot;
cfgfile_ptr->slot_index = slot_index;
if (fl_fswrite((unsigned char*)sdfecfg_file, 1,0, cfg_file_handle) != 1)
{
#ifdef DEBUG
dbg_printf("fl_fswrite error : header %d !\n",0);
#endif
return -ERR_WRITE_FILE_ACCESS;
}
break;
case 2:
#ifdef DEBUG
dbg_printf("V2 config file format\n");
#endif
number_of_slot = 1;
cfgfile_ptr=(cfgfile * )sdfecfg_file;
slot_index = 1;
i = 1;
do
{
if( (ctx->change_map[i>>3] & (0x80 >> (i&7))) && ( (i*ctx->number_of_drive) < MAX_NUMBER_OF_SLOT ) ) // Is the slot modified ?
{
// Yes, save the modified sector
floppyselectorindex=(64*ctx->number_of_drive)*i;
sect_nb = (unsigned short) ENDIAN_32BIT(cfgfile_ptr->slots_position) + (floppyselectorindex >> 9);
if (fl_fswrite( ((unsigned char*)&disks_slots) + (floppyselectorindex & ~0x1FF) , 1, sect_nb, cfg_file_handle) != 1)
{
#ifdef DEBUG
dbg_printf("fl_fswrite error : slot sect %d !\n",sect_nb);
#endif
return -ERR_WRITE_FILE_ACCESS;
}
// And clear the modified flags for all slots into this sector.
j = 0;
do
{
ctx->change_map[(i+j)>>3] &= ~(0x80 >> ((i+j)&7));
j++;
}while( ((64*ctx->number_of_drive)*(i+j)) & 0x1FF );
}
i++;
}while( i < ctx->config_file_number_max_of_slot );
// Update the map
if (fl_fswrite((unsigned char*)&ctx->slot_map, 1, ENDIAN_32BIT(cfgfile_ptr->slots_map_position), cfg_file_handle) != 1)
{
#ifdef DEBUG
dbg_printf("fl_fswrite error : map sect %d !\n",ENDIAN_32BIT(cfgfile_ptr->slots_map_position));
#endif
return -ERR_WRITE_FILE_ACCESS;
}
fl_fseek(cfg_file_handle , 0 , SEEK_SET);
// Update the file header
if(pre_selected_slot>=0)
cfgfile_ptr->cur_slot_number = ENDIAN_32BIT(pre_selected_slot);
else
cfgfile_ptr->cur_slot_number = ENDIAN_32BIT(1);
cfgfile_ptr->slot_index = 0;
if (fl_fswrite((unsigned char*)sdfecfg_file, 1,0, cfg_file_handle) != 1)
{
#ifdef DEBUG
dbg_printf("fl_fswrite error : header %d !\n",0);
#endif
return -ERR_WRITE_FILE_ACCESS;
}
break;
}
}
else
{
return -ERR_INVALID_HANDLER;
}
#ifdef DEBUG
dbg_printf("leave save_cfg_file\n");
#endif
return ret;
}
#ifdef CORTEX_FW_SUPPORT
int save_cortex_cfg_file(ui_context * ctx,unsigned char * sdfecfg_file, int pre_selected_slot)
{
int ret;
unsigned int number_of_slot,slot_index;
unsigned int sect_nb;
unsigned int i;
Cortex_cfgfile * cfgfile_ptr;
Cortex_disk_in_drive * cortex_disk;
unsigned int floppyselectorindex;
unsigned char writeneeded;
FL_FILE *file;
unsigned char temp_buf[512];
ret = ERR_NO_ERROR;
writeneeded = 0;
file = fl_fopen("/SELECTOR.ADF", "r");
if (file)
{
number_of_slot=1;
slot_index=1;
i=1;
memset( temp_buf,0,512); // Clear the sector
floppyselectorindex = 128; // First slot offset
sect_nb=2; // Slots Sector offset
do
{
if( ctx->slot_map[i>>3] & (0x80 >> (i&7)) ) // Valid slot found
{
// Copy it to the actual file sector
cortex_disk = (Cortex_disk_in_drive *)&temp_buf[floppyselectorindex];
cortex_disk->DirEnt.attributes = disks_slots[i].attributes;
cortex_disk->DirEnt.firstCluster = disks_slots[i].firstCluster;
cortex_disk->DirEnt.size = disks_slots[i].size;
memcpy(&cortex_disk->DirEnt.longName, &disks_slots[i].name, 40);
cortex_disk->DirEnt.longName[40] = 0;
memset(&temp_buf[floppyselectorindex+64],0,sizeof(Cortex_disk_in_drive));
if( cortex_disk->DirEnt.size )
slot_index = i;
}
else
{
memset(&temp_buf[floppyselectorindex],0,sizeof(Cortex_disk_in_drive));
memset(&temp_buf[floppyselectorindex+64],0,sizeof(Cortex_disk_in_drive));
}
if( ctx->change_map[i>>3] & (0x80 >> (i&7)) )
{
ctx->change_map[i>>3] ^= (0x80 >> (i&7));
writeneeded = 0xFF;
}
//Next slot...
number_of_slot++;
floppyselectorindex = (floppyselectorindex+128) & 0x1FF;
if((!(number_of_slot&0x3))) // Need to change to the next sector
{
if(writeneeded)
{
// Save the sector
if (fl_fswrite((unsigned char*)temp_buf, 1,330+sect_nb, file) != 1)
{
return -ERR_WRITE_FILE_ACCESS;
}
// Next sector
writeneeded = 0x00;
}
sect_nb++;
memset( temp_buf,0,512); // Clear the next sector
}
i++;
}while( i < MAX_NUMBER_OF_SLOT );
if((number_of_slot&0x3) && writeneeded )
{
writeneeded = 0x00;
if (fl_fswrite((unsigned char*)temp_buf, 1,330+sect_nb, file) != 1)
{
return -ERR_WRITE_FILE_ACCESS;
}
}
number_of_slot = slot_index + 1;
fl_fseek(file , 0 , SEEK_SET);
// Update the file header
if(pre_selected_slot>=0)
slot_index = pre_selected_slot;
else
slot_index = 1;
cfgfile_ptr = (Cortex_cfgfile * )cfgfile_header;
cfgfile_ptr->number_of_slot = ENDIAN_16BIT(number_of_slot);
cfgfile_ptr->slot_index = ENDIAN_16BIT(slot_index);
cfgfile_ptr->update_cnt = ENDIAN_16BIT( (ENDIAN_16BIT(cfgfile_ptr->update_cnt) + 1 ) );
if (fl_fswrite((unsigned char*)cfgfile_header, 1,330, file) != 1)
{
return -ERR_WRITE_FILE_ACCESS;
}
}
else
{
return -ERR_CONFIG_FILE_ACCESS;
}
// Close file
fl_fclose(file);
return ret;
}
#endif
int save_cfg_file(ui_context * ctx,unsigned char * sdfecfg_file, int pre_selected_slot)
{
if( ctx->firmware_type == HXC_LEGACY_FIRMWARE || ctx->firmware_type == HXC_CLONE_FIRMWARE )
{
return save_hxc_cfg_file(ctx,sdfecfg_file, pre_selected_slot);
}
else
{
#ifdef CORTEX_FW_SUPPORT
return save_cortex_cfg_file(ctx,sdfecfg_file, pre_selected_slot);
#endif
}
return -ERR_BAD_DRIVE_ID;
}