fw-build.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. # Extract and patch the symbol table/language map
  34. color 4 "generating firmware symbol map" >&2
  35. "$OBJCOPY" -I ihex -O binary "$INOHEX" "$BIN"
  36. ./lang-map.py "$INOELF" "$BIN" > "$MAP"
  37. # Get the maximum size of a single language table
  38. maxsize=$(grep '^#define \+LANG_SIZE_RESERVED \+' "$SRCDIR/Firmware/config.h" | sed -e 's/\s\+/ /g' | cut -d ' ' -f3)
  39. # Build language catalogs
  40. for lang in $LANGUAGES; do
  41. pofile="po/Firmware_$lang.po"
  42. binfile="$TMPDIR/lang_$lang.bin"
  43. color 4 "compiling language \"$lang\" from $pofile" >&2
  44. ./lang-check.py --map "$MAP" "$pofile"
  45. if [ "$?" != 0 ]; then
  46. color 1 "$pofile: NG! - translation contains warnings or errors" >&2
  47. fi
  48. ./lang-build.py "$MAP" "$pofile" "$binfile"
  49. # ensure each catalog fits the reserved size
  50. if [[ $(stat -c '%s' "$binfile") -gt $maxsize ]]; then
  51. color 1 "$pofile: NG! - language data exceeds $maxsize bytes" >&2
  52. finish 1
  53. fi
  54. done
  55. # Detect the printer type and choose the language type
  56. if grep -q '^#define \+PRINTER_TYPE \+PRINTER_\(MK25\|MK25S\)\b' "$SRCDIR/Firmware/Configuration_prusa.h"; then
  57. has_xflash=0
  58. else
  59. has_xflash=1
  60. fi
  61. if [ "$has_xflash" = 1 ]; then
  62. # Build the final hex file with XFLASH support (catalogs appended to a single hex file)
  63. OUTHEX="${INTLHEX}.hex"
  64. color 4 "assembling final firmware image" >&2
  65. "$OBJCOPY" -I binary -O ihex "$BIN" "$OUTHEX"
  66. truncate -s0 "$TMPDIR/lang.bin"
  67. for lang in $LANGUAGES; do
  68. cat "$TMPDIR/lang_$lang.bin" >> "$TMPDIR/lang.bin"
  69. done
  70. "$OBJCOPY" -I binary -O ihex "$TMPDIR/lang.bin" "$TMPDIR/lang.hex"
  71. cat "$TMPDIR/lang.hex" >> "$OUTHEX"
  72. # Check that the language data doesn't exceed the reserved XFLASH space
  73. lang_size=$(stat -c '%s' "$TMPDIR/lang.bin")
  74. lang_size_pad=$(( ($lang_size+4096-1) / 4096 * 4096 ))
  75. # TODO: hard-coded! get value by preprocessing LANG_SIZE from xflash_layout.h!
  76. lang_reserved=249856
  77. echo >&2
  78. echo -n " total size usage: " >&2
  79. [ $lang_size_pad -gt $lang_reserved ] && c=1 || c=2
  80. color $c "$lang_size_pad ($lang_size)" >&2
  81. echo -n " reserved size: " >&2
  82. color 2 "$lang_reserved" >&2
  83. if [ $lang_size_pad -gt $lang_reserved ]; then
  84. color 1 "NG! - language data too large" >&2
  85. finish 1
  86. fi
  87. echo -n " multilanguage fw: " >&2
  88. color 2 "$OUTHEX" >&2
  89. else
  90. # Build one hex file for each secondary language
  91. color 4 "assembling final firmware images" >&2
  92. echo >&2
  93. echo -n " maximum size: " >&2
  94. color 2 "$(( $maxsize ))" >&2
  95. for lang in $LANGUAGES; do
  96. OUTHEX="${INTLHEX}-en_${lang}.hex"
  97. catfile="$TMPDIR/lang_$lang.bin"
  98. bintmp="$TMPDIR/fw-en_$lang.bin"
  99. # patch the secondary language table
  100. cp "$BIN" "$bintmp"
  101. ./lang-patchsec.py "$INOELF" "$catfile" "$bintmp"
  102. "$OBJCOPY" -I binary -O ihex "$bintmp" "$OUTHEX"
  103. # print some stats
  104. catsize=$(stat -c '%s' "$catfile")
  105. echo -n " $lang: " >&2
  106. color 2 "$(printf "%5d %s" "$catsize" "$OUTHEX")" >&2
  107. done
  108. fi
  109. finish 0