• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
47 STATIC CHAR *g_cmdLine = NULL;
48 STATIC UINT64 g_alignSize = 0;
49 STATIC struct BootArgs g_bootArgs[MAX_ARGS_NUM] = {0};
50 
LOS_GetCmdLine(VOID)51 INT32 LOS_GetCmdLine(VOID)
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");
77     if (mtd == NULL) {
78         PRINT_ERR("Get spinor mtd failed!\n");
79         goto ERROUT;
80     }
81     g_alignSize = mtd->eraseSize;
82     ret = mtd->read(mtd, COMMAND_LINE_ADDR, COMMAND_LINE_SIZE, g_cmdLine);
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");
90     if (mtd == NULL) {
91         PRINT_ERR("Get nand mtd failed!\n");
92         goto ERROUT;
93     }
94     g_alignSize = mtd->eraseSize;
95     ret = mtd->read(mtd, COMMAND_LINE_ADDR, COMMAND_LINE_SIZE, g_cmdLine);
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");
115 ERROUT:
116     free(g_cmdLine);
117     g_cmdLine = NULL;
118     return LOS_NOK;
119 }
120 
LOS_FreeCmdLine(VOID)121 VOID LOS_FreeCmdLine(VOID)
122 {
123     if (g_cmdLine != NULL) {
124         free(g_cmdLine);
125         g_cmdLine = NULL;
126     }
127 }
128 
GetBootargs(CHAR ** args)129 STATIC 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 
LOS_ParseBootargs(VOID)158 INT32 LOS_ParseBootargs(VOID)
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 
LOS_GetArgValue(CHAR * argName,CHAR ** argValue)189 INT32 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 
LOS_GetAlignsize(VOID)207 UINT64 LOS_GetAlignsize(VOID)
208 {
209     return g_alignSize;
210 }
211 
LOS_SizeStrToNum(CHAR * value)212 UINT64 LOS_SizeStrToNum(CHAR *value)
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 
247 ERROUT:
248     PRINT_ERR("Invalid value string \"%s\"!\n", value);
249     return num;
250 }
251