--- a/fdtput.c 2026-08-15 15:43:04.550373900 +0200 +++ b/fdtput.c 2026-08-15 16:02:11.926917822 +0200 @@ -410,7 +410,7 @@ static int do_fdtput(struct display_info uint32_t dt_total = 0; uint32_t page_size = 0; uint32_t fdt_addr = 0; - int fdt_count = 0; + int fdt_target_id = -1; char **fdts = NULL; char *fdt; char amlogic_dt_id[256] = {0}; @@ -452,20 +452,22 @@ static int do_fdtput(struct display_info int tmp_node; const struct fdt_property *dt_id = NULL; tmp_node = fdt_path_offset(blob + tmp_fdt_addr, "/"); - if (tmp_node >= 0) + if (tmp_node >= 0) { dt_id = fdt_getprop(blob + tmp_fdt_addr, tmp_node, "amlogic-dt-id", &len); + len--; // include nul ? + } // check if correct dtb in multidtb got found if (dt_id && - (len >= strlen(amlogic_dt_id)) && + (len == strlen(amlogic_dt_id)) && (!strncmp((const char *)dt_id, amlogic_dt_id, strlen(amlogic_dt_id)))) { fdt_addr = tmp_fdt_addr; + fdt_target_id = i; } // make a backup and leave the dtb untouched else if (fdts) { - fdts[fdt_count] = xmalloc(tmp_fdt_size); - memcpy(fdts[fdt_count], blob + tmp_fdt_addr, tmp_fdt_size); - fdt_count++; + fdts[i] = xmalloc(tmp_fdt_size); + memcpy(fdts[i], blob + tmp_fdt_addr, tmp_fdt_size); } } } @@ -531,9 +533,12 @@ static int do_fdtput(struct display_info // add single dtb to multidtb before save to file // free memory allocated before fdt_size = fdt_totalsize(blob); - if (fdts) { - fdts[fdt_count] = xmalloc(fdt_size); - memcpy(fdts[fdt_count], blob, fdt_size); + if (fdts && fdt_target_id >= 0) { + if (fdts[fdt_target_id]) + free(fdts[fdt_target_id]); + + fdts[fdt_target_id] = xmalloc(fdt_size); + memcpy(fdts[fdt_target_id], blob, fdt_size); free(blob); blob = fdt; create_multidtb(&blob, dt_total, page_size, fdts); @@ -547,8 +552,11 @@ static int do_fdtput(struct display_info } if (fdts) { - for (i = 0; i < (dt_total - 1); i++) - free(fdts[i]); + for (i = 0; i < (dt_total - 1); i++) { + if (fdts[i]) + free(fdts[i]); + } + free(fdts); } free(blob);