#!/bin/bash # === Detect instalation source === echo "1 1 1 1" > /proc/sys/kernel/printk mkdir /mnt > /dev/null 2>&1 mkdir /multiboot > /dev/null 2>&1 mkdir /run/inst > /dev/null 2>&1 #------------------------------------------------------- print_header() { clear printf "\n\033[36m\033[1m\n" printf " ======================================\n" printf " = Jeedom installer %s =\n" "${SCRIPT_VERSION}" printf " ======================================\n" printf "\033[22m\033[37m\n" } #------------------------------------------------------- print_header # Set variables CARD1="" CARD2="" CARD1_EMMC="" CARD2_EMMC="" DEST_CARD="" DEST_EMMC="" INST_ANDROID="" INST_OELEC="" INST_LINUX="" INST_LINUX2="" A_FROMDIR="" L_FROMDIR="" L2_FROMDIR="" O_FROMDIR="" SYSTEM_SIZE=1024 SYSTEM_MINSIZE=768 DATA_SIZE=1024 DATA_MINSIZE=1024 CACHE_SIZE=256 CACHE_MINSIZE=128 USERDATA_SIZE=1024 USERDATA_MINSIZE=512 LINUX_SIZE=2048 LINUX_MINSIZE=2048 OELEC_SIZE=512 OELEC_MINSIZE=512 LINUX_FSTYPE="ext4" _dest_system="/dev/disk/by-label/sdsystem" _dest_cache="/dev/disk/by-label/sdcache" _dest_data="/dev/disk/by-label/sddata" _erase_data="no" _part6_label="auxpart" _part6_dlabel="Reserved" _instfound="no" andro_updatearchive="no" _inst_mbkernel="no" _inst_no_=0 #----------------------------------------------------- # Check card type #---------------- CARD1="/dev/mmcblk${BOOT_CARDDEV}" CARD1_EMMC="${BOOT_CARDTYPE}" if [ "${NUMCARDS}" = "2" ]; then CARD2="/dev/mmcblk${OTHER_CARDDEV}" CARD2_EMMC="${OTHER_CARDTYPE}" fi #----------------------------------------------------- #----------------------------------------------------- # Check where the installation files are located check_source() { if [ -b ${1} ]; then /bin/mountx ${1} /mnt > /dev/null 2>&1 [ $? -ne 0 ] && return 1 else return 1 fi sleep 1 # === Source drive found and 1st partition mounted on /mnt === # === Check installation files on source drive === # LINUX if [ -d /mnt/${BKP_PREFIX}_backup/linux ]; then if [ -f /mnt/${BKP_PREFIX}_backup/linux/jeedom.img ]; then INST_LINUX="yes" _instfound="yes" L_FROMDIR="yes" fi else if [ -f /mnt/linux.img ]; then INST_LINUX="yes" _instfound="yes" fi fi [ "${_instfound}" = "no" ] && return 1 return 0 } #----------------------------------------------------- #---------------------------------------------------------------------------------------------------- exit_script() { local _delay=60 local B="X" echo "" while [ $_delay -gt 0 ]; do printf " \033[33mPress \033[1mE\033[22m to exit to main menu (%s) \033[37m\r" "${_delay}" if read -n 1 -t 1 -s B; then _delay=60 if [ "${B}" = "E" ] || [ "${B}" = "e" ]; then break elif [ "${B}" = "s" ]; then # SCREENSHOT === fbcat > /install_exit.ppm fi else let _delay=_delay-1 fi done printf "\r\033[2K" umount /mnt1 > /dev/null 2>&1 umount /mnt2 > /dev/null 2>&1 umount /mnt > /dev/null 2>&1 udevadm trigger --action=add > /dev/null 2>&1 exit 0 } #---------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------- check_install_sources() { _inst_no_=0 if [ ! "${INST_ANDROID}" = "yes" ]; then SYSTEM_MINSIZE=8 DATA_MINSIZE=8 SYSTEM_SIZE=8 DATA_SIZE=8 CACHE_SIZE=8 CACHE_MINSIZE=8 USERDATA_MINSIZE=256 USERDATA_SIZE=256 else let _inst_no_=_inst_no_+1 fi if [ ! "${INST_OELEC}" = "yes" ] && [ ! "${INST_LINUX2}" = "yes" ]; then # Partition 6 is reserved partition OELEC_MINSIZE=8 OELEC_SIZE=256 else if [ "${INST_OELEC}" = "yes" ]; then # Partition 6 is oelec partition OELEC_MINSIZE=256 OELEC_SIZE=512 if [ $USERDATA_MINSIZE -lt 384 ]; then USERDATA_MINSIZE=384 USERDATA_SIZE=384 fi let _inst_no_=_inst_no_+1 elif [ "${INST_LINUX2}" = "yes" ]; then # Partition 6 is Linux2 OELEC_MINSIZE=1563 OELEC_SIZE=1536 let _inst_no_=_inst_no_+1 fi fi if [ ! "${INST_LINUX}" = "yes" ]; then LINUX_MINSIZE=8 LINUX_SIZE=8 else let _inst_no_=_inst_no_+1 fi } #--------------------------------------------------------------------------- print_header # =============================================================== # === Copy multiboot files from boot drive to RAMfs directory === # =============================================================== mount -t vfat /dev/mmcblk${BOOT_CARDDEV}p1 /mnt > /dev/null 2>&1 if [ $? -ne 0 ]; then printf " ERROR mounting boot card" exit_script fi cp -rf /mnt/multiboot/* /multiboot > /dev/null 2>&1 if [ $? -ne 0 ]; then printf " ERROR copying multiboot files" exit_script fi umount /mnt > /dev/null 2>&1 if [ $? -ne 0 ]; then printf " ERROR unmounting boot card" exit_script fi sleep 1 # =============================================================== #================================================= printf " Searching for installation drive ... " # === Set default dest card to NON BOOT if exists === DEST_CARD=${CARD1} DEST_EMMC=${CARD1_EMMC} if [ "${NUMCARDS}" = "2" ]; then DEST_CARD=${CARD2} DEST_EMMC=${CARD2_EMMC} fi _instfound="no" #---------------------------------------------------- for src_drive in a b c do for src_part in 1 2 3 4 do check_source "/dev/sd${src_drive}${src_part}" [ "${_instfound}" = "yes" ] && break done [ "${_instfound}" = "yes" ] && break done if [ "${_instfound}" = "no" ]; then printf " \033[33m\033[1mNOT FOUND, aborted\033[22m\033[37m\n" exit_script fi #---------------------------------------------------- print_header printf " \033[33mInstallation sources found on \033[1msd%s%s\033[22m for:\033[32m\n" "${src_drive}${src_part}" print_header check_install_sources if [ $_inst_no_ -eq 0 ]; then printf " \033[33m\033[1mNOTHING TO INSTALL\033[22m\033[37m\n" exit_script fi #================================================= #================================================= if [ ! "${O_BOARD}" = "XU" ]; then part_start=65536 # RESERVE 32M AT CARD START else part_start=8192 #131072 # RESERVE 64MB AT CARD START fi PARTSIZE_INC=128 export CARD_SIZE=8000 export source_mount="/mnt" rm /mnt/install.log > /dev/null 2>&1 log_file="/mnt/install.log" #log_file="ls/dev/null" #--------------------------------------------------------------------------------- # Detect and set installation card size, leave 32M at end unused set_cardsize() { _card_sectno=`fdisk -l ${DEST_CARD} | grep "Disk ${DEST_CARD}" | awk '{print $7}'` CARD_SIZE=$(( ( $_card_sectno - $part_start - 65536 ) / 2048)) } #--------------------------------------------------------------------------------- #-------------------------------------------------------------------------------------------------------- # Set linux or userdata partition size to fill card size set_auto_partsize() { LINUX_SIZE=$(( $CARD_SIZE - $USERDATA_SIZE - $SYSTEM_SIZE - $DATA_SIZE -$CACHE_SIZE - $OELEC_SIZE)) if [ "${LINUX_SIZE}" -lt "${LINUX_MINSIZE}" ]; then LINUX_SIZE=${LINUX_MINSIZE} return 1 else return 0 fi } #-------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------------------------- print_settings() { set_auto_partsize printf " Install:" if [ "${INST_LINUX}" = "yes" ]; then printf " \033[36mJeedom\033[37m from " if [ "${L_FROMDIR}" = "yes" ]; then printf "backup" else printf "image" fi fi printf "\033[37m\n" printf " Install to: \033[36m%s Card (%s)\033[37m (%sM)\033[37m\n\n" "${DEST_EMMC}" "${DEST_CARD}" "${CARD_SIZE}" printf " Card size: \033[36m%sM\033[37m\n" "${CARD_SIZE}" printf " userdata partition size: \033[36m%sM\033[37m\n" "${USERDATA_SIZE}" printf " system partition size: \033[36m%sM\033[37m\n" "${SYSTEM_SIZE}" printf " data partition size: \033[36m%sM\033[37m\n" "${DATA_SIZE}" printf " cache partition size: \033[36m%sM\033[37m\n" "${CACHE_SIZE}" printf " %s partition size: \033[36m%sM\033[37m\n" "${_part6_dlabel}" "${OELEC_SIZE}" printf " linux partition size: \033[36m%sM\033[37m\n" "${LINUX_SIZE}" if [ "${INST_LINUX}" = "yes" ]; then printf " linux file system type: \033[36m%s\033[37m\n" "${LINUX_FSTYPE}" [ "${O_BOARD}" = "C2" ] && printf " install multiboot kernel: \033[36m%s\033[37m\n" "${_inst_mbkernel}" fi printf "\033[36m\n" [ "${NUMCARDS}" = "2" ] && printf " \033[32m\033[1m1\033[22m Change installation \033[1mcard\033[22m\033[36m\n" printf " \033[32m\033[1m2\033[22m Change \033[1muserdata\033[22m partition size\033[36m\n" printf " \033[32m\033[1m3\033[22m Change \033[1msystem\033[22m partition size\033[36m\n" printf " \033[32m\033[1m4\033[22m Change \033[1mdata\033[22m partition size\033[36m\n" printf " \033[32m\033[1m5\033[22m Change \033[1mcache\033[22m partition size\033[36m\n" printf " \033[32m\033[1m6\033[22m Change \033[1m%s\033[22m partition size\033[36m\n" "${_part6_dlabel}" if [ "${INST_LINUX}" = "yes" ]; then printf " \033[32m\033[1mt\033[22m Change Linux \033[1mfile system type\033[22m\033[36m\n" if [ "${O_BOARD}" = "C2" ]; then if [ "${_inst_mbkernel}" = "yes" ]; then printf " \033[32m\033[1mM\033[22m Install \033[1moriginal Linux kernel\033[22m\033[36m\n" else printf " \033[32m\033[1mM\033[22m Install \033[1mmultiboot Linux kernel\033[22m\033[36m\n" fi fi fi printf "\n \033[35m\033[1mI\033[22m Start installation\033[36m\n" printf " \033[33m\033[1mE\033[22m EXIT\033[37m\n\n" } #--------------------------------------------------------------------------------------------------------------------------- #---- Select destination card ------------------------------------------------------------------------- set_dest() { printf " Press \033[35m+\033[37m key to change installation card, \033[35m9\033[37m to select\n\n" _wait_delay=30 while [ $_wait_delay -gt 0 ]; do printf " Install to: \033[36m%s Card (%s)\033[37m \r" "${DEST_EMMC}" "${DEST_CARD}" if read -n 1 -t 1 -s A; then _wait_delay=30 if [ "${A}" = "+" ]; then if [ "${NUMCARDS}" = "2" ]; then if [ "${DEST_CARD}" = "${CARD1}" ]; then DEST_CARD=${CARD2} DEST_EMMC=${CARD2_EMMC} elif [ "${DEST_CARD}" = "${CARD2}" ]; then DEST_CARD=${CARD1} DEST_EMMC=${CARD1_EMMC} fi elif [ ! "${O_BOARD}" = "XU" ]; then if [ "${DEST_EMMC}" = "SD" ]; then DEST_EMMC="EMMC" else DEST_EMMC="SD" fi fi elif [ "${A}" = "9" ]; then set_cardsize break fi else let _wait_delay=_wait_delay-1 fi printf "\r" done } #----------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------------------------------- set_systemsize() { printf " Press \033[35m+\033[37m|\033[35m-\033[37m key to change system partition size, \033[35m9\033[37m to select\n\n" _wait_delay=30 while [ $_wait_delay -gt 0 ]; do printf " system partition size: \033[36m%sM\033[37m \r" "${SYSTEM_SIZE}" if read -n 1 -t 1 -s A; then _wait_delay=30 if [ "${A}" = "+" ]; then let SYSTEM_SIZE=SYSTEM_SIZE+PARTSIZE_INC [ ${SYSTEM_SIZE} -eq 136 ] && SYSTEM_SIZE=$PARTSIZE_INC set_auto_partsize if [ $? -ne 0 ]; then let SYSTEM_SIZE=SYSTEM_SIZE-PARTSIZE_INC set_auto_partsize fi elif [ "${A}" = "-" ]; then let SYSTEM_SIZE=SYSTEM_SIZE-${PARTSIZE_INC} [ $SYSTEM_SIZE -lt $SYSTEM_MINSIZE ] && SYSTEM_SIZE=$SYSTEM_MINSIZE set_auto_partsize elif [ "${A}" = "9" ]; then break fi else let _wait_delay=_wait_delay-1 fi printf "\r" done } #---------------------------------------------------------------------------------------------------------------------------- #-------------------------------------------------------------------------------------------------------------------------- set_datasize() { printf " Press \033[35m+\033[37m|\033[35m-\033[37m key to change data partition size, \033[35m9\033[37m to select\n\n" _wait_delay=30 while [ $_wait_delay -gt 0 ]; do printf " data partition size: \033[36m%sM\033[37m \r" "${DATA_SIZE}" if read -n 1 -t 1 -s A; then _wait_delay=30 if [ "${A}" = "+" ]; then let DATA_SIZE=DATA_SIZE+PARTSIZE_INC [ ${DATA_SIZE} -eq 136 ] && DATA_SIZE=$PARTSIZE_INC set_auto_partsize if [ $? -ne 0 ]; then let DATA_SIZE=DATA_SIZE-PARTSIZE_INC set_auto_partsize fi elif [ "${A}" = "-" ]; then let DATA_SIZE=DATA_SIZE-PARTSIZE_INC [ $DATA_SIZE -lt $DATA_MINSIZE ] && DATA_SIZE=$DATA_MINSIZE set_auto_partsize elif [ "${A}" = "9" ]; then break fi else let _wait_delay=_wait_delay-1 fi printf "\r" done } #-------------------------------------------------------------------------------------------------------------------------- #------------------------------------------------------------------------------------------------------------------------------ set_userdatasize() { printf " Press \033[35m+\033[37m|\033[35m-\033[37m key to change userdata partition size, \033[35m9\033[37m to select\n\n" _wait_delay=30 while [ $_wait_delay -gt 0 ]; do printf " userdata partition size: \033[36m%sM\033[37m \r" "${USERDATA_SIZE}" if read -n 1 -t 1 -s A; then _wait_delay=30 if [ "${A}" = "+" ]; then let USERDATA_SIZE=USERDATA_SIZE+PARTSIZE_INC [ ${USERDATA_SIZE} -eq 136 ] && USERDATA_SIZE=$PARTSIZE_INC set_auto_partsize if [ $? -ne 0 ]; then let USERDATA_SIZE=USERDATA_SIZE-PARTSIZE_INC set_auto_partsize fi elif [ "${A}" = "-" ]; then let USERDATA_SIZE=USERDATA_SIZE-PARTSIZE_INC [ $USERDATA_SIZE -lt $USERDATA_MINSIZE ] && USERDATA_SIZE=$USERDATA_MINSIZE set_auto_partsize elif [ "${A}" = "9" ]; then break fi else let _wait_delay=_wait_delay-1 fi printf "\r" done } #------------------------------------------------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------------------------------------------------ set_cachesize() { printf " Press \033[35m+\033[37m|\033[35m-\033[37m key to change cache partition size, \033[35m9\033[37m to select\n\n" _wait_delay=30 while [ $_wait_delay -gt 0 ]; do printf " cache partition size: \033[36m%sM\033[37m \r" "${CACHE_SIZE}" if read -n 1 -t 1 -s A; then _wait_delay=30 if [ "${A}" = "+" ]; then let CACHE_SIZE=CACHE_SIZE+PARTSIZE_INC [ ${CACHE_SIZE} -eq 136 ] && CACHE_SIZE=$PARTSIZE_INC set_auto_partsize if [ $? -ne 0 ]; then let CACHE_SIZE=CACHE_SIZE-PARTSIZE_INC set_auto_partsize fi elif [ "${A}" = "-" ]; then let CACHE_SIZE=CACHE_SIZE-PARTSIZE_INC [ ${CACHE_SIZE} -lt $CACHE_MINSIZE ] && CACHE_SIZE=$CACHE_MINSIZE set_auto_partsize elif [ "${A}" = "9" ]; then break fi else let _wait_delay=_wait_delay-1 fi printf "\r" done } #------------------------------------------------------------------------------------------------------------------------------ #------------------------------------------------------------------------------------------------ create_partitions_full() { # == MULTI BOOT PARTITION LAYOUT === # userdata # android system # android cache # extended # android data # oelec/linux2 # linux _userdata_size=$(( $USERDATA_SIZE * 2048 )) _system_size=$(( $SYSTEM_SIZE * 2048 )) _cache_size=$(( $CACHE_SIZE * 2048 )) userpart_end=$(( $_userdata_size + $part_start - 1 )) syspart_start=$(( $userpart_end + 1)) syspart_end=$(( $syspart_start + $_system_size - 1 )) cachepart_start=$(( $syspart_end + 1 )) cachepart_end=$(( $cachepart_start + $_cache_size - 1 )) extpart_start=$(( $cachepart_end + 1)) dd if=/dev/zero of=${DEST_CARD} bs=512 seek=${extpart_start} count=131072 >> ${log_file} 2>&1 sync echo "n p 1 $part_start $userpart_end t c n p 2 $syspart_start $syspart_end n p 3 $cachepart_start $cachepart_end n e $extpart_start n +${DATA_SIZE}M n +${OELEC_SIZE}M n +${LINUX_SIZE}M w" | fdisk ${DEST_CARD} >> ${log_file} 2>&1 if [ $? -ne 0 ]; then printf " fdisk ERROR " return 1 fi return 0 } #------------------------------------------------------------------------------------------------ #---------------------------------------------------------------------------------------- # Partition and format installation card prepare_card() { echo "==== PREPARE CARD ====" >> ${log_file} printf " Partitioning %s card " "${DEST_EMMC}" # ---- Erase card ------------------------------------------------------- /bin/dd if=/dev/zero of=${DEST_CARD} bs=512 count=${part_start} >> ${log_file} 2>&1 sync sleep 1 /bin/partprobe -s ${DEST_CARD} >> ${log_file} 2>&1 sleep 1 #---- Make new partition table -------------------------- echo -e "o\nw" | /bin/fdisk ${DEST_CARD} >> ${log_file} 2>&1 sync sleep 1 /bin/partprobe -s ${DEST_CARD} >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo " ERROR creating partition table." return 1 fi sleep 1 create_partitions_full [ $? -ne 0 ] && return 1 sleep 2 /bin/partprobe -s ${DEST_CARD} >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo " ERROR creating partitions." return 1 fi sleep 1 sync echo "OK" sleep 1 # *** FORMATING partitions *************************************** printf " Formating partitions... " # === USERDATA/BOOT ======================================================= printf "\033[36muserdata\033[37m... " dd if=/dev/zero of=${DEST_CARD}p1 bs=512 count=2048 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR" return 1 fi sync mkfs -t vfat -F 32 -n ${label_pref}userdata ${DEST_CARD}p1 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "mkfs ERROR" return 1 fi sync sleep 1 if [ "${INST_LINUX}" = "yes" ]; then # === linux ========================================================================= printf "\033[36mJEEDOM\033[37m... " dd if=/dev/zero of=${DEST_CARD}p7 bs=512 count=2048 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR" return 1 fi sync if [ "${LINUX_FSTYPE}" = "btrfs" ] ; then mkfs.btrfs -f -L ${label_pref}linux ${DEST_CARD}p7 >> ${log_file} 2>&1 else mkfs.ext4 -L ${label_pref}linux ${DEST_CARD}p7 >> ${log_file} 2>&1 fi if [ $? -ne 0 ]; then echo "mkfs ERROR" return 1 fi sync sleep 1 fi /bin/partprobe -s ${DEST_CARD} >> ${log_file} 2>&1 echo "OK" return 0 } #---------------------------------------------------------------------------------------- #---------------------------------------------------------------------------- # copy Odroid boot files to installation card install_boot_c2() { echo "==== INSTALL BOOT ====" >> ${log_file} printf " Installing Odroid boot files ... " dd if=/multi/bl1.bin of=${DEST_CARD} bs=1 count=442 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing bl1.bin." return 1 fi dd if=/multi/bl1.bin of=${DEST_CARD} bs=512 skip=1 seek=1 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing bl1.bin." return 1 fi sync dd if=/multi/u-boot.bin of=${DEST_CARD} bs=512 seek=97 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing u-boot.bin." return 1 fi sync echo "OK" sleep 1 } #---------------------------------------------------------------------------- #---------------------------------------------------------------------------- # copy Odroid boot files to installation card install_boot_c1() { echo "==== INSTALL BOOT ====" >> ${log_file} printf " Installing Odroid boot files ... " dd if=/multi/bl1.bin of=${DEST_CARD} bs=1 count=442 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing bl1.bin." return 1 fi dd if=/multi/bl1.bin of=${DEST_CARD} bs=512 skip=1 seek=1 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing bl1.bin." return 1 fi sync dd if=/multi/u-boot.bin of=${DEST_CARD} bs=512 seek=64 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing u-boot.bin." return 1 fi sync echo "OK" sleep 1 } #---------------------------------------------------------------------------- #---------------------------------------------------------------------------- # copy Odroid boot files to installation card install_boot_xu() { if [ "${label_pref}" = "sd" ]; then printf " Installing Odroid boot files to SD Card ... " dd if=/multi/bl1.bin of=${DEST_CARD} bs=512 seek=1 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing bl1.bin." return 1 fi sync dd if=/multi/bl2.bin of=${DEST_CARD} seek=31 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing bl2.bin." return 1 fi sync dd if=/multi/u-boot.bin of=${DEST_CARD} seek=63 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing u-boot.bin." return 1 fi sync dd if=/multi/tzsw.bin of=${DEST_CARD} seek=2111 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing tzsw.bin." return 1 fi sync echo "OK" else # UNLOCK boot0 _dest_card_=$(basename ${DEST_CARD}) printf " Installing Odroid boot files to EMMC Card (%s [%s])... " "${DEST_CARD}" "${_dest_card_}" if [ -e /sys/block/${_dest_card_}boot0/force_ro ]; then echo 0 > /sys/block/${_dest_card_}boot0/force_ro if [ $? -ne 0 ]; then echo "ERROR enabling boot0." return 1 fi else echo "ERROR: boot0 not found." return 1 fi dd if=/dev/zero of=${DEST_CARD}boot0 count=8192 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then printf "WARNING: error erasing boot0 " fi sync dd if=/multi/bl1.bin of=${DEST_CARD}boot0 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing bl1.bin." return 1 fi sync dd if=/multi/bl2.bin of=${DEST_CARD}boot0 seek=30 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing bl2.bin." return 1 fi sync dd if=/multi/u-boot.bin of=${DEST_CARD}boot0 seek=62 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing u-boot.bin." return 1 fi sync dd if=/multi/tzsw.bin of=${DEST_CARD}boot0 seek=2110 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR installing tzsw.bin." return 1 fi sync echo 1 > /sys/block/${_dest_card_}boot0/force_ro echo "OK" fi sleep 1 } #---------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------------- install_multi_sel() { echo "==== INSTALL MULTIBOOT SUPPORT ====" >> ${log_file} umount /mnt2 >> ${log_file} 2>&1 # Update userdata partition for multiboot /bin/mountx -t vfat ${DEST_CARD}p1 /mnt2 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo " ERROR mounting boot partition." return 1 fi sleep 1 mkdir /mnt2/multiboot >> ${log_file} 2>&1 if [ ! -d /multiboot ]; then echo " ERROR: missing multiboot directory." return 1 fi if [ -d /multiboot/mb_kernel ]; then mkdir /mnt2/multiboot/mb_kernel >> ${log_file} 2>&1 cp -f /multiboot/mb_kernel/* /mnt2/multiboot/mb_kernel >> ${log_file} 2>&1 fi cp /multiboot/boot.ini.multi /mnt2/multiboot >> ${log_file} 2>&1 [ $? -ne 0 ] && return 1 if [ "${O_BOARD}" = "XU" ]; then cp /multiboot/boot.scr.multi /mnt2/multiboot >> ${log_file} 2>&1 [ $? -ne 0 ] && return 1 cp /mnt2/multiboot/boot.scr.multi /mnt2/boot.scr >> ${log_file} 2>&1 [ $? -ne 0 ] && return 1 else cp /mnt2/multiboot/boot.ini.multi /mnt2/boot.ini >> ${log_file} 2>&1 [ $? -ne 0 ] && return 1 if [ "${label_pref}" = "em" ]; then sed -i "s/setenv CARD_TYPE SD/setenv CARD_TYPE EMMC/g" /mnt2/multiboot/boot.ini.multi fi fi cp /multiboot/${ODROID_IMAGE}.multi /mnt2/multiboot >> ${log_file} 2>&1 [ $? -ne 0 ] && return 1 cp /multiboot/uInitrd.multi /mnt2/multiboot >> ${log_file} 2>&1 [ $? -ne 0 ] && return 1 cp /multiboot/*.dtb.multi /mnt2/multiboot >> ${log_file} 2>&1 [ $? -ne 0 ] && return 1 #chmod -wx /mnt2/multiboot/${ODROID_IMAGE}.multi >> ${log_file} 2>&1 #chmod -wx /mnt2/multiboot/uInitrd.multi >> ${log_file} 2>&1 #chmod -wx /mnt2/multiboot/*.dtb.multi >> ${log_file} 2>&1 if [ "${label_pref}" = "sd" ]; then touch /mnt2/THIS_IS_SDCARD >> ${log_file} 2>&1 [ -f /mnt2/THIS_IS_EMMC ] && rm /mnt2/THIS_IS_EMMC >> ${log_file} 2>&1 else touch /mnt2/THIS_IS_EMMC >> ${log_file} 2>&1 [ -f /mnt2/THIS_IS_SDCARD ] && rm /mnt2/THIS_IS_SDCARD >> ${log_file} 2>&1 fi return 0 } #--------------------------------------------------------------------------------------------------------- #------------------------------------------------------------------------- # Calculate partitions offsets and sizes of the linux installation image get_lin_partstruct() { echo "==== CHECK JEEDOM IMAGE ====" >> ${log_file} printf " Checking Linux image ... " local re='^[0-9]+$' _card_sectno=`fdisk -l ${1} | grep "Disk ${1}" | awk '{print $7}'` #printf "%s " "${_card_sectno}" if ! [[ ${_card_sectno} =~ ${re} ]]; then _card_sectno=`fdisk -l ${1} | grep total | awk '{print $8}'` fi if ! [[ ${_card_sectno} =~ ${re} ]]; then echo "ERROR: sectNo?" return 1 fi #_card_end=$(( $_card_sectno - 1024)) _card_sectsize=`fdisk -l ${1} | grep "Sector size" | awk '{print $4}'` #printf "%s " "${_card_sectsize}" if ! [[ ${_card_sectsize} =~ ${re} ]]; then echo "ERROR: sectSize?" return 1 fi # get boot partition offset _partstart=`fdisk -l ${1} | grep ${1}1 | awk '{print $2}'` #printf "%s " "${_partstart}" if [ "${_partstart}" = "*" ]; then _partstart=`fdisk -l ${1} | grep ${1}1 | awk '{print $3}'` printf "[%s] " "${_partstart}" fi if ! [[ ${_partstart} =~ ${re} ]]; then echo "ERROR: FATpartStart?" return 1 fi _fat_partstart=$(( $_partstart * $_card_sectsize )) # get linux partition offset _partstart=`fdisk -l ${1} | grep ${1}2 | awk '{print $2}'` #printf "%s " "${_partstart}" if [ "${_partstart}" = "*" ]; then _partstart=`fdisk -l ${1} | grep ${1}2 | awk '{print $3}'` #printf "[%s] " "${_partstart}" fi if ! [[ ${_partstart} =~ ${re} ]]; then echo "ERROR: LINpartStart?" return 1 fi _linux_partstart=$(( $_partstart * $_card_sectsize )) #printf "(%s,%s) " "${_fat_partstart}" "${_linux_partstart}" echo "OK." return 0 } #------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------------------------------- prepare_linux_boot() { echo "==== PREPARE JEEDOM BOOT ====" >> ${log_file} # Prepare for multi boot if [ -d /mnt2/etc ]; then if [ -f /mnt2/etc/fstab ]; then # === Change fstab for multiboot === # save original fstab cp /mnt2/etc/fstab /mnt2/etc/fstab.orig >> ${log_file} 2>&1 # delete original "/" & "media/boot" mount sed -i '/\/dev\/mmcblk/d' /mnt2/etc/fstab >> ${log_file} 2>&1 sed -i '/UUID=/d' /mnt2/etc/fstab >> ${log_file} 2>&1 sed -i '/LABEL=/d' /mnt2/etc/fstab >> ${log_file} 2>&1 # on DietPi, do not use ramdisk sed -i '/DietPi/d' /mnt2/etc/fstab >> ${log_file} 2>&1 # save the rest mv /mnt2/etc/fstab /mnt2/etc/_fstab >> ${log_file} 2>&1 if [ "${LINUX_FSTYPE}" = "btrfs" ] ; then echo "LABEL=${label_pref}linux / btrfs noatime,nodiratime,compress=lzo 0 1" > /mnt2/etc/fstab else echo "LABEL=${label_pref}linux / ext4 errors=remount-ro,noatime,nodiratime 0 1" > /mnt2/etc/fstab fi echo "LABEL=${label_pref}userdata /media/boot vfat rw,user,sync,exec,dev,suid,uid=1000,gid=1000,dmask=000,fmask=111,iocharset=utf8 0 0" >> /mnt2/etc/fstab # append saved fstab cat /mnt2/etc/_fstab >> /mnt2/etc/fstab rm /mnt2/etc/_fstab >> ${log_file} 2>&1 else echo "ERROR: \"etc/fstab\" not found!" return 1 fi if [ -f /mnt2/etc/rc.local ]; then # === Change rc.local to prevent partition resize === cp /mnt2/etc/rc.local /mnt2/etc/rc.local.orig >> ${log_file} 2>&1 sed -i '/first-boot/d' /mnt2/etc/rc.local >> ${log_file} 2>&1 sed -i '/aafirstboot/d' /mnt2/etc/rc.local >> ${log_file} 2>&1 sed -i '/resizefs/d' /mnt2/etc/rc.local >> ${log_file} 2>&1 sed -i '/^.*exit/d' /mnt2/etc/rc.local >> ${log_file} 2>&1 if [ -f /mnt2/aafirstboot ]; then # on original odroid Linux echo "" >> /mnt2/etc/rc.local echo "if [ -f /.first_boot ]; then" >> /mnt2/etc/rc.local echo " rm -f /.first_boot" >> /mnt2/etc/rc.local echo " [ ! -f /etc/ssh/ssh_host_rsa_key ] && dpkg-reconfigure openssh-server" >> /mnt2/etc/rc.local echo "fi" >> /mnt2/etc/rc.local fi echo "exit 0" >> /mnt2/etc/rc.local chmod 0755 /mnt2/etc/rc.local rm -f /mnt2/aafirstboot >> ${log_file} 2>&1 rm -f /mnt2/.resizefs >> ${log_file} 2>&1 fi else echo "ERROR: \"etc\" directory not found!" return 1 fi if [ -d /mnt2/media/odroid ]; then chown 1000:1000 /mnt2/media/odroid >> ${log_file} 2>&1 chmod 0666 /mnt2/media/odroid >> ${log_file} 2>&1 fi if [ -f /mnt2/usr/local/bin/fs_resize_warning ]; then rm -f /mnt2/usr/local/bin/fs_resize_warning >> ${log_file} 2>&1 fi mkdir /mnt2/media/boot >> ${log_file} 2>&1 chown 1000:1000 /mnt2/media/boot >> ${log_file} 2>&1 chmod 0666 /mnt2/media/boot >> ${log_file} 2>&1 return 0 } #---------------------------------------------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------------- install_linux() { echo "==== INSTALL JEEDOM ====" >> ${log_file} local lin_fromdir="no" if [ "${1}" = "C2_backup/linux/jeedom.img" ] && [ "${L_FROMDIR}" = "yes" ]; then lin_fromdir="yes" fi printf "\n \033[33mInstalling \033[1mJEEDOM\033[22m\033[37m\n" if [ "${lin_fromdir}" = "no" ]; then get_lin_partstruct "/mnt/${1}" if [ $? -ne 0 ]; then printf " \033[35mWRONG Linux image partition structure!\033[37m\n" return 1 fi sleep 1 fi mkdir /mnt1 >> ${log_file} 2>&1 mkdir /mnt2 >> ${log_file} 2>&1 umount /mnt2 >> ${log_file} 2>&1 umount /mnt1 >> ${log_file} 2>&1 local _linpart="p7" local _linsufix="" # === Copy Linux partition === printf " \033[35mcopying JEEDOM image can take more than 15 min to complete on eMMC!\033[37m\n" if [ "${lin_fromdir}" = "no" ]; then printf " Installing JEEDOM root file system from image ... " /bin/mountx -t ext4 -o ro,loop,offset=${_linux_partstart} ${source_mount}/${1} /mnt1 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo " ERROR mounting JEEDOM source image." return 1 fi else printf " Installing JEEDOM root file system from backup ... " /bin/mountx -t ext4 ${source_mount}/${BKP_PREFIX}_backup/linux${_linsufix}/jeedom.img /mnt1 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo " ERROR mounting JEEDOM backup image." return 1 fi fi if [ "${LINUX_FSTYPE}" = "btrfs" ] ; then /bin/mountx -t btrfs -o compress-force=lzo ${DEST_CARD}${_linpart} /mnt2 >> ${log_file} 2>&1 else /bin/mountx -t ext4 ${DEST_CARD}${_linpart} /mnt2 >> ${log_file} 2>&1 fi if [ $? -ne 0 ]; then umount /mnt1 >> ${log_file} 2>&1 echo " ERROR mounting Linux destination partition." return 1 fi printf "copying... " rsync -r -t -p -o -g -x -l -H -D --numeric-ids -s --stats /mnt1/ /mnt2/ >> ${log_file} 2>&1 if [ $? -ne 0 ]; then umount /mnt1 >> ${log_file} 2>&1 umount /mnt2 >> ${log_file} 2>&1 echo "ERROR copying JEEDOM." return 1 fi sync printf "preparing... " prepare_linux_boot if [ $? -ne 0 ]; then umount /mnt1 >> ${log_file} 2>&1 umount /mnt2 >> ${log_file} 2>&1 return 1 fi if [ "${_inst_mbkernel}" = "yes" ]; then if [ -f /multiboot/mb_kernel/lib.tar.gz ]; then printf "[prepare multiboot kernel: " cd /mnt2 tar -tf /multiboot/mb_kernel/lib.tar.gz --warning=no-timestamp > /dev/null 2>&1 if [ $? -eq 0 ]; then tar -xf /multiboot/mb_kernel/lib.tar.gz --same-owner --warning=no-timestamp >> ${log_file} 2>&1 if [ $? -ne 0 ]; then echo "ERROR]" cd / return 1 fi printf "ok] " else printf "cannot extract] " _inst_mbkernel="no" fi cd / else _inst_mbkernel="no" fi fi if [ ! "${O_BOARD}" = "XU" ] && [ -f /multi/linux ]; then cp /multi/linux /mnt2/usr/share/terminfo/l >> ${log_file} 2>&1 echo "tput cnorm" >> /mnt2/root/.bashrc [ -d /mnt2/home/odroid ] && echo "tput cnorm" >> /mnt2/home/odroid/.bashrc fi cp /check_kernel /mnt2/bin >> ${log_file} 2>&1 cp /check_u-boot /mnt2/bin >> ${log_file} 2>&1 mkdir -p /mnt2/opt/multiboot >> ${log_file} 2>&1 cp /multi/u-boot.bin /mnt2/opt/multiboot >> ${log_file} 2>&1 # prevent installation/update of some packages [ ! -d /mnt2/etc/apt/preferences.d ] && mkdir -p /mnt2/etc/apt/preferences.d cat > /mnt2/etc/apt/preferences.d/odroid.pref << _EOF_ Package: u-boot Pin: release * Pin-Priority: -100 Package: bootini Pin: release * Pin-Priority: -100 _EOF_ #Package: linux-image-arm64-odroid-c2 #Pin: release * #Pin-Priority: -100 umount /mnt1 >> ${log_file} 2>&1 umount /mnt2 >> ${log_file} 2>&1 sleep 1 echo "OK" # ===================================================== # === Update userdata partition for Linux multiboot === printf " Copying JEEDOM boot files ... " local boot_src="/mnt1" if [ "${lin_fromdir}" = "no" ]; then /bin/mountx -t vfat -o ro,loop,offset=${_fat_partstart} ${source_mount}/${1} /mnt1 if [ $? -ne 0 ]; then echo "ERROR mounting JEEDOM boot image." return 1 fi else boot_src="${source_mount}/${BKP_PREFIX}_backup/linux${_linsufix}" fi /bin/mountx -t vfat ${DEST_CARD}p1 /mnt2 >> ${log_file} 2>&1 if [ $? -ne 0 ]; then umount /mnt1 >> ${log_file} 2>&1 echo "ERROR mounting boot partition." return 1 fi sync sleep 1 # Prepare boot.ini local _dtbdir="" if [ ! -f ${boot_src}/${ODROID_DTB} ]; then # on Armbian dtb is in separate directory [ -f ${boot_src}/dtb/${ODROID_DTB} ] && _dtbdir="dtb/" fi if [ -f ${boot_src}/${_dtbdir}${ODROID_DTB} ] && [ -f ${boot_src}/${ODROID_IMAGE} ] && [ -f ${boot_src}/uInitrd ]; then mkdir /mnt2/multiboot >> ${log_file} 2>&1 cp /multi/boot.ini.linux${_linsufix} /mnt2/multiboot/boot.ini.linux${_linsufix} sed -i "s/root=LABEL=XXlinux/root=LABEL=${label_pref}linux/g" /mnt2/multiboot/boot.ini.linux${_linsufix} cp ${boot_src}/${_dtbdir}${ODROID_DTB} /mnt2/multiboot/${ODROID_DTB}.linux${_linsufix} >> ${log_file} 2>&1 cp ${boot_src}/${ODROID_IMAGE} /mnt2/multiboot/${ODROID_IMAGE}.linux${_linsufix} >> ${log_file} 2>&1 cp ${boot_src}/uInitrd /mnt2/multiboot/uInitrd.linux${_linsufix} >> ${log_file} 2>&1 if [ "${_inst_mbkernel}" = "yes" ]; then if [ -f /multiboot/mb_kernel/${ODROID_DTB} ] && [ -f /multiboot/mb_kernel/${ODROID_IMAGE} ] && [ -f /multiboot/mb_kernel/uInitrd ]; then printf "[Using multiboot kernel] " cp -f /multiboot/mb_kernel/${ODROID_DTB} /mnt2/multiboot/${ODROID_DTB}.linux${_linsufix} >> ${log_file} 2>&1 cp -f /multiboot/mb_kernel/${ODROID_IMAGE} /mnt2/multiboot/${ODROID_IMAGE}.linux${_linsufix} >> ${log_file} 2>&1 cp -f /multiboot/mb_kernel/uInitrd /mnt2/multiboot/uInitrd.linux${_linsufix} >> ${log_file} 2>&1 fi fi else echo "ERROR: not all Linux boot files found! " umount /mnt1 >> ${log_file} 2>&1 umount /mnt2 >> ${log_file} 2>&1 return 1 fi umount /mnt1 >> ${log_file} 2>&1 umount /mnt2 >> ${log_file} 2>&1 sleep 1 echo "OK" } #---------------------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------------------------- check_inst() { # INSTALLATION DRIVE IS MOUNTED ON /MNT if [ "${INST_ANDROID}" != "yes" ] && [ "${INST_LINUX}" != "yes" ] && [ "${INST_OELEC}" != "yes" ]; then printf " \033[33m\033[1mNOTHING TO INSTALL !\033[22m\033[37m\n" printf " \033[33m\033[1mInstallation aborted.\033[22m\033[37m\n" umount ${source_mount} > /dev/null 2>&1 exit_script fi } #----------------------------------------------------------------------------------------------------------------- # ==================================================================================================== # START # ==================================================================================================== set_cardsize print_header check_inst #---------------------------------------------------------------- if [ "${INST_LINUX}" = "yesa" ]; then get_lin_partstruct "/mnt/linux.img" if [ $? -ne 0 ]; then echo " JEEDOM won't be installed!" INST_LINUX="no" check_install_sources if [ $_inst_no_ -eq 0 ]; then printf " \033[33m\033[1mNOTHING TO INSTALL\033[22m\033[37m\n" exit_script fi sleep 3 fi fi #---------------------------------------------------------------- print_header check_inst print_settings #================================== #---- Main menu ------------------- #================================== while true; do LINUX_FSTYPE="ext4" _inst_mbkernel="yes" break A="z" if read -n 1 -t 1 -s A; then if [ "${A}" = "1" ] && [ "${NUMCARDS}" = "2" ]; then print_header set_dest print_header print_settings elif [ "${A}" = "2" ]; then print_header set_userdatasize print_header print_settings elif [ "${A}" = "3" ]; then print_header set_systemsize print_header print_settings elif [ "${A}" = "4" ]; then print_header set_datasize print_header print_settings elif [ "${A}" = "5" ]; then print_header set_cachesize print_header print_settings elif [ "${A}" = "t" ] && [ "${INST_LINUX}" = "yes" ]; then if [ "${LINUX_FSTYPE}" = "btrfs" ]; then LINUX_FSTYPE="ext4" else LINUX_FSTYPE="btrfs" fi print_header print_settings elif [ "${O_BOARD}" = "C2" ] && [ "${A}" = "M" ] && [ "${INST_LINUX}" = "yes" ]; then if [ "${_inst_mbkernel}" = "yes" ]; then _inst_mbkernel="no" else _inst_mbkernel="yes" fi print_header print_settings elif [ "${A}" = "E" ]; then echo "" _reboot_delay=10 while [ $_reboot_delay -gt 0 ]; do printf " \033[33m\033[1mEXIT in %s sec, press \033[22ma\033[1m to abort, \033[22me\033[1m to exit \033[22m\033[37m\r" "$_reboot_delay" if read -n 1 -t 1 -s B; then _reboot_delay=10 if [ "${B}" = "a" ] || [ "${B}" = "A" ]; then break elif [ "${B}" = "e" ] || [ "${B}" = "E" ]; then _reboot_delay=0 break fi else let _reboot_delay=_reboot_delay-1 fi done if [ $_reboot_delay -eq 0 ]; then umount ${source_mount} > /dev/null 2>&1 exit 0 else print_header print_settings fi elif [ "${A}" = "I" ]; then echo "" _reboot_delay=15 while [ $_reboot_delay -gt 0 ]; do printf " \033[31m\033[1mWARNING: !! ALL DATA ON %s CARD WILL BE ERASED !!, PRESS Y TO CONTINUE\033[22m\033[37m\r" "${DEST_EMMC}" B="Z" if read -n 1 -t 1 -s B; then break else let _reboot_delay=_reboot_delay-1 fi done if [ "${B}" = "Y" ]; then break else print_header print_settings fi elif [ "${A}" = "s" ]; then # SCREENSHOT === fbcat > /install.ppm fi fi done # --- Set partition label prefix --- if [ "${DEST_EMMC}" = "EMMC" ]; then label_pref="em" else label_pref="sd" fi if [ "${INST_ANDROID}" != "yes" ] && [ "${INST_LINUX}" != "yes" ] && [ "${INST_OELEC}" != "yes" ]; then printf " \033[33m\033[1mNothing to install!\033[22m\033[37m\n" printf " \033[33m\033[1mInstallation aborted.\033[22m\033[37m\n" umount ${source_mount} > /dev/null 2>&1 exit_script fi # -------------------------------------------------------------------------------------------------------------------- print_header # --- Partition and format the card ---------------------------- prepare_card if [ $? -ne 0 ]; then printf " \033[35mERROR PREPARING %s CARD!\033[37m\n" "${DEST_EMMC}" else # install bootloader if [ "${O_BOARD}" = "C2" ]; then install_boot_c2 elif [ "${O_BOARD}" = "C1" ]; then install_boot_c1 else install_boot_xu fi if [ $? -eq 0 ]; then if [ "${INST_LINUX}" = "yes" ]; then # --- Install Linux --- install_linux "C2_backup/linux/jeedom.img" if [ $? -ne 0 ]; then printf " \033[35mERROR INSTALLING JEEDOM!\033[37m\n" else printf " \033[36mJEEDOM PREPARED!\033[37m\n" fi fi printf "\n \033[33mInstalling \033[1mMulti Boot support\033[22m\033[37m\n" install_multi_sel if [ $? -ne 0 ]; then printf " \033[35mERROR INSTALLING Multi Boot!\033[37m\n" else printf " \033[36m%s CARD PREPARED FOR MULTI BOOT!\033[37m\n" "${DEST_EMMC}" fi umount /mnt2 >> ${log_file} 2>&1 fi fi echo "" umount ${source_mount} > /dev/null 2>&1 printf " \033[35mRenaming Backup File !\033[37m\n" mountx /dev/sda1 /mnt1 rm -Rf /mnt1/C2_backup mv /mnt1/backupJeedom.tar.gz /mnt1/backupJeedom.tar.gz.installed printf " \033[35mREBOOT !\033[37m\n" sleep 2 reboot