1/*	$OpenBSD: stand.h,v 1.72 2021/12/01 17:25:35 kettenis Exp $	*/
2/*	$NetBSD: stand.h,v 1.18 1996/11/30 04:35:51 gwr Exp $	*/
3
4/*-
5 * Copyright (c) 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	@(#)stand.h	8.1 (Berkeley) 6/11/93
33 */
34
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/stdarg.h>
38#include <sys/stdint.h>
39#include "saerrno.h"
40
41#ifndef NULL
42#define	NULL	0
43#endif
44
45struct open_file;
46
47/*
48 * Useful macros
49 */
50/* don't define if libkern included */
51#ifndef LIBKERN_INLINE
52#define	max(a,b)	(((a)>(b))? (a) : (b))
53#define	min(a,b)	(((a)>(b))? (b) : (a))
54#endif
55
56/*
57 * This structure is used to define file system operations in a file system
58 * independent way.
59 */
60struct fs_ops {
61	int	(*open)(char *path, struct open_file *f);
62	int	(*close)(struct open_file *f);
63	int	(*read)(struct open_file *f, void *buf,
64		    size_t size, size_t *resid);
65	int	(*write)(struct open_file *f, void *buf,
66		    size_t size, size_t *resid);
67	off_t	(*seek)(struct open_file *f, off_t offset, int where);
68	int	(*stat)(struct open_file *f, struct stat *sb);
69	int	(*readdir)(struct open_file *f, char *);
70	int	(*fchmod)(struct open_file *f, mode_t);
71};
72
73extern struct fs_ops file_system[];
74extern int nfsys;
75
76/* where values for lseek(2) */
77#define	SEEK_SET	0	/* set file offset to offset */
78#define	SEEK_CUR	1	/* set file offset to current plus offset */
79#define	SEEK_END	2	/* set file offset to EOF plus offset */
80
81/* Device switch */
82struct devsw {
83	char	*dv_name;
84	int	(*dv_strategy)(void *devdata, int rw,
85				    daddr_t blk, size_t size,
86				    void *buf, size_t *rsize);
87	int	(*dv_open)(struct open_file *f, ...);
88	int	(*dv_close)(struct open_file *f);
89	int	(*dv_ioctl)(struct open_file *f, u_long cmd, void *data);
90};
91
92extern struct devsw devsw[];	/* device array */
93extern int ndevs;		/* number of elements in devsw[] */
94
95extern struct consdev *cn_tab;
96
97struct open_file {
98	int		f_flags;	/* see F_* below */
99	struct devsw	*f_dev;		/* pointer to device operations */
100	void		*f_devdata;	/* device specific data */
101	struct fs_ops	*f_ops;		/* pointer to file system operations */
102	void		*f_fsdata;	/* file system specific data */
103	off_t		f_offset;	/* current file offset (F_RAW) */
104};
105
106#define	SOPEN_MAX	4
107extern struct open_file files[];
108
109/* f_flags values */
110#define F_READ          0x0001 /* file opened for reading */
111#define F_WRITE         0x0002 /* file opened for writing */
112#define F_RAW           0x0004 /* raw device open - no file system */
113#define F_NODEV         0x0008 /* network open - no device */
114#define F_NOWRITE       0x0010 /* bootblock writing broken or unsupported */
115
116#define isupper(c)	((c) >= 'A' && (c) <= 'Z')
117#define islower(c)	((c) >= 'a' && (c) <= 'z')
118#define isalpha(c)	(isupper(c)||islower(c))
119#define tolower(c)	(isupper(c)?((c) - 'A' + 'a'):(c))
120#define toupper(c)	(islower(c)?((c) - 'a' + 'A'):(c))
121#define isspace(c)	((c) == ' ' || (c) == '\t')
122#define isdigit(c)	((c) >= '0' && (c) <= '9')
123
124#define	btochs(b,c,h,s,nh,ns)			\
125	c = (b) / ((nh) * (ns));		\
126	h = ((b) % ((nh) * (ns))) / (ns);	\
127	s = ((b) % ((nh) * (ns))) % (ns);
128
129void	*alloc(u_int);
130void	free(void *, u_int);
131struct	disklabel;
132char	*getdisklabel(const char *, struct disklabel *);
133u_int	dkcksum(const struct disklabel *);
134
135#define BOOTRANDOM	"/etc/random.seed"
136#define BOOTRANDOM_MAX	256	/* no point being greater than RC4STATE */
137extern char rnddata[BOOTRANDOM_MAX];
138
139void	printf(const char *, ...);
140int	snprintf(char *, size_t, const char *, ...);
141void	vprintf(const char *, __va_list);
142void	twiddle(void);
143void	getln(char *, size_t);
144__dead void	panic(const char *, ...) __attribute__((noreturn));
145__dead void	_rtt(void) __attribute__((noreturn));
146#define	bzero(s,n)	((void)memset((s),0,(n)))
147#define bcmp(s1,s2,n)	(memcmp((s2),(s1),(n)))
148#define	bcopy(s1,s2,n)	((void)memmove((s2),(s1),(n)))
149void	explicit_bzero(void *, size_t);
150void	hexdump(const void *, size_t);
151void	*memcpy(void *, const void *, size_t);
152void	*memmove(void *, const void *, size_t);
153int	memcmp(const void *, const void *, size_t);
154char	*strncpy(char *, const char *, size_t);
155int	strncmp(const char *, const char *, size_t);
156int	strcmp(const char *, const char *);
157size_t	strlen(const char *);
158long	strtol(const char *, char **, int);
159long long	strtoll(const char *, char **, int);
160char	*strchr(const char *, int);
161void	*memset(void *, int, size_t);
162void	exit(void);
163#define O_RDONLY        0x0000          /* open for reading only */
164#define O_WRONLY        0x0001          /* open for writing only */
165#define O_RDWR          0x0002          /* open for reading and writing */
166int	open(const char *, int);
167int	close(int);
168void	closeall(void);
169ssize_t	read(int, void *, size_t);
170ssize_t	write(int, void *, size_t);
171int	stat(const char *path, struct stat *sb);
172int	fstat(int fd, struct stat *sb);
173off_t	lseek(int, off_t, int);
174int	opendir(const char *);
175int	readdir(int, char *);
176void	closedir(int);
177int	nodev(void);
178int	noioctl(struct open_file *, u_long, void *);
179void	nullsys(void);
180
181int	null_open(char *path, struct open_file *f);
182int	null_close(struct open_file *f);
183ssize_t	null_read(struct open_file *f, void *buf, size_t size, size_t *resid);
184ssize_t	null_write(struct open_file *f, void *buf, size_t size, size_t *resid);
185off_t	null_seek(struct open_file *f, off_t offset, int where);
186int	null_stat(struct open_file *f, struct stat *sb);
187int	null_readdir(struct open_file *f, char *name);
188char	*ttyname(int); /* match userland decl, but ignore !0 */
189dev_t	ttydev(char *);
190void	cninit(void);
191int	cnset(dev_t);
192void	cnputc(int);
193int	cngetc(void);
194int	cnischar(void);
195int	cnspeed(dev_t, int);
196u_int	sleep(u_int);
197void	usleep(u_int);
198char	*ctime(const time_t *);
199
200int	ioctl(int, u_long, char *);
201
202void	putchar(int);
203int	getchar(void);
204
205#ifdef __INTERNAL_LIBSA_CREAD
206int	oopen(const char *, int);
207int	oclose(int);
208ssize_t	oread(int, void *, size_t);
209off_t	olseek(int, off_t, int);
210#endif
211
212/* Machine dependent functions */
213int	devopen(struct open_file *, const char *, char **);
214void	machdep_start(char *, int, char *, char *, char *);
215time_t	getsecs(void);
216