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