summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/hci/linux_args.c
blob: 58eeb063ec3ae2c87dcdef7a750a2df3bffe6f8a (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
/*
 * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@gmail.com>
 *
 * 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 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

FILE_LICENCE(GPL2_OR_LATER);

#include <hci/linux_args.h>
#include <getopt.h>
#include <string.h>
#include <stdio.h>
#include <ipxe/settings.h>
#include <ipxe/linux.h>
#include <ipxe/malloc.h>
#include <ipxe/init.h>

/** Saved argc */
static int saved_argc = 0;
/** Saved argv */
static char ** saved_argv;

/**
 * Save argc and argv for later access.
 *
 * To be called by linuxprefix
 */
__asmcall void save_args(int argc, char **argv)
{
	saved_argc = argc;
	saved_argv = argv;
}

/** Supported command-line options */
static struct option options[] = {
	{"net", 1, NULL, 'n'},
	{"settings", 1, NULL, 's'},
	{NULL, 0, NULL, 0}
};

/**
 * Parse k1=v1[,k2=v2]* into linux_settings
 */
static int parse_kv(char *kv, struct list_head *list)
{
	char *token;
	char *name;
	char *value;
	struct linux_setting *setting;

	while ((token = strsep(&kv, ",")) != NULL) {
		name = strsep(&token, "=");
		if (name == NULL)
			continue;
		value = token;
		if (value == NULL) {
			DBG("Bad parameter: '%s'\n", name);
			continue;
		}

		setting = malloc(sizeof(*setting));

		if (! setting)
			return -1;

		setting->name = name;
		setting->value = value;
		setting->applied = 0;
		list_add(&setting->list, list);
	}

	return 0;
}

/**
 * Parse --net arguments
 *
 * Format is --net driver_name[,name=value]*
 */
static int parse_net_args(char *args)
{
	char *driver;
	struct linux_device_request *dev_request;
	int rc;

	driver = strsep(&args, ",");

	if (strlen(driver) == 0) {
		printf("Missing driver name");
		return -1;
	}

	dev_request = malloc(sizeof(*dev_request));

	dev_request->driver = driver;
	INIT_LIST_HEAD(&dev_request->settings);
	list_add_tail(&dev_request->list, &linux_device_requests);

	/* Parse rest of the settings */
	rc = parse_kv(args, &dev_request->settings);

	if (rc)
		printf("Parsing net settings failed");

	return rc;
}

/**
 * Parse --settings arguments
 *
 * Format is --settings name=value[,name=value]*
 */
static int parse_settings_args(char *args)
{
	return parse_kv(args, &linux_global_settings);
}


/** Parse passed command-line arguments */
void linux_args_parse()
{
	int c;
	int rc;

	reset_getopt();
	while (1) {
		int option_index = 0;

		c = getopt_long(saved_argc, saved_argv, "", options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'n':
			if ((rc = parse_net_args(optarg)) != 0)
				return;
			break;
		case 's':
			if ((rc = parse_settings_args(optarg)) != 0)
				return;
			break;
		default:
			return;
		}
	}

	return;
}

/** Clean up requests and settings */
void linux_args_cleanup(int flags __unused)
{
	struct linux_device_request *request;
	struct linux_device_request *rtmp;
	struct linux_setting *setting;
	struct linux_setting *stmp;

	/* Clean up requests and their settings */
	list_for_each_entry_safe(request, rtmp, &linux_device_requests, list) {
		list_for_each_entry_safe(setting, stmp, &request->settings, list) {
			list_del(&setting->list);
			free(setting);
		}
		list_del(&request->list);
		free(request);
	}

	/* Clean up global settings */
	list_for_each_entry_safe(setting, stmp, &linux_global_settings, list) {
		list_del(&setting->list);
		free(setting);
	}
}

struct startup_fn startup_linux_args __startup_fn(STARTUP_EARLY) = {
	.startup = linux_args_parse,
	.shutdown = linux_args_cleanup,
};