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 19KB

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