1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mos/platform/platform.h"
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(str1: arg, str2: "true") == 0 || strcmp(str1: arg, str2: "1") == 0 || strcmp(str1: arg, str2: "yes") == 0 || strcmp(str1: arg, str2: "on") == 0;
14}
15
16static bool cmdline_is_falsy(const char *arg)
17{
18 return strcmp(str1: arg, str2: "false") == 0 || strcmp(str1: arg, str2: "0") == 0 || strcmp(str1: arg, str2: "no") == 0 || strcmp(str1: arg, str2: "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(str1: platform_info->cmdlines[i].name, str2: 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;
40 if (MOS_EXTRA_CMDLINE)
41 {
42 cmdline_len = strlen(MOS_EXTRA_CMDLINE);
43 memcpy(dest: cmdline_buf, MOS_EXTRA_CMDLINE, n: 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(dest: cmdline_buf + cmdline_len, src: cmdline, n: strlen(str: cmdline));
56 cmdline_len += strlen(str: 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(inbuf: cmdline_buf, length: cmdline_len, MOS_MAX_CMDLINE_COUNT, cmdlines_count: &platform_info->n_cmdlines, 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(s: cmdlines_tmp[i], c: '=');
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}
98