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

Last change on this file since 215 was 215, checked in by karaoui, 12 years ago

New components are now mandotory in the XML description:

The files giet_vsegs.ld and hard_config.h are now autogenerated by the xml2bin tool.

File size: 64.3 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        else if ( strcmp( str, "DMA" ) == 0 ) 
1133        {
1134            periph[periph_index]->type = PERIPH_TYPE_DMA;
1135            if(found_dma) error = 1;
1136            found_dma = 1;
1137
1138            if(dma_base_offset == 0xFFFFFFFF)
1139                dma_base_offset = pseg[ periph[periph_index]->psegid ]->base;
1140            if(nb_dma_channel_max < periph[periph_index]->channels)
1141                nb_dma_channel_max = periph[periph_index]->channels;
1142        }
1143        else
1144        {
1145            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
1146                   periph_loc_index, cluster_index);
1147            exit(1);
1148        }
1149
1150        if(error)
1151        {
1152            printf("[XML ERROR] illegal <type> for peripheral %d in cluster %d\n",
1153                   periph_loc_index, cluster_index);
1154            exit(1);
1155        }
1156    }
1157    else
1158    {
1159        printf("[XML ERROR] missing <type> for peripheral  %d in cluster %d\n",
1160               periph_loc_index, cluster_index);
1161        exit(1);
1162    }
1163
1164   
1165    periph_index++;
1166    periph_loc_index++;
1167    cluster[cluster_index]->periphs++;
1168
1169} // end periphNode
1170
1171/////////////////////////////////////////
1172void coprocNode ( xmlTextReaderPtr reader )
1173/////////////////////////////////////////
1174{
1175    char*           str;
1176    unsigned int        ok;
1177
1178    cp_port_loc_index = 0;
1179
1180    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1181
1182    if ( coproc_index >= MAX_COPROCS )
1183    {
1184        printf("[XML ERROR] The number of coprocs is larger than %d\n", MAX_COPROCS);
1185    }
1186
1187#if XML_PARSER_DEBUG
1188printf("\n  coproc %d\n", coproc_index);
1189#endif
1190
1191    coproc[coproc_index] = (mapping_coproc_t*)malloc(sizeof(mapping_coproc_t));
1192
1193    /////////// get name attribute
1194    str = getStringValue( reader, "name", &ok );
1195    if ( ok ) 
1196    {
1197#if XML_PARSER_DEBUG
1198printf("      name = %s\n", str);
1199#endif
1200        strncpy(coproc[coproc_index]->name, str, 31);
1201    }
1202    else
1203    {
1204        printf("[XML ERROR] illegal or missing <name> for coproc %d in cluster %d\n",
1205               coproc_index, cluster_index);
1206        exit(1);
1207    }
1208
1209    /////////// get psegname attribute
1210    str = getStringValue(reader,"psegname", &ok);
1211    if ( ok == 0 )
1212    {
1213        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1214                 coproc_index, cluster_index);
1215        exit(1);
1216    }
1217
1218    /////////// set psegid attribute
1219    int index = getPsegId( cluster_index, str );
1220    if ( index >= 0 ) 
1221    {
1222#if XML_PARSER_DEBUG
1223printf("      clusterid = %d\n", cluster_index);
1224printf("      psegname  = %s\n", str);
1225printf("      psegid    = %d\n", index);
1226#endif
1227        coproc[coproc_index]->psegid = index;
1228        assert(pseg[index]->type == PSEG_TYPE_PERI && "coproc psegname attribute must refer to a pseg of type PERI" );
1229    }
1230    else             
1231    {
1232        printf("[XML ERROR] pseg not found for coproc %d / clusterid = %d / psegname = %s\n", 
1233                   coproc_index, cluster_index, str );
1234        exit(1);
1235    } 
1236
1237    ////////// set port_offset
1238    coproc[coproc_index]->port_offset = cp_port_index;
1239
1240#if XML_PARSER_DEBUG
1241printf("      port_offset = %d\n", cp_port_index);
1242#endif
1243
1244    int status = xmlTextReaderRead(reader);
1245    while ( status == 1 ) 
1246    {
1247        const char* tag = (const char*)xmlTextReaderConstName(reader);
1248
1249        if      ( strcmp(tag, "port")     == 0 ) cpPortNode(reader);
1250        else if ( strcmp(tag, "#text")    == 0 ) { }
1251        else if ( strcmp(tag, "#comment") == 0 ) { }
1252        else if ( strcmp(tag, "coproc")   == 0 ) 
1253        {
1254            coproc[coproc_index]->ports = cp_port_loc_index;
1255            cluster[cluster_index]->coprocs++;
1256            coproc_loc_index++;
1257            coproc_index++;
1258            return;
1259        }
1260        else 
1261        {
1262            printf("[XML ERROR] Unknown tag %s",tag);
1263            exit(1);
1264        }
1265        status = xmlTextReaderRead ( reader );
1266    }
1267} // end coprocNode()
1268
1269///////////////////////////////////////
1270void irqNode( xmlTextReaderPtr reader )
1271///////////////////////////////////////
1272{
1273    unsigned int        ok;
1274    unsigned int        value;
1275    char*               str;
1276
1277    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1278
1279    if ( irq_index >= MAX_IRQS )
1280    {
1281        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
1282    }
1283
1284#if XML_PARSER_DEBUG
1285printf("     irq %d\n", irq_loc_index);
1286#endif
1287
1288    irq[irq_index] = (mapping_irq_t*)malloc(sizeof(mapping_irq_t));
1289
1290    ///////// get type attribute
1291    str = getStringValue(reader,"type", &ok);
1292    if ( ok )
1293    {
1294#if XML_PARSER_DEBUG
1295printf("        type    = %s\n", str);
1296#endif
1297        if      ( strcmp(str, "HARD") == 0 ) irq[irq_index]->type = 0;
1298        else if ( strcmp(str, "SOFT") == 0 ) irq[irq_index]->type = 1;
1299        else
1300        {
1301            printf("[XML ERROR] undefined IRQ  <type> for processor %d in cluster %d\n",
1302                   cluster_index, proc_loc_index );
1303            exit(1);
1304        }
1305    } 
1306    else
1307    {
1308        printf("[XML ERROR] missing IRQ <type> for processor %d in cluster %d\n",
1309               cluster_index, proc_loc_index );
1310        exit(1);
1311    }
1312
1313    ///////// get icuid attribute
1314    value = getIntValue(reader, "icuid", &ok);
1315    if ( ok )
1316    {
1317#if XML_PARSER_DEBUG
1318printf("        icuid   = %d\n", value);
1319#endif
1320        irq[irq_index]->icuid = value;
1321        if ( value >= 32 )
1322        {
1323            printf("[XML ERROR] IRQ <icuid> too large for processor %d in cluster %d\n",
1324                   cluster_index, proc_loc_index );
1325            exit(1);
1326        }
1327    }
1328    else
1329    {
1330        printf("[XML ERROR] missing IRQ <icuid> for processor %d in cluster %d\n",
1331               cluster_index, proc_loc_index );
1332        exit(1);
1333    }
1334
1335    ///////// get isr attribute
1336    str = getStringValue(reader,"isr", &ok);
1337    if ( ok )
1338    {
1339#if XML_PARSER_DEBUG
1340printf("        isr     = %s\n", str);
1341#endif
1342        if      ( strcmp(str, "ISR_SWITCH" ) == 0 ) irq[irq_index]->isr = ISR_SWITCH;
1343        else if ( strcmp(str, "ISR_IOC"    ) == 0 ) irq[irq_index]->isr = ISR_IOC;
1344        else if ( strcmp(str, "ISR_DMA"    ) == 0 ) irq[irq_index]->isr = ISR_DMA;
1345        else if ( strcmp(str, "ISR_TTY"    ) == 0 ) irq[irq_index]->isr = ISR_TTY;
1346        else if ( strcmp(str, "ISR_TIMER"  ) == 0 ) irq[irq_index]->isr = ISR_TIMER;
1347        else
1348        {
1349            printf("[XML ERROR] illegal IRQ <isr> for processor %d in cluster %d\n",
1350                   cluster_index, proc_loc_index );
1351            exit(1);
1352        }
1353#if XML_PARSER_DEBUG
1354printf("        isrnum  = %d\n", irq[irq_index]->isr);
1355#endif
1356    } 
1357    else
1358    {
1359        printf("[XML ERROR] missing IRQ <isr> for processor %d in cluster %d\n",
1360               cluster_index, proc_loc_index );
1361        exit(1);
1362    }
1363
1364    ///////// get channel attribute (optionnal : 0 if missing)
1365    value = getIntValue(reader, "channel", &ok);
1366    if ( ok )
1367    {
1368#if XML_PARSER_DEBUG
1369printf("        channel = %d\n", value);
1370#endif
1371        irq[irq_index]->channel = value;
1372    }
1373    else
1374    {
1375        irq[irq_index]->channel = 0;
1376    }
1377
1378    irq_index++;
1379    irq_loc_index++;
1380
1381} // end irqNode
1382
1383/////////////////////////////////////////
1384void procNode ( xmlTextReaderPtr reader )
1385/////////////////////////////////////////
1386{
1387    unsigned int        ok;
1388    unsigned int    value;
1389
1390    irq_loc_index = 0;
1391
1392    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1393
1394    if ( proc_index >= MAX_PROCS )
1395    {
1396        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
1397    }
1398
1399#if XML_PARSER_DEBUG
1400printf("\n  proc %d\n", proc_index);
1401#endif
1402
1403    proc[proc_index] = (mapping_proc_t*)malloc(sizeof(mapping_proc_t));
1404
1405
1406    /////////// get index attribute (optional)
1407    value = getIntValue(reader,"index",&ok);
1408    if ( ok && (value != proc_loc_index) )
1409    {
1410            printf("[XML ERROR] wrong proc index / expected value is %d", 
1411                proc_loc_index);
1412            exit(1);
1413    }
1414
1415    ////////// set irq_offset attribute
1416    proc[proc_index]->irq_offset = irq_index;
1417
1418#if XML_PARSER_DEBUG
1419printf("    irq_offset = %d\n", irq_index);
1420#endif
1421
1422    int status = xmlTextReaderRead(reader);
1423    while ( status == 1 ) 
1424    {
1425        const char* tag = (const char*)xmlTextReaderConstName(reader);
1426
1427        if      ( strcmp(tag, "irq")      == 0 ) irqNode(reader);
1428        else if ( strcmp(tag, "#text")    == 0 ) { }
1429        else if ( strcmp(tag, "#comment") == 0 ) { }
1430        else if ( strcmp(tag, "proc")     == 0 ) 
1431        {
1432            proc[proc_index]->irqs = irq_loc_index;
1433            cluster[cluster_index]->procs++;
1434            proc_loc_index++;
1435            proc_index++;
1436            return;
1437        }
1438        else 
1439        {
1440            printf("[XML ERROR] Unknown tag %s",tag);
1441            exit(1);
1442        }
1443        status = xmlTextReaderRead ( reader );
1444    }
1445} // end procNode()
1446
1447
1448//////////////////////////////////////////
1449void  psegNode ( xmlTextReaderPtr reader )
1450//////////////////////////////////////////
1451{
1452    unsigned int        ok;
1453    unsigned int        value;
1454    char*               str;
1455
1456    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1457
1458    if ( pseg_index >= MAX_PSEGS )
1459    {
1460        printf("[XML ERROR] The number of psegs is larger than %d\n", MAX_PSEGS);
1461        exit(1);
1462    }
1463
1464#if XML_PARSER_DEBUG
1465printf("    pseg %d\n", pseg_index);
1466#endif
1467
1468    pseg[pseg_index] = (mapping_pseg_t*)malloc(sizeof(mapping_pseg_t));
1469
1470    /////// get name attribute
1471    str = getStringValue( reader, "name", &ok );
1472#if XML_PARSER_DEBUG
1473printf("      name = %s\n", str);
1474#endif
1475    if ( ok ) 
1476    {
1477        strncpy(pseg[pseg_index]->name, str, 31);
1478    }
1479    else
1480    {
1481        printf("[XML ERROR] illegal or missing <name> for pseg %d in cluster %d\n",
1482               pseg_index, cluster_index);
1483        exit(1);
1484    }
1485
1486    //////// get type attribute
1487    str = getStringValue(reader, "type", &ok);
1488#if XML_PARSER_DEBUG
1489printf("      type = %s\n", str);
1490#endif
1491    if      (ok && (strcmp(str, "RAM" ) == 0)) pseg[pseg_index]->type = PSEG_TYPE_RAM;
1492    else if (ok && (strcmp(str, "ROM" ) == 0)) pseg[pseg_index]->type = PSEG_TYPE_ROM;
1493    else if (ok && (strcmp(str, "PERI") == 0)) pseg[pseg_index]->type = PSEG_TYPE_PERI;
1494    else
1495    {
1496        printf("[XML ERROR] illegal or missing <type> for pseg %s in cluster %d\n",
1497        pseg[pseg_index]->name, cluster_index);
1498        exit(1);
1499    }
1500
1501    //////// get base attribute
1502    value = getIntValue( reader, "base", &ok );
1503#if XML_PARSER_DEBUG
1504printf("      base = 0x%x\n", value);
1505#endif
1506    if ( ok ) 
1507    {
1508        pseg[pseg_index]->base = value;
1509    }
1510    else
1511    {
1512        printf("[XML ERROR] illegal or missing <base> for pseg %s in cluster %d\n",
1513        pseg[pseg_index]->name, cluster_index);
1514        exit(1);
1515    }
1516
1517    //////// get length attribute
1518    value = getIntValue( reader, "length", &ok );
1519#if XML_PARSER_DEBUG
1520printf("      length = 0x%x\n", value);
1521#endif
1522    if ( ok )
1523    {
1524        pseg[pseg_index]->length = value;
1525    } 
1526    else
1527    {
1528        printf("[XML ERROR] illegal or missing <length> for pseg %s in cluster %d\n",
1529        pseg[pseg_index]->name, cluster_index);
1530        exit(1);
1531    }
1532
1533    //////// set cluster attribute
1534    pseg[pseg_index]->cluster = cluster_index;
1535
1536    pseg_index++;
1537    cluster[cluster_index]->psegs++;
1538} // end psegNode()
1539
1540/////////////////////////////////////////////
1541void  clusterNode ( xmlTextReaderPtr reader )
1542/////////////////////////////////////////////
1543{
1544    unsigned int ok;
1545    unsigned int value;
1546
1547    cluster[cluster_index] = (mapping_cluster_t*)malloc(sizeof(mapping_cluster_t));
1548   
1549    //initialise all variables
1550    //they will be incremented by *Node() functions
1551    //FIXME: calloc?
1552    cluster[cluster_index]->psegs = 0;
1553    cluster[cluster_index]->procs = 0;
1554    cluster[cluster_index]->coprocs = 0;
1555    cluster[cluster_index]->periphs = 0;
1556
1557
1558    //initialise global variables
1559    //TODO: delete those three
1560    proc_loc_index = 0;
1561    coproc_loc_index = 0;
1562    periph_loc_index = 0;
1563
1564    // for replicated periph
1565    found_timer = 0;
1566    found_icu = 0;
1567    found_xicu = 0;
1568    found_dma = 0;
1569
1570    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1571
1572    // checking source file consistency
1573    if ( cluster_index >= header->clusters )
1574    {
1575        printf("[XML ERROR] The cluster index is too large : %d\n", cluster_index);
1576        exit(1);
1577    }
1578
1579#if XML_PARSER_DEBUG
1580printf("  cluster %d\n", cluster_index);
1581#endif
1582
1583
1584    /////////// check cluster index attribute (optional)
1585    value = getIntValue(reader,"index",&ok);
1586    if ( ok && (value != cluster_index) )
1587    {
1588            printf("[XML ERROR] wrong cluster index / expected value is %d", 
1589                cluster_index);
1590            exit(1);
1591    }
1592
1593    ////////// set offsets
1594    cluster[cluster_index]->pseg_offset   = pseg_index;
1595    cluster[cluster_index]->proc_offset   = proc_index;
1596    cluster[cluster_index]->coproc_offset = coproc_index;
1597    cluster[cluster_index]->periph_offset = periph_index;
1598
1599#if XML_PARSER_DEBUG
1600printf("    pseg_offset   = %d\n", pseg_index);
1601printf("    proc_offset   = %d\n", proc_index);
1602printf("    coproc_offset = %d\n", coproc_index);
1603printf("    periph_offset = %d\n", coproc_index);
1604#endif
1605
1606    ////////// get psegs, procs, coprocs and periphs
1607    int status = xmlTextReaderRead(reader);
1608
1609    while ( status == 1 )
1610    {
1611        const char* tag = (const char*)xmlTextReaderConstName(reader);
1612
1613        if      ( strcmp(tag, "pseg")     == 0 ) psegNode(reader);
1614        else if ( strcmp(tag, "proc")     == 0 ) procNode(reader);
1615        else if ( strcmp(tag, "coproc")   == 0 ) coprocNode(reader);
1616        else if ( strcmp(tag, "periph")   == 0 ) periphNode(reader);
1617        else if ( strcmp(tag, "#text")    == 0 ) { }
1618        else if ( strcmp(tag, "#comment") == 0 ) { }
1619        else if ( strcmp(tag, "cluster")  == 0 ) 
1620        {
1621
1622            if(use_xicu == 0xFFFFFFFF)
1623                use_xicu = found_xicu;
1624
1625            ////////////////// peripherals checks ////////////////////
1626            if( (found_timer  && use_xicu) || (!found_timer  && !use_xicu) )
1627            {
1628                printf("[XML ERROR] illegal or missing timer peripheral in cluster %d\n", cluster_index);
1629                exit(1);
1630            }
1631
1632            if( (found_icu  && use_xicu) || (!found_icu  && !use_xicu) )
1633            {
1634                printf("[XML ERROR] illegal or missing icu peripheral in cluster %d\n", cluster_index);
1635                exit(1);
1636            }
1637
1638            if( !found_xicu && use_xicu)
1639            {
1640                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
1641                exit(1);
1642            }
1643
1644            if(!found_dma)
1645            {
1646                printf("[XML ERROR] illegal or missing dma peripheral in cluster %d\n", cluster_index);
1647                exit(1);
1648            }
1649
1650               
1651            if(nb_proc_max < cluster[cluster_index]->procs)
1652                nb_proc_max = cluster[cluster_index]->procs;
1653
1654#if XML_PARSER_DEBUG
1655printf("    psegs   = %d\n", cluster[cluster_index]->psegs);
1656printf("    procs   = %d\n", cluster[cluster_index]->procs);
1657printf("    coprocs = %d\n", cluster[cluster_index]->coprocs);
1658printf("    periphs = %d\n", cluster[cluster_index]->periphs);
1659printf("    end cluster %d\n", cluster_index);
1660#endif
1661            cluster_index++;
1662            return;
1663        }
1664        status = xmlTextReaderRead(reader);
1665    }
1666} // end clusterNode()
1667
1668//////////////////////////////////////////////
1669void clusterSetNode( xmlTextReaderPtr reader )
1670//////////////////////////////////////////////
1671{
1672    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1673
1674#if XML_PARSER_DEBUG
1675printf("\n  clusters set\n");
1676#endif
1677
1678    int status = xmlTextReaderRead(reader);
1679    while ( status == 1 ) 
1680    {
1681        const char* tag = (const char*)xmlTextReaderConstName(reader);
1682
1683        if      ( strcmp(tag, "cluster")    == 0 ) clusterNode(reader);
1684        else if ( strcmp(tag, "#text")      == 0 ) { }
1685        else if ( strcmp(tag, "#comment")   == 0 ) { }
1686        else if ( strcmp(tag, "clusterset") == 0 ) 
1687        {
1688            // checking source file consistency
1689            if ( cluster_index != header->clusters )
1690            {
1691                printf("[XML ERROR] Wrong number of clusters\n");
1692                exit(1);
1693            }
1694
1695            if(header->tty_clusterid == 0xFFFFFFFF)
1696            {
1697                printf("[XML ERROR] illegal or missing tty peripheral");
1698                exit(1);
1699            }
1700
1701#if XML_PARSER_DEBUG
1702printf("  end cluster set\n\n");
1703#endif
1704            header->psegs    = pseg_index;
1705            header->procs    = proc_index;
1706            header->irqs     = irq_index;
1707            header->coprocs  = coproc_index;
1708            header->cp_ports = cp_port_index;
1709            return;
1710        }
1711        else 
1712        {
1713            printf("[XML ERROR] Unknown tag in clusterset node : %s",tag);
1714            exit(1);
1715        }
1716        status = xmlTextReaderRead(reader);
1717    }
1718} // end clusterSetNode()
1719
1720/////////////////////////////////////////////
1721void globalSetNode( xmlTextReaderPtr reader )
1722/////////////////////////////////////////////
1723{
1724    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1725
1726#if XML_PARSER_DEBUG
1727printf("  globals set\n");
1728#endif
1729
1730    int status = xmlTextReaderRead(reader);
1731    while ( status == 1 ) 
1732    {
1733        const char* tag = (const char*)xmlTextReaderConstName(reader);
1734
1735        if      ( strcmp(tag, "vseg")      == 0 ) vsegNode(reader);
1736        else if ( strcmp(tag, "#text"  )   == 0 ) { }
1737        else if ( strcmp(tag, "#comment")  == 0 ) { }
1738        else if ( strcmp(tag, "globalset") == 0 ) 
1739        {
1740#if XML_PARSER_DEBUG
1741printf("  end global set\n\n");
1742#endif
1743            header->globals = vseg_index;
1744            vseg_loc_index = 0;
1745            return;
1746        }
1747        else 
1748        {
1749            printf("[XML ERROR] Unknown tag in globalset node : %s",tag);
1750            exit(1);
1751        }
1752        status = xmlTextReaderRead ( reader );
1753    }
1754} // end globalSetNode()
1755
1756/////////////////////////////////////////////
1757void vspaceSetNode( xmlTextReaderPtr reader )
1758/////////////////////////////////////////////
1759{
1760    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1761
1762#if XML_PARSER_DEBUG
1763printf("\n  vspaces set\n");
1764#endif
1765
1766    int status = xmlTextReaderRead ( reader );
1767    while ( status == 1 ) 
1768    {
1769        const char* tag = (const char*)xmlTextReaderConstName(reader);
1770
1771        if      ( strcmp(tag, "vspace"   ) == 0 ) vspaceNode(reader);
1772        else if ( strcmp(tag, "#text"    ) == 0 ) { }
1773        else if ( strcmp(tag, "#comment" ) == 0 ) { }
1774        else if ( strcmp(tag, "vspaceset") == 0 ) 
1775        {
1776            // checking source file consistency
1777            if ( vspace_index != header->vspaces )
1778            {
1779                printf("[XML ERROR] Wrong number of vspaces\n");
1780                exit(1);
1781            }
1782            else
1783            {
1784                header->vsegs = vseg_index;
1785                header->vobjs = vobj_index;
1786                header->tasks = task_index;
1787                return;
1788            }
1789        }
1790        else 
1791        {
1792            printf("[XML ERROR] Unknown tag in vspaceset node : %s",tag);
1793            exit(1);
1794        }
1795        status = xmlTextReaderRead ( reader );
1796    }
1797} // end globalSetNode()
1798
1799//////////////////////////////////////////
1800void headerNode(xmlTextReaderPtr reader )
1801//////////////////////////////////////////
1802{
1803    char*               name;
1804    unsigned int        value;
1805    unsigned int        ok;
1806
1807    if ( xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT ) return;
1808
1809#if XML_PARSER_DEBUG
1810printf("mapping_info\n");
1811#endif
1812
1813    header = (mapping_header_t*)malloc(sizeof(mapping_header_t));
1814
1815    ////////// get name attribute
1816    name = getStringValue(reader, "name", &ok);
1817    if ( ok )
1818    {
1819#if XML_PARSER_DEBUG
1820printf("  name = %s\n", name);
1821#endif
1822        strncpy( header->name, name, 31);
1823    }     
1824    else
1825    {
1826        printf("[XML ERROR] illegal or missing <name> attribute in header\n");
1827        exit(1);
1828    }
1829
1830    /////////// get cluster_x attribute
1831    cluster_x = getIntValue(reader, "cluster_x", &ok);
1832    if ( ok )
1833    {
1834#if XML_PARSER_DEBUG
1835printf("  cluster_x = %d\n", cluster_x);
1836#endif
1837        header->cluster_x = cluster_x;
1838    }
1839    else
1840    {
1841        printf("[XML ERROR] illegal or missing <cluster_x> attribute in header\n");
1842        exit(1);
1843    }
1844
1845    /////////// get cluster_y attribute
1846    cluster_y = getIntValue(reader, "cluster_y", &ok);
1847    if ( ok )
1848    {
1849#if XML_PARSER_DEBUG
1850printf("  cluster_y = %d\n", cluster_y);
1851#endif
1852        header->cluster_y = cluster_y;
1853    }
1854    else
1855    {
1856        printf("[XML ERROR] illegal or missing <cluster_y> attribute in header\n");
1857        exit(1);
1858    }
1859
1860    //check the number of cluster
1861    value = cluster_x*cluster_y;
1862    if ( value >= MAX_CLUSTERS )
1863    {
1864        printf("[XML ERROR] The number of clusters is larger than %d\n", MAX_CLUSTERS);
1865        exit(1);
1866    }
1867       
1868    header->clusters  = value;
1869
1870#if XML_PARSER_DEBUG
1871printf("  clusters = %d\n", value);
1872#endif
1873
1874    ///////// get vspaces attribute
1875    value = getIntValue(reader, "vspaces", &ok);
1876    if ( ok )
1877    {
1878        if ( value >= MAX_VSPACES )
1879        {
1880            printf("[XML ERROR] The number of vspaces is larger than %d\n", MAX_VSPACES);
1881            exit(1);
1882        }
1883#if XML_PARSER_DEBUG
1884printf("  vspaces = %d\n", value);
1885#endif
1886        header->vspaces  = value;
1887    }
1888    else
1889    {
1890        printf("[XML ERROR] illegal or missing <vspaces> attribute in mapping\n");
1891        exit(1);
1892    }
1893
1894    //////// initialise non replicated peripherals cluster_id
1895    header->tty_clusterid = 0xFFFFFFFF;
1896    header->nic_clusterid = 0xFFFFFFFF;
1897    header->ioc_clusterid = 0xFFFFFFFF;
1898    header->fbf_clusterid = 0xFFFFFFFF;
1899
1900    ///////// set signature
1901    header->signature = IN_MAPPING_SIGNATURE;
1902
1903    int status = xmlTextReaderRead(reader);
1904    while ( status == 1 ) 
1905    {
1906        const char* tag = (const char*)xmlTextReaderConstName(reader);
1907
1908        if      ( strcmp(tag, "clusterset")   == 0 ) clusterSetNode(reader);
1909        else if ( strcmp(tag, "globalset")    == 0 ) globalSetNode(reader);
1910        else if ( strcmp(tag, "vspaceset")    == 0 ) vspaceSetNode(reader);
1911        else if ( strcmp(tag, "#text")        == 0 ) { }
1912        else if ( strcmp(tag, "#comment")     == 0 ) { }
1913        else if ( strcmp(tag, "mapping_info") == 0 ) 
1914        {
1915#if XML_PARSER_DEBUG
1916printf("end mapping_info\n");
1917#endif
1918            return;
1919        }
1920        else 
1921        {
1922            printf("[XML ERROR] Unknown tag in header node : %s\n",tag);
1923            exit(1);
1924        }
1925        status = xmlTextReaderRead(reader);
1926    }
1927} // end headerNode()
1928   
1929///////////////////////////////////////
1930void BuildTable( int fdout,
1931               const char* type,
1932               unsigned int nb_elem,
1933               unsigned int elem_size,
1934               char** table)       
1935////////////////////////////////////////
1936{
1937    unsigned int i;
1938    // write element
1939    for ( i = 0 ; i < nb_elem ; i++ )
1940    {
1941        if(elem_size != write(fdout, table[i], elem_size))
1942        {
1943            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
1944            exit(1);
1945        }
1946
1947#if XML_PARSER_DEBUG
1948printf("Building binary: writing %s %d\n", type, i);
1949#endif
1950    }
1951}
1952
1953int open_file(const char* file_path)
1954{
1955
1956    //open file
1957    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
1958    if ( fdout < 0)
1959    {
1960        perror("open");
1961        exit(1);
1962    }
1963
1964    //reinitialise the file
1965    if( ftruncate(fdout, 0) )
1966    {
1967        perror("truncate");
1968        exit(1);
1969    }
1970
1971//#if XML_PARSER_DEBUG
1972    printf("%s\n",file_path);
1973//#endif
1974
1975    return fdout;
1976}
1977
1978
1979///////////////////////////
1980void  buildBin( const char* file_path )
1981///////////////////////////
1982{
1983    unsigned int    length;
1984
1985    int fdout = open_file(file_path);
1986
1987    // write header to binary file
1988    length = write(fdout, (char*)header, sizeof(mapping_header_t));
1989    if ( length != sizeof(mapping_header_t) )
1990    {
1991        printf("write header error : length = %d \n", length);
1992        exit(1);
1993    }
1994
1995    // write clusters
1996    BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char**) cluster);
1997    // write psegs
1998    BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char**) pseg);
1999    // write vspaces
2000    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char**) vspace);
2001    // write vsegs
2002    BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char**) vseg);
2003    // write vobjs
2004    BuildTable(fdout, "vobj", vobj_index, sizeof(mapping_vobj_t), (char**) vobj);
2005    // write tasks array
2006    BuildTable(fdout, "task", task_index, sizeof(mapping_task_t), (char**) task);
2007    //building procs array
2008    BuildTable(fdout, "proc", proc_index, sizeof(mapping_proc_t), (char**) proc);
2009    //building irqs array
2010    BuildTable(fdout, "irq", irq_index, sizeof(mapping_irq_t), (char**)irq);
2011    //building coprocs array
2012    BuildTable(fdout, "coproc", coproc_index, sizeof(mapping_coproc_t), (char**)coproc);
2013    //building cp_ports array
2014    BuildTable(fdout, "cp_port", cp_port_index, sizeof(mapping_cp_port_t),(char**) cp_port);
2015    //building periphs array
2016    BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char**)periph);
2017   
2018    close(fdout);
2019
2020} // end buildBin()
2021
2022///////////////////////////////////////////////////////////////////////
2023// this function set the value the vobj_id fiels of all cp_ports
2024///////////////////////////////////////////////////////////////////////
2025void prepareBuild()
2026{
2027    unsigned int i;
2028    //asign for all cp_ports the correct vspaceid and vobjid
2029    for(i=0; i< cp_port_index; i++)
2030    {
2031        int vspace_id = getVspaceId( cp_port_vobj_ref[i]->vspace_name );
2032        if ( vspace_id < 0 ) 
2033        {
2034            printf("[XML ERROR] illegal  <vspacename> for cp_port %d,\n", 
2035                    i);
2036            exit(1);
2037        }
2038        cp_port[i]->vspaceid = vspace_id;
2039       
2040        int vobj_id = getVobjLocId( vspace_id, 
2041                                    cp_port_vobj_ref[i]->vobj_name, 
2042                                    vspace[vspace_id]->vobjs );
2043        if ( vobj_id >= 0 ) 
2044        {
2045
2046#if XML_PARSER_DEBUG
2047printf("\ncp_port = %d\n", i);
2048printf("      vspace_name  = %s\n", cp_port_vobj_ref[i]->vspace_name);
2049printf("      vobj_name    = %s\n", cp_port_vobj_ref[i]->vobj_name);
2050printf("      vobj_index   = %d\n", vobj_id);
2051#endif
2052            cp_port[i]->vobjlocid = vobj_id;
2053
2054            assert((vobj[ vspace[vspace_id]->vobj_offset + vobj_id]->type == VOBJ_TYPE_MWMR)
2055            && "coproc ports has to refere to a vobj of type MWMR");
2056        }
2057        else             
2058        {
2059            printf("[XML ERROR] illegal  <vobjname> for cp_port %d,\n", 
2060                    i);
2061            exit(1);
2062        }
2063    }
2064}
2065
2066
2067void file_write(int fdout, char* towrite)
2068{
2069    unsigned int size = strlen(towrite);
2070    if(size != write(fdout, towrite, size))
2071    {
2072        printf("file_write error");
2073        exit(1);
2074    }
2075}
2076
2077void def_int_write(int fdout, char* def, int num)
2078{
2079    char  buf[64];
2080    sprintf(buf, "#define\t %s  %d\n", def, num);
2081    file_write(fdout, buf);
2082
2083}
2084
2085void def_hex_write(int fdout, char* def, int num)
2086{
2087    char  buf[64];
2088    sprintf(buf, "#define\t %s  0x%x\n", def, num);
2089    file_write(fdout, buf);
2090}
2091
2092///////////////////////////
2093void  genHd( const char* file_path )
2094///////////////////////////
2095{
2096    int fdout = open_file(file_path);
2097
2098    char* prol = " /* AUTO GENRATED FILE */\n\n#ifndef _HD_CONFIG_H\n#define _HD_CONFIG_H\n\n";
2099    file_write(fdout, prol);
2100
2101    def_int_write(fdout, "CLUSTER_X"       , cluster_x);
2102    def_int_write(fdout, "CLUSTER_Y"       , cluster_y);
2103    def_int_write(fdout, "NB_CLUSTERS"      , cluster_index);
2104    def_hex_write(fdout, "CLUSTER_SIZE"    , ( ((unsigned long)1) << 32)/cluster_index);
2105    def_int_write(fdout, "NB_PROCS_MAX"     , nb_proc_max);
2106    def_int_write(fdout, "NB_TIMERS_MAX"   , nb_timer_channel_max);
2107    def_int_write(fdout, "NB_DMAS_MAX"     , nb_dma_channel_max);
2108    def_int_write(fdout, "NB_TTYS"         , nb_tty_channel);
2109    def_int_write(fdout, "NB_IOCS"         , nb_ioc_channel);
2110    def_int_write(fdout, "NB_NICS"         , nb_nic_channel);
2111   
2112    file_write(fdout, "\n");
2113    def_int_write(fdout, "USE_XICU"         , use_xicu);
2114    def_int_write(fdout, "IOMMU_ACTIVE "    , io_mmu_active);
2115
2116    char* epil = "\n#endif //_HD_CONFIG_H";
2117    file_write(fdout, epil);
2118
2119    close(fdout);
2120}
2121
2122void ld_write(int fdout, char* seg, unsigned int addr)
2123{
2124    char  buf[64];
2125    sprintf(buf, "%s = 0x%x;\n", seg, addr);
2126    file_write(fdout, buf);
2127
2128}
2129
2130///////////////////////////
2131void  genLd( const char* file_path )
2132///////////////////////////
2133{
2134    int fdout = open_file(file_path);
2135
2136    char* prol = "/* AUTO GENRATED FILE */\n\n";
2137    file_write(fdout, prol);
2138
2139    //boot
2140    ld_write(fdout, "seg_boot_code_base"           , boot_code_base);
2141    ld_write(fdout, "seg_boot_stack_base"          , boot_stack_base);
2142    ld_write(fdout, "seg_mapping_base"        , boot_mapping_base );
2143
2144    //kernel
2145    ld_write(fdout, "\nseg_kernel_code_base"         , kernel_code_base);
2146    ld_write(fdout, "seg_kernel_data_base"         , kernel_data_base);
2147    ld_write(fdout, "seg_kernel_uncdata_base"      , kernel_uncdata_base);
2148    ld_write(fdout, "seg_kernel_init_base"         , kernel_init_base);
2149
2150    //peripherals
2151    ld_write(fdout, "\nseg_fbf_base" ,  fbf_base_offset);
2152    ld_write(fdout, "seg_icu_base" ,  icu_base_offset);
2153    ld_write(fdout, "seg_ioc_base" ,  ioc_base_offset);
2154    ld_write(fdout, "seg_tty_base" ,  tty_base_offset);
2155    ld_write(fdout, "seg_dma_base" ,  dma_base_offset);
2156    ld_write(fdout, "seg_tim_base" ,  tim_base_offset);
2157    ld_write(fdout, "seg_gcd_base" ,  gcd_base_offset);
2158    ld_write(fdout, "seg_iob_base" ,  iob_base_offset);
2159
2160    close(fdout);
2161}
2162
2163char* buildPath(const char* path, const char*name)
2164{
2165    char *res = calloc(strlen(path) + strlen(name) + 1, 1);
2166    strcat(res, path);
2167    strcat(res, "/");
2168    strcat(res, name);
2169    return res; 
2170}
2171
2172/////////////////////////////////////
2173int  main ( int argc, char* argv[] )
2174/////////////////////////////////////
2175{
2176    if ( argc < 3 ) 
2177    {
2178        printf("Usage: xml2bin <input_file_path> <output_path>\n");
2179        return 1;
2180    }
2181
2182    struct stat dir_st;
2183    if(stat( argv[2], &dir_st ))
2184    {
2185        perror("bad path");
2186        exit(1);
2187    }
2188
2189    if((dir_st.st_mode & S_IFDIR) == 0)
2190    {
2191        printf("path is not a dir: %s", argv[2] );
2192        exit(1);
2193    }
2194
2195   
2196    char *map_path = buildPath(argv[2], "map.bin"); 
2197    char *ld_path = buildPath(argv[2],  "giet_vsegs.ld"); 
2198    char *hd_path = buildPath(argv[2],  "hard_config.h"); 
2199
2200    LIBXML_TEST_VERSION;
2201
2202    int status;
2203    xmlTextReaderPtr reader = xmlReaderForFile ( argv[1], NULL, 0 );
2204
2205    if ( reader != NULL ) 
2206    {
2207        status = xmlTextReaderRead ( reader );
2208        while ( status == 1 ) 
2209        {
2210            const char* tag = (const char*)xmlTextReaderConstName(reader);
2211
2212            if ( strcmp(tag,"mapping_info") == 0 )
2213            { 
2214                headerNode( reader );
2215                prepareBuild();
2216                buildBin( map_path );
2217                genHd(hd_path);
2218                genLd(ld_path);
2219            }
2220            else
2221            {
2222                printf("[XML ERROR] Wrong file type: \"%s\"\n", argv[1]);
2223                return 1;
2224            }
2225            status = xmlTextReaderRead ( reader );
2226        }
2227        xmlFreeTextReader ( reader );
2228
2229        if ( status != 0 ) 
2230        {
2231            printf("[XML ERROR] Wrong Syntax in \"%s\" file\n", argv[1]);
2232            return 1;
2233        }
2234    }
2235    return 0;
2236} // end main()
Note: See TracBrowser for help on using the repository browser.