summaryrefslogtreecommitdiff
path: root/src/bios_dev_name.c
blob: 5757363c692c7f1a45252dd02627b0d27589d007 (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
/*
 *  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 "libbiosdevname.h"
#include "bios_dev_name.h"

static struct bios_dev_name_opts opts;

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, "   -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, " 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 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'},
			{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':
			opts.interface = 1;
			break;
		case 'p':
			opts.namingpolicy = set_policy(optarg);
			break;
		case 'P':
			opts.prefix = optarg;
			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";
}

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

	parse_opts(argc, argv);
	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) {
		fprintf(stderr, "Unknown device type, try passing an option like -i\n");
		rc = 1;
		goto out_usage;
	}

	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 */
	}
	goto out_cleanup;

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