xml.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /**
  2. * @file xml.c
  3. * @version 1.0
  4. *
  5. * @brief this file is the main source file for the implementation of dynamic registers using xml and several funtionalities for the "pci-tool" line command tool from XML files. the xml part has been implemented using libxml2
  6. *
  7. * @details registers and banks nodes are parsed using xpath expression, but their properties is get by recursively getting all properties nodes. In this sense, if a property changes, the algorithm has to be changed, but if it's registers or banks nodes, just the xpath expression modification should be enough.
  8. Libxml2 considers blank spaces within the XML as node natively and this code as been produced considering blank spaces in the XML files. In case XML files would not contain blank spaces anymore, please change the code.
  9. In case the performance is not good enough, please consider the following : hard code of formulas
  10. */
  11. #define _XOPEN_SOURCE 700
  12. #define _BSD_SOURCE
  13. #define _DEFAULT_SOURCE
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <strings.h>
  18. #include <dirent.h>
  19. #include <errno.h>
  20. #include <alloca.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <libxml/xmlschemastypes.h>
  25. #include <libxml/tree.h>
  26. #include <libxml/parser.h>
  27. #include <libxml/xpath.h>
  28. #include <libxml/xpathInternals.h>
  29. #include "config.h"
  30. #include "pci.h"
  31. #include "bank.h"
  32. #include "register.h"
  33. #include "xml.h"
  34. #include "error.h"
  35. #include "view.h"
  36. #include "py.h"
  37. #include "views/enum.h"
  38. #include "views/transform.h"
  39. #define BANKS_PATH ((xmlChar*)"/model/bank") /**< path to complete nodes of banks */
  40. #define REGISTERS_PATH ((xmlChar*)"./register") /**< all standard registers nodes */
  41. #define BIT_REGISTERS_PATH ((xmlChar*)"./field") /**< all bits registers nodes */
  42. #define REGISTER_VIEWS_PATH ((xmlChar*)"./view") /**< supported register & field views */
  43. #define TRANSFORM_VIEWS_PATH ((xmlChar*)"/model/transform") /**< path to complete nodes of views */
  44. #define ENUM_VIEWS_PATH ((xmlChar*)"/model/enum") /**< path to complete nodes of views */
  45. #define ENUM_ELEMENTS_PATH ((xmlChar*)"./name") /**< all elements in the enum */
  46. #define UNITS_PATH ((xmlChar*)"/model/unit") /**< path to complete nodes of units */
  47. #define UNIT_TRANSFORMS_PATH ((xmlChar*)"./transform") /**< all transforms of the unit */
  48. static const char *pcilib_xml_bank_default_format = "0x%lx";
  49. static const char *pcilib_xml_enum_view_unit = "name";
  50. typedef struct {
  51. pcilib_register_description_t base;
  52. pcilib_register_value_range_t range;
  53. } pcilib_xml_register_description_t;
  54. /*
  55. static xmlNodePtr pcilib_xml_get_parent_bank_node(xmlDocPtr doc, xmlNodePtr node) {
  56. xmlNodePtr bank_node;
  57. bank_node = node->parent->parent;
  58. // bank_description is always a first node according to the XSD schema
  59. return xmlFirstElementChild(bank_node);
  60. }
  61. static xmlNodePtr pcilib_xml_get_parent_register_node(xmlDocPtr doc, xmlNodePtr node) {
  62. return node->parent->parent;
  63. }
  64. */
  65. int pcilib_get_xml_attr(pcilib_t *ctx, pcilib_xml_node_t *node, const char *attr, pcilib_value_t *val) {
  66. xmlAttr *prop;
  67. xmlChar *str;
  68. prop = xmlHasProp(node, BAD_CAST attr);
  69. if ((!prop)||(!prop->children)) return PCILIB_ERROR_NOTFOUND;
  70. str = prop->children->content;
  71. return pcilib_set_value_from_static_string(ctx, val, (const char*)str);
  72. }
  73. static int pcilib_xml_parse_view_reference(pcilib_t *ctx, xmlDocPtr doc, xmlNodePtr node, pcilib_view_reference_t *desc) {
  74. xmlAttr *cur;
  75. char *value, *name;
  76. for (cur = node->properties; cur != NULL; cur = cur->next) {
  77. if(!cur->children) continue;
  78. if(!xmlNodeIsText(cur->children)) continue;
  79. name = (char*)cur->name;
  80. value = (char*)cur->children->content;
  81. if (!strcasecmp(name, "name")) {
  82. desc->name = value;
  83. } else if (!strcasecmp(name, "view")) {
  84. desc->view = value;
  85. }
  86. }
  87. if (!desc->name)
  88. desc->name = desc->view;
  89. return 0;
  90. }
  91. static int pcilib_xml_parse_register(pcilib_t *ctx, pcilib_xml_register_description_t *xml_desc, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node, pcilib_register_bank_description_t *bdesc) {
  92. int err;
  93. pcilib_register_description_t *desc = (pcilib_register_description_t*)xml_desc;
  94. xmlXPathObjectPtr nodes;
  95. xmlNodeSetPtr nodeset;
  96. xmlAttrPtr cur;
  97. char *value, *name;
  98. char *endptr;
  99. for (cur = node->properties; cur != NULL; cur = cur->next) {
  100. if (!cur->children) continue;
  101. if (!xmlNodeIsText(cur->children)) continue;
  102. name = (char*)cur->name;
  103. value = (char*)cur->children->content;
  104. if (!value) continue;
  105. if (!strcasecmp((char*)name, "address")) {
  106. uintptr_t addr = strtol(value, &endptr, 0);
  107. if ((strlen(endptr) > 0)) {
  108. pcilib_error("Invalid address (%s) is specified in the XML register description", value);
  109. return PCILIB_ERROR_INVALID_DATA;
  110. }
  111. desc->addr = addr;
  112. } else if(!strcasecmp(name, "offset")) {
  113. int offset = strtol(value, &endptr, 0);
  114. if ((strlen(endptr) > 0)||(offset < 0)||(offset > (8 * sizeof(pcilib_register_value_t)))) {
  115. pcilib_error("Invalid offset (%s) is specified in the XML register description", value);
  116. return PCILIB_ERROR_INVALID_DATA;
  117. }
  118. desc->offset = (pcilib_register_size_t)offset;
  119. } else if (!strcasecmp(name,"size")) {
  120. int size = strtol(value, &endptr, 0);
  121. if ((strlen(endptr) > 0)||(size <= 0)||(size > (8 * sizeof(pcilib_register_value_t)))) {
  122. pcilib_error("Invalid size (%s) is specified in the XML register description", value);
  123. return PCILIB_ERROR_INVALID_DATA;
  124. }
  125. desc->bits = (pcilib_register_size_t)size;
  126. } else if (!strcasecmp(name, "default")) {
  127. pcilib_register_value_t val = strtol(value, &endptr, 0);
  128. if ((strlen(endptr) > 0)) {
  129. pcilib_error("Invalid default value (%s) is specified in the XML register description", value);
  130. return PCILIB_ERROR_INVALID_DATA;
  131. }
  132. desc->defvalue = val;
  133. } else if (!strcasecmp(name,"min")) {
  134. pcilib_register_value_t min = strtol(value, &endptr, 0);
  135. if ((strlen(endptr) > 0)) {
  136. pcilib_error("Invalid minimum value (%s) is specified in the XML register description", value);
  137. return PCILIB_ERROR_INVALID_DATA;
  138. }
  139. xml_desc->range.min = min;
  140. } else if (!strcasecmp(name, "max")) {
  141. pcilib_register_value_t max = strtol(value, &endptr, 0);
  142. if ((strlen(endptr) > 0)) {
  143. pcilib_error("Invalid minimum value (%s) is specified in the XML register description", value);
  144. return PCILIB_ERROR_INVALID_DATA;
  145. }
  146. xml_desc->range.max = max;
  147. } else if (!strcasecmp((char*)name,"rwmask")) {
  148. if (!strcasecmp(value, "all")) {
  149. desc->rwmask = PCILIB_REGISTER_ALL_BITS;
  150. } else if (!strcasecmp(value, "none")) {
  151. desc->rwmask = 0;
  152. } else {
  153. uintptr_t mask = strtol(value, &endptr, 0);
  154. if ((strlen(endptr) > 0)) {
  155. pcilib_error("Invalid mask (%s) is specified in the XML register description", value);
  156. return PCILIB_ERROR_INVALID_DATA;
  157. }
  158. desc->rwmask = mask;
  159. }
  160. } else if (!strcasecmp(name, "mode")) {
  161. if (!strcasecmp(value, "R")) {
  162. desc->mode = PCILIB_REGISTER_R;
  163. } else if (!strcasecmp(value, "W")) {
  164. desc->mode = PCILIB_REGISTER_W;
  165. } else if (!strcasecmp(value, "RW")) {
  166. desc->mode = PCILIB_REGISTER_RW;
  167. } else if (!strcasecmp(value, "RW1C")) {
  168. desc->mode = PCILIB_REGISTER_RW1C;
  169. } else if (!strcasecmp(value, "W1C")) {
  170. desc->mode = PCILIB_REGISTER_W1C;
  171. } else if (!strcasecmp(value, "RW1I")) {
  172. desc->mode = PCILIB_REGISTER_RW1I;
  173. } else if (!strcasecmp(value, "W1I")) {
  174. desc->mode = PCILIB_REGISTER_W1I;
  175. } else if (!strcasecmp(value, "-")) {
  176. desc->mode = 0;
  177. } else {
  178. pcilib_error("Invalid access mode (%s) is specified in the XML register description", value);
  179. return PCILIB_ERROR_INVALID_DATA;
  180. }
  181. } else if (!strcasecmp(name, "type")) {
  182. if (!strcasecmp(value, "fifo")) {
  183. desc->type = PCILIB_REGISTER_FIFO;
  184. } else {
  185. pcilib_error("Invalid register type (%s) is specified in the XML register description", value);
  186. return PCILIB_ERROR_INVALID_DATA;
  187. }
  188. } else if (!strcasecmp(name,"name")) {
  189. desc->name = value;
  190. } else if (!strcasecmp(name,"description")) {
  191. desc->description = value;
  192. }
  193. }
  194. xpath->node = node;
  195. nodes = xmlXPathEvalExpression(REGISTER_VIEWS_PATH, xpath);
  196. xpath->node = NULL;
  197. if (!nodes) {
  198. xmlErrorPtr xmlerr = xmlGetLastError();
  199. if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", REGISTER_VIEWS_PATH, xmlerr->code, xmlerr->message);
  200. else pcilib_error("Failed to parse XPath expression %s", REGISTER_VIEWS_PATH);
  201. return PCILIB_ERROR_FAILED;
  202. }
  203. nodeset = nodes->nodesetval;
  204. if (!xmlXPathNodeSetIsEmpty(nodeset)) {
  205. int i;
  206. desc->views = (pcilib_view_reference_t*)malloc((nodeset->nodeNr + 1) * sizeof(pcilib_view_reference_t));
  207. if (!desc->views) {
  208. xmlXPathFreeObject(nodes);
  209. pcilib_error("Failed to allocate %zu bytes of memory to store supported register views", (nodeset->nodeNr + 1) * sizeof(char*));
  210. return PCILIB_ERROR_MEMORY;
  211. }
  212. memset(desc->views, 0, (nodeset->nodeNr + 1) * sizeof(pcilib_view_reference_t));
  213. for (i = 0; i < nodeset->nodeNr; i++) {
  214. err = pcilib_xml_parse_view_reference(ctx, doc, nodeset->nodeTab[i], &desc->views[i]);
  215. if (err) {
  216. xmlXPathFreeObject(nodes);
  217. return err;
  218. }
  219. }
  220. }
  221. xmlXPathFreeObject(nodes);
  222. return 0;
  223. }
  224. static int pcilib_xml_create_register(pcilib_t *ctx, pcilib_register_bank_t bank, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) {
  225. int err;
  226. xmlXPathObjectPtr nodes;
  227. xmlNodeSetPtr nodeset;
  228. pcilib_xml_register_description_t desc = {{0}};
  229. pcilib_xml_register_description_t fdesc;
  230. pcilib_register_t reg;
  231. desc.base.bank = ctx->banks[bank].addr;
  232. desc.base.rwmask = PCILIB_REGISTER_ALL_BITS;
  233. desc.base.mode = PCILIB_REGISTER_R;
  234. desc.base.type = PCILIB_REGISTER_STANDARD;
  235. err = pcilib_xml_parse_register(ctx, &desc, xpath, doc, node, &ctx->banks[bank]);
  236. if (err) {
  237. pcilib_error("Error (%i) parsing an XML register", err);
  238. return err;
  239. }
  240. err = pcilib_add_registers(ctx, PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE, 1, &desc.base, &reg);
  241. if (err) {
  242. if (desc.base.views) free(desc.base.views);
  243. pcilib_error("Error (%i) adding a new XML register (%s) to the model", err, desc.base.name);
  244. return err;
  245. }
  246. ctx->register_ctx[reg].xml = node;
  247. memcpy(&ctx->register_ctx[reg].range, &desc.range, sizeof(pcilib_register_value_range_t));
  248. ctx->register_ctx[reg].views = desc.base.views;
  249. xpath->node = node;
  250. nodes = xmlXPathEvalExpression(BIT_REGISTERS_PATH, xpath);
  251. xpath->node = NULL;
  252. if (!nodes) {
  253. xmlErrorPtr xmlerr = xmlGetLastError();
  254. if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", BIT_REGISTERS_PATH, xmlerr->code, xmlerr->message);
  255. else pcilib_error("Failed to parse XPath expression %s", BIT_REGISTERS_PATH);
  256. return PCILIB_ERROR_FAILED;
  257. }
  258. nodeset = nodes->nodesetval;
  259. if (!xmlXPathNodeSetIsEmpty(nodeset)) {
  260. int i;
  261. for (i = 0; i < nodeset->nodeNr; i++) {
  262. memset(&fdesc, 0, sizeof(pcilib_xml_register_description_t));
  263. fdesc.base.bank = desc.base.bank;
  264. fdesc.base.addr = desc.base.addr;
  265. fdesc.base.mode = desc.base.mode;
  266. fdesc.base.rwmask = desc.base.rwmask;
  267. fdesc.base.type = PCILIB_REGISTER_BITS;
  268. err = pcilib_xml_parse_register(ctx, &fdesc, xpath, doc, nodeset->nodeTab[i], &ctx->banks[bank]);
  269. if (err) {
  270. pcilib_error("Error parsing field in the XML register %s", desc.base.name);
  271. continue;
  272. }
  273. err = pcilib_add_registers(ctx, PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE, 1, &fdesc.base, &reg);
  274. if (err) {
  275. if (fdesc.base.views) free(fdesc.base.views);
  276. pcilib_error("Error (%i) adding a new XML register (%s) to the model", err, fdesc.base.name);
  277. continue;
  278. }
  279. ctx->register_ctx[reg].xml = nodeset->nodeTab[i];
  280. memcpy(&ctx->register_ctx[reg].range, &fdesc.range, sizeof(pcilib_register_value_range_t));
  281. ctx->register_ctx[reg].views = fdesc.base.views;
  282. }
  283. }
  284. xmlXPathFreeObject(nodes);
  285. return 0;
  286. }
  287. static int pcilib_xml_create_bank(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) {
  288. int err;
  289. int override = 0;
  290. pcilib_register_bank_description_t desc = {0};
  291. pcilib_register_bank_t bank;
  292. xmlAttrPtr cur;
  293. char *value, *name;
  294. char *endptr;
  295. xmlXPathObjectPtr nodes;
  296. xmlNodeSetPtr nodeset;
  297. desc.format = pcilib_xml_bank_default_format;
  298. desc.addr = PCILIB_REGISTER_BANK_DYNAMIC;
  299. desc.bar = PCILIB_BAR_NOBAR;
  300. desc.size = 0x1000;
  301. desc.protocol = PCILIB_REGISTER_PROTOCOL_DEFAULT;
  302. desc.access = 32;
  303. desc.endianess = PCILIB_HOST_ENDIAN;
  304. desc.raw_endianess = PCILIB_HOST_ENDIAN;
  305. // iterate through all children, representing bank properties, to fill the structure
  306. for (cur = node->properties; cur != NULL; cur = cur->next) {
  307. if (!cur->children) continue;
  308. if (!xmlNodeIsText(cur->children)) continue;
  309. name = (char*)cur->name;
  310. value = (char*)cur->children->content;
  311. if (!value) continue;
  312. if (!strcasecmp(name, "bar")) {
  313. char bar = value[0]-'0';
  314. if ((strlen(value) != 1)||(bar < 0)||(bar > 5)) {
  315. pcilib_error("Invalid BAR (%s) is specified in the XML bank description", value);
  316. return PCILIB_ERROR_INVALID_DATA;
  317. }
  318. desc.bar = (pcilib_bar_t)bar;
  319. override = 1;
  320. } else if (!strcasecmp(name,"size")) {
  321. long size = strtol(value, &endptr, 0);
  322. if ((strlen(endptr) > 0)||(size<=0)) {
  323. pcilib_error("Invalid bank size (%s) is specified in the XML bank description", value);
  324. return PCILIB_ERROR_INVALID_DATA;
  325. }
  326. desc.size = (size_t)size;
  327. override = 1;
  328. } else if (!strcasecmp(name,"protocol")) {
  329. pcilib_register_protocol_t protocol = pcilib_find_register_protocol_by_name(ctx, value);
  330. if (protocol == PCILIB_REGISTER_PROTOCOL_INVALID) {
  331. pcilib_error("Unsupported protocol (%s) is specified in the XML bank description", value);
  332. return PCILIB_ERROR_NOTSUPPORTED;
  333. }
  334. desc.protocol = ctx->protocols[protocol].addr;
  335. override = 1;
  336. } else if (!strcasecmp(name,"address")) {
  337. uintptr_t addr = strtol(value, &endptr, 0);
  338. if ((strlen(endptr) > 0)) {
  339. pcilib_error("Invalid address (%s) is specified in the XML bank description", value);
  340. return PCILIB_ERROR_INVALID_DATA;
  341. }
  342. desc.read_addr = addr;
  343. desc.write_addr = addr;
  344. override = 1;
  345. } else if (!strcasecmp(name,"read_address")) {
  346. uintptr_t addr = strtol(value, &endptr, 0);
  347. if ((strlen(endptr) > 0)) {
  348. pcilib_error("Invalid address (%s) is specified in the XML bank description", value);
  349. return PCILIB_ERROR_INVALID_DATA;
  350. }
  351. desc.read_addr = addr;
  352. override = 1;
  353. } else if (!strcasecmp(name,"write_address")) {
  354. uintptr_t addr = strtol(value, &endptr, 0);
  355. if ((strlen(endptr) > 0)) {
  356. pcilib_error("Invalid address (%s) is specified in the XML bank description", value);
  357. return PCILIB_ERROR_INVALID_DATA;
  358. }
  359. desc.write_addr = addr;
  360. override = 1;
  361. } else if(strcasecmp((char*)name,"word_size")==0) {
  362. int access = strtol(value, &endptr, 0);
  363. if ((strlen(endptr) > 0)||(access%8)||(access<=0)||(access>(8 * sizeof(pcilib_register_value_t)))) {
  364. pcilib_error("Invalid word size (%s) is specified in the XML bank description", value);
  365. return PCILIB_ERROR_INVALID_DATA;
  366. }
  367. desc.access = access;
  368. override = 1;
  369. } else if (!strcasecmp(name,"endianess")) {
  370. if (!strcasecmp(value,"little")) desc.endianess = PCILIB_LITTLE_ENDIAN;
  371. else if (!strcasecmp(value,"big")) desc.endianess = PCILIB_BIG_ENDIAN;
  372. else if (!strcasecmp(value,"host")) desc.endianess = PCILIB_HOST_ENDIAN;
  373. else {
  374. pcilib_error("Invalid endianess (%s) is specified in the XML bank description", value);
  375. return PCILIB_ERROR_INVALID_DATA;
  376. }
  377. override = 1;
  378. } else if (!strcasecmp(name,"format")) {
  379. desc.format = value;
  380. override = 1;
  381. } else if (!strcasecmp((char*)name,"name")) {
  382. desc.name = value;
  383. } else if (!strcasecmp((char*)name,"description")) {
  384. desc.description = value;
  385. override = 1;
  386. } else if (!strcasecmp((char*)name,"override")) {
  387. override = 1;
  388. }
  389. }
  390. err = pcilib_add_register_banks(ctx, override?PCILIB_MODEL_MODIFICATION_FLAG_OVERRIDE:PCILIB_MODEL_MODIFICATION_FLAG_SKIP_EXISTING, 1, &desc, &bank);
  391. if (err) {
  392. pcilib_error("Error adding register bank (%s) specified in the XML bank description", desc.name);
  393. return err;
  394. }
  395. ctx->xml.bank_nodes[bank] = node;
  396. if (ctx->bank_ctx[bank]) {
  397. ctx->bank_ctx[bank]->xml = node;
  398. }
  399. xpath->node = node;
  400. nodes = xmlXPathEvalExpression(REGISTERS_PATH, xpath);
  401. xpath->node = NULL;
  402. if (!nodes) {
  403. xmlErrorPtr xmlerr = xmlGetLastError();
  404. if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", REGISTERS_PATH, xmlerr->code, xmlerr->message);
  405. else pcilib_error("Failed to parse XPath expression %s", REGISTERS_PATH);
  406. return PCILIB_ERROR_FAILED;
  407. }
  408. nodeset = nodes->nodesetval;
  409. if (!xmlXPathNodeSetIsEmpty(nodeset)) {
  410. int i;
  411. for (i = 0; i < nodeset->nodeNr; i++) {
  412. err = pcilib_xml_create_register(ctx, bank, xpath, doc, nodeset->nodeTab[i]);
  413. if (err) pcilib_error("Error creating XML registers for bank %s", desc.name);
  414. }
  415. }
  416. xmlXPathFreeObject(nodes);
  417. return 0;
  418. }
  419. static int pcilib_xml_parse_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node, pcilib_view_description_t *desc) {
  420. xmlAttrPtr cur;
  421. const char *value, *name;
  422. int inconsistent = (desc->mode & PCILIB_ACCESS_INCONSISTENT);
  423. for (cur = node->properties; cur != NULL; cur = cur->next) {
  424. if (!cur->children) continue;
  425. if (!xmlNodeIsText(cur->children)) continue;
  426. name = (char*)cur->name;
  427. value = (char*)cur->children->content;
  428. if (!value) continue;
  429. if (!strcasecmp(name, "name")) {
  430. // Overriden by path
  431. if (desc->name) continue;
  432. if (*value == '/')
  433. desc->flags |= PCILIB_VIEW_FLAG_PROPERTY;
  434. desc->name = value;
  435. } else if (!strcasecmp(name, "path")) {
  436. desc->name = value;
  437. desc->flags |= PCILIB_VIEW_FLAG_PROPERTY;
  438. } else if (!strcasecmp(name, "register")) {
  439. desc->regname = value;
  440. desc->flags |= PCILIB_VIEW_FLAG_REGISTER;
  441. } else if (!strcasecmp((char*)name, "description")) {
  442. desc->description = value;
  443. } else if (!strcasecmp((char*)name, "unit")) {
  444. desc->unit = value;
  445. } else if (!strcasecmp((char*)name, "type")) {
  446. if (!strcasecmp(value, "string")) desc->type = PCILIB_TYPE_STRING;
  447. else if (!strcasecmp(value, "float")) desc->type = PCILIB_TYPE_DOUBLE;
  448. else if (!strcasecmp(value, "int")) desc->type = PCILIB_TYPE_LONG;
  449. else {
  450. pcilib_error("Invalid type (%s) of register view is specified in the XML bank description", value);
  451. return PCILIB_ERROR_INVALID_DATA;
  452. }
  453. } else if (!strcasecmp(name, "mode")) {
  454. if (!strcasecmp(value, "R")) {
  455. desc->mode = PCILIB_REGISTER_R;
  456. } else if (!strcasecmp(value, "W")) {
  457. desc->mode = PCILIB_REGISTER_W;
  458. } else if (!strcasecmp(value, "RW")) {
  459. desc->mode = PCILIB_REGISTER_RW;
  460. } else if (!strcasecmp(value, "-")) {
  461. desc->mode = 0;
  462. } else {
  463. pcilib_error("Invalid access mode (%s) is specified in the XML register description", value);
  464. return PCILIB_ERROR_INVALID_DATA;
  465. }
  466. } else if (!strcasecmp(name, "write_verification")) {
  467. if (strcmp(value, "0")) inconsistent = 0;
  468. else inconsistent = 1;
  469. }
  470. }
  471. if (inconsistent) desc->mode |= PCILIB_ACCESS_INCONSISTENT;
  472. else desc->mode &= ~PCILIB_ACCESS_INCONSISTENT;
  473. return 0;
  474. }
  475. static int pcilib_xml_create_transform_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) {
  476. int err;
  477. xmlAttrPtr cur;
  478. const char *value, *name;
  479. pcilib_view_context_t *view_ctx;
  480. pcilib_access_mode_t mode = 0;
  481. pcilib_transform_view_description_t desc = {{0}};
  482. desc.base.api = &pcilib_transform_view_api;
  483. desc.base.type = PCILIB_TYPE_DOUBLE;
  484. desc.base.mode = PCILIB_ACCESS_RW;
  485. err = pcilib_xml_parse_view(ctx, xpath, doc, node, (pcilib_view_description_t*)&desc);
  486. if (err) return err;
  487. for (cur = node->properties; cur != NULL; cur = cur->next) {
  488. if (!cur->children) continue;
  489. if (!xmlNodeIsText(cur->children)) continue;
  490. name = (char*)cur->name;
  491. value = (char*)cur->children->content;
  492. if (!value) continue;
  493. if (!strcasecmp(name, "read_from_register")) {
  494. if (desc.base.flags&PCILIB_VIEW_FLAG_PROPERTY) {
  495. if (strstr(value, "$value")) {
  496. pcilib_error("Invalid transform specified in XML property (%s). The properties can't reference $value (%s)", desc.base.name, value);
  497. return PCILIB_ERROR_INVALID_DATA;
  498. }
  499. }
  500. desc.read_from_reg = value;
  501. if ((value)&&(*value)) mode |= PCILIB_ACCESS_R;
  502. } else if (!strcasecmp(name, "write_to_register")) {
  503. if (desc.base.flags&PCILIB_VIEW_FLAG_PROPERTY) {
  504. if (strstr(value, "$value")) {
  505. pcilib_error("Invalid transform specified in XML property (%s). The properties can't reference $value (%s)", desc.base.name, value);
  506. return PCILIB_ERROR_INVALID_DATA;
  507. }
  508. }
  509. desc.write_to_reg = value;
  510. if ((value)&&(*value)) mode |= PCILIB_ACCESS_W;
  511. } else if (!strcasecmp(name, "script")) {
  512. desc.script = value;
  513. break;
  514. }
  515. }
  516. desc.base.mode &= (~PCILIB_ACCESS_RW)|mode;
  517. err = pcilib_add_views_custom(ctx, 1, (pcilib_view_description_t*)&desc, &view_ctx);
  518. if (err) return err;
  519. view_ctx->xml = node;
  520. return 0;
  521. }
  522. static int pcilib_xml_parse_value_name(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node, pcilib_register_value_name_t *desc) {
  523. xmlAttr *cur;
  524. char *value, *name;
  525. char *endptr;
  526. int min_set = 0, max_set = 0;
  527. pcilib_register_value_t val;
  528. for (cur = node->properties; cur != NULL; cur = cur->next) {
  529. if(!cur->children) continue;
  530. if(!xmlNodeIsText(cur->children)) continue;
  531. name = (char*)cur->name;
  532. value = (char*)cur->children->content;
  533. if (!strcasecmp(name, "name")) {
  534. desc->name = value;
  535. } else if (!strcasecmp(name, "description")) {
  536. desc->description = value;
  537. } else if (!strcasecmp(name, "value")) {
  538. val = strtol(value, &endptr, 0);
  539. if ((strlen(endptr) > 0)) {
  540. pcilib_error("Invalid enum value (%s) is specified in the XML enum node", value);
  541. return PCILIB_ERROR_INVALID_DATA;
  542. }
  543. desc->value = val;
  544. } else if (!strcasecmp(name, "min")) {
  545. val = strtol(value, &endptr, 0);
  546. if ((strlen(endptr) > 0)) {
  547. pcilib_error("Invalid enum min-value (%s) is specified in the XML enum node", value);
  548. return PCILIB_ERROR_INVALID_DATA;
  549. }
  550. desc->min = val;
  551. min_set = 1;
  552. } else if (!strcasecmp(name, "max")) {
  553. val = strtol(value, &endptr, 0);
  554. if ((strlen(endptr) > 0)) {
  555. pcilib_error("Invalid enum max-value (%s) is specified in the XML enum node", value);
  556. return PCILIB_ERROR_INVALID_DATA;
  557. }
  558. desc->max = val;
  559. max_set = 1;
  560. }
  561. }
  562. if ((!min_set)&&(!max_set)) {
  563. desc->min = desc->value;
  564. desc->max = desc->value;
  565. } else if (max_set) {
  566. desc->min = 0;
  567. } else if (min_set) {
  568. desc->max = (pcilib_register_value_t)-1;
  569. }
  570. if ((desc->min > desc->max)||(desc->value < desc->min)||(desc->value > desc->max)) {
  571. pcilib_error("Invalid enum configuration (min: %lu, max: %lu, value: %lu)", desc->min, desc->max, desc->value);
  572. return PCILIB_ERROR_INVALID_DATA;
  573. }
  574. return 0;
  575. }
  576. static int pcilib_xml_create_enum_view(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) {
  577. int i;
  578. int err;
  579. xmlXPathObjectPtr nodes;
  580. xmlNodeSetPtr nodeset;
  581. pcilib_view_context_t *view_ctx;
  582. pcilib_enum_view_description_t desc = {{0}};
  583. desc.base.type = PCILIB_TYPE_STRING;
  584. desc.base.unit = pcilib_xml_enum_view_unit;
  585. desc.base.api = &pcilib_enum_view_xml_api;
  586. desc.base.mode = PCILIB_ACCESS_RW;
  587. err = pcilib_xml_parse_view(ctx, xpath, doc, node, (pcilib_view_description_t*)&desc);
  588. if (err) return err;
  589. xpath->node = node;
  590. nodes = xmlXPathEvalExpression(ENUM_ELEMENTS_PATH, xpath);
  591. xpath->node = NULL;
  592. if (!nodes) {
  593. xmlErrorPtr xmlerr = xmlGetLastError();
  594. if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", ENUM_ELEMENTS_PATH, xmlerr->code, xmlerr->message);
  595. else pcilib_error("Failed to parse XPath expression %s", ENUM_ELEMENTS_PATH);
  596. return PCILIB_ERROR_FAILED;
  597. }
  598. nodeset = nodes->nodesetval;
  599. if (xmlXPathNodeSetIsEmpty(nodeset)) {
  600. xmlXPathFreeObject(nodes);
  601. pcilib_error("No names is defined for enum view (%s)", desc.base.name);
  602. return PCILIB_ERROR_INVALID_DATA;
  603. }
  604. desc.names = (pcilib_register_value_name_t*)malloc((nodeset->nodeNr + 1) * sizeof(pcilib_register_value_name_t));
  605. if (!desc.names) {
  606. xmlXPathFreeObject(nodes);
  607. pcilib_error("No names is defined for enum view (%s)", desc.base.name);
  608. return PCILIB_ERROR_INVALID_DATA;
  609. }
  610. memset(desc.names, 0, (nodeset->nodeNr + 1) * sizeof(pcilib_register_value_name_t));
  611. for (i = 0; i < nodeset->nodeNr; i++) {
  612. err = pcilib_xml_parse_value_name(ctx, xpath, doc, nodeset->nodeTab[i], &desc.names[i]);
  613. if (err) {
  614. xmlXPathFreeObject(nodes);
  615. free(desc.names);
  616. return err;
  617. }
  618. }
  619. xmlXPathFreeObject(nodes);
  620. err = pcilib_add_views_custom(ctx, 1, (pcilib_view_description_t*)&desc, &view_ctx);
  621. if (err) {
  622. free(desc.names);
  623. return err;
  624. }
  625. view_ctx->xml = node;
  626. return 0;
  627. }
  628. static int pcilib_xml_parse_unit_transform(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node, pcilib_unit_transform_t *desc) {
  629. xmlAttrPtr cur;
  630. char *value, *name;
  631. for (cur = node->properties; cur != NULL; cur = cur->next) {
  632. if (!cur->children) continue;
  633. if (!xmlNodeIsText(cur->children)) continue;
  634. name = (char*)cur->name;
  635. value = (char*)cur->children->content;
  636. if (!strcasecmp(name, "unit")) {
  637. desc->unit = value;
  638. } else if (!strcasecmp(name, "transform")) {
  639. desc->transform = value;
  640. }
  641. }
  642. return 0;
  643. }
  644. /**
  645. * function to create a unit from a unit xml node, then populating ctx with it
  646. *@param[in,out] ctx - the pcilib_t running
  647. *@param[in] xpath - the xpath context of the unis xml file
  648. *@param[in] doc - the AST of the unit xml file
  649. *@param[in] node - the node representing the unit
  650. *@return an error code: 0 if evrythinh is ok
  651. */
  652. static int pcilib_xml_create_unit(pcilib_t *ctx, xmlXPathContextPtr xpath, xmlDocPtr doc, xmlNodePtr node) {
  653. int err;
  654. pcilib_unit_description_t desc = {0};
  655. xmlXPathObjectPtr nodes;
  656. xmlNodeSetPtr nodeset;
  657. xmlAttrPtr cur;
  658. char *value, *name;
  659. for (cur = node->properties; cur != NULL; cur = cur->next) {
  660. if (!cur->children) continue;
  661. if (!xmlNodeIsText(cur->children)) continue;
  662. name = (char*)cur->name;
  663. value = (char*)cur->children->content;
  664. if (!strcasecmp(name, "name")) {
  665. desc.name = value;
  666. }
  667. }
  668. xpath->node = node;
  669. nodes = xmlXPathEvalExpression(UNIT_TRANSFORMS_PATH, xpath);
  670. xpath->node = NULL;
  671. if (!nodes) {
  672. xmlErrorPtr xmlerr = xmlGetLastError();
  673. if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", UNIT_TRANSFORMS_PATH, xmlerr->code, xmlerr->message);
  674. else pcilib_error("Failed to parse XPath expression %s", UNIT_TRANSFORMS_PATH);
  675. return PCILIB_ERROR_FAILED;
  676. }
  677. nodeset = nodes->nodesetval;
  678. if (!xmlXPathNodeSetIsEmpty(nodeset)) {
  679. int i;
  680. if (nodeset->nodeNr > PCILIB_MAX_TRANSFORMS_PER_UNIT) {
  681. xmlXPathFreeObject(nodes);
  682. pcilib_error("Too many transforms for unit %s are defined, only %lu are supported", desc.name, PCILIB_MAX_TRANSFORMS_PER_UNIT);
  683. return PCILIB_ERROR_INVALID_DATA;
  684. }
  685. for (i = 0; i < nodeset->nodeNr; i++) {
  686. err = pcilib_xml_parse_unit_transform(ctx, xpath, doc, nodeset->nodeTab[i], &desc.transforms[i]);
  687. if (err) {
  688. xmlXPathFreeObject(nodes);
  689. return err;
  690. }
  691. }
  692. }
  693. xmlXPathFreeObject(nodes);
  694. return pcilib_add_units(ctx, 1, &desc);
  695. }
  696. /** pcilib_xml_initialize_banks
  697. *
  698. * function to create the structures to store the banks from the AST
  699. * @see pcilib_xml_create_bank
  700. * @param[in] doc the AST of the xml file.
  701. * @param[in] pci the pcilib_t running, which will be filled
  702. */
  703. static int pcilib_xml_process_document(pcilib_t *ctx, xmlDocPtr doc, xmlXPathContextPtr xpath) {
  704. int err;
  705. xmlXPathObjectPtr bank_nodes = NULL, transform_nodes = NULL, enum_nodes = NULL, unit_nodes = NULL;
  706. xmlNodeSetPtr nodeset;
  707. int i;
  708. bank_nodes = xmlXPathEvalExpression(BANKS_PATH, xpath);
  709. if (bank_nodes) transform_nodes = xmlXPathEvalExpression(TRANSFORM_VIEWS_PATH, xpath);
  710. if (transform_nodes) enum_nodes = xmlXPathEvalExpression(ENUM_VIEWS_PATH, xpath);
  711. if (enum_nodes) unit_nodes = xmlXPathEvalExpression(UNITS_PATH, xpath);
  712. if (!unit_nodes) {
  713. const unsigned char *expr = (enum_nodes?UNITS_PATH:(transform_nodes?ENUM_VIEWS_PATH:(bank_nodes?TRANSFORM_VIEWS_PATH:BANKS_PATH)));
  714. if (enum_nodes) xmlXPathFreeObject(enum_nodes);
  715. if (transform_nodes) xmlXPathFreeObject(transform_nodes);
  716. if (bank_nodes) xmlXPathFreeObject(bank_nodes);
  717. xmlErrorPtr xmlerr = xmlGetLastError();
  718. if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", expr, xmlerr->code, xmlerr->message);
  719. else pcilib_error("Failed to parse XPath expression %s", expr);
  720. return PCILIB_ERROR_FAILED;
  721. }
  722. nodeset = unit_nodes->nodesetval;
  723. if(!xmlXPathNodeSetIsEmpty(nodeset)) {
  724. for(i=0; i < nodeset->nodeNr; i++) {
  725. err = pcilib_xml_create_unit(ctx, xpath, doc, nodeset->nodeTab[i]);
  726. if (err) pcilib_error("Error (%i) creating unit", err);
  727. }
  728. }
  729. nodeset = transform_nodes->nodesetval;
  730. if (!xmlXPathNodeSetIsEmpty(nodeset)) {
  731. for(i=0; i < nodeset->nodeNr; i++) {
  732. err = pcilib_xml_create_transform_view(ctx, xpath, doc, nodeset->nodeTab[i]);
  733. if (err) pcilib_error("Error (%i) creating register transform", err);
  734. }
  735. }
  736. nodeset = enum_nodes->nodesetval;
  737. if (!xmlXPathNodeSetIsEmpty(nodeset)) {
  738. for(i=0; i < nodeset->nodeNr; i++) {
  739. err = pcilib_xml_create_enum_view(ctx, xpath, doc, nodeset->nodeTab[i]);
  740. if (err) pcilib_error("Error (%i) creating register enum", err);
  741. }
  742. }
  743. nodeset = bank_nodes->nodesetval;
  744. if (!xmlXPathNodeSetIsEmpty(nodeset)) {
  745. for (i = 0; i < nodeset->nodeNr; i++) {
  746. err = pcilib_xml_create_bank(ctx, xpath, doc, nodeset->nodeTab[i]);
  747. if (err) pcilib_error("Error (%i) creating bank", err);
  748. }
  749. }
  750. xmlXPathFreeObject(unit_nodes);
  751. xmlXPathFreeObject(enum_nodes);
  752. xmlXPathFreeObject(transform_nodes);
  753. xmlXPathFreeObject(bank_nodes);
  754. return 0;
  755. }
  756. static int pcilib_xml_load_xsd_file(pcilib_t *ctx, const char *xsd_filename, xmlSchemaPtr *schema, xmlSchemaValidCtxtPtr *validator) {
  757. int err;
  758. xmlSchemaParserCtxtPtr ctxt;
  759. *schema = NULL;
  760. *validator = NULL;
  761. /** we first parse the xsd file for AST with validation*/
  762. ctxt = xmlSchemaNewParserCtxt(xsd_filename);
  763. if (!ctxt) {
  764. xmlErrorPtr xmlerr = xmlGetLastError();
  765. if (xmlerr) pcilib_error("xmlSchemaNewParserCtxt reported error %d - %s", xmlerr->code, xmlerr->message);
  766. else pcilib_error("Failed to create a parser for XML schemas");
  767. return PCILIB_ERROR_FAILED;
  768. }
  769. *schema = xmlSchemaParse(ctxt);
  770. if (!*schema) {
  771. xmlErrorPtr xmlerr = xmlGetLastError();
  772. xmlSchemaFreeParserCtxt(ctxt);
  773. if (xmlerr) pcilib_error("Failed to parse XML schema, xmlSchemaParse reported error %d - %s", xmlerr->code, xmlerr->message);
  774. else pcilib_error("Failed to parse XML schema");
  775. return PCILIB_ERROR_INVALID_DATA;
  776. }
  777. xmlSchemaFreeParserCtxt(ctxt);
  778. *validator = xmlSchemaNewValidCtxt(*schema);
  779. if (!*validator) {
  780. xmlErrorPtr xmlerr = xmlGetLastError();
  781. xmlSchemaFree(*schema); *schema = NULL;
  782. if (xmlerr) pcilib_error("xmlSchemaNewValidCtxt reported error %d - %s", xmlerr->code, xmlerr->message);
  783. else pcilib_error("Failed to create a validation context");
  784. return PCILIB_ERROR_FAILED;
  785. }
  786. err = xmlSchemaSetValidOptions(*validator, XML_SCHEMA_VAL_VC_I_CREATE);
  787. if (err) {
  788. xmlErrorPtr xmlerr = xmlGetLastError();
  789. xmlSchemaFreeValidCtxt(*validator); *validator = NULL;
  790. xmlSchemaFree(*schema); *schema = NULL;
  791. if (xmlerr) pcilib_error("xmlSchemaSetValidOptions reported error %d - %s", xmlerr->code, xmlerr->message);
  792. else pcilib_error("Failed to configure the validation context to populate default attributes");
  793. return PCILIB_ERROR_FAILED;
  794. }
  795. return 0;
  796. }
  797. /*
  798. static xmlDocPtr pcilib_xml_load_xml_file(pcilib_t *ctx, const char *xsd_filename, const char *xml_filename) {
  799. int err;
  800. xmlDocPtr doc;
  801. xmlSchemaPtr schema;
  802. xmlSchemaValidCtxtPtr validator;
  803. xmlParserCtxtPtr parser;
  804. err = pcilib_xml_load_xsd_file(ctx, xsd_filename, &schema, &validator);
  805. if (err) {
  806. pcilib_error("Error (%i) parsing the devices schema (%s)", err, xsd_filename);
  807. return NULL;
  808. }
  809. parser = xmlNewParserCtxt();
  810. if (!parser) {
  811. xmlErrorPtr xmlerr = xmlGetLastError();
  812. xmlSchemaFree(schema);
  813. xmlSchemaFreeValidCtxt(validator);
  814. if (xmlerr) pcilib_error("xmlNewParserCtxt reported error %d (%s)", xmlerr->code, xmlerr->message);
  815. else pcilib_error("Failed to create an XML parser context");
  816. return NULL;
  817. }
  818. doc = xmlCtxtReadFile(parser, xml_filename, NULL, 0);
  819. if (!doc) {
  820. xmlErrorPtr xmlerr = xmlGetLastError();
  821. xmlFreeParserCtxt(parser);
  822. xmlSchemaFree(schema);
  823. xmlSchemaFreeValidCtxt(validator);
  824. if (xmlerr) pcilib_error("Error parsing %s, xmlCtxtReadFile reported error %d - %s", xml_filename, xmlerr->code, xmlerr->message);
  825. else pcilib_error("Error parsing %s", xml_filename);
  826. return NULL;
  827. }
  828. err = xmlSchemaValidateDoc(validator, doc);
  829. if (err) {
  830. xmlErrorPtr xmlerr = xmlCtxtGetLastError(parser);
  831. xmlFreeDoc(doc);
  832. xmlFreeParserCtxt(parser);
  833. xmlSchemaFree(schema);
  834. xmlSchemaFreeValidCtxt(validator);
  835. if (xmlerr) pcilib_error("Error validating %s, xmlSchemaValidateDoc reported error %d - %s", xml_filename, xmlerr->code, xmlerr->message);
  836. else pcilib_error("Error validating %s", xml_filename);
  837. return NULL;
  838. }
  839. xmlFreeParserCtxt(parser);
  840. xmlSchemaFree(schema);
  841. xmlSchemaFreeValidCtxt(validator);
  842. return doc;
  843. }
  844. */
  845. static xmlXPathObjectPtr pcilib_xml_eval_xpath_expression(pcilib_t *ctx, xmlDocPtr doc, const xmlChar *query) {
  846. xmlXPathContextPtr xpath;
  847. xmlXPathObjectPtr nodes;
  848. xpath = xmlXPathNewContext(doc);
  849. if (!xpath) {
  850. xmlErrorPtr xmlerr = xmlGetLastError();
  851. if (xmlerr) pcilib_error("xmlXpathNewContext reported error %d - %s", xmlerr->code, xmlerr->message);
  852. else pcilib_error("Error creating XPath context");
  853. return NULL;
  854. }
  855. nodes = xmlXPathEvalExpression(query, xpath);
  856. if (!nodes) {
  857. xmlErrorPtr xmlerr = xmlGetLastError();
  858. xmlXPathFreeContext(xpath);
  859. if (xmlerr) pcilib_error("Failed to parse XPath expression %s, xmlXPathEvalExpression reported error %d - %s", query, xmlerr->code, xmlerr->message);
  860. else pcilib_error("Failed to parse XPath expression %s", query);
  861. return NULL;
  862. }
  863. xmlXPathFreeContext(xpath);
  864. return nodes;
  865. }
  866. static int pcilib_xml_load_xsd(pcilib_t *ctx, const char *model_dir) {
  867. int err;
  868. struct stat st;
  869. char *xsd_path;
  870. xsd_path = (char*)alloca(strlen(model_dir) + 32);
  871. if (!xsd_path) return PCILIB_ERROR_MEMORY;
  872. sprintf(xsd_path, "%s/model.xsd", model_dir);
  873. if (stat(xsd_path, &st)) {
  874. pcilib_info("XML models are not present, missing parts schema");
  875. return PCILIB_ERROR_NOTFOUND;
  876. }
  877. err = pcilib_xml_load_xsd_file(ctx, xsd_path, &ctx->xml.parts_schema, &ctx->xml.parts_validator);
  878. if (err) return err;
  879. sprintf(xsd_path, "%s/references.xsd", model_dir);
  880. if (stat(xsd_path, &st)) {
  881. pcilib_info("XML models are not present, missing schema");
  882. return PCILIB_ERROR_NOTFOUND;
  883. }
  884. return pcilib_xml_load_xsd_file(ctx, xsd_path, &ctx->xml.schema, &ctx->xml.validator);
  885. }
  886. static xmlDocPtr pcilib_xml_load_file(pcilib_t *ctx, xmlParserCtxtPtr parser, xmlSchemaValidCtxtPtr validator, const char *path, const char *name) {
  887. int err;
  888. char *full_name;
  889. xmlDocPtr doc;
  890. full_name = (char*)alloca(strlen(path) + strlen(name) + 2);
  891. if (!name) {
  892. pcilib_error("Error allocating %zu bytes of memory in stack to create a file name", strlen(path) + strlen(name) + 2);
  893. return NULL;
  894. }
  895. sprintf(full_name, "%s/%s", path, name);
  896. doc = xmlCtxtReadFile(parser, full_name, NULL, 0);
  897. if (!doc) {
  898. xmlErrorPtr xmlerr = xmlCtxtGetLastError(parser);
  899. if (xmlerr) pcilib_error("Error parsing %s, xmlCtxtReadFile reported error %d - %s", full_name, xmlerr->code, xmlerr->message);
  900. else pcilib_error("Error parsing %s", full_name);
  901. return NULL;
  902. }
  903. err = xmlSchemaValidateDoc(validator, doc);
  904. if (err) {
  905. xmlErrorPtr xmlerr = xmlCtxtGetLastError(parser);
  906. xmlFreeDoc(doc);
  907. if (xmlerr) pcilib_error("Error validating %s, xmlSchemaValidateDoc reported error %d - %s", full_name, xmlerr->code, xmlerr->message);
  908. else pcilib_error("Error validating %s", full_name);
  909. return NULL;
  910. }
  911. return doc;
  912. }
  913. static xmlDocPtr pcilib_xml_load_model_file(pcilib_t *ctx, const char *path, const char *name) {
  914. return pcilib_xml_load_file(ctx, ctx->xml.parser, ctx->xml.parts_validator, path, name);
  915. }
  916. static int pcilib_process_xml_internal(pcilib_t *ctx, const char *model, const char *location) {
  917. int err;
  918. DIR *rep;
  919. struct dirent *file = NULL;
  920. char *model_dir, *model_path;
  921. xmlDocPtr doc = NULL;
  922. xmlNodePtr root = NULL;
  923. xmlXPathContextPtr xpath;
  924. if (ctx->xml.num_files == PCILIB_MAX_MODEL_FILES) {
  925. pcilib_error("Too many XML locations for a model, only up to %zu are supported", PCILIB_MAX_MODEL_FILES);
  926. return PCILIB_ERROR_TOOBIG;
  927. }
  928. model_dir = getenv("PCILIB_MODEL_DIR");
  929. if (!model_dir) model_dir = PCILIB_MODEL_DIR;
  930. if (!model) model = ctx->model;
  931. if (!location) location = "";
  932. model_path = (char*)alloca(strlen(model_dir) + strlen(model) + strlen(location) + 3);
  933. if (!model_path) return PCILIB_ERROR_MEMORY;
  934. sprintf(model_path, "%s/%s/%s", model_dir, model, location);
  935. rep = opendir(model_path);
  936. if (!rep) return PCILIB_ERROR_NOTFOUND;
  937. while ((file = readdir(rep)) != NULL) {
  938. xmlDocPtr newdoc;
  939. size_t len = strlen(file->d_name);
  940. if ((len < 4)||(strcasecmp(file->d_name + len - 4, ".xml"))) continue;
  941. if (file->d_type != DT_REG) continue;
  942. newdoc = pcilib_xml_load_model_file(ctx, model_path, file->d_name);
  943. if (!newdoc) {
  944. pcilib_error("Error processing XML file %s", file->d_name);
  945. continue;
  946. }
  947. if (doc) {
  948. xmlNodePtr node;
  949. node = xmlDocGetRootElement(newdoc);
  950. if (node) node = xmlFirstElementChild(node);
  951. if (node) node = xmlDocCopyNodeList(doc, node);
  952. xmlFreeDoc(newdoc);
  953. if ((!node)||(!xmlAddChildList(root, node))) {
  954. xmlErrorPtr xmlerr = xmlCtxtGetLastError(ctx->xml.parser);
  955. if (node) xmlFreeNode(node);
  956. if (xmlerr) pcilib_error("Error manipulating XML tree of %s, libXML2 reported error %d - %s", file->d_name, xmlerr->code, xmlerr->message);
  957. else pcilib_error("Error manipulating XML tree of %s", file->d_name);
  958. continue;
  959. }
  960. } else {
  961. root = xmlDocGetRootElement(newdoc);
  962. if (!root) {
  963. xmlErrorPtr xmlerr = xmlCtxtGetLastError(ctx->xml.parser);
  964. xmlFreeDoc(newdoc);
  965. if (xmlerr) pcilib_error("Error manipulating XML tree of %s, libXML2 reported error %d - %s", file->d_name, xmlerr->code, xmlerr->message);
  966. else pcilib_error("Error manipulating XML tree of %s", file->d_name);
  967. continue;
  968. }
  969. doc = newdoc;
  970. // This is undocumented, but should be fine...
  971. if (doc->URL) xmlFree((xmlChar*)doc->URL);
  972. doc->URL = xmlStrdup(BAD_CAST model_path);
  973. }
  974. }
  975. closedir(rep);
  976. if (!doc)
  977. return 0;
  978. err = xmlSchemaValidateDoc(ctx->xml.validator, doc);
  979. if (err) {
  980. xmlErrorPtr xmlerr = xmlCtxtGetLastError(ctx->xml.parser);
  981. xmlFreeDoc(doc);
  982. if (xmlerr) pcilib_error("Error validating %s, xmlSchemaValidateDoc reported error %d - %s", model_path, xmlerr->code, xmlerr->message);
  983. else pcilib_error("Error validating %s", model_path);
  984. return PCILIB_ERROR_VERIFY;
  985. }
  986. xpath = xmlXPathNewContext(doc);
  987. if (!xpath) {
  988. xmlErrorPtr xmlerr = xmlGetLastError();
  989. xmlFreeDoc(doc);
  990. if (xmlerr) pcilib_error("Document %s: xmlXpathNewContext reported error %d - %s for document %s", model_path, xmlerr->code, xmlerr->message);
  991. else pcilib_error("Error creating XPath context for %s", model_path);
  992. return PCILIB_ERROR_FAILED;
  993. }
  994. // This can only partially fail... Therefore we need to keep XML and just return the error...
  995. err = pcilib_xml_process_document(ctx, doc, xpath);
  996. ctx->xml.docs[ctx->xml.num_files] = doc;
  997. ctx->xml.xpath[ctx->xml.num_files] = xpath;
  998. ctx->xml.num_files++;
  999. return err;
  1000. }
  1001. int pcilib_process_xml(pcilib_t *ctx, const char *location) {
  1002. return pcilib_process_xml_internal(ctx, NULL, location);
  1003. }
  1004. int pcilib_init_xml(pcilib_t *ctx, const char *model) {
  1005. int err;
  1006. char *model_dir;
  1007. model_dir = getenv("PCILIB_MODEL_DIR");
  1008. if (!model_dir) model_dir = PCILIB_MODEL_DIR;
  1009. ctx->xml.parser = xmlNewParserCtxt();
  1010. if (!ctx->xml.parser) {
  1011. xmlErrorPtr xmlerr = xmlGetLastError();
  1012. if (xmlerr) pcilib_error("xmlNewParserCtxt reported error %d (%s)", xmlerr->code, xmlerr->message);
  1013. else pcilib_error("Failed to create an XML parser context");
  1014. return PCILIB_ERROR_FAILED;
  1015. }
  1016. err = pcilib_xml_load_xsd(ctx, model_dir);
  1017. if (err) return err;
  1018. return pcilib_process_xml_internal(ctx, model, NULL);
  1019. }
  1020. void pcilib_free_xml(pcilib_t *ctx) {
  1021. int i;
  1022. memset(ctx->xml.bank_nodes, 0, sizeof(ctx->xml.bank_nodes));
  1023. for (i = 0; i < ctx->num_banks; i++) {
  1024. if (ctx->bank_ctx[i])
  1025. ctx->bank_ctx[i]->xml = NULL;
  1026. }
  1027. for (i = 0; i < ctx->num_reg; i++) {
  1028. ctx->register_ctx[i].xml = NULL;
  1029. }
  1030. for (i = 0; i < ctx->xml.num_files; i++) {
  1031. if (ctx->xml.docs[i]) {
  1032. xmlFreeDoc(ctx->xml.docs[i]);
  1033. ctx->xml.docs[i] = NULL;
  1034. }
  1035. if (ctx->xml.xpath[i]) {
  1036. xmlXPathFreeContext(ctx->xml.xpath[i]);
  1037. ctx->xml.xpath[i] = NULL;
  1038. }
  1039. }
  1040. ctx->xml.num_files = 0;
  1041. if (ctx->xml.validator) {
  1042. xmlSchemaFreeValidCtxt(ctx->xml.validator);
  1043. ctx->xml.validator = NULL;
  1044. }
  1045. if (ctx->xml.schema) {
  1046. xmlSchemaFree(ctx->xml.schema);
  1047. ctx->xml.schema = NULL;
  1048. }
  1049. if (ctx->xml.parts_validator) {
  1050. xmlSchemaFreeValidCtxt(ctx->xml.parts_validator);
  1051. ctx->xml.parts_validator = NULL;
  1052. }
  1053. if (ctx->xml.parts_schema) {
  1054. xmlSchemaFree(ctx->xml.parts_schema);
  1055. ctx->xml.parts_schema = NULL;
  1056. }
  1057. if (ctx->xml.parser) {
  1058. xmlFreeParserCtxt(ctx->xml.parser);
  1059. ctx->xml.parser = NULL;
  1060. }
  1061. /*
  1062. xmlSchemaCleanupTypes();
  1063. xmlCleanupParser();
  1064. xmlMemoryDump();
  1065. */
  1066. }
  1067. char *pcilib_detect_xml_model(pcilib_t *ctx, unsigned int vendor_id, unsigned int device_id) {
  1068. int err;
  1069. char *model = NULL;
  1070. xmlSchemaPtr schema; /**< Pointer to the parsed xsd schema */
  1071. xmlSchemaValidCtxtPtr validator; /**< Pointer to the XML validation context */
  1072. xmlParserCtxtPtr parser; /**< Pointer to the XML parser context */
  1073. DIR *rep;
  1074. struct dirent *file = NULL;
  1075. struct stat st;
  1076. const char *data_dir;
  1077. char *xsd_path, *xml_path;
  1078. xmlXPathObjectPtr nodes;
  1079. xmlChar xpath_query[64];
  1080. xmlStrPrintf(xpath_query, sizeof(xpath_query), (xmlChar*)"/devices/device[@vendor=\"%04x\" and @device=\"%04x\"]/@model", vendor_id, device_id);
  1081. data_dir = getenv("PCILIB_DATA_DIR");
  1082. if (!data_dir) data_dir = PCILIB_DATA_DIR;
  1083. xsd_path = (char*)alloca(strlen(data_dir) + 32);
  1084. xml_path = (char*)alloca(strlen(data_dir) + 32);
  1085. if ((!xsd_path)||(!xml_path)) {
  1086. pcilib_error("Error allocating stack memory");
  1087. return NULL;
  1088. }
  1089. sprintf(xsd_path, "%s/devices.xsd", data_dir);
  1090. sprintf(xml_path, "%s/devices", data_dir);
  1091. if (stat(xsd_path, &st)||stat(xml_path, &st)) {
  1092. pcilib_info("No XML devices are defined, missing devices schema or list");
  1093. return NULL;
  1094. }
  1095. parser = xmlNewParserCtxt();
  1096. if (!parser) {
  1097. xmlErrorPtr xmlerr = xmlGetLastError();
  1098. if (xmlerr) pcilib_error("xmlNewParserCtxt reported error %d (%s)", xmlerr->code, xmlerr->message);
  1099. else pcilib_error("Failed to create an XML parser context");
  1100. return NULL;
  1101. }
  1102. err = pcilib_xml_load_xsd_file(ctx, xsd_path, &schema, &validator);
  1103. if (err) {
  1104. xmlFreeParserCtxt(parser);
  1105. pcilib_error("Error (%i) parsing the device schema (%s)", err, xsd_path);
  1106. return NULL;
  1107. }
  1108. rep = opendir(xml_path);
  1109. if (!rep) goto cleanup;
  1110. while ((model == NULL)&&((file = readdir(rep)) != NULL)) {
  1111. xmlDocPtr doc;
  1112. size_t len = strlen(file->d_name);
  1113. if ((len < 4)||(strcasecmp(file->d_name + len - 4, ".xml"))) continue;
  1114. if (file->d_type != DT_REG) continue;
  1115. doc = pcilib_xml_load_file(ctx, parser, validator, xml_path, file->d_name);
  1116. if (!doc) continue;
  1117. nodes = pcilib_xml_eval_xpath_expression(ctx, doc, xpath_query);
  1118. if (!nodes) {
  1119. xmlFreeDoc(doc);
  1120. continue;
  1121. }
  1122. if (!xmlXPathNodeSetIsEmpty(nodes->nodesetval)) {
  1123. xmlNodePtr node = nodes->nodesetval->nodeTab[0];
  1124. model = strdup((char*)node->children->content);
  1125. }
  1126. xmlXPathFreeObject(nodes);
  1127. xmlFreeDoc(doc);
  1128. }
  1129. closedir(rep);
  1130. cleanup:
  1131. xmlSchemaFree(schema);
  1132. xmlSchemaFreeValidCtxt(validator);
  1133. xmlFreeParserCtxt(parser);
  1134. return model;
  1135. }