build.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #!/bin/bash
  2. set -e
  3. ################
  4. ## CONFIG ##
  5. ################
  6. CWD="$(pwd)" # current working directory
  7. flash_arduino="u2" # u2 or ftdi
  8. arduino_device="ttyACM0" # ls /dev/tty* and find your arduino
  9. mega=2560 #2560 or 1280
  10. script_version="2.2.0"
  11. rdate="13/01/2022"
  12. ################
  13. ## SOFTWARE ##
  14. ################
  15. clear
  16. echo ""
  17. echo -n -e "\e[97m\u2554"
  18. for i in {1..34}; do echo -e -n "\u2550"; done
  19. echo -e "\u2557\e[0m"
  20. echo -e "\e[97m\u2551 SPI Bios Flashrom \e[93m"V$script_version"\e[0m\e[97m \u2551\e[0m"
  21. echo -e "\e[97m\u2551 Release date: \e[93m"$rdate"\e[0m\e[97m \u2551\e[0m"
  22. echo -n -e "\e[97m\u255A"
  23. for j in {1..34}; do echo -e -n "\u2550";done
  24. echo -e -n "\u255D\e[0m"
  25. echo
  26. function arduino_ {
  27. echo -e ""
  28. echo -e "\e[97mBuild Arduino SPI_Flash tools:\e[0m\e[24m"
  29. if [ -a /dev/ttyACM0 ]
  30. then
  31. arduino_device="ttyACM0"
  32. fi
  33. if [ -a /dev/ttyUSB0 ]
  34. then
  35. arduino_device="ttyUSB0"
  36. fi
  37. if [ -f .lastbuild ];then rm .lastbuild;fi
  38. cat <<'EOF'>> .lastbuild
  39. Mode=arduino
  40. Device=xxx
  41. EOF
  42. sed -i -e "s/xxx/$arduino_device/g" .lastbuild
  43. echo -e "\nArduino device found at: \e[91m$arduino_device\e[0m"
  44. echo -e "\n\e[93mPerforming Update :\e[0m"
  45. sudo apt-get update
  46. sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude libpci-dev git libusb-1.0-0-dev libftdi-dev libftdi1-dev -y
  47. if [ -d frser-duino ]
  48. then
  49. rm -r -f frser-duino
  50. fi
  51. echo -e "\n\e[93mDownload frser-duino :\e[0m"
  52. git clone --recursive https://github.com/urjaman/frser-duino frser-duino
  53. cd frser-duino
  54. if [ $arduino_device = "ttyUSB0" ]
  55. then
  56. sed -i -e "s/dev\/ttyACM0/dev\/$arduino_device/g" Makefile
  57. fi
  58. echo -e "\n\e[93mBuild frser-arduino :\e[0m"
  59. make $flash_arduino -j$(nproc)
  60. echo -e "\n\e[93mFlash Arduino :\e[0m"
  61. make flash-$flash_arduino
  62. sleep 2
  63. }
  64. function mega_ {
  65. echo -e ""
  66. echo -e "\e[93mBuild ATMEGA$mega SPIFlash tools:\e[0m\e[24m"
  67. if [ -a /dev/ttyACM0 ]
  68. then
  69. arduino_device="ttyACM0"
  70. fi
  71. if [ -a /dev/ttyUSB0 ]
  72. then
  73. arduino_device="ttyUSB0"
  74. fi
  75. if [ -f .lastbuild ];then rm .lastbuild;fi
  76. cat <<'EOF'>> .lastbuild
  77. Mode=mega
  78. Device=xxx
  79. EOF
  80. sed -i -e "s/xxx/$arduino_device/g" .lastbuild
  81. echo -e ""
  82. echo -e "Arduino device found at: \e[91m$arduino_device\e[0m"
  83. echo -e "\n\e[93mPerforming Update :\e[0m"
  84. sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude libpci-dev git libusb-1.0-0-dev libftdi-dev libftdi1-dev -y
  85. if [ -d frser-duino ]
  86. then
  87. rm -r -f frser-duino
  88. fi
  89. echo -e "\n\e[93mDownload frser-mega :\e[0m"
  90. git clone --recursive https://github.com/urjaman/frser-duino frser-duino
  91. cd frser-duino
  92. if [ $arduino_device = "ttyUSB0" ]
  93. then
  94. sed -i -e "s/dev\/dev/ttyACM0/dev\/$arduino_device/g" Makefile
  95. fi
  96. echo -e "\n\e[93mBuild frser-duino :\e[0m"
  97. make mega$mega -j$(nproc)
  98. echo -e "\n\e[93mFlash ATMega$mega :\e[0m"
  99. make flash-mega$mega
  100. sleep 2
  101. }
  102. function raspberry_ {
  103. cat <<'EOF'>> .lastbuild
  104. Mode=raspberry
  105. Device=raspberry
  106. EOF
  107. echo -e ""
  108. echo -e "\n\e[93mBuild Raspberry SPIFlash tools:\e[0m\e[24m"
  109. echo -e "\n\e[93mPerforming Update :\e[0m"
  110. sudo apt-get update
  111. sudo apt-get upgrade -y
  112. sudo apt-get install build-essential pciutils usbutils libpci-dev libusb-dev libusb-1.0-0 libusb-1.0-0-dev libftdi1 libftdi-dev zlib1g-dev subversion git ghex libusb-1.0-0-dev libftdi-dev libftdi1-dev libjaylink-dev -y
  113. sudo modprobe spi_bcm2708
  114. sudo modprobe spi_bcm2835
  115. sudo modprobe spidev
  116. if [ -f /boot/config.txt ]
  117. then
  118. sudo sed -i -e "s/\#dtparam=spi=on/dtparam=spi=on/g" /boot/config.txt
  119. fi
  120. }
  121. function ch341_ {
  122. cat <<'EOF'>> .lastbuild
  123. Mode=ch341
  124. Device=ch341
  125. EOF
  126. echo -e ""
  127. echo -e "\n\e[93mBuild CH341 SPIFlash tools:\e[0m\e[24m"
  128. echo -e "\n\e[93mPerforming Update :\e[0m"
  129. sudo apt-get update
  130. sudo apt-get upgrade -y
  131. sudo apt-get install build-essential pciutils usbutils libpci-dev libusb-dev libusb-1.0-0 libusb-1.0-0-dev libftdi1 libftdi-dev zlib1g-dev subversion git ghex libusb-1.0-0-dev libftdi-dev libftdi1-dev libjaylink-dev -y
  132. cat <<'EOF'>>02-ch341a.rules
  133. # Ch341A
  134. # Ch341A
  135. SUBSYSTEM=="usb", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="5512", MODE:="0666"
  136. EOF
  137. sudo usermod -a -G dialout,plugdev $USER
  138. sudo cp 02-ch341a.rules /etc/udev/rules.d/
  139. rm 02-ch341a.rules
  140. }
  141. function flashrom_ {
  142. if ! [ -x "$(command -v flashrom)" ]
  143. then
  144. cd $CWD
  145. if [ ! -d flashrom ]
  146. then
  147. echo -e "\n\e[93mDownload flashrom :\e[0m"
  148. git clone https://github.com/flashrom/flashrom.git
  149. fi
  150. cd flashrom
  151. sed -i -e "s/WARNERROR ?= yes/WARNERROR ?= no/g" Makefile
  152. echo -e "\n\e[93mBuild and install flashrom:\e[0m"
  153. make -j$(nproc)
  154. sudo make install
  155. fi
  156. }
  157. function clean_ {
  158. echo -e ""
  159. echo -e "\e[97mCleaning:\e[0m\e[24m"
  160. if [ -d flashrom ]; then rm -r -f flashrom; fi
  161. if [ -d frser-duino ]; then rm -r -f frser-duino; fi
  162. sleep 3
  163. if [ -f .lastbuild ]
  164. then
  165. source .lastbuild
  166. if [ $Mode = "mega" ]
  167. then
  168. avrdude -p m$mega -c stk500v2 -P /dev/$Device -F -U flash:w:.clean/Mega_blink.hex
  169. fi
  170. if [ $Mode = "arduino" ]
  171. then
  172. avrdude -p atmega328p -c arduino -P /dev/$Device -b 115200 -D -U flash:w:.clean/Arduino_blink.hex
  173. fi
  174. rm .lastbuild
  175. fi
  176. if [ -f /usr/sbin/flashrom ]
  177. then
  178. sudo rm /usr/sbin/flashrom
  179. fi
  180. echo -e "\n\e[95mCleaning...Done !\e[0m"
  181. echo""
  182. }
  183. function flashrom_raspberry_check_ {
  184. echo -e "\n\e[93mCheck flashrom :\e[0m"
  185. flashrom -p linux_spi:dev=/dev/spidev0.0 || :
  186. }
  187. function flashrom_arduino_check_ {
  188. echo -e "\n\e[93mCheck flashrom :\e[0m"
  189. sleep 1
  190. flashrom -p serprog:dev=/dev/$arduino_device:115200 || :
  191. }
  192. function flashrom_mega_check_ {
  193. echo -e "\n\e[93mCheck flashrom :\e[0m"
  194. flashrom -p serprog:dev=/dev/$arduino_device:115200 || :
  195. }
  196. function flashrom_ch341_check_ {
  197. echo -e "\n\e[93mCheck flashrom :\e[0m"
  198. flashrom -p ch341a_spi || :
  199. }
  200. function footer_ {
  201. echo ""
  202. echo -e "\e[92m\e[21mMemo Command line: \e[0m"
  203. echo -e ""
  204. echo -e "\e[93m\e[21mArduino/Mega2560/Mega1280: \e[0m"
  205. echo -e "flashrom -p serprog:dev=/dev/$arduino_device:115200"
  206. echo -e ""
  207. echo -e "\e[93m\e[21mRaspberryPI/beagleBone: \e[0m"
  208. echo -e "flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=2000"
  209. echo -e ""
  210. echo -e "\e[93m\e[21mCh341a: \e[0m"
  211. echo -e "flashrom -p ch341a_spi"
  212. echo -e ""
  213. if ! [ -x "$(command -v flashrom)" ]
  214. then
  215. echo 'Error: flashrom is not yet installed.' >&2
  216. echo 'Build and come back to help section....' >&2
  217. else
  218. echo -e "\e[93m\e[21mFlashrom version: \e[0m"
  219. flashrom -R
  220. fi
  221. echo ""
  222. }
  223. function show_help {
  224. echo "\
  225. Usage: $0 [--arduino] [--mega] [--raspberry] [--clean] [--help]
  226. --help Display extended help message
  227. --arduino Build Serial SPI flashrom tools for arduino328
  228. --mega Build Serial SPI flashrom tools for arduinoMega
  229. --raspberry Build Serial SPI flashrom tools for raspberryPI
  230. --ch341 Build Serial SPI flashrom tools for CH341 USB
  231. --clean Clean all build files
  232. Install script Written by: wareck <wareck@gmail.com>
  233. "
  234. }
  235. if [ "$#" == "0" ]; then
  236. $0 *
  237. exit 0
  238. fi
  239. for i in "$@"
  240. do
  241. case $i in
  242. --help)
  243. show_help && footer_
  244. exit
  245. ;;
  246. --arduino)
  247. arduino_ && flashrom_ && flashrom_arduino_check_ && footer_
  248. ;;
  249. --mega)
  250. mega_ && flashrom_ && flashrom_mega_check_ && footer_
  251. ;;
  252. --raspberry)
  253. raspberry_ && flashrom_ && flashrom_raspberry_check_ && footer_
  254. ;;
  255. --ch341)
  256. ch341_ && flashrom_ && flashrom_ch341_check_ && footer_
  257. ;;
  258. --clean)
  259. clean_
  260. ;;
  261. *)
  262. show_help
  263. exit
  264. ;;
  265. esac
  266. done