install-cubox.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env bash
  2. # This file is part of the OpenADK project. OpenADK is copyrighted
  3. # material, please see the LICENCE file in the top-level directory.
  4. if [ $(id -u) -ne 0 ];then
  5. echo "Installation is only possible as root"
  6. exit 1
  7. fi
  8. f=0
  9. for tool in parted sfdisk mkfs.ext4;do
  10. if ! which $tool >/dev/null; then
  11. echo "Checking if $tool is installed... failed"
  12. f=1
  13. fi
  14. done
  15. if [ $f -eq 1 ];then exit 1;fi
  16. datadir=0
  17. keep=0
  18. while getopts "dk" ch; do
  19. case $ch in
  20. d)
  21. datadir=1
  22. ;;
  23. k)
  24. keep=1
  25. ;;
  26. esac
  27. done
  28. shift $((OPTIND - 1))
  29. if [ -z $1 ];then
  30. echo "Please give your SD card device as first parameter"
  31. exit 1
  32. else
  33. if [ -z $2 ];then
  34. echo "Please give your install tar archive as second parameter"
  35. exit 1
  36. fi
  37. if [ -f $2 ];then
  38. echo "Installing $2 on $1"
  39. else
  40. echo "$2 is not a file, exiting"
  41. exit 1
  42. fi
  43. if [ -z $3 ];then
  44. echo "Please give your firmware directory as third parameter"
  45. exit 1
  46. fi
  47. if [ ! -d $3 ];then
  48. echo "$3 is not a directory, exiting"
  49. exit 1
  50. fi
  51. if [ -b $1 ];then
  52. echo "Using $1 as SD card disk for installation"
  53. echo "WARNING: This will destroy all data on $1 - type Yes to continue!"
  54. read y
  55. if [ "$y" = "Yes" ];then
  56. env LC_ALL=C sfdisk -l $1 2>&1 |grep 'No medium'
  57. if [ $? -eq 0 ];then
  58. echo "No medium found"
  59. exit 1
  60. else
  61. echo "Starting with installation"
  62. fi
  63. else
  64. echo "Exiting."
  65. exit 1
  66. fi
  67. else
  68. echo "Sorry $1 is not a block device"
  69. exit 1
  70. fi
  71. fi
  72. if [ $(mount | grep $1| wc -l) -ne 0 ];then
  73. echo "Block device $1 is in use, please umount first"
  74. exit 1
  75. fi
  76. maxsize=$(env LC_ALL=C parted $1 -s unit s print |awk '/^Disk/ { print $3 }'|sed -e 's/s//')
  77. maxsize=$(($maxsize-1))
  78. rootsize=$(($maxsize-32768))
  79. rootsizeend=$(($rootsize+1))
  80. echo "Install bootloader for cubox-i"
  81. parted -s $1 mklabel msdos >/dev/null 2>&1
  82. dd if=${3}/SPL of=${1} bs=1K seek=1 >/dev/null 2>&1
  83. dd if=${3}/u-boot.img of=${1} bs=1K seek=42 >/dev/null 2>&1
  84. parted -a optimal -s $1 unit s mkpart primary ext2 -- 2048 $rootsize >/dev/null 2>&1
  85. parted -a optimal -s $1 unit s mkpart primary fat32 $rootsizeend $maxsize >/dev/null 2>&1
  86. sfdisk --change-id $1 2 88 >/dev/null 2>&1
  87. echo "Creating filesystem"
  88. mkfs.ext4 -q -O ^huge_file ${1}1
  89. sync
  90. tmp=$(mktemp -d)
  91. mount -t ext4 ${1}1 $tmp
  92. echo "Extracting install archive"
  93. tar -C $tmp -xzpf $2
  94. echo "Fixing permissions"
  95. chmod 1777 $tmp/tmp
  96. chmod 4755 $tmp/bin/busybox
  97. cp ${3}/*.dtb $tmp/boot/
  98. umount $tmp
  99. echo "Successfully installed."
  100. exit 0