1 /*
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _BOOTSTRAP_H_
28 #define	_BOOTSTRAP_H_
29 
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <sys/linker_set.h>
33 #include <stdbool.h>
34 
35 /* Commands and return values; nonzero return sets command_errmsg != NULL */
36 typedef int	(bootblk_cmd_t)(int argc, char *argv[]);
37 #define	COMMAND_ERRBUFSZ	(256)
38 extern const char	*command_errmsg;
39 extern char	command_errbuf[COMMAND_ERRBUFSZ];
40 #define	CMD_OK		0
41 #define	CMD_WARN	1
42 #define	CMD_ERROR	2
43 #define	CMD_CRIT	3
44 #define	CMD_FATAL	4
45 
46 /* interp.c */
47 void	interact(const char *rc);
48 int	include(const char *filename);
49 
50 /* interp_backslash.c */
51 char	*backslash(char *str);
52 
53 /* interp_parse.c */
54 int	parse(int *argc, char ***argv, char *str);
55 
56 /* interp_forth.c */
57 void	bf_init(char *rc);
58 int	bf_run(char *line);
59 
60 /* boot.c */
61 int	autoboot(int timeout, char *prompt);
62 void	autoboot_maybe(void);
63 int	getrootmount(char *rootdev);
64 
65 /* misc.c */
66 char	*unargv(int argc, char *argv[]);
67 void	hexdump(caddr_t region, size_t len);
68 size_t	strlenout(vm_offset_t str);
69 char	*strdupout(vm_offset_t str);
70 void	kern_bzero(vm_offset_t dest, size_t len);
71 int	kern_pread(int fd, vm_offset_t dest, size_t len, off_t off);
72 void	*alloc_pread(int fd, off_t off, size_t len);
73 
74 /* bcache.c */
75 void	bcache_init(size_t nblks, size_t bsize);
76 void	bcache_add_dev(int);
77 void	*bcache_allocate(void);
78 void	bcache_free(void *);
79 int	bcache_strategy(void *devdata, int rw, daddr_t blk,
80     size_t size, char *buf, size_t *rsize);
81 
82 /*
83  * Disk block cache
84  */
85 struct bcache_devdata
86 {
87 	int	(*dv_strategy)(void *devdata, int rw, daddr_t blk,
88 		size_t size, char *buf, size_t *rsize);
89 	void	*dv_devdata;
90 	void	*dv_cache;
91 };
92 
93 /*
94  * Modular console support.
95  */
96 struct console
97 {
98 	const char	*c_name;
99 	const char	*c_desc;
100 	int		c_flags;
101 #define	C_PRESENTIN	(1<<0)		/* console can provide input */
102 #define	C_PRESENTOUT	(1<<1)		/* console can provide output */
103 #define	C_ACTIVEIN	(1<<2)		/* user wants input from console */
104 #define	C_ACTIVEOUT	(1<<3)		/* user wants output to console */
105 #define	C_WIDEOUT	(1<<4)		/* c_out routine groks wide chars */
106 #define	C_MODERAW	(1<<5)		/* raw mode */
107 
108 	/* set c_flags to match hardware */
109 	void	(*c_probe)(struct console *);
110 	/* reinit XXX may need more args */
111 	int		(*c_init)(struct console *, int);
112 	/* emit c */
113 	void		(*c_out)(struct console *, int);
114 	/* wait for and return input */
115 	int		(*c_in)(struct console *);
116 	/* return nonzero if input is waiting */
117 	int		(*c_ready)(struct console *);
118 	int		(*c_ioctl)(struct console *, int, void *);
119 	/* Print device info */
120 	void		(*c_devinfo)(struct console *);
121 	void		*c_private;	/* private data */
122 };
123 extern struct console	*consoles[];
124 void	cons_probe(void);
125 void	cons_mode(int);
126 void	autoload_font(bool);
127 
128 /*
129  * Plug-and-play enumerator/configurator interface.
130  */
131 struct pnphandler
132 {
133 	const char	*pp_name;		/* handler/bus name */
134 	/* enumerate PnP devices, add to chain */
135 	void		(*pp_enumerate)(void);
136 };
137 
138 struct pnpident
139 {
140 	/* ASCII identifier, actual format varies with bus/handler */
141 	char			*id_ident;
142 	STAILQ_ENTRY(pnpident)	id_link;
143 };
144 
145 struct pnpinfo
146 {
147 	/* ASCII description, optional */
148 	char			*pi_desc;
149 	/* optional revision (or -1) if not supported */
150 	int			pi_revision;
151 	/* module/args nominated to handle device */
152 	char			*pi_module;
153 	/* module arguments */
154 	int			pi_argc;
155 	char			**pi_argv;
156 	/* handler which detected this device */
157 	struct pnphandler	*pi_handler;
158 	/* list of identifiers */
159 	STAILQ_HEAD(, pnpident)	pi_ident;
160 	STAILQ_ENTRY(pnpinfo)	pi_link;
161 };
162 
163 STAILQ_HEAD(pnpinfo_stql, pnpinfo);
164 
165 extern struct pnphandler *pnphandlers[];	/* provided by MD code */
166 
167 void			pnp_addident(struct pnpinfo *pi, char *ident);
168 struct pnpinfo		*pnp_allocinfo(void);
169 void			pnp_freeinfo(struct pnpinfo *pi);
170 void			pnp_addinfo(struct pnpinfo *pi);
171 char			*pnp_eisaformat(uint8_t *data);
172 
173 /*
174  *  < 0	- No ISA in system
175  * == 0	- Maybe ISA, search for read data port
176  *  > 0	- ISA in system, value is read data port address
177  */
178 extern int			isapnp_readport;
179 
180 /*
181  * Version information
182  */
183 extern char bootprog_info[];
184 
185 /*
186  * Preloaded file metadata header.
187  *
188  * Metadata are allocated on our heap, and copied into kernel space
189  * before executing the kernel.
190  */
191 struct file_metadata
192 {
193 	size_t			md_size;
194 	uint16_t		md_type;
195 	struct file_metadata	*md_next;
196 	/* data are immediately appended */
197 	char			md_data[1];
198 };
199 
200 struct preloaded_file;
201 struct mod_depend;
202 
203 struct kernel_module
204 {
205 	char			*m_name;	/* module name */
206 	int			m_version;	/* module version */
207 	char			*m_args;	/* arguments for the module */
208 	struct preloaded_file	*m_fp;
209 	struct kernel_module	*m_next;
210 };
211 
212 /*
213  * Preloaded file information. Depending on type, file can contain
214  * additional units called 'modules'.
215  *
216  * At least one file (the kernel) must be loaded in order to boot.
217  * The kernel is always loaded first.
218  *
219  * String fields (m_name, m_type) should be dynamically allocated.
220  */
221 struct preloaded_file
222 {
223 	char			*f_name;	/* file name */
224 	/* verbose file type, eg 'ELF kernel', 'pnptable', etc. */
225 	char			*f_type;
226 	char			*f_args;	/* arguments for the file */
227 	/* metadata that will be placed in the module directory */
228 	struct file_metadata	*f_metadata;
229 	/* index of the loader that read the file */
230 	int			f_loader;
231 	vm_offset_t		f_addr;		/* load address */
232 	size_t			f_size;		/* file size */
233 	struct kernel_module	*f_modules;	/* list of modules if any */
234 	struct preloaded_file	*f_next;	/* next file */
235 };
236 
237 struct file_format
238 {
239 	/*
240 	 * Load function must return EFTYPE if it can't handle the module
241 	 * supplied.
242 	 */
243 	int (*l_load)(char *, uint64_t, struct preloaded_file **);
244 	/*
245 	 * Only a loader that will load a kernel (first module)
246 	 * should have an exec handler.
247 	 */
248 	int (*l_exec)(struct preloaded_file *);
249 };
250 
251 extern struct file_format *file_formats[];	/* supplied by consumer */
252 extern struct preloaded_file *preloaded_files;
253 
254 int mod_load(char *name, struct mod_depend *verinfo, int argc, char *argv[]);
255 int mod_loadkld(const char *name, int argc, char *argv[]);
256 void unload(void);
257 
258 struct preloaded_file *file_alloc(void);
259 struct preloaded_file *file_findfile(const char *name, const char *type);
260 struct file_metadata *file_findmetadata(struct preloaded_file *fp, int type);
261 struct preloaded_file *file_loadraw(const char *name, char *type, int argc,
262 	char **argv, int insert);
263 void file_discard(struct preloaded_file *fp);
264 void file_addmetadata(struct preloaded_file *, int, size_t, void *);
265 int  file_addmodule(struct preloaded_file *, char *, int,
266 	struct kernel_module **);
267 void build_environment_module(void);
268 void build_font_module(void);
269 vm_offset_t bi_copyenv(vm_offset_t);
270 
271 /* MI module loaders */
272 #ifdef __elfN
273 /* Relocation types. */
274 #define	ELF_RELOC_REL	1
275 #define	ELF_RELOC_RELA	2
276 
277 /* Relocation offset for some architectures */
278 extern uint64_t __elfN(relocation_offset);
279 
280 struct elf_file;
281 typedef Elf_Addr(symaddr_fn)(struct elf_file *, Elf_Size);
282 
283 int	elf64_loadfile(char *, uint64_t, struct preloaded_file **);
284 int	elf32_loadfile(char *, uint64_t, struct preloaded_file **);
285 int	elf64_obj_loadfile(char *, uint64_t, struct preloaded_file **);
286 int	elf32_obj_loadfile(char *, uint64_t, struct preloaded_file **);
287 int	__elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr,
288 	    const void *reldata, int reltype, Elf_Addr relbase,
289 	    Elf_Addr dataaddr, void *data, size_t len);
290 int	elf64_loadfile_raw(char *, uint64_t, struct preloaded_file **, int);
291 int	elf32_loadfile_raw(char *, uint64_t, struct preloaded_file **, int);
292 int	elf64_load_modmetadata(struct preloaded_file *, uint64_t);
293 int	elf32_load_modmetadata(struct preloaded_file *, uint64_t);
294 #endif
295 
296 /*
297  * Support for commands
298  */
299 struct bootblk_command
300 {
301 	const char	*c_name;
302 	const char	*c_desc;
303 	bootblk_cmd_t	*c_fn;
304 };
305 
306 #define	COMMAND_SET(tag, key, desc, func)				\
307     static bootblk_cmd_t func;						\
308     static struct bootblk_command _cmd_ ## tag = { key, desc, func };	\
309     DATA_SET(Xcommand_set, _cmd_ ## tag)
310 
311 SET_DECLARE(Xcommand_set, struct bootblk_command);
312 
313 /*
314  * The intention of the architecture switch is to provide a convenient
315  * encapsulation of the interface between the bootstrap MI and MD code.
316  * MD code may selectively populate the switch at runtime based on the
317  * actual configuration of the target system.
318  */
319 struct arch_switch
320 {
321 	/* Automatically load modules as required by detected hardware */
322 	int	(*arch_autoload)(void);
323 	/* Locate the device for (name), return pointer to tail in (*path) */
324 	int	(*arch_getdev)(void **dev, const char *name, const char **path);
325 	/*
326 	 * Copy from local address space to module address space,
327 	 * similar to bcopy()
328 	 */
329 	ssize_t	(*arch_copyin)(const void *src, vm_offset_t dest,
330 		const size_t len);
331 	/*
332 	 * Copy to local address space from module address space,
333 	 * similar to bcopy()
334 	 */
335 	ssize_t	(*arch_copyout)(const vm_offset_t src, void *dest,
336 				const size_t len);
337 	/* Read from file to module address space, same semantics as read() */
338 	ssize_t	(*arch_readin)(const int fd, vm_offset_t dest,
339 		const size_t len);
340 	/* Perform ISA byte port I/O (only for systems with ISA) */
341 	int	(*arch_isainb)(int port);
342 	void	(*arch_isaoutb)(int port, int value);
343 
344 	/*
345 	 * Interface to adjust the load address according to the "object"
346 	 * being loaded.
347 	 */
348 	vm_offset_t (*arch_loadaddr)(uint_t type, void *data, vm_offset_t addr);
349 #define	LOAD_ELF	1	/* data points to the ELF header. */
350 #define	LOAD_RAW	2	/* data points to the module file name. */
351 #define	LOAD_KERN	3	/* data points to the kernel file name. */
352 #define	LOAD_MEM	4	/* data points to int for buffer size. */
353 	/*
354 	 * Interface to release the load address.
355 	 */
356 	void	(*arch_free_loadaddr)(vm_offset_t addr, size_t pages);
357 
358 	/*
359 	 * Interface to inform MD code about a loaded (ELF) segment. This
360 	 * can be used to flush caches and/or set up translations.
361 	 */
362 #ifdef __elfN
363 	void	(*arch_loadseg)(Elf_Ehdr *eh, Elf_Phdr *ph, uint64_t delta);
364 #else
365 	void	(*arch_loadseg)(void *eh, void *ph, uint64_t delta);
366 #endif
367 
368 	/* Probe ZFS pool(s), if needed. */
369 	void	(*arch_zfs_probe)(void);
370 };
371 extern struct arch_switch archsw;
372 
373 /* This must be provided by the MD code, but should it be in the archsw? */
374 void	delay(int delay);
375 
376 void	dev_cleanup(void);
377 
378 /*
379  * nvstore API.
380  */
381 typedef int (nvstore_getter_cb_t)(void *, const char *, void **);
382 typedef int (nvstore_setter_cb_t)(void *, int, const char *,
383     const void *, size_t);
384 typedef int (nvstore_setter_str_cb_t)(void *, const char *, const char *,
385     const char *);
386 typedef int (nvstore_unset_cb_t)(void *, const char *);
387 typedef int (nvstore_print_cb_t)(void *, void *);
388 typedef int (nvstore_iterate_cb_t)(void *, int (*)(void *, void *));
389 
390 typedef struct nvs_callbacks {
391 	nvstore_getter_cb_t	*nvs_getter;
392 	nvstore_setter_cb_t	*nvs_setter;
393 	nvstore_setter_str_cb_t *nvs_setter_str;
394 	nvstore_unset_cb_t	*nvs_unset;
395 	nvstore_print_cb_t	*nvs_print;
396 	nvstore_iterate_cb_t	*nvs_iterate;
397 } nvs_callbacks_t;
398 
399 int nvstore_init(const char *, nvs_callbacks_t *, void *);
400 int nvstore_fini(const char *);
401 void *nvstore_get_store(const char *);
402 int nvstore_print(void *);
403 int nvstore_get_var(void *, const char *, void **);
404 int nvstore_set_var(void *, int, const char *, void *, size_t);
405 int nvstore_set_var_from_string(void *, const char *, const char *,
406     const char *);
407 int nvstore_unset_var(void *, const char *);
408 
409 #ifndef CTASSERT		/* Allow lint to override */
410 #define	CTASSERT(x)		_CTASSERT(x, __LINE__)
411 #define	_CTASSERT(x, y)		__CTASSERT(x, y)
412 #define	__CTASSERT(x, y)	typedef char __assert ## y[(x) ? 1 : -1]
413 #endif
414 
415 #endif /* !_BOOTSTRAP_H_ */
416