1 /* 2 * Copyright (c) 1998 Michael Smith. 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 * From $NetBSD: stand.h,v 1.22 1997/06/26 19:17:40 drochner Exp $ 26 */ 27 28 /*- 29 * Copyright (c) 1993 30 * The Regents of the University of California. All rights reserved. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 3. Neither the name of the University nor the names of its contributors 41 * may be used to endorse or promote products derived from this software 42 * without specific prior written permission. 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54 * SUCH DAMAGE. 55 * 56 * @(#)stand.h 8.1 (Berkeley) 6/11/93 57 */ 58 59 #ifndef STAND_H 60 #define STAND_H 61 62 #include <sys/cdefs.h> 63 64 #include <sys/types.h> 65 #include <sys/stat.h> 66 #include <sys/dirent.h> 67 #include <sys/queue.h> 68 69 /* this header intentionally exports NULL from <string.h> */ 70 #include <string.h> 71 #define strcoll(a, b) strcmp((a), (b)) 72 73 #define CHK(fmt, args...) printf("%s(%d): " fmt "\n", __func__, __LINE__ , ##args) 74 #define PCHK(fmt, args...) {printf("%s(%d): " fmt "\n", __func__, __LINE__ , ##args); getchar();} 75 76 #include <sys/errno.h> 77 78 /* special stand error codes */ 79 #define EADAPT (ELAST+1) /* bad adaptor */ 80 #define ECTLR (ELAST+2) /* bad controller */ 81 #define EUNIT (ELAST+3) /* bad unit */ 82 #define ESLICE (ELAST+4) /* bad slice */ 83 #define EPART (ELAST+5) /* bad partition */ 84 #define ERDLAB (ELAST+6) /* can't read disk label */ 85 #define EUNLAB (ELAST+7) /* unlabeled disk */ 86 #define EOFFSET (ELAST+8) /* relative seek not supported */ 87 #define ESALAST (ELAST+8) /* */ 88 89 /* Partial signal emulation for sig_atomic_t */ 90 #include <machine/signal.h> 91 92 __BEGIN_DECLS 93 94 struct open_file; 95 96 /* 97 * This structure is used to define file system operations in a file system 98 * independent way. 99 * 100 * XXX note that filesystem providers should export a pointer to their fs_ops 101 * struct, so that consumers can reference this and thus include the 102 * filesystems that they require. 103 */ 104 struct fs_ops { 105 const char *fs_name; 106 int (*fo_open)(const char *path, struct open_file *f); 107 int (*fo_close)(struct open_file *f); 108 int (*fo_read)(struct open_file *f, void *buf, 109 size_t size, size_t *resid); 110 int (*fo_write)(struct open_file *f, const void *buf, 111 size_t size, size_t *resid); 112 off_t (*fo_seek)(struct open_file *f, off_t offset, int where); 113 int (*fo_stat)(struct open_file *f, struct stat *sb); 114 int (*fo_readdir)(struct open_file *f, struct dirent *d); 115 int (*fo_preload)(struct open_file *f); 116 int (*fo_mount)(const char *, const char *, void **); 117 int (*fo_unmount)(const char *, void *); 118 }; 119 120 /* 121 * libsa-supplied filesystems 122 */ 123 extern struct fs_ops ufs_fsops; 124 extern struct fs_ops tftp_fsops; 125 extern struct fs_ops nfs_fsops; 126 extern struct fs_ops cd9660_fsops; 127 extern struct fs_ops gzipfs_fsops; 128 extern struct fs_ops bzipfs_fsops; 129 extern struct fs_ops dosfs_fsops; 130 extern struct fs_ops ext2fs_fsops; 131 extern struct fs_ops splitfs_fsops; 132 extern struct fs_ops pkgfs_fsops; 133 extern struct fs_ops efihttp_fsops; 134 135 /* where values for lseek(2) */ 136 #define SEEK_SET 0 /* set file offset to offset */ 137 #define SEEK_CUR 1 /* set file offset to current plus offset */ 138 #define SEEK_END 2 /* set file offset to EOF plus offset */ 139 140 /* 141 * Device switch 142 */ 143 #define DEV_NAMLEN 8 /* Length of name of device class */ 144 #define DEV_DEVLEN 128 /* Length of longest device instance name */ 145 struct devdesc; 146 struct devsw { 147 const char dv_name[DEV_NAMLEN]; 148 int dv_type; /* opaque type constant */ 149 #define DEVT_NONE 0 150 #define DEVT_DISK 1 151 #define DEVT_NET 2 152 #define DEVT_CD 3 153 #define DEVT_ZFS 4 154 #define DEVT_FD 5 155 int (*dv_init)(void); /* early probe call */ 156 int (*dv_strategy)(void *devdata, int rw, daddr_t blk, 157 size_t size, char *buf, size_t *rsize); 158 int (*dv_open)(struct open_file *f, ...); 159 int (*dv_close)(struct open_file *f); 160 int (*dv_ioctl)(struct open_file *f, u_long cmd, void *data); 161 int (*dv_print)(int verbose); /* print device information */ 162 void (*dv_cleanup)(void); 163 char * (*dv_fmtdev)(struct devdesc *); 164 int (*dv_parsedev)(struct devdesc **, const char *, const char **); 165 bool (*dv_match)(struct devsw *, const char *); 166 }; 167 168 /* 169 * libsa-supplied device switch 170 */ 171 extern struct devsw netdev; 172 173 extern int errno; 174 175 /* 176 * Generic device specifier; architecture-dependent versions may be larger, but 177 * should be allowed to overlap. The larger device specifiers store more data 178 * than can fit in the generic one that's gleaned after parsing the device 179 * string, or used in some cases to indicate wildcards that match a variety of 180 * situations based on what's on the drive itself rather than what the progammer 181 * might know in advance. Information about open files is stored in d_opendata, 182 * though what's passed into the open routine may differ from what's present 183 * after the open on some configurations. 184 */ 185 struct devdesc { 186 struct devsw *d_dev; 187 int d_unit; 188 void *d_opendata; 189 }; 190 191 char *devformat(struct devdesc *d); 192 int devparse(struct devdesc **, const char *, const char **); 193 int devinit(void); 194 void dev_cleanup(void); 195 196 struct open_file { 197 int f_flags; /* see F_* below */ 198 struct devsw *f_dev; /* pointer to device operations */ 199 void *f_devdata; /* device specific data */ 200 struct fs_ops *f_ops; /* pointer to file system operations */ 201 void *f_fsdata; /* file system specific data */ 202 off_t f_offset; /* current file offset */ 203 char *f_rabuf; /* readahead buffer pointer */ 204 size_t f_ralen; /* valid data in readahead buffer */ 205 off_t f_raoffset; /* consumer offset in readahead buffer */ 206 int f_id; /* file number */ 207 TAILQ_ENTRY(open_file) f_link; /* next entry */ 208 #define SOPEN_RASIZE 512 209 }; 210 211 typedef TAILQ_HEAD(file_list, open_file) file_list_t; 212 extern file_list_t files; 213 extern struct open_file *fd2open_file(int); 214 215 /* f_flags values */ 216 #define F_READ 0x0001 /* file opened for reading */ 217 #define F_WRITE 0x0002 /* file opened for writing */ 218 #define F_RAW 0x0004 /* raw device open - no file system */ 219 #define F_NODEV 0x0008 /* network open - no device */ 220 #define F_MASK 0xFFFF 221 /* Mode modifier for strategy() */ 222 #define F_NORA (0x01 << 16) /* Disable Read-Ahead */ 223 224 #define isascii(c) (((c) & ~0x7F) == 0) 225 226 static __inline int isupper(int c) 227 { 228 return c >= 'A' && c <= 'Z'; 229 } 230 231 static __inline int islower(int c) 232 { 233 return c >= 'a' && c <= 'z'; 234 } 235 236 static __inline int isspace(int c) 237 { 238 return c == ' ' || (c >= 0x9 && c <= 0xd); 239 } 240 241 static __inline int isdigit(int c) 242 { 243 return c >= '0' && c <= '9'; 244 } 245 246 static __inline int isxdigit(int c) 247 { 248 return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); 249 } 250 251 static __inline int isalpha(int c) 252 { 253 return isupper(c) || islower(c); 254 } 255 256 static __inline int isalnum(int c) 257 { 258 return isalpha(c) || isdigit(c); 259 } 260 261 static __inline int iscntrl(int c) 262 { 263 return (c >= 0 && c < ' ') || c == 127; 264 } 265 266 static __inline int isgraph(int c) 267 { 268 return c >= '!' && c <= '~'; 269 } 270 271 static __inline int ispunct(int c) 272 { 273 return (c >= '!' && c <= '/') || (c >= ':' && c <= '@') || 274 (c >= '[' && c <= '`') || (c >= '{' && c <= '~'); 275 } 276 277 static __inline int toupper(int c) 278 { 279 return islower(c) ? c - 'a' + 'A' : c; 280 } 281 282 static __inline int tolower(int c) 283 { 284 return isupper(c) ? c - 'A' + 'a' : c; 285 } 286 287 /* sbrk emulation */ 288 extern void setheap(void *base, void *top); 289 extern char *sbrk(int incr); 290 291 extern int printf(const char *fmt, ...) __printflike(1, 2); 292 extern int asprintf(char **buf, const char *cfmt, ...) __printflike(2, 3); 293 extern int sprintf(char *buf, const char *cfmt, ...) __printflike(2, 3); 294 extern int snprintf(char *buf, size_t size, const char *cfmt, ...) __printflike(3, 4); 295 extern int vprintf(const char *fmt, __va_list); 296 extern int vsprintf(char *buf, const char *cfmt, __va_list); 297 extern int vsnprintf(char *buf, size_t size, const char *cfmt, __va_list); 298 299 extern void twiddle(u_int callerdiv); 300 extern void twiddle_divisor(u_int globaldiv); 301 302 extern void ngets(char *, int); 303 #define gets(x) ngets((x), 0) 304 extern int fgetstr(char *buf, int size, int fd); 305 306 extern int mount(const char *dev, const char *path, int flags, void *data); 307 extern int unmount(const char *dev, int flags); 308 extern int open(const char *, int); 309 #define O_RDONLY 0x0 310 #define O_WRONLY 0x1 311 #define O_RDWR 0x2 312 #define O_ACCMODE 0x3 313 /* NOT IMPLEMENTED */ 314 #define O_CREAT 0x0200 /* create if nonexistent */ 315 #define O_TRUNC 0x0400 /* truncate to zero length */ 316 extern int close(int); 317 extern void closeall(void); 318 extern ssize_t read(int, void *, size_t); 319 extern ssize_t write(int, const void *, size_t); 320 extern int ioctl(int, u_long, void *); 321 extern struct dirent *readdirfd(int); 322 extern void preload(int); 323 324 extern void srandom(unsigned int); 325 extern long random(void); 326 327 /* imports from stdlib, locally modified */ 328 extern char *optarg; /* getopt(3) external variables */ 329 extern int optind, opterr, optopt, optreset; 330 extern int getopt(int, char * const [], const char *); 331 332 /* pager.c */ 333 extern void pager_open(void); 334 extern void pager_close(void); 335 extern int pager_output(const char *lines); 336 extern int pager_file(const char *fname); 337 338 /* No signal state to preserve */ 339 #define setjmp _setjmp 340 #define longjmp _longjmp 341 342 /* environment.c */ 343 #define EV_DYNAMIC (1<<0) /* value was dynamically allocated, free if changed/unset */ 344 #define EV_VOLATILE (1<<1) /* value is volatile, make a copy of it */ 345 #define EV_NOHOOK (1<<2) /* don't call hook when setting */ 346 347 struct env_var; 348 typedef char *(ev_format_t)(struct env_var *ev); 349 typedef int (ev_sethook_t)(struct env_var *ev, int flags, 350 const void *value); 351 typedef int (ev_unsethook_t)(struct env_var *ev); 352 353 struct env_var 354 { 355 char *ev_name; 356 int ev_flags; 357 void *ev_value; 358 ev_sethook_t *ev_sethook; 359 ev_unsethook_t *ev_unsethook; 360 struct env_var *ev_next, *ev_prev; 361 }; 362 extern struct env_var *environ; 363 364 extern struct env_var *env_getenv(const char *name); 365 extern int env_setenv(const char *name, int flags, 366 const void *value, ev_sethook_t sethook, 367 ev_unsethook_t unsethook); 368 extern void env_discard(struct env_var *); 369 extern char *getenv(const char *name); 370 extern int setenv(const char *name, const char *value, 371 int overwrite); 372 extern int putenv(char *string); 373 extern int unsetenv(const char *name); 374 375 extern ev_sethook_t env_noset; /* refuse set operation */ 376 extern ev_unsethook_t env_nounset; /* refuse unset operation */ 377 378 /* stdlib.h routines */ 379 extern int abs(int a); 380 extern void abort(void) __dead2; 381 extern long strtol(const char * __restrict, char ** __restrict, int); 382 extern long long strtoll(const char * __restrict, char ** __restrict, int); 383 extern unsigned long strtoul(const char * __restrict, char ** __restrict, int); 384 extern unsigned long long strtoull(const char * __restrict, char ** __restrict, int); 385 386 /* BCD conversions (undocumented) */ 387 extern u_char const bcd2bin_data[]; 388 extern u_char const bin2bcd_data[]; 389 extern char const hex2ascii_data[]; 390 391 #define bcd2bin(bcd) (bcd2bin_data[bcd]) 392 #define bin2bcd(bin) (bin2bcd_data[bin]) 393 #define hex2ascii(hex) (hex2ascii_data[hex]) 394 #define validbcd(bcd) (bcd == 0 || (bcd > 0 && bcd <= 0x99 && bcd2bin_data[bcd] != 0)) 395 396 /* min/max (undocumented) */ 397 static __inline int imax(int a, int b) { return (a > b ? a : b); } 398 static __inline int imin(int a, int b) { return (a < b ? a : b); } 399 static __inline long lmax(long a, long b) { return (a > b ? a : b); } 400 static __inline long lmin(long a, long b) { return (a < b ? a : b); } 401 static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); } 402 static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); } 403 static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); } 404 static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); } 405 static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); } 406 static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); } 407 408 /* null functions for device/filesystem switches (undocumented) */ 409 extern int nodev(void); 410 extern int noioctl(struct open_file *, u_long, void *); 411 extern void nullsys(void); 412 413 extern int null_open(const char *path, struct open_file *f); 414 extern int null_close(struct open_file *f); 415 extern int null_read(struct open_file *f, void *buf, size_t size, size_t *resid); 416 extern int null_write(struct open_file *f, const void *buf, size_t size, size_t *resid); 417 extern off_t null_seek(struct open_file *f, off_t offset, int where); 418 extern int null_stat(struct open_file *f, struct stat *sb); 419 extern int null_readdir(struct open_file *f, struct dirent *d); 420 421 422 /* 423 * Machine dependent functions and data, must be provided or stubbed by 424 * the consumer 425 */ 426 extern void exit(int) __dead2; 427 extern int getchar(void); 428 extern int ischar(void); 429 extern void putchar(int); 430 extern int devopen(struct open_file *, const char *, const char **); 431 extern int devclose(struct open_file *f); 432 extern void panic(const char *, ...) __dead2 __printflike(1, 2); 433 extern void panic_action(void) __weak_symbol __dead2; 434 extern time_t getsecs(void); 435 extern struct fs_ops *file_system[]; 436 extern struct fs_ops *exclusive_file_system; 437 extern struct devsw *devsw[]; 438 439 /* 440 * Time routines 441 */ 442 time_t time(time_t *); 443 444 /* 445 * Expose byteorder(3) functions. 446 */ 447 #ifndef _BYTEORDER_PROTOTYPED 448 #define _BYTEORDER_PROTOTYPED 449 extern uint32_t htonl(uint32_t); 450 extern uint16_t htons(uint16_t); 451 extern uint32_t ntohl(uint32_t); 452 extern uint16_t ntohs(uint16_t); 453 #endif 454 455 #ifndef _BYTEORDER_FUNC_DEFINED 456 #define _BYTEORDER_FUNC_DEFINED 457 #define htonl(x) __htonl(x) 458 #define htons(x) __htons(x) 459 #define ntohl(x) __ntohl(x) 460 #define ntohs(x) __ntohs(x) 461 #endif 462 463 void *Malloc(size_t, const char *, int); 464 void *Memalign(size_t, size_t, const char *, int); 465 void *Calloc(size_t, size_t, const char *, int); 466 void *Realloc(void *, size_t, const char *, int); 467 void *Reallocf(void *, size_t, const char *, int); 468 void Free(void *, const char *, int); 469 extern void mallocstats(void); 470 471 const char *x86_hypervisor(void); 472 473 #ifdef USER_MALLOC 474 extern void *malloc(size_t); 475 extern void *memalign(size_t, size_t); 476 extern void *calloc(size_t, size_t); 477 extern void free(void *); 478 extern void *realloc(void *, size_t); 479 extern void *reallocf(void *, size_t); 480 #elif defined(DEBUG_MALLOC) 481 #define malloc(x) Malloc(x, __FILE__, __LINE__) 482 #define memalign(x, y) Memalign(x, y, __FILE__, __LINE__) 483 #define calloc(x, y) Calloc(x, y, __FILE__, __LINE__) 484 #define free(x) Free(x, __FILE__, __LINE__) 485 #define realloc(x, y) Realloc(x, y, __FILE__, __LINE__) 486 #define reallocf(x, y) Reallocf(x, y, __FILE__, __LINE__) 487 #else 488 #define malloc(x) Malloc(x, NULL, 0) 489 #define memalign(x, y) Memalign(x, y, NULL, 0) 490 #define calloc(x, y) Calloc(x, y, NULL, 0) 491 #define free(x) Free(x, NULL, 0) 492 #define realloc(x, y) Realloc(x, y, NULL, 0) 493 #define reallocf(x, y) Reallocf(x, y, NULL, 0) 494 #endif 495 496 /* 497 * va <-> pa routines. MD code must supply. 498 */ 499 caddr_t ptov(uintptr_t); 500 501 /* hexdump.c */ 502 void hexdump(caddr_t region, size_t len); 503 504 /* nvstore.c */ 505 typedef int (nvstore_getter_cb_t)(void *, const char *, void **); 506 typedef int (nvstore_setter_cb_t)(void *, int, const char *, 507 const void *, size_t); 508 typedef int (nvstore_setter_str_cb_t)(void *, const char *, const char *, 509 const char *); 510 typedef int (nvstore_unset_cb_t)(void *, const char *); 511 typedef int (nvstore_print_cb_t)(void *, void *); 512 typedef int (nvstore_iterate_cb_t)(void *, int (*)(void *, void *)); 513 514 typedef struct nvs_callbacks { 515 nvstore_getter_cb_t *nvs_getter; 516 nvstore_setter_cb_t *nvs_setter; 517 nvstore_setter_str_cb_t *nvs_setter_str; 518 nvstore_unset_cb_t *nvs_unset; 519 nvstore_print_cb_t *nvs_print; 520 nvstore_iterate_cb_t *nvs_iterate; 521 } nvs_callbacks_t; 522 523 int nvstore_init(const char *, nvs_callbacks_t *, void *); 524 int nvstore_fini(const char *); 525 void *nvstore_get_store(const char *); 526 int nvstore_print(void *); 527 int nvstore_get_var(void *, const char *, void **); 528 int nvstore_set_var(void *, int, const char *, void *, size_t); 529 int nvstore_set_var_from_string(void *, const char *, const char *, 530 const char *); 531 int nvstore_unset_var(void *, const char *); 532 533 /* tslog.c */ 534 #define TSRAW(a, b, c) tslog(a, b, c) 535 #define TSENTER() TSRAW("ENTER", __func__, NULL) 536 #define TSENTER2(x) TSRAW("ENTER", __func__, x) 537 #define TSEXIT() TSRAW("EXIT", __func__, NULL) 538 #define TSLINE() TSRAW("EVENT", __FILE__, __XSTRING(__LINE__)) 539 void tslog(const char *, const char *, const char *); 540 void tslog_setbuf(void * buf, size_t len); 541 void tslog_getbuf(void ** buf, size_t * len); 542 543 __END_DECLS 544 545 #endif /* STAND_H */ 546