xref: /freebsd/stand/i386/libi386/biossmap.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 /*
29  * Obtain memory configuration information from the BIOS
30  */
31 #include <stand.h>
32 #include <sys/param.h>
33 #include <sys/linker.h>
34 #include <sys/queue.h>
35 #include <sys/stddef.h>
36 #include <machine/metadata.h>
37 #include <machine/pc/bios.h>
38 #include "bootstrap.h"
39 #include "libi386.h"
40 #include "btxv86.h"
41 
42 struct smap_buf {
43 	struct bios_smap	smap;
44 	uint32_t		xattr;	/* Extended attribute from ACPI 3.0 */
45 	STAILQ_ENTRY(smap_buf)	bufs;
46 };
47 
48 #define	SMAP_BUFSIZE		offsetof(struct smap_buf, bufs)
49 
50 static struct bios_smap		*smapbase;
51 static uint32_t			*smapattr;
52 static u_int			smaplen;
53 
54 void
55 bios_getsmap(void)
56 {
57 	struct smap_buf		buf;
58 	STAILQ_HEAD(smap_head, smap_buf) head =
59 	    STAILQ_HEAD_INITIALIZER(head);
60 	struct smap_buf		*cur, *next;
61 	u_int			n, x;
62 
63 	STAILQ_INIT(&head);
64 	n = 0;
65 	x = 0;
66 	v86.ebx = 0;
67 	do {
68 		v86.ctl = V86_FLAGS;
69 		v86.addr = 0x15;
70 		v86.eax = 0xe820;	/* int 0x15 function 0xe820 */
71 		v86.ecx = SMAP_BUFSIZE;
72 		v86.edx = SMAP_SIG;
73 		v86.es = VTOPSEG(&buf);
74 		v86.edi = VTOPOFF(&buf);
75 		v86int();
76 		if (V86_CY(v86.efl) || v86.eax != SMAP_SIG ||
77 		    v86.ecx < sizeof(buf.smap) || v86.ecx > SMAP_BUFSIZE)
78 			break;
79 
80 		next = malloc(sizeof(*next));
81 		if (next == NULL)
82 			break;
83 		next->smap = buf.smap;
84 		if (v86.ecx == SMAP_BUFSIZE) {
85 			next->xattr = buf.xattr;
86 			x++;
87 		}
88 		STAILQ_INSERT_TAIL(&head, next, bufs);
89 		n++;
90 	} while (v86.ebx != 0);
91 	smaplen = n;
92 
93 	if (smaplen > 0) {
94 		smapbase = malloc(smaplen * sizeof(*smapbase));
95 		if (smapbase != NULL) {
96 			n = 0;
97 			STAILQ_FOREACH(cur, &head, bufs)
98 				smapbase[n++] = cur->smap;
99 		}
100 		if (smaplen == x) {
101 			smapattr = malloc(smaplen * sizeof(*smapattr));
102 			if (smapattr != NULL) {
103 				n = 0;
104 				STAILQ_FOREACH(cur, &head, bufs)
105 					smapattr[n++] = cur->xattr &
106 					    SMAP_XATTR_MASK;
107 			}
108 		} else
109 			smapattr = NULL;
110 		cur = STAILQ_FIRST(&head);
111 		while (cur != NULL) {
112 			next = STAILQ_NEXT(cur, bufs);
113 			free(cur);
114 			cur = next;
115 		}
116 	}
117 }
118 
119 void
120 bios_addsmapdata(struct preloaded_file *kfp)
121 {
122 	size_t			size;
123 
124 	if (smapbase == NULL || smaplen == 0)
125 		return;
126 	size = smaplen * sizeof(*smapbase);
127 	file_addmetadata(kfp, MODINFOMD_SMAP, size, smapbase);
128 	if (smapattr != NULL) {
129 		size = smaplen * sizeof(*smapattr);
130 		file_addmetadata(kfp, MODINFOMD_SMAP_XATTR, size, smapattr);
131 	}
132 }
133 
134 COMMAND_SET(smap, "smap", "show BIOS SMAP", command_smap);
135 
136 static int
137 command_smap(int argc, char *argv[])
138 {
139 	u_int			i;
140 
141 	if (smapbase == NULL || smaplen == 0)
142 		return (CMD_ERROR);
143 	if (smapattr != NULL)
144 		for (i = 0; i < smaplen; i++)
145 			printf("SMAP type=%02x base=%016llx len=%016llx attr=%02x\n",
146 			    (unsigned int)smapbase[i].type,
147 			    (unsigned long long)smapbase[i].base,
148 			    (unsigned long long)smapbase[i].length,
149 			    (unsigned int)smapattr[i]);
150 	else
151 		for (i = 0; i < smaplen; i++)
152 			printf("SMAP type=%02x base=%016llx len=%016llx\n",
153 			    (unsigned int)smapbase[i].type,
154 			    (unsigned long long)smapbase[i].base,
155 			    (unsigned long long)smapbase[i].length);
156 	return (CMD_OK);
157 }
158