feat: add support for gn build system
add BUILD.gn for all kernel modules Signed-off-by: Caoruihong <crh.cao@huawei.com> Change-Id: I018446427bf64615f2596d47862b219659b58b34
This commit is contained in:
44
kernel/common/rootfs/BUILD.gn
Normal file
44
kernel/common/rootfs/BUILD.gn
Normal file
@@ -0,0 +1,44 @@
|
||||
# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
# 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.
|
||||
|
||||
import("//kernel/liteos_a/liteos.gni")
|
||||
|
||||
module_switch = defined(LOSCFG_PLATFORM_ROOTFS)
|
||||
module_name = get_path_info(rebase_path("."), "name")
|
||||
kernel_module(module_name) {
|
||||
sources = [
|
||||
"los_bootargs.c",
|
||||
"los_rootfs.c",
|
||||
]
|
||||
public_configs = [ ":public" ]
|
||||
}
|
||||
|
||||
config("public") {
|
||||
include_dirs = [ "." ]
|
||||
}
|
||||
50
kernel/common/rootfs/Kconfig
Normal file
50
kernel/common/rootfs/Kconfig
Normal file
@@ -0,0 +1,50 @@
|
||||
config PLATFORM_ROOTFS
|
||||
bool "Enable ROOTFS"
|
||||
default y
|
||||
depends on FS_JFFS || FS_FAT || DRIVERS_MTD_NAND
|
||||
help
|
||||
Answer Y to enable LiteOS support rootfs.
|
||||
|
||||
choice
|
||||
prompt "Storage device type"
|
||||
default STORAGE_SPINOR
|
||||
depends on PLATFORM_ROOTFS
|
||||
help
|
||||
SPI_NOR or SPI_NAND or EMMC.
|
||||
|
||||
config STORAGE_SPINOR
|
||||
depends on PLATFORM_ROOTFS && DRIVERS_MTD_SPI_NOR
|
||||
bool "SPI_NOR Flash"
|
||||
|
||||
config STORAGE_SPINAND
|
||||
depends on PLATFORM_ROOTFS && DRIVERS_MTD_NAND
|
||||
bool "SPI_NAND Flash"
|
||||
|
||||
config STORAGE_EMMC
|
||||
depends on PLATFORM_ROOTFS && DRIVERS_EMMC
|
||||
bool "EMMC"
|
||||
|
||||
endchoice
|
||||
|
||||
config BOOTENV_ADDR
|
||||
int "Address of boot command line (KB)"
|
||||
depends on PLATFORM_ROOTFS && (STORAGE_SPINOR || STORAGE_SPINAND || STORAGE_EMMC)
|
||||
range 0 1024
|
||||
default 512
|
||||
help
|
||||
Boot command line addr, range from 0 to 1MB.
|
||||
|
||||
config BOOTENV_RAM
|
||||
bool "Read bootenv from RAM"
|
||||
default n
|
||||
depends on PLATFORM_ROOTFS
|
||||
help
|
||||
Answer Y to read bootenv from ram. Need boot copy to RAM.
|
||||
|
||||
config BOOTENV_RAMSIZE
|
||||
int "Size of boot environment in RAM (Byte)"
|
||||
depends on PLATFORM_ROOTFS && BOOTENV_RAM
|
||||
range 128 1024
|
||||
default 512
|
||||
help
|
||||
Boot environment in Ram space size, range from 128 to 1024 byte.
|
||||
46
kernel/common/rootfs/Makefile
Normal file
46
kernel/common/rootfs/Makefile
Normal file
@@ -0,0 +1,46 @@
|
||||
# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
# 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 $(LITEOSTOPDIR)/config.mk
|
||||
|
||||
MODULE_NAME := rootfs
|
||||
|
||||
LOCAL_SRCS := $(wildcard los_rootfs.c los_bootargs.c)
|
||||
|
||||
LOCAL_INCLUDE := \
|
||||
-I $(LITEOSTOPDIR)/kernel/common \
|
||||
|
||||
ifeq ($(LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7), y)
|
||||
LOCAL_INCLUDE += -I $(LITEOSTOPDIR)/../../device/qemu/drivers/cfiflash
|
||||
endif
|
||||
|
||||
LOCAL_FLAGS := $(LOCAL_INCLUDE) $(LITEOS_GCOV_OPTS)
|
||||
|
||||
include $(MODULE)
|
||||
|
||||
245
kernel/common/rootfs/los_bootargs.c
Normal file
245
kernel/common/rootfs/los_bootargs.c
Normal file
@@ -0,0 +1,245 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
61
kernel/common/rootfs/los_bootargs.h
Normal file
61
kernel/common/rootfs/los_bootargs.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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);
|
||||
|
||||
#ifdef LOSCFG_BOOTENV_RAM
|
||||
CHAR *OsGetArgsAddr(VOID);
|
||||
#endif
|
||||
#endif /* _LOS_BOOTARGS_H */
|
||||
330
kernel/common/rootfs/los_rootfs.c
Normal file
330
kernel/common/rootfs/los_rootfs.c
Normal file
@@ -0,0 +1,330 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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_rootfs.h"
|
||||
#include "los_bootargs.h"
|
||||
#include "los_base.h"
|
||||
#include "string.h"
|
||||
#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_PLATFORM_QEMU_ARM_VIRT_CA7
|
||||
#include "cfiflash.h"
|
||||
#endif
|
||||
|
||||
#ifdef LOSCFG_STORAGE_EMMC
|
||||
#include "disk.h"
|
||||
#include "ff.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef LOSCFG_STORAGE_EMMC
|
||||
struct disk_divide_info *StorageBlockGetEmmc(void);
|
||||
struct block_operations *StorageBlockGetMmcOps(void);
|
||||
char *StorageBlockGetEmmcNodeName(void *block);
|
||||
|
||||
STATIC INT32 AddEmmcParts(INT32 rootAddr, INT32 rootSize, INT32 userAddr, INT32 userSize)
|
||||
{
|
||||
INT32 ret;
|
||||
|
||||
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(emmcDisk->disk_id) != ENOERR) {
|
||||
PRINT_ERR("Failed to deinit emmc disk!\n");
|
||||
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 LOS_NOK;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
ret = add_mmc_partition(emmc, userdataStartCnt, userdataSizeCnt);
|
||||
if (ret != LOS_OK) {
|
||||
PRINT_ERR("Failed to add mmc userdata partition!\n");
|
||||
return LOS_NOK;
|
||||
}
|
||||
|
||||
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 (los_disk_init(node_name, StorageBlockGetMmcOps(), block, diskId, emmc) != ENOERR) {
|
||||
PRINT_ERR("Failed to init emmc disk!\n");
|
||||
return LOS_NOK;
|
||||
}
|
||||
|
||||
return LOS_OK;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
STATIC INT32 AddPartitions(CHAR *dev, UINT64 rootAddr, UINT64 rootSize, UINT64 userAddr, UINT64 userSize)
|
||||
{
|
||||
#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;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND) || defined(LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7)
|
||||
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;
|
||||
}
|
||||
|
||||
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, UINT32 *mountFlags) {
|
||||
INT32 ret;
|
||||
CHAR *rootAddrStr;
|
||||
CHAR *rootSizeStr;
|
||||
CHAR *rwTag;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
ret = LOS_GetArgValue("ro", &rwTag);
|
||||
if (ret == LOS_OK) {
|
||||
*mountFlags = MS_RDONLY;
|
||||
} else {
|
||||
*mountFlags = 0;
|
||||
}
|
||||
|
||||
return LOS_OK;
|
||||
}
|
||||
|
||||
STATIC INT32 ParseUserArgs(UINT64 rootAddr, UINT64 rootSize, UINT64 *userAddr, UINT64 *userSize) {
|
||||
INT32 ret;
|
||||
CHAR *userAddrStr;
|
||||
CHAR *userSizeStr;
|
||||
|
||||
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, UINT32 mountFlags) {
|
||||
INT32 ret;
|
||||
INT32 err;
|
||||
|
||||
/* Mount rootfs */
|
||||
ret = mount(ROOT_DEV_NAME, ROOT_DIR_NAME, fsType, mountFlags, 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;
|
||||
UINT32 mountFlags;
|
||||
|
||||
ret = ParseRootArgs(&dev, &fstype, &rootAddr, &rootSize, &mountFlags);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = ParseUserArgs(rootAddr, rootSize, &userAddr, &userSize);
|
||||
if (ret != LOS_OK) {
|
||||
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, mountFlags);
|
||||
if (ret != LOS_OK) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return LOS_OK;
|
||||
}
|
||||
84
kernel/common/rootfs/los_rootfs.h
Normal file
84
kernel/common/rootfs/los_rootfs.h
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* 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_ROOTFS_H
|
||||
#define _LOS_ROOTFS_H
|
||||
|
||||
#include "los_typedef.h"
|
||||
|
||||
#define ROOT_DIR_NAME "/"
|
||||
#define STORAGE_DIR_NAME "/storage"
|
||||
#ifdef LOSCFG_STORAGE_EMMC
|
||||
#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 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 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
|
||||
|
||||
INT32 OsMountRootfs(VOID);
|
||||
|
||||
#endif /* _LOS_ROOTFS_H */
|
||||
Reference in New Issue
Block a user