You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

settings.h 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #ifndef _IPXE_SETTINGS_H
  2. #define _IPXE_SETTINGS_H
  3. /** @file
  4. *
  5. * Configuration settings
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <ipxe/tables.h>
  11. #include <ipxe/list.h>
  12. #include <ipxe/refcnt.h>
  13. struct settings;
  14. struct in_addr;
  15. struct in6_addr;
  16. union uuid;
  17. /** A setting */
  18. struct setting {
  19. /** Name
  20. *
  21. * This is the human-readable name for the setting.
  22. */
  23. const char *name;
  24. /** Description */
  25. const char *description;
  26. /** Setting type
  27. *
  28. * This identifies the type of setting (e.g. string, IPv4
  29. * address, etc.).
  30. */
  31. const struct setting_type *type;
  32. /** Setting tag, if applicable
  33. *
  34. * The setting tag is a numerical description of the setting
  35. * (such as a DHCP option number, or an SMBIOS structure and
  36. * field number).
  37. */
  38. unsigned int tag;
  39. /** Setting scope (or NULL)
  40. *
  41. * For historic reasons, a NULL scope with a non-zero tag
  42. * indicates a DHCPv4 option setting.
  43. */
  44. const struct settings_scope *scope;
  45. };
  46. /** Configuration setting table */
  47. #define SETTINGS __table ( struct setting, "settings" )
  48. /** Declare a configuration setting */
  49. #define __setting( setting_order, name ) \
  50. __table_entry ( SETTINGS, setting_order.name )
  51. /** @defgroup setting_order Setting ordering
  52. * @{
  53. */
  54. #define SETTING_NETDEV 01 /**< Network device settings */
  55. #define SETTING_NETDEV_EXTRA 02 /**< Network device additional settings */
  56. #define SETTING_IP 03 /**< IPv4 settings */
  57. #define SETTING_IP_EXTRA 04 /**< IPv4 additional settings */
  58. #define SETTING_BOOT 05 /**< Generic boot settings */
  59. #define SETTING_BOOT_EXTRA 06 /**< Generic boot additional settings */
  60. #define SETTING_SANBOOT 07 /**< SAN boot settings */
  61. #define SETTING_SANBOOT_EXTRA 08 /**< SAN boot additional settings */
  62. #define SETTING_HOST 09 /**< Host identity settings */
  63. #define SETTING_HOST_EXTRA 10 /**< Host identity additional settings */
  64. #define SETTING_AUTH 11 /**< Authentication settings */
  65. #define SETTING_AUTH_EXTRA 12 /**< Authentication additional settings */
  66. #define SETTING_CRYPTO 13 /**< Cryptography settings */
  67. #define SETTING_MISC 14 /**< Miscellaneous settings */
  68. /** @} */
  69. /** Settings block operations */
  70. struct settings_operations {
  71. /** Redirect to underlying settings block (if applicable)
  72. *
  73. * @v settings Settings block
  74. * @ret settings Underlying settings block
  75. */
  76. struct settings * ( * redirect ) ( struct settings *settings );
  77. /** Check applicability of setting
  78. *
  79. * @v settings Settings block
  80. * @v setting Setting
  81. * @ret applies Setting applies within this settings block
  82. */
  83. int ( * applies ) ( struct settings *settings,
  84. const struct setting *setting );
  85. /** Store value of setting
  86. *
  87. * @v settings Settings block
  88. * @v setting Setting to store
  89. * @v data Setting data, or NULL to clear setting
  90. * @v len Length of setting data
  91. * @ret rc Return status code
  92. */
  93. int ( * store ) ( struct settings *settings,
  94. const struct setting *setting,
  95. const void *data, size_t len );
  96. /** Fetch value of setting
  97. *
  98. * @v settings Settings block
  99. * @v setting Setting to fetch
  100. * @v data Buffer to fill with setting data
  101. * @v len Length of buffer
  102. * @ret len Length of setting data, or negative error
  103. *
  104. * The actual length of the setting will be returned even if
  105. * the buffer was too small.
  106. */
  107. int ( * fetch ) ( struct settings *settings, struct setting *setting,
  108. void *data, size_t len );
  109. /** Clear settings block
  110. *
  111. * @v settings Settings block
  112. */
  113. void ( * clear ) ( struct settings *settings );
  114. };
  115. /** A settings block */
  116. struct settings {
  117. /** Reference counter */
  118. struct refcnt *refcnt;
  119. /** Name */
  120. const char *name;
  121. /** Parent settings block */
  122. struct settings *parent;
  123. /** Sibling settings blocks */
  124. struct list_head siblings;
  125. /** Child settings blocks */
  126. struct list_head children;
  127. /** Settings block operations */
  128. struct settings_operations *op;
  129. /** Default scope for numerical settings constructed for this block */
  130. const struct settings_scope *default_scope;
  131. };
  132. /**
  133. * A setting scope
  134. *
  135. * Users can construct tags for settings that are not explicitly known
  136. * to iPXE using the generic syntax for numerical settings. For
  137. * example, the setting name "60" will be interpreted as referring to
  138. * DHCP option 60 (the vendor class identifier).
  139. *
  140. * This creates a potential for namespace collisions, since the
  141. * interpretation of the numerical description will vary according to
  142. * the settings block. When a user attempts to fetch a generic
  143. * numerical setting, we need to ensure that only the intended
  144. * settings blocks interpret this numerical description. (For
  145. * example, we do not want to attempt to retrieve the subnet mask from
  146. * SMBIOS, or the system UUID from DHCP.)
  147. *
  148. * This potential problem is resolved by including a user-invisible
  149. * "scope" within the definition of each setting. Settings blocks may
  150. * use this to determine whether or not the setting is applicable.
  151. * Any settings constructed from a numerical description
  152. * (e.g. "smbios/1.4.0") will be assigned the default scope of the
  153. * settings block specified in the description (e.g. "smbios"); this
  154. * provides behaviour matching the user's expectations in most
  155. * circumstances.
  156. */
  157. struct settings_scope {
  158. /** Dummy field
  159. *
  160. * This is included only to ensure that pointers to different
  161. * scopes always compare differently.
  162. */
  163. uint8_t dummy;
  164. } __attribute__ (( packed ));
  165. /**
  166. * A setting type
  167. *
  168. * This represents a type of setting (e.g. string, IPv4 address,
  169. * etc.).
  170. */
  171. struct setting_type {
  172. /** Name
  173. *
  174. * This is the name exposed to the user (e.g. "string").
  175. */
  176. const char *name;
  177. /** Parse formatted string to setting value
  178. *
  179. * @v type Setting type
  180. * @v value Formatted setting value
  181. * @v buf Buffer to contain raw value
  182. * @v len Length of buffer
  183. * @ret len Length of raw value, or negative error
  184. */
  185. int ( * parse ) ( const struct setting_type *type, const char *value,
  186. void *buf, size_t len );
  187. /** Format setting value as a string
  188. *
  189. * @v type Setting type
  190. * @v raw Raw setting value
  191. * @v raw_len Length of raw setting value
  192. * @v buf Buffer to contain formatted value
  193. * @v len Length of buffer
  194. * @ret len Length of formatted value, or negative error
  195. */
  196. int ( * format ) ( const struct setting_type *type, const void *raw,
  197. size_t raw_len, char *buf, size_t len );
  198. /** Convert number to setting value
  199. *
  200. * @v type Setting type
  201. * @v value Numeric value
  202. * @v buf Buffer to contain raw value
  203. * @v len Length of buffer
  204. * @ret len Length of raw value, or negative error
  205. */
  206. int ( * denumerate ) ( const struct setting_type *type,
  207. unsigned long value,
  208. void *buf, size_t len );
  209. /** Convert setting value to number
  210. *
  211. * @v type Setting type
  212. * @v raw Raw setting value
  213. * @v raw_len Length of raw setting value
  214. * @v value Numeric value to fill in
  215. * @ret rc Return status code
  216. */
  217. int ( * numerate ) ( const struct setting_type *type, const void *raw,
  218. size_t raw_len, unsigned long *value );
  219. };
  220. /** Configuration setting type table */
  221. #define SETTING_TYPES __table ( struct setting_type, "setting_types" )
  222. /** Declare a configuration setting type */
  223. #define __setting_type __table_entry ( SETTING_TYPES, 01 )
  224. /**
  225. * A settings applicator
  226. *
  227. */
  228. struct settings_applicator {
  229. /** Apply updated settings
  230. *
  231. * @ret rc Return status code
  232. */
  233. int ( * apply ) ( void );
  234. };
  235. /** Settings applicator table */
  236. #define SETTINGS_APPLICATORS \
  237. __table ( struct settings_applicator, "settings_applicators" )
  238. /** Declare a settings applicator */
  239. #define __settings_applicator __table_entry ( SETTINGS_APPLICATORS, 01 )
  240. /** A built-in setting */
  241. struct builtin_setting {
  242. /** Setting */
  243. const struct setting *setting;
  244. /** Fetch setting value
  245. *
  246. * @v data Buffer to fill with setting data
  247. * @v len Length of buffer
  248. * @ret len Length of setting data, or negative error
  249. */
  250. int ( * fetch ) ( void *data, size_t len );
  251. };
  252. /** Built-in settings table */
  253. #define BUILTIN_SETTINGS __table ( struct builtin_setting, "builtin_settings" )
  254. /** Declare a built-in setting */
  255. #define __builtin_setting __table_entry ( BUILTIN_SETTINGS, 01 )
  256. /** Built-in setting scope */
  257. extern const struct settings_scope builtin_scope;
  258. /** IPv6 setting scope */
  259. extern const struct settings_scope ipv6_scope;
  260. /**
  261. * A generic settings block
  262. *
  263. */
  264. struct generic_settings {
  265. /** Settings block */
  266. struct settings settings;
  267. /** List of generic settings */
  268. struct list_head list;
  269. };
  270. /** A child settings block locator function */
  271. typedef struct settings * ( *get_child_settings_t ) ( struct settings *settings,
  272. const char *name );
  273. extern struct settings_operations generic_settings_operations;
  274. extern int generic_settings_store ( struct settings *settings,
  275. const struct setting *setting,
  276. const void *data, size_t len );
  277. extern int generic_settings_fetch ( struct settings *settings,
  278. struct setting *setting,
  279. void *data, size_t len );
  280. extern void generic_settings_clear ( struct settings *settings );
  281. extern int register_settings ( struct settings *settings,
  282. struct settings *parent, const char *name );
  283. extern void unregister_settings ( struct settings *settings );
  284. extern struct settings * settings_target ( struct settings *settings );
  285. extern int setting_applies ( struct settings *settings,
  286. const struct setting *setting );
  287. extern int store_setting ( struct settings *settings,
  288. const struct setting *setting,
  289. const void *data, size_t len );
  290. extern int fetch_setting ( struct settings *settings,
  291. const struct setting *setting,
  292. struct settings **origin, struct setting *fetched,
  293. void *data, size_t len );
  294. extern int fetch_setting_copy ( struct settings *settings,
  295. const struct setting *setting,
  296. struct settings **origin,
  297. struct setting *fetched, void **data );
  298. extern int fetch_raw_setting ( struct settings *settings,
  299. const struct setting *setting,
  300. void *data, size_t len );
  301. extern int fetch_raw_setting_copy ( struct settings *settings,
  302. const struct setting *setting,
  303. void **data );
  304. extern int fetch_string_setting ( struct settings *settings,
  305. const struct setting *setting,
  306. char *data, size_t len );
  307. extern int fetch_string_setting_copy ( struct settings *settings,
  308. const struct setting *setting,
  309. char **data );
  310. extern int fetch_ipv4_array_setting ( struct settings *settings,
  311. const struct setting *setting,
  312. struct in_addr *inp, unsigned int count );
  313. extern int fetch_ipv4_setting ( struct settings *settings,
  314. const struct setting *setting,
  315. struct in_addr *inp );
  316. extern int fetch_ipv6_array_setting ( struct settings *settings,
  317. const struct setting *setting,
  318. struct in6_addr *inp, unsigned int count);
  319. extern int fetch_ipv6_setting ( struct settings *settings,
  320. const struct setting *setting,
  321. struct in6_addr *inp );
  322. extern int fetch_int_setting ( struct settings *settings,
  323. const struct setting *setting, long *value );
  324. extern int fetch_uint_setting ( struct settings *settings,
  325. const struct setting *setting,
  326. unsigned long *value );
  327. extern long fetch_intz_setting ( struct settings *settings,
  328. const struct setting *setting );
  329. extern unsigned long fetch_uintz_setting ( struct settings *settings,
  330. const struct setting *setting );
  331. extern int fetch_uuid_setting ( struct settings *settings,
  332. const struct setting *setting,
  333. union uuid *uuid );
  334. extern void clear_settings ( struct settings *settings );
  335. extern int setting_cmp ( const struct setting *a, const struct setting *b );
  336. extern struct settings * find_child_settings ( struct settings *parent,
  337. const char *name );
  338. extern struct settings * autovivify_child_settings ( struct settings *parent,
  339. const char *name );
  340. extern const char * settings_name ( struct settings *settings );
  341. extern struct settings * find_settings ( const char *name );
  342. extern struct setting * find_setting ( const char *name );
  343. extern int parse_setting_name ( char *name, get_child_settings_t get_child,
  344. struct settings **settings,
  345. struct setting *setting );
  346. extern int setting_name ( struct settings *settings,
  347. const struct setting *setting,
  348. char *buf, size_t len );
  349. extern int setting_format ( const struct setting_type *type, const void *raw,
  350. size_t raw_len, char *buf, size_t len );
  351. extern int setting_parse ( const struct setting_type *type, const char *value,
  352. void *buf, size_t len );
  353. extern int setting_numerate ( const struct setting_type *type, const void *raw,
  354. size_t raw_len, unsigned long *value );
  355. extern int setting_denumerate ( const struct setting_type *type,
  356. unsigned long value, void *buf, size_t len );
  357. extern int fetchf_setting ( struct settings *settings,
  358. const struct setting *setting,
  359. struct settings **origin, struct setting *fetched,
  360. char *buf, size_t len );
  361. extern int fetchf_setting_copy ( struct settings *settings,
  362. const struct setting *setting,
  363. struct settings **origin,
  364. struct setting *fetched, char **value );
  365. extern int storef_setting ( struct settings *settings,
  366. const struct setting *setting, const char *value );
  367. extern int fetchn_setting ( struct settings *settings,
  368. const struct setting *setting,
  369. struct settings **origin, struct setting *fetched,
  370. unsigned long *value );
  371. extern int storen_setting ( struct settings *settings,
  372. const struct setting *setting,
  373. unsigned long value );
  374. extern char * expand_settings ( const char *string );
  375. extern const struct setting_type setting_type_string __setting_type;
  376. extern const struct setting_type setting_type_uristring __setting_type;
  377. extern const struct setting_type setting_type_ipv4 __setting_type;
  378. extern const struct setting_type setting_type_ipv6 __setting_type;
  379. extern const struct setting_type setting_type_int8 __setting_type;
  380. extern const struct setting_type setting_type_int16 __setting_type;
  381. extern const struct setting_type setting_type_int32 __setting_type;
  382. extern const struct setting_type setting_type_uint8 __setting_type;
  383. extern const struct setting_type setting_type_uint16 __setting_type;
  384. extern const struct setting_type setting_type_uint32 __setting_type;
  385. extern const struct setting_type setting_type_hex __setting_type;
  386. extern const struct setting_type setting_type_hexhyp __setting_type;
  387. extern const struct setting_type setting_type_hexraw __setting_type;
  388. extern const struct setting_type setting_type_uuid __setting_type;
  389. extern const struct setting_type setting_type_busdevfn __setting_type;
  390. extern const struct setting_type setting_type_dnssl __setting_type;
  391. extern const struct setting
  392. ip_setting __setting ( SETTING_IP, ip );
  393. extern const struct setting
  394. netmask_setting __setting ( SETTING_IP, netmask );
  395. extern const struct setting
  396. gateway_setting __setting ( SETTING_IP, gateway );
  397. extern const struct setting
  398. dns_setting __setting ( SETTING_IP_EXTRA, dns );
  399. extern const struct setting
  400. hostname_setting __setting ( SETTING_HOST, hostname );
  401. extern const struct setting
  402. domain_setting __setting ( SETTING_IP_EXTRA, domain );
  403. extern const struct setting
  404. filename_setting __setting ( SETTING_BOOT, filename );
  405. extern const struct setting
  406. root_path_setting __setting ( SETTING_SANBOOT, root-path );
  407. extern const struct setting
  408. username_setting __setting ( SETTING_AUTH, username );
  409. extern const struct setting
  410. password_setting __setting ( SETTING_AUTH, password );
  411. extern const struct setting
  412. priority_setting __setting ( SETTING_MISC, priority );
  413. extern const struct setting
  414. uuid_setting __setting ( SETTING_HOST, uuid );
  415. extern const struct setting
  416. next_server_setting __setting ( SETTING_BOOT, next-server );
  417. extern const struct setting
  418. mac_setting __setting ( SETTING_NETDEV, mac );
  419. extern const struct setting
  420. busid_setting __setting ( SETTING_NETDEV, busid );
  421. extern const struct setting
  422. user_class_setting __setting ( SETTING_HOST_EXTRA, user-class );
  423. /**
  424. * Initialise a settings block
  425. *
  426. * @v settings Settings block
  427. * @v op Settings block operations
  428. * @v refcnt Containing object reference counter, or NULL
  429. * @v default_scope Default scope
  430. */
  431. static inline void settings_init ( struct settings *settings,
  432. struct settings_operations *op,
  433. struct refcnt *refcnt,
  434. const struct settings_scope *default_scope ){
  435. INIT_LIST_HEAD ( &settings->siblings );
  436. INIT_LIST_HEAD ( &settings->children );
  437. settings->op = op;
  438. settings->refcnt = refcnt;
  439. settings->default_scope = default_scope;
  440. }
  441. /**
  442. * Initialise a settings block
  443. *
  444. * @v generics Generic settings block
  445. * @v refcnt Containing object reference counter, or NULL
  446. */
  447. static inline void generic_settings_init ( struct generic_settings *generics,
  448. struct refcnt *refcnt ) {
  449. settings_init ( &generics->settings, &generic_settings_operations,
  450. refcnt, NULL );
  451. INIT_LIST_HEAD ( &generics->list );
  452. }
  453. /**
  454. * Delete setting
  455. *
  456. * @v settings Settings block
  457. * @v setting Setting to delete
  458. * @ret rc Return status code
  459. */
  460. static inline int delete_setting ( struct settings *settings,
  461. const struct setting *setting ) {
  462. return store_setting ( settings, setting, NULL, 0 );
  463. }
  464. /**
  465. * Check existence of predefined setting
  466. *
  467. * @v settings Settings block, or NULL to search all blocks
  468. * @v setting Setting to fetch
  469. * @ret exists Setting exists
  470. */
  471. static inline int setting_exists ( struct settings *settings,
  472. const struct setting *setting ) {
  473. return ( fetch_setting ( settings, setting, NULL, NULL,
  474. NULL, 0 ) >= 0 );
  475. }
  476. #endif /* _IPXE_SETTINGS_H */