1/*	$NetBSD: bootinfo_biosgeom.c,v 1.24 2019/08/01 13:11:03 manu 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
41static 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} };
47static 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
59void
60bi_getbiosgeom(void)
61{
62	struct btinfo_biosgeom *bibg;
63	size_t bibg_len = sizeof(*bibg);
64	int i, j, nvalid;
65	int nhd;
66	unsigned int cksum;
67	struct biosdisk_ll d;
68	struct biosdisk_extinfo ed;
69	char buf[BIOSDISK_DEFAULT_SECSIZE];
70
71	nhd = get_harddrives();
72#ifdef GEOM_DEBUG
73	printf("nhd %d\n", nhd);
74#endif
75
76	bibg_len += nhd * sizeof(struct bi_biosgeom_entry);
77	bibg = alloc(bibg_len);
78	if (bibg == NULL)
79		return;
80
81	for (i = nvalid = 0; i < MAX_BIOSDISKS && nvalid < nhd; i++) {
82		d.dev = 0x80 + i;
83
84		if (set_geometry(&d, &ed))
85			continue;
86		memset(&bibg->disk[nvalid], 0, sizeof(bibg->disk[nvalid]));
87
88		bibg->disk[nvalid].sec = d.sec;
89		bibg->disk[nvalid].head = d.head;
90		bibg->disk[nvalid].cyl = d.cyl;
91		bibg->disk[nvalid].dev = d.dev;
92
93		if (readsects(&d, 0, 1, buf, 0)) {
94			bibg->disk[nvalid].flags |= BI_GEOM_INVALID;
95			nvalid++;
96			continue;
97		}
98
99#ifdef GEOM_DEBUG
100		printf("#%d: %x: C %d H %d S %d\n", nvalid,
101		       d.dev, d.cyl, d.head, d.sec);
102		printf("   sz %d fl %x cyl %d head %d sec %d totsec %"PRId64" sbytes %d\n",
103		       ed.size, ed.flags, ed.cyl, ed.head, ed.sec,
104		       ed.totsec, ed.sbytes);
105#endif
106
107		if (d.flags & BIOSDISK_INT13EXT) {
108			bibg->disk[nvalid].totsec = ed.totsec;
109			bibg->disk[nvalid].flags |= BI_GEOM_EXTINT13;
110		}
111#ifdef BIOSDISK_EXTINFO_V3
112#ifdef GEOM_DEBUG
113		printf("   edd_cfg %x, sig %x, len %x, bus %s type %s\n",
114		       ed.edd_cfg, ed.devpath_sig, ed.devpath_len,
115		       ed.host_bus, ed.iface_type);
116#endif
117
118		/* The v3.0 stuff will help identify the disks */
119		if (ed.size >= offsetof(struct biosdisk_ext13info, checksum)
120		    && ed.devpath_sig == EXTINFO_DEVPATH_SIGNATURE) {
121			char *cp;
122
123			for (cp = (void *)&ed.devpath_sig, cksum = 0;
124			     cp <= (char *)&ed.checksum; cp++) {
125				cksum += *cp;
126			}
127			if ((cksum & 0xff) != 0)
128				bibg->disk[nvalid].flags |= BI_GEOM_BADCKSUM;
129#ifdef GEOM_DEBUG
130			printf("checksum %x\n", cksum & 0xff);
131#endif
132			for (j = 0; ; j++) {
133				cp = bus_names[j].name;
134				if (cp == NULL)
135					break;
136				if (strncmp(cp, ed.host_bus,
137					    sizeof(ed.host_bus)) == 0)
138					break;
139			}
140#ifdef GEOM_DEBUG
141			printf("bus %s (%x)\n", cp ? cp : "null",
142			       bus_names[j].flag);
143#endif
144			bibg->disk[nvalid].flags |= bus_names[j].flag;
145			for (j = 0; ; j++) {
146				cp = iface_names[j].name;
147				if (cp == NULL)
148					break;
149				if (strncmp(cp, ed.iface_type,
150					    sizeof(ed.iface_type)) == 0)
151					break;
152			}
153			bibg->disk[nvalid].flags |= iface_names[j].flag;
154			/* Dump raw interface path and device path */
155			bibg->disk[nvalid].interface_path =
156					ed.interface_path.ip_32[0];
157			bibg->disk[nvalid].device_path =
158					ed.device_path.dp_64[0];
159#ifdef GEOM_DEBUG
160			printf("device %s (%x) interface %x path %llx\n",
161			       cp ? cp : "null",
162			       iface_names[j].flag,
163			       ed.interface_path.ip_32[0],
164			       ed.device_path.dp_64[0]);
165#endif
166		}
167#endif
168
169		for (j = 0, cksum = 0; j < BIOSDISK_DEFAULT_SECSIZE; j++)
170			cksum += buf[j];
171		bibg->disk[nvalid].cksum = cksum;
172		memcpy(bibg->disk[nvalid].mbrparts, &buf[MBR_PART_OFFSET],
173		       sizeof(bibg->disk[nvalid].mbrparts));
174		nvalid++;
175	}
176
177	bibg->num = nvalid;
178
179	if (nvalid < nhd)
180		bibg_len -= (nhd - nvalid) * sizeof(struct bi_biosgeom_entry);
181
182	BI_ADD(bibg, BTINFO_BIOSGEOM, bibg_len);
183}
184