2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2007 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "loadimage.h"
24 GenAlloc (CArray
& req
, uint16_t start
, uint16_t len
, uint8_t access
,
25 uint8_t type
, bool check
)
27 const uchar zero
[10] = { 0 };
30 req
[2] = (start
>> 8) & 0xff;
31 req
[3] = (start
) & 0xff;
32 req
[4] = (len
>> 8) & 0xff;
33 req
[5] = (len
) & 0xff;
36 req
[8] = check
? 0x80 : 0x00;
46 AddSegmentOverlap (Array
< Segment
> &s
, uint16_t start
, uint16_t len
)
51 for (i
= 0; i
< s (); i
++)
53 if (start
>= s
[i
].start
&& start
< s
[i
].start
+ s
[i
].len
)
55 if (s
[i
].start
>= start
&& s
[i
].start
< start
+ len
)
67 PrepareLoadImage (const CArray
& im
, BCUImage
* &img
)
69 Array
< Segment
> seg
;
71 Image
*i
= Image::fromArray (im
);
73 return IMG_UNRECOG_FORMAT
;
78 return IMG_INVALID_FORMAT
;
81 STR_BCUType
*b
= (STR_BCUType
*) i
->findStream (S_BCUType
);
85 return IMG_NO_BCUTYPE
;
87 STR_Code
*c
= (STR_Code
*) i
->findStream (S_Code
);
93 if (b
->bcutype
== 0x0012)
95 STR_BCU1Size
*s
= (STR_BCU1Size
*) i
->findStream (S_BCU1Size
);
102 if (s
->datasize
+ s
->bsssize
+ s
->stacksize
> 18)
105 return IMG_LODATA_OVERFLOW
;
107 if (s
->textsize
> 0xfe)
110 return IMG_TEXT_OVERFLOW
;
113 if (s
->textsize
!= c
->code ())
116 return IMG_WRONG_SIZE
;
119 if (s
->textsize
< 0x18)
122 return IMG_NO_ADDRESS
;
124 if (c
->code
[8] < 8 || c
->code
[8] > c
->code () + 1)
127 return IMG_WRONG_CHECKLIM
;
131 img
->BCUType
= BCUImage::B_bcu1
;
132 img
->addr
= (c
->code
[0x17] << 8) | (c
->code
[0x18]);
134 return IMG_IMAGE_LOADABLE
;
136 if (b
->bcutype
== 0x0020 || b
->bcutype
== 0x0021)
138 STR_BCU2Size
*s
= (STR_BCU2Size
*) i
->findStream (S_BCU2Size
);
144 if (s
->lo_datasize
+ s
->lo_bsssize
> 18)
147 return IMG_LODATA_OVERFLOW
;
149 if (s
->hi_datasize
+ s
->hi_bsssize
> 24)
152 return IMG_HIDATA_OVERFLOW
;
154 if (s
->textsize
> 0x36f)
157 return IMG_TEXT_OVERFLOW
;
160 if (s
->textsize
!= c
->code ())
163 return IMG_WRONG_SIZE
;
166 if (s
->textsize
< 0x18)
169 return IMG_NO_ADDRESS
;
171 STR_BCU2Start
*s1
= (STR_BCU2Start
*) i
->findStream (S_BCU2Start
);
177 if (s1
->addrtab_start
!= 0x116 || s1
->addrtab_size
< 4)
180 return IMG_WRONG_ADDRTAB
;
182 if (s1
->addrtab_size
> 0xff)
185 return IMG_ADDRTAB_OVERFLOW
;
188 AddSegmentOverlap (seg
, s1
->addrtab_start
, s1
->addrtab_size
);
190 if (!AddSegmentOverlap (seg
, s1
->assoctab_start
, s1
->assoctab_size
))
193 return IMG_OVERLAP_ASSOCTAB
;
195 if (s1
->assoctab_size
> 0xff)
198 return IMG_ADDRTAB_OVERFLOW
;
200 if (s1
->readonly_end
< s1
->readonly_start
)
203 return IMG_NEGATIV_TEXT_SIZE
;
205 if (!AddSegmentOverlap
206 (seg
, s1
->readonly_start
, s1
->readonly_end
- s1
->readonly_start
))
209 return IMG_OVERLAP_TEXT
;
211 if (s1
->param_end
< s1
->param_start
)
214 return IMG_NEGATIV_TEXT_SIZE
;
216 if (s1
->eeprom_end
< s1
->eeprom_start
)
219 return IMG_NEGATIV_TEXT_SIZE
;
221 if (!AddSegmentOverlap
222 (seg
, s1
->eeprom_start
, s1
->eeprom_end
- s1
->eeprom_start
))
225 return IMG_OVERLAP_EEPROM
;
227 if (!AddSegmentOverlap
228 (seg
, s1
->param_start
, s1
->param_end
- s1
->param_start
))
231 return IMG_OVERLAP_PARAM
;
233 if (s1
->obj_count
> 0xff)
236 return IMG_OBJTAB_OVERFLOW
;
238 if (s1
->param_end
> c
->code () + 0x100)
241 return IMG_WRONG_LOADCTL
;
244 STR_BCU2Key
*s2
= (STR_BCU2Key
*) i
->findStream (S_BCU2Key
);
245 if (s2
&& s2
->keys () != 3)
248 return IMG_INVALID_KEY
;
254 (b
->bcutype
== 0x0020 ? BCUImage::B_bcu20
: BCUImage::B_bcu21
);
255 img
->addr
= (c
->code
[0x17] << 8) | (c
->code
[0x18]);
259 img
->installkey
= s2
->installkey
;
260 img
->keys
= s2
->keys
;
264 img
->installkey
= 0xFFFFFFFF;
265 img
->keys
.resize (3);
266 img
->keys
[0] = 0xFFFFFFFF;
267 img
->keys
[1] = 0xFFFFFFFF;
268 img
->keys
[2] = 0xFFFFFFFF;
271 const uchar zero
[10] = { 0 };
278 r
.req
.set (zero
, 10);
282 r
.error
= IMG_UNLOAD_ADDR
;
285 r
.error
= IMG_UNLOAD_ASSOC
;
288 r
.error
= IMG_UNLOAD_PROG
;
292 r
.req
.set (zero
, 10);
296 r
.error
= IMG_LOAD_ADDR
;
299 GenAlloc (r
.req
, s1
->addrtab_start
, s1
->addrtab_size
, 0x31, 0x03, 1);
300 r
.memaddr
= s1
->addrtab_start
;
302 r
.error
= IMG_WRITE_ADDR
;
307 r
.len
= s1
->addrtab_size
- 4;
314 r
.req
[2] = (s1
->addrtab_start
>> 8) & 0xff;
315 r
.req
[3] = (s1
->addrtab_start
) & 0xff;
316 r
.req
[4] = c
->code
[0x09];
317 r
.req
.setpart (c
->code
.array () + 0x03, 5, 5);
318 r
.error
= IMG_SET_ADDR
;
321 r
.req
.set (zero
, 10);
324 r
.error
= IMG_FINISH_ADDR
;
328 r
.req
.set (zero
, 10);
332 r
.error
= IMG_LOAD_ASSOC
;
335 GenAlloc (r
.req
, s1
->assoctab_start
, s1
->assoctab_size
, 0x31, 0x03, 1);
336 r
.memaddr
= s1
->assoctab_start
;
337 r
.len
= s1
->assoctab_size
- 1;
338 r
.error
= IMG_WRITE_ASSOC
;
344 r
.req
[2] = (s1
->assoctab_start
>> 8) & 0xff;
345 r
.req
[3] = (s1
->assoctab_start
) & 0xff;
346 r
.req
[4] = c
->code
[0x09];
347 r
.req
.setpart (c
->code
.array () + 0x03, 5, 5);
348 r
.error
= IMG_SET_ASSOC
;
351 r
.req
.set (zero
, 10);
354 r
.error
= IMG_FINISH_ASSOC
;
358 r
.req
.set (zero
, 10);
362 r
.error
= IMG_LOAD_PROG
;
365 GenAlloc (r
.req
, 0x00C8, 0x0018, 0x32, 0x01, 0);
366 r
.error
= IMG_ALLOC_LORAM
;
369 GenAlloc (r
.req
, 0x0972, 0x004A, 0x32, 0x02, 0);
370 r
.error
= IMG_ALLOC_HIRAM
;
373 GenAlloc (r
.req
, 0x0100, 0x0016, 0x32, 0x03, 0);
374 r
.error
= IMG_ALLOC_INIT
;
385 GenAlloc (r
.req
, s1
->readonly_start
,
386 s1
->readonly_end
- s1
->readonly_start
, 0x30, 0x03, 1);
387 r
.error
= IMG_ALLOC_RO
;
388 r
.len
= s1
->readonly_end
- s1
->readonly_start
- 1;
389 r
.memaddr
= s1
->readonly_start
;
393 GenAlloc (r
.req
, s1
->eeprom_start
, s1
->eeprom_end
- s1
->eeprom_start
,
395 r
.error
= IMG_ALLOC_EEPROM
;
396 r
.len
= s1
->eeprom_end
- s1
->eeprom_start
;
397 r
.memaddr
= s1
->eeprom_start
;
401 GenAlloc (r
.req
, s1
->param_start
, s1
->param_end
- s1
->param_start
, 0x32,
403 r
.error
= IMG_ALLOC_PARAM
;
404 r
.len
= s1
->param_end
- s1
->param_start
;
405 r
.memaddr
= s1
->param_start
;
412 r
.req
[2] = (s1
->runaddr
>> 8) & 0xff;
413 r
.req
[3] = (s1
->runaddr
) & 0xff;
414 r
.req
[4] = c
->code
[0x09];
415 r
.req
.setpart (c
->code
.array () + 0x03, 5, 5);
416 r
.error
= IMG_SET_PROG
;
419 r
.req
.set (zero
, 10);
422 r
.req
[2] = (s1
->initaddr
>> 8) & 0xff;
423 r
.req
[3] = (s1
->initaddr
) & 0xff;
424 r
.req
[4] = (s1
->saveaddr
>> 8) & 0xff;
425 r
.req
[5] = (s1
->saveaddr
) & 0xff;
426 r
.req
[6] = (s1
->sphandler
>> 8) & 0xff;
427 r
.req
[7] = (s1
->sphandler
) & 0xff;
428 r
.error
= IMG_SET_TASK_PTR
;
431 r
.req
.set (zero
, 10);
434 r
.req
[2] = (s1
->obj_ptr
>> 8) & 0xff;
435 r
.req
[3] = (s1
->obj_ptr
) & 0xff;
436 r
.req
[4] = (s1
->obj_count
) & 0xff;
437 r
.error
= IMG_SET_OBJ
;
440 r
.req
.set (zero
, 10);
443 r
.req
[2] = (s1
->appcallback
>> 8) & 0xff;
444 r
.req
[3] = (s1
->appcallback
) & 0xff;
445 r
.req
[4] = (s1
->groupobj_ptr
>> 8) & 0xff;
446 r
.req
[5] = (s1
->groupobj_ptr
) & 0xff;
447 r
.req
[6] = (s1
->seg0
>> 8) & 0xff;
448 r
.req
[7] = (s1
->seg0
) & 0xff;
449 r
.req
[8] = (s1
->seg1
>> 8) & 0xff;
450 r
.req
[9] = (s1
->seg1
) & 0xff;
451 r
.error
= IMG_SET_TASK2
;
454 r
.req
.set (zero
, 10);
457 r
.error
= IMG_FINISH_PROC
;
460 return IMG_IMAGE_LOADABLE
;
465 return IMG_UNKNOWN_BCUTYPE
;
471 decodeBCULoadResult (BCU_LOAD_RESULT r
)
475 case IMG_UNKNOWN_ERROR
:
476 return _("unknown error");
478 case IMG_UNRECOG_FORMAT
:
479 return _("data not regcognized as image");
481 case IMG_INVALID_FORMAT
:
482 return _("invalid streams in the image");
485 return _("no bcu type specified");
487 case IMG_UNKNOWN_BCUTYPE
:
488 return _("don't know how to load the bcutype");
491 return _("no text segment found");
494 return _("size information not found");
496 case IMG_LODATA_OVERFLOW
:
497 return _("too many data for low-ram");
499 case IMG_HIDATA_OVERFLOW
:
500 return _("too many data for hi-ram");
502 case IMG_TEXT_OVERFLOW
:
503 return _("too many data for eeprom");
505 case IMG_IMAGE_LOADABLE
:
506 return _("Image is loadable");
509 return _("no address found in the image");
512 return _("unexpected size of the text segment");
514 case IMG_NO_DEVICE_CONNECTION
:
515 return _("connection to the device failed");
517 case IMG_MASK_READ_FAILED
:
518 return _("read of mask version failed");
520 case IMG_WRONG_MASK_VERSION
:
521 return _("incompatible mask version");
523 case IMG_CLEAR_ERROR
:
524 return _("reseting of RunFlags failed");
526 case IMG_RESET_ADDR_TAB
:
527 return _("reseting of the address table failed");
529 case IMG_LOAD_HEADER
:
530 return _("loading of the header failed");
533 return _("loading of the code in the eeprom failed");
536 return _("cleaning the ram failed");
538 case IMG_FINALIZE_ADDR_TAB
:
539 return _("finalizing the address table failed");
541 case IMG_PREPARE_RUN
:
542 return _("setting the RunFlags failed");
545 return _("restart failed");
548 return _("image successful loaded");
551 return _("no BCU2 load control information present");
553 case IMG_WRONG_ADDRTAB
:
554 return _("wrong start address of the address table");
556 case IMG_ADDRTAB_OVERFLOW
:
557 return _("address table too big");
559 case IMG_OVERLAP_ASSOCTAB
:
560 return _("association table overlaps with an other segment");
562 case IMG_OVERLAP_TEXT
:
563 return _("text segement overlaps with an other segment");
565 case IMG_NEGATIV_TEXT_SIZE
:
566 return _("segment end < text segment");
568 case IMG_OVERLAP_PARAM
:
569 return _("param segment overlaps with an other segment");
571 case IMG_OVERLAP_EEPROM
:
572 return _("eeprom segment overlaps with an other segment");
574 case IMG_OBJTAB_OVERFLOW
:
575 return _("too many objects");
577 case IMG_WRONG_LOADCTL
:
578 return _("param end not in the text segment");
580 case IMG_UNLOAD_ADDR
:
581 return _("error unloading address table");
583 case IMG_UNLOAD_ASSOC
:
584 return _("error unloading assocation table");
586 case IMG_UNLOAD_PROG
:
587 return _("error unloading user programm");
590 return _("error start loading address table");
593 return _("error allocation address table");
596 return _("error setting address table start");
598 case IMG_FINISH_ADDR
:
599 return _("error finishing address table");
602 return _("error start loading association table");
604 case IMG_WRITE_ASSOC
:
605 return _("error allocation assocation table");
608 return _("error setting assocation table start");
610 case IMG_FINISH_ASSOC
:
611 return _("error finishing assocation table");
614 return _("error start loading programm");
616 case IMG_ALLOC_LORAM
:
617 return _("error allocation low ram");
619 case IMG_ALLOC_HIRAM
:
620 return _("error allocation high ram");
623 return _("error allocation config section");
626 return _("error loading text segment");
628 case IMG_ALLOC_EEPROM
:
629 return _("error loading eeprom segment");
631 case IMG_ALLOC_PARAM
:
632 return _("error loading parameter segement");
635 return _("error setting programm entry");
637 case IMG_SET_TASK_PTR
:
638 return _("error setting task pointer");
641 return _("error setting object pointer");
644 return _("error setting group object pointer");
646 case IMG_FINISH_PROC
:
647 return _("error finishing application programm");
649 case IMG_WRONG_CHECKLIM
:
650 return _("wrong check limit");
652 case IMG_AUTHORIZATION_FAILED
:
653 return _("authorization failed");
655 case IMG_INVALID_KEY
:
656 return _("invalid key information");
659 return _("key write failed");
663 return _("errorcode not defined");