xref: /freebsd/stand/common/bootstrap.h (revision c83457486b042f0d08e8f8add4fc3b64b33b7d7d)
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  * $FreeBSD$
27  */
28 
29 #ifndef _BOOTSTRAP_H_
30 #define	_BOOTSTRAP_H_
31 
32 #include <sys/types.h>
33 #include <sys/queue.h>
34 #include <sys/linker_set.h>
35 
36 /* Commands and return values; nonzero return sets command_errmsg != NULL */
37 typedef int	(bootblk_cmd_t)(int argc, char *argv[]);
38 #define	COMMAND_ERRBUFSZ	(256)
39 extern char	*command_errmsg;
40 extern char	command_errbuf[COMMAND_ERRBUFSZ];
41 #define CMD_OK		0
42 #define CMD_WARN	1
43 #define CMD_ERROR	2
44 #define CMD_CRIT	3
45 #define CMD_FATAL	4
46 
47 /* interp.c */
48 void	interact(void);
49 int	include(const char *filename);
50 
51 /* interp_backslash.c */
52 char	*backslash(const char *str);
53 
54 /* interp_parse.c */
55 int	parse(int *argc, char ***argv, const char *str);
56 
57 /* boot.c */
58 int	autoboot(int timeout, char *prompt);
59 void	autoboot_maybe(void);
60 int	getrootmount(char *rootdev);
61 
62 /* misc.c */
63 char	*unargv(int argc, char *argv[]);
64 void	hexdump(caddr_t region, size_t len);
65 size_t	strlenout(vm_offset_t str);
66 char	*strdupout(vm_offset_t str);
67 void	kern_bzero(vm_offset_t dest, size_t len);
68 int	kern_pread(int fd, vm_offset_t dest, size_t len, off_t off);
69 void	*alloc_pread(int fd, off_t off, size_t len);
70 
71 /* bcache.c */
72 void	bcache_init(size_t nblks, size_t bsize);
73 void	bcache_add_dev(int);
74 void	*bcache_allocate(void);
75 void	bcache_free(void *);
76 int	bcache_strategy(void *devdata, int rw, daddr_t blk, size_t size,
77 			char *buf, size_t *rsize);
78 
79 /*
80  * Disk block cache
81  */
82 struct bcache_devdata
83 {
84     int         (*dv_strategy)(void *devdata, int rw, daddr_t blk,
85 			size_t size, char *buf, size_t *rsize);
86     void	*dv_devdata;
87     void	*dv_cache;
88 };
89 
90 /*
91  * Modular console support.
92  */
93 struct console
94 {
95     const char	*c_name;
96     const char	*c_desc;
97     int		c_flags;
98 #define C_PRESENTIN	(1<<0)	    /* console can provide input */
99 #define C_PRESENTOUT	(1<<1)	    /* console can provide output */
100 #define C_ACTIVEIN	(1<<2)	    /* user wants input from console */
101 #define C_ACTIVEOUT	(1<<3)	    /* user wants output to console */
102 #define	C_WIDEOUT	(1<<4)	    /* c_out routine groks wide chars */
103     void	(* c_probe)(struct console *cp);	/* set c_flags to match hardware */
104     int		(* c_init)(int arg);			/* reinit XXX may need more args */
105     void	(* c_out)(int c);			/* emit c */
106     int		(* c_in)(void);				/* wait for and return input */
107     int		(* c_ready)(void);			/* return nonzer if input waiting */
108 };
109 extern struct console	*consoles[];
110 void		cons_probe(void);
111 
112 /*
113  * Plug-and-play enumerator/configurator interface.
114  */
115 struct pnphandler
116 {
117     const char	*pp_name;		/* handler/bus name */
118     void	(* pp_enumerate)(void);	/* enumerate PnP devices, add to chain */
119 };
120 
121 struct pnpident
122 {
123     char			*id_ident;	/* ASCII identifier, actual format varies with bus/handler */
124     STAILQ_ENTRY(pnpident)	id_link;
125 };
126 
127 struct pnpinfo
128 {
129     char			*pi_desc;	/* ASCII description, optional */
130     int				pi_revision;	/* optional revision (or -1) if not supported */
131     char			*pi_module;	/* module/args nominated to handle device */
132     int				pi_argc;	/* module arguments */
133     char			**pi_argv;
134     struct pnphandler		*pi_handler;	/* handler which detected this device */
135     STAILQ_HEAD(,pnpident)	pi_ident;	/* list of identifiers */
136     STAILQ_ENTRY(pnpinfo)	pi_link;
137 };
138 
139 STAILQ_HEAD(pnpinfo_stql, pnpinfo);
140 
141 extern struct pnphandler	*pnphandlers[];		/* provided by MD code */
142 
143 void			pnp_addident(struct pnpinfo *pi, char *ident);
144 struct pnpinfo		*pnp_allocinfo(void);
145 void			pnp_freeinfo(struct pnpinfo *pi);
146 void			pnp_addinfo(struct pnpinfo *pi);
147 char			*pnp_eisaformat(u_int8_t *data);
148 
149 /*
150  *  < 0	- No ISA in system
151  * == 0	- Maybe ISA, search for read data port
152  *  > 0	- ISA in system, value is read data port address
153  */
154 extern int			isapnp_readport;
155 
156 /*
157  * Preloaded file metadata header.
158  *
159  * Metadata are allocated on our heap, and copied into kernel space
160  * before executing the kernel.
161  */
162 struct file_metadata
163 {
164     size_t			md_size;
165     u_int16_t			md_type;
166     struct file_metadata	*md_next;
167     char			md_data[1];	/* data are immediately appended */
168 };
169 
170 struct preloaded_file;
171 struct mod_depend;
172 
173 struct kernel_module
174 {
175     char			*m_name;	/* module name */
176     int				m_version;	/* module version */
177 /*    char			*m_args;*/	/* arguments for the module */
178     struct preloaded_file	*m_fp;
179     struct kernel_module	*m_next;
180 };
181 
182 /*
183  * Preloaded file information. Depending on type, file can contain
184  * additional units called 'modules'.
185  *
186  * At least one file (the kernel) must be loaded in order to boot.
187  * The kernel is always loaded first.
188  *
189  * String fields (m_name, m_type) should be dynamically allocated.
190  */
191 struct preloaded_file
192 {
193     char			*f_name;	/* file name */
194     char			*f_type;	/* verbose file type, eg 'ELF kernel', 'pnptable', etc. */
195     char			*f_args;	/* arguments for the file */
196     struct file_metadata	*f_metadata;	/* metadata that will be placed in the module directory */
197     int				f_loader;	/* index of the loader that read the file */
198     vm_offset_t			f_addr;		/* load address */
199     size_t			f_size;		/* file size */
200     struct kernel_module	*f_modules;	/* list of modules if any */
201     struct preloaded_file	*f_next;	/* next file */
202 };
203 
204 struct file_format
205 {
206     /* Load function must return EFTYPE if it can't handle the module supplied */
207     int		(* l_load)(char *filename, u_int64_t dest, struct preloaded_file **result);
208     /* Only a loader that will load a kernel (first module) should have an exec handler */
209     int		(* l_exec)(struct preloaded_file *mp);
210 };
211 
212 extern struct file_format	*file_formats[];	/* supplied by consumer */
213 extern struct preloaded_file	*preloaded_files;
214 
215 int			mod_load(char *name, struct mod_depend *verinfo, int argc, char *argv[]);
216 int			mod_loadkld(const char *name, int argc, char *argv[]);
217 void			unload(void);
218 
219 struct preloaded_file *file_alloc(void);
220 struct preloaded_file *file_findfile(const char *name, const char *type);
221 struct file_metadata *file_findmetadata(struct preloaded_file *fp, int type);
222 struct preloaded_file *file_loadraw(const char *name, char *type, int insert);
223 void file_discard(struct preloaded_file *fp);
224 void file_addmetadata(struct preloaded_file *fp, int type, size_t size, void *p);
225 int  file_addmodule(struct preloaded_file *fp, char *modname, int version,
226 	struct kernel_module **newmp);
227 void file_removemetadata(struct preloaded_file *fp);
228 
229 /* MI module loaders */
230 #ifdef __elfN
231 /* Relocation types. */
232 #define ELF_RELOC_REL	1
233 #define ELF_RELOC_RELA	2
234 
235 /* Relocation offset for some architectures */
236 extern u_int64_t __elfN(relocation_offset);
237 
238 struct elf_file;
239 typedef Elf_Addr (symaddr_fn)(struct elf_file *ef, Elf_Size symidx);
240 
241 int	__elfN(loadfile)(char *filename, u_int64_t dest, struct preloaded_file **result);
242 int	__elfN(obj_loadfile)(char *filename, u_int64_t dest,
243 	    struct preloaded_file **result);
244 int	__elfN(reloc)(struct elf_file *ef, symaddr_fn *symaddr,
245 	    const void *reldata, int reltype, Elf_Addr relbase,
246 	    Elf_Addr dataaddr, void *data, size_t len);
247 int __elfN(loadfile_raw)(char *filename, u_int64_t dest,
248 	    struct preloaded_file **result, int multiboot);
249 int __elfN(load_modmetadata)(struct preloaded_file *fp, u_int64_t dest);
250 #endif
251 
252 /*
253  * Support for commands
254  */
255 struct bootblk_command
256 {
257     const char		*c_name;
258     const char		*c_desc;
259     bootblk_cmd_t	*c_fn;
260 };
261 
262 #define COMMAND_SET(tag, key, desc, func)				\
263     static bootblk_cmd_t func;						\
264     static struct bootblk_command _cmd_ ## tag = { key, desc, func };	\
265     DATA_SET(Xcommand_set, _cmd_ ## tag)
266 
267 SET_DECLARE(Xcommand_set, struct bootblk_command);
268 
269 /*
270  * The intention of the architecture switch is to provide a convenient
271  * encapsulation of the interface between the bootstrap MI and MD code.
272  * MD code may selectively populate the switch at runtime based on the
273  * actual configuration of the target system.
274  */
275 struct arch_switch
276 {
277     /* Automatically load modules as required by detected hardware */
278     int		(*arch_autoload)(void);
279     /* Locate the device for (name), return pointer to tail in (*path) */
280     int		(*arch_getdev)(void **dev, const char *name, const char **path);
281     /* Copy from local address space to module address space, similar to bcopy() */
282     ssize_t	(*arch_copyin)(const void *src, vm_offset_t dest,
283 			       const size_t len);
284     /* Copy to local address space from module address space, similar to bcopy() */
285     ssize_t	(*arch_copyout)(const vm_offset_t src, void *dest,
286 				const size_t len);
287     /* Read from file to module address space, same semantics as read() */
288     ssize_t	(*arch_readin)(const int fd, vm_offset_t dest,
289 			       const size_t len);
290     /* Perform ISA byte port I/O (only for systems with ISA) */
291     int		(*arch_isainb)(int port);
292     void	(*arch_isaoutb)(int port, int value);
293 
294     /*
295      * Interface to adjust the load address according to the "object"
296      * being loaded.
297      */
298     uint64_t	(*arch_loadaddr)(u_int type, void *data, uint64_t addr);
299 #define	LOAD_ELF	1	/* data points to the ELF header. */
300 #define	LOAD_RAW	2	/* data points to the file name. */
301 
302     /*
303      * Interface to inform MD code about a loaded (ELF) segment. This
304      * can be used to flush caches and/or set up translations.
305      */
306 #ifdef __elfN
307     void	(*arch_loadseg)(Elf_Ehdr *eh, Elf_Phdr *ph, uint64_t delta);
308 #else
309     void	(*arch_loadseg)(void *eh, void *ph, uint64_t delta);
310 #endif
311 
312     /* Probe ZFS pool(s), if needed. */
313     void	(*arch_zfs_probe)(void);
314 };
315 extern struct arch_switch archsw;
316 
317 /* This must be provided by the MD code, but should it be in the archsw? */
318 void	delay(int delay);
319 
320 void	dev_cleanup(void);
321 
322 time_t	time(time_t *tloc);
323 
324 #ifndef CTASSERT                /* Allow lint to override */
325 #define CTASSERT(x)             _CTASSERT(x, __LINE__)
326 #define _CTASSERT(x, y)         __CTASSERT(x, y)
327 #define __CTASSERT(x, y)        typedef char __assert ## y[(x) ? 1 : -1]
328 #endif
329 
330 #endif /* !_BOOTSTRAP_H_ */
331