Add backup size checking
Converted bootloader.cpp to TWRP and removed AOSP fstab parsing
from startup
diff --git a/Android.mk b/Android.mk
index 4fd182d..e13af68 100644
--- a/Android.mk
+++ b/Android.mk
@@ -20,7 +20,7 @@
LOCAL_SRC_FILES := \
recovery.cpp \
- bootloader.cpp \
+ twbootloader.cpp \
install.cpp \
roots.cpp \
ui.cpp \
diff --git a/partition.cpp b/partition.cpp
index a9d5091..6f21309 100644
--- a/partition.cpp
+++ b/partition.cpp
@@ -101,9 +101,8 @@
if (full_line[index] <= 32)
full_line[index] = '\0';
}
- string mount_pt(full_line);
- Mount_Point = mount_pt;
- LOGI("Processing '%s'\n", mount_pt);
+ Mount_Point = full_line;
+ LOGI("Processing '%s'\n", Mount_Point.c_str());
Backup_Path = Mount_Point;
index = Mount_Point.size();
while (index < line_len) {
@@ -1076,7 +1075,7 @@
string Full_FileName, Split_FileName, Tar_Args, Command;
int use_compression, index, backup_count;
struct stat st;
- unsigned long long total_bsize = 0;
+ unsigned long long total_bsize = 0, file_size;
if (!Mount(true))
return false;
@@ -1110,11 +1109,12 @@
ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
system(Command.c_str()); // sending backup command formed earlier above
- if (stat(Full_FileName.c_str(), &st) != 0 || st.st_size == 0) {
- LOGE("File size is zero bytes. Aborting...\n\n"); // oh noes! file size is 0, abort! abort!
+ file_size = TWFunc::Get_File_Size(Full_FileName);
+ if (file_size == 0) {
+ LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str()); // oh noes! file size is 0, abort! abort!
return false;
}
- total_bsize += st.st_size;
+ total_bsize += file_size;
}
ui_print(" * Total size: %llu bytes.\n", total_bsize);
system("cd /tmp && rm -rf list");
@@ -1126,6 +1126,10 @@
Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
LOGI("Backup command: '%s'\n", Command.c_str());
system(Command.c_str());
+ if (TWFunc::Get_File_Size(Full_FileName) == 0) {
+ LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
+ return false;
+ }
}
return true;
}
@@ -1146,6 +1150,10 @@
Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
LOGI("Backup command: '%s'\n", Command.c_str());
system(Command.c_str());
+ if (TWFunc::Get_File_Size(Full_FileName) != Backup_Size) {
+ LOGE("Backup file size %lu for '%s' is does not match backup size %llu.\n", TWFunc::Get_File_Size(Full_FileName), Full_FileName.c_str(), Backup_Size);
+ return false;
+ }
return true;
}
@@ -1165,6 +1173,11 @@
Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
LOGI("Backup command: '%s'\n", Command.c_str());
system(Command.c_str());
+ if (TWFunc::Get_File_Size(Full_FileName) == 0) {
+ // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
+ LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
+ return false;
+ }
return true;
}
diff --git a/partitions.hpp b/partitions.hpp
index c04f0cf..5bd1197 100644
--- a/partitions.hpp
+++ b/partitions.hpp
@@ -61,6 +61,10 @@
virtual bool Update_Size(bool Display_Error); // Updates size information
virtual void Recreate_Media_Folder(); // Recreates the /data/media folder
+public:
+ string Current_File_System; // Current file system
+ string Actual_Block_Device; // Actual block device (one of primary, alternate, or decrypted)
+
protected:
bool Process_Fstab_Line(string Line, bool Display_Error); // Processes a fstab line
void Find_Actual_Block_Device(); // Determines the correct block device and stores it in Actual_Block_Device
@@ -77,7 +81,6 @@
string Symlink_Mount_Point; // /sdcard could be the symlink mount point for /data/media
string Mount_Point; // Mount point for this partition (e.g. /system or /data)
string Backup_Path; // Path for backup
- string Actual_Block_Device; // Actual block device (one of primary, alternate, or decrypted)
string Primary_Block_Device; // Block device (e.g. /dev/block/mmcblk1p1)
string Alternate_Block_Device; // Alternate block device (e.g. /dev/block/mmcblk1)
string Decrypted_Block_Device; // Decrypted block device available after decryption
@@ -100,7 +103,6 @@
bool Has_Android_Secure; // Indicates the presence of .android_secure on this partition
bool Is_Storage; // Indicates if this partition is used for storage for backup, restore, and installing zips
string Storage_Path; // Indicates the path to the storage -- root indicates mount point, media/ indicates e.g. /data/media
- string Current_File_System; // Current file system
string Fstab_File_System; // File system from the recovery.fstab
int Format_Block_Size; // Block size for formatting
diff --git a/recovery.cpp b/recovery.cpp
index 8cf3106..d6ed79c 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -803,7 +803,7 @@
//ui->Init();
//ui->SetBackground(RecoveryUI::NONE);
- load_volume_table();
+ //load_volume_table();
// Load default values to set DataManager constants and handle ifdefs
DataManager_LoadDefaults();
diff --git a/twbootloader.cpp b/twbootloader.cpp
new file mode 100644
index 0000000..905c78e
--- /dev/null
+++ b/twbootloader.cpp
@@ -0,0 +1,204 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "bootloader.h"
+#include "common.h"
+extern "C" {
+#include "mtdutils/mtdutils.h"
+}
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include "partitions.hpp"
+
+static int get_bootloader_message_mtd(struct bootloader_message *out, const TWPartition* Partition);
+static int set_bootloader_message_mtd(const struct bootloader_message *in, const TWPartition* Partition);
+static int get_bootloader_message_block(struct bootloader_message *out, const TWPartition* Partition);
+static int set_bootloader_message_block(const struct bootloader_message *in, const TWPartition* Partition);
+
+int get_bootloader_message(struct bootloader_message *out) {
+ TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
+ if (Part == NULL) {
+ //LOGE("Cannot load volume /misc!\n");
+ return -1;
+ }
+ if (Part->Current_File_System == "mtd") {
+ return get_bootloader_message_mtd(out, Part);
+ } else if (Part->Current_File_System == "emmc") {
+ return get_bootloader_message_block(out, Part);
+ }
+ LOGE("unknown misc partition fs_type \"%s\"\n", Part->Current_File_System.c_str());
+ return -1;
+}
+
+int set_bootloader_message(const struct bootloader_message *in) {
+ TWPartition* Part = PartitionManager.Find_Partition_By_Path("/misc");
+ if (Part == NULL) {
+ //LOGE("Cannot load volume /misc!\n");
+ return -1;
+ }
+ if (Part->Current_File_System == "mtd") {
+ return set_bootloader_message_mtd(in, Part);
+ } else if (Part->Current_File_System == "emmc") {
+ return set_bootloader_message_block(in, Part);
+ }
+ LOGE("unknown misc partition fs_type \"%s\"\n", Part->Current_File_System.c_str());
+ return -1;
+}
+
+// ------------------------------
+// for misc partitions on MTD
+// ------------------------------
+
+static const int MISC_PAGES = 3; // number of pages to save
+static const int MISC_COMMAND_PAGE = 1; // bootloader command is this page
+
+static int get_bootloader_message_mtd(struct bootloader_message *out,
+ const TWPartition* Partition) {
+ size_t write_size;
+ mtd_scan_partitions();
+ const MtdPartition *part = mtd_find_partition_by_name(Partition->Actual_Block_Device.c_str());
+ if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
+ LOGE("Can't find %s\n", Partition->Actual_Block_Device.c_str());
+ return -1;
+ }
+
+ MtdReadContext *read = mtd_read_partition(part);
+ if (read == NULL) {
+ LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+
+ const ssize_t size = write_size * MISC_PAGES;
+ char data[size];
+ ssize_t r = mtd_read_data(read, data, size);
+ if (r != size) LOGE("Can't read %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ mtd_read_close(read);
+ if (r != size) return -1;
+
+ memcpy(out, &data[write_size * MISC_COMMAND_PAGE], sizeof(*out));
+ return 0;
+}
+static int set_bootloader_message_mtd(const struct bootloader_message *in,
+ const TWPartition* Partition) {
+ size_t write_size;
+ mtd_scan_partitions();
+ const MtdPartition *part = mtd_find_partition_by_name(Partition->Actual_Block_Device.c_str());
+ if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) {
+ LOGE("Can't find %s\n", Partition->Actual_Block_Device.c_str());
+ return -1;
+ }
+
+ MtdReadContext *read = mtd_read_partition(part);
+ if (read == NULL) {
+ LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+
+ ssize_t size = write_size * MISC_PAGES;
+ char data[size];
+ ssize_t r = mtd_read_data(read, data, size);
+ if (r != size) LOGE("Can't read %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ mtd_read_close(read);
+ if (r != size) return -1;
+
+ memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in));
+
+ MtdWriteContext *write = mtd_write_partition(part);
+ if (write == NULL) {
+ LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+ if (mtd_write_data(write, data, size) != size) {
+ LOGE("Can't write %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ mtd_write_close(write);
+ return -1;
+ }
+ if (mtd_write_close(write)) {
+ LOGE("Can't finish %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+
+ LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : "");
+ return 0;
+}
+
+
+// ------------------------------------
+// for misc partitions on block devices
+// ------------------------------------
+
+static void wait_for_device(const char* fn) {
+ int tries = 0;
+ int ret;
+ struct stat buf;
+ do {
+ ++tries;
+ ret = stat(fn, &buf);
+ if (ret) {
+ printf("stat %s try %d: %s\n", fn, tries, strerror(errno));
+ sleep(1);
+ }
+ } while (ret && tries < 10);
+ if (ret) {
+ printf("failed to stat %s\n", fn);
+ }
+}
+
+static int get_bootloader_message_block(struct bootloader_message *out,
+ const TWPartition* Partition) {
+ wait_for_device(Partition->Actual_Block_Device.c_str());
+ FILE* f = fopen(Partition->Actual_Block_Device.c_str(), "rb");
+ if (f == NULL) {
+ LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+ struct bootloader_message temp;
+ int count = fread(&temp, sizeof(temp), 1, f);
+ if (count != 1) {
+ LOGE("Failed reading %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+ if (fclose(f) != 0) {
+ LOGE("Failed closing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+ memcpy(out, &temp, sizeof(temp));
+ return 0;
+}
+
+static int set_bootloader_message_block(const struct bootloader_message *in,
+ const TWPartition* Partition) {
+ wait_for_device(Partition->Actual_Block_Device.c_str());
+ FILE* f = fopen(Partition->Actual_Block_Device.c_str(), "wb");
+ if (f == NULL) {
+ LOGE("Can't open %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+ int count = fwrite(in, sizeof(*in), 1, f);
+ if (count != 1) {
+ LOGE("Failed writing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+ if (fclose(f) != 0) {
+ LOGE("Failed closing %s\n(%s)\n", Partition->Actual_Block_Device.c_str(), strerror(errno));
+ return -1;
+ }
+ return 0;
+}
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index a681de2..066e6e5 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -221,9 +221,9 @@
bool TWFunc::Path_Exists(string Path) {
// Check to see if the Path exists
- struct statfs st;
+ struct stat st;
- if (statfs(Path.c_str(), &st) != 0)
+ if (stat(Path.c_str(), &st) != 0)
return false;
else
return true;
@@ -249,4 +249,12 @@
DataManager::SetValue("tw_operation", Display_Text);
DataManager::SetValue("tw_partition", Partition_Name);
+}
+
+unsigned long TWFunc::Get_File_Size(string Path) {
+ struct stat st;
+
+ if (stat(Path.c_str(), &st) != 0)
+ return 0;
+ return st.st_size;
}
\ No newline at end of file
diff --git a/twrp-functions.hpp b/twrp-functions.hpp
index e4af7ee..aebd7c3 100644
--- a/twrp-functions.hpp
+++ b/twrp-functions.hpp
@@ -22,6 +22,7 @@
static bool Path_Exists(string Path); // Returns true if the path exists
static void GUI_Operation_Text(string Read_Value, string Default_Text); // Updates text for display in the GUI, e.g. Backing up %partition name%
static void GUI_Operation_Text(string Read_Value, string Partition_Name, string Default_Text); // Same as above but includes partition name
+ static unsigned long Get_File_Size(string Path); // Returns the size of a file
};