fw-build.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/bash
  2. # TODO: write some up-to-date description
  3. # Config:
  4. if [ -z "$CONFIG_OK" ]; then source config.sh; fi
  5. if [ -z "$CONFIG_OK" -o "$CONFIG_OK" -eq 0 ]; then echo "$(tput setaf 1)Config NG!$(tput sgr0)" >&2; exit 1; fi
  6. # Community languages
  7. if [ ! -z "$COMMUNITY_LANGUAGES" ]; then
  8. LANGUAGES+=" $COMMUNITY_LANGUAGES"
  9. fi
  10. color 2 "fw-build.sh started" >&2
  11. echo -n "fw-build languages: " >&2
  12. color 2 "$LANGUAGES" >&2
  13. finish()
  14. {
  15. echo >&2
  16. if [ "$1" = "0" ]; then
  17. color 2 "fw-build.sh finished with success" >&2
  18. else
  19. color 1 "fw-build.sh finished with errors!" >&2
  20. fi
  21. case "$-" in
  22. *i*)
  23. echo "press enter key"; read ;;
  24. esac
  25. exit $1
  26. }
  27. # Clean the temporary directory
  28. TMPDIR=$(dirname "$0")/tmp
  29. rm -rf "$TMPDIR"
  30. mkdir -p "$TMPDIR"
  31. BIN=$TMPDIR/firmware.bin
  32. MAP=$TMPDIR/firmware.map
  33. VARIANT=$(grep '^#define \+PRINTER_TYPE ' "$SRCDIR/Firmware/Configuration_prusa.h"|cut -d '_' -f3)
  34. # Extract and patch the symbol table/language map
  35. color 4 "generating firmware symbol map" >&2
  36. "$OBJCOPY" -I ihex -O binary "$INOHEX" "$BIN"
  37. ./lang-map.py "$INOELF" "$BIN" > "$MAP"
  38. cp -f $MAP firmware_$VARIANT.map
  39. # Get the maximum size of a single language table
  40. maxsize=$(grep '^#define \+LANG_SIZE_RESERVED \+' "$SRCDIR/Firmware/config.h" | sed -e 's/\s\+/ /g' | cut -d ' ' -f3)
  41. # Build language catalogs
  42. for lang in $LANGUAGES; do
  43. pofile="po/Firmware_$lang.po"
  44. binfile="$TMPDIR/lang_$lang.bin"
  45. color 4 "compiling language \"$lang\" from $pofile" >&2
  46. ./lang-check.py --map "$MAP" "$pofile" --no-suggest
  47. if [ "$?" != 0 ]; then
  48. color 1 "$pofile: NG! - translation contains warnings or errors" >&2
  49. fi
  50. ./lang-build.py "$MAP" "$pofile" "$binfile"
  51. # ensure each catalog fits the reserved size
  52. currentsize=$(stat -c '%s' "$binfile")
  53. if [[ $currentsize -gt $maxsize ]]; then
  54. color 1 "$pofile: NG! - language data exceeds $maxsize bytes, it uses $currentsize" >&2
  55. finish 1
  56. fi
  57. done
  58. # Detect the printer type and choose the language type
  59. if grep -q '^#define \+PRINTER_TYPE \+PRINTER_\(MK25\|MK25S\)\b' "$SRCDIR/Firmware/Configuration_prusa.h"; then
  60. has_xflash=0
  61. else
  62. has_xflash=1
  63. fi
  64. if [ "$has_xflash" = 1 ]; then
  65. # Build the final hex file with XFLASH support (catalogs appended to a single hex file)
  66. OUTHEX="${INTLHEX}.hex"
  67. color 4 "assembling final firmware image" >&2
  68. "$OBJCOPY" -I binary -O ihex "$BIN" "$OUTHEX"
  69. truncate -s0 "$TMPDIR/lang.bin"
  70. individual_lang_reserved_hex=$(grep --max-count=1 "^#define LANG_SIZE_RESERVED *" $SRCDIR/Firmware/config.h|sed -e's/ */ /g'|cut -d ' ' -f3|cut -d 'x' -f2)
  71. individual_lang_reserved=$((16#$individual_lang_reserved_hex))
  72. for lang in $LANGUAGES; do
  73. cat "$TMPDIR/lang_$lang.bin" >> "$TMPDIR/lang.bin"
  74. lang_size=$(stat -c '%s' "$TMPDIR/lang_$lang.bin")
  75. lang_size_pad=$(( ($lang_size+256-1) / 256 * 256 ))
  76. lang_free=$(($individual_lang_reserved - $lang_size))
  77. echo >&2
  78. echo -n " size usage" >&2
  79. [ $lang_size -gt $individual_lang_reserved ] && c=1 || c=2
  80. color $c " $lang $lang_size_pad ($lang_size)" >&2
  81. echo -n " reserved size " >&2
  82. color 2 "$individual_lang_reserved" >&2
  83. echo -n " free bytes " >&2
  84. color 2 "$lang_free" >&2
  85. if [ $lang_size_pad -gt $individual_lang_reserved ]; then
  86. color 1 "NG! - language $lang data too large" >&2
  87. finish 1
  88. fi
  89. done
  90. "$OBJCOPY" -I binary -O ihex "$TMPDIR/lang.bin" "$TMPDIR/lang.hex"
  91. cat "$TMPDIR/lang.hex" >> "$OUTHEX"
  92. # Check that the language data doesn't exceed the reserved XFLASH space
  93. lang_size=$(stat -c '%s' "$TMPDIR/lang.bin")
  94. lang_size_pad=$(( ($lang_size+4096-1) / 4096 * 4096 ))
  95. # TODO: hard-coded! get value by preprocessing LANG_SIZE from xflash_layout.h!
  96. lang_reserved=249856
  97. echo >&2
  98. echo -n " total size usage: " >&2
  99. [ $lang_size_pad -gt $lang_reserved ] && c=1 || c=2
  100. color $c "$lang_size_pad ($lang_size)" >&2
  101. echo -n " reserved size: " >&2
  102. color 2 "$lang_reserved" >&2
  103. if [ $lang_size_pad -gt $lang_reserved ]; then
  104. color 1 "NG! - language data too large" >&2
  105. finish 1
  106. fi
  107. echo -n " multilanguage fw: " >&2
  108. color 2 "$OUTHEX" >&2
  109. else
  110. # Build one hex file for each secondary language
  111. color 4 "assembling final firmware images" >&2
  112. echo >&2
  113. echo -n " maximum size: " >&2
  114. color 2 "$(( $maxsize ))" >&2
  115. for lang in $LANGUAGES; do
  116. OUTHEX="${INTLHEX}-en_${lang}.hex"
  117. catfile="$TMPDIR/lang_$lang.bin"
  118. bintmp="$TMPDIR/fw-en_$lang.bin"
  119. # patch the secondary language table
  120. cp "$BIN" "$bintmp"
  121. ./lang-patchsec.py "$INOELF" "$catfile" "$bintmp"
  122. "$OBJCOPY" -I binary -O ihex "$bintmp" "$OUTHEX"
  123. # print some stats
  124. catsize=$(stat -c '%s' "$catfile")
  125. echo -n " $lang: " >&2
  126. color 2 "$(printf "%5d %s" "$catsize" "$OUTHEX")" >&2
  127. done
  128. fi
  129. finish 0