xref: /f-stack/freebsd/arm64/arm64/machdep_boot.c (revision 22ce4aff)
1 /*-
2  * Copyright (c) 2004 Olivier Houchard
3  * Copyright (c) 1994-1998 Mark Brinicombe.
4  * Copyright (c) 1994 Brini.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "opt_platform.h"
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/ctype.h>
37 #include <sys/linker.h>
38 #include <sys/reboot.h>
39 #include <sys/sysctl.h>
40 #ifdef FDT
41 #include <sys/boot.h>
42 #endif
43 
44 #include <machine/cpu.h>
45 #include <machine/machdep.h>
46 #include <machine/metadata.h>
47 #include <machine/vmparam.h>
48 
49 #ifdef FDT
50 #include <contrib/libfdt/libfdt.h>
51 #include <dev/fdt/fdt_common.h>
52 #endif
53 
54 extern int *end;
55 static char *loader_envp;
56 static char static_kenv[4096];
57 
58 #ifdef FDT
59 #define	CMDLINE_GUARD "FreeBSD:"
60 #define	LBABI_MAX_COMMAND_LINE 512
61 static char linux_command_line[LBABI_MAX_COMMAND_LINE + 1];
62 #endif
63 
64 /*
65  * Fake up a boot descriptor table
66  */
67  #define PRELOAD_PUSH_VALUE(type, value) do {		\
68 	*(type *)(preload_ptr + size) = (value);	\
69 	size += sizeof(type);				\
70 } while (0)
71 
72  #define PRELOAD_PUSH_STRING(str) do {			\
73  	uint32_t ssize;					\
74  	ssize = strlen(str) + 1;			\
75  	PRELOAD_PUSH_VALUE(uint32_t, ssize);		\
76 	strcpy((char*)(preload_ptr + size), str);	\
77 	size += ssize;					\
78 	size = roundup(size, sizeof(u_long));		\
79 } while (0)
80 
81 /* Build minimal set of metatda. */
82 static vm_offset_t
fake_preload_metadata(void * dtb_ptr,size_t dtb_size)83 fake_preload_metadata(void *dtb_ptr, size_t dtb_size)
84 {
85 	vm_offset_t lastaddr;
86 	static char fake_preload[256];
87 	caddr_t preload_ptr;
88 	size_t size;
89 
90 	lastaddr = (vm_offset_t)&end;
91 	preload_ptr = (caddr_t)&fake_preload[0];
92 	size = 0;
93 
94 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_NAME);
95 	PRELOAD_PUSH_STRING("kernel");
96 
97 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_TYPE);
98 	PRELOAD_PUSH_STRING("elf kernel");
99 
100 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_ADDR);
101 	PRELOAD_PUSH_VALUE(uint32_t, sizeof(vm_offset_t));
102 	PRELOAD_PUSH_VALUE(uint64_t, VM_MIN_KERNEL_ADDRESS);
103 
104 	PRELOAD_PUSH_VALUE(uint32_t, MODINFO_SIZE);
105 	PRELOAD_PUSH_VALUE(uint32_t, sizeof(size_t));
106 	PRELOAD_PUSH_VALUE(uint64_t, (size_t)(&end - VM_MIN_KERNEL_ADDRESS));
107 
108 	if (dtb_ptr != NULL) {
109 		/* Copy DTB to KVA space and insert it into module chain. */
110 		lastaddr = roundup(lastaddr, sizeof(int));
111 		PRELOAD_PUSH_VALUE(uint32_t, MODINFO_METADATA | MODINFOMD_DTBP);
112 		PRELOAD_PUSH_VALUE(uint32_t, sizeof(uint64_t));
113 		PRELOAD_PUSH_VALUE(uint64_t, (uint64_t)lastaddr);
114 		memmove((void *)lastaddr, dtb_ptr, dtb_size);
115 		lastaddr += dtb_size;
116 		lastaddr = roundup(lastaddr, sizeof(int));
117 	}
118 	/* End marker */
119 	PRELOAD_PUSH_VALUE(uint32_t, 0);
120 	PRELOAD_PUSH_VALUE(uint32_t, 0);
121 
122 	preload_metadata = (caddr_t)(uintptr_t)fake_preload;
123 
124 	init_static_kenv(NULL, 0);
125 
126 	return (lastaddr);
127 }
128 
129 #ifdef FDT
130 
131 /* Convert the U-Boot command line into FreeBSD kenv and boot options. */
132 static void
cmdline_set_env(char * cmdline,const char * guard)133 cmdline_set_env(char *cmdline, const char *guard)
134 {
135 	size_t guard_len;
136 
137 	/* Skip leading spaces. */
138 	while (isspace(*cmdline))
139 		cmdline++;
140 
141 	/* Test and remove guard. */
142 	if (guard != NULL && guard[0] != '\0') {
143 		guard_len  =  strlen(guard);
144 		if (strncasecmp(cmdline, guard, guard_len) != 0)
145 			return;
146 		cmdline += guard_len;
147 	}
148 
149 	boothowto |= boot_parse_cmdline(cmdline);
150 }
151 
152 void
parse_fdt_bootargs(void)153 parse_fdt_bootargs(void)
154 {
155 
156 	if (loader_envp == NULL && fdt_get_chosen_bootargs(linux_command_line,
157 	    LBABI_MAX_COMMAND_LINE) == 0) {
158 		init_static_kenv(static_kenv, sizeof(static_kenv));
159 		cmdline_set_env(linux_command_line, CMDLINE_GUARD);
160 	}
161 }
162 
163 #endif
164 
165 #if defined(LINUX_BOOT_ABI) && defined(FDT)
166 static vm_offset_t
linux_parse_boot_param(struct arm64_bootparams * abp)167 linux_parse_boot_param(struct arm64_bootparams *abp)
168 {
169 	struct fdt_header *dtb_ptr;
170 	size_t dtb_size;
171 
172 	if (abp->modulep == 0)
173 		return (0);
174 	/* Test if modulep point to valid DTB. */
175 	dtb_ptr = (struct fdt_header *)abp->modulep;
176 	if (fdt_check_header(dtb_ptr) != 0)
177 		return (0);
178 	dtb_size = fdt_totalsize(dtb_ptr);
179 	return (fake_preload_metadata(dtb_ptr, dtb_size));
180 }
181 
182 #endif
183 
184 static vm_offset_t
freebsd_parse_boot_param(struct arm64_bootparams * abp)185 freebsd_parse_boot_param(struct arm64_bootparams *abp)
186 {
187 	vm_offset_t lastaddr = 0;
188 	void *kmdp;
189 #ifdef DDB
190 	vm_offset_t ksym_start;
191 	vm_offset_t ksym_end;
192 #endif
193 
194 	if (abp->modulep == 0)
195 		return (0);
196 
197 	preload_metadata = (caddr_t)(uintptr_t)(abp->modulep);
198 	kmdp = preload_search_by_type("elf kernel");
199 	if (kmdp == NULL)
200 		return (0);
201 
202 	boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
203 	loader_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
204 	init_static_kenv(loader_envp, 0);
205 	lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
206 #ifdef DDB
207 	ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
208 	ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
209 	db_fetch_ksymtab(ksym_start, ksym_end, 0);
210 #endif
211 	return (lastaddr);
212 }
213 
214 vm_offset_t
parse_boot_param(struct arm64_bootparams * abp)215 parse_boot_param(struct arm64_bootparams *abp)
216 {
217 	vm_offset_t lastaddr;
218 
219 #if defined(LINUX_BOOT_ABI) && defined(FDT)
220 	lastaddr = linux_parse_boot_param(abp);
221 	if (lastaddr != 0)
222 		return (lastaddr);
223 #endif
224 	lastaddr = freebsd_parse_boot_param(abp);
225 	if (lastaddr != 0)
226 		return (lastaddr);
227 
228 	/* Fall back to hardcoded metadata. */
229 	lastaddr = fake_preload_metadata(NULL, 0);
230 
231 	return (lastaddr);
232 }
233