Skip to content

Commit

Permalink
cmd: dtimg: get devicetree index based on board identifier and revision
Browse files Browse the repository at this point in the history
The dtimg includes several devicetrees selected by an index.
Each device tree is added in the dtimg associated with a board identifier
(and optionally a board revision).

This service is added to get back the index associated to the board id
and optionally the board rev.

Change-Id: Ie7670d4a9ee132953296a8616ce5575b6fa65de4
Reviewed-on: https://gerrit.st.com/c/mpu/oe/st/u-boot/+/135279
Reviewed-by: CITOOLS <smet-aci-reviews@lists.codex.cro.st.com>
Reviewed-by: CIBUILD <smet-aci-builds@lists.codex.cro.st.com>
Reviewed-by: Patrick DELAUNAY <patrick.delaunay@st.com>
Tested-by: Patrick DELAUNAY <patrick.delaunay@st.com>
  • Loading branch information
patrickdelaunay committed Apr 9, 2020
1 parent 2086a35 commit 21b57a1
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
50 changes: 49 additions & 1 deletion cmd/dtimg.c
Expand Up @@ -99,10 +99,52 @@ static int do_dtimg_size(cmd_tbl_t *cmdtp, int flag, int argc,
return dtimg_get_fdt(argc, argv, CMD_DTIMG_SIZE);
}

static int do_dtimg_getindex(cmd_tbl_t *cmdtp, int flag, int argc,
char * const argv[])
{
char *endp;
ulong hdr_addr;
int index;
char buf[512] = { 0 };

if (argc < 4)
return CMD_RET_USAGE;

hdr_addr = simple_strtoul(argv[1], &endp, 16);
if (*endp != '\0') {
printf("Error: Wrong image address\n");
return CMD_RET_FAILURE;
}

if (!android_dt_check_header(hdr_addr)) {
printf("Error: DT image header is incorrect\n");
return CMD_RET_FAILURE;
}

index = android_dt_get_index(hdr_addr, strtoul(argv[2], NULL, 0),
strtoul(argv[3], NULL, 0));

if (index < 0) {
printf("Error: board id %04lx not found in DT table\n",
strtoul(argv[2], NULL, 0));
return CMD_RET_FAILURE;
}

snprintf(buf, sizeof(buf), "%i", index);

if (argc == 5)
env_set(argv[4], buf);
else
printf("%s\n", buf);

return CMD_RET_SUCCESS;
}

static cmd_tbl_t cmd_dtimg_sub[] = {
U_BOOT_CMD_MKENT(dump, 2, 0, do_dtimg_dump, "", ""),
U_BOOT_CMD_MKENT(start, 4, 0, do_dtimg_start, "", ""),
U_BOOT_CMD_MKENT(size, 4, 0, do_dtimg_size, "", ""),
U_BOOT_CMD_MKENT(getindex, 5, 0, do_dtimg_getindex, "", ""),
};

static int do_dtimg(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Expand Down Expand Up @@ -138,5 +180,11 @@ U_BOOT_CMD(
" - get size (hex, bytes) of FDT in the image, by index\n"
" <addr>: image address in RAM, in hex\n"
" <index>: index of desired FDT in the image\n"
" <varname>: name of variable where to store size of FDT"
" <varname>: name of variable where to store size of FDT\n"
"dtimg getindex <addr> <board_id> <board_rev> [varname]\n"
" - get index of FDT in the image, by board identifier and revision\n"
" <addr>: image address in RAM, in hex\n"
" <board_id>: board identifier\n"
" <board_rev>: board revision (0 if not used)\n"
" [varname]: name of variable where to store index of FDT"
);
42 changes: 42 additions & 0 deletions common/image-android-dt.c
Expand Up @@ -154,4 +154,46 @@ void android_dt_print_contents(ulong hdr_addr)
unmap_sysmem(fdt);
}
}

#endif

/**
* Get dtb index based on board identifier and revision.
*
* @param hdr_addr Start address of DT image
* @param board_id board identifier
* @param board_rev board revision (0 if not used)
*
* @return index in dt table
*/
int android_dt_get_index(ulong hdr_addr, u32 board_id, u32 board_rev)
{
const struct dt_table_header *hdr;
u32 entry_count, entries_offset, entry_size;
u32 i;
int ret = -1;

hdr = map_sysmem(hdr_addr, sizeof(*hdr));
entry_count = fdt32_to_cpu(hdr->dt_entry_count);
entries_offset = fdt32_to_cpu(hdr->dt_entries_offset);
entry_size = fdt32_to_cpu(hdr->dt_entry_size);
unmap_sysmem(hdr);

for (i = 0; i < entry_count; ++i) {
const ulong e_addr = hdr_addr + entries_offset + i * entry_size;
const struct dt_table_entry *e;

e = map_sysmem(e_addr, sizeof(*e));

if ((fdt32_to_cpu(e->id) == board_id) &&
(board_rev == 0 || fdt32_to_cpu(e->rev) == board_rev)) {
ret = i;
unmap_sysmem(e);
break;
}

unmap_sysmem(e);
}

return ret;
}
2 changes: 2 additions & 0 deletions include/image-android-dt.h
Expand Up @@ -17,4 +17,6 @@ bool android_dt_get_fdt_by_index(ulong hdr_addr, u32 index, ulong *addr,
void android_dt_print_contents(ulong hdr_addr);
#endif

int android_dt_get_index(ulong hdr_addr, u32 board_id, u32 board_rev);

#endif /* IMAGE_ANDROID_DT_H */

0 comments on commit 21b57a1

Please sign in to comment.