diff --git a/firehose.c b/firehose.c
index ebe4696c34709910d5b89a7958009c502d6a880d..1e286ca497e1e83504f85d5317d1038a754b6906 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 4557ec7dd3817a910b61d954445c1fb5889f467f..574c47113c47ce0c64bf0ded279a42cbe8a9d47a 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 9edc0cb9c443545644f38b367523bb6a47c03cec..fa8366e80e0fbc2eb69101bb3639578a0f76b23e 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 bba9e0528c72929e151216e05c3ce3a7dd5a8aca..7ee0e93baca678f5f18928ad1b89b623fc762234 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 be27c5206311e6721d621cfc8414bec3097388cd..4e340f77c66434addd6d2818f4a43c0948205571 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