Skip to content
This repository
Newer
Older
100644 226 lines (203 sloc) 6.646 kb
7edf44a2 » joshwilsdon
2010-10-28 Initial import, moved from old illumos-extra repo.
1 /*
2 * Copyright (c) 2010 Joyent Inc., All rights reserved.
3 *
4 * Prints system parameters from libdevinfo
5 *
6 * Compile: gcc -Wall -o bootparams bootparams.c -ldevinfo
7 */
8
80fee9b6 » joshwilsdon
2012-01-21 OS-831 add support for /tmp/bootparams file to override actual bootpa…
9 #include <err.h>
10 #include <fcntl.h>
7edf44a2 » joshwilsdon
2010-10-28 Initial import, moved from old illumos-extra repo.
11 #include <libdevinfo.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
80fee9b6 » joshwilsdon
2012-01-21 OS-831 add support for /tmp/bootparams file to override actual bootpa…
16 #include <sys/types.h>
17 #include <sys/stat.h>
7edf44a2 » joshwilsdon
2010-10-28 Initial import, moved from old illumos-extra repo.
18
80fee9b6 » joshwilsdon
2012-01-21 OS-831 add support for /tmp/bootparams file to override actual bootpa…
19 void raw_cat(int rfd);
7edf44a2 » joshwilsdon
2010-10-28 Initial import, moved from old illumos-extra repo.
20
21 /*
22 * In this comment typed properties are those of type DI_PROP_TYPE_UNDEF_IT,
23 * DI_PROP_TYPE_BOOLEAN, DI_PROP_TYPE_INT, DI_PROP_TYPE_INT64,
24 * DI_PROP_TYPE_BYTE, and DI_PROP_TYPE_STRING.
25 *
26 * The guessing algorithm is:
27 * 1. If the property is typed and the type is consistent with the value of
28 * the property, then the property is of that type. If the type is not
29 * consistent with value of the property, then the type is treated as
30 * alien to prtconf.
31 * 2. If the property is of type DI_PROP_TYPE_UNKNOWN the following steps
32 * are carried out.
33 * a. If the value of the property is consistent with a string property,
34 * the type of the property is DI_PROP_TYPE_STRING.
35 * b. Otherwise, if the value of the property is consistent with an integer
36 * property, the type of the property is DI_PROP_TYPE_INT.
37 * c. Otherwise, the property type is treated as alien to prtconf.
38 * 3. If the property type is alien to prtconf, then the property value is
39 * read by the appropriate routine for untyped properties and the following
40 * steps are carried out.
41 * a. If the length that the property routine returned is zero, the
42 * property is of type DI_PROP_TYPE_BOOLEAN.
43 * b. Otherwise, if the length that the property routine returned is
44 * positive, then the property value is treated as raw data of type
45 * DI_PROP_TYPE_UNKNOWN.
46 * c. Otherwise, if the length that the property routine returned is
47 * negative, then there is some internal inconsistency and this is
48 * treated as an error and no type is determined.
49 *
50 *
51 * Joyent/jwilsdon: This function was taken and modified from:
52 *
53 * <illumos>/usr/src/cmd/prtconf/pdevinfo.c
54 *
55 */
56 static int
57 prop_type_guess(di_prop_t prop, void **prop_data, int *prop_type)
58 {
59 int len, type;
60
61 type = di_prop_type(prop);
62 switch (type) {
63 case DI_PROP_TYPE_UNDEF_IT:
64 case DI_PROP_TYPE_BOOLEAN:
65 *prop_data = NULL;
66 *prop_type = type;
67 return (0);
68 case DI_PROP_TYPE_INT:
69 len = di_prop_ints(prop, (int **)prop_data);
70 break;
71 case DI_PROP_TYPE_INT64:
72 len = di_prop_int64(prop, (int64_t **)prop_data);
73 break;
74 case DI_PROP_TYPE_BYTE:
75 len = di_prop_bytes(prop, (uchar_t **)prop_data);
76 break;
77 case DI_PROP_TYPE_STRING:
78 len = di_prop_strings(prop, (char **)prop_data);
79 break;
80 case DI_PROP_TYPE_UNKNOWN:
81 len = di_prop_strings(prop, (char **)prop_data);
82 if ((len > 0) && ((*(char **)prop_data)[0] != 0)) {
83 *prop_type = DI_PROP_TYPE_STRING;
84 return (len);
85 }
86
87 len = di_prop_ints(prop, (int **)prop_data);
88 type = DI_PROP_TYPE_INT;
89
90 break;
91 default:
92 len = -1;
93 }
94
95 if (len > 0) {
96 *prop_type = type;
97 return (len);
98 }
99
100 len = di_prop_rawdata(prop, (uchar_t **)prop_data);
101 if (len < 0) {
102 return (-1);
103 } else if (len == 0) {
104 *prop_type = DI_PROP_TYPE_BOOLEAN;
105 return (0);
106 }
107
108 *prop_type = DI_PROP_TYPE_UNKNOWN;
109 return (len);
110 }
111
112 void prt_prop(di_prop_t prop)
113 {
114 int i, prop_type, nitems;
115 char *p;
116 void *prop_data;
117
118 nitems = prop_type_guess(prop, &prop_data, &prop_type);
119
120 /* XXX: currently we only handle single string properties because those are
121 * all that are needed for showing boot parameters.
122 */
123 if ((nitems != 1) || (prop_type != DI_PROP_TYPE_STRING))
124 return;
125
126 printf("%s=", di_prop_name(prop));
127 switch (prop_type) {
128 case DI_PROP_TYPE_INT:
129 for (i = 0; i < nitems - 1; i++)
130 (void) printf("%8.8x.", ((int *)prop_data)[i]);
131 (void) printf("%8.8x", ((int *)prop_data)[i]);
132 break;
133 case DI_PROP_TYPE_INT64:
134 for (i = 0; i < nitems - 1; i++)
135 (void) printf("%16.16llx.",
136 ((long long *)prop_data)[i]);
137 (void) printf("%16.16llx", ((long long *)prop_data)[i]);
138 break;
139 case DI_PROP_TYPE_STRING:
140 p = (char *)prop_data;
141 for (i = 0; i < nitems - 1; i++) {
f45ccb2f » joshwilsdon
2010-11-01 Without single quotes is easier to parse.
142 (void) printf("%s + ", p);
7edf44a2 » joshwilsdon
2010-10-28 Initial import, moved from old illumos-extra repo.
143 p += strlen(p) + 1;
144 }
f45ccb2f » joshwilsdon
2010-11-01 Without single quotes is easier to parse.
145 (void) printf("%s", p);
7edf44a2 » joshwilsdon
2010-10-28 Initial import, moved from old illumos-extra repo.
146 break;
147 default:
148 for (i = 0; i < nitems - 1; i++)
149 (void) printf("%2.2x.",
150 ((uint8_t *)prop_data)[i]);
151 (void) printf("%2.2x", ((uint8_t *)prop_data)[i]);
152 }
153 printf("\n");
154 }
155
156 int prt_node(di_node_t node, void *arg)
157 {
158 di_prop_t prop = DI_PROP_NIL;
159
160 if (strcmp(di_node_name(node), "i86pc") == 0) {
161 while ((prop = di_prop_next(node, prop)) != DI_PROP_NIL) {
162 prt_prop(prop);
163 }
164 return (DI_WALK_TERMINATE);
165 }
166 return (DI_WALK_CONTINUE);
167 }
168
80fee9b6 » joshwilsdon
2012-01-21 OS-831 add support for /tmp/bootparams file to override actual bootpa…
169 /*
170 * ported from NetBSD's cat rev 1.47
171 */
172 void raw_cat(int rfd)
173 {
174 static char *buf;
175 static char fb_buf[BUFSIZ];
176 static size_t bsize;
177
178 ssize_t nr, nw, off;
179 int wfd;
180
181 wfd = fileno(stdout);
182 if (buf == NULL) {
183 struct stat sbuf;
184
185 if (fstat(wfd, &sbuf) == 0 &&
fa1dcf1b » wesolows
2012-05-15 smartos-live#2 Fixed identation, spaces should be tabs
186 sbuf.st_blksize > (long)sizeof(fb_buf)) {
80fee9b6 » joshwilsdon
2012-01-21 OS-831 add support for /tmp/bootparams file to override actual bootpa…
187 bsize = sbuf.st_blksize;
188 buf = malloc(bsize);
189 }
190 if (buf == NULL) {
191 bsize = sizeof(fb_buf);
192 buf = fb_buf;
193 }
194 }
195 while ((nr = read(rfd, buf, bsize)) > 0) {
196 for (off = 0; nr; nr -= nw, off += nw) {
197 if ((nw = write(wfd, buf + off, (size_t)nr)) < 0) {
198 err(EXIT_FAILURE, "stdout");
199 }
200 }
201 }
202 }
203
7edf44a2 » joshwilsdon
2010-10-28 Initial import, moved from old illumos-extra repo.
204 int main()
205 {
206 di_node_t root_node;
207
80fee9b6 » joshwilsdon
2012-01-21 OS-831 add support for /tmp/bootparams file to override actual bootpa…
208 int fd;
209
210 if ((fd = open("/tmp/bootparams", O_RDONLY)) != -1) {
211 /* If this file exists it acts as a replacement for the normal data */
212 raw_cat(fd);
213 close(fd);
214 } else {
215 /* We didn't have or couldn't open the file, so just get actual params */
216 root_node = di_init("/", (DINFOSUBTREE | DINFOPROP));
217 if (root_node == DI_NODE_NIL) {
218 fprintf(stderr, "di_init() failed\n");
219 exit(1);
220 }
221 di_walk_node(root_node, DI_WALK_CLDFIRST, NULL, prt_node);
222 di_fini(root_node);
7edf44a2 » joshwilsdon
2010-10-28 Initial import, moved from old illumos-extra repo.
223 }
224
225 exit(EXIT_SUCCESS);
226 }
Something went wrong with that request. Please try again.