Skip to contentSkip to conversation

hw/arm/boot: Report error msg if loading elf/dtb failed

PATCH Superseded Changbin Du via 4 participants 6 messages Aug 30, 2024 – Sep 3, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hw/arm/boot: Report error msg if loading elf/dtb failed

Print errors before exit. Do not exit silently.

---
v2: remove msg for arm_load_dtb.

---
 hw/arm/boot.c | 1 +
 1 file changed, 1 insertion(+)
hw/arm/boot.c +1 -0
index d480a7da02cf..e15bf097a559 100644
@@ -839,6 +839,7 @@ static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
                       1, data_swab, as);
     if (ret <= 0) {
         /* The header loaded but the image didn't */
+        error_report("could not load elf '%s'", info->kernel_filename);
         exit(1);
     }
 
Signature · 1 line
2.34.1
#1 lore raw

Hi Changbin,

Quoted 15 lines · Changbin Du via

Print errors before exit. Do not exit silently.

---
v2: remove msg for arm_load_dtb.

---
  hw/arm/boot.c | 1 +
  1 file changed, 1 insertion(+)
hw/arm/boot.c +0 -0
index d480a7da02cf..e15bf097a559 100644

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, · line 842

Quoted 4 lines
hw/arm/boot.c +1 -0
@@ -839,6 +839,7 @@ static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
                        1, data_swab, as);
      if (ret <= 0) {
          /* The header loaded but the image didn't */
+        error_report("could not load elf '%s'", info->kernel_filename);

"Could ..." (caps)

"hw/loader.h" is not well documented, but it seems load_elf*() returns:

  #define ELF_LOAD_FAILED       -1
  #define ELF_LOAD_NOT_ELF      -2
  #define ELF_LOAD_WRONG_ARCH   -3

#define ELF_LOAD_WRONG_ENDIAN -4
#define ELF_LOAD_TOO_BIG -5

And we can display this error calling:

  const char *load_elf_strerror(ssize_t error);

So we can be more precise here using:

error_report("Could not load elf '%s'", info->kernel_filename,

               load_elf_strerror(ret));

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, · line 845

Quoted 3 lines
hw/arm/boot.c +0 -0
          exit(1);
      }
 

Better (but out of scope of this patch) could be to pass an Error *errp argument to the load_elf*() family of functions, and fill it with the appropriate error message.

Regards,

Phil.

#2 lore raw
Quoted 13 lines · Philippe Mathieu-Daudé

Hi Changbin,

Quoted 10 lines · Changbin Du via

Print errors before exit. Do not exit silently.

---
v2: remove msg for arm_load_dtb.
---
  hw/arm/boot.c | 1 +

  1 file changed, 1 insertion(+)
Quoted 5 lines

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info

Quoted 5 lines
hw/arm/boot.c +0 -0
index d480a7da02cf..e15bf097a559 100644
@@ -839,6 +839,7 @@ static ssize_t arm_load_elf(struct arm_boot_info 

Note that header error is also silently ignored and could be logged:

    load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
    if (err) {
-        error_free(err);
+        error_report_err(err);
        return ret;
    }

(untested)

Quoted 33 lines

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, · line 842

Quoted 3 lines
hw/arm/boot.c +1 -0
       if (ret <= 0) {
           /* The header loaded but the image didn't */
+        error_report("could not load elf '%s'", info->kernel_filename);

"Could ..." (caps)

"hw/loader.h" is not well documented, but it seems load_elf*() returns:

  #define ELF_LOAD_FAILED       -1
  #define ELF_LOAD_NOT_ELF      -2
  #define ELF_LOAD_WRONG_ARCH   -3
  #define ELF_LOAD_WRONG_ENDIAN -4
  #define ELF_LOAD_TOO_BIG      -5

And we can display this error calling:

  const char *load_elf_strerror(ssize_t error);

So we can be more precise here using:

  error_report("Could not load elf '%s'", info->kernel_filename, load_elf_strerror(ret));

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, · line 844

Quoted 2 lines
hw/arm/boot.c +0 -0
           exit(1);
       }

Better (but out of scope of this patch) could be to pass an Error *errp argument to the load_elf*() family of functions, and fill it with the appropriate error message.

Regards,

Phil.

#3 lore raw
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Philippe Mathieu-Daudé

Hi Changbin,

Quoted 10 lines · Changbin Du via

Print errors before exit. Do not exit silently.

---
v2: remove msg for arm_load_dtb.

---
  hw/arm/boot.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 5 lines

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info

Quoted 5 lines
hw/arm/boot.c +0 -0
index d480a7da02cf..e15bf097a559 100644
@@ -839,6 +839,7 @@ static ssize_t arm_load_elf(struct arm_boot_info

Note that header error is also silently ignored and could be logged:

     load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
     if (err) {
(quoted diff) +1 -1
-        error_free(err);
+        error_report_err(err);
         return ret;
     }
Quoted 1 line

(untested)

This one is deliberate -- if the file is not an ELF file we want to silently fall back to trying it as a uimage or an AArch64 Image file or as last resort a bare raw binary.

-- PMM

#4 lore raw
Quoted 31 lines · Peter Maydell
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Philippe Mathieu-Daudé

Hi Changbin,

Quoted 10 lines · Changbin Du via

Print errors before exit. Do not exit silently.

---
v2: remove msg for arm_load_dtb.

---
   hw/arm/boot.c | 1 +
   1 file changed, 1 insertion(+)
Quoted 5 lines

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info

Quoted 5 lines
hw/arm/boot.c +0 -0
index d480a7da02cf..e15bf097a559 100644
@@ -839,6 +839,7 @@ static ssize_t arm_load_elf(struct arm_boot_info

Note that header error is also silently ignored and could be logged:

      load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
      if (err) {
(quoted diff) +1 -1
-        error_free(err);
+        error_report_err(err);
          return ret;
      }
Quoted 5 lines
Quoted 1 line

(untested)

This one is deliberate -- if the file is not an ELF file we want to silently fall back to trying it as a uimage or an AArch64 Image file or as last resort a bare raw binary.

Oh right. I'll add a comment about it to clarify, thanks.

#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 40 lines · Philippe Mathieu-Daudé

Hi Changbin,

Quoted 15 lines · Changbin Du via

Print errors before exit. Do not exit silently.

---
v2: remove msg for arm_load_dtb.

---
  hw/arm/boot.c | 1 +
  1 file changed, 1 insertion(+)
hw/arm/boot.c +0 -0
index d480a7da02cf..e15bf097a559 100644

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, · line 842

Quoted 4 lines
hw/arm/boot.c +1 -0
@@ -839,6 +839,7 @@ static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
                        1, data_swab, as);
      if (ret <= 0) {
          /* The header loaded but the image didn't */
+        error_report("could not load elf '%s'", info->kernel_filename);

"Could ..." (caps)

"hw/loader.h" is not well documented, but it seems load_elf*() returns:

  #define ELF_LOAD_FAILED       -1
  #define ELF_LOAD_NOT_ELF      -2
  #define ELF_LOAD_WRONG_ARCH   -3

#define ELF_LOAD_WRONG_ENDIAN -4
#define ELF_LOAD_TOO_BIG -5

And we can display this error calling:

  const char *load_elf_strerror(ssize_t error);

So we can be more precise here using:

error_report("Could not load elf '%s'", info->kernel_filename,

Quoted 9 lines
               load_elf_strerror(ret));

hw/arm/boot.c static ssize_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry, · line 844

Quoted 2 lines
hw/arm/boot.c +0 -0
          exit(1);
      }

Better (but out of scope of this patch) could be to pass an Error *errp argument to the load_elf*() family of functions, and fill it with the appropriate error message.

Thanks for your suggestion. I changed it as below:
+ error_report("Couldn't load elf '%s': %s",
+ info->kernel_filename, load_elf_strerror(ret));

$ qemu-system-aarch64 -M virt -kernel /work/linux/vmlinux
qemu-system-aarch64: Couldn't load elf '/work/linux/vmlinux': The image is from incompatible architecture
Quoted 3 lines

Regards,

Phil.

Signature · 2 lines
Cheers,
Changbin Du