weston_rootfs.sh 18.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
# Must be called after make_prepare in main script
# generate weston rootfs in input dir
# $1 - rootfs base dir
function make_debian_weston_rootfs()
{
	local ROOTFS_BASE=$1

	pr_info "Make Debian (${DEB_RELEASE}) rootfs start..."

	# umount previus mounts (if fail)
	umount ${ROOTFS_BASE}/{sys,proc,dev/pts,dev} 2>/dev/null || true

	# clear rootfs dir
	rm -rf ${ROOTFS_BASE}/*

	pr_info "rootfs: debootstrap"
	debootstrap --verbose --no-check-gpg --foreign --arch arm64 ${DEB_RELEASE} \
		${ROOTFS_BASE}/ ${PARAM_DEB_LOCAL_MIRROR}

	# prepare qemu
	pr_info "rootfs: debootstrap in rootfs (second-stage)"
	cp ${G_EMBEDIAN_PATH}/qemu_64bit/qemu-aarch64-static ${ROOTFS_BASE}/usr/bin/qemu-aarch64-static
	mount -o bind /proc ${ROOTFS_BASE}/proc
	mount -o bind /dev ${ROOTFS_BASE}/dev
	mount -o bind /dev/pts ${ROOTFS_BASE}/dev/pts
	mount -o bind /sys ${ROOTFS_BASE}/sys
	chroot $ROOTFS_BASE /debootstrap/debootstrap --second-stage

	# delete unused folder
	chroot $ROOTFS_BASE rm -rf ${ROOTFS_BASE}/debootstrap

	pr_info "rootfs: generate default configs"
	mkdir -p ${ROOTFS_BASE}/etc/sudoers.d/
	echo "user ALL=(root) /usr/bin/apt-get, /usr/bin/dpkg, /usr/bin/vi, /sbin/reboot" > ${ROOTFS_BASE}/etc/sudoers.d/user
	chmod 0440 ${ROOTFS_BASE}/etc/sudoers.d/user
	mkdir -p ${ROOTFS_BASE}/srv/local-apt-repository

	# imx-firmware
	cp -r ${G_EMBEDIAN_PATH}/deb/imx-firmware-${IMX_FIRMWARE_VERSION}/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# cairo
	cp -r ${G_EMBEDIAN_PATH}/deb/cairo/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# libdrm
	cp -r ${G_EMBEDIAN_PATH}/deb/libdrm/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# waylandprotocols
	cp -r ${G_EMBEDIAN_PATH}/deb/waylandprotocols/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# Vivante GPU libraries
	if [ ! -z "${G_GPU_IMX_VIV_PACKAGE_DIR}" ]; then
		cp -r ${G_EMBEDIAN_PATH}/deb/${G_GPU_IMX_VIV_PACKAGE_DIR}/* \
		${ROOTFS_BASE}/srv/local-apt-repository
	fi
	# Vivante GPU SDK Binaries
	if [ ! -z "${G_GPU_IMX_VIV_SDK_PACKAGE_DIR}" ]; then
		cp -r ${G_EMBEDIAN_PATH}/deb/${G_GPU_IMX_VIV_SDK_PACKAGE_DIR}/* \
		${ROOTFS_BASE}/srv/local-apt-repository
	fi

	# imxcodec
	if [ ! -z "${G_IMX_CODEC_DIR}" ]; then
		cp -r ${G_EMBEDIAN_PATH}/deb/${G_IMX_CODEC_DIR}/* \
			${ROOTFS_BASE}/srv/local-apt-repository
	fi

	# imxparser
	if [ ! -z "${G_IMX_PARSER_DIR}" ]; then
		cp -r ${G_EMBEDIAN_PATH}/deb/${G_IMX_PARSER_DIR}/* \
			${ROOTFS_BASE}/srv/local-apt-repository
	fi

	# imxvpuhantro
	if [ ! -z "${G_IMX_VPU_HANTRO_DIR}" ]; then
		cp -r ${G_EMBEDIAN_PATH}/deb/${G_IMX_VPU_HANTRO_DIR}/* \
			${ROOTFS_BASE}/srv/local-apt-repository
	fi

	# gstpluginsbad
	cp -r ${G_EMBEDIAN_PATH}/deb/gstpluginsbad/${GST_MM_VERSION}/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# gstpluginsbase
	cp -r ${G_EMBEDIAN_PATH}/deb/gstpluginsbase/${GST_MM_VERSION}/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# gstpluginsgood
	cp -r ${G_EMBEDIAN_PATH}/deb/gstpluginsgood/${GST_MM_VERSION}/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# gstreamer
	cp -r ${G_EMBEDIAN_PATH}/deb/gstreamer/${GST_MM_VERSION}/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# imx-vpuwrap
	if [ ! -z "${G_IMX_VPU_WRAPPER_DIR}" ]; then
		cp -r ${G_EMBEDIAN_PATH}/deb/${G_IMX_VPU_WRAPPER_DIR}/* \
			${ROOTFS_BASE}/srv/local-apt-repository
	fi

	# imxgstplugin
	cp -r ${G_EMBEDIAN_PATH}/deb/imxgstplugin/${GST_MM_VERSION}/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# weston
	cp -r ${G_EMBEDIAN_PATH}/deb/weston/${WESTON_PACKAGE_DIR}/* \
		${ROOTFS_BASE}/srv/local-apt-repository

	# opencv
	if [ ! -z "${G_OPENCV_DIR}" ]; then
		cp -r ${G_EMBEDIAN_PATH}/deb/${G_OPENCV_DIR}/* \
			${ROOTFS_BASE}/srv/local-apt-repository
	fi

# add mirror to source list
echo "deb ${DEF_DEBIAN_MIRROR} ${DEB_RELEASE} main contrib non-free
deb-src ${DEF_DEBIAN_MIRROR} ${DEB_RELEASE} main contrib non-free
deb ${DEF_DEBIAN_MIRROR} ${DEB_RELEASE}-backports main contrib non-free
deb-src ${DEF_DEBIAN_MIRROR} ${DEB_RELEASE}-backports main contrib non-free
deb ${DEF_DEBIAN_MIRROR} testing main contrib non-free
deb-src ${DEF_DEBIAN_MIRROR} testing main contrib non-free
" > etc/apt/sources.list

# maximize local repo priority
echo "Package: *
Pin: origin ""
Pin-Priority: 1000
" > etc/apt/preferences.d/local

# raise backports priority
echo "Package: *
Pin: release n=${DEB_RELEASE}-backports
Pin-Priority: 600
" > etc/apt/preferences.d/backports

echo "
# /dev/mmcblk0p1  /boot           vfat    defaults        0       0
" > etc/fstab

echo "${MACHINE}" > etc/hostname

echo "auto lo
iface lo inet loopback
" > etc/network/interfaces

echo "
locales locales/locales_to_be_generated multiselect en_US.UTF-8 UTF-8
locales locales/default_environment_locale select en_US.UTF-8
console-common	console-data/keymap/policy	select	Select keymap from full list
keyboard-configuration keyboard-configuration/variant select 'English (US)'
openssh-server openssh-server/permit-root-login select true
" > debconf.set

	pr_info "rootfs: prepare install packages in rootfs"
# apt-get install without starting
cat > ${ROOTFS_BASE}/usr/sbin/policy-rc.d << EOF
#!/bin/sh
exit 101
EOF

chmod +x ${ROOTFS_BASE}/usr/sbin/policy-rc.d

# third packages stage
cat > third-stage << EOF
#!/bin/bash
# apply debconfig options
debconf-set-selections /debconf.set
rm -f /debconf.set

function protected_install()
{
	local _name=\${1}
	local repeated_cnt=5;
	local RET_CODE=1;

	echo Installing \${_name}
	for (( c=0; c<\${repeated_cnt}; c++ ))
	do
		apt install -y \${_name} && {
			RET_CODE=0;
			break;
		};

		echo
		echo "##########################"
		echo "## Fix missing packages ##"
		echo "##########################"
		echo

		sleep 2;

		apt --fix-broken install -y && {
			RET_CODE=0;
			break;
		};
	done

	return \${RET_CODE}
}

# update packages and install base
apt-get update || apt-get upgrade

# local-apt-repository support
protected_install local-apt-repository

# update packages and install base
apt-get update || apt-get upgrade

# libc6 >=2.29 by Vivnate GPU drivers
protected_install libc6/testing

# maximize local repo priority
echo "Package: *
Pin: origin ""
Pin-Priority: 1000
" > etc/apt/preferences.d/local

echo "Package: *
Pin: release n=${DEB_RELEASE}
Pin-Priority: 990
" > etc/apt/preferences.d/buster
# raise backports priority
echo "Package: *
Pin: release n=${DEB_RELEASE}-backports
Pin-Priority: 500
" > etc/apt/preferences.d/backports

# raise backports priority
echo "Package: *
Pin: release n=testing
Pin-Priority: 90
" > etc/apt/preferences.d/testing

# update packages and install base
apt-get update

protected_install libc6-dev/testing
protected_install locales
protected_install ntp
protected_install openssh-server
protected_install nfs-common

# packages required when flashing emmc
protected_install dosfstools

# fix config for sshd (permit root login)
sed -i -e 's/#PermitRootLogin.*/PermitRootLogin\tyes/g' /etc/ssh/sshd_config

# net-tools (ifconfig, etc.)
protected_install net-tools
protected_install network-manager

# sdma package
protected_install imx-firmware-sdma

# VPU package
protected_install imx-firmware-vpu

# epdc package
protected_install imx-firmware-epdc

# graphical packages
protected_install libdrm-vivante1
protected_install imx-gpu-viv-core
protected_install dpkg-dev
protected_install imx-gpu-viv-wl

# GPU SDK
if [ ! -z "${G_GPU_IMX_VIV_SDK_PACKAGE_DIR}" ]
then
       protected_install libwayland-egl-backend-dev/buster
       protected_install glslang-tools/buster
       protected_install libtiff-dev/buster
       protected_install libdevil-dev/buster
       protected_install libassimp-dev/buster
       protected_install imx-gpu-sdk-console
       protected_install imx-gpu-sdk-gles2
       protected_install imx-gpu-sdk-gles3
       protected_install imx-gpu-sdk-opencl
       protected_install imx-gpu-sdk-window
fi

protected_install weston

# alsa & gstreamer
protected_install alsa-utils
protected_install gstreamer1.0-alsa
protected_install gstreamer1.0-plugins-bad
protected_install gstreamer1.0-plugins-base
protected_install gstreamer1.0-plugins-base-apps
protected_install gstreamer1.0-plugins-ugly
protected_install gstreamer1.0-plugins-good
protected_install gstreamer1.0-tools
protected_install ${IMXGSTPLG}

# i2c tools
protected_install i2c-tools

# usb tools
protected_install usbutils

# net tools
protected_install iperf

protected_install rng-tools

# mtd
protected_install mtd-utils

# bluetooth
protected_install bluetooth
protected_install bluez-obexd
protected_install bluez-tools
protected_install blueman
protected_install gconf2

# wifi support packages
protected_install hostapd
protected_install udhcpd

# disable the hostapd service by default
systemctl disable hostapd.service

# can support
protected_install can-utils

# pmount
protected_install pmount

# pm-utils
protected_install pm-utils

# build-essential (Fix dependendy issue)
protected_install libgcc/testing
protected_install gcc-9/testing
protected_install gcc/testing
protected_install libc6-dev/testing
protected_install libstdc++-9-dev/testing
protected_install g++-9/testing
protected_install g++/testing
protected_install build-essential/testing

apt-get -y autoremove

#update iptables alternatives to legacy
update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy

#install usleep busybox applet
ln -sf /bin/busybox /bin/usleep

# create users and set password
useradd -m -G audio -s /bin/bash user
usermod -a -G video user
echo "user:user" | chpasswd
echo "root:root" | chpasswd

# sado kill
rm -f third-stage
EOF

	pr_info "rootfs: install selected Debian packages (third-stage)"
	chmod +x third-stage
	chroot ${ROOTFS_BASE} /third-stage

	# fourth-stage
	#install securetty
	install -m 0644 ${G_EMBEDIAN_PATH}/securetty \
		${ROOTFS_BASE}/etc/securetty

	# install weston service
	install -d ${ROOTFS_BASE}/etc/xdg/weston
        if [ "${MACHINE}" = "smarcimx8m2g" ] || \
           [ "${MACHINE}" = "smarcimx8m4g" ]; then
	install -m 0644 ${G_EMBEDIAN_PATH}/smarcimx8mq/weston.ini \
		${ROOTFS_BASE}/etc/xdg/weston
	install -m 0755 ${G_EMBEDIAN_PATH}/smarcimx8mq/weston.config \
		${ROOTFS_BASE}/etc/default/weston
	fi
	install -m 0755 ${G_EMBEDIAN_PATH}/weston-start \
		${ROOTFS_BASE}/usr/bin/weston-start
	install -m 0755 ${G_EMBEDIAN_PATH}/weston.profile \
		${ROOTFS_BASE}/etc/profile.d/weston.sh
	install -m 0644 ${G_EMBEDIAN_PATH}/weston.service \
		${ROOTFS_BASE}/lib/systemd/system
	ln -s /lib/systemd/system/weston.service \
		${ROOTFS_BASE}/etc/systemd/system/multi-user.target.wants/weston.service

	# remove pm-utils default scripts 
	rm -rf ${ROOTFS_BASE}/usr/lib/pm-utils/sleep.d/
	rm -rf ${ROOTFS_BASE}/usr/lib/pm-utils/module.d/
	rm -rf ${ROOTFS_BASE}/usr/lib/pm-utils/power.d/

	## end packages stage ##
	if [ "${G_USER_PACKAGES}" != "" ] ; then
		pr_info "rootfs: install user defined packages (user-stage)"
		pr_info "rootfs: G_USER_PACKAGES \"${G_USER_PACKAGES}\" "

echo "#!/bin/bash
# update packages
apt-get update
apt-get upgrade

# install all user packages
apt-get -y install ${G_USER_PACKAGES}

rm -f user-stage
" > user-stage

		chmod +x user-stage
		chroot ${ROOTFS_BASE} /user-stage
	fi

	# binaries rootfs patching
	install -m 0644 ${G_EMBEDIAN_PATH}/issue ${ROOTFS_BASE}/etc/
	install -m 0644 ${G_EMBEDIAN_PATH}/issue.net ${ROOTFS_BASE}/etc/
	install -m 0755 ${G_EMBEDIAN_PATH}/rc.local ${ROOTFS_BASE}/etc/
	install -d ${ROOTFS_BASE}/boot/
	install -m 0644 ${G_EMBEDIAN_PATH}/splash.bmp ${ROOTFS_BASE}/boot/
	cp ${PARAM_OUTPUT_DIR}/Image ${ROOTFS_BASE}/boot
	cp ${PARAM_OUTPUT_DIR}/*.dtb ${ROOTFS_BASE}/boot
	if [ "$DEFAULT_BOOT_DTB" != "$BOOT_DTB" ]; then
		ln -sf ${DEFAULT_BOOT_DTB} ${ROOTFS_BASE}/boot/${BOOT_DTB}
	fi

	mkdir -p ${ROOTFS_BASE}/usr/share/images/desktop-base/
	install -m 0644 ${G_EMBEDIAN_PATH}/wallpaper_hd.png \
		${ROOTFS_BASE}/usr/share/images/desktop-base/default

	# Add alsa default configs
	install -m 0644 ${G_EMBEDIAN_PATH}/asound.state \
		${ROOTFS_BASE}/var/lib/alsa/
	install -m 0644 ${G_EMBEDIAN_PATH}/asound.conf ${ROOTFS_BASE}/etc/

	# Revert regular booting
	rm -f ${ROOTFS_BASE}/usr/sbin/policy-rc.d

	# install kernel modules in rootfs
	install_kernel_modules \
		${G_CROSS_COMPILER_PATH}/${G_CROSS_COMPILER_PREFIX} \
		${G_LINUX_KERNEL_DEF_CONFIG} ${G_LINUX_KERNEL_SRC_DIR} \
		${ROOTFS_BASE}

	# copy all kernel headers for development
	mkdir -p ${ROOTFS_BASE}/usr/local/src/linux-imx/drivers/staging/android/uapi
	cp ${G_LINUX_KERNEL_SRC_DIR}/drivers/staging/android/uapi/* \
	${ROOTFS_BASE}/usr/local/src/linux-imx/drivers/staging/android/uapi
	cp -r ${G_LINUX_KERNEL_SRC_DIR}/include \
		${ROOTFS_BASE}/usr/local/src/linux-imx/

	# copy custom files
	cp ${G_EMBEDIAN_PATH}/10-imx.rules ${ROOTFS_BASE}/etc/udev/rules.d
	cp ${G_EMBEDIAN_PATH}/mount.blacklist ${ROOTFS_BASE}/etc/udev/rules.d
	cp ${G_EMBEDIAN_PATH}/automount.rules ${ROOTFS_BASE}/etc/udev/rules.d
	mkdir -p ${ROOTFS_BASE}/etc/udev/scripts/
	install -m 0755 ${G_EMBEDIAN_PATH}/mount.sh \
		${ROOTFS_BASE}/etc/udev/scripts/mount.sh

	if [ "${MACHINE}" = "smarcimx8m2g" ] || \
	   [ "${MACHINE}" = "smarcimx8m4g" ]; then
		cp ${G_EMBEDIAN_PATH}/smarcimx8mq/*.rules ${ROOTFS_BASE}/etc/udev/rules.d
	fi

## clenup command
echo "#!/bin/bash
apt-get clean
rm -f cleanup
" > cleanup

	# clean all packages
	pr_info "rootfs: clean"
	chmod +x cleanup
	chroot ${ROOTFS_BASE} /cleanup
	umount ${ROOTFS_BASE}/{sys,proc,dev/pts,dev} 2>/dev/null || true

	# kill latest dbus-daemon instance due to qemu-aarch64-static
	QEMU_PROC_ID=$(ps axf | grep dbus-daemon | grep qemu-aarch64-static | awk '{print $1}')
	if [ -n "$QEMU_PROC_ID" ]
	then
		kill -9 $QEMU_PROC_ID
	fi

	rm -f ${ROOTFS_BASE}/usr/bin/qemu-aarch64-static
}

# make SD card for device
# $1 -- block device
# $2 -- output images dir
function make_weston_sdcard()
{
	readonly local LPARAM_BLOCK_DEVICE=${1}
	readonly local LPARAM_OUTPUT_DIR=${2}
	readonly local P1_MOUNT_DIR="${G_TMP_DIR}/p1"
        readonly local P2_MOUNT_DIR="${G_TMP_DIR}/p2"
	readonly local DEBIAN_IMAGES_TO_ROOTFS_POINT="opt/images/Debian"

	readonly local BOOTLOAD_RESERVE_SIZE=2
        readonly local PART1_SIZE=48
        readonly local BOOTPART=1
        readonly local ROOTFSPART=2
	readonly local SPARE_SIZE=4

	[ "${LPARAM_BLOCK_DEVICE}" = "na" ] && {
		pr_error "No valid block device: ${LPARAM_BLOCK_DEVICE}"
		return 1;
	};

	local part=""
	if [ `echo ${LPARAM_BLOCK_DEVICE} | grep -c mmcblk` -ne 0 ]; then
		part="p"
	fi

	# Check that we're using a valid device
	if ! check_sdcard ${LPARAM_BLOCK_DEVICE}; then
		return 1
	fi

	for ((i=0; i<10; i++))
	do
		if [ `mount | grep -c ${LPARAM_BLOCK_DEVICE}${part}$i` -ne 0 ]; then
			umount ${LPARAM_BLOCK_DEVICE}${part}$i
		fi
	done

	function format_sdcard
	{
		pr_info "Formating SD card partitions"
                mkfs.vfat -F 16 ${LPARAM_BLOCK_DEVICE}${part}1 -n boot
                mkfs.ext4 ${LPARAM_BLOCK_DEVICE}${part}2 -L rootfs
	}

	function flash_u-boot
	{
		pr_info "Flashing U-Boot"
                dd if=${LPARAM_OUTPUT_DIR}/${G_UBOOT_NAME_FOR_EMMC} \
		of=${LPARAM_BLOCK_DEVICE} bs=1K seek=${BOOTLOADER_OFFSET}; sync
	}

	function flash_sdcard
	{
                pr_info "Flashing \"Image and device tree\" partition"
                cp -v ${LPARAM_OUTPUT_DIR}/Image ${P1_MOUNT_DIR}/
                mkdir -p ${P1_MOUNT_DIR}/dtbs/
                cp -v ${LPARAM_OUTPUT_DIR}/fsl-smarcimx8mq${DISPLAY}.dtb ${P1_MOUNT_DIR}/dtbs/fsl-smarcimx8mq.dtb
                cp -v ${G_EMBEDIAN_PATH}/uEnv.txt ${P1_MOUNT_DIR}/
		pr_info "Flashing \"rootfs\" partition"
                tar -xpf ${LPARAM_OUTPUT_DIR}/${DEF_ROOTFS_TARBALL_NAME} \
			-C ${P2_MOUNT_DIR}/
	}

	function copy_debian_images
	{
		mkdir -p ${P2_MOUNT_DIR}/${DEBIAN_IMAGES_TO_ROOTFS_POINT}

		pr_info "Copying Debian images to /${DEBIAN_IMAGES_TO_ROOTFS_POINT}"
		cp ${LPARAM_OUTPUT_DIR}/${DEF_ROOTFS_TARBALL_NAME} \
			${P2_MOUNT_DIR}/${DEBIAN_IMAGES_TO_ROOTFS_POINT}/${DEF_ROOTFS_TARBALL_NAME}

		pr_info "Copying U-Boot to /${DEBIAN_IMAGES_TO_ROOTFS_POINT}"
		if [ "${MACHINE}" = "smarcimx8m2g" ] || \
		   [ "${MACHINE}" = "smarcimx8m4g" ]; then
			cp ${LPARAM_OUTPUT_DIR}/imx-boot-* \
				${P2_MOUNT_DIR}/${DEBIAN_IMAGES_TO_ROOTFS_POINT}/
		else
			cp ${LPARAM_OUTPUT_DIR}/${G_UBOOT_NAME_FOR_EMMC} \
				${P2_MOUNT_DIR}/${DEBIAN_IMAGES_TO_ROOTFS_POINT}/
		fi
                pr_info "Copying Image and device tree files to /${DEBIAN_IMAGES_TO_ROOTFS_POINT}"
                cp ${LPARAM_OUTPUT_DIR}/Image \
			${P2_MOUNT_DIR}/${DEBIAN_IMAGES_TO_ROOTFS_POINT}/
                cp ${LPARAM_OUTPUT_DIR}/*.dtb \
			${P2_MOUNT_DIR}/${DEBIAN_IMAGES_TO_ROOTFS_POINT}/
                cp ${G_EMBEDIAN_PATH}/uEnv_emmc.txt \
			${P2_MOUNT_DIR}/${DEBIAN_IMAGES_TO_ROOTFS_POINT}/uEnv.txt
	}

	function copy_scripts
	{
		pr_info "Copying scripts to /${DEBIAN_IMAGES_TO_ROOTFS_POINT}"
		cp ${G_EMBEDIAN_PATH}/mx8_install_debian.sh \
			${P2_MOUNT_DIR}/usr/sbin/install_debian.sh
	}

	function ceildiv
	{
		local num=$1
		local div=$2
		echo $(( (num + div - 1) / div ))
	}

	# Delete the partitions
	for ((i=0; i<=10; i++))
	do
		if [ -e ${LPARAM_BLOCK_DEVICE}${part}${i} ]; then
			dd if=/dev/zero of=${LPARAM_BLOCK_DEVICE}${part}$i bs=512 count=1024 2> /dev/null || true
		fi
	done
	sync

	((echo d; echo 1; echo d; echo 2; echo d; echo 3; echo d; echo w) | \
		fdisk ${LPARAM_BLOCK_DEVICE} &> /dev/null) || true
	sync

	dd if=/dev/zero of=${LPARAM_BLOCK_DEVICE} bs=1M count=100
	sync; sleep 2

	# Create a new partition table
	pr_info "Creating new partitions"

        # Get total card size
        TOTAL_SIZE=`fdisk -s ${LPARAM_BLOCK_DEVICE}`
        TOTAL_SIZE=`expr ${TOTAL_SIZE} / 1024`
        ROOTFS_SIZE=`expr ${TOTAL_SIZE} - ${BOOTLOAD_RESERVE_SIZE} - ${PART1_SIZE} - ${SPARE_SIZE}`

        pr_info "ROOT SIZE=${ROOTFS_SIZE} TOTAl SIZE=${TOTAL_SIZE}"

        BLOCK=`echo ${LPARAM_BLOCK_DEVICE} | cut -d "/" -f 3`
        SECT_SIZE_BYTES=`cat /sys/block/${BLOCK}/queue/physical_block_size`

        BOOTLOAD_RESERVE_SIZE_BYTES=$((BOOTLOAD_RESERVE_SIZE * 1024 * 1024))
        PART1_SIZE_BYTES=$((PART1_SIZE * 1024 * 1024))
        PART1_END_BYTES=`expr ${BOOTLOAD_RESERVE_SIZE_BYTES} + ${PART1_SIZE_BYTES}`
        ROOTFS_SIZE_BYTES=$((ROOTFS_SIZE * 1024 * 1024))

        PART1_FIRST_SECT=`ceildiv ${BOOTLOAD_RESERVE_SIZE_BYTES} ${SECT_SIZE_BYTES}`
        PART1_END_SECT=`ceildiv ${PART1_END_BYTES} ${SECT_SIZE_BYTES}`
        PART2_FIRST_SECT=`expr ${PART1_END_SECT} + 1 `

        (echo n; echo p; echo $BOOTPART; echo $PART1_FIRST_SECT; \
         echo $PART1_END_SECT; echo n; echo p; echo $ROOTFSPART; \
         echo $PART2_FIRST_SECT; echo; echo p; echo w) | fdisk -u ${LPARAM_BLOCK_DEVICE} > /dev/null

        sleep 2; sync;
        fdisk -l ${LPARAM_BLOCK_DEVICE}

        sleep 2; sync;

	# Format the partitions
	format_sdcard
	sleep 2; sync;

	flash_u-boot
	sleep 2; sync;

        # Mount the partitions
        mkdir -p ${P1_MOUNT_DIR}
        mkdir -p ${P2_MOUNT_DIR}
        sync

        mount ${LPARAM_BLOCK_DEVICE}${part}1  ${P1_MOUNT_DIR}
        mount ${LPARAM_BLOCK_DEVICE}${part}2  ${P2_MOUNT_DIR}
        sleep 2; sync;

        flash_sdcard
        copy_debian_images
        copy_scripts

        pr_info "Sync sdcard..."
        sync
        umount ${P1_MOUNT_DIR}
        umount ${P2_MOUNT_DIR}

        rm -rf ${P1_MOUNT_DIR}
        rm -rf ${P2_MOUNT_DIR}

        pr_info "Done make sdcard!"

        return 0;
}