summaryrefslogtreecommitdiffstats
path: root/dmiopt.c
blob: d08288fe804b27a5c05072a752619244825f7709 (plain)
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
/*
 * Command line handling of dmidecode
 * This file is part of the dmidecode project.
 *
 *   Copyright (C) 2005-2008 Jean Delvare <jdelvare@suse.de>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <getopt.h>

#include "config.h"
#include "types.h"
#include "util.h"
#include "dmidecode.h"
#include "dmiopt.h"


/* Options are global */
struct opt opt;


/*
 * Handling of option --type
 */

struct type_keyword
{
	const char *keyword;
	const u8 *type;
};

static const u8 opt_type_bios[] = { 0, 13, 255 };
static const u8 opt_type_system[] = { 1, 12, 15, 23, 32, 255 };
static const u8 opt_type_baseboard[] = { 2, 10, 41, 255 };
static const u8 opt_type_chassis[] = { 3, 255 };
static const u8 opt_type_processor[] = { 4, 255 };
static const u8 opt_type_memory[] = { 5, 6, 16, 17, 255 };
static const u8 opt_type_cache[] = { 7, 255 };
static const u8 opt_type_connector[] = { 8, 255 };
static const u8 opt_type_slot[] = { 9, 255 };

static const struct type_keyword opt_type_keyword[] = {
	{ "bios", opt_type_bios },
	{ "system", opt_type_system },
	{ "baseboard", opt_type_baseboard },
	{ "chassis", opt_type_chassis },
	{ "processor", opt_type_processor },
	{ "memory", opt_type_memory },
	{ "cache", opt_type_cache },
	{ "connector", opt_type_connector },
	{ "slot", opt_type_slot },
};

static void print_opt_type_list(void)
{
	unsigned int i;

	fprintf(stderr, "Valid type keywords are:\n");
	for (i = 0; i < ARRAY_SIZE(opt_type_keyword); i++)
	{
		fprintf(stderr, "  %s\n", opt_type_keyword[i].keyword);
	}
}

static u8 *parse_opt_type(u8 *p, const char *arg)
{
	unsigned int i;

	/* Allocate memory on first call only */
	if (p == NULL)
	{
		p = (u8 *)calloc(256, sizeof(u8));
		if (p == NULL)
		{
			perror("calloc");
			return NULL;
		}
	}

	/* First try as a keyword */
	for (i = 0; i < ARRAY_SIZE(opt_type_keyword); i++)
	{
		if (!strcasecmp(arg, opt_type_keyword[i].keyword))
		{
			int j = 0;
			while (opt_type_keyword[i].type[j] != 255)
				p[opt_type_keyword[i].type[j++]] = 1;
			goto found;
		}
	}

	/* Else try as a number */
	while (*arg != '\0')
	{
		unsigned long val;
		char *next;

		val = strtoul(arg, &next, 0);
		if (next == arg || (*next != '\0' && *next != ',' && *next != ' '))
		{
			fprintf(stderr, "Invalid type keyword: %s\n", arg);
			print_opt_type_list();
			goto exit_free;
		}
		if (val > 0xff)
		{
			fprintf(stderr, "Invalid type number: %lu\n", val);
			goto exit_free;
		}

		p[val] = 1;
		arg = next;
		while (*arg == ',' || *arg == ' ')
			arg++;
	}

found:
	return p;

exit_free:
	free(p);
	return NULL;
}


/*
 * Handling of option --string
 */

/* This lookup table could admittedly be reworked for improved performance.
   Due to the low count of items in there at the moment, it did not seem
   worth the additional code complexity though. */
static const struct string_keyword opt_string_keyword[] = {
	{ "bios-vendor", 0, 0x04 },
	{ "bios-version", 0, 0x05 },
	{ "bios-release-date", 0, 0x08 },
	{ "bios-revision", 0, 0x15 },		/* 0x14 and 0x15 */
	{ "firmware-revision", 0, 0x17 },	/* 0x16 and 0x17 */
	{ "system-manufacturer", 1, 0x04 },
	{ "system-product-name", 1, 0x05 },
	{ "system-version", 1, 0x06 },
	{ "system-serial-number", 1, 0x07 },
	{ "system-uuid", 1, 0x08 },             /* dmi_system_uuid() */
	{ "system-sku-number", 1, 0x19 },
	{ "system-family", 1, 0x1a },
	{ "baseboard-manufacturer", 2, 0x04 },
	{ "baseboard-product-name", 2, 0x05 },
	{ "baseboard-version", 2, 0x06 },
	{ "baseboard-serial-number", 2, 0x07 },
	{ "baseboard-asset-tag", 2, 0x08 },
	{ "chassis-manufacturer", 3, 0x04 },
	{ "chassis-type", 3, 0x05 },            /* dmi_chassis_type() */
	{ "chassis-version", 3, 0x06 },
	{ "chassis-serial-number", 3, 0x07 },
	{ "chassis-asset-tag", 3, 0x08 },
	{ "processor-family", 4, 0x06 },        /* dmi_processor_family() */
	{ "processor-manufacturer", 4, 0x07 },
	{ "processor-version", 4, 0x10 },
	{ "processor-frequency", 4, 0x16 },     /* dmi_processor_frequency() */
};

/* This is a template, 3rd field is set at runtime. */
static struct string_keyword opt_oem_string_keyword =
	{ NULL, 11, 0x00 };

static void print_opt_string_list(void)
{
	unsigned int i;

	fprintf(stderr, "Valid string keywords are:\n");
	for (i = 0; i < ARRAY_SIZE(opt_string_keyword); i++)
	{
		fprintf(stderr, "  %s\n", opt_string_keyword[i].keyword);
	}
}

static int parse_opt_string(const char *arg)
{
	unsigned int i;

	if (opt.string)
	{
		fprintf(stderr, "Only one string can be specified\n");
		return -1;
	}

	for (i = 0; i < ARRAY_SIZE(opt_string_keyword); i++)
	{
		if (!strcasecmp(arg, opt_string_keyword[i].keyword))
		{
			opt.string = &opt_string_keyword[i];
			return 0;
		}
	}

	fprintf(stderr, "Invalid string keyword: %s\n", arg);
	print_opt_string_list();
	return -1;
}

static int parse_opt_oem_string(const char *arg)
{
	unsigned long val;
	char *next;

	if (opt.string)
	{
		fprintf(stderr, "Only one string can be specified\n");
		return -1;
	}

	/* Return the number of OEM strings */
	if (strcmp(arg, "count") == 0)
		goto done;

	val = strtoul(arg, &next, 10);
	if (next == arg  || *next != '\0' || val == 0x00 || val > 0xff)
	{
		fprintf(stderr, "Invalid OEM string number: %s\n", arg);
		return -1;
	}

	opt_oem_string_keyword.offset = val;
done:
	opt.string = &opt_oem_string_keyword;
	return 0;
}

static u32 parse_opt_handle(const char *arg)
{
	u32 val;
	char *next;

	val = strtoul(arg, &next, 0);
	if (next == arg || *next != '\0' || val > 0xffff)
	{
		fprintf(stderr, "Invalid handle number: %s\n", arg);
		return ~0;
	}
	return val;
}

/*
 * Command line options handling
 */

/* Return -1 on error, 0 on success */
int parse_command_line(int argc, char * const argv[])
{
	int option;
	const char *optstring = "d:hqs:t:uH:V";
	struct option longopts[] = {
		{ "dev-mem", required_argument, NULL, 'd' },
		{ "help", no_argument, NULL, 'h' },
		{ "quiet", no_argument, NULL, 'q' },
		{ "string", required_argument, NULL, 's' },
		{ "type", required_argument, NULL, 't' },
		{ "dump", no_argument, NULL, 'u' },
		{ "dump-bin", required_argument, NULL, 'B' },
		{ "from-dump", required_argument, NULL, 'F' },
		{ "handle", required_argument, NULL, 'H' },
		{ "oem-string", required_argument, NULL, 'O' },
		{ "no-sysfs", no_argument, NULL, 'S' },
		{ "version", no_argument, NULL, 'V' },
		{ NULL, 0, NULL, 0 }
	};

	while ((option = getopt_long(argc, argv, optstring, longopts, NULL)) != -1)
		switch (option)
		{
			case 'B':
				opt.flags |= FLAG_DUMP_BIN;
				opt.dumpfile = optarg;
				break;
			case 'F':
				opt.flags |= FLAG_FROM_DUMP;
				opt.dumpfile = optarg;
				break;
			case 'd':
				opt.devmem = optarg;
				break;
			case 'h':
				opt.flags |= FLAG_HELP;
				break;
			case 'q':
				opt.flags |= FLAG_QUIET;
				break;
			case 's':
				if (parse_opt_string(optarg) < 0)
					return -1;
				opt.flags |= FLAG_QUIET;
				break;
			case 'O':
				if (parse_opt_oem_string(optarg) < 0)
					return -1;
				opt.flags |= FLAG_QUIET;
				break;
			case 't':
				opt.type = parse_opt_type(opt.type, optarg);
				if (opt.type == NULL)
					return -1;
				break;
			case 'H':
				opt.handle = parse_opt_handle(optarg);
				if (opt.handle  == ~0U)
					return -1;
				break;
			case 'u':
				opt.flags |= FLAG_DUMP;
				break;
			case 'S':
				opt.flags |= FLAG_NO_SYSFS;
				break;
			case 'V':
				opt.flags |= FLAG_VERSION;
				break;
			case '?':
				switch (optopt)
				{
					case 's':
						fprintf(stderr, "String keyword expected\n");
						print_opt_string_list();
						break;
					case 't':
						fprintf(stderr, "Type number or keyword expected\n");
						print_opt_type_list();
						break;
				}
				return -1;
		}

	/* Check for mutually exclusive output format options */
	if ((opt.string != NULL) + (opt.type != NULL)
	  + !!(opt.flags & FLAG_DUMP_BIN) + (opt.handle != ~0U) > 1)
	{
		fprintf(stderr, "Options --string, --type, --handle and --dump-bin are mutually exclusive\n");
		return -1;
	}

	if ((opt.flags & FLAG_FROM_DUMP) && (opt.flags & FLAG_DUMP_BIN))
	{
		fprintf(stderr, "Options --from-dump and --dump-bin are mutually exclusive\n");
		return -1;
	}

	return 0;
}

void print_help(void)
{
	static const char *help =
		"Usage: dmidecode [OPTIONS]\n"
		"Options are:\n"
		" -d, --dev-mem FILE     Read memory from device FILE (default: " DEFAULT_MEM_DEV ")\n"
		" -h, --help             Display this help text and exit\n"
		" -q, --quiet            Less verbose output\n"
		" -s, --string KEYWORD   Only display the value of the given DMI string\n"
		" -t, --type TYPE        Only display the entries of given type\n"
		" -H, --handle HANDLE    Only display the entry of given handle\n"
		" -u, --dump             Do not decode the entries\n"
		"     --dump-bin FILE    Dump the DMI data to a binary file\n"
		"     --from-dump FILE   Read the DMI data from a binary file\n"
		"     --no-sysfs         Do not attempt to read DMI data from sysfs files\n"
		"     --oem-string N     Only display the value of the given OEM string\n"
		" -V, --version          Display the version and exit\n";

	printf("%s", help);
}