Mount system as read-only by default

Mounting system as rw can prevent future OTA updates. The purpose
of this patch set is to prevent TWRP from mounting sytem as rw on
the first boot. Device maintainers should update their twrp.fstab
files on these devices to include an additional line:
/system_image emmc /dev/block/../system

This line will allow TWRP to create a raw system image backup to
ensure that the user can return to an original state for future
OTA updates.

Change-Id: I8929d85bc3a5b96cc564bc7f734b58d5612ec833
diff --git a/twrp.cpp b/twrp.cpp
index 0c012d7..b7fc981 100644
--- a/twrp.cpp
+++ b/twrp.cpp
@@ -332,13 +332,27 @@
 	PartitionManager.Disable_MTP();
 #endif
 
+	// Check if system has never been changed
+	if (DataManager::GetIntValue("tw_mount_system_ro") != 0 && DataManager::GetIntValue("tw_never_show_system_ro_page") == 0) {
+		TWPartition* sys = PartitionManager.Find_Partition_By_Path("/system");
+		if (sys && sys->Check_Lifetime_Writes() == 0) {
+			LOGINFO("System writes is 0, show system_readonly page\n");
+			DataManager::SetValue("tw_back", "main");
+			if (gui_startPage("system_readonly", 1, 1) != 0) {
+				LOGERR("Failed to start system_readonly GUI page.\n");
+			}
+		} else {
+			DataManager::SetValue("tw_mount_system_ro", 0);
+		}
+	}
+
 	// Launch the main GUI
 	gui_start();
 
 	// Disable flashing of stock recovery
 	TWFunc::Disable_Stock_Recovery_Replace();
 	// Check for su to see if the device is rooted or not
-	if (PartitionManager.Mount_By_Path("/system", false)) {
+	if (PartitionManager.Mount_By_Path("/system", false) && DataManager::GetIntValue("tw_mount_system_ro") == 0) {
 		if (TWFunc::Path_Exists("/supersu/su") && !TWFunc::Path_Exists("/system/bin/su") && !TWFunc::Path_Exists("/system/xbin/su") && !TWFunc::Path_Exists("/system/bin/.ext/.su")) {
 			// Device doesn't have su installed
 			DataManager::SetValue("tw_busy", 1);