prepare_selfinst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #!/bin/bash
  2. if [ "$(id -u)" != "0" ]; then
  3. printf "\n\033[31m\033[1mScript must be run as root !\033[22m\033[37m\n\n"
  4. exit 1
  5. fi
  6. usage() {
  7. printf "\n\033[33m"
  8. printf "USAGE: prepare_selfinst dest board [big]\n"
  9. printf " \033[1mdest\033[22m destination, block device (/dev/sdX) or image name\n"
  10. printf " \033[1mboard\033[22m Odroid board type: \033[1mc2\033[22m or \033[1mxu4\033[22m\n"
  11. printf "\033[37m\n"
  12. }
  13. needed_packages=""
  14. _testpkg=$(dpkg -l | grep parted)
  15. if [ "${_testpkg}" = "" ]; then
  16. needed_packages="${needed_packages}parted "
  17. fi
  18. _testpkg=$(dpkg -l | grep dosfstools)
  19. if [ "${_testpkg}" = "" ]; then
  20. needed_packages="${needed_packages}dosfstools "
  21. fi
  22. if [ ! "${needed_packages}" = "" ]; then
  23. printf "\n\033[31m\033[1mYou have to install \033[22m\033[37m%s !\033[37m\n\n" "${needed_packages}"
  24. exit 1
  25. fi
  26. if [ "${1}" = "" ]; then
  27. printf "\n\033[31m\033[1mdestination must be specified\033[22m\033[37m\n"
  28. usage
  29. exit 1
  30. fi
  31. if [ ! "${2}" = "xu4" ] && [ ! "${2}" = "c2" ]; then
  32. printf "\n\033[31m\033[1mOdroid board type must be \033[33mxu4\033[31m or \033[33mc2\033[31m\033[37m\n"
  33. usage
  34. exit 1
  35. fi
  36. if [ "${2}" = "c2" ]; then
  37. part_start=65536 # RESERVE 32MB AT CARD START
  38. o_device="c2"
  39. src_dir="C2"
  40. else
  41. part_start=131072 # RESERVE 64MB AT CARD START
  42. o_device="xu4"
  43. src_dir="XU4"
  44. if [ -b $sdcard ]; then
  45. printf "\n\033[33m\033[1mWARNING:\033[22m Do not use EMMC in SD adapter to prepare the card!\033[37m\n"
  46. fi
  47. fi
  48. _image_size=250
  49. sdcard=${1}
  50. #====================
  51. image_error_exit() {
  52. echo ""
  53. umount _mnt > /dev/null 2>&1
  54. rmdir _mnt > /dev/null 2>&1
  55. rm -rf tmp/* > /dev/null 2>&1
  56. rmdir tmp > /dev/null 2>&1
  57. printf "\033[31m\033[1m%s\033[37m\033[22m\n" "${1}"
  58. exit 1
  59. }
  60. #====================================================================================
  61. echo ""
  62. printf "\033[36m\033[1m\n"
  63. echo "========================================================"
  64. echo "= Preparing Odroid MultiBoot selfinstall sd card/image ="
  65. echo "========================================================"
  66. printf "\033[22m\033[37m\n"
  67. printf "\033[33mPreparing for \033[1mOdroid-%s\033[22m\033[37m\n\n" "${o_device}"
  68. # =================
  69. # Check destination
  70. # =================
  71. if [ -b $sdcard ]; then
  72. # we are working with block device
  73. # Test if requested drive is removable
  74. ISREMOVABLE=`udevadm info -a -n ${sdcard} | grep -o "ATTR{removable}==\"1\""`
  75. if [ ! "${ISREMOVABLE}" = "ATTR{removable}==\"1\"" ] ; then
  76. printf "\033[31m\033[1m%s IS NOT REMOVABLE DRIVE !, Exiting.\033[37m\033[22m\n" "${sdcard}"
  77. exit 1
  78. fi
  79. _sdok=`fdisk -l $sdcard 2> /dev/null | grep Disk`
  80. if [ "$_sdok" = "" ]; then
  81. printf "\033[31m\033[1m%s NOT FOUND !, Exiting.\033[37m\033[22m\n" "${sdcard}"
  82. exit 1
  83. fi
  84. _image_size=2048
  85. _isimage="no"
  86. else
  87. _tstname=`echo ${sdcard} | grep "/dev"`
  88. if [ ! "${_tstname}" = "" ]; then
  89. printf "\n\033[31m\033[1mWrong image name!\033[22m\033[37m\n\n"
  90. exit 1
  91. fi
  92. _isimage="yes"
  93. fi
  94. # =================================
  95. # Create partition images if needed
  96. if [ "${_isimage}" = "yes" ]; then
  97. echo "Creating image file...."
  98. dd if=/dev/zero of=${sdcard}1 bs=1M count=${_image_size} > /dev/null 2>&1
  99. dd if=/dev/zero of=${sdcard}0 bs=512 count=${part_start} > /dev/null 2>&1
  100. mkfs -t vfat -n SELFINST ${sdcard}1 > /dev/null 2>&1
  101. if [ $? -ne 0 ]; then
  102. image_error_exit " ERROR formating vfat partition."
  103. fi
  104. fi
  105. umount ${sdcard}* > /dev/null 2>&1
  106. sleep 1
  107. # ==========================================================
  108. # Partition and format sd/emmc card if not creating an image
  109. if [ ! "${_isimage}" = "yes" ]; then
  110. printf "\033[31m\033[1mWARNING: SD card \033[36m%s\033[31m WILL BE ERASED !\033[22m\033[37m, Continue (y/N)? " "${sdcard}"
  111. read -n 1 ANSWER
  112. if [ ! "${ANSWER}" = "y" ] ; then
  113. echo "."
  114. image_error_exit "Canceled.."
  115. fi
  116. echo ""
  117. echo "Erasing ${sdcard} ..."
  118. dd if=/dev/zero of=${sdcard} bs=1M count=10 oflag=direct > /dev/null 2>&1
  119. sync
  120. sleep 1
  121. partprobe -s ${sdcard} > /dev/null 2>&1
  122. if [ $? -ne 0 ]; then
  123. image_error_exit "ERROR."
  124. exit 1
  125. fi
  126. sleep 1
  127. echo "Creating new filesystem on $sdcard ..."
  128. echo -e "o\nw" | fdisk ${sdcard} > /dev/null 2>&1
  129. sync
  130. echo " New filesystem created on SD card."
  131. sleep 1
  132. partprobe -s ${sdcard} > /dev/null 2>&1
  133. if [ $? -ne 0 ]; then
  134. image_error_exit "ERROR."
  135. exit 1
  136. fi
  137. sleep 1
  138. echo ""
  139. echo "Creating installer partition on $sdcard..."
  140. echo -e "n\np\n1\n$part_start\n\nt\nc\nw" | fdisk ${sdcard} > /dev/null 2>&1
  141. sleep 1
  142. partprobe -s ${sdcard} > /dev/null 2>&1
  143. if [ $? -ne 0 ]; then
  144. image_error_exit "ERROR."
  145. exit 1
  146. fi
  147. sleep 1
  148. echo ""
  149. echo "Formating partition ..."
  150. dd if=/dev/zero of=${sdcard}1 bs=1M count=1 oflag=direct > /dev/null 2>&1
  151. sync
  152. mkfs -t vfat -F 32 -n SELFINST ${sdcard}1 > /dev/null 2>&1
  153. if [ $? -ne 0 ]; then
  154. image_error_exit " ERROR formating installer partition."
  155. fi
  156. echo " selfinst partition formated."
  157. sync
  158. echo ""
  159. echo "Copying boot section..."
  160. if [ ${o_device} = "xu4" ]; then
  161. dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=512 seek=1 > /dev/null 2>&1
  162. if [ $? -ne 0 ]; then
  163. echo "ERROR installing bl1.bin."
  164. fi
  165. sync
  166. dd if=${src_dir}/initramfs/ramfs/multi/bl2.bin of=${sdcard} bs=512 seek=31 > /dev/null 2>&1
  167. if [ $? -ne 0 ]; then
  168. echo "ERROR installing bl2.bin."
  169. fi
  170. sync
  171. dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} bs=512 seek=63 > /dev/null 2>&1
  172. if [ $? -ne 0 ]; then
  173. echo "ERROR installing u-boot.bin."
  174. fi
  175. sync
  176. dd if=${src_dir}/initramfs/ramfs/multi/tzsw.bin of=${sdcard} bs=512 seek=2111 > /dev/null 2>&1
  177. if [ $? -ne 0 ]; then
  178. echo "ERROR installing tzsw.bin."
  179. fi
  180. sync
  181. else
  182. dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=1 count=442 > /dev/null 2>&1
  183. if [ $? -ne 0 ]; then
  184. echo "ERROR installing bl1.bin."
  185. fi
  186. dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=512 skip=1 seek=1 > /dev/null 2>&1
  187. if [ $? -ne 0 ]; then
  188. echo "ERROR installing bl1.bin."
  189. fi
  190. sync
  191. dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} bs=512 seek=97 > /dev/null 2>&1
  192. if [ $? -ne 0 ]; then
  193. echo "ERROR installing u-boot.bin."
  194. fi
  195. sync
  196. # u-boot env erase
  197. dd if=/dev/zero of=${sdcard} seek=1440 count=64 > /dev/null 2>&1
  198. if [ $? -ne 0 ]; then
  199. echo "ERROR erasing u-boot environment."
  200. fi
  201. fi
  202. fi
  203. sync
  204. mkdir _mnt > /dev/null 2>&1
  205. echo ""
  206. mount ${sdcard}1 _mnt > /dev/null 2>&1
  207. if [ $? -ne 0 ]; then
  208. image_error_exit "ERROR mounting destination partition ${sdcard}1."
  209. exit 1
  210. fi
  211. echo "Copying installation files..."
  212. mkdir _mnt/multiboot > /dev/null 2>&1
  213. cp ${src_dir}/multiboot/*.multi _mnt/multiboot > /dev/null 2>&1
  214. if [ $? -ne 0 ]; then
  215. image_error_exit "ERROR installing multiboot files on ${sdcard}1."
  216. exit 1
  217. fi
  218. if [ ${o_device} = "xu4" ]; then
  219. cp ${src_dir}/multiboot/boot.scr.multi _mnt/boot.scr > /dev/null 2>&1
  220. else
  221. cp ${src_dir}/multiboot/boot.ini.multi _mnt/boot.ini > /dev/null 2>&1
  222. fi
  223. if [ $? -ne 0 ]; then
  224. image_error_exit "ERROR installing multiboot ini file on ${sdcard}1."
  225. exit 1
  226. fi
  227. umount _mnt > /dev/null 2>&1
  228. if [ $? -ne 0 ]; then
  229. echo "umount ${sdcard}1, ERROR"
  230. else
  231. rmdir _mnt
  232. fi
  233. if [ "${_isimage}" = "yes" ]; then
  234. echo "Creating final image..."
  235. dd if=${sdcard}0 of=${sdcard} > /dev/null 2>&1
  236. dd if=${sdcard}1 of=${sdcard} conv=notrunc oflag=append > /dev/null 2>&1
  237. rm ${sdcard}0 > /dev/null 2>&1
  238. rm ${sdcard}1 > /dev/null 2>&1
  239. echo -e "o\nw" | fdisk ${sdcard} > /dev/null 2>&1
  240. echo -e "n\np\n1\n$part_start\n\nt\nc\nw" | fdisk ${sdcard} > /dev/null 2>&1
  241. if [ ${o_device} = "xu4" ]; then
  242. dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=512 seek=1 > /dev/null 2>&1
  243. if [ $? -ne 0 ]; then
  244. echo "ERROR installing bl1.bin."
  245. fi
  246. sync
  247. dd if=${src_dir}/initramfs/ramfs/multi/bl2.bin of=${sdcard} conv=notrunc seek=31 > /dev/null 2>&1
  248. if [ $? -ne 0 ]; then
  249. echo "ERROR installing bl2.bin."
  250. fi
  251. sync
  252. dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} conv=notrunc seek=63 > /dev/null 2>&1
  253. if [ $? -ne 0 ]; then
  254. echo "ERROR installing u-boot.bin."
  255. fi
  256. sync
  257. dd if=${src_dir}/initramfs/ramfs/multi/tzsw.bin of=${sdcard} conv=notrunc seek=2111 > /dev/null 2>&1
  258. if [ $? -ne 0 ]; then
  259. echo "ERROR installing tzsw.bin."
  260. fi
  261. sync
  262. else
  263. dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=1 count=442 > /dev/null 2>&1
  264. if [ $? -ne 0 ]; then
  265. echo "ERROR installing bl1.bin."
  266. fi
  267. dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=512 skip=1 seek=1 > /dev/null 2>&1
  268. if [ $? -ne 0 ]; then
  269. echo "ERROR installing bl1.bin."
  270. fi
  271. sync
  272. dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} conv=notrunc bs=512 seek=97 > /dev/null 2>&1
  273. if [ $? -ne 0 ]; then
  274. echo "ERROR installing u-boot.bin."
  275. fi
  276. sync
  277. # u-boot env erase
  278. dd if=/dev/zero of=${sdcard} conv=notrunc seek=1440 count=64 > /dev/null 2>&1
  279. if [ $? -ne 0 ]; then
  280. echo "ERROR erasing u-boot environment."
  281. fi
  282. fi
  283. sleep 1
  284. fi
  285. rm -rf tmp/* > /dev/null 2>&1
  286. rmdir tmp > /dev/null 2>&1
  287. echo ""
  288. echo "OK."
  289. printf "\033[36m\033[1m\n"
  290. if [ "${_isimage}" = "yes" ]; then
  291. echo "======================================================="
  292. echo "= Odroid MultiBoot selfinstall sd card image prepared ="
  293. echo "======================================================="
  294. else
  295. echo "================================================="
  296. echo "= Odroid MultiBoot selfinstall sd card prepared ="
  297. echo "================================================="
  298. fi
  299. printf "\033[22m\033[37m\n"
  300. exit 0