virpartff.c 27.1 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
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
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "ffconf.h"
#include "virpartff.h"
#include "string.h"
#include "diskio.h"

#ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION

#if FF_FS_REENTRANT
#if FF_USE_LFN == 1
#error Static LFN work area cannot be used at thread-safe configuration
#endif
#define ENTER_FF(fs)        do { if (!lock_fs(fs)) return FR_TIMEOUT; } while (0)
#define LEAVE_FF(fs, res)   do { unlock_fs(fs, res); return res; } while (0)
#else
#define ENTER_FF(fs)
#define LEAVE_FF(fs, res) return (res)
#endif

extern FATFS *FatFs[FF_VOLUMES];

/*
* follow_virentry:
* Compare the top segment with the virtual partition entry and replace it to its CHILD FATFS
*
* Acceptable return vaule:
* - FR_OK      : The top segment matches one of the virtual partition entries, and the FATFS
*              has been replaced to the corresponding FATFS.
* - FR_DENIED  : The top segment does not matched any of the virtual partition entries, and
*              the FATFS has kept as it is. Or the virtual partition feature has been
*              switched down by any reason.
* - FR_INT_ERR : Assertion error
*/
FRESULT follow_virentry(FFOBJID *obj, const TCHAR *path)
{
66 67 68 69 70 71 72 73 74 75
    TCHAR keyword[FF_MAX_LFN + 1] = {0};
    FATFS *fs = obj->fs;
    INT len;
    UINT i;

    (void)memset_s(keyword, sizeof(keyword), 0, sizeof(keyword));
    /* Search and copy the first segment in path */
    for (len = 0; *path != '/' && *path != '\\' && *path != '\0' && len < FF_MAX_LFN; path++, len++) {
        keyword[len] = *path;
    }
W
wenjun 已提交
76

77 78 79
    if (len == 0 || len > _MAX_ENTRYLENGTH) {
        return FR_DENIED;
    }
W
wenjun 已提交
80

81 82 83 84 85 86 87 88 89 90 91 92 93
    /*
    * Compare the segment does match one for virtual partitions' entry or not,
    * replace the FATFS if the result is positive
    */
    for (i = 0; i < fs->vir_amount; i++) {
        if (!CHILDFS(fs, i)) {
            return FR_INT_ERR;
        }
        if (memcmp((CHILDFS(fs, i))->namelabel, keyword, _MAX_ENTRYLENGTH + 1) == 0) {
            obj->fs = CHILDFS(fs, i);
            return FR_OK;
        }
    }
W
wenjun 已提交
94

95
    return FR_DENIED;
W
wenjun 已提交
96 97 98 99
}

FRESULT f_checkname(const TCHAR *path)
{
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    FRESULT res;
    DIR dj;
    FATFS fs;
    TCHAR *label = (TCHAR *)path;
    DEF_NAMBUF

    (void)memset_s(&fs, sizeof(fs), 0, sizeof(fs));
    dj.obj.fs = &fs;
    INIT_NAMBUF(&fs);
    res = create_name(&dj, &path);
    /* the last byte of file name can't be a space */
    if (res == FR_OK && dj.fn[11] == 0x20) {
        res = FR_INVALID_NAME;
        return res;
    }
W
wenjun 已提交
115

116
    FREE_NAMBUF();
W
wenjun 已提交
117

118 119 120 121 122 123 124 125 126 127
    for (; *label != '\0'; label++) {
        if (label - path > _MAX_ENTRYLENGTH) {
            res = FR_INVALID_NAME;
            return res;
        }
        if (*label == '/' || *label == '\\') {
            res = FR_INVALID_NAME;
            return res;
        }
    }
W
wenjun 已提交
128 129 130 131 132
    return res;
}

FATFS *f_getfatfs(int vol)
{
133 134 135 136 137 138 139
    FATFS *fs = NULL;
    if (vol < 0 || vol >= FF_VOLUMES) {
        fs = NULL;
    } else {
        fs = FatFs[vol];
    }
    return fs;
W
wenjun 已提交
140 141 142 143
}

static FRESULT FatfsCheckBoundParam(FATFS *fs, DWORD clust)
{
144 145 146 147 148 149 150 151 152
    if (fs->st_clst <= 2 || (fs->st_clst + fs->ct_clst) > fs->n_fatent) {
        return FR_INT_ERR;
    }
    if (clust < 2 || clust > fs->n_fatent) {
        return FR_INT_ERR;
    }
    if (clust >= (fs->st_clst + fs->ct_clst) || clust < fs->st_clst) {
        return FR_CHAIN_ERR;
    }
W
wenjun 已提交
153

154
    return FR_OK;
W
wenjun 已提交
155 156 157 158 159 160 161 162 163 164 165 166
}

/*
* check_boundary:
* Check the chain occupied more than one virtual partition or not start at the var 'clust'
*
* Acceptable Return Value:
* - FR_OK          : The chain occupied only one virtual parition
* - FR_CHAIN_ERR   : The chain occupied more than one virtual parition
*/
FRESULT f_boundary(FATFS *fs, DWORD clust)
{
167 168 169 170 171 172 173 174 175 176
    FFOBJID obj;
    FRESULT res;
    obj.fs = fs;
    if (fs == NULL) {
        return FR_INT_ERR;
    }
    if (fs->fs_type != FS_FAT32) {
        return FR_INVAILD_FATFS;
    }
    ENTER_FF(fs);
W
wenjun 已提交
177

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    res = FatfsCheckBoundParam(fs, clust);
    if (res != FR_OK) {
        LEAVE_FF(fs, res);
    }
    for (;;) {
        clust = get_fat(&obj, clust);
        if (clust == 0xFFFFFFFF) {
            LEAVE_FF(fs, FR_DISK_ERR);
        }
        if (clust == 0x0FFFFFFF) {
            break;
        }
        if (clust < 2 || clust >= fs->n_fatent) {
            LEAVE_FF(fs, FR_INT_ERR);
        }
        if (clust >= (fs->st_clst + fs->ct_clst) || clust < fs->st_clst) {
            LEAVE_FF(fs, FR_CHAIN_ERR);
        }
    }
W
wenjun 已提交
197

198
    LEAVE_FF(fs, FR_OK);
W
wenjun 已提交
199 200 201 202 203 204 205 206 207 208 209
}

/*
* f_unregvirfs:
* Uninitialized the CHILD FATFS object
*
* Acceptable Return Value:
* - FR_OK : Successfully initialized the CHILD FATFS object.
*/
FRESULT f_disvirfs(FATFS *fs)
{
210 211 212
    if (ISCHILD(fs)) {
        return FR_INVAILD_FATFS;
    }
W
wenjun 已提交
213

214 215 216
    if (fs->vir_amount > _MAX_VIRVOLUMES) {
        return FR_INT_ERR;
    }
W
wenjun 已提交
217

218
    ENTER_FF(fs);
W
wenjun 已提交
219

220 221
    (void)f_unregvirfs(fs);
    LEAVE_FF(fs, FR_OK);
W
wenjun 已提交
222 223 224 225
}

FRESULT f_unregvirfs(FATFS *fs)
{
226
    UINT i;
W
wenjun 已提交
227

228 229 230
    if (fs == NULL || ISCHILD(fs)) {
        return FR_INVAILD_FATFS;
    }
W
wenjun 已提交
231

232 233 234 235 236 237 238 239 240 241 242 243 244
    fs->vir_avail = FS_VIRDISABLE;
    /* PARENT FATFS has linked to CHILD FATFS already */
    if (fs->child_fs != NULL) {
        /* Followed the CHILD FATFS and free the memory */
        for (i = 0; i < fs->vir_amount; i++) {
            if (CHILDFS(fs, i) != NULL) {
                ff_memfree(CHILDFS(fs, i));
            }
        }
        /* Free the 'child_fs' feild */
        ff_memfree(fs->child_fs);
        fs->child_fs = NULL;
        fs->vir_amount = 0xFFFFFFFF;
W
wenjun 已提交
245 246
    }

247
    return FR_OK;
W
wenjun 已提交
248 249 250 251
}

static void FatfsSetParentFs(FATFS *pfs, FATFS *fs)
{
252 253 254 255 256 257
    pfs->fs_type = fs->fs_type; /* Copy the feild info from PARENT FATFS object */
    pfs->pdrv = fs->pdrv;
    pfs->n_fats = fs->n_fats;
    pfs->id = fs->id;
    pfs->n_rootdir = fs->n_rootdir;
    pfs->csize = fs->csize;
W
wenjun 已提交
258
#if FF_MAX_SS != FF_MIN_SS
259
    pfs->ssize = fs->ssize;
W
wenjun 已提交
260
#endif
261
    pfs->sobj = fs->sobj;
W
wenjun 已提交
262 263

#if FF_FS_RPATH != 0
264
    pfs->cdir = 0;
W
wenjun 已提交
265
#endif
266 267 268 269 270 271 272 273 274 275 276 277 278 279
    pfs->n_fatent = fs->n_fatent;
    pfs->fsize = fs->fsize;
    pfs->volbase = fs->volbase;
    pfs->fatbase = fs->fatbase;
    pfs->dirbase = fs->dirbase;
    pfs->database = fs->database;
    pfs->last_clst = 0xFFFFFFFF; /* Mark the 'last_clst' and 'free_clst' in CHILD FATFS is not been updated for now */
    pfs->free_clst = 0xFFFFFFFF;
    pfs->st_clst = 0xFFFFFFFF; /* Mark the 'st_clst' and 'ct_clst' in CHILD FATFS is not been update for now. */
    pfs->ct_clst = 0xFFFFFFFF;
    pfs->vir_flag = FS_CHILD; /* Mark the FATFS object is a CHILD */
    pfs->vir_avail = FS_VIRENABLE; /* Mark the CHILD object is enable for now */
    pfs->parent_fs = (void *)fs; /* Link to the PARENT object */
    pfs->child_fs = (void *)NULL; /* Link the unrelated feild to NULL */
W
wenjun 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

/*
* f_regvirfs:
* Initialized the CHILD FATFS object
*
* Acceptable Return Value:
* - FR_OK : Successfully initialized the CHILD FATFS object.
*
* Others Return Value:
* - FR_INVAILD_FATFS       :   The FATFS object has error or the info in it has been occuried
* - FR_DENIED              :   The virtual partition feature has been shut down by switcher
* - FR_DISK_ERR            :   A disk error happened
* - FR_NOT_ENOUGH_CORE     :   Not enough memory for allocate space for CHILD FATFS
* - FR_INVALID_PARAMETER   :   There is a invaild value in current setting
*/
FRESULT f_regvirfs(FATFS *fs)
{
298 299
    UINT i;
    FATFS *pfs = NULL;
W
wenjun 已提交
300

301 302 303
    if (fs == NULL || ISCHILD(fs)) {
        return FR_INVAILD_FATFS;
    }
W
wenjun 已提交
304

305 306 307
    if (fs->vir_amount > _MAX_VIRVOLUMES) {
        return FR_INT_ERR;
    }
W
wenjun 已提交
308

309 310 311 312 313 314 315 316 317 318
    fs->parent_fs = (void *)fs; /* Relink to itself */
    /* Mark the FATFS object is PARENT */
    fs->st_clst = 0xFFFFFFFF;
    fs->ct_clst = 0xFFFFFFFF;
    /* Allocate a space for linking to the child FATFS */
    fs->child_fs = (void **)ff_memalloc(fs->vir_amount * sizeof(void *));
    if (fs->child_fs == NULL) {
        return FR_NOT_ENOUGH_CORE;
    }
    fs->vir_avail = FS_VIRENABLE; /* Mark the PARENT object is enable for now */
W
wenjun 已提交
319

320 321 322 323 324 325 326 327 328
    /* Set the CHILD object field */
    for (i = 0; i < fs->vir_amount; i++) {
        pfs = ff_memalloc(sizeof(FATFS)); /* Allocate a memeory for current child FATFS object */
        if (pfs == NULL) { /* If allocate failed, must call 'f_unregvirfs' to free the previous FATFS object memory */
            goto ERROUT;
        }
        FatfsSetParentFs(pfs, fs);
        *(fs->child_fs + i) = (void *)pfs;
    }
W
wenjun 已提交
329

330
    return FR_OK;
W
wenjun 已提交
331
ERROUT:
332 333 334 335 336 337
    while (i > 0) {
        --i;
        ff_memfree(*(fs->child_fs + i));
    }
    ff_memfree(fs->child_fs);
    fs->child_fs = NULL;
W
wenjun 已提交
338

339
    return FR_NOT_ENOUGH_CORE;
W
wenjun 已提交
340 341 342 343
}

static FRESULT FatfsCheckScanFatParam(FATFS *fs)
{
344 345 346
    if (fs == NULL) {
        return FR_INVAILD_FATFS;
    }
W
wenjun 已提交
347

348 349 350
    if (ISNORMAL(fs)) {
        return FR_DENIED;
    }
W
wenjun 已提交
351

352 353 354
    if (fs->fs_type != FS_FAT32 || ISPARENT(fs)) {
        return FR_INVAILD_FATFS;
    }
W
wenjun 已提交
355

356 357 358
    if (fs->st_clst < 3 || fs->st_clst >= fs->n_fatent) {
        return FR_INVAILD_FATFS;
    }
W
wenjun 已提交
359

360 361 362
    if (fs->ct_clst == 0 || fs->ct_clst > (fs->n_fatent - 3)) {
        return FR_INVAILD_FATFS;
    }
W
wenjun 已提交
363

364 365 366
    if ((fs->st_clst + fs->ct_clst) > fs->n_fatent || (fs->st_clst + fs->ct_clst) < 3) {
        return FR_INVAILD_FATFS;
    }
W
wenjun 已提交
367

368
    return FR_OK;
W
wenjun 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
}

/*
* f_scanfat:
* Scan the FAT inside the boundary of CHILD FATFS limit, and update the free cluster and last cluster
*
* Acceptable Return Value:
* - FR_OK : Successfully scaned the FAT and update field.
*
* Others Return Value:
* - FR_INVAILD_FATFS   :   The FATFS object has error or the info in it has been occuried
* - FR_DENIED          :   The virtual partition feature has been shut down by switcher
* - FR_DISK_ERR        :   A disk error happened
*/
FRESULT f_scanfat(FATFS *fs)
{
385 386 387 388
    FRESULT res;
    DWORD clst;
    DWORD link;
    FFOBJID obj;
W
wenjun 已提交
389

390 391 392 393
    res = FatfsCheckScanFatParam(fs);
    if (res != FR_OK) {
        return res;
    }
W
wenjun 已提交
394

395 396 397
    ENTER_FF(fs);
    res = FR_OK;
    obj.fs = fs;
W
wenjun 已提交
398

399 400 401 402 403 404 405 406 407 408 409 410 411 412
    fs->free_clst = fs->ct_clst;
    for (clst = fs->st_clst; clst < fs->st_clst + fs->ct_clst; clst++) {
        link = get_fat(&obj, clst);
        if (link == 0xFFFFFFFF) {
            LEAVE_FF(fs, FR_DISK_ERR);
        }
        if (link == 0) {
            continue;
        }
        fs->free_clst--;
    }
    fs->last_clst = fs->st_clst - 1;

    LEAVE_FF(fs, res);
W
wenjun 已提交
413 414 415 416
}

static FRESULT FatfsCheckStart(BYTE *work, FATFS *fs, BYTE vol)
{
417
    DWORD startBaseSect, countBaseSect;
W
wenjun 已提交
418

419 420
    countBaseSect = LD2PC(vol); /* Volume Base Sectors Count */
    startBaseSect = LD2PS(vol); /* Volume Base Start Sector */
W
wenjun 已提交
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
    /* Check ASCII for Keyword "LITE" */
    if (ld_dword(work + VR_VertifyString) != 0x4C495445) {
        return FR_NOVIRPART;
    }
    /* Check whether filesystem has been changed or not */
    if (work[VR_PartitionFSType] != fs->fs_type) {
        return FR_MODIFIED;
    }
    /* Check whether volume base sector has benn changed or not */
    if (ld_dword(work + VR_PartitionStSec) != startBaseSect) {
        return FR_MODIFIED;
    }
    /* Check whether volume base size hase been changed or not */
    if (ld_dword(work + VR_PartitionCtSec) != countBaseSect) {
        return FR_MODIFIED;
    }
    /* Check whether volume cluster size has been changed or not */
    if (ld_word(work + VR_PartitionClstSz) != fs->csize) {
        return FR_MODIFIED;
    }
    /* Check whether volume start cluster is cluster #3 or not */
    if (ld_dword(work + VR_PartitionCtClst) != fs->n_fatent) {
        return FR_MODIFIED;
    }
    /* Check whether virtual partition overlimit */
    if (work[VR_PartitionCnt] > _MAX_VIRVOLUMES) {
        return FR_MODIFIED;
    }
W
wenjun 已提交
450

451
    return FR_OK;
W
wenjun 已提交
452 453 454 455
}

static FRESULT FatfsCheckPercent(FATFS *fs, WORD i)
{
456 457 458 459 460 461 462 463 464 465
    if ((CHILDFS(fs, i))->st_clst + (CHILDFS(fs, i))->ct_clst < fs->n_fatent) {
        fs->st_clst = (CHILDFS(fs, i))->st_clst + (CHILDFS(fs, i))->ct_clst;
        fs->ct_clst = fs->n_fatent - ((CHILDFS(fs, i))->st_clst + (CHILDFS(fs, i))->ct_clst);
    } else if ((CHILDFS(fs, i))->st_clst + (CHILDFS(fs, i))->ct_clst == fs->n_fatent) {
        fs->st_clst = 0xFFFFFFFF;
        fs->ct_clst = 0xFFFFFFFF;
    } else {
        (void)f_unregvirfs(fs);
        return FR_MODIFIED;
    }
W
wenjun 已提交
466

467
    return FR_OK;
W
wenjun 已提交
468 469 470 471
}

static FRESULT FatfsCheckPartClst(FATFS *fs, WORD i)
{
472 473 474 475 476 477 478 479 480 481 482 483 484
    if (i == 0) {
        /* First virtual partition must start at cluster #3 */
        if ((CHILDFS(fs, i))->st_clst != 3) {
            (void)f_unregvirfs(fs);
            return FR_MODIFIED;
        }
    } else {
        /* Check whether the current virtual partition is closely next to the previous virtual partition */
        if ((CHILDFS(fs, i))->st_clst != (CHILDFS(fs, (i - 1))->st_clst + CHILDFS(fs, (i - 1))->ct_clst)) {
            (void)f_unregvirfs(fs);
            return FR_MODIFIED;
        }
    }
W
wenjun 已提交
485

486
    return FR_OK;
W
wenjun 已提交
487 488 489 490
}

static void FatfsSetChildClst(BYTE *work, FATFS *fs, WORD i)
{
491 492
    (CHILDFS(fs, i))->st_clst = ld_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_StartClust);
    (CHILDFS(fs, i))->ct_clst = ld_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_CountClust);
W
wenjun 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
}

/*
* f_ckvtlpt :
* Check the external SD virtual paritition sectors and read configure from it
*
* Acceptable Return Value:
* - FR_OK          : The external SD configure is complete, all info has been set to the
*                  each CHILD FATFS
* - FR_NOT_MATCHED : The virtual partition's configure does not matched as current setting
* - FR_MODIFIED    : The virtual partition's configure has been destoried partly or completely
* - FR_NOVIRPART   : The external SD has not been apllied as virtual partition yet
*
* Others Return Value:
* - FR_INVAILD_FATFS : The FATFS object has error or the info in it has been occuried
* - FR_DENIED          : The virtual partition feature has been shut down by switcher
* - FR_INVALID_DRIVE   : The drive index is error
* - FR_DISK_ERR        : A Disk error happend
*/
FRESULT f_checkvirpart(FATFS *fs, const TCHAR *path, BYTE vol)
{
514 515 516 517 518 519 520 521 522 523 524 525
    FRESULT res;
    WORD i;
    DWORD virSect;
    DWORD tmp;
    BYTE pdrv;
    BYTE *work = NULL;
    CHAR label[_MAX_ENTRYLENGTH + 1];
    DWORD *labelTmp = NULL; /* to clear the compilation warning */

    if (fs == NULL || (disk_status(fs->pdrv) & STA_NOINIT)) {
        return FR_INVAILD_FATFS; /* The object is invalid */
    }
W
wenjun 已提交
526

527
    /* Lock the filesystem object */
528
    res = mount_volume(&path, &fs, FA_WRITE); /* Update the filesystem info to the parent fs */
529 530 531
    if (res != FR_OK) {
        LEAVE_FF(fs, res);
    }
W
wenjun 已提交
532

533 534 535 536 537
    if (ISCHILD(fs)) {
        LEAVE_FF(fs, FR_INT_ERR);
    }
    /* Data will be save at the last reserve sector ,which is the front one of the fat base sector */
    virSect = fs->fatbase - 1;
W
wenjun 已提交
538

539
    pdrv = LD2PD(vol); /* Driver index */
W
wenjun 已提交
540

541 542 543 544 545 546 547 548 549
    work = (BYTE *)ff_memalloc(SS(fs));
    if (work == NULL) {
        LEAVE_FF(fs, FR_NOT_ENOUGH_CORE);
    }
    /* Check and vertify partition information */
    if (disk_read(pdrv, work, virSect, 1) != RES_OK) {
        res = FR_DISK_ERR;
        goto EXIT;
    } /* Load VBR */
W
wenjun 已提交
550

551 552
    res = FatfsCheckStart(work, fs, vol);
    if (res != FR_OK) {
W
wenjun 已提交
553 554
        goto EXIT;
    }
555 556 557 558
    /* Check the virtual parition amount if matched current setting or not */
    fs->vir_amount = work[VR_PartitionCnt];
    res = f_regvirfs(fs);
    if (res != FR_OK) {
W
wenjun 已提交
559 560 561
        goto EXIT;
    }

562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    for (i = 0; i < _MAX_VIRVOLUMES; i++) {
        if (i < work[VR_PartitionCnt]) {
            if (work[VR_PARTITION + i * VR_ITEMSIZE + VR_Available] != 0x80) {
                (void)f_unregvirfs(fs);
                res = FR_MODIFIED;
                goto EXIT;
            }
        } else {
            if (work[VR_PARTITION + i * VR_ITEMSIZE + VR_Available] != 0x00) {
                (void)f_unregvirfs(fs);
                res = FR_MODIFIED;
                goto EXIT;
            }
            break;
        }
W
wenjun 已提交
577

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
        (void)memset_s(label, sizeof(label), 0, sizeof(label));

        tmp = ld_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_Entry + 0);
        labelTmp = (DWORD *)label;
        *labelTmp = tmp;
        tmp = ld_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_Entry + 4);
        *((DWORD * )(label + 4)) = tmp;
        tmp = ld_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_Entry + 8);
        *((DWORD * )(label + 8)) = tmp;
        tmp = ld_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_Entry + 12);
        *((DWORD * )(label + 12)) = tmp;

        if (f_checkname(label) != FR_OK) {
            (void)f_unregvirfs(fs);
            res = FR_MODIFIED;
            goto EXIT;
        }
        (void)memcpy_s((CHILDFS(fs, i))->namelabel, _MAX_ENTRYLENGTH + 1, label, _MAX_ENTRYLENGTH + 1);
W
wenjun 已提交
596

597
        FatfsSetChildClst(work, fs, i);
W
wenjun 已提交
598

599 600 601 602 603 604
        /* External SD setting has overlimit the whole partition cluster amount */
        if ((QWORD)(CHILDFS(fs, i))->st_clst + (QWORD)((CHILDFS(fs, i))->ct_clst) > (QWORD)fs->n_fatent) {
            (void)f_unregvirfs(fs);
            res = FR_MODIFIED;
            goto EXIT;
        }
W
wenjun 已提交
605

606 607 608 609 610 611 612 613 614 615 616 617 618 619
        res = FatfsCheckPartClst(fs, i);
        if (res != FR_OK) {
            goto EXIT;
        }
        if (i == (work[VR_PartitionCnt] - 1)) {
            /*
             * If the external SD virtual partition percent exceeds the error tolerance based on current virtual
             * partition percent setting
             */
            res = FatfsCheckPercent(fs, i);
            if (res != FR_OK) {
                goto EXIT;
            }
        }
W
wenjun 已提交
620 621
    }
EXIT:
622 623
    ff_memfree(work);
    LEAVE_FF(fs, res);
W
wenjun 已提交
624 625 626 627
}

static void FatfsClacPartInfo(FATFS *fs, DOUBLE virpartper, UINT i)
{
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    if (i == 0) {
        (CHILDFS(fs, i))->st_clst = 3;
        (CHILDFS(fs, i))->ct_clst = (DWORD)((fs->n_fatent - 3) *
                                            g_fatVirPart.virtualinfo.virpartpercent[i]);

        fs->st_clst = (CHILDFS(fs, i))->st_clst + (CHILDFS(fs, i))->ct_clst;
        fs->ct_clst = fs->n_fatent - fs->st_clst;
    } else if (i != (fs->vir_amount - 1)) {
        (CHILDFS(fs, i))->st_clst = (CHILDFS(fs, (i - 1)))->st_clst + (CHILDFS(fs, (i - 1)))->ct_clst;
        (CHILDFS(fs, i))->ct_clst = (DWORD)((fs->n_fatent - 3) *
                                            g_fatVirPart.virtualinfo.virpartpercent[i]);
    } else {
        (CHILDFS(fs, i))->st_clst = (CHILDFS(fs, (i - 1)))->st_clst + (CHILDFS(fs, (i - 1)))->ct_clst;
        if (virpartper <= (1 + _FLOAT_ACC) && virpartper >= (1 - _FLOAT_ACC)) {
            (CHILDFS(fs, i))->ct_clst = fs->n_fatent - (CHILDFS(fs, i))->st_clst;
            fs->st_clst = 0xFFFFFFFF;
            fs->ct_clst = 0xFFFFFFFF;
        } else {
            (CHILDFS(fs, i))->ct_clst = (DWORD)((fs->n_fatent - 3) *
                                                g_fatVirPart.virtualinfo.virpartpercent[i]);
            fs->st_clst = (CHILDFS(fs, i))->st_clst + (CHILDFS(fs, i))->ct_clst;
            fs->ct_clst = fs->n_fatent - fs->st_clst;
        }
    }
W
wenjun 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
}

/*
* f_mkvtlpt:
* Apply the current virtual partition's setting to external SD card and to the CHILD FATFS
*
* Acceptable Return Value:
* - FR_OK : Successfully applied current setting to external SD card and
*              CHILD FATFS
*
* Others Return Value  :
* - FR_INVAILD_FATFS   : The FATFS object has error or the info in it has been occuried
* - FR_DENIED          : The virtual partition feature has been shut down by switcher
* - FR_INVALID_DRIVE   : The drive index is error
* - FR_DISK_ERR        : A Disk error happend
*/
FRESULT f_makevirpart(FATFS *fs, const TCHAR *path, BYTE vol)
{
670 671 672 673 674 675 676 677 678 679 680 681 682 683
    FRESULT res;
    DWORD virSect;
    DWORD startBaseSect, countBaseSect;
    DWORD tmp;
    CHAR label[_MAX_ENTRYLENGTH + 1];
    DWORD *labelTmp = NULL; /* to clear the compilation warning */
    UINT i;
    BYTE pdrv;
    BYTE *work = NULL;
    DOUBLE virpartper = 0.0;

    if (fs == NULL || (disk_status(fs->pdrv) & STA_NOINIT)) {
        return FR_INVAILD_FATFS; /* The object is invalid */
    }
W
wenjun 已提交
684

685
    /* Lock the filesystem object */
686
    res = mount_volume(&path, &fs, FA_WRITE); /* Update the filesystem info to the parent fs */
687 688 689
    if (res != FR_OK) {
        LEAVE_FF(fs, res);
    }
W
wenjun 已提交
690

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
    /* Only available in FAT32 filesystem */
    if (ISCHILD(fs)) {
        LEAVE_FF(fs, FR_INVAILD_FATFS);
    }
    /* Data will be save at the last reserve sector,which is the front one of the fat base sector */
    virSect = fs->fatbase - 1;
    /* Search the fs index, which same as the volume index */
    pdrv = LD2PD(vol); /* Driver index */
    countBaseSect = LD2PC(vol); /* Volume Base Sectors Count */
    startBaseSect = LD2PS(vol); /* Volume Base Start Sector */

    fs->vir_amount = g_fatVirPart.virtualinfo.virpartnum;
    res = f_regvirfs(fs);
    if (res != FR_OK) {
        LEAVE_FF(fs, res);
    }
W
wenjun 已提交
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
    work = (BYTE *)ff_memalloc(SS(fs));
    if (work == NULL) {
        LEAVE_FF(fs, FR_NOT_ENOUGH_CORE);
    }
    /* Data Cluster is begin from the Cluster #3 to the last cluster */
    /* Cluster #0 #1 is for VBR, reserve sectors and fat */
    /* Cluster #2 is for root directory */
    (void)memset_s(work, SS(fs), 0, SS(fs));

    for (i = 0; i < fs->vir_amount; i++) {
        /* Copy the Entry label and write to work sector's buffer */
        (void)memset_s(label, sizeof(label), 0, sizeof(label));
        (void)memcpy_s(label, _MAX_ENTRYLENGTH + 1, g_fatVirPart.virtualinfo.virpartname[i], _MAX_ENTRYLENGTH + 1);
        labelTmp = (DWORD *)label;
        tmp = *labelTmp;
        st_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_Entry + 0, tmp);
        tmp = *((DWORD * )(label + 4));
        st_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_Entry + 4, tmp);
        tmp = *((DWORD * )(label + 8));
        st_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_Entry + 8, tmp);
        tmp = *((DWORD * )(label + 12));
        st_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_Entry + 12, tmp);

        virpartper += g_fatVirPart.virtualinfo.virpartpercent[i];

        (void)memcpy_s((CHILDFS(fs, i))->namelabel, _MAX_ENTRYLENGTH + 1, g_fatVirPart.virtualinfo.virpartname[i],
                       _MAX_ENTRYLENGTH + 1);
        FatfsClacPartInfo(fs, virpartper, i);
        (CHILDFS(fs, i))->last_clst = (CHILDFS(fs, i))->st_clst - 1;
        work[VR_PARTITION + i * VR_ITEMSIZE + VR_Available] = 0x80;
    }
W
wenjun 已提交
739

740 741 742 743 744 745 746 747 748 749 750 751 752
    /* Set the data to sector */
    work[VR_PartitionCnt] = fs->vir_amount;
    work[VR_PartitionFSType] = fs->fs_type;
    st_dword(work + VR_PartitionStSec, startBaseSect);
    st_dword(work + VR_PartitionCtSec, countBaseSect);
    st_word(work + VR_PartitionClstSz, fs->csize);
    st_dword(work + VR_PartitionCtClst, fs->n_fatent);
    for (i = 0; i < fs->vir_amount; i++) {
        st_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_StartClust,
                 (CHILDFS(fs, i))->st_clst);
        st_dword(work + VR_PARTITION + i * VR_ITEMSIZE + VR_CountClust,
                 (CHILDFS(fs, i))->ct_clst);
    }
W
wenjun 已提交
753

754 755
    /* ASCII for Keyword "LITE" */
    st_dword(work + VR_VertifyString, 0x4C495445);
W
wenjun 已提交
756

757 758 759 760 761
    /* Write into the data area */
    if (disk_write(pdrv, work, virSect, 1) != RES_OK) {
        (void)f_unregvirfs(fs);
        res = FR_DISK_ERR;
    }
W
wenjun 已提交
762

763 764
    ff_memfree(work);
    LEAVE_FF(fs, res);
W
wenjun 已提交
765 766 767 768
}

FRESULT f_getvirfree(const TCHAR *path, DWORD *nclst, DWORD *cclst)
{
769 770 771 772 773 774 775 776
    FATFS *fs = NULL;
    FRESULT res;
    DWORD clst, link;
    DWORD nfree;
    UINT i;
    DIR dj;

    /* Find volume to Update the global FSINFO */
777
    res = mount_volume(&path, &fs, 0);
778 779 780
    if (res != FR_OK) {
        LEAVE_FF(fs, res);
    }
W
wenjun 已提交
781

782
    /* Following the entry keyword, decide to replace the PARENT FATFS to CHILD FATFS or not */
W
wenjun 已提交
783
    dj.obj.fs = fs;
784 785 786 787 788
    if (ISVIRPART(fs)) {
        /* Check the virtual partition top directory, and match the virtual fs */
        res = follow_virentry(&dj.obj, path);
        if (res == FR_INT_ERR) {
            LEAVE_FF(fs, res);
W
wenjun 已提交
789
        }
790 791 792 793 794 795
        if (res == FR_OK) {
            fs = dj.obj.fs;
        }
    } else {
        /* Virtual Partition Feature was off, deny this operation */
        LEAVE_FF(fs, FR_DENIED);
W
wenjun 已提交
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

    /* If current FATFS is a CHILD FATFS */
    if (ISCHILD(fs)) {
        /* If CHILD FATFS' free_clst is invaild, the scan the FAT and update it */
        if (fs->free_clst > fs->ct_clst) {
            dj.obj.fs = fs;
            fs->free_clst = fs->ct_clst;
            for (clst = fs->st_clst; clst < fs->st_clst + fs->ct_clst; clst++) {
                link = get_fat(&dj.obj, clst);
                if (link == 0) {
                    continue;
                }
                fs->free_clst--;
            }
        }
        *nclst = fs->free_clst;
        *cclst = fs->ct_clst;
        LEAVE_FF(fs, FR_OK);
    } else {
        nfree = 0;
        if (fs->ct_clst == 0xFFFFFFFF) {
            LEAVE_FF(fs, FR_DENIED);
        }
        for (i = 0; i < fs->vir_amount; i++) {
            if (CHILDFS(fs, i)->free_clst > CHILDFS(fs, i)->ct_clst) {
                dj.obj.fs = CHILDFS(fs, i);
                CHILDFS(fs, i)->free_clst = CHILDFS(fs, i)->ct_clst;
                for (clst = CHILDFS(fs, i)->st_clst; clst < CHILDFS(fs, i)->st_clst + CHILDFS(fs, i)->ct_clst; clst++) {
                    link = get_fat(&dj.obj, clst);
                    if (link == 0) {
                        continue;
                    }
                    CHILDFS(fs, i)->free_clst--;
                }
W
wenjun 已提交
831
            }
832
            nfree += CHILDFS(fs, i)->free_clst;
W
wenjun 已提交
833
        }
834 835 836
        *nclst = fs->free_clst - nfree;
        *cclst = fs->ct_clst;
        LEAVE_FF(fs, FR_OK);
W
wenjun 已提交
837 838 839
    }
}
#endif