MOS Source Code
Loading...
Searching...
No Matches
cmdline.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
4
5#include <mos/lib/cmdline.h>
6#include <mos/misc/cmdline.h>
7#include <mos/misc/kallsyms.h>
8#include <mos/syslog/printk.h>
9#include <mos_string.h>
10
11static bool cmdline_is_truthy(const char *arg)
12{
13 return strcmp(arg, "true") == 0 || strcmp(arg, "1") == 0 || strcmp(arg, "yes") == 0 || strcmp(arg, "on") == 0;
14}
15
16static bool cmdline_is_falsy(const char *arg)
17{
18 return strcmp(arg, "false") == 0 || strcmp(arg, "0") == 0 || strcmp(arg, "no") == 0 || strcmp(arg, "off") == 0;
19}
20
21cmdline_option_t *cmdline_get_option(const char *option_name)
22{
23 for (u32 i = 0; i < platform_info->n_cmdlines; i++)
24 {
25 if (strcmp(platform_info->cmdlines[i].name, option_name) == 0)
26 {
27 return &platform_info->cmdlines[i];
28 }
29 }
30 return NULL;
31}
32
33void mos_cmdline_init(const char *cmdline)
34{
35 // must be static so that it doesn't get freed
36 static char cmdline_buf[MOS_PRINTK_BUFFER_SIZE];
37
38 // first we copy EXTRA_CMDLINE to cmdline_buf
39 size_t cmdline_len = 0;
41 {
42 cmdline_len = strlen(MOS_EXTRA_CMDLINE);
43 memcpy(cmdline_buf, MOS_EXTRA_CMDLINE, cmdline_len);
44 }
45
46 // then we append cmdline
47 if (cmdline)
48 {
49 if (cmdline_len > 0)
50 {
51 cmdline_buf[cmdline_len] = ' ';
52 cmdline_len++;
53 }
54
55 memcpy(cmdline_buf + cmdline_len, cmdline, strlen(cmdline));
56 cmdline_len += strlen(cmdline);
57 }
58
59 cmdline_buf[cmdline_len] = '\0'; // ensure null terminator
60
61 pr_dinfo2(setup, "cmdline: '%s'", cmdline_buf);
62
63 const char *cmdlines_tmp[MOS_MAX_CMDLINE_COUNT] = { 0 };
64 bool result = cmdline_parse_inplace(cmdline_buf, cmdline_len, MOS_MAX_CMDLINE_COUNT, &platform_info->n_cmdlines, cmdlines_tmp);
65 if (!result)
66 pr_warn("cmdline_parse: too many cmdlines");
67
68 for (size_t i = 0; i < platform_info->n_cmdlines; i++)
69 {
70 pr_dinfo2(setup, "%s", cmdlines_tmp[i]);
71 platform_info->cmdlines[i].name = cmdlines_tmp[i];
72
73 // find the = sign
74 char *equal_sign = strchr(cmdlines_tmp[i], '=');
75 if (!equal_sign)
76 continue;
77
78 *equal_sign = '\0';
79 platform_info->cmdlines[i].arg = equal_sign + 1;
80 }
81}
82
83bool cmdline_string_truthiness(const char *arg, bool default_value)
84{
85 const char *func = mos_caller();
86 func = func ? func : "";
87
88 if (unlikely(!arg))
89 return default_value;
90
91 if (cmdline_is_truthy(arg))
92 return true;
93 else if (cmdline_is_falsy(arg))
94 return false;
95 else
96 return default_value;
97}
#define MOS_PRINTK_BUFFER_SIZE
Definition autoconf.h:19
#define MOS_EXTRA_CMDLINE
Definition autoconf.h:14
#define MOS_MAX_CMDLINE_COUNT
Definition autoconf.h:24
MOSAPI char * strchr(const char *s, int c)
Definition mos_string.c:236
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
#define mos_caller()
Definition kallsyms.h:15
bool cmdline_parse_inplace(char *inbuf, size_t length, size_t cmdline_max, size_t *out_count, const char **out_cmdlines)
Definition cmdline.c:93
static bool cmdline_is_falsy(const char *arg)
Definition cmdline.c:16
bool cmdline_string_truthiness(const char *arg, bool default_value)
Definition cmdline.c:83
void mos_cmdline_init(const char *cmdline)
Definition cmdline.c:33
static bool cmdline_is_truthy(const char *arg)
Definition cmdline.c:11
cmdline_option_t * cmdline_get_option(const char *option_name)
Definition cmdline.c:21
#define unlikely(x)
Definition mos_global.h:40
static void * memcpy(void *s1, const void *s2, size_t n)
Definition pb_syshdr.h:90
#define NULL
Definition pb_syshdr.h:46
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
#define pr_warn(fmt,...)
Definition printk.h:38
#define pr_dinfo2(feat, fmt,...)
Definition printk.h:27
mos_platform_info_t *const platform_info
const char * name
Definition cmdline.h:9
const char * arg
Definition cmdline.h:10
cmdline_option_t cmdlines[MOS_MAX_CMDLINE_COUNT]
Definition platform.h:130
unsigned int u32
Definition types.h:21