prepare_selfinst 12 KB

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