code:
/*	$OpenBSD: sgl_float.h,v 1.12 2013/11/26 20:33:12 deraadt Exp $	*/
/*
  (c) Copyright 1986 HEWLETT-PACKARD COMPANY
  To anyone who acknowledges that this file is provided "AS IS"
  without any express or implied warranty:
      permission to use, copy, modify, and distribute this file
  for any purpose is hereby granted without fee, provided that
  the above copyright notice and this notice appears in all
  copies, and that the name of Hewlett-Packard Company not be
  used in advertising or publicity pertaining to distribution
  of the software without specific, written prior permission.
  Hewlett-Packard Company makes no representations about the
  suitability of this software for any purpose.
*/
/* @(#)sgl_float.h: Revision: 2.8.88.1 Date: 93/12/07 15:07:17 */

#include <sys/cdefs.h>

/******************************
 * Single precision functions *
 ******************************/

/* 32-bit word grabing functions */
#define Sgl_firstword(value) Sall(value)
#define Sgl_secondword(value) dummy_location
#define Sgl_thirdword(value) dummy_location
#define Sgl_fourthword(value) dummy_location

#define Sgl_sign(object) Ssign(object)
#define Sgl_exponent(object) Sexponent(object)
#define Sgl_signexponent(object) Ssignexponent(object)
#define Sgl_mantissa(object) Smantissa(object)
#define Sgl_exponentmantissa(object) Sexponentmantissa(object)
#define Sgl_all(object) Sall(object)

/* sgl_and_signs ands the sign bits of each argument and puts the result
 * into the first argument. sgl_or_signs ors those same sign bits */
#define Sgl_and_signs( src1dst, src2)		\
    Sall(src1dst) = (Sall(src2)|~(1<<31)) & Sall(src1dst)
#define Sgl_or_signs( src1dst, src2)		\
    Sall(src1dst) = (Sall(src2)&(1<<31)) | Sall(src1dst)

/* The hidden bit is always the low bit of the exponent */
#define Sgl_clear_exponent_set_hidden(srcdst) Deposit_sexponent(srcdst,1)
#define Sgl_clear_signexponent_set_hidden(srcdst) \
    Deposit_ssignexponent(srcdst,1)
#define Sgl_clear_sign(srcdst) Sall(srcdst) &= ~(1<<31)
#define Sgl_clear_signexponent(srcdst) Sall(srcdst) &= 0x007fffff

/* varamount must be less than 32 for the next three functions */
#define Sgl_rightshift(srcdst, varamount)	\
    Sall(srcdst) >>= varamount
#define Sgl_leftshift(srcdst, varamount)	\
    Sall(srcdst) <<= varamount
#define Sgl_rightshift_exponentmantissa(srcdst, varamount) \
    Sall(srcdst) = \
	(Sexponentmantissa(srcdst) >> (varamount)) | (Sall(srcdst) & (1<<31))

#define Sgl_leftshiftby1_withextent(left,right,result) \
    Shiftdouble(Sall(left),Extall(right),31,Sall(result))

#define Sgl_rightshiftby1_withextent(left,right,dst)		\
    Shiftdouble(Sall(left),Extall(right),1,Extall(right))
#define Sgl_arithrightshiftby1(srcdst)	\
    Sall(srcdst) = (int)Sall(srcdst) >> 1

/* Sign extend the sign bit with an integer destination */
#define Sgl_signextendedsign(value) Ssignedsign(value)

#define Sgl_isone_hidden(sgl_value) (Shidden(sgl_value))
#define Sgl_increment(sgl_value) Sall(sgl_value) += 1
#define Sgl_increment_mantissa(sgl_value) \
    Deposit_smantissa(sgl_value,sgl_value+1)
#define Sgl_decrement(sgl_value) Sall(sgl_value) -= 1

#define Sgl_isone_sign(sgl_value) (Is_ssign(sgl_value)!=0)
#define Sgl_isone_hiddenoverflow(sgl_value) \
    (Is_shiddenoverflow(sgl_value)!=0)
#define Sgl_isone_lowmantissa(sgl_value) (Is_slow(sgl_value)!=0)
#define Sgl_isone_signaling(sgl_value) (Is_ssignaling(sgl_value)!=0)
#define Sgl_is_signalingnan(sgl_value) (Ssignalingnan(sgl_value)==0x1ff)
#define Sgl_isnotzero(sgl_value) (Sall(sgl_value)!=0)
#define Sgl_isnotzero_hiddenhigh7mantissa(sgl_value) \
    (Shiddenhigh7mantissa(sgl_value)!=0)
#define Sgl_isnotzero_low4(sgl_value) (Slow4(sgl_value)!=0)
#define Sgl_isnotzero_exponent(sgl_value) (Sexponent(sgl_value)!=0)
#define Sgl_isnotzero_mantissa(sgl_value) (Smantissa(sgl_value)!=0)
#define Sgl_isnotzero_exponentmantissa(sgl_value) \
    (Sexponentmantissa(sgl_value)!=0)
#define Sgl_iszero(sgl_value) (Sall(sgl_value)==0)
#define Sgl_iszero_signaling(sgl_value) (Is_ssignaling(sgl_value)==0)
#define Sgl_iszero_hidden(sgl_value) (Is_shidden(sgl_value)==0)
#define Sgl_iszero_hiddenoverflow(sgl_value) \
    (Is_shiddenoverflow(sgl_value)==0)
#define Sgl_iszero_hiddenhigh3mantissa(sgl_value) \
    (Shiddenhigh3mantissa(sgl_value)==0)
#define Sgl_iszero_hiddenhigh7mantissa(sgl_value) \
    (Shiddenhigh7mantissa(sgl_value)==0)
#define Sgl_iszero_sign(sgl_value) (Is_ssign(sgl_value)==0)
#define Sgl_iszero_exponent(sgl_value) (Sexponent(sgl_value)==0)
#define Sgl_iszero_mantissa(sgl_value) (Smantissa(sgl_value)==0)
#define Sgl_iszero_exponentmantissa(sgl_value) \
    (Sexponentmantissa(sgl_value)==0)
#define Sgl_isinfinity_exponent(sgl_value)		\
    (Sgl_exponent(sgl_value)==SGL_INFINITY_EXPONENT)
#define Sgl_isnotinfinity_exponent(sgl_value)		\
    (Sgl_exponent(sgl_value)!=SGL_INFINITY_EXPONENT)
#define Sgl_isinfinity(sgl_value)			\
    (Sgl_exponent(sgl_value)==SGL_INFINITY_EXPONENT &&	\
    Sgl_mantissa(sgl_value)==0)
#define Sgl_isnan(sgl_value)				\
    (Sgl_exponent(sgl_value)==SGL_INFINITY_EXPONENT &&	\
    Sgl_mantissa(sgl_value)!=0)
#define Sgl_isnotnan(sgl_value)				\
    (Sgl_exponent(sgl_value)!=SGL_INFINITY_EXPONENT ||	\
    Sgl_mantissa(sgl_value)==0)
#define Sgl_islessthan(sgl_op1,sgl_op2)			\
    (Sall(sgl_op1) < Sall(sgl_op2))
#define Sgl_isgreaterthan(sgl_op1,sgl_op2)		\
    (Sall(sgl_op1) > Sall(sgl_op2))
#define Sgl_isnotlessthan(sgl_op1,sgl_op2)		\
    (Sall(sgl_op1) >= Sall(sgl_op2))
#define Sgl_isequal(sgl_op1,sgl_op2)			\
    (Sall(sgl_op1) == Sall(sgl_op2))

#define Sgl_leftshiftby8(sgl_value) \
    Sall(sgl_value) <<= 8
#define Sgl_leftshiftby4(sgl_value) \
    Sall(sgl_value) <<= 4
#define Sgl_leftshiftby3(sgl_value) \
    Sall(sgl_value) <<= 3
#define Sgl_leftshiftby2(sgl_value) \
    Sall(sgl_value) <<= 2
#define Sgl_leftshiftby1(sgl_value) \
    Sall(sgl_value) <<= 1
#define Sgl_rightshiftby1(sgl_value) \
    Sall(sgl_value) >>= 1
#define Sgl_rightshiftby4(sgl_value) \
    Sall(sgl_value) >>= 4
#define Sgl_rightshiftby8(sgl_value) \
    Sall(sgl_value) >>= 8

#define Sgl_ismagnitudeless(signlessleft,signlessright)			\
/*  unsigned int signlessleft, signlessright; */			\
      (signlessleft < signlessright)

#define Sgl_copytoint_exponentmantissa(source,dest)     \
    dest = Sexponentmantissa(source)

/* A quiet NaN has the high mantissa bit clear and at least on other (in this
 * case the adjacent bit) bit set. */
#define Sgl_set_quiet(sgl_value) Deposit_shigh2mantissa(sgl_value,1)
#define Sgl_set_exponent(sgl_value,exp) Deposit_sexponent(sgl_value,exp)

#define Sgl_set_mantissa(dest,value) Deposit_smantissa(dest,value)
#define Sgl_set_exponentmantissa(dest,value) \
    Deposit_sexponentmantissa(dest,value)

/*  An infinity is represented with the max exponent and a zero mantissa */
#define Sgl_setinfinity_exponent(sgl_value) \
    Deposit_sexponent(sgl_value,SGL_INFINITY_EXPONENT)
#define Sgl_setinfinity_exponentmantissa(sgl_value)	\
    Deposit_sexponentmantissa(sgl_value, \
	(SGL_INFINITY_EXPONENT << (32-(1+SGL_EXP_LENGTH))))
#define Sgl_setinfinitypositive(sgl_value)		\
    Sall(sgl_value) = (SGL_INFINITY_EXPONENT << (32-(1+SGL_EXP_LENGTH)))
#define Sgl_setinfinitynegative(sgl_value)		\
    Sall(sgl_value) = (SGL_INFINITY_EXPONENT << (32-(1+SGL_EXP_LENGTH))) \
    | (1<<31)
#define Sgl_setinfinity(sgl_value,sign)					\
    Sall(sgl_value) = (SGL_INFINITY_EXPONENT << (32-(1+SGL_EXP_LENGTH))) | \
     (sign << 31)
#define Sgl_sethigh4bits(sgl_value, extsign)  \
    Deposit_shigh4(sgl_value,extsign)
#define Sgl_set_sign(sgl_value,sign) Deposit_ssign(sgl_value,sign)
#define Sgl_invert_sign(sgl_value)  \
    Deposit_ssign(sgl_value,~Ssign(sgl_value))
#define Sgl_setone_sign(sgl_value) Deposit_ssign(sgl_value,1)
#define Sgl_setone_lowmantissa(sgl_value) Deposit_slow(sgl_value,1)
#define Sgl_setzero_sign(sgl_value)  Sall(sgl_value) &= 0x7fffffff
#define Sgl_setzero_exponent(sgl_value) Sall(sgl_value) &= 0x807fffff
#define Sgl_setzero_mantissa(sgl_value) Sall(sgl_value) &= 0xff800000
#define Sgl_setzero_exponentmantissa(sgl_value)  Sall(sgl_value) &= 0x80000000
#define Sgl_setzero(sgl_value) Sall(sgl_value) = 0
#define Sgl_setnegativezero(sgl_value) Sall(sgl_value) = 1U << 31

/* Use following macro for both overflow & underflow conditions */
#define ovfl -
#define unfl +
#define Sgl_setwrapped_exponent(sgl_value,exponent,op) \
    Deposit_sexponent(sgl_value,(exponent op SGL_WRAP))

#define Sgl_setlargestpositive(sgl_value)				\
    Sall(sgl_value) = ((SGL_EMAX+SGL_BIAS) << (32-(1+SGL_EXP_LENGTH)))	\
			| ((1<<(32-(1+SGL_EXP_LENGTH))) - 1)
#define Sgl_setlargestnegative(sgl_value)				\
    Sall(sgl_value) = ((SGL_EMAX+SGL_BIAS) << (32-(1+SGL_EXP_LENGTH)))	\
			| ((1<<(32-(1+SGL_EXP_LENGTH))) - 1 ) | (1<<31)

#define Sgl_setnegativeinfinity(sgl_value)	\
    Sall(sgl_value) =				\
    ((1<<SGL_EXP_LENGTH) | SGL_INFINITY_EXPONENT) << (32-(1+SGL_EXP_LENGTH))
#define Sgl_setlargest(sgl_value,sign)					\
    Sall(sgl_value) = ((sign) << 31) |					\
	(((SGL_EMAX+SGL_BIAS) << (32-(1+SGL_EXP_LENGTH)))		\
	  | ((1 << (32-(1+SGL_EXP_LENGTH))) - 1 ))
#define Sgl_setlargest_exponentmantissa(sgl_value)			\
    Sall(sgl_value) = (Sall(sgl_value) & (1<<31)) |			\
	(((SGL_EMAX+SGL_BIAS) << (32-(1+SGL_EXP_LENGTH)))		\
	  | ((1 << (32-(1+SGL_EXP_LENGTH))) - 1 ))

/* The high bit is always zero so arithmetic or logical shifts will work. */
#define Sgl_right_align(srcdst,shift,extent)				\
    /* sgl_floating_point srcdst; int shift; extension extent */	\
    if (shift < 32) {							\
	Extall(extent) = Sall(srcdst) << (32-(shift));			\
	Sall(srcdst) >>= shift;						\
    }									\
    else {								\
	Extall(extent) = Sall(srcdst);					\
	Sall(srcdst) = 0;						\
    }
#define Sgl_hiddenhigh3mantissa(sgl_value) Shiddenhigh3mantissa(sgl_value)
#define Sgl_hidden(sgl_value) Shidden(sgl_value)
#define Sgl_lowmantissa(sgl_value) Slow(sgl_value)

/* The left argument is never smaller than the right argument */
#define Sgl_subtract(sgl_left,sgl_right,sgl_result) \
    Sall(sgl_result) = Sall(sgl_left) - Sall(sgl_right)

/* Subtract right augmented with extension from left augmented with zeros and
 * store into result and extension. */
#define Sgl_subtract_withextension(left,right,extent,result)		\
    /* sgl_floating_point left,right,result; extension extent */	\
  Sgl_subtract(left,right,result);					\
  if((Extall(extent) = 0-Extall(extent)))				\
      Sall(result) = Sall(result)-1

#define Sgl_addition(sgl_left,sgl_right,sgl_result) \
    Sall(sgl_result) = Sall(sgl_left) + Sall(sgl_right)

#define Sgl_xortointp1(left,right,result)			\
    result = Sall(left) XOR Sall(right);

#define Sgl_xorfromintp1(left,right,result)			\
    Sall(result) = left XOR Sall(right)

/* Need to Initialize */
#define Sgl_makequietnan(dest)						\
    Sall(dest) = ((SGL_EMAX+SGL_BIAS)+1)<< (32-(1+SGL_EXP_LENGTH))	\
		| (1<<(32-(1+SGL_EXP_LENGTH+2)))
#define Sgl_makesignalingnan(dest)					\
    Sall(dest) = ((SGL_EMAX+SGL_BIAS)+1)<< (32-(1+SGL_EXP_LENGTH))	\
		| (1<<(32-(1+SGL_EXP_LENGTH+1)))

#define Sgl_normalize(sgl_opnd,exponent)			\
	while(Sgl_iszero_hiddenhigh7mantissa(sgl_opnd)) {	\
		Sgl_leftshiftby8(sgl_opnd);			\
		exponent -= 8;					\
	}							\
	if(Sgl_iszero_hiddenhigh3mantissa(sgl_opnd)) {		\
		Sgl_leftshiftby4(sgl_opnd);			\
		exponent -= 4;					\
	}							\
	while(Sgl_iszero_hidden(sgl_opnd)) {			\
		Sgl_leftshiftby1(sgl_opnd);			\
		exponent -= 1;					\
	}

#define Sgl_setoverflow(sgl_opnd)				\
	/* set result to infinity or largest number */		\
	switch (Rounding_mode()) {				\
		case ROUNDPLUS:					\
			if (Sgl_isone_sign(sgl_opnd)) {		\
				Sgl_setlargestnegative(sgl_opnd); \
			}					\
			else {					\
				Sgl_setinfinitypositive(sgl_opnd); \
			}					\
			break;					\
		case ROUNDMINUS:				\
			if (Sgl_iszero_sign(sgl_opnd)) {	\
				Sgl_setlargestpositive(sgl_opnd); \
			}					\
			else {					\
				Sgl_setinfinitynegative(sgl_opnd); \
			}					\
			break;					\
		case ROUNDNEAREST:				\
			Sgl_setinfinity_exponentmantissa(sgl_opnd); \
			break;					\
		case ROUNDZERO:					\
			Sgl_setlargest_exponentmantissa(sgl_opnd); \
	}

#define Sgl_denormalize(opnd,exponent,guard,sticky,inexact)		\
	Sgl_clear_signexponent_set_hidden(opnd);			\
	if (exponent >= (1 - SGL_P)) {					\
		guard = (Sall(opnd) >> (-(exponent))) & 1;		\
		if (exponent < 0) sticky |= Sall(opnd) << (32+exponent); \
		inexact = (guard) | (sticky);				\
		Sall(opnd) >>= (1-exponent);				\
	}								\
	else {								\
		guard = 0;						\
		sticky |= Sall(opnd);					\
		inexact = sticky;					\
		Sgl_setzero(opnd);					\
	}

int sgl_fadd(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
int sgl_fcmp(sgl_floating_point *, sgl_floating_point *, unsigned int, unsigned int *);
int sgl_fdiv(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
int sgl_fmpy(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
int sgl_frem(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
int sgl_fsqrt(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
int sgl_fsub(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);
int sgl_frnd(sgl_floating_point *, sgl_floating_point *, sgl_floating_point *, unsigned int *);


code:
/Makefile/1.4/Sun Nov 25 20:37:24 2007//TOPENBSD_5_8_BASE
/Makefile.inc/1.19/Fri Aug 31 16:13:20 2012//TOPENBSD_5_8_BASE
D/boot////
D/cdboot////
D/libsa////
D/libz////
D/mkboot////

path: sys/arch/hppa/stand/CVS/Entries

license: unknown


code: src/sys/arch/hppa/stand

path: sys/arch/hppa/stand/CVS/Repository


code:
#	$OpenBSD: Makefile,v 1.4 2007/11/25 20:37:24 maja Exp $

.if ${MACHINE} == "hppa"
SUBDIR= libsa libz
.endif
SUBDIR+= mkboot boot
.if ${MACHINE} == "hppa"
SUBDIR+= cdboot
.endif

.include <bsd.subdir.mk>

path: sys/arch/hppa/stand/Makefile


code:
#	$OpenBSD: Makefile.inc,v 1.19 2012/08/31 16:13:20 pascal Exp $

CFLAGS=${DEBUG} -Os -Wall -Werror
CPPFLAGS+=-I${S} -I. -I${.CURDIR}
SACFLAGS=-nostdinc -fno-builtin -D_STANDALONE -I${STANDIR}/libsa
SACFLAGS+=-mdisable-fpregs -mfast-indirect-calls -mpa-risc-1-1
SACFLAGS+=-fno-stack-protector
#DEBUGFLAGS=-DDEBUG
#DEBUGFLAGS+=-DPDCDEBUG
#DEBUGFLAGS+=-DLIFDEBUG
#DEBUGFLAGS+=-DEXEC_DEBUG
#DEBUGFLAGS+=-DALLOC_TRACE
LINKADDR=0xf80000
LOADADDR=0xf80000
HEAP_LIMIT=0xff8000
CLEANFILES+=	machine
DEBUGLIBS=	no

.if !make(libdep) && !make(sadep) && !make(salibdir) && !make(obj) && !(defined(PROG) && ${PROG} == "mkboot")
.BEGIN:
	@([ -h machine ] || ln -s $(S)/arch/hppa/include machine)
.endif

.if exists(${STANDIR}/libsa/libsa.a)
LIBSA=${STANDIR}/libsa/libsa.a
.else
LIBSA=${STANDIR}/libsa/${__objdir}/libsa.a
.endif
.if exists(${STANDIR}/libz/libz.a)
LIBZ=${STANDIR}/libz/libz.a
.else
LIBZ=${STANDIR}/libz/${__objdir}/libz.a
.endif
.if exists(${STANDIR}/mkboot/mkboot)
MKBOOT=${STANDIR}/mkboot/mkboot
.else
MKBOOT=${STANDIR}/mkboot/${__objdir}/mkboot
.endif

BINDIR=	/usr/mdec


path: sys/arch/hppa/stand/boot/CVS/Entries


code: src/sys/arch/hppa/stand/boot

path: sys/arch/hppa/stand/boot/CVS/Repository


code:
#	$OpenBSD: Makefile,v 1.24 2013/08/16 08:16:45 otto Exp $

MAN=	boot.8
MANSUBDIR=hppa
MLINKS=	boot.8 boot.conf.5
S	=${.CURDIR}/../../../..
CLEANFILES+=	boot.gdb boot.map boot.lif

.if ${MACHINE} == "hppa"
PROG=	boot
SRCS=	srt0.S exec.c conf.c

.PATH:	${S}/stand/boot
SRCS+=	bootarg.c boot.c vars.c cmd.c

.PATH:	${S}/lib/libkern/arch/hppa ${S}/lib/libkern
SRCS+=	milli.S ashrdi3.c memcmp.c memcpy.c memset.c moddi3.c muldi3.c \
	qdivrem.c strcmp.c strlcpy.c strlen.c strncmp.c strncpy.c divdi3.c

LD?=	ld
LDFLAGS+=-Bstatic -nostartfiles -nostdlib -N -Ttext $(LINKADDR) -nopie
LDFLAGS+=-T ${.CURDIR}/ld.script -Map boot.map
SIZE?=	size
STRIP?=	strip
STANDIR=${.CURDIR}/..
CRTBEGIN=
CRTEND=
LINKS=	${BINDIR}/boot.lif ${BINDIR}/sdboot \
	${BINDIR}/boot.lif ${BINDIR}/stboot \
	${BINDIR}/boot.lif ${BINDIR}/xxboot

LDADD=	${LIBSA} ${LIBZ}
DPADD=	${LIBSA} ${LIBZ}

all:	boot.lif

realinstall:
	${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
	    boot ${DESTDIR}${BINDIR}
	${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
	    boot.lif ${DESTDIR}${BINDIR}

# probably we should check for 256k limit for ISL
boot.lif:	${PROG} ${ADDBOOT}
	-@cp ${PROG} ${PROG}.gdb
	${STRIP} ${PROG}
	${MKBOOT} -v ${PROG} ${ADDBOOT} boot.lif

${PROG}: $(OBJS) $(DPADD)
	$(LD) $(LDFLAGS) -o $(PROG) $(OBJS) $(LDADD)
	@${SIZE} $(PROG)
.else
NOPROG=
.endif

.include <bsd.prog.mk>

CPPFLAGS+=${DEBUGFLAGS} -DRELOC=${LOADADDR} -DHEAP_LIMIT=${HEAP_LIMIT}
CFLAGS+=$(SACFLAGS)
CFLAGS+=-fno-pie
AFLAGS+=-fno-pie

path: sys/arch/hppa/stand/boot/Makefile


code:
/*	$OpenBSD: conf.c,v 1.27 2013/12/28 02:53:03 deraadt Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/types.h>
#include <libsa.h>
#include <lib/libsa/ufs.h>
#include <lib/libsa/cd9660.h>
#include <dev/cons.h>

const char version[] = "1.3";
int	debug = 0;

struct fs_ops file_system[] = {
	{ ufs_open,    ufs_close,    ufs_read,    ufs_write,    ufs_seek,
	  ufs_stat,    ufs_readdir    },
	{ cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
	  cd9660_stat, cd9660_readdir },
	{ lif_open,    lif_close,    lif_read,    lif_write,    lif_seek,
	  lif_stat,    lif_readdir    },
};
int nfsys = nitems(file_system);

struct devsw devsw[] = {
	{ "dk",	iodcstrategy, dkopen, dkclose, noioctl },
	{ "ct",	iodcstrategy, ctopen, ctclose, noioctl },
	{ "lf", iodcstrategy, lfopen, lfclose, noioctl }
};
int	ndevs = nitems(devsw);

struct consdev	constab[] = {
	{ ite_probe, ite_init, ite_getc, ite_putc },
	{ NULL }
};
struct consdev *cn_tab;


code:
/*	$OpenBSD: exec.c,v 1.5 2012/06/26 16:18:43 deraadt Exp $	*/

/*
 * Copyright (c) 2002-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <machine/cpu.h>
#include <machine/pdc.h>
#include "libsa.h"
#include <lib/libsa/loadfile.h>
#include <stand/boot/bootarg.h>
#include "dev_hppa.h"

typedef void (*startfuncp)(int, int, int, int, int, int, caddr_t)
    __attribute__ ((noreturn));

void
run_loadfile(u_long *marks, int howto)
{
	fcacheall();

	__asm("mtctl %r0, %cr17");
	__asm("mtctl %r0, %cr17");
	/* stack and the gung is ok at this point, so, no need for asm setup */
	(*(startfuncp)(marks[MARK_ENTRY]))((int)pdc, howto, bootdev,
	    marks[MARK_END], BOOTARG_APIVER, BOOTARG_LEN, (caddr_t)BOOTARG_OFF);

	/* not reached */
}


code:
/*	$OpenBSD: ld.script,v 1.4 2010/05/09 13:27:30 kettenis Exp $	*/

OUTPUT_FORMAT("elf32-hppa")
OUTPUT_ARCH(hppa)
ENTRY(begin)

SECTIONS {

	/* Read-only sections, merged into text segment: */
	. = + SIZEOF_HEADERS;
	.text : {
		*(.text)
		*(.text.*)
		*(.rodata)
		*(.rodata1)
		*(.rodata.*)
		*($CODE$)
		etext = .;
	} = 0x08000240

	/* Read-write sections, merged into data segment: */
	.data : {
		$global$ = .;
		*(.data)
		*(.data1)
		*(.sdata)
		*(.sdata2)
		*(.dynamic)
		CONSTRUCTORS
		edata = ABSOLUTE(.);
	} = 0

	.bss : {
		__bss_start = .;
		*(.dynbss)
		*(.bss)
		*(.sbss)
		*(.scommon)
		*(COMMON)
		end = ABSOLUTE(.);
		__bss_end = .;
	}

	/* don't need an unwind in the boot, unless we'll have a boot-ddb (; */
	/DISCARD/ :
	{
		*(.PARISC.unwind)
	}
}


code:
/*	$OpenBSD: srt0.S,v 1.11 2011/04/17 09:49:48 kettenis Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Copyright 1996 1995 by Open Software Foundation, Inc.
 *              All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */
;
; Copyright (c) 1990 mt Xinu, Inc.  All rights reserved.
; Copyright (c) 1990 University of Utah.  All rights reserved.
;
; This file may be freely distributed in any form as long as
; this copyright notice is included.
; THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
; IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
; WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
;
;	Utah $Hdr: srt0.c 1.3 94/12/13$
;

#define	_LOCORE
#include <machine/asm.h>
#include <machine/cpu.h>
#include <machine/iomod.h>

/*
 * This is the ending of the begin
 */
ENTRY(begin,0)

	blr	%r0,%r5			; Get address of 'boff' into 'r5',
	ldo	begin-boff(%r5),%r5	;   and subtract to get 'begin'.
boff
	ldil	L%RELOC,%r4
	ldo	R%RELOC(%r4),%r4
	ldo	start-begin(%r4),%rp
	ldil	L%edata,%r3
	ldo	R%edata(%r3),%r3	; Get address of edata.
	ldil	L%begin,%r1
	ldo	R%begin(%r1),%r1	; Get address of begin
	sub	%r3,%r1,%r3		; Subtract to get # of bytes to copy
copyloop				; do
	ldwm	4(%r5),%r1		;   *r4++ = *r5++;
	addib,>= -4,%r3,copyloop	; while (--r3 >= 0);
	stwm	%r1,4(%r4)

	; here we zero the .bss
	ldil	L%__bss_start, %r4
	ldo	R%__bss_start(%r4), %r4
	ldil	L%__bss_end, %r3
	ldo	R%__bss_end(%r3), %r3
zeroloop
	combf,<,n %r3,%r4, zeroloop	; while (r4 < r3);
	stwm	%r0,4(%r4)		;	*r4++ = 0;

	ldil	L%$global$,%dp
	ldo	R%$global$(%dp),%dp
	ldil	L%start,%r1
	ldo	R%start(%r1),%r1
	sub	%dp,%r1,%dp		; Subtract to get difference
	add	%rp,%dp,%dp		;   and relocate it.

;
; We have relocated ourself to RELOC.  If we are running on a machine
; with separate instruction and data caches, we must flush our data
; cache before trying to execute the code starting at rp.
;
	ldil	L%RELOC,%r22		; Set %t1 to start of relocated code.
	ldo	R%RELOC(%r22),%r22
	ldil	L%edata,%r21		; Set r21 to address of edata
	ldo	R%edata(%r21),%r21
	ldil	L%begin,%r1		; set %r1 to address of begin
	ldo	R%begin(%r1),%r1
	sub	%r21,%r1,%r21		; Subtract to get length
	mtsp	%r0,%sr0		; Set sr0 to kernel space.
	ldo	-1(%r21),%r21
	fdc	%r21(0,%r22)
loop	addib,>,n -16,%r21,loop		; Decrement by cache line size (16).
	fdc	%r21(%sr0,%r22)
	fdc	0(%sr0,%r22)		; Flush first word at addr to handle
	sync				;   arbitrary cache line boundary.
	nop				; Prevent prefetching.
	nop
	nop
	nop
	nop
	nop
	nop
	bv	0(rp)
	nop
EXIT(begin)				/* jump to relocated code */

start
	ldil	L%HEAP_LIMIT, %sp
	ldo	R%HEAP_LIMIT(%sp), %sp

	comb,=	%r0, %r26, call_boot
	nop
	.import	boottimeout, data
	ldil	L%boottimeout, %r22
	stw	%r0, R%boottimeout(%r22)

call_boot
	b	boot			; Call boot(),
	copy	%r0, arg0		; use default boot device
	nop

/*
 * rtt - restart the box
 */
LEAF_ENTRY(_rtt)
	ldil    L%HPPA_LBCAST, %r25
	ldi     CMD_RESET, %r26
	stw     %r26,R%iomod_command(%r25)
forever                                 ; Loop until bus reset takes effect.
	b,n     forever

	bv	0(rp)
	ldo     -48(sp),sp
EXIT(_rtt)

	.end


path: sys/arch/hppa/stand/cdboot/CVS/Entries


path: sys/arch/hppa/stand/cdboot/CVS/Tag


code:
#	$OpenBSD: Makefile,v 1.9 2014/07/13 21:18:26 miod Exp $

NOMAN=	no man
S	=${.CURDIR}/../../../..
CLEANFILES+=	cdboot.gdb cdboot.map

.if ${MACHINE} == "hppa"

PROG=	cdboot

.PATH:	${.CURDIR}/../boot
SRCS+=	srt0.S

SRCS=	srt0.S cdboot.c

.PATH:	${.CURDIR}/../libsa
SRCS+=	pdc.c itecons.c dev_hppa.c dk.c elf32.c elf64.c
CPPFLAGS+=-DCONSPEED=9600

.PATH:	${S}/lib/libkern/arch/hppa ${S}/lib/libkern
SRCS+=	milli.S strlen.c strlcpy.c

LD?=	ld
LDFLAGS+=-Bstatic -nostartfiles -nostdlib -N -Ttext $(LINKADDR) -nopie
LDFLAGS+=-T ${.CURDIR}/ld.script -Map cdboot.map
SIZE?=	size
STRIP?=	strip
STANDIR=${.CURDIR}/..
CRTBEGIN=
CRTEND=

SAREL=
USE_LOADFILE=yes
.include "${S}/lib/libsa/Makefile.inc"
DPADD+= $(SALIB)
LDADD+=	$(SALIB)

all:	${PROG}

clean::
	rm -f a.out [Ee]rrs mklog *.core \
	    ${PROG} ${OBJS} ${LOBJS} ${CLEANFILES}

realinstall:
	${INSTALL} ${INSTALL_COPY} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \
	    ${PROG} ${DESTDIR}${BINDIR}

# TODO check for <=32k
${PROG}: $(OBJS) $(DPADD)
	$(LD) $(LDFLAGS) -o $(PROG) $(OBJS) $(LDADD)
	@cp ${PROG} ${PROG}.gdb
	${STRIP} ${PROG}
	@${SIZE} $(PROG)
	@${MKBOOT} -v ${PROG} ${PROG}.lif
	@mv ${PROG}.lif ${PROG}
.else
NOPROG=
.endif

.include <bsd.prog.mk>

CPPFLAGS+=-DRELOC=${LOADADDR} -DHEAP_LIMIT=${HEAP_LIMIT}
CFLAGS+=$(SACFLAGS) -I../.. -I${.CURDIR}/../libsa -I${S}/stand/boot
CFLAGS+=-fno-pie
AFLAGS+=-fno-pie

path: sys/arch/hppa/stand/cdboot/Makefile


code:
/*	$OpenBSD: cdboot.c,v 1.13 2013/12/28 02:51:07 deraadt Exp $	*/

/*
 * Copyright (c) 2003 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <libsa.h>
#include <lib/libsa/cd9660.h>
#include <lib/libsa/loadfile.h>
#include <dev/cons.h>
#include <machine/pdc.h>
#include <machine/cpu.h>
#include <stand/boot/bootarg.h>
#include "dev_hppa.h"
#include "cmd.h"

dev_t bootdev;
int debug = 1;
int boottimeout = 5;

struct fs_ops file_system[] = {
	{ cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek,
	  cd9660_stat, cd9660_readdir },
};
int nfsys = nitems(file_system);

struct devsw devsw[] = {
	{ "dk",	iodcstrategy, dkopen, dkclose, noioctl },
};
int	ndevs = nitems(devsw);

struct consdev	constab[] = {
	{ ite_probe, ite_init, ite_getc, ite_putc },
	{ NULL }
};
struct consdev *cn_tab;

typedef void (*startfuncp)(int, int, int, int, int, int, caddr_t)
    __attribute__ ((noreturn));

char   rnddata[BOOTRANDOM_MAX];		/* XXX dummy */

void
boot(dev_t dev)
{
	u_long marks[MARK_MAX];
	char path[128];

	pdc_init();
	cninit();
	devboot(dev, path);
	strncpy(path + strlen(path), ":/bsd.rd", 9);
	printf(">> OpenBSD/" MACHINE " CDBOOT 0.2\n"
	    "booting %s: ", path);

	marks[MARK_START] = (u_long)DEFAULT_KERNEL_ADDRESS;
	if (!loadfile(path, marks, LOAD_KERNEL)) {
		marks[MARK_END] = ALIGN(marks[MARK_END] -
		    (u_long)DEFAULT_KERNEL_ADDRESS);
		fcacheall();

		__asm("mtctl %r0, %cr17");
		__asm("mtctl %r0, %cr17");
		(*(startfuncp)(marks[MARK_ENTRY]))((int)pdc, 0, bootdev,
		    marks[MARK_END], BOOTARG_APIVER, BOOTARG_LEN,
		    (caddr_t)BOOTARG_OFF);
		/* not reached */
	}
}


code:
/*	$OpenBSD: elf64.c,v 1.1 2010/08/16 13:04:52 kettenis Exp $	*/

/*
 * Copyright (c) 2010 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <lib/libkern/libkern.h>
#include <lib/libsa/stand.h>

#include <sys/param.h>
#include <sys/exec.h>

#include "../../../../lib/libsa/loadfile.h"

#undef ELFSIZE
#define ELFSIZE  64

#include <sys/exec_elf.h>

int
ELFNAME(exec)(int fd, Elf_Ehdr *elf, u_long *marks, int flags)
{
	return 1;
}


code:
/*	$OpenBSD: ld.script,v 1.2 2010/05/09 13:27:30 kettenis Exp $	*/

OUTPUT_FORMAT("elf32-hppa")
OUTPUT_ARCH(hppa)
ENTRY(begin)

SECTIONS {

	/* Read-only sections, merged into text segment: */
	. = + SIZEOF_HEADERS;
	.text : {
		*(.text)
		*(.text.*)
		*(.rodata)
		*(.rodata1)
		*(.rodata.*)
		*($CODE$)
		etext = .;
	} = 0x08000240

	/* Read-write sections, merged into data segment: */
	.data : {
		$global$ = .;
		*(.data)
		*(.data1)
		*(.sdata)
		*(.sdata2)
		*(.dynamic)
		CONSTRUCTORS
		edata = ABSOLUTE(.);
	} = 0

	.bss : {
		__bss_start = .;
		*(.dynbss)
		*(.bss)
		*(.sbss)
		*(.scommon)
		*(COMMON)
		end = ABSOLUTE(.);
		__bss_end = .;
	}

	/* don't need an unwind in the boot, unless we'll have a boot-ddb (; */
	/DISCARD/ :
	{
		*(.PARISC.unwind)
	}
}


path: sys/arch/hppa/stand/libsa/CVS/Entries


path: sys/arch/hppa/stand/libsa/CVS/Tag


code:
#	$OpenBSD: Makefile,v 1.19 2014/07/13 09:26:08 jasper Exp $

.include "${.CURDIR}/../Makefile.inc"

LIB=	sa

S=${.CURDIR}/../../../..
SADIR=${.CURDIR}/..
DIR_SA=	$S/lib/libsa

NOPROFILE=noprofile
NOPIC=nopic

#AS=cat ;
#AS+=	-R
#AS+=	-a

# hppa stuff
SRCS=	machdep.c pdc.c itecons.c dev_hppa.c time.c \
	ct.c dk.c lf.c lif.c cmd_hppa.c loadfile.c elf32.c elf64.c

# stand routines
SRCS+=	alloc.c exit.c getfile.c gets.c getchar.c globals.c \
	printf.c putchar.c strerror.c strtol.c strchr.c ctime.c snprintf.c

# io routines
SRCS+=	close.c closeall.c dev.c disklabel.c dkcksum.c fstat.c ioctl.c lseek.c \
	open.c read.c stat.c write.c cread.c readdir.c cons.c

# boot filesystems
SRCS+=	ufs.c cd9660.c

.PATH:  ${DIR_SA}

all: ${SALIB}

install:

.include <bsd.lib.mk>

CPPFLAGS+=-DHEAP_LIMIT=${HEAP_LIMIT} ${DEBUGFLAGS} -DNO_NET
CPPFLAGS+=-I${S}/stand/boot
CFLAGS+=${SACFLAGS} -D__INTERNAL_LIBSA_CREAD -DCOMPAT_UFS -fno-pie
CFLAGS+=-DCONSPEED=9600

path: sys/arch/hppa/stand/libsa/Makefile


code:
/*	$OpenBSD: cmd_hppa.c,v 1.12 2010/12/06 22:51:45 jasper Exp $	*/

/*
 * Copyright (c) 2002, 2009 Miodrag Vallat
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
/* would come from <sys/param.h> if -D_KERNEL */
#define offsetof(s, e) ((size_t)&((s *)0)->e)

#include <machine/iomod.h>
#include <machine/pdc.h>

#include <arch/hppa/dev/cpudevs.h>
#include <arch/hppa/dev/elroyreg.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>

#include <libsa.h>
#include "cmd.h"
#include "dev_hppa.h"	/* pdc */

extern struct stable_storage sstor;
extern int sstorsiz;

/* storage sizes we're interested in */
#define	CONSOLEOFFSET \
	offsetof(struct stable_storage, ss_console)
#define	CONSOLESIZE \
	(offsetof(struct stable_storage, ss_console) + \
	 sizeof(struct device_path))

#define	KEYBOARDOFFSET \
	offsetof(struct stable_storage, ss_keyboard)
#define	KEYBOARDSIZE \
	(offsetof(struct stable_storage, ss_keyboard) + \
	 sizeof(struct device_path))

/*
 * Table for the possible console devices found during the device walk.
 */
struct consoledev {
	struct device_path dp;
	int	type;
	int	iodc_type;
	int	iodc_model;
};

#define	PS2		1
#define	HIL		2
#define	USB		3
#define	SERIAL		4
#define	GRAPHICS	5

#define	MAX_SERIALS	4
#define	MAX_KEYBOARDS	4
#define	MAX_GRAPHICS	4

struct consoledev serials[MAX_SERIALS];
struct consoledev keyboards[MAX_KEYBOARDS];
struct consoledev graphics[MAX_GRAPHICS];

int walked;

void	bus_walk(struct device_path *, int);
uint32_t dino_conf_read(u_int, int, int, u_int);
uint32_t elroy_conf_read(u_int, int, int, u_int);
int	path_match(struct device_path *, struct device_path *);
void	path_shift(struct device_path *, int);
void	pci_bus_walk(struct device_path *, struct iodc_data *,
	    struct pdc_memmap *);
void	register_device(struct consoledev *, int, struct device_path *,
	    struct iodc_data *, int, int);

int	Xconsole(void);
void	print_console(void);
int	set_graphics(struct device_path *, int, char *);
int	set_serial(struct device_path *, int, char *);
int	set_console(struct device_path *);

int	Xkeyboard(void);
void	print_keyboard(void);
int	set_keyboard(struct device_path *);

struct cmd_table cmd_machine[] = {
	{ "console",	CMDT_CMD,	Xconsole },
	{ "keyboard",	CMDT_CMD,	Xkeyboard },
	{ NULL, },
};

/* value to console speed table */
const int i_speeds[] = {
	50,
	75,
	110,
	150,
	300,
	600,
	1200,
	2400,
	4800,
	7200,
	9600,
	19200,
	38400,
	57600,
	115200,
	230400,
};

const char *c_speeds[] = {
	"50",
	"75",
	"110",
	"150",
	"300",
	"600",
	"1200",
	"2400",
	"4800",
	"7200",
	"9600",
	"19200",
	"38400",
	"57600",
	"115200",
	"230400",
};

/* values to console parity table */
const char *parities[] = {
	"none",
	"odd",
	"<unknown parity>",
	"even",
};

/*
 * C O N S O L E   S E T T I N G S
 */

void
print_console()
{
	int port, mode, speed, parity, bits;
	int i;

#ifdef DEBUG
	printf("console flags %x mod %x bc %d/%d/%d/%d/%d/%d\n",
	    sstor.ss_console.dp_flags,
	    sstor.ss_console.dp_mod,
	    sstor.ss_console.dp_bc[0],
	    sstor.ss_console.dp_bc[1],
	    sstor.ss_console.dp_bc[2],
	    sstor.ss_console.dp_bc[3],
	    sstor.ss_console.dp_bc[4],
	    sstor.ss_console.dp_bc[5]);

	printf("console path %x/%x/%x/%x/%x/%x\n",
	    sstor.ss_console.dp_layers[0],
	    sstor.ss_console.dp_layers[1],
	    sstor.ss_console.dp_layers[2],
	    sstor.ss_console.dp_layers[3],
	    sstor.ss_console.dp_layers[4],
	    sstor.ss_console.dp_layers[5]);
#endif

	printf("Console path: ");

	/* look for a serial console */
	for (port = i = 0; i < MAX_SERIALS; i++)
		if (path_match(&serials[i].dp, &sstor.ss_console)) {
			port = i + 1;
			break;
		}

	if (port == 0) {
		/*
		 * Graphics console
		 */

		for (port = i = 0; i < MAX_GRAPHICS; i++)
			if (path_match(&graphics[i].dp, &sstor.ss_console)) {
				port = i;
				break;
			}

		/*
		 * If the console could still not be identified, consider
		 * it is a simplified encoding for the default graphics
		 * console. Hence port == 0, no need to check.
		 */
		if (port == 0)
			printf("graphics");
		else
			printf("graphics_%d", port);

		mode = sstor.ss_console.dp_layers[0];
		if (mode != 0)
			printf(".%d", mode);
	} else {
		/*
		 * Serial console
		 */

		if (port == 1)
			printf("rs232");
		else
			printf("rs232_%d", port);

		speed = PZL_SPEED(sstor.ss_console.dp_layers[0]);
		printf(".%d", i_speeds[speed]);

		bits = PZL_BITS(sstor.ss_console.dp_layers[0]);
		printf(".%d", bits);

		parity = PZL_PARITY(sstor.ss_console.dp_layers[0]);
		printf(".%s", parities[parity]);
	}

	printf("\n");
}

int
set_graphics(console, port, arg)
	struct device_path *console;
	int port;
	char *arg;
{
	int maxmode, mode = 0;
	char *digit;

	/* head */
	if (graphics[port].type == 0) {
		printf("no such device found\n");
		return 0;
	}

	/* mode */
	if (arg != NULL) {
		for (digit = arg; *digit != '\0'; digit++) {
			if (*digit >= '0' && *digit <= '9')
				mode = 10 * mode + (*digit - '0');
			else {
				printf("invalid mode specification, %s\n", arg);
				return 0;
			}
		}

		if (mode <= 0) {
			printf("invalid mode specification, %s\n", arg);
			return 0;
		}
	}

	/*
	 * If we are just changing the mode of the same graphics
	 * console, check that our mode is in the valid range.
	 */
	if (path_match(&graphics[port].dp, &sstor.ss_console)) {
		maxmode = sstor.ss_console.dp_layers[1];

		/* pick back same mode if unspecified */
		if (mode == 0)
			mode = sstor.ss_console.dp_layers[0];

		if (mode > maxmode) {
			printf("invalid mode value, available range is 1-%d\n",
			    maxmode);
			return 0;
		}
	} else {
		if (mode == 0)
			mode = 1;
		maxmode = mode;
	}

	*console = graphics[port].dp;
	console->dp_layers[0] = mode;
	console->dp_layers[1] = maxmode;
	console->dp_layers[2] = console->dp_layers[3] =
	console->dp_layers[4] = console->dp_layers[5] = 0;

	return 1;
}

int
set_serial(console, port, arg)
	struct device_path *console;
	int port;
	char *arg;
{
	char *dot;
	int i;
	int speed, parity, bits;

	/* port */
	port--;
	if (serials[port].type == 0) {
		printf("no such device found\n");
		return 0;
	}

	/* speed */
	dot = strchr(arg, '.');
	if (dot != NULL)
		*dot++ = '\0';

	speed = 0;
	if (arg == NULL || *arg == '\0') {
		for (i = 0; i < nitems(i_speeds); i++)
			if (i_speeds[i] == 9600) {
				speed = i;
				break;
			}
	} else {
		for (i = 0; i < nitems(c_speeds); i++)
			if (strcmp(arg, c_speeds[i]) == 0) {
				speed = i;
				break;
			}
		if (speed == 0) {
			printf("invalid speed specification, %s\n", arg);
			return 0;
		}
	}

	/* data bits */
	arg = dot;
	dot = strchr(arg, '.');

	if (arg == NULL || *arg == '\0')
		bits = 8;
	else {
		if (dot == arg + 1)
			bits = *arg - '0';
		else
			bits = 0;

		if (bits < 5 || bits > 8) {
			printf("invalid bits specification, %s\n", arg);
			return 0;
		}
	}
	if (dot != NULL)
		*dot++ = '\0';

	/* parity */
	arg = dot;
	if (arg == NULL || *arg == '\0')
		parity = 0;	/* none */
	else {
		parity = -1;
		for (i = 0; i <= 3; i++)
			if (strcmp(arg, parities[i]) == 0) {
				parity = i;
				break;
			}
		if (parity == 2)
			parity = -1;	/* unknown parity */
	}
	if (parity < 0) {
		printf("invalid parity specification, %s\n", arg);
		return 0;
	}

	*console = serials[port].dp;
	console->dp_layers[0] = PZL_ENCODE(bits, parity, speed);

	return 1;
}

int
set_console(console)
	struct device_path *console;
{
	char *arg = cmd.argv[1], *dot;
	int port;

	/* extract first word */
	dot = strchr(arg, '.');
	if (dot != NULL)
		*dot++ = '\0';

	/*
	 * Graphics console
	 */
	if (strcmp(arg, "graphics") == 0)
		return set_graphics(console, 0, dot);
	if (strncmp(arg, "graphics_", 9) == 0) {
		port = arg[9] - '0';
		if (port > 0 && port < MAX_GRAPHICS)
			return set_graphics(console, port, dot);
	}

	/*
	 * Serial console
	 */
	if (strcmp(arg, "rs232") == 0)
		return set_serial(console, 1, dot);
	if (strncmp(arg, "rs232_", 6) == 0) {
		port = arg[6] - '0';
		if (port > 0 && port <= MAX_SERIALS)
			return set_serial(console, port, dot);
	}

	printf("invalid device specification, %s\n", arg);
	return 0;
}

int
Xconsole()
{
	struct device_path console;
	int rc;

	/* walk the device list if not already done */
	if (walked == 0) {
		bus_walk(NULL, MAXMODBUS);
		walked++;
	}

	if (sstorsiz < CONSOLESIZE) {
		printf("no console information in stable storage\n");
		return 0;
	}

	if (cmd.argc == 1) {
		print_console();
	} else {
		console = sstor.ss_console;
		if (set_console(&console)) {
			if (memcmp(&sstor.ss_console, &console,
			    sizeof console) != 0) {
				sstor.ss_console = console;

				/* alea jacta est */
				rc = (*pdc)(PDC_STABLE, PDC_STABLE_WRITE,
				    CONSOLEOFFSET, &sstor.ss_console,
				    sizeof(sstor.ss_console));
				if (rc != 0) {
					printf("failed to save console"
					    " settings, error %d\n", rc);
					/* read sstor again for safety */
					(*pdc)(PDC_STABLE, PDC_STABLE_READ,
					    CONSOLEOFFSET, &sstor.ss_console,
					    sizeof(sstor.ss_console));
				} else
					printf("you will need to power-cycle "
					       "your machine for the changes "
					       "to take effect.\n");
			}
			print_console();
		}
	}

	return 0;
}

/*
 * K E Y B O A R D   S E T T I N G S
 */

void
print_keyboard()
{
	int type;
	int i;

#ifdef DEBUG
	printf("keyboard flags %x mod %x bc %d/%d/%d/%d/%d/%d\n",
	    sstor.ss_keyboard.dp_flags,
	    sstor.ss_keyboard.dp_mod,
	    sstor.ss_keyboard.dp_bc[0],
	    sstor.ss_keyboard.dp_bc[1],
	    sstor.ss_keyboard.dp_bc[2],
	    sstor.ss_keyboard.dp_bc[3],
	    sstor.ss_keyboard.dp_bc[4],
	    sstor.ss_keyboard.dp_bc[5]);

	printf("keyboard path %x/%x/%x/%x/%x/%x\n",
	    sstor.ss_keyboard.dp_layers[0],
	    sstor.ss_keyboard.dp_layers[1],
	    sstor.ss_keyboard.dp_layers[2],
	    sstor.ss_keyboard.dp_layers[3],
	    sstor.ss_keyboard.dp_layers[4],
	    sstor.ss_keyboard.dp_layers[5]);
#endif

	printf("Keyboard path: ");

	for (type = i = 0; i < MAX_KEYBOARDS; i++)
		if (path_match(&keyboards[i].dp, &sstor.ss_keyboard)) {
			type = keyboards[i].type;
			break;
		}

	switch (type) {
	case USB:
		printf("usb");
		break;
	case HIL:
		printf("hil");
		break;
	case PS2:
		printf("ps2");
		break;
	default:
		printf("unknown");
		break;
	}

	printf("\n");
}

int
set_keyboard(keyboard)
	struct device_path *keyboard;
{
	int i;
	char *arg = cmd.argv[1];
	int type;

	if (strcmp(arg, "hil") == 0)
		type = HIL;
	else if (strcmp(arg, "ps2") == 0)
		type = PS2;
	else if (strcmp(arg, "usb") == 0)
		type = USB;
	else {
		/* XXX should probably handle multiple USB controllers */
		printf("invalid device specification, %s\n", arg);
		return 0;
	}

	for (i = 0; i < MAX_KEYBOARDS; i++)
		if (keyboards[i].type == type) {
			*keyboard = keyboards[i].dp;
			return 1;
		}

	printf("no such device found\n");
	return 0;
}

int
Xkeyboard()
{
	struct device_path keyboard;
	int rc;

	/* walk the device list if not already done */
	if (walked == 0) {
		bus_walk(NULL, MAXMODBUS);
		walked++;
	}

	if (sstorsiz < KEYBOARDSIZE) {
		printf("no keyboard information in stable storage\n");
		return 0;
	}

	if (cmd.argc == 1) {
		print_keyboard();
	} else {
		keyboard = sstor.ss_keyboard;
		if (set_keyboard(&keyboard)) {
			if (memcmp(&sstor.ss_keyboard, &keyboard,
			    sizeof keyboard) != 0) {
				sstor.ss_keyboard = keyboard;

				/* alea jacta est */
				rc = (*pdc)(PDC_STABLE, PDC_STABLE_WRITE,
				    KEYBOARDOFFSET, &sstor.ss_keyboard,
				    sizeof(sstor.ss_keyboard));
				if (rc != 0) {
					printf("failed to save keyboard"
					    " settings, error %d\n", rc);
					/* read sstor again for safety */
					(*pdc)(PDC_STABLE, PDC_STABLE_READ,
					    KEYBOARDOFFSET, &sstor.ss_keyboard,
					    sizeof(sstor.ss_keyboard));
				} else
					printf("you will need to power-cycle "
					       "your machine for the changes "
					       "to take effect.\n");
			}
			print_keyboard();
		}
	}

	return 0;
}

/*
 * U T I L I T I E S
 */

/*
 * Bus walker.
 * This routine will walk all the modules on a given bus, registering
 * serial ports, keyboard and graphics devices as they are found.
 */
void
bus_walk(struct device_path *idp, int maxmod)
{
	struct device_path dp;
	struct pdc_memmap memmap;
	struct iodc_data mptr;
	int err, i, kluge_ps2 = 0;	/* kluge, see below */

	for (i = 0; i < maxmod; i++) {
		if (idp) {
			dp = *idp;
			path_shift(&dp, i);
		} else {
			dp.dp_flags = 0;
			dp.dp_bc[0] = dp.dp_bc[1] = dp.dp_bc[2] =
			dp.dp_bc[3] = dp.dp_bc[4] = dp.dp_bc[5] = -1;
			dp.dp_mod = i;
			bzero(&dp.dp_layers, sizeof dp.dp_layers);
		}

		if ((pdc)(PDC_MEMMAP, PDC_MEMMAP_HPA, &memmap, &dp) < 0 &&
		    (pdc)(PDC_SYSMAP, PDC_SYSMAP_HPA, &memmap, &dp) < 0)
			continue;

		if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, &pdcbuf, memmap.hpa,
		    IODC_DATA, &mptr, sizeof(mptr))) < 0)
			continue;

#ifdef DEBUG
		printf("device %d/%d/%d/%d/%d/%d "
		    "flags %d mod %x type %x model %x\n",
		    dp.dp_bc[0], dp.dp_bc[1], dp.dp_bc[2], dp.dp_bc[3],
		    dp.dp_bc[4], dp.dp_bc[5], dp.dp_flags, dp.dp_mod,
		    mptr.iodc_type, mptr.iodc_sv_model);
#endif

		/*
		 * If the device can be considered as a valid rs232,
		 * graphics console or keyboard, register it.
		 *
		 * Unfortunately, devices which should be considered as
		 * ``main'' aren't necessarily seen first.
		 * The rules we try to enforce here are as follows:
		 * - GIO PS/2 ports wins over any other PS/2 port.
		 * - the first GIO serial found wins over any other
		 *   serial port.
		 * The second rule is a bit tricky to achieve, since on
		 * some machines (for example, 715/100XC), the two serial
		 * ports are not seen as attached to the same busses...
		 */
		switch (mptr.iodc_type) {
		case HPPA_TYPE_BCPORT:
			bus_walk(&dp, MAXMODBUS);
			break;
		case HPPA_TYPE_BRIDGE:
			if (mptr.iodc_sv_model == HPPA_BRIDGE_DINO) {
				pci_bus_walk(&dp, &mptr, &memmap);
				break;
			}
			/* FALLTHROUGH */
		case HPPA_TYPE_BHA:
			/* if there was no phantomas(4) here */
			if (dp.dp_bc[5] == -1)
				path_shift(&dp, 0);
			bus_walk(&dp, MAXMODBUS);
			break;
		case HPPA_TYPE_ADIRECT:
			switch (mptr.iodc_sv_model) {
			case HPPA_ADIRECT_RS232:
				register_device(serials, MAX_SERIALS,
				    &dp, &mptr, SERIAL, 0);
				break;
			case HPPA_ADIRECT_HIL:
				register_device(keyboards, MAX_KEYBOARDS,
				    &dp, &mptr, HIL, 0);
				break;
			case HPPA_ADIRECT_PEACOCK:
			case HPPA_ADIRECT_LEONARDO:
				register_device(graphics, MAX_GRAPHICS,
				    &dp, &mptr, GRAPHICS, 0);
				break;
			}
			break;
		case HPPA_TYPE_FIO:
			switch (mptr.iodc_sv_model) {
			case HPPA_FIO_HIL:
				register_device(keyboards, MAX_KEYBOARDS,
				    &dp, &mptr, HIL, 0);
				break;
			case HPPA_FIO_RS232:	/* com@gsc */
				register_device(serials, MAX_SERIALS,
				    &dp, &mptr, SERIAL, 0);
				break;
			case HPPA_FIO_DINOPCK:
				register_device(keyboards, MAX_KEYBOARDS,
				    &dp, &mptr, PS2, 0);
				break;
			case HPPA_FIO_GPCIO:
				/*
				 * KLUGE! At this point, there is no way to
				 * know if this port is the keyboard port or
				 * the mouse port.
				 * Let's assume the first port found is the
				 * keyboard, and ignore the others.
				 */
				if (kluge_ps2 != 0)
					break;
				register_device(keyboards, MAX_KEYBOARDS,
				    &dp, &mptr, PS2, 1);
				kluge_ps2++;
				break;
			case HPPA_FIO_GRS232:	/* com@dino, com@gsc */
			{
				int j, first;

				/*
				 * If a GIO serial port is already registered,
				 * register as extra port...
				 */
				first = 1;
				for (j = 0; j < MAX_SERIALS; j++)
					if (serials[j].type == SERIAL &&
					    serials[j].iodc_type ==
					      HPPA_TYPE_FIO &&
					    serials[j].iodc_model ==
					      HPPA_FIO_GRS232) {
						first = 0;
						break;
					}

				register_device(serials, MAX_SERIALS,
				    &dp, &mptr, SERIAL, first);
			}
				break;
			case HPPA_FIO_SGC:
				register_device(graphics, MAX_GRAPHICS,
				    &dp, &mptr, GRAPHICS, 0);
				break;
			case HPPA_FIO_GSGC:
				register_device(graphics, MAX_GRAPHICS,
				    &dp, &mptr, GRAPHICS, 1);
				break;
#if 0 /* can these really be used as console? */
			case HPPA_FIO_GRJ16:	/* com@gsc */
				register_device(serials, MAX_SERIALS,
				    &dp, &mptr, SERIAL, 0);
				break;
#endif
			}
			break;
		case HPPA_TYPE_IOA:
			switch (mptr.iodc_sv_model) {
			case HPPA_IOA_UTURN:
				bus_walk(&dp, MAXMODBUS - 1);
				break;
			}
			break;
		}
	}
}

/*
 * PCI bus walker.
 * The PDC device enumeration stops at the PCI bridge level, however
 * in order to properly handle console path on systems with PCI graphics
 * and USB controllers, it is necessary to dig further.
 *
 * Note that there are apparently PDC routines to access bridge configuration
 * space, but I have yet to find documentation about them.
 *
 * We ignore multi-function devices and subordinate PCI busses here, since
 * PDC PCI device paths stop at the PCI device number, and subordinate
 * busses are unlikely to be configured by the PDC.
 */

#define	ELROY_MODEL	0x78
#define	DINO_PAMR	0x804
#define	DINO_CFG_ADDR	0x64
#define	DINO_CFG_DATA	0x68

void
pci_bus_walk(struct device_path *idp, struct iodc_data *mptr,
    struct pdc_memmap *memmap)
{
	struct device_path dp;
	int dev, fn, nfuncs;
	uint32_t id, bhlcr, class;
	uint32_t (*conf_read)(u_int, int, int, u_int);

	if (mptr->iodc_model == ELROY_MODEL)
		conf_read = elroy_conf_read;
	else
		conf_read = dino_conf_read;

	for (dev = 0; dev < 32; dev++) {
		id = (*conf_read)(memmap->hpa, dev, 0, PCI_ID_REG);

		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID || PCI_VENDOR(id) == 0)
			continue;

		bhlcr = (*conf_read)(memmap->hpa, dev, 0, PCI_BHLC_REG);
		nfuncs = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;

		for (fn = 0; fn < nfuncs; fn++) {
			dp = *idp;
			path_shift(&dp, dev);
			path_shift(&dp, fn);

			if (fn != 0)
				id = (*conf_read)(memmap->hpa, dev, fn,
				    PCI_ID_REG);
			class = (*conf_read)(memmap->hpa, dev, fn,
			    PCI_CLASS_REG);

			/*
			 * We are only interested in two kinds of devices
			 * here: sti graphics, and USB controllers.
			 */
			if (PCI_CLASS(class) == PCI_CLASS_SERIALBUS &&
			    PCI_SUBCLASS(class) == PCI_SUBCLASS_SERIALBUS_USB) {
				/*
				 * Note about the last parameter of the
				 * register_device() call below being zero:
				 * machines with USB keyboards have neither
				 * PS/2 nor HIL controllers, so it doesn't
				 * matter what order the USB controllers are
				 * in.
				 * However machines with PS/2 keyboards
				 * might have an USB PCI card plugged in,
				 * which better appear after the PS/2
				 * keyboard.
				 */
				register_device(keyboards, MAX_KEYBOARDS,
				    &dp, mptr, USB, 0);
				continue;
			}

			switch (PCI_VENDOR(id)) {
			case PCI_VENDOR_HP:
				switch (PCI_PRODUCT(id)) {
				case PCI_PRODUCT_HP_VISUALIZE_EG:
				case PCI_PRODUCT_HP_VISUALIZE_FX2:
				case PCI_PRODUCT_HP_VISUALIZE_FX4:
				case PCI_PRODUCT_HP_VISUALIZE_FX6:
				case PCI_PRODUCT_HP_VISUALIZE_FXE:
					register_device(graphics, MAX_GRAPHICS,
					    &dp, mptr, GRAPHICS, 0);
					break;
				}
				break;
			case PCI_VENDOR_NS:
				if (PCI_PRODUCT(id) == PCI_PRODUCT_NS_PC87560) {
					/* serial_2 */
					path_shift(&dp, 2);
					register_device(serials, MAX_SERIALS,
					    &dp, mptr, SERIAL, 1);
					/* serial_1 */
					dp.dp_mod = 1;
					register_device(serials, MAX_SERIALS,
					    &dp, mptr, SERIAL, 1);
				}
				break;
			}
		}
	}
}

uint32_t
dino_conf_read(u_int hpa, int dev, int fn, u_int reg)
{
	volatile uint32_t *dino = (volatile uint32_t *)hpa;
	uint32_t pamr;
	uint32_t addr, id;

	addr = (dev << 11) | (fn << 8) | reg;

	pamr = dino[DINO_PAMR / 4];
	dino[DINO_PAMR / 4] = 0;
	dino[DINO_CFG_ADDR / 4] = addr;
	id = dino[DINO_CFG_DATA / 4];
	dino[DINO_PAMR / 4] = pamr;

	return letoh32(id);
}

uint32_t
elroy_conf_read(u_int hpa, int dev, int fn, u_int reg)
{
	volatile struct elroy_regs *elroy = (volatile struct elroy_regs *)hpa;
	uint32_t arb_mask, err_cfg, control;
	uint32_t addr, id;

	addr = (dev << 11) | (fn << 8) | reg;

	arb_mask = *(volatile uint32_t *)&elroy->arb_mask;
	err_cfg = *(volatile uint32_t *)&elroy->err_cfg;
	control = *(volatile uint32_t *)&elroy->control;

	if (arb_mask == 0)
		*(volatile uint32_t *)&elroy->arb_mask =
		    htole32(ELROY_ARB_ENABLE);
	*(volatile uint32_t *)&elroy->err_cfg = err_cfg |
	    htole32(ELROY_ERRCFG_SMART | ELROY_ERRCFG_CM);
	*(volatile uint32_t *)&elroy->control =
	    (control | htole32(ELROY_CONTROL_CE)) & ~htole32(ELROY_CONTROL_HF);

	*(volatile uint32_t *)&elroy->pci_conf_addr = htole32(addr);
	addr = *(volatile uint32_t *)&elroy->pci_conf_addr;
	id = *(volatile uint32_t *)&elroy->pci_conf_data;

	*(volatile uint32_t *)&elroy->control =
	    control | htole32(ELROY_CONTROL_CE | ELROY_CONTROL_CL);
	*(volatile uint32_t *)&elroy->control = control;
	*(volatile uint32_t *)&elroy->err_cfg = err_cfg;
	if (arb_mask == 0)
		*(volatile uint32_t *)&elroy->arb_mask = arb_mask;

	return letoh32(id);
}

/*
 * Relaxed device comparison
 */
int
path_match(struct device_path *dev1, struct device_path *dev2)
{
	return dev1->dp_mod == dev2->dp_mod &&
	    dev1->dp_bc[0] == dev2->dp_bc[0] &&
	    dev1->dp_bc[1] == dev2->dp_bc[1] &&
	    dev1->dp_bc[2] == dev2->dp_bc[2] &&
	    dev1->dp_bc[3] == dev2->dp_bc[3] &&
	    dev1->dp_bc[4] == dev2->dp_bc[4] &&
	    dev1->dp_bc[5] == dev2->dp_bc[5];
}

/*
 * Shift a device path, inserting a new value as dp_mod.
 */
void
path_shift(struct device_path *dp, int nmod)
{
	dp->dp_bc[0] = dp->dp_bc[1];
	dp->dp_bc[1] = dp->dp_bc[2];
	dp->dp_bc[2] = dp->dp_bc[3];
	dp->dp_bc[3] = dp->dp_bc[4];
	dp->dp_bc[4] = dp->dp_bc[5];
	dp->dp_bc[5] = dp->dp_mod;
	dp->dp_mod = nmod;
}

void
register_device(devlist, cnt, dp, mptr, type, first)
	struct consoledev *devlist;
	int cnt;
	struct device_path *dp;
	struct iodc_data *mptr;
	int type;
	int first;
{
	int i;
	struct consoledev *dev;

	for (i = 0, dev = devlist; i < cnt; i++, dev++)
		if (dev->type == 0)
			break;

	if (i == cnt) {
#ifdef DEBUG
		printf("can't register device, need more room!\n");
#endif
		return;
	}

	/*
	 * If this is supposedly the main device, insert on top
	 */
	if (first != 0) {
		memcpy(devlist + 1, devlist,
		    (cnt - 1) * sizeof(struct consoledev));
		dev = devlist;
	}

	dev->dp = *dp;
	dev->type = type;
	dev->iodc_type = mptr->iodc_type;
	dev->iodc_model = mptr->iodc_sv_model;

#ifdef DEBUG
	printf("(registered as type %d)\n", type);
#endif
}


code:
/*	$OpenBSD: ct.c,v 1.9 2004/04/07 18:24:20 mickey Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Copyright 1996 1995 by Open Software Foundation, Inc.
 *              All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "libsa.h"

#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/reboot.h>

#include <machine/pdc.h>
#include <machine/iomod.h>

#include "dev_hppa.h"

int
ctopen(struct open_file *f, ...)
{
	struct hppa_dev *dp = f->f_devdata;

	if (!(dp->pz_dev = pdc_findev(-1, PCL_SEQU)))
		return (ENXIO);

	return (0);
}

/*ARGSUSED*/
int
ctclose(f)
	struct open_file *f;
{
	free (f->f_devdata, sizeof(struct hppa_dev));
	f->f_devdata = NULL;
	return 0;
}


code:
/*	$OpenBSD: dev_hppa.c,v 1.17 2014/07/13 09:26:08 jasper Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "libsa.h"
#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/reboot.h>
#include <dev/cons.h>

#include <machine/iomod.h>

#include "dev_hppa.h"

extern int debug;

const char cdevs[][4] = {
	"ite", "", "", "", "", "", "", "",
	"", "", "", "", ""
};
const int ncdevs = nitems(cdevs);

const struct pdc_devs {
	char	name[3];
	int	dev_type;
} pdc_devs[] = {
	{ "dk",  0 },
	{ "ct",  1 },
	{ "lf",  2 },
	{ "",   -1 },
	{ "rd", -1 },
	{ "sw", -1 },
	{ "fl", -1 },
};

/* pass dev_t to the open routines */
int
devopen(f, fname, file)
	struct open_file *f;
	const char *fname;
	char **file;
{
	struct hppa_dev *hpd;
	const struct pdc_devs *dp = pdc_devs;
	int rc = 1;

	if (!(*file = strchr(fname, ':')))
		return ENODEV;
	else
		(*file)++;

#ifdef DEBUG
	if (debug)
		printf("devopen: ");
#endif

	for (dp = pdc_devs; dp < &pdc_devs[nitems(pdc_devs)]; dp++)
		if (!strncmp(fname, dp->name, sizeof(dp->name)-1))
			break;

	if (dp >= &pdc_devs[nitems(pdc_devs)] || dp->dev_type < 0)
		return ENODEV;
#ifdef DEBUG
	if (debug)
		printf("%s\n", dp->name);
#endif

	if (!(hpd = alloc(sizeof *hpd))) {
#ifdef DEBUG
		printf ("devopen: no mem\n");
#endif
	} else {
		bzero(hpd, sizeof *hpd);
		hpd->bootdev = bootdev;
		hpd->buf = (char *)(((u_int)hpd->ua_buf + IODC_MINIOSIZ-1) &
			~(IODC_MINIOSIZ-1));
		f->f_devdata = hpd;
		if ((rc = (*devsw[dp->dev_type].dv_open)(f, file)) == 0) {
			f->f_dev = &devsw[dp->dev_type];
			return 0;
		}
		free (hpd, 0);
		f->f_devdata = NULL;
	}

	if (!(f->f_flags & F_NODEV))
		f->f_dev = &devsw[dp->dev_type];

	if (!f->f_devdata)
		*file = NULL;

	return rc;
}

void
devboot(dev, p)
	dev_t dev;
	char *p;
{
	const char *q;
	int unit;

	if (!dev) {
		int type;

		switch (PAGE0->mem_boot.pz_class) {
		case PCL_RANDOM:
			type = 0;
			unit = PAGE0->mem_boot.pz_layers[0];
			break;
		case PCL_SEQU:
			type = 1;
			unit = PAGE0->mem_boot.pz_layers[0];
			break;
		case PCL_NET_MASK|PCL_SEQU:
			type = 2;
			unit = 0;
			break;
		default:
			type = 0;
			unit = 0;
			break;
		}
		dev = bootdev = MAKEBOOTDEV(type, 0, 0, unit, B_PARTITION(dev));
	}
#ifdef _TEST
	*p++ = '/';
	*p++ = 'd';
	*p++ = 'e';
	*p++ = 'v';
	*p++ = '/';
	*p++ = 'r';
#endif
	/* quick copy device name */
	for (q = pdc_devs[B_TYPE(dev)].name; (*p++ = *q++););
	unit = B_UNIT(dev);
	if (unit >= 10) {
		p[-1] = '0' + unit / 10;
		*p++ = '0' + (unit % 10);
	} else
		p[-1] = '0' + unit;
	*p++ = 'a' + B_PARTITION(dev);
	*p = '\0';
}

char ttyname_buf[8];

char *
ttyname(fd)
	int fd;
{
	snprintf(ttyname_buf, sizeof ttyname_buf, "%s%d",
	    cdevs[major(cn_tab->cn_dev)],
	    minor(cn_tab->cn_dev));
	return (ttyname_buf);
}

dev_t
ttydev(name)
	char *name;
{
	int i, unit = -1;
	char *no = name + strlen(name) - 1;

	while (no >= name && *no >= '0' && *no <= '9')
		unit = (unit < 0 ? 0 : (unit * 10)) + *no-- - '0';
	if (no < name || unit < 0)
		return (NODEV);
	for (i = 0; i < ncdevs; i++)
		if (strncmp(name, cdevs[i], no - name + 1) == 0)
			return (makedev(i, unit));
	return (NODEV);
}


code:
/*	$OpenBSD: dev_hppa.h,v 1.9 2011/03/13 00:13:52 deraadt Exp $	*/

#define IOPGSHIFT	11
#define	IONBPG		(1 << IOPGSHIFT)
#define IOPGOFSET	(IONBPG - 1)

struct disklabel;
struct hppa_dev {
	dev_t	bootdev;
	struct pz_device *pz_dev;	/* device descriptor */
	daddr32_t fsoff;			/* offset to the file system */
	daddr32_t	last_blk;		/* byte offset for last read blk */
	size_t	last_read;		/* amount read last time */
	struct disklabel *label;
	/* buffer to cache data (aligned properly) */
	char	*buf;
	char	ua_buf[IODC_IOSIZ + IODC_MINIOSIZ];
};

#ifdef PDCDEBUG
#define	DEVPATH_PRINT(dp) \
	printf("%x, %d.%d.%d.%d.%d.%d, 0x%x, %x.%x.%x.%x.%x.%x\n", \
	       (dp)->dp_flags, (dp)->dp_bc[0], (dp)->dp_bc[1], (dp)->dp_bc[2], \
	       (dp)->dp_bc[3], (dp)->dp_bc[4], (dp)->dp_bc[5], (dp)->dp_mod, \
	       (dp)->dp_layers[0], (dp)->dp_layers[1], (dp)->dp_layers[2], \
	       (dp)->dp_layers[3], (dp)->dp_layers[4], (dp)->dp_layers[5]);
#define	PZDEV_PRINT(dp) \
	printf("devpath={%x, %d.%d.%d.%d.%d.%d, 0x%x, %x.%x.%x.%x.%x.%x}," \
	       "\n\thpa=%p, spa=%p, io=%p, class=%u\n", \
	       (dp)->pz_flags, (dp)->pz_bc[0], (dp)->pz_bc[1], (dp)->pz_bc[2], \
	       (dp)->pz_bc[3], (dp)->pz_bc[4], (dp)->pz_bc[5], (dp)->pz_mod, \
	       (dp)->pz_layers[0], (dp)->pz_layers[1], (dp)->pz_layers[2], \
	       (dp)->pz_layers[3], (dp)->pz_layers[4], (dp)->pz_layers[5], \
	       (dp)->pz_hpa, (dp)->pz_spa, (dp)->pz_iodc_io, (dp)->pz_class);
#endif

extern pdcio_t pdc;
extern int pdcbuf[];			/* PDC returns, pdc.c */

int iodc_rw(char *, u_int, u_int, int func, struct pz_device *);
const char *dk_disklabel(struct hppa_dev *dp, struct disklabel *label);


code:
/*	$OpenBSD: dk.c,v 1.14 2012/02/25 20:12:00 miod Exp $	*/

/*
 * Copyright 1996 1995 by Open Software Foundation, Inc.
 *              All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "libsa.h"

#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/reboot.h>
#include <machine/pdc.h>
#include <machine/iomod.h>

#include "dev_hppa.h"

const char *
dk_disklabel(struct hppa_dev *dp, struct disklabel *label)
{
	char buf[DEV_BSIZE];
	size_t ret;

	if (iodcstrategy(dp, F_READ, LABELSECTOR, DEV_BSIZE, buf, &ret))
		if (ret != DEV_BSIZE)
			return "cannot read disklabel";

	return (getdisklabel(buf, label));
}

int
dkopen(struct open_file *f, ...)
{
	struct disklabel *lp;
	struct hppa_dev *dp = f->f_devdata;
	const char *st;

#ifdef	DEBUG
	if (debug)
		printf("dkopen(%p)\n", f);
#endif

	if (!(dp->pz_dev = pdc_findev(-1, PCL_RANDOM)))
		return ENXIO;

	lp = dp->label;
	st = NULL;

#ifdef DEBUG
	if (debug)
		printf ("disklabel\n");
#endif

	if ((st = dk_disklabel(dp, lp)) != NULL) {
#ifdef DEBUG
		if (debug)
			printf ("dkopen: %s\n", st);
#endif
		/* we do not know if it's a disk or net, but do not fail */
	} else {
		u_int i;

		i = B_PARTITION(dp->bootdev);
		if (i >= lp->d_npartitions || !lp->d_partitions[i].p_size)
			return (EPART);

		dp->fsoff = lp->d_partitions[i].p_offset;
	}

#ifdef DEBUG
	if (debug)
		printf ("dkopen() ret\n");
#endif
	return (0);
}

int
dkclose(f)
	struct open_file *f;
{
	free (f->f_devdata, sizeof(struct hppa_dev));
	f->f_devdata = NULL;
	return 0;
}


code:
/*	$OpenBSD: elf32.c,v 1.1 2007/12/26 18:52:59 kettenis Exp $	*/

/*
 * Copyright (c) 2007 Tom Cosgrove <tom@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <lib/libkern/libkern.h>
#include <lib/libsa/stand.h>

#include <sys/param.h>
#include <sys/exec.h>

#include "../../../../lib/libsa/loadfile.h"

#undef ELFSIZE
#define ELFSIZE  32

#include <sys/exec_elf.h>

#include "../../../../lib/libsa/loadfile_elf.c"


code:
/*	$OpenBSD: elf64.c,v 1.1 2007/12/26 18:52:59 kettenis Exp $	*/

/*
 * Copyright (c) 2007 Tom Cosgrove <tom@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <lib/libkern/libkern.h>
#include <lib/libsa/stand.h>

#include <sys/param.h>
#include <sys/exec.h>

#include "../../../../lib/libsa/loadfile.h"

#undef ELFSIZE
#define ELFSIZE  64

#include <sys/exec_elf.h>

#include "../../../../lib/libsa/loadfile_elf.c"


code:
/*	$OpenBSD: itecons.c,v 1.12 2014/07/17 12:37:46 miod Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Copyright 1996 1995 by Open Software Foundation, Inc.
 *              All Rights Reserved
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appears in all copies and
 * that both the copyright notice and this permission notice appear in
 * supporting documentation.
 *
 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE.
 *
 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#include "libsa.h"

#include <sys/param.h>
#include <sys/disklabel.h>
#include <machine/pdc.h>
#include <machine/iomod.h>
#include <dev/cons.h>

#include "dev_hppa.h"

iodcio_t cniodc;	/* console IODC entry point */
iodcio_t kyiodc;	/* keyboard IODC entry point */
pz_device_t *cons_pzdev, *kbd_pzdev;

/*
 * Console.
 */

char cnbuf[IODC_MINIOSIZ] __attribute__ ((aligned (IODC_MINIOSIZ)));
int kycode[IODC_MAXSIZE/sizeof(int)];

int
cnspeed(dev_t dev, int sp)
{
	return CONSPEED;
}

void
ite_probe(cn)
	struct consdev *cn;
{
	cniodc = (iodcio_t)PAGE0->mem_free;
	cons_pzdev = &PAGE0->mem_cons;
	kbd_pzdev = &PAGE0->mem_kbd;

	if ((*pdc)   (PDC_IODC, PDC_IODC_READ, pdcbuf, cons_pzdev->pz_hpa,
		      IODC_INIT, cniodc, IODC_MAXSIZE) < 0 ||
	    (*cniodc)(cons_pzdev->pz_hpa,
		      (cons_pzdev->pz_hpa==PAGE0->mem_boot.pz_hpa)?
			IODC_INIT_DEV: IODC_INIT_ALL, cons_pzdev->pz_spa,
			cons_pzdev->pz_layers, pdcbuf, 0,0,0,0) < 0 ||
	    (*pdc)   (PDC_IODC, PDC_IODC_READ, pdcbuf, cons_pzdev->pz_hpa,
		      IODC_IO, cniodc, IODC_MAXSIZE) < 0) {
		/* morse code with the LED's?!! */
		cons_pzdev->pz_iodc_io = kbd_pzdev->pz_iodc_io = NULL;
	} else {
		cn->cn_pri = CN_MIDPRI;
		cn->cn_dev = makedev(0, 0);
	}
}

void
ite_init(cn)
	struct consdev *cn;
{
	/*
	 * If the keyboard is separate from the console output device,
	 * we load the keyboard code at `kycode'.
	 *
	 * N.B. In this case, since the keyboard code is part of the
	 * boot code, it will be overwritten when we load a kernel.
	 */
	if (cons_pzdev->pz_class != PCL_DUPLEX ||
	    kbd_pzdev->pz_class == PCL_KEYBD) {

		kyiodc = (iodcio_t)kycode;

		if ((*pdc)   (PDC_IODC, PDC_IODC_READ, pdcbuf, kbd_pzdev->pz_hpa,
			      IODC_INIT, kyiodc, IODC_MAXSIZE) < 0 ||
		    (*kyiodc)(kbd_pzdev->pz_hpa,
			      (kbd_pzdev->pz_hpa == PAGE0->mem_boot.pz_hpa ||
			       kbd_pzdev->pz_hpa == cons_pzdev->pz_hpa)?
			      IODC_INIT_DEV: IODC_INIT_ALL, kbd_pzdev->pz_spa,
			      kbd_pzdev->pz_layers, pdcbuf, 0, 0, 0, 0) < 0 ||
		    (*pdc)   (PDC_IODC, PDC_IODC_READ, pdcbuf, kbd_pzdev->pz_hpa,
			      IODC_IO, kyiodc, IODC_MAXSIZE))
			kyiodc = NULL;
	} else {
		kyiodc = cniodc;

		bcopy((char *)&PAGE0->mem_cons, (char *)&PAGE0->mem_kbd,
		      sizeof(struct pz_device));
	}

	cons_pzdev->pz_iodc_io = (u_int)cniodc;
	kbd_pzdev->pz_iodc_io = (u_int)kyiodc;
#ifdef DEBUG
	if (!kyiodc)
		printf("ite_init: no kbd\n");
#endif
}

void
ite_putc(dev, c)
	dev_t dev;
	int c;
{
	if (cniodc == NULL)
		return;

	*cnbuf = c;

	(*cniodc)(cons_pzdev->pz_hpa, IODC_IO_CONSOUT, cons_pzdev->pz_spa,
		  cons_pzdev->pz_layers, pdcbuf, 0, cnbuf, 1, 0);
}

/*
 * since i don't know how to 'just check the char available'
 * i store the key into the stash removing on read op later;
 */
int
ite_getc(dev)
	dev_t dev;
{
	static int stash = 0;
	int err, c, l, i;

	if (kyiodc == NULL)
		return(0x100);

	if (stash) {
		c = stash;
		if (!(dev & 0x80))
			stash = 0;
		return c;
	}

	i = 16;
	do {
		err = (*kyiodc)(kbd_pzdev->pz_hpa, IODC_IO_CONSIN,
				kbd_pzdev->pz_spa, kbd_pzdev->pz_layers,
				pdcbuf, 0, cnbuf, 1, 0);
		l = pdcbuf[0];
		c = cnbuf[0];
#ifdef DEBUG
		if (debug && err < 0)
			printf("KBD input error: %d", err);
#endif

		/* if we are doing ischar() report immediately */
		if (!i-- && (dev & 0x80) && l == 0) {
#ifdef DEBUG
			if (debug > 2)
				printf("ite_getc(0x%x): no char %d(%x)\n",
				       dev, l, c);
#endif
			return (0);
		}
	} while(!l);

#if DEBUG
	if (debug && l > 1)
		printf("KBD input got too much (%d)\n", l);

	if (debug > 3)
		printf("kbd: \'%c\' (0x%x)\n", c, c);
#endif
	if (dev & 0x80)
		stash = c;

	return (c);
}

void
ite_pollc(dev, on)
	dev_t dev;
	int on;
{

}


code:
/*	$OpenBSD: lf.c,v 1.10 2014/07/12 21:03:38 tedu Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include "libsa.h"
#include <machine/pdc.h>
#include <machine/iomod.h>

#include "dev_hppa.h"

int
lfopen(struct open_file *f, ...)
{
	struct hppa_dev *dp = f->f_devdata;

	if (!(dp->pz_dev = pdc_findev(-1, PCL_NET_MASK|PCL_SEQU)))
		return ENXIO;

	return 0;
}

int
lfclose(struct open_file *f)
{
	free(f->f_devdata, sizeof(struct hppa_dev));
	f->f_devdata = NULL;
	return 0;
}


code:
/*	$OpenBSD: libsa.h,v 1.14 2011/03/13 00:13:52 deraadt Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <lib/libsa/stand.h>

#define	EXEC_ELF
#define	EXEC_SOM

#define	DEFAULT_KERNEL_ADDRESS	0

extern dev_t bootdev;

void pdc_init(void);
struct pz_device;
struct pz_device *pdc_findev(int, int);

int iodcstrategy(void *, int, daddr32_t, size_t, void *, size_t *);

int ctopen(struct open_file *, ...);
int ctclose(struct open_file *);

int dkopen(struct open_file *, ...);
int dkclose(struct open_file *);

int lfopen(struct open_file *, ...);
int lfstrategy(void *, int, daddr32_t, size_t, void *, size_t *);
int lfclose(struct open_file *);

void ite_probe(struct consdev *);
void ite_init(struct consdev *);
int ite_getc(dev_t);
void ite_putc(dev_t, int);
void ite_pollc(dev_t, int);

void machdep(void);
void devboot(dev_t, char *);
void fcacheall(void);
void run_loadfile(u_long *marks, int howto);

int lif_open(char *path, struct open_file *f);
int lif_close(struct open_file *f);
int lif_read(struct open_file *f, void *buf, size_t size, size_t *resid);
int lif_write(struct open_file *f, void *buf, size_t size, size_t *resid);
off_t lif_seek(struct open_file *f, off_t offset, int where);
int lif_stat(struct open_file *f, struct stat *sb);
int lif_readdir(struct open_file *f, char *name);

union x_header;
struct x_param;
int som_probe(int, union x_header *);
int som_load(int, struct x_param *);
int som_ldsym(int, struct x_param *);

extern int debug;

#define	MACHINE_CMD	cmd_machine	/* we have hppa specific commands */


code:
/*	$OpenBSD: lif.c,v 1.10 2004/11/22 18:41:41 mickey Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/disklabel.h>
#include "libsa.h"

extern int debug;

struct file {
	char f_buf[LIF_FILESTART];/* buffer for lif volume header and dir */
	struct lifvol *f_lp;	/* lif volume header pointer */
	struct lifdir *f_ld;	/* lif dir pointer */
	int	f_nfiles;	/* gross number for lif dir entries */

	off_t	f_seek;		/* seek pointer for file read */
	struct lifdir *f_rd;	/* lif dir pointer for readdir */

	int	f_isdir;	/* special hacky flag for '.' dir */
	int	f_count;	/* this file length */
	int	f_off;		/* this file offset */
};

int
lif_open (path, f)
	char *path;
	struct open_file *f;
{
	struct file *fp;
	struct lifdir *dp;
	char *p, *q;
	struct lif_load load;
	size_t buf_size;
	int err, l;

#ifdef LIFDEBUG
	if (debug)
		printf("lif_open(%s, %p)\n", path, f);
#endif

	fp = alloc(sizeof(*fp));
	/* XXX we're assuming here that sizeof(fp->f_buf) >= LIF_FILESTART */
	if ((err = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, 0,
	    sizeof(fp->f_buf), &fp->f_buf, &buf_size)) ||
	    buf_size != sizeof(fp->f_buf)) {
#ifdef LIFDEBUG
		if (debug)
			printf("lif_open: unable to read LIF header (%d)\n", err);
#endif
	} else if ((fp->f_lp = (struct lifvol *)fp->f_buf)->vol_id == LIF_VOL_ID) {
		f->f_fsdata = fp;
		fp->f_ld = (struct lifdir *)(fp->f_buf + LIF_DIRSTART);
		fp->f_seek = 0;
		fp->f_rd = fp->f_ld;
		fp->f_nfiles = lifstob(fp->f_lp->vol_dirsize) /
			sizeof(struct lifdir);

		/* no dirs on the lif */
		for (p = path + (l = strlen(path)); p >= path; p--)
			if (*p == '/') {
				p++;
				break;
			}
		if (p > path)
			path = p;
	} else
		err = EINVAL;

	if (!err && *path != '.') {
		fp->f_isdir = 0;
		err = ENOENT;
		for (dp = fp->f_ld; dp < &fp->f_ld[fp->f_nfiles]; dp++) {
#ifdef LIFDEBUG
			if (debug)
				printf("lif_open: "
				       "%s <--> '%c%c%c%c%c%c%c%c%c%c'\n",
				       path, dp->dir_name[0], dp->dir_name[1],
				       dp->dir_name[2], dp->dir_name[3],
				       dp->dir_name[4], dp->dir_name[5],
				       dp->dir_name[6], dp->dir_name[7],
				       dp->dir_name[8], dp->dir_name[9]);
#endif
			for (p = path, q = dp->dir_name;
			     *q && *q != ' '; q++, p++)
				if (tolower(*q) != tolower(*p))
					break;
			if ((!*q || *q == ' ') && !*p) {
				err = 0;
				break;
			}
		}
		if (!err) {
			fp->f_off = lifstodb(dp->dir_addr);
			if (!(err =(f->f_dev->dv_strategy)(f->f_devdata, F_READ,
			      fp->f_off, sizeof(load), &load, &buf_size)) &&
			    buf_size == sizeof(load)) {
				/* no checksum */
				fp->f_count = load.count - sizeof(int);
				fp->f_off = dbtob(fp->f_off) + sizeof(load);
#ifdef LIFDEBUG
				if (debug)
					printf("lif_open: %u @ %u [%x]\n",
					       fp->f_count, fp->f_off,
					       load.address);
#endif
			} else if (!err)
				err = EIO;
		}
	} else
		fp->f_isdir = 1;

	if (err) {
		free (fp, sizeof(*fp));
		f->f_fsdata = NULL;
	}
#ifdef LIFDEBUG
	if (debug)
		printf("ret(%d)\n", err);
#endif
	return err;
}

int
lif_close(f)
	struct open_file *f;
{
	free (f->f_fsdata, sizeof(struct file));
	f->f_fsdata = NULL;
	return 0;
}

int
lif_read(f, buf, size, resid)
	struct open_file *f;
	void *buf;
	size_t size;
	size_t *resid;
{
	struct file *fp = (struct file *)f->f_fsdata;
	char *p;
	char bbuf[DEV_BSIZE];
	size_t bsize, count = sizeof(bbuf);
	int err = 0;
	int foff;

#ifdef LIFDEBUG
	if (debug)
		printf("lif_read(%p, %p, %u, %p)\n", f, buf, size, resid);
#endif

	for (p = bbuf; size; fp->f_seek += bsize, p += bsize) {
		twiddle();
		foff = fp->f_off + fp->f_seek;
		if (fp->f_seek >= fp->f_count ||
		    (err = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
		     btodb(foff), count, p, &bsize)))
			break;
		if (p == bbuf) {
			bsize = sizeof(bbuf) - (foff & (sizeof(bbuf) - 1));
			bsize = min(bsize, size);
			bcopy(bbuf + (foff & (sizeof(bbuf) - 1)), buf, bsize);
			p = buf;
		}
		count = size -= bsize;
	}
	if (resid)
		*resid = size;

	return err;
}

int
lif_write(f, buf, size, resid)
	struct open_file *f;
	void *buf;
	size_t size;
	size_t *resid;
{
	return EOPNOTSUPP;
}

off_t
lif_seek(f, offset, where)
	struct open_file *f;
	off_t offset;
	int where;
{
	struct file *fp = (struct file *)f->f_fsdata;

	switch (where) {
	case SEEK_SET:
		fp->f_seek = offset;
		break;
	case SEEK_CUR:
		fp->f_seek += offset;
		break;
	case SEEK_END:
		fp->f_seek = fp->f_count - offset;
		break;
	default:
		return (-1);
	}
	return (fp->f_seek);
}

int
lif_stat(f, sb)
	struct open_file *f;
	struct stat *sb;
{
	struct file *fp = (struct file *)f->f_fsdata;

	sb->st_mode = 0755 | (fp->f_isdir? S_IFDIR: 0);	/* XXX */
	sb->st_uid = 0;
	sb->st_gid = 0;
	sb->st_size = fp->f_count;
	return 0;
}

int
lif_readdir(f, name)
	struct open_file *f;
	char *name;
{
	struct file *fp = (struct file *)f->f_fsdata;
	char *p;

	if (name) {
		while ((fp->f_rd->dir_name[0] == ' ' ||
			!fp->f_rd->dir_name[0]) &&
		       (fp->f_rd - fp->f_ld) < fp->f_nfiles)
			fp->f_rd++;
		if ((fp->f_rd - fp->f_ld) >= fp->f_nfiles) {
			*name = '\0';
			return -1;
		}
		strncpy(name, fp->f_rd->dir_name, sizeof(fp->f_rd->dir_name));
		if ((p = strchr(name, ' ')))
			*p = '\0';
		fp->f_rd++;
	} else
		fp->f_rd = fp->f_ld;

	return 0;
}


code:
/*	$OpenBSD: machdep.c,v 1.9 2004/04/07 18:24:20 mickey Exp $	*/

/*
 * Copyright (c) 1998-2004 Michael Shalayeff
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/disklabel.h>
#include <sys/reboot.h>
#include "libsa.h"
#include <machine/iomod.h>
#include <machine/pdc.h>

#include "dev_hppa.h"

extern struct	stable_storage sstor;	/* contents of Stable Storage */
int howto;
dev_t bootdev;

void
machdep()
{
	pdc_init();
#ifdef notyet
	debug_init();
#endif
	cninit();

#ifdef PDCDEBUG
	if (debug) {
		int i;

		printf("SSTOR:\n");
		printf("pri_boot=");	DEVPATH_PRINT(&sstor.ss_pri_boot);
		printf("alt_boot=");	DEVPATH_PRINT(&sstor.ss_alt_boot);
		printf("console =");	DEVPATH_PRINT(&sstor.ss_console);
		printf("keyboard=");	DEVPATH_PRINT(&sstor.ss_keyboard);
		printf("mem=%d, fn=%s, osver=%d\nos={",
		       sstor.ss_fast_size, sstor.ss_filenames,
		       sstor.ss_os_version);
		for (i = 0; i < sizeof(sstor.ss_os); i++)
			printf ("%x%c", sstor.ss_os[i], (i%8)? ',' : '\n');

		printf("}\nPAGE0:\n");
		printf("ivec=%x, pf=%p[%u], toc=%p[%u], rndz=%p, clk/10ms=%u\n",
		       PAGE0->ivec_special, PAGE0->ivec_mempf,
		       PAGE0->ivec_mempflen, PAGE0->ivec_toc,
		       PAGE0->ivec_toclen, PAGE0->ivec_rendz,
		       PAGE0->mem_10msec);
		printf ("mem: cont=%u, phys=%u, pdc_spa=%u, resv=%u, free=%x\n"
			"cpu_hpa=%x, pdc=%p, imm_hpa=%p[%u,%u], soft=%u\n",
		       PAGE0->memc_cont, PAGE0->memc_phsize, PAGE0->memc_adsize,
		       PAGE0->memc_resv, PAGE0->mem_free, PAGE0->mem_hpa,
		       PAGE0->mem_pdc, PAGE0->imm_hpa, PAGE0->imm_spa_size,
		       PAGE0->imm_max_mem, PAGE0->imm_soft_boot);

		printf("console:  ");	PZDEV_PRINT(&PAGE0->mem_cons);
		printf("boot:     ");	PZDEV_PRINT(&PAGE0->mem_boot);
		printf("keyboard: ");	PZDEV_PRINT(&PAGE0->mem_kbd);
	}
#endif
}