prepare_selfinst 10 KB

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