-
Notifications
You must be signed in to change notification settings - Fork 813
Expand file tree
/
Copy pathtopo_usb_metadata.c
More file actions
471 lines (410 loc) · 12.6 KB
/
Copy pathtopo_usb_metadata.c
File metadata and controls
471 lines (410 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright (c) 2018, Joyent, Inc.
*/
/*
* This module parses the private file format used for describing
* platform-specific USB overrides.
*
* FILE FORMAT
* -----------
*
* A USB topology file contains a series of lines which are separated by new
* lines. Leading and trailing whitespace on a line are ignored and empty lines
* are ignored as well. The '#' character is used as a comment character. There
* are a series of keywords that are supported which are used to indicate
* different control aspects. These keywords are all treated in a
* case-insensitive fashion. There are both top-level keywords and keywords that
* are only accepted within the context of a scope.
*
* Top-level keywords
* ------------------
*
* The following keywords are accepted, but must not be found inside a nested
* scope:
*
* 'disable-acpi' Disables the use of ACPI for this platform. This
* includes getting information about the port's
* type and visibility. This implies
* 'disable-acpi-match'.
*
* 'disable-acpi-match' Disables the act of trying to match ports based
* on ACPI.
*
*
* 'enable-acpi-match' Explicitly enables ACPI port matching on the
* platform based on ACPI.
*
* 'enable-metadata-match' Enables port matching based on metadata. This is
* most commonly used on platforms that have ehci
* and xhci controllers that share ports.
*
* 'port' Begins a port stanza that describes a single
* physical port. This stanza will continue until
* the 'end-port' keyword is encountered.
*
* Port Keywords
* -------------
*
* Some port keywords take arguments and others do not. When an argument exists,
* will occur on the subsequent line. Ports have a series of directives that
* describe metadata as well as directives that describe how to determine the
* port.
*
* 'label' Indicates that the next line contains the
* human-readable label for the port.
*
* 'chassis' Indicates that this port is part of the chassis
* and should not be enumerated elsewhere.
*
* 'external' Indicates that this port is externally visible.
*
* 'internal' Indicates that this port is internal to the
* chassis and cannot be accessed without opening
* the chassis.
*
* 'port-type' Indicates that the next line contains a number
* which corresponds to the type of the port. The
* port numbers are based on the ACPI table and
* may be in either base 10 or hexadecimal.
*
* 'acpi-path' Indicates that the next line contains an ACPI
* based name that matches the port.
*
* 'end-port' Closes the port-clause.
*/
#include <libnvpair.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fm/topo_list.h>
#include <fm/topo_mod.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <libnvpair.h>
#include <sys/debug.h>
#include <ctype.h>
#include <unistd.h>
#include "topo_usb.h"
#include "topo_usb_int.h"
/*
* Maximum number of characters we expect to encounter in a line.
*/
#define TOPO_USB_META_LINE_MAX 1000
/*
* This constant is the default set of flags that we'd like to apply when there
* is no configuration file present to determine the desired behavior. If one is
* present, we always defer to what it asks for.
*
* It's a difficult decision to enable ACPI by default or not. Unfortunately,
* we've encountered some systems where the ACPI information is wrong. However,
* we've encountered a larger number where it is correct. When it's correct,
* this greatly simplifies some of the work that we have to do. Our default
* disposition at the moment is to opt to decide its correct as that ends up
* giving us much better information.
*/
#define USB_TOPO_META_DEFAULT_FLAGS TOPO_USB_M_ACPI_MATCH
typedef enum {
TOPO_USB_P_START,
TOPO_USB_P_PORT,
TOPO_USB_P_LABEL,
TOPO_USB_P_PORT_TYPE,
TOPO_USB_P_ACPI_PATH
} topo_usb_parse_state_t;
typedef struct topo_usb_parse {
topo_usb_parse_state_t tp_state;
topo_list_t *tp_ports;
topo_usb_meta_port_t *tp_cport;
topo_usb_meta_flags_t tp_flags;
} topo_usb_parse_t;
/*
* Read the next line in the file with content. Trim trailing and leading
* whitespace and trim comments out. If this results in an empty line, read the
* next. Returns zero if we hit EOF. Otherwise, returns one if data, or negative
* one if an error occurred.
*/
static int
topo_usb_getline(topo_mod_t *mod, char *buf, size_t len, FILE *f, char **first)
{
while (fgets(buf, len, f) != NULL) {
char *c;
size_t i;
if ((c = strrchr(buf, '\n')) == NULL) {
topo_mod_dprintf(mod, "failed to find new line in "
"metadata file");
return (-1);
}
while (isspace(*c) != 0 && c >= buf) {
*c = '\0';
c--;
continue;
}
if ((c = strchr(buf, '#')) != 0) {
*c = '\0';
}
for (i = 0; buf[i] != '\0'; i++) {
if (isspace(buf[i]) == 0)
break;
}
if (buf[i] == '\0')
continue;
*first = &buf[i];
return (1);
}
return (0);
}
static boolean_t
topo_usb_parse_start(topo_mod_t *mod, topo_usb_parse_t *parse, const char *line)
{
topo_usb_meta_port_t *port;
VERIFY3S(parse->tp_state, ==, TOPO_USB_P_START);
VERIFY3P(parse->tp_cport, ==, NULL);
if (strcasecmp(line, "disable-acpi") == 0) {
parse->tp_flags |= TOPO_USB_M_NO_ACPI;
parse->tp_flags &= ~TOPO_USB_M_ACPI_MATCH;
return (B_TRUE);
} else if (strcasecmp(line, "disable-acpi-match") == 0) {
parse->tp_flags &= ~TOPO_USB_M_ACPI_MATCH;
return (B_TRUE);
} else if (strcasecmp(line, "enable-acpi-match") == 0) {
parse->tp_flags |= TOPO_USB_M_ACPI_MATCH;
return (B_TRUE);
} else if (strcasecmp(line, "enable-metadata-match") == 0) {
parse->tp_flags |= TOPO_USB_M_METADATA_MATCH;
return (B_TRUE);
} else if (strcasecmp(line, "port") != 0) {
topo_mod_dprintf(mod, "expected 'port', encountered %s",
line);
return (B_FALSE);
}
if ((port = topo_mod_zalloc(mod, sizeof (topo_usb_meta_port_t))) ==
NULL) {
topo_mod_dprintf(mod, "failed to allocate metadata port");
return (B_FALSE);
}
port->tmp_port_type = 0xff;
parse->tp_cport = port;
parse->tp_state = TOPO_USB_P_PORT;
return (B_TRUE);
}
static boolean_t
topo_usb_parse_port(topo_mod_t *mod, topo_usb_parse_t *parse, const char *line)
{
VERIFY3S(parse->tp_state, ==, TOPO_USB_P_PORT);
VERIFY3P(parse->tp_cport, !=, NULL);
if (strcasecmp(line, "label") == 0) {
parse->tp_state = TOPO_USB_P_LABEL;
} else if (strcasecmp(line, "chassis") == 0) {
parse->tp_cport->tmp_flags |= TOPO_USB_F_CHASSIS;
} else if (strcasecmp(line, "external") == 0) {
parse->tp_cport->tmp_flags |= TOPO_USB_F_EXTERNAL;
} else if (strcasecmp(line, "internal") == 0) {
parse->tp_cport->tmp_flags |= TOPO_USB_F_INTERNAL;
} else if (strcasecmp(line, "port-type") == 0) {
parse->tp_state = TOPO_USB_P_PORT_TYPE;
} else if (strcasecmp(line, "acpi-path") == 0) {
parse->tp_state = TOPO_USB_P_ACPI_PATH;
} else if (strcasecmp(line, "end-port") == 0) {
topo_list_append(parse->tp_ports, parse->tp_cport);
parse->tp_cport = NULL;
parse->tp_state = TOPO_USB_P_START;
} else {
topo_mod_dprintf(mod, "illegal directive in port block: %s",
line);
return (B_FALSE);
}
return (B_TRUE);
}
static boolean_t
topo_usb_parse_label(topo_mod_t *mod, topo_usb_parse_t *parse, const char *line)
{
size_t i, len;
VERIFY3S(parse->tp_state, ==, TOPO_USB_P_LABEL);
len = strlen(line);
for (i = 0; i < len; i++) {
if (isascii(line[i]) == 0 || isprint(line[i]) == 0) {
topo_mod_dprintf(mod, "label character %llu is "
"invalid: 0x%x", i, line[i]);
return (B_FALSE);
}
}
if (parse->tp_cport->tmp_label != NULL) {
topo_mod_strfree(mod, parse->tp_cport->tmp_label);
}
if ((parse->tp_cport->tmp_label = topo_mod_strdup(mod, line)) == NULL) {
topo_mod_dprintf(mod, "failed to duplicate label for port");
return (B_FALSE);
}
parse->tp_state = TOPO_USB_P_PORT;
return (B_TRUE);
}
static boolean_t
topo_usb_parse_port_type(topo_mod_t *mod, topo_usb_parse_t *parse,
const char *line)
{
unsigned long val;
char *eptr;
VERIFY3S(parse->tp_state, ==, TOPO_USB_P_PORT_TYPE);
errno = 0;
val = strtoul(line, &eptr, 0);
if (errno != 0 || *eptr != '\0' || val >= UINT_MAX) {
topo_mod_dprintf(mod, "encountered bad value for port-type "
"line: %s", line);
return (B_FALSE);
}
parse->tp_cport->tmp_port_type = (uint_t)val;
parse->tp_state = TOPO_USB_P_PORT;
return (B_TRUE);
}
static boolean_t
topo_usb_parse_path(topo_mod_t *mod, topo_usb_parse_t *parse,
topo_usb_path_type_t ptype, const char *line)
{
char *fspath;
topo_usb_meta_port_path_t *path;
VERIFY(parse->tp_state == TOPO_USB_P_ACPI_PATH);
VERIFY3P(parse->tp_cport, !=, NULL);
if ((fspath = topo_mod_strdup(mod, line)) == NULL) {
topo_mod_dprintf(mod, "failed to duplicate path");
return (B_FALSE);
}
if ((path = topo_mod_zalloc(mod, sizeof (topo_usb_meta_port_path_t))) ==
NULL) {
topo_mod_dprintf(mod, "failed to allocate meta port path "
"structure");
topo_mod_strfree(mod, fspath);
return (B_FALSE);
}
path->tmpp_type = ptype;
path->tmpp_path = fspath;
topo_list_append(&parse->tp_cport->tmp_paths, path);
parse->tp_state = TOPO_USB_P_PORT;
return (B_TRUE);
}
void
topo_usb_free_metadata(topo_mod_t *mod, topo_list_t *metadata)
{
topo_usb_meta_port_t *mp;
while ((mp = topo_list_next(metadata)) != NULL) {
topo_usb_meta_port_path_t *path;
while ((path = topo_list_next((&mp->tmp_paths))) != NULL) {
topo_list_delete(&mp->tmp_paths, path);
topo_mod_strfree(mod, path->tmpp_path);
topo_mod_free(mod, path,
sizeof (topo_usb_meta_port_path_t));
}
topo_list_delete(metadata, mp);
topo_mod_strfree(mod, mp->tmp_label);
topo_mod_free(mod, mp, sizeof (topo_usb_meta_port_t));
}
}
int
topo_usb_load_metadata(topo_mod_t *mod, tnode_t *pnode, topo_list_t *list,
topo_usb_meta_flags_t *flagsp)
{
int fd;
FILE *f = NULL;
char buf[TOPO_USB_META_LINE_MAX], *first, *prod;
int ret;
topo_usb_parse_t parse;
char pbuf[PATH_MAX];
*flagsp = USB_TOPO_META_DEFAULT_FLAGS;
/*
* If no product string, just leave it as is and don't attempt to get
* metadata.
*/
if ((topo_prop_get_string(pnode, FM_FMRI_AUTHORITY,
FM_FMRI_AUTH_PRODUCT, &prod, &ret)) != 0) {
topo_mod_dprintf(mod, "skipping metadata load: failed to get "
"auth");
return (0);
}
if (snprintf(pbuf, sizeof (pbuf), "maps/%s-usb.usbtopo", prod) >=
sizeof (pbuf)) {
topo_mod_dprintf(mod, "skipping metadata load: product name "
"too long");
topo_mod_strfree(mod, prod);
return (0);
}
topo_mod_strfree(mod, prod);
if ((fd = topo_mod_file_search(mod, pbuf, O_RDONLY)) < 0) {
topo_mod_dprintf(mod, "skipping metadata load: couldn't find "
"%s", pbuf);
return (0);
}
if ((f = fdopen(fd, "r")) == NULL) {
topo_mod_dprintf(mod, "failed to fdopen metadata file %s: %s",
pbuf, strerror(errno));
VERIFY0(close(fd));
ret = topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
goto err;
}
bzero(&parse, sizeof (parse));
parse.tp_ports = list;
parse.tp_state = TOPO_USB_P_START;
while ((ret = topo_usb_getline(mod, buf, sizeof (buf), f, &first)) !=
0) {
if (ret == -1) {
ret = topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
goto err;
}
switch (parse.tp_state) {
case TOPO_USB_P_START:
if (!topo_usb_parse_start(mod, &parse, first)) {
ret = topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
goto err;
}
break;
case TOPO_USB_P_PORT:
if (!topo_usb_parse_port(mod, &parse, first)) {
ret = topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
goto err;
}
break;
case TOPO_USB_P_LABEL:
if (!topo_usb_parse_label(mod, &parse, first)) {
ret = topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
goto err;
}
break;
case TOPO_USB_P_PORT_TYPE:
if (!topo_usb_parse_port_type(mod, &parse, first)) {
ret = topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
goto err;
}
break;
case TOPO_USB_P_ACPI_PATH:
if (!topo_usb_parse_path(mod, &parse, TOPO_USB_T_ACPI,
first)) {
ret = topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
goto err;
}
break;
}
}
if (parse.tp_state != TOPO_USB_P_START) {
topo_mod_dprintf(mod, "metadata file didn't end in correct "
"state, failing");
ret = topo_mod_seterrno(mod, EMOD_UKNOWN_ENUM);
goto err;
}
topo_mod_dprintf(mod, "successfully loaded metadata %s", pbuf);
VERIFY0(fclose(f));
*flagsp = parse.tp_flags;
return (0);
err:
if (f != NULL)
VERIFY0(fclose(f));
topo_usb_free_metadata(mod, list);
return (ret);
}