Commit 9404fc85ab2e60fb81c8faaa58349ee1187a2501

Authored by Simon Glass
1 parent ab761ce9f9

fdtgrep: Improve error handling with invalid device tree

This tool requires that the aliases node be the first node in the tree. But
when it is not, it does not handle things gracefully. In fact it crashes.

Fix this, and add a more helpful error message.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reported-by: Masahiro Yamada <yamada.masahiro@socionext.com>

Showing 1 changed file with 10 additions and 4 deletions Side-by-side Diff

... ... @@ -660,6 +660,8 @@
660 660 if (!ret)
661 661 count++;
662 662 }
  663 + if (ret && ret != -FDT_ERR_NOTFOUND)
  664 + return ret;
663 665  
664 666 /* Find all the aliases and add those regions back in */
665 667 if (disp->add_aliases && count < max_regions) {
... ... @@ -667,7 +669,11 @@
667 669  
668 670 new_count = fdt_add_alias_regions(fdt, region, count,
669 671 max_regions, &state);
670   - if (new_count <= max_regions) {
  672 + if (new_count == -FDT_ERR_NOTFOUND) {
  673 + /* No alias node found */
  674 + } else if (new_count < 0) {
  675 + return new_count;
  676 + } else if (new_count <= max_regions) {
671 677 /*
672 678 * The alias regions will now be at the end of the list.
673 679 * Sort the regions by offset to get things into the
... ... @@ -679,9 +685,6 @@
679 685 }
680 686 }
681 687  
682   - if (ret != -FDT_ERR_NOTFOUND)
683   - return ret;
684   -
685 688 return count;
686 689 }
687 690  
... ... @@ -807,6 +810,9 @@
807 810 disp->flags);
808 811 if (count < 0) {
809 812 report_error("fdt_find_regions", count);
  813 + if (count == -FDT_ERR_BADLAYOUT)
  814 + fprintf(stderr,
  815 + "/aliases node must come before all other nodes\n");
810 816 return -1;
811 817 }
812 818 if (count <= max_regions)