summaryrefslogtreecommitdiff
path: root/src/bios_dev_name.c
blob: 1abc6d828490a79e097d170b3b1815943f07d9c7 (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
/*
 *  Copyright (c) 2006 Dell, Inc.
 *  by Matt Domsch <Matt_Domsch@dell.com>
 *  Licensed under the GNU General Public license, version 2.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/types.h>

#include "libbiosdevname.h"
#include "bios_dev_name.h"

static struct bios_dev_name_opts opts;
int nopirq;
int allow_vm;
int smver_mjr;
int smver_mnr;

static void usage(void)
{
	fprintf(stderr, "Usage:  biosdevname [options] [args]...\n");
	fprintf(stderr, " Options:\n");
	fprintf(stderr, "   -i        or --interface           treat [args] as ethernet devs\n");
	fprintf(stderr, "   --pci-spec                         treat [args] as PCI specification\n");
	fprintf(stderr, "   -d        or --debug               enable debugging\n");
	fprintf(stderr, "   --policy [physical | all_ethN ]\n");
	fprintf(stderr, "   --prefix [string]                  string use for embedded NICs (default='em')\n");
	fprintf(stderr, "   --smbios [x.y]		       Require SMBIOS x.y or greater\n");
	fprintf(stderr, "   --invm                             Still run in VM\n");
	fprintf(stderr, "   --nopirq			       Don't use $PIR table for slot numbers\n");
	fprintf(stderr, " Example:  biosdevname -i eth0\n");
	fprintf(stderr, "  returns: em1\n");
	fprintf(stderr, "  when eth0 is an embedded NIC with label '1' on the chassis.\n");
	fprintf(stderr, " You must be root to run this, as it must read from /dev/mem.\n");
}

static void conflict(void)
{
	fprintf(stderr, "can't use both interface and pci options\n");
	exit(1);
}

static int
set_policy(const char *arg)
{
	int rc = physical;

	if (!strncmp("physical", arg, sizeof("physical")))
		rc = physical;
	else if (!strncmp("all_ethN", arg, sizeof("all_ethN")))
		rc = all_ethN;
	return rc;
}

static void
parse_opts(int argc, char **argv)
{
	int c;
	int option_index = 0;

	while (1) {
		static struct option long_options[] =
			/* name, has_arg, flag, val */
		{
			{"debug",             no_argument, 0, 'd'},
			{"interface",         no_argument, 0, 'i'},
			{"policy",      required_argument, 0, 'p'},
			{"prefix",      required_argument, 0, 'P'},
			{"nopirq",	      no_argument, 0, 'x'},
			{"invm",              no_argument, 0, 'V'},
			{"smbios",	required_argument, 0, 's'},
			{"pci-spec",          no_argument, 0, 'S'},
			{0, 0, 0, 0}
		};
		c = getopt_long(argc, argv,
				"dinp:",
				long_options, &option_index);
		if (c == -1)
			break;
		switch(c) {
		case 'd':
			opts.debug = 1;
			break;
		case 'i':
			if (opts.usepci)
				conflict();
			opts.interface = 1;
			break;
		case 'S':
			if (opts.interface)
				conflict();
			opts.usepci = 1;
			break;
		case 'p':
			opts.namingpolicy = set_policy(optarg);
			break;
		case 'P':
			opts.prefix = optarg;
			break;
		case 's':
			sscanf(optarg, "%u.%u", &smver_mjr, &smver_mnr);
			break;
		case 'x':
			nopirq = 1;
			break;
		case 'V':
			allow_vm = 1;
			break;
		default:
			usage();
			exit(1);
		}
	}

	if (optind < argc) {
		opts.argc = argc-optind;
		opts.argv = &argv[optind];
		opts.optind = optind;
	}

	if (opts.prefix == NULL)
		opts.prefix = "em";
}

#if defined(__x86_64__) || defined(__i386__)

static u_int32_t
cpuid (u_int32_t eax, u_int32_t ecx)
{
    asm volatile (
        "xor %%ebx, %%ebx; cpuid"
        : "=a" (eax),  "=c" (ecx)
        : "a" (eax)
	: "%ebx", "%edx");
    return ecx;
}

/*
  Algorithm suggested by:
  http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458
*/

static int
running_in_virtual_machine (void)
{
    u_int32_t eax=1U, ecx=0U;

    ecx = cpuid (eax, ecx);
    if (ecx & 0x80000000U)
       return 1;
    return 0;
}

#else   /* ! __x86_64__ */
static int
running_in_virtual_machine (void)
{
    return 1;
}
#endif

static int
running_as_root(void)
{
	uid_t uid = geteuid();
	if (uid != 0) {
		fprintf(stderr, "This program must be run as root.\n");
		return 0;
	}
	return 1;
}

int main(int argc, char *argv[])
{
	int i, rc=0;
	char *name;
	void *cookie = NULL;

	parse_opts(argc, argv);

	if (!running_as_root())
		exit(3);
	if (!allow_vm && running_in_virtual_machine())
		exit(4);
	cookie = setup_bios_devices(opts.namingpolicy, opts.prefix);
	if (!cookie) {
		rc = 1;
		goto out;
	}

	if (opts.debug) {
		unparse_bios_devices(cookie);
		rc = 0;
		goto out_cleanup;
	}

	if (opts.interface) {
		for (i=0; i<opts.argc; i++) {
			name = kern_to_bios(cookie, opts.argv[i]);
			if (name)
				printf("%s\n", name);
			else
				rc |= 2; /* one or more given devices weren't found */
		}
	} else if(opts.usepci) {
		for (i=0; i<opts.argc; i++) {
			name = pci_to_bios(cookie, opts.argv[i]);
			if (name)
				printf("%s\n", name);
			else
				rc |= 2;
		}
	} else {
		fprintf(stderr, "Unknown device type, try passing an option like -i\n");
		rc = 1;
		goto out_usage;
	}


	goto out_cleanup;

 out_usage:
	usage();
 out_cleanup:
	cleanup_bios_devices(cookie);
 out:
	return rc;
}