db845c: Enable booting from mmc-sdcard

This is an experimental build configuration and only used to
boot AOSP on mmc sdcard on DB845c and RB5 in the LKFT lab.
Regular users should not enable this build flag and should
boot from UFS instead.

Booting from external sdcards will help prevent the internal
emmc/ufs wear off in the long run and extend the lab-life of
most of our devboards.

Set TARGET_SDCARD_BOOT=true at build time to build and boot
AOSP from mmc sdcard. This device configuration need atleast
16GB sdcard to boot from. As of today, 1st AOSP/main boot
with Linux kernel v6.7 on a 16GB Samsung EVO+ SDHCI sdcard
takes just under 3mins to boot to UI and subsequent reboot
to UI takes just over 1min.

Using U-Boot to partition and flash mmc sdcard:
To avoid flashing anything on internal UFS and boot solely
from a sdcard, we are switching to chainloading U-Boot from
ABL bootloader. For now we are using an upstream U-Boot fork
for DB845c and RB5 and its currently under work-in-progress:
https://source.devboardsforandroid.linaro.org/platform/external/u-boot/
Since upstream U-Boot only support boot image header version
2, so we are also using the same. It also helps us load and
boot with boot.img from RAM using "fastboot boot boot.img"
command in the lab. In the long run we plan to switch to
AOSP/external/u-boot project to catch up with all the
Android bootloader features. We are using U-Boot "gpt" and
"fastboot" commands to create partition table on external
sdcard and flash AOSP super and userdata images. Follow the
U-Boot instructions here to prepare a sdcard to boot AOSP:
https://devboardsforandroid.linaro.org/en/latest/devices/rb3.html#booting-aosp-from-mmc-sdcard

Loading UFS driver from vendor_dlkm partition:
The AOSP's first_stage_init mount take the first platform
block device it finds and continues with it, irrespective of
the androidboot.boot_devices bootarg. This is a blocker for
us to boot from mmc sdcard because first_stage_init always
tried to look for and mount UFS partitions. In order to
avoid touching first_stage_init boot / mount flow in
AOSP/system/core project, we will be loading UFS driver from
vendor_dlkm partition so that UFS gets probed later in the
boot cycle.

Signed-off-by: Amit Pundir <amit.pundir@linaro.org>
Change-Id: I20cd89df8b5f1fdb1bf712232284d6d754518d8f
diff --git a/shared/utils/dlkm_loader/device.mk b/shared/utils/dlkm_loader/device.mk
index 57b951f..d4df84c 100644
--- a/shared/utils/dlkm_loader/device.mk
+++ b/shared/utils/dlkm_loader/device.mk
@@ -2,5 +2,10 @@
 PRODUCT_PACKAGES += dlkm_loader
 
 # List of modules that should not load automatically
+ifeq ($(TARGET_SDCARD_BOOT), true)
+PRODUCT_COPY_FILES += \
+    device/linaro/dragonboard/shared/utils/dlkm_loader/vendor_ramdisk.modules.blocklist:$(TARGET_COPY_OUT_RAMDISK)/lib/modules/modules.blocklist
+else
 PRODUCT_COPY_FILES += \
     device/linaro/dragonboard/shared/utils/dlkm_loader/vendor_ramdisk.modules.blocklist:$(TARGET_COPY_OUT_VENDOR_RAMDISK)/lib/modules/modules.blocklist
+endif
diff --git a/shared/utils/init.utils.rc b/shared/utils/init.utils.rc
index 38d8d00..a17edfd 100644
--- a/shared/utils/init.utils.rc
+++ b/shared/utils/init.utils.rc
@@ -26,7 +26,7 @@
     disabled
     oneshot
 
-service rmtfs /vendor/bin/rmtfs -o /dev/block/platform/soc@0/1d84000.ufshc/by-name -P -r -s
+service rmtfs /vendor/bin/rmtfs -o /dev/block/by-name -P -r -s
     class core
     user root
     group system
diff --git a/shared/utils/sdcard-boot/fstab.mmc b/shared/utils/sdcard-boot/fstab.mmc
new file mode 100644
index 0000000..387a0cd
--- /dev/null
+++ b/shared/utils/sdcard-boot/fstab.mmc
@@ -0,0 +1,11 @@
+system		/system		erofs	ro				wait,logical,first_stage_mount,slotselect
+/dev/block/platform/soc@0/8804000.mmc/by-name/userdata	/data		ext4	discard,noatime,noauto_da_alloc,data=ordered,user_xattr,barrier=1,inlinecrypt	latemount,wait,formattable,fileencryption=aes-256-xts:aes-256-cts:v2+inlinecrypt_optimized,keydirectory=/metadata/vold/metadata_encryption,quota
+/dev/block/platform/soc@0/8804000.mmc/by-name/metadata	/metadata	ext4	noatime,nosuid,nodev,discard					wait,formattable,first_stage_mount,check
+/dev/block/platform/soc@0/8804000.mmc/by-name/misc /misc emmc defaults defaults
+/devices/platform/soc@0/a8f8800.usb/*/usb*		auto	auto	defaults	voldmanaged=usb1:auto
+/devices/platform/soc@0/1c00000.pci/*/usb*		auto	auto	defaults	voldmanaged=usb2:auto
+vendor		/vendor		erofs	ro				wait,logical,first_stage_mount,slotselect
+system_ext	/system_ext	erofs	ro				wait,logical,first_stage_mount,slotselect
+product		/product	erofs	ro				wait,logical,first_stage_mount,slotselect
+system_dlkm	/system_dlkm	erofs	ro				wait,logical,first_stage_mount,slotselect
+vendor_dlkm	/vendor_dlkm	erofs	ro				wait,logical,first_stage_mount,slotselect
diff --git a/shared/utils/sdcard-boot/fstab.sdhci b/shared/utils/sdcard-boot/fstab.sdhci
new file mode 100644
index 0000000..c385ba6
--- /dev/null
+++ b/shared/utils/sdcard-boot/fstab.sdhci
@@ -0,0 +1,11 @@
+system		/system		erofs	ro				wait,logical,first_stage_mount,slotselect
+/dev/block/platform/soc@0/8804000.sdhci/by-name/userdata	/data		ext4	discard,noatime,noauto_da_alloc,data=ordered,user_xattr,barrier=1,inlinecrypt	latemount,wait,formattable,fileencryption=aes-256-xts:aes-256-cts:v2+inlinecrypt_optimized,keydirectory=/metadata/vold/metadata_encryption,quota
+/dev/block/platform/soc@0/8804000.sdhci/by-name/metadata	/metadata	ext4	noatime,nosuid,nodev,discard					wait,formattable,first_stage_mount,check
+/dev/block/platform/soc@0/8804000.sdhci/by-name/misc /misc emmc defaults defaults
+/devices/platform/soc@0/a8f8800.usb/*/usb*		auto	auto	defaults	voldmanaged=usb1:auto
+/devices/platform/soc@0/1c00000.pci/*/usb*		auto	auto	defaults	voldmanaged=usb2:auto
+vendor		/vendor		erofs	ro				wait,logical,first_stage_mount,slotselect
+system_ext	/system_ext	erofs	ro				wait,logical,first_stage_mount,slotselect
+product		/product	erofs	ro				wait,logical,first_stage_mount,slotselect
+system_dlkm	/system_dlkm	erofs	ro				wait,logical,first_stage_mount,slotselect
+vendor_dlkm	/vendor_dlkm	erofs	ro				wait,logical,first_stage_mount,slotselect
diff --git a/shared/utils/sdcard-boot/sepolicy/file_contexts b/shared/utils/sdcard-boot/sepolicy/file_contexts
new file mode 100644
index 0000000..79d5de8
--- /dev/null
+++ b/shared/utils/sdcard-boot/sepolicy/file_contexts
@@ -0,0 +1,15 @@
+/dev/block/platform/soc@0/8804000\.mmc/by-name/fsc		u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/8804000\.mmc/by-name/fsg		u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/8804000\.mmc/by-name/modemst[12]	u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/8804000\.mmc/by-name/metadata		u:object_r:metadata_block_device:s0
+/dev/block/platform/soc@0/8804000\.mmc/by-name/super		u:object_r:super_block_device:s0
+/dev/block/platform/soc@0/8804000\.mmc/by-name/userdata		u:object_r:userdata_block_device:s0
+/dev/block/platform/soc@0/8804000\.mmc/by-name/misc		u:object_r:misc_block_device:s0
+
+/dev/block/platform/soc@0/8804000\.sdhci/by-name/fsc		u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/8804000\.sdhci/by-name/fsg		u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/8804000\.sdhci/by-name/modemst[12]	u:object_r:modem_block_device:s0
+/dev/block/platform/soc@0/8804000\.sdhci/by-name/metadata		u:object_r:metadata_block_device:s0
+/dev/block/platform/soc@0/8804000\.sdhci/by-name/super		u:object_r:super_block_device:s0
+/dev/block/platform/soc@0/8804000\.sdhci/by-name/userdata		u:object_r:userdata_block_device:s0
+/dev/block/platform/soc@0/8804000\.sdhci/by-name/misc		u:object_r:misc_block_device:s0