1From 7bd94a64cd5424e74ad49dbda65a15e83670268f Mon Sep 17 00:00:00 2001
2From: Andrea Adami <andrea.adami@gmail.com>
3Date: Mon, 17 Dec 2018 11:25:20 +0100
4Subject: [PATCH] kexec-tools: fix non-device tree devices on mips
5
6Add additional argument '--no-dtb' which disables device tree
7search in currently loaded kernel.
8
9Taken from LEDE-DEV:
10https://patchwork.ozlabs.org/patch/852961/
11
12Rebased for kexec-tools 2.0.18
13Removed ppc change (unwanted ?)
14
15Signed-off-by: Konstantin Kuzov <master.nosferatu@gmail.com>
16Signed-off-by: Andrea Adami <andrea.adami@gmail.com>
17
18---
19 kexec/arch/mips/include/arch/options.h |  4 +-
20 kexec/arch/mips/kexec-elf-mips.c       | 58 ++++++++++++++------------
21 kexec/arch/mips/kexec-mips.c           |  4 ++
22 kexec/arch/mips/kexec-mips.h           |  1 +
23 4 files changed, 39 insertions(+), 28 deletions(-)
24
25diff --git a/kexec/arch/mips/include/arch/options.h b/kexec/arch/mips/include/arch/options.h
26index 416e224..18d2811 100644
27--- a/kexec/arch/mips/include/arch/options.h
28+++ b/kexec/arch/mips/include/arch/options.h
29@@ -5,6 +5,7 @@
30 #define OPT_APPEND	(OPT_ARCH_MAX+0)
31 #define OPT_DTB		(OPT_ARCH_MAX+1)
32 #define OPT_RAMDISK	(OPT_ARCH_MAX+2)
33+#define OPT_NO_DTB	(OPT_ARCH_MAX+3)
34
35 /* Options relevant to the architecture (excluding loader-specific ones),
36  * in this case none:
37@@ -14,7 +15,8 @@
38 	{"command-line", 1, 0, OPT_APPEND}, \
39 	{"append",	 1, 0, OPT_APPEND}, \
40 	{"dtb",		1, 0, OPT_DTB }, \
41-	{"initrd",	1, 0, OPT_RAMDISK },
42+	{"initrd",	1, 0, OPT_RAMDISK }, \
43+	{"no-dtb",	0, 0, OPT_NO_DTB },
44
45
46 #define KEXEC_ARCH_OPT_STR KEXEC_OPT_STR ""
47diff --git a/kexec/arch/mips/kexec-elf-mips.c b/kexec/arch/mips/kexec-elf-mips.c
48index 849a7ba..5c0e535 100644
49--- a/kexec/arch/mips/kexec-elf-mips.c
50+++ b/kexec/arch/mips/kexec-elf-mips.c
51@@ -141,35 +141,37 @@ int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
52 	else
53 		cmdline_addr = 0;
54
55-	/* MIPS systems that have been converted to use device tree
56-	 * passed through UHI will use commandline in the DTB and
57-	 * the DTB passed as a separate buffer. Note that
58-	 * CMDLINE_PREFIX is skipped here intentionally, as it is
59-	 * used only in the legacy method */
60-
61-	if (arch_options.dtb_file) {
62-		dtb_buf = slurp_file(arch_options.dtb_file, &dtb_length);
63-	} else {
64-		create_flatten_tree(&dtb_buf, &dtb_length, cmdline_buf + strlen(CMDLINE_PREFIX));
65-	}
66
67-	if (arch_options.initrd_file) {
68-		initrd_buf = slurp_file(arch_options.initrd_file, &initrd_size);
69+	if (!arch_options.no_dtb) {
70+		/* MIPS systems that have been converted to use device tree
71+		 * passed through UHI will use commandline in the DTB and
72+		 * the DTB passed as a separate buffer. Note that
73+		 * CMDLINE_PREFIX is skipped here intentionally, as it is
74+		 * used only in the legacy method */
75+
76+		if (arch_options.dtb_file) {
77+			dtb_buf = slurp_file(arch_options.dtb_file, &dtb_length);
78+		} else {
79+			create_flatten_tree(&dtb_buf, &dtb_length, cmdline_buf + strlen(CMDLINE_PREFIX));
80+		}
81
82-		/* Create initrd entries in dtb - although at this time
83-		 * they would not point to the correct location */
84-		dtb_set_initrd(&dtb_buf, &dtb_length, initrd_buf, initrd_buf + initrd_size);
85+		if (arch_options.initrd_file) {
86+			initrd_buf = slurp_file(arch_options.initrd_file, &initrd_size);
87
88-		initrd_base = add_buffer(info, initrd_buf, initrd_size,
89-					initrd_size, sizeof(void *),
90-					_ALIGN_UP(kernel_addr + kernel_size + dtb_length,
91-						pagesize), 0x0fffffff, 1);
92+			/* Create initrd entries in dtb - although at this time
93+			 * they would not point to the correct location */
94+			dtb_set_initrd(&dtb_buf, &dtb_length, initrd_buf, initrd_buf + initrd_size);
95
96-		/* Now that the buffer for initrd is prepared, update the dtb
97-		 * with an appropriate location */
98-		dtb_set_initrd(&dtb_buf, &dtb_length, initrd_base, initrd_base + initrd_size);
99-	}
100+			initrd_base = add_buffer(info, initrd_buf, initrd_size,
101+						initrd_size, sizeof(void *),
102+						_ALIGN_UP(kernel_addr + kernel_size + dtb_length,
103+							pagesize), 0x0fffffff, 1);
104
105+			/* Now that the buffer for initrd is prepared, update the dtb
106+			 * with an appropriate location */
107+			dtb_set_initrd(&dtb_buf, &dtb_length, initrd_base, initrd_base + initrd_size);
108+		}
109+	}
110
111 	/* This is a legacy method for commandline passing used
112 	 * currently by Octeon CPUs only */
113@@ -177,9 +179,11 @@ int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
114 			sizeof(cmdline_buf), sizeof(void *),
115 			cmdline_addr, 0x0fffffff, 1);
116
117-	add_buffer(info, dtb_buf, dtb_length, dtb_length, 0,
118-		_ALIGN_UP(kernel_addr + kernel_size, pagesize),
119-		0x0fffffff, 1);
120+	if (!arch_options.no_dtb) {
121+		add_buffer(info, dtb_buf, dtb_length, dtb_length, 0,
122+			_ALIGN_UP(kernel_addr + kernel_size, pagesize),
123+			0x0fffffff, 1);
124+	}
125
126 	return 0;
127 }
128diff --git a/kexec/arch/mips/kexec-mips.c b/kexec/arch/mips/kexec-mips.c
129index 415c2ed..e557f8b 100644
130--- a/kexec/arch/mips/kexec-mips.c
131+++ b/kexec/arch/mips/kexec-mips.c
132@@ -89,6 +89,7 @@ void arch_usage(void)
133 	"    --append=STRING       Set the kernel command line to STRING.\n"
134 	"    --dtb=FILE            Use FILE as the device tree blob.\n"
135 	"    --initrd=FILE         Use FILE as initial ramdisk.\n"
136+	"    --no-dtb              Don't try to find device tree\n"
137 	);
138 }
139
140@@ -121,6 +122,9 @@ int arch_process_options(int argc, char **argv)
141 		case OPT_RAMDISK:
142 			arch_options.initrd_file = optarg;
143 			break;
144+		case OPT_NO_DTB:
145+			arch_options.no_dtb = 1;
146+			break;
147 		default:
148 			break;
149 		}
150diff --git a/kexec/arch/mips/kexec-mips.h b/kexec/arch/mips/kexec-mips.h
151index 222c815..90b21c3 100644
152--- a/kexec/arch/mips/kexec-mips.h
153+++ b/kexec/arch/mips/kexec-mips.h
154@@ -22,6 +22,7 @@ struct arch_options_t {
155 	char *dtb_file;
156 	char *initrd_file;
157 	int core_header_type;
158+	int no_dtb;
159 };
160
161 extern struct memory_ranges usablemem_rgns;
162