Fix MTD Mounting

Pulled part of change from kokotas in this patch set:
http://review.teamw.in/#/c/586/

Change-Id: If1c5b1563df16f9c561c94f61010f6a99f36efa0
diff --git a/partition.cpp b/partition.cpp
index f92d62f..9ab60ba 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -637,6 +637,8 @@
 			if (sscanf(device,"mtd%d", &deviceId) == 1) {
 				sprintf(device, "/dev/block/mtdblock%d", deviceId);
 				Primary_Block_Device = device;
+				fclose(fp);
+				return true;
 			}
 		}
 	}
@@ -828,22 +830,41 @@
 	}
 	if (Fstab_File_System == "yaffs2") {
 		// mount an MTD partition as a YAFFS2 filesystem.
-		mtd_scan_partitions();
-		const MtdPartition* partition;
-		partition = mtd_find_partition_by_name(MTD_Name.c_str());
-		if (partition == NULL) {
-			LOGERR("Failed to find '%s' partition to mount at '%s'\n",
-			MTD_Name.c_str(), Mount_Point.c_str());
-			return false;
-		}
-		if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
-			if (Display_Error)
-				LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
-			else
-				LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
-			return false;
-		} else
+		const unsigned long flags = MS_NOATIME | MS_NODEV | MS_NODIRATIME;
+		if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags, NULL) < 0) {
+			if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Fstab_File_System.c_str(), flags | MS_RDONLY, NULL) < 0) {
+				if (Display_Error)
+					LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
+				else
+					LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
+				return false;
+			} else {
+				LOGINFO("Mounted '%s' (MTD) as RO\n", Mount_Point.c_str());
+				return true;
+			}
+		} else {
+			struct stat st;
+			string test_path = Mount_Point;
+			if (stat(test_path.c_str(), &st) < 0) {
+				if (Display_Error)
+					LOGERR("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
+				else
+					LOGINFO("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
+				return false;
+			}
+			mode_t new_mode = st.st_mode | S_IXUSR | S_IXGRP | S_IXOTH;
+			if (new_mode != st.st_mode) {
+				LOGINFO("Fixing execute permissions for %s\n", Mount_Point.c_str());
+				if (chmod(Mount_Point.c_str(), new_mode) < 0) {
+					if (Display_Error)
+						LOGERR("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
+					else
+						LOGINFO("Couldn't fix permissions for %s: %s\n", Mount_Point.c_str(), strerror(errno));
+					return false;
+				}
+			}
 			return true;
+		}
 	} else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
 #ifdef TW_NO_EXFAT_FUSE
 		if (Current_File_System == "exfat") {