Advertisement
dheadshot

Modified ReSpeaker Installer

Aug 24th, 2020 (edited)
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.43 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #FORCE_KERNEL="1.20190925+1-1"
  4. #FORCE_KERNEL="1.20200212-1"
  5. #FORCE_KERNEL="1.20190517-1"
  6. FORCE_KERNEL="1.20190401-1"
  7.  
  8. if [[ $EUID -ne 0 ]]; then
  9.    echo "This script must be run as root (use sudo)" 1>&2
  10.    exit 1
  11. fi
  12.  
  13. # Check for enough space on /boot volume
  14. boot_line=$(df -h | grep /boot | head -n 1)
  15. if [ "x${boot_line}" = "x" ]; then
  16.   echo "Warning: /boot volume not found .."
  17. else
  18.   boot_space=$(echo $boot_line | awk '{print $4;}')
  19.   free_space=$(echo "${boot_space%?}")
  20.   unit="${boot_space: -1}"
  21.   if [[ "$unit" = "K" ]]; then
  22.     echo "Error: Not enough space left ($boot_space) on /boot"
  23.     exit 1
  24.   elif [[ "$unit" = "M" ]]; then
  25.     if [ "$free_space" -lt "25" ]; then
  26.       echo "Error: Not enough space left ($boot_space) on /boot"
  27.       exit 1
  28.     fi
  29.   fi
  30. fi
  31.  
  32. #
  33. # make sure that we are on something ARM/Raspberry related
  34. # either a bare metal Raspberry or a qemu session with
  35. # Raspberry stuff available
  36. # - check for /boot/overlays
  37. # - dtparam and dtoverlay is available
  38. errorFound=0
  39. if [ ! -d /boot/overlays ] ; then
  40.   echo "/boot/overlays not found or not a directory" 1>&2
  41.   errorFound=1
  42. fi
  43. # should we also check for alsactl and amixer used in seeed-voicecard?
  44. for cmd in dtparam dtoverlay ; do
  45.   if ! which $cmd &>/dev/null ; then
  46.     echo "$cmd not found" 1>&2
  47.     errorFound=1
  48.   fi
  49. done
  50. if [ $errorFound = 1 ] ; then
  51.   echo "Errors found, exiting." 1>&2
  52.   exit 1
  53. fi
  54.  
  55. ver="0.3"
  56. uname_r=$(uname -r)
  57.  
  58. # we create a dir with this version to ensure that 'dkms remove' won't delete
  59. # the sources during kernel updates
  60. marker="0.0.0"
  61.  
  62. _VER_RUN=
  63. function get_kernel_version() {
  64.   local ZIMAGE IMG_OFFSET
  65.  
  66.   _VER_RUN=""
  67.   [ -z "$_VER_RUN" ] && {
  68.     ZIMAGE=/boot/kernel.img
  69.     IMG_OFFSET=$(LC_ALL=C grep -abo $'\x1f\x8b\x08\x00' $ZIMAGE | head -n 1 | cut -d ':' -f 1)
  70.     _VER_RUN=$(dd if=$ZIMAGE obs=64K ibs=4 skip=$(( IMG_OFFSET / 4)) | zcat | grep -a -m1 "Linux version" | strings | awk '{ print $3; }')
  71.   }
  72.   echo "$_VER_RUN"
  73.   return 0
  74. }
  75.  
  76. function check_kernel_headers() {
  77.   VER_RUN=$(get_kernel_version)
  78.   VER_HDR=$(dpkg -L raspberrypi-kernel-headers | egrep -m1 "/lib/modules/[^-]+/build" | awk -F'/' '{ print $4; }')
  79.   [ "X$VER_RUN" == "X$VER_HDR" ] && {
  80.     return 0
  81.   }
  82.  
  83.   # echo RUN=$VER_RUN HDR=$VER_HDR
  84.   echo " !!! Your kernel version is $VER_RUN"
  85.   echo "     Not found *** coressponding *** kernel headers with apt-get."
  86.   echo "     This may occur if you have ran 'rpi-update'."
  87.   echo " Choose  *** y *** will revert the kernel to version $VER_HDR then continue."
  88.   echo " Choose  *** N *** will exit without this driver support, by default."
  89.   read -p "Would you like to proceed? (y/N)" -n 1 -r -s
  90.   echo
  91.   if ! [[ $REPLY =~ ^[Yy]$ ]]; then
  92.     exit 1;
  93.   fi
  94.  
  95.   #apt-get -y --reinstall install raspberrypi-kernel
  96. }
  97.  
  98. function download_install_debpkg() {
  99.   local prefix name r
  100.   prefix=$1
  101.   name=$2
  102.  
  103.   for (( i = 0; i < 3; i++ )); do
  104.     wget $prefix$name -O /tmp/$name && break
  105.   done
  106.   dpkg -i /tmp/$name; r=$?
  107.   rm -f /tmp/$name
  108.   return $r
  109. }
  110.  
  111. option_pattern="compat-kernel"
  112. #if [[ $1 =~ ${option_pattern} ]]; then
  113.   echo "will compile with a compatible kernel..."
  114. #else
  115. #  FORCE_KERNEL=""
  116. #  echo "will compile with the latest kernel..."
  117. #fi
  118.  
  119. function install_kernel() {
  120.   local _url _prefix
  121.  
  122.   # Instead of retriving the lastest kernel & headers
  123. #&& {
  124. #    apt-get -y --force-yes install raspberrypi-kernel-headers raspberrypi-kernel
  125. #  }
  126. #
  127.   [ "X$FORCE_KERNEL" == "X" ] || {
  128.     # We would like to a fixed version
  129.     KERN_NAME=raspberrypi-kernel_${FORCE_KERNEL}_armhf.deb
  130.     HDR_NAME=raspberrypi-kernel-headers_${FORCE_KERNEL}_armhf.deb
  131.     #_url=$(apt-get download --print-uris raspberrypi-kernel | sed -nre "s/'([^']+)'.*$/\1/g;p")
  132.  
  133.     _prefix='http://archive.raspberrypi.org/debian/pool/main/r/raspberrypi-firmware/'
  134.  
  135.     #_prefix=$(echo $_url | sed -nre 's/^(.*)raspberrypi-kernel_.*$/\1/g;p')
  136.  
  137.     download_install_debpkg "$_prefix" "$KERN_NAME"
  138.     download_install_debpkg "$_prefix" "$HDR_NAME"
  139.   }
  140. }
  141.  
  142. # update and install required packages
  143. which apt &>/dev/null
  144. if [[ $? -eq 0 ]]; then
  145.   apt update -y
  146.   #apt-get -y install dkms git i2c-tools libasound2-plugins
  147.   install_kernel
  148.   # rpi-update checker
  149.   check_kernel_headers
  150. fi
  151.  
  152. # Arch Linux
  153. which pacman &>/dev/null
  154. if [[ $? -eq 0 ]]; then
  155.   pacman -Syu --needed git gcc automake make dkms linux-raspberrypi-headers i2c-tools
  156. fi
  157.  
  158. # locate currently installed kernels (may be different to running kernel if
  159. # it's just been updated)
  160. base_ver=$(get_kernel_version)
  161. base_ver=${base_ver%%[-+]*}
  162. kernels="${base_ver}+ ${base_ver}-v7+ ${base_ver}-v7l+"
  163.  
  164. echo kernels
  165. exit
  166.  
  167.  
  168. function install_module {
  169.   local _i
  170.  
  171.   src=$1
  172.   mod=$2
  173.  
  174.   if [[ -d /var/lib/dkms/$mod/$ver/$marker ]]; then
  175.     rmdir /var/lib/dkms/$mod/$ver/$marker
  176.   fi
  177.  
  178.   if [[ -e /usr/src/$mod-$ver || -e /var/lib/dkms/$mod/$ver ]]; then
  179.     dkms remove --force -m $mod -v $ver --all
  180.     rm -rf /usr/src/$mod-$ver
  181.   fi
  182.  
  183.   mkdir -p /usr/src/$mod-$ver
  184.   cp -a $src/* /usr/src/$mod-$ver/
  185.  
  186.   dkms add -m $mod -v $ver
  187.   for _i in $kernels; do
  188.     dkms build -k $_i -m $mod -v $ver && {
  189.       dkms install --force -k $_i -m $mod -v $ver
  190.     } || {
  191.       echo "can not compile with this kernel, abort"
  192.       echo "please try compile with the option --compat-kernel"
  193.       exit 1
  194.     }
  195.   done
  196.  
  197.   mkdir -p /var/lib/dkms/$mod/$ver/$marker
  198. }
  199.  
  200. install_module "./" "seeed-voicecard"
  201.  
  202.  
  203. # install dtbos
  204. cp seeed-2mic-voicecard.dtbo /boot/overlays
  205. cp seeed-4mic-voicecard.dtbo /boot/overlays
  206. cp seeed-8mic-voicecard.dtbo /boot/overlays
  207.  
  208. #install alsa plugins
  209. # no need this plugin now
  210. # install -D ac108_plugin/libasound_module_pcm_ac108.so /usr/lib/arm-linux-gnueabihf/alsa-lib/
  211. rm -f /usr/lib/arm-linux-gnueabihf/alsa-lib/libasound_module_pcm_ac108.so
  212.  
  213. #set kernel modules
  214. grep -q "^snd-soc-seeed-voicecard$" /etc/modules || \
  215.   echo "snd-soc-seeed-voicecard" >> /etc/modules
  216. grep -q "^snd-soc-ac108$" /etc/modules || \
  217.   echo "snd-soc-ac108" >> /etc/modules
  218. grep -q "^snd-soc-wm8960$" /etc/modules || \
  219.   echo "snd-soc-wm8960" >> /etc/modules  
  220.  
  221. #set dtoverlays
  222. sed -i -e 's:#dtparam=i2c_arm=on:dtparam=i2c_arm=on:g'  /boot/config.txt || true
  223. grep -q "^dtoverlay=i2s-mmap$" /boot/config.txt || \
  224.   echo "dtoverlay=i2s-mmap" >> /boot/config.txt
  225.  
  226.  
  227. grep -q "^dtparam=i2s=on$" /boot/config.txt || \
  228.   echo "dtparam=i2s=on" >> /boot/config.txt
  229.  
  230. #install config files
  231. mkdir /etc/voicecard || true
  232. cp *.conf /etc/voicecard
  233. cp *.state /etc/voicecard
  234.  
  235. #create git repo
  236. git_email=$(git config --global --get user.email)
  237. git_name=$(git config --global --get user.name)
  238. if [ "x${git_email}" == "x" ] || [ "x${git_name}" == "x" ] ; then
  239.     echo "setup git config"
  240.     git config --global user.email "respeaker@seeed.cc"
  241.     git config --global user.name "respeaker"
  242. fi
  243. echo "git init"
  244. git --git-dir=/etc/voicecard/.git init
  245. echo "git add --all"
  246. git --git-dir=/etc/voicecard/.git --work-tree=/etc/voicecard/ add --all
  247. echo "git commit -m \"origin configures\""
  248. git --git-dir=/etc/voicecard/.git --work-tree=/etc/voicecard/ commit  -m "origin configures"
  249.  
  250. cp seeed-voicecard /usr/bin/
  251. cp seeed-voicecard.service /lib/systemd/system/
  252. systemctl enable  seeed-voicecard.service
  253. systemctl start   seeed-voicecard
  254.  
  255. echo "------------------------------------------------------"
  256. echo "Please reboot your raspberry pi to apply all settings"
  257. echo "Enjoy!"
  258. echo "------------------------------------------------------"
  259.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement