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

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