build.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.1"
  11. rdate="24/12/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 ch341a_ {
  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 \
  132. ghex libusb-1.0-0-dev libftdi-dev libftdi1-dev libjaylink-dev -y
  133. cat <<'EOF'>>02-ch341a.rules
  134. # Ch341A
  135. # Ch341A
  136. SUBSYSTEM=="usb", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="5512", MODE:="0666"
  137. EOF
  138. sudo usermod -a -G dialout,plugdev $USER
  139. sudo cp 02-ch341a.rules /etc/udev/rules.d/
  140. rm 02-ch341a.rules
  141. }
  142. function flashrom_ {
  143. if ! [ -x "$(command -v flashrom)" ]
  144. then
  145. cd $CWD
  146. if [ ! -d flashrom ]
  147. then
  148. echo -e "\n\e[93mDownload flashrom :\e[0m"
  149. git clone https://github.com/flashrom/flashrom.git
  150. fi
  151. cd flashrom
  152. sed -i -e "s/WARNERROR ?= yes/WARNERROR ?= no/g" Makefile
  153. echo -e "\n\e[93mBuild and install flashrom:\e[0m"
  154. make -j$(nproc)
  155. sudo make install
  156. fi
  157. }
  158. function clean_ {
  159. echo -e ""
  160. echo -e "\e[97mCleaning:\e[0m\e[24m"
  161. if [ -d flashrom ]; then rm -r -f flashrom; fi
  162. if [ -d frser-duino ]; then rm -r -f frser-duino; fi
  163. sleep 3
  164. if [ -f .lastbuild ]
  165. then
  166. source .lastbuild
  167. if [ $Mode = "mega" ]
  168. then
  169. avrdude -p m$mega -c stk500v2 -P /dev/$Device -F -U flash:w:.clean/Mega_blink.hex
  170. fi
  171. if [ $Mode = "arduino" ]
  172. then
  173. avrdude -p atmega328p -c arduino -P /dev/$Device -b 115200 -D -U flash:w:.clean/Arduino_blink.hex
  174. fi
  175. rm .lastbuild
  176. fi
  177. if [ -f /usr/sbin/flashrom ]
  178. then
  179. sudo rm /usr/sbin/flashrom
  180. fi
  181. echo -e "\n\e[95mCleaning...Done !\e[0m"
  182. echo""
  183. }
  184. function flashrom_raspberry_check_ {
  185. echo -e "\n\e[93mCheck flashrom :\e[0m"
  186. flashrom -p linux_spi:dev=/dev/spidev0.0 || :
  187. }
  188. function flashrom_arduino_check_ {
  189. echo -e "\n\e[93mCheck flashrom :\e[0m"
  190. sleep 1
  191. flashrom -p serprog:dev=/dev/$arduino_device:115200 || :
  192. }
  193. function flashrom_mega_check_ {
  194. echo -e "\n\e[93mCheck flashrom :\e[0m"
  195. flashrom -p serprog:dev=/dev/$arduino_device:115200 || :
  196. }
  197. function flashrom_ch341a_check_ {
  198. echo -e "\n\e[93mCheck flashrom :\e[0m"
  199. sudo flashrom -p ch341a_spi || :
  200. }
  201. function footer_ {
  202. echo ""
  203. echo -e "\e[92m\e[21mMemo Command line: \e[0m"
  204. echo -e ""
  205. echo -e "\e[93m\e[21mArduino/Mega2560/Mega1280: \e[0m"
  206. echo -e "flashrom -p serprog:dev=/dev/$arduino_device:115200"
  207. echo -e ""
  208. echo -e "\e[93m\e[21mRaspberryPI/beagleBone: \e[0m"
  209. echo -e "flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=2000"
  210. echo -e ""
  211. echo -e "\e[93m\e[21mCh341a: \e[0m"
  212. echo -e "flashrom -p ch341a_spi"
  213. echo -e "(ch341a need one reboot before use or use sudo)"
  214. echo -e ""
  215. if ! [ -x "$(command -v flashrom)" ]
  216. then
  217. echo 'Error: flashrom is not yet installed.' >&2
  218. echo 'Build and come back to help section....' >&2
  219. else
  220. echo -e "\e[93m\e[21mFlashrom version: \e[0m"
  221. flashrom -R
  222. fi
  223. echo ""
  224. }
  225. function show_help {
  226. echo "\
  227. Usage: $0 [--arduino] [--mega] [--raspberry] [--clean] [--help]
  228. --help Display extended help message
  229. --arduino Build Serial SPI flashrom tools for arduino328
  230. --mega Build Serial SPI flashrom tools for arduinoMega
  231. --raspberry Build Serial SPI flashrom tools for raspberryPI
  232. --ch341a Build Serial SPI flashrom tools for CH341 USB
  233. --clean Clean all build files
  234. Install script Written by: wareck <wareck@gmail.com>
  235. "
  236. }
  237. if [ "$#" == "0" ]; then
  238. $0 *
  239. exit 0
  240. fi
  241. for i in "$@"
  242. do
  243. case $i in
  244. --help)
  245. show_help && footer_
  246. exit
  247. ;;
  248. --arduino)
  249. arduino_ && flashrom_ && flashrom_arduino_check_ && footer_
  250. ;;
  251. --mega)
  252. mega_ && flashrom_ && flashrom_mega_check_ && footer_
  253. ;;
  254. --raspberry)
  255. raspberry_ && flashrom_ && flashrom_raspberry_check_ && footer_
  256. ;;
  257. --ch341a)
  258. ch341a_ && flashrom_ && flashrom_ch341a_check_ && footer_
  259. ;;
  260. --clean)
  261. clean_
  262. ;;
  263. *)
  264. show_help
  265. exit
  266. ;;
  267. esac
  268. done