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.c 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <strings.h>
  24. #include <byteswap.h>
  25. #include <errno.h>
  26. #include <assert.h>
  27. #include <gpxe/in.h>
  28. #include <gpxe/vsprintf.h>
  29. #include <gpxe/dhcp.h>
  30. #include <gpxe/uuid.h>
  31. #include <gpxe/uri.h>
  32. #include <gpxe/settings.h>
  33. /** @file
  34. *
  35. * Configuration settings
  36. *
  37. */
  38. /******************************************************************************
  39. *
  40. * Generic settings blocks
  41. *
  42. ******************************************************************************
  43. */
  44. /**
  45. * A generic setting
  46. *
  47. */
  48. struct generic_setting {
  49. /** List of generic settings */
  50. struct list_head list;
  51. /** Setting */
  52. struct setting setting;
  53. /** Size of setting name */
  54. size_t name_len;
  55. /** Size of setting data */
  56. size_t data_len;
  57. };
  58. /**
  59. * Get generic setting name
  60. *
  61. * @v generic Generic setting
  62. * @ret name Generic setting name
  63. */
  64. static inline void * generic_setting_name ( struct generic_setting *generic ) {
  65. return ( ( ( void * ) generic ) + sizeof ( *generic ) );
  66. }
  67. /**
  68. * Get generic setting data
  69. *
  70. * @v generic Generic setting
  71. * @ret data Generic setting data
  72. */
  73. static inline void * generic_setting_data ( struct generic_setting *generic ) {
  74. return ( ( ( void * ) generic ) + sizeof ( *generic ) +
  75. generic->name_len );
  76. }
  77. /**
  78. * Find generic setting
  79. *
  80. * @v generics Generic settings block
  81. * @v setting Setting to find
  82. * @ret generic Generic setting, or NULL
  83. */
  84. static struct generic_setting *
  85. find_generic_setting ( struct generic_settings *generics,
  86. struct setting *setting ) {
  87. struct generic_setting *generic;
  88. list_for_each_entry ( generic, &generics->list, list ) {
  89. if ( setting_cmp ( &generic->setting, setting ) == 0 )
  90. return generic;
  91. }
  92. return NULL;
  93. }
  94. /**
  95. * Store value of generic setting
  96. *
  97. * @v settings Settings block
  98. * @v setting Setting to store
  99. * @v data Setting data, or NULL to clear setting
  100. * @v len Length of setting data
  101. * @ret rc Return status code
  102. */
  103. int generic_settings_store ( struct settings *settings,
  104. struct setting *setting,
  105. const void *data, size_t len ) {
  106. struct generic_settings *generics =
  107. container_of ( settings, struct generic_settings, settings );
  108. struct generic_setting *old;
  109. struct generic_setting *new = NULL;
  110. size_t name_len;
  111. /* Identify existing generic setting, if any */
  112. old = find_generic_setting ( generics, setting );
  113. /* Create new generic setting, if required */
  114. if ( len ) {
  115. /* Allocate new generic setting */
  116. name_len = ( strlen ( setting->name ) + 1 );
  117. new = zalloc ( sizeof ( *new ) + name_len + len );
  118. if ( ! new )
  119. return -ENOMEM;
  120. /* Populate new generic setting */
  121. new->name_len = name_len;
  122. new->data_len = len;
  123. memcpy ( &new->setting, setting, sizeof ( new->setting ) );
  124. new->setting.name = generic_setting_name ( new );
  125. memcpy ( generic_setting_name ( new ),
  126. setting->name, name_len );
  127. memcpy ( generic_setting_data ( new ), data, len );
  128. }
  129. /* Delete existing generic setting, if any */
  130. if ( old ) {
  131. list_del ( &old->list );
  132. free ( old );
  133. }
  134. /* Add new setting to list, if any */
  135. if ( new )
  136. list_add ( &new->list, &generics->list );
  137. return 0;
  138. }
  139. /**
  140. * Fetch value of generic setting
  141. *
  142. * @v settings Settings block
  143. * @v setting Setting to fetch
  144. * @v data Buffer to fill with setting data
  145. * @v len Length of buffer
  146. * @ret len Length of setting data, or negative error
  147. */
  148. int generic_settings_fetch ( struct settings *settings,
  149. struct setting *setting,
  150. void *data, size_t len ) {
  151. struct generic_settings *generics =
  152. container_of ( settings, struct generic_settings, settings );
  153. struct generic_setting *generic;
  154. /* Find generic setting */
  155. generic = find_generic_setting ( generics, setting );
  156. if ( ! generic )
  157. return -ENOENT;
  158. /* Copy out generic setting data */
  159. if ( len > generic->data_len )
  160. len = generic->data_len;
  161. memcpy ( data, generic_setting_data ( generic ), len );
  162. return generic->data_len;
  163. }
  164. /**
  165. * Clear generic settings block
  166. *
  167. * @v settings Settings block
  168. */
  169. void generic_settings_clear ( struct settings *settings ) {
  170. struct generic_settings *generics =
  171. container_of ( settings, struct generic_settings, settings );
  172. struct generic_setting *generic;
  173. struct generic_setting *tmp;
  174. list_for_each_entry_safe ( generic, tmp, &generics->list, list ) {
  175. list_del ( &generic->list );
  176. free ( generic );
  177. }
  178. assert ( list_empty ( &generics->list ) );
  179. }
  180. /** Generic settings operations */
  181. struct settings_operations generic_settings_operations = {
  182. .store = generic_settings_store,
  183. .fetch = generic_settings_fetch,
  184. .clear = generic_settings_clear,
  185. };
  186. /******************************************************************************
  187. *
  188. * Registered settings blocks
  189. *
  190. ******************************************************************************
  191. */
  192. /** Root generic settings block */
  193. struct generic_settings generic_settings_root = {
  194. .settings = {
  195. .refcnt = NULL,
  196. .name = "",
  197. .siblings =
  198. LIST_HEAD_INIT ( generic_settings_root.settings.siblings ),
  199. .children =
  200. LIST_HEAD_INIT ( generic_settings_root.settings.children ),
  201. .op = &generic_settings_operations,
  202. },
  203. .list = LIST_HEAD_INIT ( generic_settings_root.list ),
  204. };
  205. /** Root settings block */
  206. #define settings_root generic_settings_root.settings
  207. /**
  208. * Find child named settings block
  209. *
  210. * @v parent Parent settings block
  211. * @v name Name within this parent
  212. * @ret settings Settings block, or NULL
  213. */
  214. static struct settings * find_child_settings ( struct settings *parent,
  215. const char *name ) {
  216. struct settings *settings;
  217. /* Treat empty name as meaning "this block" */
  218. if ( ! *name )
  219. return parent;
  220. /* Look for child with matching name */
  221. list_for_each_entry ( settings, &parent->children, siblings ) {
  222. if ( strcmp ( settings->name, name ) == 0 )
  223. return settings;
  224. }
  225. return NULL;
  226. }
  227. /**
  228. * Find or create child named settings block
  229. *
  230. * @v parent Parent settings block
  231. * @v name Name within this parent
  232. * @ret settings Settings block, or NULL
  233. */
  234. static struct settings * autovivify_child_settings ( struct settings *parent,
  235. const char *name ) {
  236. struct {
  237. struct generic_settings generic;
  238. char name[ strlen ( name ) + 1 /* NUL */ ];
  239. } *new_child;
  240. struct settings *settings;
  241. /* Return existing settings, if existent */
  242. if ( ( settings = find_child_settings ( parent, name ) ) != NULL )
  243. return settings;
  244. /* Create new generic settings block */
  245. new_child = zalloc ( sizeof ( *new_child ) );
  246. if ( ! new_child ) {
  247. DBGC ( parent, "Settings %p could not create child %s\n",
  248. parent, name );
  249. return NULL;
  250. }
  251. memcpy ( new_child->name, name, sizeof ( new_child->name ) );
  252. generic_settings_init ( &new_child->generic, NULL, new_child->name );
  253. settings = &new_child->generic.settings;
  254. register_settings ( settings, parent );
  255. return settings;
  256. }
  257. /**
  258. * Return settings block name (for debug only)
  259. *
  260. * @v settings Settings block
  261. * @ret name Settings block name
  262. */
  263. static const char * settings_name ( struct settings *settings ) {
  264. static char buf[64];
  265. char tmp[ sizeof ( buf ) ];
  266. int count;
  267. for ( count = 0 ; settings ; settings = settings->parent ) {
  268. memcpy ( tmp, buf, sizeof ( tmp ) );
  269. snprintf ( buf, sizeof ( buf ), "%s%c%s", settings->name,
  270. ( count++ ? '.' : '\0' ), tmp );
  271. }
  272. return ( buf + 1 );
  273. }
  274. /**
  275. * Parse settings block name
  276. *
  277. * @v name Name
  278. * @v get_child Function to find or create child settings block
  279. * @ret settings Settings block, or NULL
  280. */
  281. static struct settings *
  282. parse_settings_name ( const char *name,
  283. struct settings * ( * get_child ) ( struct settings *,
  284. const char * ) ) {
  285. struct settings *settings = &settings_root;
  286. char name_copy[ strlen ( name ) + 1 ];
  287. char *subname;
  288. char *remainder;
  289. /* Create modifiable copy of name */
  290. memcpy ( name_copy, name, sizeof ( name_copy ) );
  291. remainder = name_copy;
  292. /* Parse each name component in turn */
  293. while ( remainder ) {
  294. subname = remainder;
  295. remainder = strchr ( subname, '.' );
  296. if ( remainder )
  297. *(remainder++) = '\0';
  298. settings = get_child ( settings, subname );
  299. if ( ! settings )
  300. break;
  301. }
  302. return settings;
  303. }
  304. /**
  305. * Find named settings block
  306. *
  307. * @v name Name
  308. * @ret settings Settings block, or NULL
  309. */
  310. struct settings * find_settings ( const char *name ) {
  311. return parse_settings_name ( name, find_child_settings );
  312. }
  313. /**
  314. * Apply all settings
  315. *
  316. * @ret rc Return status code
  317. */
  318. static int apply_settings ( void ) {
  319. struct settings_applicator *applicator;
  320. int rc;
  321. /* Call all settings applicators */
  322. for_each_table_entry ( applicator, SETTINGS_APPLICATORS ) {
  323. if ( ( rc = applicator->apply() ) != 0 ) {
  324. DBG ( "Could not apply settings using applicator "
  325. "%p: %s\n", applicator, strerror ( rc ) );
  326. return rc;
  327. }
  328. }
  329. return 0;
  330. }
  331. /**
  332. * Reprioritise settings
  333. *
  334. * @v settings Settings block
  335. *
  336. * Reorders the settings block amongst its siblings according to its
  337. * priority.
  338. */
  339. static void reprioritise_settings ( struct settings *settings ) {
  340. struct settings *parent = settings->parent;
  341. long priority;
  342. struct settings *tmp;
  343. long tmp_priority;
  344. /* Stop when we reach the top of the tree */
  345. if ( ! parent )
  346. return;
  347. /* Read priority, if present */
  348. priority = fetch_intz_setting ( settings, &priority_setting );
  349. /* Remove from siblings list */
  350. list_del ( &settings->siblings );
  351. /* Reinsert after any existing blocks which have a higher priority */
  352. list_for_each_entry ( tmp, &parent->children, siblings ) {
  353. tmp_priority = fetch_intz_setting ( tmp, &priority_setting );
  354. if ( priority > tmp_priority )
  355. break;
  356. }
  357. list_add_tail ( &settings->siblings, &tmp->siblings );
  358. /* Recurse up the tree */
  359. reprioritise_settings ( parent );
  360. }
  361. /**
  362. * Register settings block
  363. *
  364. * @v settings Settings block
  365. * @v parent Parent settings block, or NULL
  366. * @ret rc Return status code
  367. */
  368. int register_settings ( struct settings *settings, struct settings *parent ) {
  369. struct settings *old_settings;
  370. /* NULL parent => add to settings root */
  371. assert ( settings != NULL );
  372. if ( parent == NULL )
  373. parent = &settings_root;
  374. /* Remove any existing settings with the same name */
  375. if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
  376. unregister_settings ( old_settings );
  377. /* Add to list of settings */
  378. ref_get ( settings->refcnt );
  379. ref_get ( parent->refcnt );
  380. settings->parent = parent;
  381. list_add_tail ( &settings->siblings, &parent->children );
  382. DBGC ( settings, "Settings %p (\"%s\") registered\n",
  383. settings, settings_name ( settings ) );
  384. /* Fix up settings priority */
  385. reprioritise_settings ( settings );
  386. /* Apply potentially-updated settings */
  387. apply_settings();
  388. return 0;
  389. }
  390. /**
  391. * Unregister settings block
  392. *
  393. * @v settings Settings block
  394. */
  395. void unregister_settings ( struct settings *settings ) {
  396. DBGC ( settings, "Settings %p (\"%s\") unregistered\n",
  397. settings, settings_name ( settings ) );
  398. /* Remove from list of settings */
  399. ref_put ( settings->refcnt );
  400. ref_put ( settings->parent->refcnt );
  401. settings->parent = NULL;
  402. list_del ( &settings->siblings );
  403. /* Apply potentially-updated settings */
  404. apply_settings();
  405. }
  406. /******************************************************************************
  407. *
  408. * Core settings routines
  409. *
  410. ******************************************************************************
  411. */
  412. /**
  413. * Store value of setting
  414. *
  415. * @v settings Settings block, or NULL
  416. * @v setting Setting to store
  417. * @v data Setting data, or NULL to clear setting
  418. * @v len Length of setting data
  419. * @ret rc Return status code
  420. */
  421. int store_setting ( struct settings *settings, struct setting *setting,
  422. const void *data, size_t len ) {
  423. int rc;
  424. /* NULL settings implies storing into the global settings root */
  425. if ( ! settings )
  426. settings = &settings_root;
  427. /* Sanity check */
  428. if ( ! settings->op->store )
  429. return -ENOTSUP;
  430. /* Store setting */
  431. if ( ( rc = settings->op->store ( settings, setting,
  432. data, len ) ) != 0 )
  433. return rc;
  434. /* Reprioritise settings if necessary */
  435. if ( setting_cmp ( setting, &priority_setting ) == 0 )
  436. reprioritise_settings ( settings );
  437. /* If these settings are registered, apply potentially-updated
  438. * settings
  439. */
  440. for ( ; settings ; settings = settings->parent ) {
  441. if ( settings == &settings_root ) {
  442. if ( ( rc = apply_settings() ) != 0 )
  443. return rc;
  444. break;
  445. }
  446. }
  447. return 0;
  448. }
  449. /**
  450. * Fetch value of setting
  451. *
  452. * @v settings Settings block, or NULL to search all blocks
  453. * @v setting Setting to fetch
  454. * @v data Buffer to fill with setting data
  455. * @v len Length of buffer
  456. * @ret len Length of setting data, or negative error
  457. *
  458. * The actual length of the setting will be returned even if
  459. * the buffer was too small.
  460. */
  461. int fetch_setting ( struct settings *settings, struct setting *setting,
  462. void *data, size_t len ) {
  463. struct settings *child;
  464. int ret;
  465. /* Avoid returning uninitialised data on error */
  466. memset ( data, 0, len );
  467. /* NULL settings implies starting at the global settings root */
  468. if ( ! settings )
  469. settings = &settings_root;
  470. /* Sanity check */
  471. if ( ! settings->op->fetch )
  472. return -ENOTSUP;
  473. /* Try this block first */
  474. if ( ( ret = settings->op->fetch ( settings, setting,
  475. data, len ) ) >= 0 )
  476. return ret;
  477. /* Recurse into each child block in turn */
  478. list_for_each_entry ( child, &settings->children, siblings ) {
  479. if ( ( ret = fetch_setting ( child, setting,
  480. data, len ) ) >= 0 )
  481. return ret;
  482. }
  483. return -ENOENT;
  484. }
  485. /**
  486. * Fetch length of setting
  487. *
  488. * @v settings Settings block, or NULL to search all blocks
  489. * @v setting Setting to fetch
  490. * @ret len Length of setting data, or negative error
  491. *
  492. * This function can also be used as an existence check for the
  493. * setting.
  494. */
  495. int fetch_setting_len ( struct settings *settings, struct setting *setting ) {
  496. return fetch_setting ( settings, setting, NULL, 0 );
  497. }
  498. /**
  499. * Fetch value of string setting
  500. *
  501. * @v settings Settings block, or NULL to search all blocks
  502. * @v setting Setting to fetch
  503. * @v data Buffer to fill with setting string data
  504. * @v len Length of buffer
  505. * @ret len Length of string setting, or negative error
  506. *
  507. * The resulting string is guaranteed to be correctly NUL-terminated.
  508. * The returned length will be the length of the underlying setting
  509. * data.
  510. */
  511. int fetch_string_setting ( struct settings *settings, struct setting *setting,
  512. char *data, size_t len ) {
  513. memset ( data, 0, len );
  514. return fetch_setting ( settings, setting, data,
  515. ( ( len > 0 ) ? ( len - 1 ) : 0 ) );
  516. }
  517. /**
  518. * Fetch value of string setting
  519. *
  520. * @v settings Settings block, or NULL to search all blocks
  521. * @v setting Setting to fetch
  522. * @v data Buffer to allocate and fill with setting string data
  523. * @ret len Length of string setting, or negative error
  524. *
  525. * The resulting string is guaranteed to be correctly NUL-terminated.
  526. * The returned length will be the length of the underlying setting
  527. * data. The caller is responsible for eventually freeing the
  528. * allocated buffer.
  529. */
  530. int fetch_string_setting_copy ( struct settings *settings,
  531. struct setting *setting,
  532. char **data ) {
  533. int len;
  534. int check_len = 0;
  535. len = fetch_setting_len ( settings, setting );
  536. if ( len < 0 )
  537. return len;
  538. *data = malloc ( len + 1 );
  539. if ( ! *data )
  540. return -ENOMEM;
  541. check_len = fetch_string_setting ( settings, setting, *data,
  542. ( len + 1 ) );
  543. assert ( check_len == len );
  544. return len;
  545. }
  546. /**
  547. * Fetch value of IPv4 address setting
  548. *
  549. * @v settings Settings block, or NULL to search all blocks
  550. * @v setting Setting to fetch
  551. * @v inp IPv4 address to fill in
  552. * @ret len Length of setting, or negative error
  553. */
  554. int fetch_ipv4_setting ( struct settings *settings, struct setting *setting,
  555. struct in_addr *inp ) {
  556. int len;
  557. len = fetch_setting ( settings, setting, inp, sizeof ( *inp ) );
  558. if ( len < 0 )
  559. return len;
  560. if ( len < ( int ) sizeof ( *inp ) )
  561. return -ERANGE;
  562. return len;
  563. }
  564. /**
  565. * Fetch value of signed integer setting
  566. *
  567. * @v settings Settings block, or NULL to search all blocks
  568. * @v setting Setting to fetch
  569. * @v value Integer value to fill in
  570. * @ret len Length of setting, or negative error
  571. */
  572. int fetch_int_setting ( struct settings *settings, struct setting *setting,
  573. long *value ) {
  574. union {
  575. uint8_t u8[ sizeof ( long ) ];
  576. int8_t s8[ sizeof ( long ) ];
  577. } buf;
  578. int len;
  579. int i;
  580. /* Avoid returning uninitialised data on error */
  581. *value = 0;
  582. /* Fetch raw (network-ordered, variable-length) setting */
  583. len = fetch_setting ( settings, setting, &buf, sizeof ( buf ) );
  584. if ( len < 0 )
  585. return len;
  586. if ( len > ( int ) sizeof ( buf ) )
  587. return -ERANGE;
  588. /* Convert to host-ordered signed long */
  589. *value = ( ( buf.s8[0] >= 0 ) ? 0 : -1L );
  590. for ( i = 0 ; i < len ; i++ ) {
  591. *value = ( ( *value << 8 ) | buf.u8[i] );
  592. }
  593. return len;
  594. }
  595. /**
  596. * Fetch value of unsigned integer setting
  597. *
  598. * @v settings Settings block, or NULL to search all blocks
  599. * @v setting Setting to fetch
  600. * @v value Integer value to fill in
  601. * @ret len Length of setting, or negative error
  602. */
  603. int fetch_uint_setting ( struct settings *settings, struct setting *setting,
  604. unsigned long *value ) {
  605. long svalue;
  606. int len;
  607. /* Avoid returning uninitialised data on error */
  608. *value = 0;
  609. /* Fetch as a signed long */
  610. len = fetch_int_setting ( settings, setting, &svalue );
  611. if ( len < 0 )
  612. return len;
  613. /* Mask off sign-extended bits */
  614. assert ( len <= ( int ) sizeof ( long ) );
  615. *value = ( svalue & ( -1UL >> ( 8 * ( sizeof ( long ) - len ) ) ) );
  616. return len;
  617. }
  618. /**
  619. * Fetch value of signed integer setting, or zero
  620. *
  621. * @v settings Settings block, or NULL to search all blocks
  622. * @v setting Setting to fetch
  623. * @ret value Setting value, or zero
  624. */
  625. long fetch_intz_setting ( struct settings *settings, struct setting *setting ){
  626. long value;
  627. fetch_int_setting ( settings, setting, &value );
  628. return value;
  629. }
  630. /**
  631. * Fetch value of unsigned integer setting, or zero
  632. *
  633. * @v settings Settings block, or NULL to search all blocks
  634. * @v setting Setting to fetch
  635. * @ret value Setting value, or zero
  636. */
  637. unsigned long fetch_uintz_setting ( struct settings *settings,
  638. struct setting *setting ) {
  639. unsigned long value;
  640. fetch_uint_setting ( settings, setting, &value );
  641. return value;
  642. }
  643. /**
  644. * Fetch value of UUID setting
  645. *
  646. * @v settings Settings block, or NULL to search all blocks
  647. * @v setting Setting to fetch
  648. * @v uuid UUID to fill in
  649. * @ret len Length of setting, or negative error
  650. */
  651. int fetch_uuid_setting ( struct settings *settings, struct setting *setting,
  652. union uuid *uuid ) {
  653. int len;
  654. len = fetch_setting ( settings, setting, uuid, sizeof ( *uuid ) );
  655. if ( len < 0 )
  656. return len;
  657. if ( len != sizeof ( *uuid ) )
  658. return -ERANGE;
  659. return len;
  660. }
  661. /**
  662. * Clear settings block
  663. *
  664. * @v settings Settings block
  665. */
  666. void clear_settings ( struct settings *settings ) {
  667. if ( settings->op->clear )
  668. settings->op->clear ( settings );
  669. }
  670. /**
  671. * Compare two settings
  672. *
  673. * @v a Setting to compare
  674. * @v b Setting to compare
  675. * @ret 0 Settings are the same
  676. * @ret non-zero Settings are not the same
  677. */
  678. int setting_cmp ( struct setting *a, struct setting *b ) {
  679. /* If the settings have tags, compare them */
  680. if ( a->tag && ( a->tag == b->tag ) )
  681. return 0;
  682. /* Otherwise, if the settings have names, compare them */
  683. if ( a->name && b->name && a->name[0] )
  684. return strcmp ( a->name, b->name );
  685. /* Otherwise, return a non-match */
  686. return ( ! 0 );
  687. }
  688. /******************************************************************************
  689. *
  690. * Formatted setting routines
  691. *
  692. ******************************************************************************
  693. */
  694. /**
  695. * Store value of typed setting
  696. *
  697. * @v settings Settings block
  698. * @v setting Setting to store
  699. * @v type Settings type
  700. * @v value Formatted setting data, or NULL
  701. * @ret rc Return status code
  702. */
  703. int storef_setting ( struct settings *settings, struct setting *setting,
  704. const char *value ) {
  705. /* NULL value implies deletion. Avoid imposing the burden of
  706. * checking for NULL values on each typed setting's storef()
  707. * method.
  708. */
  709. if ( ! value )
  710. return delete_setting ( settings, setting );
  711. return setting->type->storef ( settings, setting, value );
  712. }
  713. /**
  714. * Find named setting
  715. *
  716. * @v name Name
  717. * @ret setting Named setting, or NULL
  718. */
  719. static struct setting * find_setting ( const char *name ) {
  720. struct setting *setting;
  721. for_each_table_entry ( setting, SETTINGS ) {
  722. if ( strcmp ( name, setting->name ) == 0 )
  723. return setting;
  724. }
  725. return NULL;
  726. }
  727. /**
  728. * Parse setting name as tag number
  729. *
  730. * @v name Name
  731. * @ret tag Tag number, or 0 if not a valid number
  732. */
  733. static unsigned int parse_setting_tag ( const char *name ) {
  734. char *tmp = ( ( char * ) name );
  735. unsigned int tag = 0;
  736. while ( 1 ) {
  737. tag = ( ( tag << 8 ) | strtoul ( tmp, &tmp, 0 ) );
  738. if ( *tmp == 0 )
  739. return tag;
  740. if ( *tmp != '.' )
  741. return 0;
  742. tmp++;
  743. }
  744. }
  745. /**
  746. * Find setting type
  747. *
  748. * @v name Name
  749. * @ret type Setting type, or NULL
  750. */
  751. static struct setting_type * find_setting_type ( const char *name ) {
  752. struct setting_type *type;
  753. for_each_table_entry ( type, SETTING_TYPES ) {
  754. if ( strcmp ( name, type->name ) == 0 )
  755. return type;
  756. }
  757. return NULL;
  758. }
  759. /**
  760. * Parse setting name
  761. *
  762. * @v name Name of setting
  763. * @v get_child Function to find or create child settings block
  764. * @v settings Settings block to fill in
  765. * @v setting Setting to fill in
  766. * @v tmp_name Buffer for copy of setting name
  767. * @ret rc Return status code
  768. *
  769. * Interprets a name of the form
  770. * "[settings_name/]tag_name[:type_name]" and fills in the appropriate
  771. * fields.
  772. *
  773. * The @c tmp_name buffer must be large enough to hold a copy of the
  774. * setting name.
  775. */
  776. static int
  777. parse_setting_name ( const char *name,
  778. struct settings * ( * get_child ) ( struct settings *,
  779. const char * ),
  780. struct settings **settings, struct setting *setting,
  781. char *tmp_name ) {
  782. char *settings_name;
  783. char *setting_name;
  784. char *type_name;
  785. struct setting *named_setting;
  786. /* Set defaults */
  787. *settings = &settings_root;
  788. memset ( setting, 0, sizeof ( *setting ) );
  789. setting->name = "";
  790. setting->type = &setting_type_string;
  791. /* Split name into "[settings_name/]setting_name[:type_name]" */
  792. strcpy ( tmp_name, name );
  793. if ( ( setting_name = strchr ( tmp_name, '/' ) ) != NULL ) {
  794. *(setting_name++) = 0;
  795. settings_name = tmp_name;
  796. } else {
  797. setting_name = tmp_name;
  798. settings_name = NULL;
  799. }
  800. if ( ( type_name = strchr ( setting_name, ':' ) ) != NULL )
  801. *(type_name++) = 0;
  802. /* Identify settings block, if specified */
  803. if ( settings_name ) {
  804. *settings = parse_settings_name ( settings_name, get_child );
  805. if ( *settings == NULL ) {
  806. DBG ( "Unrecognised settings block \"%s\" in \"%s\"\n",
  807. settings_name, name );
  808. return -ENODEV;
  809. }
  810. }
  811. /* Identify setting */
  812. if ( ( named_setting = find_setting ( setting_name ) ) != NULL ) {
  813. /* Matches a defined named setting; use that setting */
  814. memcpy ( setting, named_setting, sizeof ( *setting ) );
  815. } else if ( ( setting->tag = parse_setting_tag ( setting_name ) ) !=0){
  816. /* Is a valid numeric tag; use the tag */
  817. setting->tag |= (*settings)->tag_magic;
  818. } else {
  819. /* Use the arbitrary name */
  820. setting->name = setting_name;
  821. }
  822. /* Identify setting type, if specified */
  823. if ( type_name ) {
  824. setting->type = find_setting_type ( type_name );
  825. if ( setting->type == NULL ) {
  826. DBG ( "Invalid setting type \"%s\" in \"%s\"\n",
  827. type_name, name );
  828. return -ENOTSUP;
  829. }
  830. }
  831. return 0;
  832. }
  833. /**
  834. * Parse and store value of named setting
  835. *
  836. * @v name Name of setting
  837. * @v value Formatted setting data, or NULL
  838. * @ret rc Return status code
  839. */
  840. int storef_named_setting ( const char *name, const char *value ) {
  841. struct settings *settings;
  842. struct setting setting;
  843. char tmp_name[ strlen ( name ) + 1 ];
  844. int rc;
  845. if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
  846. &settings, &setting, tmp_name )) != 0)
  847. return rc;
  848. return storef_setting ( settings, &setting, value );
  849. }
  850. /**
  851. * Fetch and format value of named setting
  852. *
  853. * @v name Name of setting
  854. * @v buf Buffer to contain formatted value
  855. * @v len Length of buffer
  856. * @ret len Length of formatted value, or negative error
  857. */
  858. int fetchf_named_setting ( const char *name, char *buf, size_t len ) {
  859. struct settings *settings;
  860. struct setting setting;
  861. char tmp_name[ strlen ( name ) + 1 ];
  862. int rc;
  863. if ( ( rc = parse_setting_name ( name, find_child_settings,
  864. &settings, &setting, tmp_name )) != 0)
  865. return rc;
  866. return fetchf_setting ( settings, &setting, buf, len );
  867. }
  868. /******************************************************************************
  869. *
  870. * Setting types
  871. *
  872. ******************************************************************************
  873. */
  874. /**
  875. * Parse and store value of string setting
  876. *
  877. * @v settings Settings block
  878. * @v setting Setting to store
  879. * @v value Formatted setting data
  880. * @ret rc Return status code
  881. */
  882. static int storef_string ( struct settings *settings, struct setting *setting,
  883. const char *value ) {
  884. return store_setting ( settings, setting, value, strlen ( value ) );
  885. }
  886. /**
  887. * Fetch and format value of string setting
  888. *
  889. * @v settings Settings block, or NULL to search all blocks
  890. * @v setting Setting to fetch
  891. * @v buf Buffer to contain formatted value
  892. * @v len Length of buffer
  893. * @ret len Length of formatted value, or negative error
  894. */
  895. static int fetchf_string ( struct settings *settings, struct setting *setting,
  896. char *buf, size_t len ) {
  897. return fetch_string_setting ( settings, setting, buf, len );
  898. }
  899. /** A string setting type */
  900. struct setting_type setting_type_string __setting_type = {
  901. .name = "string",
  902. .storef = storef_string,
  903. .fetchf = fetchf_string,
  904. };
  905. /**
  906. * Parse and store value of URI-encoded string setting
  907. *
  908. * @v settings Settings block
  909. * @v setting Setting to store
  910. * @v value Formatted setting data
  911. * @ret rc Return status code
  912. */
  913. static int storef_uristring ( struct settings *settings,
  914. struct setting *setting,
  915. const char *value ) {
  916. char buf[ strlen ( value ) + 1 ]; /* Decoding never expands string */
  917. size_t len;
  918. len = uri_decode ( value, buf, sizeof ( buf ) );
  919. return store_setting ( settings, setting, buf, len );
  920. }
  921. /**
  922. * Fetch and format value of URI-encoded string setting
  923. *
  924. * @v settings Settings block, or NULL to search all blocks
  925. * @v setting Setting to fetch
  926. * @v buf Buffer to contain formatted value
  927. * @v len Length of buffer
  928. * @ret len Length of formatted value, or negative error
  929. */
  930. static int fetchf_uristring ( struct settings *settings,
  931. struct setting *setting,
  932. char *buf, size_t len ) {
  933. ssize_t raw_len;
  934. /* We need to always retrieve the full raw string to know the
  935. * length of the encoded string.
  936. */
  937. raw_len = fetch_setting ( settings, setting, NULL, 0 );
  938. if ( raw_len < 0 )
  939. return raw_len;
  940. {
  941. char raw_buf[ raw_len + 1 ];
  942. fetch_string_setting ( settings, setting, raw_buf,
  943. sizeof ( raw_buf ) );
  944. return uri_encode ( raw_buf, buf, len );
  945. }
  946. }
  947. /** A URI-encoded string setting type */
  948. struct setting_type setting_type_uristring __setting_type = {
  949. .name = "uristring",
  950. .storef = storef_uristring,
  951. .fetchf = fetchf_uristring,
  952. };
  953. /**
  954. * Parse and store value of IPv4 address setting
  955. *
  956. * @v settings Settings block
  957. * @v setting Setting to store
  958. * @v value Formatted setting data
  959. * @ret rc Return status code
  960. */
  961. static int storef_ipv4 ( struct settings *settings, struct setting *setting,
  962. const char *value ) {
  963. struct in_addr ipv4;
  964. if ( inet_aton ( value, &ipv4 ) == 0 )
  965. return -EINVAL;
  966. return store_setting ( settings, setting, &ipv4, sizeof ( ipv4 ) );
  967. }
  968. /**
  969. * Fetch and format value of IPv4 address setting
  970. *
  971. * @v settings Settings block, or NULL to search all blocks
  972. * @v setting Setting to fetch
  973. * @v buf Buffer to contain formatted value
  974. * @v len Length of buffer
  975. * @ret len Length of formatted value, or negative error
  976. */
  977. static int fetchf_ipv4 ( struct settings *settings, struct setting *setting,
  978. char *buf, size_t len ) {
  979. struct in_addr ipv4;
  980. int raw_len;
  981. if ( ( raw_len = fetch_ipv4_setting ( settings, setting, &ipv4 ) ) < 0)
  982. return raw_len;
  983. return snprintf ( buf, len, "%s", inet_ntoa ( ipv4 ) );
  984. }
  985. /** An IPv4 address setting type */
  986. struct setting_type setting_type_ipv4 __setting_type = {
  987. .name = "ipv4",
  988. .storef = storef_ipv4,
  989. .fetchf = fetchf_ipv4,
  990. };
  991. /**
  992. * Parse and store value of integer setting
  993. *
  994. * @v settings Settings block
  995. * @v setting Setting to store
  996. * @v value Formatted setting data
  997. * @v size Integer size, in bytes
  998. * @ret rc Return status code
  999. */
  1000. static int storef_int ( struct settings *settings, struct setting *setting,
  1001. const char *value, unsigned int size ) {
  1002. union {
  1003. uint32_t num;
  1004. uint8_t bytes[4];
  1005. } u;
  1006. char *endp;
  1007. u.num = htonl ( strtoul ( value, &endp, 0 ) );
  1008. if ( *endp )
  1009. return -EINVAL;
  1010. return store_setting ( settings, setting,
  1011. &u.bytes[ sizeof ( u ) - size ], size );
  1012. }
  1013. /**
  1014. * Parse and store value of 8-bit integer setting
  1015. *
  1016. * @v settings Settings block
  1017. * @v setting Setting to store
  1018. * @v value Formatted setting data
  1019. * @v size Integer size, in bytes
  1020. * @ret rc Return status code
  1021. */
  1022. static int storef_int8 ( struct settings *settings, struct setting *setting,
  1023. const char *value ) {
  1024. return storef_int ( settings, setting, value, 1 );
  1025. }
  1026. /**
  1027. * Parse and store value of 16-bit integer setting
  1028. *
  1029. * @v settings Settings block
  1030. * @v setting Setting to store
  1031. * @v value Formatted setting data
  1032. * @v size Integer size, in bytes
  1033. * @ret rc Return status code
  1034. */
  1035. static int storef_int16 ( struct settings *settings, struct setting *setting,
  1036. const char *value ) {
  1037. return storef_int ( settings, setting, value, 2 );
  1038. }
  1039. /**
  1040. * Parse and store value of 32-bit integer setting
  1041. *
  1042. * @v settings Settings block
  1043. * @v setting Setting to store
  1044. * @v value Formatted setting data
  1045. * @v size Integer size, in bytes
  1046. * @ret rc Return status code
  1047. */
  1048. static int storef_int32 ( struct settings *settings, struct setting *setting,
  1049. const char *value ) {
  1050. return storef_int ( settings, setting, value, 4 );
  1051. }
  1052. /**
  1053. * Fetch and format value of signed integer setting
  1054. *
  1055. * @v settings Settings block, or NULL to search all blocks
  1056. * @v setting Setting to fetch
  1057. * @v buf Buffer to contain formatted value
  1058. * @v len Length of buffer
  1059. * @ret len Length of formatted value, or negative error
  1060. */
  1061. static int fetchf_int ( struct settings *settings, struct setting *setting,
  1062. char *buf, size_t len ) {
  1063. long value;
  1064. int rc;
  1065. if ( ( rc = fetch_int_setting ( settings, setting, &value ) ) < 0 )
  1066. return rc;
  1067. return snprintf ( buf, len, "%ld", value );
  1068. }
  1069. /**
  1070. * Fetch and format value of unsigned integer setting
  1071. *
  1072. * @v settings Settings block, or NULL to search all blocks
  1073. * @v setting Setting to fetch
  1074. * @v buf Buffer to contain formatted value
  1075. * @v len Length of buffer
  1076. * @ret len Length of formatted value, or negative error
  1077. */
  1078. static int fetchf_uint ( struct settings *settings, struct setting *setting,
  1079. char *buf, size_t len ) {
  1080. unsigned long value;
  1081. int rc;
  1082. if ( ( rc = fetch_uint_setting ( settings, setting, &value ) ) < 0 )
  1083. return rc;
  1084. return snprintf ( buf, len, "%#lx", value );
  1085. }
  1086. /** A signed 8-bit integer setting type */
  1087. struct setting_type setting_type_int8 __setting_type = {
  1088. .name = "int8",
  1089. .storef = storef_int8,
  1090. .fetchf = fetchf_int,
  1091. };
  1092. /** A signed 16-bit integer setting type */
  1093. struct setting_type setting_type_int16 __setting_type = {
  1094. .name = "int16",
  1095. .storef = storef_int16,
  1096. .fetchf = fetchf_int,
  1097. };
  1098. /** A signed 32-bit integer setting type */
  1099. struct setting_type setting_type_int32 __setting_type = {
  1100. .name = "int32",
  1101. .storef = storef_int32,
  1102. .fetchf = fetchf_int,
  1103. };
  1104. /** An unsigned 8-bit integer setting type */
  1105. struct setting_type setting_type_uint8 __setting_type = {
  1106. .name = "uint8",
  1107. .storef = storef_int8,
  1108. .fetchf = fetchf_uint,
  1109. };
  1110. /** An unsigned 16-bit integer setting type */
  1111. struct setting_type setting_type_uint16 __setting_type = {
  1112. .name = "uint16",
  1113. .storef = storef_int16,
  1114. .fetchf = fetchf_uint,
  1115. };
  1116. /** An unsigned 32-bit integer setting type */
  1117. struct setting_type setting_type_uint32 __setting_type = {
  1118. .name = "uint32",
  1119. .storef = storef_int32,
  1120. .fetchf = fetchf_uint,
  1121. };
  1122. /**
  1123. * Parse and store value of hex string setting
  1124. *
  1125. * @v settings Settings block
  1126. * @v setting Setting to store
  1127. * @v value Formatted setting data
  1128. * @ret rc Return status code
  1129. */
  1130. static int storef_hex ( struct settings *settings, struct setting *setting,
  1131. const char *value ) {
  1132. char *ptr = ( char * ) value;
  1133. uint8_t bytes[ strlen ( value ) ]; /* cannot exceed strlen(value) */
  1134. unsigned int len = 0;
  1135. while ( 1 ) {
  1136. bytes[len++] = strtoul ( ptr, &ptr, 16 );
  1137. switch ( *ptr ) {
  1138. case '\0' :
  1139. return store_setting ( settings, setting, bytes, len );
  1140. case ':' :
  1141. ptr++;
  1142. break;
  1143. default :
  1144. return -EINVAL;
  1145. }
  1146. }
  1147. }
  1148. /**
  1149. * Fetch and format value of hex string setting
  1150. *
  1151. * @v settings Settings block, or NULL to search all blocks
  1152. * @v setting Setting to fetch
  1153. * @v buf Buffer to contain formatted value
  1154. * @v len Length of buffer
  1155. * @ret len Length of formatted value, or negative error
  1156. */
  1157. static int fetchf_hex ( struct settings *settings, struct setting *setting,
  1158. char *buf, size_t len ) {
  1159. int raw_len;
  1160. int check_len;
  1161. int used = 0;
  1162. int i;
  1163. raw_len = fetch_setting_len ( settings, setting );
  1164. if ( raw_len < 0 )
  1165. return raw_len;
  1166. {
  1167. uint8_t raw[raw_len];
  1168. check_len = fetch_setting ( settings, setting, raw,
  1169. sizeof ( raw ) );
  1170. if ( check_len < 0 )
  1171. return check_len;
  1172. assert ( check_len == raw_len );
  1173. if ( len )
  1174. buf[0] = 0; /* Ensure that a terminating NUL exists */
  1175. for ( i = 0 ; i < raw_len ; i++ ) {
  1176. used += ssnprintf ( ( buf + used ), ( len - used ),
  1177. "%s%02x", ( used ? ":" : "" ),
  1178. raw[i] );
  1179. }
  1180. return used;
  1181. }
  1182. }
  1183. /** A hex-string setting */
  1184. struct setting_type setting_type_hex __setting_type = {
  1185. .name = "hex",
  1186. .storef = storef_hex,
  1187. .fetchf = fetchf_hex,
  1188. };
  1189. /**
  1190. * Parse and store value of UUID setting
  1191. *
  1192. * @v settings Settings block
  1193. * @v setting Setting to store
  1194. * @v value Formatted setting data
  1195. * @ret rc Return status code
  1196. */
  1197. static int storef_uuid ( struct settings *settings __unused,
  1198. struct setting *setting __unused,
  1199. const char *value __unused ) {
  1200. return -ENOTSUP;
  1201. }
  1202. /**
  1203. * Fetch and format value of UUID setting
  1204. *
  1205. * @v settings Settings block, or NULL to search all blocks
  1206. * @v setting Setting to fetch
  1207. * @v buf Buffer to contain formatted value
  1208. * @v len Length of buffer
  1209. * @ret len Length of formatted value, or negative error
  1210. */
  1211. static int fetchf_uuid ( struct settings *settings, struct setting *setting,
  1212. char *buf, size_t len ) {
  1213. union uuid uuid;
  1214. int raw_len;
  1215. if ( ( raw_len = fetch_uuid_setting ( settings, setting, &uuid ) ) < 0)
  1216. return raw_len;
  1217. return snprintf ( buf, len, "%s", uuid_ntoa ( &uuid ) );
  1218. }
  1219. /** UUID setting type */
  1220. struct setting_type setting_type_uuid __setting_type = {
  1221. .name = "uuid",
  1222. .storef = storef_uuid,
  1223. .fetchf = fetchf_uuid,
  1224. };
  1225. /******************************************************************************
  1226. *
  1227. * Settings
  1228. *
  1229. ******************************************************************************
  1230. */
  1231. /** Hostname setting */
  1232. struct setting hostname_setting __setting = {
  1233. .name = "hostname",
  1234. .description = "Host name",
  1235. .tag = DHCP_HOST_NAME,
  1236. .type = &setting_type_string,
  1237. };
  1238. /** Filename setting */
  1239. struct setting filename_setting __setting = {
  1240. .name = "filename",
  1241. .description = "Boot filename",
  1242. .tag = DHCP_BOOTFILE_NAME,
  1243. .type = &setting_type_string,
  1244. };
  1245. /** Root path setting */
  1246. struct setting root_path_setting __setting = {
  1247. .name = "root-path",
  1248. .description = "NFS/iSCSI root path",
  1249. .tag = DHCP_ROOT_PATH,
  1250. .type = &setting_type_string,
  1251. };
  1252. /** Username setting */
  1253. struct setting username_setting __setting = {
  1254. .name = "username",
  1255. .description = "User name",
  1256. .tag = DHCP_EB_USERNAME,
  1257. .type = &setting_type_string,
  1258. };
  1259. /** Password setting */
  1260. struct setting password_setting __setting = {
  1261. .name = "password",
  1262. .description = "Password",
  1263. .tag = DHCP_EB_PASSWORD,
  1264. .type = &setting_type_string,
  1265. };
  1266. /** Priority setting */
  1267. struct setting priority_setting __setting = {
  1268. .name = "priority",
  1269. .description = "Priority of these settings",
  1270. .tag = DHCP_EB_PRIORITY,
  1271. .type = &setting_type_int8,
  1272. };