Merge "app: aboot: check battery voltage before erase or flash the whole image"
diff --git a/app/aboot/aboot.c b/app/aboot/aboot.c
index 6a46a19..7b8861b 100644
--- a/app/aboot/aboot.c
+++ b/app/aboot/aboot.c
@@ -253,7 +253,12 @@
 char sn_buf[13];
 char display_panel_buf[MAX_PANEL_BUF_SIZE];
 char panel_display_mode[MAX_RSP_SIZE];
+
+#if CHECK_BAT_VOLTAGE
 char battery_voltage[MAX_RSP_SIZE];
+char battery_soc_ok [MAX_RSP_SIZE];
+#endif
+
 char get_variant[MAX_RSP_SIZE];
 
 extern int emmc_recovery_init(void);
@@ -288,6 +293,14 @@
 	*ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
 }
 
+#if CHECK_BAT_VOLTAGE
+void update_battery_status(void)
+{
+	snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
+	snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
+}
+#endif
+
 unsigned char *update_cmdline(const char * cmdline)
 {
 	int cmdline_len = 0;
@@ -2380,13 +2393,6 @@
 
 void cmd_erase(const char *arg, void *data, unsigned sz)
 {
-#if CHECK_BAT_VOLTAGE
-	if (!target_battery_soc_ok()) {
-		fastboot_fail("Warning: battery's capacity is very low\n");
-		return;
-	}
-#endif
-
 #if VERIFIED_BOOT
 	if (target_build_variant_user())
 	{
@@ -2986,12 +2992,6 @@
 
 void cmd_flash(const char *arg, void *data, unsigned sz)
 {
-#if CHECK_BAT_VOLTAGE
-	if (!target_battery_soc_ok()) {
-		fastboot_fail("Warning: battery's capacity is very low\n");
-		return;
-	}
-#endif
 	if(target_is_emmc_boot())
 		cmd_flash_mmc(arg, data, sz);
 	else
@@ -3519,10 +3519,9 @@
 		target_is_emmc_boot()? "eMMC":"UFS");
 	fastboot_publish("variant", (const char *) get_variant);
 #if CHECK_BAT_VOLTAGE
-	snprintf(battery_voltage, MAX_RSP_SIZE, "%d",
-		target_get_battery_voltage());
+	update_battery_status();
 	fastboot_publish("battery-voltage", (const char *) battery_voltage);
-	fastboot_publish("battery-soc-ok", target_battery_soc_ok()? "yes":"no");
+	fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
 #endif
 }
 
diff --git a/app/aboot/fastboot.c b/app/aboot/fastboot.c
index 545a24d..205b74a 100644
--- a/app/aboot/fastboot.c
+++ b/app/aboot/fastboot.c
@@ -448,6 +448,10 @@
 {
 	struct fastboot_var *var;
 
+#if CHECK_BAT_VOLTAGE
+	update_battery_status();
+#endif
+
 	if (!strncmp("all", arg, strlen(arg)))
 	{
 		getvar_all();
@@ -496,6 +500,10 @@
 {
 	struct fastboot_cmd *cmd;
 	int r;
+#if CHECK_BAT_VOLTAGE
+	boolean is_first_erase_flash = false;
+#endif
+
 	dprintf(INFO,"fastboot: processing commands\n");
 
 	uint8_t *buffer = (uint8_t *)memalign(CACHE_LINE, ROUNDUP(4096, CACHE_LINE));
@@ -519,6 +527,24 @@
 		buffer[r] = 0;
 		dprintf(INFO,"fastboot: %s\n", buffer);
 
+#if CHECK_BAT_VOLTAGE
+		/* check battery voltage before erase or flash image */
+		if (!strncmp((const char*) buffer, "getvar:partition-type", 21))
+			is_first_erase_flash = true;
+
+		if (is_first_erase_flash) {
+			if (!strncmp((const char*) buffer, "erase", 5) ||
+				!strncmp((const char*) buffer, "flash", 5)) {
+				if (!target_battery_soc_ok()) {
+					dprintf(INFO,"fastboot: battery voltage: %d\n",
+						target_get_battery_voltage());
+					fastboot_fail("Warning: battery's capacity is very low\n");
+					return;
+				}
+			}
+		}
+#endif
+
 		fastboot_state = STATE_COMMAND;
 
 		for (cmd = cmdlist; cmd; cmd = cmd->next) {
@@ -528,6 +554,15 @@
 				    (void*) download_base, download_size);
 			if (fastboot_state == STATE_COMMAND)
 				fastboot_fail("unknown reason");
+
+#if CHECK_BAT_VOLTAGE
+			if (!strncmp((const char*) buffer, "erase", 5) ||
+				!strncmp((const char*) buffer, "flash", 5)) {
+				if (is_first_erase_flash) {
+					is_first_erase_flash = false;
+				}
+			}
+#endif
 			goto again;
 		}
 
diff --git a/include/target.h b/include/target.h
index 6784206..1da87f1 100644
--- a/include/target.h
+++ b/include/target.h
@@ -96,6 +96,10 @@
 uint32_t get_vibration_type();
 #endif
 
+#if CHECK_BAT_VOLTAGE
+void update_battery_status(void);
+#endif
+
 uint32_t target_get_battery_voltage();
 bool target_battery_soc_ok();
 bool target_battery_is_present();