2 BCU SDK bcu development enviroment
3 Copyright (C) 2005-2010 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "loadimage.h"
26 #include "xmlreadconfig.h"
30 ToTargetFloat (uchar v
[4], float f
)
32 memcpy (v
, &f
, 4); /* TODO: conversation */
36 ImageToDevice (Image
& i
, Device
& d
)
39 for (j
= 0; j
< i
.str (); j
++)
41 switch (i
.str
[j
]->getType ())
43 case S_StringParameter
:
46 STR_StringParameter
*l
= ((STR_StringParameter
*) i
.str
[j
]);
49 d
.StringParameters
.add (p
);
55 STR_IntParameter
*l
= ((STR_IntParameter
*) i
.str
[j
]);
58 d
.IntParameters
.add (p
);
61 case S_FloatParameter
:
64 STR_FloatParameter
*l
= ((STR_FloatParameter
*) i
.str
[j
]);
67 d
.FloatParameters
.add (p
);
73 STR_ListParameter
*l
= ((STR_ListParameter
*) i
.str
[j
]);
76 for (int k
= 0; k
< l
->elements (); k
++)
79 m
.Value
= l
->elements
[k
];
82 d
.ListParameters
.add (p
);
88 STR_GroupObject
*l
= ((STR_GroupObject
*) i
.str
[j
]);
92 d
.GroupObjects
.add (p
);
96 d
.BCU
= (BCUType
) ((STR_BCUType
*) i
.str
[j
])->bcutype
;
103 PatchImage (Image
& i
, Device
& d
)
106 STR_Code
*co
= (STR_Code
*) i
.findStream (S_Code
);
107 uchar
*c
= (uchar
*) co
->code
.array ();
108 for (j
= 0; j
< i
.str (); j
++)
110 switch (i
.str
[j
]->getType ())
112 case S_StringParameter
:
114 STR_StringParameter
*l
= ((STR_StringParameter
*) i
.str
[j
]);
115 for (k
= 0; k
< d
.StringParameters (); k
++)
116 if (d
.StringParameters
[k
].ID
== l
->name
)
118 const StringParameter
& p
= d
.StringParameters
[k
];
120 die (_("missing parameter value for %s"), p
.ID ());
121 if (strlen (p
.Value ()) + 1 > l
->length
)
122 die (_("string value for %s too long"), p
.ID ());
123 strcpy ((char *) c
+ l
->addr
- 0x100, p
.Value ());
128 STR_IntParameter
*l
= ((STR_IntParameter
*) i
.str
[j
]);
129 for (k
= 0; k
< d
.IntParameters (); k
++)
130 if (d
.IntParameters
[k
].ID
== l
->name
)
132 const IntParameter
& p
= d
.IntParameters
[k
];
134 die (_("missing parameter value for %s"), p
.ID ());
139 c
[l
->addr
- 0x100] = p
.Value
& 0xff;
143 c
[l
->addr
+ 0 - 0x100] = (p
.Value
>> 8) & 0xff;
144 c
[l
->addr
+ 1 - 0x100] = p
.Value
& 0xff;
148 c
[l
->addr
+ 0 - 0x100] = (p
.Value
>> 24) & 0xff;
149 c
[l
->addr
+ 1 - 0x100] = (p
.Value
>> 16) & 0xff;
150 c
[l
->addr
+ 2 - 0x100] = (p
.Value
>> 8) & 0xff;
151 c
[l
->addr
+ 3 - 0x100] = p
.Value
& 0xff;
155 c
[l
->addr
+ 0 - 0x100] = (p
.Value
>> 56) & 0xff;
156 c
[l
->addr
+ 1 - 0x100] = (p
.Value
>> 48) & 0xff;
157 c
[l
->addr
+ 2 - 0x100] = (p
.Value
>> 40) & 0xff;
158 c
[l
->addr
+ 3 - 0x100] = (p
.Value
>> 32) & 0xff;
159 c
[l
->addr
+ 4 - 0x100] = (p
.Value
>> 24) & 0xff;
160 c
[l
->addr
+ 5 - 0x100] = (p
.Value
>> 16) & 0xff;
161 c
[l
->addr
+ 6 - 0x100] = (p
.Value
>> 8) & 0xff;
162 c
[l
->addr
+ 7 - 0x100] = p
.Value
& 0xff;
165 die (_("unknown type %d for parameter %s"), l
->type
, p
.ID ());
169 case S_FloatParameter
:
171 STR_FloatParameter
*l
= ((STR_FloatParameter
*) i
.str
[j
]);
172 for (k
= 0; k
< d
.FloatParameters (); k
++)
173 if (d
.FloatParameters
[k
].ID
== l
->name
)
175 const FloatParameter
& p
= d
.FloatParameters
[k
];
177 die (_("missing parameter value for %s"), p
.ID ());
180 ToTargetFloat (v
, f
);
181 memcpy (c
+ l
->addr
- 0x100, v
, 4);
184 case S_ListParameter
:
186 STR_ListParameter
*l
= ((STR_ListParameter
*) i
.str
[j
]);
187 for (k
= 0; k
< d
.ListParameters (); k
++)
188 if (d
.ListParameters
[k
].ID
== l
->name
)
190 const ListParameter
& p
= d
.ListParameters
[k
];
192 die (_("missing parameter value for %s"), p
.ID ());
194 for (i
= 0; i
< l
->elements (); i
++)
195 if (l
->elements
[i
] == p
.Value
)
197 if (i
== l
->elements ())
198 die (_("unknown element %s for parameter %s"), p
.Value (),
202 c
[l
->addr
- 0x100] = i
& 0xff;
206 c
[l
->addr
+ 0 - 0x100] = (i
>> 8) & 0xff;
207 c
[l
->addr
+ 1 - 0x100] = i
& 0xff;
217 if (d
.BCU
== BCU_bcu12
)
219 addrtab_start
= 0x16;
220 assoctab_start
= c
[0x11];
221 groupobj_start
= c
[0x12];
225 STR_BCU2Start
*s
= (STR_BCU2Start
*) i
.findStream (S_BCU2Start
);
226 addrtab_start
= s
->addrtab_start
- 0x100;
227 assoctab_start
= s
->assoctab_start
- 0x100;
228 groupobj_start
= s
->groupobj_ptr
- 0x100;
234 for (i
= 0; i
< d
.GroupObjects (); i
++)
235 BuildObjAddress (d
.GroupObjects
[i
], d
.BCU
);
237 BuildAddrTable (t
, d
);
241 if (t
.addr () + 1 > c
[addrtab_start
])
242 die (_("need %d addresses, only space for %d addresses"), t
.addr () + 1,
244 c
[addrtab_start
] = t
.addr () + 1;
245 c
[addrtab_start
+ 1] = (d
.PhysicalAddress
>> 8) & 0xff;
246 c
[addrtab_start
+ 2] = (d
.PhysicalAddress
) & 0xff;
247 for (i
= 0; i
< t
.addr (); i
++)
249 c
[addrtab_start
+ i
* 2 + 3] = (t
.addr
[i
] >> 8) & 0xff;
250 c
[addrtab_start
+ i
* 2 + 4] = (t
.addr
[i
]) & 0xff;
252 if (maxs
> c
[addrtab_start
])
253 die (_("need %d associations, only space for %d assocations"), maxs
,
255 c
[assoctab_start
] = maxs
;
256 for (i
= 0; i
< maxs
; i
++)
258 c
[assoctab_start
+ i
* 2 + 1] = t
.Addr
[i
] + 1;
259 c
[assoctab_start
+ i
* 2 + 2] = t
.ObjNo
[i
];
261 for (i
= 0; i
< d
.GroupObjects (); i
++)
263 uchar f
= GroupObjectFlag (d
.GroupObjects
[i
], d
.BCU
);
264 c
[groupobj_start
+ 2 + 3 * d
.GroupObjects
[i
].ObjNo
+ 1] = f
;
269 STR_BCU2Key
*k
= (STR_BCU2Key
*) i
.findStream (S_BCU2Key
);
275 if (d
.InstallKey_lineno
)
276 k
->installkey
= d
.InstallKey
;
279 for (i
= 0; i
< 3; i
++)
280 k
->keys
[i
] = 0xffffffff;
281 for (i
= 0; i
< d
.Keys (); i
++)
283 if (d
.Keys
[i
].level
< 0 || d
.Keys
[i
].level
> 2)
284 die (_("unsupported key level %d"), d
.Keys
[i
].level
);
285 k
->keys
[d
.Keys
[i
].level
] = d
.Keys
[i
].key
;
291 main (int ac
, char *ag
[])
299 die (_("%s image config output"), ag
[0]);
301 FILE *f
= fopen (ag
[1], "r");
303 die (_("open of %s failed"), ag
[1]);
307 int i
= fread (buf
, 1, sizeof (buf
), f
);
308 p
.setpart (buf
, p (), i
);
313 printf ("%s", HexDump (p
) ());
315 Image
*i
= Image::fromArray (p
);
317 die (_("not a image"));
320 die (_("not a valid image\n"));
322 printf ("%s\n", i
->decode ()());
326 BCU_LOAD_RESULT r
= PrepareLoadImage (p
, im
);
327 if (r
!= IMG_IMAGE_LOADABLE
)
328 die (_("%s"), decodeBCULoadResult (r
) ());
330 ImageToDevice (*i
, d
);
332 xmlReadConfigInformation (d
, ag
[2]);
336 printf ("%s\n", i
->decode ()());
338 f
= fopen (ag
[3], "w");
340 die (_("can not write to %s"), ag
[3]);
343 printf ("%s", HexDump (p
) ());
344 fwrite (p
.array (), 1, p (), f
);