hw-properties.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /* The common simulator framework for GDB, the GNU Debugger.
  2. Copyright 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Andrew Cagney and Red Hat.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "hw-main.h"
  16. #include "hw-base.h"
  17. #include "sim-io.h"
  18. #include "sim-assert.h"
  19. #ifdef HAVE_STRING_H
  20. #include <string.h>
  21. #else
  22. #ifdef HAVE_STRINGS_H
  23. #include <strings.h>
  24. #endif
  25. #endif
  26. /* property entries */
  27. struct hw_property_data
  28. {
  29. struct hw_property_data *next;
  30. struct hw_property *property;
  31. const void *init_array;
  32. unsigned sizeof_init_array;
  33. };
  34. void
  35. create_hw_property_data (struct hw *me)
  36. {
  37. }
  38. void
  39. delete_hw_property_data (struct hw *me)
  40. {
  41. }
  42. /* Device Properties: */
  43. static struct hw_property_data *
  44. find_property_data (struct hw *me,
  45. const char *property)
  46. {
  47. struct hw_property_data *entry;
  48. ASSERT (property != NULL);
  49. entry = me->properties_of_hw;
  50. while (entry != NULL)
  51. {
  52. if (strcmp (entry->property->name, property) == 0)
  53. return entry;
  54. entry = entry->next;
  55. }
  56. return NULL;
  57. }
  58. static void
  59. hw_add_property (struct hw *me,
  60. const char *property,
  61. hw_property_type type,
  62. const void *init_array,
  63. unsigned sizeof_init_array,
  64. const void *array,
  65. unsigned sizeof_array,
  66. const struct hw_property *original,
  67. object_disposition disposition)
  68. {
  69. struct hw_property_data *new_entry = NULL;
  70. struct hw_property *new_value = NULL;
  71. /* find the list end */
  72. struct hw_property_data **insertion_point = &me->properties_of_hw;
  73. while (*insertion_point != NULL)
  74. {
  75. if (strcmp ((*insertion_point)->property->name, property) == 0)
  76. return;
  77. insertion_point = &(*insertion_point)->next;
  78. }
  79. /* create a new value */
  80. new_value = HW_ZALLOC (me, struct hw_property);
  81. new_value->name = (char *) strdup (property);
  82. new_value->type = type;
  83. if (sizeof_array > 0)
  84. {
  85. void *new_array = hw_zalloc (me, sizeof_array);
  86. memcpy (new_array, array, sizeof_array);
  87. new_value->array = new_array;
  88. new_value->sizeof_array = sizeof_array;
  89. }
  90. new_value->owner = me;
  91. new_value->original = original;
  92. new_value->disposition = disposition;
  93. /* insert the value into the list */
  94. new_entry = HW_ZALLOC (me, struct hw_property_data);
  95. *insertion_point = new_entry;
  96. if (sizeof_init_array > 0)
  97. {
  98. void *new_init_array = hw_zalloc (me, sizeof_init_array);
  99. memcpy (new_init_array, init_array, sizeof_init_array);
  100. new_entry->init_array = new_init_array;
  101. new_entry->sizeof_init_array = sizeof_init_array;
  102. }
  103. new_entry->property = new_value;
  104. }
  105. static void
  106. hw_set_property (struct hw *me,
  107. const char *property,
  108. hw_property_type type,
  109. const void *array,
  110. int sizeof_array)
  111. {
  112. /* find the property */
  113. struct hw_property_data *entry = find_property_data (me, property);
  114. if (entry != NULL)
  115. {
  116. /* existing property - update it */
  117. void *new_array = 0;
  118. struct hw_property *value = entry->property;
  119. /* check the type matches */
  120. if (value->type != type)
  121. hw_abort (me, "conflict between type of new and old value for property %s", property);
  122. /* replace its value */
  123. if (value->array != NULL)
  124. hw_free (me, (void*)value->array);
  125. new_array = (sizeof_array > 0
  126. ? hw_zalloc (me, sizeof_array)
  127. : (void*)0);
  128. value->array = new_array;
  129. value->sizeof_array = sizeof_array;
  130. if (sizeof_array > 0)
  131. memcpy (new_array, array, sizeof_array);
  132. return;
  133. }
  134. else
  135. {
  136. /* new property - create it */
  137. hw_add_property (me, property, type,
  138. NULL, 0, array, sizeof_array,
  139. NULL, temporary_object);
  140. }
  141. }
  142. #if 0
  143. static void
  144. clean_hw_properties (struct hw *me)
  145. {
  146. struct hw_property_data **delete_point = &me->properties_of_hw;
  147. while (*delete_point != NULL)
  148. {
  149. struct hw_property_data *current = *delete_point;
  150. switch (current->property->disposition)
  151. {
  152. case permenant_object:
  153. /* zap the current value, will be initialized later */
  154. ASSERT (current->init_array != NULL);
  155. if (current->property->array != NULL)
  156. {
  157. hw_free (me, (void*)current->property->array);
  158. current->property->array = NULL;
  159. }
  160. delete_point = &(*delete_point)->next;
  161. break;
  162. case temporary_object:
  163. /* zap the actual property, was created during simulation run */
  164. ASSERT (current->init_array == NULL);
  165. *delete_point = current->next;
  166. if (current->property->array != NULL)
  167. hw_free (me, (void*)current->property->array);
  168. hw_free (me, current->property);
  169. hw_free (me, current);
  170. break;
  171. }
  172. }
  173. }
  174. #endif
  175. #if 0
  176. void
  177. hw_init_static_properties (SIM_DESC sd,
  178. struct hw *me,
  179. void *data)
  180. {
  181. struct hw_property_data *property;
  182. for (property = me->properties_of_hw;
  183. property != NULL;
  184. property = property->next)
  185. {
  186. ASSERT (property->init_array != NULL);
  187. ASSERT (property->property->array == NULL);
  188. ASSERT (property->property->disposition == permenant_object);
  189. switch (property->property->type)
  190. {
  191. case array_property:
  192. case boolean_property:
  193. case range_array_property:
  194. case reg_array_property:
  195. case string_property:
  196. case string_array_property:
  197. case integer_property:
  198. /* delete the property, and replace it with the original */
  199. hw_set_property (me, property->property->name,
  200. property->property->type,
  201. property->init_array,
  202. property->sizeof_init_array);
  203. break;
  204. #if 0
  205. case ihandle_property:
  206. break;
  207. #endif
  208. }
  209. }
  210. }
  211. #endif
  212. #if 0
  213. void
  214. hw_init_runtime_properties (SIM_DESC sd,
  215. struct hw *me,
  216. void *data)
  217. {
  218. struct hw_property_data *property;
  219. for (property = me->properties_of_hw;
  220. property != NULL;
  221. property = property->next)
  222. {
  223. switch (property->property->disposition)
  224. {
  225. case permenant_object:
  226. switch (property->property->type)
  227. {
  228. #if 0
  229. case ihandle_property:
  230. {
  231. struct hw_instance *ihandle;
  232. ihandle_runtime_property_spec spec;
  233. ASSERT (property->init_array != NULL);
  234. ASSERT (property->property->array == NULL);
  235. hw_find_ihandle_runtime_property (me, property->property->name, &spec);
  236. ihandle = tree_instance (me, spec.full_path);
  237. hw_set_ihandle_property (me, property->property->name, ihandle);
  238. break;
  239. }
  240. #endif
  241. case array_property:
  242. case boolean_property:
  243. case range_array_property:
  244. case integer_property:
  245. case reg_array_property:
  246. case string_property:
  247. case string_array_property:
  248. ASSERT (property->init_array != NULL);
  249. ASSERT (property->property->array != NULL);
  250. break;
  251. }
  252. break;
  253. case temporary_object:
  254. ASSERT (property->init_array == NULL);
  255. ASSERT (property->property->array != NULL);
  256. break;
  257. }
  258. }
  259. }
  260. #endif
  261. const struct hw_property *
  262. hw_next_property (const struct hw_property *property)
  263. {
  264. /* find the property in the list */
  265. struct hw *owner = property->owner;
  266. struct hw_property_data *entry = owner->properties_of_hw;
  267. while (entry != NULL && entry->property != property)
  268. entry = entry->next;
  269. /* now return the following property */
  270. ASSERT (entry != NULL); /* must be a member! */
  271. if (entry->next != NULL)
  272. return entry->next->property;
  273. else
  274. return NULL;
  275. }
  276. const struct hw_property *
  277. hw_find_property (struct hw *me,
  278. const char *property)
  279. {
  280. if (me == NULL)
  281. {
  282. return NULL;
  283. }
  284. else if (property == NULL || strcmp (property, "") == 0)
  285. {
  286. if (me->properties_of_hw == NULL)
  287. return NULL;
  288. else
  289. return me->properties_of_hw->property;
  290. }
  291. else
  292. {
  293. struct hw_property_data *entry = find_property_data (me, property);
  294. if (entry != NULL)
  295. return entry->property;
  296. }
  297. return NULL;
  298. }
  299. void
  300. hw_add_array_property (struct hw *me,
  301. const char *property,
  302. const void *array,
  303. int sizeof_array)
  304. {
  305. hw_add_property (me, property, array_property,
  306. array, sizeof_array, array, sizeof_array,
  307. NULL, permenant_object);
  308. }
  309. void
  310. hw_set_array_property (struct hw *me,
  311. const char *property,
  312. const void *array,
  313. int sizeof_array)
  314. {
  315. hw_set_property (me, property, array_property, array, sizeof_array);
  316. }
  317. const struct hw_property *
  318. hw_find_array_property (struct hw *me,
  319. const char *property)
  320. {
  321. const struct hw_property *node;
  322. node = hw_find_property (me, property);
  323. if (node == NULL)
  324. hw_abort (me, "property \"%s\" not found", property);
  325. if (node->type != array_property)
  326. hw_abort (me, "property \"%s\" of wrong type (array)", property);
  327. return node;
  328. }
  329. void
  330. hw_add_boolean_property (struct hw *me,
  331. const char *property,
  332. int boolean)
  333. {
  334. signed32 new_boolean = (boolean ? -1 : 0);
  335. hw_add_property (me, property, boolean_property,
  336. &new_boolean, sizeof (new_boolean),
  337. &new_boolean, sizeof (new_boolean),
  338. NULL, permenant_object);
  339. }
  340. int
  341. hw_find_boolean_property (struct hw *me,
  342. const char *property)
  343. {
  344. const struct hw_property *node;
  345. unsigned_cell boolean;
  346. node = hw_find_property (me, property);
  347. if (node == NULL)
  348. hw_abort (me, "property \"%s\" not found", property);
  349. if (node->type != boolean_property)
  350. hw_abort (me, "property \"%s\" of wrong type (boolean)", property);
  351. ASSERT (sizeof (boolean) == node->sizeof_array);
  352. memcpy (&boolean, node->array, sizeof (boolean));
  353. return boolean;
  354. }
  355. #if 0
  356. void
  357. hw_add_ihandle_runtime_property (struct hw *me,
  358. const char *property,
  359. const ihandle_runtime_property_spec *ihandle)
  360. {
  361. /* enter the full path as the init array */
  362. hw_add_property (me, property, ihandle_property,
  363. ihandle->full_path, strlen (ihandle->full_path) + 1,
  364. NULL, 0,
  365. NULL, permenant_object);
  366. }
  367. #endif
  368. #if 0
  369. void
  370. hw_find_ihandle_runtime_property (struct hw *me,
  371. const char *property,
  372. ihandle_runtime_property_spec *ihandle)
  373. {
  374. struct hw_property_data *entry = find_property_data (me, property);
  375. HW_TRACE ((me, "hw_find_ihandle_runtime_property(property=%s)\n", property));
  376. if (entry == NULL)
  377. hw_abort (me, "property \"%s\" not found", property);
  378. if (entry->property->type != ihandle_property
  379. || entry->property->disposition != permenant_object)
  380. hw_abort (me, "property \"%s\" of wrong type", property);
  381. ASSERT (entry->init_array != NULL);
  382. /* the full path */
  383. ihandle->full_path = entry->init_array;
  384. }
  385. #endif
  386. #if 0
  387. void
  388. hw_set_ihandle_property (struct hw *me,
  389. const char *property,
  390. hw_instance *ihandle)
  391. {
  392. unsigned_cell cells;
  393. cells = H2BE_cell (hw_instance_to_external (ihandle));
  394. hw_set_property (me, property, ihandle_property,
  395. &cells, sizeof (cells));
  396. }
  397. #endif
  398. #if 0
  399. hw_instance *
  400. hw_find_ihandle_property (struct hw *me,
  401. const char *property)
  402. {
  403. const hw_property_data *node;
  404. unsigned_cell ihandle;
  405. hw_instance *instance;
  406. node = hw_find_property (me, property);
  407. if (node == NULL)
  408. hw_abort (me, "property \"%s\" not found", property);
  409. if (node->type != ihandle_property)
  410. hw_abort (me, "property \"%s\" of wrong type (ihandle)", property);
  411. if (node->array == NULL)
  412. hw_abort (me, "runtime property \"%s\" not yet initialized", property);
  413. ASSERT (sizeof (ihandle) == node->sizeof_array);
  414. memcpy (&ihandle, node->array, sizeof (ihandle));
  415. instance = external_to_hw_instance (me, BE2H_cell (ihandle));
  416. ASSERT (instance != NULL);
  417. return instance;
  418. }
  419. #endif
  420. void
  421. hw_add_integer_property (struct hw *me,
  422. const char *property,
  423. signed_cell integer)
  424. {
  425. H2BE (integer);
  426. hw_add_property (me, property, integer_property,
  427. &integer, sizeof (integer),
  428. &integer, sizeof (integer),
  429. NULL, permenant_object);
  430. }
  431. signed_cell
  432. hw_find_integer_property (struct hw *me,
  433. const char *property)
  434. {
  435. const struct hw_property *node;
  436. signed_cell integer;
  437. HW_TRACE ((me, "hw_find_integer(property=%s)\n", property));
  438. node = hw_find_property (me, property);
  439. if (node == NULL)
  440. hw_abort (me, "property \"%s\" not found", property);
  441. if (node->type != integer_property)
  442. hw_abort (me, "property \"%s\" of wrong type (integer)", property);
  443. ASSERT (sizeof (integer) == node->sizeof_array);
  444. memcpy (&integer, node->array, sizeof (integer));
  445. return BE2H_cell (integer);
  446. }
  447. int
  448. hw_find_integer_array_property (struct hw *me,
  449. const char *property,
  450. unsigned index,
  451. signed_cell *integer)
  452. {
  453. const struct hw_property *node;
  454. int sizeof_integer = sizeof (*integer);
  455. signed_cell *cell;
  456. HW_TRACE ((me, "hw_find_integer(property=%s)\n", property));
  457. /* check things sane */
  458. node = hw_find_property (me, property);
  459. if (node == NULL)
  460. hw_abort (me, "property \"%s\" not found", property);
  461. if (node->type != integer_property
  462. && node->type != array_property)
  463. hw_abort (me, "property \"%s\" of wrong type (integer or array)", property);
  464. if ((node->sizeof_array % sizeof_integer) != 0)
  465. hw_abort (me, "property \"%s\" contains an incomplete number of cells", property);
  466. if (node->sizeof_array <= sizeof_integer * index)
  467. return 0;
  468. /* Find and convert the value */
  469. cell = ((signed_cell*)node->array) + index;
  470. *integer = BE2H_cell (*cell);
  471. return node->sizeof_array / sizeof_integer;
  472. }
  473. static unsigned_cell *
  474. unit_address_to_cells (const hw_unit *unit,
  475. unsigned_cell *cell,
  476. int nr_cells)
  477. {
  478. int i;
  479. ASSERT (nr_cells == unit->nr_cells);
  480. for (i = 0; i < unit->nr_cells; i++)
  481. {
  482. *cell = H2BE_cell (unit->cells[i]);
  483. cell += 1;
  484. }
  485. return cell;
  486. }
  487. static const unsigned_cell *
  488. cells_to_unit_address (const unsigned_cell *cell,
  489. hw_unit *unit,
  490. int nr_cells)
  491. {
  492. int i;
  493. memset (unit, 0, sizeof (*unit));
  494. unit->nr_cells = nr_cells;
  495. for (i = 0; i < unit->nr_cells; i++)
  496. {
  497. unit->cells[i] = BE2H_cell (*cell);
  498. cell += 1;
  499. }
  500. return cell;
  501. }
  502. static unsigned
  503. nr_range_property_cells (struct hw *me,
  504. int nr_ranges)
  505. {
  506. return ((hw_unit_nr_address_cells (me)
  507. + hw_unit_nr_address_cells (hw_parent (me))
  508. + hw_unit_nr_size_cells (me))
  509. ) * nr_ranges;
  510. }
  511. void
  512. hw_add_range_array_property (struct hw *me,
  513. const char *property,
  514. const range_property_spec *ranges,
  515. unsigned nr_ranges)
  516. {
  517. unsigned sizeof_cells = (nr_range_property_cells (me, nr_ranges)
  518. * sizeof (unsigned_cell));
  519. unsigned_cell *cells = hw_zalloc (me, sizeof_cells);
  520. unsigned_cell *cell;
  521. int i;
  522. /* copy the property elements over */
  523. cell = cells;
  524. for (i = 0; i < nr_ranges; i++)
  525. {
  526. const range_property_spec *range = &ranges[i];
  527. /* copy the child address */
  528. cell = unit_address_to_cells (&range->child_address, cell,
  529. hw_unit_nr_address_cells (me));
  530. /* copy the parent address */
  531. cell = unit_address_to_cells (&range->parent_address, cell,
  532. hw_unit_nr_address_cells (hw_parent (me)));
  533. /* copy the size */
  534. cell = unit_address_to_cells (&range->size, cell,
  535. hw_unit_nr_size_cells (me));
  536. }
  537. ASSERT (cell == &cells[nr_range_property_cells (me, nr_ranges)]);
  538. /* add it */
  539. hw_add_property (me, property, range_array_property,
  540. cells, sizeof_cells,
  541. cells, sizeof_cells,
  542. NULL, permenant_object);
  543. hw_free (me, cells);
  544. }
  545. int
  546. hw_find_range_array_property (struct hw *me,
  547. const char *property,
  548. unsigned index,
  549. range_property_spec *range)
  550. {
  551. const struct hw_property *node;
  552. unsigned sizeof_entry = (nr_range_property_cells (me, 1)
  553. * sizeof (unsigned_cell));
  554. const unsigned_cell *cells;
  555. /* locate the property */
  556. node = hw_find_property (me, property);
  557. if (node == NULL)
  558. hw_abort (me, "property \"%s\" not found", property);
  559. if (node->type != range_array_property)
  560. hw_abort (me, "property \"%s\" of wrong type (range array)", property);
  561. /* aligned ? */
  562. if ((node->sizeof_array % sizeof_entry) != 0)
  563. hw_abort (me, "property \"%s\" contains an incomplete number of entries",
  564. property);
  565. /* within bounds? */
  566. if (node->sizeof_array < sizeof_entry * (index + 1))
  567. return 0;
  568. /* find the range of interest */
  569. cells = (unsigned_cell*)((char*)node->array + sizeof_entry * index);
  570. /* copy the child address out - converting as we go */
  571. cells = cells_to_unit_address (cells, &range->child_address,
  572. hw_unit_nr_address_cells (me));
  573. /* copy the parent address out - converting as we go */
  574. cells = cells_to_unit_address (cells, &range->parent_address,
  575. hw_unit_nr_address_cells (hw_parent (me)));
  576. /* copy the size - converting as we go */
  577. cells = cells_to_unit_address (cells, &range->size,
  578. hw_unit_nr_size_cells (me));
  579. return node->sizeof_array / sizeof_entry;
  580. }
  581. static unsigned
  582. nr_reg_property_cells (struct hw *me,
  583. int nr_regs)
  584. {
  585. return (hw_unit_nr_address_cells (hw_parent (me))
  586. + hw_unit_nr_size_cells (hw_parent (me))
  587. ) * nr_regs;
  588. }
  589. void
  590. hw_add_reg_array_property (struct hw *me,
  591. const char *property,
  592. const reg_property_spec *regs,
  593. unsigned nr_regs)
  594. {
  595. unsigned sizeof_cells = (nr_reg_property_cells (me, nr_regs)
  596. * sizeof (unsigned_cell));
  597. unsigned_cell *cells = hw_zalloc (me, sizeof_cells);
  598. unsigned_cell *cell;
  599. int i;
  600. /* copy the property elements over */
  601. cell = cells;
  602. for (i = 0; i < nr_regs; i++)
  603. {
  604. const reg_property_spec *reg = &regs[i];
  605. /* copy the address */
  606. cell = unit_address_to_cells (&reg->address, cell,
  607. hw_unit_nr_address_cells (hw_parent (me)));
  608. /* copy the size */
  609. cell = unit_address_to_cells (&reg->size, cell,
  610. hw_unit_nr_size_cells (hw_parent (me)));
  611. }
  612. ASSERT (cell == &cells[nr_reg_property_cells (me, nr_regs)]);
  613. /* add it */
  614. hw_add_property (me, property, reg_array_property,
  615. cells, sizeof_cells,
  616. cells, sizeof_cells,
  617. NULL, permenant_object);
  618. hw_free (me, cells);
  619. }
  620. int
  621. hw_find_reg_array_property (struct hw *me,
  622. const char *property,
  623. unsigned index,
  624. reg_property_spec *reg)
  625. {
  626. const struct hw_property *node;
  627. unsigned sizeof_entry = (nr_reg_property_cells (me, 1)
  628. * sizeof (unsigned_cell));
  629. const unsigned_cell *cells;
  630. /* locate the property */
  631. node = hw_find_property (me, property);
  632. if (node == NULL)
  633. hw_abort (me, "property \"%s\" not found", property);
  634. if (node->type != reg_array_property)
  635. hw_abort (me, "property \"%s\" of wrong type (reg array)", property);
  636. /* aligned ? */
  637. if ((node->sizeof_array % sizeof_entry) != 0)
  638. hw_abort (me, "property \"%s\" contains an incomplete number of entries",
  639. property);
  640. /* within bounds? */
  641. if (node->sizeof_array < sizeof_entry * (index + 1))
  642. return 0;
  643. /* find the range of interest */
  644. cells = (unsigned_cell*)((char*)node->array + sizeof_entry * index);
  645. /* copy the address out - converting as we go */
  646. cells = cells_to_unit_address (cells, &reg->address,
  647. hw_unit_nr_address_cells (hw_parent (me)));
  648. /* copy the size out - converting as we go */
  649. cells = cells_to_unit_address (cells, &reg->size,
  650. hw_unit_nr_size_cells (hw_parent (me)));
  651. return node->sizeof_array / sizeof_entry;
  652. }
  653. void
  654. hw_add_string_property (struct hw *me,
  655. const char *property,
  656. const char *string)
  657. {
  658. hw_add_property (me, property, string_property,
  659. string, strlen (string) + 1,
  660. string, strlen (string) + 1,
  661. NULL, permenant_object);
  662. }
  663. const char *
  664. hw_find_string_property (struct hw *me,
  665. const char *property)
  666. {
  667. const struct hw_property *node;
  668. const char *string;
  669. node = hw_find_property (me, property);
  670. if (node == NULL)
  671. hw_abort (me, "property \"%s\" not found", property);
  672. if (node->type != string_property)
  673. hw_abort (me, "property \"%s\" of wrong type (string)", property);
  674. string = node->array;
  675. ASSERT (strlen (string) + 1 == node->sizeof_array);
  676. return string;
  677. }
  678. void
  679. hw_add_string_array_property (struct hw *me,
  680. const char *property,
  681. const string_property_spec *strings,
  682. unsigned nr_strings)
  683. {
  684. int sizeof_array;
  685. int string_nr;
  686. char *array;
  687. char *chp;
  688. if (nr_strings == 0)
  689. hw_abort (me, "property \"%s\" must be non-null", property);
  690. /* total up the size of the needed array */
  691. for (sizeof_array = 0, string_nr = 0;
  692. string_nr < nr_strings;
  693. string_nr ++)
  694. {
  695. sizeof_array += strlen (strings[string_nr]) + 1;
  696. }
  697. /* create the array */
  698. array = (char*) hw_zalloc (me, sizeof_array);
  699. chp = array;
  700. for (string_nr = 0;
  701. string_nr < nr_strings;
  702. string_nr++)
  703. {
  704. strcpy (chp, strings[string_nr]);
  705. chp += strlen (chp) + 1;
  706. }
  707. ASSERT (chp == array + sizeof_array);
  708. /* now enter it */
  709. hw_add_property (me, property, string_array_property,
  710. array, sizeof_array,
  711. array, sizeof_array,
  712. NULL, permenant_object);
  713. }
  714. int
  715. hw_find_string_array_property (struct hw *me,
  716. const char *property,
  717. unsigned index,
  718. string_property_spec *string)
  719. {
  720. const struct hw_property *node;
  721. node = hw_find_property (me, property);
  722. if (node == NULL)
  723. hw_abort (me, "property \"%s\" not found", property);
  724. switch (node->type)
  725. {
  726. default:
  727. hw_abort (me, "property \"%s\" of wrong type", property);
  728. break;
  729. case string_property:
  730. if (index == 0)
  731. {
  732. *string = node->array;
  733. ASSERT (strlen (*string) + 1 == node->sizeof_array);
  734. return 1;
  735. }
  736. break;
  737. case array_property:
  738. if (node->sizeof_array == 0
  739. || ((char*)node->array)[node->sizeof_array - 1] != '\0')
  740. hw_abort (me, "property \"%s\" invalid for string array", property);
  741. /* FALL THROUGH */
  742. case string_array_property:
  743. ASSERT (node->sizeof_array > 0);
  744. ASSERT (((char*)node->array)[node->sizeof_array - 1] == '\0');
  745. {
  746. const char *chp = node->array;
  747. int nr_entries = 0;
  748. /* count the number of strings, keeping an eye out for the one
  749. we're looking for */
  750. *string = chp;
  751. do
  752. {
  753. if (*chp == '\0')
  754. {
  755. /* next string */
  756. nr_entries++;
  757. chp++;
  758. if (nr_entries == index)
  759. *string = chp;
  760. }
  761. else
  762. {
  763. chp++;
  764. }
  765. } while (chp < (char*)node->array + node->sizeof_array);
  766. if (index < nr_entries)
  767. return nr_entries;
  768. else
  769. {
  770. *string = NULL;
  771. return 0;
  772. }
  773. }
  774. break;
  775. }
  776. return 0;
  777. }
  778. void
  779. hw_add_duplicate_property (struct hw *me,
  780. const char *property,
  781. const struct hw_property *original)
  782. {
  783. struct hw_property_data *master;
  784. HW_TRACE ((me, "hw_add_duplicate_property(property=%s, ...)\n", property));
  785. if (original->disposition != permenant_object)
  786. hw_abort (me, "Can only duplicate permenant objects");
  787. /* find the original's master */
  788. master = original->owner->properties_of_hw;
  789. while (master->property != original)
  790. {
  791. master = master->next;
  792. ASSERT (master != NULL);
  793. }
  794. /* now duplicate it */
  795. hw_add_property (me, property,
  796. original->type,
  797. master->init_array, master->sizeof_init_array,
  798. original->array, original->sizeof_array,
  799. original, permenant_object);
  800. }