#!/bin/bash # Original Authors: # LT Thomas # Chase Maupin # Franklin Cooper Jr. # # Adapted by: # Devon Clark # create-sdcard.sh v0.4 # # This distribution contains contributions or derivatives under copyright # as follows: # # Copyright (c) 2010-2015, Texas Instruments Incorporated # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # - Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # - Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # - Neither the name of Texas Instruments nor the names of its # contributors may be used to endorse or promote products derived # from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # Change log: # - v0.3: Original release included in Processor SDK RTOS for AM57xx, version number 5.0.0.15 # - v0.4: This script has been updated to fix a bug when partitioning/writing to SD cards # Force locale language to be set to English. This avoids issues when doing # text and string processing. export LANG=C # Determine the absolute path to the executable # EXE will have the PWD removed so we can concatenate with the PWD safely PWD=`pwd` EXE=`echo $0 | sed s=$PWD==` EXEPATH="$PWD"/"$EXE" clear cat << EOM ################################################################################ This script will create a bootable SD card from custom or pre-built binaries. The script must be run with root permissions and from the bin directory of the SDK Example: $ sudo ./create-sdcard.sh path/to/sdcard/files NOTE: path/to/sdcard/file should be the path to the directory containing the files you wish to write to the SD card like the MLO and app files. This is not the path to the SD card (i.e. /media//) Formatting can be skipped if the SD card is already formatted and partitioned properly. ################################################################################ EOM AMIROOT=`whoami | awk {'print $1'}` if [ "$AMIROOT" != "root" ] ; then echo " **** Error *** must run script with sudo" echo "" exit fi THEPWD=$EXEPATH check_for_sdcards() { # find the avaible SD cards ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} | cut -c6-8` PARTITION_TEST=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''` if [ "$PARTITION_TEST" = "" ]; then echo -e "Please insert a SD card to continue\n" while [ "$PARTITION_TEST" = "" ]; do read -p "Type 'y' to re-detect the SD card or 'n' to exit the script: " REPLY if [ "$REPLY" = 'n' ]; then exit 1 fi ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} | cut -c6-8` PARTITION_TEST=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''` done fi } # find the avaible SD cards ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} | cut -c6-9` if [ "$ROOTDRIVE" = "root" ]; then ROOTDRIVE=`readlink /dev/root | cut -c1-3` else ROOTDRIVE=`echo $ROOTDRIVE | cut -c1-3` fi PARTITION_TEST=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n ''` # Check for available mounts check_for_sdcards echo -e "\nAvailable Drives to write images to: \n" echo "# major minor size name " cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' echo " " DEVICEDRIVENUMBER= while true; do read -p 'Enter Device Number or 'n' to exit: ' DEVICEDRIVENUMBER echo " " if [ "$DEVICEDRIVENUMBER" = 'n' ]; then exit 1 fi if [ "$DEVICEDRIVENUMBER" = "" ]; then # Check to see if there are any changes check_for_sdcards echo -e "These are the Drives available to write images to:" echo "# major minor size name " cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' echo " " continue fi DEVICEDRIVENAME=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' | grep "${DEVICEDRIVENUMBER}:" | awk '{print $5}'` if [ -n "$DEVICEDRIVENAME" ] then DRIVE=/dev/$DEVICEDRIVENAME DEVICESIZE=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' | grep "${DEVICEDRIVENUMBER}:" | awk '{print $4}'` break else echo -e "Invalid selection!" # Check to see if there are any changes check_for_sdcards echo -e "These are the only Drives available to write images to: \n" echo "# major minor size name " cat /proc/partitions | grep -v $ROOTDRIVE | grep '\\|\' | grep -n '' echo " " fi done echo "$DEVICEDRIVENAME was selected" #Check the size of disk to make sure its under 16GB if [ $DEVICESIZE -gt 17000000 ] ; then cat << EOM ################################################################################ **********WARNING********** Selected Device is greater then 16GB Continuing past this point will erase data from device Double check that this is the correct SD Card ################################################################################ EOM ENTERCORRECTLY=0 while [ $ENTERCORRECTLY -ne 1 ] do read -p 'Would you like to continue [y/n] : ' SIZECHECK echo "" echo " " ENTERCORRECTLY=1 case $SIZECHECK in "y") ;; "n") exit;; *) echo "Please enter y or n";ENTERCORRECTLY=0;; esac echo "" done fi echo "" DRIVE=/dev/$DEVICEDRIVENAME NUM_OF_DRIVES=`df | grep -c $DEVICEDRIVENAME` # This if statement will determine if we have a mounted sdX or mmcblkX device. # If it is mmcblkX, then we need to set an extra char in the partition names, 'p', # to account for /dev/mmcblkXpY labled partitions. # * * * * * * * * * * * * * * * * * * * * * * * * * * * * # Block commented out by DRC because adding a "p" to # the partition label causes the script to fail # #if [[ ${DEVICEDRIVENAME} =~ ^sd. ]]; then # echo "$DRIVE is an sdx device" # P='' #else # echo "$DRIVE is an mmcblkx device" # P='p' #fi # #if [ "$NUM_OF_DRIVES" != "0" ]; then # echo "Unmounting the $DEVICEDRIVENAME drives" # for ((c=1; c<="$NUM_OF_DRIVES"; c++ )) # do # unmounted=`df | grep '\<'$DEVICEDRIVENAME$P$c'\>' | awk '{print $1}'` # if [ -n "$unmounted" ] # then # echo " unmounted ${DRIVE}$P$c" # sudo umount -f ${DRIVE}$P$c # fi # # done # echo " unmounted ${DRIVE}$P" # sudo umount -f ${DRIVE}$P #else # echo " unmounted ${DRIVE}$P" # sudo umount -f ${DRIVE}$P #fi # * * * * * * * * * * * * * * * * * * * * * * * * * * * * echo " unmounted ${DRIVE}" sudo umount -f ${DRIVE} # Refresh this variable as the device may not be mounted at script instantiation time # This will always return one more then needed #NUM_OF_PARTS=`cat /proc/partitions | grep -v $ROOTDRIVE | grep -c $DEVICEDRIVENAME` #for ((c=1; c<"$NUM_OF_PARTS"; c++ )) #do # SIZE=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\<'$DEVICEDRIVENAME$P$c'\>' | awk '{print $3}'` # echo "Current size of $DEVICEDRIVENAME$P$c $SIZE bytes" #done SIZE=`cat /proc/partitions | grep -v $ROOTDRIVE | grep '\<'$DEVICEDRIVENAME'\>' | awk '{print $3}'` echo "Current size of $DEVICEDRIVENAME $SIZE bytes" #Section for partitioning the drive #Section for partitioning the drive #create 1 partition cat << EOM ################################################################################ Now erasing partition table ################################################################################ EOM dd if=/dev/zero of=$DRIVE bs=1024 count=1024 cat << EOM ################################################################################ Partitioning Boot ################################################################################ EOM mkfs.vfat -I -F 32 -n "boot" ${DRIVE} #Add directories for images export START_DIR=$PWD export PATH_TO_SDBOOT=boot echo " " echo "Mount the partitions " mkdir $PATH_TO_SDBOOT sudo mount -t vfat ${DRIVE} boot/ echo " " echo "Emptying partitions " echo " " sudo rm -rf $PATH_TO_SDBOOT/* echo "" echo "Syncing...." echo "" sync sync sync cat << EOM ################################################################################ Copying files now... will take minutes ################################################################################ Copying boot partition EOM echo "" #copy boot files out of board support if [ -f "$1/MLO" ] ; then cp -f $1/* $PATH_TO_SDBOOT/ else cat << EOM !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ERROR: SD card file directory does not exist ERROR: ($1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! EOM fi echo "" echo "" echo "Syncing..." sync sync sync echo " " echo "Un-mount the partitions " sudo umount -f $PATH_TO_SDBOOT echo " " echo "Remove created temp directories " sudo rm -rf $PATH_TO_SDBOOT echo " " echo "Operation Finished" echo " "