Commit ac25ac1d authored by Yuri's avatar Yuri Committed by GitHub
Browse files

Add files via upload

parent c4b13318
Loading
Loading
Loading
Loading

repack_img

0 → 100644
+236 −0
Original line number Diff line number Diff line
#!/bin/bash
e="\x1b[";c=$e"39;49;00m";y=$e"93;01m";cy=$e"96;01m";r=$e"1;91m";g=$e"92;01m";
##########################################################
#                                                        #
#           Carliv Image Kitchen for Android             #
#     boot+recovery images copyright-2016 carliv@xda     #
#    including support for MTK powered phones images     #
#                                                        #
##########################################################
abort() { cd "$PWD"; echo " "; echo -e "$cy >>$c$r Exit script$c\n"; }
###########################################################
bin="$PWD/.bin";
chmod -R 755 "$bin"/*;
cd "$PWD";
mkbootfs=$bin/mkbootfs;
minigzip=$bin/minigzip;
mkbootimg=$bin/mkbootimg;
###########################################################
clear;
echo -e "
****************************************************
*                                                  *
*     $cy Carliv Image Kitchen for Android$c v1.3       *
*  boot+recovery images copyright-2016$cy carliv@xda$c  *
*  including support for MTK powered phones images *
*                $g LINUX x64 version$c                *
*             The repacking images script          *
*                                                  *
**************************************************** 
";
###########################################################
if [ ! "$1" ]; 
	then
	echo -e "$r No folder selected. ERROR!$c";
	abort;
	exit 1;
fi;
###########################################################
imgfolder=$(basename "$1");
echo -e "Processing the$y $imgfolder$c folder.";
echo " ";
cd "$imgfolder";
###########################################################
echo "Repacking the image....";
echo " ";
getfile=$(find . -name "*.img-kernel");
myname="${getfile%.*}";
kernel="$myname".img-kernel;
if [[ -z "$kernel" ]];
	then
	echo -e "$r The$c$y kernel$c$r file is missing. Check your folder!. ERROR!$c";
	abort;
	exit 1;
fi;
echo -e "The kernel is:$y $kernel$c";
echo " ";
echo -e "Getting the$y ramdisk$c compression....";
echo " ";
if [ ! -d "ramdisk" ];
	then
	echo -e "$r The$c$y ramdisk$c$r folder is missing. Check your folder!. ERROR!$c";
	abort;
	exit 1;
fi;
compress=$(cat "$myname".img-ramdisk-compress);
echo -e "Compression used:$y $compress$c";
echo " ";
###########################################################
if [ "$compress" == "gz" ];
	then
	$mkbootfs ramdisk | $minigzip -c -9 > "$myname".img-ramdisk.gz;
	ramdisk="$myname".img-ramdisk.gz;
	echo -e "The ramdisk is:$y $ramdisk$c";
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r The$c$y ramdisk$c$r repacking process failed. Check on screen printed errors and find a solution. ERROR!$c";
	  abort;
	  exit 1;
	fi;
fi;
if [ "$compress" == "lzma" ];
	then
	$mkbootfs ramdisk | xz --format=lzma -1zv > "$myname".img-ramdisk.lzma;
	ramdisk="$myname".img-ramdisk.lzma;
	echo -e "The ramdisk is:$y $ramdisk$c";
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r The$c$y ramdisk$c$r repacking process failed. Check on screen printed errors and find a solution. ERROR!$c";
	  abort;
	  exit 1;
	fi;
fi;
if [ "$compress" == "xz" ];
	then
	$mkbootfs ramdisk | xz -1zv -Ccrc32 > "$myname".img-ramdisk.xz;
	ramdisk="$myname".img-ramdisk.xz;
	echo -e "The ramdisk is:$y $ramdisk$c";
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r The$c$y ramdisk$c$r repacking process failed. Check on screen printed errors and find a solution. ERROR!$c";
	  abort;
	  exit 1;
	fi;
fi;
if [ "$compress" == "bz2" ];
	then
	$mkbootfs ramdisk | bzip2 -v > "$myname".img-ramdisk.bz2;
	ramdisk="$myname".img-ramdisk.bz2;
	echo "The ramdisk is: $ramdisk";
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r The$c$y ramdisk$c$r repacking process failed. Check on screen printed errors and find a solution. ERROR!$c";
	  abort;
	  exit 1;
	fi;
fi;
if [ "$compress" == "lzo" ];
	then
	$mkbootfs ramdisk | lzop -v > "$myname".img-ramdisk.lzo;
	ramdisk="$myname".img-ramdisk.lzo;
	echo "The ramdisk is: $ramdisk";
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r The$c$y ramdisk$c$r repacking process failed. Check on screen printed errors and find a solution. ERROR!$c";
	  abort;
	  exit 1;
	fi;
fi;
if [ "$compress" == "lz4" ];
	then
	$mkbootfs ramdisk | lz4 -l stdin stdout > "$myname".img-ramdisk.lz4;
	ramdisk="$myname".img-ramdisk.lz4;
	echo "The ramdisk is: $ramdisk";
	if [ ! $? -eq "0" ]; 
	then
	  echo -e "$r The$c$y ramdisk$c$r repacking process failed. Check on screen printed errors and find a solution. ERROR!$c";
	  abort;
	  exit 1;
	fi;
fi;
echo " ";
###########################################################
echo " ";
echo "Getting the image repacking arguments....";
echo " ";
######################### BOARD ###########################
if [[ -f "$myname".img-board ]];
	then
	bname=$(cat "$myname".img-board);
	echo -e "Board:$y             $bname$c";echo " ";
fi;
########################## BASE ############################
if [[ -f "$myname".img-base ]];
	then
	base=$(cat "$myname".img-base);
	echo -e "Base:$y              $base$c";echo " ";
fi;
######################## PAGESIZE ##########################
if [[ -f "$myname".img-pagesize ]];
	then
	pagesize=$(cat "$myname".img-pagesize);
	echo -e "Pagesize:$y          $pagesize$c";echo " ";
fi;
######################## CMDLINE ###########################
if [[ -f "$myname".img-cmdline ]];
	then
	lcmd=$(cat "$myname".img-cmdline);
	echo -e "Command line:$y      $lcmd$c";echo " ";
fi;
##################### KERNEL OFFSET ########################
if [[ -f "$myname".img-kernel_offset ]];
	then
	keoff=$(cat "$myname".img-kernel_offset);
	echo -e "Kernel offset:$y     $keoff$c";echo " ";
fi;
##################### RAMDISK OFFSET #######################
if [[ -f "$myname".img-ramdisk_offset ]];
	then
	ramoff=$(cat "$myname".img-ramdisk_offset);
	echo -e "Ramdisk offset:$y    $ramoff$c";echo " ";
fi;
###################### SECOND OFFSET ########################
if [[ -f "$myname".img-second_offset ]];
	then
	flsec=$(cat "$myname".img-second_offset);
	losec=" --second_offset $flsec";
	echo -e "Second offset:$y     $flsec$c";echo " ";	
fi;
#################### SECOND BOOTLOADER ######################
if [[ -f "$myname".img-second ]];
	then
	secboot="$myname".img-second;
	second=" --second $secboot";
	echo -e "Second bootloader:$y $secboot$c";echo " ";
fi;
###################### TAGS OFFSET ##########################
if [[ -f "$myname".img-tags_offset ]];
	then
	tagoff=$(cat "$myname".img-tags_offset);
	echo -e "Tags offset:$y       $tagoff$c";echo " ";
fi;
############################ DT ##############################
if [[ -f "$myname".img-dt ]];
	then
	ldtb="$myname".img-dt;
	dtb=" --dt $ldtb";
	echo -e "Device tree blob:$y  $ldtb$c";echo " ";
fi;
###########################################################
newimage="$imgfolder"_"repacked".img;
echo " ";
echo "Executing the repacking command...."
echo " ";
if [[ -f "$myname".img-mtk ]];
	then
	$mkbootimg --kernel "$kernel" --ramdisk "$ramdisk" --pagesize "$pagesize" --cmdline "$lcmd" --board "$bname" --base "$base" --kernel_offset "$keoff" --ramdisk_offset "$ramoff" --tags_offset "$tagoff"$losec$second$dtb --mtk 1 -o ../output/"$newimage";
	if [ ! $? -eq "0" ]; 
		then
		abort;
		exit 1;
	fi;
	else
	$mkbootimg --kernel "$kernel" --ramdisk "$ramdisk" --pagesize "$pagesize" --cmdline "$lcmd" --board "$bname" --base "$base" --kernel_offset "$keoff" --ramdisk_offset "$ramoff" --tags_offset "$tagoff"$losec$second$dtb -o ../output/"$newimage";
	if [ ! $? -eq "0" ]; 
		then
		abort;
		exit 1;
	fi;
fi;
compramdisk="$myname".img-ramdisk."$compress";
rm -f "$compramdisk";
cd ../;
echo " ";
###########################################################
echo -e "Done! Your new image is packed as$y $newimage$c in$g [output]$c folder.";
echo " ";