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

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