fix: bootargs解析与rootfs挂载解耦,并支持自定义bootargs参数

1、bootargs解析与rootfs挂载进行解耦;
2、支持开发者自定义bootargs参数,内核存储bootargs中所有参数;
3、开发者可获取bootargs中的参数内容,并自行解析。

close #I41CL8

Change-Id: If384476714854ba0cf64eb70b785eb26737dd3d2
Signed-off-by: chenjing <chenjing139@huawei.com>
This commit is contained in:
chenjing 2021-07-19 15:18:16 +08:00
parent 700c3415c4
commit 80473f0975
8 changed files with 595 additions and 526 deletions

View File

@ -725,6 +725,9 @@ INT32 los_alloc_diskid_byname(const CHAR *diskName);
*/
INT32 los_get_diskid_byname(const CHAR *diskName);
los_disk *los_get_mmcdisk_bytype(UINT8 type);
#ifdef __cplusplus
#if __cplusplus
}

View File

@ -187,6 +187,22 @@ INT32 los_get_diskid_byname(const CHAR *diskName)
return diskID;
}
los_disk *los_get_mmcdisk_bytype(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->type == type) && (strncmp(disk->disk_name, mmcDevHead, strlen(mmcDevHead)) == 0)) {
return disk;
}
}
PRINT_ERR("Cannot find the mmc disk!\n");
return NULL;
}
VOID OsSetUsbStatus(UINT32 diskID)
{
if (diskID < SYS_MAX_DISK) {

View File

@ -31,7 +31,7 @@ include $(LITEOSTOPDIR)/config.mk
MODULE_NAME := rootfs
LOCAL_SRCS := $(wildcard los_rootfs.c)
LOCAL_SRCS := $(wildcard los_rootfs.c los_bootargs.c)
LOCAL_INCLUDE := \
-I $(LITEOSTOPDIR)/kernel/common \

View File

@ -0,0 +1,246 @@
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* 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_bootargs.h"
#include "los_base.h"
#include "string.h"
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND) || defined(LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7)
#include "mtd_list.h"
#endif
#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
#include "cfiflash.h"
#endif
#ifdef LOSCFG_STORAGE_EMMC
#include "disk.h"
#endif
STATIC CHAR *g_cmdLine = NULL;
STATIC UINT64 g_alignSize = 0;
STATIC struct BootArgs g_bootArgs[MAX_ARGS_NUM] = {0};
INT32 LOS_GetCmdLine() {
int ret = 0;
g_cmdLine = (CHAR *)malloc(COMMAND_LINE_SIZE);
if (g_cmdLine == NULL) {
PRINT_ERR("Malloc g_cmdLine space error!\n");
return LOS_NOK;
}
#ifdef LOSCFG_STORAGE_EMMC
los_disk *emmcDisk = los_get_mmcdisk_bytype(EMMC);
if (emmcDisk == NULL) {
PRINT_ERR("Get EMMC disk failed!\n");
goto ERROUT;
}
g_alignSize = EMMC_SEC_SIZE;
ret = los_disk_read(emmcDisk->disk_id, g_cmdLine, COMMAND_LINE_ADDR / EMMC_SEC_SIZE,
COMMAND_LINE_SIZE / EMMC_SEC_SIZE, TRUE);
if (ret == 0) {
return LOS_OK;
}
#endif
#ifdef LOSCFG_STORAGE_SPINOR
struct MtdDev *mtd = GetMtd("spinor");
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, g_cmdLine);
if (ret == COMMAND_LINE_SIZE) {
return LOS_OK;
}
#endif
#ifdef LOSCFG_STORAGE_SPINAND
struct MtdDev *mtd = GetMtd("nand");
if (mtd == NULL) {
PRINT_ERR("Get nand mtd failed!\n");
goto ERROUT;
}
g_alignSize = mtd->eraseSize;
ret = mtd->read(mtd, COMMAND_LINE_ADDR, COMMAND_LINE_SIZE, g_cmdLine);
if (ret == COMMAND_LINE_SIZE) {
return LOS_OK;
}
#endif
#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
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, g_cmdLine);
if (ret == COMMAND_LINE_SIZE) {
return LOS_OK;
}
#endif
PRINT_ERR("Read cmdline error!\n");
ERROUT:
free(g_cmdLine);
g_cmdLine = NULL;
return LOS_NOK;
}
VOID LOS_FreeCmdLine() {
if (g_cmdLine != NULL) {
free(g_cmdLine);
g_cmdLine = NULL;
}
}
STATIC INT32 GetBootargs(CHAR **args)
{
#ifdef LOSCFG_BOOTENV_RAM
*args = OsGetArgsAddr();
return LOS_OK;
#else
INT32 i;
INT32 len = 0;
CHAR *tmp = NULL;
const CHAR *bootargsName = "bootargs=";
if (g_cmdLine == NULL) {
PRINT_ERR("Should call LOS_GetCmdLine() first!\n");
return LOS_NOK;
}
for (i = 0; i < COMMAND_LINE_SIZE; i += len + 1) {
len = strlen(g_cmdLine + i);
tmp = strstr(g_cmdLine + i, bootargsName);
if (tmp != NULL) {
*args = tmp + strlen(bootargsName);
return LOS_OK;
}
}
PRINT_ERR("Cannot find bootargs!\n");
return LOS_NOK;
#endif
}
INT32 LOS_ParseBootargs() {
INT32 idx = 0;
INT32 ret;
CHAR *args;
CHAR *argName;
CHAR *argValue;
ret = GetBootargs(&args);
if (ret != LOS_OK) {
return LOS_NOK;
}
while ((argValue = strsep(&args, " ")) != NULL) {
argName = strsep(&argValue, "=");
if (argValue == NULL) {
/* If the argument is not compliance with the format 'foo=bar' */
g_bootArgs[idx].argName = argName;
g_bootArgs[idx].argValue = argName;
} else {
g_bootArgs[idx].argName = argName;
g_bootArgs[idx].argValue = argValue;
}
if (++idx >= MAX_ARGS_NUM) {
/* Discard the rest arguments */
break;
}
}
return LOS_OK;
}
INT32 LOS_GetArgValue(CHAR *argName, CHAR **argValue) {
INT32 idx = 0;
while (idx < MAX_ARGS_NUM) {
if (g_bootArgs[idx].argName == NULL) {
break;
}
if (strcmp(argName, g_bootArgs[idx].argName) == 0) {
*argValue = g_bootArgs[idx].argValue;
return LOS_OK;
}
idx++;
}
return LOS_NOK;
}
UINT64 LOS_GetAlignsize() {
return g_alignSize;
}
UINT64 LOS_SizeStrToNum(CHAR *value)
{
UINT64 num = 0;
/* If the string is a hexadecimal value */
if (sscanf_s(value, "0x%x", &num) > 0) {
value += strlen("0x");
if (strspn(value, "0123456789abcdefABCDEF") < strlen(value)) {
goto ERROUT;
}
return num;
}
/* If the string is a decimal value in unit *Bytes */
INT32 ret = sscanf_s(value, "%d", &num);
INT32 decOffset = strspn(value, "0123456789");
CHAR *endPos = value + decOffset;
if ((ret <= 0) || (decOffset < (strlen(value) - 1))) {
goto ERROUT;
}
if (strlen(endPos) == 0) {
return num;
} else if (strcasecmp(endPos, "k") == 0) {
num = num * BYTES_PER_KBYTE;
} else if (strcasecmp(endPos, "m") == 0) {
num = num * BYTES_PER_MBYTE;
} else if (strcasecmp(endPos, "g") == 0) {
num = num * BYTES_PER_GBYTE;
} else {
goto ERROUT;
}
return num;
ERROUT:
PRINT_ERR("Invalid value string \"%s\"!\n", value);
return num;
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* 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.
*/
#ifndef _LOS_BOOTARGS_H
#define _LOS_BOOTARGS_H
#include "los_typedef.h"
#define BYTES_PER_GBYTE (1 << 30)
#define BYTES_PER_MBYTE (1 << 20)
#define BYTES_PER_KBYTE (1 << 10)
#define COMMAND_LINE_ADDR (LOSCFG_BOOTENV_ADDR * BYTES_PER_KBYTE)
#define COMMAND_LINE_SIZE 1024
#define MAX_ARGS_NUM 100
#ifdef LOSCFG_STORAGE_EMMC
#define EMMC_SEC_SIZE 512
#endif
struct BootArgs {
CHAR *argName;
CHAR *argValue;
};
INT32 LOS_GetCmdLine(VOID);
VOID LOS_FreeCmdLine(VOID);
INT32 LOS_ParseBootargs(VOID);
INT32 LOS_GetArgValue(CHAR *argName, CHAR **argValue);
UINT64 LOS_GetAlignsize(VOID);
UINT64 LOS_SizeStrToNum(CHAR *value);
#endif /* _LOS_BOOTARGS_H */

View File

@ -28,555 +28,294 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "los_rootfs.h"
#include "los_bootargs.h"
#include "los_base.h"
#include "los_typedef.h"
#include "string.h"
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND)
#include "sys/mount.h"
#include "sys/stat.h"
#include "sys/types.h"
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND) || defined(LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7)
#include "mtd_list.h"
#include "mtd_partition.h"
#endif
#ifdef LOSCFG_DRIVERS_MMC
#include "disk.h"
#endif
#include "sys/mount.h"
#ifdef LOSCFG_PLATFORM_ROOTFS
#include "los_rootfs.h"
#endif
#include "mtd_list.h"
#include "fs/driver.h"
#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
#define STORAGE_SIZE 0x1400000
#endif
#ifdef LOSCFG_STORAGE_SPINOR
#define DEV_STORAGE_PATH "/dev/spinorblk2"
#define SECOND_MTD_PART_NUM 2
#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
#endif
#ifdef LOSCFG_STORAGE_EMMC
#include "disk.h"
#include "ff.h"
#define STORAGE_SIZE 0x3200000
STATIC los_disk *g_emmcDisk = NULL;
#endif
#ifndef LOSCFG_SECURITY_BOOT
STATIC INT32 g_alignSize = 0;
#endif
#define VFAT_STORAGE_MOUNT_DIR_MODE 0777
#define DEFAULT_STORAGE_MOUNT_DIR_MODE 0755
#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
#ifdef LOSCFG_STORAGE_EMMC
struct disk_divide_info *StorageBlockGetEmmc(void);
struct block_operations *StorageBlockGetMmcOps(void);
char *StorageBlockGetEmmcNodeName(void *block);
STATIC const CHAR *AddEmmcRootfsPart(INT32 rootAddr, INT32 rootSize, INT32 userAddr, INT32 userSize)
STATIC INT32 AddEmmcParts(INT32 rootAddr, INT32 rootSize, INT32 userAddr, INT32 userSize)
{
INT32 ret;
void *block = ((struct drv_data *)g_emmcDisk->dev->data)->priv;
los_disk *emmcDisk = los_get_mmcdisk_bytype(EMMC);
if (emmcDisk == NULL) {
PRINT_ERR("Get EMMC disk failed!\n");
return LOS_NOK;
}
void *block = ((struct drv_data *)emmcDisk->dev->data)->priv;
const char *node_name = StorageBlockGetEmmcNodeName(block);
if (los_disk_deinit(g_emmcDisk->disk_id) != ENOERR) {
if (los_disk_deinit(emmcDisk->disk_id) != ENOERR) {
PRINT_ERR("Failed to deinit emmc disk!\n");
return NULL;
return LOS_NOK;
}
struct disk_divide_info *emmc = StorageBlockGetEmmc();
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 = userAddr / EMMC_SEC_SIZE;
UINT64 storageSizeCnt = userSize / 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, StorageBlockGetMmcOps(), 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, INT32 userAddr, INT32 userSize)
{
const CHAR *rootDev = NULL;
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND)
INT32 ret;
if (strcmp(rootType, "flash") == 0) {
ret = add_mtd_partition(FLASH_TYPE, rootAddr, rootSize, 0);
if (ret != LOS_OK) {
PRINT_ERR("Failed to add spinor/spinand root partition!\n");
} else {
rootDev = FLASH_DEV_NAME;
ret = add_mtd_partition(FLASH_TYPE, userAddr, userSize, SECOND_MTD_PART_NUM);
if (ret != LOS_OK) {
PRINT_ERR("Failed to add spinor/spinand storage partition!\n");
}
}
} 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
#ifdef LOSCFG_STORAGE_EMMC
if (strcmp(rootType, "emmc") == 0) {
rootDev = AddEmmcRootfsPart(rootAddr, rootSize, userAddr, userSize);
} else
#endif
#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
if (strcmp(rootType, FLASH_TYPE) == 0) {
INT32 ret;
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);
if (ret != LOS_OK) {
PRINT_ERR("Failed to add %s root partition!\n", FLASH_TYPE);
} else {
rootDev = "/dev/cfiflash0";
ret = add_mtd_partition(FLASH_TYPE, (rootAddr + rootSize),
CFIFLASH_CAPACITY - rootAddr - rootSize, SECOND_MTD_PART_NUM);
if (ret != LOS_OK) {
PRINT_ERR("Failed to add %s storage partition!\n", FLASH_TYPE);
}
}
} else
#endif
{
PRINT_ERR("Failed to find root dev type: %s\n", rootType);
}
return rootDev;
}
#ifndef LOSCFG_SECURITY_BOOT
STATIC INT32 GetArgs(CHAR **args)
{
#ifdef LOSCFG_BOOTENV_RAM
*args = OsGetArgsAddr();
return LOS_OK;
#else
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;
}
#ifdef LOSCFG_STORAGE_EMMC
g_emmcDisk = GetMmcDisk(EMMC);
if (g_emmcDisk == NULL) {
PRINT_ERR("Get EMMC disk failed!\n");
goto ERROUT;
}
/* param4 is TRUE for not reading large contiguous data */
ret = los_disk_read(g_emmcDisk->disk_id, cmdLine, COMMAND_LINE_ADDR / EMMC_SEC_SIZE,
COMMAND_LINE_SIZE / EMMC_SEC_SIZE, TRUE);
if (ret != 0) {
PRINT_ERR("Read EMMC command line failed!\n");
goto ERROUT;
}
g_alignSize = EMMC_SEC_SIZE;
#endif
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND)
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
#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
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;
}
#endif
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;
#endif
}
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, INT32 *userAddr, INT32 *userSize)
{
const CHAR *rootName = "root=";
const CHAR *fsName = "fstype=";
const CHAR *rootAddrName = "rootaddr=";
const CHAR *rootSizeName = "rootsize=";
const CHAR *userAddrName = "useraddr=";
const CHAR *userSizeName = "usersize=";
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;
}
}
if (*userAddr < 0) {
if (MatchRootPos(p, userAddrName, userAddr) != LOS_OK) {
return LOS_NOK;
} else if (*userAddr >= 0) {
return LOS_OK;
}
}
if (*userSize < 0) {
if (MatchRootPos(p, userSizeName, userSize) != LOS_OK) {
return LOS_NOK;
}
}
return LOS_OK;
}
STATIC INT32 GetRootType(CHAR **rootType, CHAR **fsType, INT32 *rootAddr, INT32 *rootSize, INT32 *userAddr, INT32 *userSize)
{
CHAR *args = NULL;
CHAR *p = NULL;
if (GetArgs(&args) != LOS_OK) {
PRINT_ERR("Cannot get bootargs!\n");
UINT64 storageStartCnt = userAddr / EMMC_SEC_SIZE;
UINT64 storageSizeCnt = userSize / EMMC_SEC_SIZE;
UINT64 userdataStartCnt = storageStartCnt + storageSizeCnt;
UINT64 userdataSizeCnt = emmcDisk->sector_count - userdataStartCnt;
ret = add_mmc_partition(emmc, storageStartCnt, storageSizeCnt);
if (ret != LOS_OK) {
PRINT_ERR("Failed to add mmc storage partition!\n");
return LOS_NOK;
}
#ifndef LOSCFG_BOOTENV_RAM
CHAR *argsBak = NULL;
argsBak = args;
#endif
p = strsep(&args, " ");
while (p != NULL) {
if (MatchRootInfo(p, rootType, fsType, rootAddr, rootSize, userAddr, userSize) != LOS_OK) {
goto ERROUT;
}
p = strsep(&args, " ");
}
if ((*fsType != NULL) && (*rootType != NULL)) {
#ifndef LOSCFG_BOOTENV_RAM
free(argsBak);
#endif
return LOS_OK;
ret = add_mmc_partition(emmc, userdataStartCnt, userdataSizeCnt);
if (ret != LOS_OK) {
PRINT_ERR("Failed to add mmc userdata partition!\n");
return LOS_NOK;
}
ERROUT:
PRINT_ERR("Invalid rootfs information!\n");
if (*rootType != NULL) {
free(*rootType);
*rootType = NULL;
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 LOS_NOK;
}
if (*fsType != NULL) {
free(*fsType);
*fsType = NULL;
if (los_disk_init(node_name, StorageBlockGetMmcOps(), block, diskId, emmc) != ENOERR) {
PRINT_ERR("Failed to init emmc disk!\n");
return LOS_NOK;
}
#ifndef LOSCFG_BOOTENV_RAM
free(argsBak);
#endif
return LOS_NOK;
return LOS_OK;
}
#endif
#ifdef LOSCFG_STORAGE_EMMC
STATIC VOID OsMountUserdata(const CHAR *fsType)
STATIC INT32 AddPartitions(CHAR *dev, UINT64 rootAddr, UINT64 rootSize, UINT64 userAddr, UINT64 userSize)
{
INT32 ret;
INT32 err;
const CHAR *userdataDir = "/userdata";
ret = mkdir(userdataDir, VFAT_STORAGE_MOUNT_DIR_MODE);
if ((ret != LOS_OK) && ((err = get_errno()) != EEXIST)) {
PRINT_ERR("Failed to mkdir /userdata, errno %d: %s\n", err, strerror(err));
return;
#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
if ((strcmp(dev, "cfi-flash") == 0) && (rootAddr != CFIFLASH_ROOT_ADDR)) {
PRINT_ERR("Error rootAddr, must be %#0x!\n", CFIFLASH_ROOT_ADDR);
return LOS_NOK;
}
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 == ENOTSUP) {
#ifdef LOSCFG_FS_FAT
ret = format(emmcUserdataDev, 0, FM_FAT32);
if (ret != LOS_OK) {
PRINT_ERR("Failed to format %s\n", emmcUserdataDev);
return;
}
#endif
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;
}
#ifdef LOSCFG_STORAGE_EMMC
ret = mkdir("/storage", VFAT_STORAGE_MOUNT_DIR_MODE);
if ((ret != LOS_OK) && ((err = get_errno()) != EEXIST)) {
PRINT_ERR("Failed to mkdir /storage, errno %d: %s\n", err, strerror(err));
return ret;
} 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();
PRINT_ERR("Failed to mount rootfs,rootDev %s, errno %d: %s\n", rootDev, err, strerror(err));
return ret;
}
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND) || defined(LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7)
ret = mkdir("/storage", DEFAULT_STORAGE_MOUNT_DIR_MODE);
if ((ret != LOS_OK) && ((err = get_errno()) != EEXIST)) {
PRINT_ERR("Failed to mkdir /storage, errno %d: %s\n", err, strerror(err));
return ret;
} 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));
}
INT32 ret;
INT32 blk0 = 0;
INT32 blk2 = 2;
if (strcmp(dev, "flash") == 0 || strcmp(dev, FLASH_TYPE) == 0) {
ret = add_mtd_partition(FLASH_TYPE, rootAddr, rootSize, blk0);
if (ret != LOS_OK) {
PRINT_ERR("Failed to add mtd root partition!\n");
return LOS_NOK;
}
#endif
ret = add_mtd_partition(FLASH_TYPE, userAddr, userSize, blk2);
if (ret != LOS_OK) {
PRINT_ERR("Failed to add mtd storage partition!\n");
return LOS_NOK;
}
return LOS_OK;
}
#endif
#ifdef LOSCFG_STORAGE_EMMC
if (strcmp(dev, "emmc") == 0) {
return AddEmmcParts(rootAddr, rootSize, userAddr, userSize);
}
#endif
PRINT_ERR("Unsupport dev type: %s\n", dev);
return LOS_NOK;
}
STATIC INT32 ParseRootArgs(CHAR **dev, CHAR **fstype, UINT64 *rootAddr, UINT64 *rootSize) {
INT32 ret;
CHAR *rootAddrStr;
CHAR *rootSizeStr;
ret = LOS_GetArgValue("root", dev);
if (ret != LOS_OK) {
PRINT_ERR("Cannot find root!");
return ret;
}
ret = LOS_GetArgValue("fstype", fstype);
if (ret != LOS_OK) {
PRINT_ERR("Cannot find fstype!");
return ret;
}
ret = LOS_GetArgValue("rootaddr", &rootAddrStr);
if (ret != LOS_OK) {
*rootAddr = ROOTFS_ADDR;
} else {
*rootAddr = LOS_SizeStrToNum(rootAddrStr);
}
ret = LOS_GetArgValue("rootsize", &rootSizeStr);
if (ret != LOS_OK) {
*rootSize = ROOTFS_SIZE;
} else {
*rootSize = LOS_SizeStrToNum(rootSizeStr);
}
return LOS_OK;
}
INT32 OsMountRootfs(VOID)
{
INT32 ret = LOS_OK;
INT32 err;
INT32 rootAddr = -1;
INT32 rootSize = -1;
INT32 userAddr = -1;
INT32 userSize = -1;
CHAR *rootType = NULL;
CHAR *fsType = NULL;
const CHAR *rootDev = NULL;
STATIC INT32 ParseUserArgs(UINT64 rootAddr, UINT64 rootSize, UINT64 *userAddr, UINT64 *userSize) {
INT32 ret;
CHAR *userAddrStr;
CHAR *userSizeStr;
#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, &userAddr, &userSize);
ret = LOS_GetArgValue("useraddr", &userAddrStr);
if (ret != LOS_OK) {
*userAddr = rootAddr + rootSize;
} else {
*userAddr = LOS_SizeStrToNum(userAddrStr);
}
ret = LOS_GetArgValue("usersize", &userSizeStr);
if (ret != LOS_OK) {
*userSize = USERFS_SIZE;
} else {
*userSize = LOS_SizeStrToNum(userSizeStr);
}
return LOS_OK;
}
STATIC INT32 MountPartitions(CHAR *fsType) {
INT32 ret;
INT32 err;
/* Mount rootfs */
ret = mount(ROOT_DEV_NAME, ROOT_DIR_NAME, fsType, MS_RDONLY, NULL);
if (ret != LOS_OK) {
err = get_errno();
PRINT_ERR("Failed to mount %s, rootDev %s, errno %d: %s\n", ROOT_DIR_NAME, ROOT_DEV_NAME, err, strerror(err));
return ret;
}
/* Mount userfs */
ret = mkdir(STORAGE_DIR_NAME, DEFAULT_MOUNT_DIR_MODE);
if ((ret != LOS_OK) && ((err = get_errno()) != EEXIST)) {
PRINT_ERR("Failed to mkdir %s, errno %d: %s\n", STORAGE_DIR_NAME, err, strerror(err));
return ret;
}
ret = mount(USER_DEV_NAME, STORAGE_DIR_NAME, fsType, 0, DEFAULT_MOUNT_DATA);
if (ret != LOS_OK) {
err = get_errno();
PRINT_ERR("Failed to mount %s, errno %d: %s\n", STORAGE_DIR_NAME, err, strerror(err));
return ret;
}
#ifdef LOSCFG_STORAGE_EMMC
/* Mount userdata */
ret = mkdir(USERDATA_DIR_NAME, DEFAULT_MOUNT_DIR_MODE);
if ((ret != LOS_OK) && ((err = get_errno()) != EEXIST)) {
PRINT_ERR("Failed to mkdir %s, errno %d: %s\n", USERDATA_DIR_NAME, err, strerror(err));
return ret;
}
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, fsType, 0, DEFAULT_MOUNT_DATA);
if ((ret != LOS_OK) && ((err = get_errno()) == ENOTSUP)) {
ret = format(USERDATA_DEV_NAME, 0, FM_FAT32);
if (ret != LOS_OK) {
PRINT_ERR("Failed to format %s\n", USERDATA_DEV_NAME);
return ret;
}
ret = mount(USERDATA_DEV_NAME, USERDATA_DIR_NAME, fsType, 0, DEFAULT_MOUNT_DATA);
if (ret != LOS_OK) {
err = get_errno();
}
}
if (ret != LOS_OK) {
PRINT_ERR("Failed to mount %s, errno %d: %s\n", USERDATA_DIR_NAME, err, strerror(err));
return ret;
}
#endif
return LOS_OK;
}
STATIC INT32 CheckValidation(UINT64 rootAddr, UINT64 rootSize, UINT64 userAddr, UINT64 userSize) {
UINT64 alignSize = LOS_GetAlignsize();
if (alignSize == 0) {
return LOS_OK;
}
if ((rootAddr & (alignSize - 1)) || (rootSize & (alignSize - 1)) ||
(userAddr & (alignSize - 1)) || (userSize & (alignSize - 1))) {
PRINT_ERR("The address or size value should be 0x%x aligned!\n", alignSize);
return LOS_NOK;
}
return LOS_OK;
}
INT32 OsMountRootfs() {
INT32 ret;
CHAR *dev;
CHAR *fstype;
UINT64 rootAddr;
UINT64 rootSize;
UINT64 userAddr;
UINT64 userSize;
ret = ParseRootArgs(&dev, &fstype, &rootAddr, &rootSize);
if (ret != LOS_OK) {
return ret;
}
rootAddr = (rootAddr >= 0) ? rootAddr : ROOTFS_FLASH_ADDR;
rootSize = (rootSize >= 0) ? rootSize : ROOTFS_FLASH_SIZE;
#endif
userAddr = (userAddr >= 0) ? userAddr : rootAddr + rootSize;
userSize = (userSize >= 0) ? userSize : STORAGE_SIZE;
rootDev = GetDevName(rootType, rootAddr, rootSize, userAddr, userSize);
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));
}
ret = ParseUserArgs(rootAddr, rootSize, &userAddr, &userSize);
if (ret != LOS_OK) {
return ret;
}
free(rootType);
free(fsType);
return ret;
}
ret = CheckValidation(rootAddr, rootSize, userAddr, userSize);
if (ret != LOS_OK) {
return ret;
}
ret = AddPartitions(dev, rootAddr, rootSize, userAddr, userSize);
if (ret != LOS_OK) {
return ret;
}
ret = MountPartitions(fstype);
if (ret != LOS_OK) {
return ret;
}
return LOS_OK;
}

View File

@ -32,45 +32,52 @@
#ifndef _LOS_ROOTFS_H
#define _LOS_ROOTFS_H
#define BYTES_PER_MBYTE 0x100000
#define BYTES_PER_KBYTE 0x400
#define COMMAND_LINE_ADDR LOSCFG_BOOTENV_ADDR * BYTES_PER_KBYTE
#define COMMAND_LINE_SIZE 1024
#ifdef LOSCFG_STORAGE_SPINOR
#define ROOTFS_ROOT_TYPE "flash"
#define ROOTFS_FS_TYPE "jffs2"
#elif defined(LOSCFG_STORAGE_SPINAND)
#define ROOTFS_ROOT_TYPE "nand"
#define ROOTFS_FS_TYPE "yaffs2"
#endif
#include "los_typedef.h"
#define ROOT_DIR_NAME "/"
#define STORAGE_DIR_NAME "/storage"
#ifdef LOSCFG_STORAGE_EMMC
#define ROOTFS_ROOT_TYPE "emmc"
#define ROOTFS_FS_TYPE "vfat"
#endif
#ifdef LOSCFG_TEE_ENABLE
#define ROOTFS_FLASH_ADDR 0x600000
#define ROOTFS_FLASH_SIZE 0x800000
#else
#define ROOTFS_FLASH_ADDR 0x400000
#define ROOTFS_FLASH_SIZE 0xa00000
#define USERDATA_DIR_NAME "/userdata"
#endif
#define DEFAULT_MOUNT_DIR_MODE 0755
#define DEFAULT_MOUNT_DATA NULL
#ifdef LOSCFG_STORAGE_SPINOR
#define FLASH_TYPE "spinor"
#define FLASH_DEV_NAME "/dev/spinorblk0"
#elif defined(LOSCFG_STORAGE_SPINAND)
#define ROOT_DEV_NAME "/dev/spinorblk0"
#define USER_DEV_NAME "/dev/spinorblk2"
#define ROOTFS_ADDR 0x600000
#define ROOTFS_SIZE 0x800000
#define USERFS_SIZE 0x80000
#elif defined (LOSCFG_STORAGE_SPINAND)
#define FLASH_TYPE "nand"
#define FLASH_DEV_NAME "/dev/nandblk0"
#define ROOT_DEV_NAME "/dev/nandblk0"
#define USER_DEV_NAME "/dev/nandblk2"
#define ROOTFS_ADDR 0x600000
#define ROOTFS_SIZE 0x800000
#define USERFS_SIZE 0x80000
#elif defined (LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7)
#define ROOT_DEV_NAME "/dev/cfiflash0"
#define USER_DEV_NAME "/dev/cfiflash2"
#define ROOTFS_ADDR CFIFLASH_ROOT_ADDR
#define ROOTFS_SIZE 0x1B00000
#define USERFS_SIZE (CFIFLASH_CAPACITY - ROOTFS_ADDR - ROOTFS_SIZE)
#elif defined (LOSCFG_STORAGE_EMMC)
#define ROOT_DEV_NAME "/dev/mmcblk0p0"
#define USER_DEV_NAME "/dev/mmcblk0p1"
#define USERDATA_DEV_NAME "/dev/mmcblk0p2"
#define ROOTFS_ADDR 0xA00000
#define ROOTFS_SIZE 0x1400000
#define USERFS_SIZE 0x3200000
#ifdef DEFAULT_MOUNT_DIR_MODE
#undef DEFAULT_MOUNT_DIR_MODE
#endif
#ifdef DEFAULT_MOUNT_DATA
#undef DEFAULT_MOUNT_DATA
#endif
#define DEFAULT_MOUNT_DIR_MODE 0777
#define DEFAULT_MOUNT_DATA "umask=000"
#endif
#define EMMC_SEC_SIZE 512
#define DEC_NUMBER_STRING "0123456789"
#define HEX_NUMBER_STRING "0123456789abcdefABCDEF"
INT32 OsMountRootfs(VOID);
VOID OsSetCmdLineAddr(UINT64 addr);

View File

@ -37,7 +37,7 @@ LOCAL_INCLUDE += -I $(LITEOSTOPDIR)/compat/posix/src \
-I $(LITEOSTOPDIR)/bsd/dev/random
LOCAL_SRCS += $(wildcard ../kernel/common/*.c)
LOCAL_SRCS := $(filter-out ../kernel/common/los_rootfs.c, $(LOCAL_SRCS))
LOCAL_SRCS := $(filter-out ../kernel/common/los_rootfs.c ../kernel/common/los_bootargs.c, $(LOCAL_SRCS))
ifneq ($(LOSCFG_FS_VFS), y)
LOCAL_SRCS := $(filter-out ../kernel/common/console.c ../kernel/common/virtual_serial.c, $(LOCAL_SRCS))
endif