progmem.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/bash
  2. #
  3. # progmem.sh - multi-language support script
  4. # Examine content of progmem sections (default is progmem1).
  5. #
  6. # Input files:
  7. # $OUTDIR/Firmware.ino.elf
  8. # $OUTDIR/sketch/*.o (all object files)
  9. #
  10. # Output files:
  11. # text.sym - formated symbol listing of section '.text'
  12. # $PROGMEM.sym - formated symbol listing of section '.progmemX'
  13. # $PROGMEM.lss - disassembly listing file
  14. # $PROGMEM.hex - variables - hex
  15. # $PROGMEM.chr - variables - char escape
  16. # $PROGMEM.var - variables - strings
  17. # $PROGMEM.txt - text data only (not used)
  18. #
  19. #
  20. # Config:
  21. if [ -z "$CONFIG_OK" ]; then eval "$(cat config.sh)"; fi
  22. if [ -z "$OUTDIR" ]; then echo 'variable OUTDIR not set!' >&2; exit 1; fi
  23. if [ -z "$OBJDIR" ]; then echo 'variable OBJDIR not set!' >&2; exit 1; fi
  24. if [ -z "$INOELF" ]; then echo 'variable INOELF not set!' >&2; exit 1; fi
  25. if [ -z "$OBJDUMP" ]; then echo 'variable OBJDUMP not set!' >&2; exit 1; fi
  26. if [ -z "$CONFIG_OK" ] | [ $CONFIG_OK -eq 0 ]; then echo 'Config NG!' >&2; exit 1; fi
  27. #
  28. # Program memory used
  29. PROGMEM=progmem$1
  30. if [ -z "$1" ]; then PROGMEM=progmem1; fi
  31. #
  32. # Description of process:
  33. # 0. check input files
  34. # 1. remove output files
  35. # 2. list symbol table of section '.text' from output elf file to text.sym (sorted by address)
  36. # 3. calculate start and stop address of section '.$PROGMEM'
  37. # 4. extract string data from elf file to $PROGMEM.hex
  38. # 5. prepare string data for character check and conversion (output to $PROGMEM.chr)
  39. # 6. perform character check and conversion (output to $PROGMEM.var and $PROGMEM.txt)
  40. #
  41. echo "progmem.sh started" >&2
  42. # (0)
  43. echo " progmem.sh (0) - checking input files" >&2
  44. if [ ! -e $OUTDIR ]; then echo "progmem.sh - file '$INOELF' not found!" >&2; exit 1; fi
  45. # (1)
  46. echo " progmem.sh (1) - removing output files" >&2
  47. #remove output files if exists
  48. if [ -e text.sym ]; then rm text.sym; fi
  49. if [ -e $PROGMEM.sym ]; then rm $PROGMEM.sym; fi
  50. if [ -e $PROGMEM.lss ]; then rm $PROGMEM.lss; fi
  51. if [ -e $PROGMEM.hex ]; then rm $PROGMEM.hex; fi
  52. if [ -e $PROGMEM.chr ]; then rm $PROGMEM.chr; fi
  53. if [ -e $PROGMEM.var ]; then rm $PROGMEM.var; fi
  54. if [ -e $PROGMEM.txt ]; then rm $PROGMEM.txt; fi
  55. # (2)
  56. echo " progmem.sh (2) - listing symbol table of section '.text'" >&2
  57. #list symbols from section '.text' into file text.sym (only address, size and name)
  58. #$OBJDUMP -t -j ".text" $INOELF | sort > text.sym
  59. $OBJDUMP -t -j ".text" $INOELF | tail -n +5 | grep -E "^[0-9a-f]{8} [gl] [O ]" | cut -c1-9,28-36,37- | sed "/^$/d" | sort > text.sym
  60. # (3)
  61. echo " progmem.sh (3) - calculating start and end address" >&2
  62. #calculate start addres of section ".$PROGMEM"
  63. PROGMEM_BEG=$(cat text.sym | grep "__loc_pri_start" | while read offs size name; do echo "0x"$offs; done)
  64. #calculate stop addres of section ".$PROGMEM"
  65. PROGMEM_END=$(cat text.sym | grep "__loc_pri_end" | while read offs size name; do echo "0x"$offs; done)
  66. echo " START address = "$PROGMEM_BEG >&2
  67. echo " STOP address = "$PROGMEM_END >&2
  68. # (4)
  69. echo " progmem.sh (4) - extracting string data from elf" >&2
  70. #dump $PROGMEM data in hex format, cut disassembly (keep hex data only)
  71. $OBJDUMP -D -j ".text" -w -z --start-address=$PROGMEM_BEG --stop-address=$PROGMEM_END $INOELF |\
  72. tail -n +7 | sed "s/ \t.*$//" > $PROGMEM.lss
  73. #convert $PROGMEM.lss to $PROGMEM.hex:
  74. # replace empty lines with '|' (variables separated by empty lines)
  75. # remove address from multiline variables (keep address at first variable line only)
  76. # remove '<' and '>:', remove whitespace at end of lines
  77. # remove line-endings, replace separator with '\n' (join hex data lines - each line will contain single variable)
  78. cat $PROGMEM.lss | sed -E 's/^$/|/;s/^ ....:\t//;s/[ ]*$/ /' | tr -d '\n' | tr '|' '\n' |\
  79. sed "s/^ //;s/<//;s/>:/ /;s/00 [1-9a-f][1-9a-f] $/00 /; s/ $//" > $PROGMEM.hex
  80. # (5)
  81. echo " progmem.sh (5) - preparing string data" >&2
  82. #convert $PROGMEM.hex to $PROGMEM.chr (prepare string data for character check and conversion)
  83. # replace first space with tab
  84. # replace second and third space with tab and space
  85. # replace all remaining spaces with '\x'
  86. # replace all tabs with spaces
  87. cat $PROGMEM.hex | sed 's/ /\t/;s/ /\t /;s/ /\\x/g;s/\t/ /g' > $PROGMEM.chr
  88. # (6)
  89. #convert $PROGMEM.chr to $PROGMEM.var (convert data to text)
  90. echo " progmem.sh (6) - converting string data" >&2
  91. (\
  92. echo "/bin\/echo -e \\"; \
  93. cat $PROGMEM.chr | \
  94. sed 's/ \\xff\\xff/ /;' | \
  95. sed 's/\\x22/\\\\\\x22/g;' | \
  96. sed 's/\\x1b/\\\\\\x1b/g;' | \
  97. sed 's/\\x01/\\\\\\x01/g;' | \
  98. sed 's/\\xf8/\\\\\\xf8/g;' | \
  99. sed 's/\\x0a/\\\\\\x0a/g;' | \
  100. sed 's/\\x00$/\n/;s/^/\"/;s/$/\"\\/'; \
  101. ) | sh > $PROGMEM.var
  102. #this step can be omitted because .txt file is not used
  103. cat $PROGMEM.var | sed 's/\r/\n/g' | sed -E 's/^[0-9a-f]{8} [^ ]* //' >$PROGMEM.txt
  104. echo "progmem.sh finished" >&2
  105. exit 0