-
Notifications
You must be signed in to change notification settings - Fork 8
/
media_access.c
413 lines (318 loc) · 8.67 KB
/
media_access.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
/*
//
// 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"
#include "conf.h"
#include "ui_context.h"
#include "gui_utils.h"
#include "hxcfeda.h"
#include "hardware.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"
static uint32_t last_setlbabase;
extern ui_context g_ui_ctx;
volatile unsigned short io_floppy_timeout;
static const char HXC_FW_ID[]="HxCFEDA";
#ifdef CORTEX_FW_SUPPORT
static const char CORTEX_FW_ID[]="CORTEXAD";
#endif
int setlbabase(unsigned long lba)
{
int ret;
unsigned char sector[512];
direct_access_cmd_sector * dacs;
#ifdef DEBUG
dbg_printf("setlbabase : 0x%.8X\n",lba);
#endif
dacs=(direct_access_cmd_sector *)sector;
memset(§or,0,512);
#ifdef CORTEX_FW_SUPPORT
if(g_ui_ctx.firmware_type == CORTEX_FIRMWARE)
{
strcpy(dacs->DAHEADERSIGNATURE,CORTEX_FW_ID);
}
else
{
strcpy(dacs->DAHEADERSIGNATURE,HXC_FW_ID);
}
#else
strcpy(dacs->DAHEADERSIGNATURE,HXC_FW_ID);
#endif
dacs->cmd_code=1;
dacs->parameter_0 = (unsigned char)((lba>>0)&0xFF);
dacs->parameter_1 = (unsigned char)((lba>>8)&0xFF);
dacs->parameter_2 = (unsigned char)((lba>>16)&0xFF);
dacs->parameter_3 = (unsigned char)((lba>>24)&0xFF);
dacs->parameter_4 = 0xA5;
ret = writesector( 0,(unsigned char *)§or);
return ret;
}
#if 0
int test_cmd(unsigned long lba)
{
int ret;
unsigned char sector[512];
direct_access_cmd_sector * dacs;
#ifdef DEBUG
dbg_printf("test_cmd\n");
#endif
dacs=(direct_access_cmd_sector *)sector;
memset(§or,0,512);
setlbabase(0xFFFFFFFF);
readsector(1,sector,1);
hxc_printf_box(&g_ui_ctx,"<%s>",sector);
setlbabase(0);
memset(§or,0,512);
#ifdef CORTEX_FW_SUPPORT
if(g_ui_ctx.firmware_type == CORTEX_FIRMWARE)
{
strcpy(dacs->DAHEADERSIGNATURE,CORTEX_FW_ID);
}
else
{
strcpy(dacs->DAHEADERSIGNATURE,HXC_FW_ID);
}
#else
strcpy(dacs->DAHEADERSIGNATURE,HXC_FW_ID);
#endif
dacs->cmd_code = 10;
strcpy((char*)&dacs->parameter_0,"WB-3_1.ADF");
ret = writesector( 0,(unsigned char *)§or);
return ret;
}
#endif
int test_floppy_if()
{
int ret;
unsigned char sector[512];
direct_access_status_sector * dass;
dass=(direct_access_status_sector *)sector;
last_setlbabase = 2;
do
{
ret = setlbabase( last_setlbabase );
if( ret != ERR_NO_ERROR )
return ret;
ret = readsector(0,sector,1);
if( ret != ERR_NO_ERROR )
return ret;
#ifdef DEBUG
dbg_printf("test_floppy_if : %.8X = %.8X ?\n",last_setlbabase,L_INDIAN(dass->lba_base));
#endif
if(last_setlbabase!=L_INDIAN(dass->lba_base))
{
return -ERR_LBA_CHANGE_FAILURE;
}
last_setlbabase--;
}while(last_setlbabase);
return ERR_NO_ERROR;
}
int media_init()
{
int ret;
unsigned char sector[512];
direct_access_status_sector * dass;
int i,count;
#ifdef DEBUG
dbg_printf("media_init\n");
#endif
last_setlbabase=0xFFFFF000;
ret = readsector(0,(unsigned char*)§or,1);
g_ui_ctx.firmware_type = INVALID_FIRMWARE;
if(ret == ERR_NO_ERROR)
{
dass = (direct_access_status_sector *)sector;
if(!strcmp(dass->DAHEADERSIGNATURE,HXC_FW_ID))
{
g_ui_ctx.firmware_type = HXC_LEGACY_FIRMWARE;
i = 0;
count = 0;
if(dass->FIRMWAREVERSION[0] != 'v' && dass->FIRMWAREVERSION[0] != 'V')
{
g_ui_ctx.firmware_type = HXC_CLONE_FIRMWARE;
}
while(dass->FIRMWAREVERSION[i] && i < sizeof(dass->FIRMWAREVERSION))
{
if(dass->FIRMWAREVERSION[i] == '.')
count++;
i++;
}
if(count != 3)
g_ui_ctx.firmware_type = HXC_CLONE_FIRMWARE;
}
#ifdef CORTEX_FW_SUPPORT
if(!strncmp(dass->DAHEADERSIGNATURE,CORTEX_FW_ID,strlen(CORTEX_FW_ID)))
{
g_ui_ctx.firmware_type = CORTEX_FIRMWARE;
}
#endif
if( g_ui_ctx.firmware_type != INVALID_FIRMWARE )
{
strncpy(g_ui_ctx.FIRMWAREVERSION,dass->FIRMWAREVERSION,sizeof(g_ui_ctx.FIRMWAREVERSION));
hxc_printf(&g_ui_ctx,LEFT_ALIGNED|INVERTED,0, g_ui_ctx.screen_txt_ysize - 1,"FW %s",g_ui_ctx.FIRMWAREVERSION);
ret = test_floppy_if();
if( ret != ERR_NO_ERROR)
return ret;
dass = (direct_access_status_sector *)sector;
last_setlbabase=0;
ret = setlbabase(last_setlbabase);
if( ret != ERR_NO_ERROR )
return ret;
#ifdef DEBUG
dbg_printf("media_init : HxC FE Found\n");
#endif
return ERR_NO_ERROR;
}
#ifdef DEBUG
dbg_printf("media_init : HxC FE not detected\n");
#endif
return -ERR_BAD_DRIVE_ID;
}
#ifdef DEBUG
dbg_printf("media_init : Media access error\n");
#endif
return ret;
}
int media_access_init(int drive)
{
int ret;
io_floppy_timeout = 0;
ret = init_fdc(drive);
if( ret != ERR_NO_ERROR )
return ret;
#ifdef DEBUG
dbg_printf("init_fdc Done\n");
#endif
ret = media_init();
if( ret == ERR_NO_ERROR )
{
#ifdef DEBUG
dbg_printf("media_init done\n");
#endif
// Initialise File IO Library
fl_init();
#ifdef DEBUG
dbg_printf("fl_init done\n");
#endif
/* Attach media access functions to library*/
if (fl_attach_media(media_read, media_write) != FAT_INIT_OK)
{
return -ERR_MEDIA_ATTACH;
}
#ifdef DEBUG
dbg_printf("fl_attach_media done\n");
#endif
return ERR_NO_ERROR;
}
return ret;
}
int media_read(uint32 sector, uint8 *buffer, uint32 sector_count)
{
int ret;
uint32 i;
direct_access_status_sector * dass;
dass = (direct_access_status_sector *)buffer;
#ifdef DEBUG
dbg_printf("media_read sector : 0x%.8X, cnt : %d \n",sector,sector_count);
#endif
set_char_pos(&g_ui_ctx,g_ui_ctx.screen_txt_xsize - 1, 0);
print_char(&g_ui_ctx, 10, INVERTED);
for(i=0;i<sector_count;i++)
{
do
{
if((sector-last_setlbabase)>=8)
{
ret = setlbabase(sector);
if( ret != ERR_NO_ERROR )
return 0;
}
ret = readsector(0,buffer,0);
if( ret != ERR_NO_ERROR )
{
hxc_printf_box(&g_ui_ctx,"ERROR: Read ERROR ! fsector %d [Err %d]",(sector-last_setlbabase)+1,ret);
return 0;
}
last_setlbabase = L_INDIAN(dass->lba_base);
}while((sector-L_INDIAN(dass->lba_base))>=8);
ret = readsector((unsigned char)((sector-last_setlbabase)+1),&buffer[i*512],0);
if( ret != ERR_NO_ERROR )
{
hxc_printf_box(&g_ui_ctx,"ERROR: Read ERROR ! fsector %d [Err %d]",(sector-last_setlbabase)+1,ret);
return 0;
}
sector++;
#ifdef DEBUG
print_hex_array(buffer,512);
#endif
}
set_char_pos(&g_ui_ctx,g_ui_ctx.screen_txt_xsize - 1, 0);
print_char(&g_ui_ctx, ' ', INVERTED);
return 1;
}
int media_write(uint32 sector, uint8 *buffer, uint32 sector_count)
{
int ret;
uint32 i;
#ifdef DEBUG
dbg_printf("media_write : 0x%.8X\n",sector);
#endif
set_char_pos(&g_ui_ctx,g_ui_ctx.screen_txt_xsize - 1, 0);
print_char(&g_ui_ctx, 1, INVERTED);
for(i=0;i<sector_count;i++)
{
if( sector - last_setlbabase >=8)
{
last_setlbabase=sector;
ret = setlbabase(sector);
if( ret != ERR_NO_ERROR )
return 0;
}
ret = writesector((unsigned char)((sector-last_setlbabase)+1),buffer);
if( ret != ERR_NO_ERROR )
{
hxc_printf_box(&g_ui_ctx,"ERROR: Write sector ERROR !");
return 0;
}
sector++;
}
set_char_pos(&g_ui_ctx,g_ui_ctx.screen_txt_xsize - 1, 0);
print_char(&g_ui_ctx, ' ', INVERTED);
#ifdef DEBUG
print_hex_array(buffer,512);
#endif
return 1;
}