Skip to content

Property with a zero value is interpreted as empty #1

@gabeusse83

Description

@gabeusse83

I have observed that the heuristic that parses property values sometimes can interpret them in unpredictable ways. For this example DTS, derived from qemu's default DTB:

/dts-v1/;

/ {
    #size-cells = <0x02>;
    #address-cells = <0x02>;
    compatible = "linux,dummy-virt";

    pl011@9000000 {
        reg = <0x00 0x9000000 0x00 0x1000>;
        compatible = "arm,pl011", "arm,primecell";
    };

    cpus {
        #size-cells = <0x00>;
        #address-cells = <0x01>;

        cpu@0 {
            reg = <0x00>;
            device_type = "cpu";
        };

        cpu@1 {
            reg = <0x01>;
            device_type = "cpu";
        };

        cpu@2 {
            reg = <0x02>;
            device_type = "cpu";
        };
    };

    aliases {
        serial0 = "/pl011@9000000";
    };

    chosen {
        stdout-path = "/pl011@9000000";
    };
};

I compiled it into its corresponding DTB, and wrote the following test:

   #[test]
    fn test_zero_data_parsed_as_empty(){
        let dtb_data =     include_bytes!("../test-data/zero_data_as_empty.dtb").to_vec();
        assert!(!dtb_data.is_empty(), "DTB data should not be empty");
        let parser = DeviceTreeParser::new(&dtb_data);
        let tree = parser.parse_tree();
        std::println!("Parsed tree: {tree:#?}");
    }

For which the print statement gives back:

arsed tree: Ok(
    DeviceTreeNode {
        name: "",
        properties: [
            Property {
                name: "#size-cells",
                value: U32(
                    2,
                ),
            },
            Property {
                name: "#address-cells",
                value: U32(
                    2,
                ),
            },
            Property {
                name: "compatible",
                value: String(
                    "linux,dummy-virt",
                ),
            },
        ],
        children: [
            DeviceTreeNode {
                name: "pl011@9000000",
                properties: [
                    Property {
                        name: "reg",
                        value: U32Array(
                            [
                                0,
                                0,
                                0,
                                0,
                                9,
                                0,
                                0,
                                0,
                                0,
                                0,
                                0,
                                0,
                                0,
                                0,
                                16,
                                0,
                            ],
                        ),
                    },
                    Property {
                        name: "compatible",
                        value: StringList(
                            [
                                "arm,pl011",
                                "arm,primecell",
                            ],
                        ),
                    },
                ],
                children: [],
            },
            DeviceTreeNode {
                name: "cpus",
                properties: [
                    Property {
                        name: "#size-cells",
                        value: Empty,
                    },
                    Property {
                        name: "#address-cells",
                        value: U32(
                            1,
                        ),
                    },
                ],
                children: [
                    DeviceTreeNode {
                        name: "cpu@0",
                        properties: [
                            Property {
                                name: "reg",
                                value: Empty,
                            },
                            Property {
                                name: "device_type",
                                value: String(
                                    "cpu",
                                ),
                            },
                        ],
                        children: [],
                    },
                    DeviceTreeNode {
                        name: "cpu@1",
                        properties: [
                            Property {
                                name: "reg",
                                value: U32(
                                    1,
                                ),
                            },
                            Property {
                                name: "device_type",
                                value: String(
                                    "cpu",
                                ),
                            },
                        ],
                        children: [],
                    },
                    DeviceTreeNode {
                        name: "cpu@2",
                        properties: [
                            Property {
                                name: "reg",
                                value: U32(
                                    2,
                                ),
                            },
                            Property {
                                name: "device_type",
                                value: String(
                                    "cpu",
                                ),
                            },
                        ],
                        children: [],
                    },
                ],
            },
            DeviceTreeNode {
                name: "aliases",
                properties: [
                    Property {
                        name: "serial0",
                        value: String(
                            "/pl011@9000000",
                        ),
                    },
                ],
                children: [],
            },
            DeviceTreeNode {
                name: "chosen",
                properties: [
                    Property {
                        name: "stdout-path",
                        value: String(
                            "/pl011@9000000",
                        ),
                    },
                ],
                children: [],
            },
        ],
    },
)

Note that the reg property for the CPU@0 is incorrectly parsed as empty:

DeviceTreeNode {
                        name: "cpu@0",
                        properties: [
                            Property {
                                name: "reg",
                                value: Empty,
                            },
                            Property {
                                name: "device_type",
                                value: String(
                                    "cpu",
                                ),
                            },
                        ],
                        children: [],
                    },

I was wondering if we can have a mode where the properties are all parsed to their raw bytes, and the conversion to their actual types is deferred to the consumer code. I would be more than happy to open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions