summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/tms320dm320/creative-zvm/ata-creativezvm.c
blob: f84d90420570aadb89ba8b6c7f1b9505518cb123 (plain)
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 by Maurus Cuelenaere
 *
 * This program 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.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"
#include "cpu.h"
#include "kernel.h"
#include "thread.h"
#include "system.h"
#include "power.h"
#include "panic.h"
#include "ata-target.h"
#include "dm320.h"
#include "ata.h"
#include "string.h"
#include "buffer.h"
#include "logf.h"

#undef ata_read_sectors
#undef ata_write_sectors

static void sleep_ms(int ms)
{
    sleep(ms*HZ/1000);
}

void ide_power_enable(bool on)
{
/* Disabled until figured out what's wrong */
#if 0
    int old_level = disable_irq_save();
    if(on)
    {
        IO_GIO_BITSET0 = (1 << 14);
        ata_reset();
    }
    else
        IO_GIO_BITCLR0 = (1 << 14);
    restore_irq(old_level);
#else
    (void)on;
#endif
}

inline bool ide_powered()
{
#if 0
    return (IO_GIO_BITSET0 & (1 << 14));
#else
    return true;
#endif
}

void ata_reset(void)
{
    int old_level = disable_irq_save();
    if(!ide_powered())
    {
        ide_power_enable(true);
        sleep_ms(150);
    }
    else
    {
        IO_GIO_BITSET0 = (1 << 5);
        IO_GIO_BITCLR0 = (1 << 3);
        sleep_ms(1);
    }
    IO_GIO_BITCLR0 = (1 << 5);
    sleep_ms(10);
    IO_GIO_BITSET0 = (1 << 3);
    while(!(ATA_COMMAND & STATUS_RDY))
        sleep_ms(10);
    restore_irq(old_level);
}

void ata_enable(bool on)
{
    (void)on;
    return;
}

bool ata_is_coldstart(void)
{
    return true;
}

void ata_device_init(void)
{
    IO_INTC_EINT1 |= INTR_EINT1_EXT2;   /* enable GIO2 interrupt */
    /* TODO: mimic OF inits... */
    return;
}

void GIO2(void)
{
#ifdef DEBUG
    logf("GIO2 interrupt...");
#endif
    IO_INTC_IRQ1 = INTR_IRQ1_EXT2; /* Mask GIO2 interrupt */
    return;
}

/*
 ---------------------------------------------------------------------------
 CreativeFileSystem parsing/handling code
 ---------------------------------------------------------------------------
 */

#define VFAT_SECTOR_SIZE(x) ( (x)/0x8000 ) /* 1GB array requires 80kB of RAM */

extern int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int count, void* buf);
extern int ata_write_sectors(IF_MD2(int drive,) unsigned long start, int count, const void* buf);

struct main_header
{
    char mblk[4];
    unsigned int sector_size;
    long long disk_size;
    struct partition_header
    {
        unsigned long end;
        unsigned long start;
        char name[8];
    } partitions[31];
};

struct cfs_header
{
    unsigned int unk;
    unsigned int unk2;
    unsigned int sector_size;
    unsigned int unk4;
    unsigned int unk5;
    char identifier[4];
    unsigned int first_inode;
    unsigned int unk8;
    unsigned int unk9;
    unsigned int unk10;
    unsigned int unk11;
};

struct cfs_inode
{
    unsigned char magic[4];
    unsigned int number;
    unsigned int parent;
    unsigned int unk;
    unsigned int type;
    unsigned int created_time;
    unsigned int lastmodified_time;
    unsigned int unk2;
    unsigned int first_class_chain[12];
    unsigned int unk3;
    unsigned int unk4;
    unsigned int second_class_chain_first_cluster;
    unsigned int unk9;
    unsigned int unk10;
    unsigned int second_class_chain_second_cluster;
    unsigned int unk11;
    unsigned int unk12;
    unsigned int unk13;
    unsigned int filesize;
    unsigned int serial_number;
    unsigned int number_of_metadata_records;
};

struct cfs_direntry
{
    unsigned char identifier[4];
    unsigned int unk;
    unsigned int items;
    unsigned int unk2;
    unsigned char maxlen[2];
    unsigned char padding[202];
    /* struct cfs_direntry_item _items[items]; */
};
struct cfs_direntry_item
{
    unsigned int inode_number;
    unsigned short strlen;
    unsigned short bytesperchar;
    char string[32];
};

static bool cfs_inited = false;
static unsigned long cfs_start;
static unsigned long *sectors;

#define CFS_START              ( ((hdr->partitions[1].start*hdr->sector_size) & ~0xFFFF) + 0x10000 )
#define CFS_CLUSTER2CLUSTER(x) ( (CFS_START/512)+((x)-1)*64 )

/* Limited version of UCS -> ASCII */
static char* ucs2letostring(unsigned char* s)
{
    static char res[256];
    int i;

    for(i=0; (s[i] == 0 && s[i+1] == 0 && i<256); i++)
        res[i] = s[i*2];

    return (char*)&res;
}

static void cfs_init(void)
{
    struct main_header *hdr;
    struct cfs_header *cfs;
    struct cfs_inode *root_inode, *vfat_inode, *inode;
    struct cfs_direntry *root_direntry, *vfat_direntry;
    struct cfs_direntry_item *root_direntry_items, *vfat_direntry_items;
    unsigned int i, j, k, vfat_inode_nr=0, vfat_inodes_nr[10], vfat_sector_count;
    unsigned char sector[512];
    static unsigned int vfat_data[2][0x8000];
    static unsigned char sector2[0x8000];

    if(cfs_inited)
        return;

    /* Read MBLK */
    _ata_read_sectors(0, 1, &sector);
    hdr = (struct main_header*)&sector;

    logf("CFS is at 0x%x [0x%x]", CFS_START, CFS_START/512);

    /* Read CFS header */
    _ata_read_sectors(CFS_START/512, 64, &sector2);
    cfs = (struct cfs_header*)&sector2;

    logf("First inode = 0x%x", cfs->first_inode);

    /* Read root inode */
    _ata_read_sectors(CFS_CLUSTER2CLUSTER(cfs->first_inode), 64, &sector2);
    root_inode = (struct cfs_inode*)&sector2;
    
    logf("Root inode = 0x%x", root_inode);
    
    logf("0x%x 0x%x", CFS_CLUSTER2CLUSTER(root_inode->first_class_chain[0]), root_inode->first_class_chain[0]);

    /* Read root inode's first sector */
    _ata_read_sectors(CFS_CLUSTER2CLUSTER(root_inode->first_class_chain[0]), 64, &sector2);
    root_direntry = (struct cfs_direntry*)&sector2;
    root_direntry_items = (struct cfs_direntry_item*)(&sector2+sizeof(struct cfs_direntry));

    logf("0x%x", root_direntry->identifier);

    logf("%d", root_direntry->items);

    /* Search VFAT inode */
    for(i=0; i < root_direntry->items; i++)
    {
        if(root_direntry_items[i].inode_number != 0)
        {
            //logf(" * [%s] at 0x%x", ucs2letostring(&root_direntry_items[i].string[0]), root_direntry_items[i].inode_number);
            if(strcmp(ucs2letostring(&root_direntry_items[i].string[0]), "VFAT") == 0)
                vfat_inode_nr = root_direntry_items[i].inode_number;
        }
    }
    
    logf("VFAT inode = 0x%x", vfat_inode_nr);
    
    if(vfat_inode_nr != 0)
    {
        /* Read VFAT inode */
        _ata_read_sectors(CFS_CLUSTER2CLUSTER(vfat_inode_nr), 64, &sector2);
        vfat_inode = (struct cfs_inode*)&sector2;

        /* Read VFAT inode's first sector */
        _ata_read_sectors(CFS_CLUSTER2CLUSTER(vfat_inode->first_class_chain[0]), 64, &sector2);
        vfat_direntry = (struct cfs_direntry*)&sector2;
        vfat_direntry_items = (struct cfs_direntry_item*)(&sector2+sizeof(struct cfs_direntry));

        /* Search for VFAT's subinodes */
        for(i=0; i < vfat_direntry->items; i++)
        {
            logf(" * [%s] at 0x%x\n", ucs2letostring(&vfat_direntry_items[i].string[0]), vfat_direntry_items[i].inode_number);
            if(i > 0)
                vfat_inodes_nr[i-1] = vfat_direntry_items[i].inode_number;
        }

        /* Determine size of VFAT file */
        _ata_read_sectors(CFS_CLUSTER2CLUSTER(vfat_inodes_nr[1]), 1, &sector);
        inode = (struct cfs_inode*)&sector;
#ifndef BOOTLOADER
        sectors = (unsigned long*)buffer_alloc(VFAT_SECTOR_SIZE(inode->filesize));
#else
        static unsigned long _sector[VFAT_SECTOR_SIZE(1024*1024*1024)]; /* 1GB guess */
        sectors = _sector;
#endif

        logf("VFAT file size: 0x%x", inode->filesize);

        /* Clear data sectors */
        memset(&sectors, 0, VFAT_SECTOR_SIZE(inode->filesize)*sizeof(unsigned long));

        /* Read all data sectors' addresses in memory */
        vfat_sector_count = 0;
        for(i=0; vfat_inodes_nr[i] != 0; i++)
        {
            _ata_read_sectors(CFS_CLUSTER2CLUSTER(vfat_inodes_nr[i]), 1, &sector);
            inode = (struct cfs_inode*)&sector;

            /* Read second & third class chain */
            _ata_read_sectors(CFS_CLUSTER2CLUSTER(inode->second_class_chain_first_cluster), 64, &vfat_data[0]);
            _ata_read_sectors(CFS_CLUSTER2CLUSTER(inode->second_class_chain_second_cluster), 64, &vfat_data[1]);

            /* First class chain */
            for(j=0; j<12; j++)
            {
                if( (inode->first_class_chain[j] & 0xFFFF) != 0xFFFF &&
                     inode->first_class_chain[j] != 0
                  )
                    sectors[vfat_sector_count++] = inode->first_class_chain[j];
            }

            /* Second class chain */
            for(j=0; j<0x8000/4; j++)
            {
                if( (vfat_data[0][j] & 0xFFFF) != 0xFFFF &&
                    vfat_data[0][j] != 0
                  )
                    sectors[vfat_sector_count++] = vfat_data[0][j];
            }

            /* Third class chain */
            for(j=0; j<0x8000/4; j++)
            {
                if( (vfat_data[1][j] & 0xFFFF) != 0xFFFF &&
                    vfat_data[1][j] != 0
                  )
                {
                    memset(&vfat_data[0], 0, 0x8000*sizeof(unsigned int));

                    /* Read third class subchain(s) */
                    _ata_read_sectors(CFS_CLUSTER2CLUSTER(vfat_data[1][j]), 64, &vfat_data[0]);

                    for(k=0; k<0x8000/4; k++)
                    {
                        if( (vfat_data[0][k] & 0xFFFF) != 0xFFFF &&
                            vfat_data[0][k] != 0
                          )
                            sectors[vfat_sector_count++] = vfat_data[0][k];
                    }
                }
            }
        }

        logf("Sector count: %d 0x%x", vfat_sector_count, vfat_sector_count);
    }
    else
        panicf("Cannot find virtual FAT filesystem!");

    cfs_inited = true;
}

static inline unsigned long map_sector(unsigned long sector)
{
    /*
     *  Sector mapping: start of CFS + FAT_SECTOR2CFS_SECTOR(sector) + missing part
     *  FAT works with sectors of 0x200 bytes, CFS with sectors of 0x8000 bytes.
     */
    return cfs_start+sectors[sector/64]*64+sector%64;
}

int ata_read_sectors(IF_MD2(int drive,) unsigned long start, int count, void* buf)
{
    if(!cfs_inited)
        cfs_init();

    /* Check if count is lesser than or equal to 1 native CFS sector */
    if(count <= 64)
        return _ata_read_sectors(IF_MD2(drive,) map_sector(start), count, buf);
    else
    {
        int i, ret;
        unsigned char* dest = (unsigned char*)buf;

        /* Read sectors in parts of 0x8000 */
        for(i=0; i<count; i+=64)
        {
            ret = _ata_read_sectors(IF_MD2(drive,) map_sector(start+i), (count-i >= 64 ? 64 : count-i), (void*)dest);
            if(ret != 0)
                return ret;

            dest += (count-i >= 64 ? 0x8000 : (count-i)*512);
        }

        return ret;
    }
}

int ata_write_sectors(IF_MD2(int drive,) unsigned long start, int count, const void* buf)
{
    if(!cfs_inited)
        cfs_init();

#if 0 /* Disabled for now */
    /* Check if count is lesser than or equal to 1 native CFS sector */
    if(count <= 64)
        return _ata_write_sectors(IF_MD2(drive,) map_sector(start), count, buf);
    else
    {
        int i, ret;
        unsigned char* dest = (unsigned char*)buf;

        /* Read sectors in parts of 0x8000 */
        for(i=0; i<count; i+=64)
        {
            ret = _ata_write_sectors(IF_MD2(drive,) map_sector(start+i), (count-i >= 64 ? 64 : count-i), (const void*)dest);
            if(ret != 0)
                return ret;

            dest += (count-i >= 64 ? 0x8000 : (count-i)*512);
        }

        return ret;
    }
#else
    (void)start;
    (void)count;
    (void)buf;
    return 0;
#endif
}

#ifdef BOOTLOADER

/*
 ---------------------------------------------------------------------------
 MiniFileSystem parsing code
 ---------------------------------------------------------------------------
 */

struct minifs_file
{
    char name[0x10];
    unsigned int unk;
    unsigned long size;
    unsigned int chain1;
    unsigned int chain2;
};

struct minifs_chain
{
    unsigned int unknown;
    unsigned short chain[0x27FE];
    unsigned int unknown2;
    unsigned long length;
};


#define DIR_BITMAP_START       0x0143
#define DIR_START              0x0144
#define DATASPACE_BITMAP_START 0x0145
#define DATASPACE_START        0x0146

#define CLUSTER_CHAIN_SIZE     0x5008
#define CLUSTER_CHAIN_HEAD     0x0000
#define CLUSTER_CHAIN_BITMAP   0x0001
#define CLUSTER_CHAIN_CHAIN    0x0002


int load_minifs_file(char* filename, unsigned char* location)
{
    struct main_header *hdr;
    static struct minifs_file files[128];
    struct minifs_chain *chain;
    unsigned int i;
    int found = -1;
    unsigned char sector[512];
    static unsigned char chain_data[42*512]; /* stack overflow if not static */

    /* Read MBLK */
    _ata_read_sectors(0, 1, &sector);
    hdr = (struct main_header*)&sector;

    /* Read directory listing */
#define CLUSTER2SECTOR(x) ( (hdr->partitions[0].start + (x)*8) )
    _ata_read_sectors(CLUSTER2SECTOR(DIR_START), 8, &files);

    for(i=0; i<127; i++)
    {
        if(strcmp(files[i].name, filename) == 0)
            found = i;
    }

    if(found == -1)
        return -1;

#define GET_CHAIN(x)   ( CLUSTER2SECTOR(CLUSTER_CHAIN_CHAIN)*512 + (x)*CLUSTER_CHAIN_SIZE )
#define FILE2SECTOR(x) ( CLUSTER2SECTOR(DATASPACE_START + (x)) )

    /* Read chain list */
    _ata_read_sectors(GET_CHAIN(files[found].chain1)/512, 41, &chain_data[0]);

    chain = (struct minifs_chain*)&chain_data[GET_CHAIN(files[found].chain1)%512];

    /* Copy data */
    for(i=0; i<chain->length; i++)
    {
        _ata_read_sectors(FILE2SECTOR(chain->chain[i]), 8, location);
        location += 0x1000;
    }

    return files[found].size;
}

#endif