source: soft/giet_vm/xml/xml_parser.c @ 228

Last change on this file since 228 was 228, checked in by meunier, 11 years ago

Added support for memspaces and const.
Added an interrupt masking to the "giet_context_switch" syscall
Corrected two bugs in boot/boot_init.c (one minor and one regarding barriers initialization)
Reformatted the code in all files.

File size: 64.4 KB
Line 
1///////////////////////////////////////////////////////////////////////////////////////
2// File     : xml_parser.c
3// Date     : 14/04/2012
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////////
7// This program translate a "map.xml" source file to a binary file "map.bin" that
8// can be directly loaded in the boot ROM and used by the GIET-VM operating system.
9//
10// This map.xml file contains :
11// 1) the multi-cluster/multi-processors hardware architecture description
12// 2) the various multi-threaded software applications
13// 3) the mapping directives bor both the tasks and the virtual segments.
14// The corresponding C structures are defined in the "mapping_info.h" file.
15//
16// This program also generates the "hard_config.h" and the "giet_vsegs.ld" files,
17// required  to compile the GIET-VM code.
18///////////////////////////////////////////////////////////////////////////////////////
19
20#include  <stdlib.h>
21#include  <fcntl.h>
22#include  <sys/types.h>
23#include  <sys/stat.h>
24#include  <unistd.h>
25#include  <stdio.h>
26#include  <string.h>
27#include  <assert.h>
28#include  <libxml/xmlreader.h>
29#include  <mapping_info.h>
30#include  <irq_handler.h>
31
32#define MAX_CLUSTERS   1024
33#define MAX_PSEGS      4096
34#define MAX_VSPACES    1024
35#define MAX_TASKS      4096
36#define MAX_MWMRS      4096
37#define MAX_VSEGS      4096
38#define MAX_VOBJS      8192
39#define MAX_PROCS      1024
40#define MAX_IRQS       8192
41#define MAX_COPROCS    4096
42#define MAX_CP_PORTS   8192
43#define MAX_PERIPHS    8192
44
45#define XML_PARSER_DEBUG  0
46
47///////////////////////////////////////////////////////////////////////////////////
48//     global variables used to store and index the data structures
49///////////////////////////////////////////////////////////////////////////////////
50
51mapping_header_t * header;
52mapping_cluster_t * cluster[MAX_CLUSTERS];  // cluster array
53mapping_pseg_t * pseg[MAX_PSEGS];           // pseg array
54mapping_vspace_t * vspace[MAX_VSPACES];     // vspace array
55mapping_vseg_t * vseg[MAX_VSEGS];           // vseg array
56mapping_vobj_t * vobj[MAX_VOBJS];           // vobj array
57mapping_task_t * task[MAX_TASKS];           // task array
58mapping_proc_t * proc[MAX_PROCS];           // proc array
59mapping_irq_t * irq[MAX_IRQS];              // irq array
60mapping_coproc_t * coproc[MAX_COPROCS];     // coproc array
61mapping_cp_port_t * cp_port[MAX_CP_PORTS];  // coproc port array
62mapping_periph_t * periph[MAX_PERIPHS];     // peripheral array
63
64// Index for the various arrays
65
66unsigned int cluster_index  = 0;
67unsigned int vspace_index = 0;
68unsigned int global_index = 0;
69unsigned int pseg_index = 0;       
70
71unsigned int proc_index = 0;
72unsigned int proc_loc_index = 0;
73
74unsigned int irq_index = 0;
75unsigned int irq_loc_index  = 0;
76
77unsigned int coproc_index = 0;
78unsigned int coproc_loc_index = 0;
79
80unsigned int cp_port_index = 0;
81unsigned int cp_port_loc_index = 0;
82
83unsigned int periph_index = 0;
84unsigned int periph_loc_index = 0;
85
86unsigned int vseg_index = 0;
87unsigned int vseg_loc_index = 0;
88
89unsigned int task_index = 0;
90unsigned int task_loc_index = 0;
91
92unsigned int vobj_index = 0;
93unsigned int vobj_loc_index = 0;
94unsigned int vobj_count = 0;
95
96
97/////////////////////////
98// found peripheral
99/////////////////////////
100char found_timer = 0;
101char found_icu = 0;
102char found_xicu = 0;
103char found_dma = 0;
104
105
106//////////////////////////////////
107//needed to generate map_config.ld
108//////////////////////////////////
109unsigned int cluster_y            = 0;
110unsigned int cluster_x            = 0; 
111unsigned int nb_proc_max          = 0; // max number of processors per cluster
112unsigned int nb_timer_channel_max = 0; // max number of user timer
113unsigned int nb_dma_channel_max   = 0;
114unsigned int nb_tty_channel       = 0;
115unsigned int nb_ioc_channel       = 0;
116unsigned int nb_nic_channel       = 0;
117unsigned int io_mmu_active        = 0;
118unsigned int use_xicu             = 0xFFFFFFFF;
119
120
121//////////////////////////////////
122//needed to generate giet_vseg.ld
123//////////////////////////////////
124
125//kernel and boot code
126unsigned int kernel_code_base    = 0x80000000; /* kernel code */
127unsigned int kernel_data_base    = 0x80010000; /* system cacheable data */
128unsigned int kernel_uncdata_base = 0x80080000; /* system uncacheable data */
129unsigned int kernel_init_base    = 0x80090000; /* system init entry */ 
130
131unsigned int boot_code_base      = 0xBFC00000; /* boot code */
132unsigned int boot_stack_base     = 0xBFC08000; /* boot temporary stack */ 
133unsigned int boot_mapping_base   = 0xBFC0C000; /* mapping_info blob */
134
135//periphs
136unsigned int tim_base_offset = 0xFFFFFFFF;
137unsigned int tty_base_offset = 0xFFFFFFFF;
138unsigned int dma_base_offset = 0xFFFFFFFF;
139unsigned int ioc_base_offset = 0xFFFFFFFF;
140unsigned int nic_base_offset = 0xFFFFFFFF;
141unsigned int fbf_base_offset = 0xFFFFFFFF;
142unsigned int icu_base_offset = 0xFFFFFFFF;
143unsigned int gcd_base_offset = 0xFFFFFFFF;
144unsigned int iob_base_offset = 0xFFFFFFFF;
145
146
147//////////////////////////////////////////////////////////////////////
148// This arrray is useful to build a temporary list of vobj references.
149// The struct vobj_ref_s is formed by a vspace_name and a vobj_name.
150// This array is used to set the attribute vobj_id of a cp_port
151// once all the vspace have been parsed.
152/////////////////////////////////////////////////////////////////////
153typedef struct vobj_ref_s {
154    char vspace_name[32];
155    char vobj_name[32];
156} vobj_ref_t;
157
158vobj_ref_t * cp_port_vobj_ref[MAX_CP_PORTS];   
159
160
161//////////////////////////////////////////////////
162unsigned int getIntValue(xmlTextReaderPtr reader, const char * attributeName, unsigned int * ok) {
163    unsigned int value = 0;
164    unsigned int i;
165    char c;
166
167    char * string = (char *) xmlTextReaderGetAttribute(reader, (const xmlChar *) attributeName);
168
169    if (string == NULL) {
170        // missing argument
171        *ok = 0;
172        return 0;
173    }
174    else {
175        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) {
176            // Hexa
177            for (i = 2 ; (string[i] != 0) && (i < 10) ; i++) {
178                c = string[i];
179                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
180                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
181                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
182                else {
183                    *ok = 0;
184                    return 0;
185                }
186            }
187        }
188        else {
189            // Decimal
190            for (i = 0; (string[i] != 0) && (i < 9); i++) {
191                c = string[i];
192                if ((c >= '0') && (c <= '9')) value = (value * 10) + string[i] - 48;
193                else {
194                    *ok = 0;
195                    return 0;
196                }
197            }
198        }
199        *ok = 1;
200        return value; 
201    }
202} // end getIntValue()
203
204
205///////////////////////////////////////////////
206char * getStringValue(xmlTextReaderPtr reader, const char * attributeName, unsigned int * ok) {
207    char * string = (char *) xmlTextReaderGetAttribute(reader, (const xmlChar *) attributeName);
208
209
210    if (string == NULL) {
211        // missing argument
212        *ok = 0;
213        return NULL;
214    }
215    else {
216        //we read only string smaller than 32 byte
217        if (strlen(string) > 32) {
218            printf("[XML ERROR] all strings must be less than 32 bytes\n");
219            exit(1);
220        }
221
222        *ok = 1;
223        return string;
224    }
225} // end getStringValue()
226
227
228///////////////////////////////////////
229int getPsegId(unsigned int cluster_id, char * pseg_name) {
230    unsigned int pseg_id;
231    unsigned int pseg_min = cluster[cluster_id]->pseg_offset;
232    unsigned int pseg_max = pseg_min + cluster[cluster_id]->psegs;
233
234    for (pseg_id = pseg_min; pseg_id < pseg_max; pseg_id++) {
235        if (strcmp(pseg[pseg_id]->name, pseg_name) == 0) {
236            return pseg_id;
237        }
238    }
239    return -1;
240}
241
242
243////////////////////////////////////////////
244int getVspaceId(char * vspace_name) {
245    unsigned int vspace_id;
246
247    for (vspace_id = 0; vspace_id < vspace_index; vspace_id++) {
248        if (!strcmp(vspace[vspace_id]->name, vspace_name)) {
249            return vspace_id;
250        }
251    }
252    return -1;
253}
254
255
256////////////////////////////////////////////
257int getVobjLocId(unsigned int vspace_id, char * vobj_name, unsigned int vspace_max) {
258    unsigned int vobj_id;
259    unsigned int vobj_min = vspace[vspace_id]->vobj_offset;
260    unsigned int vobj_max = vobj_min + vspace_max;
261
262    for (vobj_id = vobj_min; vobj_id < vobj_max; vobj_id++) {
263        if (strcmp(vobj[vobj_id]->name, vobj_name) == 0) {
264            return (vobj_id - vobj_min);
265        }
266    }
267    return -1;
268}
269
270
271/////////////////////////////////////////
272void taskNode(xmlTextReaderPtr reader) {
273    unsigned int ok;
274    unsigned int value;
275    char * str;
276
277    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
278        return;
279    }
280
281    if (task_index >= MAX_TASKS) {
282        printf("[XML ERROR] The number of tasks is larger than %d\n", MAX_TASKS);
283    }
284
285#if XML_PARSER_DEBUG
286    printf("   task %d\n", task_loc_index);
287#endif
288
289    task[task_index] = (mapping_task_t *) malloc(sizeof(mapping_task_t));
290
291    ////////// get name attribute
292    str = getStringValue(reader, "name", &ok);
293    if (ok) {
294#if XML_PARSER_DEBUG
295        printf("      name = %s\n", str);
296#endif
297        strncpy( task[task_index]->name, str, 31 );
298    }
299    else {
300        printf("[XML ERROR] illegal or missing <name> attribute for task (%d,%d)\n", 
301                vspace_index, task_loc_index);
302        exit(1);
303    }
304
305    ///////// get clusterid attribute
306    value = getIntValue(reader, "clusterid", &ok);
307    if (ok) {
308#if XML_PARSER_DEBUG
309        printf("      clusterid = %x\n", value);
310#endif
311        if (value >= header->clusters) {
312            printf("[XML ERROR] <clusterid> too large for task (%d,%d)\n",
313                    vspace_index, task_loc_index);
314            exit(1);
315        }
316        task[task_index]->clusterid = value;
317    } 
318    else {
319        printf("[XML ERROR] illegal or missing <clusterid> attribute for task (%d,%d)\n",
320                vspace_index, task_loc_index);
321        exit(1);
322    }
323
324    ////////// get proclocid attribute
325    value = getIntValue(reader, "proclocid", &ok);
326    if (ok) {
327#if XML_PARSER_DEBUG
328        printf("      proclocid = %x\n", value);
329#endif
330        if (value >= cluster[task[task_index]->clusterid]->procs) {
331            printf("[XML ERROR] <proclocid> too large for task (%d,%d)\n",
332                    vspace_index, task_loc_index);
333            exit(1);
334        }
335        task[task_index]->proclocid = value;
336    } 
337    else {
338        printf("[XML ERROR] illegal or missing <locprocid> attribute for task (%d,%d)\n", 
339                vspace_index, task_loc_index);
340        exit(1);
341    }
342
343    ////////// get stackname attribute
344    str = getStringValue(reader, "stackname" , &ok);
345    if (ok) {
346        int index = getVobjLocId(vspace_index, str , vobj_loc_index);
347        if (index >= 0) {
348#if XML_PARSER_DEBUG
349            printf("      stackname = %s\n", str);
350            printf("      stackid   = %d\n", index);
351#endif
352            task[task_index]->vobjlocid = index;
353        }
354        else {
355            printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
356                    vspace_index, task_loc_index);
357            exit(1);
358        }
359    } 
360    else {
361        printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
362                vspace_index, task_loc_index);
363        exit(1);
364    }
365
366    ////////// get startid  attribute
367    value = getIntValue(reader, "startid", &ok);
368    if (ok) {
369#if XML_PARSER_DEBUG
370        printf("      startid = %x\n", value);
371#endif
372        task[task_index]->startid = value;
373    } 
374    else {
375        printf("[XML ERROR] illegal or missing <startid> attribute for task (%d,%d)\n", 
376                vspace_index, task_loc_index);
377        exit(1);
378    }
379
380    /////////// get usetty  attribute (optionnal : 0 if missing)
381    value = getIntValue(reader, "usetty", &ok);
382    if (ok) {
383#if XML_PARSER_DEBUG
384        printf("      usetty = %x\n", value);
385#endif
386        task[task_index]->use_tty = value;
387    } 
388    else {
389        task[task_index]->use_tty = 0;
390    }
391
392    /////////// get usenic  attribute (optionnal : 0 if missing)
393    value = getIntValue(reader, "usenic", &ok);
394    if (ok) {
395#if XML_PARSER_DEBUG
396        printf("      usenic = %x\n", value);
397#endif
398        task[task_index]->use_nic = value;
399    } 
400    else {
401        task[task_index]->use_nic = 0;
402    }
403
404    /////////// get usetimer attribute (optionnal : 0 if missing)
405    value = getIntValue(reader, "usetimer", &ok);
406    if (ok) {
407#if XML_PARSER_DEBUG
408        printf("      usetimer = %x\n", value);
409#endif
410        task[task_index]->use_timer = value;
411    } 
412    else {
413        task[task_index]->use_timer = 0;
414    }
415
416    /////////// get usefbdma  attribute (optionnal : 0 if missing)
417    value = getIntValue(reader, "usefbdma", &ok);
418    if (ok) {
419#if XML_PARSER_DEBUG
420        printf("      usefbdma = %x\n", value);
421#endif
422        task[task_index]->use_fbdma = value;
423    }
424    else {
425        task[task_index]->use_fbdma = 0;
426    }
427
428    task_index++;
429    task_loc_index++;
430} // end taskNode()
431
432
433//////////////////////////////////////////
434void vobjNode(xmlTextReaderPtr reader) {
435    unsigned int ok;
436    unsigned int value;
437    char * str;
438
439    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
440        return;
441    }
442
443    if (vobj_index >= MAX_VOBJS) {
444        printf("[XML ERROR] The number of vobjs is larger than %d\n", MAX_VSEGS);
445        exit(1);
446    }
447
448#if XML_PARSER_DEBUG
449    printf("      vobj %d\n", vobj_loc_index);
450#endif
451
452    vobj[vobj_index] = (mapping_vobj_t *) malloc(sizeof(mapping_vobj_t));
453
454    ///////// get name attribute
455    str = getStringValue(reader, "name", &ok);
456    if (ok) {
457#if XML_PARSER_DEBUG
458        printf("        name = %s\n", str);
459#endif
460        strncpy(vobj[vobj_index]->name, str, 31);
461    }
462    else {
463        printf("[XML ERROR] illegal or missing <name> attribute for vobj (%d,%d)\n", 
464                vseg_index, vobj_loc_index);
465        exit(1);
466    }
467
468    //////// get type attribute
469    str = getStringValue(reader, "type", &ok);
470#if XML_PARSER_DEBUG
471    printf("        type = %s\n", str);
472#endif
473    if (ok && (strcmp(str, "ELF") == 0)) {
474        vobj[vobj_index]->type = VOBJ_TYPE_ELF;
475
476        //check that this vobj is the first in vseg
477        if (vobj_count != 0) {
478            printf("[XML ERROR] an ELF vobj must be alone in a vseg (%d,%d)\n", 
479                    vspace_index, vobj_loc_index);
480            exit(1);
481        }
482    }
483    else if (ok && (strcmp(str, "BLOB")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BLOB; }
484    else if (ok && (strcmp(str, "PTAB")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_PTAB; }
485    else if (ok && (strcmp(str, "PERI")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_PERI; }
486    else if (ok && (strcmp(str, "MWMR")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_MWMR; }
487    else if (ok && (strcmp(str, "LOCK")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_LOCK; }
488    else if (ok && (strcmp(str, "BUFFER")   == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BUFFER; }
489    else if (ok && (strcmp(str, "BARRIER")  == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BARRIER; }
490    else if (ok && (strcmp(str, "CONST")    == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_CONST; }
491    else if (ok && (strcmp(str, "MEMSPACE") == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_MEMSPACE; }
492    else {
493        printf("[XML ERROR] illegal or missing <type> attribute for vobj (%d,%d)\n", 
494                vspace_index, vobj_loc_index);
495        exit(1);
496    }
497
498    ////////// get length attribute
499    value = getIntValue(reader, "length", &ok);
500    if (ok) {
501#if XML_PARSER_DEBUG
502        printf("        length = %d\n", value);
503#endif
504        vobj[vobj_index]->length = value;
505    } 
506    else {
507        printf("[XML ERROR] illegal or missing <length> attribute for vobj (%d,%d)\n", 
508                vspace_index, vobj_loc_index);
509        exit(1);
510    }
511
512    ////////// get align attribute (optional : 0 if missing)
513    value = getIntValue(reader, "align", &ok);
514    if (ok) {
515#if XML_PARSER_DEBUG
516        printf("        align = %d\n", value);
517#endif
518        vobj[vobj_index]->align = value;
519    } 
520    else {
521        vobj[vobj_index]->align = 0;
522    }
523
524    ////////// get binpath attribute (optional : '\0' if missing)
525    str = getStringValue(reader, "binpath", &ok);
526    if (ok) {
527#if XML_PARSER_DEBUG
528        printf("        binpath = %s\n", str);
529#endif
530        strncpy(vobj[vobj_index]->binpath, str, 63);
531    } 
532    else {
533        vobj[vobj_index]->binpath[0] = '\0';
534    }
535
536    ////////// get init attribute (mandatory for mwmr and barrier)
537    value = getIntValue(reader, "init", &ok);
538    if (ok) {
539#if XML_PARSER_DEBUG
540        printf("        init  = %d\n", value);
541#endif
542        vobj[vobj_index]->init = value;
543    } 
544    else {
545        if ((vobj[vobj_index]->type == VOBJ_TYPE_MWMR) || 
546                (vobj[vobj_index]->type == VOBJ_TYPE_BARRIER) ||
547                (vobj[vobj_index]->type == VOBJ_TYPE_CONST)) {
548            printf("[XML ERROR] illegal or missing <value> attribute for vobj (%d,%d). \
549                    All MWMR or BARRIER vobj must have a init value \n", 
550                    vspace_index, vobj_loc_index);
551            exit(1);
552        }
553        vobj[vobj_index]->init = 0;
554    }
555
556    vobj_index++;
557    vobj_count++;
558    vobj_loc_index++;
559} // end vobjNode()
560
561
562//////////////////////////////////////////
563void vsegNode(xmlTextReaderPtr reader) {
564    unsigned int ok;
565    unsigned int value;
566    char * str;
567
568    vobj_count = 0;
569
570    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
571        return;
572    }
573
574    if (vseg_index >= MAX_VSEGS) {
575        printf("[XML ERROR] The number of vsegs is larger than %d\n", MAX_VSEGS);
576        exit(1);
577    }
578
579#if XML_PARSER_DEBUG
580    printf("    vseg %d\n", vseg_loc_index);
581#endif
582
583    vseg[vseg_index] = (mapping_vseg_t *) malloc(sizeof(mapping_vseg_t));
584
585    ////////// set vobj_offset attributes
586    vseg[vseg_index]->vobj_offset = vobj_index;
587#if XML_PARSER_DEBUG
588    printf("      vobj_offset = %d\n", vobj_index);
589#endif
590
591    ///////// get name attribute
592    str = getStringValue(reader, "name", &ok);
593    if (ok) {
594#if XML_PARSER_DEBUG
595        printf("      name = %s\n", str);
596#endif
597        strncpy( vseg[vseg_index]->name, str, 31);
598    }
599    else {
600        printf("[XML ERROR] illegal or missing <name> attribute for vseg (%d,%d)\n", 
601                vspace_index, vseg_loc_index);
602        exit(1);
603    }
604
605    ////////// get ident attribute (optional : 0 if missing)
606    value = getIntValue(reader, "ident", &ok);
607    if (ok) {
608#if XML_PARSER_DEBUG
609        printf("      ident = %d\n", value);
610#endif
611        vseg[vseg_index]->ident = value;
612    } 
613    else {
614        vseg[vseg_index]->ident = 0;
615    }
616
617    /////////// get vbase attribute
618    value = getIntValue(reader, "vbase", &ok);
619    if (ok) {
620#if XML_PARSER_DEBUG
621        printf("      vbase = 0x%x\n", value);
622#endif
623        vseg[vseg_index]->vbase = value;
624    }
625    else {
626        printf("[XML ERROR] illegal or missing <vbase> attribute for vseg (%d,%d)\n", 
627                vspace_index, vseg_loc_index);
628        exit(1);
629    }
630
631    ////////// get clusterid and psegname attributes
632    value = getIntValue(reader, "clusterid", &ok);
633    if (ok == 0) {
634        printf("[XML ERROR] illegal or missing <clusterid> for vseg %d\n", 
635                vseg_loc_index);
636        exit(1);
637    }
638    str = getStringValue(reader, "psegname", &ok);
639    if (ok == 0) {
640        printf("[XML ERROR] illegal or missing <psegname> for vseg %d\n", 
641                vseg_loc_index);
642        exit(1);
643    }
644
645    /////////// set psegid field
646    int index = getPsegId(value, str);
647    if (index >= 0) {
648#if XML_PARSER_DEBUG
649        printf("      clusterid = %d\n", value);
650        printf("      psegname  = %s\n", str);
651        printf("      psegid    = %d\n", index);
652#endif
653        vseg[vseg_index]->psegid = index;
654    }
655    else {
656        printf("[XML ERROR] pseg not found for vseg %d / clusterid = %d / psegname = %s\n", 
657                vseg_loc_index, value, str );
658        exit(1);
659    } 
660
661    //////// get mode attribute
662    str = getStringValue(reader, "mode", &ok);
663#if XML_PARSER_DEBUG
664    printf("      mode = %s\n", str);
665#endif
666    if      (ok && (strcmp(str, "CXWU") == 0)) { vseg[vseg_index]->mode = 0xF; }
667    else if (ok && (strcmp(str, "CXW_") == 0)) { vseg[vseg_index]->mode = 0xE; }
668    else if (ok && (strcmp(str, "CX_U") == 0)) { vseg[vseg_index]->mode = 0xD; }
669    else if (ok && (strcmp(str, "CX__") == 0)) { vseg[vseg_index]->mode = 0xC; }
670    else if (ok && (strcmp(str, "C_WU") == 0)) { vseg[vseg_index]->mode = 0xB; }
671    else if (ok && (strcmp(str, "C_W_") == 0)) { vseg[vseg_index]->mode = 0xA; }
672    else if (ok && (strcmp(str, "C__U") == 0)) { vseg[vseg_index]->mode = 0x9; }
673    else if (ok && (strcmp(str, "C___") == 0)) { vseg[vseg_index]->mode = 0x8; }
674    else if (ok && (strcmp(str, "_XWU") == 0)) { vseg[vseg_index]->mode = 0x7; }
675    else if (ok && (strcmp(str, "_XW_") == 0)) { vseg[vseg_index]->mode = 0x6; }
676    else if (ok && (strcmp(str, "_X_U") == 0)) { vseg[vseg_index]->mode = 0x5; }
677    else if (ok && (strcmp(str, "_X__") == 0)) { vseg[vseg_index]->mode = 0x4; }
678    else if (ok && (strcmp(str, "__WU") == 0)) { vseg[vseg_index]->mode = 0x3; }
679    else if (ok && (strcmp(str, "__W_") == 0)) { vseg[vseg_index]->mode = 0x2; }
680    else if (ok && (strcmp(str, "___U") == 0)) { vseg[vseg_index]->mode = 0x1; }
681    else if (ok && (strcmp(str, "____") == 0)) { vseg[vseg_index]->mode = 0x0; }
682    else {
683        printf("[XML ERROR] illegal or missing <mode> attribute for vseg (%d,%d)\n", 
684                vspace_index, vseg_loc_index);
685        exit(1);
686    }
687
688    ////////// get vobjs in vseg
689    int status = xmlTextReaderRead(reader);
690    while (status == 1) {
691        const char * tag = (const char *) xmlTextReaderConstName(reader);
692
693        if (strcmp(tag, "vobj")     == 0 ) {
694            vobjNode(reader);
695        }
696        else if (strcmp(tag, "#text"  )  == 0 ) { }
697        else if (strcmp(tag, "#comment") == 0 ) { }
698        else if (strcmp(tag, "vseg")     == 0 ) {
699            vseg[vseg_index]->vobjs = vobj_count;
700            vseg_index++;
701            vseg_loc_index++;
702            return;
703        }
704        else {
705            printf("[XML ERROR] Unknown tag %s", tag);
706            exit(1);
707        }
708        status = xmlTextReaderRead (reader);
709    }
710} // end vsegNode()
711
712
713//////////////////////////////////////////
714void vspaceNode(xmlTextReaderPtr reader) {
715    char * str;
716    unsigned int ok;
717
718    vobj_loc_index = 0;
719    vseg_loc_index = 0;
720    task_loc_index = 0;
721
722    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
723        return;
724    }
725
726    // checking source file consistency
727    if (vspace_index >= header->vspaces) {
728        printf("[XML ERROR] The vspace index is too large : %d\n", 
729                vspace_index);
730        exit(1);
731    }
732
733#if XML_PARSER_DEBUG
734    printf("\n  vspace %d\n", vspace_index);
735#endif
736
737    vspace[vspace_index] = (mapping_vspace_t *) malloc(sizeof(mapping_vspace_t));
738
739    ////////// get name attribute
740    str = getStringValue(reader, "name", &ok);
741    if (ok) {
742#if XML_PARSER_DEBUG
743        printf("  name = %s\n", str);
744#endif
745        strncpy(vspace[vspace_index]->name, str, 31);
746    }
747    else {
748        printf("[XML ERROR] illegal or missing <name> attribute for vspace %d\n", 
749                vspace_index);
750        exit(1);
751    }
752
753    ////////// set vseg_offset and task_offset attributes
754    vspace[vspace_index]->vseg_offset = vseg_index;
755    vspace[vspace_index]->vobj_offset = vobj_index;
756    vspace[vspace_index]->task_offset = task_index;
757
758#if XML_PARSER_DEBUG
759    printf("  vseg_offset = %d\n", vseg_index);
760    printf("  vobj_offset = %d\n", vobj_index);
761    printf("  task_offset = %d\n", task_index);
762#endif
763
764    ////////// get startname attribute
765    str = getStringValue(reader, "startname", &ok);
766    if (ok) {
767        //used after parsing the vobjs
768    }
769    else {
770        printf("[XML ERROR] illegal or missing <startname> attribute for vspace %s\n", 
771                vspace[vspace_index]->name);
772        exit(1);
773    }
774
775    int status = xmlTextReaderRead(reader);
776    while (status == 1) {
777        const char * tag = (const char *) xmlTextReaderConstName(reader);
778
779        if (strcmp(tag, "vseg")     == 0 ) {
780            vsegNode(reader);
781        }
782        else if (strcmp(tag, "task")     == 0 ) {
783            taskNode(reader);
784        }
785        else if (strcmp(tag, "#text")    == 0 ) { }
786        else if (strcmp(tag, "#comment") == 0 ) { }
787        else if (strcmp(tag, "vspace")   == 0 ) {
788            vspace[vspace_index]->vobjs = vobj_loc_index; 
789            vspace[vspace_index]->tasks = task_loc_index ;
790            vspace[vspace_index]->vsegs = vseg_loc_index ;
791
792            // get index of the vobj containing the start vector
793            int index = getVobjLocId(vspace_index, str , vobj_loc_index);
794            if (index == -1) {
795                printf("[XML ERROR] vobj containing start vector not found in vspace %s\n",
796                        vspace[vspace_index]->name);
797                exit(-1);
798            }
799            else {
800                vspace[vspace_index]->start_offset = index;
801#if XML_PARSER_DEBUG
802                printf("      startname = %s\n", str);
803                printf("      startid   = %d\n", index);
804                printf("  end vspace %d\n\n", vspace_index);
805#endif
806            }
807
808            // checking startid values for all tasks in vspace
809            int task_id;
810            int task_min = vspace[vspace_index]->task_offset;
811            int task_max = task_min + vspace[vspace_index]->tasks; 
812            for (task_id = task_min; task_id < task_max; task_id++) {
813                if (task[task_id]->startid >= vspace[vspace_index]->tasks) {
814                    printf("[XML ERROR] <startid> too large for task (%d,%d)\n", 
815                            vspace_index, task_id );
816                    exit(1);
817                }
818            }
819
820            vspace_index++;
821            return;
822        }
823        else {
824            printf("[XML ERROR] Unknown tag %s", tag);
825            exit(1);
826        }
827        status = xmlTextReaderRead(reader);
828    }
829} // end vspaceNode()
830
831
832///////////////////////////////////////////
833void cpPortNode(xmlTextReaderPtr reader) {
834    char * str;
835    unsigned int ok;
836
837    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
838        return;
839    }
840
841    if (cp_port_index >= MAX_CP_PORTS) {
842        printf("[XML ERROR] The number of ports (for coprocs) is larger than %d\n", MAX_CP_PORTS);
843    }
844
845#if XML_PARSER_DEBUG
846    printf("\n  port %d\n", cp_port_index);
847#endif
848
849    cp_port[cp_port_index] = (mapping_cp_port_t *) malloc(sizeof(mapping_cp_port_t));
850    cp_port_vobj_ref[cp_port_index] = (vobj_ref_t *) malloc(sizeof(vobj_ref_t));
851
852
853
854    ///////// get direction attribute
855    str = getStringValue(reader, "direction", &ok);
856    if (ok) {
857#if XML_PARSER_DEBUG
858        printf("      direction = %s\n", str);
859#endif
860        if (strcmp(str, "TO_COPROC")   ==  0) {
861            cp_port[cp_port_index]->direction = PORT_TO_COPROC;
862        }
863        else if (strcmp(str, "FROM_COPROC") ==  0) {
864            cp_port[cp_port_index]->direction = PORT_FROM_COPROC;
865        }
866        else {
867            printf("[XML ERROR] illegal <direction> for cp_port %d in cluster %d\n",
868                    cp_port_index, cluster_index);
869            exit(1);
870        }
871    } 
872    else {
873        printf("[XML ERROR] missing <direction> for cp_port %d in cluster %d\n",
874                cp_port_index, cluster_index);
875        exit(1);
876    }
877
878    /////////// get vspacename attribute
879    str = getStringValue(reader, "vspacename", &ok);
880#if XML_PARSER_DEBUG
881    printf("      vspacename = %s\n", str);
882#endif
883    if (ok) {
884        strncpy(cp_port_vobj_ref[cp_port_index]->vspace_name, str, 31);
885    }
886    else {
887        printf("[XML ERROR] missing <vspacename> for cp_port %d in cluster %d\n",
888                cp_port_index, cluster_index);
889        exit(1);
890    }
891
892    /////////// get vobjname attribute
893    str = getStringValue(reader, "vobjname", &ok);
894#if XML_PARSER_DEBUG
895    printf("      vobjname = %s\n", str);
896#endif
897    if (ok) {
898        strncpy(cp_port_vobj_ref[cp_port_index]->vobj_name, str, 31);
899    }
900    else {
901        printf("[XML ERROR] missing <vobjname> for cp_port %d in cluster %d\n",
902                cp_port_index, cluster_index);
903        exit(1);
904    }
905
906    cp_port_index++;
907    cp_port_loc_index++;
908
909} // end cpPortNode()
910
911
912///////////////////////////////////////////
913void periphNode(xmlTextReaderPtr reader) {
914    char * str;
915    unsigned int value;
916    unsigned int ok;
917
918    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
919        return;
920    }
921
922    if (periph_index >= MAX_PERIPHS) {
923        printf("[XML ERROR] The number of periphs is larger than %d\n", MAX_PERIPHS);
924    }
925
926#if XML_PARSER_DEBUG
927    printf("\n  periph %d\n", periph_index);
928#endif
929
930    periph[periph_index] = (mapping_periph_t *) malloc(sizeof(mapping_periph_t));
931
932
933    ///////// get channels attribute (optionnal : 1 if missing)
934    value = getIntValue(reader, "channels", &ok);
935    if (ok) {
936#if XML_PARSER_DEBUG
937        printf("      channels = %d\n", value);
938#endif
939        periph[periph_index]->channels = value;
940    }
941    else {
942        periph[periph_index]->channels = 1;
943    }
944
945    /////////// get psegname attribute
946    str = getStringValue(reader, "psegname", &ok);
947    if (ok == 0) {
948        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
949                coproc_index, cluster_index);
950        exit(1);
951    }
952
953    /////////// set psegid attribute
954    int index = getPsegId(cluster_index, str);
955    if (index >= 0) {
956#if XML_PARSER_DEBUG
957        printf("      clusterid = %d\n", cluster_index);
958        printf("      psegname  = %s\n", str);
959        printf("      psegid    = %d\n", index);
960#endif
961        periph[periph_index]->psegid = index;
962        assert(pseg[index]->type == PSEG_TYPE_PERI && 
963                "peripheral psegname attribute must refer to a pseg of type PERI" );
964    }
965    else {
966        printf("[XML ERROR] pseg not found for periph %d / clusterid = %d / psegname = %s\n", 
967                periph_loc_index, cluster_index, str );
968        exit(1);
969    } 
970
971
972    /////////// get type attribute
973    str = getStringValue(reader, "type", &ok);
974    if (ok) {
975#if XML_PARSER_DEBUG
976        printf("      type     = %s\n", str);
977#endif
978        unsigned int error = 0;
979
980        // The TTY, IOC, NIC, FBF and IOB peripherals cannot be replicated
981        // one per architecture
982        if (strcmp(str, "IOC") == 0) {
983            periph[periph_index]->type = PERIPH_TYPE_IOC;
984            if (header->ioc_clusterid == 0xFFFFFFFF) {
985                header->ioc_clusterid = cluster_index;
986            }
987            else {
988                error = 1;
989            }
990
991            ioc_base_offset = pseg[periph[periph_index]->psegid]->base;
992            nb_ioc_channel = periph[periph_index]->channels;
993        } 
994        else if (strcmp(str, "TTY") == 0) {
995            periph[periph_index]->type = PERIPH_TYPE_TTY;
996            if (header->tty_clusterid == 0xFFFFFFFF) {
997                header->tty_clusterid = cluster_index;
998            }
999            else  {
1000                error = 1;
1001            }
1002
1003            tty_base_offset = pseg[periph[periph_index]->psegid]->base;
1004            nb_tty_channel = periph[periph_index]->channels;
1005        }
1006        else if (strcmp(str, "FBF") == 0) {
1007            periph[periph_index]->type = PERIPH_TYPE_FBF;
1008            if (header->fbf_clusterid == 0xFFFFFFFF) {
1009                header->fbf_clusterid = cluster_index;
1010            }
1011            else {
1012                error = 1;
1013            }
1014            fbf_base_offset = pseg[periph[periph_index]->psegid]->base;
1015        }
1016        else if (strcmp(str, "NIC") == 0) {
1017            periph[periph_index]->type = PERIPH_TYPE_NIC;
1018            if (header->nic_clusterid == 0xFFFFFFFF) {
1019                header->nic_clusterid = cluster_index;
1020            }
1021            else {
1022                error = 1;
1023            }
1024            nic_base_offset = pseg[periph[periph_index]->psegid]->base;
1025            nb_nic_channel = periph[periph_index]->channels;
1026        }
1027        else if (strcmp(str, "IOB") == 0) {
1028            periph[periph_index]->type = PERIPH_TYPE_IOB;
1029            iob_base_offset = pseg[periph[periph_index]->psegid]->base;
1030
1031            if (io_mmu_active) {
1032                error = 1;
1033            }
1034            io_mmu_active = 1;
1035        }
1036        // The TIM, ICU, XICU, DMA and IOB peripherals can be replicated in several clusters
1037        // one per cluster
1038        else if (strcmp(str, "TIM") == 0 ) {
1039            periph[periph_index]->type = PERIPH_TYPE_TIM;
1040            if (found_timer) {
1041                error = 1;
1042            }
1043            found_timer = 1;
1044
1045            if (tim_base_offset == 0xFFFFFFFF) {
1046                tim_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1047            }
1048
1049            if (nb_timer_channel_max < periph[periph_index]->channels) {
1050                nb_timer_channel_max = periph[periph_index]->channels;
1051            }
1052        }
1053        else if (strcmp(str, "ICU") == 0) {
1054            periph[periph_index]->type = PERIPH_TYPE_ICU;
1055            if (found_icu) {
1056                error = 1;
1057            }
1058            found_icu = 1;
1059
1060            if (icu_base_offset == 0xFFFFFFFF) {
1061                icu_base_offset = pseg[periph[periph_index]->psegid]->base;
1062            }
1063        }
1064        else if (strcmp(str, "XICU") == 0) {
1065            periph[periph_index]->type = PERIPH_TYPE_XICU;
1066            if (found_xicu) {
1067                error = 1;
1068            }
1069            found_xicu = 1;
1070
1071            //'icu' since we can't have both xicu and icu in an arch
1072            if (icu_base_offset == 0xFFFFFFFF) {
1073                icu_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1074            }
1075
1076            if (nb_timer_channel_max == 0) {
1077                nb_timer_channel_max = 32;
1078            }
1079        }
1080        else if (strcmp(str, "DMA") == 0) {
1081            periph[periph_index]->type = PERIPH_TYPE_DMA;
1082            if (found_dma) {
1083                error = 1;
1084            }
1085            found_dma = 1;
1086
1087            if (dma_base_offset == 0xFFFFFFFF) {
1088                dma_base_offset = pseg[periph[periph_index]->psegid]->base;
1089            }
1090            if (nb_dma_channel_max < periph[periph_index]->channels) {
1091                nb_dma_channel_max = periph[periph_index]->channels;
1092            }
1093        }
1094        else {
1095            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
1096                    periph_loc_index, cluster_index);
1097            exit(1);
1098        }
1099
1100        if (error) {
1101            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
1102                    periph_loc_index, cluster_index);
1103            exit(1);
1104        }
1105    }
1106    else {
1107        printf("[XML ERROR] missing <type> for peripheral  %d in cluster %d\n",
1108                periph_loc_index, cluster_index);
1109        exit(1);
1110    }
1111
1112
1113    periph_index++;
1114    periph_loc_index++;
1115    cluster[cluster_index]->periphs++;
1116
1117} // end periphNode
1118
1119
1120/////////////////////////////////////////
1121void coprocNode(xmlTextReaderPtr reader) {
1122    char * str;
1123    unsigned int ok;
1124
1125    cp_port_loc_index = 0;
1126
1127    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1128        return;
1129    }
1130
1131    if (coproc_index >= MAX_COPROCS) {
1132        printf("[XML ERROR] The number of coprocs is larger than %d\n", MAX_COPROCS);
1133    }
1134
1135#if XML_PARSER_DEBUG
1136    printf("\n  coproc %d\n", coproc_index);
1137#endif
1138
1139    coproc[coproc_index] = (mapping_coproc_t *) malloc(sizeof(mapping_coproc_t));
1140
1141    /////////// get name attribute
1142    str = getStringValue(reader, "name", &ok);
1143    if (ok) {
1144#if XML_PARSER_DEBUG
1145        printf("      name = %s\n", str);
1146#endif
1147        strncpy(coproc[coproc_index]->name, str, 31);
1148    }
1149    else {
1150        printf("[XML ERROR] illegal or missing <name> for coproc %d in cluster %d\n",
1151                coproc_index, cluster_index);
1152        exit(1);
1153    }
1154
1155    /////////// get psegname attribute
1156    str = getStringValue(reader, "psegname", &ok);
1157    if (ok == 0) {
1158        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1159                coproc_index, cluster_index);
1160        exit(1);
1161    }
1162
1163    /////////// set psegid attribute
1164    int index = getPsegId(cluster_index, str);
1165    if (index >= 0) {
1166#if XML_PARSER_DEBUG
1167        printf("      clusterid = %d\n", cluster_index);
1168        printf("      psegname  = %s\n", str);
1169        printf("      psegid    = %d\n", index);
1170#endif
1171        coproc[coproc_index]->psegid = index;
1172        assert(pseg[index]->type == PSEG_TYPE_PERI && "coproc psegname attribute must refer to a pseg of type PERI" );
1173    }
1174    else {
1175        printf("[XML ERROR] pseg not found for coproc %d / clusterid = %d / psegname = %s\n", 
1176                coproc_index, cluster_index, str );
1177        exit(1);
1178    } 
1179
1180    ////////// set port_offset
1181    coproc[coproc_index]->port_offset = cp_port_index;
1182
1183#if XML_PARSER_DEBUG
1184    printf("      port_offset = %d\n", cp_port_index);
1185#endif
1186
1187    int status = xmlTextReaderRead(reader);
1188    while (status == 1) {
1189        const char * tag = (const char *) xmlTextReaderConstName(reader);
1190
1191        if (strcmp(tag, "port") == 0 ) {
1192            cpPortNode(reader);
1193        }
1194        else if (strcmp(tag, "#text")    == 0 ) { }
1195        else if (strcmp(tag, "#comment") == 0 ) { }
1196        else if (strcmp(tag, "coproc") == 0 ) {
1197            coproc[coproc_index]->ports = cp_port_loc_index;
1198            cluster[cluster_index]->coprocs++;
1199            coproc_loc_index++;
1200            coproc_index++;
1201            return;
1202        }
1203        else {
1204            printf("[XML ERROR] Unknown tag %s", tag);
1205            exit(1);
1206        }
1207        status = xmlTextReaderRead(reader);
1208    }
1209} // end coprocNode()
1210
1211
1212///////////////////////////////////////
1213void irqNode(xmlTextReaderPtr reader) {
1214    unsigned int ok;
1215    unsigned int value;
1216    char * str;
1217
1218    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1219        return;
1220    }
1221
1222    if (irq_index >= MAX_IRQS) {
1223        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
1224    }
1225
1226#if XML_PARSER_DEBUG
1227    printf("     irq %d\n", irq_loc_index);
1228#endif
1229
1230    irq[irq_index] = (mapping_irq_t *) malloc(sizeof(mapping_irq_t));
1231
1232    ///////// get type attribute
1233    str = getStringValue(reader, "type", &ok);
1234    if (ok) {
1235#if XML_PARSER_DEBUG
1236        printf("        type    = %s\n", str);
1237#endif
1238        if (strcmp(str, "HARD") == 0 ) {
1239            irq[irq_index]->type = 0;
1240        }
1241        else if (strcmp(str, "SOFT") == 0 ) {
1242            irq[irq_index]->type = 1;
1243        }
1244        else {
1245            printf("[XML ERROR] undefined IRQ  <type> for processor %d in cluster %d\n",
1246                    cluster_index, proc_loc_index);
1247            exit(1);
1248        }
1249    } 
1250    else {
1251        printf("[XML ERROR] missing IRQ <type> for processor %d in cluster %d\n",
1252                cluster_index, proc_loc_index);
1253        exit(1);
1254    }
1255
1256    ///////// get icuid attribute
1257    value = getIntValue(reader, "icuid", &ok);
1258    if (ok) {
1259#if XML_PARSER_DEBUG
1260        printf("        icuid   = %d\n", value);
1261#endif
1262        irq[irq_index]->icuid = value;
1263        if (value >= 32) {
1264            printf("[XML ERROR] IRQ <icuid> too large for processor %d in cluster %d\n",
1265                    cluster_index, proc_loc_index);
1266            exit(1);
1267        }
1268    }
1269    else {
1270        printf("[XML ERROR] missing IRQ <icuid> for processor %d in cluster %d\n",
1271                cluster_index, proc_loc_index);
1272        exit(1);
1273    }
1274
1275    ///////// get isr attribute
1276    str = getStringValue(reader, "isr", &ok);
1277    if (ok) {
1278#if XML_PARSER_DEBUG
1279        printf("        isr     = %s\n", str);
1280#endif
1281        if      (strcmp(str, "ISR_SWITCH" ) == 0) { irq[irq_index]->isr = ISR_SWITCH; }
1282        else if (strcmp(str, "ISR_IOC"    ) == 0) { irq[irq_index]->isr = ISR_IOC; }
1283        else if (strcmp(str, "ISR_DMA"    ) == 0) { irq[irq_index]->isr = ISR_DMA; }
1284        else if (strcmp(str, "ISR_TTY"    ) == 0) { irq[irq_index]->isr = ISR_TTY; }
1285        else if (strcmp(str, "ISR_TIMER"  ) == 0) { irq[irq_index]->isr = ISR_TIMER; }
1286        else {
1287            printf("[XML ERROR] illegal IRQ <isr> for processor %d in cluster %d\n",
1288                    cluster_index, proc_loc_index);
1289            exit(1);
1290        }
1291#if XML_PARSER_DEBUG
1292        printf("        isrnum  = %d\n", irq[irq_index]->isr);
1293#endif
1294    } 
1295    else {
1296        printf("[XML ERROR] missing IRQ <isr> for processor %d in cluster %d\n",
1297                cluster_index, proc_loc_index);
1298        exit(1);
1299    }
1300
1301    ///////// get channel attribute (optionnal : 0 if missing)
1302    value = getIntValue(reader, "channel", &ok);
1303    if (ok) {
1304#if XML_PARSER_DEBUG
1305        printf("        channel = %d\n", value);
1306#endif
1307        irq[irq_index]->channel = value;
1308    }
1309    else {
1310        irq[irq_index]->channel = 0;
1311    }
1312
1313    irq_index++;
1314    irq_loc_index++;
1315
1316} // end irqNode
1317
1318
1319/////////////////////////////////////////
1320void procNode(xmlTextReaderPtr reader) {
1321    unsigned int ok;
1322    unsigned int value;
1323
1324    irq_loc_index = 0;
1325
1326    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1327        return;
1328    }
1329
1330    if (proc_index >= MAX_PROCS) {
1331        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
1332    }
1333
1334#if XML_PARSER_DEBUG
1335    printf("\n  proc %d\n", proc_index);
1336#endif
1337
1338    proc[proc_index] = (mapping_proc_t *) malloc(sizeof(mapping_proc_t));
1339
1340
1341    /////////// get index attribute (optional)
1342    value = getIntValue(reader, "index", &ok);
1343    if (ok && (value != proc_loc_index)) {
1344        printf("[XML ERROR] wrong proc index / expected value is %d", 
1345                proc_loc_index);
1346        exit(1);
1347    }
1348
1349    ////////// set irq_offset attribute
1350    proc[proc_index]->irq_offset = irq_index;
1351
1352#if XML_PARSER_DEBUG
1353    printf("    irq_offset = %d\n", irq_index);
1354#endif
1355
1356    int status = xmlTextReaderRead(reader);
1357    while (status == 1) {
1358        const char * tag = (const char *) xmlTextReaderConstName(reader);
1359
1360        if (strcmp(tag, "irq") == 0) {
1361            irqNode(reader);
1362        }
1363        else if (strcmp(tag, "#text")    == 0) { }
1364        else if (strcmp(tag, "#comment") == 0) { }
1365        else if (strcmp(tag, "proc")     == 0) {
1366            proc[proc_index]->irqs = irq_loc_index;
1367            cluster[cluster_index]->procs++;
1368            proc_loc_index++;
1369            proc_index++;
1370            return;
1371        }
1372        else {
1373            printf("[XML ERROR] Unknown tag %s", tag);
1374            exit(1);
1375        }
1376        status = xmlTextReaderRead(reader);
1377    }
1378} // end procNode()
1379
1380
1381//////////////////////////////////////////
1382void psegNode(xmlTextReaderPtr reader) {
1383    unsigned int ok;
1384    unsigned int value;
1385    char * str;
1386
1387    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1388        return;
1389    }
1390
1391    if (pseg_index >= MAX_PSEGS) {
1392        printf("[XML ERROR] The number of psegs is larger than %d\n", MAX_PSEGS);
1393        exit(1);
1394    }
1395
1396#if XML_PARSER_DEBUG
1397    printf("    pseg %d\n", pseg_index);
1398#endif
1399
1400    pseg[pseg_index] = (mapping_pseg_t *) malloc(sizeof(mapping_pseg_t));
1401
1402    /////// get name attribute
1403    str = getStringValue(reader, "name", &ok);
1404#if XML_PARSER_DEBUG
1405    printf("      name = %s\n", str);
1406#endif
1407    if (ok) {
1408        strncpy(pseg[pseg_index]->name, str, 31);
1409    }
1410    else {
1411        printf("[XML ERROR] illegal or missing <name> for pseg %d in cluster %d\n",
1412                pseg_index, cluster_index);
1413        exit(1);
1414    }
1415
1416    //////// get type attribute
1417    str = getStringValue(reader, "type", &ok);
1418#if XML_PARSER_DEBUG
1419    printf("      type = %s\n", str);
1420#endif
1421    if      (ok && (strcmp(str, "RAM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_RAM; }
1422    else if (ok && (strcmp(str, "ROM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_ROM; }
1423    else if (ok && (strcmp(str, "PERI") == 0)) { pseg[pseg_index]->type = PSEG_TYPE_PERI; }
1424    else {
1425        printf("[XML ERROR] illegal or missing <type> for pseg %s in cluster %d\n",
1426                pseg[pseg_index]->name, cluster_index);
1427        exit(1);
1428    }
1429
1430    //////// get base attribute
1431    value = getIntValue(reader, "base", &ok);
1432#if XML_PARSER_DEBUG
1433    printf("      base = 0x%x\n", value);
1434#endif
1435    if (ok) {
1436        pseg[pseg_index]->base = value;
1437    }
1438    else {
1439        printf("[XML ERROR] illegal or missing <base> for pseg %s in cluster %d\n",
1440                pseg[pseg_index]->name, cluster_index);
1441        exit(1);
1442    }
1443
1444    //////// get length attribute
1445    value = getIntValue(reader, "length", &ok);
1446#if XML_PARSER_DEBUG
1447    printf("      length = 0x%x\n", value);
1448#endif
1449    if (ok) {
1450        pseg[pseg_index]->length = value;
1451    } 
1452    else {
1453        printf("[XML ERROR] illegal or missing <length> for pseg %s in cluster %d\n",
1454                pseg[pseg_index]->name, cluster_index);
1455        exit(1);
1456    }
1457
1458    //////// set cluster attribute
1459    pseg[pseg_index]->cluster = cluster_index;
1460
1461    pseg_index++;
1462    cluster[cluster_index]->psegs++;
1463} // end psegNode()
1464
1465
1466/////////////////////////////////////////////
1467void clusterNode(xmlTextReaderPtr reader) {
1468    unsigned int ok;
1469    unsigned int value;
1470
1471    cluster[cluster_index] = (mapping_cluster_t *) malloc(sizeof(mapping_cluster_t));
1472
1473    //initialise all variables
1474    //they will be incremented by *Node() functions
1475    //FIXME: calloc?
1476    cluster[cluster_index]->psegs = 0;
1477    cluster[cluster_index]->procs = 0;
1478    cluster[cluster_index]->coprocs = 0;
1479    cluster[cluster_index]->periphs = 0;
1480
1481
1482    //initialise global variables
1483    //TODO: delete those three
1484    proc_loc_index = 0;
1485    coproc_loc_index = 0;
1486    periph_loc_index = 0;
1487
1488    // for replicated periph
1489    found_timer = 0;
1490    found_icu = 0;
1491    found_xicu = 0;
1492    found_dma = 0;
1493
1494    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1495        return;
1496    }
1497
1498    // checking source file consistency
1499    if (cluster_index >= header->clusters) {
1500        printf("[XML ERROR] The cluster index is too large : %d\n", cluster_index);
1501        exit(1);
1502    }
1503
1504#if XML_PARSER_DEBUG
1505    printf("  cluster %d\n", cluster_index);
1506#endif
1507
1508
1509    /////////// check cluster index attribute (optional)
1510    value = getIntValue(reader, "index", &ok);
1511    if (ok && (value != cluster_index)) {
1512        printf("[XML ERROR] wrong cluster index / expected value is %d", 
1513                cluster_index);
1514        exit(1);
1515    }
1516
1517    ////////// set offsets
1518    cluster[cluster_index]->pseg_offset = pseg_index;
1519    cluster[cluster_index]->proc_offset = proc_index;
1520    cluster[cluster_index]->coproc_offset = coproc_index;
1521    cluster[cluster_index]->periph_offset = periph_index;
1522
1523#if XML_PARSER_DEBUG
1524    printf("    pseg_offset   = %d\n", pseg_index);
1525    printf("    proc_offset   = %d\n", proc_index);
1526    printf("    coproc_offset = %d\n", coproc_index);
1527    printf("    periph_offset = %d\n", coproc_index);
1528#endif
1529
1530    ////////// get psegs, procs, coprocs and periphs
1531    int status = xmlTextReaderRead(reader);
1532
1533    while (status == 1) {
1534        const char * tag = (const char *) xmlTextReaderConstName(reader);
1535
1536        if      (strcmp(tag, "pseg")     == 0) psegNode(reader);
1537        else if (strcmp(tag, "proc")     == 0) procNode(reader);
1538        else if (strcmp(tag, "coproc")   == 0) coprocNode(reader);
1539        else if (strcmp(tag, "periph")   == 0) periphNode(reader);
1540        else if (strcmp(tag, "#text")    == 0) { }
1541        else if (strcmp(tag, "#comment") == 0) { }
1542        else if (strcmp(tag, "cluster")  == 0) {
1543
1544            if (use_xicu == 0xFFFFFFFF) {
1545                use_xicu = found_xicu;
1546            }
1547
1548            ////////////////// peripherals checks ////////////////////
1549            if ((found_timer  && use_xicu) || (!found_timer  && !use_xicu)) {
1550                printf("[XML ERROR] illegal or missing timer peripheral in cluster %d\n", cluster_index);
1551                exit(1);
1552            }
1553
1554            if ((found_icu && use_xicu) || (!found_icu && !use_xicu)) {
1555                printf("[XML ERROR] illegal or missing icu peripheral in cluster %d\n", cluster_index);
1556                exit(1);
1557            }
1558
1559            if (!found_xicu && use_xicu) {
1560                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
1561                exit(1);
1562            }
1563
1564            if (!found_dma) {
1565                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
1566                exit(1);
1567            }
1568
1569
1570            if (nb_proc_max < cluster[cluster_index]->procs) {
1571                nb_proc_max = cluster[cluster_index]->procs;
1572            }
1573
1574#if XML_PARSER_DEBUG
1575            printf("    psegs   = %d\n", cluster[cluster_index]->psegs);
1576            printf("    procs   = %d\n", cluster[cluster_index]->procs);
1577            printf("    coprocs = %d\n", cluster[cluster_index]->coprocs);
1578            printf("    periphs = %d\n", cluster[cluster_index]->periphs);
1579            printf("    end cluster %d\n", cluster_index);
1580#endif
1581            cluster_index++;
1582            return;
1583        }
1584        status = xmlTextReaderRead(reader);
1585    }
1586} // end clusterNode()
1587
1588
1589//////////////////////////////////////////////
1590void clusterSetNode(xmlTextReaderPtr reader) {
1591    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1592        return;
1593    }
1594
1595#if XML_PARSER_DEBUG
1596    printf("\n  clusters set\n");
1597#endif
1598
1599    int status = xmlTextReaderRead(reader);
1600    while (status == 1) {
1601        const char * tag = (const char *) xmlTextReaderConstName(reader);
1602
1603        if (strcmp(tag, "cluster")    == 0) {
1604            clusterNode(reader);
1605        }
1606        else if (strcmp(tag, "#text")      == 0) { }
1607        else if (strcmp(tag, "#comment")   == 0) { }
1608        else if (strcmp(tag, "clusterset") == 0) {
1609            // checking source file consistency
1610            if (cluster_index != header->clusters) {
1611                printf("[XML ERROR] Wrong number of clusters\n");
1612                exit(1);
1613            }
1614
1615            if (header->tty_clusterid == 0xFFFFFFFF) {
1616                printf("[XML ERROR] illegal or missing tty peripheral");
1617                exit(1);
1618            }
1619
1620#if XML_PARSER_DEBUG
1621            printf("  end cluster set\n\n");
1622#endif
1623            header->psegs = pseg_index;
1624            header->procs = proc_index;
1625            header->irqs = irq_index;
1626            header->coprocs = coproc_index;
1627            header->cp_ports = cp_port_index;
1628            return;
1629        }
1630        else {
1631            printf("[XML ERROR] Unknown tag in clusterset node : %s",tag);
1632            exit(1);
1633        }
1634        status = xmlTextReaderRead(reader);
1635    }
1636} // end clusterSetNode()
1637
1638
1639/////////////////////////////////////////////
1640void globalSetNode(xmlTextReaderPtr reader) {
1641    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1642        return;
1643    }
1644
1645#if XML_PARSER_DEBUG
1646    printf("  globals set\n");
1647#endif
1648
1649    int status = xmlTextReaderRead(reader);
1650    while (status == 1) {
1651        const char * tag = (const char *) xmlTextReaderConstName(reader);
1652
1653        if (strcmp(tag, "vseg") == 0) {
1654            vsegNode(reader);
1655        }
1656        else if (strcmp(tag, "#text")     == 0) { }
1657        else if (strcmp(tag, "#comment")  == 0) { }
1658        else if (strcmp(tag, "globalset") == 0) {
1659#if XML_PARSER_DEBUG
1660            printf("  end global set\n\n");
1661#endif
1662            header->globals = vseg_index;
1663            vseg_loc_index = 0;
1664            return;
1665        }
1666        else {
1667            printf("[XML ERROR] Unknown tag in globalset node : %s",tag);
1668            exit(1);
1669        }
1670        status = xmlTextReaderRead(reader);
1671    }
1672} // end globalSetNode()
1673
1674
1675/////////////////////////////////////////////
1676void vspaceSetNode(xmlTextReaderPtr reader) {
1677    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1678        return;
1679    }
1680
1681#if XML_PARSER_DEBUG
1682    printf("\n  vspaces set\n");
1683#endif
1684
1685    int status = xmlTextReaderRead ( reader );
1686    while (status == 1) {
1687        const char * tag = (const char *) xmlTextReaderConstName(reader);
1688
1689        if (strcmp(tag, "vspace") == 0) {
1690            vspaceNode(reader);
1691        }
1692        else if (strcmp(tag, "#text"    ) == 0 ) { }
1693        else if (strcmp(tag, "#comment" ) == 0 ) { }
1694        else if (strcmp(tag, "vspaceset") == 0 ) {
1695            // checking source file consistency
1696            if (vspace_index != header->vspaces) {
1697                printf("[XML ERROR] Wrong number of vspaces\n");
1698                exit(1);
1699            }
1700            else {
1701                header->vsegs = vseg_index;
1702                header->vobjs = vobj_index;
1703                header->tasks = task_index;
1704                return;
1705            }
1706        }
1707        else {
1708            printf("[XML ERROR] Unknown tag in vspaceset node : %s",tag);
1709            exit(1);
1710        }
1711        status = xmlTextReaderRead(reader);
1712    }
1713} // end globalSetNode()
1714
1715
1716//////////////////////////////////////////
1717void headerNode(xmlTextReaderPtr reader) {
1718    char * name;
1719    unsigned int value;
1720    unsigned int ok;
1721
1722    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
1723        return;
1724    }
1725
1726#if XML_PARSER_DEBUG
1727    printf("mapping_info\n");
1728#endif
1729
1730    header = (mapping_header_t *) malloc(sizeof(mapping_header_t));
1731
1732    ////////// get name attribute
1733    name = getStringValue(reader, "name", &ok);
1734    if (ok) {
1735#if XML_PARSER_DEBUG
1736        printf("  name = %s\n", name);
1737#endif
1738        strncpy( header->name, name, 31);
1739    }
1740    else {
1741        printf("[XML ERROR] illegal or missing <name> attribute in header\n");
1742        exit(1);
1743    }
1744
1745    /////////// get cluster_x attribute
1746    cluster_x = getIntValue(reader, "cluster_x", &ok);
1747    if (ok) {
1748#if XML_PARSER_DEBUG
1749        printf("  cluster_x = %d\n", cluster_x);
1750#endif
1751        header->cluster_x = cluster_x;
1752    }
1753    else {
1754        printf("[XML ERROR] illegal or missing <cluster_x> attribute in header\n");
1755        exit(1);
1756    }
1757
1758    /////////// get cluster_y attribute
1759    cluster_y = getIntValue(reader, "cluster_y", &ok);
1760    if (ok) {
1761#if XML_PARSER_DEBUG
1762        printf("  cluster_y = %d\n", cluster_y);
1763#endif
1764        header->cluster_y = cluster_y;
1765    }
1766    else {
1767        printf("[XML ERROR] illegal or missing <cluster_y> attribute in header\n");
1768        exit(1);
1769    }
1770
1771    //check the number of cluster
1772    value = cluster_x * cluster_y;
1773    if (value >= MAX_CLUSTERS) {
1774        printf("[XML ERROR] The number of clusters is larger than %d\n", MAX_CLUSTERS);
1775        exit(1);
1776    }
1777
1778    header->clusters  = value;
1779
1780#if XML_PARSER_DEBUG
1781    printf("  clusters = %d\n", value);
1782#endif
1783
1784    ///////// get vspaces attribute
1785    value = getIntValue(reader, "vspaces", &ok);
1786    if (ok) {
1787        if (value >= MAX_VSPACES) {
1788            printf("[XML ERROR] The number of vspaces is larger than %d\n", MAX_VSPACES);
1789            exit(1);
1790        }
1791#if XML_PARSER_DEBUG
1792        printf("  vspaces = %d\n", value);
1793#endif
1794        header->vspaces  = value;
1795    }
1796    else {
1797        printf("[XML ERROR] illegal or missing <vspaces> attribute in mapping\n");
1798        exit(1);
1799    }
1800
1801    //////// initialise non replicated peripherals cluster_id
1802    header->tty_clusterid = 0xFFFFFFFF;
1803    header->nic_clusterid = 0xFFFFFFFF;
1804    header->ioc_clusterid = 0xFFFFFFFF;
1805    header->fbf_clusterid = 0xFFFFFFFF;
1806
1807    ///////// set signature
1808    header->signature = IN_MAPPING_SIGNATURE;
1809
1810    int status = xmlTextReaderRead(reader);
1811    while (status == 1) {
1812        const char * tag = (const char *) xmlTextReaderConstName(reader);
1813
1814        if (strcmp(tag, "clusterset") == 0) {
1815            clusterSetNode(reader);
1816        }
1817        else if (strcmp(tag, "globalset")    == 0) { globalSetNode(reader); }
1818        else if (strcmp(tag, "vspaceset")    == 0) { vspaceSetNode(reader); }
1819        else if (strcmp(tag, "#text")        == 0) { }
1820        else if (strcmp(tag, "#comment")     == 0) { }
1821        else if (strcmp(tag, "mapping_info") == 0) {
1822#if XML_PARSER_DEBUG
1823            printf("end mapping_info\n");
1824#endif
1825            return;
1826        }
1827        else {
1828            printf("[XML ERROR] Unknown tag in header node : %s\n",tag);
1829            exit(1);
1830        }
1831        status = xmlTextReaderRead(reader);
1832    }
1833} // end headerNode()
1834
1835
1836///////////////////////////////////////
1837void BuildTable(int fdout, const char * type, unsigned int nb_elem,
1838                unsigned int elem_size, char ** table) {
1839    unsigned int i;
1840    // write element
1841    for (i = 0; i < nb_elem; i++) {
1842        if (elem_size != write(fdout, table[i], elem_size)) {
1843            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
1844            exit(1);
1845        }
1846
1847#if XML_PARSER_DEBUG
1848        printf("Building binary: writing %s %d\n", type, i);
1849#endif
1850    }
1851}
1852
1853
1854int open_file(const char * file_path) {
1855
1856    //open file
1857    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
1858    if (fdout < 0) {
1859        perror("open");
1860        exit(1);
1861    }
1862
1863    //reinitialise the file
1864    if (ftruncate(fdout, 0)) {
1865        perror("truncate");
1866        exit(1);
1867    }
1868
1869    //#if XML_PARSER_DEBUG
1870    printf("%s\n", file_path);
1871    //#endif
1872
1873    return fdout;
1874}
1875
1876
1877///////////////////////////
1878void buildBin(const char * file_path) {
1879    unsigned int length;
1880
1881    int fdout = open_file(file_path);
1882
1883    // write header to binary file
1884    length = write(fdout, (char *) header, sizeof(mapping_header_t));
1885    if (length != sizeof(mapping_header_t)) {
1886        printf("write header error : length = %d \n", length);
1887        exit(1);
1888    }
1889
1890    // write clusters
1891    BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char **) cluster);
1892    // write psegs
1893    BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char **) pseg);
1894    // write vspaces
1895    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char **) vspace);
1896    // write vsegs
1897    BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char **) vseg);
1898    // write vobjs
1899    BuildTable(fdout, "vobj", vobj_index, sizeof(mapping_vobj_t), (char **) vobj);
1900    // write tasks array
1901    BuildTable(fdout, "task", task_index, sizeof(mapping_task_t), (char **) task);
1902    //building procs array
1903    BuildTable(fdout, "proc", proc_index, sizeof(mapping_proc_t), (char **) proc);
1904    //building irqs array
1905    BuildTable(fdout, "irq", irq_index, sizeof(mapping_irq_t), (char **) irq);
1906    //building coprocs array
1907    BuildTable(fdout, "coproc", coproc_index, sizeof(mapping_coproc_t), (char **) coproc);
1908    //building cp_ports array
1909    BuildTable(fdout, "cp_port", cp_port_index, sizeof(mapping_cp_port_t),(char **) cp_port);
1910    //building periphs array
1911    BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char **) periph);
1912
1913    close(fdout);
1914
1915} // end buildBin()
1916
1917
1918///////////////////////////////////////////////////////////////////////
1919// this function set the value the vobj_id fiels of all cp_ports
1920///////////////////////////////////////////////////////////////////////
1921void prepareBuild() {
1922    unsigned int i;
1923    //asign for all cp_ports the correct vspaceid and vobjid
1924    for (i = 0; i < cp_port_index; i++) {
1925        int vspace_id = getVspaceId(cp_port_vobj_ref[i]->vspace_name);
1926        if (vspace_id < 0) {
1927            printf("[XML ERROR] illegal  <vspacename> for cp_port %d,\n", i);
1928            exit(1);
1929        }
1930        cp_port[i]->vspaceid = vspace_id;
1931
1932        int vobj_id = getVobjLocId(vspace_id, cp_port_vobj_ref[i]->vobj_name, vspace[vspace_id]->vobjs);
1933        if (vobj_id >= 0) {
1934
1935#if XML_PARSER_DEBUG
1936            printf("\ncp_port = %d\n", i);
1937            printf("      vspace_name  = %s\n", cp_port_vobj_ref[i]->vspace_name);
1938            printf("      vobj_name    = %s\n", cp_port_vobj_ref[i]->vobj_name);
1939            printf("      vobj_index   = %d\n", vobj_id);
1940#endif
1941            cp_port[i]->vobjlocid = vobj_id;
1942
1943            assert((vobj[ vspace[vspace_id]->vobj_offset + vobj_id]->type == VOBJ_TYPE_MWMR)
1944                    && "coproc ports have to refer to a vobj of type MWMR");
1945        }
1946        else {
1947            printf("[XML ERROR] illegal  <vobjname> for cp_port %d,\n", i);
1948            exit(1);
1949        }
1950    }
1951}
1952
1953
1954//////////////////////////////////////////
1955void file_write(int fdout, char * towrite) {
1956    unsigned int size = strlen(towrite);
1957    if (size != write(fdout, towrite, size)) {
1958        printf("file_write error");
1959        exit(1);
1960    }
1961}
1962
1963
1964//////////////////////////////////////////////////
1965void def_int_write(int fdout, char * def, int num) {
1966    char  buf[64];
1967    sprintf(buf, "#define\t %s  %d\n", def, num);
1968    file_write(fdout, buf);
1969}
1970
1971
1972/////////////////////////////////////////////////
1973void def_hex_write(int fdout, char * def, int num) {
1974    char  buf[64];
1975    sprintf(buf, "#define\t %s  0x%x\n", def, num);
1976    file_write(fdout, buf);
1977}
1978
1979
1980///////////////////////////////////////
1981void  genHd(const char * file_path) {
1982    int fdout = open_file(file_path);
1983
1984    char * prol = " /* Generated from the mapping_info file */\n\n#ifndef _HD_CONFIG_H\n#define _HD_CONFIG_H\n\n";
1985    file_write(fdout, prol);
1986
1987    def_int_write(fdout, "CLUSTER_X"    , cluster_x);
1988    def_int_write(fdout, "CLUSTER_Y"    , cluster_y);
1989    def_int_write(fdout, "NB_CLUSTERS"  , cluster_index);
1990    def_hex_write(fdout, "CLUSTER_SIZE" , (((unsigned long long) 1) << 32) / cluster_index);
1991    def_int_write(fdout, "NB_PROCS_MAX" , nb_proc_max);
1992    def_int_write(fdout, "NB_TIMERS_MAX", nb_timer_channel_max);
1993    def_int_write(fdout, "NB_DMAS_MAX"  , nb_dma_channel_max);
1994    def_int_write(fdout, "NB_TTYS"      , nb_tty_channel);
1995    def_int_write(fdout, "NB_IOCS"      , nb_ioc_channel);
1996    def_int_write(fdout, "NB_NICS"      , nb_nic_channel);
1997
1998    file_write(fdout, "\n");
1999    def_int_write(fdout, "USE_XICU"     , use_xicu);
2000    def_int_write(fdout, "IOMMU_ACTIVE ", io_mmu_active);
2001
2002    char * epil = "\n#endif //_HD_CONFIG_H";
2003    file_write(fdout, epil);
2004
2005    close(fdout);
2006}
2007
2008
2009////////////////////////////////////////////////////////
2010void ld_write(int fdout, char * seg, unsigned int addr) {
2011    char buf[64];
2012    sprintf(buf, "%s = 0x%x;\n", seg, addr);
2013    file_write(fdout, buf);
2014
2015}
2016
2017
2018///////////////////////////////////////
2019void genLd(const char * file_path) {
2020    int fdout = open_file(file_path);
2021
2022    char * prol = "/* Generated from the mapping_info file */\n\n";
2023    file_write(fdout, prol);
2024
2025    //boot
2026    ld_write(fdout, "seg_boot_code_base", boot_code_base);
2027    ld_write(fdout, "seg_boot_stack_base", boot_stack_base);
2028    ld_write(fdout, "seg_mapping_base", boot_mapping_base);
2029
2030    //kernel
2031    ld_write(fdout, "\nseg_kernel_code_base",  kernel_code_base);
2032    ld_write(fdout, "seg_kernel_data_base",    kernel_data_base);
2033    ld_write(fdout, "seg_kernel_uncdata_base", kernel_uncdata_base);
2034    ld_write(fdout, "seg_kernel_init_base",    kernel_init_base);
2035
2036    //peripherals
2037    ld_write(fdout, "\nseg_fbf_base", fbf_base_offset);
2038    ld_write(fdout, "seg_icu_base",   icu_base_offset);
2039    ld_write(fdout, "seg_ioc_base",   ioc_base_offset);
2040    ld_write(fdout, "seg_nic_base",   nic_base_offset);
2041    ld_write(fdout, "seg_tty_base",   tty_base_offset);
2042    ld_write(fdout, "seg_dma_base",   dma_base_offset);
2043    ld_write(fdout, "seg_tim_base",   tim_base_offset);
2044    ld_write(fdout, "seg_gcd_base",   gcd_base_offset);
2045    ld_write(fdout, "seg_iob_base",   iob_base_offset);
2046
2047    close(fdout);
2048}
2049
2050
2051char * buildPath(const char * path, const char * name) {
2052    char * res = calloc(strlen(path) + strlen(name) + 1, 1);
2053    strcat(res, path);
2054    strcat(res, "/");
2055    strcat(res, name);
2056    return res; 
2057}
2058
2059
2060/////////////////////////////////////
2061int main(int argc, char * argv[]) {
2062    if (argc < 3) {
2063        printf("Usage: xml2bin <input_file_path> <output_path>\n");
2064        return 1;
2065    }
2066
2067    struct stat dir_st;
2068    if (stat( argv[2], &dir_st)) {
2069        perror("bad path");
2070        exit(1);
2071    }
2072
2073    if ((dir_st.st_mode & S_IFDIR) == 0) {
2074        printf("path is not a dir: %s", argv[2] );
2075        exit(1);
2076    }
2077
2078
2079    char * map_path = buildPath(argv[2], "map.bin"); 
2080    char * ld_path = buildPath(argv[2], "giet_vsegs.ld"); 
2081    char * hd_path = buildPath(argv[2], "hard_config.h"); 
2082
2083    LIBXML_TEST_VERSION;
2084
2085    int status;
2086    xmlTextReaderPtr reader = xmlReaderForFile(argv[1], NULL, 0);
2087
2088    if (reader != NULL) {
2089        status = xmlTextReaderRead (reader);
2090        while (status == 1) {
2091            const char * tag = (const char *) xmlTextReaderConstName(reader);
2092
2093            if (strcmp(tag, "mapping_info") == 0) { 
2094                headerNode(reader);
2095                prepareBuild();
2096                buildBin(map_path);
2097                genHd(hd_path);
2098                genLd(ld_path);
2099            }
2100            else {
2101                printf("[XML ERROR] Wrong file type: \"%s\"\n", argv[1]);
2102                return 1;
2103            }
2104            status = xmlTextReaderRead(reader);
2105        }
2106        xmlFreeTextReader(reader);
2107
2108        if (status != 0) {
2109            printf("[XML ERROR] Wrong Syntax in \"%s\" file\n", argv[1]);
2110            return 1;
2111        }
2112    }
2113    return 0;
2114} // end main()
2115
2116
2117// Local Variables:
2118// tab-width: 4
2119// c-basic-offset: 4
2120// c-file-offsets:((innamespace . 0)(inline-open . 0))
2121// indent-tabs-mode: nil
2122// End:
2123// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
2124
Note: See TracBrowser for help on using the repository browser.