10d163575Sopenharmony_ci/* 20d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 30d163575Sopenharmony_ci * 40d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 50d163575Sopenharmony_ci * are permitted provided that the following conditions are met: 60d163575Sopenharmony_ci * 70d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 80d163575Sopenharmony_ci * conditions and the following disclaimer. 90d163575Sopenharmony_ci * 100d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 110d163575Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 120d163575Sopenharmony_ci * provided with the distribution. 130d163575Sopenharmony_ci * 140d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 150d163575Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 160d163575Sopenharmony_ci * permission. 170d163575Sopenharmony_ci * 180d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 190d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 200d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 210d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 220d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 230d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 240d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 250d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 260d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 270d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 280d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 290d163575Sopenharmony_ci */ 300d163575Sopenharmony_ci 310d163575Sopenharmony_ci#include "los_bootargs.h" 320d163575Sopenharmony_ci#include "los_base.h" 330d163575Sopenharmony_ci#include "string.h" 340d163575Sopenharmony_ci 350d163575Sopenharmony_ci#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND) 360d163575Sopenharmony_ci#include "mtd_list.h" 370d163575Sopenharmony_ci#endif 380d163575Sopenharmony_ci 390d163575Sopenharmony_ci#ifdef LOSCFG_STORAGE_EMMC 400d163575Sopenharmony_ci#include "disk.h" 410d163575Sopenharmony_ci#endif 420d163575Sopenharmony_ci 430d163575Sopenharmony_ciSTATIC CHAR *g_cmdLine = NULL; 440d163575Sopenharmony_ciSTATIC UINT64 g_alignSize = 0; 450d163575Sopenharmony_ciSTATIC struct BootArgs g_bootArgs[MAX_ARGS_NUM] = {0}; 460d163575Sopenharmony_ci 470d163575Sopenharmony_ciINT32 LOS_GetCmdLine(VOID) 480d163575Sopenharmony_ci{ 490d163575Sopenharmony_ci int ret; 500d163575Sopenharmony_ci 510d163575Sopenharmony_ci g_cmdLine = (CHAR *)malloc(COMMAND_LINE_SIZE); 520d163575Sopenharmony_ci if (g_cmdLine == NULL) { 530d163575Sopenharmony_ci PRINT_ERR("Malloc g_cmdLine space error!\n"); 540d163575Sopenharmony_ci return LOS_NOK; 550d163575Sopenharmony_ci } 560d163575Sopenharmony_ci 570d163575Sopenharmony_ci#ifdef LOSCFG_STORAGE_EMMC 580d163575Sopenharmony_ci los_disk *emmcDisk = los_get_mmcdisk_bytype(EMMC); 590d163575Sopenharmony_ci if (emmcDisk == NULL) { 600d163575Sopenharmony_ci PRINT_ERR("Get EMMC disk failed!\n"); 610d163575Sopenharmony_ci goto ERROUT; 620d163575Sopenharmony_ci } 630d163575Sopenharmony_ci g_alignSize = EMMC_SEC_SIZE; 640d163575Sopenharmony_ci ret = los_disk_read(emmcDisk->disk_id, g_cmdLine, COMMAND_LINE_ADDR / EMMC_SEC_SIZE, 650d163575Sopenharmony_ci COMMAND_LINE_SIZE / EMMC_SEC_SIZE, TRUE); 660d163575Sopenharmony_ci if (ret == 0) { 670d163575Sopenharmony_ci return LOS_OK; 680d163575Sopenharmony_ci } 690d163575Sopenharmony_ci#endif 700d163575Sopenharmony_ci 710d163575Sopenharmony_ci#ifdef LOSCFG_STORAGE_SPINOR 720d163575Sopenharmony_ci struct MtdDev *mtd = GetMtd("spinor"); 730d163575Sopenharmony_ci if (mtd == NULL) { 740d163575Sopenharmony_ci PRINT_ERR("Get spinor mtd failed!\n"); 750d163575Sopenharmony_ci goto ERROUT; 760d163575Sopenharmony_ci } 770d163575Sopenharmony_ci g_alignSize = mtd->eraseSize; 780d163575Sopenharmony_ci ret = mtd->read(mtd, COMMAND_LINE_ADDR, COMMAND_LINE_SIZE, g_cmdLine); 790d163575Sopenharmony_ci if (ret == COMMAND_LINE_SIZE) { 800d163575Sopenharmony_ci return LOS_OK; 810d163575Sopenharmony_ci } 820d163575Sopenharmony_ci#endif 830d163575Sopenharmony_ci 840d163575Sopenharmony_ci#ifdef LOSCFG_STORAGE_SPINAND 850d163575Sopenharmony_ci struct MtdDev *mtd = GetMtd("nand"); 860d163575Sopenharmony_ci if (mtd == NULL) { 870d163575Sopenharmony_ci PRINT_ERR("Get nand mtd failed!\n"); 880d163575Sopenharmony_ci goto ERROUT; 890d163575Sopenharmony_ci } 900d163575Sopenharmony_ci g_alignSize = mtd->eraseSize; 910d163575Sopenharmony_ci ret = mtd->read(mtd, COMMAND_LINE_ADDR, COMMAND_LINE_SIZE, g_cmdLine); 920d163575Sopenharmony_ci if (ret == COMMAND_LINE_SIZE) { 930d163575Sopenharmony_ci return LOS_OK; 940d163575Sopenharmony_ci } 950d163575Sopenharmony_ci#endif 960d163575Sopenharmony_ci 970d163575Sopenharmony_ci PRINT_ERR("Read cmdline error!\n"); 980d163575Sopenharmony_ciERROUT: 990d163575Sopenharmony_ci free(g_cmdLine); 1000d163575Sopenharmony_ci g_cmdLine = NULL; 1010d163575Sopenharmony_ci return LOS_NOK; 1020d163575Sopenharmony_ci} 1030d163575Sopenharmony_ci 1040d163575Sopenharmony_ciVOID LOS_FreeCmdLine(VOID) 1050d163575Sopenharmony_ci{ 1060d163575Sopenharmony_ci if (g_cmdLine != NULL) { 1070d163575Sopenharmony_ci free(g_cmdLine); 1080d163575Sopenharmony_ci g_cmdLine = NULL; 1090d163575Sopenharmony_ci } 1100d163575Sopenharmony_ci} 1110d163575Sopenharmony_ci 1120d163575Sopenharmony_ciSTATIC INT32 GetBootargs(CHAR **args) 1130d163575Sopenharmony_ci{ 1140d163575Sopenharmony_ci#ifdef LOSCFG_BOOTENV_RAM 1150d163575Sopenharmony_ci *args = OsGetArgsAddr(); 1160d163575Sopenharmony_ci return LOS_OK; 1170d163575Sopenharmony_ci#else 1180d163575Sopenharmony_ci INT32 i; 1190d163575Sopenharmony_ci INT32 len = 0; 1200d163575Sopenharmony_ci CHAR *tmp = NULL; 1210d163575Sopenharmony_ci const CHAR *bootargsName = "bootargs="; 1220d163575Sopenharmony_ci 1230d163575Sopenharmony_ci if (g_cmdLine == NULL) { 1240d163575Sopenharmony_ci PRINT_ERR("Should call LOS_GetCmdLine() first!\n"); 1250d163575Sopenharmony_ci return LOS_NOK; 1260d163575Sopenharmony_ci } 1270d163575Sopenharmony_ci 1280d163575Sopenharmony_ci for (i = 0; i < COMMAND_LINE_SIZE; i += len + 1) { 1290d163575Sopenharmony_ci len = strlen(g_cmdLine + i); 1300d163575Sopenharmony_ci tmp = strstr(g_cmdLine + i, bootargsName); 1310d163575Sopenharmony_ci if (tmp != NULL) { 1320d163575Sopenharmony_ci *args = tmp + strlen(bootargsName); 1330d163575Sopenharmony_ci return LOS_OK; 1340d163575Sopenharmony_ci } 1350d163575Sopenharmony_ci } 1360d163575Sopenharmony_ci PRINT_ERR("Cannot find bootargs!\n"); 1370d163575Sopenharmony_ci return LOS_NOK; 1380d163575Sopenharmony_ci#endif 1390d163575Sopenharmony_ci} 1400d163575Sopenharmony_ci 1410d163575Sopenharmony_ciINT32 LOS_ParseBootargs(VOID) 1420d163575Sopenharmony_ci{ 1430d163575Sopenharmony_ci INT32 idx = 0; 1440d163575Sopenharmony_ci INT32 ret; 1450d163575Sopenharmony_ci CHAR *args = NULL; 1460d163575Sopenharmony_ci CHAR *argName = NULL; 1470d163575Sopenharmony_ci CHAR *argValue = NULL; 1480d163575Sopenharmony_ci 1490d163575Sopenharmony_ci ret = GetBootargs(&args); 1500d163575Sopenharmony_ci if (ret != LOS_OK) { 1510d163575Sopenharmony_ci return LOS_NOK; 1520d163575Sopenharmony_ci } 1530d163575Sopenharmony_ci 1540d163575Sopenharmony_ci while ((argValue = strsep(&args, " ")) != NULL) { 1550d163575Sopenharmony_ci argName = strsep(&argValue, "="); 1560d163575Sopenharmony_ci if (argValue == NULL) { 1570d163575Sopenharmony_ci /* If the argument is not compliance with the format 'foo=bar' */ 1580d163575Sopenharmony_ci g_bootArgs[idx].argName = argName; 1590d163575Sopenharmony_ci g_bootArgs[idx].argValue = argName; 1600d163575Sopenharmony_ci } else { 1610d163575Sopenharmony_ci g_bootArgs[idx].argName = argName; 1620d163575Sopenharmony_ci g_bootArgs[idx].argValue = argValue; 1630d163575Sopenharmony_ci } 1640d163575Sopenharmony_ci if (++idx >= MAX_ARGS_NUM) { 1650d163575Sopenharmony_ci /* Discard the rest arguments */ 1660d163575Sopenharmony_ci break; 1670d163575Sopenharmony_ci } 1680d163575Sopenharmony_ci } 1690d163575Sopenharmony_ci return LOS_OK; 1700d163575Sopenharmony_ci} 1710d163575Sopenharmony_ci 1720d163575Sopenharmony_ciINT32 LOS_GetArgValue(CHAR *argName, CHAR **argValue) 1730d163575Sopenharmony_ci{ 1740d163575Sopenharmony_ci INT32 idx = 0; 1750d163575Sopenharmony_ci 1760d163575Sopenharmony_ci while (idx < MAX_ARGS_NUM) { 1770d163575Sopenharmony_ci if (g_bootArgs[idx].argName == NULL) { 1780d163575Sopenharmony_ci break; 1790d163575Sopenharmony_ci } 1800d163575Sopenharmony_ci if (strcmp(argName, g_bootArgs[idx].argName) == 0) { 1810d163575Sopenharmony_ci *argValue = g_bootArgs[idx].argValue; 1820d163575Sopenharmony_ci return LOS_OK; 1830d163575Sopenharmony_ci } 1840d163575Sopenharmony_ci idx++; 1850d163575Sopenharmony_ci } 1860d163575Sopenharmony_ci 1870d163575Sopenharmony_ci return LOS_NOK; 1880d163575Sopenharmony_ci} 1890d163575Sopenharmony_ci 1900d163575Sopenharmony_ciUINT64 LOS_GetAlignsize(VOID) 1910d163575Sopenharmony_ci{ 1920d163575Sopenharmony_ci return g_alignSize; 1930d163575Sopenharmony_ci} 1940d163575Sopenharmony_ci 1950d163575Sopenharmony_ciUINT64 LOS_SizeStrToNum(CHAR *value) 1960d163575Sopenharmony_ci{ 1970d163575Sopenharmony_ci UINT64 num = 0; 1980d163575Sopenharmony_ci 1990d163575Sopenharmony_ci /* If the string is a hexadecimal value */ 2000d163575Sopenharmony_ci if (sscanf_s(value, "0x%llx", &num) > 0) { 2010d163575Sopenharmony_ci value += strlen("0x"); 2020d163575Sopenharmony_ci if (strspn(value, "0123456789abcdefABCDEF") < strlen(value)) { 2030d163575Sopenharmony_ci goto ERROUT; 2040d163575Sopenharmony_ci } 2050d163575Sopenharmony_ci return num; 2060d163575Sopenharmony_ci } 2070d163575Sopenharmony_ci 2080d163575Sopenharmony_ci /* If the string is a decimal value in unit *Bytes */ 2090d163575Sopenharmony_ci INT32 ret = sscanf_s(value, "%d", &num); 2100d163575Sopenharmony_ci INT32 decOffset = strspn(value, "0123456789"); 2110d163575Sopenharmony_ci CHAR *endPos = value + decOffset; 2120d163575Sopenharmony_ci if ((ret <= 0) || (decOffset < (strlen(value) - 1))) { 2130d163575Sopenharmony_ci goto ERROUT; 2140d163575Sopenharmony_ci } 2150d163575Sopenharmony_ci 2160d163575Sopenharmony_ci if (strlen(endPos) == 0) { 2170d163575Sopenharmony_ci return num; 2180d163575Sopenharmony_ci } else if (strcasecmp(endPos, "k") == 0) { 2190d163575Sopenharmony_ci num = num * BYTES_PER_KBYTE; 2200d163575Sopenharmony_ci } else if (strcasecmp(endPos, "m") == 0) { 2210d163575Sopenharmony_ci num = num * BYTES_PER_MBYTE; 2220d163575Sopenharmony_ci } else if (strcasecmp(endPos, "g") == 0) { 2230d163575Sopenharmony_ci num = num * BYTES_PER_GBYTE; 2240d163575Sopenharmony_ci } else { 2250d163575Sopenharmony_ci goto ERROUT; 2260d163575Sopenharmony_ci } 2270d163575Sopenharmony_ci 2280d163575Sopenharmony_ci return num; 2290d163575Sopenharmony_ci 2300d163575Sopenharmony_ciERROUT: 2310d163575Sopenharmony_ci PRINT_ERR("Invalid value string \"%s\"!\n", value); 2320d163575Sopenharmony_ci return num; 2330d163575Sopenharmony_ci} 234