source: soft/giet_vm/giet_xml/xml_parser.c @ 323

Last change on this file since 323 was 323, checked in by alain, 10 years ago

ntroducing several modifs in the mapping_info data structure
to comply with the genmap tool.

  • Property svn:executable set to *
File size: 89.5 KB
Line 
1//////////////////////////////////////////////////////////////////////////////////////
2// File     : xml_parser.c
3// Date     : 14/04/2012
4// Author   : alain greiner
5// Copyright (c) UPMC-LIP6
6///////////////////////////////////////////////////////////////////////////////////////
7// This program translate a "map.xml" source file to a binary file "map.bin" that
8// can be directly loaded in memory and used by the GIET-VM operating system.
9//
10// This map.xml file contains :
11// 1) the multi-cluster/multi-processors hardware architecture description
12// 2) the various multi-threaded software applications
13// 3) the mapping directives bor both the tasks and the virtual segments.
14// The corresponding C structures are defined in the "mapping_info.h" file.
15//
16// This parser also generates the "hard_config.h" and the "giet_vsegs.ld" files,
17// required  to compile the GIET-VM code.
18///////////////////////////////////////////////////////////////////////////////////////
19
20#include  <stdlib.h>
21#include  <fcntl.h>
22#include  <sys/types.h>
23#include  <sys/stat.h>
24#include  <unistd.h>
25#include  <stdio.h>
26#include  <string.h>
27#include  <assert.h>
28#include  <libxml/xmlreader.h>
29#include  <mapping_info.h>
30#include  <irq_handler.h>
31
32#define MAX_CLUSTERS   1024
33#define MAX_PSEGS      4096
34#define MAX_VSPACES    1024
35#define MAX_TASKS      4096
36#define MAX_MWMRS      4096
37#define MAX_VSEGS      4096
38#define MAX_VOBJS      8192
39#define MAX_PROCS      1024
40#define MAX_IRQS       8192
41#define MAX_COPROCS    4096
42#define MAX_CP_PORTS   8192
43#define MAX_PERIPHS    8192
44
45#define XML_PARSER_DEBUG  0
46
47///////////////////////////////////////////////////////////////////////////////////
48//     global variables used to store and index the data structures
49///////////////////////////////////////////////////////////////////////////////////
50
51mapping_header_t *   header;
52mapping_cluster_t *  cluster[MAX_CLUSTERS];  // cluster array
53mapping_pseg_t *     pseg[MAX_PSEGS];        // pseg array
54mapping_vspace_t *   vspace[MAX_VSPACES];    // vspace array
55mapping_vseg_t *     vseg[MAX_VSEGS];        // vseg array
56mapping_vobj_t *     vobj[MAX_VOBJS];        // vobj array
57mapping_task_t *     task[MAX_TASKS];        // task array
58mapping_proc_t *     proc[MAX_PROCS];        // proc array
59mapping_irq_t *      irq[MAX_IRQS];          // irq array
60mapping_coproc_t *   coproc[MAX_COPROCS];    // coproc array
61mapping_cp_port_t *  cp_port[MAX_CP_PORTS];  // coproc port array
62mapping_periph_t *   periph[MAX_PERIPHS];    // peripheral array
63
64// Index for the various arrays
65
66unsigned int cluster_index  = 0;
67unsigned int vspace_index = 0;
68unsigned int global_index = 0;
69unsigned int pseg_index = 0;       
70
71unsigned int proc_index = 0;
72unsigned int proc_loc_index = 0;
73
74unsigned int irq_index = 0;
75unsigned int irq_loc_index  = 0;
76
77unsigned int coproc_index = 0;
78unsigned int coproc_loc_index = 0;
79
80unsigned int cp_port_index = 0;
81unsigned int cp_port_loc_index = 0;
82
83unsigned int periph_index = 0;
84unsigned int periph_loc_index = 0;
85
86unsigned int vseg_index = 0;
87unsigned int vseg_loc_index = 0;
88
89unsigned int task_index = 0;
90unsigned int task_loc_index = 0;
91
92unsigned int vobj_index = 0;
93unsigned int vobj_loc_index = 0;
94unsigned int vobj_count = 0;
95
96
97//////////////////////////////
98// for replicated peripheral
99//////////////////////////////
100char found_timer = 0;
101char found_icu   = 0;
102char found_xcu   = 0;
103char found_dma   = 0;
104char found_mmc   = 0;
105
106////////////////////////////////////////////////////////////////////////
107// These variables are used to generate the hard_config.h file.
108////////////////////////////////////////////////////////////////////////
109
110unsigned int total_procs      = 0; // total number of processors
111unsigned int nb_procs_max     = 0; // max number of processors per cluster
112unsigned int nb_tasks_max     = 0; // max number of tasks (in all vspaces)
113
114unsigned int tim_channels     = 0; // number of user timers  (per cluster)
115unsigned int dma_channels     = 0; // number of DMA channels (per cluster)
116
117unsigned int tty_channels     = 0; // number of TTY channels
118unsigned int ioc_channels     = 0; // number of HBA channels
119unsigned int nic_channels     = 0; // number of NIC channels
120unsigned int cma_channels     = 0; // number of CMA channels
121unsigned int pic_channels     = 0; // number of PIC channels
122
123unsigned int use_iob          = 0; // using IOB component
124unsigned int use_pic          = 0; // using PIC component
125unsigned int use_xcu          = 0; // using XCU (not ICU)
126unsigned int use_fbf          = 0; // using Frame Buffer
127
128// These variables define the IOC peripheral subtype
129
130unsigned int use_hba          = 0; // using SoClib AHCI controller
131unsigned int use_bdv          = 0; // using SoCLIB block device controller
132unsigned int use_spi          = 0; // using SDCard-SPI
133
134////////////////////////////////////////////////////////////////
135// These variables are used to generate the giet_vseg.ld file
136////////////////////////////////////////////////////////////////
137
138unsigned int periph_vbase_array[PERIPH_TYPE_MAX_VALUE] 
139         = { [0 ... (PERIPH_TYPE_MAX_VALUE - 1)] = 0xFFFFFFFF };
140
141//////////////////////////////////////////////////////////////////////
142// This arrray is useful to build a temporary list of vobj references.
143// The struct vobj_ref_s is formed by a vspace_name and a vobj_name.
144// This array is used to set the attribute vobj_id of a cp_port
145// once all the vspace have been parsed.
146/////////////////////////////////////////////////////////////////////
147typedef struct vobj_ref_s
148{
149    char vspace_name[32];
150    char vobj_name[32];
151} vobj_ref_t;
152
153vobj_ref_t * cp_port_vobj_ref[MAX_CP_PORTS];   
154
155
156//////////////////////////////////////////////////
157unsigned int getIntValue( xmlTextReaderPtr reader, 
158                          const char * attributeName, 
159                          unsigned int * ok) 
160{
161    unsigned int value = 0;
162    unsigned int i;
163    char c;
164
165    char * string = (char *) xmlTextReaderGetAttribute(reader, 
166                                (const xmlChar *) attributeName);
167
168    if (string == NULL) 
169    {
170        // missing argument
171        *ok = 0;
172        return 0;
173    }
174    else 
175    {
176        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) 
177        {
178            // Hexa
179            for (i = 2 ; (string[i] != 0) && (i < 10) ; i++) 
180            {
181                c = string[i];
182                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
183                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
184                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
185                else 
186                {
187                    *ok = 0;
188                    return 0;
189                }
190            }
191        }
192        else 
193        {
194            // Decimal
195            for (i = 0; (string[i] != 0) && (i < 9); i++) 
196            {
197                c = string[i];
198                if ((c >= '0') && (c <= '9')) value = (value * 10) + string[i] - 48;
199                else 
200                {
201                    *ok = 0;
202                    return 0;
203                }
204            }
205        }
206        *ok = 1;
207        return value; 
208    }
209} // end getIntValue()
210
211////////////////////////////////////////////////
212paddr_t getPaddrValue( xmlTextReaderPtr reader, 
213                       const char * attributeName, 
214                       unsigned int * ok) 
215{
216    paddr_t value = 0;
217    unsigned int i;
218    char c;
219
220    char * string = (char *) xmlTextReaderGetAttribute(reader, 
221                                (const xmlChar *) attributeName);
222
223    if (string == NULL) 
224    {
225        // missing argument
226        *ok = 0;
227        return 0;
228    }
229    else 
230    {
231        if ((string[0] == '0') && ((string[1] == 'x') || (string[1] == 'X'))) 
232        {
233            // Hexa
234            for (i = 2 ; (string[i] != 0) && (i < 18) ; i++) 
235            {
236                c = string[i];
237                if      ((c >= '0') && (c <= '9')) { value = (value << 4) + string[i] - 48; }
238                else if ((c >= 'a') && (c <= 'f')) { value = (value << 4) + string[i] - 87; }
239                else if ((c >= 'A') && (c <= 'F')) { value = (value << 4) + string[i] - 55; }
240                else 
241                {
242                    *ok = 0;
243                    return 0;
244                }
245            }
246        }
247        else 
248        {
249            // Decimal not supported for paddr_t
250            *ok = 0;
251            return 0;
252        }
253        *ok = 1;
254        return value; 
255    }
256} // end getPaddrValue()
257
258////////////////////////////////////////////////
259char * getStringValue( xmlTextReaderPtr reader, 
260                       const char * attributeName, 
261                       unsigned int * ok ) 
262{
263    char * string = (char *) xmlTextReaderGetAttribute(reader, 
264                               (const xmlChar *) attributeName);
265
266
267    if (string == NULL) 
268    {
269        // missing argument
270        *ok = 0;
271        return NULL;
272    }
273    else 
274    {
275        //we read only string smaller than 32 byte
276        if (strlen(string) > 32) 
277        {
278            printf("[XML ERROR] all strings must be less than 32 bytes\n");
279            exit(1);
280        }
281
282        *ok = 1;
283        return string;
284    }
285} // end getStringValue()
286
287///////////////////////////////////////////////////////////////////////////////////
288// This function set the vbase addresses for all peripheral types, in order
289// to generate the ldscript file, that contains one single virtual address
290// for peripherals replicated in all clusters, and one virtual addresses for
291// each non replicated peripheral type.
292//
293// It makes the following checks on the virtual addresses:
294//
295// - For replicated peripherals the virtual base address must be:
296//   vbase = seg_type_base & 0XFF000000 + (cluster_xy * 0x00010000) & 0x00FF0000
297//
298// - For non-replicated peripherals, the cluster index must be cluster_io.
299///////////////////////////////////////////////////////////////////////////////////
300void set_periph_vbase_array()
301{
302    unsigned int vseg_id;      // vseg global index
303    unsigned int periph_id;    // periph global index
304    unsigned int pseg_id;      // pseg global index
305    unsigned int cluster_id;   // cluster linear index
306    unsigned int cluster_xy;   // cluster topological index
307    unsigned int type;         // peripheral type
308
309    unsigned int type_mask      = 0xFF000000;
310    unsigned int cluster_mask   = 0x00FF0000;
311    unsigned int vseg_increment = 0x00010000;
312
313#if XML_PARSER_DEBUG
314printf("\n set peripherals vbase array\n");
315#endif
316
317    // scan all vsegs
318    for (vseg_id = 0 ; vseg_id < header->vsegs ; vseg_id++)
319    {
320        // keep only vseg corresponding to a periph       
321        if ( vobj[vseg[vseg_id]->vobj_offset]->type == VOBJ_TYPE_PERI )
322        {
323            pseg_id = vseg[vseg_id]->psegid; 
324
325#if XML_PARSER_DEBUG
326printf(" - found vseg %s associated to pseg %d", vseg[vseg_id]->name, pseg_id );
327#endif
328
329            // scan all periphs to retrieve peripheral type (same psegid)
330            for ( periph_id = 0 ; periph_id < header->periphs ; periph_id++)
331            {
332                if( periph[periph_id]->psegid == pseg_id ) // matching !!!
333                {
334                    cluster_id = pseg[pseg_id]->clusterid;
335                    type       = periph[periph_id]->type;
336
337#if XML_PARSER_DEBUG
338printf(" / matching periph type %d\n", type );
339#endif
340
341                    if ( (type == PERIPH_TYPE_DMA) ||
342                         (type == PERIPH_TYPE_MMC) ||
343                         (type == PERIPH_TYPE_ICU) ||
344                         (type == PERIPH_TYPE_XCU) ||
345                         (type == PERIPH_TYPE_TIM) )   // replicated peripheral
346                    {
347                        cluster_xy = (cluster[cluster_id]->x << header->y_width) +
348                                      cluster[cluster_id]->y;
349
350                        if( (vseg[vseg_id]->vbase & cluster_mask) != 
351                            (vseg_increment * cluster_xy) )
352                        {
353                            printf("[XML ERROR] All replicated peripherals "
354                                   "must have cluster bits = cluster_xy * increment\n");
355                            printf("periph index = %d / periph type = %d / vbase = %x\n",
356                                    periph_id, type, vseg[vseg_id]->vbase);
357                            exit(1);
358                        }
359                        else if ( periph_vbase_array[type] == 0xFFFFFFFF ) // vbase not set
360                        {
361                            periph_vbase_array[type] = vseg[vseg_id]->vbase & type_mask;   
362                        }
363                        else if ((vseg[vseg_id]->vbase & type_mask) != (periph_vbase_array[type]))
364                        {
365                            printf("[XML ERROR] All peripherals with same type"
366                                   " should share the same 8 MSB bits in vbase address\n");
367                            printf("periph index = %d / periph type = %d / vbase = %x\n",
368                                    periph_id, type, vseg[vseg_id]->vbase);
369                            exit(1);
370                        }
371                    }
372                    else                               // non replicated peripheral
373                    {
374                        if ( (cluster[cluster_id]->x == header->x_io) && 
375                             (cluster[cluster_id]->y == header->y_io) )   
376                        {
377                            periph_vbase_array[type] = vseg[vseg_id]->vbase;   
378                        }
379                        else
380                        {
381                            printf("[XML ERROR] Non replicated peripherals must be in cluster_io\n");
382                            printf(" periph index = %d / periph type = %d / vbase = %x"
383                                   " / pseg index = %d / cluster index = %d\n",
384                                    periph_id, type, vseg[vseg_id]->vbase, pseg_id, cluster_id);
385                            exit(1);
386                        }
387                    }
388                }
389            }
390        }   
391    }
392}  // end set_periph_vbase_array()
393
394///////////////////////////////////////////////////////////////
395int getClusterId( unsigned int x, unsigned int y )
396{
397    // associative search of cluster index
398    unsigned int cluster_id;
399
400    for( cluster_id = 0 ; cluster_id < (header->x_size * header->y_size) ; cluster_id++ )
401    {
402        if( (cluster[cluster_id]->x == x) && (cluster[cluster_id]->y == y) )
403        {
404            return cluster_id;
405        }
406    }
407    return -1;
408}  // end getClusterId()
409
410///////////////////////////////////////////////////////////////
411int getPsegId(unsigned int x, unsigned int y, char * pseg_name) 
412{
413    int cluster_id = getClusterId( x, y );
414
415    if ( cluster_id == -1 ) return -1;
416
417    // associative search for pseg index
418    unsigned int pseg_id;
419    unsigned int pseg_min = cluster[cluster_id]->pseg_offset;
420    unsigned int pseg_max = pseg_min + cluster[cluster_id]->psegs;
421
422    for (pseg_id = pseg_min; pseg_id < pseg_max; pseg_id++) 
423    {
424        if (strcmp(pseg[pseg_id]->name, pseg_name) == 0) 
425        {
426            return pseg_id;
427        }
428    }
429    return -1;
430}  // end getPsegId()
431
432///////////////////////////////////
433int getVspaceId(char * vspace_name) 
434{
435    unsigned int vspace_id;
436
437    for (vspace_id = 0; vspace_id < vspace_index; vspace_id++) 
438    {
439        if (strcmp(vspace[vspace_id]->name, vspace_name) == 0) 
440        {
441            return vspace_id;
442        }
443    }
444    return -1;
445}
446
447////////////////////////////////////////////////////////////////////////////////
448int getVobjId(unsigned int vspace_id, char * vobj_name, unsigned int vspace_max) 
449{
450    unsigned int vobj_id;
451    unsigned int vobj_min = vspace[vspace_id]->vobj_offset;
452    unsigned int vobj_max = vobj_min + vspace_max;
453
454    for (vobj_id = vobj_min; vobj_id < vobj_max; vobj_id++) 
455    {
456        if (strcmp(vobj[vobj_id]->name, vobj_name) == 0) return vobj_id;
457    }
458    return -1;
459}
460
461///////////////////////////////////////////////////////////
462unsigned int alignTo( unsigned int value, unsigned int pow2 )
463{
464    unsigned int mask = (1 << pow2) - 1;
465    return ( (value + mask) & ~mask);
466}
467
468////////////////////
469void setVsegLength()
470{
471    // for a given vseg identified vseg_index
472    // scan all contained vobjs to compute the vseg lenth
473
474    unsigned int vobj_id;
475    unsigned int cur_length = 0;
476
477    unsigned int first = vseg[vseg_index]->vobj_offset;
478    unsigned int last  = first + vseg[vseg_index]->vobjs;
479
480    for ( vobj_id = first ; vobj_id < last ; vobj_id++ ) 
481    {
482        if (vobj[vobj_id]->align)
483        {
484            cur_length = alignTo( cur_length, vobj[vobj_id]->align );
485        }
486        cur_length += vobj[vobj_id]->length;
487    }
488    vseg[vseg_index]->length = alignTo( cur_length, 12 );
489}
490
491///////////////////////
492void checkVsegOverlap()
493{
494    // for a given vseg identified by vseg_index,
495    // check overlap with all vsegs in same vspace,
496    // and check overlap with all global vsegs.
497
498    unsigned int vseg_id;
499    unsigned int prev_vbase;                          // previous vseg vbase
500    unsigned int prev_length;                         // previous vseg length
501
502    unsigned int vbase  = vseg[vseg_index]->vbase;    // new vseg vbase
503    unsigned int length = vseg[vseg_index]->length;   // new vseg length
504 
505    // checking overlap with other vsegs in same vspace
506    if ( header->vspaces > 0 )
507    {
508        unsigned int first = vspace[vspace_index]->vseg_offset;   
509        unsigned int last  = vseg_index;
510   
511        for( vseg_id = first ; vseg_id < last ; vseg_id++ )
512        {
513            prev_vbase  = vseg[vseg_id]->vbase;
514            prev_length = vseg[vseg_id]->length;
515            if ( ((vbase + length) > prev_vbase) && ((prev_vbase + prev_length) > vbase) )
516            { 
517                printf("[XML ERROR] vseg %s in vspace %s overlaps other vseg %s\n",
518                vseg[vseg_index]->name, vspace[vspace_index]->name, vseg[vseg_id]->name );
519                exit(1);
520            }
521        }
522    }
523
524    // checking overlap with existing global vsegs
525    for ( vseg_id = 0 ; vseg_id < header->globals ; vseg_id++ )
526    {
527        prev_vbase  = vseg[vseg_id]->vbase;
528        prev_length = vseg[vseg_id]->length;
529        if ( ((vbase + length) > prev_vbase) && ((prev_vbase + prev_length) > vbase) )
530        { 
531            printf("[XML ERROR] vseg %s in vspace %s overlaps global vseg %s\n",
532            vseg[vseg_index]->name, vspace[vspace_index]->name, vseg[vseg_id]->name );
533            exit(1);
534        }
535    }
536}
537
538//////////////////////////////////////
539void taskNode(xmlTextReaderPtr reader) 
540{
541    unsigned int ok;
542    unsigned int value;
543    unsigned int x,y;
544    char * str;
545
546    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
547
548    if (task_index >= MAX_TASKS) 
549    {
550        printf("[XML ERROR] The number of tasks is larger than %d\n", MAX_TASKS);
551        exit(1);
552    }
553
554#if XML_PARSER_DEBUG
555printf("   task %d\n", task_loc_index);
556#endif
557
558    task[task_index] = (mapping_task_t *) malloc(sizeof(mapping_task_t));
559
560    ////////// get name attribute
561    str = getStringValue(reader, "name", &ok);
562    if (ok) 
563    {
564#if XML_PARSER_DEBUG
565printf("      name      = %s\n", str);
566#endif
567        strncpy( task[task_index]->name, str, 31 );
568    }
569    else 
570    {
571        printf("[XML ERROR] illegal or missing <name> attribute for task (%d,%d)\n", 
572                vspace_index, task_loc_index);
573        exit(1);
574    }
575
576    ///////// get trdid attribute (optional)
577    task[task_index]->trdid = getIntValue(reader, "trdid", &ok);
578#if XML_PARSER_DEBUG
579printf("      trdid     = %d\n", x);
580#endif
581    if ( !ok ) 
582    {
583        task[task_index]->trdid = task_loc_index;
584    } 
585
586    ///////// get x coordinate
587    x = getIntValue(reader, "x", &ok);
588#if XML_PARSER_DEBUG
589printf("      x         = %d\n", x);
590#endif
591    if ( !(ok && (x < header->x_size)) ) 
592    {
593        printf("[XML ERROR] illegal or missing < x > attribute for task (%d,%d)\n",
594                vspace_index, task_loc_index);
595        exit(1);
596    } 
597
598    ///////// get y coordinate
599    y = getIntValue(reader, "y", &ok);
600#if XML_PARSER_DEBUG
601printf("      y         = %d\n", y);
602#endif
603    if ( !(ok && (y < header->y_size)) ) 
604    {
605        printf("[XML ERROR] illegal or missing < y > attribute for task (%d,%d)\n",
606                vspace_index, task_loc_index);
607        exit(1);
608    } 
609
610    ///////// set clusterid attribute
611    int index = getClusterId( x, y );
612#if XML_PARSER_DEBUG
613printf("      clusterid = %d\n", index);
614#endif
615    if( index >= 0 )
616    {
617        task[task_index]->clusterid = index;
618    }
619    else
620    {
621        printf("[XML ERROR] <clusterid> not found for task (%d,%d)\n",
622                vspace_index, task_loc_index);
623        exit(1);
624    }
625
626    ////////// get p attribute
627    value = getIntValue(reader, "p", &ok);
628    if (ok) 
629    {
630#if XML_PARSER_DEBUG
631printf("      proclocid = %x\n", value);
632#endif
633        if (value >= cluster[task[task_index]->clusterid]->procs) 
634        {
635            printf("[XML ERROR] <proclocid> too large for task (%d,%d)\n",
636                    vspace_index, task_loc_index);
637            exit(1);
638        }
639        task[task_index]->proclocid = value;
640    } 
641    else 
642    {
643        printf("[XML ERROR] illegal or missing <p> attribute for task (%d,%d)\n", 
644                vspace_index, task_loc_index);
645        exit(1);
646    }
647
648    ////////// get stackname attribute
649    str = getStringValue(reader, "stackname" , &ok);
650    if (ok) 
651    {
652        int index = getVobjId(vspace_index, str , vobj_loc_index);
653        if (index >= 0) 
654        {
655#if XML_PARSER_DEBUG
656printf("      stackname = %s\n", str);
657printf("      stack_id  = %d\n", index);
658#endif
659            task[task_index]->stack_vobj_id = index;
660        }
661        else 
662        {
663            printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
664                    vspace_index, task_loc_index);
665            exit(1);
666        }
667    } 
668    else 
669    {
670        printf("[XML ERROR] illegal or missing <stackname> for task (%d,%d)\n", 
671                vspace_index, task_loc_index);
672        exit(1);
673    }
674
675    ////////// get heap attribute
676    str = getStringValue(reader, "heapname", &ok);
677    if (ok) 
678    {
679        int index = getVobjId(vspace_index, str, vobj_loc_index);
680        if (index >= 0) 
681        {
682#if XML_PARSER_DEBUG
683printf("      heapname  = %s\n", str);
684printf("      heap_id   = %d\n", index );
685#endif
686            task[task_index]->heap_vobj_id = index;
687        }
688        else 
689        {
690            printf("[XML ERROR] illegal or missing <heapname> for task (%d,%d)\n", 
691                   vspace_index, task_loc_index);
692            exit(1);
693        }
694    } 
695    else 
696    {
697        task[task_index]->heap_vobj_id = -1;
698    }
699
700    ////////// get startid  attribute
701    value = getIntValue(reader, "startid", &ok);
702    if (ok) 
703    {
704#if XML_PARSER_DEBUG
705printf("      startid   = %x\n", value);
706#endif
707        task[task_index]->startid = value;
708    } 
709    else 
710    {
711        printf("[XML ERROR] illegal or missing <startid> attribute for task (%d,%d)\n", 
712                vspace_index, task_loc_index);
713        exit(1);
714    }
715
716    /////////// get use_tty  attribute (optionnal : 0 if missing)
717    value = getIntValue(reader, "usetty", &ok);
718#if XML_PARSER_DEBUG
719printf("      usetty = %x\n", value);
720#endif
721    task[task_index]->use_tty = (ok)? value : 0;
722
723    /////////// get use_nic  attribute (optionnal : 0 if missing)
724    value = getIntValue(reader, "usenic", &ok);
725#if XML_PARSER_DEBUG
726printf("      usenic = %x\n", value);
727#endif
728    task[task_index]->use_nic = (ok)? value : 0;
729
730    /////////// get use_tim attribute (optionnal : 0 if missing)
731    value = getIntValue(reader, "usetim", &ok);
732#if XML_PARSER_DEBUG
733printf("      usetim = %x\n", value);
734#endif
735    task[task_index]->use_tim = (ok)? value : 0;
736
737    /////////// get use_hba  attribute (optionnal : 0 if missing)
738    value = getIntValue(reader, "usehba", &ok);
739#if XML_PARSER_DEBUG
740printf("      usehba = %x\n", value);
741#endif
742    task[task_index]->use_hba = (ok)? value : 0;
743
744    /////////// get usecma  attribute (optionnal : 0 if missing)
745    value = getIntValue(reader, "usecma", &ok);
746#if XML_PARSER_DEBUG
747printf("      usecma = %x\n", value);
748#endif
749    task[task_index]->use_cma = (ok)? value : 0;
750
751    task_index++;
752    task_loc_index++;
753} // end taskNode()
754
755//////////////////////////////////////
756void vobjNode(xmlTextReaderPtr reader) 
757{
758    unsigned int ok;
759    unsigned int value;
760    char * str;
761
762    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
763
764    if (vobj_index >= MAX_VOBJS) 
765    {
766        printf("[XML ERROR] The number of vobjs is larger than %d\n", MAX_VOBJS);
767        exit(1);
768    }
769
770#if XML_PARSER_DEBUG
771printf("      vobj %d\n", vobj_loc_index);
772#endif
773
774    vobj[vobj_index] = (mapping_vobj_t *) malloc(sizeof(mapping_vobj_t));
775
776    ///////// get name attribute
777    str = getStringValue(reader, "name", &ok);
778    if (ok) 
779    {
780#if XML_PARSER_DEBUG
781printf("        name = %s\n", str);
782#endif
783        strncpy(vobj[vobj_index]->name, str, 31);
784    }
785    else 
786    {
787        printf("[XML ERROR] illegal or missing <name> attribute for vobj (%d,%d)\n", 
788                vseg_index, vobj_loc_index);
789        exit(1);
790    }
791
792    //////// get type attribute
793    str = getStringValue(reader, "type", &ok);
794#if XML_PARSER_DEBUG
795printf("        type = %s\n", str);
796#endif
797
798    if      (ok && (strcmp(str, "ELF")      == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_ELF;      }
799    else if (ok && (strcmp(str, "PERI")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_PERI;     }
800    else if (ok && (strcmp(str, "BLOB")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BLOB;     }
801    else if (ok && (strcmp(str, "PTAB")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_PTAB;     }
802    else if (ok && (strcmp(str, "MWMR")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_MWMR;     }
803    else if (ok && (strcmp(str, "LOCK")     == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_LOCK;     }
804    else if (ok && (strcmp(str, "BUFFER")   == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BUFFER;   }
805    else if (ok && (strcmp(str, "BARRIER")  == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_BARRIER;  }
806    else if (ok && (strcmp(str, "CONST")    == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_CONST;    }
807    else if (ok && (strcmp(str, "MEMSPACE") == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_MEMSPACE; }
808    else if (ok && (strcmp(str, "SCHED")    == 0)) { vobj[vobj_index]->type = VOBJ_TYPE_SCHED;    }
809    else 
810    {
811        printf("[XML ERROR] illegal or missing <type> attribute for vobj (%d,%d)\n", 
812                vspace_index, vobj_loc_index);
813        exit(1);
814    }
815    // some more checking
816    if ( (vobj[vobj_index]->type == VOBJ_TYPE_ELF) ||
817         (vobj[vobj_index]->type == VOBJ_TYPE_PERI) )
818    {
819        assert( (vobj_count == 0) && 
820        "[XML ERROR] an ELF or PERI vobj must be alone in a vseg");
821    }
822       
823
824    ////////// get length attribute
825    value = getIntValue(reader, "length", &ok);
826    if (ok) 
827    {
828#if XML_PARSER_DEBUG
829printf("        length = %x\n", value);
830#endif
831        vobj[vobj_index]->length = value;
832    } 
833    else {
834        printf("[XML ERROR] illegal or missing <length> attribute for vobj (%d,%d)\n", 
835                vspace_index, vobj_loc_index);
836        exit(1);
837    }
838
839    ////////// get align attribute (optional : 0 if missing)
840    value = getIntValue(reader, "align", &ok);
841    if (ok) 
842    {
843#if XML_PARSER_DEBUG
844printf("        align = %d\n", value);
845#endif
846        vobj[vobj_index]->align = value;
847    } 
848    else 
849    {
850        vobj[vobj_index]->align = 0;
851    }
852
853    ////////// get binpath attribute (optional : "" if missing)
854    str = getStringValue(reader, "binpath", &ok);
855    if (ok) 
856    {
857#if XML_PARSER_DEBUG
858printf("        binpath = %s\n", str);
859#endif
860        strncpy(vobj[vobj_index]->binpath, str, 63);
861    } 
862    else 
863    {
864        vobj[vobj_index]->binpath[0] = 0;
865    }
866
867    ////////// get init attribute (optional, mandatory for mwmr and barrier)
868    value = getIntValue(reader, "init", &ok);
869    if (ok) 
870    {
871#if XML_PARSER_DEBUG
872printf("        init  = %d\n", value);
873#endif
874        vobj[vobj_index]->init = value;
875    } 
876    else 
877    {
878        if ((vobj[vobj_index]->type == VOBJ_TYPE_MWMR) || 
879            (vobj[vobj_index]->type == VOBJ_TYPE_BARRIER) ||
880            (vobj[vobj_index]->type == VOBJ_TYPE_CONST)) 
881        {
882            printf("[XML ERROR] illegal or missing <value> attribute for vobj (%d,%d). \
883                    All MWMR or BARRIER or CONST vobj must have a init value \n", 
884                    vspace_index, vobj_loc_index);
885            exit(1);
886        }
887        vobj[vobj_index]->init = 0;
888    }
889
890    vobj_index++;
891    vobj_count++;
892    vobj_loc_index++;
893} // end vobjNode()
894
895//////////////////////////////////////
896void vsegNode(xmlTextReaderPtr reader) 
897{
898    unsigned int ok;
899    unsigned int value;
900    unsigned int x,y;
901    char * str;
902
903    vobj_count = 0;
904
905    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
906
907    if (vseg_index >= MAX_VSEGS) 
908    {
909        printf("[XML ERROR] The number of vsegs is larger than %d\n", MAX_VSEGS);
910        exit(1);
911    }
912
913#if XML_PARSER_DEBUG
914printf("    vseg %d\n", vseg_loc_index);
915#endif
916
917    vseg[vseg_index] = (mapping_vseg_t *) malloc(sizeof(mapping_vseg_t));
918
919    ////////// set vobj_offset attribute
920    vseg[vseg_index]->vobj_offset = vobj_index;
921
922#if XML_PARSER_DEBUG
923printf("      vobj_offset = %d\n", vobj_index);
924#endif
925
926    ///////// set mapped attribute
927    vseg[vseg_index]->mapped = 0;
928
929    //////// set next_vseg attribute
930    vseg[vseg_index]->next_vseg = 0;
931 
932    ///////// get name attribute
933    str = getStringValue(reader, "name", &ok);
934    if (ok) 
935    {
936#if XML_PARSER_DEBUG
937printf("      name        = %s\n", str);
938#endif
939        strncpy( vseg[vseg_index]->name, str, 31);
940    }
941    else 
942    {
943        printf("[XML ERROR] illegal or missing <name> attribute for vseg (%d,%d)\n", 
944                vspace_index, vseg_loc_index);
945        exit(1);
946    }
947
948    ////////// get ident attribute (optional : 0 if missing)
949    value = getIntValue(reader, "ident", &ok);
950    if (ok) 
951    {
952#if XML_PARSER_DEBUG
953printf("      ident       = %d\n", value);
954#endif
955        vseg[vseg_index]->ident = value;
956    } 
957    else 
958    {
959        vseg[vseg_index]->ident = 0;
960    }
961
962    /////////// get vbase attribute
963    value = getIntValue(reader, "vbase", &ok);
964    if (ok) 
965    {
966#if XML_PARSER_DEBUG
967printf("      vbase       = 0x%x\n", value);
968#endif
969        vseg[vseg_index]->vbase = value;
970    }
971    else 
972    {
973        printf("[XML ERROR] illegal or missing <vbase> attribute for vseg (%d,%d)\n", 
974                vspace_index, vseg_loc_index);
975        exit(1);
976    }
977
978    ////////// get x coordinate
979    x = getIntValue(reader, "x", &ok);
980#if XML_PARSER_DEBUG
981printf("      x           = %d\n", x);
982#endif
983    if ( !(ok && (x < header->x_size)) ) 
984    {
985        printf("[XML ERROR] illegal or missing < x > attribute for vseg %d\n", 
986                vseg_loc_index);
987        exit(1);
988    }
989
990    ////////// get y coordinate
991    y = getIntValue(reader, "y", &ok);
992#if XML_PARSER_DEBUG
993printf("      y           = %d\n", y);
994#endif
995    if ( !(ok && (y < header->y_size)) ) 
996    {
997        printf("[XML ERROR] illegal or missing < y > attribute for vseg %d\n", 
998                vseg_loc_index);
999        exit(1);
1000    }
1001
1002    ///////// get psegname attribute
1003    str = getStringValue(reader, "psegname", &ok);
1004#if XML_PARSER_DEBUG
1005printf("      psegname    = %s\n", str);
1006#endif
1007    if (ok == 0) 
1008    {
1009        printf("[XML ERROR] illegal or missing <psegname> for vseg %d\n", 
1010                vseg_loc_index);
1011        exit(1);
1012    }
1013
1014    /////////// set psegid field
1015    int psegid = getPsegId( x, y, str );
1016#if XML_PARSER_DEBUG
1017printf("      psegid      = %d\n", psegid);
1018#endif
1019    if (psegid >= 0) 
1020    {
1021        vseg[vseg_index]->psegid = psegid;
1022    }
1023    else 
1024    {
1025        printf("[XML ERROR] pseg not found for vseg %d / x = %d / y = %d / psegname = %s\n", 
1026                vseg_loc_index, x, y, str );
1027        exit(1);
1028    } 
1029
1030    //////// get mode attribute
1031    str = getStringValue(reader, "mode", &ok);
1032#if XML_PARSER_DEBUG
1033printf("      mode        = %s\n", str);
1034#endif
1035    if      (ok && (strcmp(str, "CXWU") == 0)) { vseg[vseg_index]->mode = 0xF; }
1036    else if (ok && (strcmp(str, "CXW_") == 0)) { vseg[vseg_index]->mode = 0xE; }
1037    else if (ok && (strcmp(str, "CX_U") == 0)) { vseg[vseg_index]->mode = 0xD; }
1038    else if (ok && (strcmp(str, "CX__") == 0)) { vseg[vseg_index]->mode = 0xC; }
1039    else if (ok && (strcmp(str, "C_WU") == 0)) { vseg[vseg_index]->mode = 0xB; }
1040    else if (ok && (strcmp(str, "C_W_") == 0)) { vseg[vseg_index]->mode = 0xA; }
1041    else if (ok && (strcmp(str, "C__U") == 0)) { vseg[vseg_index]->mode = 0x9; }
1042    else if (ok && (strcmp(str, "C___") == 0)) { vseg[vseg_index]->mode = 0x8; }
1043    else if (ok && (strcmp(str, "_XWU") == 0)) { vseg[vseg_index]->mode = 0x7; }
1044    else if (ok && (strcmp(str, "_XW_") == 0)) { vseg[vseg_index]->mode = 0x6; }
1045    else if (ok && (strcmp(str, "_X_U") == 0)) { vseg[vseg_index]->mode = 0x5; }
1046    else if (ok && (strcmp(str, "_X__") == 0)) { vseg[vseg_index]->mode = 0x4; }
1047    else if (ok && (strcmp(str, "__WU") == 0)) { vseg[vseg_index]->mode = 0x3; }
1048    else if (ok && (strcmp(str, "__W_") == 0)) { vseg[vseg_index]->mode = 0x2; }
1049    else if (ok && (strcmp(str, "___U") == 0)) { vseg[vseg_index]->mode = 0x1; }
1050    else if (ok && (strcmp(str, "____") == 0)) { vseg[vseg_index]->mode = 0x0; }
1051    else {
1052        printf("[XML ERROR] illegal or missing <mode> attribute for vseg (%d,%d)\n", 
1053                vspace_index, vseg_loc_index);
1054        exit(1);
1055    }
1056
1057    ////////// get vobjs in vseg
1058    int status = xmlTextReaderRead(reader);
1059    while (status == 1) 
1060    {
1061        const char * tag = (const char *) xmlTextReaderConstName(reader);
1062
1063        if      (strcmp(tag, "vobj")     == 0 ) { vobjNode(reader); }
1064        else if (strcmp(tag, "#text"  )  == 0 ) { }
1065        else if (strcmp(tag, "#comment") == 0 ) { }
1066        else if (strcmp(tag, "vseg")     == 0 ) 
1067        {
1068            vseg[vseg_index]->vobjs = vobj_count;
1069            setVsegLength();
1070            checkVsegOverlap();
1071            vseg_index++;
1072            vseg_loc_index++;
1073            return;
1074        }
1075        else 
1076        {
1077            printf("[XML ERROR] Unknown tag %s", tag);
1078            exit(1);
1079        }
1080        status = xmlTextReaderRead (reader);
1081    }
1082} // end vsegNode()
1083
1084////////////////////////////////////////
1085void vspaceNode(xmlTextReaderPtr reader) 
1086{
1087    char * str;
1088    unsigned int ok;
1089    unsigned int nb_task_vspace = 0;
1090
1091    vobj_loc_index = 0;
1092    vseg_loc_index = 0;
1093    task_loc_index = 0;
1094
1095    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1096
1097#if XML_PARSER_DEBUG
1098printf("\n  vspace %d\n", vspace_index);
1099#endif
1100
1101    vspace[vspace_index] = (mapping_vspace_t *) malloc(sizeof(mapping_vspace_t));
1102    header->vspaces      = header->vspaces + 1;
1103
1104    ////////// get name attribute
1105    str = getStringValue(reader, "name", &ok);
1106    if (ok) {
1107#if XML_PARSER_DEBUG
1108printf("  name = %s\n", str);
1109#endif
1110        strncpy(vspace[vspace_index]->name, str, 31);
1111    }
1112    else 
1113    {
1114        printf("[XML ERROR] illegal or missing <name> attribute for vspace %d\n", 
1115                vspace_index);
1116        exit(1);
1117    }
1118
1119    ////////// set vseg_offset and task_offset attributes
1120    vspace[vspace_index]->vseg_offset = vseg_index;
1121    vspace[vspace_index]->vobj_offset = vobj_index;
1122    vspace[vspace_index]->task_offset = task_index;
1123
1124#if XML_PARSER_DEBUG
1125    printf("  vseg_offset = %d\n", vseg_index);
1126    printf("  vobj_offset = %d\n", vobj_index);
1127    printf("  task_offset = %d\n", task_index);
1128#endif
1129
1130    ////////// get startname attribute
1131    str = getStringValue(reader, "startname", &ok);
1132    if (ok) 
1133    {
1134        //used after parsing the vobjs
1135    }
1136    else 
1137    {
1138        printf("[XML ERROR] illegal or missing <startname> attribute for vspace %s\n", 
1139                vspace[vspace_index]->name);
1140        exit(1);
1141    }
1142
1143    int status = xmlTextReaderRead(reader);
1144    while (status == 1) 
1145    {
1146        const char * tag = (const char *) xmlTextReaderConstName(reader);
1147
1148        if (strcmp(tag, "vseg") == 0) 
1149        {
1150            vsegNode(reader);
1151        }
1152        else if (strcmp(tag, "task") == 0) 
1153        {
1154            taskNode(reader);
1155            nb_task_vspace++;
1156        }
1157        else if (strcmp(tag, "#text")    == 0) { }
1158        else if (strcmp(tag, "#comment") == 0) { }
1159        else if (strcmp(tag, "vspace")   == 0) 
1160        {
1161            vspace[vspace_index]->vobjs = vobj_loc_index; 
1162            vspace[vspace_index]->tasks = task_loc_index ;
1163            vspace[vspace_index]->vsegs = vseg_loc_index ;
1164
1165            // get index of the vobj containing the start vector
1166            int index = getVobjId(vspace_index, str , vobj_loc_index);
1167            if (index == -1) 
1168            {
1169                printf("[XML ERROR] vobj containing start vector not found in vspace %s\n",
1170                        vspace[vspace_index]->name);
1171                exit(1);
1172            }
1173            else 
1174            {
1175                vspace[vspace_index]->start_vobj_id = index;
1176#if XML_PARSER_DEBUG
1177                printf("      startname = %s\n", str);
1178                printf("      start_id  = %d\n", index);
1179                printf("  end vspace %d\n\n", vspace_index);
1180#endif
1181            }
1182
1183            // checking for all tasks that the startid
1184            // is smaller than the number of tasks in vspace
1185            int task_id;
1186            int task_min = vspace[vspace_index]->task_offset;
1187            int task_max = task_min + vspace[vspace_index]->tasks; 
1188            for (task_id = task_min; task_id < task_max; task_id++) 
1189            {
1190                if (task[task_id]->startid >= vspace[vspace_index]->tasks) 
1191                {
1192                    printf("[XML ERROR] <startid> too large for task (%d,%d)\n", 
1193                            vspace_index, task_id );
1194                    exit(1);
1195                }
1196            }
1197
1198            nb_tasks_max += nb_task_vspace;
1199            vspace_index++;
1200            return;
1201        }
1202        else 
1203        {
1204            printf("[XML ERROR] Unknown tag %s", tag);
1205            exit(1);
1206        }
1207        status = xmlTextReaderRead(reader);
1208    }
1209} // end vspaceNode()
1210
1211/////////////////////////////////////
1212void irqNode(xmlTextReaderPtr reader) 
1213{
1214    unsigned int ok;
1215    unsigned int value;
1216    char * str;
1217
1218    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1219
1220    if (irq_index >= MAX_IRQS) 
1221    {
1222        printf("[XML ERROR] The number of irqs is larger than %d\n", MAX_IRQS);
1223    }
1224
1225#if XML_PARSER_DEBUG
1226printf("      irq %d\n", irq_loc_index);
1227#endif
1228
1229    irq[irq_index] = (mapping_irq_t *) malloc(sizeof(mapping_irq_t));
1230
1231    ///////// get srcid attribute
1232    value = getIntValue(reader, "srcid", &ok);
1233    if (ok) 
1234    {
1235#if XML_PARSER_DEBUG
1236printf("        srcid   = %d\n", value);
1237#endif
1238        irq[irq_index]->srcid = value;
1239        if (value >= 32) 
1240        {
1241            printf("[XML ERROR] IRQ <srcid> too large for periph %d in cluster %d\n",
1242                    cluster_index, periph_loc_index);
1243            exit(1);
1244        }
1245    }
1246    else 
1247    {
1248        printf("[XML ERROR] missing IRQ <srcid> for periph %d in cluster %d\n",
1249                cluster_index, periph_loc_index);
1250        exit(1);
1251    }
1252
1253    ///////// get srctype attribute
1254    str = getStringValue(reader, "srctype", &ok);
1255    if (ok) 
1256    {
1257#if XML_PARSER_DEBUG
1258printf("        srctype = %s\n", str);
1259#endif
1260        if      ( strcmp(str, "HWI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_HWI;
1261        else if ( strcmp(str, "WTI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_WTI;
1262        else if ( strcmp(str, "PTI") == 0 ) irq[irq_index]->srctype = IRQ_TYPE_PTI;
1263        else   
1264        {
1265            printf("[XML ERROR] illegal IRQ <srctype> for periph %d in cluster %d\n",
1266                   cluster_index, periph_loc_index);
1267            exit(1);
1268        }
1269    }
1270    else 
1271    {
1272        printf("[XML ERROR] missing IRQ <srctype> for periph %d in cluster %d\n",
1273               cluster_index, periph_loc_index);
1274        exit(1);
1275    }
1276
1277    ///////// get isr attribute
1278    str = getStringValue(reader, "isr", &ok);
1279    if (ok) 
1280    {
1281#if XML_PARSER_DEBUG
1282printf("        isr     = %s\n", str);
1283#endif
1284        if      (strcmp(str, "ISR_TICK"   ) == 0)  irq[irq_index]->isr = ISR_TICK;
1285        else if (strcmp(str, "ISR_BDV"    ) == 0)  irq[irq_index]->isr = ISR_BDV;
1286        else if (strcmp(str, "ISR_CMA"    ) == 0)  irq[irq_index]->isr = ISR_CMA;
1287        else if (strcmp(str, "ISR_TTY_RX" ) == 0)  irq[irq_index]->isr = ISR_TTY_RX;
1288        else if (strcmp(str, "ISR_TTY_TX" ) == 0)  irq[irq_index]->isr = ISR_TTY_TX;
1289        else if (strcmp(str, "ISR_TIMER"  ) == 0)  irq[irq_index]->isr = ISR_TIMER;
1290        else if (strcmp(str, "ISR_WAKUP"  ) == 0)  irq[irq_index]->isr = ISR_WAKUP;
1291        else if (strcmp(str, "ISR_NIC_RX" ) == 0)  irq[irq_index]->isr = ISR_NIC_RX;
1292        else if (strcmp(str, "ISR_NIC_TX" ) == 0)  irq[irq_index]->isr = ISR_NIC_TX;
1293        else if (strcmp(str, "ISR_MMC"    ) == 0)  irq[irq_index]->isr = ISR_MMC;
1294        else if (strcmp(str, "ISR_DMA"    ) == 0)  irq[irq_index]->isr = ISR_DMA;
1295        else if (strcmp(str, "ISR_SPI"    ) == 0)  irq[irq_index]->isr = ISR_SPI;
1296        else if (strcmp(str, "ISR_DEFAULT") == 0)  irq[irq_index]->isr = ISR_DEFAULT;
1297        else 
1298        {
1299            printf("[XML ERROR] illegal IRQ <isr> for periph %d in cluster %d\n",
1300                   periph_loc_index, cluster_index );
1301            exit(1);
1302        }
1303    } 
1304    else 
1305    {
1306        printf("[XML ERROR] missing IRQ <isr> for periph %d in cluster %d\n",
1307                cluster_index, periph_loc_index);
1308        exit(1);
1309    }
1310
1311    ///////// get channel attribute (optionnal : 0 if missing)
1312    value = getIntValue(reader, "channel", &ok);
1313    if (ok) 
1314    {
1315#if XML_PARSER_DEBUG
1316printf("        channel = %d\n", value);
1317#endif
1318        irq[irq_index]->channel = value;
1319    }
1320    else 
1321    {
1322        irq[irq_index]->channel = 0;
1323    }
1324
1325    irq_index++;
1326    irq_loc_index++;
1327
1328} // end irqNode
1329
1330
1331
1332////////////////////////////////////////
1333void cpPortNode(xmlTextReaderPtr reader) 
1334{
1335    char * str;
1336    unsigned int ok;
1337
1338    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1339
1340    if (cp_port_index >= MAX_CP_PORTS) 
1341    {
1342        printf("[XML ERROR] The number of ports (for coprocs) is larger than %d\n", 
1343                 MAX_CP_PORTS);
1344        exit(1);
1345    }
1346
1347#if XML_PARSER_DEBUG
1348printf("\n  port %d\n", cp_port_index);
1349#endif
1350
1351    cp_port[cp_port_index] = (mapping_cp_port_t *) malloc(sizeof(mapping_cp_port_t));
1352    cp_port_vobj_ref[cp_port_index] = (vobj_ref_t *) malloc(sizeof(vobj_ref_t));
1353
1354    ///////// get direction attribute
1355    str = getStringValue(reader, "direction", &ok);
1356    if (ok) 
1357    {
1358#if XML_PARSER_DEBUG
1359printf("      direction = %s\n", str);
1360#endif
1361        if (strcmp(str, "TO_COPROC")   ==  0) 
1362        {
1363            cp_port[cp_port_index]->direction = PORT_TO_COPROC;
1364        }
1365        else if (strcmp(str, "FROM_COPROC") ==  0) 
1366        {
1367            cp_port[cp_port_index]->direction = PORT_FROM_COPROC;
1368        }
1369        else 
1370        {
1371            printf("[XML ERROR] illegal <direction> for cp_port %d in cluster %d\n",
1372                    cp_port_index, cluster_index);
1373            exit(1);
1374        }
1375    } 
1376    else 
1377    {
1378        printf("[XML ERROR] missing <direction> for cp_port %d in cluster %d\n",
1379                cp_port_index, cluster_index);
1380        exit(1);
1381    }
1382
1383    /////////// get vspacename attribute
1384    str = getStringValue(reader, "vspacename", &ok);
1385#if XML_PARSER_DEBUG
1386printf("      vspacename = %s\n", str);
1387#endif
1388    if (ok) 
1389    {
1390        strncpy(cp_port_vobj_ref[cp_port_index]->vspace_name, str, 31);
1391    }
1392    else 
1393    {
1394        printf("[XML ERROR] missing <vspacename> for cp_port %d in cluster %d\n",
1395                cp_port_index, cluster_index);
1396        exit(1);
1397    }
1398
1399    /////////// get vobjname attribute
1400    str = getStringValue(reader, "vobjname", &ok);
1401#if XML_PARSER_DEBUG
1402printf("      vobjname = %s\n", str);
1403#endif
1404    if (ok) 
1405    {
1406        strncpy(cp_port_vobj_ref[cp_port_index]->vobj_name, str, 31);
1407    }
1408    else 
1409    {
1410        printf("[XML ERROR] missing <vobjname> for cp_port %d in cluster %d\n",
1411                cp_port_index, cluster_index);
1412        exit(1);
1413    }
1414    cp_port_index++;
1415    cp_port_loc_index++;
1416} // end cpPortNode()
1417
1418////////////////////////////////////////
1419void periphNode(xmlTextReaderPtr reader) 
1420{
1421    char * str;
1422    unsigned int value;
1423    unsigned int ok;
1424
1425    irq_loc_index = 0;
1426
1427    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1428
1429    if (periph_index >= MAX_PERIPHS) 
1430    {
1431        printf("[XML ERROR] The number of periphs is larger than %d\n", MAX_PERIPHS);
1432        exit(1);
1433    }
1434
1435#if XML_PARSER_DEBUG
1436printf("\n    periph %d\n", periph_index);
1437#endif
1438
1439    periph[periph_index] = (mapping_periph_t *) malloc(sizeof(mapping_periph_t));
1440
1441    ///////// get channels attribute (optionnal : 1 if missing)
1442    value = getIntValue(reader, "channels", &ok);
1443    if (ok) 
1444    {
1445#if XML_PARSER_DEBUG
1446printf("      channels    = %d\n", value);
1447#endif
1448        periph[periph_index]->channels = value;
1449    }
1450    else 
1451    {
1452        periph[periph_index]->channels = 1;
1453    }
1454
1455    ///////// get arg attribute (optionnal : 0 if missing)
1456    value = getIntValue(reader, "arg", &ok);
1457    if (ok) 
1458    {
1459#if XML_PARSER_DEBUG
1460printf("      arg         = %d\n", value);
1461#endif
1462        periph[periph_index]->arg = value;
1463    }
1464    else 
1465    {
1466        periph[periph_index]->arg = 1;
1467    }
1468
1469    /////////// get psegname attribute
1470    str = getStringValue(reader, "psegname", &ok);
1471    if (ok == 0) 
1472    {
1473        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1474                coproc_index, cluster_index);
1475        exit(1);
1476    }
1477
1478    /////////// set psegid attribute
1479    int index = getPsegId( cluster[cluster_index]->x, cluster[cluster_index]->y, str);
1480    if (index >= 0) 
1481    {
1482#if XML_PARSER_DEBUG
1483printf("      clusterid   = %d\n", cluster_index);
1484printf("      psegname    = %s\n", str);
1485printf("      psegid      = %d\n", index);
1486#endif
1487        periph[periph_index]->psegid = index;
1488    }
1489    else 
1490    {
1491        printf("[XML ERROR] pseg not found for periph %d / clusterid = %d / psegname = %s\n", 
1492                periph_loc_index, cluster_index, str );
1493        exit(1);
1494    } 
1495
1496    /////////// get type attribute
1497    str = getStringValue(reader, "type", &ok);
1498    if (ok) 
1499    {
1500#if XML_PARSER_DEBUG
1501printf("      type        = %s\n", str);
1502#endif
1503        unsigned int error = 0;
1504
1505        // initialize peripheral subtype
1506        periph[periph_index]->subtype = 0xFFFFFFFF;
1507
1508        // The CMA, FBF, HBA, IOB, IOC, NIC, ROM, SIM, TTY, peripherals are not
1509        // replicated in all clusters but can be instanciated twice.
1510
1511        ////////////////////////////
1512        if (strcmp(str, "CMA") == 0) 
1513        {
1514            periph[periph_index]->type = PERIPH_TYPE_CMA;
1515            if ( cma_channels < periph[periph_index]->channels )
1516            {
1517                cma_channels = periph[periph_index]->channels;
1518            }
1519        }
1520        /////////////////////////////////
1521        else if (strcmp(str, "FBF") == 0) 
1522        {
1523            periph[periph_index]->type = PERIPH_TYPE_FBF;
1524            use_fbf = 1;
1525        }
1526        /////////////////////////////////
1527        else if (strcmp(str, "IOB") == 0) 
1528        {
1529            periph[periph_index]->type = PERIPH_TYPE_IOB;
1530            use_iob = 1;
1531        }
1532        /////////////////////////////////
1533        else if (strcmp(str, "IOC") == 0) 
1534        {
1535            char* subtype = getStringValue(reader, "subtype", &ok);
1536            if (!ok)
1537            {
1538                printf("[XML ERROR] IOC peripheral needs a subtype: BDV, HBA or SPI\n");
1539                exit(1);
1540            } 
1541
1542            if ( strcmp(subtype, "BDV") == 0 )
1543            {
1544                periph[periph_index]->type    = PERIPH_TYPE_IOC;
1545                periph[periph_index]->subtype = PERIPH_SUBTYPE_BDV;
1546                ioc_channels = 1;
1547                if ( header->use_ram_disk == 0 ) use_bdv = 1;
1548            }
1549            else if ( strcmp(subtype, "HBA") == 0 )
1550            {
1551                periph[periph_index]->type    = PERIPH_TYPE_IOC;
1552                periph[periph_index]->subtype = PERIPH_SUBTYPE_HBA;
1553                ioc_channels = periph[periph_index]->channels;
1554                if ( header->use_ram_disk == 0 ) use_hba = 1;
1555            }
1556            else if ( strcmp(subtype, "SPI") == 0 )
1557            {
1558                periph[periph_index]->type    = PERIPH_TYPE_IOC;
1559                periph[periph_index]->subtype = PERIPH_SUBTYPE_SPI;
1560                ioc_channels = periph[periph_index]->channels;
1561                if ( header->use_ram_disk == 0 ) use_spi = 1;
1562            }
1563            else
1564            {
1565                printf("[XML ERROR] illegal subtype for IOC peripheral\n");
1566                exit(1);
1567            }
1568        } 
1569        /////////////////////////////////
1570        else if (strcmp(str, "NIC") == 0) 
1571        {
1572            periph[periph_index]->type = PERIPH_TYPE_NIC;
1573            if ( nic_channels < periph[periph_index]->channels )
1574            {
1575                nic_channels = periph[periph_index]->channels;
1576            }
1577        }
1578        /////////////////////////////////
1579        else if (strcmp(str, "ROM") == 0) 
1580        {
1581            periph[periph_index]->type = PERIPH_TYPE_ROM;
1582        } 
1583        /////////////////////////////////
1584        else if (strcmp(str, "SIM") == 0) 
1585        {
1586            periph[periph_index]->type = PERIPH_TYPE_SIM;
1587        } 
1588        /////////////////////////////////
1589        else if (strcmp(str, "TTY") == 0) 
1590        {
1591            periph[periph_index]->type = PERIPH_TYPE_TTY;
1592            if ( tty_channels < periph[periph_index]->channels )
1593            {
1594                tty_channels = periph[periph_index]->channels;
1595            }
1596        }
1597        /////////////////////////////////
1598        else if (strcmp(str, "PIC") == 0) 
1599        {
1600            periph[periph_index]->type = PERIPH_TYPE_PIC;
1601            if ( pic_channels < periph[periph_index]->channels )
1602            {
1603                pic_channels = periph[periph_index]->channels;
1604            }
1605            use_pic = 1;
1606        }
1607
1608
1609        // The DMA, ICU, MMC, TIM, XCU peripherals can be replicated in all clusters
1610        // but no more than one component of each type per cluster
1611
1612        /////////////////////////////////
1613        else if (strcmp(str, "DMA") == 0) 
1614        {
1615            periph[periph_index]->type = PERIPH_TYPE_DMA;
1616            if (found_dma)  error = 1; 
1617            found_dma = 1;
1618            if (dma_channels < periph[periph_index]->channels)
1619                dma_channels = periph[periph_index]->channels;
1620        }
1621        //////////////////////////////////
1622        else if (strcmp(str, "ICU") == 0) 
1623        {
1624            periph[periph_index]->type = PERIPH_TYPE_ICU;
1625            if (found_icu || use_xcu)  error = 1; 
1626            found_icu = 1;
1627
1628            if ( periph[periph_index]->channels < 
1629                 (header->irq_per_proc * cluster[cluster_index]->procs) )
1630            {
1631                printf("[XML ERROR] ICU channels smaller than PROCS * IRQ_PER_PROC\n");
1632                printf(" - icu channels = %d\n - nprocs = %d\n - irq_per_proc = %d\n",
1633                       periph[periph_index]->channels, 
1634                       cluster[cluster_index]->procs, 
1635                       header->irq_per_proc );
1636                exit(1);
1637            }
1638        }
1639        //////////////////////////////////
1640        else if (strcmp(str, "MMC") == 0) 
1641        {
1642            periph[periph_index]->type = PERIPH_TYPE_MMC;
1643            if (found_mmc)  error = 1; 
1644            found_mmc = 1;
1645            if ( periph[periph_index]->channels != 1 ) error = 1;
1646        }
1647        //////////////////////////////////
1648        else if (strcmp(str, "TIM") == 0 ) 
1649        {
1650            periph[periph_index]->type = PERIPH_TYPE_TIM;
1651            if (found_timer || use_xcu)  error = 1; 
1652            found_timer = 1;
1653            if (tim_channels < periph[periph_index]->channels) 
1654                tim_channels = periph[periph_index]->channels;
1655        }
1656        //////////////////////////////////
1657        else if (strcmp(str, "XCU") == 0) 
1658        {
1659            periph[periph_index]->type = PERIPH_TYPE_XCU;
1660            if (found_xcu || found_icu || found_timer)  error = 1; 
1661            found_xcu    = 1;
1662            found_timer  = 1;
1663            tim_channels = 32; 
1664            use_xcu      = 1;
1665
1666            if ( periph[periph_index]->channels < 
1667                 (header->irq_per_proc * cluster[cluster_index]->procs) )
1668            {
1669                printf("[XML ERROR] XCU channels smaller than PROCS * IRQ_PER_PROC\n");
1670                printf(" - xcu channels = %d\n - nprocs = %d\n - irq_per_proc = %d\n",
1671                       periph[periph_index]->channels, 
1672                       cluster[cluster_index]->procs, 
1673                       header->irq_per_proc );
1674                exit(1);
1675            }
1676        }
1677        else 
1678        {
1679            printf("[XML ERROR] illegal peripheral type: %s in cluster %d\n",
1680                    str, cluster_index);
1681            exit(1);
1682        }
1683
1684        if (error) 
1685        {
1686            printf("[XML ERROR] illegal peripheral %s in cluster %d\n",
1687                    str, cluster_index);
1688            exit(1);
1689        }
1690    }
1691    else 
1692    {
1693        printf("[XML ERROR] illegal or missing <type> for peripheral  %d in cluster %d\n",
1694                periph_loc_index, cluster_index);
1695        exit(1);
1696    }
1697
1698    ////////////// set irq_offset attribute
1699    periph[periph_index]->irq_offset = irq_index;
1700
1701    ///////////// get IRQs
1702    int status = xmlTextReaderRead(reader);
1703    while (status == 1) 
1704    {
1705        const char * tag = (const char *) xmlTextReaderConstName(reader);
1706
1707        if (strcmp(tag, "irq") == 0) 
1708        {
1709            if ( (periph[periph_index]->type != PERIPH_TYPE_ICU) &&
1710                 (periph[periph_index]->type != PERIPH_TYPE_XCU) &&
1711                 (periph[periph_index]->type != PERIPH_TYPE_PIC) )
1712            {
1713                printf("[XML ERROR] periph %d in cluster(%d,%d) "
1714                       " only ICU, XCU and PIC can contain IRQs",
1715                periph_loc_index, cluster[cluster_index]->x, cluster[cluster_index]->y);
1716                exit(1);
1717            }
1718            else
1719            {
1720                  irqNode(reader);
1721            }
1722        }
1723        else if (strcmp(tag, "#text")    == 0) { }
1724        else if (strcmp(tag, "#comment") == 0) { }
1725        else if (strcmp(tag, "periph")   == 0) 
1726        {
1727            periph[periph_index]->irqs = irq_loc_index;
1728            cluster[cluster_index]->periphs++;
1729            periph_loc_index++;
1730            periph_index++;
1731
1732#if XML_PARSER_DEBUG
1733printf("      irqs        = %d\n", irq_loc_index);
1734printf("      irq_offset  = %d\n", irq_index);
1735#endif
1736            return;
1737        }
1738        else 
1739        {
1740            printf("[XML ERROR] Unknown tag %s", tag);
1741            exit(1);
1742        }
1743        status = xmlTextReaderRead(reader);
1744    }
1745} // end periphNode
1746
1747////////////////////////////////////////
1748void coprocNode(xmlTextReaderPtr reader) 
1749{
1750    char * str;
1751    unsigned int ok;
1752
1753    cp_port_loc_index = 0;
1754
1755    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1756
1757    if (coproc_index >= MAX_COPROCS) 
1758    {
1759        printf("[XML ERROR] The number of coprocs is larger than %d\n", MAX_COPROCS);
1760        exit(1);
1761    }
1762
1763#if XML_PARSER_DEBUG
1764printf("\n  coproc %d\n", coproc_index);
1765#endif
1766
1767    coproc[coproc_index] = (mapping_coproc_t *) malloc(sizeof(mapping_coproc_t));
1768
1769    /////////// get name attribute
1770    str = getStringValue(reader, "name", &ok);
1771    if (ok) 
1772    {
1773#if XML_PARSER_DEBUG
1774printf("      name = %s\n", str);
1775#endif
1776        strncpy(coproc[coproc_index]->name, str, 31);
1777    }
1778    else 
1779    {
1780        printf("[XML ERROR] illegal or missing <name> for coproc %d in cluster %d\n",
1781                coproc_index, cluster_index);
1782        exit(1);
1783    }
1784
1785    /////////// get psegname attribute
1786    str = getStringValue(reader, "psegname", &ok);
1787    if (ok == 0) 
1788    {
1789        printf("[XML ERROR] illegal or missing <psegname> for coproc %d in cluster %d\n", 
1790                coproc_index, cluster_index);
1791        exit(1);
1792    }
1793
1794    /////////// set psegid attribute
1795    int index = getPsegId( cluster[cluster_index]->x, cluster[cluster_index]->y, str);
1796    if (index >= 0) 
1797    {
1798#if XML_PARSER_DEBUG
1799printf("      clusterid = %d\n", cluster_index);
1800printf("      psegname  = %s\n", str);
1801printf("      psegid    = %d\n", index);
1802#endif
1803        coproc[coproc_index]->psegid = index;
1804        assert(pseg[index]->type == PSEG_TYPE_PERI && 
1805        "coproc psegname attribute must refer to a pseg of type PERI" );
1806    }
1807    else 
1808    {
1809        printf("[XML ERROR] pseg not found for coproc %d / clusterid = %d / psegname = %s\n", 
1810                coproc_index, cluster_index, str );
1811        exit(1);
1812    } 
1813
1814    ////////// set port_offset
1815    coproc[coproc_index]->port_offset = cp_port_index;
1816
1817#if XML_PARSER_DEBUG
1818printf("      port_offset = %d\n", cp_port_index);
1819#endif
1820
1821    int status = xmlTextReaderRead(reader);
1822    while (status == 1) 
1823    {
1824        const char * tag = (const char *) xmlTextReaderConstName(reader);
1825
1826        if (strcmp(tag, "port") == 0 ) 
1827        {
1828            cpPortNode(reader);
1829        }
1830        else if (strcmp(tag, "#text")    == 0 ) { }
1831        else if (strcmp(tag, "#comment") == 0 ) { }
1832        else if (strcmp(tag, "coproc") == 0 ) 
1833        {
1834            coproc[coproc_index]->ports = cp_port_loc_index;
1835            cluster[cluster_index]->coprocs++;
1836            coproc_loc_index++;
1837            coproc_index++;
1838            return;
1839        }
1840        else 
1841        {
1842            printf("[XML ERROR] Unknown tag %s", tag);
1843            exit(1);
1844        }
1845        status = xmlTextReaderRead(reader);
1846    }
1847} // end coprocNode()
1848
1849
1850//////////////////////////////////////
1851void procNode(xmlTextReaderPtr reader) 
1852{
1853    unsigned int ok;
1854    unsigned int value;
1855
1856    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1857
1858    if (proc_index >= MAX_PROCS) 
1859    {
1860        printf("[XML ERROR] The number of procs is larger than %d\n", MAX_PROCS);
1861        exit(1);
1862    }
1863
1864#if XML_PARSER_DEBUG
1865printf("\n    proc %d\n", proc_index);
1866#endif
1867
1868    proc[proc_index] = (mapping_proc_t *) malloc(sizeof(mapping_proc_t));
1869
1870    /////////// get index attribute (optional)
1871    value = getIntValue(reader, "index", &ok);
1872    if (ok && (value != proc_loc_index)) 
1873    {
1874        printf("[XML ERROR] wrong local proc index / expected value is %d", 
1875                proc_loc_index);
1876        exit(1);
1877    }
1878    proc[proc_index]->index = proc_loc_index;
1879
1880    cluster[cluster_index]->procs++;
1881    proc_loc_index++;
1882    proc_index++;
1883    total_procs++;
1884} // end procNode()
1885
1886
1887//////////////////////////////////////
1888void psegNode(xmlTextReaderPtr reader) 
1889{
1890    unsigned int ok;
1891    paddr_t      ll_value;
1892    char * str;
1893
1894    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
1895
1896    if (pseg_index >= MAX_PSEGS) 
1897    {
1898        printf("[XML ERROR] The number of psegs is larger than %d\n", MAX_PSEGS);
1899        exit(1);
1900    }
1901
1902#if XML_PARSER_DEBUG
1903printf("    pseg %d\n", pseg_index);
1904#endif
1905
1906    pseg[pseg_index] = (mapping_pseg_t *) malloc(sizeof(mapping_pseg_t));
1907
1908    /////// get name attribute
1909    str = getStringValue(reader, "name", &ok);
1910#if XML_PARSER_DEBUG
1911printf("      name = %s\n", str);
1912#endif
1913    if (ok) 
1914    {
1915        strncpy(pseg[pseg_index]->name, str, 31);
1916    }
1917    else 
1918    {
1919        printf("[XML ERROR] illegal or missing <name> for pseg %d in cluster %d\n",
1920                pseg_index, cluster_index);
1921        exit(1);
1922    }
1923
1924    //////// get type attribute
1925    str = getStringValue(reader, "type", &ok);
1926#if XML_PARSER_DEBUG
1927printf("      type = %s\n", str);
1928#endif
1929    if      (ok && (strcmp(str, "RAM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_RAM; }
1930    else if (ok && (strcmp(str, "ROM" ) == 0)) { pseg[pseg_index]->type = PSEG_TYPE_ROM; }
1931    else if (ok && (strcmp(str, "PERI") == 0)) { pseg[pseg_index]->type = PSEG_TYPE_PERI; }
1932    else 
1933    {
1934        printf("[XML ERROR] illegal or missing <type> for pseg %s in cluster %d\n",
1935                pseg[pseg_index]->name, cluster_index);
1936        exit(1);
1937    }
1938
1939    //////// get base attribute
1940    ll_value = getPaddrValue(reader, "base", &ok);
1941#if XML_PARSER_DEBUG
1942printf("      base = 0x%llx\n", ll_value);
1943#endif
1944    if (ok) 
1945    {
1946        pseg[pseg_index]->base = ll_value;
1947    }
1948    else {
1949        printf("[XML ERROR] illegal or missing <base> for pseg %s in cluster %d\n",
1950                pseg[pseg_index]->name, cluster_index);
1951        exit(1);
1952    }
1953
1954    //////// get length attribute
1955    ll_value = getPaddrValue(reader, "length", &ok);
1956#if XML_PARSER_DEBUG
1957printf("      length = 0x%llx\n", ll_value);
1958#endif
1959    if (ok) 
1960    {
1961        pseg[pseg_index]->length = ll_value;
1962    } 
1963    else 
1964    {
1965        printf("[XML ERROR] illegal or missing <length> for pseg %s in cluster %d\n",
1966                pseg[pseg_index]->name, cluster_index);
1967        exit(1);
1968    }
1969
1970    //////// set cluster attribute
1971    pseg[pseg_index]->clusterid = cluster_index;
1972
1973    //////// set next_vseg attribute
1974    pseg[pseg_index]->next_vseg = 0;
1975
1976    pseg_index++;
1977    cluster[cluster_index]->psegs++;
1978} // end psegNode()
1979
1980
1981/////////////////////////////////////////
1982void clusterNode(xmlTextReaderPtr reader) 
1983{
1984    unsigned int ok;
1985    unsigned int value;
1986
1987    cluster[cluster_index] = (mapping_cluster_t *) malloc(sizeof(mapping_cluster_t));
1988
1989    //initialise variables that will be incremented by *Node() functions
1990    cluster[cluster_index]->psegs = 0;
1991    cluster[cluster_index]->procs = 0;
1992    cluster[cluster_index]->coprocs = 0;
1993    cluster[cluster_index]->periphs = 0;
1994
1995    //initialise global variables
1996    proc_loc_index = 0;
1997    coproc_loc_index = 0;
1998    periph_loc_index = 0;
1999
2000    // for replicated periph
2001    found_timer = 0;
2002    found_icu   = 0;
2003    found_xcu   = 0;
2004    found_dma   = 0;
2005    found_mmc   = 0;
2006
2007    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT)  return;
2008
2009#if XML_PARSER_DEBUG
2010printf("\n  cluster %d\n", cluster_index);
2011#endif
2012
2013    /////////// get x coordinate
2014    value = getIntValue(reader, "x", &ok);
2015#if XML_PARSER_DEBUG
2016printf("    x             = %d\n", value);
2017#endif
2018    if (ok && (value < header->x_size) ) 
2019    {
2020        cluster[cluster_index]->x = value;
2021    }
2022    else
2023    {
2024        printf("[XML ERROR] Illegal or missing < x > attribute for cluster %d", 
2025                cluster_index);
2026        exit(1);
2027    }
2028
2029    /////////// get y coordinate
2030    value = getIntValue(reader, "y", &ok);
2031#if XML_PARSER_DEBUG
2032printf("    y             = %d\n", value);
2033#endif
2034    if (ok && (value < header->y_size) ) 
2035    {
2036        cluster[cluster_index]->y = value;
2037    }
2038    else
2039    {
2040        printf("[XML ERROR] Illegal or missing < y > attribute for cluster %d", 
2041                cluster_index);
2042        exit(1);
2043    }
2044
2045    ////////// set offsets
2046    cluster[cluster_index]->pseg_offset = pseg_index;
2047    cluster[cluster_index]->proc_offset = proc_index;
2048    cluster[cluster_index]->coproc_offset = coproc_index;
2049    cluster[cluster_index]->periph_offset = periph_index;
2050
2051#if XML_PARSER_DEBUG
2052printf("    pseg_offset   = %d\n", pseg_index);
2053printf("    proc_offset   = %d\n", proc_index);
2054printf("    coproc_offset = %d\n", coproc_index);
2055printf("    periph_offset = %d\n", coproc_index);
2056#endif
2057
2058    ////////// get psegs, procs, coprocs and periphs
2059    int status = xmlTextReaderRead(reader);
2060
2061    while (status == 1) 
2062    {
2063        const char * tag = (const char *) xmlTextReaderConstName(reader);
2064
2065        if      (strcmp(tag, "pseg")     == 0) psegNode(reader);
2066        else if (strcmp(tag, "proc")     == 0) procNode(reader);
2067        else if (strcmp(tag, "coproc")   == 0) coprocNode(reader);
2068        else if (strcmp(tag, "periph")   == 0) periphNode(reader);
2069        else if (strcmp(tag, "#text")    == 0) { }
2070        else if (strcmp(tag, "#comment") == 0) { }
2071        else if (strcmp(tag, "cluster")  == 0) 
2072        {
2073            ///////// TIMER and ICU peripheral are mandatory when nprocs != 0
2074
2075            unsigned int procs = cluster[cluster_index]->procs;
2076            if ( procs && !found_timer && !found_xcu)
2077            {
2078                printf("[XML ERROR] missing timer peripheral in cluster %d\n", cluster_index);
2079                exit(1);
2080            }
2081
2082            if ( procs && !found_icu && !found_xcu) 
2083            {
2084                printf("[XML ERROR] missing icu peripheral in cluster %d\n", cluster_index);
2085                exit(1);
2086            }
2087
2088            if (nb_procs_max < procs) nb_procs_max = procs;
2089
2090#if XML_PARSER_DEBUG
2091printf("    psegs   = %d\n", cluster[cluster_index]->psegs);
2092printf("    procs   = %d\n", cluster[cluster_index]->procs);
2093printf("    coprocs = %d\n", cluster[cluster_index]->coprocs);
2094printf("    periphs = %d\n", cluster[cluster_index]->periphs);
2095printf("    end cluster %d\n", cluster_index);
2096#endif
2097            cluster_index++;
2098            return;
2099        }
2100        status = xmlTextReaderRead(reader);
2101    }
2102} // end clusterNode()
2103
2104
2105//////////////////////////////////////////////
2106void clusterSetNode(xmlTextReaderPtr reader) 
2107{
2108    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
2109
2110#if XML_PARSER_DEBUG
2111printf("\n  clusters set\n");
2112#endif
2113
2114    int status = xmlTextReaderRead(reader);
2115    while (status == 1) 
2116    {
2117        const char * tag = (const char *) xmlTextReaderConstName(reader);
2118
2119        if      (strcmp(tag, "cluster")    == 0) { clusterNode(reader); }
2120        else if (strcmp(tag, "#text")      == 0) { }
2121        else if (strcmp(tag, "#comment")   == 0) { }
2122        else if (strcmp(tag, "clusterset") == 0) 
2123        {
2124            // checking number of clusters
2125            if ( cluster_index != (header->x_size * header->y_size) ) 
2126            {
2127                printf("[XML ERROR] Wrong number of clusters\n");
2128                exit(1);
2129            }
2130
2131            // checking TTY terminal for system boot
2132            if ( tty_channels == 0 )
2133            {
2134                printf("[XML ERROR] missing TTY peripheral\n");
2135                exit(1);
2136            }
2137
2138            // checking IOC sub-types
2139            if ( (use_bdv + use_hba + use_spi) > 1 )
2140            {
2141                printf("[XML ERROR] all IOC peripherals must have the same type\n");
2142                exit(1);
2143            }
2144
2145#if XML_PARSER_DEBUG
2146            printf("  end cluster set\n\n");
2147#endif
2148            header->psegs = pseg_index;
2149            header->procs = proc_index;
2150            header->irqs = irq_index;
2151            header->coprocs = coproc_index;
2152            header->cp_ports = cp_port_index;
2153            header->periphs = periph_index;
2154            return;
2155        }
2156        else 
2157        {
2158            printf("[XML ERROR] Unknown tag in clusterset node : %s",tag);
2159            exit(1);
2160        }
2161        status = xmlTextReaderRead(reader);
2162    }
2163} // end clusterSetNode()
2164
2165
2166///////////////////////////////////////////
2167void globalSetNode(xmlTextReaderPtr reader) 
2168{
2169    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
2170
2171#if XML_PARSER_DEBUG
2172    printf("  globals set\n");
2173#endif
2174
2175    int status = xmlTextReaderRead(reader);
2176    while (status == 1) 
2177    {
2178        const char * tag = (const char *) xmlTextReaderConstName(reader);
2179
2180        if      (strcmp(tag, "vseg")      == 0) 
2181        { 
2182            vsegNode( reader ); 
2183            header->globals = header->globals + 1;
2184        }
2185        else if (strcmp(tag, "#text")     == 0) { }
2186        else if (strcmp(tag, "#comment")  == 0) { }
2187        else if (strcmp(tag, "globalset") == 0) 
2188        {
2189#if XML_PARSER_DEBUG
2190            printf("  end global set\n\n");
2191#endif
2192            vseg_loc_index = 0;
2193            return;
2194        }
2195        else 
2196        {
2197            printf("[XML ERROR] Unknown tag in globalset node : %s",tag);
2198            exit(1);
2199        }
2200        status = xmlTextReaderRead(reader);
2201    }
2202} // end globalSetNode()
2203
2204
2205///////////////////////////////////////////
2206void vspaceSetNode(xmlTextReaderPtr reader)
2207{
2208    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
2209        return;
2210    }
2211
2212#if XML_PARSER_DEBUG
2213    printf("\n  vspaces set\n");
2214#endif
2215
2216    int status = xmlTextReaderRead ( reader );
2217    while (status == 1) {
2218        const char * tag = (const char *) xmlTextReaderConstName(reader);
2219
2220        if (strcmp(tag, "vspace") == 0) {
2221            vspaceNode(reader);
2222        }
2223        else if (strcmp(tag, "#text"    ) == 0 ) { }
2224        else if (strcmp(tag, "#comment" ) == 0 ) { }
2225        else if (strcmp(tag, "vspaceset") == 0 ) 
2226        {
2227            header->vsegs = vseg_index;
2228            header->vobjs = vobj_index;
2229            header->tasks = task_index;
2230            return;
2231        }
2232        else 
2233        {
2234            printf("[XML ERROR] Unknown tag in vspaceset node : %s",tag);
2235            exit(1);
2236        }
2237        status = xmlTextReaderRead(reader);
2238    }
2239} // end globalSetNode()
2240
2241
2242////////////////////////////////////////
2243void headerNode(xmlTextReaderPtr reader) 
2244{
2245    char * name;
2246    unsigned int value;
2247    unsigned int ok;
2248
2249    if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) return;
2250
2251#if XML_PARSER_DEBUG
2252    printf("mapping_info\n");
2253#endif
2254
2255    header = (mapping_header_t *) malloc(sizeof(mapping_header_t));
2256
2257    ////////// get name attribute
2258    name = getStringValue(reader, "name", &ok);
2259    if (ok) 
2260    {
2261#if XML_PARSER_DEBUG
2262        printf("  name = %s\n", name);
2263#endif
2264        strncpy( header->name, name, 31);
2265    }
2266    else 
2267    {
2268        printf("[XML ERROR] illegal or missing <name> attribute in header\n");
2269        exit(1);
2270    }
2271
2272    /////////// get signature attribute
2273    value = getIntValue(reader, "signature", &ok);
2274    if ( ok && (value == IN_MAPPING_SIGNATURE) )
2275    {
2276#if XML_PARSER_DEBUG
2277        printf("  signature = %x\n", value);
2278#endif
2279        header->signature = IN_MAPPING_SIGNATURE;
2280    }
2281    else
2282    {
2283        printf("[XML ERROR] illegal or missing <signature> for mapping %s\n",
2284        header->name );
2285        exit(1);
2286    }
2287
2288    /////////// get x_width attribute
2289    value = getIntValue(reader, "x_width", &ok);
2290    if (ok) 
2291    {
2292#if XML_PARSER_DEBUG
2293        printf("  x_width = %d\n", value);
2294#endif
2295        header->x_width = value;
2296    }
2297
2298    /////////// get y_width attribute
2299    value = getIntValue(reader, "y_width", &ok);
2300    if (ok) 
2301    {
2302#if XML_PARSER_DEBUG
2303        printf("  y_width = %d\n", value);
2304#endif
2305        header->y_width = value;
2306    }
2307
2308    /////////// get x_size attribute
2309    unsigned int x_size = getIntValue(reader, "x_size", &ok);
2310    if (ok) 
2311    {
2312#if XML_PARSER_DEBUG
2313        printf("  x_size  = %d\n", x_size);
2314#endif
2315        header->x_size = x_size;
2316    }
2317    else 
2318    {
2319        printf("[XML ERROR] illegal or missing <x_size> attribute in header\n");
2320        exit(1);
2321    }
2322
2323    /////////// get y_size attribute
2324    unsigned int y_size = getIntValue(reader, "y_size", &ok);
2325    if (ok) 
2326    {
2327#if XML_PARSER_DEBUG
2328        printf("  y_size  = %d\n", y_size);
2329#endif
2330        header->y_size = y_size;
2331    }
2332    else 
2333    {
2334        printf("[XML ERROR] illegal or missing <y_size> attribute in header\n");
2335        exit(1);
2336    }
2337
2338    /////////// get x_io attribute
2339    unsigned int x_io = getIntValue(reader, "x_io", &ok);
2340#if XML_PARSER_DEBUG
2341        printf("  x_io      = %d\n", x_io);
2342#endif
2343    if ( ok && (x_io < x_size) ) 
2344    {
2345        header->x_io = x_io;
2346    }
2347    else 
2348    {
2349        printf("[XML ERROR] illegal or missing <x_io> attribute in header\n");
2350        exit(1);
2351    }
2352
2353    /////////// get y_io attribute
2354    unsigned int y_io = getIntValue(reader, "y_io", &ok);
2355#if XML_PARSER_DEBUG
2356        printf("  y_io      = %d\n", y_io);
2357#endif
2358    if ( ok &&(y_io < y_size) ) 
2359    {
2360        header->y_io = y_io;
2361    }
2362    else 
2363    {
2364        printf("[XML ERROR] illegal or missing <y_io> attribute in header\n");
2365        exit(1);
2366    }
2367
2368    // check the number of cluster
2369    if ( (x_size * y_size) >= MAX_CLUSTERS )
2370    {
2371        printf("[XML ERROR] Number of clusters cannot be larger than %d\n", MAX_CLUSTERS);
2372        exit(1);
2373    }
2374
2375    ///////// get irq_per_proc attribute
2376    value = getIntValue(reader, "irq_per_proc", &ok);
2377    if (ok) 
2378    {
2379#if XML_PARSER_DEBUG
2380        printf("  irq_per_proc = %d\n", value);
2381#endif
2382        header->irq_per_proc = value;
2383    }
2384    else 
2385    {
2386        printf("[XML ERROR] illegal or missing <irq_per_proc> attribute in mapping\n");
2387        exit(1);
2388    }
2389
2390    ///////// get use_ram_disk attribute (default = 0)
2391    value = getIntValue(reader, "use_ram_disk", &ok);
2392    if (ok) 
2393    {
2394#if XML_PARSER_DEBUG
2395        printf("  use_ram_disk = %d\n", value);
2396#endif
2397        header->use_ram_disk = value;
2398    }
2399    else 
2400    {
2401        header->use_ram_disk = 0;
2402    }
2403
2404    ///////// set other header fields
2405    header->globals   = 0;
2406    header->vspaces   = 0;
2407    header->psegs     = 0;
2408    header->vsegs     = 0;
2409    header->vobjs     = 0;
2410    header->tasks     = 0;
2411    header->procs     = 0;
2412    header->irqs      = 0;
2413    header->coprocs   = 0;
2414    header->cp_ports  = 0;
2415    header->periphs   = 0;
2416
2417
2418    int status = xmlTextReaderRead(reader);
2419    while (status == 1) 
2420    {
2421        const char * tag = (const char *) xmlTextReaderConstName(reader);
2422
2423        if      (strcmp(tag, "clusterset")   == 0) { clusterSetNode(reader); }
2424        else if (strcmp(tag, "globalset")    == 0) { globalSetNode(reader); }
2425        else if (strcmp(tag, "vspaceset")    == 0) { vspaceSetNode(reader); }
2426        else if (strcmp(tag, "#text")        == 0) { }
2427        else if (strcmp(tag, "#comment")     == 0) { }
2428        else if (strcmp(tag, "mapping_info") == 0) 
2429        {
2430#if XML_PARSER_DEBUG
2431            printf("end mapping_info\n");
2432#endif
2433            return;
2434        }
2435        else 
2436        {
2437            printf("[XML ERROR] Unknown tag in header node : %s\n",tag);
2438            exit(1);
2439        }
2440        status = xmlTextReaderRead(reader);
2441    }
2442} // end headerNode()
2443
2444
2445///////////////////////////////////////
2446void BuildTable(int fdout, const char * type, unsigned int nb_elem,
2447                unsigned int elem_size, char ** table) 
2448{
2449    unsigned int i;
2450    // write element
2451    for (i = 0; i < nb_elem; i++) {
2452        if (elem_size != write(fdout, table[i], elem_size)) {
2453            printf("function %s: %s(%d) write  error \n", __FUNCTION__, type, i);
2454            exit(1);
2455        }
2456
2457#if XML_PARSER_DEBUG
2458        printf("Building binary: writing %s %d\n", type, i);
2459#endif
2460    }
2461}
2462
2463/////////////////////////////////////
2464int open_file(const char * file_path) 
2465{
2466    //open file
2467    int fdout = open( file_path, (O_CREAT | O_RDWR), (S_IWUSR | S_IRUSR) );
2468    if (fdout < 0) 
2469    {
2470        perror("open");
2471        exit(1);
2472    }
2473
2474    //reinitialise the file
2475    if (ftruncate(fdout, 0)) 
2476    {
2477        perror("truncate");
2478        exit(1);
2479    }
2480
2481    //#if XML_PARSER_DEBUG
2482    printf("%s\n", file_path);
2483    //#endif
2484
2485    return fdout;
2486}
2487
2488
2489/////////////////////////////////////
2490void buildBin(const char * file_path) 
2491{
2492    unsigned int length;
2493
2494    int fdout = open_file(file_path);
2495
2496#if XML_PARSER_DEBUG
2497printf("Building map.bin for %s\n", header->name);
2498printf("signature = %x\n", header->signature);
2499printf("x_size    = %d\n", header->x_size);
2500printf("y_size    = %d\n", header->y_size);
2501printf("x_width   = %d\n", header->x_width);
2502printf("y_width   = %d\n", header->y_width);
2503printf("vspaces   = %d\n", header->vspaces);
2504printf("psegs     = %d\n", header->psegs);
2505printf("vobjs     = %d\n", header->vobjs);
2506printf("vsegs     = %d\n", header->vsegs);
2507printf("tasks     = %d\n", header->tasks);
2508printf("procs     = %d\n", header->procs);
2509printf("irqs      = %d\n", header->irqs);
2510printf("coprocs   = %d\n", header->coprocs);
2511printf("periphs   = %d\n", header->periphs);
2512#endif
2513
2514    // write header to binary file
2515    length = write(fdout, (char *) header, sizeof(mapping_header_t));
2516    if (length != sizeof(mapping_header_t)) 
2517    {
2518        printf("write header error : length = %d \n", length);
2519        exit(1);
2520    }
2521
2522    // write clusters
2523    BuildTable(fdout, "cluster", cluster_index, sizeof(mapping_cluster_t), (char **) cluster);
2524    // write psegs
2525    BuildTable(fdout, "pseg", pseg_index, sizeof(mapping_pseg_t), (char **) pseg);
2526    // write vspaces
2527    BuildTable(fdout, "vspace", vspace_index, sizeof(mapping_vspace_t), (char **) vspace);
2528    // write vsegs
2529    BuildTable(fdout, "vseg", vseg_index, sizeof(mapping_vseg_t), (char **) vseg);
2530    // write vobjs
2531    BuildTable(fdout, "vobj", vobj_index, sizeof(mapping_vobj_t), (char **) vobj);
2532    // write tasks array
2533    BuildTable(fdout, "task", task_index, sizeof(mapping_task_t), (char **) task);
2534    //building procs array
2535    BuildTable(fdout, "proc", proc_index, sizeof(mapping_proc_t), (char **) proc);
2536    //building irqs array
2537    BuildTable(fdout, "irq", irq_index, sizeof(mapping_irq_t), (char **) irq);
2538    //building coprocs array
2539    BuildTable(fdout, "coproc", coproc_index, sizeof(mapping_coproc_t), (char **) coproc);
2540    //building cp_ports array
2541    BuildTable(fdout, "cp_port", cp_port_index, sizeof(mapping_cp_port_t),(char **) cp_port);
2542    //building periphs array
2543    BuildTable(fdout, "periph", periph_index, sizeof(mapping_periph_t), (char **) periph);
2544
2545    close(fdout);
2546
2547} // end buildBin()
2548
2549
2550///////////////////////////////////////////////////////////////////////////////////
2551// this function set the values of vspace_id and vobj_id fields for all cp_ports
2552///////////////////////////////////////////////////////////////////////////////////
2553void prepareBuild() 
2554{
2555    unsigned int i;
2556   
2557    for (i = 0; i < cp_port_index; i++) 
2558    {
2559        int vspace_id = getVspaceId(cp_port_vobj_ref[i]->vspace_name);
2560        if (vspace_id < 0) 
2561        {
2562            printf("[XML ERROR] illegal  <vspacename> for cp_port %d,\n", i);
2563            exit(1);
2564        }
2565        cp_port[i]->vspaceid = vspace_id;
2566
2567        int vobj_id = getVobjId( vspace_id, 
2568                                 cp_port_vobj_ref[i]->vobj_name, 
2569                                 vspace[vspace_id]->vobjs );
2570        if (vobj_id >= 0) 
2571        {
2572
2573#if XML_PARSER_DEBUG
2574            printf("\ncp_port = %d\n", i);
2575            printf("      vspace_name  = %s\n", cp_port_vobj_ref[i]->vspace_name);
2576            printf("      vobj_name    = %s\n", cp_port_vobj_ref[i]->vobj_name);
2577            printf("      vobj_index   = %d\n", vobj_id);
2578#endif
2579            cp_port[i]->mwmr_vobj_id = vobj_id;
2580
2581            assert((vobj[vspace[vspace_id]->vobj_offset + vobj_id]->type == VOBJ_TYPE_MWMR)
2582                    && "coproc ports must refer a vobj of type MWMR");
2583        }
2584        else 
2585        {
2586            printf("[XML ERROR]  <vobjname> not found for cp_port %d,\n", i);
2587            exit(1);
2588        }
2589    }
2590}
2591
2592//////////////////////////////////////////
2593void file_write(int fdout, char * towrite) 
2594{
2595    unsigned int size = strlen(towrite);
2596    if (size != write(fdout, towrite, size)) 
2597    {
2598        printf("file_write error");
2599        exit(1);
2600    }
2601}
2602
2603//////////////////////////////////////////////////
2604void def_int_write(int fdout, char * def, int num) 
2605{
2606    char  buf[64];
2607    sprintf(buf, "#define\t %s  %d\n", def, num);
2608    file_write(fdout, buf);
2609}
2610
2611//////////////////////////////////////////////////
2612void def_hex_write(int fdout, char * def, int num) 
2613{
2614    char  buf[64];
2615    sprintf(buf, "#define\t %s  0x%x\n", def, num);
2616    file_write(fdout, buf);
2617}
2618
2619///////////////////////////////////
2620void  genHd(const char * file_path) 
2621{
2622    int fdout = open_file(file_path);
2623
2624    char prol[80];
2625    sprintf(prol, "/* Generated from file %s.xml */\n\n",header->name);
2626
2627    char * ifdef  = "#ifndef _HARD_CONFIG_H\n#define _HARD_CONFIG_H\n\n";
2628    char * epil   = "\n#endif //_HARD_CONFIG_H";
2629
2630    file_write(fdout, prol);
2631    file_write(fdout, ifdef);
2632
2633    def_int_write(fdout, "X_SIZE            ", header->x_size);
2634    def_int_write(fdout, "Y_SIZE            ", header->y_size);
2635    def_int_write(fdout, "X_WIDTH           ", header->x_width);
2636    def_int_write(fdout, "Y_WIDTH           ", header->y_width);
2637    def_int_write(fdout, "X_IO              ", header->x_io);
2638    def_int_write(fdout, "Y_IO              ", header->y_io);
2639
2640    file_write(fdout, "\n");
2641
2642    def_int_write(fdout, "TOTAL_PROCS       ", total_procs);
2643    def_int_write(fdout, "NB_PROCS_MAX      ", nb_procs_max);
2644    def_int_write(fdout, "NB_TASKS_MAX      ", nb_tasks_max);
2645
2646    file_write(fdout, "\n");
2647
2648    def_int_write(fdout, "NB_TIM_CHANNELS   ", tim_channels);
2649    def_int_write(fdout, "NB_DMA_CHANNELS   ", dma_channels);
2650
2651    file_write(fdout, "\n");
2652
2653    def_int_write(fdout, "NB_TTY_CHANNELS   ", tty_channels);
2654    def_int_write(fdout, "NB_IOC_CHANNELS   ", ioc_channels);
2655    def_int_write(fdout, "NB_NIC_CHANNELS   ", nic_channels);
2656    def_int_write(fdout, "NB_CMA_CHANNELS   ", cma_channels);
2657
2658    file_write(fdout, "\n");
2659
2660    def_int_write(fdout, "USE_XICU          ", use_xcu);
2661    def_int_write(fdout, "USE_IOB           ", use_iob);
2662    def_int_write(fdout, "USE_PIC           ", use_pic);
2663    def_int_write(fdout, "USE_FBF           ", use_fbf);
2664
2665    file_write(fdout, "\n");
2666
2667    def_int_write(fdout, "USE_IOC_RDK       ", header->use_ram_disk);
2668    def_int_write(fdout, "USE_IOC_HBA       ", use_hba);
2669    def_int_write(fdout, "USE_IOC_BDV       ", use_bdv);
2670    def_int_write(fdout, "USE_IOC_SPI       ", use_spi);
2671
2672    file_write(fdout, "\n");
2673
2674    def_int_write(fdout, "IRQ_PER_PROCESSOR ", header->irq_per_proc);
2675
2676    file_write(fdout, epil);
2677
2678    close(fdout);
2679}
2680
2681////////////////////////////////////////////////////////
2682void ld_write(int fdout, char * seg, unsigned int addr) 
2683{
2684    char buf[64];
2685    sprintf(buf, "%s = 0x%x;\n", seg, addr);
2686    file_write(fdout, buf);
2687}
2688
2689//////////////////////////////////
2690void genLd(const char * file_path) 
2691{
2692    int          fdout = open_file(file_path);
2693    unsigned int count;
2694    unsigned int vseg_id;
2695    unsigned int base;      // vseg base
2696    unsigned int size;      // vseg size
2697
2698    char prol[80];
2699    sprintf(prol, "/* Generated from file %s.xml */\n\n",header->name);
2700
2701    file_write(fdout, prol);
2702
2703    // boot mandatory global vsegs
2704    for (vseg_id = 0 , count = 0 ; vseg_id < header->vsegs ; vseg_id++)
2705    {
2706        if ( strcmp(vseg[vseg_id]->name, "seg_boot_code") == 0 )
2707        {
2708            base = vseg[vseg_id]->vbase;
2709            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2710            ld_write(fdout, "seg_boot_code_base      ", base);
2711            ld_write(fdout, "seg_boot_code_size      ", size);
2712            count++;
2713        }
2714        else if ( strcmp(vseg[vseg_id]->name, "seg_boot_data") == 0 )
2715        {
2716            base = vseg[vseg_id]->vbase;
2717            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2718            ld_write(fdout, "seg_boot_data_base      ", base);
2719            ld_write(fdout, "seg_boot_data_size      ", size);
2720            count++;
2721        }
2722        else if ( strcmp(vseg[vseg_id]->name, "seg_boot_stack") == 0 )
2723        {
2724            base = vseg[vseg_id]->vbase;
2725            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2726            ld_write(fdout, "seg_boot_stack_base     ", base);
2727            ld_write(fdout, "seg_boot_stack_size     ", size);
2728            count++;       
2729        }
2730        else if ( strcmp(vseg[vseg_id]->name, "seg_boot_mapping") == 0 )
2731        {
2732            base = vseg[vseg_id]->vbase;
2733            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2734            ld_write(fdout, "seg_boot_mapping_base   ", base);
2735            ld_write(fdout, "seg_boot_mapping_size   ", size);
2736            count++;
2737        }
2738        else if ( strcmp(vseg[vseg_id]->name, "seg_boot_buffer") == 0 )
2739        {
2740            base = vseg[vseg_id]->vbase;
2741            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2742            ld_write(fdout, "seg_boot_buffer_base    ", base);
2743            ld_write(fdout, "seg_boot_buffer_size    ", size);
2744            count++;
2745        }
2746    }
2747
2748    if ( count != 5 )
2749    { 
2750        printf ("[XML ERROR] Missing mandatory Boot global vseg : only %d\n", count);
2751        printf ("Mandatory segments are :\n");
2752        printf (" - seg_boot_code\n");
2753        printf (" - seg_boot_data\n");
2754        printf (" - seg_boot_stack\n");
2755        printf (" - seg_boot_mapping\n");
2756        printf (" - seg_boot_buffer\n");
2757        exit(0);
2758    }
2759
2760    file_write(fdout, "\n");
2761
2762    // kernel mandatory global vsegs
2763    for (vseg_id = 0, count = 0 ; vseg_id < header->vsegs ; vseg_id++)
2764    {
2765        if ( strcmp(vseg[vseg_id]->name, "seg_kernel_code") == 0 )
2766        {
2767            base = vseg[vseg_id]->vbase;
2768            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2769            ld_write(fdout, "seg_kernel_code_base    ", base);
2770            ld_write(fdout, "seg_kernel_code_size    ", size);
2771            count++;
2772        }
2773        else if ( strcmp(vseg[vseg_id]->name, "seg_kernel_data") == 0 )
2774        {
2775            base = vseg[vseg_id]->vbase;
2776            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2777            ld_write(fdout, "seg_kernel_data_base    ", base);
2778            ld_write(fdout, "seg_kernel_data_size    ", size);
2779            count++;
2780        }
2781        else if ( strcmp(vseg[vseg_id]->name, "seg_kernel_uncdata") == 0 )
2782        {
2783            base = vseg[vseg_id]->vbase;
2784            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2785            ld_write(fdout, "seg_kernel_uncdata_base ", base);
2786            ld_write(fdout, "seg_kernel_uncdata_size ", size);
2787            count++;
2788        }
2789        else if ( strcmp(vseg[vseg_id]->name, "seg_kernel_init") == 0 )
2790        {
2791            base = vseg[vseg_id]->vbase;
2792            size = vobj[vseg[vseg_id]->vobj_offset]->length;
2793            ld_write(fdout, "seg_kernel_init_base    ", base);
2794            ld_write(fdout, "seg_kernel_init_size    ", size);
2795            count++;
2796        }
2797    }
2798    if ( count != 4 )
2799    { 
2800        printf ("[XML ERROR] Missing mandatory Kernel global vseg : only %d\n", count);
2801        printf ("Mandatory segments are :\n");
2802        printf (" - seg_kernel_code\n");
2803        printf (" - seg_kernel_data\n");
2804        printf (" - seg_kernel_uncdata\n");
2805        printf (" - seg_kernel_init\n");
2806        exit(0);
2807    }
2808
2809    file_write(fdout, "\n");
2810
2811    // boot and kernel optionnal global vsegs (pseudo ROMs)
2812    unsigned int seg_ram_disk_base    = 0xFFFFFFFF;
2813    unsigned int seg_ram_disk_size    = 0;
2814    unsigned int seg_reset_code_base  = 0xFFFFFFFF;
2815    unsigned int seg_reset_code_size  = 0;
2816    for (vseg_id = 0 ; vseg_id < header->vsegs ; vseg_id++)
2817    {
2818        if ( strcmp(vseg[vseg_id]->name, "seg_reset_code") == 0 )
2819        {
2820            seg_reset_code_base = vseg[vseg_id]->vbase;
2821            seg_reset_code_size = vobj[vseg[vseg_id]->vobj_offset]->length;
2822        }
2823        if ( strcmp(vseg[vseg_id]->name, "seg_ram_disk") == 0 )
2824        {
2825            seg_ram_disk_base   = vseg[vseg_id]->vbase;
2826            seg_ram_disk_size   = vobj[vseg[vseg_id]->vobj_offset]->length;
2827        }
2828    }
2829
2830    ld_write(fdout, "seg_reset_code_base     ", seg_reset_code_base);
2831    ld_write(fdout, "seg_reset_code_size     ", seg_reset_code_size);
2832    ld_write(fdout, "seg_ram_disk_base       ", seg_ram_disk_base);
2833    ld_write(fdout, "seg_ram_disk_size       ", seg_ram_disk_size);
2834   
2835    file_write(fdout, "\n");
2836
2837    // fill the peripherals base address array
2838    set_periph_vbase_array();
2839
2840    //  non replicated peripherals
2841    ld_write(fdout, "seg_cma_base            ",   periph_vbase_array[PERIPH_TYPE_CMA]);
2842    ld_write(fdout, "seg_fbf_base            ",   periph_vbase_array[PERIPH_TYPE_FBF]);
2843    ld_write(fdout, "seg_iob_base            ",   periph_vbase_array[PERIPH_TYPE_IOB]);
2844    ld_write(fdout, "seg_ioc_base            ",   periph_vbase_array[PERIPH_TYPE_IOC]);
2845    ld_write(fdout, "seg_nic_base            ",   periph_vbase_array[PERIPH_TYPE_NIC]);
2846    ld_write(fdout, "seg_rom_base            ",   periph_vbase_array[PERIPH_TYPE_ROM]);
2847    ld_write(fdout, "seg_sim_base            ",   periph_vbase_array[PERIPH_TYPE_SIM]);
2848    ld_write(fdout, "seg_tty_base            ",   periph_vbase_array[PERIPH_TYPE_TTY]);
2849    ld_write(fdout, "seg_pic_base            ",   periph_vbase_array[PERIPH_TYPE_PIC]);
2850
2851    file_write(fdout, "\n");
2852
2853    // replicated peripherals
2854    ld_write(fdout, "seg_dma_base            ",   periph_vbase_array[PERIPH_TYPE_DMA]);
2855    ld_write(fdout, "seg_icu_base            ",   periph_vbase_array[PERIPH_TYPE_ICU]);
2856    ld_write(fdout, "seg_mmc_base            ",   periph_vbase_array[PERIPH_TYPE_MMC]);
2857    ld_write(fdout, "seg_tim_base            ",   periph_vbase_array[PERIPH_TYPE_TIM]);
2858    ld_write(fdout, "seg_xcu_base            ",   periph_vbase_array[PERIPH_TYPE_XCU]);
2859
2860    file_write(fdout, "\n");
2861
2862    ld_write(fdout, "vseg_cluster_increment  ",   0x00010000);
2863
2864    close(fdout);
2865}
2866
2867//////////////////////////////////////////////////////
2868char * buildPath(const char * path, const char * name) 
2869{
2870    char * res = calloc(strlen(path) + strlen(name) + 1, 1);
2871    strcat(res, path);
2872    strcat(res, "/");
2873    strcat(res, name);
2874    return res; 
2875}
2876
2877
2878//////////////////////////////////
2879int main(int argc, char * argv[]) 
2880{
2881    if (argc < 3) {
2882        printf("Usage: xml2bin <input_file_path> <output_path>\n");
2883        return 1;
2884    }
2885
2886    struct stat dir_st;
2887    if (stat( argv[2], &dir_st)) {
2888        perror("bad path");
2889        exit(1);
2890    }
2891
2892    if ((dir_st.st_mode & S_IFDIR) == 0) {
2893        printf("path is not a dir: %s", argv[2] );
2894        exit(1);
2895    }
2896
2897    char * map_path = buildPath(argv[2], "map.bin"); 
2898    char * ld_path = buildPath(argv[2], "giet_vsegs.ld"); 
2899    char * hd_path = buildPath(argv[2], "hard_config.h"); 
2900
2901    LIBXML_TEST_VERSION;
2902
2903    int status;
2904    xmlTextReaderPtr reader = xmlReaderForFile(argv[1], NULL, 0);
2905
2906    if (reader != NULL) 
2907    {
2908        status = xmlTextReaderRead (reader);
2909        while (status == 1) 
2910        {
2911            const char * tag = (const char *) xmlTextReaderConstName(reader);
2912
2913            if (strcmp(tag, "mapping_info") == 0) 
2914            { 
2915                headerNode(reader);
2916                prepareBuild();
2917                buildBin(map_path);
2918                genHd(hd_path);
2919                genLd(ld_path);
2920            }
2921            else 
2922            {
2923                printf("[XML ERROR] Wrong file type: \"%s\"\n", argv[1]);
2924                return 1;
2925            }
2926            status = xmlTextReaderRead(reader);
2927        }
2928        xmlFreeTextReader(reader);
2929
2930        if (status != 0) 
2931        {
2932            printf("[XML ERROR] Wrong Syntax in \"%s\" file\n", argv[1]);
2933            return 1;
2934        }
2935    }
2936    return 0;
2937} // end main()
2938
2939
2940// Local Variables:
2941// tab-width: 4
2942// c-basic-offset: 4
2943// c-file-offsets:((innamespace . 0)(inline-open . 0))
2944// indent-tabs-mode: nil
2945// End:
2946// vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
2947
Note: See TracBrowser for help on using the repository browser.