| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 | #!/bin/sh## postbuild.sh - multi-language support script#  Generate binary with secondary language.## Input files:#  $OUTDIR/Firmware.ino.elf#  $OUTDIR/sketch/*.o (all object files)## Output files:#  text.sym#  $PROGMEM.sym (progmem1.sym)#  $PROGMEM.lss (...)#  $PROGMEM.hex#  $PROGMEM.chr#  $PROGMEM.var#  $PROGMEM.txt#  textaddr.txt### Config:if [ -z "$CONFIG_OK" ]; then eval "$(cat config.sh)"; fiif [ -z "$CONFIG_OK" ] | [ $CONFIG_OK -eq 0 ]; then echo 'Config NG!' >&2; exit 1; fi## Selected language:LNG=$1#if [ -z "$LNG" ]; then LNG='cz'; fi## Params:IGNORE_MISSING_TEXT=1finish(){ echo if [ "$1" = "0" ]; then  echo "postbuild.sh finished with success" >&2 else  echo "postbuild.sh finished with errors!" >&2 fi case "$-" in  *i*) echo "press enter key"; read ;; esac exit $1}echo "postbuild.sh started" >&2#check input filesecho " checking files:" >&2if [ ! -e $OUTDIR ]; then echo "  folder '$OUTDIR' not found!" >&2; finish 1; fiecho "  folder  OK" >&2if [ ! -e $INOELF ]; then echo "  elf file '$INOELF' not found!" >&2; finish 1; fiecho "  elf     OK" >&2if ! ls $OBJDIR/*.o >/dev/null 2>&1; then echo "  no object files in '$OBJDIR/'!" >&2; finish 1; fiecho "  objects OK" >&2#run progmem.sh - examine content of progmem1echo -n " running progmem.sh..." >&2./progmem.sh 1 2>progmem.outif [ $? -ne 0 ]; then echo "NG! - check progmem.out file" >&2; finish 1; fiecho "OK" >&2#run textaddr.sh - map progmem addreses to text identifiersecho -n " running textaddr.sh..." >&2./textaddr.sh 2>textaddr.outif [ $? -ne 0 ]; then echo "NG! - check progmem.out file" >&2; finish 1; fiecho "OK" >&2#check for messages declared in progmem1, but not found in lang_en.txtecho -n " checking textaddr.txt..." >&2if cat textaddr.txt | grep "^ADDR NF" >/dev/null; then echo "NG! - some texts not found in lang_en.txt!" if [ $IGNORE_MISSING_TEXT -eq 0 ]; then  finish 1 else  echo "  missing text ignored!" >&2 fielse echo "OK" >&2fi#extract binary fileecho -n " extracting binary..." >&2$OBJCOPY -I ihex -O binary $INOHEX ./firmware.binecho "OK" >&2#update binary fileecho " updating binary:" >&2#update progmem1 id entries in binary fileecho -n "  primary language ids..." >&2cat textaddr.txt | grep "^ADDR OK" | cut -f3- -d' ' | sed "s/^0000/0x/" |\ awk '{ id = $2 - 1; hi = int(id / 256); lo = int(id - 256 * hi); printf("%d \\\\x%02x\\\\x%02x\n", strtonum($1), lo, hi); }' |\ while read addr data; do  /bin/echo -n -e $data | dd of=./firmware.bin bs=1 count=2 seek=$addr conv=notrunc oflag=nonblock 2>/dev/null doneecho "OK" >&2#update primary language signature in binary fileecho -n "  primary language signature..." >&2if [ -e lang_en.bin ]; then #find symbol _PRI_LANG_SIGNATURE in section '.text' pri_lang=$(cat text.sym | grep -E "\b_PRI_LANG_SIGNATURE\b") if [ -z "$pri_lang" ]; then echo "NG!\n  symbol _PRI_LANG_SIGNATURE not found!" >&2; finish 1; fi #get pri_lang address pri_lang_addr='0x'$(echo $pri_lang | cut -f1 -d' ') #read header from primary language binary file header=$(dd if=lang_en.bin bs=1 count=16 2>/dev/null | xxd | cut -c11-49 | sed 's/\([0-9a-f][0-9a-f]\)[\ ]*/\1 /g') #read checksum and count data as 4 byte signature chscnt=$(echo $header | cut -c18-29 | sed "s/ /\\\\x/g") /bin/echo -e -n "$chscnt" |\  dd of=firmware.bin bs=1 count=4 seek=$(($pri_lang_addr)) conv=notrunc 2>/dev/null echo "OK" >&2else echo "NG! - file lang_en.bin not found!" >&2; finish 1fi#convert bin to hexecho -n " converting to hex..." >&2$OBJCOPY -I binary -O ihex ./firmware.bin ./firmware.hexecho "OK" >&2#update _SEC_LANG in binary file if language is selectedecho -n "  secondary language data..." >&2if [ ! -z "$LNG" ]; then ./update_lang.sh $LNG 2>./update_lang.out if [ $? -ne 0 ]; then echo "NG! - check update_lang.out file" >&2; finish 1; fi echo "OK" >&2 finish 0else echo "Updating languages:" >&2 if [ -e lang_cz.bin ]; then  echo -n " Czech  : " >&2  ./update_lang.sh cz 2>./update_lang_cz.out 1>/dev/null  if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi fi if [ -e lang_de.bin ]; then  echo -n " German : " >&2  ./update_lang.sh de 2>./update_lang_de.out 1>/dev/null  if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi fi if [ -e lang_it.bin ]; then  echo -n " Italian: " >&2  ./update_lang.sh it 2>./update_lang_it.out 1>/dev/null  if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi fi if [ -e lang_es.bin ]; then  echo -n " Spanish: " >&2  ./update_lang.sh es 2>./update_lang_es.out 1>/dev/null  if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi fi if [ -e lang_pl.bin ]; then  echo -n " Polish : " >&2  ./update_lang.sh pl 2>./update_lang_pl.out 1>/dev/null  if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi fi# echo "skipped" >&2fi#create binary file with all languagesrm -f lang.binif [ -e lang_cz.bin ]; then cat lang_cz.bin >> lang.bin; fiif [ -e lang_de.bin ]; then cat lang_de.bin >> lang.bin; fiif [ -e lang_es.bin ]; then cat lang_es.bin >> lang.bin; fiif [ -e lang_it.bin ]; then cat lang_it.bin >> lang.bin; fiif [ -e lang_pl.bin ]; then cat lang_pl.bin >> lang.bin; fi#convert lang.bin to lang.hexecho -n " converting to hex..." >&2$OBJCOPY -I binary -O ihex ./lang.bin ./lang.hexecho "OK" >&2#append languages to hex filecat ./lang.hex >> firmware.hexfinish 0
 |