code:
/* $OpenBSD: tsc.c,v 1.16 2014/12/24 18:46:14 miod Exp $ */
/* $NetBSD: tsc.c,v 1.3 2000/06/25 19:17:40 thorpej Exp $ */

/*-
 * Copyright (c) 1999 by Ross Harvey.  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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ross Harvey.
 * 4. The name of Ross Harvey may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY 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 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/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <machine/autoconf.h>
#include <machine/rpb.h>

#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <alpha/pci/tsreg.h>
#include <alpha/pci/tsvar.h>

#ifdef DEC_6600
#include <alpha/pci/pci_6600.h>
#endif

#define tsc() { Generate ctags(1) key. }

int	tscmatch(struct device *, void *, void *);
void	tscattach(struct device *, struct device *, void *);

struct cfattach tsc_ca = {
	sizeof(struct device), tscmatch, tscattach,
};

struct cfdriver tsc_cd = {
        NULL, "tsc", DV_DULL,
};

struct tsp_config tsp_configuration[4];

static int tscprint(void *, const char *pnp);

int	tspmatch(struct device *, void *, void *);
void	tspattach(struct device *, struct device *, void *);

struct cfattach tsp_ca = {
	sizeof(struct tsp_softc), tspmatch, tspattach,
};

struct cfdriver tsp_cd = {
        NULL, "tsp", DV_DULL,
};

static int tspprint(void *, const char *pnp);

#if	0
static int tsp_bus_get_window(int, int,
	struct alpha_bus_space_translation *);
#endif

/* There can be only one */
static int tscfound;

/* Which hose is the display console connected to? */
int tsp_console_hose;

int
tscmatch(parent, match, aux)
	struct device *parent;
	void *match;
	void *aux;
{
	struct mainbus_attach_args *ma = aux;

	switch (cputype) {
	case ST_DEC_6600:
	case ST_DEC_TITAN:
		return strcmp(ma->ma_name, tsc_cd.cd_name) == 0 && !tscfound;
	default:
		return 0;
	}
}

void
tscattach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	int i;
	int nbus;
	u_int64_t csc, aar;
	struct tsp_attach_args tsp;
	struct mainbus_attach_args *ma = aux;
	int titan = cputype == ST_DEC_TITAN;

	tscfound = 1;

	csc = LDQP(TS_C_CSC);

	nbus = 1 + (CSC_BC(csc) >= 2);
	printf(": 2127%c Chipset, Cchip rev %d\n"
	       "%s%d: %c Dchips, %d memory bus%s of %d bytes\n",
	    titan ? '4' : '2', (int)MISC_REV(LDQP(TS_C_MISC)),
	    ma->ma_name, ma->ma_slot, "2448"[CSC_BC(csc)],
	    nbus, nbus > 1 ? "es" : "", 16 + 16 * ((csc & CSC_AW) != 0));
	printf("%s%d: arrays present: ", ma->ma_name, ma->ma_slot);
	for(i = 0; i < 4; ++i) {
		aar = LDQP(TS_C_AAR0 + i * TS_STEP);
		printf("%s%dMB%s", i ? ", " : "", (8 << AAR_ASIZ(aar)) & ~0xf,
		    aar & AAR_SPLIT ? " (split)" : "");
	}
	printf(", Dchip 0 rev %d\n", (int)LDQP(TS_D_DREV) & 0xf);

	tsp.tsp_name = "tsp";
	tsp.tsp_slot = 0;
	config_found(self, &tsp, tscprint);
	if (titan) {
		tsp.tsp_slot += 2;
		config_found(self, &tsp, tscprint);
	}

	if (csc & CSC_P1P) {
		tsp.tsp_slot = 1;
		config_found(self, &tsp, tscprint);
		if (titan) {
			tsp.tsp_slot += 2;
			config_found(self, &tsp, tscprint);
		}
	}

	tsp.tsp_name = "tsciic";
	tsp.tsp_slot = -1;
	config_found(self, &tsp, tscprint);
}

static int
tscprint(aux, p)
	void *aux;
	const char *p;
{
	struct tsp_attach_args *tsp = aux;

	if (p)
		printf("%s at %s", tsp->tsp_name, p);
	if (tsp->tsp_slot >= 0)
		printf(" hose %d", tsp->tsp_slot);
	return UNCONF;
}

#define tsp() { Generate ctags(1) key. }

int
tspmatch(parent, match, aux)
	struct device *parent;
	void *match;
	void *aux;
{
	struct tsp_attach_args *t = aux;

	switch (cputype) {
	case ST_DEC_6600:
	case ST_DEC_TITAN:
		return strcmp(t->tsp_name, tsp_cd.cd_name) == 0;
	default:
		return 0;
	}
}

void
tspattach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct pcibus_attach_args pba;
	struct tsp_attach_args *t = aux;
	struct tsp_config *pcp;

	printf("\n");
	pcp = tsp_init(1, t->tsp_slot);

	tsp_dma_init(self, pcp);

	/*
	 * Do PCI memory initialization that needs to be deferred until
	 * malloc is safe.  On the Tsunami, we need to do this after
	 * DMA is initialized, as well.
	 */
	tsp_bus_mem_init2(pcp);

	pci_6600_pickintr(pcp);

	bzero(&pba, sizeof(pba));
	pba.pba_busname = "pci";
	pba.pba_iot = &pcp->pc_iot;
	pba.pba_memt = &pcp->pc_memt;
	pba.pba_dmat =
	    alphabus_dma_get_tag(&pcp->pc_dmat_direct, ALPHA_BUS_PCI);
	pba.pba_pc = &pcp->pc_pc;
	pba.pba_domain = pci_ndomains++;
	pba.pba_bus = 0;
#ifdef	notyet
	pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
	    PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
#endif
	config_found(self, &pba, tspprint);
}

struct tsp_config *
tsp_init(mallocsafe, n)
	int mallocsafe;
	int n;	/* hose number */
{
	struct tsp_config *pcp;
	int titan = cputype == ST_DEC_TITAN;

	KASSERT(n >= 0 && n < nitems(tsp_configuration));
	pcp = &tsp_configuration[n];
	pcp->pc_pslot = n;
	pcp->pc_iobase = TS_Pn(n, 0);
	pcp->pc_csr = S_PAGE(TS_Pn(n & 1, P_CSRBASE));
	if (n & 2) {
		/* `A' port of PA Chip */
		pcp->pc_csr++;
	}
	if (titan) {
		/* same address on G and A ports */
		pcp->pc_tlbia = &pcp->pc_csr->port.g.tsp_tlbia.tsg_r;
	} else {
		pcp->pc_tlbia = &pcp->pc_csr->port.p.tsp_tlbia.tsg_r;
	}
	snprintf(pcp->pc_io_ex_name, sizeof pcp->pc_io_ex_name,
	    "tsp%d_bus_io", n);
	snprintf(pcp->pc_mem_ex_name, sizeof pcp->pc_mem_ex_name,
	    "tsp%d_bus_mem", n);

	if (!pcp->pc_initted) {
		tsp_bus_io_init(&pcp->pc_iot, pcp);
		tsp_bus_mem_init(&pcp->pc_memt, pcp);
#if 0
		alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_IO] = 1;
		alpha_bus_window_count[ALPHA_BUS_TYPE_PCI_MEM] = 1;

		alpha_bus_get_window = tsp_bus_get_window;
#endif
	}
	pcp->pc_mallocsafe = mallocsafe;
	tsp_pci_init(&pcp->pc_pc, pcp);
	alpha_pci_chipset = &pcp->pc_pc;
	if (titan)
		alpha_pci_chipset->pc_name = "titan";
	else
		alpha_pci_chipset->pc_name = "tsunami";
	alpha_pci_chipset->pc_mem = P_PCI_MEM;
	alpha_pci_chipset->pc_ports = P_PCI_IO;
	alpha_pci_chipset->pc_hae_mask = 0;
	alpha_pci_chipset->pc_dense = TS_P0(0);	/* XXX */
	alpha_pci_chipset->pc_bwx = 1;
	pcp->pc_initted = 1;
	return pcp;
}

static int
tspprint(aux, p)
	void *aux;
	const char *p;
{
	register struct pcibus_attach_args *pci = aux;

	if (p)
		printf("%s at %s", pci->pba_busname, p);
	printf(" bus %d", pci->pba_bus);
	return UNCONF;
}

#if 0
static int
tsp_bus_get_window(type, window, abst)
	int type, window;
	struct alpha_bus_space_translation *abst;
{
	struct tsp_config *tsp = &tsp_configuration[tsp_console_hose];
	bus_space_tag_t st;
	int error;

	switch (type) {
	case ALPHA_BUS_TYPE_PCI_IO:
		st = &tsp->pc_iot;
		break;

	case ALPHA_BUS_TYPE_PCI_MEM:
		st = &tsp->pc_memt;
		break;

	default:
		panic("tsp_bus_get_window");
	}

	error = alpha_bus_space_get_window(st, window, abst);
	if (error)
		return (error);

	abst->abst_sys_start = TS_PHYSADDR(abst->abst_sys_start);
	abst->abst_sys_end = TS_PHYSADDR(abst->abst_sys_end);

	return (0);
}
#endif


code:
/*	$OpenBSD: tsciic.c,v 1.1 2014/12/24 18:46:14 miod Exp $	*/
/*	$NetBSD: tsciic.c,v 1.1 2014/02/21 12:23:30 jdc Exp $	*/

/*
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Julian Coleman.
 *
 * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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 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/systm.h>
#include <sys/device.h>
#include <sys/rwlock.h>

#include <alpha/pci/tsreg.h>
#include <alpha/pci/tsvar.h>

#include <dev/i2c/i2cvar.h>
#include <dev/i2c/i2c_bitbang.h>

struct tsciic_softc {
	struct device		sc_dev;

	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;

	struct i2c_controller	sc_i2c_tag;
	struct rwlock		sc_i2c_lock;
};

int	tsciic_match(struct device *, void *, void *);
void	tsciic_attach(struct device *, struct device *, void *);

const struct cfattach tsciic_ca = {
	.ca_devsize = sizeof(struct tsciic_softc),
	.ca_match = tsciic_match,
	.ca_attach = tsciic_attach
};

struct cfdriver tsciic_cd = {
	.cd_name = "tsciic",
	.cd_class = DV_DULL
};

/* I2C glue */
int	tsciic_acquire_bus(void *, int);
void	tsciic_release_bus(void *, int);
int	tsciic_send_start(void *, int);
int	tsciic_send_stop(void *, int);
int	tsciic_initiate_xfer(void *, i2c_addr_t, int);
int	tsciic_read_byte(void *, uint8_t *, int);
int	tsciic_write_byte(void *, uint8_t, int);

/* I2C bitbang glue */
void	tsciicbb_set_bits(void *, uint32_t);
void	tsciicbb_set_dir(void *, uint32_t);
uint32_t tsciicbb_read(void *);

#define MPD_BIT_SDA 0x01
#define MPD_BIT_SCL 0x02
static const struct i2c_bitbang_ops tsciicbb_ops = {
	.ibo_set_bits = tsciicbb_set_bits,
	.ibo_set_dir = tsciicbb_set_dir,
	.ibo_read_bits = tsciicbb_read,
	.ibo_bits = {
		[I2C_BIT_SDA] MPD_BIT_SDA,
		[I2C_BIT_SCL] MPD_BIT_SCL
	}
};

int
tsciic_match(struct device *parent, void *match, void *aux)
{
	struct tsp_attach_args *tsp = (struct tsp_attach_args *)aux;

	return strcmp(tsp->tsp_name, tsciic_cd.cd_name) == 0;
}

void
tsciic_attach(struct device *parent, struct device *self, void *aux)
{
	struct tsciic_softc *sc = (struct tsciic_softc *)self;
	struct i2cbus_attach_args iba;

	printf("\n");

	rw_init(&sc->sc_i2c_lock, "tsciiclk");
	sc->sc_i2c_tag.ic_cookie = sc;
	sc->sc_i2c_tag.ic_acquire_bus = tsciic_acquire_bus;
	sc->sc_i2c_tag.ic_release_bus = tsciic_release_bus;
	sc->sc_i2c_tag.ic_send_start = tsciic_send_start;
	sc->sc_i2c_tag.ic_send_stop = tsciic_send_stop;
	sc->sc_i2c_tag.ic_initiate_xfer = tsciic_initiate_xfer;
	sc->sc_i2c_tag.ic_read_byte = tsciic_read_byte;
	sc->sc_i2c_tag.ic_write_byte = tsciic_write_byte;

	memset(&iba, 0, sizeof iba);
	iba.iba_name = "iic";
	iba.iba_tag = &sc->sc_i2c_tag;
	config_found(self, &iba, iicbus_print);
}

int
tsciic_acquire_bus(void *cookie, int flags)
{
	struct tsciic_softc *sc = cookie;

	if (cold || (flags & I2C_F_POLL))
		return 0;

	return rw_enter(&sc->sc_i2c_lock, RW_WRITE | RW_INTR);
}

void
tsciic_release_bus(void *cookie, int flags)
{
	struct tsciic_softc *sc = cookie;

	if (cold || (flags & I2C_F_POLL))
		return;

	rw_exit(&sc->sc_i2c_lock);
}

int
tsciic_send_start(void *cookie, int flags)
{
	return i2c_bitbang_send_start(cookie, flags, &tsciicbb_ops);
}

int
tsciic_send_stop(void *cookie, int flags)
{
	return i2c_bitbang_send_stop(cookie, flags, &tsciicbb_ops);
}

int
tsciic_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
{
	return i2c_bitbang_initiate_xfer(cookie, addr, flags, &tsciicbb_ops);
}

int
tsciic_read_byte(void *cookie, uint8_t *valp, int flags)
{
	return i2c_bitbang_read_byte(cookie, valp, flags, &tsciicbb_ops);
}

int
tsciic_write_byte(void *cookie, uint8_t val, int flags)
{
	return i2c_bitbang_write_byte(cookie, val, flags, &tsciicbb_ops);
}

/* I2C bitbanging */
void
tsciicbb_set_bits(void *cookie, uint32_t bits)
{
	uint64_t val;

	val = (bits & MPD_BIT_SDA ? MPD_DS : 0) |
	    (bits & MPD_BIT_SCL ? MPD_CKS : 0);
	alpha_mb();
	STQP(TS_C_MPD) = val;
	alpha_mb();
}

void
tsciicbb_set_dir(void *cookie, uint32_t dir)
{
	/* Nothing to do */
}

uint32_t
tsciicbb_read(void *cookie)
{
	uint64_t val;
	uint32_t bits;

	val = LDQP(TS_C_MPD);
	bits = (val & MPD_DR ? MPD_BIT_SDA : 0) |
	    (val & MPD_CKR ? MPD_BIT_SCL : 0);
	return bits;
}

license: unknown


code:
/* $OpenBSD: tsp_bus_io.c,v 1.3 2003/09/02 17:25:21 miod Exp $ */
/* $NetBSD: tsp_bus_io.c,v 1.4 2000/06/26 19:46:24 thorpej Exp $ */

/*-
 * Copyright (c) 1999 by Ross Harvey.  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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ross Harvey.
 * 4. The name of Ross Harvey may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY 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 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/systm.h>
#include <sys/malloc.h>
#include <sys/syslog.h>
#include <sys/device.h>

#include <uvm/uvm_extern.h>

#include <machine/bus.h>

#include <alpha/pci/tsreg.h>
#include <alpha/pci/tsvar.h>

#define	CHIP	tsp

typedef struct tsp_config *TSPCON;

#define	CHIP_EX_MALLOC_SAFE(v)  (((TSPCON)(v))->pc_mallocsafe)
#define CHIP_IO_EXTENT(v)       (((TSPCON)(v))->pc_io_ex)

#define CHIP_IO_SYS_START(v)    (((TSPCON)(v))->pc_iobase | P_PCI_IO)

/*
 * Tsunami core logic appears on EV6.  We require at least EV56
 * support for the assembler to emit BWX opcodes.
 */
__asm(".arch ev6");

#define	CHIP_EXTENT_NAME(v)	((struct tsp_config *)(v))->pc_io_ex_name
#define	CHIP_EXTENT_STORAGE(v)	((struct tsp_config *)(v))->pc_io_ex_storage

#include <alpha/pci/pci_bwx_bus_io_chipdep.c>


code:
/* $OpenBSD: tsp_bus_mem.c,v 1.5 2014/05/08 20:46:49 miod Exp $ */
/* $NetBSD: tsp_bus_mem.c,v 1.4 2000/06/26 19:46:25 thorpej Exp $ */

/*-
 * Copyright (c) 1999 by Ross Harvey.  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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ross Harvey.
 * 4. The name of Ross Harvey may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY 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 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/systm.h>
#include <sys/malloc.h>
#include <sys/syslog.h>
#include <sys/device.h>

#include <uvm/uvm_extern.h>

#include <machine/bus.h>
#include <machine/autoconf.h>
#include <machine/rpb.h>

#include <alpha/pci/tsreg.h>
#include <alpha/pci/tsvar.h>

#define tsp_bus_mem() { Generate ctags(1) key. }

#define	CHIP	tsp

#define	CHIP_EX_MALLOC_SAFE(v)  (((struct tsp_config *)(v))->pc_mallocsafe)
#define CHIP_MEM_EXTENT(v)       (((struct tsp_config *)(v))->pc_mem_ex)

#define CHIP_MEM_SYS_START(v)    (((struct tsp_config *)(v))->pc_iobase)

/*
 * Tsunami core logic appears on EV6.  We require at least EV56
 * support for the assembler to emit BWX opcodes.
 */
__asm(".arch ev6");

#define	CHIP_EXTENT_NAME(v)	((struct tsp_config *)(v))->pc_mem_ex_name
#define	CHIP_EXTENT_STORAGE(v)	((struct tsp_config *)(v))->pc_mem_ex_storage

#include <alpha/pci/pci_bwx_bus_mem_chipdep.c>

void
tsp_bus_mem_init2(void *v)
{
	struct tsp_config *pcp = v;
	struct ts_pchip *pccsr = pcp->pc_csr;
	int i, error;

	/*
	 * Allocate the DMA windows out of the extent map.
	 */
	for (i = 0; i < 4; i++) {
		alpha_mb();
		if ((pccsr->tsp_wsba[i].tsg_r & WSBA_ENA) == 0) {
			/* Window not in use. */
			continue;
		}

		error = extent_alloc_region(CHIP_MEM_EXTENT(v),
		    WSBA_ADDR(pccsr->tsp_wsba[i].tsg_r),
		    WSM_LEN(pccsr->tsp_wsm[i].tsg_r),
		    EX_NOWAIT | (CHIP_EX_MALLOC_SAFE(v) ? EX_MALLOCOK : 0));
		if (error) {
			printf("WARNING: unable to reserve DMA window "
			    "0x%llx - 0x%llx\n",
			    WSBA_ADDR(pccsr->tsp_wsba[i].tsg_r),
			    WSBA_ADDR(pccsr->tsp_wsba[i].tsg_r) +
			    (WSM_LEN(pccsr->tsp_wsm[i].tsg_r) - 1));
		}
	}
}


code:
/* $OpenBSD: tsp_dma.c,v 1.12 2015/01/03 20:44:23 miod Exp $ */
/* $NetBSD: tsp_dma.c,v 1.1 1999/06/29 06:46:47 ross Exp $ */

/*-
 * Copyright (c) 1999 by Ross Harvey.  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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ross Harvey.
 * 4. The name of Ross Harvey may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY 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 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 (c) 1997, 1998 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
 * NASA Ames Research Center.
 *
 * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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 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.
 */

#define _ALPHA_BUS_DMA_PRIVATE

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/malloc.h>

#include <uvm/uvm_extern.h>

#include <machine/autoconf.h>

#include <machine/bus.h>
#include <machine/rpb.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <alpha/pci/tsreg.h>
#include <alpha/pci/tsvar.h>

#define tsp_dma() { Generate ctags(1) key. }

#define	EDIFF(a, b) (((a) | WSBA_ENA | WSBA_SG)	!= ((b) | WSBA_ENA | WSBA_SG))

bus_dma_tag_t tsp_dma_get_tag(bus_dma_tag_t, alpha_bus_t);
int	tsp_bus_dmamap_load_sgmap(bus_dma_tag_t, bus_dmamap_t, void *,
	    bus_size_t, struct proc *, int);
int	tsp_bus_dmamap_load_mbuf_sgmap(bus_dma_tag_t, bus_dmamap_t,
	    struct mbuf *, int);
int	tsp_bus_dmamap_load_uio_sgmap(bus_dma_tag_t, bus_dmamap_t,
	    struct uio *, int);
int	tsp_bus_dmamap_load_raw_sgmap(bus_dma_tag_t, bus_dmamap_t,
	    bus_dma_segment_t *, int, bus_size_t, int);
void	tsp_bus_dmamap_unload_sgmap(bus_dma_tag_t, bus_dmamap_t);
void	tsp_tlb_invalidate(struct tsp_config *);

void
tsp_dma_init(struct device *tsp, struct tsp_config *pcp)
{
	int i;
	bus_dma_tag_t t;
	struct ts_pchip *pccsr = pcp->pc_csr;
	bus_addr_t dwbase, dwlen, sgwbase, sgwlen, tbase;
	static const struct map_expected {
		u_int32_t base, mask, physbase, enables;
	} premap[4] = {
		/* 0-8MB at 8MB with S/G DMA (isa space) */
		{ 0x00800000, 0x00700000, 0x00000000, WSBA_ENA | WSBA_SG },
		/* 0-1GB at 2GB direct */
		{ 0x80000000, 0x3ff00000, 0x00000000, WSBA_ENA           },
		{ 0, 0, 0 },
		{ 0, 0, 0 }
	};

	alpha_mb();
	for(i = 0; i < 4; ++i) {
#ifdef DEBUG
		if (EDIFF(pccsr->tsp_wsba[i].tsg_r, premap[i].base) ||
		    EDIFF(pccsr->tsp_wsm[i].tsg_r, premap[i].mask) ||
		    ((premap[i].enables & WSBA_SG) == 0 &&
		      pccsr->tsp_tba[i].tsg_r != premap[i].physbase))
			printf("%s: window %d: %lx/base %lx/mask %lx"
			    " reinitialized\n",
			    tsp->dv_xname, i,
			    pccsr->tsp_wsba[i].tsg_r,
			    pccsr->tsp_wsm[i].tsg_r,
			    pccsr->tsp_tba[i].tsg_r);
#endif
		pccsr->tsp_wsba[i].tsg_r = premap[i].base | premap[i].enables;
		pccsr->tsp_wsm[i].tsg_r = premap[i].mask;
		if ((premap[i].enables & WSBA_SG) == 0)
			pccsr->tsp_tba[i].tsg_r = premap[i].physbase;
	}
	alpha_mb();

	/*
	 * Initialize the DMA tags used for direct-mapped DMA.
	 */
	t = &pcp->pc_dmat_direct;
	t->_cookie = pcp;
	t->_wbase = dwbase = WSBA_ADDR(pccsr->tsp_wsba[1].tsg_r);
	t->_wsize = dwlen = WSM_LEN(pccsr->tsp_wsm[1].tsg_r);
	t->_next_window = &pcp->pc_dmat_sgmap;
	t->_boundary = 0;
	t->_sgmap = NULL;
	t->_get_tag = tsp_dma_get_tag;
	/*
	 * Since we fall back to sgmap if the direct mapping fails,
	 * we need to set up for sgmap in any case.
	 */
	t->_dmamap_create = alpha_sgmap_dmamap_create;
	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
	t->_dmamap_load = _bus_dmamap_load_direct;
	t->_dmamap_load_mbuf = _bus_dmamap_load_mbuf_direct;
	t->_dmamap_load_uio = _bus_dmamap_load_uio_direct;
	t->_dmamap_load_raw = _bus_dmamap_load_raw_direct;
	t->_dmamap_unload = _bus_dmamap_unload;
	t->_dmamap_sync = _bus_dmamap_sync;

	t->_dmamem_alloc = _bus_dmamem_alloc;
	t->_dmamem_free = _bus_dmamem_free;
	t->_dmamem_map = _bus_dmamem_map;
	t->_dmamem_unmap = _bus_dmamem_unmap;
	t->_dmamem_mmap = _bus_dmamem_mmap;

	/*
	 * Initialize the DMA tag used for sgmap-mapped DMA.
	 */
	t = &pcp->pc_dmat_sgmap;
	t->_cookie = pcp;
	t->_wbase = sgwbase = WSBA_ADDR(pccsr->tsp_wsba[0].tsg_r);
	t->_wsize = sgwlen = WSM_LEN(pccsr->tsp_wsm[0].tsg_r);
	t->_next_window = NULL;
	t->_boundary = 0;
	t->_sgmap = &pcp->pc_sgmap;
	t->_get_tag = tsp_dma_get_tag;
	t->_dmamap_create = alpha_sgmap_dmamap_create;
	t->_dmamap_destroy = alpha_sgmap_dmamap_destroy;
	t->_dmamap_load = tsp_bus_dmamap_load_sgmap;
	t->_dmamap_load_mbuf = tsp_bus_dmamap_load_mbuf_sgmap;
	t->_dmamap_load_uio = tsp_bus_dmamap_load_uio_sgmap;
	t->_dmamap_load_raw = tsp_bus_dmamap_load_raw_sgmap;
	t->_dmamap_unload = tsp_bus_dmamap_unload_sgmap;
	t->_dmamap_sync = _bus_dmamap_sync;

	t->_dmamem_alloc = _bus_dmamem_alloc;
	t->_dmamem_free = _bus_dmamem_free;
	t->_dmamem_map = _bus_dmamem_map;
	t->_dmamem_unmap = _bus_dmamem_unmap;
	t->_dmamem_mmap = _bus_dmamem_mmap;

	/*
	 * Initialize the SGMAP.  Align page table to 32k in case
	 * window is somewhat larger than expected.
	 */
	alpha_sgmap_init(t, &pcp->pc_sgmap, "tsp_sgmap",
	    sgwbase, 0, sgwlen, sizeof(u_int64_t), NULL, (32*1024));

	/*
	 * Check windows for sanity, especially if we later decide to
	 * use the firmware's initialization in some cases.
	 */
	if ((sgwbase <= dwbase && dwbase < sgwbase + sgwlen) ||
	    (dwbase <= sgwbase && sgwbase < dwbase + dwlen))
		panic("%s: overlap sg 0x%lx len 0x%lx d 0x%lx len 0x%lx",
		    __func__, sgwbase, sgwlen, dwbase, dwlen);

	tbase = pcp->pc_sgmap.aps_ptpa;
	if (tbase & ~0x7fffffc00UL)
		panic("tsp_dma_init: bad page table address");
	alpha_mb();
	pccsr->tsp_tba[0].tsg_r = tbase;
	alpha_mb();

	tsp_tlb_invalidate(pcp);
	alpha_mb();

	/* XXX XXX BEGIN XXX XXX */
	{							/* XXX */
		extern paddr_t alpha_XXX_dmamap_or;		/* XXX */
		alpha_XXX_dmamap_or = dwbase;			/* XXX */
	}							/* XXX */
	/* XXX XXX END XXX XXX */
}

/*
 * Return the bus dma tag to be used for the specified bus type.
 * INTERNAL USE ONLY!
 */
bus_dma_tag_t
tsp_dma_get_tag(t, bustype)
	bus_dma_tag_t t;
	alpha_bus_t bustype;
{
	struct tsp_config *pcp = t->_cookie;

	switch (bustype) {
	case ALPHA_BUS_PCI:
	case ALPHA_BUS_EISA:
		/*
		 * The direct mapped window will work for most systems,
		 * most of the time. When it doesn't, we chain to the sgmap
		 * window automatically.
		 */
		return (&pcp->pc_dmat_direct);

	case ALPHA_BUS_ISA:
		/*
		 * ISA doesn't have enough address bits to use
		 * the direct-mapped DMA window, so we must use
		 * SGMAPs.
		 */
		return (&pcp->pc_dmat_sgmap);

	default:
		panic("tsp_dma_get_tag: shouldn't be here, really...");
	}
}

/*
 * Load a TSP SGMAP-mapped DMA map with a linear buffer.
 */
int
tsp_bus_dmamap_load_sgmap(t, map, buf, buflen, p, flags)
	bus_dma_tag_t t;
	bus_dmamap_t map;
	void *buf;
	bus_size_t buflen;
	struct proc *p;
	int flags;
{
	int error;

	error = pci_sgmap_pte64_load(t, map, buf, buflen, p, flags,
	    t->_sgmap);
	if (error == 0)
		tsp_tlb_invalidate(t->_cookie);

	return (error);
}

/*
 * Load a TSP SGMAP-mapped DMA map with an mbuf chain.
 */
int
tsp_bus_dmamap_load_mbuf_sgmap(t, map, m, flags)
	bus_dma_tag_t t;
	bus_dmamap_t map;
	struct mbuf *m;
	int flags;
{
	int error;

	error = pci_sgmap_pte64_load_mbuf(t, map, m, flags, t->_sgmap);
	if (error == 0)
		tsp_tlb_invalidate(t->_cookie);

	return (error);
}

/*
 * Load a TSP SGMAP-mapped DMA map with a uio.
 */
int
tsp_bus_dmamap_load_uio_sgmap(t, map, uio, flags)
	bus_dma_tag_t t;
	bus_dmamap_t map;
	struct uio *uio;
	int flags;
{
	int error;

	error = pci_sgmap_pte64_load_uio(t, map, uio, flags, t->_sgmap);
	if (error == 0)
		tsp_tlb_invalidate(t->_cookie);

	return (error);
}

/*
 * Load a TSP SGMAP-mapped DMA map with raw memory.
 */
int
tsp_bus_dmamap_load_raw_sgmap(t, map, segs, nsegs, size, flags)
	bus_dma_tag_t t;
	bus_dmamap_t map;
	bus_dma_segment_t *segs;
	int nsegs;
	bus_size_t size;
	int flags;
{
	int error;

	error = pci_sgmap_pte64_load_raw(t, map, segs, nsegs, size, flags,
	    t->_sgmap);
	if (error == 0)
		tsp_tlb_invalidate(t->_cookie);

	return (error);
}

/*
 * Unload a TSP DMA map.
 */
void
tsp_bus_dmamap_unload_sgmap(t, map)
	bus_dma_tag_t t;
	bus_dmamap_t map;
{

	/*
	 * Invalidate any SGMAP page table entries used by this
	 * mapping.
	 */
	pci_sgmap_pte64_unload(t, map, t->_sgmap);
	tsp_tlb_invalidate(t->_cookie);

	/*
	 * Do the generic bits of the unload.
	 */
	_bus_dmamap_unload(t, map);
}

/*
 * Flush the TSP scatter/gather TLB.
 */
void
tsp_tlb_invalidate(pcp)
	struct tsp_config *pcp;
{

	alpha_mb();
	*pcp->pc_tlbia = 0;
	alpha_mb();
}


code:
/* $OpenBSD: tsp_pci.c,v 1.4 2010/12/04 17:06:31 miod Exp $ */
/* $NetBSD: tsp_pci.c,v 1.1 1999/06/29 06:46:47 ross Exp $ */

/*-
 * Copyright (c) 1999 by Ross Harvey.  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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ross Harvey.
 * 4. The name of Ross Harvey may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY 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 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/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>

#include <uvm/uvm_extern.h>

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

#include <machine/autoconf.h>
#include <machine/rpb.h>

#include <alpha/pci/tsreg.h>
#include <alpha/pci/tsvar.h>

#define tsp_pci() { Generate ctags(1) key. }

void		tsp_attach_hook(struct device *, struct device *,
		    struct pcibus_attach_args *);
int		tsp_bus_maxdevs(void *, int);
pcitag_t	tsp_make_tag(void *, int, int, int);
void		tsp_decompose_tag(void *, pcitag_t, int *, int *,
		    int *);
int		tsp_conf_size(void *, pcitag_t);
pcireg_t	tsp_conf_read(void *, pcitag_t, int);
void		tsp_conf_write(void *, pcitag_t, int, pcireg_t);

void
tsp_pci_init(pc, v)
	pci_chipset_tag_t pc;
	void *v;
{
	pc->pc_conf_v = v;
	pc->pc_attach_hook = tsp_attach_hook;
	pc->pc_bus_maxdevs = tsp_bus_maxdevs;
	pc->pc_make_tag = tsp_make_tag;
	pc->pc_decompose_tag = tsp_decompose_tag;
	pc->pc_conf_size = tsp_conf_size;
	pc->pc_conf_read = tsp_conf_read;
	pc->pc_conf_write = tsp_conf_write;
}

void
tsp_attach_hook(parent, self, pba)
	struct device *parent, *self;
	struct pcibus_attach_args *pba;
{
}

int
tsp_bus_maxdevs(cpv, busno)
	void *cpv;
	int busno;
{
	return 32;
}

pcitag_t
tsp_make_tag(cpv, b, d, f)
	void *cpv;
	int b, d, f;
{
	return b << 16 | d << 11 | f << 8;
}

void
tsp_decompose_tag(cpv, tag, bp, dp, fp)
	void *cpv;
	pcitag_t tag;
	int *bp, *dp, *fp;
{
	if (bp != NULL)
		*bp = (tag >> 16) & 0xff;
	if (dp != NULL)
		*dp = (tag >> 11) & 0x1f;
	if (fp != NULL)
		*fp = (tag >> 8) & 0x7;
}

int
tsp_conf_size(void *cpv, pcitag_t tag)
{
	return PCI_CONFIG_SPACE_SIZE;
}

/*
 * Tsunami makes this a lot easier than it used to be, automatically
 * generating type 0 or type 1 cycles, and quietly returning -1 with
 * no errors on unanswered probes.
 */
pcireg_t
tsp_conf_read(cpv, tag, offset)
	void *cpv;
	pcitag_t tag;
	int offset;
{
	pcireg_t *datap, data;
	struct tsp_config *pcp = cpv;

	datap = S_PAGE(pcp->pc_iobase | P_PCI_CONFIG | tag | (offset & ~3));
	alpha_mb();
	data = *datap;
	alpha_mb();
	return data;
}

void
tsp_conf_write(cpv, tag, offset, data)
	void *cpv;
	pcitag_t tag;
	int offset;
	pcireg_t data;
{
	pcireg_t *datap;
	struct tsp_config *pcp = cpv;

	datap = S_PAGE(pcp->pc_iobase | P_PCI_CONFIG | tag | (offset & ~3));
	alpha_mb();
	*datap = data;
	alpha_mb();
}


code:
/* $OpenBSD: tsreg.h,v 1.6 2014/12/24 18:46:14 miod Exp $ */
/* $NetBSD: tsreg.h,v 1.1 1999/06/29 06:46:47 ross Exp $ */

/*-
 * Copyright (c) 1999 by Ross Harvey.  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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ross Harvey.
 * 4. The name of Ross Harvey may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY 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 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.
 *
 */

/*
 * 21272 Core Logic registers and constants.
 */

#define	tsreg() { Generate ctags(1) key. }

/*
 * Superpage pointer from physical address.
 */
#define	S_PAGE(phys) ((void *)ALPHA_PHYS_TO_K0SEG(phys))

/*
 * {LD,ST}QP: LoaD and STore Quad Physical
 */
#define	LDQP(a)	(*(volatile long *)ALPHA_PHYS_TO_K0SEG(a))
#define	STQP(a) LDQP(a)

/*
 * Define extraction functions for bit fields via length and left,right bitno
 */
#define	TSFIELD(r,offs,len) (((r) >> (offs)) & (~0UL >> (64 - (len))))
#define	TSFIELDBB(r,lb,rb) TSFIELD((r), (rb), (lb) - (rb) + 1)

/*
 * EV6 has a new superpage which can pass through 44 address bits.  (Umm, a
 * superduperpage?) But, the firmware doesn't turn it on, so we use the old
 * one and let the HW sign extend va/pa<40> to get us the pa<43> that makes
 * the needed I/O space access. This is just as well; it means we don't have
 * to worry about which GENERIC code might get called on other CPU models.
 *
 *	E.g., we want this:	      0x0801##a000##0000
 *	We use this:		      0x0101##a000##0000
 *	...mix in the old SP:	0xffff##fc00##0000##0000
 *	...after PA sign ext:	0xffff##ff00##a000##0000
 *	(PA<42:41> ignored)
 */

/*
 * This hack allows us to map the I/O address space without using
 * the KSEG sign extension hack.
 */
#define	TS_PHYSADDR(x)							\
	(((x) & ~0x0100##0000##0000) | 0x0800##0000##0000)

/*
 * Cchip CSR Map
 */

#define TS_C_CSC	0x101##a000##0000UL	/* Cchip System Configuration */

#	define	CSC_P1P		(1L << 14)
#	define	CSC_BC(r)	TSFIELD((r), 0, 2)
#	define	CSC_AW		(1L << 8)

#define TS_C_MTR	0x101##a000##0040UL

#define TS_C_MISC	0x101##a000##0080UL	/* Miscellaneous Register */

#	define	MISC_REV(r)	TSFIELD((r), 39, 8)

#define TS_C_MPD	0x101##a000##00c0UL

#	define	MPD_DR	0x08	/* RO: Data receive */
#	define	MPD_CKR	0x04	/* RO: Clock receive */
#	define	MPD_DS	0x02	/* WO: Data send - Must be a 1 to receive */
#	define	MPD_CKS	0x01	/* WO: Clock send */

#define TS_C_AAR0	0x101##a000##0100UL
#define TS_C_AAR1	0x101##a000##0140UL
#define TS_C_AAR2	0x101##a000##0180UL
#define TS_C_AAR3	0x101##a000##01c0UL

#	define	AAR_ASIZ(r)	TSFIELD((r), 12, 4)
#	define	AAR_SPLIT	(1L << 8)

#define TS_C_DIM0	0x101##a000##0200UL
#define TS_C_DIM1	0x101##a000##0240UL
#define TS_C_DIR0	0x101##a000##0280UL
#define TS_C_DIR1	0x101##a000##02c0UL
#define TS_C_DRIR	0x101##a000##0300UL
#define TS_C_PRBEN	0x101##a000##0340UL
#define TS_C_IIC0	0x101##a000##0380UL
#define TS_C_IIC1	0x101##a000##03c0UL
#define TS_C_MPR0	0x101##a000##0400UL
#define TS_C_MPR1	0x101##a000##0440UL
#define TS_C_MPR2	0x101##a000##0480UL
#define TS_C_MPR3	0x101##a000##04c0UL
#define TS_C_MCTL	0x101##a000##0500UL

#define TS_C_TTR	0x101##a000##0580UL
#define TS_C_TDR	0x101##a000##05c0UL

/*
 * Dchip CSR Map
 */

#define TS_D_DSC	0x101##b000##0800UL
#define TS_D_STR	0x101##b000##0840UL
#define TS_D_DREV	0x101##b000##0880UL
#define TS_D_DSC2	0x101##b000##08c0UL

/*
 * Pchip CSR Offsets
 */

#define P_WSBA0		0x0000
#define P_WSBA1		0x0040
#define P_WSBA2		0x0080
#define P_WSBA3		0x00c0

#	define	WSBA_ADDR(r) (TSFIELDBB((r), 31, 20) << 20)
#	define	WSBA_SG	     2
#	define	WSBA_ENA     1

#define P_WSM0		0x0100
#define P_WSM1		0x0140
#define P_WSM2		0x0180
#define P_WSM3		0x01c0

#	define	WSM_AM(r)    TSFIELDBB((r), 31, 20)
#	define	WSM_LEN(r)   ((WSM_AM(r) + 1) << 20)

#define P_TBA0		0x0200
#define P_TBA1		0x0240
#define P_TBA2		0x0280
#define P_TBA3		0x02c0

#define P_PCTL		0x0300
#define P_PLAT		0x0340
	/* reserved	0x0380 */
#define P_PERROR	0x03c0

#define P_PERRMASK	0x0400
#define P_PERRSET	0x0440
#define P_TLBIV		0x0480
#define P_TLBIA		0x04c0

#define P_PMONCTL	0x0500
#define P_PMONCNT	0x0540

#define P_SPRST		0x0800

#define	TS_STEP		0x40

/*
 * Pchip I/O offsets
 */

#define	P_CSRBASE	 0x001##8000##0000UL
#define	P_PCI_MEM	 0x800##0000##0000UL
#define	P_PCI_IO	 0x001##fc00##0000UL
#define	P_PCI_CONFIG	 0x001##fe00##0000UL

/*
 * Construct EV6 I/O Space Address for Pchip 0 and Pchip 1.
 */

#define	TS_P0(offs)	(0x100##0000##0000UL + (offs))
#define	TS_P1(offs)	(0x102##0000##0000UL + (offs))
#define	TS_Pn(n, offs)	(0x100##0000##0000UL + 0x2##0000##0000UL * (n) + (offs))

/*
 * Tsunami Generic Register Type
 */
typedef struct _ts_gr {
	volatile u_int64_t tsg_r;
	long	tsg_deadspace[7];
} TS_GR;

/*
 * Tsunami Pchip
 */

struct	ts_pport {
	TS_GR	tsp_resA;
	TS_GR	tsp_error;	/* Pchip Error */

	TS_GR	tsp_perrmask;	/* Pchip Error Mask */
	TS_GR	tsp_perrset;	/* Pchip Error Set */
	TS_GR	tsp_tlbiv;	/* Translation Buffer Invalidate Virtual */
	TS_GR	tsp_tlbia;	/* Translation Buffer Invalidate All */

	TS_GR	tsp_pmonctl;	/* PChip Monitor Control */
	TS_GR	tsp_pmoncnt;	/* PChip Monitor Counters */
	TS_GR	tsp_resB;
	TS_GR	tsp_resC;

	TS_GR	tsp_resD_K[8];
};

struct	ts_gport {
	TS_GR	tsp_resA[2];
	TS_GR	tsp_serror;
	TS_GR	tsp_serrmask;
	TS_GR	tsp_serrset;
	TS_GR	tsp_resB;
	TS_GR	tsp_gperrmask;
	TS_GR	tsp_gperren;
	TS_GR	tsp_gperrset;
	TS_GR	tsp_resC;
	TS_GR	tsp_tlbiv;
	TS_GR	tsp_tlbia;
	TS_GR	tsp_resD[2];
	TS_GR	tsp_sctl;
	TS_GR	tsp_resE[3];
};

struct	ts_aport {
	TS_GR	tsp_resA[2];
	TS_GR	tsp_agperror;
	TS_GR	tsp_agperrmask;
	TS_GR	tsp_agperrset;
	TS_GR	tsp_agplastwr;
	TS_GR	tsp_aperror;
	TS_GR	tsp_aperrmask;
	TS_GR	tsp_aperrset;
	TS_GR	tsp_resB;
	TS_GR	tsp_tlbiv;
	TS_GR	tsp_tlbia;
	TS_GR	tsp_resC[6];
};

struct	ts_pchip {
	TS_GR	tsp_wsba[4];	/* Window Space Base Address */

	TS_GR	tsp_wsm[4];	/* Window Space Mask */

	TS_GR	tsp_tba[4];	/* Translated Base Address */

	TS_GR	tsp_pctl;	/* Pchip Control */
	TS_GR	tsp_plat;	/* Pchip Latency */

	union {
		struct ts_pport	p;
		struct ts_gport	g;
		struct ts_aport	a;
	} port;

	TS_GR	tsp_sprts;	/* ??? */
	TS_GR	tsp_res[31];
};


code:
/* $OpenBSD: tsvar.h,v 1.7 2009/10/02 18:01:47 miod Exp $ */
/* $NetBSD: tsvar.h,v 1.1 1999/06/29 06:46:47 ross Exp $ */

/*-
 * Copyright (c) 1999 by Ross Harvey.  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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Ross Harvey.
 * 4. The name of Ross Harvey may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ROSS HARVEY ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURP0SE
 * ARE DISCLAIMED.  IN NO EVENT SHALL ROSS HARVEY 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 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 <dev/isa/isavar.h>
#include <dev/pci/pcivar.h>
#include <alpha/pci/pci_sgmap_pte64.h>

#define	tsvar() { Generate ctags(1) key. }

struct tsp_config {
	int	pc_pslot;		/* Pchip 0 or 1 */
	int	pc_initted;		/* Initialized */
	u_int64_t pc_iobase;		/* All Pchip space starts here */
	struct	ts_pchip *pc_csr;	/* Pchip CSR space starts here */
	volatile u_int64_t *pc_tlbia;	/* Pchip TLBIA register address */

	struct	alpha_bus_space pc_iot, pc_memt;
	struct	alpha_pci_chipset pc_pc;

	struct	alpha_bus_dma_tag pc_dmat_direct;
	struct	alpha_bus_dma_tag pc_dmat_sgmap;

	struct alpha_sgmap pc_sgmap;

	u_int32_t pc_hae_mem;
	u_int32_t pc_hae_io;

	struct	extent *pc_io_ex, *pc_mem_ex;
	char	pc_io_ex_name[16], pc_mem_ex_name[16];
	long	pc_io_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8) / sizeof(long)];
	long	pc_mem_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8) / sizeof(long)];
	int	pc_mallocsafe;
};

struct tsp_softc {
	struct	device sc_dev;
	struct	tsp_config *sc_ccp;
};

struct tsp_attach_args {
	char	*tsp_name;
	int	tsp_slot;
};

extern int tsp_console_hose;

struct	tsp_config *tsp_init(int, int);
void	tsp_pci_init(pci_chipset_tag_t, void *);
void	tsp_dma_init(struct device *, struct tsp_config *);

void	tsp_bus_io_init(bus_space_tag_t, void *);
void	tsp_bus_mem_init(bus_space_tag_t, void *);

void tsp_bus_mem_init2(void *);


code:
/Makefile/1.9/Sun Nov 25 18:25:29 2007//TOPENBSD_5_8_BASE
/Makefile.inc/1.15/Sun Apr 13 08:35:27 2014//TOPENBSD_5_8_BASE
/OSFpal.c/1.8/Sun Dec 22 18:31:29 2013//TOPENBSD_5_8_BASE
/bbinfo.h/1.5/Mon May  5 06:01:45 1997//TOPENBSD_5_8_BASE
/bootxx.c/1.10/Mon Jul  5 19:59:17 2004//TOPENBSD_5_8_BASE
/headersize.c/1.10/Tue Oct 15 05:17:31 2013//TOPENBSD_5_8_BASE
/installboot.8/1.11/Sat Mar  6 16:16:42 2010//TOPENBSD_5_8_BASE
/installboot.c/1.19/Sat Jul 12 19:01:49 2014//TOPENBSD_5_8_BASE
/loadfile_subr.c/1.3/Thu Mar 21 21:50:59 2013//TOPENBSD_5_8_BASE
/prom.c/1.8/Fri May 14 21:08:28 2010//TOPENBSD_5_8_BASE
/prom_disp.S/1.4/Wed Oct 30 22:40:34 1996//TOPENBSD_5_8_BASE
/puts.c/1.3/Wed Oct 30 22:40:35 1996//TOPENBSD_5_8_BASE
/start.S/1.6/Sun Jun  5 21:49:36 2011//TOPENBSD_5_8_BASE
D/boot////
D/bootxx////
D/installboot////
D/libsa////
D/libz////
D/netboot////
D/setnetbootinfo////

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


code: src/sys/arch/alpha/stand

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


code:
#	$OpenBSD: Makefile,v 1.9 2007/11/25 18:25:29 deraadt Exp $
#	$NetBSD: Makefile,v 1.7 1997/04/10 23:03:38 cgd Exp $

.if ${MACHINE} == "alpha"
SUBDIR=	boot bootxx netboot libsa libz
.endif

SUBDIR+= setnetbootinfo installboot

.include <bsd.subdir.mk>

path: sys/arch/alpha/stand/Makefile


code:
#	$OpenBSD: Makefile.inc,v 1.15 2014/04/13 08:35:27 brad Exp $
#	$NetBSD: Makefile.inc,v 1.8 1997/04/06 08:39:38 cgd Exp $

BINDIR=			/usr/mdec

# For descriptions of regions available to bootstrap programs, see
# section 3.4.1.2 (pp. III 3-14 - III 3-18) of the second edition of
# the Alpha AXP Architecture Reference Manual.

# "Region 1 start"
PRIMARY_LOAD_ADDRESS=	20000000
# "Region 1 start" + 32k
SECONDARY_LOAD_ADDRESS=	20004000
# "Region 1 start" + 256k
HEAP_LIMIT=		20040000

CPPFLAGS+=		-DPRIMARY_LOAD_ADDRESS="0x${PRIMARY_LOAD_ADDRESS}"
CPPFLAGS+=		-DSECONDARY_LOAD_ADDRESS="0x${SECONDARY_LOAD_ADDRESS}"

CPPFLAGS+=		-D_STANDALONE

EXTRACFLAGS+=		-DHEAP_LIMIT="0x${HEAP_LIMIT}"

CFLAGS+=		-fno-stack-protector -fno-builtin-printf \
			-fno-builtin-vprintf -fno-builtin-putchar \
			-fno-builtin-puts -fno-builtin-exit


code:
/* $OpenBSD: OSFpal.c,v 1.8 2013/12/22 18:31:29 miod Exp $ */
/* $NetBSD: OSFpal.c,v 1.8 2001/07/22 15:17:30 wiz Exp $ */

/*
 * Copyright (c) 1994, 1996 Carnegie-Mellon University.
 * All rights reserved.
 *
 * Author: Keith Bostic
 *
 * Permission to use, copy, modify and distribute this software and
 * its documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include <sys/types.h>
#include <lib/libsa/stand.h>

#include <machine/prom.h>
#include <machine/rpb.h>
#include <machine/alpha_cpu.h>

paddr_t ptbr_save;

void
OSFpal()
{
	struct rpb *r;
	struct pcs *p;

	r = (struct rpb *)HWRPB_ADDR;
	/*
	 * Note, cpu_number() is a VMS op, can't necessarily call it.
	 * Real fun: PAL_VMS_mfpr_whami == PAL_OSF1_rti...
	 * We might not be rpb_primary_cpu_id, but it is supposed to go
	 * first so the answer should apply to everyone.
	 */
	p = LOCATE_PCS(r, r->rpb_primary_cpu_id);

	printf("VMS PAL rev: 0x%lx\n", p->pcs_palrevisions[PALvar_OpenVMS]);
	printf("OSF PAL rev: 0x%lx\n", p->pcs_palrevisions[PALvar_OSF1]);

	if(p->pcs_pal_type==PAL_TYPE_OSF1) {
		printf("OSF PAL code already running.\n");
		ptbr_save = ((struct alpha_pcb *)p)->apcb_ptbr;
		printf("PTBR is:          0x%lx\n", ptbr_save);
		return;
	}
	switch_palcode();
	memcpy(&p->pcs_pal_rev, &p->pcs_palrevisions[PALvar_OSF1],
	    sizeof(p->pcs_pal_rev));
	printf("Switch to OSF PAL code succeeded.\n");
}


code:
/*	$OpenBSD: bbinfo.h,v 1.5 1997/05/05 06:01:45 millert Exp $	*/
/*	$NetBSD: bbinfo.h,v 1.2 1997/04/06 08:40:57 cgd Exp $	*/

/*
 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
 * All rights reserved.
 *
 * Author: Chris G. Demetriou
 *
 * Permission to use, copy, modify and distribute this software and
 * its documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

struct bbinfoloc {
	u_int64_t	magic1;
	u_int64_t	start;
	u_int64_t	end;
	u_int64_t	pad1[4];
	u_int64_t	magic2;
};

struct bbinfo {
	int32_t		cksum;
	int32_t		nblocks;
	int32_t		bsize;
	int32_t		pad1[8];
	int32_t		blocks[1];
};

struct netbbinfo {
	u_int64_t	magic1;
	u_int8_t	set;
	u_int8_t	ether_addr[6];
	u_int8_t	force;
	u_int64_t	pad1[4];
	u_int64_t	cksum;
	u_int64_t	magic2;
};


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


path: sys/arch/alpha/stand/boot/CVS/Tag


code:
#	$OpenBSD: Makefile,v 1.19 2012/09/01 15:10:06 pascal Exp $
#	$NetBSD: Makefile,v 1.17 1997/04/17 07:27:46 thorpej Exp $

S=	${.CURDIR}/../../../..

.PATH: ${.CURDIR}/..

PROG = boot

SRCS = start.S boot.c disk.c conf.c prom.c prom_disp.S OSFpal.c
SRCS+= devopen.c filesystem.c loadfile_subr.c prom_swpal.S
NOMAN=
INSTALL_STRIP=
BINMODE= 444

BOOT_RELOC = ${SECONDARY_LOAD_ADDRESS}

CLEANFILES+= vers.c vers.o ${PROG}.sym ${PROG}.nosym

.PATH:	${S}/lib/libkern/arch/alpha ${S}/lib/libkern
SRCS+=	__divlu.S __remqu.S __reml.S strlen.c bzero.c __divl.S __divqu.S \
	__remlu.S

.PATH:	${S}/lib/libkern/arch/alpha ${S}/lib/libsa
SRCS+=	loadfile.c

DEFNS= -DCOMPAT_UFS

AFLAGS += -DASSEMBLER ${DEFNS} -fno-pie
CPPFLAGS += -I${.CURDIR}/../../../../lib/libsa -I${.CURDIR}/../.. -I${S} ${DEFNS}
CFLAGS = -Os -Werror -mno-fp-regs -fno-pie

${PROG}.nosym: ${PROG}.sym
	cp ${PROG}.sym ${PROG}.nosym
	strip ${PROG}.nosym

${PROG}: ${PROG}.nosym
	objcopy --output-target=binary ${PROG}.nosym ${PROG}

# no lint here (yet?)
lint:

.include <bsd.prog.mk>

### find out what to use for libz
Z_AS=		library
ZDST=		${.CURDIR}/../libz/${__objdir}
.include "${.CURDIR}/../libz/Makefile.inc"
LIBZ=		${ZLIB}

### find out what to use for libsa
SA_AS=		library
SADST=		${.CURDIR}/../libsa/${__objdir}
SAREL=
SA_ZLIB=	yes
.include "${.CURDIR}/../libsa/Makefile.inc"
LIBSA=		${SALIB}

${PROG}.sym: ${OBJS} ${LIBSA} ${LIBZ}
	sh ${.CURDIR}/newvers.sh ${.CURDIR}/version
	${COMPILE.c} vers.c
	${LD} -nopie -Ttext ${BOOT_RELOC} -N -e start -o ${PROG}.sym \
	    ${OBJS} vers.o ${LIBSA} ${LIBZ} ${LIBSA}
	size ${PROG}.sym

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


code:
/*	$OpenBSD: boot.c,v 1.23 2014/02/19 22:02:14 miod Exp $	*/
/*	$NetBSD: boot.c,v 1.10 1997/01/18 01:58:33 cgd Exp $	*/

/*
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Ralph Campbell.
 *
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 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.
 *
 *	@(#)boot.c	8.1 (Berkeley) 6/10/93
 */

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

#include <sys/param.h>
#include <sys/exec.h>
#include <sys/stat.h>
#define _KERNEL
#include <sys/fcntl.h>
#undef _KERNEL

#include <machine/rpb.h>
#include <machine/prom.h>
#include <machine/autoconf.h>

char boot_file[128];
char boot_flags[128];

extern char bootprog_name[];

struct bootinfo_v1 bootinfo_v1;

paddr_t ptbr_save;

int debug;

char   rnddata[BOOTRANDOM_MAX];

void
loadrandom(char *name, char *buf, size_t buflen)
{
	struct stat sb;
	int fd, i;

	fd = open(name, O_RDONLY);
	if (fd == -1) {
		if (errno != EPERM)
			printf("cannot open %s: %s\n", name, strerror(errno));
		return;
	}
	if (fstat(fd, &sb) == -1 || sb.st_uid != 0 || !S_ISREG(sb.st_mode) ||
	    (sb.st_mode & (S_IWOTH|S_IROTH)))
		goto fail;
	(void) read(fd, buf, buflen);
fail:
	close(fd);
}

int
main()
{
	char *name, **namep;
	u_int64_t entry;
	int rc;
	u_long marks[MARK_MAX];
#ifdef DEBUG
	struct rpb *r;
	struct mddt *mddtp;
	struct mddt_cluster *memc;
	int i;
#endif

	/* Init prom callback vector. */
	init_prom_calls();

	/* print a banner */
	printf("%s\n", bootprog_name);

	/* switch to OSF pal code. */
	OSFpal();

#ifdef DEBUG
	r = (struct rpb *)HWRPB_ADDR;
	mddtp = (struct mddt *)(HWRPB_ADDR + r->rpb_memdat_off);
	printf("%d memory clusters\n", mddtp->mddt_cluster_cnt);
	for (i = 0; i < mddtp->mddt_cluster_cnt; i++) {
		memc = &mddtp->mddt_clusters[i];
		printf("%d: (%d) %lx-%lx\n", i, memc->mddt_usage,
		    memc->mddt_pfn << PAGE_SHIFT,
		    (memc->mddt_pfn + memc->mddt_pg_cnt) << PAGE_SHIFT);
	}
#endif

	loadrandom(BOOTRANDOM, rnddata, sizeof(rnddata));

	prom_getenv(PROM_E_BOOTED_FILE, boot_file, sizeof(boot_file));
	prom_getenv(PROM_E_BOOTED_OSFLAGS, boot_flags, sizeof(boot_flags));

	if (boot_file[0] != '\0') {
		(void)printf("Boot file: %s %s\n", boot_file, boot_flags);
		name = boot_file;
	} else
		name = "bsd";

	(void)printf("Loading %s...\n", name);
	marks[MARK_START] = 0;
	rc = loadfile(name, marks, LOAD_KERNEL | COUNT_KERNEL);
	(void)printf("\n");
	if (rc != 0)
		goto fail;

	/*
	 * Fill in the bootinfo for the kernel.
	 */
	bzero(&bootinfo_v1, sizeof(bootinfo_v1));
	bootinfo_v1.ssym = marks[MARK_SYM];
	bootinfo_v1.esym = marks[MARK_END];
	bcopy(name, bootinfo_v1.booted_kernel,
	    sizeof(bootinfo_v1.booted_kernel));
	bcopy(boot_flags, bootinfo_v1.boot_flags,
	    sizeof(bootinfo_v1.boot_flags));
	bootinfo_v1.hwrpb = (void *)HWRPB_ADDR;
	bootinfo_v1.hwrpbsize = ((struct rpb *)HWRPB_ADDR)->rpb_size;
	bootinfo_v1.cngetc = NULL;
	bootinfo_v1.cnputc = NULL;
	bootinfo_v1.cnpollc = NULL;

	entry = marks[MARK_START];
	(*(void (*)(u_int64_t, u_int64_t, u_int64_t, void *, u_int64_t,
	    u_int64_t))entry)(0, ptbr_save, BOOTINFO_MAGIC, &bootinfo_v1, 1, 0);

fail:
	halt();
}


code:
/*	$OpenBSD: conf.c,v 1.6 2003/06/02 23:27:44 millert Exp $	*/
/*	$NetBSD: conf.c,v 1.3 1995/11/23 02:39:31 cgd Exp $	*/

/*
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Ralph Campbell.
 *
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 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.
 *
 *	@(#)conf.c	8.1 (Berkeley) 6/10/93
 */

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

#include "disk.h"

int	diskopen(struct open_file *, ...);	/* XXX */

int	errno;

struct devsw devsw[] = {
	{ "disk", diskstrategy,	diskopen, diskclose, diskioctl }, /*0*/
};

int	ndevs = (sizeof(devsw)/sizeof(devsw[0]));


code:
/*	$OpenBSD: devopen.c,v 1.8 2003/06/02 23:27:44 millert Exp $	*/
/*	$NetBSD: devopen.c,v 1.1 1995/11/23 02:39:37 cgd Exp $	*/

/*-
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Ralph Campbell.
 *
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 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.
 *
 *	@(#)devopen.c	8.1 (Berkeley) 6/10/93
 */

#include <lib/libsa/stand.h>

/*
 * Decode the string 'fname', open the device and return the remaining
 * file name if any.
 */
devopen(f, fname, file)
	struct open_file *f;
	const char *fname;
	char **file;	/* out */
{
	register char *cp;
	register char *ncp;
	register struct devsw *dp;
	register int c, i;
	int ctlr = 0, unit = 0, part = 0;
	char namebuf[20];
	int rc;

	cp = (char *)fname;
	ncp = namebuf;

	/* look for a string like 'disk(0,0,0)bsd' */
	if (strchr(cp, '(')) {
		while ((c = *cp) != '\0') {
			if (c == '(') {
				cp++;
				break;
			}
			if (ncp < namebuf + sizeof(namebuf) - 1)
				*ncp++ = c;
			cp++;
		}

		/* get controller number */
		if ((c = *cp) >= '0' && c <= '9') {
			ctlr = c - '0';
			c = *++cp;
		}

		if (c == ',') {
			/* get SCSI device number */
			if ((c = *++cp) >= '0' && c <= '9') {
				unit = c - '0';
				c = *++cp;
			}

			if (c == ',') {
				/* get partition number */
				if ((c = *++cp) >= '0' && c <= '9') {
					part = c - '0';
					c = *++cp;
				}
			}
		}
		if (c != ')')
			goto defdev;
		cp++;
		*ncp = '\0';
	} else {
defdev:
		/* No valid device specification */
		cp = (char *)fname;
		ncp = namebuf;
		dp = devsw;
		ctlr = unit = part = 0;
		goto fnd;
	}

	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
		if (dp->dv_name && strcmp(namebuf, dp->dv_name) == 0)
			goto fnd;
	printf("Unknown device '%s'\nKnown devices are:", namebuf);
	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
		if (dp->dv_name)
			printf(" %s", dp->dv_name);
	printf("\n");
	return (ENXIO);

fnd:
	rc = (dp->dv_open)(f, ctlr, unit, part);
	if (rc)
		return (rc);

	f->f_dev = dp;
	if (file && *cp != '\0')
		*file = cp;
	return (0);
}


code:
/*	$OpenBSD: disk.c,v 1.16 2015/07/17 16:13:26 miod Exp $	*/
/*	$NetBSD: disk.c,v 1.6 1997/04/06 08:40:33 cgd Exp $	*/

/*
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Van Jacobson of Lawrence Berkeley Laboratory and Ralph Campbell.
 *
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 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.
 *
 *	@(#)rz.c	8.1 (Berkeley) 6/10/93
 */

#include <lib/libsa/stand.h>

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

#include <machine/rpb.h>
#include <machine/prom.h>

#include "disk.h"

struct	disk_softc {
	int	sc_fd;			/* PROM channel number */
	int	sc_ctlr;		/* controller number */
	int	sc_unit;		/* disk unit number */
	int	sc_part;		/* disk partition number */
	struct	disklabel sc_label;	/* disk label for this disk */
};

int
diskstrategy(void *devdata, int rw, daddr32_t bn, size_t reqcnt, void *addrvoid,
    size_t *cnt)
{
	char *addr = addrvoid;
	struct disk_softc *sc;
	struct partition *pp;
	prom_return_t ret;
	int s;

	if ((reqcnt & 0xffffff) != reqcnt ||
	    reqcnt == 0)
		asm("call_pal 0");

	twiddle();

	/* Partial-block transfers not handled. */
	if (reqcnt & (DEV_BSIZE - 1)) {
		*cnt = 0;
		return (EINVAL);
	}

	sc = (struct disk_softc *)devdata;
	pp = &sc->sc_label.d_partitions[sc->sc_part];

	ret.bits = prom_read(sc->sc_fd, reqcnt, addr, bn + pp->p_offset);
	if (ret.u.status)
		return (EIO);
	*cnt = ret.u.retval;
	return (0);
}

int
diskopen(struct open_file *f, int ctlr, int unit, int part)
{
	struct disklabel *lp;
	prom_return_t ret;
	size_t cnt;
	int devlen, i;
	char *msg, buf[DEV_BSIZE], devname[32];
	struct disk_softc *sc;

	if (unit >= 16 || part >= MAXPARTITIONS)
		return (ENXIO);
	/*
	 * XXX
	 * We don't know what device names look like yet,
	 * so we can't change them.
	 */
	ret.bits = prom_getenv(PROM_E_BOOTED_DEV, devname, sizeof(devname));
	devlen = ret.u.retval;

	ret.bits = prom_open(devname, devlen);
	if (ret.u.status == 2)
		return (ENXIO);
	if (ret.u.status == 3)
		return (EIO);

	sc = alloc(sizeof(struct disk_softc));
	bzero(sc, sizeof(struct disk_softc));
	f->f_devdata = (void *)sc;

	sc->sc_fd = ret.u.retval;
	sc->sc_ctlr = ctlr;
	sc->sc_unit = unit;
	sc->sc_part = part;

	/* Try to read disk label and partition table information. */
	lp = &sc->sc_label;
	lp->d_secsize = DEV_BSIZE;
	lp->d_secpercyl = 1;
	lp->d_npartitions = MAXPARTITIONS;
	DL_SETPOFFSET(&lp->d_partitions[part], 0);
	DL_SETPSIZE(&lp->d_partitions[part], 0x7fffffff);
	i = diskstrategy(sc, F_READ,
	    (daddr32_t)LABELSECTOR, DEV_BSIZE, buf, &cnt);
	if (i || cnt != DEV_BSIZE) {
		printf("disk%d: error reading disk label\n", unit);
		goto bad;
	} else if (((struct disklabel *)(buf + LABELOFFSET))->d_magic !=
		    DISKMAGIC) {
		/* No label at all.  Fake all partitions as whole disk. */
		for (i = 0; i < MAXPARTITIONS; i++) {
			DL_SETPOFFSET(&lp->d_partitions[part], 0);
			DL_SETPSIZE(&lp->d_partitions[part], 0x7fffffff);
		}
	} else {
		msg = getdisklabel(buf + LABELOFFSET, lp);
		if (msg) {
			printf("disk%d: %s\n", unit, msg);
			goto bad;
		}
	}

	if (part >= lp->d_npartitions ||
	    DL_GETPSIZE(&lp->d_partitions[part]) == 0) {
bad:		free(sc, sizeof(struct disk_softc));
		return (ENXIO);
	}
	return (0);
}

int
diskclose(struct open_file *f)
{
	struct disk_softc *sc;

	sc = f->f_devdata;
	(void)prom_close(sc->sc_fd);

	free(sc, sizeof(struct disk_softc));
	f->f_devdata = NULL;
	return (0);
}


code:
/*	$OpenBSD: disk.h,v 1.5 2011/03/13 00:13:52 deraadt Exp $	*/
/*	$NetBSD: disk.h,v 1.1 1995/11/23 02:39:42 cgd Exp $	*/

int	diskstrategy(void *, int, daddr32_t, size_t, void *, size_t *);
/* int     diskopen(struct open_file *, int, int, int); */
int     diskclose(struct open_file *);

#define	diskioctl	noioctl


code:
/*	$OpenBSD: filesystem.c,v 1.4 1997/05/05 06:01:52 millert Exp $	*/
/*	$NetBSD: filesystem.c,v 1.3 1997/04/06 08:40:35 cgd Exp $	*/

/*
 * Copyright (c) 1993 Philip A. Nelson.
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Philip A. Nelson.
 * 4. The name of Philip A. Nelson may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY PHILIP NELSON ``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 PHILIP NELSON 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 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.
 *
 *	filesystem.c
 */

#include <lib/libsa/stand.h>
#include <lib/libsa/ufs.h>
#include <lib/libsa/cd9660.h>

struct fs_ops file_system[] = {
	{ ufs_open, ufs_close, ufs_read, ufs_write, ufs_seek, ufs_stat },
	{ cd9660_open, cd9660_close, cd9660_read, cd9660_write, cd9660_seek, cd9660_stat },
};

int nfsys = sizeof(file_system)/sizeof(struct fs_ops);


code:
#!/bin/sh -
#
#	$OpenBSD: newvers.sh,v 1.10 2008/03/09 12:03:03 sobrado Exp $
#	$NetBSD: newvers.sh,v 1.3 1996/06/14 20:03:04 cgd Exp $
#
# Copyright (c) 1984, 1986, 1990, 1993
#	The Regents of the University of California.  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.
# 3. Neither the name of the University nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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 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.
#
#	@(#)newvers.sh	8.1 (Berkeley) 4/20/94

u=${USER-root} h=`hostname` t=`date`
r=`awk ' { print $3 ; exit } ' < $1`

echo "char bootprog_name[] = \"OpenBSD/alpha boot ${r}\";" > vers.c
#echo "char bootprog_rev[] = \"${r}\";" >> vers.c
#echo "char bootprog_date[] = \"${t}\";" >> vers.c
#echo "char bootprog_maker[] = \"${u}@${h}\";" >> vers.c


code:
/*	$OpenBSD: prom_swpal.S,v 1.3 1996/10/30 22:40:45 niklas Exp $	*/
/*	$NetBSD: prom_swpal.S,v 1.4 1996/10/17 02:50:41 cgd Exp $	*/

/*
 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
 * All rights reserved.
 *
 * Author: Keith Bostic
 *
 * Permission to use, copy, modify and distribute this software and
 * its documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include "../../include/asm.h"
#include "../../include/prom.h"
#include "../../include/rpb.h"

/* Offsets from base of HWRPB. */
#define	RPB_SELFREF	0x00
#define	RPB_SLOTSIZE 	0x98
#define	RPB_PERCPU_OFF	0xA0

/* Offsets in a boot PCB. */
#define	PCB_KSP		0x00
#define	PCB_PTBR	0x10
#define	PCB_ASN		0x1c
#define	PCB_FEN		0x28

/* Pal values. */
#define PAL_RESERVED	0		/* Reserved to Digital. */
#define PAL_VMS		1		/* VMS */
#define PAL_OSF		2		/* OSF */

/*
 * PAL code switch routine.
 */
#define	D_RA			 (7*8)
#define	D_S0			 (8*8)
#define	D_S1			 (9*8)
#define	D_S2			(10*8)
#define	D_S3			(11*8)
#define	D_S4			(12*8)
#define	D_S5			(13*8)
#define	PALSW_FRAME_SIZE	(14*8)
#define	PALSW_REGS		IM_RA|IM_S0|IM_S1|IM_S2|IM_S3|IM_S4|IM_S5

	.comm	ptbr_save,8

	.text
	.align	4

NESTED(switch_palcode, 0, PALSW_FRAME_SIZE, ra, PALSW_REGS, 0)
	LDGP(pv)
/*	ldgp	gp, 0(pv)*/

	lda 	sp, -PALSW_FRAME_SIZE(sp)
	stq 	ra, D_RA(sp)
	stq	s0, D_S0(sp)
	stq	s1, D_S1(sp)
	stq	s2, D_S2(sp)
	stq	s3, D_S3(sp)
	stq	s4, D_S4(sp)
	stq	s5, D_S5(sp)

	stq	pv, 0(sp)
	stq	gp, 8(sp)

	ldiq 	s0, HWRPB_ADDR		/* s0 HWRPB_ADDR */
	ldq 	s1, RPB_SLOTSIZE(s0)
	call_pal PAL_VMS_mfpr_whami
	mulq 	s1, v0, s1		/* s1 per_cpu offset from base */
	ldq 	s2, RPB_PERCPU_OFF(s0)
	addq 	s0, s2, s2
	addq	s2, s1, s2		/* s2  PCB (virtual) */

	call_pal PAL_VMS_mfpr_ptbr
	stq 	v0,   PCB_PTBR(s2)
	stq	v0,   ptbr_save		/* save PTBR for the kernel */
	stl 	zero, PCB_ASN(s2)
	stq	zero, PCB_FEN(s2)
	stq	sp,   PCB_KSP(s2)

	ldq	t0, RPB_SELFREF(s0)	/* HWRBP base (physical) */
	ldq 	t1, RPB_PERCPU_OFF(s0)
	addq 	t0, t1, t0
	addq 	t0, s1, t0		/* PCB base (phys) */
	stq 	t0, 16(sp)

	call_pal PAL_VMS_mfpr_vptb
	mov 	v0, a3
	ldiq	a0, PAL_OSF
	lda	a1, contin
	ldq 	a2, 16(sp)

	call_pal PAL_swppal		/* a0, a1, a2, a3 */

contin:	ldq	pv, 0(sp)
	ldq	gp, 8(sp)

	ldq	ra, D_RA(sp)
	ldq	s0, D_S0(sp)
	ldq	s1, D_S1(sp)
	ldq	s2, D_S2(sp)
	ldq	s3, D_S3(sp)
	ldq	s4, D_S4(sp)
	ldq	s5, D_S5(sp)
	lda	sp, PALSW_FRAME_SIZE(sp)
	RET
END(switch_palcode)

#undef	D_RA
#undef	D_S0
#undef	D_S1
#undef	D_S2
#undef	D_S3
#undef	D_S4
#undef	D_S5
#undef	PALSW_FRAME_SIZE
#undef	PALSW_REGS


code:
/*	$OpenBSD: test.c,v 1.3 1996/10/30 22:40:46 niklas Exp $	*/
/*	$NetBSD: test.c,v 1.2 1995/02/16 02:33:00 cgd Exp $	*/

/*
 * Mach Operating System
 * Copyright (c) 1993 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
#include <sys/types.h>

#include <machine/prom.h>
#include <machine/rpb.h>
#include <machine/varargs.h>

main(a1, a2, a3, a4, a5)
	u_int64_t a1, a2, a3;
	char *a4[], *a5[];
{
	extern int console;
	prom_return_t ret;
	int cnt;
	char devname[128];

	init_prom_calls();		/* Init prom callback vector. */

	(void)printf("TEST BOOT\n");
	(void)printf("PFN: %lx\n", a1);
	(void)printf("PTBR: %lx\n", a2);
	(void)printf("argc: %lu\n", a3);
	(void)printf("argv[0]: %ls\n", a4[0]);
	(void)printf("envp: %lx\n", a5);

	ret.bits = prom_getenv(PROM_E_BOOTED_DEV, devname, sizeof(devname));
	devname[ret.u.retval] = '\0';
	(void)printf("booted_dev: %s\n", devname);

	ret.bits = prom_getenv(PROM_E_BOOTED_FILE, devname, sizeof(devname));
	devname[ret.u.retval] = '\0';
	(void)printf("booted_file: %s\n", devname);

	halt();
}


code:
$OpenBSD: version,v 1.10 2014/02/19 20:52:57 miod Exp $
$NetBSD: version,v 1.9 1997/01/23 23:10:38 cgd Exp $

1.1:	Initial version
1.2:	don't forget the Id string!
1.3:	make it work on AlphaStations
(OpenBSD 1.1)

1.4-1:	Re-import from master sources
1.4-2:	Complete rewrite of boot block code
(OpenBSD 1.2)

1.5:	Update for new ECOFF headers
(OpenBSD 1.3/1.4)

1.6:	Don't pass arguments to the kernel, trim unnecessary environment
	calls, kill 'ask' loop (i.e. if boot fails, halt), separate
	ECOFF and a.out support into separate options.
(OpenBSD 1.5)

1.7:   Use libsa and libkern rather than building the objects locally.
1.8:   Search for a kernel, if none specified.
1.9:   Add support for gzipped kernels.
(OpenBSD 1.6/1.7)

1.8:	Use MI loadfile, warn if kernel image would overlap SRM
1.9:	Better PALcode switching sequence
1.10:	/etc/random.seed support

path: sys/arch/alpha/stand/boot/version


code:
/*	$OpenBSD: bootxx.c,v 1.10 2004/07/05 19:59:17 deraadt Exp $	*/
/*	$NetBSD: bootxx.c,v 1.4 1997/01/18 00:28:59 cgd Exp $	*/

/*
 * Copyright (c) 1995 Carnegie-Mellon University.
 * All rights reserved.
 *
 * Author: Chris G. Demetriou
 *
 * Permission to use, copy, modify and distribute this software and
 * its documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include <sys/param.h>
#include <machine/rpb.h>
#include <machine/prom.h>

#include "bbinfo.h"

extern _end, start;

struct bbinfoloc desc = {
	0xbabefacedeadbeef,
	(u_int64_t)&start,
	(u_int64_t)&_end,
	{ 0, },
	0xdeadbeeffacebabe
};

int
open_dev(fd)
	int *fd;
{
	prom_return_t ret;
	char devname[64];
	int devlen;

	/*
	 * XXX
         * We don't know what device names look like yet,
         * so we can't change them.
         */
        ret.bits = prom_getenv(PROM_E_BOOTED_DEV, devname, sizeof(devname));
        devlen = ret.u.retval;

        ret.bits = prom_open(devname, devlen);
        if (ret.u.status)
                return 0;

	*fd = ret.u.retval;

	return 1;
}

int
load_file(bbinfop, loadaddr)
	struct bbinfo *bbinfop;
	char *loadaddr;
{
	prom_return_t ret;
	int32_t cksum, *int32p;
	int i, n, fd, rv;
	char *cp;

	if (bbinfop->nblocks <= 0) {
		puts("invalid number of blocks in boot program description\n");
		return 0;
	}
	if (bbinfop->bsize < DEV_BSIZE || bbinfop->bsize > MAXBSIZE) {
		puts("invalid block size in boot program description\n");
		return 0;
	}

	int32p = (int32_t *)&_end;
	n = bbinfop->nblocks +
	    (sizeof(*bbinfop) / sizeof(bbinfop->blocks[0])) - 1;
	if ((long)&_end - (long)&start + sizeof(bbinfop->blocks[0]) * n >
	    15 * 512) {
		puts("way too many blocks\n");
		return 0;
	}

	for (i = 0, cksum = 0; i < n; i++)
		cksum += *int32p++;
	if (cksum != 0) {
		puts("invalid checksum in boot program description\n");
		return 0;
	}

	if (!open_dev(&fd)) {
		puts("couldn't open disk device\n");
		return 0;
	}

	cp = loadaddr;
	rv = 1;
	for (i = 0; i < bbinfop->nblocks; i++) {
puts(".");
		ret.bits = prom_read(fd, bbinfop->bsize, cp,
		    bbinfop->blocks[i]);
puts("\b");
		if (ret.u.status) {
			rv = 0;
			puts("BLOCK READ ERROR!\n");
			break;
		}
		cp += bbinfop->bsize;
	}
	prom_close(fd);

	return (rv);
}

int
main()
{
	struct bbinfo *bbinfop;
	char *loadaddr;
	void (*entry)(void);

	/* Init prom callback vector. */
	init_prom_calls();

	puts("\nOpenBSD/Alpha Primary Boot\n");

	bbinfop = (struct bbinfo *)&_end;
	loadaddr = (char *)SECONDARY_LOAD_ADDRESS;
	if (!load_file(bbinfop, loadaddr)) {
		puts("\nLOAD FAILED!\n\n");
		return;
	}

#if 0
	puts("Jumping to entry point...\n");
#endif
	entry = (void (*)())loadaddr;
	(*entry)();
	puts("SECONDARY BOOT BLOCK RETURNED!\n");
}


path: sys/arch/alpha/stand/bootxx/CVS/Entries


path: sys/arch/alpha/stand/bootxx/CVS/Tag


code:
#	$OpenBSD: Makefile,v 1.14 2013/06/13 11:27:56 deraadt Exp $
#	$NetBSD: Makefile,v 1.12 1997/04/17 07:27:49 thorpej Exp $

S=	${.CURDIR}/../../../..

.PATH: ${.CURDIR}/..

PROG = bootxx

SRCS = start.S bootxx.c prom.c prom_disp.S puts.c
NOMAN=
INSTALL_STRIP=
BINMODE= 444

BOOT_RELOC = ${PRIMARY_LOAD_ADDRESS}

CLEANFILES+= ${PROG}.sym ${PROG}.nosym ${PROG}.trunc

.PATH:	${S}/lib/libkern/arch/alpha ${S}/lib/libkern
SRCS+=	memmove.S bzero.c

DEFNS= -DPRIMARY_BOOTBLOCK

AFLAGS += -DASSEMBLER ${DEFNS} -fno-pie
CPPFLAGS += -I${.CURDIR}/../.. -I${S} ${DEFNS}
CFLAGS = -Werror -mno-fp-regs -fno-pie

${PROG}.nosym: ${PROG}.sym
	cp ${PROG}.sym ${PROG}.nosym
	strip ${PROG}.nosym

${PROG}: ${PROG}.nosym
	objcopy --output-target=binary ${PROG}.nosym ${PROG}.trunc
	dd if=${PROG}.trunc of=${PROG} obs=`expr 15 \* 512` conv=osync

# no lint here (yet?)
lint:

.include <bsd.prog.mk>

${PROG}.sym: ${OBJS}
	${LD} -nopie -Ttext ${BOOT_RELOC} -N -e start -o ${PROG}.sym ${OBJS}
	size ${PROG}.sym

path: sys/arch/alpha/stand/bootxx/Makefile


code:
/*	$OpenBSD: headersize.c,v 1.10 2013/10/15 05:17:31 deraadt Exp $	*/
/*	$NetBSD: headersize.c,v 1.5 1996/09/23 04:32:59 cgd Exp $	*/

/*
 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
 * All rights reserved.
 *
 * Author: Chris G. Demetriou
 *
 * Permission to use, copy, modify and distribute this software and
 * its documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#define	ELFSIZE		64

#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/exec.h>
#include <sys/exec_elf.h>

#include <unistd.h>
#include <stdio.h>
#include <err.h>

#define	HDR_BUFSIZE	512

int
main(argc, argv)
	int argc;
	char *argv[];
{
	char buf[HDR_BUFSIZE], *fname;
#ifdef ALPHA_BOOT_ELF
	Elf64_Ehdr *elfp;
#endif
	int fd;
	unsigned long loadaddr;

	if (argc != 3)
		errx(1, "must be given two arguments (load addr, file name)");
	if (sscanf(argv[1], "%lx", &loadaddr) != 1)
		errx(1, "load addr argument (%s) not valid", argv[1]);
	fname = argv[2];

	if ((fd = open(fname, O_RDONLY, 0)) == -1)
		err(1, "%s: open failed", fname);

	if (read(fd, &buf, HDR_BUFSIZE) != HDR_BUFSIZE)
		err(1, "%s: read failed", fname);
#ifdef ALPHA_BOOT_ELF
	elfp = (Elf64_Ehdr *)buf;
#endif

#ifdef ALPHA_BOOT_ELF
	if (memcmp(ELFMAG, elfp->e_ident, SELFMAG) == 0) {
		Elf64_Phdr phdr;

		/* XXX assume the first segment is the one we want */
		if (lseek(fd, elfp->e_phoff, SEEK_SET) == -1)
			err(1, "%s: lseek phdr failed", fname);
		if (read(fd, (void *)&phdr, sizeof(phdr)) != sizeof(phdr))
			err(1, "%s: read phdr failed", fname);

		printf("%ld\n", phdr.p_offset + (loadaddr - phdr.p_vaddr));
	}
	else
#endif
		errx(1, "%s: bad magic number", fname);

	close(fd);
	exit(0);
}


code:
/*	$OpenBSD: installboot.c,v 1.19 2014/07/12 19:01:49 tedu Exp $	*/
/*	$NetBSD: installboot.c,v 1.2 1997/04/06 08:41:12 cgd Exp $	*/

/*
 * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
 * Copyright (c) 1994 Paul Kranenburg
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Paul Kranenburg.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * 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 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 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/mount.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ufs/dir.h>
#include <ufs/ffs/fs.h>
#include <sys/disklabel.h>
#include <sys/dkio.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <util.h>

#include "bbinfo.h"

#ifndef	ISO_DEFAULT_BLOCK_SIZE
#define	ISO_DEFAULT_BLOCK_SIZE	2048
#endif

int	verbose, nowrite, hflag;
char	*boot, *proto, *dev;

struct bbinfoloc *bbinfolocp;
struct bbinfo *bbinfop;
int	max_block_count;

char		*loadprotoblocks(char *, long *);
int		loadblocknums(char *, int, unsigned long);
static void	devread(int, void *, daddr32_t, size_t, char *);
static void	usage(void);
int		main(int, char *[]);

int	isofsblk = 0;
int	isofseblk = 0;

static void
usage(void)
{
	(void)fprintf(stderr,
	    "usage: installboot [-n] [-v] [-s isofsblk -e isofseblk] "
	    "<boot> <proto> <device>\n");
	exit(1);
}

int
main(int argc, char *argv[])
{
	int	c, devfd;
	char	*protostore;
	long	protosize;
	struct stat disksb, bootsb;
	struct disklabel dl;
	daddr_t partoffset;
#define BBPAD   0x1e0
	struct bb {
		char	bb_pad[BBPAD];	/* disklabel lives in here, actually */
		long	bb_secsize;	/* size of secondary boot block */
		long	bb_secstart;	/* start of secondary boot block */
		long	bb_flags;	/* unknown; always zero */
		long	bb_cksum;	/* checksum of the boot block, as longs. */
	} bb;
	long *lp, *ep;

	while ((c = getopt(argc, argv, "vns:e:")) != -1) {
		switch (c) {
		case 'n':
			/* Do not actually write the bootblock to disk */
			nowrite = 1;
			break;
		case 'v':
			/* Chat */
			verbose = 1;
			break;
		case 's':
			isofsblk = atoi(optarg);
			break;
		case 'e':
			isofseblk = atoi(optarg);
			break;
		default:
			usage();
		}
	}

	if (argc - optind < 3)
		usage();

	boot = argv[optind];
	proto = argv[optind + 1];
	dev = argv[optind + 2];

	if (verbose) {
		(void)printf("boot: %s\n", boot);
		(void)printf("proto: %s\n", proto);
		(void)printf("device: %s\n", dev);
	}

	/* Load proto blocks into core */
	if ((protostore = loadprotoblocks(proto, &protosize)) == NULL)
		exit(1);

	/* Open and check raw disk device */
	if ((devfd = opendev(dev, O_RDONLY, OPENDEV_PART, &dev)) < 0)
		err(1, "open: %s", dev);
	if (fstat(devfd, &disksb) == -1)
		err(1, "fstat: %s", dev);
	if (!S_ISCHR(disksb.st_mode))
		errx(1, "%s must be a character device node", dev);
	if ((minor(disksb.st_rdev) % getmaxpartitions()) != getrawpartition())
		errx(1, "%s must be the raw partition", dev);

	/* Extract and load block numbers */
	if (stat(boot, &bootsb) == -1)
		err(1, "stat: %s", boot);
	if (!S_ISREG(bootsb.st_mode))
		errx(1, "%s must be a regular file", boot);
	if ((minor(disksb.st_rdev) / getmaxpartitions()) !=
	    (minor(bootsb.st_dev) / getmaxpartitions()))
		errx(1, "%s must be somewhere on %s", boot, dev);

	/*
	 * Find the offset of the secondary boot block's partition
	 * into the disk.  If disklabels not supported, assume zero.
	 */
	if (ioctl(devfd, DIOCGDINFO, &dl) != -1) {
		partoffset = DL_GETPOFFSET(&dl.d_partitions[minor(bootsb.st_dev) %
		    getmaxpartitions()]);
	} else {
		if (errno != ENOTTY)
			err(1, "read disklabel: %s", dev);
		warnx("couldn't read label from %s, using part offset of 0",
		    dev);
		partoffset = 0;
	}
	if (verbose)
		(void)printf("%s partition offset = 0x%llx\n", boot, partoffset);

	/* Sync filesystems (make sure boot's block numbers are stable) */
	sync();
	sleep(2);
	sync();
	sleep(2);

	if (loadblocknums(boot, devfd, partoffset) != 0)
		exit(1);

	(void)close(devfd);

	if (nowrite)
		return 0;

#if 0
	/* Write patched proto bootblocks into the superblock */
	if (protosize > SBSIZE - DEV_BSIZE)
		errx(1, "proto bootblocks too big");
#endif

	if ((devfd = opendev(dev, O_RDWR, OPENDEV_PART, &dev)) < 0)
		err(1, "open: %s", dev);

	if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE)
		err(1, "lseek bootstrap");

	if (write(devfd, protostore, protosize) != protosize)
		err(1, "write bootstrap");

	if (lseek(devfd, 0, SEEK_SET) != 0)
		err(1, "lseek label");

	if (read(devfd, &bb, sizeof (bb)) != sizeof (bb))
		err(1, "read label");

	bb.bb_secsize = 15;
	bb.bb_secstart = 1;
	bb.bb_flags = 0;
	bb.bb_cksum = 0;

	for (lp = (long *)&bb, ep = &bb.bb_cksum; lp < ep; lp++)
		bb.bb_cksum += *lp;

	if (lseek(devfd, 0, SEEK_SET) != 0)
		err(1, "lseek label 2");

	if (write(devfd, &bb, sizeof bb) != sizeof bb)
		err(1, "write label ");

	(void)close(devfd);
	return 0;
}

char *
loadprotoblocks(char *fname, long *size)
{
	int	fd, sz;
	char	*bp;
	struct	stat statbuf;
	u_int64_t *matchp;

	/*
	 * Read the prototype boot block into memory.
	 */
	if ((fd = open(fname, O_RDONLY)) < 0) {
		warn("open: %s", fname);
		return NULL;
	}
	if (fstat(fd, &statbuf) != 0) {
		warn("fstat: %s", fname);
		close(fd);
		return NULL;
	}
	sz = roundup(statbuf.st_size, DEV_BSIZE);
	if ((bp = calloc(sz, 1)) == NULL) {
		warnx("malloc: %s: no memory", fname);
		close(fd);
		return NULL;
	}
	if (read(fd, bp, statbuf.st_size) != statbuf.st_size) {
		warn("read: %s", fname);
		free(bp);
		close(fd);
		return NULL;
	}
	close(fd);

	/*
	 * Find the magic area of the program, and figure out where
	 * the 'blocks' struct is, from that.
	 */
	bbinfolocp = NULL;
	for (matchp = (u_int64_t *)bp; (char *)matchp < bp + sz; matchp++) {
		if (*matchp != 0xbabefacedeadbeef)
			continue;
		bbinfolocp = (struct bbinfoloc *)matchp;
		if (bbinfolocp->magic1 == 0xbabefacedeadbeef &&
		    bbinfolocp->magic2 == 0xdeadbeeffacebabe)
			break;
		bbinfolocp = NULL;
	}

	if (bbinfolocp == NULL) {
		warnx("%s: not a valid boot block?", fname);
		return NULL;
	}

	bbinfop = (struct bbinfo *)(bp + bbinfolocp->end - bbinfolocp->start);
	memset(bbinfop, 0, sz - (bbinfolocp->end - bbinfolocp->start));
	max_block_count =
	    ((char *)bbinfop->blocks - bp) / sizeof (bbinfop->blocks[0]);

	if (verbose) {
		(void)printf("boot block info locator at offset 0x%x\n",
			(char *)bbinfolocp - bp);
		(void)printf("boot block info at offset 0x%x\n",
			(char *)bbinfop - bp);
		(void)printf("max number of blocks: %d\n", max_block_count);
	}

	*size = sz;
	return (bp);
}

static void
devread(int fd, void *buf, daddr32_t blk, size_t size, char *msg)
{
	if (lseek(fd, dbtob(blk), SEEK_SET) != dbtob(blk))
		err(1, "%s: devread: lseek", msg);

	if (read(fd, buf, size) != size)
		err(1, "%s: devread: read", msg);
}

static char sblock[SBSIZE];

int
loadblocknums(char *boot, int devfd, unsigned long partoffset)
{
	int		i, fd, ndb;
	struct	stat	statbuf;
	struct	statfs	statfsbuf;
	struct fs	*fs;
	char		*buf;
	daddr32_t	blk, *ap;
	struct ufs1_dinode	*ip;
	int32_t		cksum;

	/*
	 * Open 2nd-level boot program and record the block numbers
	 * it occupies on the filesystem represented by `devfd'.
	 */
	if ((fd = open(boot, O_RDONLY)) < 0)
		err(1, "open: %s", boot);

	if (fstatfs(fd, &statfsbuf) != 0)
		err(1, "statfs: %s", boot);

	if (isofsblk) {
		bbinfop->bsize = ISO_DEFAULT_BLOCK_SIZE;
		bbinfop->nblocks = isofseblk - isofsblk + 1;
		if (bbinfop->nblocks > max_block_count)
			errx(1, "%s: Too many blocks", boot);
		if (verbose)
			(void)printf("%s: starting block %d (%d total):\n\t",
			    boot, isofsblk, bbinfop->nblocks);
		for (i = 0; i < bbinfop->nblocks; i++) {
			blk = (isofsblk + i) * (bbinfop->bsize / DEV_BSIZE);
			bbinfop->blocks[i] = blk;
			if (verbose)
				(void)printf("%d ", blk);
		}
		if (verbose)
			(void)printf("\n");

		cksum = 0;
		for (i = 0; i < bbinfop->nblocks +
		    (sizeof(*bbinfop) / sizeof(bbinfop->blocks[0])) - 1; i++)
			cksum += ((int32_t *)bbinfop)[i];
		bbinfop->cksum = -cksum;

		return 0;
	}

	if (strncmp(statfsbuf.f_fstypename, MOUNT_FFS, MFSNAMELEN))
		errx(1, "%s: must be on a FFS filesystem", boot);

	if (fsync(fd) != 0)
		err(1, "fsync: %s", boot);

	if (fstat(fd, &statbuf) != 0)
		err(1, "fstat: %s", boot);

	close(fd);

	/* Read superblock */
	devread(devfd, sblock, btodb(SBOFF) + partoffset, SBSIZE,
	    "superblock");
	fs = (struct fs *)sblock;

	/* Read inode */
	if ((buf = malloc(fs->fs_bsize)) == NULL)
		errx(1, "No memory for filesystem block");

	blk = fsbtodb(fs, ino_to_fsba(fs, statbuf.st_ino));
	devread(devfd, buf, blk + partoffset, fs->fs_bsize, "inode");
	ip = (struct ufs1_dinode *)(buf) + ino_to_fsbo(fs, statbuf.st_ino);

	/*
	 * Register filesystem block size.
	 */
	bbinfop->bsize = fs->fs_bsize;

	/*
	 * Get the block numbers; we don't handle fragments
	 */
	ndb = howmany(ip->di_size, fs->fs_bsize);
	if (ndb > max_block_count)
		errx(1, "%s: Too many blocks", boot);

	/*
	 * Register block count.
	 */
	bbinfop->nblocks = ndb;

	if (verbose)
		(void)printf("%s: block numbers: ", boot);
	ap = ip->di_db;
	for (i = 0; i < NDADDR && *ap && ndb; i++, ap++, ndb--) {
		blk = fsbtodb(fs, *ap);
		bbinfop->blocks[i] = blk + partoffset;
		if (verbose)
			(void)printf("%d ", bbinfop->blocks[i]);
	}
	if (verbose)
		(void)printf("\n");

	if (ndb == 0)
		goto checksum;

	/*
	 * Just one level of indirections; there isn't much room
	 * for more in the 1st-level bootblocks anyway.
	 */
	if (verbose)
		(void)printf("%s: block numbers (indirect): ", boot);
	blk = ip->di_ib[0];
	devread(devfd, buf, blk + partoffset, fs->fs_bsize,
	    "indirect block");
	ap = (daddr32_t *)buf;
	for (; i < NINDIR(fs) && *ap && ndb; i++, ap++, ndb--) {
		blk = fsbtodb(fs, *ap);
		bbinfop->blocks[i] = blk + partoffset;
		if (verbose)
			(void)printf("%d ", bbinfop->blocks[i]);
	}
	if (verbose)
		(void)printf("\n");

	if (ndb)
		errx(1, "%s: Too many blocks", boot);

checksum:
	cksum = 0;
	for (i = 0; i < bbinfop->nblocks +
	    (sizeof (*bbinfop) / sizeof (bbinfop->blocks[0])) - 1; i++)
		cksum += ((int32_t *)bbinfop)[i];
	bbinfop->cksum = -cksum;

	return 0;
}


code:
/Makefile/1.8/Sat Jul 19 00:03:51 2003//TOPENBSD_5_8_BASE
D


code: src/sys/arch/alpha/stand/installboot


code:
#	$OpenBSD: Makefile,v 1.8 2003/07/19 00:03:51 david Exp $
#	$NetBSD: Makefile,v 1.2 1996/11/06 23:09:13 cgd Exp $

.PATH:	${.CURDIR}/..

MAN=	installboot.8
MANSUBDIR=alpha

.if ${MACHINE} == "alpha"
PROG=	installboot
BINDIR=	/usr/mdec
DPADD+= ${LIBUTIL}
LDADD+= -lutil
LDSTATIC=-static
.else
NOPROG=
.endif

.include <bsd.prog.mk>


code:
/Makefile/1.1/Mon May  5 06:01:57 1997//TOPENBSD_5_8_BASE
/Makefile.inc/1.3/Wed Jul 16 20:08:25 2014//TOPENBSD_5_8_BASE
D

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


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


code:
#	$OpenBSD: Makefile,v 1.1 1997/05/05 06:01:57 millert Exp $

.include <bsd.obj.mk>
.include <bsd.dep.mk>
.include <bsd.subdir.mk>

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