adkinstall 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/mksh
  2. # This file is part of the OpenADK project.
  3. # install to ADK to a block/flash device
  4. if [ $(id -u) -ne 0 ];then
  5. print Installation is only possible as root
  6. exit 1
  7. fi
  8. if [ -z $1 ];then
  9. print give the filename of the archive you want to install
  10. exit 1
  11. fi
  12. archive=$1
  13. # get adk target system
  14. target=$(cat /etc/.adktarget)
  15. if [ -z $target ];then
  16. print autodetection of target failed
  17. exit 1
  18. fi
  19. case $target {
  20. (mikrotik-rb532)
  21. cfgfssize=32768
  22. nand=0
  23. cf=0
  24. fs=ext4
  25. while getopts "cnf:" ch; do
  26. case $ch in
  27. c)
  28. cf=1
  29. fs=ext4
  30. ;;
  31. n)
  32. nand=1
  33. fs=yaffs2
  34. ;;
  35. f)
  36. fs=$OPTARG
  37. ;;
  38. esac
  39. done
  40. shift $((OPTIND - 1))
  41. ;;
  42. (*)
  43. print target $target not supported
  44. exit 1
  45. ;;
  46. }
  47. f=0
  48. for tool in parted sfdisk mkfs.$fs;do
  49. if ! which $tool >/dev/null; then
  50. echo "Checking if $tool is installed... failed"
  51. f=1
  52. fi
  53. done
  54. if [ $f -eq 1 ];then exit 1;fi
  55. # create empty partition table
  56. function create_label {
  57. print "Creating empty partition table"
  58. parted -s $1 mklabel msdos
  59. }
  60. # get max size of disk in sectors
  61. function get_max_size {
  62. maxsize=$(env LC_ALL=C parted $1 -s unit s print |awk '/^Disk/ { print $3 }'|sed -e 's/s//')
  63. rootsize=$(($maxsize-$cfgfssize))
  64. print device has $maxsize sectors. Using $rootsize for root.
  65. }
  66. # create partition, with fstype start and end in sectors
  67. function create_partition {
  68. print creating partition on $1 for filesystem $2
  69. parted -s $1 unit s mkpart primary $2 $3 $4
  70. }
  71. function set_boot_flag {
  72. print setting bootflag on $1 partition $2
  73. parted -s $1 set $2 boot on
  74. }
  75. function change_part_type {
  76. print setting partition type on $1 partition $2 to $3
  77. sfdisk --change-id $1 $2 $3 >/dev/null 2>&1
  78. }
  79. function create_filesystem {
  80. print creating filesystem $2 on $1 partition $3
  81. mkfs.$fs -q ${1}${3}
  82. }
  83. function mount_fs {
  84. print mounting ${1}${2} to $4 with filesystem $3
  85. mount -t $3 ${1}${2} $4
  86. }
  87. function extract_archive {
  88. print extracting archive $1 onto $2
  89. tar -C $2 -xpf $1
  90. }
  91. function fix_perm {
  92. print fixing permissions
  93. chmod 1777 ${1}/tmp
  94. chmod 4755 ${1}/bin/busybox
  95. }
  96. case $target {
  97. (mikrotik-rb532)
  98. if (( cf )); then
  99. get_max_size /dev/sda
  100. create_label /dev/sda
  101. create_partition /dev/sda ext2 0 1
  102. create_partition /dev/sda ext2 1 $rootsize
  103. create_partition /dev/sda ext2 $rootsize $maxsize
  104. set_boot_flag /dev/sda 1
  105. change_part_type /dev/sda 1 27
  106. change_part_type /dev/sda 3 88
  107. create_filesystem /dev/sda $fs 2
  108. mount_fs /dev/sda 2 $fs /mnt
  109. extract_archive $archive /mnt
  110. print installing kernel to cf disk /dev/sda1
  111. dd if=/mnt/boot/kernel of=/dev/sda1 bs=2048 >/dev/null 2>&1
  112. fix_perm /mnt
  113. umount /mnt
  114. fi
  115. if (( nand )); then
  116. mount_fs /dev/mtdblock 1 $fs /mnt
  117. rm -rf /mnt/* >/dev/null 2>&1
  118. mkdir /mnt/boot
  119. mount_fs /dev/mtdblock 0 $fs /mnt/boot
  120. extract_archive $archive /mnt
  121. fix_perm /mnt
  122. umount /mnt/boot
  123. umount /mnt
  124. fi
  125. ;;
  126. }
  127. echo "Successfully installed OpenADK on $target."
  128. exit 0