#4728 closed defect (fixed)

RISC-V console device tree parsing fails with a list of properties

Reported by: Alan Cudmore Owned by: Alan Cudmore <alan.cudmore@…>
Priority: normal Milestone:
Component: bsps Version: 6
Severity: normal Keywords: riscv, fdt, console
Cc: Blocked By:


The riscv console configuration code uses a macro:
#define RISCV_CONSOLE_IS_COMPATIBLE(actual, actual_len, desired) \

(actual_len == sizeof(desired) \

&& memcmp(actual, desired, sizeof(desired) - 1) == 0)

And is used to find a compatible console device in the device tree like this:

if (RISCV_CONSOLE_IS_COMPATIBLE(compat, compat_len, "ucb,htif0")) {

htif_console_context_init(&htif_console_instance.base, node);

An example device tree entry:
compatible = "ns16550a";
works with the macro, but when the device tree entry has multiple items like:
compatible = "canaan,k210-uarths", "sifive,uart0";
the macro cannot parse the entry because the list is being treated like a single string. The compat_len variable ends up having the length of the entire list, causing the comparison to fail.

If the macro is replaced with a call to fdt_stringlist_contains like:

if (fdt_stringlist_contains(compat, compat_len, "ucb,htif0")) {

htif_console_context_init(&htif_console_instance.base, node);

The substring can be found in the compatible list.

This works with my use case where there are multiple items in the compatible list.

Unless there is a reason why the macro is used, I can submit a patch that replaces the macros with fdt_stringlist_contains calls.

Note: I am not sure how to test all cases of this code, I can test the generic riscv-v/qemu based BSP, and the BSP I am working on, but I do not have access to a FE310/Arty system

Change History (1)

comment:1 Changed on 10/14/22 at 13:04:07 by Alan Cudmore <alan.cudmore@…>

Owner: set to Alan Cudmore <alan.cudmore@…>
Resolution: fixed
Status: newclosed

In 1d2fab8/rtems:

bsps: Improve riscv console FDT parsing

This fixes a problem with parsing the FDT compatible property by
replacing the RISCV_CONSOLE_IS_COMPATIBLE macro with calls to
the fdt_stringlist_contains function. The macro only works when
the compatible FDT entry is a single string and not a list of
strings. The new call will compare each item in the string list.

Close #4728.

Note: See TracTickets for help on using tickets.