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 
get_early_cmdline(uintptr_t dtb_pa)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 		strlcat(early_cmdline, CONFIG_CMDLINE, COMMAND_LINE_SIZE);
42 	}
43 
44 	return early_cmdline;
45 }
46 
match_noXlvl(char * cmdline)47 static u64 match_noXlvl(char *cmdline)
48 {
49 	if (strstr(cmdline, "no4lvl"))
50 		return SATP_MODE_48;
51 	else if (strstr(cmdline, "no5lvl"))
52 		return SATP_MODE_57;
53 
54 	return 0;
55 }
56 
set_satp_mode_from_cmdline(uintptr_t dtb_pa)57 u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa)
58 {
59 	char *cmdline = get_early_cmdline(dtb_pa);
60 
61 	return match_noXlvl(cmdline);
62 }
63 
match_nokaslr(char * cmdline)64 static bool match_nokaslr(char *cmdline)
65 {
66 	return strstr(cmdline, "nokaslr");
67 }
68 
set_nokaslr_from_cmdline(uintptr_t dtb_pa)69 bool set_nokaslr_from_cmdline(uintptr_t dtb_pa)
70 {
71 	char *cmdline = get_early_cmdline(dtb_pa);
72 
73 	return match_nokaslr(cmdline);
74 }
75