about summary refs log tree commit diff stats
path: root/403unicode.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-08-14 18:02:57 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-08-14 18:02:57 -0700
commitbfe3a175be9048a5dc7425569969a2edd66de3c3 (patch)
treeabbfac88bd8ccea2fa2f015beb73bdf26aaace83 /403unicode.mu
parenta3ffd93c57cc24666dc622fc2666b165e083502f (diff)
downloadmu-bfe3a175be9048a5dc7425569969a2edd66de3c3.tar.gz
slack: quick 'n' dirty thread screen
Diffstat (limited to '403unicode.mu')
0 files changed, 0 insertions, 0 deletions
n106'>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

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
#include "fatfilesystem.h"
#include "common.h"
#include "fs.h"
#include "alloc.h"
#include "fatfs_ff.h"
#include "fatfs_diskio.h"
#include "screen.h"

#define SEEK_SET	0	/* Seek from beginning of file.  */
#define SEEK_CUR	1	/* Seek from current position.  */
#define SEEK_END	2

#define O_RDONLY	     00
#define O_WRONLY	     01
#define O_RDWR		     02

static BOOL mount(const char* sourcePath, const char* targetPath, uint32 flags, void *data);
static BOOL checkMount(const char* sourcePath, const char* targetPath, uint32 flags, void *data);
static FileSystemDirent* readdir(FileSystemNode *node, uint32 index);
static FileSystemNode* finddir(FileSystemNode *node, char *name);
static int32 read(File *file, uint32 size, uint8 *buffer);
static int32 write(File *file, uint32 size, uint8 *buffer);
static int32 lseek(File *file, int32 offset, int32 whence);
static int32 stat(FileSystemNode *node, struct stat* buf);
static BOOL open(File *file, uint32 flags);
static void close(File *file);

static FileSystemDirent gFileSystemDirent;

static FileSystemNode* gMountedBlockDevices[FF_VOLUMES];


void initializeFatFileSystem() {
    FileSystem fs;
    memset((uint8*)&fs, 0, sizeof(fs));
    strcpy(fs.name, "fat");
    fs.mount = mount;
    fs.checkMount = checkMount;

    registerFileSystem(&fs);

    for (int i = 0; i < FF_VOLUMES; ++i) {
        gMountedBlockDevices[i] = NULL;
    }
}

static BOOL mount(const char* sourcePath, const char* targetPath, uint32 flags, void *data) {
    printkf("fat mount source: %s\n", sourcePath);

    FileSystemNode* node = getFileSystemNode(sourcePath);
    if (node && node->nodeType == FT_BlockDevice) {
        FileSystemNode* targetNode = getFileSystemNode(targetPath);
        if (targetNode) {
            if (targetNode->nodeType == FT_Directory) {
                printkf("fat mount target: %s\n", targetPath);

                int32 volume = -1;
                for (int32 v = 0; v < FF_VOLUMES; ++v) {
                    if (NULL == gMountedBlockDevices[v]) {
                        volume = v;
                        break;
                    }
                }

                if (volume < 0) {
                    return FALSE;
                }

                FileSystemNode* newNode = kmalloc(sizeof(FileSystemNode));

                memset((uint8*)newNode, 0, sizeof(FileSystemNode));
                strcpy(newNode->name, targetNode->name);
                newNode->nodeType = FT_Directory;
                newNode->open = open;
                newNode->readdir = readdir;
                newNode->finddir = finddir;
                newNode->parent = targetNode->parent;
                newNode->mountSource = node;
                newNode->privateNodeData = (void*)volume;

                gMountedBlockDevices[volume] = node;

                FATFS* fatFs = (FATFS*)kmalloc(sizeof(FATFS));
                //uint8 work[512];
                //FRESULT fr = f_mkfs("", FM_FAT | FM_SFD, 512, work, 512);
                //printkf("f_mkfs: %d\n", fr);
                char path[8];
                sprintf(path, "%d:", volume);
                FRESULT fr = f_mount(fatFs, path, 1);
                //printkf("f_mount: fr:%d drv:%d\n", fr, fatFs->pdrv);

                if (FR_OK == fr) {
                    targetNode->nodeType |= FT_MountPoint;
                    targetNode->mountPoint = newNode;

                    return TRUE;
                }
                else {
                    kfree(newNode);

                    kfree(fatFs);

                    gMountedBlockDevices[volume] = NULL;
                }
            }
        }
    }

    return FALSE;
}

static BOOL checkMount(const char* sourcePath, const char* targetPath, uint32 flags, void *data) {
    FileSystemNode* node = getFileSystemNode(sourcePath);
    if (node && node->nodeType == FT_BlockDevice) {
        FileSystemNode* targetNode = getFileSystemNode(targetPath);
        if (targetNode) {
            if (targetNode->nodeType == FT_Directory) {
                return TRUE;
            }
        }
    }

    return FALSE;
}

static FileSystemDirent* readdir(FileSystemNode *node, uint32 index) {
    //when node is the root of mounted filesystem,
    //node->mountSource is the source node (eg. disk partition /dev/hd1p1)

    //printkf("readdir1: node->name:%s\n", node->name);

    uint8 targetPath[128];

    FileSystemNode *n = node;
    int charIndex = 126;
    memset(targetPath, 0, 128);
    while (NULL == n->mountSource) {
        int length = strlen(n->name);

        charIndex -= length;

        if (charIndex < 2) {
            return NULL;
        }

        strcpyNonNull((char*)(targetPath + charIndex), n->name);
        charIndex -= 1;
        targetPath[charIndex] = '/';

        n = n->parent;
    }

    char number[8];
    sprintf(number, "%d", n->privateNodeData);//volume nuber

    targetPath[charIndex] = ':';
    int length = strlen(number);
    charIndex -= length;
    if (charIndex < 0) {
        return NULL;
    }

    strcpyNonNull((char*)(targetPath + charIndex), number);
    uint8* target = targetPath + charIndex;

    //printkf("readdir: targetpath:[%s]\n", target);

    DIR dir;
    FRESULT fr = f_opendir(&dir, (TCHAR*)target);
    if (FR_OK == fr) {
        FILINFO fileInfo;
        for (int i = 0; i <= index; ++i) {
            memset((uint8*)&fileInfo, 0, sizeof(FILINFO));
            fr = f_readdir(&dir, &fileInfo);

            if (strlen(fileInfo.fname) <= 0) {
                f_closedir(&dir);

                return NULL;
            }
        }

        gFileSystemDirent.inode = 0;
        strcpy(gFileSystemDirent.name, fileInfo.fname);
        if ((fileInfo.fattrib & AM_DIR) == AM_DIR) {
            gFileSystemDirent.fileType = FT_Directory;
        }
        else {
            gFileSystemDirent.fileType = FT_File;
        }

        f_closedir(&dir);

        return &gFileSystemDirent;
    }

    return NULL;
}

static FileSystemNode* finddir(FileSystemNode *node, char *name) {
    //when node is the root of mounted filesystem,
    //node->mountSource is the source node (eg. disk partition /dev/hd1p1)

    //printkf("finddir1: node->name:%s name:%s\n", node->name, name);

    FileSystemNode* child = node->firstChild;
    while (NULL != child) {
        if (strcmp(name, child->name) == 0) {
            return child;
        }

        child = child->nextSibling;
    }

    //If we are here, this file is accesed first time in this session.
    //So we create its node...

    uint8 targetPath[128];

    FileSystemNode *n = node;
    int charIndex = 126;
    memset(targetPath, 0, 128);
    int length = strlen(name);
    charIndex -= length;
    strcpyNonNull((char*)(targetPath + charIndex), name);
    charIndex -= 1;
    targetPath[charIndex] = '/';
    while (NULL == n->mountSource) {
        length = strlen(n->name);
        charIndex -= length;

        if (charIndex < 2) {
            return NULL;
        }

        strcpyNonNull((char*)(targetPath + charIndex), n->name);
        charIndex -= 1;
        targetPath[charIndex] = '/';

        n = n->parent;
    }

    char number[8];
    sprintf(number, "%d", n->privateNodeData);//volume nuber

    targetPath[charIndex] = ':';
    length = strlen(number);
    charIndex -= length;
    if (charIndex < 0) {
        return NULL;
    }

    strcpyNonNull((char*)(targetPath + charIndex), number);
    uint8* target = targetPath + charIndex;

    //printkf("finddir: targetpath:[%s]\n", target);

    FILINFO fileInfo;
    memset((uint8*)&fileInfo, 0, sizeof(FILINFO));
    FRESULT fr = f_stat((TCHAR*)target, &fileInfo);
    if (FR_OK == fr) {
        FileSystemNode* newNode = kmalloc(sizeof(FileSystemNode));

        memset((uint8*)newNode, 0, sizeof(FileSystemNode));
        strcpy(newNode->name, name);
        newNode->parent = node;
        newNode->readdir = readdir;
        newNode->finddir = finddir;
        newNode->open = open;
        newNode->close = close;
        newNode->read = read;
        newNode->write = write;
        newNode->lseek = lseek;
        newNode->stat = stat;
        newNode->length = fileInfo.fsize;

        if ((fileInfo.fattrib & AM_DIR) == AM_DIR) {
            newNode->nodeType = FT_Directory;
        }
        else {
            newNode->nodeType = FT_File;
        }

        if (NULL == node->firstChild) {
            node->firstChild = newNode;
        }
        else {
            FileSystemNode* child = node->firstChild;
            while (NULL != child->nextSibling) {
                child = child->nextSibling;
            }
            child->nextSibling = newNode;
        }

        //printkf("finddir: returning [%s]\n", name);
        return newNode;
    }
    else {
        //printkf("finddir error: fr: %d]\n", fr);
    }

    return NULL;
}

static int32 read(File *file, uint32 size, uint8 *buffer) {
    if (file->privateData == NULL) {
        return -1;
    }

    FIL* f = (FIL*)file->privateData;

    UINT br = 0;
    FRESULT fr = f_read(f, buffer, size, &br);
    file->offset = f->fptr;
    //printkf("fat read: name:%s size:%d hasRead:%d, fr:%d\n", file->node->name, size, br, fr);
    if (FR_OK == fr) {
        return br;
    }

    return -1;
}

static int32 write(File *file, uint32 size, uint8 *buffer) {
    if (file->privateData == NULL) {
        return -1;
    }

    FIL* f = (FIL*)file->privateData;

    UINT bw = 0;
    FRESULT fr = f_write(f, buffer, size, &bw);
    file->offset = f->fptr;
    if (FR_OK == fr) {
        return bw;
    }

    return -1;
}

static int32 lseek(File *file, int32 offset, int32 whence) {
    if (file->privateData == NULL) {
        return -1;
    }

    FIL* f = (FIL*)file->privateData;

    FRESULT fr = FR_INVALID_OBJECT;

    switch (whence) {
    case SEEK_SET:
        fr = f_lseek(f, offset);
        break;
    case SEEK_CUR:
        fr = f_lseek(f, f_tell(f) + offset);
        break;
    case SEEK_END:
        fr = f_lseek(f, f_size(f) + offset);
        break;
    default:
        break;
    }


    if (FR_OK == fr) {
        file->offset = f->fptr;

        return file->offset;
    }

    return -1;
}

static int32 stat(FileSystemNode *node, struct stat* buf) {
    //printkf("fat stat [%s]\n", node->name);

    uint8 targetPath[128];

    FileSystemNode *n = node;
    int charIndex = 126;
    memset(targetPath, 0, 128);
    while (NULL == n->mountSource) {
        int length = strlen(n->name);
        charIndex -= length;

        if (charIndex < 2) {
            return NULL;
        }

        strcpyNonNull((char*)(targetPath + charIndex), n->name);
        charIndex -= 1;
        targetPath[charIndex] = '/';

        n = n->parent;
    }

    char number[8];
    sprintf(number, "%d", n->privateNodeData);//volume nuber

    targetPath[charIndex] = ':';
    int length = strlen(number);
    charIndex -= length;
    if (charIndex < 0) {
        return NULL;
    }

    strcpyNonNull((char*)(targetPath + charIndex), number);
    uint8* target = targetPath + charIndex;

    //printkf("fat stat target:[%s]\n", target);

    FILINFO fileInfo;
    memset((uint8*)&fileInfo, 0, sizeof(FILINFO));
    FRESULT fr = f_stat((TCHAR*)target, &fileInfo);
    if (FR_OK == fr) {
        if ((fileInfo.fattrib & AM_DIR) == AM_DIR) {
            node->nodeType = FT_Directory;
        }
        else {
            node->nodeType = FT_File;
        }

        node->length = fileInfo.fsize;

        return 1;
    }

    return -1; //Error
}

static BOOL open(File *file, uint32 flags) {
    //printkf("fat open %s\n", file->node->name);

    FileSystemNode *node = file->node;

    if (node->nodeType == FT_Directory) {
        return TRUE;
    }

    uint8 targetPath[128];

    FileSystemNode *n = node;
    int charIndex = 126;
    memset(targetPath, 0, 128);
    while (NULL == n->mountSource) {
        int length = strlen(n->name);
        charIndex -= length;

        if (charIndex < 2) {
            return NULL;
        }

        strcpyNonNull((char*)(targetPath + charIndex), n->name);
        charIndex -= 1;
        targetPath[charIndex] = '/';

        n = n->parent;
    }

    char number[8];
    sprintf(number, "%d", n->privateNodeData);//volume nuber

    targetPath[charIndex] = ':';
    int length = strlen(number);
    charIndex -= length;
    if (charIndex < 0) {
        return NULL;
    }

    strcpyNonNull((char*)(targetPath + charIndex), number);
    uint8* target = targetPath + charIndex;

    //printkf("fat open %s\n", target);

    int fatfsMode = FA_READ;

    switch (flags) {
    case O_RDONLY:
        fatfsMode = FA_READ;
        break;
    case O_WRONLY:
        fatfsMode = FA_WRITE;
        break;
    case O_RDWR:
        fatfsMode = (FA_READ | FA_WRITE);
        break;
        //TODO: append, create
    default:
        break;
    }

    FIL* f = (FIL*)kmalloc(sizeof(FIL));
    FRESULT fr = f_open(f, (TCHAR*)target, fatfsMode);
    if (FR_OK == fr) {
        file->offset = f->fptr;

        file->privateData = f;

        return TRUE;
    }

    return FALSE;
}

static void close(File *file) {
    if (file->privateData == NULL) {
        return;
    }

    FIL* f = (FIL*)file->privateData;

    f_close(f);

    kfree(f);

    file->privateData = NULL;
}

DSTATUS disk_initialize(
        BYTE pdrv		//Physical drive nmuber
) {
    return 0;
}

DSTATUS disk_status(BYTE pdrv) {
    return 0;
}

DRESULT disk_read (
    BYTE pdrv,			/* Physical drive nmuber (0) */
    BYTE *buff,			/* Pointer to the data buffer to store read data */
    DWORD sector,		/* Start sector number (LBA) */
    UINT count			/* Number of sectors to read */
) {
    //printkf("disk_read() drv:%d sector:%d count:%d\n", pdrv, sector, count);

    if (gMountedBlockDevices[pdrv] == NULL) return RES_NOTRDY;

    //if (sector >= RamDiskSize) return RES_PARERR;

    gMountedBlockDevices[pdrv]->readBlock(gMountedBlockDevices[pdrv], (uint32)sector, count, buff);

    return RES_OK;
}

DRESULT disk_write (
    BYTE pdrv,			/* Physical drive nmuber (0) */
    const BYTE *buff,	/* Pointer to the data to be written */
    DWORD sector,		/* Start sector number (LBA) */
    UINT count			/* Number of sectors to write */
) {
    if (gMountedBlockDevices[pdrv] == NULL) return RES_NOTRDY;

    //if (sector >= RamDiskSize) return RES_PARERR;

    gMountedBlockDevices[pdrv]->writeBlock(gMountedBlockDevices[pdrv], (uint32)sector, count, (uint8*)buff);

    return RES_OK;
}

DRESULT disk_ioctl (
    BYTE pdrv,		/* Physical drive nmuber (0) */
    BYTE ctrl,		/* Control code */
    void* buff		/* Buffer to send/receive data block */
) {
    if (gMountedBlockDevices[pdrv] == NULL) return RES_ERROR;

    DRESULT dr = RES_ERROR;

    File* f = NULL;

    uint32 value = 0;

    switch (ctrl) {
    case CTRL_SYNC:
        dr = RES_OK;
        break;
    case GET_SECTOR_COUNT:
        f = open_fs(gMountedBlockDevices[pdrv], 0);
        if (f) {
            ioctl_fs(f, IC_GetSectorCount, &value);
            *(DWORD*)buff = value;
            dr = RES_OK;
            close_fs(f);
        }
        printkf("disk_ioctl GET_SECTOR_COUNT: %d\n", value);
        break;
    case GET_BLOCK_SIZE:
        f = open_fs(gMountedBlockDevices[pdrv], 0);
        if (f) {
            ioctl_fs(f, IC_GetSectorSizeInBytes, &value);
            *(DWORD*)buff = value;
            dr = RES_OK;
            close_fs(f);
        }
        printkf("disk_ioctl GET_BLOCK_SIZE: %d\n", value);
        *(DWORD*)buff = value;
        dr = RES_OK;
        break;
    }
    return dr;
}