2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <gpxe/settings.h>
32 * Configuration settings
36 /** Registered configuration setting types */
37 static struct config_setting_type config_setting_types[0]
38 __table_start ( struct config_setting_type, config_setting_types );
39 static struct config_setting_type config_setting_types_end[0]
40 __table_end ( struct config_setting_type, config_setting_types );
42 /** Registered configuration settings */
43 static struct config_setting config_settings[0]
44 __table_start ( struct config_setting, config_settings );
45 static struct config_setting config_settings_end[0]
46 __table_end ( struct config_setting, config_settings );
49 * Find configuration setting type
52 * @ret type Configuration setting type, or NULL
54 static struct config_setting_type *
55 find_config_setting_type ( const char *name ) {
56 struct config_setting_type *type;
58 for ( type = config_setting_types ; type < config_setting_types_end ;
60 if ( strcasecmp ( name, type->name ) == 0 )
67 * Find configuration setting
70 * @ret setting Configuration setting, or NULL
72 static struct config_setting * find_config_setting ( const char *name ) {
73 struct config_setting *setting;
75 for ( setting = config_settings ; setting < config_settings_end ;
77 if ( strcasecmp ( name, setting->name ) == 0 )
84 * Find or build configuration setting
87 * @v tmp_setting Temporary buffer for constructing a setting
88 * @ret setting Configuration setting, or NULL
90 * Find setting if it exists. If it doesn't exist, but the name is of
91 * the form "<num>.<type>" (e.g. "12.string"), then construct a
92 * setting for that tag and data type, and return it. The constructed
93 * setting will be placed in the temporary buffer.
95 static struct config_setting *
96 find_or_build_config_setting ( const char *name,
97 struct config_setting *tmp_setting ) {
98 struct config_setting *setting;
101 /* Look in the list of registered settings first */
102 setting = find_config_setting ( name );
106 /* If name is of the form "<num>.<type>", try to construct a setting */
107 setting = tmp_setting;
108 memset ( setting, 0, sizeof ( *setting ) );
109 setting->name = name;
110 setting->tag = strtoul ( name, &separator, 10 );
111 if ( *separator != '.' )
113 setting->type = find_config_setting_type ( separator + 1 );
114 if ( ! setting->type )
120 * Show value of named setting
122 * @v context Configuration context
123 * @v name Configuration setting name
124 * @v buf Buffer to contain value
125 * @v len Length of buffer
126 * @ret len Length of formatted value, or negative error
128 int show_named_setting ( struct config_context *context, const char *name,
129 char *buf, size_t len ) {
130 struct config_setting *setting;
131 struct config_setting tmp_setting;
133 setting = find_or_build_config_setting ( name, &tmp_setting );
136 return show_setting ( context, setting, buf, len );
140 * Set value of named setting
142 * @v context Configuration context
143 * @v name Configuration setting name
144 * @v value Setting value (as a string)
145 * @ret rc Return status code
147 int set_named_setting ( struct config_context *context, const char *name,
148 const char *value ) {
149 struct config_setting *setting;
150 struct config_setting tmp_setting;
152 setting = find_or_build_config_setting ( name, &tmp_setting );
155 return setting->type->set ( context, setting, value );
159 * Set value of setting
161 * @v context Configuration context
162 * @v setting Configuration setting
163 * @v value Setting value (as a string), or NULL
164 * @ret rc Return status code
166 int set_setting ( struct config_context *context,
167 struct config_setting *setting,
168 const char *value ) {
169 if ( ( ! value ) || ( ! *value ) ) {
170 /* Save putting deletion logic in each individual handler */
171 return clear_setting ( context, setting );
173 return setting->type->set ( context, setting, value );
177 * Show value of string setting
179 * @v context Configuration context
180 * @v setting Configuration setting
181 * @v buf Buffer to contain value
182 * @v len Length of buffer
183 * @ret len Length of formatted value, or negative error
185 static int show_string ( struct config_context *context,
186 struct config_setting *setting,
187 char *buf, size_t len ) {
188 struct dhcp_option *option;
190 option = find_dhcp_option ( context->options, setting->tag );
193 return dhcp_snprintf ( buf, len, option );
197 * Set value of string setting
199 * @v context Configuration context
200 * @v setting Configuration setting
201 * @v value Setting value (as a string)
202 * @ret rc Return status code
204 static int set_string ( struct config_context *context,
205 struct config_setting *setting,
206 const char *value ) {
207 struct dhcp_option *option;
209 option = set_dhcp_option ( context->options, setting->tag,
210 value, strlen ( value ) );
216 /** A string configuration setting */
217 struct config_setting_type config_setting_type_string __config_setting_type = {
219 .description = "Text string",
225 * Show value of IPv4 setting
227 * @v context Configuration context
228 * @v setting Configuration setting
229 * @v buf Buffer to contain value
230 * @v len Length of buffer
231 * @ret len Length of formatted value, or negative error
233 static int show_ipv4 ( struct config_context *context,
234 struct config_setting *setting,
235 char *buf, size_t len ) {
236 struct dhcp_option *option;
239 option = find_dhcp_option ( context->options, setting->tag );
242 dhcp_ipv4_option ( option, &ipv4 );
243 return snprintf ( buf, len, inet_ntoa ( ipv4 ) );
247 * Set value of IPV4 setting
249 * @v context Configuration context
250 * @v setting Configuration setting
251 * @v value Setting value (as a string)
252 * @ret rc Return status code
254 static int set_ipv4 ( struct config_context *context,
255 struct config_setting *setting,
256 const char *value ) {
257 struct dhcp_option *option;
260 if ( inet_aton ( value, &ipv4 ) == 0 )
262 option = set_dhcp_option ( context->options, setting->tag,
263 &ipv4, sizeof ( ipv4 ) );
269 /** An IPv4 configuration setting */
270 struct config_setting_type config_setting_type_ipv4 __config_setting_type = {
272 .description = "IPv4 address",
278 * Show value of integer setting
280 * @v context Configuration context
281 * @v setting Configuration setting
282 * @v buf Buffer to contain value
283 * @v len Length of buffer
284 * @ret len Length of formatted value, or negative error
286 static int show_int ( struct config_context *context,
287 struct config_setting *setting,
288 char *buf, size_t len ) {
289 struct dhcp_option *option;
292 option = find_dhcp_option ( context->options, setting->tag );
295 num = dhcp_num_option ( option );
296 return snprintf ( buf, len, "%ld", num );
300 * Set value of integer setting
302 * @v context Configuration context
303 * @v setting Configuration setting
304 * @v value Setting value (as a string)
305 * @v size Size of integer (in bytes)
306 * @ret rc Return status code
308 static int set_int ( struct config_context *context,
309 struct config_setting *setting,
310 const char *value, unsigned int size ) {
311 struct dhcp_option *option;
321 u.num = htonl ( strtoul ( value, &endp, 0 ) );
326 option = set_dhcp_option ( context->options, setting->tag,
327 &u.bytes[ sizeof ( u ) - size ], size );
334 * Set value of 8-bit integer setting
336 * @v context Configuration context
337 * @v setting Configuration setting
338 * @v value Setting value (as a string)
339 * @v size Size of integer (in bytes)
340 * @ret rc Return status code
342 static int set_int8 ( struct config_context *context,
343 struct config_setting *setting,
344 const char *value ) {
345 return set_int ( context, setting, value, 1 );
348 /** An 8-bit integer configuration setting */
349 struct config_setting_type config_setting_type_int8 __config_setting_type = {
351 .description = "8-bit integer",
356 /** Some basic setting definitions */
357 struct config_setting basic_config_settings[] __config_setting = {
360 .description = "IP address of this machine (e.g. 192.168.0.1)",
361 .tag = DHCP_EB_YIADDR,
362 .type = &config_setting_type_ipv4,
366 .description = "Host name of this machine",
367 .tag = DHCP_HOST_NAME,
368 .type = &config_setting_type_string,
372 .description = "User name for authentication to servers",
373 .tag = DHCP_EB_USERNAME,
374 .type = &config_setting_type_string,
378 .description = "Password for authentication to servers",
379 .tag = DHCP_EB_PASSWORD,
380 .type = &config_setting_type_string,
384 .description = "NFS/iSCSI root path",
385 .tag = DHCP_ROOT_PATH,
386 .type = &config_setting_type_string,
390 .description = "Priority of these options",
391 .tag = DHCP_EB_PRIORITY,
392 .type = &config_setting_type_int8,
395 .name = "initiator-iqn",
396 .description = "iSCSI qualified name of this machine",
397 .tag = DHCP_ISCSI_INITIATOR_IQN,
398 .type = &config_setting_type_string,