Skip to contentSkip to conversation

image-fit: use real size of fit image

PATCH In review Daniel Brát 2 participants 2 messages Jul 29, 2022 – Aug 8, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 image-fit: use real size of fit image

The real size of the fit image might be significantly smaller than it may appear to be based on the specified filename. For example, if path to raw disk partition is passed (eg. /dev/disk0.1), the size of the partition itself might be several times larger than the fit image it contains at the moment (so it has headroom for possible future image size changes).
This modification uses the fdt header field 'totalsize' to read-in only what is needed.

---
v2: use fdt32_to_cpu to read the totalsize from header

---
 common/image-fit.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
common/image-fit.c +24 -3
index a410632d7..de65e3dd1 100644
@@ -774,13 +774,18 @@ struct fit_handle *fit_open_buf(const void *buf, size_t size, bool verbose,
 				enum bootm_verify verify)
 {
 	struct fit_handle *handle;
+	struct fdt_header *header;
 	int ret;
 
+	if (size < sizeof(*header))
+		return ERR_PTR(-EINVAL);
+
+	header = (struct fdt_header*)buf;
 	handle = xzalloc(sizeof(struct fit_handle));
 
 	handle->verbose = verbose;
 	handle->fit = buf;
-	handle->size = size;
+	handle->size = fdt32_to_cpu(header->totalsize);
 	handle->verify = verify;
 
 	ret = fit_do_open(handle);
@@ -807,15 +812,31 @@ struct fit_handle *fit_open(const char *filename, bool verbose,
 			    enum bootm_verify verify)
 {
 	struct fit_handle *handle;
-	int ret;
+	struct fdt_header header;
+	int fd, ret;
 
 	handle = xzalloc(sizeof(struct fit_handle));
 
 	handle->verbose = verbose;
 	handle->verify = verify;
 
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
+		pr_err("unable to open %s: %s\n", filename, strerror(errno));
+		return ERR_PTR(-errno);
+	}
+
+	ret = read_full(fd, &header, sizeof(header));
+	if (ret < 0) {
+		close(fd);
+		pr_err("unable to read %s: %s\n", filename, strerror(errno));
+		return ERR_PTR(-errno);
+	}
+
+	close(fd);
+
 	ret = read_file_2(filename, &handle->size, &handle->fit_alloc,
-			  FILESIZE_MAX);
+			  fdt32_to_cpu(header.totalsize));
 	if (ret) {
 		pr_err("unable to read %s: %s\n", filename, strerror(-ret));
 		return ERR_PTR(ret);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 26 lines · Daniel Brát

The real size of the fit image might be significantly smaller than it may appear to be based on the specified filename. For example, if path to raw disk partition is passed (eg. /dev/disk0.1), the size of the partition itself might be several times larger than the fit image it contains at the moment (so it has headroom for possible future image size changes).
This modification uses the fdt header field 'totalsize' to read-in only what is needed.

---
v2: use fdt32_to_cpu to read the totalsize from header

---
 common/image-fit.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
common/image-fit.c +1 -0
index a410632d7..de65e3dd1 100644
@@ -774,13 +774,18 @@ struct fit_handle *fit_open_buf(const void *buf, size_t size, bool verbose,
 				enum bootm_verify verify)
 {
 	struct fit_handle *handle;
+	struct fdt_header *header;
 	int ret;
 

common/image-fit.c struct fit_handle *fit_open_buf(const void *buf, size_t size, bool verbose, · line 788

Quoted 10 lines
common/image-fit.c +5 -1
+	if (size < sizeof(*header))
+		return ERR_PTR(-EINVAL);
+
+	header = (struct fdt_header*)buf;
 	handle = xzalloc(sizeof(struct fit_handle));
 
 	handle->verbose = verbose;
 	handle->fit = buf;
-	handle->size = size;
+	handle->size = fdt32_to_cpu(header->totalsize);

Should we check that header->totalsize doesn't exceed the passed buffer size?

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |