1 /*        $NetBSD: idprom.c,v 1.7 2008/04/28 20:23:37 martin Exp $    */
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Adam Glass, Gordon W. Ross, and Matthew Fredette.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Machine ID PROM - system type and serial number
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: idprom.c,v 1.7 2008/04/28 20:23:37 martin Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/kernel.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <machine/autoconf.h>
47 #include <machine/idprom.h>
48 
49 #include <sun2/sun2/machdep.h>
50 #include <sun2/sun2/control.h>
51 
52 /*
53  * This structure is what this driver is all about.
54  * It is copied from the device early in startup.
55  */
56 struct idprom identity_prom;
57 
58 static int idprom_cksum(u_char *);
59 static void idprom_get(u_char *);
60 static int idprom_hostid(void);
61 
62 /*
63  * Copy the IDPROM contents,
64  * verify the checksum,
65  * set the hostid...
66  */
67 void
idprom_init(void)68 idprom_init(void)
69 {
70 
71           idprom_get((u_char *)&identity_prom);
72           if (idprom_cksum((u_char *) &identity_prom))
73                     printf("idprom: bad checksum\n");
74           if (identity_prom.idp_format < 1)
75                     printf("idprom: bad version\n");
76 
77           cpu_machine_id = identity_prom.idp_machtype;
78           hostid = idprom_hostid();
79 }
80 
81 static int
idprom_cksum(u_char * p)82 idprom_cksum(u_char *p)
83 {
84           int len, x;
85 
86           len = IDPROM_CKSUM_SIZE;
87           x = 0;    /* xor of data */
88           do x ^= *p++;
89           while (--len > 0);
90           return (x);
91 }
92 
93 static int
idprom_hostid(void)94 idprom_hostid(void)
95 {
96           struct idprom *idp;
97           union {
98                     long l;
99                     char c[4];
100           } hid;
101 
102           /*
103            * Construct the hostid from the idprom contents.
104            * This appears to be the way SunOS does it.
105            */
106           idp = &identity_prom;
107           hid.c[0] = idp->idp_machtype;
108           hid.c[1] = idp->idp_serialnum[0];
109           hid.c[2] = idp->idp_serialnum[1];
110           hid.c[3] = idp->idp_serialnum[2];
111           return (hid.l);
112 }
113 
114 void
idprom_etheraddr(u_char * eaddrp)115 idprom_etheraddr(u_char *eaddrp)
116 {
117 
118           memcpy(eaddrp, identity_prom.idp_etheraddr, 6);
119 }
120 
121 /*
122  * Machine specific stuff follows.
123  */
124 
125 /*
126  * Copy the IDPROM to memory.
127  *
128  * On the Sun2, this is called very early,
129  * because we need the cputype.
130  */
131 static void
idprom_get(u_char * dst)132 idprom_get(u_char *dst)
133 {
134           vaddr_t src;        /* control space address */
135           int len, x;
136 
137           src = IDPROM_BASE;
138           len = IDPROM_SIZE;
139           do {
140                     x = get_control_byte(src);
141                     src += PAGE_SIZE;
142                     *dst++ = x;
143           } while (--len > 0);
144 }
145 
146