imx: add a compilation flag to support the persistence of the rootfs.

The image generation for the board NXP® i.MX 8MQuad Evaluation Kit (EVK)
has been refactored, so the rootfs can either be loaded from a RAMDisk
or from a partition on a media storage such as an SD card, by enabling
the compilation flag USE_PERSISTENT_ROOTFS.
In addition, the image size is now dynamically allocated, depending on
the size of the partition(s) that is/are part of the image.

The flash-image target (part of the all target) produces a out/boot.img
which can be copied to a SD card using the command:
dd if=out/boot.img of=/dev/sdX bs=1M conv=fsync

Where 'X' has to be adjusted to the device of the SD card when connected
to the host system.

     ****** Please be careful to choose the correct device ******
     ****** or there's a risk of damaging the host system. ******

Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
Reviewed-by: Joakim Bech <joakim.bech@linaro.org>
Signed-off-by: Jämes Ménétrey <james.menetrey@unine.ch>
diff --git a/imx.mk b/imx.mk
index f5a4eef..caa5073 100644
--- a/imx.mk
+++ b/imx.mk
@@ -10,6 +10,8 @@
 
 OPTEE_OS_PLATFORM = imx-mx8mqevk
 BR2_TARGET_GENERIC_GETTY_PORT = ttymxc0
+BR2_TARGET_ROOTFS_EXT2=y
+BR2_TARGET_ROOTFS_EXT2_4=y
 
 include common.mk
 
@@ -175,6 +177,42 @@
 	$(U-BOOT_PATH)/tools/mkimage -T script -C none -n 'Boot script' \
 		-d $< $@
 
+################################################################################
+# Flash images
+################################################################################
+USE_PERSISTENT_ROOTFS ?= 0
+
+# Configuration of the BOOT partition
+FLASH_PARTITIONS_BLOCK_SIZE = 512
+FLASH_PARTITION_BOOT_START_BLOCK = 16384
+FLASH_PARTITION_BOOT_SIZE_IN_BYTES = \
+	$(shell echo $$(( 64 * 1024 * 1024 )))
+FLASH_PARTITION_BOOT_SIZE_IN_BLOCKS = \
+	$(shell echo $$(( $(FLASH_PARTITION_BOOT_SIZE_IN_BYTES) / $(FLASH_PARTITIONS_BLOCK_SIZE) )))
+FLASH_PARTITIONS_TABLE = "\
+	start=$(FLASH_PARTITION_BOOT_START_BLOCK) \
+	size=$(FLASH_PARTITION_BOOT_SIZE_IN_BLOCKS) \
+	type=7\n"
+FLASH_IMAGE_SIZE = \
+	$(shell echo $$(( $(FLASH_PARTITION_BOOT_START_BLOCK) * $(FLASH_PARTITIONS_BLOCK_SIZE) \
+	+ $(FLASH_PARTITION_BOOT_SIZE_IN_BYTES) )))
+
+# Configuration of the ROOTFS partition if enabled
+ifeq ($(USE_PERSISTENT_ROOTFS),1)
+FLASH_PARTITION_ROOTFS_IMAGE_PATH = $(ROOT)/out-br/images/rootfs.ext4
+FLASH_PARTITION_ROOTFS_START_BLOCK = \
+	$(shell echo $$(( $(FLASH_PARTITION_BOOT_START_BLOCK) + $(FLASH_PARTITION_BOOT_SIZE_IN_BLOCKS) )))
+FLASH_PARTITION_ROOTFS_SIZE_IN_BYTES = \
+	$(shell stat -L --printf="%s" $(FLASH_PARTITION_ROOTFS_IMAGE_PATH))
+FLASH_PARTITION_ROOTFS_SIZE_IN_BLOCKS = \
+	$(shell echo $$(( $(FLASH_PARTITION_ROOTFS_SIZE_IN_BYTES) / $(FLASH_PARTITIONS_BLOCK_SIZE) )))
+FLASH_PARTITIONS_TABLE += "\
+	start=$(FLASH_PARTITION_ROOTFS_START_BLOCK) \
+	size=$(FLASH_PARTITION_ROOTFS_SIZE_IN_BLOCKS) \
+	type=83\n"
+FLASH_IMAGE_SIZE := $(shell echo $$(( $(FLASH_IMAGE_SIZE) + $(FLASH_PARTITION_ROOTFS_SIZE_IN_BYTES) )))
+endif
+
 .PHONY: flash-image
 flash-image: buildroot mkimage
 	$(MAKE) flash-image-only
@@ -182,15 +220,22 @@
 .PHONY: flash-image-only
 flash-image-only: $(ROOT)/out-br/images/ramdisk.img $(ROOT)/out/boot.scr
 	rm -f $(BOOT_IMG)
-	truncate -s 128M ${BOOT_IMG}
-	echo -ne "16384 64M 7\n147456 + 83\n" | sfdisk ${BOOT_IMG}
-	mformat -i ${BOOT_IMG}.fat -n 64 -h 255 -T 131072 -v "BOOT IMG" -C ::
-	mcopy -i ${BOOT_IMG}.fat $(LINUX_PATH)/arch/arm64/boot/Image ::
-	mcopy -i ${BOOT_IMG}.fat \
+	truncate -s $(FLASH_IMAGE_SIZE) $(BOOT_IMG)
+	echo -ne $(FLASH_PARTITIONS_TABLE) | sfdisk $(BOOT_IMG)
+	mformat -i $(BOOT_IMG).fat -n 64 -h 255 -T 131072 -v "BOOT IMG" -C ::
+	mcopy -i $(BOOT_IMG).fat $(LINUX_PATH)/arch/arm64/boot/Image ::
+	mcopy -i $(BOOT_IMG).fat \
 		$(LINUX_PATH)/arch/arm64/boot/dts/freescale/imx8mq-evk.dtb ::
-	mcopy -i ${BOOT_IMG}.fat $(ROOT)/out-br/images/ramdisk.img ::
-	mcopy -i ${BOOT_IMG}.fat $(ROOT)/out/boot.scr ::
-	dd if=${BOOT_IMG}.fat of=${BOOT_IMG} bs=512 seek=16384 \
-		conv=fsync,notrunc
-	dd if=${ROOT}/imx-mkimage/iMX8M/flash.bin of=${BOOT_IMG} bs=1k seek=33 \
+	mcopy -i $(BOOT_IMG).fat $(ROOT)/out/boot.scr ::
+
+ifeq ($(USE_PERSISTENT_ROOTFS),1)
+	dd if=$(FLASH_PARTITION_ROOTFS_IMAGE_PATH) of=$(BOOT_IMG) bs=$(FLASH_PARTITIONS_BLOCK_SIZE) \
+		seek=$(FLASH_PARTITION_ROOTFS_START_BLOCK) conv=fsync,notrunc
+else
+	mcopy -i $(BOOT_IMG).fat $(ROOT)/out-br/images/ramdisk.img ::
+endif
+
+	dd if=$(BOOT_IMG).fat of=$(BOOT_IMG) bs=$(FLASH_PARTITIONS_BLOCK_SIZE) \
+		seek=$(FLASH_PARTITION_BOOT_START_BLOCK) conv=fsync,notrunc
+	dd if=$(ROOT)/imx-mkimage/iMX8M/flash.bin of=$(BOOT_IMG) bs=1k seek=33 \
 		conv=fsync,notrunc
diff --git a/imx/u-boot_boot_script b/imx/u-boot_boot_script
index 4bb473d..a2ee4fe 100644
--- a/imx/u-boot_boot_script
+++ b/imx/u-boot_boot_script
@@ -13,5 +13,10 @@
 fi;
 fdt print /firmware/optee;
 run loadimage;
-fatload mmc ${mmcdev}:${mmcpart}  ${initrd_addr} ramdisk.img;
-booti ${loadaddr} ${initrd_addr} ${fdt_addr};
+if fatload mmc ${mmcdev}:${mmcpart}  ${initrd_addr} ramdisk.img; then
+	booti ${loadaddr} ${initrd_addr} ${fdt_addr};
+else
+	echo "Booting on the persistent file system ..."
+	run loadimage;run mmcargs;
+	booti ${loadaddr} - ${fdt_addr};
+fi;
\ No newline at end of file