daily pastebin goal
13%
SHARE
TWEET

beaglebone-black-eMMC-flasher.sh

osteth Apr 7th, 2016 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash -e
  2. #
  3. # Copyright (c) 2013-2015 Robert Nelson <robertcnelson@gmail.com>
  4. # Portions copyright (c) 2014 Charles Steinkuehler <charles@steinkuehler.net>
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23.  
  24. #This script assumes, these packages are installed, as network may not be setup
  25. #dosfstools initramfs-tools rsync u-boot-tools
  26.  
  27. if ! id | grep -q root; then
  28.     echo "must be run as root"
  29.     exit
  30. fi
  31.  
  32. unset boot_drive
  33. boot_drive=$(LC_ALL=C lsblk -l | grep "/boot/uboot" | awk '{print $1}')
  34.  
  35. if [ "x${boot_drive}" = "x" ] ; then
  36.     echo "Error: script halting, system unrecognized..."
  37.     exit 1
  38. fi
  39.  
  40. if [ "x${boot_drive}" = "xmmcblk0p1" ] ; then
  41.     source="/dev/mmcblk0"
  42.     destination="/dev/mmcblk1"
  43. fi
  44.  
  45. if [ "x${boot_drive}" = "xmmcblk1p1" ] ; then
  46.     source="/dev/mmcblk1"
  47.     destination="/dev/mmcblk0"
  48. fi
  49.  
  50. flush_cache () {
  51.     sync
  52.     blockdev --flushbufs ${destination}
  53. }
  54.  
  55. write_failure () {
  56.     echo "writing to [${destination}] failed..."
  57.  
  58.     [ -e /proc/$CYLON_PID ]  && kill $CYLON_PID > /dev/null 2>&1
  59.  
  60.     if [ -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
  61.         echo heartbeat > /sys/class/leds/beaglebone\:green\:usr0/trigger
  62.         echo heartbeat > /sys/class/leds/beaglebone\:green\:usr1/trigger
  63.         echo heartbeat > /sys/class/leds/beaglebone\:green\:usr2/trigger
  64.         echo heartbeat > /sys/class/leds/beaglebone\:green\:usr3/trigger
  65.     fi
  66.     echo "-----------------------------"
  67.     flush_cache
  68.     umount ${destination}p1 || true
  69.     umount ${destination}p2 || true
  70.     exit
  71. }
  72.  
  73. check_running_system () {
  74.     if [ ! -f /boot/uboot/uEnv.txt ] ; then
  75.         echo "Error: script halting, system unrecognized..."
  76.         echo "unable to find: [/boot/uboot/uEnv.txt] is ${source}p1 mounted?"
  77.         exit 1
  78.     fi
  79.  
  80.     echo "-----------------------------"
  81.     echo "debug copying: [${source}] -> [${destination}]"
  82.     lsblk
  83.     echo "-----------------------------"
  84.  
  85.     if [ ! -b "${destination}" ] ; then
  86.         echo "Error: [${destination}] does not exist"
  87.         write_failure
  88.     fi
  89. }
  90.  
  91. cylon_leds () {
  92.     if [ -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
  93.         BASE=/sys/class/leds/beaglebone\:green\:usr
  94.         echo none > ${BASE}0/trigger
  95.         echo none > ${BASE}1/trigger
  96.         echo none > ${BASE}2/trigger
  97.         echo none > ${BASE}3/trigger
  98.  
  99.         STATE=1
  100.         while : ; do
  101.             case $STATE in
  102.             1)  echo 255 > ${BASE}0/brightness
  103.                 echo 0   > ${BASE}1/brightness
  104.                 STATE=2
  105.                 ;;
  106.             2)  echo 255 > ${BASE}1/brightness
  107.                 echo 0   > ${BASE}0/brightness
  108.                 STATE=3
  109.                 ;;
  110.             3)  echo 255 > ${BASE}2/brightness
  111.                 echo 0   > ${BASE}1/brightness
  112.                 STATE=4
  113.                 ;;
  114.             4)  echo 255 > ${BASE}3/brightness
  115.                 echo 0   > ${BASE}2/brightness
  116.                 STATE=5
  117.                 ;;
  118.             5)  echo 255 > ${BASE}2/brightness
  119.                 echo 0   > ${BASE}3/brightness
  120.                 STATE=6
  121.                 ;;
  122.             6)  echo 255 > ${BASE}1/brightness
  123.                 echo 0   > ${BASE}2/brightness
  124.                 STATE=1
  125.                 ;;
  126.             *)  echo 255 > ${BASE}0/brightness
  127.                 echo 0   > ${BASE}1/brightness
  128.                 STATE=2
  129.                 ;;
  130.             esac
  131.             sleep 0.1
  132.         done
  133.     fi
  134. }
  135.  
  136. update_boot_files () {
  137.     #We need an initrd.img to find the uuid partition
  138.     if [ ! -f /boot/uboot/initrd.img ] ; then
  139.         if [ ! -f /boot/initrd.img-$(uname -r) ] ; then
  140.             update-initramfs -c -k $(uname -r)
  141.         else
  142.             update-initramfs -u -k $(uname -r)
  143.         fi
  144.  
  145.         if [ -f /boot/initrd.img-$(uname -r) ] ; then
  146.             cp -v /boot/initrd.img-$(uname -r) /boot/uboot/initrd.img
  147.         fi
  148.     fi
  149. }
  150.  
  151. fdisk_toggle_boot () {
  152.     fdisk ${destination} <<-__EOF__
  153.     a
  154.     1
  155.     w
  156.     __EOF__
  157.     flush_cache
  158. }
  159.  
  160. format_boot () {
  161.     LC_ALL=C fdisk -l ${destination} | grep ${destination}p1 | grep '*' || fdisk_toggle_boot
  162.  
  163.     mkfs.vfat -F 16 ${destination}p1 -n BOOT
  164.     flush_cache
  165. }
  166.  
  167. format_root () {
  168.     mkfs.ext4 ${destination}p2 -L rootfs
  169.     flush_cache
  170. }
  171.  
  172. partition_drive () {
  173.     flush_cache
  174.  
  175.     NUM_MOUNTS=$(mount | grep -v none | grep "${destination}" | wc -l)
  176.  
  177.     for ((i=1;i<=${NUM_MOUNTS};i++))
  178.     do
  179.         DRIVE=$(mount | grep -v none | grep "${destination}" | tail -1 | awk '{print $1}')
  180.         umount ${DRIVE} >/dev/null 2>&1 || umount -l ${DRIVE} >/dev/null 2>&1 || write_failure
  181.     done
  182.  
  183.     flush_cache
  184.     dd if=/dev/zero of=${destination} bs=1M count=108
  185.     sync
  186.     dd if=${destination} of=/dev/null bs=1M count=108
  187.     sync
  188.     flush_cache
  189.  
  190.     #96Mb fat formatted boot partition
  191.     LC_ALL=C sfdisk --force --in-order --Linux --unit M "${destination}" <<-__EOF__
  192.         1,96,0xe,*
  193.         ,,,-
  194.     __EOF__
  195.  
  196.     flush_cache
  197.     format_boot
  198.     format_root
  199. }
  200.  
  201. copy_boot () {
  202.     mkdir -p /tmp/boot/ || true
  203.     mount ${destination}p1 /tmp/boot/ -o sync
  204.     #Make sure the BootLoader gets copied first:
  205.     cp -v /boot/uboot/MLO /tmp/boot/MLO || write_failure
  206.     flush_cache
  207.  
  208.     cp -v /boot/uboot/u-boot.img /tmp/boot/u-boot.img || write_failure
  209.     flush_cache
  210.  
  211.     echo "rsync: /boot/uboot/ -> /tmp/boot/"
  212.     rsync -aAX /boot/uboot/ /tmp/boot/ --exclude={MLO,u-boot.img,*bak,flash-eMMC.txt,flash-eMMC.log} || write_failure
  213.     #touch /tmp/boot/flash-eMMC.txt
  214.     flush_cache
  215.  
  216.     unset root_uuid
  217.     root_uuid=$(/sbin/blkid -c /dev/null -s UUID -o value ${destination}p2)
  218.     if [ "${root_uuid}" ] ; then
  219.         root_uuid="UUID=${root_uuid}"
  220.         device_id=$(cat /tmp/boot/uEnv.txt | grep mmcroot | grep mmcblk | awk '{print $1}' | awk -F '=' '{print $2}')
  221.         if [ ! "${device_id}" ] ; then
  222.             device_id=$(cat /tmp/boot/uEnv.txt | grep mmcroot | grep UUID | awk '{print $1}' | awk -F '=' '{print $3}')
  223.             device_id="UUID=${device_id}"
  224.         fi
  225.         sed -i -e 's:'${device_id}':'${root_uuid}':g' /tmp/boot/uEnv.txt
  226.     else
  227.         root_uuid="${source}p2"
  228.     fi
  229.  
  230.     flush_cache
  231.     umount /tmp/boot/ || umount -l /tmp/boot/ || write_failure
  232. }
  233.  
  234. copy_rootfs () {
  235.     mkdir -p /tmp/rootfs/ || true
  236.     mount ${destination}p2 /tmp/rootfs/ -o async,noatime
  237.  
  238.     echo "rsync: / -> /tmp/rootfs/"
  239.     rsync -aAX /* /tmp/rootfs/ --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/boot/*,/lib/modules/*} || write_failure
  240.     flush_cache
  241.  
  242.     if [ -f /tmp/rootfs/opt/scripts/images/beaglebg.jpg ] ; then
  243.         if [ -f /tmp/rootfs/opt/desktop-background.jpg ] ; then
  244.             rm -f /tmp/rootfs/opt/desktop-background.jpg || true
  245.         fi
  246.         cp -v /tmp/rootfs/opt/scripts/images/beaglebg.jpg /tmp/rootfs/opt/desktop-background.jpg
  247.     fi
  248.  
  249.     #ssh keys will now get regenerated on the next bootup
  250.     touch /tmp/rootfs/etc/ssh/ssh.regenerate
  251.     flush_cache
  252.  
  253.     mkdir -p /tmp/rootfs/boot/uboot/ || true
  254.     mkdir -p /tmp/rootfs/lib/modules/$(uname -r)/ || true
  255.  
  256.     echo "rsync: /lib/modules/$(uname -r)/ -> /tmp/rootfs/lib/modules/$(uname -r)/"
  257.     rsync -aAX /lib/modules/$(uname -r)/* /tmp/rootfs/lib/modules/$(uname -r)/ || write_failure
  258.     flush_cache
  259.  
  260.     unset boot_uuid
  261.     boot_uuid=$(/sbin/blkid -c /dev/null -s UUID -o value ${destination}p1)
  262.     if [ "${boot_uuid}" ] ; then
  263.         boot_uuid="UUID=${boot_uuid}"
  264.     else
  265.         boot_uuid="${source}p1"
  266.     fi
  267.  
  268.     echo "Generating: /etc/fstab"
  269.     echo "# /etc/fstab: static file system information." > /tmp/rootfs/etc/fstab
  270.     echo "#" >> /tmp/rootfs/etc/fstab
  271.     echo "${root_uuid}  /  ext4  noatime,errors=remount-ro  0  1" >> /tmp/rootfs/etc/fstab
  272.     echo "${boot_uuid}  /boot/uboot  auto  defaults  0  0" >> /tmp/rootfs/etc/fstab
  273.     echo "debugfs  /sys/kernel/debug  debugfs  defaults  0  0" >> /tmp/rootfs/etc/fstab
  274.     cat /tmp/rootfs/etc/fstab
  275.     flush_cache
  276.     umount /tmp/rootfs/ || umount -l /tmp/rootfs/ || write_failure
  277.  
  278.     #https://github.com/beagleboard/meta-beagleboard/blob/master/contrib/bone-flash-tool/emmc.sh#L158-L159
  279.     # force writeback of eMMC buffers
  280.     dd if=${destination} of=/dev/null count=100000
  281.  
  282.     [ -e /proc/$CYLON_PID ]  && kill $CYLON_PID
  283.  
  284.     if [ -e /sys/class/leds/beaglebone\:green\:usr0/trigger ] ; then
  285.         echo default-on > /sys/class/leds/beaglebone\:green\:usr0/trigger
  286.         echo default-on > /sys/class/leds/beaglebone\:green\:usr1/trigger
  287.         echo default-on > /sys/class/leds/beaglebone\:green\:usr2/trigger
  288.         echo default-on > /sys/class/leds/beaglebone\:green\:usr3/trigger
  289.     fi
  290.  
  291.     echo ""
  292.     echo "This script has now completed it's task"
  293.     echo "-----------------------------"
  294.     echo "Note: Actually unpower the board, a reset [sudo reboot] is not enough."
  295.     echo "-----------------------------"
  296.  
  297.     echo "Shutting Down..."
  298.     sync
  299.     halt
  300. }
  301.  
  302. check_running_system
  303. cylon_leds & CYLON_PID=$!
  304. update_boot_files
  305. partition_drive
  306. copy_boot
  307. copy_rootfs
RAW Paste Data
Top