From a29f44cc07ce5d1d30a846d711a5decee09f8181 Mon Sep 17 00:00:00 2001
From: Bjorn Andersson <bjorn.andersson@linaro.org>
Date: Wed, 21 Sep 2016 11:52:27 -0700
Subject: [PATCH] qdl: Make failed program or patch operation stop execution

Failures to apply program or patch operations are silently ignored,
forcing the user to inspect the logs to conclude if the flashing
succeded. Instead make the operations halt execution.

Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
---
 firehose.c | 15 +++++++++++----
 patch.c    |  7 +++++--
 patch.h    |  2 +-
 program.c  |  7 +++++--
 program.h  |  2 +-
 5 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/firehose.c b/firehose.c
index ebe4696..1e286ca 100644
--- a/firehose.c
+++ b/firehose.c
@@ -250,7 +250,7 @@ static int firehose_configure(int fd)
 	return firehose_read(fd, -1, firehose_nop_parser);
 }
 
-static void firehose_program(int usbfd, struct program *program, int fd)
+static int firehose_program(int usbfd, struct program *program, int fd)
 {
 	unsigned num_sectors;
 	struct stat sb;
@@ -324,9 +324,10 @@ static void firehose_program(int usbfd, struct program *program, int fd)
 
 out:
 	xmlFreeDoc(doc);
+	return ret;
 }
 
-static void firehose_apply_patch(int fd, struct patch *patch)
+static int firehose_apply_patch(int fd, struct patch *patch)
 {
 	xmlNode *root;
 	xmlNode *node;
@@ -358,6 +359,7 @@ static void firehose_apply_patch(int fd, struct patch *patch)
 
 out:
 	xmlFreeDoc(doc);
+	return ret;
 }
 
 static int firehose_set_bootable(int fd, int lun)
@@ -439,8 +441,13 @@ int firehose_run(int fd)
 	}
 #endif
 
-	program_execute(fd, firehose_program);
-	patch_execute(fd, firehose_apply_patch);
+	ret = program_execute(fd, firehose_program);
+	if (ret)
+		return ret;
+
+	ret = patch_execute(fd, firehose_apply_patch);
+	if (ret)
+		return ret;
 
 	firehose_set_bootable(fd, 1);
 
diff --git a/patch.c b/patch.c
index 4557ec7..574c471 100644
--- a/patch.c
+++ b/patch.c
@@ -87,15 +87,18 @@ int patch_load(const char *patch_file)
 	return 0;
 }
 	
-int patch_execute(int fd, void (*apply)(int fd, struct patch *patch))
+int patch_execute(int fd, int (*apply)(int fd, struct patch *patch))
 {
 	struct patch *patch;
+	int ret;
 
 	for (patch = patches; patch; patch = patch->next) {
 		if (strcmp(patch->filename, "DISK"))
 			continue;
 
-		apply(fd, patch);
+		ret = apply(fd, patch);
+		if (ret)
+			return ret;
 	}
 
 	return 0;
diff --git a/patch.h b/patch.h
index 9edc0cb..fa8366e 100644
--- a/patch.h
+++ b/patch.h
@@ -15,6 +15,6 @@ struct patch {
 };
 
 int patch_load(const char *patch_file);
-int patch_execute(int fd, void (*apply)(int fd, struct patch *patch));
+int patch_execute(int fd, int (*apply)(int fd, struct patch *patch));
 
 #endif
diff --git a/program.c b/program.c
index bba9e05..7ee0e93 100644
--- a/program.c
+++ b/program.c
@@ -117,9 +117,10 @@ int program_load(const char *program_file)
 	return 0;
 }
 	
-int program_execute(int usbfd, void (*apply)(int usbfd, struct program *program, int fd))
+int program_execute(int usbfd, int (*apply)(int usbfd, struct program *program, int fd))
 {
 	struct program *program;
+	int ret;
 	int fd;
 
 	for (program = programes; program; program = program->next) {
@@ -131,9 +132,11 @@ int program_execute(int usbfd, void (*apply)(int usbfd, struct program *program,
 				continue;
 			}
 		}
-		apply(usbfd, program, fd);
+		ret = apply(usbfd, program, fd);
 
 		close(fd);
+		if (ret)
+			return ret;
 	}
 
 	return 0;
diff --git a/program.h b/program.h
index be27c52..4e340f7 100644
--- a/program.h
+++ b/program.h
@@ -20,6 +20,6 @@ struct program {
 };
 
 int program_load(const char *program_file);
-int program_execute(int usbfd, void (*apply)(int usbfd, struct program *program, int fd));
+int program_execute(int usbfd, int (*apply)(int usbfd, struct program *program, int fd));
 
 #endif
-- 
GitLab