xref: /netbsd-src/sys/arch/i386/stand/lib/bootinfo_biosgeom.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: bootinfo_biosgeom.c,v 1.23 2017/01/24 11:09:14 nonaka Exp $	*/
2 
3 /*
4  * Copyright (c) 1997
5  *	Matthias Drochner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/types.h>
30 #include <machine/disklabel.h>
31 #include <machine/cpu.h>
32 
33 #include <lib/libkern/libkern.h>
34 #include <lib/libsa/stand.h>
35 
36 #include "libi386.h"
37 #include "biosdisk_ll.h"
38 #include "bootinfo.h"
39 
40 #ifdef BIOSDISK_EXTINFO_V3
41 static struct {
42 	char	*name;
43 	int	flag;
44 } bus_names[] = { {"ISA", BI_GEOM_BUS_ISA},
45 		  {"PCI", BI_GEOM_BUS_PCI},
46 		  {NULL, BI_GEOM_BUS_OTHER} };
47 static struct {
48 	char	*name;
49 	int	flag;
50 } iface_names[] = { {"ATA", BI_GEOM_IFACE_ATA},
51 		    {"ATAPI", BI_GEOM_IFACE_ATAPI},
52 		    {"SCSI", BI_GEOM_IFACE_SCSI},
53 		    {"USB", BI_GEOM_IFACE_USB},
54 		    {"1394", BI_GEOM_IFACE_1394},
55 		    {"FIBRE", BI_GEOM_IFACE_FIBRE},
56 		    {NULL, BI_GEOM_IFACE_OTHER} };
57 #endif
58 
59 void
60 bi_getbiosgeom(void)
61 {
62 	struct btinfo_biosgeom *bibg;
63 	int i, j, nvalid;
64 	int nhd;
65 	unsigned int cksum;
66 	struct biosdisk_ll d;
67 	struct biosdisk_extinfo ed;
68 	char buf[BIOSDISK_DEFAULT_SECSIZE];
69 
70 	nhd = get_harddrives();
71 #ifdef GEOM_DEBUG
72 	printf("nhd %d\n", nhd);
73 #endif
74 
75 	bibg = alloc(sizeof(struct btinfo_biosgeom)
76 		     + (nhd - 1) * sizeof(struct bi_biosgeom_entry));
77 	if (bibg == NULL)
78 		return;
79 
80 	for (i = nvalid = 0; i < MAX_BIOSDISKS && nvalid < nhd; i++) {
81 		d.dev = 0x80 + i;
82 
83 		if (set_geometry(&d, &ed))
84 			continue;
85 		memset(&bibg->disk[nvalid], 0, sizeof(bibg->disk[nvalid]));
86 
87 		bibg->disk[nvalid].sec = d.sec;
88 		bibg->disk[nvalid].head = d.head;
89 		bibg->disk[nvalid].cyl = d.cyl;
90 		bibg->disk[nvalid].dev = d.dev;
91 
92 		if (readsects(&d, 0, 1, buf, 0)) {
93 			bibg->disk[nvalid].flags |= BI_GEOM_INVALID;
94 			nvalid++;
95 			continue;
96 		}
97 
98 #ifdef GEOM_DEBUG
99 		printf("#%d: %x: C %d H %d S %d\n", nvalid,
100 		       d.dev, d.cyl, d.head, d.sec);
101 		printf("   sz %d fl %x cyl %d head %d sec %d totsec %"PRId64" sbytes %d\n",
102 		       ed.size, ed.flags, ed.cyl, ed.head, ed.sec,
103 		       ed.totsec, ed.sbytes);
104 #endif
105 
106 		if (d.flags & BIOSDISK_INT13EXT) {
107 			bibg->disk[nvalid].totsec = ed.totsec;
108 			bibg->disk[nvalid].flags |= BI_GEOM_EXTINT13;
109 		}
110 #ifdef BIOSDISK_EXTINFO_V3
111 #ifdef GEOM_DEBUG
112 		printf("   edd_cfg %x, sig %x, len %x, bus %s type %s\n",
113 		       ed.edd_cfg, ed.devpath_sig, ed.devpath_len,
114 		       ed.host_bus, ed.iface_type);
115 #endif
116 
117 		/* The v3.0 stuff will help identify the disks */
118 		if (ed.size >= offsetof(struct biosdisk_ext13info, checksum)
119 		    && ed.devpath_sig == EXTINFO_DEVPATH_SIGNATURE) {
120 			char *cp;
121 
122 			for (cp = (void *)&ed.devpath_sig, cksum = 0;
123 			     cp <= (char *)&ed.checksum; cp++) {
124 				cksum += *cp;
125 			}
126 			if ((cksum & 0xff) != 0)
127 				bibg->disk[nvalid].flags |= BI_GEOM_BADCKSUM;
128 #ifdef GEOM_DEBUG
129 			printf("checksum %x\n", cksum & 0xff);
130 #endif
131 			for (j = 0; ; j++) {
132 				cp = bus_names[j].name;
133 				if (cp == NULL)
134 					break;
135 				if (strncmp(cp, ed.host_bus,
136 					    sizeof(ed.host_bus)) == 0)
137 					break;
138 			}
139 #ifdef GEOM_DEBUG
140 			printf("bus %s (%x)\n", cp ? cp : "null",
141 			       bus_names[j].flag);
142 #endif
143 			bibg->disk[nvalid].flags |= bus_names[j].flag;
144 			for (j = 0; ; j++) {
145 				cp = iface_names[j].name;
146 				if (cp == NULL)
147 					break;
148 				if (strncmp(cp, ed.iface_type,
149 					    sizeof(ed.iface_type)) == 0)
150 					break;
151 			}
152 			bibg->disk[nvalid].flags |= iface_names[j].flag;
153 			/* Dump raw interface path and device path */
154 			bibg->disk[nvalid].interface_path =
155 					ed.interface_path.ip_32[0];
156 			bibg->disk[nvalid].device_path =
157 					ed.device_path.dp_64[0];
158 #ifdef GEOM_DEBUG
159 			printf("device %s (%x) interface %x path %llx\n",
160 			       cp ? cp : "null",
161 			       iface_names[j].flag,
162 			       ed.interface_path.ip_32[0],
163 			       ed.device_path.dp_64[0]);
164 #endif
165 		}
166 #endif
167 
168 		for (j = 0, cksum = 0; j < BIOSDISK_DEFAULT_SECSIZE; j++)
169 			cksum += buf[j];
170 		bibg->disk[nvalid].cksum = cksum;
171 		memcpy(bibg->disk[nvalid].mbrparts, &buf[MBR_PART_OFFSET],
172 		       sizeof(bibg->disk[nvalid].mbrparts));
173 		nvalid++;
174 	}
175 
176 	bibg->num = nvalid;
177 
178 	BI_ADD(bibg, BTINFO_BIOSGEOM, sizeof(struct btinfo_biosgeom)
179 	       + nvalid * sizeof(struct bi_biosgeom_entry));
180 }
181