Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

settings.c 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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 <ipxe/in.h>
  28. #include <ipxe/vsprintf.h>
  29. #include <ipxe/dhcp.h>
  30. #include <ipxe/uuid.h>
  31. #include <ipxe/uri.h>
  32. #include <ipxe/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. /** Autovivified settings block */
  208. struct autovivified_settings {
  209. /** Reference count */
  210. struct refcnt refcnt;
  211. /** Generic settings block */
  212. struct generic_settings generic;
  213. };
  214. /**
  215. * Free autovivified settings block
  216. *
  217. * @v refcnt Reference count
  218. */
  219. static void autovivified_settings_free ( struct refcnt *refcnt ) {
  220. struct autovivified_settings *autovivified =
  221. container_of ( refcnt, struct autovivified_settings, refcnt );
  222. generic_settings_clear ( &autovivified->generic.settings );
  223. free ( autovivified );
  224. }
  225. /**
  226. * Find child named settings block
  227. *
  228. * @v parent Parent settings block
  229. * @v name Name within this parent
  230. * @ret settings Settings block, or NULL
  231. */
  232. static struct settings * find_child_settings ( struct settings *parent,
  233. const char *name ) {
  234. struct settings *settings;
  235. /* Treat empty name as meaning "this block" */
  236. if ( ! *name )
  237. return parent;
  238. /* Look for child with matching name */
  239. list_for_each_entry ( settings, &parent->children, siblings ) {
  240. if ( strcmp ( settings->name, name ) == 0 )
  241. return settings;
  242. }
  243. return NULL;
  244. }
  245. /**
  246. * Find or create child named settings block
  247. *
  248. * @v parent Parent settings block
  249. * @v name Name within this parent
  250. * @ret settings Settings block, or NULL
  251. */
  252. static struct settings * autovivify_child_settings ( struct settings *parent,
  253. const char *name ) {
  254. struct {
  255. struct autovivified_settings autovivified;
  256. char name[ strlen ( name ) + 1 /* NUL */ ];
  257. } *new_child;
  258. struct settings *settings;
  259. /* Return existing settings, if existent */
  260. if ( ( settings = find_child_settings ( parent, name ) ) != NULL )
  261. return settings;
  262. /* Create new generic settings block */
  263. new_child = zalloc ( sizeof ( *new_child ) );
  264. if ( ! new_child ) {
  265. DBGC ( parent, "Settings %p could not create child %s\n",
  266. parent, name );
  267. return NULL;
  268. }
  269. memcpy ( new_child->name, name, sizeof ( new_child->name ) );
  270. ref_init ( &new_child->autovivified.refcnt,
  271. autovivified_settings_free );
  272. generic_settings_init ( &new_child->autovivified.generic,
  273. &new_child->autovivified.refcnt );
  274. settings = &new_child->autovivified.generic.settings;
  275. register_settings ( settings, parent, new_child->name );
  276. return settings;
  277. }
  278. /**
  279. * Return settings block name
  280. *
  281. * @v settings Settings block
  282. * @ret name Settings block name
  283. */
  284. const char * settings_name ( struct settings *settings ) {
  285. static char buf[16];
  286. char tmp[ sizeof ( buf ) ];
  287. for ( buf[2] = buf[0] = 0 ; settings ; settings = settings->parent ) {
  288. memcpy ( tmp, buf, sizeof ( tmp ) );
  289. snprintf ( buf, sizeof ( buf ), ".%s%s", settings->name, tmp );
  290. }
  291. return ( buf + 2 );
  292. }
  293. /**
  294. * Parse settings block name
  295. *
  296. * @v name Name
  297. * @v get_child Function to find or create child settings block
  298. * @ret settings Settings block, or NULL
  299. */
  300. static struct settings *
  301. parse_settings_name ( const char *name,
  302. struct settings * ( * get_child ) ( struct settings *,
  303. const char * ) ) {
  304. struct settings *settings = &settings_root;
  305. char name_copy[ strlen ( name ) + 1 ];
  306. char *subname;
  307. char *remainder;
  308. /* Create modifiable copy of name */
  309. memcpy ( name_copy, name, sizeof ( name_copy ) );
  310. remainder = name_copy;
  311. /* Parse each name component in turn */
  312. while ( remainder ) {
  313. struct net_device *netdev;
  314. subname = remainder;
  315. remainder = strchr ( subname, '.' );
  316. if ( remainder )
  317. *(remainder++) = '\0';
  318. /* Special case "netX" root settings block */
  319. if ( ( subname == name_copy ) && ! strcmp ( subname, "netX" ) &&
  320. ( ( netdev = last_opened_netdev() ) != NULL ) )
  321. settings = get_child ( settings, netdev->name );
  322. else
  323. settings = get_child ( settings, subname );
  324. if ( ! settings )
  325. break;
  326. }
  327. return settings;
  328. }
  329. /**
  330. * Find named settings block
  331. *
  332. * @v name Name
  333. * @ret settings Settings block, or NULL
  334. */
  335. struct settings * find_settings ( const char *name ) {
  336. return parse_settings_name ( name, find_child_settings );
  337. }
  338. /**
  339. * Apply all settings
  340. *
  341. * @ret rc Return status code
  342. */
  343. static int apply_settings ( void ) {
  344. struct settings_applicator *applicator;
  345. int rc;
  346. /* Call all settings applicators */
  347. for_each_table_entry ( applicator, SETTINGS_APPLICATORS ) {
  348. if ( ( rc = applicator->apply() ) != 0 ) {
  349. DBG ( "Could not apply settings using applicator "
  350. "%p: %s\n", applicator, strerror ( rc ) );
  351. return rc;
  352. }
  353. }
  354. return 0;
  355. }
  356. /**
  357. * Reprioritise settings
  358. *
  359. * @v settings Settings block
  360. *
  361. * Reorders the settings block amongst its siblings according to its
  362. * priority.
  363. */
  364. static void reprioritise_settings ( struct settings *settings ) {
  365. struct settings *parent = settings->parent;
  366. long priority;
  367. struct settings *tmp;
  368. long tmp_priority;
  369. /* Stop when we reach the top of the tree */
  370. if ( ! parent )
  371. return;
  372. /* Read priority, if present */
  373. priority = fetch_intz_setting ( settings, &priority_setting );
  374. /* Remove from siblings list */
  375. list_del ( &settings->siblings );
  376. /* Reinsert after any existing blocks which have a higher priority */
  377. list_for_each_entry ( tmp, &parent->children, siblings ) {
  378. tmp_priority = fetch_intz_setting ( tmp, &priority_setting );
  379. if ( priority > tmp_priority )
  380. break;
  381. }
  382. list_add_tail ( &settings->siblings, &tmp->siblings );
  383. /* Recurse up the tree */
  384. reprioritise_settings ( parent );
  385. }
  386. /**
  387. * Register settings block
  388. *
  389. * @v settings Settings block
  390. * @v parent Parent settings block, or NULL
  391. * @v name Settings block name
  392. * @ret rc Return status code
  393. */
  394. int register_settings ( struct settings *settings, struct settings *parent,
  395. const char *name ) {
  396. struct settings *old_settings;
  397. /* NULL parent => add to settings root */
  398. assert ( settings != NULL );
  399. if ( parent == NULL )
  400. parent = &settings_root;
  401. /* Apply settings block name */
  402. settings->name = name;
  403. /* Remove any existing settings with the same name */
  404. if ( ( old_settings = find_child_settings ( parent, settings->name ) ))
  405. unregister_settings ( old_settings );
  406. /* Add to list of settings */
  407. ref_get ( settings->refcnt );
  408. ref_get ( parent->refcnt );
  409. settings->parent = parent;
  410. list_add_tail ( &settings->siblings, &parent->children );
  411. DBGC ( settings, "Settings %p (\"%s\") registered\n",
  412. settings, settings_name ( settings ) );
  413. /* Fix up settings priority */
  414. reprioritise_settings ( settings );
  415. /* Apply potentially-updated settings */
  416. apply_settings();
  417. return 0;
  418. }
  419. /**
  420. * Unregister settings block
  421. *
  422. * @v settings Settings block
  423. */
  424. void unregister_settings ( struct settings *settings ) {
  425. struct settings *child;
  426. struct settings *tmp;
  427. /* Unregister child settings */
  428. list_for_each_entry_safe ( child, tmp, &settings->children, siblings ) {
  429. unregister_settings ( child );
  430. }
  431. DBGC ( settings, "Settings %p (\"%s\") unregistered\n",
  432. settings, settings_name ( settings ) );
  433. /* Remove from list of settings */
  434. ref_put ( settings->parent->refcnt );
  435. settings->parent = NULL;
  436. list_del ( &settings->siblings );
  437. ref_put ( settings->refcnt );
  438. /* Apply potentially-updated settings */
  439. apply_settings();
  440. }
  441. /******************************************************************************
  442. *
  443. * Core settings routines
  444. *
  445. ******************************************************************************
  446. */
  447. /**
  448. * Check applicability of setting
  449. *
  450. * @v settings Settings block
  451. * @v setting Setting
  452. * @ret applies Setting applies within this settings block
  453. */
  454. int setting_applies ( struct settings *settings, struct setting *setting ) {
  455. return ( settings->op->applies ?
  456. settings->op->applies ( settings, setting ) : 1 );
  457. }
  458. /**
  459. * Store value of setting
  460. *
  461. * @v settings Settings block, or NULL
  462. * @v setting Setting to store
  463. * @v data Setting data, or NULL to clear setting
  464. * @v len Length of setting data
  465. * @ret rc Return status code
  466. */
  467. int store_setting ( struct settings *settings, struct setting *setting,
  468. const void *data, size_t len ) {
  469. int rc;
  470. /* NULL settings implies storing into the global settings root */
  471. if ( ! settings )
  472. settings = &settings_root;
  473. /* Fail if tag does not apply to this settings block */
  474. if ( ! setting_applies ( settings, setting ) )
  475. return -ENOTTY;
  476. /* Sanity check */
  477. if ( ! settings->op->store )
  478. return -ENOTSUP;
  479. /* Store setting */
  480. if ( ( rc = settings->op->store ( settings, setting,
  481. data, len ) ) != 0 )
  482. return rc;
  483. /* Reprioritise settings if necessary */
  484. if ( setting_cmp ( setting, &priority_setting ) == 0 )
  485. reprioritise_settings ( settings );
  486. /* If these settings are registered, apply potentially-updated
  487. * settings
  488. */
  489. for ( ; settings ; settings = settings->parent ) {
  490. if ( settings == &settings_root ) {
  491. if ( ( rc = apply_settings() ) != 0 )
  492. return rc;
  493. break;
  494. }
  495. }
  496. return 0;
  497. }
  498. /**
  499. * Fetch value and origin of setting
  500. *
  501. * @v settings Settings block, or NULL to search all blocks
  502. * @v setting Setting to fetch
  503. * @v origin Origin of setting to fill in
  504. * @v data Buffer to fill with setting data
  505. * @v len Length of buffer
  506. * @ret len Length of setting data, or negative error
  507. *
  508. * The actual length of the setting will be returned even if
  509. * the buffer was too small.
  510. */
  511. static int fetch_setting_and_origin ( struct settings *settings,
  512. struct setting *setting,
  513. struct settings **origin,
  514. void *data, size_t len ) {
  515. struct settings *child;
  516. int ret;
  517. /* Avoid returning uninitialised data on error */
  518. memset ( data, 0, len );
  519. if ( origin )
  520. *origin = NULL;
  521. /* NULL settings implies starting at the global settings root */
  522. if ( ! settings )
  523. settings = &settings_root;
  524. /* Sanity check */
  525. if ( ! settings->op->fetch )
  526. return -ENOTSUP;
  527. /* Try this block first, if applicable */
  528. if ( setting_applies ( settings, setting ) &&
  529. ( ( ret = settings->op->fetch ( settings, setting,
  530. data, len ) ) >= 0 ) ) {
  531. if ( origin )
  532. *origin = settings;
  533. return ret;
  534. }
  535. /* Recurse into each child block in turn */
  536. list_for_each_entry ( child, &settings->children, siblings ) {
  537. if ( ( ret = fetch_setting_and_origin ( child, setting, origin,
  538. data, len ) ) >= 0 )
  539. return ret;
  540. }
  541. return -ENOENT;
  542. }
  543. /**
  544. * Fetch value of setting
  545. *
  546. * @v settings Settings block, or NULL to search all blocks
  547. * @v setting Setting to fetch
  548. * @v data Buffer to fill with setting data
  549. * @v len Length of buffer
  550. * @ret len Length of setting data, or negative error
  551. *
  552. * The actual length of the setting will be returned even if
  553. * the buffer was too small.
  554. */
  555. int fetch_setting ( struct settings *settings, struct setting *setting,
  556. void *data, size_t len ) {
  557. return fetch_setting_and_origin ( settings, setting, NULL, data, len );
  558. }
  559. /**
  560. * Fetch origin of setting
  561. *
  562. * @v settings Settings block, or NULL to search all blocks
  563. * @v setting Setting to fetch
  564. * @ret origin Origin of setting, or NULL if not found
  565. *
  566. * This function can also be used as an existence check for the
  567. * setting.
  568. */
  569. struct settings * fetch_setting_origin ( struct settings *settings,
  570. struct setting *setting ) {
  571. struct settings *origin;
  572. fetch_setting_and_origin ( settings, setting, &origin, NULL, 0 );
  573. return origin;
  574. }
  575. /**
  576. * Fetch length of setting
  577. *
  578. * @v settings Settings block, or NULL to search all blocks
  579. * @v setting Setting to fetch
  580. * @ret len Length of setting data, or negative error
  581. *
  582. * This function can also be used as an existence check for the
  583. * setting.
  584. */
  585. int fetch_setting_len ( struct settings *settings, struct setting *setting ) {
  586. return fetch_setting ( settings, setting, NULL, 0 );
  587. }
  588. /**
  589. * Fetch value of string setting
  590. *
  591. * @v settings Settings block, or NULL to search all blocks
  592. * @v setting Setting to fetch
  593. * @v data Buffer to fill with setting string data
  594. * @v len Length of buffer
  595. * @ret len Length of string setting, or negative error
  596. *
  597. * The resulting string is guaranteed to be correctly NUL-terminated.
  598. * The returned length will be the length of the underlying setting
  599. * data.
  600. */
  601. int fetch_string_setting ( struct settings *settings, struct setting *setting,
  602. char *data, size_t len ) {
  603. memset ( data, 0, len );
  604. return fetch_setting ( settings, setting, data,
  605. ( ( len > 0 ) ? ( len - 1 ) : 0 ) );
  606. }
  607. /**
  608. * Fetch value of string setting
  609. *
  610. * @v settings Settings block, or NULL to search all blocks
  611. * @v setting Setting to fetch
  612. * @v data Buffer to allocate and fill with setting string data
  613. * @ret len Length of string setting, or negative error
  614. *
  615. * The resulting string is guaranteed to be correctly NUL-terminated.
  616. * The returned length will be the length of the underlying setting
  617. * data. The caller is responsible for eventually freeing the
  618. * allocated buffer.
  619. *
  620. * To allow the caller to distinguish between a non-existent setting
  621. * and an error in allocating memory for the copy, this function will
  622. * return success (and a NULL buffer pointer) for a non-existent
  623. * setting.
  624. */
  625. int fetch_string_setting_copy ( struct settings *settings,
  626. struct setting *setting,
  627. char **data ) {
  628. int len;
  629. int check_len = 0;
  630. /* Avoid returning uninitialised data on error */
  631. *data = NULL;
  632. /* Fetch setting length, and return success if non-existent */
  633. len = fetch_setting_len ( settings, setting );
  634. if ( len < 0 )
  635. return 0;
  636. /* Allocate string buffer */
  637. *data = malloc ( len + 1 );
  638. if ( ! *data )
  639. return -ENOMEM;
  640. /* Fetch setting */
  641. check_len = fetch_string_setting ( settings, setting, *data,
  642. ( len + 1 ) );
  643. assert ( check_len == len );
  644. return len;
  645. }
  646. /**
  647. * Fetch value of IPv4 address setting
  648. *
  649. * @v settings Settings block, or NULL to search all blocks
  650. * @v setting Setting to fetch
  651. * @v inp IPv4 addresses to fill in
  652. * @v count Maximum number of IPv4 addresses
  653. * @ret len Length of setting, or negative error
  654. */
  655. int fetch_ipv4_array_setting ( struct settings *settings,
  656. struct setting *setting,
  657. struct in_addr *inp, unsigned int count ) {
  658. int len;
  659. len = fetch_setting ( settings, setting, inp,
  660. ( sizeof ( *inp ) * count ) );
  661. if ( len < 0 )
  662. return len;
  663. if ( ( len % sizeof ( *inp ) ) != 0 )
  664. return -ERANGE;
  665. return len;
  666. }
  667. /**
  668. * Fetch value of IPv4 address setting
  669. *
  670. * @v settings Settings block, or NULL to search all blocks
  671. * @v setting Setting to fetch
  672. * @v inp IPv4 address to fill in
  673. * @ret len Length of setting, or negative error
  674. */
  675. int fetch_ipv4_setting ( struct settings *settings, struct setting *setting,
  676. struct in_addr *inp ) {
  677. return fetch_ipv4_array_setting ( settings, setting, inp, 1 );
  678. }
  679. /**
  680. * Fetch value of signed integer setting
  681. *
  682. * @v settings Settings block, or NULL to search all blocks
  683. * @v setting Setting to fetch
  684. * @v value Integer value to fill in
  685. * @ret len Length of setting, or negative error
  686. */
  687. int fetch_int_setting ( struct settings *settings, struct setting *setting,
  688. long *value ) {
  689. union {
  690. uint8_t u8[ sizeof ( long ) ];
  691. int8_t s8[ sizeof ( long ) ];
  692. } buf;
  693. int len;
  694. int i;
  695. /* Avoid returning uninitialised data on error */
  696. *value = 0;
  697. /* Fetch raw (network-ordered, variable-length) setting */
  698. len = fetch_setting ( settings, setting, &buf, sizeof ( buf ) );
  699. if ( len < 0 )
  700. return len;
  701. if ( len > ( int ) sizeof ( buf ) )
  702. return -ERANGE;
  703. /* Convert to host-ordered signed long */
  704. *value = ( ( buf.s8[0] >= 0 ) ? 0 : -1L );
  705. for ( i = 0 ; i < len ; i++ ) {
  706. *value = ( ( *value << 8 ) | buf.u8[i] );
  707. }
  708. return len;
  709. }
  710. /**
  711. * Fetch value of unsigned integer setting
  712. *
  713. * @v settings Settings block, or NULL to search all blocks
  714. * @v setting Setting to fetch
  715. * @v value Integer value to fill in
  716. * @ret len Length of setting, or negative error
  717. */
  718. int fetch_uint_setting ( struct settings *settings, struct setting *setting,
  719. unsigned long *value ) {
  720. long svalue;
  721. int len;
  722. /* Avoid returning uninitialised data on error */
  723. *value = 0;
  724. /* Fetch as a signed long */
  725. len = fetch_int_setting ( settings, setting, &svalue );
  726. if ( len < 0 )
  727. return len;
  728. /* Mask off sign-extended bits */
  729. assert ( len <= ( int ) sizeof ( long ) );
  730. *value = ( svalue & ( -1UL >> ( 8 * ( sizeof ( long ) - len ) ) ) );
  731. return len;
  732. }
  733. /**
  734. * Fetch value of signed integer setting, or zero
  735. *
  736. * @v settings Settings block, or NULL to search all blocks
  737. * @v setting Setting to fetch
  738. * @ret value Setting value, or zero
  739. */
  740. long fetch_intz_setting ( struct settings *settings, struct setting *setting ){
  741. long value;
  742. fetch_int_setting ( settings, setting, &value );
  743. return value;
  744. }
  745. /**
  746. * Fetch value of unsigned integer setting, or zero
  747. *
  748. * @v settings Settings block, or NULL to search all blocks
  749. * @v setting Setting to fetch
  750. * @ret value Setting value, or zero
  751. */
  752. unsigned long fetch_uintz_setting ( struct settings *settings,
  753. struct setting *setting ) {
  754. unsigned long value;
  755. fetch_uint_setting ( settings, setting, &value );
  756. return value;
  757. }
  758. /**
  759. * Fetch value of UUID setting
  760. *
  761. * @v settings Settings block, or NULL to search all blocks
  762. * @v setting Setting to fetch
  763. * @v uuid UUID to fill in
  764. * @ret len Length of setting, or negative error
  765. */
  766. int fetch_uuid_setting ( struct settings *settings, struct setting *setting,
  767. union uuid *uuid ) {
  768. int len;
  769. len = fetch_setting ( settings, setting, uuid, sizeof ( *uuid ) );
  770. if ( len < 0 )
  771. return len;
  772. if ( len != sizeof ( *uuid ) )
  773. return -ERANGE;
  774. return len;
  775. }
  776. /**
  777. * Clear settings block
  778. *
  779. * @v settings Settings block
  780. */
  781. void clear_settings ( struct settings *settings ) {
  782. if ( settings->op->clear )
  783. settings->op->clear ( settings );
  784. }
  785. /**
  786. * Compare two settings
  787. *
  788. * @v a Setting to compare
  789. * @v b Setting to compare
  790. * @ret 0 Settings are the same
  791. * @ret non-zero Settings are not the same
  792. */
  793. int setting_cmp ( struct setting *a, struct setting *b ) {
  794. /* If the settings have tags, compare them */
  795. if ( a->tag && ( a->tag == b->tag ) )
  796. return 0;
  797. /* Otherwise, if the settings have names, compare them */
  798. if ( a->name && b->name && a->name[0] )
  799. return strcmp ( a->name, b->name );
  800. /* Otherwise, return a non-match */
  801. return ( ! 0 );
  802. }
  803. /******************************************************************************
  804. *
  805. * Formatted setting routines
  806. *
  807. ******************************************************************************
  808. */
  809. /**
  810. * Store value of typed setting
  811. *
  812. * @v settings Settings block
  813. * @v setting Setting to store
  814. * @v type Settings type
  815. * @v value Formatted setting data, or NULL
  816. * @ret rc Return status code
  817. */
  818. int storef_setting ( struct settings *settings, struct setting *setting,
  819. const char *value ) {
  820. /* NULL value implies deletion. Avoid imposing the burden of
  821. * checking for NULL values on each typed setting's storef()
  822. * method.
  823. */
  824. if ( ! value )
  825. return delete_setting ( settings, setting );
  826. return setting->type->storef ( settings, setting, value );
  827. }
  828. /**
  829. * Find named setting
  830. *
  831. * @v name Name
  832. * @ret setting Named setting, or NULL
  833. */
  834. struct setting * find_setting ( const char *name ) {
  835. struct setting *setting;
  836. for_each_table_entry ( setting, SETTINGS ) {
  837. if ( strcmp ( name, setting->name ) == 0 )
  838. return setting;
  839. }
  840. return NULL;
  841. }
  842. /**
  843. * Parse setting name as tag number
  844. *
  845. * @v settings Settings block
  846. * @v name Name
  847. * @ret tag Tag number, or 0 if not a valid number
  848. */
  849. static unsigned int parse_setting_tag ( struct settings *settings,
  850. const char *name ) {
  851. char *tmp = ( ( char * ) name );
  852. unsigned int tag = 0;
  853. while ( 1 ) {
  854. tag = ( ( tag << 8 ) | strtoul ( tmp, &tmp, 0 ) );
  855. if ( *tmp == 0 )
  856. return ( tag | settings->tag_magic );
  857. if ( *tmp != '.' )
  858. return 0;
  859. tmp++;
  860. }
  861. }
  862. /**
  863. * Find setting type
  864. *
  865. * @v name Name
  866. * @ret type Setting type, or NULL
  867. */
  868. static struct setting_type * find_setting_type ( const char *name ) {
  869. struct setting_type *type;
  870. for_each_table_entry ( type, SETTING_TYPES ) {
  871. if ( strcmp ( name, type->name ) == 0 )
  872. return type;
  873. }
  874. return NULL;
  875. }
  876. /**
  877. * Parse setting name
  878. *
  879. * @v name Name of setting
  880. * @v get_child Function to find or create child settings block
  881. * @v settings Settings block to fill in
  882. * @v setting Setting to fill in
  883. * @v tmp_name Buffer for copy of setting name
  884. * @ret rc Return status code
  885. *
  886. * Interprets a name of the form
  887. * "[settings_name/]tag_name[:type_name]" and fills in the appropriate
  888. * fields.
  889. *
  890. * The @c tmp_name buffer must be large enough to hold a copy of the
  891. * setting name.
  892. */
  893. static int
  894. parse_setting_name ( const char *name,
  895. struct settings * ( * get_child ) ( struct settings *,
  896. const char * ),
  897. struct settings **settings, struct setting *setting,
  898. char *tmp_name ) {
  899. char *settings_name;
  900. char *setting_name;
  901. char *type_name;
  902. struct setting *named_setting;
  903. /* Set defaults */
  904. *settings = &settings_root;
  905. memset ( setting, 0, sizeof ( *setting ) );
  906. setting->name = "";
  907. setting->type = &setting_type_string;
  908. /* Split name into "[settings_name/]setting_name[:type_name]" */
  909. strcpy ( tmp_name, name );
  910. if ( ( setting_name = strchr ( tmp_name, '/' ) ) != NULL ) {
  911. *(setting_name++) = 0;
  912. settings_name = tmp_name;
  913. } else {
  914. setting_name = tmp_name;
  915. settings_name = NULL;
  916. }
  917. if ( ( type_name = strchr ( setting_name, ':' ) ) != NULL )
  918. *(type_name++) = 0;
  919. /* Identify settings block, if specified */
  920. if ( settings_name ) {
  921. *settings = parse_settings_name ( settings_name, get_child );
  922. if ( *settings == NULL ) {
  923. DBG ( "Unrecognised settings block \"%s\" in \"%s\"\n",
  924. settings_name, name );
  925. return -ENODEV;
  926. }
  927. }
  928. /* Identify setting */
  929. setting->tag = parse_setting_tag ( *settings, setting_name );
  930. setting->name = setting_name;
  931. for_each_table_entry ( named_setting, SETTINGS ) {
  932. /* Matches a defined named setting; use that setting */
  933. if ( setting_cmp ( named_setting, setting ) == 0 ) {
  934. memcpy ( setting, named_setting, sizeof ( *setting ) );
  935. break;
  936. }
  937. }
  938. /* Identify setting type, if specified */
  939. if ( type_name ) {
  940. setting->type = find_setting_type ( type_name );
  941. if ( setting->type == NULL ) {
  942. DBG ( "Invalid setting type \"%s\" in \"%s\"\n",
  943. type_name, name );
  944. return -ENOTSUP;
  945. }
  946. }
  947. return 0;
  948. }
  949. /**
  950. * Return full setting name
  951. *
  952. * @v settings Settings block, or NULL
  953. * @v setting Setting
  954. * @v buf Buffer
  955. * @v len Length of buffer
  956. * @ret len Length of setting name, or negative error
  957. */
  958. int setting_name ( struct settings *settings, struct setting *setting,
  959. char *buf, size_t len ) {
  960. const char *name;
  961. if ( ! settings )
  962. settings = &settings_root;
  963. name = settings_name ( settings );
  964. return snprintf ( buf, len, "%s%s%s:%s", name, ( name[0] ? "/" : "" ),
  965. setting->name, setting->type->name );
  966. }
  967. /**
  968. * Parse and store value of named setting
  969. *
  970. * @v name Name of setting
  971. * @v value Formatted setting data, or NULL
  972. * @ret rc Return status code
  973. */
  974. int storef_named_setting ( const char *name, const char *value ) {
  975. struct settings *settings;
  976. struct setting setting;
  977. char tmp_name[ strlen ( name ) + 1 ];
  978. int rc;
  979. /* Parse setting name */
  980. if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
  981. &settings, &setting, tmp_name )) != 0)
  982. return rc;
  983. /* Store setting */
  984. if ( ( rc = storef_setting ( settings, &setting, value ) ) != 0 )
  985. return rc;
  986. return 0;
  987. }
  988. /**
  989. * Fetch and format value of named setting
  990. *
  991. * @v name Name of setting
  992. * @v name_buf Buffer to contain canonicalised name
  993. * @v name_len Length of canonicalised name buffer
  994. * @v value_buf Buffer to contain formatted value
  995. * @v value_len Length of formatted value buffer
  996. * @ret len Length of formatted value, or negative error
  997. */
  998. int fetchf_named_setting ( const char *name,
  999. char *name_buf, size_t name_len,
  1000. char *value_buf, size_t value_len ) {
  1001. struct settings *settings;
  1002. struct setting setting;
  1003. struct settings *origin;
  1004. char tmp_name[ strlen ( name ) + 1 ];
  1005. int len;
  1006. int rc;
  1007. /* Parse setting name */
  1008. if ( ( rc = parse_setting_name ( name, find_child_settings,
  1009. &settings, &setting, tmp_name )) != 0)
  1010. return rc;
  1011. /* Fetch setting */
  1012. if ( ( len = fetchf_setting ( settings, &setting, value_buf,
  1013. value_len ) ) < 0 )
  1014. return len;
  1015. /* Construct setting name */
  1016. origin = fetch_setting_origin ( settings, &setting );
  1017. assert ( origin != NULL );
  1018. setting_name ( origin, &setting, name_buf, name_len );
  1019. return len;
  1020. }
  1021. /******************************************************************************
  1022. *
  1023. * Setting types
  1024. *
  1025. ******************************************************************************
  1026. */
  1027. /**
  1028. * Parse and store value of string setting
  1029. *
  1030. * @v settings Settings block
  1031. * @v setting Setting to store
  1032. * @v value Formatted setting data
  1033. * @ret rc Return status code
  1034. */
  1035. static int storef_string ( struct settings *settings, struct setting *setting,
  1036. const char *value ) {
  1037. return store_setting ( settings, setting, value, strlen ( value ) );
  1038. }
  1039. /**
  1040. * Fetch and format value of string setting
  1041. *
  1042. * @v settings Settings block, or NULL to search all blocks
  1043. * @v setting Setting to fetch
  1044. * @v buf Buffer to contain formatted value
  1045. * @v len Length of buffer
  1046. * @ret len Length of formatted value, or negative error
  1047. */
  1048. static int fetchf_string ( struct settings *settings, struct setting *setting,
  1049. char *buf, size_t len ) {
  1050. return fetch_string_setting ( settings, setting, buf, len );
  1051. }
  1052. /** A string setting type */
  1053. struct setting_type setting_type_string __setting_type = {
  1054. .name = "string",
  1055. .storef = storef_string,
  1056. .fetchf = fetchf_string,
  1057. };
  1058. /**
  1059. * Parse and store value of URI-encoded string setting
  1060. *
  1061. * @v settings Settings block
  1062. * @v setting Setting to store
  1063. * @v value Formatted setting data
  1064. * @ret rc Return status code
  1065. */
  1066. static int storef_uristring ( struct settings *settings,
  1067. struct setting *setting,
  1068. const char *value ) {
  1069. char buf[ strlen ( value ) + 1 ]; /* Decoding never expands string */
  1070. size_t len;
  1071. len = uri_decode ( value, buf, sizeof ( buf ) );
  1072. return store_setting ( settings, setting, buf, len );
  1073. }
  1074. /**
  1075. * Fetch and format value of URI-encoded string setting
  1076. *
  1077. * @v settings Settings block, or NULL to search all blocks
  1078. * @v setting Setting to fetch
  1079. * @v buf Buffer to contain formatted value
  1080. * @v len Length of buffer
  1081. * @ret len Length of formatted value, or negative error
  1082. */
  1083. static int fetchf_uristring ( struct settings *settings,
  1084. struct setting *setting,
  1085. char *buf, size_t len ) {
  1086. ssize_t raw_len;
  1087. /* We need to always retrieve the full raw string to know the
  1088. * length of the encoded string.
  1089. */
  1090. raw_len = fetch_setting ( settings, setting, NULL, 0 );
  1091. if ( raw_len < 0 )
  1092. return raw_len;
  1093. {
  1094. char raw_buf[ raw_len + 1 ];
  1095. fetch_string_setting ( settings, setting, raw_buf,
  1096. sizeof ( raw_buf ) );
  1097. return uri_encode ( raw_buf, buf, len, URI_FRAGMENT );
  1098. }
  1099. }
  1100. /** A URI-encoded string setting type */
  1101. struct setting_type setting_type_uristring __setting_type = {
  1102. .name = "uristring",
  1103. .storef = storef_uristring,
  1104. .fetchf = fetchf_uristring,
  1105. };
  1106. /**
  1107. * Parse and store value of IPv4 address setting
  1108. *
  1109. * @v settings Settings block
  1110. * @v setting Setting to store
  1111. * @v value Formatted setting data
  1112. * @ret rc Return status code
  1113. */
  1114. static int storef_ipv4 ( struct settings *settings, struct setting *setting,
  1115. const char *value ) {
  1116. struct in_addr ipv4;
  1117. if ( inet_aton ( value, &ipv4 ) == 0 )
  1118. return -EINVAL;
  1119. return store_setting ( settings, setting, &ipv4, sizeof ( ipv4 ) );
  1120. }
  1121. /**
  1122. * Fetch and format value of IPv4 address setting
  1123. *
  1124. * @v settings Settings block, or NULL to search all blocks
  1125. * @v setting Setting to fetch
  1126. * @v buf Buffer to contain formatted value
  1127. * @v len Length of buffer
  1128. * @ret len Length of formatted value, or negative error
  1129. */
  1130. static int fetchf_ipv4 ( struct settings *settings, struct setting *setting,
  1131. char *buf, size_t len ) {
  1132. struct in_addr ipv4;
  1133. int raw_len;
  1134. if ( ( raw_len = fetch_ipv4_setting ( settings, setting, &ipv4 ) ) < 0)
  1135. return raw_len;
  1136. return snprintf ( buf, len, "%s", inet_ntoa ( ipv4 ) );
  1137. }
  1138. /** An IPv4 address setting type */
  1139. struct setting_type setting_type_ipv4 __setting_type = {
  1140. .name = "ipv4",
  1141. .storef = storef_ipv4,
  1142. .fetchf = fetchf_ipv4,
  1143. };
  1144. /**
  1145. * Parse and store value of integer setting
  1146. *
  1147. * @v settings Settings block
  1148. * @v setting Setting to store
  1149. * @v value Formatted setting data
  1150. * @v size Integer size, in bytes
  1151. * @ret rc Return status code
  1152. */
  1153. static int storef_int ( struct settings *settings, struct setting *setting,
  1154. const char *value, unsigned int size ) {
  1155. union {
  1156. uint32_t num;
  1157. uint8_t bytes[4];
  1158. } u;
  1159. char *endp;
  1160. u.num = htonl ( strtoul ( value, &endp, 0 ) );
  1161. if ( *endp )
  1162. return -EINVAL;
  1163. return store_setting ( settings, setting,
  1164. &u.bytes[ sizeof ( u ) - size ], size );
  1165. }
  1166. /**
  1167. * Parse and store value of 8-bit integer setting
  1168. *
  1169. * @v settings Settings block
  1170. * @v setting Setting to store
  1171. * @v value Formatted setting data
  1172. * @v size Integer size, in bytes
  1173. * @ret rc Return status code
  1174. */
  1175. static int storef_int8 ( struct settings *settings, struct setting *setting,
  1176. const char *value ) {
  1177. return storef_int ( settings, setting, value, 1 );
  1178. }
  1179. /**
  1180. * Parse and store value of 16-bit integer setting
  1181. *
  1182. * @v settings Settings block
  1183. * @v setting Setting to store
  1184. * @v value Formatted setting data
  1185. * @v size Integer size, in bytes
  1186. * @ret rc Return status code
  1187. */
  1188. static int storef_int16 ( struct settings *settings, struct setting *setting,
  1189. const char *value ) {
  1190. return storef_int ( settings, setting, value, 2 );
  1191. }
  1192. /**
  1193. * Parse and store value of 32-bit integer setting
  1194. *
  1195. * @v settings Settings block
  1196. * @v setting Setting to store
  1197. * @v value Formatted setting data
  1198. * @v size Integer size, in bytes
  1199. * @ret rc Return status code
  1200. */
  1201. static int storef_int32 ( struct settings *settings, struct setting *setting,
  1202. const char *value ) {
  1203. return storef_int ( settings, setting, value, 4 );
  1204. }
  1205. /**
  1206. * Fetch and format value of signed integer setting
  1207. *
  1208. * @v settings Settings block, or NULL to search all blocks
  1209. * @v setting Setting to fetch
  1210. * @v buf Buffer to contain formatted value
  1211. * @v len Length of buffer
  1212. * @ret len Length of formatted value, or negative error
  1213. */
  1214. static int fetchf_int ( struct settings *settings, struct setting *setting,
  1215. char *buf, size_t len ) {
  1216. long value;
  1217. int rc;
  1218. if ( ( rc = fetch_int_setting ( settings, setting, &value ) ) < 0 )
  1219. return rc;
  1220. return snprintf ( buf, len, "%ld", value );
  1221. }
  1222. /**
  1223. * Fetch and format value of unsigned integer setting
  1224. *
  1225. * @v settings Settings block, or NULL to search all blocks
  1226. * @v setting Setting to fetch
  1227. * @v buf Buffer to contain formatted value
  1228. * @v len Length of buffer
  1229. * @ret len Length of formatted value, or negative error
  1230. */
  1231. static int fetchf_uint ( struct settings *settings, struct setting *setting,
  1232. char *buf, size_t len ) {
  1233. unsigned long value;
  1234. int rc;
  1235. if ( ( rc = fetch_uint_setting ( settings, setting, &value ) ) < 0 )
  1236. return rc;
  1237. return snprintf ( buf, len, "%#lx", value );
  1238. }
  1239. /** A signed 8-bit integer setting type */
  1240. struct setting_type setting_type_int8 __setting_type = {
  1241. .name = "int8",
  1242. .storef = storef_int8,
  1243. .fetchf = fetchf_int,
  1244. };
  1245. /** A signed 16-bit integer setting type */
  1246. struct setting_type setting_type_int16 __setting_type = {
  1247. .name = "int16",
  1248. .storef = storef_int16,
  1249. .fetchf = fetchf_int,
  1250. };
  1251. /** A signed 32-bit integer setting type */
  1252. struct setting_type setting_type_int32 __setting_type = {
  1253. .name = "int32",
  1254. .storef = storef_int32,
  1255. .fetchf = fetchf_int,
  1256. };
  1257. /** An unsigned 8-bit integer setting type */
  1258. struct setting_type setting_type_uint8 __setting_type = {
  1259. .name = "uint8",
  1260. .storef = storef_int8,
  1261. .fetchf = fetchf_uint,
  1262. };
  1263. /** An unsigned 16-bit integer setting type */
  1264. struct setting_type setting_type_uint16 __setting_type = {
  1265. .name = "uint16",
  1266. .storef = storef_int16,
  1267. .fetchf = fetchf_uint,
  1268. };
  1269. /** An unsigned 32-bit integer setting type */
  1270. struct setting_type setting_type_uint32 __setting_type = {
  1271. .name = "uint32",
  1272. .storef = storef_int32,
  1273. .fetchf = fetchf_uint,
  1274. };
  1275. /**
  1276. * Parse and store value of hex string setting
  1277. *
  1278. * @v settings Settings block
  1279. * @v setting Setting to store
  1280. * @v value Formatted setting data
  1281. * @ret rc Return status code
  1282. */
  1283. static int storef_hex ( struct settings *settings, struct setting *setting,
  1284. const char *value ) {
  1285. char *ptr = ( char * ) value;
  1286. uint8_t bytes[ strlen ( value ) ]; /* cannot exceed strlen(value) */
  1287. unsigned int len = 0;
  1288. while ( 1 ) {
  1289. bytes[len++] = strtoul ( ptr, &ptr, 16 );
  1290. switch ( *ptr ) {
  1291. case '\0' :
  1292. return store_setting ( settings, setting, bytes, len );
  1293. case ':' :
  1294. case '-' :
  1295. ptr++;
  1296. break;
  1297. default :
  1298. return -EINVAL;
  1299. }
  1300. }
  1301. }
  1302. /**
  1303. * Fetch and format value of hex string setting
  1304. *
  1305. * @v settings Settings block, or NULL to search all blocks
  1306. * @v setting Setting to fetch
  1307. * @v buf Buffer to contain formatted value
  1308. * @v len Length of buffer
  1309. * @v delimiter Byte delimiter
  1310. * @ret len Length of formatted value, or negative error
  1311. */
  1312. static int fetchf_hex ( struct settings *settings, struct setting *setting,
  1313. char *buf, size_t len, const char *delimiter ) {
  1314. int raw_len;
  1315. int check_len;
  1316. int used = 0;
  1317. int i;
  1318. raw_len = fetch_setting_len ( settings, setting );
  1319. if ( raw_len < 0 )
  1320. return raw_len;
  1321. {
  1322. uint8_t raw[raw_len];
  1323. check_len = fetch_setting ( settings, setting, raw,
  1324. sizeof ( raw ) );
  1325. if ( check_len < 0 )
  1326. return check_len;
  1327. assert ( check_len == raw_len );
  1328. if ( len )
  1329. buf[0] = 0; /* Ensure that a terminating NUL exists */
  1330. for ( i = 0 ; i < raw_len ; i++ ) {
  1331. used += ssnprintf ( ( buf + used ), ( len - used ),
  1332. "%s%02x", ( used ? delimiter : "" ),
  1333. raw[i] );
  1334. }
  1335. return used;
  1336. }
  1337. }
  1338. /**
  1339. * Fetch and format value of hex string setting (using colon delimiter)
  1340. *
  1341. * @v settings Settings block, or NULL to search all blocks
  1342. * @v setting Setting to fetch
  1343. * @v buf Buffer to contain formatted value
  1344. * @v len Length of buffer
  1345. * @ret len Length of formatted value, or negative error
  1346. */
  1347. static int fetchf_hex_colon ( struct settings *settings,
  1348. struct setting *setting,
  1349. char *buf, size_t len ) {
  1350. return fetchf_hex ( settings, setting, buf, len, ":" );
  1351. }
  1352. /**
  1353. * Fetch and format value of hex string setting (using hyphen delimiter)
  1354. *
  1355. * @v settings Settings block, or NULL to search all blocks
  1356. * @v setting Setting to fetch
  1357. * @v buf Buffer to contain formatted value
  1358. * @v len Length of buffer
  1359. * @ret len Length of formatted value, or negative error
  1360. */
  1361. static int fetchf_hex_hyphen ( struct settings *settings,
  1362. struct setting *setting,
  1363. char *buf, size_t len ) {
  1364. return fetchf_hex ( settings, setting, buf, len, "-" );
  1365. }
  1366. /** A hex-string setting (colon-delimited) */
  1367. struct setting_type setting_type_hex __setting_type = {
  1368. .name = "hex",
  1369. .storef = storef_hex,
  1370. .fetchf = fetchf_hex_colon,
  1371. };
  1372. /** A hex-string setting (hyphen-delimited) */
  1373. struct setting_type setting_type_hexhyp __setting_type = {
  1374. .name = "hexhyp",
  1375. .storef = storef_hex,
  1376. .fetchf = fetchf_hex_hyphen,
  1377. };
  1378. /**
  1379. * Parse and store value of UUID setting
  1380. *
  1381. * @v settings Settings block
  1382. * @v setting Setting to store
  1383. * @v value Formatted setting data
  1384. * @ret rc Return status code
  1385. */
  1386. static int storef_uuid ( struct settings *settings __unused,
  1387. struct setting *setting __unused,
  1388. const char *value __unused ) {
  1389. return -ENOTSUP;
  1390. }
  1391. /**
  1392. * Fetch and format value of UUID setting
  1393. *
  1394. * @v settings Settings block, or NULL to search all blocks
  1395. * @v setting Setting to fetch
  1396. * @v buf Buffer to contain formatted value
  1397. * @v len Length of buffer
  1398. * @ret len Length of formatted value, or negative error
  1399. */
  1400. static int fetchf_uuid ( struct settings *settings, struct setting *setting,
  1401. char *buf, size_t len ) {
  1402. union uuid uuid;
  1403. int raw_len;
  1404. if ( ( raw_len = fetch_uuid_setting ( settings, setting, &uuid ) ) < 0)
  1405. return raw_len;
  1406. return snprintf ( buf, len, "%s", uuid_ntoa ( &uuid ) );
  1407. }
  1408. /** UUID setting type */
  1409. struct setting_type setting_type_uuid __setting_type = {
  1410. .name = "uuid",
  1411. .storef = storef_uuid,
  1412. .fetchf = fetchf_uuid,
  1413. };
  1414. /******************************************************************************
  1415. *
  1416. * Setting expansion
  1417. *
  1418. ******************************************************************************
  1419. */
  1420. /**
  1421. * Expand variables within string
  1422. *
  1423. * @v string String
  1424. * @ret expstr Expanded string
  1425. *
  1426. * The expanded string is allocated with malloc() and the caller must
  1427. * eventually free() it.
  1428. */
  1429. char * expand_settings ( const char *string ) {
  1430. char *expstr;
  1431. char *start;
  1432. char *end;
  1433. char *head;
  1434. char *name;
  1435. char *tail;
  1436. int setting_len;
  1437. int new_len;
  1438. char *tmp;
  1439. /* Obtain temporary modifiable copy of string */
  1440. expstr = strdup ( string );
  1441. if ( ! expstr )
  1442. return NULL;
  1443. /* Expand while expansions remain */
  1444. while ( 1 ) {
  1445. head = expstr;
  1446. /* Locate setting to be expanded */
  1447. start = NULL;
  1448. end = NULL;
  1449. for ( tmp = expstr ; *tmp ; tmp++ ) {
  1450. if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) )
  1451. start = tmp;
  1452. if ( start && ( tmp[0] == '}' ) ) {
  1453. end = tmp;
  1454. break;
  1455. }
  1456. }
  1457. if ( ! end )
  1458. break;
  1459. *start = '\0';
  1460. name = ( start + 2 );
  1461. *end = '\0';
  1462. tail = ( end + 1 );
  1463. /* Determine setting length */
  1464. setting_len = fetchf_named_setting ( name, NULL, 0, NULL, 0 );
  1465. if ( setting_len < 0 )
  1466. setting_len = 0; /* Treat error as empty setting */
  1467. /* Read setting into temporary buffer */
  1468. {
  1469. char setting_buf[ setting_len + 1 ];
  1470. setting_buf[0] = '\0';
  1471. fetchf_named_setting ( name, NULL, 0, setting_buf,
  1472. sizeof ( setting_buf ) );
  1473. /* Construct expanded string and discard old string */
  1474. tmp = expstr;
  1475. new_len = asprintf ( &expstr, "%s%s%s",
  1476. head, setting_buf, tail );
  1477. free ( tmp );
  1478. if ( new_len < 0 )
  1479. return NULL;
  1480. }
  1481. }
  1482. return expstr;
  1483. }
  1484. /******************************************************************************
  1485. *
  1486. * Settings
  1487. *
  1488. ******************************************************************************
  1489. */
  1490. /** Hostname setting */
  1491. struct setting hostname_setting __setting ( SETTING_HOST ) = {
  1492. .name = "hostname",
  1493. .description = "Host name",
  1494. .tag = DHCP_HOST_NAME,
  1495. .type = &setting_type_string,
  1496. };
  1497. /** Filename setting */
  1498. struct setting filename_setting __setting ( SETTING_BOOT ) = {
  1499. .name = "filename",
  1500. .description = "Boot filename",
  1501. .tag = DHCP_BOOTFILE_NAME,
  1502. .type = &setting_type_string,
  1503. };
  1504. /** Root path setting */
  1505. struct setting root_path_setting __setting ( SETTING_SANBOOT ) = {
  1506. .name = "root-path",
  1507. .description = "SAN root path",
  1508. .tag = DHCP_ROOT_PATH,
  1509. .type = &setting_type_string,
  1510. };
  1511. /** Username setting */
  1512. struct setting username_setting __setting ( SETTING_AUTH ) = {
  1513. .name = "username",
  1514. .description = "User name",
  1515. .tag = DHCP_EB_USERNAME,
  1516. .type = &setting_type_string,
  1517. };
  1518. /** Password setting */
  1519. struct setting password_setting __setting ( SETTING_AUTH ) = {
  1520. .name = "password",
  1521. .description = "Password",
  1522. .tag = DHCP_EB_PASSWORD,
  1523. .type = &setting_type_string,
  1524. };
  1525. /** Priority setting */
  1526. struct setting priority_setting __setting ( SETTING_MISC ) = {
  1527. .name = "priority",
  1528. .description = "Settings priority",
  1529. .tag = DHCP_EB_PRIORITY,
  1530. .type = &setting_type_int8,
  1531. };