| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- #!/bin/bash
- if [ "$(id -u)" != "0" ]; then
- printf "\n\033[31m\033[1mScript must be run as root !\033[22m\033[37m\n\n"
- exit 1
- fi
- usage() {
- printf "\n\033[33m"
- printf "USAGE: prepare_selfinst dest board [emmc]\n"
- printf " \033[1mdest\033[22m destination, block device (/dev/sdX) or image name\n"
- printf " \033[1mboard\033[22m Odroid board type: \033[1mc2\033[22m or \033[1mxu4\033[22m\n"
- printf " \033[1memmc\033[22m Prepare EMMC card, only valid for Odroid-C2, default is SD\n"
- printf "\033[37m\n"
- }
- needed_packages=""
- _testpkg=$(dpkg -l | grep parted)
- if [ "${_testpkg}" = "" ]; then
- needed_packages="${needed_packages}parted "
- fi
- _testpkg=$(dpkg -l | grep dosfstools)
- if [ "${_testpkg}" = "" ]; then
- needed_packages="${needed_packages}dosfstools "
- fi
- if [ ! "${needed_packages}" = "" ]; then
- printf "\n\033[31m\033[1mYou have to install \033[22m\033[37m%s !\033[37m\n\n" "${needed_packages}"
- exit 1
- fi
- if [ "${1}" = "" ]; then
- printf "\n\033[31m\033[1mdestination must be specified\033[22m\033[37m\n"
- usage
- exit 1
- fi
- if [ ! "${2}" = "xu4" ] && [ ! "${2}" = "c2" ]; then
- printf "\n\033[31m\033[1mOdroid board type must be \033[33mxu4\033[31m or \033[33mc2\033[31m\033[37m\n"
- usage
- exit 1
- fi
- emmc_install=""
- emmc_disp=""
- if [ "${2}" = "c2" ]; then
- part_start=65536 # RESERVE 32MB AT CARD START
- o_device="c2"
- src_dir="C2"
- if [ "${3}" = "emmc" ] || [ "${3}" = "EMMC" ]; then
- emmc_install="yes"
- emmc_disp="(EMMC card)"
- else
- emmc_install="no"
- emmc_disp="(SD card)"
- fi
- else
- part_start=131072 # RESERVE 64MB AT CARD START
- o_device="xu4"
- src_dir="XU4"
- if [ -b $sdcard ]; then
- printf "\n\033[33m\033[1mWARNING:\033[22m Do not use EMMC in SD adapter to prepare the card!\033[37m\n"
- fi
- fi
- _image_size=250
- sdcard=${1}
- #====================
- image_error_exit() {
- echo ""
- umount _mnt > /dev/null 2>&1
- rmdir _mnt > /dev/null 2>&1
- rm -rf tmp/* > /dev/null 2>&1
- rmdir tmp > /dev/null 2>&1
- printf "\033[31m\033[1m%s\033[37m\033[22m\n" "${1}"
- exit 1
- }
- #====================================================================================
- echo ""
- printf "\033[36m\033[1m\n"
- echo "========================================================"
- echo "= Preparing Odroid MultiBoot selfinstall sd card/image ="
- echo "========================================================"
- printf "\033[22m\033[37m\n"
- printf "\033[33mPreparing for \033[1mOdroid-%s %s\033[22m\033[37m\n\n" "${o_device}" "${emmc_disp}"
- # =================
- # Check destination
- # =================
- if [ -b $sdcard ]; then
- # we are working with block device
- # Test if requested drive is removable
- ISREMOVABLE=`udevadm info -a -n ${sdcard} | grep -o "ATTR{removable}==\"1\""`
- if [ ! "${ISREMOVABLE}" = "ATTR{removable}==\"1\"" ] ; then
- printf "\033[31m\033[1m%s IS NOT REMOVABLE DRIVE !, Exiting.\033[37m\033[22m\n" "${sdcard}"
- exit 1
- fi
- _sdok=`fdisk -l $sdcard 2> /dev/null | grep Disk`
- if [ "$_sdok" = "" ]; then
- printf "\033[31m\033[1m%s NOT FOUND !, Exiting.\033[37m\033[22m\n" "${sdcard}"
- exit 1
- fi
- _image_size=2048
- _isimage="no"
- else
- _tstname=`echo ${sdcard} | grep "/dev"`
- if [ ! "${_tstname}" = "" ]; then
- printf "\n\033[31m\033[1mWrong image name!\033[22m\033[37m\n\n"
- exit 1
- fi
- _isimage="yes"
- fi
- # =================================
- # Create partition images if needed
- if [ "${_isimage}" = "yes" ]; then
- echo "Creating image file...."
- dd if=/dev/zero of=${sdcard}1 bs=1M count=${_image_size} > /dev/null 2>&1
- dd if=/dev/zero of=${sdcard}0 bs=512 count=${part_start} > /dev/null 2>&1
- mkfs -t vfat -n SELFINST ${sdcard}1 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- image_error_exit " ERROR formating vfat partition."
- fi
- fi
- umount ${sdcard}* > /dev/null 2>&1
- sleep 1
- # ==========================================================
- # Partition and format sd/emmc card if not creating an image
- if [ ! "${_isimage}" = "yes" ]; then
- printf "\033[31m\033[1mWARNING: SD card \033[36m%s\033[31m WILL BE ERASED !\033[22m\033[37m, Continue (y/N)? " "${sdcard}"
- read -n 1 ANSWER
- if [ ! "${ANSWER}" = "y" ] ; then
- echo "."
- image_error_exit "Canceled.."
- fi
- echo ""
- echo "Erasing ${sdcard} ..."
- dd if=/dev/zero of=${sdcard} bs=1M count=10 oflag=direct > /dev/null 2>&1
- sync
- sleep 1
- partprobe -s ${sdcard} > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- image_error_exit "ERROR."
- exit 1
- fi
- sleep 1
- echo "Creating new filesystem on $sdcard ..."
- echo -e "o\nw" | fdisk ${sdcard} > /dev/null 2>&1
- sync
- echo " New filesystem created on SD card."
- sleep 1
- partprobe -s ${sdcard} > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- image_error_exit "ERROR."
- exit 1
- fi
- sleep 1
- echo ""
- echo "Creating installer partition on $sdcard..."
- echo -e "n\np\n1\n$part_start\n\nt\nc\nw" | fdisk ${sdcard} > /dev/null 2>&1
- sleep 1
- partprobe -s ${sdcard} > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- image_error_exit "ERROR."
- exit 1
- fi
- sleep 1
- echo ""
- echo "Formating partition ..."
- dd if=/dev/zero of=${sdcard}1 bs=1M count=1 oflag=direct > /dev/null 2>&1
- sync
- mkfs -t vfat -F 32 -n SELFINST ${sdcard}1 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- image_error_exit " ERROR formating installer partition."
- fi
- echo " selfinst partition formated."
- sync
- echo ""
- echo "Copying boot section..."
- if [ ${o_device} = "xu4" ]; then
- dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=512 seek=1 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing bl1.bin."
- fi
- sync
- dd if=${src_dir}/initramfs/ramfs/multi/bl2.bin of=${sdcard} bs=512 seek=31 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing bl2.bin."
- fi
- sync
- dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} bs=512 seek=63 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing u-boot.bin."
- fi
- sync
- dd if=${src_dir}/initramfs/ramfs/multi/tzsw.bin of=${sdcard} bs=512 seek=2111 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing tzsw.bin."
- fi
- sync
- else
- dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=1 count=442 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing bl1.bin."
- fi
- dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} bs=512 skip=1 seek=1 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing bl1.bin."
- fi
- sync
- dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} bs=512 seek=97 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing u-boot.bin."
- fi
- sync
- # u-boot env erase
- dd if=/dev/zero of=${sdcard} seek=1440 count=64 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR erasing u-boot environment."
- fi
- fi
- fi
- sync
- mkdir _mnt > /dev/null 2>&1
- echo ""
- mount ${sdcard}1 _mnt > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- image_error_exit "ERROR mounting destination partition ${sdcard}1."
- exit 1
- fi
- echo "Copying installation files..."
- mkdir _mnt/multiboot > /dev/null 2>&1
- cp ${src_dir}/multiboot/*.multi _mnt/multiboot > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- image_error_exit "ERROR installing multiboot files on ${sdcard}1."
- exit 1
- fi
- if [ ${o_device} = "xu4" ]; then
- cp ${src_dir}/multiboot/boot.scr.multi _mnt/boot.scr > /dev/null 2>&1
- else
- if [ "${emmc_install}" = "yes" ]; then
- sed -i "s/setenv CARD_TYPE SD/setenv CARD_TYPE EMMC/g" _mnt/multiboot/boot.ini.multi > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- image_error_exit "ERROR modifying multiboot ini file on ${sdcard}1."
- exit 1
- fi
- fi
- cp _mnt/multiboot/boot.ini.multi _mnt/boot.ini > /dev/null 2>&1
- fi
- if [ $? -ne 0 ]; then
- image_error_exit "ERROR installing multiboot ini file on ${sdcard}1."
- exit 1
- fi
- umount _mnt > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "umount ${sdcard}1, ERROR"
- else
- rmdir _mnt
- fi
- if [ "${_isimage}" = "yes" ]; then
- echo "Creating final image..."
- dd if=${sdcard}0 of=${sdcard} > /dev/null 2>&1
- dd if=${sdcard}1 of=${sdcard} conv=notrunc oflag=append > /dev/null 2>&1
- rm ${sdcard}0 > /dev/null 2>&1
- rm ${sdcard}1 > /dev/null 2>&1
- echo -e "o\nw" | fdisk ${sdcard} > /dev/null 2>&1
- echo -e "n\np\n1\n$part_start\n\nt\nc\nw" | fdisk ${sdcard} > /dev/null 2>&1
- if [ ${o_device} = "xu4" ]; then
- dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=512 seek=1 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing bl1.bin."
- fi
- sync
- dd if=${src_dir}/initramfs/ramfs/multi/bl2.bin of=${sdcard} conv=notrunc seek=31 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing bl2.bin."
- fi
- sync
- dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} conv=notrunc seek=63 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing u-boot.bin."
- fi
- sync
- dd if=${src_dir}/initramfs/ramfs/multi/tzsw.bin of=${sdcard} conv=notrunc seek=2111 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing tzsw.bin."
- fi
- sync
- else
- dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=1 count=442 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing bl1.bin."
- fi
- dd if=${src_dir}/initramfs/ramfs/multi/bl1.bin of=${sdcard} conv=notrunc bs=512 skip=1 seek=1 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing bl1.bin."
- fi
- sync
- dd if=${src_dir}/initramfs/ramfs/multi/u-boot.bin of=${sdcard} conv=notrunc bs=512 seek=97 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR installing u-boot.bin."
- fi
- sync
- # u-boot env erase
- dd if=/dev/zero of=${sdcard} conv=notrunc seek=1440 count=64 > /dev/null 2>&1
- if [ $? -ne 0 ]; then
- echo "ERROR erasing u-boot environment."
- fi
- fi
- sleep 1
- fi
- rm -rf tmp/* > /dev/null 2>&1
- rmdir tmp > /dev/null 2>&1
- echo ""
- echo "OK."
- printf "\033[36m\033[1m\n"
- if [ "${_isimage}" = "yes" ]; then
- echo "======================================================="
- echo "= Odroid MultiBoot selfinstall sd card image prepared ="
- echo "======================================================="
- else
- echo "================================================="
- echo "= Odroid MultiBoot selfinstall sd card prepared ="
- echo "================================================="
- fi
- printf "\033[22m\033[37m\n"
- exit 0
|