No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rtc.sh 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/bin/bash
  2. : <<'DISCLAIMER'
  3. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  4. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  5. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  6. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  7. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  8. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  9. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  10. OTHER DEALINGS IN THE SOFTWARE.
  11. This script is licensed under the terms of the MIT license.
  12. Unless otherwise noted, code reproduced herein
  13. was written for this script.
  14. Originally written by the - The Pimoroni Crew -
  15. DISCLAIMER
  16. productname="Real Time Clock module" # the name of the product to install
  17. rtcsubtype="ds1307"
  18. baseurl="http://www.adafru.it"
  19. scriptname="rtc.sh" # the name of this script
  20. spacereq=1 # minimum size required on root partition in MB
  21. debugmode="no" # whether the script should use debug routines
  22. debuguser="none" # optional test git user to use in debug mode
  23. debugpoint="none" # optional git repo branch or tag to checkout
  24. forcesudo="yes" # whether the script requires to be ran with root privileges
  25. promptreboot="no" # whether the script should always prompt user to reboot
  26. customcmd="yes" # whether to execute commands specified before exit
  27. i2creq="yes" # whether the i2c interface is required
  28. i2sreq="no" # whether the i2s interface is required
  29. spireq="no" # whether the spi interface is required
  30. uartreq="no" # whether uart communication is required
  31. armhfonly="yes" # whether the script is allowed to run on other arch
  32. armv6="yes" # whether armv6 processors are supported
  33. armv7="yes" # whether armv7 processors are supported
  34. armv8="yes" # whether armv8 processors are supported
  35. raspbianonly="yes" # whether the script is allowed to run on other OSes
  36. macosxsupport="no" # whether Mac OS X is supported by the script
  37. osreleases=( "Raspbian" ) # list os-releases supported
  38. oswarning=( "Debian" "Ubuntu" "Mate" ) # list experimental os-releases
  39. squeezesupport="no" # whether Squeeze is supported
  40. wheezysupport="no" # whether Wheezy is supported
  41. jessiesupport="yes" # whether Jessie is supported
  42. FORCE=$1
  43. DEVICE_TREE=true
  44. ASK_TO_REBOOT=false
  45. CURRENT_SETTING=false
  46. UPDATE_DB=false
  47. BOOTCMD=/boot/cmdline.txt
  48. CONFIG=/boot/config.txt
  49. APTSRC=/etc/apt/sources.list
  50. INITABCONF=/etc/inittab
  51. BLACKLIST=/etc/modprobe.d/raspi-blacklist.conf
  52. LOADMOD=/etc/modules
  53. DTBODIR=/boot/overlays
  54. cred=`tput setaf 1`
  55. cyellow=`tput setaf 3`
  56. cgreen=`tput setaf 2`
  57. cblue=`tput setaf 4`
  58. creset=`tput sgr0`
  59. confirm() {
  60. if [ "$FORCE" == '-y' ]; then
  61. true
  62. else
  63. read -r -p "$1 [y/N] " response < /dev/tty
  64. if [[ $response =~ ^(yes|y|Y)$ ]]; then
  65. true
  66. else
  67. false
  68. fi
  69. fi
  70. }
  71. prompt() {
  72. read -r -p "$1 [y/N] " response < /dev/tty
  73. if [[ $response =~ ^(yes|y|Y)$ ]]; then
  74. true
  75. else
  76. false
  77. fi
  78. }
  79. success() {
  80. echo "$(tput setaf 2)$1$(tput sgr0)"
  81. }
  82. warning() {
  83. echo "$(tput setaf 1)$1$(tput sgr0)"
  84. }
  85. newline() {
  86. echo ""
  87. }
  88. sudocheck() {
  89. if [ $(id -u) -ne 0 ]; then
  90. echo -e "Install must be run as root. Try 'sudo ./$scriptname'\n"
  91. exit 1
  92. fi
  93. }
  94. sysclean() {
  95. sudo apt-get clean && sudo apt-get autoclean
  96. sudo apt-get -y autoremove &> /dev/null
  97. }
  98. sysupdate() {
  99. if ! $UPDATE_DB; then
  100. sudo apt-get update
  101. UPDATE_DB=true
  102. fi
  103. }
  104. sysupgrade() {
  105. sudo apt-get update && sudo apt-get upgrade
  106. sudo apt-get clean && sudo apt-get autoclean
  107. sudo apt-get -y autoremove &> /dev/null
  108. }
  109. sysreboot() {
  110. warning "Some changes made to your system require"
  111. warning "your computer to reboot to take effect."
  112. newline
  113. if prompt "Would you like to reboot now?"; then
  114. sync && sudo reboot
  115. fi
  116. }
  117. arch_check() {
  118. IS_ARMHF=false
  119. IS_ARMv6=false
  120. if uname -m | grep "armv.l" > /dev/null; then
  121. IS_ARMHF=true
  122. if uname -m | grep "armv6l" > /dev/null; then
  123. IS_ARMv6=true
  124. fi
  125. fi
  126. }
  127. os_check() {
  128. IS_RASPBIAN=false
  129. IS_MACOSX=false
  130. IS_SUPPORTED=false
  131. IS_EXPERIMENTAL=false
  132. if [ -f /etc/os-release ]; then
  133. if cat /etc/os-release | grep "Raspbian" > /dev/null; then
  134. IS_RASPBIAN=true && IS_SUPPORTED=true
  135. fi
  136. if command -v apt-get > /dev/null; then
  137. for os in ${osreleases[@]}; do
  138. if cat /etc/os-release | grep "$os" > /dev/null; then
  139. IS_SUPPORTED=true
  140. fi
  141. done
  142. for os in ${oswarning[@]}; do
  143. if cat /etc/os-release | grep "$os" > /dev/null; then
  144. IS_EXPERIMENTAL=true
  145. fi
  146. done
  147. fi
  148. elif uname -s | grep "Darwin" > /dev/null; then
  149. IS_MACOSX=true
  150. if [ $macosxsupport == "yes" ]; then
  151. IS_SUPPORTED=true
  152. fi
  153. fi
  154. }
  155. raspbian_check() {
  156. IS_SQUEEZE=false
  157. IS_WHEEZY=false
  158. IS_JESSIE=false
  159. if [ -f /etc/os-release ]; then
  160. if cat /etc/os-release | grep "jessie" > /dev/null; then
  161. IS_JESSIE=true
  162. elif cat /etc/os-release | grep "wheezy" > /dev/null; then
  163. IS_WHEEZY=true
  164. elif cat /etc/os-release | grep "squeeze" > /dev/null; then
  165. IS_SQUEEZE=true
  166. else
  167. echo "Unsupported distribution"
  168. exit 1
  169. fi
  170. fi
  171. }
  172. raspbian_old() {
  173. if $IS_SQUEEZE || $IS_WHEEZY ;then
  174. true
  175. else
  176. false
  177. fi
  178. }
  179. dt_check() {
  180. if [ -e $CONFIG ] && grep -q "^device_tree=$" $CONFIG; then
  181. DEVICE_TREE=false
  182. fi
  183. }
  184. i2c_check() {
  185. if [ -e $CONFIG ] && grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*i2c(_arm)?(=(on|true|yes|1))?(,.*)?$" $CONFIG; then
  186. CURRENT_SETTING=true
  187. else
  188. CURRENT_SETTING=false
  189. fi
  190. }
  191. spi_check() {
  192. if [ -e $CONFIG ] && grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*spi(=(on|true|yes|1))?(,.*)?$" $CONFIG; then
  193. CURRENT_SETTING=true
  194. else
  195. CURRENT_SETTING=false
  196. fi
  197. }
  198. get_init_sys() {
  199. if command -v systemctl > /dev/null && systemctl | grep -q '\-\.mount'; then
  200. SYSTEMD=1
  201. elif [ -f /etc/init.d/cron ] && [ ! -h /etc/init.d/cron ]; then
  202. SYSTEMD=0
  203. else
  204. echo "Unrecognised init system" && exit 1
  205. fi
  206. }
  207. get_hw_rev() {
  208. hwrid=$(grep "^Revision" /proc/cpuinfo | rev | cut -c 2-3 | rev)
  209. if [ "$hwrid" == "00" ] || [ "$hwrid" == "01" ];then # Pi 1
  210. hwver="$(grep "^Revision" /proc/cpuinfo | rev | cut -c 1-4 | rev)"
  211. if [ "$hwrid" == "00" ];then
  212. hwgen="126" # P1
  213. elif [ "$hwrid" == "01" ];then
  214. hwgen="140" # J8
  215. fi
  216. else
  217. hwver="$(grep "^Revision" /proc/cpuinfo | rev | cut -c 1-6 | rev)"
  218. if [ "$hwrid" == "04" ];then # Pi 2
  219. hwgen="240"
  220. elif [ "$hwrid" == "08" ];then # Pi 3
  221. hwgen="340"
  222. elif [ "$hwrid" == "09" ];then # Pi 0
  223. hwgen="040"
  224. else # Unknown
  225. hwgen="000"
  226. fi
  227. fi
  228. }
  229. : <<'MAINSTART'
  230. Perform all global variables declarations as well as function definition
  231. above this section for clarity, thanks!
  232. MAINSTART
  233. dt_check
  234. arch_check
  235. os_check
  236. if ! $IS_ARMHF; then
  237. warning "This hardware is not supported, sorry!"
  238. warning "Config files have been left untouched"
  239. newline && exit 1
  240. fi
  241. if $IS_RASPBIAN; then
  242. raspbian_check
  243. if [ $wheezysupport == "no" ] && raspbian_old; then
  244. newline && warning "--- Warning ---" && newline
  245. echo "The $productname installer"
  246. echo "does not work on this version of Raspbian."
  247. echo "Check https://github.com/$gitusername/$gitreponame"
  248. echo "for additional information and support"
  249. newline && exit 1
  250. fi
  251. elif [ $raspbianonly == "yes" ];then
  252. warning "This script is intended for Raspbian on a Raspberry Pi!"
  253. newline && exit 1
  254. else
  255. if ! $IS_SUPPORTED && ! $IS_EXPERIMENTAL; then
  256. warning "Your operating system is not supported, sorry!"
  257. warning "Config files have been left untouched"
  258. newline && exit 1
  259. fi
  260. fi
  261. if [ $forcesudo == "yes" ]; then
  262. sudocheck
  263. fi
  264. newline
  265. echo "This script will install everything needed to use"
  266. echo "$productname"
  267. newline
  268. warning "--- Warning ---"
  269. newline
  270. echo "Always be careful when running scripts and commands"
  271. echo "copied from the internet. Ensure they are from a"
  272. echo "trusted source."
  273. newline
  274. echo "If you want to see what this script does before"
  275. echo "running it, you should run:"
  276. echo " \curl -sS $baseurl/$scriptname"
  277. newline
  278. if confirm "Do you wish to continue?"; then
  279. newline
  280. echo "Checking hardware requirements..."
  281. if [ $i2creq == "yes" ]; then
  282. newline
  283. if confirm "Hardware requires I2C, enable now?"; then
  284. \curl -sS $baseurl/i2c.sh | bash -s - "-y"
  285. fi
  286. fi
  287. if ! $DEVICE_TREE; then
  288. newline
  289. warning "Device Tree support required!"
  290. warning "Config files have been left untouched"
  291. newline && exit 1
  292. fi
  293. read -r -p "What RTC would you like to install? [ds1307 ds3231 or pcf8523] " response < /dev/tty
  294. if [[ $response == "ds1307" || $response == "ds3231" || $response == "pcf8523" ]]; then
  295. rtcsubtype=$response
  296. else
  297. warning "RTC type $response not supported!"
  298. newline && exit 1
  299. fi
  300. echo "Disabling any current RTC"
  301. sudo sed --in-place '/dtoverlay[[:space:]]*=[[:space:]]*i2c-rtc/d' $CONFIG &> /dev/null
  302. echo "Adding DT overlay for $rtcsubtype"
  303. echo "dtoverlay=i2c-rtc,$rtcsubtype" | sudo tee -a $CONFIG &> /dev/null
  304. echo "Removing fake-hwclock"
  305. sudo apt-get -y remove fake-hwclock
  306. sudo update-rc.d -f fake-hwclock remove
  307. if ! [ -e "/lib/udev/hwclock-set" ]; then
  308. newline
  309. warning "Couldn't find /lib/udev/hwclock-set"
  310. newline && exit 1
  311. fi
  312. echo "${cyellow}Configuring HW Clock ${creset}"
  313. sudo sed --in-place '/if \[ -e \/run\/systemd\/system \] ; then/,+2 s/^#*/#/' /lib/udev/hwclock-set
  314. sysreboot
  315. else
  316. newline
  317. warning "Aborting..."
  318. newline
  319. fi
  320. exit 0