xref: /minix3/usr.sbin/installboot/arch/sparc.c (revision 9f8e6353e54579f3834bed052b7960946fc4bb01)
1*9f8e6353SEvgeniy Ivanov /*	$NetBSD: sparc.c,v 1.11 2008/04/28 20:24:16 martin Exp $ */
2*9f8e6353SEvgeniy Ivanov 
3*9f8e6353SEvgeniy Ivanov /*-
4*9f8e6353SEvgeniy Ivanov  * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
5*9f8e6353SEvgeniy Ivanov  * All rights reserved.
6*9f8e6353SEvgeniy Ivanov  *
7*9f8e6353SEvgeniy Ivanov  * This code is derived from software contributed to The NetBSD Foundation
8*9f8e6353SEvgeniy Ivanov  * by Paul Kranenburg and Luke Mewburn.
9*9f8e6353SEvgeniy Ivanov  *
10*9f8e6353SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
11*9f8e6353SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
12*9f8e6353SEvgeniy Ivanov  * are met:
13*9f8e6353SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
14*9f8e6353SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
15*9f8e6353SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
16*9f8e6353SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
17*9f8e6353SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
18*9f8e6353SEvgeniy Ivanov  *
19*9f8e6353SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*9f8e6353SEvgeniy Ivanov  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*9f8e6353SEvgeniy Ivanov  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*9f8e6353SEvgeniy Ivanov  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*9f8e6353SEvgeniy Ivanov  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*9f8e6353SEvgeniy Ivanov  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*9f8e6353SEvgeniy Ivanov  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*9f8e6353SEvgeniy Ivanov  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*9f8e6353SEvgeniy Ivanov  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*9f8e6353SEvgeniy Ivanov  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*9f8e6353SEvgeniy Ivanov  * POSSIBILITY OF SUCH DAMAGE.
30*9f8e6353SEvgeniy Ivanov  */
31*9f8e6353SEvgeniy Ivanov 
32*9f8e6353SEvgeniy Ivanov #if HAVE_NBTOOL_CONFIG_H
33*9f8e6353SEvgeniy Ivanov #include "nbtool_config.h"
34*9f8e6353SEvgeniy Ivanov #endif
35*9f8e6353SEvgeniy Ivanov 
36*9f8e6353SEvgeniy Ivanov #include <sys/cdefs.h>
37*9f8e6353SEvgeniy Ivanov #if !defined(__lint)
38*9f8e6353SEvgeniy Ivanov __RCSID("$NetBSD: sparc.c,v 1.11 2008/04/28 20:24:16 martin Exp $");
39*9f8e6353SEvgeniy Ivanov #endif	/* !__lint */
40*9f8e6353SEvgeniy Ivanov 
41*9f8e6353SEvgeniy Ivanov #include <sys/param.h>
42*9f8e6353SEvgeniy Ivanov 
43*9f8e6353SEvgeniy Ivanov #include <assert.h>
44*9f8e6353SEvgeniy Ivanov #include <err.h>
45*9f8e6353SEvgeniy Ivanov #include <stddef.h>
46*9f8e6353SEvgeniy Ivanov #include <stdio.h>
47*9f8e6353SEvgeniy Ivanov #include <stdlib.h>
48*9f8e6353SEvgeniy Ivanov #include <string.h>
49*9f8e6353SEvgeniy Ivanov #include <unistd.h>
50*9f8e6353SEvgeniy Ivanov 
51*9f8e6353SEvgeniy Ivanov #include "installboot.h"
52*9f8e6353SEvgeniy Ivanov 
53*9f8e6353SEvgeniy Ivanov static struct bbinfo_params bbparams = {
54*9f8e6353SEvgeniy Ivanov 	SPARC_BBINFO_MAGIC,
55*9f8e6353SEvgeniy Ivanov 	SPARC_BOOT_BLOCK_OFFSET,
56*9f8e6353SEvgeniy Ivanov 	SPARC_BOOT_BLOCK_BLOCKSIZE,
57*9f8e6353SEvgeniy Ivanov 	SPARC_BOOT_BLOCK_MAX_SIZE,
58*9f8e6353SEvgeniy Ivanov 	32,			/* leave room for a.out header */
59*9f8e6353SEvgeniy Ivanov 	BBINFO_BIG_ENDIAN,
60*9f8e6353SEvgeniy Ivanov };
61*9f8e6353SEvgeniy Ivanov 
62*9f8e6353SEvgeniy Ivanov static int sparc_clearheader(ib_params *, struct bbinfo_params *, uint8_t *);
63*9f8e6353SEvgeniy Ivanov static int sparc_setheader(ib_params *, struct bbinfo_params *, uint8_t *);
64*9f8e6353SEvgeniy Ivanov 
65*9f8e6353SEvgeniy Ivanov static int sparc_clearboot(ib_params *);
66*9f8e6353SEvgeniy Ivanov static int sparc_setboot(ib_params *);
67*9f8e6353SEvgeniy Ivanov 
68*9f8e6353SEvgeniy Ivanov struct ib_mach ib_mach_sparc =
69*9f8e6353SEvgeniy Ivanov 	{ "sparc", sparc_setboot, sparc_clearboot, no_editboot,
70*9f8e6353SEvgeniy Ivanov 		IB_STAGE2START };
71*9f8e6353SEvgeniy Ivanov 
72*9f8e6353SEvgeniy Ivanov static int
sparc_clearboot(ib_params * params)73*9f8e6353SEvgeniy Ivanov sparc_clearboot(ib_params *params)
74*9f8e6353SEvgeniy Ivanov {
75*9f8e6353SEvgeniy Ivanov 
76*9f8e6353SEvgeniy Ivanov 	assert(params != NULL);
77*9f8e6353SEvgeniy Ivanov 
78*9f8e6353SEvgeniy Ivanov 	return (shared_bbinfo_clearboot(params, &bbparams, sparc_clearheader));
79*9f8e6353SEvgeniy Ivanov }
80*9f8e6353SEvgeniy Ivanov 
81*9f8e6353SEvgeniy Ivanov static int
sparc_setboot(ib_params * params)82*9f8e6353SEvgeniy Ivanov sparc_setboot(ib_params *params)
83*9f8e6353SEvgeniy Ivanov {
84*9f8e6353SEvgeniy Ivanov 	assert(params != NULL);
85*9f8e6353SEvgeniy Ivanov 
86*9f8e6353SEvgeniy Ivanov 	return (shared_bbinfo_setboot(params, &bbparams, sparc_setheader));
87*9f8e6353SEvgeniy Ivanov }
88*9f8e6353SEvgeniy Ivanov 
89*9f8e6353SEvgeniy Ivanov 
90*9f8e6353SEvgeniy Ivanov static int
sparc_clearheader(ib_params * params,struct bbinfo_params * bb_params,uint8_t * bb)91*9f8e6353SEvgeniy Ivanov sparc_clearheader(ib_params *params, struct bbinfo_params *bb_params,
92*9f8e6353SEvgeniy Ivanov 	uint8_t *bb)
93*9f8e6353SEvgeniy Ivanov {
94*9f8e6353SEvgeniy Ivanov 
95*9f8e6353SEvgeniy Ivanov 	assert(params != NULL);
96*9f8e6353SEvgeniy Ivanov 	assert(bb_params != NULL);
97*9f8e6353SEvgeniy Ivanov 	assert(bb != NULL);
98*9f8e6353SEvgeniy Ivanov 
99*9f8e6353SEvgeniy Ivanov 	memset(bb, 0, bb_params->headeroffset);
100*9f8e6353SEvgeniy Ivanov 	return (1);
101*9f8e6353SEvgeniy Ivanov }
102*9f8e6353SEvgeniy Ivanov 
103*9f8e6353SEvgeniy Ivanov static int
sparc_setheader(ib_params * params,struct bbinfo_params * bb_params,uint8_t * bb)104*9f8e6353SEvgeniy Ivanov sparc_setheader(ib_params *params, struct bbinfo_params *bb_params, uint8_t *bb)
105*9f8e6353SEvgeniy Ivanov {
106*9f8e6353SEvgeniy Ivanov 
107*9f8e6353SEvgeniy Ivanov 	assert(params != NULL);
108*9f8e6353SEvgeniy Ivanov 	assert(bb_params != NULL);
109*9f8e6353SEvgeniy Ivanov 	assert(bb != NULL);
110*9f8e6353SEvgeniy Ivanov 
111*9f8e6353SEvgeniy Ivanov 	/*
112*9f8e6353SEvgeniy Ivanov 	 * sun4c/sun4m PROMs require an a.out(5) format header.
113*9f8e6353SEvgeniy Ivanov 	 * Old-style sun4 PROMs do not expect a header at all.
114*9f8e6353SEvgeniy Ivanov 	 * To deal with this, we construct a header that is also executable
115*9f8e6353SEvgeniy Ivanov 	 * code containing a forward branch that gets us past the 32-byte
116*9f8e6353SEvgeniy Ivanov 	 * header where the actual code begins. In assembly:
117*9f8e6353SEvgeniy Ivanov 	 * 	.word	MAGIC		! a NOP
118*9f8e6353SEvgeniy Ivanov 	 * 	ba,a	start		!
119*9f8e6353SEvgeniy Ivanov 	 * 	.skip	24		! pad
120*9f8e6353SEvgeniy Ivanov 	 * start:
121*9f8e6353SEvgeniy Ivanov 	 */
122*9f8e6353SEvgeniy Ivanov #define SUN_MAGIC	0x01030107
123*9f8e6353SEvgeniy Ivanov #define SUN4_BASTART	0x30800007	/* i.e.: ba,a `start' */
124*9f8e6353SEvgeniy Ivanov 	*((uint32_t *)bb) = htobe32(SUN_MAGIC);
125*9f8e6353SEvgeniy Ivanov 	*((uint32_t *)bb + 1) = htobe32(SUN4_BASTART);
126*9f8e6353SEvgeniy Ivanov 
127*9f8e6353SEvgeniy Ivanov 	return (1);
128*9f8e6353SEvgeniy Ivanov }
129