更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_bootargs.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "los_bootargs.h"
32#include "los_base.h"
33#include "string.h"
34
35#if defined(LOSCFG_STORAGE_SPINOR) || defined(LOSCFG_STORAGE_SPINAND) || defined(LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7)
36#include "mtd_list.h"
37#endif
38
39#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
40#include "cfiflash.h"
41#endif
42
43#ifdef LOSCFG_STORAGE_EMMC
44#include "disk.h"
45#endif
46
47STATIC CHAR *g_cmdLine = NULL;
49STATIC struct BootArgs g_bootArgs[MAX_ARGS_NUM] = {0};
50/// 读取环境变量bootcmd 来启动内核
52{
53 int ret;
54
55 g_cmdLine = (CHAR *)malloc(COMMAND_LINE_SIZE);
56 if (g_cmdLine == NULL) {
57 PRINT_ERR("Malloc g_cmdLine space error!\n");
58 return LOS_NOK;
59 }
60
61#ifdef LOSCFG_STORAGE_EMMC
62 los_disk *emmcDisk = los_get_mmcdisk_bytype(EMMC); //获取硬盘信息
63 if (emmcDisk == NULL) {
64 PRINT_ERR("Get EMMC disk failed!\n");
65 goto ERROUT;
66 }
67 g_alignSize = EMMC_SEC_SIZE;
68 ret = los_disk_read(emmcDisk->disk_id, g_cmdLine, COMMAND_LINE_ADDR / EMMC_SEC_SIZE,
69 COMMAND_LINE_SIZE / EMMC_SEC_SIZE, TRUE);//读取命令行内容
70 if (ret == 0) {
71 return LOS_OK;
72 }
73#endif
74
75#ifdef LOSCFG_STORAGE_SPINOR
76 struct MtdDev *mtd = GetMtd("spinor");//获取flash描述信息
77 if (mtd == NULL) {
78 PRINT_ERR("Get spinor mtd failed!\n");
79 goto ERROUT;
80 }
82 ret = mtd->read(mtd, COMMAND_LINE_ADDR, COMMAND_LINE_SIZE, g_cmdLine);//读取spinor下的命令行内容
83 if (ret == COMMAND_LINE_SIZE) {
84 return LOS_OK;
85 }
86#endif
87
88#ifdef LOSCFG_STORAGE_SPINAND
89 struct MtdDev *mtd = GetMtd("nand");//获取flash描述信息
90 if (mtd == NULL) {
91 PRINT_ERR("Get nand mtd failed!\n");
92 goto ERROUT;
93 }
95 ret = mtd->read(mtd, COMMAND_LINE_ADDR, COMMAND_LINE_SIZE, g_cmdLine);//读取nand下的命令行内容
96 if (ret == COMMAND_LINE_SIZE) {
97 return LOS_OK;
98 }
99#endif
100
101#ifdef LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7
102 struct MtdDev *mtd = GetCfiMtdDev();
103 if (mtd == NULL) {
104 PRINT_ERR("Get CFI mtd failed!\n");
105 goto ERROUT;
106 }
107 g_alignSize = mtd->eraseSize;
108 ret = mtd->read(mtd, CFIFLASH_BOOTARGS_ADDR, COMMAND_LINE_SIZE, g_cmdLine);
109 if (ret == COMMAND_LINE_SIZE) {
110 return LOS_OK;
111 }
112#endif
113
114 PRINT_ERR("Read cmdline error!\n");
115ERROUT:
117 g_cmdLine = NULL;
118 return LOS_NOK;
119}
120
122{
123 if (g_cmdLine != NULL) {
125 g_cmdLine = NULL;
126 }
127}
128///获取boot参数
129STATIC INT32 GetBootargs(CHAR **args)
130{
131#ifdef LOSCFG_BOOTENV_RAM
132 *args = OsGetArgsAddr();
133 return LOS_OK;
134#else
135 INT32 i;
136 INT32 len = 0;
137 CHAR *tmp = NULL;
138 const CHAR *bootargsName = "bootargs=";
139
140 if (g_cmdLine == NULL) {
141 PRINT_ERR("Should call LOS_GetCmdLine() first!\n");
142 return LOS_NOK;
143 }
144
145 for (i = 0; i < COMMAND_LINE_SIZE; i += len + 1) {
146 len = strlen(g_cmdLine + i);
147 tmp = strstr(g_cmdLine + i, bootargsName);
148 if (tmp != NULL) {
149 *args = tmp + strlen(bootargsName);
150 return LOS_OK;
151 }
152 }
153 PRINT_ERR("Cannot find bootargs!\n");
154 return LOS_NOK;
155#endif
156}
157///解析boot参数
159{
160 INT32 idx = 0;
161 INT32 ret;
162 CHAR *args = NULL;
163 CHAR *argName = NULL;
164 CHAR *argValue = NULL;
165
166 ret = GetBootargs(&args);
167 if (ret != LOS_OK) {
168 return LOS_NOK;
169 }
170
171 while ((argValue = strsep(&args, " ")) != NULL) {
172 argName = strsep(&argValue, "=");
173 if (argValue == NULL) {
174 /* If the argument is not compliance with the format 'foo=bar' */
175 g_bootArgs[idx].argName = argName;
176 g_bootArgs[idx].argValue = argName;
177 } else {
178 g_bootArgs[idx].argName = argName;
179 g_bootArgs[idx].argValue = argValue;
180 }
181 if (++idx >= MAX_ARGS_NUM) {
182 /* Discard the rest arguments */
183 break;
184 }
185 }
186 return LOS_OK;
187}
188
189INT32 LOS_GetArgValue(CHAR *argName, CHAR **argValue)
190{
191 INT32 idx = 0;
192
193 while (idx < MAX_ARGS_NUM) {
194 if (g_bootArgs[idx].argName == NULL) {
195 break;
196 }
197 if (strcmp(argName, g_bootArgs[idx].argName) == 0) {
198 *argValue = g_bootArgs[idx].argValue;
199 return LOS_OK;
200 }
201 idx++;
202 }
203
204 return LOS_NOK;
205}
206
208{
209 return g_alignSize;
210}
211
213{
214 UINT64 num = 0;
215
216 /* If the string is a hexadecimal value */
217 if (sscanf_s(value, "0x%llx", &num) > 0) {
218 value += strlen("0x");
219 if (strspn(value, "0123456789abcdefABCDEF") < strlen(value)) {
220 goto ERROUT;
221 }
222 return num;
223 }
224
225 /* If the string is a decimal value in unit *Bytes */
226 INT32 ret = sscanf_s(value, "%d", &num);
227 INT32 decOffset = strspn(value, "0123456789");
228 CHAR *endPos = value + decOffset;
229 if ((ret <= 0) || (decOffset < (strlen(value) - 1))) {
230 goto ERROUT;
231 }
232
233 if (strlen(endPos) == 0) {
234 return num;
235 } else if (strcasecmp(endPos, "k") == 0) {
236 num = num * BYTES_PER_KBYTE;
237 } else if (strcasecmp(endPos, "m") == 0) {
238 num = num * BYTES_PER_MBYTE;
239 } else if (strcasecmp(endPos, "g") == 0) {
240 num = num * BYTES_PER_GBYTE;
241 } else {
242 goto ERROUT;
243 }
244
245 return num;
246
247ERROUT:
248 PRINT_ERR("Invalid value string \"%s\"!\n", value);
249 return num;
250}
los_disk * los_get_mmcdisk_bytype(UINT8 type)
Definition: disk.c:226
INT32 los_disk_read(INT32 drvID, VOID *buf, UINT64 sector, UINT32 count, BOOL useRead)
Read data from disk driver.
Definition: disk.c:911
STATIC INT32 GetBootargs(CHAR **args)
获取boot参数
Definition: los_bootargs.c:129
STATIC struct BootArgs g_bootArgs[MAX_ARGS_NUM]
Definition: los_bootargs.c:49
STATIC UINT64 g_alignSize
Definition: los_bootargs.c:48
VOID LOS_FreeCmdLine(VOID)
Definition: los_bootargs.c:121
UINT64 LOS_GetAlignsize(VOID)
Definition: los_bootargs.c:207
STATIC CHAR * g_cmdLine
Definition: los_bootargs.c:47
INT32 LOS_GetCmdLine(VOID)
读取环境变量bootcmd 来启动内核
Definition: los_bootargs.c:51
INT32 LOS_GetArgValue(CHAR *argName, CHAR **argValue)
Definition: los_bootargs.c:189
INT32 LOS_ParseBootargs(VOID)
解析boot参数
Definition: los_bootargs.c:158
UINT64 LOS_SizeStrToNum(CHAR *value)
Definition: los_bootargs.c:212
CHAR * OsGetArgsAddr(VOID)
signed int INT32
Definition: los_typedef.h:60
long unsigned int UINT64
Definition: los_typedef.h:66
char CHAR
Definition: los_typedef.h:63
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
void * GetMtd(const char *type)
UINT32 disk_id
Definition: disk.h:177
CHAR * argValue
Definition: los_bootargs.h:48
CHAR * argName
Definition: los_bootargs.h:47
flash MTD 层 描述符
Definition: mtd_dev.h:76
UINT32 eraseSize
4K, 跟PAGE_CACHE_SIZE对应
Definition: mtd_dev.h:81
int(* read)(struct MtdDev *mtd, UINT64 start, UINT64 len, const char *buf)
读操作
Definition: mtd_dev.h:84