fw-build.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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"
  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. if [[ $(stat -c '%s' "$binfile") -gt $maxsize ]]; then
  53. color 1 "$pofile: NG! - language data exceeds $maxsize bytes" >&2
  54. finish 1
  55. fi
  56. done
  57. # Detect the printer type and choose the language type
  58. if grep -q '^#define \+PRINTER_TYPE \+PRINTER_\(MK25\|MK25S\)\b' "$SRCDIR/Firmware/Configuration_prusa.h"; then
  59. has_xflash=0
  60. else
  61. has_xflash=1
  62. fi
  63. if [ "$has_xflash" = 1 ]; then
  64. # Build the final hex file with XFLASH support (catalogs appended to a single hex file)
  65. OUTHEX="${INTLHEX}.hex"
  66. color 4 "assembling final firmware image" >&2
  67. "$OBJCOPY" -I binary -O ihex "$BIN" "$OUTHEX"
  68. truncate -s0 "$TMPDIR/lang.bin"
  69. 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)
  70. individual_lang_reserved=$((16#$individual_lang_reserved_hex))
  71. for lang in $LANGUAGES; do
  72. cat "$TMPDIR/lang_$lang.bin" >> "$TMPDIR/lang.bin"
  73. lang_size=$(stat -c '%s' "$TMPDIR/lang_$lang.bin")
  74. lang_size_pad=$(( ($lang_size+256-1) / 256 * 256 ))
  75. lang_free=$(($individual_lang_reserved - $lang_size))
  76. echo >&2
  77. echo -n " size usage" >&2
  78. [ $lang_size -gt $individual_lang_reserved ] && c=1 || c=2
  79. color $c " $lang $lang_size_pad ($lang_size)" >&2
  80. echo -n " reserved size " >&2
  81. color 2 "$individual_lang_reserved" >&2
  82. echo -n " free bytes " >&2
  83. color 2 "$lang_free" >&2
  84. if [ $lang_size_pad -gt $individual_lang_reserved ]; then
  85. color 1 "NG! - language $lang data too large" >&2
  86. finish 1
  87. fi
  88. done
  89. "$OBJCOPY" -I binary -O ihex "$TMPDIR/lang.bin" "$TMPDIR/lang.hex"
  90. cat "$TMPDIR/lang.hex" >> "$OUTHEX"
  91. # Check that the language data doesn't exceed the reserved XFLASH space
  92. lang_size=$(stat -c '%s' "$TMPDIR/lang.bin")
  93. lang_size_pad=$(( ($lang_size+4096-1) / 4096 * 4096 ))
  94. # TODO: hard-coded! get value by preprocessing LANG_SIZE from xflash_layout.h!
  95. lang_reserved=249856
  96. echo >&2
  97. echo -n " total size usage: " >&2
  98. [ $lang_size_pad -gt $lang_reserved ] && c=1 || c=2
  99. color $c "$lang_size_pad ($lang_size)" >&2
  100. echo -n " reserved size: " >&2
  101. color 2 "$lang_reserved" >&2
  102. if [ $lang_size_pad -gt $lang_reserved ]; then
  103. color 1 "NG! - language data too large" >&2
  104. finish 1
  105. fi
  106. echo -n " multilanguage fw: " >&2
  107. color 2 "$OUTHEX" >&2
  108. else
  109. # Build one hex file for each secondary language
  110. color 4 "assembling final firmware images" >&2
  111. echo >&2
  112. echo -n " maximum size: " >&2
  113. color 2 "$(( $maxsize ))" >&2
  114. for lang in $LANGUAGES; do
  115. OUTHEX="${INTLHEX}-en_${lang}.hex"
  116. catfile="$TMPDIR/lang_$lang.bin"
  117. bintmp="$TMPDIR/fw-en_$lang.bin"
  118. # patch the secondary language table
  119. cp "$BIN" "$bintmp"
  120. ./lang-patchsec.py "$INOELF" "$catfile" "$bintmp"
  121. "$OBJCOPY" -I binary -O ihex "$bintmp" "$OUTHEX"
  122. # print some stats
  123. catsize=$(stat -c '%s' "$catfile")
  124. echo -n " $lang: " >&2
  125. color 2 "$(printf "%5d %s" "$catsize" "$OUTHEX")" >&2
  126. done
  127. fi
  128. finish 0