postbuild.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/sh
  2. #
  3. # postbuild.sh - multi-language support script
  4. # Generate binary with secondary language.
  5. #
  6. # Input files:
  7. # $OUTDIR/Firmware.ino.elf
  8. # $OUTDIR/sketch/*.o (all object files)
  9. #
  10. # Output files:
  11. # text.sym
  12. # $PROGMEM.sym (progmem1.sym)
  13. # $PROGMEM.lss (...)
  14. # $PROGMEM.hex
  15. # $PROGMEM.chr
  16. # $PROGMEM.var
  17. # $PROGMEM.txt
  18. # textaddr.txt
  19. #
  20. #
  21. # Config:
  22. if [ -z "$CONFIG_OK" ]; then eval "$(cat config.sh)"; fi
  23. if [ -z "$CONFIG_OK" ] | [ $CONFIG_OK -eq 0 ]; then echo 'Config NG!' >&2; exit 1; fi
  24. #
  25. # Selected language:
  26. LNG=$1
  27. #if [ -z "$LNG" ]; then LNG='cz'; fi
  28. #
  29. # Params:
  30. IGNORE_MISSING_TEXT=1
  31. finish()
  32. {
  33. echo
  34. if [ "$1" = "0" ]; then
  35. echo "postbuild.sh finished with success" >&2
  36. else
  37. echo "postbuild.sh finished with errors!" >&2
  38. fi
  39. case "$-" in
  40. *i*) echo "press enter key"; read ;;
  41. esac
  42. exit $1
  43. }
  44. echo "postbuild.sh started" >&2
  45. #check input files
  46. echo " checking files:" >&2
  47. if [ ! -e $OUTDIR ]; then echo " folder '$OUTDIR' not found!" >&2; finish 1; fi
  48. echo " folder OK" >&2
  49. if [ ! -e $INOELF ]; then echo " elf file '$INOELF' not found!" >&2; finish 1; fi
  50. echo " elf OK" >&2
  51. if ! ls $OBJDIR/*.o >/dev/null 2>&1; then echo " no object files in '$OBJDIR/'!" >&2; finish 1; fi
  52. echo " objects OK" >&2
  53. #run progmem.sh - examine content of progmem1
  54. echo -n " running progmem.sh..." >&2
  55. ./progmem.sh 1 2>progmem.out
  56. if [ $? -ne 0 ]; then echo "NG! - check progmem.out file" >&2; finish 1; fi
  57. echo "OK" >&2
  58. #run textaddr.sh - map progmem addreses to text identifiers
  59. echo -n " running textaddr.sh..." >&2
  60. ./textaddr.sh 2>textaddr.out
  61. if [ $? -ne 0 ]; then echo "NG! - check progmem.out file" >&2; finish 1; fi
  62. echo "OK" >&2
  63. #check for messages declared in progmem1, but not found in lang_en.txt
  64. echo -n " checking textaddr.txt..." >&2
  65. if cat textaddr.txt | grep "^ADDR NF" >/dev/null; then
  66. echo "NG! - some texts not found in lang_en.txt!"
  67. if [ $IGNORE_MISSING_TEXT -eq 0 ]; then
  68. finish 1
  69. else
  70. echo " missing text ignored!" >&2
  71. fi
  72. else
  73. echo "OK" >&2
  74. fi
  75. #extract binary file
  76. echo -n " extracting binary..." >&2
  77. $OBJCOPY -I ihex -O binary $INOHEX ./firmware.bin
  78. echo "OK" >&2
  79. #update binary file
  80. echo " updating binary:" >&2
  81. #update progmem1 id entries in binary file
  82. echo -n " primary language ids..." >&2
  83. cat textaddr.txt | grep "^ADDR OK" | cut -f3- -d' ' | sed "s/^0000/0x/" |\
  84. awk '{ id = $2 - 1; hi = int(id / 256); lo = int(id - 256 * hi); printf("%d \\\\x%02x\\\\x%02x\n", strtonum($1), lo, hi); }' |\
  85. while read addr data; do
  86. /bin/echo -n -e $data | dd of=./firmware.bin bs=1 count=2 seek=$addr conv=notrunc oflag=nonblock 2>/dev/null
  87. done
  88. echo "OK" >&2
  89. #update primary language signature in binary file
  90. echo -n " primary language signature..." >&2
  91. if [ -e lang_en.bin ]; then
  92. #find symbol _PRI_LANG_SIGNATURE in section '.text'
  93. pri_lang=$(cat text.sym | grep -E "\b_PRI_LANG_SIGNATURE\b")
  94. if [ -z "$pri_lang" ]; then echo "NG!\n symbol _PRI_LANG_SIGNATURE not found!" >&2; finish 1; fi
  95. #get pri_lang address
  96. pri_lang_addr='0x'$(echo $pri_lang | cut -f1 -d' ')
  97. #read header from primary language binary file
  98. 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')
  99. #read checksum and count data as 4 byte signature
  100. chscnt=$(echo $header | cut -c18-29 | sed "s/ /\\\\x/g")
  101. /bin/echo -e -n "$chscnt" |\
  102. dd of=firmware.bin bs=1 count=4 seek=$(($pri_lang_addr)) conv=notrunc 2>/dev/null
  103. echo "OK" >&2
  104. else
  105. echo "NG! - file lang_en.bin not found!" >&2;
  106. finish 1
  107. fi
  108. #convert bin to hex
  109. echo -n " converting to hex..." >&2
  110. $OBJCOPY -I binary -O ihex ./firmware.bin ./firmware.hex
  111. echo "OK" >&2
  112. #update _SEC_LANG in binary file if language is selected
  113. echo -n " secondary language data..." >&2
  114. if [ ! -z "$LNG" ]; then
  115. ./update_lang.sh $LNG 2>./update_lang.out
  116. if [ $? -ne 0 ]; then echo "NG! - check update_lang.out file" >&2; finish 1; fi
  117. echo "OK" >&2
  118. finish 0
  119. else
  120. echo "Updating languages:" >&2
  121. if [ -e lang_cz.bin ]; then
  122. echo -n " Czech : " >&2
  123. ./update_lang.sh cz 2>./update_lang_cz.out 1>/dev/null
  124. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi
  125. fi
  126. if [ -e lang_de.bin ]; then
  127. echo -n " German : " >&2
  128. ./update_lang.sh de 2>./update_lang_de.out 1>/dev/null
  129. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi
  130. fi
  131. if [ -e lang_it.bin ]; then
  132. echo -n " Italian: " >&2
  133. ./update_lang.sh it 2>./update_lang_it.out 1>/dev/null
  134. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi
  135. fi
  136. if [ -e lang_es.bin ]; then
  137. echo -n " Spanish: " >&2
  138. ./update_lang.sh es 2>./update_lang_es.out 1>/dev/null
  139. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi
  140. fi
  141. if [ -e lang_pl.bin ]; then
  142. echo -n " Polish : " >&2
  143. ./update_lang.sh pl 2>./update_lang_pl.out 1>/dev/null
  144. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi
  145. fi
  146. # echo "skipped" >&2
  147. fi
  148. #create binary file with all languages
  149. rm -f lang.bin
  150. if [ -e lang_cz.bin ]; then cat lang_cz.bin >> lang.bin; fi
  151. if [ -e lang_de.bin ]; then cat lang_de.bin >> lang.bin; fi
  152. if [ -e lang_es.bin ]; then cat lang_es.bin >> lang.bin; fi
  153. if [ -e lang_it.bin ]; then cat lang_it.bin >> lang.bin; fi
  154. if [ -e lang_pl.bin ]; then cat lang_pl.bin >> lang.bin; fi
  155. #convert lang.bin to lang.hex
  156. echo -n " converting to hex..." >&2
  157. $OBJCOPY -I binary -O ihex ./lang.bin ./lang.hex
  158. echo "OK" >&2
  159. #append languages to hex file
  160. cat ./lang.hex >> firmware.hex
  161. finish 0