1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/init.h>
4 #include <linux/libfdt.h>
5 #include <linux/string.h>
6 #include <asm/pgtable.h>
7 #include <asm/setup.h>
8 
9 static char early_cmdline[COMMAND_LINE_SIZE];
10 
11 /*
12  * Declare the functions that are exported (but prefixed) here so that LLVM
13  * does not complain it lacks the 'static' keyword (which, if added, makes
14  * LLVM complain because the function is actually unused in this file).
15  */
16 u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa);
17 bool set_nokaslr_from_cmdline(uintptr_t dtb_pa);
18 
19 static char *get_early_cmdline(uintptr_t dtb_pa)
20 {
21 	const char *fdt_cmdline = NULL;
22 	unsigned int fdt_cmdline_size = 0;
23 	int chosen_node;
24 
25 	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
26 		chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
27 		if (chosen_node >= 0) {
28 			fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
29 						  "bootargs", NULL);
30 			if (fdt_cmdline) {
31 				fdt_cmdline_size = strlen(fdt_cmdline);
32 				strscpy(early_cmdline, fdt_cmdline,
33 					COMMAND_LINE_SIZE);
34 			}
35 		}
36 	}
37 
38 	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
39 	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
40 	    fdt_cmdline_size == 0 /* CONFIG_CMDLINE_FALLBACK */) {
41 		strncat(early_cmdline, CONFIG_CMDLINE,
42 			COMMAND_LINE_SIZE - fdt_cmdline_size);
43 	}
44 
45 	return early_cmdline;
46 }
47 
48 static u64 match_noXlvl(char *cmdline)
49 {
50 	if (strstr(cmdline, "no4lvl"))
51 		return SATP_MODE_48;
52 	else if (strstr(cmdline, "no5lvl"))
53 		return SATP_MODE_57;
54 
55 	return 0;
56 }
57 
58 u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa)
59 {
60 	char *cmdline = get_early_cmdline(dtb_pa);
61 
62 	return match_noXlvl(cmdline);
63 }
64 
65 static bool match_nokaslr(char *cmdline)
66 {
67 	return strstr(cmdline, "nokaslr");
68 }
69 
70 bool set_nokaslr_from_cmdline(uintptr_t dtb_pa)
71 {
72 	char *cmdline = get_early_cmdline(dtb_pa);
73 
74 	return match_nokaslr(cmdline);
75 }
76