tree.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _PARSE_C_
  15. #define _PARSE_C_
  16. #include <stdio.h>
  17. #include <stdarg.h>
  18. #include "basics.h"
  19. #include "device.h"
  20. #include "tree.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include "libiberty.h"
  25. /* manipulate/lookup device names */
  26. typedef struct _name_specifier {
  27. /* components in the full length name */
  28. char *path;
  29. char *property;
  30. char *value;
  31. /* current device */
  32. char *name;
  33. char *base;
  34. char *unit;
  35. char *args;
  36. /* previous device */
  37. char *last_name;
  38. char *last_base;
  39. char *last_unit;
  40. char *last_args;
  41. /* work area */
  42. char buf[1024];
  43. } name_specifier;
  44. /* Given a device specifier, break it up into its main components:
  45. path (and if present) property name and property value. */
  46. STATIC_INLINE_TREE\
  47. (int)
  48. split_device_specifier(device *current,
  49. const char *device_specifier,
  50. name_specifier *spec)
  51. {
  52. char *chp = NULL;
  53. /* expand any leading alias if present */
  54. if (current != NULL
  55. && *device_specifier != '\0'
  56. && *device_specifier != '.'
  57. && *device_specifier != '/') {
  58. device *aliases = tree_find_device(current, "/aliases");
  59. char alias[32];
  60. int len = 0;
  61. while (device_specifier[len] != '\0'
  62. && device_specifier[len] != '/'
  63. && device_specifier[len] != ':'
  64. && !isspace(device_specifier[len])) {
  65. alias[len] = device_specifier[len];
  66. len++;
  67. if (len >= sizeof(alias))
  68. error("split_device_specifier: buffer overflow");
  69. }
  70. alias[len] = '\0';
  71. if (aliases != NULL
  72. && device_find_property(aliases, alias)) {
  73. strcpy(spec->buf, device_find_string_property(aliases, alias));
  74. strcat(spec->buf, device_specifier + len);
  75. }
  76. else {
  77. strcpy(spec->buf, device_specifier);
  78. }
  79. }
  80. else {
  81. strcpy(spec->buf, device_specifier);
  82. }
  83. /* check no overflow */
  84. if (strlen(spec->buf) >= sizeof(spec->buf))
  85. error("split_device_specifier: buffer overflow\n");
  86. /* strip leading spaces */
  87. chp = spec->buf;
  88. while (*chp != '\0' && isspace(*chp))
  89. chp++;
  90. if (*chp == '\0')
  91. return 0;
  92. /* find the path and terminate it with null */
  93. spec->path = chp;
  94. while (*chp != '\0' && !isspace(*chp))
  95. chp++;
  96. if (*chp != '\0') {
  97. *chp = '\0';
  98. chp++;
  99. }
  100. /* and any value */
  101. while (*chp != '\0' && isspace(*chp))
  102. chp++;
  103. spec->value = chp;
  104. /* now go back and chop the property off of the path */
  105. if (spec->value[0] == '\0') {
  106. spec->property = NULL; /*not a property*/
  107. spec->value = NULL;
  108. }
  109. else if (spec->value[0] == '>'
  110. || spec->value[0] == '<') {
  111. /* an interrupt spec */
  112. spec->property = NULL;
  113. }
  114. else {
  115. chp = strrchr(spec->path, '/');
  116. if (chp == NULL) {
  117. spec->property = spec->path;
  118. spec->path = strchr(spec->property, '\0');
  119. }
  120. else {
  121. *chp = '\0';
  122. spec->property = chp+1;
  123. }
  124. }
  125. /* and mark the rest as invalid */
  126. spec->name = NULL;
  127. spec->base = NULL;
  128. spec->unit = NULL;
  129. spec->args = NULL;
  130. spec->last_name = NULL;
  131. spec->last_base = NULL;
  132. spec->last_unit = NULL;
  133. spec->last_args = NULL;
  134. return 1;
  135. }
  136. /* given a device specifier break it up into its main components -
  137. path and property name - assuming that the last `device' is a
  138. property name. */
  139. STATIC_INLINE_DEVICE\
  140. (int)
  141. split_property_specifier(device *current,
  142. const char *property_specifier,
  143. name_specifier *spec)
  144. {
  145. if (split_device_specifier(current, property_specifier, spec)) {
  146. if (spec->property == NULL) {
  147. /* force the last name to be a property name */
  148. char *chp = strrchr(spec->path, '/');
  149. if (chp == NULL) {
  150. spec->property = spec->path;
  151. spec->path = strrchr(spec->property, '\0');;
  152. }
  153. else {
  154. *chp = '\0';
  155. spec->property = chp+1;
  156. }
  157. }
  158. return 1;
  159. }
  160. else
  161. return 0;
  162. }
  163. /* device the next device name and split it up, return 0 when no more
  164. names to device */
  165. STATIC_INLINE_TREE\
  166. (int)
  167. split_device_name(name_specifier *spec)
  168. {
  169. char *chp;
  170. /* remember what came before */
  171. spec->last_name = spec->name;
  172. spec->last_base = spec->base;
  173. spec->last_unit = spec->unit;
  174. spec->last_args = spec->args;
  175. /* finished? */
  176. if (spec->path[0] == '\0') {
  177. spec->name = NULL;
  178. spec->base = NULL;
  179. spec->unit = NULL;
  180. spec->args = NULL;
  181. return 0;
  182. }
  183. /* break the current device spec from the path */
  184. spec->name = spec->path;
  185. chp = strchr(spec->name, '/');
  186. if (chp == NULL)
  187. spec->path = strchr(spec->name, '\0');
  188. else {
  189. spec->path = chp+1;
  190. *chp = '\0';
  191. }
  192. /* break out the base */
  193. if (spec->name[0] == '(') {
  194. chp = strchr(spec->name, ')');
  195. if (chp == NULL) {
  196. spec->base = spec->name;
  197. }
  198. else {
  199. *chp = '\0';
  200. spec->base = spec->name + 1;
  201. spec->name = chp + 1;
  202. }
  203. }
  204. else {
  205. spec->base = spec->name;
  206. }
  207. /* now break out the unit */
  208. chp = strchr(spec->name, '@');
  209. if (chp == NULL) {
  210. spec->unit = NULL;
  211. chp = spec->name;
  212. }
  213. else {
  214. *chp = '\0';
  215. chp += 1;
  216. spec->unit = chp;
  217. }
  218. /* finally any args */
  219. chp = strchr(chp, ':');
  220. if (chp == NULL)
  221. spec->args = NULL;
  222. else {
  223. *chp = '\0';
  224. spec->args = chp+1;
  225. }
  226. return 1;
  227. }
  228. /* device the value, returning the next non-space token */
  229. STATIC_INLINE_TREE\
  230. (char *)
  231. split_value(name_specifier *spec)
  232. {
  233. char *token;
  234. if (spec->value == NULL)
  235. return NULL;
  236. /* skip leading white space */
  237. while (isspace(spec->value[0]))
  238. spec->value++;
  239. if (spec->value[0] == '\0') {
  240. spec->value = NULL;
  241. return NULL;
  242. }
  243. token = spec->value;
  244. /* find trailing space */
  245. while (spec->value[0] != '\0' && !isspace(spec->value[0]))
  246. spec->value++;
  247. /* chop this value out */
  248. if (spec->value[0] != '\0') {
  249. spec->value[0] = '\0';
  250. spec->value++;
  251. }
  252. return token;
  253. }
  254. /* traverse the path specified by spec starting at current */
  255. STATIC_INLINE_TREE\
  256. (device *)
  257. split_find_device(device *current,
  258. name_specifier *spec)
  259. {
  260. /* strip off (and process) any leading ., .., ./ and / */
  261. while (1) {
  262. if (strncmp(spec->path, "/", strlen("/")) == 0) {
  263. /* cd /... */
  264. while (current != NULL && device_parent(current) != NULL)
  265. current = device_parent(current);
  266. spec->path += strlen("/");
  267. }
  268. else if (strncmp(spec->path, "./", strlen("./")) == 0) {
  269. /* cd ./... */
  270. current = current;
  271. spec->path += strlen("./");
  272. }
  273. else if (strncmp(spec->path, "../", strlen("../")) == 0) {
  274. /* cd ../... */
  275. if (current != NULL && device_parent(current) != NULL)
  276. current = device_parent(current);
  277. spec->path += strlen("../");
  278. }
  279. else if (strcmp(spec->path, ".") == 0) {
  280. /* cd . */
  281. current = current;
  282. spec->path += strlen(".");
  283. }
  284. else if (strcmp(spec->path, "..") == 0) {
  285. /* cd . */
  286. if (current != NULL && device_parent(current) != NULL)
  287. current = device_parent(current);
  288. spec->path += strlen("..");
  289. }
  290. else
  291. break;
  292. }
  293. /* now go through the path proper */
  294. if (current == NULL) {
  295. split_device_name(spec);
  296. return NULL;
  297. }
  298. while (split_device_name(spec)) {
  299. device *child;
  300. for (child = device_child(current);
  301. child != NULL; child = device_sibling(child)) {
  302. if (strcmp(spec->name, device_name(child)) == 0) {
  303. if (spec->unit == NULL)
  304. break;
  305. else {
  306. device_unit phys;
  307. device_decode_unit(current, spec->unit, &phys);
  308. if (memcmp(&phys, device_unit_address(child),
  309. sizeof(device_unit)) == 0)
  310. break;
  311. }
  312. }
  313. }
  314. if (child == NULL)
  315. return current; /* search failed */
  316. current = child;
  317. }
  318. return current;
  319. }
  320. STATIC_INLINE_TREE\
  321. (device *)
  322. split_fill_path(device *current,
  323. const char *device_specifier,
  324. name_specifier *spec)
  325. {
  326. /* break it up */
  327. if (!split_device_specifier(current, device_specifier, spec))
  328. device_error(current, "error parsing %s\n", device_specifier);
  329. /* fill our tree with its contents */
  330. current = split_find_device(current, spec);
  331. /* add any additional devices as needed */
  332. if (spec->name != NULL) {
  333. do {
  334. current = device_create(current, spec->base, spec->name,
  335. spec->unit, spec->args);
  336. } while (split_device_name(spec));
  337. }
  338. return current;
  339. }
  340. INLINE_TREE\
  341. (void)
  342. tree_init(device *root,
  343. psim *system)
  344. {
  345. TRACE(trace_device_tree, ("tree_init(root=%p, system=%p)\n",
  346. root,
  347. system));
  348. /* remove the old, rebuild the new */
  349. tree_traverse(root, device_clean, NULL, system);
  350. tree_traverse(root, device_init_static_properties, NULL, system);
  351. tree_traverse(root, device_init_address, NULL, system);
  352. tree_traverse(root, device_init_runtime_properties, NULL, system);
  353. tree_traverse(root, device_init_data, NULL, system);
  354. }
  355. /* <non-white-space> */
  356. STATIC_INLINE_TREE\
  357. (const char *)
  358. skip_token(const char *chp)
  359. {
  360. while (!isspace(*chp) && *chp != '\0')
  361. chp++;
  362. while (isspace(*chp) && *chp != '\0')
  363. chp++;
  364. return chp;
  365. }
  366. /* count the number of entries */
  367. STATIC_INLINE_TREE\
  368. (int)
  369. count_entries(device *current,
  370. const char *property_name,
  371. const char *property_value,
  372. int modulo)
  373. {
  374. const char *chp = property_value;
  375. int nr_entries = 0;
  376. while (*chp != '\0') {
  377. nr_entries += 1;
  378. chp = skip_token(chp);
  379. }
  380. if ((nr_entries % modulo) != 0) {
  381. device_error(current, "incorrect number of entries for %s property %s, should be multiple of %d",
  382. property_name, property_value, modulo);
  383. }
  384. return nr_entries / modulo;
  385. }
  386. /* parse: <address> ::= <token> ; device dependant */
  387. STATIC_INLINE_TREE\
  388. (const char *)
  389. parse_address(device *current,
  390. device *bus,
  391. const char *chp,
  392. device_unit *address)
  393. {
  394. ASSERT(device_nr_address_cells(bus) > 0);
  395. if (device_decode_unit(bus, chp, address) < 0)
  396. device_error(current, "invalid unit address in %s", chp);
  397. return skip_token(chp);
  398. }
  399. /* parse: <size> ::= <number> { "," <number> } ; */
  400. STATIC_INLINE_TREE\
  401. (const char *)
  402. parse_size(device *current,
  403. device *bus,
  404. const char *chp,
  405. device_unit *size)
  406. {
  407. int i;
  408. int nr;
  409. const char *curr = chp;
  410. memset(size, 0, sizeof(*size));
  411. /* parse the numeric list */
  412. size->nr_cells = device_nr_size_cells(bus);
  413. nr = 0;
  414. ASSERT(size->nr_cells > 0);
  415. while (1) {
  416. char *next;
  417. size->cells[nr] = strtoul(curr, &next, 0);
  418. if (curr == next)
  419. device_error(current, "Problem parsing <size> %s", chp);
  420. nr += 1;
  421. if (next[0] != ',')
  422. break;
  423. if (nr == size->nr_cells)
  424. device_error(current, "Too many values in <size> %s", chp);
  425. curr = next + 1;
  426. }
  427. ASSERT(nr > 0 && nr <= size->nr_cells);
  428. /* right align the numbers */
  429. for (i = 1; i <= size->nr_cells; i++) {
  430. if (i <= nr)
  431. size->cells[size->nr_cells - i] = size->cells[nr - i];
  432. else
  433. size->cells[size->nr_cells - i] = 0;
  434. }
  435. return skip_token(chp);
  436. }
  437. /* parse: <reg> ::= { <address> <size> } ; */
  438. STATIC_INLINE_TREE\
  439. (void)
  440. parse_reg_property(device *current,
  441. const char *property_name,
  442. const char *property_value)
  443. {
  444. int nr_regs;
  445. int reg_nr;
  446. reg_property_spec *regs;
  447. const char *chp;
  448. device *bus = device_parent(current);
  449. /* determine the number of reg entries by counting tokens */
  450. nr_regs = count_entries(current, property_name, property_value,
  451. 1 + (device_nr_size_cells(bus) > 0));
  452. /* create working space */
  453. regs = zalloc(nr_regs * sizeof(*regs));
  454. /* fill it in */
  455. chp = property_value;
  456. for (reg_nr = 0; reg_nr < nr_regs; reg_nr++) {
  457. chp = parse_address(current, bus, chp, &regs[reg_nr].address);
  458. if (device_nr_size_cells(bus) > 0)
  459. chp = parse_size(current, bus, chp, &regs[reg_nr].size);
  460. else
  461. memset(&regs[reg_nr].size, 0, sizeof (regs[reg_nr].size));
  462. }
  463. /* create it */
  464. device_add_reg_array_property(current, property_name,
  465. regs, nr_regs);
  466. free(regs);
  467. }
  468. /* { <child-address> <parent-address> <child-size> }* */
  469. STATIC_INLINE_TREE\
  470. (void)
  471. parse_ranges_property(device *current,
  472. const char *property_name,
  473. const char *property_value)
  474. {
  475. int nr_ranges;
  476. int range_nr;
  477. range_property_spec *ranges;
  478. const char *chp;
  479. /* determine the number of ranges specified */
  480. nr_ranges = count_entries(current, property_name, property_value, 3);
  481. /* create a property of that size */
  482. ranges = zalloc(nr_ranges * sizeof(*ranges));
  483. /* fill it in */
  484. chp = property_value;
  485. for (range_nr = 0; range_nr < nr_ranges; range_nr++) {
  486. chp = parse_address(current, current,
  487. chp, &ranges[range_nr].child_address);
  488. chp = parse_address(current, device_parent(current),
  489. chp, &ranges[range_nr].parent_address);
  490. chp = parse_size(current, current,
  491. chp, &ranges[range_nr].size);
  492. }
  493. /* create it */
  494. device_add_range_array_property(current, property_name, ranges, nr_ranges);
  495. free(ranges);
  496. }
  497. /* <integer> ... */
  498. STATIC_INLINE_TREE\
  499. (void)
  500. parse_integer_property(device *current,
  501. const char *property_name,
  502. const char *property_value)
  503. {
  504. int nr_entries;
  505. unsigned_cell words[1024];
  506. /* integer or integer array? */
  507. nr_entries = 0;
  508. while (1) {
  509. char *end;
  510. words[nr_entries] = strtoul(property_value, &end, 0);
  511. if (property_value == end)
  512. break;
  513. nr_entries += 1;
  514. if (nr_entries * sizeof(words[0]) >= sizeof(words))
  515. device_error(current, "buffer overflow");
  516. property_value = end;
  517. }
  518. if (nr_entries == 0)
  519. device_error(current, "error parsing integer property %s (%s)",
  520. property_name, property_value);
  521. else if (nr_entries == 1)
  522. device_add_integer_property(current, property_name, words[0]);
  523. else {
  524. int i;
  525. for (i = 0; i < nr_entries; i++) {
  526. H2BE(words[i]);
  527. }
  528. /* perhaps integer array property is better */
  529. device_add_array_property(current, property_name, words,
  530. sizeof(words[0]) * nr_entries);
  531. }
  532. }
  533. /* PROPERTY_VALUE is a raw property value. Quote it as required by
  534. parse_string_property. It is the caller's responsibility to free
  535. the memory returned. */
  536. EXTERN_TREE\
  537. (char *)
  538. tree_quote_property(const char *property_value)
  539. {
  540. char *p;
  541. char *ret;
  542. const char *chp;
  543. int quotees;
  544. /* Count characters needing quotes in PROPERTY_VALUE. */
  545. quotees = 0;
  546. for (chp = property_value; *chp; ++chp)
  547. if (*chp == '\\' || *chp == '"')
  548. ++quotees;
  549. ret = (char *) xmalloc (strlen (property_value)
  550. + 2 /* quotes */
  551. + quotees
  552. + 1 /* terminator */);
  553. p = ret;
  554. /* Add the opening quote. */
  555. *p++ = '"';
  556. /* Copy the value. */
  557. for (chp = property_value; *chp; ++chp)
  558. if (*chp == '\\' || *chp == '"')
  559. {
  560. /* Quote this character. */
  561. *p++ = '\\';
  562. *p++ = *chp;
  563. }
  564. else
  565. *p++ = *chp;
  566. /* Add the closing quote. */
  567. *p++ = '"';
  568. /* Terminate the string. */
  569. *p++ = '\0';
  570. return ret;
  571. }
  572. /* <string> ... */
  573. STATIC_INLINE_TREE\
  574. (void)
  575. parse_string_property(device *current,
  576. const char *property_name,
  577. const char *property_value)
  578. {
  579. char **strings;
  580. const char *chp;
  581. int nr_strings;
  582. int approx_nr_strings;
  583. /* get an estimate as to the number of strings by counting double
  584. quotes */
  585. approx_nr_strings = 2;
  586. for (chp = property_value; *chp; chp++) {
  587. if (*chp == '"')
  588. approx_nr_strings++;
  589. }
  590. approx_nr_strings = (approx_nr_strings) / 2;
  591. /* create a string buffer for that many (plus a null) */
  592. strings = (char**)zalloc((approx_nr_strings + 1) * sizeof(char*));
  593. /* now find all the strings */
  594. chp = property_value;
  595. nr_strings = 0;
  596. while (1) {
  597. /* skip leading space */
  598. while (*chp != '\0' && isspace(*chp))
  599. chp += 1;
  600. if (*chp == '\0')
  601. break;
  602. /* copy it in */
  603. if (*chp == '"') {
  604. /* a quoted string - watch for '\' et.al. */
  605. /* estimate the size and allocate space for it */
  606. int pos;
  607. chp++;
  608. pos = 0;
  609. while (chp[pos] != '\0' && chp[pos] != '"') {
  610. if (chp[pos] == '\\' && chp[pos+1] != '\0')
  611. pos += 2;
  612. else
  613. pos += 1;
  614. }
  615. strings[nr_strings] = zalloc(pos + 1);
  616. /* copy the string over */
  617. pos = 0;
  618. while (*chp != '\0' && *chp != '"') {
  619. if (*chp == '\\' && *(chp+1) != '\0') {
  620. strings[nr_strings][pos] = *(chp+1);
  621. chp += 2;
  622. pos++;
  623. }
  624. else {
  625. strings[nr_strings][pos] = *chp;
  626. chp += 1;
  627. pos++;
  628. }
  629. }
  630. if (*chp != '\0')
  631. chp++;
  632. strings[nr_strings][pos] = '\0';
  633. }
  634. else {
  635. /* copy over a single unquoted token */
  636. int len = 0;
  637. while (chp[len] != '\0' && !isspace(chp[len]))
  638. len++;
  639. strings[nr_strings] = zalloc(len + 1);
  640. strncpy(strings[nr_strings], chp, len);
  641. strings[nr_strings][len] = '\0';
  642. chp += len;
  643. }
  644. nr_strings++;
  645. if (nr_strings > approx_nr_strings)
  646. device_error(current, "String property %s badly formatted",
  647. property_name);
  648. }
  649. ASSERT(strings[nr_strings] == NULL); /* from zalloc */
  650. /* install it */
  651. if (nr_strings == 0)
  652. device_add_string_property(current, property_name, "");
  653. else if (nr_strings == 1)
  654. device_add_string_property(current, property_name, strings[0]);
  655. else {
  656. const char **specs = (const char**)strings; /* stop a bogus error */
  657. device_add_string_array_property(current, property_name,
  658. specs, nr_strings);
  659. }
  660. /* flush the created string */
  661. while (nr_strings > 0) {
  662. nr_strings--;
  663. free(strings[nr_strings]);
  664. }
  665. free(strings);
  666. }
  667. /* <path-to-ihandle-device> */
  668. STATIC_INLINE_TREE\
  669. (void)
  670. parse_ihandle_property(device *current,
  671. const char *property,
  672. const char *value)
  673. {
  674. ihandle_runtime_property_spec ihandle;
  675. /* pass the full path */
  676. ihandle.full_path = value;
  677. /* save this ready for the ihandle create */
  678. device_add_ihandle_runtime_property(current, property,
  679. &ihandle);
  680. }
  681. EXTERN_TREE\
  682. (device *)
  683. tree_parse(device *current,
  684. const char *fmt,
  685. ...)
  686. {
  687. char device_specifier[1024];
  688. name_specifier spec;
  689. /* format the path */
  690. {
  691. va_list ap;
  692. va_start(ap, fmt);
  693. vsprintf(device_specifier, fmt, ap);
  694. va_end(ap);
  695. if (strlen(device_specifier) >= sizeof(device_specifier))
  696. error("device_tree_add_deviced: buffer overflow\n");
  697. }
  698. /* construct the tree down to the final device */
  699. current = split_fill_path(current, device_specifier, &spec);
  700. /* is there an interrupt spec */
  701. if (spec.property == NULL
  702. && spec.value != NULL) {
  703. char *op = split_value(&spec);
  704. switch (op[0]) {
  705. case '>':
  706. {
  707. char *my_port_name = split_value(&spec);
  708. int my_port;
  709. char *dest_port_name = split_value(&spec);
  710. int dest_port;
  711. name_specifier dest_spec;
  712. char *dest_device_name = split_value(&spec);
  713. device *dest;
  714. /* find my name */
  715. my_port = device_interrupt_decode(current, my_port_name,
  716. output_port);
  717. /* find the dest device and port */
  718. dest = split_fill_path(current, dest_device_name, &dest_spec);
  719. dest_port = device_interrupt_decode(dest, dest_port_name,
  720. input_port);
  721. /* connect the two */
  722. device_interrupt_attach(current,
  723. my_port,
  724. dest,
  725. dest_port,
  726. permenant_object);
  727. }
  728. break;
  729. default:
  730. device_error(current, "unreconised interrupt spec %s\n", spec.value);
  731. break;
  732. }
  733. }
  734. /* is there a property */
  735. if (spec.property != NULL) {
  736. if (strcmp(spec.value, "true") == 0)
  737. device_add_boolean_property(current, spec.property, 1);
  738. else if (strcmp(spec.value, "false") == 0)
  739. device_add_boolean_property(current, spec.property, 0);
  740. else {
  741. const device_property *property;
  742. switch (spec.value[0]) {
  743. case '*':
  744. parse_ihandle_property(current, spec.property, spec.value + 1);
  745. break;
  746. case '[':
  747. {
  748. uint8_t words[1024];
  749. char *curr = spec.value + 1;
  750. int nr_words = 0;
  751. while (1) {
  752. char *next;
  753. words[nr_words] = H2BE_1(strtoul(curr, &next, 0));
  754. if (curr == next)
  755. break;
  756. curr = next;
  757. nr_words += 1;
  758. }
  759. device_add_array_property(current, spec.property,
  760. words, sizeof(words[0]) * nr_words);
  761. }
  762. break;
  763. case '"':
  764. parse_string_property(current, spec.property, spec.value);
  765. break;
  766. case '!':
  767. spec.value++;
  768. property = tree_find_property(current, spec.value);
  769. if (property == NULL)
  770. device_error(current, "property %s not found\n", spec.value);
  771. device_add_duplicate_property(current,
  772. spec.property,
  773. property);
  774. break;
  775. default:
  776. if (strcmp(spec.property, "reg") == 0
  777. || strcmp(spec.property, "assigned-addresses") == 0
  778. || strcmp(spec.property, "alternate-reg") == 0){
  779. parse_reg_property(current, spec.property, spec.value);
  780. }
  781. else if (strcmp(spec.property, "ranges") == 0) {
  782. parse_ranges_property(current, spec.property, spec.value);
  783. }
  784. else if (isdigit(spec.value[0])
  785. || (spec.value[0] == '-' && isdigit(spec.value[1]))
  786. || (spec.value[0] == '+' && isdigit(spec.value[1]))) {
  787. parse_integer_property(current, spec.property, spec.value);
  788. }
  789. else
  790. parse_string_property(current, spec.property, spec.value);
  791. break;
  792. }
  793. }
  794. }
  795. return current;
  796. }
  797. INLINE_TREE\
  798. (void)
  799. tree_traverse(device *root,
  800. tree_traverse_function *prefix,
  801. tree_traverse_function *postfix,
  802. void *data)
  803. {
  804. device *child;
  805. if (prefix != NULL)
  806. prefix(root, data);
  807. for (child = device_child(root);
  808. child != NULL;
  809. child = device_sibling(child)) {
  810. tree_traverse(child, prefix, postfix, data);
  811. }
  812. if (postfix != NULL)
  813. postfix(root, data);
  814. }
  815. STATIC_INLINE_TREE\
  816. (void)
  817. print_address(device *bus,
  818. const device_unit *phys)
  819. {
  820. char unit[32];
  821. device_encode_unit(bus, phys, unit, sizeof(unit));
  822. printf_filtered(" %s", unit);
  823. }
  824. STATIC_INLINE_TREE\
  825. (void)
  826. print_size(device *bus,
  827. const device_unit *size)
  828. {
  829. int i;
  830. for (i = 0; i < size->nr_cells; i++)
  831. if (size->cells[i] != 0)
  832. break;
  833. if (i < size->nr_cells) {
  834. printf_filtered(" 0x%lx", (unsigned long)size->cells[i]);
  835. i++;
  836. for (; i < size->nr_cells; i++)
  837. printf_filtered(",0x%lx", (unsigned long)size->cells[i]);
  838. }
  839. else
  840. printf_filtered(" 0");
  841. }
  842. STATIC_INLINE_TREE\
  843. (void)
  844. print_reg_property(device *me,
  845. const device_property *property)
  846. {
  847. int reg_nr;
  848. reg_property_spec reg;
  849. for (reg_nr = 0;
  850. device_find_reg_array_property(me, property->name, reg_nr, &reg);
  851. reg_nr++) {
  852. print_address(device_parent(me), &reg.address);
  853. print_size(me, &reg.size);
  854. }
  855. }
  856. STATIC_INLINE_TREE\
  857. (void)
  858. print_ranges_property(device *me,
  859. const device_property *property)
  860. {
  861. int range_nr;
  862. range_property_spec range;
  863. for (range_nr = 0;
  864. device_find_range_array_property(me, property->name, range_nr, &range);
  865. range_nr++) {
  866. print_address(me, &range.child_address);
  867. print_address(device_parent(me), &range.parent_address);
  868. print_size(me, &range.size);
  869. }
  870. }
  871. STATIC_INLINE_TREE\
  872. (void)
  873. print_string(const char *string)
  874. {
  875. printf_filtered(" \"");
  876. while (*string != '\0') {
  877. switch (*string) {
  878. case '"':
  879. printf_filtered("\\\"");
  880. break;
  881. case '\\':
  882. printf_filtered("\\\\");
  883. break;
  884. default:
  885. printf_filtered("%c", *string);
  886. break;
  887. }
  888. string++;
  889. }
  890. printf_filtered("\"");
  891. }
  892. STATIC_INLINE_TREE\
  893. (void)
  894. print_string_array_property(device *me,
  895. const device_property *property)
  896. {
  897. int nr;
  898. string_property_spec string;
  899. for (nr = 0;
  900. device_find_string_array_property(me, property->name, nr, &string);
  901. nr++) {
  902. print_string(string);
  903. }
  904. }
  905. STATIC_INLINE_TREE\
  906. (void)
  907. print_properties(device *me)
  908. {
  909. const device_property *property;
  910. for (property = device_find_property(me, NULL);
  911. property != NULL;
  912. property = device_next_property(property)) {
  913. printf_filtered("%s/%s", device_path(me), property->name);
  914. if (property->original != NULL) {
  915. printf_filtered(" !");
  916. printf_filtered("%s/%s",
  917. device_path(property->original->owner),
  918. property->original->name);
  919. }
  920. else {
  921. switch (property->type) {
  922. case array_property:
  923. if ((property->sizeof_array % sizeof(signed_cell)) == 0) {
  924. unsigned_cell *w = (unsigned_cell*)property->array;
  925. int cell_nr;
  926. for (cell_nr = 0;
  927. cell_nr < (property->sizeof_array / sizeof(unsigned_cell));
  928. cell_nr++) {
  929. printf_filtered(" 0x%lx", (unsigned long)BE2H_cell(w[cell_nr]));
  930. }
  931. }
  932. else {
  933. uint8_t *w = (uint8_t*)property->array;
  934. printf_filtered(" [");
  935. while ((char*)w - (char*)property->array < property->sizeof_array) {
  936. printf_filtered(" 0x%2x", BE2H_1(*w));
  937. w++;
  938. }
  939. }
  940. break;
  941. case boolean_property:
  942. {
  943. int b = device_find_boolean_property(me, property->name);
  944. printf_filtered(" %s", b ? "true" : "false");
  945. }
  946. break;
  947. case ihandle_property:
  948. {
  949. if (property->array != NULL) {
  950. device_instance *instance = device_find_ihandle_property(me, property->name);
  951. printf_filtered(" *%s", device_instance_path(instance));
  952. }
  953. else {
  954. /* not yet initialized, ask the device for the path */
  955. ihandle_runtime_property_spec spec;
  956. device_find_ihandle_runtime_property(me, property->name, &spec);
  957. printf_filtered(" *%s", spec.full_path);
  958. }
  959. }
  960. break;
  961. case integer_property:
  962. {
  963. unsigned_word w = device_find_integer_property(me, property->name);
  964. printf_filtered(" 0x%lx", (unsigned long)w);
  965. }
  966. break;
  967. case range_array_property:
  968. print_ranges_property(me, property);
  969. break;
  970. case reg_array_property:
  971. print_reg_property(me, property);
  972. break;
  973. case string_property:
  974. {
  975. const char *s = device_find_string_property(me, property->name);
  976. print_string(s);
  977. }
  978. break;
  979. case string_array_property:
  980. print_string_array_property(me, property);
  981. break;
  982. }
  983. }
  984. printf_filtered("\n");
  985. }
  986. }
  987. STATIC_INLINE_TREE\
  988. (void)
  989. print_interrupts(device *me,
  990. int my_port,
  991. device *dest,
  992. int dest_port,
  993. void *ignore_or_null)
  994. {
  995. char src[32];
  996. char dst[32];
  997. device_interrupt_encode(me, my_port, src, sizeof(src), output_port);
  998. device_interrupt_encode(dest, dest_port, dst, sizeof(dst), input_port);
  999. printf_filtered("%s > %s %s %s\n",
  1000. device_path(me),
  1001. src, dst,
  1002. device_path(dest));
  1003. }
  1004. STATIC_INLINE_TREE\
  1005. (void)
  1006. print_device(device *me,
  1007. void *ignore_or_null)
  1008. {
  1009. printf_filtered("%s\n", device_path(me));
  1010. print_properties(me);
  1011. device_interrupt_traverse(me, print_interrupts, NULL);
  1012. }
  1013. INLINE_TREE\
  1014. (void)
  1015. tree_print(device *root)
  1016. {
  1017. tree_traverse(root,
  1018. print_device, NULL,
  1019. NULL);
  1020. }
  1021. INLINE_TREE\
  1022. (void)
  1023. tree_usage(int verbose)
  1024. {
  1025. if (verbose == 1) {
  1026. printf_filtered("\n");
  1027. printf_filtered("A device/property specifier has the form:\n");
  1028. printf_filtered("\n");
  1029. printf_filtered(" /path/to/a/device [ property-value ]\n");
  1030. printf_filtered("\n");
  1031. printf_filtered("and a possible device is\n");
  1032. printf_filtered("\n");
  1033. }
  1034. if (verbose > 1) {
  1035. printf_filtered("\n");
  1036. printf_filtered("A device/property specifier (<spec>) has the format:\n");
  1037. printf_filtered("\n");
  1038. printf_filtered(" <spec> ::= <path> [ <value> ] ;\n");
  1039. printf_filtered(" <path> ::= { <prefix> } { <node> \"/\" } <node> ;\n");
  1040. printf_filtered(" <prefix> ::= ( | \"/\" | \"../\" | \"./\" ) ;\n");
  1041. printf_filtered(" <node> ::= <name> [ \"@\" <unit> ] [ \":\" <args> ] ;\n");
  1042. printf_filtered(" <unit> ::= <number> { \",\" <number> } ;\n");
  1043. printf_filtered("\n");
  1044. printf_filtered("Where:\n");
  1045. printf_filtered("\n");
  1046. printf_filtered(" <name> is the name of a device (list below)\n");
  1047. printf_filtered(" <unit> is the unit-address relative to the parent bus\n");
  1048. printf_filtered(" <args> additional arguments used when creating the device\n");
  1049. printf_filtered(" <value> ::= ( <number> # integer property\n");
  1050. printf_filtered(" | \"[\" { <number> } # array property (byte)\n");
  1051. printf_filtered(" | \"{\" { <number> } # array property (cell)\n");
  1052. printf_filtered(" | [ \"true\" | \"false\" ] # boolean property\n");
  1053. printf_filtered(" | \"*\" <path> # ihandle property\n");
  1054. printf_filtered(" | \"!\" <path> # copy property\n");
  1055. printf_filtered(" | \">\" [ <number> ] <path> # attach interrupt\n");
  1056. printf_filtered(" | \"<\" <path> # attach child interrupt\n");
  1057. printf_filtered(" | \"\\\"\" <text> # string property\n");
  1058. printf_filtered(" | <text> # string property\n");
  1059. printf_filtered(" ) ;\n");
  1060. printf_filtered("\n");
  1061. printf_filtered("And the following are valid device names:\n");
  1062. printf_filtered("\n");
  1063. }
  1064. }
  1065. INLINE_TREE\
  1066. (device_instance *)
  1067. tree_instance(device *root,
  1068. const char *device_specifier)
  1069. {
  1070. /* find the device node */
  1071. device *me;
  1072. name_specifier spec;
  1073. if (!split_device_specifier(root, device_specifier, &spec))
  1074. return NULL;
  1075. me = split_find_device(root, &spec);
  1076. if (spec.name != NULL)
  1077. return NULL;
  1078. /* create the instance */
  1079. return device_create_instance(me, device_specifier, spec.last_args);
  1080. }
  1081. INLINE_TREE\
  1082. (device *)
  1083. tree_find_device(device *root,
  1084. const char *path_to_device)
  1085. {
  1086. device *node;
  1087. name_specifier spec;
  1088. /* parse the path */
  1089. split_device_specifier(root, path_to_device, &spec);
  1090. if (spec.value != NULL)
  1091. return NULL; /* something wierd */
  1092. /* now find it */
  1093. node = split_find_device(root, &spec);
  1094. if (spec.name != NULL)
  1095. return NULL; /* not a leaf */
  1096. return node;
  1097. }
  1098. INLINE_TREE\
  1099. (const device_property *)
  1100. tree_find_property(device *root,
  1101. const char *path_to_property)
  1102. {
  1103. name_specifier spec;
  1104. if (!split_property_specifier(root, path_to_property, &spec))
  1105. device_error(root, "Invalid property path %s", path_to_property);
  1106. root = split_find_device(root, &spec);
  1107. return device_find_property(root, spec.property);
  1108. }
  1109. INLINE_TREE\
  1110. (int)
  1111. tree_find_boolean_property(device *root,
  1112. const char *path_to_property)
  1113. {
  1114. name_specifier spec;
  1115. if (!split_property_specifier(root, path_to_property, &spec))
  1116. device_error(root, "Invalid property path %s", path_to_property);
  1117. root = split_find_device(root, &spec);
  1118. return device_find_boolean_property(root, spec.property);
  1119. }
  1120. INLINE_TREE\
  1121. (signed_cell)
  1122. tree_find_integer_property(device *root,
  1123. const char *path_to_property)
  1124. {
  1125. name_specifier spec;
  1126. if (!split_property_specifier(root, path_to_property, &spec))
  1127. device_error(root, "Invalid property path %s", path_to_property);
  1128. root = split_find_device(root, &spec);
  1129. return device_find_integer_property(root, spec.property);
  1130. }
  1131. INLINE_TREE\
  1132. (device_instance *)
  1133. tree_find_ihandle_property(device *root,
  1134. const char *path_to_property)
  1135. {
  1136. name_specifier spec;
  1137. if (!split_property_specifier(root, path_to_property, &spec))
  1138. device_error(root, "Invalid property path %s", path_to_property);
  1139. root = split_find_device(root, &spec);
  1140. return device_find_ihandle_property(root, spec.property);
  1141. }
  1142. INLINE_TREE\
  1143. (const char *)
  1144. tree_find_string_property(device *root,
  1145. const char *path_to_property)
  1146. {
  1147. name_specifier spec;
  1148. if (!split_property_specifier(root, path_to_property, &spec))
  1149. device_error(root, "Invalid property path %s", path_to_property);
  1150. root = split_find_device(root, &spec);
  1151. return device_find_string_property(root, spec.property);
  1152. }
  1153. #endif /* _PARSE_C_ */