[efi] Include installed protocol list in unknown handle names
[ipxe.git] / src / core / settings.c
1 /*
2 * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <byteswap.h>
32 #include <errno.h>
33 #include <assert.h>
34 #include <ipxe/in.h>
35 #include <ipxe/ip.h>
36 #include <ipxe/ipv6.h>
37 #include <ipxe/vsprintf.h>
38 #include <ipxe/dhcp.h>
39 #include <ipxe/uuid.h>
40 #include <ipxe/uri.h>
41 #include <ipxe/base16.h>
42 #include <ipxe/base64.h>
43 #include <ipxe/pci.h>
44 #include <ipxe/init.h>
45 #include <ipxe/version.h>
46 #include <ipxe/settings.h>
47
48 /** @file
49 *
50 * Configuration settings
51 *
52 */
53
54 /******************************************************************************
55 *
56 * Generic settings blocks
57 *
58 ******************************************************************************
59 */
60
61 /**
62 * A generic setting
63 *
64 */
65 struct generic_setting {
66 /** List of generic settings */
67 struct list_head list;
68 /** Setting */
69 struct setting setting;
70 /** Size of setting name */
71 size_t name_len;
72 /** Size of setting data */
73 size_t data_len;
74 };
75
76 /**
77 * Get generic setting name
78 *
79 * @v generic Generic setting
80 * @ret name Generic setting name
81 */
82 static inline void * generic_setting_name ( struct generic_setting *generic ) {
83 return ( ( ( void * ) generic ) + sizeof ( *generic ) );
84 }
85
86 /**
87 * Get generic setting data
88 *
89 * @v generic Generic setting
90 * @ret data Generic setting data
91 */
92 static inline void * generic_setting_data ( struct generic_setting *generic ) {
93 return ( ( ( void * ) generic ) + sizeof ( *generic ) +
94 generic->name_len );
95 }
96
97 /**
98 * Find generic setting
99 *
100 * @v generics Generic settings block
101 * @v setting Setting to find
102 * @ret generic Generic setting, or NULL
103 */
104 static struct generic_setting *
105 find_generic_setting ( struct generic_settings *generics,
106 const struct setting *setting ) {
107 struct generic_setting *generic;
108
109 list_for_each_entry ( generic, &generics->list, list ) {
110 if ( setting_cmp ( &generic->setting, setting ) == 0 )
111 return generic;
112 }
113 return NULL;
114 }
115
116 /**
117 * Store value of generic setting
118 *
119 * @v settings Settings block
120 * @v setting Setting to store
121 * @v data Setting data, or NULL to clear setting
122 * @v len Length of setting data
123 * @ret rc Return status code
124 */
125 int generic_settings_store ( struct settings *settings,
126 const struct setting *setting,
127 const void *data, size_t len ) {
128 struct generic_settings *generics =
129 container_of ( settings, struct generic_settings, settings );
130 struct generic_setting *old;
131 struct generic_setting *new = NULL;
132 size_t name_len;
133
134 /* Identify existing generic setting, if any */
135 old = find_generic_setting ( generics, setting );
136
137 /* Create new generic setting, if required */
138 if ( len ) {
139 /* Allocate new generic setting */
140 name_len = ( strlen ( setting->name ) + 1 );
141 new = zalloc ( sizeof ( *new ) + name_len + len );
142 if ( ! new )
143 return -ENOMEM;
144
145 /* Populate new generic setting */
146 new->name_len = name_len;
147 new->data_len = len;
148 memcpy ( &new->setting, setting, sizeof ( new->setting ) );
149 new->setting.name = generic_setting_name ( new );
150 memcpy ( generic_setting_name ( new ),
151 setting->name, name_len );
152 memcpy ( generic_setting_data ( new ), data, len );
153 }
154
155 /* Delete existing generic setting, if any */
156 if ( old ) {
157 list_del ( &old->list );
158 free ( old );
159 }
160
161 /* Add new setting to list, if any */
162 if ( new )
163 list_add ( &new->list, &generics->list );
164
165 return 0;
166 }
167
168 /**
169 * Fetch value of generic setting
170 *
171 * @v settings Settings block
172 * @v setting Setting to fetch
173 * @v data Buffer to fill with setting data
174 * @v len Length of buffer
175 * @ret len Length of setting data, or negative error
176 */
177 int generic_settings_fetch ( struct settings *settings,
178 struct setting *setting,
179 void *data, size_t len ) {
180 struct generic_settings *generics =
181 container_of ( settings, struct generic_settings, settings );
182 struct generic_setting *generic;
183
184 /* Find generic setting */
185 generic = find_generic_setting ( generics, setting );
186 if ( ! generic )
187 return -ENOENT;
188
189 /* Copy out generic setting data */
190 if ( len > generic->data_len )
191 len = generic->data_len;
192 memcpy ( data, generic_setting_data ( generic ), len );
193
194 /* Set setting type, if not yet specified */
195 if ( ! setting->type )
196 setting->type = generic->setting.type;
197
198 return generic->data_len;
199 }
200
201 /**
202 * Clear generic settings block
203 *
204 * @v settings Settings block
205 */
206 void generic_settings_clear ( struct settings *settings ) {
207 struct generic_settings *generics =
208 container_of ( settings, struct generic_settings, settings );
209 struct generic_setting *generic;
210 struct generic_setting *tmp;
211
212 list_for_each_entry_safe ( generic, tmp, &generics->list, list ) {
213 list_del ( &generic->list );
214 free ( generic );
215 }
216 assert ( list_empty ( &generics->list ) );
217 }
218
219 /** Generic settings operations */
220 struct settings_operations generic_settings_operations = {
221 .store = generic_settings_store,
222 .fetch = generic_settings_fetch,
223 .clear = generic_settings_clear,
224 };
225
226 /******************************************************************************
227 *
228 * Registered settings blocks
229 *
230 ******************************************************************************
231 */
232
233 /** Root generic settings block */
234 struct generic_settings generic_settings_root = {
235 .settings = {
236 .refcnt = NULL,
237 .name = "",
238 .siblings =
239 LIST_HEAD_INIT ( generic_settings_root.settings.siblings ),
240 .children =
241 LIST_HEAD_INIT ( generic_settings_root.settings.children ),
242 .op = &generic_settings_operations,
243 },
244 .list = LIST_HEAD_INIT ( generic_settings_root.list ),
245 };
246
247 /** Root settings block */
248 #define settings_root generic_settings_root.settings
249
250 /** Autovivified settings block */
251 struct autovivified_settings {
252 /** Reference count */
253 struct refcnt refcnt;
254 /** Generic settings block */
255 struct generic_settings generic;
256 };
257
258 /**
259 * Free autovivified settings block
260 *
261 * @v refcnt Reference count
262 */
263 static void autovivified_settings_free ( struct refcnt *refcnt ) {
264 struct autovivified_settings *autovivified =
265 container_of ( refcnt, struct autovivified_settings, refcnt );
266
267 generic_settings_clear ( &autovivified->generic.settings );
268 free ( autovivified );
269 }
270
271 /**
272 * Find child settings block
273 *
274 * @v parent Parent settings block
275 * @v name Name within this parent
276 * @ret settings Settings block, or NULL
277 */
278 struct settings * find_child_settings ( struct settings *parent,
279 const char *name ) {
280 struct settings *settings;
281
282 /* Find target parent settings block */
283 parent = settings_target ( parent );
284
285 /* Treat empty name as meaning "this block" */
286 if ( ! *name )
287 return parent;
288
289 /* Look for child with matching name */
290 list_for_each_entry ( settings, &parent->children, siblings ) {
291 if ( strcmp ( settings->name, name ) == 0 )
292 return settings_target ( settings );
293 }
294
295 return NULL;
296 }
297
298 /**
299 * Find or create child settings block
300 *
301 * @v parent Parent settings block
302 * @v name Name within this parent
303 * @ret settings Settings block, or NULL
304 */
305 struct settings * autovivify_child_settings ( struct settings *parent,
306 const char *name ) {
307 struct {
308 struct autovivified_settings autovivified;
309 char name[ strlen ( name ) + 1 /* NUL */ ];
310 } *new_child;
311 struct settings *settings;
312
313 /* Find target parent settings block */
314 parent = settings_target ( parent );
315
316 /* Return existing settings, if existent */
317 if ( ( settings = find_child_settings ( parent, name ) ) != NULL )
318 return settings;
319
320 /* Create new generic settings block */
321 new_child = zalloc ( sizeof ( *new_child ) );
322 if ( ! new_child ) {
323 DBGC ( parent, "Settings %p could not create child %s\n",
324 parent, name );
325 return NULL;
326 }
327 memcpy ( new_child->name, name, sizeof ( new_child->name ) );
328 ref_init ( &new_child->autovivified.refcnt,
329 autovivified_settings_free );
330 generic_settings_init ( &new_child->autovivified.generic,
331 &new_child->autovivified.refcnt );
332 settings = &new_child->autovivified.generic.settings;
333 register_settings ( settings, parent, new_child->name );
334 return settings;
335 }
336
337 /**
338 * Return settings block name
339 *
340 * @v settings Settings block
341 * @ret name Settings block name
342 */
343 const char * settings_name ( struct settings *settings ) {
344 static char buf[16];
345 char tmp[ 1 /* '.' */ + sizeof ( buf ) ];
346
347 /* Find target settings block */
348 settings = settings_target ( settings );
349
350 /* Construct name */
351 buf[0] = '\0';
352 tmp[0] = '\0';
353 for ( ; settings->parent ; settings = settings->parent ) {
354 memcpy ( ( tmp + 1 ), buf, ( sizeof ( tmp ) - 1 ) );
355 snprintf ( buf, sizeof ( buf ), "%s%s", settings->name, tmp );
356 tmp[0] = '.';
357 }
358 return buf;
359 }
360
361 /**
362 * Parse settings block name
363 *
364 * @v name Name
365 * @v get_child Function to find or create child settings block
366 * @ret settings Settings block, or NULL
367 */
368 static struct settings *
369 parse_settings_name ( const char *name, get_child_settings_t get_child ) {
370 struct settings *settings = &settings_root;
371 char name_copy[ strlen ( name ) + 1 ];
372 char *subname;
373 char *remainder;
374
375 /* Create modifiable copy of name */
376 memcpy ( name_copy, name, sizeof ( name_copy ) );
377 remainder = name_copy;
378
379 /* Parse each name component in turn */
380 while ( remainder ) {
381 subname = remainder;
382 remainder = strchr ( subname, '.' );
383 if ( remainder )
384 *(remainder++) = '\0';
385 settings = get_child ( settings, subname );
386 if ( ! settings )
387 break;
388 }
389
390 return settings;
391 }
392
393 /**
394 * Find settings block
395 *
396 * @v name Name
397 * @ret settings Settings block, or NULL
398 */
399 struct settings * find_settings ( const char *name ) {
400
401 return parse_settings_name ( name, find_child_settings );
402 }
403
404 /**
405 * Apply all settings
406 *
407 * @ret rc Return status code
408 */
409 static int apply_settings ( void ) {
410 struct settings_applicator *applicator;
411 int rc;
412
413 /* Call all settings applicators */
414 for_each_table_entry ( applicator, SETTINGS_APPLICATORS ) {
415 if ( ( rc = applicator->apply() ) != 0 ) {
416 DBG ( "Could not apply settings using applicator "
417 "%p: %s\n", applicator, strerror ( rc ) );
418 return rc;
419 }
420 }
421
422 return 0;
423 }
424
425 /**
426 * Reprioritise settings
427 *
428 * @v settings Settings block
429 *
430 * Reorders the settings block amongst its siblings according to its
431 * priority.
432 */
433 static void reprioritise_settings ( struct settings *settings ) {
434 struct settings *parent = settings->parent;
435 long priority;
436 struct settings *tmp;
437 long tmp_priority;
438
439 /* Stop when we reach the top of the tree */
440 if ( ! parent )
441 return;
442
443 /* Read priority, if present */
444 priority = fetch_intz_setting ( settings, &priority_setting );
445
446 /* Remove from siblings list */
447 list_del ( &settings->siblings );
448
449 /* Reinsert after any existing blocks which have a higher priority */
450 list_for_each_entry ( tmp, &parent->children, siblings ) {
451 tmp_priority = fetch_intz_setting ( tmp, &priority_setting );
452 if ( priority > tmp_priority )
453 break;
454 }
455 list_add_tail ( &settings->siblings, &tmp->siblings );
456
457 /* Recurse up the tree */
458 reprioritise_settings ( parent );
459 }
460
461 /**
462 * Register settings block
463 *
464 * @v settings Settings block
465 * @v parent Parent settings block, or NULL
466 * @v name Settings block name
467 * @ret rc Return status code
468 */
469 int register_settings ( struct settings *settings, struct settings *parent,
470 const char *name ) {
471 struct settings *old_settings;
472
473 /* Sanity check */
474 assert ( settings != NULL );
475
476 /* Find target parent settings block */
477 parent = settings_target ( parent );
478
479 /* Apply settings block name */
480 settings->name = name;
481
482 /* Remove any existing settings with the same name */
483 if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
484 unregister_settings ( old_settings );
485
486 /* Add to list of settings */
487 ref_get ( settings->refcnt );
488 ref_get ( parent->refcnt );
489 settings->parent = parent;
490 list_add_tail ( &settings->siblings, &parent->children );
491 DBGC ( settings, "Settings %p (\"%s\") registered\n",
492 settings, settings_name ( settings ) );
493
494 /* Fix up settings priority */
495 reprioritise_settings ( settings );
496
497 /* Apply potentially-updated settings */
498 apply_settings();
499
500 return 0;
501 }
502
503 /**
504 * Unregister settings block
505 *
506 * @v settings Settings block
507 */
508 void unregister_settings ( struct settings *settings ) {
509 struct settings *child;
510
511 /* Unregister child settings */
512 while ( ( child = list_first_entry ( &settings->children,
513 struct settings, siblings ) ) ) {
514 unregister_settings ( child );
515 }
516
517 DBGC ( settings, "Settings %p (\"%s\") unregistered\n",
518 settings, settings_name ( settings ) );
519
520 /* Remove from list of settings */
521 ref_put ( settings->parent->refcnt );
522 settings->parent = NULL;
523 list_del ( &settings->siblings );
524 ref_put ( settings->refcnt );
525
526 /* Apply potentially-updated settings */
527 apply_settings();
528 }
529
530 /******************************************************************************
531 *
532 * Core settings routines
533 *
534 ******************************************************************************
535 */
536
537 /**
538 * Redirect to target settings block
539 *
540 * @v settings Settings block, or NULL
541 * @ret settings Underlying settings block
542 */
543 struct settings * settings_target ( struct settings *settings ) {
544
545 /* NULL settings implies the global settings root */
546 if ( ! settings )
547 settings = &settings_root;
548
549 /* Redirect to underlying settings block, if applicable */
550 if ( settings->op->redirect )
551 return settings->op->redirect ( settings );
552
553 /* Otherwise, return this settings block */
554 return settings;
555 }
556
557 /**
558 * Check applicability of setting
559 *
560 * @v settings Settings block
561 * @v setting Setting
562 * @ret applies Setting applies within this settings block
563 */
564 int setting_applies ( struct settings *settings,
565 const struct setting *setting ) {
566
567 /* Find target settings block */
568 settings = settings_target ( settings );
569
570 /* Check applicability of setting */
571 return ( settings->op->applies ?
572 settings->op->applies ( settings, setting ) : 1 );
573 }
574
575 /**
576 * Find setting applicable to settings block, if any
577 *
578 * @v settings Settings block
579 * @v setting Setting
580 * @ret setting Applicable setting, if any
581 */
582 static const struct setting *
583 applicable_setting ( struct settings *settings, const struct setting *setting ){
584 const struct setting *applicable;
585
586 /* If setting is already applicable, use it */
587 if ( setting_applies ( settings, setting ) )
588 return setting;
589
590 /* Otherwise, look for a matching predefined setting which does apply */
591 for_each_table_entry ( applicable, SETTINGS ) {
592 if ( ( setting_cmp ( setting, applicable ) == 0 ) &&
593 ( setting_applies ( settings, applicable ) ) )
594 return applicable;
595 }
596
597 return NULL;
598 }
599
600 /**
601 * Store value of setting
602 *
603 * @v settings Settings block, or NULL
604 * @v setting Setting to store
605 * @v data Setting data, or NULL to clear setting
606 * @v len Length of setting data
607 * @ret rc Return status code
608 */
609 int store_setting ( struct settings *settings, const struct setting *setting,
610 const void *data, size_t len ) {
611 int rc;
612
613 /* Find target settings block */
614 settings = settings_target ( settings );
615
616 /* Fail if setting does not apply to this settings block */
617 if ( ! setting_applies ( settings, setting ) )
618 return -ENOTTY;
619
620 /* Sanity check */
621 if ( ! settings->op->store )
622 return -ENOTSUP;
623
624 /* Store setting */
625 if ( ( rc = settings->op->store ( settings, setting,
626 data, len ) ) != 0 )
627 return rc;
628
629 /* Reprioritise settings if necessary */
630 if ( setting_cmp ( setting, &priority_setting ) == 0 )
631 reprioritise_settings ( settings );
632
633 /* If these settings are registered, apply potentially-updated
634 * settings
635 */
636 for ( ; settings ; settings = settings->parent ) {
637 if ( settings == &settings_root ) {
638 if ( ( rc = apply_settings() ) != 0 )
639 return rc;
640 break;
641 }
642 }
643
644 return 0;
645 }
646
647 /**
648 * Fetch setting
649 *
650 * @v settings Settings block, or NULL to search all blocks
651 * @v setting Setting to fetch
652 * @v origin Origin of setting to fill in, or NULL
653 * @v fetched Fetched setting to fill in, or NULL
654 * @v data Buffer to fill with setting data
655 * @v len Length of buffer
656 * @ret len Length of setting data, or negative error
657 *
658 * The actual length of the setting will be returned even if
659 * the buffer was too small.
660 */
661 int fetch_setting ( struct settings *settings, const struct setting *setting,
662 struct settings **origin, struct setting *fetched,
663 void *data, size_t len ) {
664 const struct setting *applicable;
665 struct settings *child;
666 struct setting tmp;
667 int ret;
668
669 /* Avoid returning uninitialised data on error */
670 memset ( data, 0, len );
671 if ( origin )
672 *origin = NULL;
673 if ( fetched )
674 memcpy ( fetched, setting, sizeof ( *fetched ) );
675
676 /* Find target settings block */
677 settings = settings_target ( settings );
678
679 /* Sanity check */
680 if ( ! settings->op->fetch )
681 return -ENOTSUP;
682
683 /* Try this block first, if an applicable setting exists */
684 if ( ( applicable = applicable_setting ( settings, setting ) ) ) {
685
686 /* Create modifiable copy of setting */
687 memcpy ( &tmp, applicable, sizeof ( tmp ) );
688 if ( ( ret = settings->op->fetch ( settings, &tmp,
689 data, len ) ) >= 0 ) {
690
691 /* Default to string type, if not yet specified */
692 if ( ! tmp.type )
693 tmp.type = &setting_type_string;
694
695 /* Record origin, if applicable */
696 if ( origin )
697 *origin = settings;
698
699 /* Record fetched setting, if applicable */
700 if ( fetched )
701 memcpy ( fetched, &tmp, sizeof ( *fetched ) );
702
703 return ret;
704 }
705 }
706
707 /* Recurse into each child block in turn */
708 list_for_each_entry ( child, &settings->children, siblings ) {
709 if ( ( ret = fetch_setting ( child, setting, origin, fetched,
710 data, len ) ) >= 0 )
711 return ret;
712 }
713
714 return -ENOENT;
715 }
716
717 /**
718 * Fetch allocated copy of setting
719 *
720 * @v settings Settings block, or NULL to search all blocks
721 * @v setting Setting to fetch
722 * @v origin Origin of setting to fill in, or NULL
723 * @v fetched Fetched setting to fill in, or NULL
724 * @v data Buffer to allocate and fill with setting data
725 * @v alloc Allocation function
726 * @ret len Length of setting, or negative error
727 *
728 * The caller is responsible for eventually freeing the allocated
729 * buffer.
730 */
731 static int fetch_setting_alloc ( struct settings *settings,
732 const struct setting *setting,
733 struct settings **origin,
734 struct setting *fetched,
735 void **data,
736 void * ( * alloc ) ( size_t len ) ) {
737 struct settings *tmp_origin;
738 struct setting tmp_fetched;
739 int len;
740 int check_len;
741
742 /* Use local buffers if necessary */
743 if ( ! origin )
744 origin = &tmp_origin;
745 if ( ! fetched )
746 fetched = &tmp_fetched;
747
748 /* Avoid returning uninitialised data on error */
749 *data = NULL;
750
751 /* Check existence, and fetch setting length */
752 len = fetch_setting ( settings, setting, origin, fetched, NULL, 0 );
753 if ( len < 0 )
754 return len;
755
756 /* Allocate buffer */
757 *data = alloc ( len );
758 if ( ! *data )
759 return -ENOMEM;
760
761 /* Fetch setting value */
762 check_len = fetch_setting ( *origin, fetched, NULL, NULL, *data, len );
763 assert ( check_len == len );
764 return len;
765 }
766
767 /**
768 * Fetch copy of setting
769 *
770 * @v settings Settings block, or NULL to search all blocks
771 * @v setting Setting to fetch
772 * @v origin Origin of setting to fill in, or NULL
773 * @v fetched Fetched setting to fill in, or NULL
774 * @v data Buffer to allocate and fill with setting data
775 * @ret len Length of setting, or negative error
776 *
777 * The caller is responsible for eventually freeing the allocated
778 * buffer.
779 */
780 int fetch_setting_copy ( struct settings *settings,
781 const struct setting *setting,
782 struct settings **origin, struct setting *fetched,
783 void **data ) {
784
785 return fetch_setting_alloc ( settings, setting, origin, fetched,
786 data, malloc );
787 }
788
789 /**
790 * Fetch value of setting
791 *
792 * @v settings Settings block, or NULL to search all blocks
793 * @v setting Setting to fetch
794 * @v data Buffer to fill with setting string data
795 * @v len Length of buffer
796 * @ret len Length of setting, or negative error
797 */
798 int fetch_raw_setting ( struct settings *settings,
799 const struct setting *setting,
800 void *data, size_t len ) {
801
802 return fetch_setting ( settings, setting, NULL, NULL, data, len );
803 }
804
805 /**
806 * Fetch value of setting
807 *
808 * @v settings Settings block, or NULL to search all blocks
809 * @v setting Setting to fetch
810 * @v data Buffer to allocate and fill with setting data
811 * @ret len Length of setting, or negative error
812 *
813 * The caller is responsible for eventually freeing the allocated
814 * buffer.
815 */
816 int fetch_raw_setting_copy ( struct settings *settings,
817 const struct setting *setting,
818 void **data ) {
819
820 return fetch_setting_copy ( settings, setting, NULL, NULL, data );
821 }
822
823 /**
824 * Fetch value of string setting
825 *
826 * @v settings Settings block, or NULL to search all blocks
827 * @v setting Setting to fetch
828 * @v data Buffer to fill with setting string data
829 * @v len Length of buffer
830 * @ret len Length of string setting, or negative error
831 *
832 * The resulting string is guaranteed to be correctly NUL-terminated.
833 * The returned length will be the length of the underlying setting
834 * data.
835 */
836 int fetch_string_setting ( struct settings *settings,
837 const struct setting *setting,
838 char *data, size_t len ) {
839
840 memset ( data, 0, len );
841 return fetch_raw_setting ( settings, setting, data,
842 ( ( len > 0 ) ? ( len - 1 ) : 0 ) );
843 }
844
845 /**
846 * Allocate memory for copy of string setting
847 *
848 * @v len Length of setting
849 * @ret ptr Allocated memory
850 */
851 static void * fetch_string_setting_copy_alloc ( size_t len ) {
852 return zalloc ( len + 1 /* NUL */ );
853 }
854
855 /**
856 * Fetch value of string setting
857 *
858 * @v settings Settings block, or NULL to search all blocks
859 * @v setting Setting to fetch
860 * @v data Buffer to allocate and fill with setting string data
861 * @ret len Length of string setting, or negative error
862 *
863 * The resulting string is guaranteed to be correctly NUL-terminated.
864 * The returned length will be the length of the underlying setting
865 * data. The caller is responsible for eventually freeing the
866 * allocated buffer.
867 */
868 int fetch_string_setting_copy ( struct settings *settings,
869 const struct setting *setting, char **data ) {
870
871 return fetch_setting_alloc ( settings, setting, NULL, NULL,
872 ( ( void ** ) data ),
873 fetch_string_setting_copy_alloc );
874 }
875
876 /**
877 * Fetch value of IPv4 address setting
878 *
879 * @v settings Settings block, or NULL to search all blocks
880 * @v setting Setting to fetch
881 * @v inp IPv4 addresses to fill in
882 * @v count Maximum number of IPv4 addresses
883 * @ret len Length of setting, or negative error
884 */
885 int fetch_ipv4_array_setting ( struct settings *settings,
886 const struct setting *setting,
887 struct in_addr *inp, unsigned int count ) {
888 int len;
889
890 len = fetch_raw_setting ( settings, setting, inp,
891 ( sizeof ( *inp ) * count ) );
892 if ( len < 0 )
893 return len;
894 if ( ( len % sizeof ( *inp ) ) != 0 )
895 return -ERANGE;
896 return len;
897 }
898
899 /**
900 * Fetch value of IPv4 address setting
901 *
902 * @v settings Settings block, or NULL to search all blocks
903 * @v setting Setting to fetch
904 * @v inp IPv4 address to fill in
905 * @ret len Length of setting, or negative error
906 */
907 int fetch_ipv4_setting ( struct settings *settings,
908 const struct setting *setting,
909 struct in_addr *inp ) {
910
911 return fetch_ipv4_array_setting ( settings, setting, inp, 1 );
912 }
913
914 /**
915 * Fetch value of IPv6 address setting
916 *
917 * @v settings Settings block, or NULL to search all blocks
918 * @v setting Setting to fetch
919 * @v inp IPv6 addresses to fill in
920 * @v count Maximum number of IPv6 addresses
921 * @ret len Length of setting, or negative error
922 */
923 int fetch_ipv6_array_setting ( struct settings *settings,
924 const struct setting *setting,
925 struct in6_addr *inp, unsigned int count ) {
926 int len;
927
928 len = fetch_raw_setting ( settings, setting, inp,
929 ( sizeof ( *inp ) * count ) );
930 if ( len < 0 )
931 return len;
932 if ( ( len % sizeof ( *inp ) ) != 0 )
933 return -ERANGE;
934 return len;
935 }
936
937 /**
938 * Fetch value of IPv6 address setting
939 *
940 * @v settings Settings block, or NULL to search all blocks
941 * @v setting Setting to fetch
942 * @v inp IPv6 address to fill in
943 * @ret len Length of setting, or negative error
944 */
945 int fetch_ipv6_setting ( struct settings *settings,
946 const struct setting *setting,
947 struct in6_addr *inp ) {
948
949 return fetch_ipv6_array_setting ( settings, setting, inp, 1 );
950 }
951
952 /**
953 * Extract numeric value of setting
954 *
955 * @v is_signed Treat value as a signed integer
956 * @v raw Raw setting data
957 * @v len Length of raw setting data
958 * @ret value Numeric value
959 * @ret len Length of setting, or negative error
960 */
961 static int numeric_setting_value ( int is_signed, const void *raw, size_t len,
962 unsigned long *value ) {
963 const uint8_t *unsigned_bytes = raw;
964 const int8_t *signed_bytes = raw;
965 int is_negative;
966 unsigned int i;
967 uint8_t pad;
968 uint8_t byte;
969
970 /* Convert to host-ordered longs */
971 is_negative = ( len && ( signed_bytes[0] < 0 ) );
972 *value = ( ( is_signed && is_negative ) ? -1L : 0 );
973 pad = *value;
974 for ( i = 0 ; i < len ; i++ ) {
975 byte = unsigned_bytes[i];
976 *value = ( ( *value << 8 ) | byte );
977 if ( ( ( i + sizeof ( *value ) ) < len ) && ( byte != pad ) )
978 return -ERANGE;
979 }
980
981 return len;
982 }
983
984 /**
985 * Fetch value of numeric setting
986 *
987 * @v settings Settings block, or NULL to search all blocks
988 * @v setting Setting to fetch
989 * @v value Integer value to fill in
990 * @ret len Length of setting, or negative error
991 */
992 int fetch_numeric_setting ( struct settings *settings,
993 const struct setting *setting,
994 unsigned long *value, int is_signed ) {
995 unsigned long tmp;
996 int len;
997
998 /* Avoid returning uninitialised data on error */
999 *value = 0;
1000
1001 /* Fetch raw (network-ordered, variable-length) setting */
1002 len = fetch_raw_setting ( settings, setting, &tmp, sizeof ( tmp ) );
1003 if ( len < 0 )
1004 return len;
1005
1006 /* Extract numeric value */
1007 return numeric_setting_value ( is_signed, &tmp, len, value );
1008 }
1009
1010 /**
1011 * Fetch value of signed integer setting
1012 *
1013 * @v settings Settings block, or NULL to search all blocks
1014 * @v setting Setting to fetch
1015 * @v value Integer value to fill in
1016 * @ret len Length of setting, or negative error
1017 */
1018 int fetch_int_setting ( struct settings *settings,
1019 const struct setting *setting,
1020 long *value ) {
1021
1022 return fetch_numeric_setting ( settings, setting,
1023 ( ( unsigned long * ) value ), 1 );
1024 }
1025
1026 /**
1027 * Fetch value of unsigned integer setting
1028 *
1029 * @v settings Settings block, or NULL to search all blocks
1030 * @v setting Setting to fetch
1031 * @v value Integer value to fill in
1032 * @ret len Length of setting, or negative error
1033 */
1034 int fetch_uint_setting ( struct settings *settings,
1035 const struct setting *setting,
1036 unsigned long *value ) {
1037
1038 return fetch_numeric_setting ( settings, setting, value, 0 );
1039 }
1040
1041 /**
1042 * Fetch value of signed integer setting, or zero
1043 *
1044 * @v settings Settings block, or NULL to search all blocks
1045 * @v setting Setting to fetch
1046 * @ret value Setting value, or zero
1047 */
1048 long fetch_intz_setting ( struct settings *settings,
1049 const struct setting *setting ) {
1050 unsigned long value;
1051
1052 fetch_numeric_setting ( settings, setting, &value, 1 );
1053 return value;
1054 }
1055
1056 /**
1057 * Fetch value of unsigned integer setting, or zero
1058 *
1059 * @v settings Settings block, or NULL to search all blocks
1060 * @v setting Setting to fetch
1061 * @ret value Setting value, or zero
1062 */
1063 unsigned long fetch_uintz_setting ( struct settings *settings,
1064 const struct setting *setting ) {
1065 unsigned long value;
1066
1067 fetch_numeric_setting ( settings, setting, &value, 0 );
1068 return value;
1069 }
1070
1071 /**
1072 * Fetch value of UUID setting
1073 *
1074 * @v settings Settings block, or NULL to search all blocks
1075 * @v setting Setting to fetch
1076 * @v uuid UUID to fill in
1077 * @ret len Length of setting, or negative error
1078 */
1079 int fetch_uuid_setting ( struct settings *settings,
1080 const struct setting *setting,
1081 union uuid *uuid ) {
1082 int len;
1083
1084 len = fetch_raw_setting ( settings, setting, uuid, sizeof ( *uuid ) );
1085 if ( len < 0 )
1086 return len;
1087 if ( len != sizeof ( *uuid ) )
1088 return -ERANGE;
1089 return len;
1090 }
1091
1092 /**
1093 * Clear settings block
1094 *
1095 * @v settings Settings block
1096 */
1097 void clear_settings ( struct settings *settings ) {
1098
1099 /* Find target settings block */
1100 settings = settings_target ( settings );
1101
1102 /* Clear settings, if applicable */
1103 if ( settings->op->clear )
1104 settings->op->clear ( settings );
1105 }
1106
1107 /**
1108 * Compare two settings
1109 *
1110 * @v a Setting to compare
1111 * @v b Setting to compare
1112 * @ret 0 Settings are the same
1113 * @ret non-zero Settings are not the same
1114 */
1115 int setting_cmp ( const struct setting *a, const struct setting *b ) {
1116
1117 /* If the settings have tags, compare them */
1118 if ( a->tag && ( a->tag == b->tag ) && ( a->scope == b->scope ) )
1119 return 0;
1120
1121 /* Otherwise, if the settings have names, compare them */
1122 if ( a->name && b->name && a->name[0] )
1123 return strcmp ( a->name, b->name );
1124
1125 /* Otherwise, return a non-match */
1126 return ( ! 0 );
1127 }
1128
1129 /******************************************************************************
1130 *
1131 * Formatted setting routines
1132 *
1133 ******************************************************************************
1134 */
1135
1136 /**
1137 * Format setting value as a string
1138 *
1139 * @v type Setting type
1140 * @v raw Raw setting value
1141 * @v raw_len Length of raw setting value
1142 * @v buf Buffer to contain formatted value
1143 * @v len Length of buffer
1144 * @ret len Length of formatted value, or negative error
1145 */
1146 int setting_format ( const struct setting_type *type, const void *raw,
1147 size_t raw_len, char *buf, size_t len ) {
1148
1149 /* Sanity check */
1150 if ( ! type->format )
1151 return -ENOTSUP;
1152
1153 return type->format ( type, raw, raw_len, buf, len );
1154 }
1155
1156 /**
1157 * Parse formatted string to setting value
1158 *
1159 * @v type Setting type
1160 * @v value Formatted setting value
1161 * @v buf Buffer to contain raw value
1162 * @v len Length of buffer
1163 * @ret len Length of raw value, or negative error
1164 */
1165 int setting_parse ( const struct setting_type *type, const char *value,
1166 void *buf, size_t len ) {
1167
1168 /* Sanity check */
1169 if ( ! type->parse )
1170 return -ENOTSUP;
1171
1172 return type->parse ( type, value, buf, len );
1173 }
1174
1175 /**
1176 * Convert setting value to number
1177 *
1178 * @v type Setting type
1179 * @v raw Raw setting value
1180 * @v raw_len Length of raw setting value
1181 * @ret value Numeric value
1182 * @ret rc Return status code
1183 */
1184 int setting_numerate ( const struct setting_type *type, const void *raw,
1185 size_t raw_len, unsigned long *value ) {
1186
1187 /* Sanity check */
1188 if ( ! type->numerate )
1189 return -ENOTSUP;
1190
1191 return type->numerate ( type, raw, raw_len, value );
1192 }
1193
1194 /**
1195 * Convert number to setting value
1196 *
1197 * @v type Setting type
1198 * @v value Numeric value
1199 * @v buf Buffer to contain raw value
1200 * @v len Length of buffer
1201 * @ret len Length of raw value, or negative error
1202 */
1203 int setting_denumerate ( const struct setting_type *type, unsigned long value,
1204 void *buf, size_t len ) {
1205
1206 /* Sanity check */
1207 if ( ! type->denumerate )
1208 return -ENOTSUP;
1209
1210 return type->denumerate ( type, value, buf, len );
1211 }
1212
1213 /**
1214 * Fetch formatted value of setting
1215 *
1216 * @v settings Settings block, or NULL to search all blocks
1217 * @v setting Setting to fetch
1218 * @v origin Origin of setting to fill in, or NULL
1219 * @v fetched Fetched setting to fill in, or NULL
1220 * @v buf Buffer to contain formatted value
1221 * @v len Length of buffer
1222 * @ret len Length of formatted value, or negative error
1223 */
1224 int fetchf_setting ( struct settings *settings, const struct setting *setting,
1225 struct settings **origin, struct setting *fetched,
1226 char *buf, size_t len ) {
1227 struct setting tmp_fetched;
1228 void *raw;
1229 int raw_len;
1230 int ret;
1231
1232 /* Use local buffers if necessary */
1233 if ( ! fetched )
1234 fetched = &tmp_fetched;
1235
1236 /* Fetch raw value */
1237 raw_len = fetch_setting_copy ( settings, setting, origin, fetched,
1238 &raw );
1239 if ( raw_len < 0 ) {
1240 ret = raw_len;
1241 goto err_fetch_copy;
1242 }
1243
1244 /* Sanity check */
1245 assert ( fetched->type != NULL );
1246
1247 /* Format setting */
1248 if ( ( ret = setting_format ( fetched->type, raw, raw_len, buf,
1249 len ) ) < 0 )
1250 goto err_format;
1251
1252 err_format:
1253 free ( raw );
1254 err_fetch_copy:
1255 return ret;
1256 }
1257
1258 /**
1259 * Fetch copy of formatted value of setting
1260 *
1261 * @v settings Settings block, or NULL to search all blocks
1262 * @v setting Setting to fetch
1263 * @v origin Origin of setting to fill in, or NULL
1264 * @v fetched Fetched setting to fill in, or NULL
1265 * @v value Buffer to allocate and fill with formatted value
1266 * @ret len Length of formatted value, or negative error
1267 *
1268 * The caller is responsible for eventually freeing the allocated
1269 * buffer.
1270 */
1271 int fetchf_setting_copy ( struct settings *settings,
1272 const struct setting *setting,
1273 struct settings **origin, struct setting *fetched,
1274 char **value ) {
1275 struct settings *tmp_origin;
1276 struct setting tmp_fetched;
1277 int len;
1278 int check_len;
1279
1280 /* Use local buffers if necessary */
1281 if ( ! origin )
1282 origin = &tmp_origin;
1283 if ( ! fetched )
1284 fetched = &tmp_fetched;
1285
1286 /* Avoid returning uninitialised data on error */
1287 *value = NULL;
1288
1289 /* Check existence, and fetch formatted value length */
1290 len = fetchf_setting ( settings, setting, origin, fetched, NULL, 0 );
1291 if ( len < 0 )
1292 return len;
1293
1294 /* Allocate buffer */
1295 *value = zalloc ( len + 1 /* NUL */ );
1296 if ( ! *value )
1297 return -ENOMEM;
1298
1299 /* Fetch formatted value */
1300 check_len = fetchf_setting ( *origin, fetched, NULL, NULL, *value,
1301 ( len + 1 /* NUL */ ) );
1302 assert ( check_len == len );
1303 return len;
1304 }
1305
1306 /**
1307 * Store formatted value of setting
1308 *
1309 * @v settings Settings block
1310 * @v setting Setting to store
1311 * @v value Formatted setting data, or NULL
1312 * @ret rc Return status code
1313 */
1314 int storef_setting ( struct settings *settings, const struct setting *setting,
1315 const char *value ) {
1316 void *raw;
1317 int raw_len;
1318 int check_len;
1319 int rc;
1320
1321 /* NULL value or empty string implies deletion */
1322 if ( ( ! value ) || ( ! value[0] ) )
1323 return delete_setting ( settings, setting );
1324
1325 /* Sanity check */
1326 assert ( setting->type != NULL );
1327
1328 /* Get raw value length */
1329 raw_len = setting_parse ( setting->type, value, NULL, 0 );
1330 if ( raw_len < 0 ) {
1331 rc = raw_len;
1332 goto err_raw_len;
1333 }
1334
1335 /* Allocate buffer for raw value */
1336 raw = malloc ( raw_len );
1337 if ( ! raw ) {
1338 rc = -ENOMEM;
1339 goto err_alloc_raw;
1340 }
1341
1342 /* Parse formatted value */
1343 check_len = setting_parse ( setting->type, value, raw, raw_len );
1344 assert ( check_len == raw_len );
1345
1346 /* Store raw value */
1347 if ( ( rc = store_setting ( settings, setting, raw, raw_len ) ) != 0 )
1348 goto err_store;
1349
1350 err_store:
1351 free ( raw );
1352 err_alloc_raw:
1353 err_raw_len:
1354 return rc;
1355 }
1356
1357 /**
1358 * Fetch numeric value of setting
1359 *
1360 * @v settings Settings block, or NULL to search all blocks
1361 * @v setting Setting to fetch
1362 * @v origin Origin of setting to fill in, or NULL
1363 * @v fetched Fetched setting to fill in, or NULL
1364 * @v value Numeric value to fill in
1365 * @ret rc Return status code
1366 */
1367 int fetchn_setting ( struct settings *settings, const struct setting *setting,
1368 struct settings **origin, struct setting *fetched,
1369 unsigned long *value ) {
1370 struct setting tmp_fetched;
1371 void *raw;
1372 int raw_len;
1373 int rc;
1374
1375 /* Use local buffers if necessary */
1376 if ( ! fetched )
1377 fetched = &tmp_fetched;
1378
1379 /* Fetch raw value */
1380 raw_len = fetch_setting_copy ( settings, setting, origin, fetched,
1381 &raw );
1382 if ( raw_len < 0 ) {
1383 rc = raw_len;
1384 goto err_fetch_copy;
1385 }
1386
1387 /* Sanity check */
1388 assert ( fetched->type != NULL );
1389
1390 /* Numerate setting */
1391 if ( ( rc = setting_numerate ( fetched->type, raw, raw_len,
1392 value ) ) < 0 )
1393 goto err_numerate;
1394
1395 err_numerate:
1396 free ( raw );
1397 err_fetch_copy:
1398 return rc;
1399 }
1400
1401 /**
1402 * Store numeric value of setting
1403 *
1404 * @v settings Settings block
1405 * @v setting Setting
1406 * @v value Numeric value
1407 * @ret rc Return status code
1408 */
1409 int storen_setting ( struct settings *settings, const struct setting *setting,
1410 unsigned long value ) {
1411 void *raw;
1412 int raw_len;
1413 int check_len;
1414 int rc;
1415
1416 /* Sanity check */
1417 assert ( setting->type != NULL );
1418
1419 /* Get raw value length */
1420 raw_len = setting_denumerate ( setting->type, value, NULL, 0 );
1421 if ( raw_len < 0 ) {
1422 rc = raw_len;
1423 goto err_raw_len;
1424 }
1425
1426 /* Allocate buffer for raw value */
1427 raw = malloc ( raw_len );
1428 if ( ! raw ) {
1429 rc = -ENOMEM;
1430 goto err_alloc_raw;
1431 }
1432
1433 /* Denumerate value */
1434 check_len = setting_denumerate ( setting->type, value, raw, raw_len );
1435 assert ( check_len == raw_len );
1436
1437 /* Store raw value */
1438 if ( ( rc = store_setting ( settings, setting, raw, raw_len ) ) != 0 )
1439 goto err_store;
1440
1441 err_store:
1442 free ( raw );
1443 err_alloc_raw:
1444 err_raw_len:
1445 return rc;
1446 }
1447
1448 /******************************************************************************
1449 *
1450 * Named settings
1451 *
1452 ******************************************************************************
1453 */
1454
1455 /**
1456 * Find predefined setting
1457 *
1458 * @v name Name
1459 * @ret setting Setting, or NULL
1460 */
1461 struct setting * find_setting ( const char *name ) {
1462 struct setting *setting;
1463
1464 for_each_table_entry ( setting, SETTINGS ) {
1465 if ( strcmp ( name, setting->name ) == 0 )
1466 return setting;
1467 }
1468 return NULL;
1469 }
1470
1471 /**
1472 * Parse setting name as tag number
1473 *
1474 * @v name Name
1475 * @ret tag Tag number, or 0 if not a valid number
1476 */
1477 static unsigned int parse_setting_tag ( const char *name ) {
1478 char *tmp = ( ( char * ) name );
1479 unsigned int tag = 0;
1480
1481 while ( 1 ) {
1482 tag = ( ( tag << 8 ) | strtoul ( tmp, &tmp, 0 ) );
1483 if ( *tmp == 0 )
1484 return tag;
1485 if ( *tmp != '.' )
1486 return 0;
1487 tmp++;
1488 }
1489 }
1490
1491 /**
1492 * Find setting type
1493 *
1494 * @v name Name
1495 * @ret type Setting type, or NULL
1496 */
1497 static const struct setting_type * find_setting_type ( const char *name ) {
1498 const struct setting_type *type;
1499
1500 for_each_table_entry ( type, SETTING_TYPES ) {
1501 if ( strcmp ( name, type->name ) == 0 )
1502 return type;
1503 }
1504 return NULL;
1505 }
1506
1507 /**
1508 * Parse setting name
1509 *
1510 * @v name Name of setting
1511 * @v get_child Function to find or create child settings block
1512 * @v settings Settings block to fill in
1513 * @v setting Setting to fill in
1514 * @ret rc Return status code
1515 *
1516 * Interprets a name of the form
1517 * "[settings_name/]tag_name[:type_name]" and fills in the appropriate
1518 * fields.
1519 *
1520 * Note that on success, this function will have modified the original
1521 * setting @c name.
1522 */
1523 int parse_setting_name ( char *name, get_child_settings_t get_child,
1524 struct settings **settings, struct setting *setting ) {
1525 char *settings_name;
1526 char *setting_name;
1527 char *type_name;
1528 struct setting *predefined;
1529 int rc;
1530
1531 /* Set defaults */
1532 *settings = &settings_root;
1533 memset ( setting, 0, sizeof ( *setting ) );
1534 setting->name = "";
1535
1536 /* Split name into "[settings_name/]setting_name[:type_name]" */
1537 if ( ( setting_name = strchr ( name, '/' ) ) != NULL ) {
1538 *(setting_name++) = 0;
1539 settings_name = name;
1540 } else {
1541 setting_name = name;
1542 settings_name = NULL;
1543 }
1544 if ( ( type_name = strchr ( setting_name, ':' ) ) != NULL )
1545 *(type_name++) = 0;
1546
1547 /* Identify settings block, if specified */
1548 if ( settings_name ) {
1549 *settings = parse_settings_name ( settings_name, get_child );
1550 if ( *settings == NULL ) {
1551 DBG ( "Unrecognised settings block \"%s\" in \"%s\"\n",
1552 settings_name, name );
1553 rc = -ENODEV;
1554 goto err;
1555 }
1556 }
1557
1558 /* Identify setting */
1559 setting->tag = parse_setting_tag ( setting_name );
1560 setting->scope = (*settings)->default_scope;
1561 setting->name = setting_name;
1562 for_each_table_entry ( predefined, SETTINGS ) {
1563 /* Matches a predefined setting; use that setting */
1564 if ( setting_cmp ( predefined, setting ) == 0 ) {
1565 memcpy ( setting, predefined, sizeof ( *setting ) );
1566 break;
1567 }
1568 }
1569
1570 /* Identify setting type, if specified */
1571 if ( type_name ) {
1572 setting->type = find_setting_type ( type_name );
1573 if ( setting->type == NULL ) {
1574 DBG ( "Invalid setting type \"%s\" in \"%s\"\n",
1575 type_name, name );
1576 rc = -ENOTSUP;
1577 goto err;
1578 }
1579 }
1580
1581 return 0;
1582
1583 err:
1584 /* Restore original name */
1585 if ( settings_name )
1586 *( setting_name - 1 ) = '/';
1587 if ( type_name )
1588 *( type_name - 1 ) = ':';
1589 return rc;
1590 }
1591
1592 /**
1593 * Return full setting name
1594 *
1595 * @v settings Settings block, or NULL
1596 * @v setting Setting
1597 * @v buf Buffer
1598 * @v len Length of buffer
1599 * @ret len Length of setting name, or negative error
1600 */
1601 int setting_name ( struct settings *settings, const struct setting *setting,
1602 char *buf, size_t len ) {
1603 const char *name;
1604
1605 settings = settings_target ( settings );
1606 name = settings_name ( settings );
1607 return snprintf ( buf, len, "%s%s%s:%s", name, ( name[0] ? "/" : "" ),
1608 setting->name, setting->type->name );
1609 }
1610
1611 /******************************************************************************
1612 *
1613 * Setting types
1614 *
1615 ******************************************************************************
1616 */
1617
1618 /**
1619 * Parse string setting value
1620 *
1621 * @v type Setting type
1622 * @v value Formatted setting value
1623 * @v buf Buffer to contain raw value
1624 * @v len Length of buffer
1625 * @ret len Length of raw value, or negative error
1626 */
1627 static int parse_string_setting ( const struct setting_type *type __unused,
1628 const char *value, void *buf, size_t len ) {
1629 size_t raw_len = strlen ( value ); /* Exclude terminating NUL */
1630
1631 /* Copy string to buffer */
1632 if ( len > raw_len )
1633 len = raw_len;
1634 memcpy ( buf, value, len );
1635
1636 return raw_len;
1637 }
1638
1639 /**
1640 * Format string setting value
1641 *
1642 * @v type Setting type
1643 * @v raw Raw setting value
1644 * @v raw_len Length of raw setting value
1645 * @v buf Buffer to contain formatted value
1646 * @v len Length of buffer
1647 * @ret len Length of formatted value, or negative error
1648 */
1649 static int format_string_setting ( const struct setting_type *type __unused,
1650 const void *raw, size_t raw_len, char *buf,
1651 size_t len ) {
1652
1653 /* Copy string to buffer, and terminate */
1654 memset ( buf, 0, len );
1655 if ( len > raw_len )
1656 len = raw_len;
1657 memcpy ( buf, raw, len );
1658
1659 return raw_len;
1660 }
1661
1662 /** A string setting type */
1663 const struct setting_type setting_type_string __setting_type = {
1664 .name = "string",
1665 .parse = parse_string_setting,
1666 .format = format_string_setting,
1667 };
1668
1669 /**
1670 * Parse URI-encoded string setting value
1671 *
1672 * @v type Setting type
1673 * @v value Formatted setting value
1674 * @v buf Buffer to contain raw value
1675 * @v len Length of buffer
1676 * @ret len Length of raw value, or negative error
1677 */
1678 static int parse_uristring_setting ( const struct setting_type *type __unused,
1679 const char *value, void *buf, size_t len ){
1680
1681 return uri_decode ( value, buf, len );
1682 }
1683
1684 /**
1685 * Format URI-encoded string setting value
1686 *
1687 * @v type Setting type
1688 * @v raw Raw setting value
1689 * @v raw_len Length of raw setting value
1690 * @v buf Buffer to contain formatted value
1691 * @v len Length of buffer
1692 * @ret len Length of formatted value, or negative error
1693 */
1694 static int format_uristring_setting ( const struct setting_type *type __unused,
1695 const void *raw, size_t raw_len,
1696 char *buf, size_t len ) {
1697
1698 return uri_encode ( 0, raw, raw_len, buf, len );
1699 }
1700
1701 /** A URI-encoded string setting type */
1702 const struct setting_type setting_type_uristring __setting_type = {
1703 .name = "uristring",
1704 .parse = parse_uristring_setting,
1705 .format = format_uristring_setting,
1706 };
1707
1708 /**
1709 * Parse IPv4 address setting value (when IPv4 support is not present)
1710 *
1711 * @v type Setting type
1712 * @v value Formatted setting value
1713 * @v buf Buffer to contain raw value
1714 * @v len Length of buffer
1715 * @ret len Length of raw value, or negative error
1716 */
1717 __weak int parse_ipv4_setting ( const struct setting_type *type __unused,
1718 const char *value __unused, void *buf __unused,
1719 size_t len __unused ) {
1720 return -ENOTSUP;
1721 }
1722
1723 /**
1724 * Format IPv4 address setting value (when IPv4 support is not present)
1725 *
1726 * @v type Setting type
1727 * @v raw Raw setting value
1728 * @v raw_len Length of raw setting value
1729 * @v buf Buffer to contain formatted value
1730 * @v len Length of buffer
1731 * @ret len Length of formatted value, or negative error
1732 */
1733 __weak int format_ipv4_setting ( const struct setting_type *type __unused,
1734 const void *raw __unused,
1735 size_t raw_len __unused, char *buf __unused,
1736 size_t len __unused ) {
1737 return -ENOTSUP;
1738 }
1739
1740 /** An IPv4 address setting type */
1741 const struct setting_type setting_type_ipv4 __setting_type = {
1742 .name = "ipv4",
1743 .parse = parse_ipv4_setting,
1744 .format = format_ipv4_setting,
1745 };
1746
1747 /**
1748 * Parse IPv6 address setting value (when IPv6 support is not present)
1749 *
1750 * @v type Setting type
1751 * @v value Formatted setting value
1752 * @v buf Buffer to contain raw value
1753 * @v len Length of buffer
1754 * @ret len Length of raw value, or negative error
1755 */
1756 __weak int parse_ipv6_setting ( const struct setting_type *type __unused,
1757 const char *value __unused, void *buf __unused,
1758 size_t len __unused ) {
1759 return -ENOTSUP;
1760 }
1761
1762 /**
1763 * Format IPv6 address setting value (when IPv6 support is not present)
1764 *
1765 * @v type Setting type
1766 * @v raw Raw setting value
1767 * @v raw_len Length of raw setting value
1768 * @v buf Buffer to contain formatted value
1769 * @v len Length of buffer
1770 * @ret len Length of formatted value, or negative error
1771 */
1772 __weak int format_ipv6_setting ( const struct setting_type *type __unused,
1773 const void *raw __unused,
1774 size_t raw_len __unused, char *buf __unused,
1775 size_t len __unused ) {
1776 return -ENOTSUP;
1777 }
1778
1779 /** An IPv6 address setting type */
1780 const struct setting_type setting_type_ipv6 __setting_type = {
1781 .name = "ipv6",
1782 .parse = parse_ipv6_setting,
1783 .format = format_ipv6_setting,
1784 };
1785
1786 /** IPv6 settings scope */
1787 const struct settings_scope ipv6_scope;
1788
1789 /**
1790 * Integer setting type indices
1791 *
1792 * These indexes are defined such that (1<<index) gives the width of
1793 * the integer, in bytes.
1794 */
1795 enum setting_type_int_index {
1796 SETTING_TYPE_INT8 = 0,
1797 SETTING_TYPE_INT16 = 1,
1798 SETTING_TYPE_INT32 = 2,
1799 };
1800
1801 /**
1802 * Integer setting type names
1803 *
1804 * These names exist as a static array in order to allow the type's
1805 * integer size and signedness to be determined from the type's name.
1806 * Note that there are no separate entries for the signed integer
1807 * types: the name pointers simply point to the second character of
1808 * the relevant string.
1809 */
1810 static const char setting_type_int_name[][8] = {
1811 [SETTING_TYPE_INT8] = "uint8",
1812 [SETTING_TYPE_INT16] = "uint16",
1813 [SETTING_TYPE_INT32] = "uint32",
1814 };
1815
1816 /**
1817 * Get unsigned integer setting type name
1818 *
1819 * @v index Integer setting type index
1820 * @ret name Setting type name
1821 */
1822 #define SETTING_TYPE_UINT_NAME( index ) setting_type_int_name[index]
1823
1824 /**
1825 * Get signed integer setting type name
1826 *
1827 * @v index Integer setting type index
1828 * @ret name Setting type name
1829 */
1830 #define SETTING_TYPE_INT_NAME( index ) ( setting_type_int_name[index] + 1 )
1831
1832 /**
1833 * Get integer setting type index
1834 *
1835 * @v type Setting type
1836 * @ret index Integer setting type index
1837 */
1838 static unsigned int setting_type_int_index ( const struct setting_type *type ) {
1839
1840 return ( ( type->name - setting_type_int_name[0] ) /
1841 sizeof ( setting_type_int_name[0] ) );
1842 }
1843
1844 /**
1845 * Get integer setting type width
1846 *
1847 * @v type Setting type
1848 * @ret index Integer setting type width
1849 */
1850 static unsigned int setting_type_int_width ( const struct setting_type *type ) {
1851
1852 return ( 1 << setting_type_int_index ( type ) );
1853 }
1854
1855 /**
1856 * Get integer setting type signedness
1857 *
1858 * @v type Setting type
1859 * @ret is_signed Integer setting type is signed
1860 */
1861 static int setting_type_int_is_signed ( const struct setting_type *type ) {
1862 return ( ( type->name - setting_type_int_name[0] ) & 1 );
1863 }
1864
1865 /**
1866 * Convert number to setting value
1867 *
1868 * @v type Setting type
1869 * @v value Numeric value
1870 * @v buf Buffer to contain raw value
1871 * @v len Length of buffer
1872 * @ret len Length of raw value, or negative error
1873 */
1874 static int denumerate_int_setting ( const struct setting_type *type,
1875 unsigned long value, void *buf,
1876 size_t len ) {
1877 unsigned int size = setting_type_int_width ( type );
1878 union {
1879 uint32_t num;
1880 uint8_t bytes[4];
1881 } u;
1882
1883 u.num = htonl ( value );
1884 if ( len > size )
1885 len = size;
1886 memcpy ( buf, &u.bytes[ sizeof ( u ) - size ], len );
1887
1888 return size;
1889 }
1890
1891 /**
1892 * Convert setting value to number
1893 *
1894 * @v type Setting type
1895 * @v raw Raw setting value
1896 * @v raw_len Length of raw setting value
1897 * @v value Numeric value to fill in
1898 * @ret rc Return status code
1899 */
1900 static int numerate_int_setting ( const struct setting_type *type,
1901 const void *raw, size_t raw_len,
1902 unsigned long *value ) {
1903 int is_signed = setting_type_int_is_signed ( type );
1904 int check_len;
1905
1906 /* Extract numeric value */
1907 check_len = numeric_setting_value ( is_signed, raw, raw_len, value );
1908 if ( check_len < 0 )
1909 return check_len;
1910 assert ( check_len == ( int ) raw_len );
1911
1912 return 0;
1913 }
1914
1915 /**
1916 * Parse integer setting value
1917 *
1918 * @v type Setting type
1919 * @v value Formatted setting value
1920 * @v buf Buffer to contain raw value
1921 * @v len Length of buffer
1922 * @ret len Length of raw value, or negative error
1923 */
1924 static int parse_int_setting ( const struct setting_type *type,
1925 const char *value, void *buf, size_t len ) {
1926 char *endp;
1927 unsigned long num_value;
1928
1929 /* Parse value */
1930 num_value = strtoul ( value, &endp, 0 );
1931 if ( *endp )
1932 return -EINVAL;
1933
1934 return type->denumerate ( type, num_value, buf, len );
1935 }
1936
1937 /**
1938 * Format signed integer setting value
1939 *
1940 * @v type Setting type
1941 * @v raw Raw setting value
1942 * @v raw_len Length of raw setting value
1943 * @v buf Buffer to contain formatted value
1944 * @v len Length of buffer
1945 * @ret len Length of formatted value, or negative error
1946 */
1947 static int format_int_setting ( const struct setting_type *type,
1948 const void *raw, size_t raw_len,
1949 char *buf, size_t len ) {
1950 unsigned long value;
1951 int ret;
1952
1953 /* Extract numeric value */
1954 if ( ( ret = type->numerate ( type, raw, raw_len, &value ) ) < 0 )
1955 return ret;
1956
1957 /* Format value */
1958 return snprintf ( buf, len, "%ld", value );
1959 }
1960
1961 /**
1962 * Format unsigned integer setting value
1963 *
1964 * @v type Setting type
1965 * @v raw Raw setting value
1966 * @v raw_len Length of raw setting value
1967 * @v buf Buffer to contain formatted value
1968 * @v len Length of buffer
1969 * @ret len Length of formatted value, or negative error
1970 */
1971 static int format_uint_setting ( const struct setting_type *type,
1972 const void *raw, size_t raw_len,
1973 char *buf, size_t len ) {
1974 unsigned long value;
1975 int ret;
1976
1977 /* Extract numeric value */
1978 if ( ( ret = type->numerate ( type, raw, raw_len, &value ) ) < 0 )
1979 return ret;
1980
1981 /* Format value */
1982 return snprintf ( buf, len, "%#lx", value );
1983 }
1984
1985 /**
1986 * Define a signed integer setting type
1987 *
1988 * @v index Integer setting type index
1989 * @ret type Setting type
1990 */
1991 #define SETTING_TYPE_INT( index ) { \
1992 .name = SETTING_TYPE_INT_NAME ( index ), \
1993 .parse = parse_int_setting, \
1994 .format = format_int_setting, \
1995 .denumerate = denumerate_int_setting, \
1996 .numerate = numerate_int_setting, \
1997 }
1998
1999 /**
2000 * Define an unsigned integer setting type
2001 *
2002 * @v index Integer setting type index
2003 * @ret type Setting type
2004 */
2005 #define SETTING_TYPE_UINT( index ) { \
2006 .name = SETTING_TYPE_UINT_NAME ( index ), \
2007 .parse = parse_int_setting, \
2008 .format = format_uint_setting, \
2009 .denumerate = denumerate_int_setting, \
2010 .numerate = numerate_int_setting, \
2011 }
2012
2013 /** A signed 8-bit integer setting type */
2014 const struct setting_type setting_type_int8 __setting_type =
2015 SETTING_TYPE_INT ( SETTING_TYPE_INT8 );
2016
2017 /** A signed 16-bit integer setting type */
2018 const struct setting_type setting_type_int16 __setting_type =
2019 SETTING_TYPE_INT ( SETTING_TYPE_INT16 );
2020
2021 /** A signed 32-bit integer setting type */
2022 const struct setting_type setting_type_int32 __setting_type =
2023 SETTING_TYPE_INT ( SETTING_TYPE_INT32 );
2024
2025 /** An unsigned 8-bit integer setting type */
2026 const struct setting_type setting_type_uint8 __setting_type =
2027 SETTING_TYPE_UINT ( SETTING_TYPE_INT8 );
2028
2029 /** An unsigned 16-bit integer setting type */
2030 const struct setting_type setting_type_uint16 __setting_type =
2031 SETTING_TYPE_UINT ( SETTING_TYPE_INT16 );
2032
2033 /** An unsigned 32-bit integer setting type */
2034 const struct setting_type setting_type_uint32 __setting_type =
2035 SETTING_TYPE_UINT ( SETTING_TYPE_INT32 );
2036
2037 /**
2038 * Parse hex string setting value (using colon delimiter)
2039 *
2040 * @v type Setting type
2041 * @v value Formatted setting value
2042 * @v buf Buffer to contain raw value
2043 * @v len Length of buffer
2044 * @v size Integer size, in bytes
2045 * @ret len Length of raw value, or negative error
2046 */
2047 static int parse_hex_setting ( const struct setting_type *type __unused,
2048 const char *value, void *buf, size_t len ) {
2049 return hex_decode ( ':', value, buf, len );
2050 }
2051
2052 /**
2053 * Format hex string setting value (using colon delimiter)
2054 *
2055 * @v type Setting type
2056 * @v raw Raw setting value
2057 * @v raw_len Length of raw setting value
2058 * @v buf Buffer to contain formatted value
2059 * @v len Length of buffer
2060 * @ret len Length of formatted value, or negative error
2061 */
2062 static int format_hex_colon_setting ( const struct setting_type *type __unused,
2063 const void *raw, size_t raw_len,
2064 char *buf, size_t len ) {
2065 return hex_encode ( ':', raw, raw_len, buf, len );
2066 }
2067
2068 /**
2069 * Parse hex string setting value (using hyphen delimiter)
2070 *
2071 * @v type Setting type
2072 * @v value Formatted setting value
2073 * @v buf Buffer to contain raw value
2074 * @v len Length of buffer
2075 * @v size Integer size, in bytes
2076 * @ret len Length of raw value, or negative error
2077 */
2078 static int parse_hex_hyphen_setting ( const struct setting_type *type __unused,
2079 const char *value, void *buf,
2080 size_t len ) {
2081 return hex_decode ( '-', value, buf, len );
2082 }
2083
2084 /**
2085 * Format hex string setting value (using hyphen delimiter)
2086 *
2087 * @v type Setting type
2088 * @v raw Raw setting value
2089 * @v raw_len Length of raw setting value
2090 * @v buf Buffer to contain formatted value
2091 * @v len Length of buffer
2092 * @ret len Length of formatted value, or negative error
2093 */
2094 static int format_hex_hyphen_setting ( const struct setting_type *type __unused,
2095 const void *raw, size_t raw_len,
2096 char *buf, size_t len ) {
2097 return hex_encode ( '-', raw, raw_len, buf, len );
2098 }
2099
2100 /**
2101 * Parse hex string setting value (using no delimiter)
2102 *
2103 * @v type Setting type
2104 * @v value Formatted setting value
2105 * @v buf Buffer to contain raw value
2106 * @v len Length of buffer
2107 * @v size Integer size, in bytes
2108 * @ret len Length of raw value, or negative error
2109 */
2110 static int parse_hex_raw_setting ( const struct setting_type *type __unused,
2111 const char *value, void *buf, size_t len ) {
2112 return hex_decode ( 0, value, buf, len );
2113 }
2114
2115 /**
2116 * Format hex string setting value (using no delimiter)
2117 *
2118 * @v type Setting type
2119 * @v raw Raw setting value
2120 * @v raw_len Length of raw setting value
2121 * @v buf Buffer to contain formatted value
2122 * @v len Length of buffer
2123 * @ret len Length of formatted value, or negative error
2124 */
2125 static int format_hex_raw_setting ( const struct setting_type *type __unused,
2126 const void *raw, size_t raw_len,
2127 char *buf, size_t len ) {
2128 return hex_encode ( 0, raw, raw_len, buf, len );
2129 }
2130
2131 /** A hex-string setting (colon-delimited) */
2132 const struct setting_type setting_type_hex __setting_type = {
2133 .name = "hex",
2134 .parse = parse_hex_setting,
2135 .format = format_hex_colon_setting,
2136 };
2137
2138 /** A hex-string setting (hyphen-delimited) */
2139 const struct setting_type setting_type_hexhyp __setting_type = {
2140 .name = "hexhyp",
2141 .parse = parse_hex_hyphen_setting,
2142 .format = format_hex_hyphen_setting,
2143 };
2144
2145 /** A hex-string setting (non-delimited) */
2146 const struct setting_type setting_type_hexraw __setting_type = {
2147 .name = "hexraw",
2148 .parse = parse_hex_raw_setting,
2149 .format = format_hex_raw_setting,
2150 };
2151
2152 /**
2153 * Parse Base64-encoded setting value
2154 *
2155 * @v type Setting type
2156 * @v value Formatted setting value
2157 * @v buf Buffer to contain raw value
2158 * @v len Length of buffer
2159 * @v size Integer size, in bytes
2160 * @ret len Length of raw value, or negative error
2161 */
2162 static int parse_base64_setting ( const struct setting_type *type __unused,
2163 const char *value, void *buf, size_t len ) {
2164
2165 return base64_decode ( value, buf, len );
2166 }
2167
2168 /**
2169 * Format Base64-encoded setting value
2170 *
2171 * @v type Setting type
2172 * @v raw Raw setting value
2173 * @v raw_len Length of raw setting value
2174 * @v buf Buffer to contain formatted value
2175 * @v len Length of buffer
2176 * @ret len Length of formatted value, or negative error
2177 */
2178 static int format_base64_setting ( const struct setting_type *type __unused,
2179 const void *raw, size_t raw_len,
2180 char *buf, size_t len ) {
2181
2182 return base64_encode ( raw, raw_len, buf, len );
2183 }
2184
2185 /** A Base64-encoded setting */
2186 const struct setting_type setting_type_base64 __setting_type = {
2187 .name = "base64",
2188 .parse = parse_base64_setting,
2189 .format = format_base64_setting,
2190 };
2191
2192 /**
2193 * Format UUID setting value
2194 *
2195 * @v type Setting type
2196 * @v raw Raw setting value
2197 * @v raw_len Length of raw setting value
2198 * @v buf Buffer to contain formatted value
2199 * @v len Length of buffer
2200 * @ret len Length of formatted value, or negative error
2201 */
2202 static int format_uuid_setting ( const struct setting_type *type __unused,
2203 const void *raw, size_t raw_len, char *buf,
2204 size_t len ) {
2205 const union uuid *uuid = raw;
2206
2207 /* Range check */
2208 if ( raw_len != sizeof ( *uuid ) )
2209 return -ERANGE;
2210
2211 /* Format value */
2212 return snprintf ( buf, len, "%s", uuid_ntoa ( uuid ) );
2213 }
2214
2215 /** UUID setting type */
2216 const struct setting_type setting_type_uuid __setting_type = {
2217 .name = "uuid",
2218 .format = format_uuid_setting,
2219 };
2220
2221 /**
2222 * Format PCI bus:dev.fn setting value
2223 *
2224 * @v type Setting type
2225 * @v raw Raw setting value
2226 * @v raw_len Length of raw setting value
2227 * @v buf Buffer to contain formatted value
2228 * @v len Length of buffer
2229 * @ret len Length of formatted value, or negative error
2230 */
2231 static int format_busdevfn_setting ( const struct setting_type *type __unused,
2232 const void *raw, size_t raw_len, char *buf,
2233 size_t len ) {
2234 unsigned long busdevfn;
2235 int check_len;
2236
2237 /* Extract numeric value */
2238 check_len = numeric_setting_value ( 0, raw, raw_len, &busdevfn );
2239 if ( check_len < 0 )
2240 return check_len;
2241 assert ( check_len == ( int ) raw_len );
2242
2243 /* Format value */
2244 return snprintf ( buf, len, "%02lx:%02lx.%lx", PCI_BUS ( busdevfn ),
2245 PCI_SLOT ( busdevfn ), PCI_FUNC ( busdevfn ) );
2246 }
2247
2248 /** PCI bus:dev.fn setting type */
2249 const struct setting_type setting_type_busdevfn __setting_type = {
2250 .name = "busdevfn",
2251 .format = format_busdevfn_setting,
2252 };
2253
2254 /******************************************************************************
2255 *
2256 * Setting expansion
2257 *
2258 ******************************************************************************
2259 */
2260
2261 /**
2262 * Expand variables within string
2263 *
2264 * @v string String
2265 * @ret expstr Expanded string
2266 *
2267 * The expanded string is allocated with malloc() and the caller must
2268 * eventually free() it.
2269 */
2270 char * expand_settings ( const char *string ) {
2271 struct settings *settings;
2272 struct setting setting;
2273 char *expstr;
2274 char *start;
2275 char *end;
2276 char *head;
2277 char *name;
2278 char *tail;
2279 char *value;
2280 char *tmp;
2281 int new_len;
2282 int rc;
2283
2284 /* Obtain temporary modifiable copy of string */
2285 expstr = strdup ( string );
2286 if ( ! expstr )
2287 return NULL;
2288
2289 /* Expand while expansions remain */
2290 while ( 1 ) {
2291
2292 head = expstr;
2293
2294 /* Locate setting to be expanded */
2295 start = NULL;
2296 end = NULL;
2297 for ( tmp = expstr ; *tmp ; tmp++ ) {
2298 if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) )
2299 start = tmp;
2300 if ( start && ( tmp[0] == '}' ) ) {
2301 end = tmp;
2302 break;
2303 }
2304 }
2305 if ( ! end )
2306 break;
2307 *start = '\0';
2308 name = ( start + 2 );
2309 *end = '\0';
2310 tail = ( end + 1 );
2311
2312 /* Expand setting */
2313 if ( ( rc = parse_setting_name ( name, find_child_settings,
2314 &settings,
2315 &setting ) ) != 0 ) {
2316 /* Treat invalid setting names as empty */
2317 value = NULL;
2318 } else {
2319 /* Fetch and format setting value. Ignore
2320 * errors; treat non-existent settings as empty.
2321 */
2322 fetchf_setting_copy ( settings, &setting, NULL, NULL,
2323 &value );
2324 }
2325
2326 /* Construct expanded string and discard old string */
2327 tmp = expstr;
2328 new_len = asprintf ( &expstr, "%s%s%s",
2329 head, ( value ? value : "" ), tail );
2330 free ( value );
2331 free ( tmp );
2332 if ( new_len < 0 )
2333 return NULL;
2334 }
2335
2336 return expstr;
2337 }
2338
2339 /******************************************************************************
2340 *
2341 * Settings
2342 *
2343 ******************************************************************************
2344 */
2345
2346 /** Hostname setting */
2347 const struct setting hostname_setting __setting ( SETTING_HOST, hostname ) = {
2348 .name = "hostname",
2349 .description = "Host name",
2350 .tag = DHCP_HOST_NAME,
2351 .type = &setting_type_string,
2352 };
2353
2354 /** Domain name setting */
2355 const struct setting domain_setting __setting ( SETTING_IP_EXTRA, domain ) = {
2356 .name = "domain",
2357 .description = "DNS domain",
2358 .tag = DHCP_DOMAIN_NAME,
2359 .type = &setting_type_string,
2360 };
2361
2362 /** TFTP server setting */
2363 const struct setting next_server_setting __setting ( SETTING_BOOT,next-server)={
2364 .name = "next-server",
2365 .description = "TFTP server",
2366 .tag = DHCP_EB_SIADDR,
2367 .type = &setting_type_ipv4,
2368 };
2369
2370 /** Filename setting */
2371 const struct setting filename_setting __setting ( SETTING_BOOT, filename ) = {
2372 .name = "filename",
2373 .description = "Boot filename",
2374 .tag = DHCP_BOOTFILE_NAME,
2375 .type = &setting_type_string,
2376 };
2377
2378 /** Root path setting */
2379 const struct setting root_path_setting __setting ( SETTING_SANBOOT, root-path)={
2380 .name = "root-path",
2381 .description = "SAN root path",
2382 .tag = DHCP_ROOT_PATH,
2383 .type = &setting_type_string,
2384 };
2385
2386 /** Username setting */
2387 const struct setting username_setting __setting ( SETTING_AUTH, username ) = {
2388 .name = "username",
2389 .description = "User name",
2390 .tag = DHCP_EB_USERNAME,
2391 .type = &setting_type_string,
2392 };
2393
2394 /** Password setting */
2395 const struct setting password_setting __setting ( SETTING_AUTH, password ) = {
2396 .name = "password",
2397 .description = "Password",
2398 .tag = DHCP_EB_PASSWORD,
2399 .type = &setting_type_string,
2400 };
2401
2402 /** Priority setting */
2403 const struct setting priority_setting __setting ( SETTING_MISC, priority ) = {
2404 .name = "priority",
2405 .description = "Settings priority",
2406 .tag = DHCP_EB_PRIORITY,
2407 .type = &setting_type_int8,
2408 };
2409
2410 /** DHCP user class setting */
2411 const struct setting user_class_setting __setting ( SETTING_HOST_EXTRA,
2412 user-class ) = {
2413 .name = "user-class",
2414 .description = "DHCP user class",
2415 .tag = DHCP_USER_CLASS_ID,
2416 .type = &setting_type_string,
2417 };
2418
2419 /******************************************************************************
2420 *
2421 * Built-in settings block
2422 *
2423 ******************************************************************************
2424 */
2425
2426 /** Built-in setting scope */
2427 const struct settings_scope builtin_scope;
2428
2429 /**
2430 * Fetch error number setting
2431 *
2432 * @v data Buffer to fill with setting data
2433 * @v len Length of buffer
2434 * @ret len Length of setting data, or negative error
2435 */
2436 static int errno_fetch ( void *data, size_t len ) {
2437 uint32_t content;
2438
2439 /* Return current error */
2440 content = htonl ( errno );
2441 if ( len > sizeof ( content ) )
2442 len = sizeof ( content );
2443 memcpy ( data, &content, len );
2444 return sizeof ( content );
2445 }
2446
2447 /** Error number setting */
2448 const struct setting errno_setting __setting ( SETTING_MISC, errno ) = {
2449 .name = "errno",
2450 .description = "Last error",
2451 .type = &setting_type_uint32,
2452 .scope = &builtin_scope,
2453 };
2454
2455 /** Error number built-in setting */
2456 struct builtin_setting errno_builtin_setting __builtin_setting = {
2457 .setting = &errno_setting,
2458 .fetch = errno_fetch,
2459 };
2460
2461 /**
2462 * Fetch build architecture setting
2463 *
2464 * @v data Buffer to fill with setting data
2465 * @v len Length of buffer
2466 * @ret len Length of setting data, or negative error
2467 */
2468 static int buildarch_fetch ( void *data, size_t len ) {
2469 static const char buildarch[] = _S2 ( ARCH );
2470
2471 strncpy ( data, buildarch, len );
2472 return ( sizeof ( buildarch ) - 1 /* NUL */ );
2473 }
2474
2475 /** Build architecture setting */
2476 const struct setting buildarch_setting __setting ( SETTING_MISC, buildarch ) = {
2477 .name = "buildarch",
2478 .description = "Build architecture",
2479 .type = &setting_type_string,
2480 .scope = &builtin_scope,
2481 };
2482
2483 /** Build architecture built-in setting */
2484 struct builtin_setting buildarch_builtin_setting __builtin_setting = {
2485 .setting = &buildarch_setting,
2486 .fetch = buildarch_fetch,
2487 };
2488
2489 /**
2490 * Fetch platform setting
2491 *
2492 * @v data Buffer to fill with setting data
2493 * @v len Length of buffer
2494 * @ret len Length of setting data, or negative error
2495 */
2496 static int platform_fetch ( void *data, size_t len ) {
2497 static const char platform[] = _S2 ( PLATFORM );
2498
2499 strncpy ( data, platform, len );
2500 return ( sizeof ( platform ) - 1 /* NUL */ );
2501 }
2502
2503 /** Platform setting */
2504 const struct setting platform_setting __setting ( SETTING_MISC, platform ) = {
2505 .name = "platform",
2506 .description = "Platform",
2507 .type = &setting_type_string,
2508 .scope = &builtin_scope,
2509 };
2510
2511 /** Platform built-in setting */
2512 struct builtin_setting platform_builtin_setting __builtin_setting = {
2513 .setting = &platform_setting,
2514 .fetch = platform_fetch,
2515 };
2516
2517 /**
2518 * Fetch version setting
2519 *
2520 * @v data Buffer to fill with setting data
2521 * @v len Length of buffer
2522 * @ret len Length of setting data, or negative error
2523 */
2524 static int version_fetch ( void *data, size_t len ) {
2525 strncpy ( data, product_version, len );
2526 return ( strlen ( product_version ) );
2527 }
2528
2529 /** Version setting */
2530 const struct setting version_setting __setting ( SETTING_MISC, version ) = {
2531 .name = "version",
2532 .description = "Version",
2533 .type = &setting_type_string,
2534 .scope = &builtin_scope,
2535 };
2536
2537 /** Version built-in setting */
2538 struct builtin_setting version_builtin_setting __builtin_setting = {
2539 .setting = &version_setting,
2540 .fetch = version_fetch,
2541 };
2542
2543 /**
2544 * Fetch built-in setting
2545 *
2546 * @v settings Settings block
2547 * @v setting Setting to fetch
2548 * @v data Buffer to fill with setting data
2549 * @v len Length of buffer
2550 * @ret len Length of setting data, or negative error
2551 */
2552 static int builtin_fetch ( struct settings *settings __unused,
2553 struct setting *setting,
2554 void *data, size_t len ) {
2555 struct builtin_setting *builtin;
2556
2557 for_each_table_entry ( builtin, BUILTIN_SETTINGS ) {
2558 if ( setting_cmp ( setting, builtin->setting ) == 0 )
2559 return builtin->fetch ( data, len );
2560 }
2561 return -ENOENT;
2562 }
2563
2564 /**
2565 * Check applicability of built-in setting
2566 *
2567 * @v settings Settings block
2568 * @v setting Setting
2569 * @ret applies Setting applies within this settings block
2570 */
2571 static int builtin_applies ( struct settings *settings __unused,
2572 const struct setting *setting ) {
2573
2574 return ( setting->scope == &builtin_scope );
2575 }
2576
2577 /** Built-in settings operations */
2578 static struct settings_operations builtin_settings_operations = {
2579 .applies = builtin_applies,
2580 .fetch = builtin_fetch,
2581 };
2582
2583 /** Built-in settings */
2584 static struct settings builtin_settings = {
2585 .refcnt = NULL,
2586 .siblings = LIST_HEAD_INIT ( builtin_settings.siblings ),
2587 .children = LIST_HEAD_INIT ( builtin_settings.children ),
2588 .op = &builtin_settings_operations,
2589 };
2590
2591 /** Initialise built-in settings */
2592 static void builtin_init ( void ) {
2593 int rc;
2594
2595 if ( ( rc = register_settings ( &builtin_settings, NULL,
2596 "builtin" ) ) != 0 ) {
2597 DBG ( "Could not register built-in settings: %s\n",
2598 strerror ( rc ) );
2599 return;
2600 }
2601 }
2602
2603 /** Built-in settings initialiser */
2604 struct init_fn builtin_init_fn __init_fn ( INIT_NORMAL ) = {
2605 .initialise = builtin_init,
2606 };