los_rootfs.c 17.8 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
 *
 * 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 "los_base.h"
#include "los_typedef.h"
#include "string.h"
M
mamingshuai 已提交
34
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND)
W
wenjun 已提交
35 36 37 38 39 40 41 42 43 44 45
#include "mtd_partition.h"
#endif
#ifdef LOSCFG_DRIVERS_MMC
#include "mmc/block.h"
#include "disk.h"
#endif
#include "sys/mount.h"
#ifdef LOSCFG_PLATFORM_ROOTFS
#include "los_rootfs.h"
#endif
#include "mtd_list.h"
W
wangchenyang 已提交
46
#include "fs/path_cache.h"
W
wenjun 已提交
47

48 49 50 51 52 53 54
#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
#include "mtd_partition.h"
#include "cfiflash.h"
#define DEV_STORAGE_PATH        "/dev/cfiflash1"
#define SECOND_MTD_PART_NUM 1
#endif

M
mamingshuai 已提交
55
#ifdef LOSCFG_STORAGE_SPINOR
W
wenjun 已提交
56 57
#define DEV_STORAGE_PATH       "/dev/spinorblk2"
#define SECOND_MTD_PART_NUM 2
M
mamingshuai 已提交
58 59 60 61 62 63 64
#define STORAGE_SIZE 0x80000
#endif

#ifdef LOSCFG_STORAGE_SPINAND
#define DEV_STORAGE_PATH       "/dev/nandblk2"
#define SECOND_MTD_PART_NUM 2
#define STORAGE_SIZE 0xa00000
W
wenjun 已提交
65 66
#endif

M
mamingshuai 已提交
67
#ifdef LOSCFG_STORAGE_EMMC
L
li_zan 已提交
68
#include "ff.h"
W
wenjun 已提交
69 70 71 72
#define STORAGE_SIZE 0x3200000
STATIC los_disk *g_emmcDisk = NULL;
#endif

L
li_zan 已提交
73

W
wenjun 已提交
74 75 76 77
#ifndef LOSCFG_SECURITY_BOOT
STATIC INT32 g_alignSize = 0;
#endif

W
wangchenyang 已提交
78 79
#define VFAT_STORAGE_MOUNT_DIR_MODE 0777
#define DEFAULT_STORAGE_MOUNT_DIR_MODE 0755
M
mamingshuai 已提交
80

W
wenjun 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#ifdef LOSCFG_DRIVERS_MMC
los_disk *GetMmcDisk(UINT8 type)
{
    const CHAR *mmcDevHead = "/dev/mmcblk";

    for (INT32 diskId = 0; diskId < SYS_MAX_DISK; diskId++) {
        los_disk *disk = get_disk(diskId);
        if (disk == NULL) {
            continue;
        } else if (disk->disk_name == NULL) {
            continue;
        } else if (strncmp(disk->disk_name, mmcDevHead, strlen(mmcDevHead))) {
            continue;
        } else {
            if (disk->type == type) {
                return disk;
            }
        }
    }
    PRINT_ERR("Cannot find the mmc disk!\n");
    return NULL;
}
#endif

M
mamingshuai 已提交
105
#ifdef LOSCFG_STORAGE_EMMC
W
wenjun 已提交
106 107 108 109
STATIC const CHAR *AddEmmcRootfsPart(INT32 rootAddr, INT32 rootSize)
{
    INT32 ret;

W
wangchenyang 已提交
110
    struct mmc_block *block = (struct mmc_block *)((struct drv_data *)g_emmcDisk->dev->data)->priv;
W
wenjun 已提交
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
    const char *node_name = mmc_block_get_node_name(block);
    if (los_disk_deinit(g_emmcDisk->disk_id) != ENOERR) {
        PRINT_ERR("Failed to deinit emmc disk!\n");
        return NULL;
    }

    struct disk_divide_info *emmc = get_emmc();
    ret = add_mmc_partition(emmc, rootAddr / EMMC_SEC_SIZE, rootSize / EMMC_SEC_SIZE);
    if (ret != LOS_OK) {
        PRINT_ERR("Failed to add mmc root partition!\n");
        return NULL;
    } else {
        UINT64 storageStartCnt = (rootAddr + rootSize) / EMMC_SEC_SIZE;
        UINT64 storageSizeCnt = STORAGE_SIZE / EMMC_SEC_SIZE;
        UINT64 userdataStartCnt = storageStartCnt + storageSizeCnt;
        UINT64 userdataSizeCnt = g_emmcDisk->sector_count - userdataStartCnt;
        ret = add_mmc_partition(emmc, storageStartCnt, storageSizeCnt);
        if (ret != LOS_OK) {
            PRINT_ERR("Failed to add mmc storage partition!\n");
        }
        ret = add_mmc_partition(emmc, userdataStartCnt, userdataSizeCnt);
        if (ret != LOS_OK) {
            PRINT_ERR("Failed to add mmc userdata partition!\n");
        }
        LOS_Msleep(10); /* waiting for device identification */
        INT32 diskId = los_alloc_diskid_byname(node_name);
        if (diskId < 0) {
            PRINT_ERR("Failed to alloc disk %s!\n", node_name);
            return NULL;
        }
        if (los_disk_init(node_name, mmc_block_get_bops(block), (void *)block, diskId, emmc) != ENOERR) {
            PRINT_ERR("Failed to init emmc disk!\n");
            return NULL;
        }
        return node_name;
    }
}
#endif

STATIC const CHAR *GetDevName(const CHAR *rootType, INT32 rootAddr, INT32 rootSize)
{
    const CHAR *rootDev = NULL;

M
mamingshuai 已提交
154
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND)
W
wenjun 已提交
155 156 157 158
    INT32 ret;
    if (strcmp(rootType, "flash") == 0) {
        ret = add_mtd_partition(FLASH_TYPE, rootAddr, rootSize, 0);
        if (ret != LOS_OK) {
M
mamingshuai 已提交
159
            PRINT_ERR("Failed to add spinor/spinand root partition!\n");
W
wenjun 已提交
160 161 162 163
        } else {
            rootDev = FLASH_DEV_NAME;
            ret = add_mtd_partition(FLASH_TYPE, (rootAddr + rootSize), STORAGE_SIZE, SECOND_MTD_PART_NUM);
            if (ret != LOS_OK) {
M
mamingshuai 已提交
164
                PRINT_ERR("Failed to add spinor/spinand storage partition!\n");
W
wenjun 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            }
        }
    } else
#endif

#ifdef LOSCFG_DRIVERS_USB_MASS_STORAGE
    if (strcmp(rootType, "usb") == 0) {
        rootDev = "/dev/sda";
    } else
#endif

#ifdef LOSCFG_DRIVERS_SD
    if (strcmp(rootType, "sdcard") == 0) {
        los_disk *sdDisk = GetMmcDisk(OTHERS);
        if (sdDisk == NULL) {
            PRINT_ERR("Get sdcard failed!\n");
        } else {
            rootDev = sdDisk->disk_name;
        }
    } else
#endif

M
mamingshuai 已提交
187
#ifdef LOSCFG_STORAGE_EMMC
W
wenjun 已提交
188 189 190 191
    if (strcmp(rootType, "emmc") == 0) {
        rootDev = AddEmmcRootfsPart(rootAddr, rootSize);
    } else
#endif
192 193

#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
194
    if (strcmp(rootType, FLASH_TYPE) == 0) {
M
mamingshuai 已提交
195
        INT32 ret;
196 197 198 199 200
        if (rootAddr != CFIFLASH_ROOT_ADDR) {
            PRINT_ERR("Error rootAddr, must be %#0x!\n", CFIFLASH_ROOT_ADDR);
            return NULL;
        }
        ret = add_mtd_partition(FLASH_TYPE, rootAddr, rootSize, 0);
201
        if (ret != LOS_OK) {
202
            PRINT_ERR("Failed to add %s root partition!\n", FLASH_TYPE);
203 204
        } else {
            rootDev = "/dev/cfiflash0";
205 206
            ret = add_mtd_partition(FLASH_TYPE, (rootAddr + rootSize),
                                    CFIFLASH_CAPACITY - rootAddr - rootSize, SECOND_MTD_PART_NUM);
207
            if (ret != LOS_OK) {
208
                PRINT_ERR("Failed to add %s storage partition!\n", FLASH_TYPE);
209 210 211 212
            }
        }
    } else
#endif
W
wenjun 已提交
213 214 215 216 217 218 219 220 221
    {
        PRINT_ERR("Failed to find root dev type: %s\n", rootType);
    }
    return rootDev;
}

#ifndef LOSCFG_SECURITY_BOOT
STATIC INT32 GetArgs(CHAR **args)
{
222
#ifdef LOSCFG_BOOTENV_RAM
M
mamingshuai 已提交
223 224 225 226
    *args = OsGetArgsAddr();
    return LOS_OK;

#else
W
wenjun 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239
    INT32 ret;
    INT32 i;
    INT32 len = 0;
    CHAR *cmdLine = NULL;
    CHAR *tmp = NULL;
    const CHAR *bootargName = "bootargs=";

    cmdLine = (CHAR *)malloc(COMMAND_LINE_SIZE);
    if (cmdLine == NULL) {
        PRINT_ERR("Malloc cmdLine space error!\n");
        return LOS_NOK;
    }

M
mamingshuai 已提交
240
#ifdef LOSCFG_STORAGE_EMMC
W
wenjun 已提交
241 242 243 244 245
    g_emmcDisk = GetMmcDisk(EMMC);
    if (g_emmcDisk == NULL) {
        PRINT_ERR("Get EMMC disk failed!\n");
        goto ERROUT;
    }
246
    /* param4 is TRUE for not reading large contiguous data */
W
wenjun 已提交
247
    ret = los_disk_read(g_emmcDisk->disk_id, cmdLine, COMMAND_LINE_ADDR / EMMC_SEC_SIZE,
248
                        COMMAND_LINE_SIZE / EMMC_SEC_SIZE, TRUE);
W
wenjun 已提交
249 250 251 252 253 254 255
    if (ret != 0) {
        PRINT_ERR("Read EMMC command line failed!\n");
        goto ERROUT;
    }
    g_alignSize = EMMC_SEC_SIZE;
#endif

M
mamingshuai 已提交
256
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND)
W
wenjun 已提交
257 258 259 260 261 262 263 264 265 266 267 268
    struct MtdDev *mtd = GetMtd(FLASH_TYPE);
    if (mtd == NULL) {
        PRINT_ERR("Get spinor mtd failed!\n");
        goto ERROUT;
    }
    g_alignSize = mtd->eraseSize;
    ret = mtd->read(mtd, COMMAND_LINE_ADDR, COMMAND_LINE_SIZE, cmdLine);
    if (ret != COMMAND_LINE_SIZE) {
        PRINT_ERR("Read spinor command line failed!\n");
        goto ERROUT;
    }
#endif
269
#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
270 271 272 273 274 275 276 277 278 279 280
    struct MtdDev *mtd = GetCfiMtdDev();
    if (mtd == NULL) {
        PRINT_ERR("Get CFI mtd failed!\n");
        goto ERROUT;
    }
    g_alignSize = mtd->eraseSize;
    ret = mtd->read(mtd, CFIFLASH_BOOTARGS_ADDR, COMMAND_LINE_SIZE, cmdLine);
    if (ret != COMMAND_LINE_SIZE) {
        PRINT_ERR("Read CFI command line failed!\n");
        goto ERROUT;
    }
281 282
#endif

W
wenjun 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    for (i = 0; i < COMMAND_LINE_SIZE; i += len + 1) {
        len = strlen(cmdLine + i);
        tmp = strstr(cmdLine + i, bootargName);
        if (tmp != NULL) {
            *args = strdup(tmp + strlen(bootargName));
            if (*args == NULL) {
                goto ERROUT;
            }
            free(cmdLine);
            return LOS_OK;
        }
    }
    PRINT_ERR("Cannot find bootargs!\n");

ERROUT:
    free(cmdLine);
    return LOS_NOK;
M
mamingshuai 已提交
300
#endif
W
wenjun 已提交
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
}

STATIC INT32 MatchRootPos(CHAR *p, const CHAR *rootInfoName, INT32 *rootInfo)
{
    UINT32 offset;
    CHAR *value = NULL;

    if (strncmp(p, rootInfoName, strlen(rootInfoName)) == 0) {
        value = p + strlen(rootInfoName);
        offset = strspn(value, DEC_NUMBER_STRING);
        if (strcmp(p + strlen(p) - 1, "M") == 0) {
            if ((offset < (strlen(value) - 1)) || (sscanf_s(value, "%d", rootInfo) <= 0)) {
                goto ERROUT;
            }
            *rootInfo = *rootInfo * BYTES_PER_MBYTE;
        } else if (strcmp(p + strlen(p) - 1, "K") == 0) {
            if ((offset < (strlen(value) - 1)) || (sscanf_s(value, "%d", rootInfo) <= 0)) {
                goto ERROUT;
            }
            *rootInfo = *rootInfo * BYTES_PER_KBYTE;
        } else if (sscanf_s(value, "0x%x", rootInfo) > 0) {
            value += strlen("0x");
            if (strspn(value, HEX_NUMBER_STRING) < strlen(value)) {
                goto ERROUT;
            }
        } else {
            goto ERROUT;
        }
    }

    if ((*rootInfo >= 0) && (g_alignSize != 0) && ((UINT32)(*rootInfo) & (UINT32)(g_alignSize - 1))) {
        PRINT_ERR("The bootarg \"%s\" will be 0x%x aligned!\n", p, g_alignSize);
    }
    return LOS_OK;

ERROUT:
    PRINT_ERR("Invalid bootarg \"%s\"!\n", p);
    return LOS_NOK;
}

STATIC INT32 MatchRootInfo(CHAR *p, CHAR **rootType, CHAR **fsType, INT32 *rootAddr, INT32 *rootSize)
{
    const CHAR *rootName = "root=";
    const CHAR *fsName = "fstype=";
    const CHAR *rootAddrName = "rootaddr=";
    const CHAR *rootSizeName = "rootsize=";

    if ((*rootType == NULL) && (strncmp(p, rootName, strlen(rootName)) == 0)) {
        *rootType = strdup(p + strlen(rootName));
        if (*rootType == NULL) {
            return LOS_NOK;
        }
        return LOS_OK;
    }

    if ((*fsType == NULL) && (strncmp(p, fsName, strlen(fsName)) == 0)) {
        *fsType = strdup(p + strlen(fsName));
        if (*fsType == NULL) {
            return LOS_NOK;
        }
        return LOS_OK;
    }

    if (*rootAddr < 0) {
        if (MatchRootPos(p, rootAddrName, rootAddr) != LOS_OK) {
            return LOS_NOK;
        } else if (*rootAddr >= 0) {
            return LOS_OK;
        }
    }

    if (*rootSize < 0) {
        if (MatchRootPos(p, rootSizeName, rootSize) != LOS_OK) {
            return LOS_NOK;
        }
    }

    return LOS_OK;
}

STATIC INT32 GetRootType(CHAR **rootType, CHAR **fsType, INT32 *rootAddr, INT32 *rootSize)
{
    CHAR *args = NULL;
    CHAR *p = NULL;

    if (GetArgs(&args) != LOS_OK) {
        PRINT_ERR("Cannot get bootargs!\n");
        return LOS_NOK;
    }
390
#ifndef LOSCFG_BOOTENV_RAM
M
mamingshuai 已提交
391
    CHAR *argsBak = NULL;
W
wenjun 已提交
392
    argsBak = args;
M
mamingshuai 已提交
393
#endif
W
wenjun 已提交
394 395 396 397 398 399 400 401
    p = strsep(&args, " ");
    while (p != NULL) {
        if (MatchRootInfo(p, rootType, fsType, rootAddr, rootSize) != LOS_OK) {
            goto ERROUT;
        }
        p = strsep(&args, " ");
    }
    if ((*fsType != NULL) && (*rootType != NULL)) {
402
#ifndef LOSCFG_BOOTENV_RAM
W
wenjun 已提交
403
        free(argsBak);
M
mamingshuai 已提交
404
#endif
W
wenjun 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417
        return LOS_OK;
    }

ERROUT:
    PRINT_ERR("Invalid rootfs information!\n");
    if (*rootType != NULL) {
        free(*rootType);
        *rootType = NULL;
    }
    if (*fsType != NULL) {
        free(*fsType);
        *fsType = NULL;
    }
418
#ifndef LOSCFG_BOOTENV_RAM
W
wenjun 已提交
419
    free(argsBak);
M
mamingshuai 已提交
420
#endif
W
wenjun 已提交
421 422 423 424
    return LOS_NOK;
}
#endif

M
mamingshuai 已提交
425
#ifdef LOSCFG_STORAGE_EMMC
W
wenjun 已提交
426 427 428 429 430 431
STATIC VOID OsMountUserdata(const CHAR *fsType)
{
    INT32 ret;
    INT32 err;
    const CHAR *userdataDir = "/userdata";
    ret = mkdir(userdataDir, VFAT_STORAGE_MOUNT_DIR_MODE);
432 433
    if ((ret != LOS_OK) && ((err = get_errno()) != EEXIST)) {
        PRINT_ERR("Failed to mkdir /userdata, errno %d: %s\n", err, strerror(err));
W
wenjun 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447
        return;
    }
    CHAR emmcUserdataDev[DISK_NAME] = {0};
    if (snprintf_s(emmcUserdataDev, sizeof(emmcUserdataDev), sizeof(emmcUserdataDev) - 1,
        "%s%s", g_emmcDisk->disk_name, "p2") < 0) {
        PRINT_ERR("Failed to get emmc userdata dev name!\n");
        return;
    }
    ret = mount(emmcUserdataDev, userdataDir, fsType, 0, "umask=000");
    if (ret == LOS_OK) {
        return;
    }
    err = get_errno();
    if (err == ENOENT) {
L
li_zan 已提交
448
#ifdef LOSCFG_FS_FAT
W
wenjun 已提交
449 450 451 452 453
        ret = format(emmcUserdataDev, 0, FM_FAT32);
        if (ret != LOS_OK) {
            PRINT_ERR("Failed to format %s\n", emmcUserdataDev);
            return;
        }
L
li_zan 已提交
454
#endif
W
wenjun 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
        ret = mount(emmcUserdataDev, userdataDir, fsType, 0, "umask=000");
        if (ret != LOS_OK) {
            err = get_errno();
            PRINT_ERR("Failed to mount /userdata, errno %d: %s\n", err, strerror(err));
        }
    } else {
        PRINT_ERR("Failed to mount /userdata, errno %d: %s\n", err, strerror(err));
    }
    return;
}
#endif

STATIC INT32 OsMountRootfsAndUserfs(const CHAR *rootDev, const CHAR *fsType)
{
    INT32 ret;
    INT32 err;
    if (strcmp(fsType, "vfat") == 0) {
        ret = mount(rootDev, "/", fsType, MS_RDONLY, NULL);
        if (ret != LOS_OK) {
            err = get_errno();
            PRINT_ERR("Failed to mount vfat rootfs, errno %d: %s\n", err, strerror(err));
            return ret;
        }
M
mamingshuai 已提交
478
#ifdef LOSCFG_STORAGE_EMMC
W
wenjun 已提交
479
        ret = mkdir("/storage", VFAT_STORAGE_MOUNT_DIR_MODE);
W
wangchenyang 已提交
480
        if ((ret != LOS_OK) && ((err = get_errno()) != EEXIST)) {
481
            PRINT_ERR("Failed to mkdir /storage, errno %d: %s\n", err, strerror(err));
J
JING 已提交
482
            return ret;
W
wenjun 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
        } else {
            CHAR emmcStorageDev[DISK_NAME] = {0};
            if (snprintf_s(emmcStorageDev, sizeof(emmcStorageDev), sizeof(emmcStorageDev) - 1,
                "%s%s", g_emmcDisk->disk_name, "p1") < 0) {
                PRINT_ERR("Failed to get emmc storage dev name!\n");
            } else {
                ret = mount(emmcStorageDev, "/storage", fsType, 0, "umask=000");
                if (ret != LOS_OK) {
                    err = get_errno();
                    PRINT_ERR("Failed to mount /storage, errno %d: %s\n", err, strerror(err));
                }
            }
        }
        OsMountUserdata(fsType);
#endif
    } else {
        ret = mount(rootDev, "/", fsType, MS_RDONLY, NULL);
        if (ret != LOS_OK) {
            err = get_errno();
M
mamingshuai 已提交
502
            PRINT_ERR("Failed to mount rootfs,rootDev %s, errno %d: %s\n", rootDev, err, strerror(err));
W
wenjun 已提交
503 504
            return ret;
        }
505
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND) || defined(LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7)
W
wenjun 已提交
506
        ret = mkdir("/storage", DEFAULT_STORAGE_MOUNT_DIR_MODE);
W
wangchenyang 已提交
507
        if ((ret != LOS_OK) && ((err = get_errno()) != EEXIST)) {
508
            PRINT_ERR("Failed to mkdir /storage, errno %d: %s\n", err, strerror(err));
J
JING 已提交
509
            return ret;
W
wenjun 已提交
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
        } else {
            ret = mount(DEV_STORAGE_PATH, "/storage", fsType, 0, NULL);
            if (ret != LOS_OK) {
                err = get_errno();
                PRINT_ERR("Failed to mount /storage, errno %d: %s\n", err, strerror(err));
            }
        }
#endif
    }
    return LOS_OK;
}

INT32 OsMountRootfs(VOID)
{
    INT32 ret = LOS_OK;
    INT32 err;
    INT32 rootAddr = -1;
    INT32 rootSize = -1;
    CHAR *rootType = NULL;
    CHAR *fsType = NULL;
    const CHAR *rootDev = NULL;

#ifdef LOSCFG_SECURITY_BOOT
    rootType = strdup(ROOTFS_ROOT_TYPE);
    fsType = strdup(ROOTFS_FS_TYPE);
    rootAddr = ROOTFS_FLASH_ADDR;
    rootSize = ROOTFS_FLASH_SIZE;
#else
    ret = GetRootType(&rootType, &fsType, &rootAddr, &rootSize);
    if (ret != LOS_OK) {
        return ret;
    }
    rootAddr = (rootAddr >= 0) ? rootAddr : ROOTFS_FLASH_ADDR;
    rootSize = (rootSize >= 0) ? rootSize : ROOTFS_FLASH_SIZE;
#endif

    rootDev = GetDevName(rootType, rootAddr, rootSize);
    if (rootDev != NULL) {
        ret = OsMountRootfsAndUserfs(rootDev, fsType);
        if (ret != LOS_OK) {
            err = get_errno();
            PRINT_ERR("Failed to mount rootfs, errno %d: %s\n", err, strerror(err));
        }
    }

    free(rootType);
    free(fsType);
    return ret;
}