guowenxue
2020-07-02 1b729d3bc1c50c978fd0ad18956c53651d127712
Merge branch 'master' of ssh://master.iot-yun.club:2280/ok335x
1 files added
1 files renamed
251 ■■■■■ changed files
tools/ref/create-sdcard.sh patch | view | raw | blame | history
tools/uboot-sdcard.sh 251 ●●●●● patch | view | raw | blame | history
tools/ref/create-sdcard.sh
tools/uboot-sdcard.sh
New file
@@ -0,0 +1,251 @@
#!/bin/bash
# Author: Guo Wenxue<guowenxue@gmail.com>  QQ: 281143292
#
# This script will create a bootable SD card and install u-boot into it.
# Formatting can be skipped if the SD card is already formatted properly.
# SD card had better smaller than 16GB.
#
# It must be run with root permissions. Example:
#   $ sudo ./uboot-sdcard.sh
#
PWD=`pwd`
PRJ_PATH=`dirname ${PWD}`
IMG_PATH=${PRJ_PATH}/linux-bsp/images
function check_root_run()
{
   AMIROOT=`whoami | awk {'print $1'}`
   if [ "$AMIROOT" != "root" ] ; then
      echo ""
      echo "  **** Error *** must run script with sudo"
      echo ""
      exit
   fi
}
# find the avaible SD cards
function find_avaible_sdcard()
{
   echo " "
   echo "Availible Drives to write images to: "
   echo " "
   ROOTDRIVE=`mount | grep 'on / ' | awk {'print $1'} |  cut -c6-8`
   echo "#  major   minor    size   name "
   cat /proc/partitions | grep -v $ROOTDRIVE | grep '\<sd.\>' | grep -n ''
   echo " "
   ENTERCORRECTLY=0
   while [ $ENTERCORRECTLY -ne 1 ]
   do
      read -p 'Enter Device Number: ' DRIVENUMBER
      echo " "
      DEVNAME=`cat /proc/partitions | grep -v 'sda' | grep '\<sd.\>' | grep -n '' | grep "${DRIVENUMBER}:" | awk '{print $5}'`
      DRIVE=/dev/$DEVNAME
      DEVSIZE=`cat /proc/partitions | grep -v 'sda' | grep '\<sd.\>' | grep -n '' | grep "${DRIVENUMBER}:" | awk '{print $4}'`
      if [ -n "$DEVNAME" ] ; then
     ENTERCORRECTLY=1
     echo "$DEVNAME was selected"
      else
     echo "Invalid selection"
      fi
      echo ""
    done
}
function check_umount_sdcard()
{
echo ""
echo "+-----------------------------------------+"
echo "|    Check and umount SD card $DEVNAME    |"
echo "+-----------------------------------------+"
echo ""
   #unmount drives if they are mounted
   unmounted1=`df | grep '\<'$DEVNAME'1\>' | awk '{print $1}'`
   unmounted2=`df | grep '\<'$DEVNAME'2\>' | awk '{print $1}'`
   unmounted3=`df | grep '\<'$DEVNAME'3\>' | awk '{print $1}'`
   if [ -n "$unmounted1" ] ; then
      echo " unmounted ${DRIVE}1"
      sudo umount -f ${DRIVE}1
   fi
   if [ -n "$unmounted2" ] ; then
      echo " unmounted ${DRIVE}2"
      sudo umount -f ${DRIVE}2
   fi
   if [ -n "$unmounted3" ] ; then
      echo " unmounted ${DRIVE}3"
      sudo umount -f ${DRIVE}3
   fi
}
function check_partition_sdcard()
{
echo ""
echo "+-----------------------------------------+"
echo "|      Check SD cards partitions          |"
echo "+-----------------------------------------+"
echo ""
   # check to see if the device is already partitioned
   SIZE1=`cat /proc/partitions | grep -v 'sda' | grep '\<'$DEVNAME'1\>'  | awk '{print $3}'`
   SIZE2=`cat /proc/partitions | grep -v 'sda' | grep '\<'$DEVNAME'2\>'  | awk '{print $3}'`
   SIZE3=`cat /proc/partitions | grep -v 'sda' | grep '\<'$DEVNAME'3\>'  | awk '{print $3}'`
   SIZE4=`cat /proc/partitions | grep -v 'sda' | grep '\<'$DEVNAME'4\>'  | awk '{print $3}'`
   echo " ${DEVNAME}1   ${DEVNAME}2     ${DEVNAME}3"
   echo $SIZE1 $SIZE2 $SIZE3
   echo ""
   PARTITION="0"
   if [ -n "$SIZE1" -a -n "$SIZE2" ] ; then
      if  [ "$SIZE1" -gt "72000" -a "$SIZE2" -gt "700000" ] ; then
          echo  "Detected device has partitions already"
      PARTITION=1
      fi
   else
      echo "WARN: SD Card is not correctly partitioned"
   fi
}
function format_sdcard()
{
   check_partition_sdcard
   if [ $PARTITION -eq 1 ] ; then
      ENTERCORRECTLY=0
      while [ $ENTERCORRECTLY -ne 1 ]
      do
         echo ""
         read -p 'Would you like to re-partition the drive anyways [y/n] : ' CASEPARTITION
         ENTERCORRECTLY=1
         echo ""
         case $CASEPARTITION in
         "y")  echo "Now partitioning $DEVICEDRIVENAME ...";PARTITION=0;;
         "n")  echo "Skipping partitioning"; return ;;
           *)  echo "Please enter y or n";ENTERCORRECTLY=0;;
         esac
       done
   fi
echo ""
echo "+-----------------------------------------+"
echo "|        Now making 2 partitions          |"
echo "+-----------------------------------------+"
echo ""
   dd if=/dev/zero of=$DRIVE bs=1024 count=1024
   SIZE=`fdisk -l $DRIVE | grep Disk | awk '{print $5}'`
   echo DISK SIZE - $SIZE bytes
   # 每一个柱面的大小为255*63*512=8,225,280 Bytes
   CYLINDERS=`echo $SIZE/255/63/512 | bc`
# sfdisk的-D参数指定与DOS兼容,并自动在每个分区前预留空间,以存放MBR(Master Boot Record);
# 第一行分区描述,40,0x0C,*, 自动分配起始柱面,数量为40,分区ID为0x0C(表示FAT32分区),< bootable >为*, 表示可启动分区。
# 第二行分区描述,41,,,-, 起始柱面为41,后面的所有空间分配到该分区。
sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE << EOF
,40,0x0C,*
41,,,-
EOF
echo ""
echo "+-----------------------------------------+"
echo "|     Format boot partition as FAT        |"
echo "+-----------------------------------------+"
echo ""
    mkfs.vfat -F 32 -n "boot" ${DRIVE}1
echo ""
echo "+-----------------------------------------+"
echo "|     Format rootfs partition as ext3     |"
echo "+-----------------------------------------+"
echo ""
    mkfs.ext3 -L "rootfs" ${DRIVE}2
    sync
    sync
}
function install_uboot()
{
echo ""
echo "+-----------------------------------------+"
echo "|     Install bootloader into SD card     |"
echo "+-----------------------------------------+"
echo ""
   ENTERCORRECTLY=0
   while [ $ENTERCORRECTLY -ne 1 ]
   do
      read -p 'Would you like to continue install u-boot ? [y/n] : ' EXITQ
      echo ""
      echo " "
      ENTERCORRECTLY=1
      case $EXITQ in
           "n") ;;
       "y")
           mkdir /media/{boot,rootfs} 2> /dev/null
           sudo mount ${DRIVE}1 /media/boot
           COPY=0
           if [ -s ${IMG_PATH}/MLO ] ; then
          echo "cp -f ${IMG_PATH}/MLO /media/boot"
                  cp -f ${IMG_PATH}/MLO /media/boot
          COPY=1
           fi
           if [ -s ${IMG_PATH}/u-boot.img ] ; then
          echo "cp -f ${IMG_PATH}/u-boot.img /media/boot"
                  cp -f ${IMG_PATH}/u-boot.img /media/boot
          COPY=2
           fi
           if [ $COPY != 2 ] ; then
          echo "ERROR: MLO or u-boot.img copy failure"
           fi
           echo "ls /medial/boot"
           ls --color /media/boot
           echo ""
           echo "Install bootloader done"
           echo ""
           sudo umount /media/boot
           return;;
       *)  echo "Please enter y or n"; ENTERCORRECTLY=0;;
      esac
   done
}
check_root_run
find_avaible_sdcard
check_umount_sdcard
format_sdcard
install_uboot