xref: /freebsd/stand/libsa/stand.h (revision c29cba408d197ed2c267d0605e4225bb54153e01)
1ca987d46SWarner Losh /*
2ca987d46SWarner Losh  * Copyright (c) 1998 Michael Smith.
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh  * are met:
8ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh  *
14ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh  * SUCH DAMAGE.
25ca987d46SWarner Losh  * From	$NetBSD: stand.h,v 1.22 1997/06/26 19:17:40 drochner Exp $
26ca987d46SWarner Losh  */
27ca987d46SWarner Losh 
28ca987d46SWarner Losh /*-
29ca987d46SWarner Losh  * Copyright (c) 1993
30ca987d46SWarner Losh  *	The Regents of the University of California.  All rights reserved.
31ca987d46SWarner Losh  *
32ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
33ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
34ca987d46SWarner Losh  * are met:
35ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
36ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
37ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
38ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
39ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
40ca987d46SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
41ca987d46SWarner Losh  *    may be used to endorse or promote products derived from this software
42ca987d46SWarner Losh  *    without specific prior written permission.
43ca987d46SWarner Losh  *
44ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54ca987d46SWarner Losh  * SUCH DAMAGE.
55ca987d46SWarner Losh  */
56ca987d46SWarner Losh 
57ca987d46SWarner Losh #ifndef	STAND_H
58ca987d46SWarner Losh #define	STAND_H
59ca987d46SWarner Losh 
605cf20707SWarner Losh #include <sys/types.h>
61ca987d46SWarner Losh #include <sys/stat.h>
62ca987d46SWarner Losh #include <sys/dirent.h>
6397cbd5e7SToomas Soome #include <sys/queue.h>
64ca987d46SWarner Losh 
65ca987d46SWarner Losh /* this header intentionally exports NULL from <string.h> */
66ca987d46SWarner Losh #include <string.h>
67c7b46ba4SWarner Losh #define strcoll(a, b)	strcmp((a), (b))
68ca987d46SWarner Losh 
69ca987d46SWarner Losh #define CHK(fmt, args...)	printf("%s(%d): " fmt "\n", __func__, __LINE__ , ##args)
70ca987d46SWarner Losh #define PCHK(fmt, args...)	{printf("%s(%d): " fmt "\n", __func__, __LINE__ , ##args); getchar();}
71ca987d46SWarner Losh 
72ca987d46SWarner Losh #include <sys/errno.h>
73ca987d46SWarner Losh 
74ca987d46SWarner Losh /* special stand error codes */
75ca987d46SWarner Losh #define	EADAPT	(ELAST+1)	/* bad adaptor */
76ca987d46SWarner Losh #define	ECTLR	(ELAST+2)	/* bad controller */
77ca987d46SWarner Losh #define	EUNIT	(ELAST+3)	/* bad unit */
78ca987d46SWarner Losh #define ESLICE	(ELAST+4)	/* bad slice */
79ca987d46SWarner Losh #define	EPART	(ELAST+5)	/* bad partition */
80ca987d46SWarner Losh #define	ERDLAB	(ELAST+6)	/* can't read disk label */
81ca987d46SWarner Losh #define	EUNLAB	(ELAST+7)	/* unlabeled disk */
82ca987d46SWarner Losh #define	EOFFSET	(ELAST+8)	/* relative seek not supported */
83ca987d46SWarner Losh #define	ESALAST	(ELAST+8)	/* */
84ca987d46SWarner Losh 
85*c29cba40SWarner Losh /*
86*c29cba40SWarner Losh  * LUA needs sig_atomic_t. This is defined to be long or int on all our
87*c29cba40SWarner Losh  * platforms. On all but powerpc, these are all the same thing as long. 64-bit
88*c29cba40SWarner Losh  * powerpc defines this as int, but long can also be accessed atomically. It's
89*c29cba40SWarner Losh  * also OK because we don't have signal handlers in the boot loader.
90*c29cba40SWarner Losh  */
91*c29cba40SWarner Losh typedef long sig_atomic_t;
9249cb0130SWarner Losh 
935cf20707SWarner Losh __BEGIN_DECLS
945cf20707SWarner Losh 
95ca987d46SWarner Losh struct open_file;
96ca987d46SWarner Losh 
97ca987d46SWarner Losh /*
98ca987d46SWarner Losh  * This structure is used to define file system operations in a file system
99ca987d46SWarner Losh  * independent way.
100ca987d46SWarner Losh  *
101ca987d46SWarner Losh  * XXX note that filesystem providers should export a pointer to their fs_ops
102ca987d46SWarner Losh  *     struct, so that consumers can reference this and thus include the
103ca987d46SWarner Losh  *     filesystems that they require.
104ca987d46SWarner Losh  */
105ca987d46SWarner Losh struct fs_ops {
106ca987d46SWarner Losh     const char	*fs_name;
107ca987d46SWarner Losh     int		(*fo_open)(const char *path, struct open_file *f);
108ca987d46SWarner Losh     int		(*fo_close)(struct open_file *f);
109ca987d46SWarner Losh     int		(*fo_read)(struct open_file *f, void *buf,
110ca987d46SWarner Losh 			   size_t size, size_t *resid);
1112e7e6fbcSConrad Meyer     int		(*fo_write)(struct open_file *f, const void *buf,
112ca987d46SWarner Losh 			    size_t size, size_t *resid);
113ca987d46SWarner Losh     off_t	(*fo_seek)(struct open_file *f, off_t offset, int where);
114ca987d46SWarner Losh     int		(*fo_stat)(struct open_file *f, struct stat *sb);
115ca987d46SWarner Losh     int		(*fo_readdir)(struct open_file *f, struct dirent *d);
116c25d9affSEmmanuel Vadot     int		(*fo_preload)(struct open_file *f);
117b4cb3fe0SToomas Soome     int		(*fo_mount)(const char *, const char *, void **);
118b4cb3fe0SToomas Soome     int		(*fo_unmount)(const char *, void *);
119ca987d46SWarner Losh };
120ca987d46SWarner Losh 
121ca987d46SWarner Losh /*
122bd001d86SWarner Losh  * libsa-supplied filesystems
123ca987d46SWarner Losh  */
124ca987d46SWarner Losh extern struct fs_ops ufs_fsops;
125ca987d46SWarner Losh extern struct fs_ops tftp_fsops;
126ca987d46SWarner Losh extern struct fs_ops nfs_fsops;
127ca987d46SWarner Losh extern struct fs_ops cd9660_fsops;
128ca987d46SWarner Losh extern struct fs_ops gzipfs_fsops;
129ca987d46SWarner Losh extern struct fs_ops bzipfs_fsops;
130ca987d46SWarner Losh extern struct fs_ops dosfs_fsops;
131ca987d46SWarner Losh extern struct fs_ops ext2fs_fsops;
132ca987d46SWarner Losh extern struct fs_ops splitfs_fsops;
133ca987d46SWarner Losh extern struct fs_ops pkgfs_fsops;
134da4961c7SRebecca Cran extern struct fs_ops efihttp_fsops;
135ca987d46SWarner Losh 
136ca987d46SWarner Losh /* where values for lseek(2) */
137ca987d46SWarner Losh #define	SEEK_SET	0	/* set file offset to offset */
138ca987d46SWarner Losh #define	SEEK_CUR	1	/* set file offset to current plus offset */
139ca987d46SWarner Losh #define	SEEK_END	2	/* set file offset to EOF plus offset */
140ca987d46SWarner Losh 
141ca987d46SWarner Losh /*
142ca987d46SWarner Losh  * Device switch
143ca987d46SWarner Losh  */
1444932a6e4SWarner Losh #define DEV_NAMLEN	8		/* Length of name of device class */
1454d4b1a29SWarner Losh #define DEV_DEVLEN	128		/* Length of longest device instance name */
1464d4b1a29SWarner Losh struct devdesc;
147ca987d46SWarner Losh struct devsw {
1484932a6e4SWarner Losh     const char	dv_name[DEV_NAMLEN];
149079f02e8SWarner Losh     int		dv_type;		/* opaque type constant */
150b3a2aad1SWarner Losh #define DEVT_NONE	0
151b3a2aad1SWarner Losh #define DEVT_DISK	1
152b3a2aad1SWarner Losh #define DEVT_NET	2
153b3a2aad1SWarner Losh #define DEVT_CD		3
154b3a2aad1SWarner Losh #define DEVT_ZFS	4
155b3a2aad1SWarner Losh #define DEVT_FD		5
156ca987d46SWarner Losh     int		(*dv_init)(void);	/* early probe call */
157ca987d46SWarner Losh     int		(*dv_strategy)(void *devdata, int rw, daddr_t blk,
158ca987d46SWarner Losh 			size_t size, char *buf, size_t *rsize);
159ca987d46SWarner Losh     int		(*dv_open)(struct open_file *f, ...);
160ca987d46SWarner Losh     int		(*dv_close)(struct open_file *f);
161ca987d46SWarner Losh     int		(*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
162ca987d46SWarner Losh     int		(*dv_print)(int verbose);	/* print device information */
163ca987d46SWarner Losh     void	(*dv_cleanup)(void);
1644d4b1a29SWarner Losh     char *	(*dv_fmtdev)(struct devdesc *);
165a0aad69fSWarner Losh     int		(*dv_parsedev)(struct devdesc **, const char *, const char **);
166a07cef5aSWarner Losh     bool	(*dv_match)(struct devsw *, const char *);
167ca987d46SWarner Losh };
168ca987d46SWarner Losh 
169ca987d46SWarner Losh /*
170bd001d86SWarner Losh  * libsa-supplied device switch
171ca987d46SWarner Losh  */
172ca987d46SWarner Losh extern struct devsw netdev;
173ca987d46SWarner Losh 
174ca987d46SWarner Losh extern int errno;
175ca987d46SWarner Losh 
176ca987d46SWarner Losh /*
177269865a8SWarner Losh  * Generic device specifier; architecture-dependent versions may be larger, but
178269865a8SWarner Losh  * should be allowed to overlap. The larger device specifiers store more data
179269865a8SWarner Losh  * than can fit in the generic one that's gleaned after parsing the device
180269865a8SWarner Losh  * string, or used in some cases to indicate wildcards that match a variety of
181269865a8SWarner Losh  * situations based on what's on the drive itself rather than what the progammer
182269865a8SWarner Losh  * might know in advance. Information about open files is stored in d_opendata,
183269865a8SWarner Losh  * though what's passed into the open routine may differ from what's present
184269865a8SWarner Losh  * after the open on some configurations.
185ca987d46SWarner Losh  */
186b3a2aad1SWarner Losh struct devdesc {
187ca987d46SWarner Losh     struct devsw	*d_dev;
188ca987d46SWarner Losh     int			d_unit;
189ca987d46SWarner Losh     void		*d_opendata;
190ca987d46SWarner Losh };
191ca987d46SWarner Losh 
1924d4b1a29SWarner Losh char *devformat(struct devdesc *d);
193781ca0afSWarner Losh int devparse(struct devdesc **, const char *, const char **);
19466012c8fSWarner Losh int devinit(void);
195bf020787SWarner Losh void	dev_cleanup(void);
1964d4b1a29SWarner Losh 
197ca987d46SWarner Losh struct open_file {
198ca987d46SWarner Losh     int			f_flags;	/* see F_* below */
199ca987d46SWarner Losh     struct devsw	*f_dev;		/* pointer to device operations */
200ca987d46SWarner Losh     void		*f_devdata;	/* device specific data */
201ca987d46SWarner Losh     struct fs_ops	*f_ops;		/* pointer to file system operations */
202ca987d46SWarner Losh     void		*f_fsdata;	/* file system specific data */
203ca987d46SWarner Losh     off_t		f_offset;	/* current file offset */
204ca987d46SWarner Losh     char		*f_rabuf;	/* readahead buffer pointer */
205ca987d46SWarner Losh     size_t		f_ralen;	/* valid data in readahead buffer */
206ca987d46SWarner Losh     off_t		f_raoffset;	/* consumer offset in readahead buffer */
20797cbd5e7SToomas Soome     int			f_id;		/* file number */
20897cbd5e7SToomas Soome     TAILQ_ENTRY(open_file) f_link;	/* next entry */
209ca987d46SWarner Losh #define SOPEN_RASIZE	512
210ca987d46SWarner Losh };
211ca987d46SWarner Losh 
21297cbd5e7SToomas Soome typedef TAILQ_HEAD(file_list, open_file) file_list_t;
21397cbd5e7SToomas Soome extern file_list_t files;
21497cbd5e7SToomas Soome extern struct open_file *fd2open_file(int);
215ca987d46SWarner Losh 
216ca987d46SWarner Losh /* f_flags values */
217ca987d46SWarner Losh #define	F_READ		0x0001	/* file opened for reading */
218ca987d46SWarner Losh #define	F_WRITE		0x0002	/* file opened for writing */
219ca987d46SWarner Losh #define	F_RAW		0x0004	/* raw device open - no file system */
220ca987d46SWarner Losh #define F_NODEV		0x0008	/* network open - no device */
221ca987d46SWarner Losh #define	F_MASK		0xFFFF
222ca987d46SWarner Losh /* Mode modifier for strategy() */
223ca987d46SWarner Losh #define	F_NORA		(0x01 << 16)	/* Disable Read-Ahead */
224ca987d46SWarner Losh 
225ca987d46SWarner Losh #define isascii(c)	(((c) & ~0x7F) == 0)
226ca987d46SWarner Losh 
isupper(int c)227ca987d46SWarner Losh static __inline int isupper(int c)
228ca987d46SWarner Losh {
229ca987d46SWarner Losh     return c >= 'A' && c <= 'Z';
230ca987d46SWarner Losh }
231ca987d46SWarner Losh 
islower(int c)232ca987d46SWarner Losh static __inline int islower(int c)
233ca987d46SWarner Losh {
234ca987d46SWarner Losh     return c >= 'a' && c <= 'z';
235ca987d46SWarner Losh }
236ca987d46SWarner Losh 
isspace(int c)237ca987d46SWarner Losh static __inline int isspace(int c)
238ca987d46SWarner Losh {
239ca987d46SWarner Losh     return c == ' ' || (c >= 0x9 && c <= 0xd);
240ca987d46SWarner Losh }
241ca987d46SWarner Losh 
isdigit(int c)242ca987d46SWarner Losh static __inline int isdigit(int c)
243ca987d46SWarner Losh {
244ca987d46SWarner Losh     return c >= '0' && c <= '9';
245ca987d46SWarner Losh }
246ca987d46SWarner Losh 
isxdigit(int c)247ca987d46SWarner Losh static __inline int isxdigit(int c)
248ca987d46SWarner Losh {
249ca987d46SWarner Losh     return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
250ca987d46SWarner Losh }
251ca987d46SWarner Losh 
isalpha(int c)252ca987d46SWarner Losh static __inline int isalpha(int c)
253ca987d46SWarner Losh {
254ca987d46SWarner Losh     return isupper(c) || islower(c);
255ca987d46SWarner Losh }
256ca987d46SWarner Losh 
isalnum(int c)257ca987d46SWarner Losh static __inline int isalnum(int c)
258ca987d46SWarner Losh {
259ca987d46SWarner Losh     return isalpha(c) || isdigit(c);
260ca987d46SWarner Losh }
261ca987d46SWarner Losh 
iscntrl(int c)2626856cf68SWarner Losh static __inline int iscntrl(int c)
2636856cf68SWarner Losh {
2646856cf68SWarner Losh 	return (c >= 0 && c < ' ') || c == 127;
2656856cf68SWarner Losh }
2666856cf68SWarner Losh 
isgraph(int c)2676856cf68SWarner Losh static __inline int isgraph(int c)
2686856cf68SWarner Losh {
2696856cf68SWarner Losh 	return c >= '!' && c <= '~';
2706856cf68SWarner Losh }
2716856cf68SWarner Losh 
ispunct(int c)2726856cf68SWarner Losh static __inline int ispunct(int c)
2736856cf68SWarner Losh {
2746856cf68SWarner Losh 	return (c >= '!' && c <= '/') || (c >= ':' && c <= '@') ||
2756856cf68SWarner Losh 	    (c >= '[' && c <= '`') || (c >= '{' && c <= '~');
2766856cf68SWarner Losh }
2776856cf68SWarner Losh 
toupper(int c)278ca987d46SWarner Losh static __inline int toupper(int c)
279ca987d46SWarner Losh {
280ca987d46SWarner Losh     return islower(c) ? c - 'a' + 'A' : c;
281ca987d46SWarner Losh }
282ca987d46SWarner Losh 
tolower(int c)283ca987d46SWarner Losh static __inline int tolower(int c)
284ca987d46SWarner Losh {
285ca987d46SWarner Losh     return isupper(c) ? c - 'A' + 'a' : c;
286ca987d46SWarner Losh }
287ca987d46SWarner Losh 
288ca987d46SWarner Losh /* sbrk emulation */
289ca987d46SWarner Losh extern void	setheap(void *base, void *top);
290ca987d46SWarner Losh extern char	*sbrk(int incr);
291ca987d46SWarner Losh 
292ca987d46SWarner Losh extern int	printf(const char *fmt, ...) __printflike(1, 2);
2935e84b578SToomas Soome extern int	asprintf(char **buf, const char *cfmt, ...) __printflike(2, 3);
294ca987d46SWarner Losh extern int	sprintf(char *buf, const char *cfmt, ...) __printflike(2, 3);
295ca987d46SWarner Losh extern int	snprintf(char *buf, size_t size, const char *cfmt, ...) __printflike(3, 4);
2963e9c7874SIan Lepore extern int	vprintf(const char *fmt, __va_list);
2973e9c7874SIan Lepore extern int	vsprintf(char *buf, const char *cfmt, __va_list);
2983e9c7874SIan Lepore extern int	vsnprintf(char *buf, size_t size, const char *cfmt, __va_list);
299ca987d46SWarner Losh 
300ca987d46SWarner Losh extern void	twiddle(u_int callerdiv);
301ca987d46SWarner Losh extern void	twiddle_divisor(u_int globaldiv);
302ca987d46SWarner Losh 
303ca987d46SWarner Losh extern void	ngets(char *, int);
304ca987d46SWarner Losh #define gets(x)	ngets((x), 0)
305ca987d46SWarner Losh extern int	fgetstr(char *buf, int size, int fd);
306ca987d46SWarner Losh 
307b4cb3fe0SToomas Soome extern int	mount(const char *dev, const char *path, int flags, void *data);
308b4cb3fe0SToomas Soome extern int	unmount(const char *dev, int flags);
309ca987d46SWarner Losh extern int	open(const char *, int);
310ca987d46SWarner Losh #define	O_RDONLY	0x0
311ca987d46SWarner Losh #define O_WRONLY	0x1
312ca987d46SWarner Losh #define O_RDWR		0x2
3132ef9ff7dSSimon J. Gerraty #define O_ACCMODE	0x3
31482c85a42SKyle Evans /* NOT IMPLEMENTED */
31582c85a42SKyle Evans #define	O_CREAT		0x0200		/* create if nonexistent */
31682c85a42SKyle Evans #define	O_TRUNC		0x0400		/* truncate to zero length */
317ca987d46SWarner Losh extern int	close(int);
318ca987d46SWarner Losh extern void	closeall(void);
319ca987d46SWarner Losh extern ssize_t	read(int, void *, size_t);
320061577c5SConrad Meyer extern ssize_t	write(int, const void *, size_t);
3212e1e68cbSWarner Losh extern int	ioctl(int, u_long, void *);
322ca987d46SWarner Losh extern struct	dirent *readdirfd(int);
323c25d9affSEmmanuel Vadot extern void	preload(int);
324ca987d46SWarner Losh 
325dcaa2d76SWarner Losh extern void	srandom(unsigned int);
326e294a126SWarner Losh extern long	random(void);
327ca987d46SWarner Losh 
328ca987d46SWarner Losh /* imports from stdlib, locally modified */
329ca987d46SWarner Losh extern char	*optarg;			/* getopt(3) external variables */
330ca987d46SWarner Losh extern int	optind, opterr, optopt, optreset;
331ca987d46SWarner Losh extern int	getopt(int, char * const [], const char *);
332ca987d46SWarner Losh 
333ca987d46SWarner Losh /* pager.c */
334ca987d46SWarner Losh extern void	pager_open(void);
335ca987d46SWarner Losh extern void	pager_close(void);
336ca987d46SWarner Losh extern int	pager_output(const char *lines);
337ca987d46SWarner Losh extern int	pager_file(const char *fname);
338ca987d46SWarner Losh 
339ca987d46SWarner Losh /* No signal state to preserve */
340ca987d46SWarner Losh #define setjmp	_setjmp
341ca987d46SWarner Losh #define longjmp	_longjmp
342ca987d46SWarner Losh 
343ca987d46SWarner Losh /* environment.c */
344ca987d46SWarner Losh #define EV_DYNAMIC	(1<<0)		/* value was dynamically allocated, free if changed/unset */
345ca987d46SWarner Losh #define EV_VOLATILE	(1<<1)		/* value is volatile, make a copy of it */
346ca987d46SWarner Losh #define EV_NOHOOK	(1<<2)		/* don't call hook when setting */
347ca987d46SWarner Losh 
348ca987d46SWarner Losh struct env_var;
349ca987d46SWarner Losh typedef char	*(ev_format_t)(struct env_var *ev);
350ca987d46SWarner Losh typedef int	(ev_sethook_t)(struct env_var *ev, int flags,
351ca987d46SWarner Losh 		    const void *value);
352ca987d46SWarner Losh typedef int	(ev_unsethook_t)(struct env_var *ev);
353ca987d46SWarner Losh 
354ca987d46SWarner Losh struct env_var
355ca987d46SWarner Losh {
356ca987d46SWarner Losh     char		*ev_name;
357ca987d46SWarner Losh     int			ev_flags;
358ca987d46SWarner Losh     void		*ev_value;
359ca987d46SWarner Losh     ev_sethook_t	*ev_sethook;
360ca987d46SWarner Losh     ev_unsethook_t	*ev_unsethook;
361ca987d46SWarner Losh     struct env_var	*ev_next, *ev_prev;
362ca987d46SWarner Losh };
363ca987d46SWarner Losh extern struct env_var	*environ;
364ca987d46SWarner Losh 
365ca987d46SWarner Losh extern struct env_var	*env_getenv(const char *name);
366ca987d46SWarner Losh extern int		env_setenv(const char *name, int flags,
367ca987d46SWarner Losh 				   const void *value, ev_sethook_t sethook,
368ca987d46SWarner Losh 				   ev_unsethook_t unsethook);
369588f0a1eSToomas Soome extern void		env_discard(struct env_var *);
370ca987d46SWarner Losh extern char		*getenv(const char *name);
371ca987d46SWarner Losh extern int		setenv(const char *name, const char *value,
372ca987d46SWarner Losh 			       int overwrite);
373e8e6a5f9SWarner Losh extern int		putenv(char *string);
374ca987d46SWarner Losh extern int		unsetenv(const char *name);
375ca987d46SWarner Losh 
376ca987d46SWarner Losh extern ev_sethook_t	env_noset;		/* refuse set operation */
377ca987d46SWarner Losh extern ev_unsethook_t	env_nounset;		/* refuse unset operation */
378ca987d46SWarner Losh 
3792b0268cfSWarner Losh /* stdlib.h routines */
38024dfa658SWarner Losh extern int		abs(int a);
38130883627SWarner Losh extern void		abort(void) __dead2;
3823a8a081bSWarner Losh extern long		strtol(const char * __restrict, char ** __restrict, int);
3833a8a081bSWarner Losh extern long long	strtoll(const char * __restrict, char ** __restrict, int);
3843a8a081bSWarner Losh extern unsigned long	strtoul(const char * __restrict, char ** __restrict, int);
3853a8a081bSWarner Losh extern unsigned long long strtoull(const char * __restrict, char ** __restrict, int);
3862b0268cfSWarner Losh 
387ca987d46SWarner Losh /* BCD conversions (undocumented) */
388ca987d46SWarner Losh extern u_char const	bcd2bin_data[];
389ca987d46SWarner Losh extern u_char const	bin2bcd_data[];
390ca987d46SWarner Losh extern char const	hex2ascii_data[];
391ca987d46SWarner Losh 
392ca987d46SWarner Losh #define	bcd2bin(bcd)	(bcd2bin_data[bcd])
393ca987d46SWarner Losh #define	bin2bcd(bin)	(bin2bcd_data[bin])
394ca987d46SWarner Losh #define	hex2ascii(hex)	(hex2ascii_data[hex])
395b6f4732cSIan Lepore #define	validbcd(bcd)	(bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0))
396ca987d46SWarner Losh 
397ca987d46SWarner Losh /* min/max (undocumented) */
imax(int a,int b)398ca987d46SWarner Losh static __inline int imax(int a, int b) { return (a > b ? a : b); }
imin(int a,int b)399ca987d46SWarner Losh static __inline int imin(int a, int b) { return (a < b ? a : b); }
lmax(long a,long b)400ca987d46SWarner Losh static __inline long lmax(long a, long b) { return (a > b ? a : b); }
lmin(long a,long b)401ca987d46SWarner Losh static __inline long lmin(long a, long b) { return (a < b ? a : b); }
max(u_int a,u_int b)402ca987d46SWarner Losh static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
min(u_int a,u_int b)403ca987d46SWarner Losh static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
qmax(quad_t a,quad_t b)404ca987d46SWarner Losh static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
qmin(quad_t a,quad_t b)405ca987d46SWarner Losh static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
ulmax(u_long a,u_long b)406ca987d46SWarner Losh static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
ulmin(u_long a,u_long b)407ca987d46SWarner Losh static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
408ca987d46SWarner Losh 
409ca987d46SWarner Losh /* null functions for device/filesystem switches (undocumented) */
410ca987d46SWarner Losh extern int	nodev(void);
411ca987d46SWarner Losh extern int	noioctl(struct open_file *, u_long, void *);
412ca987d46SWarner Losh extern void	nullsys(void);
413ca987d46SWarner Losh 
414ca987d46SWarner Losh extern int	null_open(const char *path, struct open_file *f);
415ca987d46SWarner Losh extern int	null_close(struct open_file *f);
416ca987d46SWarner Losh extern int	null_read(struct open_file *f, void *buf, size_t size, size_t *resid);
4172e7e6fbcSConrad Meyer extern int	null_write(struct open_file *f, const void *buf, size_t size, size_t *resid);
418ca987d46SWarner Losh extern off_t	null_seek(struct open_file *f, off_t offset, int where);
419ca987d46SWarner Losh extern int	null_stat(struct open_file *f, struct stat *sb);
420ca987d46SWarner Losh extern int	null_readdir(struct open_file *f, struct dirent *d);
421ca987d46SWarner Losh 
422ca987d46SWarner Losh 
423ca987d46SWarner Losh /*
424ca987d46SWarner Losh  * Machine dependent functions and data, must be provided or stubbed by
425ca987d46SWarner Losh  * the consumer
426ca987d46SWarner Losh  */
42786bb84d5SWarner Losh extern void		exit(int) __dead2;
428ca987d46SWarner Losh extern int		getchar(void);
429ca987d46SWarner Losh extern int		ischar(void);
430ca987d46SWarner Losh extern void		putchar(int);
431ca987d46SWarner Losh extern int		devopen(struct open_file *, const char *, const char **);
432ca987d46SWarner Losh extern int		devclose(struct open_file *f);
433ca987d46SWarner Losh extern void		panic(const char *, ...) __dead2 __printflike(1, 2);
43486bb84d5SWarner Losh extern void		panic_action(void) __weak_symbol __dead2;
43531d05586SWarner Losh extern time_t		getsecs(void);
436ca987d46SWarner Losh extern struct fs_ops	*file_system[];
437ca987d46SWarner Losh extern struct fs_ops	*exclusive_file_system;
438ca987d46SWarner Losh extern struct devsw	*devsw[];
439ca987d46SWarner Losh 
440ca987d46SWarner Losh /*
441b9c5b432SWarner Losh  * Time routines
442b9c5b432SWarner Losh  */
443b9c5b432SWarner Losh time_t time(time_t *);
444b9c5b432SWarner Losh 
445b9c5b432SWarner Losh /*
446ca987d46SWarner Losh  * Expose byteorder(3) functions.
447ca987d46SWarner Losh  */
448ca987d46SWarner Losh #ifndef _BYTEORDER_PROTOTYPED
449ca987d46SWarner Losh #define	_BYTEORDER_PROTOTYPED
450ca987d46SWarner Losh extern uint32_t		htonl(uint32_t);
451ca987d46SWarner Losh extern uint16_t		htons(uint16_t);
452ca987d46SWarner Losh extern uint32_t		ntohl(uint32_t);
453ca987d46SWarner Losh extern uint16_t		ntohs(uint16_t);
454ca987d46SWarner Losh #endif
455ca987d46SWarner Losh 
456ca987d46SWarner Losh #ifndef _BYTEORDER_FUNC_DEFINED
457ca987d46SWarner Losh #define	_BYTEORDER_FUNC_DEFINED
458ca987d46SWarner Losh #define	htonl(x)	__htonl(x)
459ca987d46SWarner Losh #define	htons(x)	__htons(x)
460ca987d46SWarner Losh #define	ntohl(x)	__ntohl(x)
461ca987d46SWarner Losh #define	ntohs(x)	__ntohs(x)
462ca987d46SWarner Losh #endif
463ca987d46SWarner Losh 
464ca987d46SWarner Losh void *Malloc(size_t, const char *, int);
46511db1a16SToomas Soome void *Memalign(size_t, size_t, const char *, int);
466ca987d46SWarner Losh void *Calloc(size_t, size_t, const char *, int);
467ca987d46SWarner Losh void *Realloc(void *, size_t, const char *, int);
46811db1a16SToomas Soome void *Reallocf(void *, size_t, const char *, int);
469ca987d46SWarner Losh void Free(void *, const char *, int);
47062ea4c11SToomas Soome extern void	mallocstats(void);
471ca987d46SWarner Losh 
472e9b148a3SSimon J. Gerraty const char *x86_hypervisor(void);
473e9b148a3SSimon J. Gerraty 
474c907ec9eSToomas Soome #ifdef USER_MALLOC
475c907ec9eSToomas Soome extern void *malloc(size_t);
476c907ec9eSToomas Soome extern void *memalign(size_t, size_t);
477c907ec9eSToomas Soome extern void *calloc(size_t, size_t);
478c907ec9eSToomas Soome extern void free(void *);
479c907ec9eSToomas Soome extern void *realloc(void *, size_t);
480c907ec9eSToomas Soome extern void *reallocf(void *, size_t);
4815292d0a7SToomas Soome #elif defined(DEBUG_MALLOC)
482ca987d46SWarner Losh #define malloc(x)	Malloc(x, __FILE__, __LINE__)
48311db1a16SToomas Soome #define memalign(x, y)	Memalign(x, y, __FILE__, __LINE__)
484ca987d46SWarner Losh #define calloc(x, y)	Calloc(x, y, __FILE__, __LINE__)
485ca987d46SWarner Losh #define free(x)		Free(x, __FILE__, __LINE__)
486ca987d46SWarner Losh #define realloc(x, y)	Realloc(x, y, __FILE__, __LINE__)
48762ea4c11SToomas Soome #define reallocf(x, y)	Reallocf(x, y, __FILE__, __LINE__)
488ca987d46SWarner Losh #else
489ca987d46SWarner Losh #define malloc(x)	Malloc(x, NULL, 0)
49011db1a16SToomas Soome #define memalign(x, y)	Memalign(x, y, NULL, 0)
491ca987d46SWarner Losh #define calloc(x, y)	Calloc(x, y, NULL, 0)
492ca987d46SWarner Losh #define free(x)		Free(x, NULL, 0)
493ca987d46SWarner Losh #define realloc(x, y)	Realloc(x, y, NULL, 0)
49462ea4c11SToomas Soome #define reallocf(x, y)	Reallocf(x, y, NULL, 0)
495ca987d46SWarner Losh #endif
496ca987d46SWarner Losh 
497ed2a6576SWarner Losh /*
498ed2a6576SWarner Losh  * va <-> pa routines. MD code must supply.
499ed2a6576SWarner Losh  */
500ed2a6576SWarner Losh caddr_t ptov(uintptr_t);
501ed2a6576SWarner Losh 
5021631382cSKyle Evans /* features.c */
5031631382cSKyle Evans typedef void (feature_iter_fn)(void *, const char *, const char *, bool);
5041631382cSKyle Evans 
5051631382cSKyle Evans extern void feature_enable(uint32_t);
5061631382cSKyle Evans extern bool feature_name_is_enabled(const char *);
5071631382cSKyle Evans extern void feature_iter(feature_iter_fn *, void *);
5081631382cSKyle Evans 
5091631382cSKyle Evans /*
5101631382cSKyle Evans  * Note that these should also be added to the mapping table in features.c,
5111631382cSKyle Evans  * which the interpreter may query to provide details from.  The name with
5121631382cSKyle Evans  * FEATURE_ removed is assumed to be the name we'll provide in the loader
5131631382cSKyle Evans  * features table, just to simplify reasoning about these.
5141631382cSKyle Evans  */
5151631382cSKyle Evans #define	FEATURE_EARLY_ACPI	0x0001
5161631382cSKyle Evans 
517ed19b7c5SSimon J. Gerraty /* hexdump.c */
518ed19b7c5SSimon J. Gerraty void	hexdump(caddr_t region, size_t len);
519ed19b7c5SSimon J. Gerraty 
520d1ea5017SWarner Losh /* nvstore.c */
521d1ea5017SWarner Losh typedef int (nvstore_getter_cb_t)(void *, const char *, void **);
522d1ea5017SWarner Losh typedef int (nvstore_setter_cb_t)(void *, int, const char *,
523d1ea5017SWarner Losh     const void *, size_t);
524d1ea5017SWarner Losh typedef int (nvstore_setter_str_cb_t)(void *, const char *, const char *,
525d1ea5017SWarner Losh     const char *);
526d1ea5017SWarner Losh typedef int (nvstore_unset_cb_t)(void *, const char *);
527d1ea5017SWarner Losh typedef int (nvstore_print_cb_t)(void *, void *);
528d1ea5017SWarner Losh typedef int (nvstore_iterate_cb_t)(void *, int (*)(void *, void *));
529d1ea5017SWarner Losh 
530d1ea5017SWarner Losh typedef struct nvs_callbacks {
531d1ea5017SWarner Losh 	nvstore_getter_cb_t	*nvs_getter;
532d1ea5017SWarner Losh 	nvstore_setter_cb_t	*nvs_setter;
533d1ea5017SWarner Losh 	nvstore_setter_str_cb_t *nvs_setter_str;
534d1ea5017SWarner Losh 	nvstore_unset_cb_t	*nvs_unset;
535d1ea5017SWarner Losh 	nvstore_print_cb_t	*nvs_print;
536d1ea5017SWarner Losh 	nvstore_iterate_cb_t	*nvs_iterate;
537d1ea5017SWarner Losh } nvs_callbacks_t;
538d1ea5017SWarner Losh 
539d1ea5017SWarner Losh int nvstore_init(const char *, nvs_callbacks_t *, void *);
540d1ea5017SWarner Losh int nvstore_fini(const char *);
541d1ea5017SWarner Losh void *nvstore_get_store(const char *);
542d1ea5017SWarner Losh int nvstore_print(void *);
543d1ea5017SWarner Losh int nvstore_get_var(void *, const char *, void **);
544d1ea5017SWarner Losh int nvstore_set_var(void *, int, const char *, void *, size_t);
545d1ea5017SWarner Losh int nvstore_set_var_from_string(void *, const char *, const char *,
546d1ea5017SWarner Losh     const char *);
547d1ea5017SWarner Losh int nvstore_unset_var(void *, const char *);
548d1ea5017SWarner Losh 
549e193d3baSColin Percival /* tslog.c */
550e193d3baSColin Percival #define TSRAW(a, b, c) tslog(a, b, c)
551e193d3baSColin Percival #define TSENTER() TSRAW("ENTER", __func__, NULL)
55201cad731SEmmanuel Vadot #define TSENTER2(x) TSRAW("ENTER", __func__, x)
553e193d3baSColin Percival #define TSEXIT() TSRAW("EXIT", __func__, NULL)
554e193d3baSColin Percival #define TSLINE() TSRAW("EVENT", __FILE__, __XSTRING(__LINE__))
555e193d3baSColin Percival void tslog(const char *, const char *, const char *);
556e193d3baSColin Percival void tslog_setbuf(void * buf, size_t len);
557e193d3baSColin Percival void tslog_getbuf(void ** buf, size_t * len);
558e193d3baSColin Percival 
5595cf20707SWarner Losh __END_DECLS
5605cf20707SWarner Losh 
561ca987d46SWarner Losh #endif	/* STAND_H */
562