[PATCHv5 2/8] kexec: Isolate probe method

Pingfan Liu piliu at redhat.com
Mon Jul 17 06:07:26 PDT 2023


Isolating the calling to the probe method, so in a later patch, a new
prototype probe2 interface can be introduced without exposure of the
change.

Signed-off-by: Pingfan Liu <piliu at redhat.com>
To: kexec at lists.infradead.org
Cc: horms at verge.net.au
Cc: ardb at kernel.org
Cc: jeremy.linton at arm.com
---
 kexec/kexec.c | 81 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 49 insertions(+), 32 deletions(-)

diff --git a/kexec/kexec.c b/kexec/kexec.c
index 36bb2ad..d5e6dc0 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -694,6 +694,48 @@ static void update_purgatory(struct kexec_info *info)
 			   sizeof(digest));
 }
 
+static int initialze_probe(const char *kern_fname,
+		char **kernel_buf, off_t *kernel_size, int *kernel_fd)
+{
+	static int probe_type = -1;
+
+	if (probe_type >= 0)
+		return probe_type;
+
+	*kernel_fd = open(kern_fname, O_RDONLY);
+	if (*kernel_fd == -1) {
+		die("Failed to open file %s:%s\n", kern_fname,
+			strerror(errno));
+		return EFAILED;
+	}
+
+	/* slurp in the input kernel */
+	*kernel_buf = slurp_decompress_file(kern_fname, kernel_size);
+	probe_type = 0;
+
+	return probe_type;
+}
+
+static int probe(struct file_type *ft, const char *kern_fname,
+		char **kernel_buf, off_t *kernel_size, int *kernel_fd)
+{
+	int ret;
+
+	initialze_probe(kern_fname, kernel_buf, kernel_size, kernel_fd);
+#ifdef __aarch64__
+	/* handle Image.gz like cases */
+	if (is_zlib_file(kern_fname, &kernel_size)) {
+		if ((ret = ft->probe(kern_fname, *kernel_size)) >= 0)
+			*kernel_fd = ret;
+	} else {
+		ret = ft->probe(*kernel_buf, *kernel_size);
+	}
+#else
+		ret = ft->probe(*kernel_buf, *kernel_size);
+#endif
+	return ret;
+}
+
 /*
  *	Load the new kernel
  */
@@ -703,6 +745,7 @@ static int my_load(const char *type, int fileind, int argc, char **argv,
 	char *kernel;
 	char *kernel_buf;
 	off_t kernel_size;
+	int kernel_fd = -1;
 	int i = 0;
 	int result;
 	struct kexec_info info;
@@ -720,11 +763,6 @@ static int my_load(const char *type, int fileind, int argc, char **argv,
 		return -1;
 	}
 	kernel = argv[fileind];
-	/* slurp in the input kernel */
-	kernel_buf = slurp_decompress_file(kernel, &kernel_size);
-
-	dbgprintf("kernel: %p kernel_size: %#llx\n",
-		  kernel_buf, (unsigned long long)kernel_size);
 
 	if (get_memory_ranges(&info.memory_range, &info.memory_ranges,
 		info.kexec_flags) < 0 || info.memory_ranges == 0) {
@@ -742,13 +780,13 @@ static int my_load(const char *type, int fileind, int argc, char **argv,
 			return -1;
 		} else {
 			/* make sure our file is really of that type */
-			if (file_type[i].probe(kernel_buf, kernel_size) < 0)
+			if (probe(&file_type[i], kernel, &kernel_buf, &kernel_size, &kernel_fd) < 0)
 				guess_only = 1;
 		}
 	}
 	if (!type || guess_only) {
 		for (i = 0; i < file_types; i++) {
-			if (file_type[i].probe(kernel_buf, kernel_size) == 0)
+			if (probe(&file_type[i], kernel, &kernel_buf, &kernel_size, &kernel_fd) == 0)
 				break;
 		}
 		if (i == file_types) {
@@ -1263,10 +1301,10 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
 			unsigned long flags) {
 
 	char *kernel;
-	int kernel_fd, i;
+	int i, kernel_fd = -1;
 	struct kexec_info info;
 	int ret = 0;
-	char *kernel_buf;
+	char *kernel_buf = NULL;
 	off_t kernel_size;
 
 	memset(&info, 0, sizeof(info));
@@ -1290,31 +1328,10 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
 
 	kernel = argv[fileind];
 
-	kernel_fd = open(kernel, O_RDONLY);
-	if (kernel_fd == -1) {
-		fprintf(stderr, "Failed to open file %s:%s\n", kernel,
-				strerror(errno));
-		return EFAILED;
-	}
-
-	/* slurp in the input kernel */
-	kernel_buf = slurp_decompress_file(kernel, &kernel_size);
-
 	for (i = 0; i < file_types; i++) {
-#ifdef __aarch64__
-		/* handle Image.gz like cases */
-		if (is_zlib_file(kernel, &kernel_size)) {
-			if ((ret = file_type[i].probe(kernel, kernel_size)) >= 0) {
-				kernel_fd = ret;
-				break;
-			}
-		} else
-			if (file_type[i].probe(kernel_buf, kernel_size) >= 0)
-				break;
-#else
-		if (file_type[i].probe(kernel_buf, kernel_size) >= 0)
+		ret = probe(&file_type[i], kernel, &kernel_buf, &kernel_size, &kernel_fd);
+		if (ret >=0)
 			break;
-#endif
 	}
 
 	if (i == file_types) {
-- 
2.31.1




More information about the kexec mailing list