hw/core/loader: improve error handling in image loading functions

Add error checking for lseek() failure and provide better error
messages when image loading fails, including filenames and addresses.

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Aditya Gupta <adityag@linux.ibm.com>
Signed-off-by: Vishal Chourasia <vishalc@linux.ibm.com>
Message-ID: <20251024130556.1942835-8-vishalc@linux.ibm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
This commit is contained in:
Vishal Chourasia
2025-10-24 18:35:57 +05:30
committed by Philippe Mathieu-Daudé
parent a9c608904f
commit f62226f7dc

View File

@@ -79,6 +79,10 @@ int64_t get_image_size(const char *filename, Error **errp)
if (fd < 0)
return -1;
size = lseek(fd, 0, SEEK_END);
if (size < 0) {
error_setg_errno(errp, errno, "lseek failure: %s", filename);
return -1;
}
close(fd);
return size;
}
@@ -132,11 +136,20 @@ ssize_t load_image_targphys_as(const char *filename,
ssize_t size;
size = get_image_size(filename, errp);
if (size < 0 || size > max_sz) {
if (size < 0) {
return -1;
}
if (size > max_sz) {
error_setg(errp, "%s exceeds maximum image size (%s)",
filename, size_to_str(max_sz));
return -1;
}
if (size > 0) {
if (rom_add_file_fixed_as(filename, addr, -1, as) < 0) {
error_setg(errp, "could not load '%s' at %" HWADDR_PRIx,
filename, addr);
return -1;
}
}