fw-build.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/bin/bash
  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. cat textaddr.txt | grep "^TEXT NF" | sed "s/[^\"]*\"//;s/\"$//" >not_used.txt
  66. cat textaddr.txt | grep "^ADDR NF" | sed "s/[^\"]*\"//;s/\"$//" >not_tran.txt
  67. if cat textaddr.txt | grep "^ADDR NF" >/dev/null; then
  68. echo "NG! - some texts not found in lang_en.txt!"
  69. if [ $IGNORE_MISSING_TEXT -eq 0 ]; then
  70. finish 1
  71. else
  72. echo " missing text ignored!" >&2
  73. fi
  74. else
  75. echo "OK" >&2
  76. fi
  77. #extract binary file
  78. echo -n " extracting binary..." >&2
  79. $OBJCOPY -I ihex -O binary $INOHEX ./firmware.bin
  80. echo "OK" >&2
  81. #update binary file
  82. echo " updating binary:" >&2
  83. #update progmem1 id entries in binary file
  84. echo -n " primary language ids..." >&2
  85. cat textaddr.txt | grep "^ADDR OK" | cut -f3- -d' ' | sed "s/^0000/0x/" |\
  86. awk '{ id = $2 - 1; hi = int(id / 256); lo = int(id - 256 * hi); printf("%d \\\\x%02x\\\\x%02x\n", strtonum($1), lo, hi); }' |\
  87. while read addr data; do
  88. /bin/echo -n -e $data | dd of=./firmware.bin bs=1 count=2 seek=$addr conv=notrunc oflag=nonblock 2>/dev/null
  89. done
  90. echo "OK" >&2
  91. #update primary language signature in binary file
  92. echo -n " primary language signature..." >&2
  93. if [ -e lang_en.bin ]; then
  94. #find symbol _PRI_LANG_SIGNATURE in section '.text'
  95. pri_lang=$(cat text.sym | grep -E "\b_PRI_LANG_SIGNATURE\b")
  96. if [ -z "$pri_lang" ]; then echo "NG!\n symbol _PRI_LANG_SIGNATURE not found!" >&2; finish 1; fi
  97. #get pri_lang address
  98. pri_lang_addr='0x'$(echo $pri_lang | cut -f1 -d' ')
  99. #read header from primary language binary file
  100. 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')
  101. #read checksum and count data as 4 byte signature
  102. chscnt=$(echo $header | cut -c18-29 | sed "s/ /\\\\x/g")
  103. /bin/echo -e -n "$chscnt" |\
  104. dd of=firmware.bin bs=1 count=4 seek=$(($pri_lang_addr)) conv=notrunc 2>/dev/null
  105. echo "OK" >&2
  106. else
  107. echo "NG! - file lang_en.bin not found!" >&2;
  108. finish 1
  109. fi
  110. #convert bin to hex
  111. echo -n " converting to hex..." >&2
  112. $OBJCOPY -I binary -O ihex ./firmware.bin ./firmware.hex
  113. echo "OK" >&2
  114. #update _SEC_LANG in binary file if language is selected
  115. echo -n " secondary language data..." >&2
  116. if [ ! -z "$LNG" ]; then
  117. ./update_lang.sh $LNG 2>./update_lang.out
  118. if [ $? -ne 0 ]; then echo "NG! - check update_lang.out file" >&2; finish 1; fi
  119. echo "OK" >&2
  120. finish 0
  121. else
  122. echo "Updating languages:" >&2
  123. if [ -e lang_cz.bin ]; then
  124. echo -n " Czech : " >&2
  125. ./update_lang.sh cz 2>./update_lang_cz.out 1>/dev/null
  126. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; finish 1; fi
  127. fi
  128. if [ -e lang_de.bin ]; then
  129. echo -n " German : " >&2
  130. ./update_lang.sh de 2>./update_lang_de.out 1>/dev/null
  131. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; finish 1; fi
  132. fi
  133. if [ -e lang_it.bin ]; then
  134. echo -n " Italian: " >&2
  135. ./update_lang.sh it 2>./update_lang_it.out 1>/dev/null
  136. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; finish 1; fi
  137. fi
  138. if [ -e lang_es.bin ]; then
  139. echo -n " Spanish: " >&2
  140. ./update_lang.sh es 2>./update_lang_es.out 1>/dev/null
  141. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; finish 1; fi
  142. fi
  143. if [ -e lang_fr.bin ]; then
  144. echo -n " French : " >&2
  145. ./update_lang.sh fr 2>./update_lang_fr.out 1>/dev/null
  146. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; finish 1; fi
  147. fi
  148. if [ -e lang_nl.bin ]; then
  149. echo -n " Dutch : " >&2
  150. ./update_lang.sh nl 2>./update_lang_nl.out 1>/dev/null
  151. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; fi
  152. fi
  153. if [ -e lang_pl.bin ]; then
  154. echo -n " Polish : " >&2
  155. ./update_lang.sh pl 2>./update_lang_pl.out 1>/dev/null
  156. if [ $? -eq 0 ]; then echo 'OK' >&2; else echo 'NG!' >&2; finish 1; fi
  157. fi
  158. # echo "skipped" >&2
  159. fi
  160. #create binary file with all languages
  161. rm -f lang.bin
  162. if [ -e lang_cz.bin ]; then cat lang_cz.bin >> lang.bin; fi
  163. if [ -e lang_de.bin ]; then cat lang_de.bin >> lang.bin; fi
  164. if [ -e lang_es.bin ]; then cat lang_es.bin >> lang.bin; fi
  165. if [ -e lang_fr.bin ]; then cat lang_fr.bin >> lang.bin; fi
  166. if [ -e lang_nl.bin ]; then cat lang_nl.bin >> lang.bin; fi
  167. if [ -e lang_it.bin ]; then cat lang_it.bin >> lang.bin; fi
  168. if [ -e lang_pl.bin ]; then cat lang_pl.bin >> lang.bin; fi
  169. #convert lang.bin to lang.hex
  170. echo -n " converting to hex..." >&2
  171. $OBJCOPY -I binary -O ihex ./lang.bin ./lang.hex
  172. echo "OK" >&2
  173. #append languages to hex file
  174. cat ./lang.hex >> firmware.hex
  175. finish 0