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

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