Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flattened device tree/devicetree blobs specification #467

Merged
merged 24 commits into from
Aug 18, 2021

Conversation

armijnhemel
Copy link
Collaborator

This is a specification for DTB (devicetree blobs/flattened device tree), the compiled result of .dts files as found in the Linux kernel. Test files can be found here:

https://github.com/qemu/qemu/tree/master/pc-bios

Note: this is NOT the same as the dtb/dtbo image format that was pulled here: #415

@armijnhemel
Copy link
Collaborator Author

Note: this specification checks the structure of the dtb, but does not process the individual properties (as there are lots of them).

@armijnhemel
Copy link
Collaborator Author

Also, I have added this to the directory "database", but that doesn't seem to be the right place.

database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
@KOLANICH
Copy link
Contributor

KOLANICH commented Apr 3, 2021

but that doesn't seem to be the right place.

We have a directory firmware, but we should really to invent a better structure for the repo (with more formal criteria what goes where which can be used to predict the location of a spec with onpy basic knowledge agout the format it implements). I guess serialization (but not firmware) will be better for this format.

database/dtb.ksy Outdated Show resolved Hide resolved
@armijnhemel
Copy link
Collaborator Author

Note: this specification checks the structure of the dtb, but does not process the individual properties (as there are lots of them).

This specification also treats the tokens in the structure block as a list of tokens. It would be more accurate to have it as a tree.

database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
Copy link
Member

@generalmimon generalmimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully it will be easier for you to address everything at once...

database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated
Comment on lines 69 to 71
memory_reservation_block:
pos: ofs_memory_reservation_block
size: ofs_structure_block - ofs_memory_reservation_block
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://github.com/devicetree-org/devicetree-specification/releases/download/v0.3/devicetree-specification-v0.3.pdf (section 5.3 Memory Reservation Block > 5.3.2 Format, page 47/56), this lacks inner structure:

5.3.2 Format

The memory reservation block consists of a list of pairs of 64-bit big-endian integers, each pair being represented by the following C structure.

struct fdt_reserve_entry {
    uint64_t address;
    uint64_t size;
};

Each pair gives the physical address and size in bytes of a reserved memory region. These given regions shall not overlap each other. The list of reserved blocks shall be terminated with an entry where both address and size are equal to 0. Note that the address and size values are always 64-bit. On 32-bit CPUs the upper 32-bits of the value are ignored.

Each uint64_t in the memory reservation block, and thus the memory reservation block as a whole, shall be located at an 8-byte aligned offset from the beginning of the devicetree blob (see section 5.6).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. I skipped this part, as for my particular use case it is not interesting. Also, none of the test files I looked at actually had anything useful in there, except the terminator.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added this now as well.

database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated
- id: boundary_padding
size: (- _io.pos) % 4
enums:
fdt:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a more meaningful enum name? I guess that fdt is an abbreviation of Flattened Devicetree Format, not something like token type.

Please don't forget about

- id: token_type
type: u4
enum: fdt

if you rename it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the word token here, maybe just type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I called it token is because they are called that in the documentation:

https://github.com/devicetree-org/devicetree-specification/blob/v0.3/source/flattened-format.rst#lexical-structure

The five token types are as follows:

Perhaps because they are in an element fdt_note it can indeed be just token. @generalmimon any thoughts?

database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated Show resolved Hide resolved
@generalmimon
Copy link
Member

@armijnhemel

Also, I have added this to the directory "database", but that doesn't seem to be the right place.

@KOLANICH

We have a directory firmware, but we should really to invent a better structure for the repo (with more formal criteria what goes where which can be used to predict the location of a spec with onpy basic knowledge agout the format it implements). I guess serialization (but not firmware) will be better for this format.

I also think serialization is quite reasonable - it's definitely better than database. However, as for #404 (comment), it doesn't really matter what the format looks from inside but rather what it's mainly used for and where people would expect it. From this perspective, I'm thinking about hardware - a devicetree is used to describe system hardware. However, the hardware folder is classified as Hardware Protocols at https://formats.kaitai.io, which is not really what we mean. I don't see any connection with firmware.

database/dtb.ksy Outdated Show resolved Hide resolved
database/dtb.ksy Outdated
type: u4
- id: version
type: u4
- id: last_compatible_version
Copy link
Member

@generalmimon generalmimon Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is misleading, at least to me - if I see last compatible version I imagine the latest one (i.e. the one that came last), not the lowest or earliest. However, it's clear that the spec intended the exact opposite:

last_comp_version
This field shall contain the lowest version of the devicetree data structure with which the version used is backwards compatible. So, for the structure as defined in this document (version 17), this field shall contain 16 because version 17 is backwards compatible with version 16, but not earlier versions. As per section 5.1, a DTSpec boot program should provide a devicetree in a format which is backwards compatible with version 16, and thus this field shall always contain 16.

I'm not sure why did the spec authors choose last_ adjective in the name. Probably when you imagine the latest version as the "first", then you go further into the version history and declare the oldest compatible version as the "last" that would the current version be compatible with (if we went beyond the "last", any older versions would no longer work). However, this concept seems to be upside down - I guess it's natural that "last" should mean latest (the last one that has been released so far) and the "first" would mean the initial version, i.e. the oldest version. In this justification for the spec's naming that I came up with, I needed to reverse the timeline and the role of "first" and "last" pointers (we go back in time).

Does first_compatible_version, oldest_compatible_version, lowest_compatible_version or min_compatible_version make more sense? Which one would you choose?

@generalmimon
Copy link
Member

generalmimon commented Aug 18, 2021

As I wrote before in #467 (comment), I moved the spec to the hardware/ directory (a8331ca) because I think it's the first place where people would look for it.

@armijnhemel
Copy link
Collaborator Author

As I wrote before in #467 (comment), I moved the spec to the hardware/ directory (a8331ca) because I think it's the first place where people would look for it.

For pure dtb: yes, but there are, as always, exceptions, namely FIT images where entire Linux kernel images and file systems are included and dtb purely acts as a container.

Copy link
Member

@generalmimon generalmimon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing! Merging in.

@generalmimon generalmimon merged commit 1ed9ea6 into kaitai-io:master Aug 18, 2021
@armijnhemel armijnhemel deleted the dtb branch August 18, 2021 17:07
ZetaTwo pushed a commit to ZetaTwo/kaitai_struct_formats that referenced this pull request May 17, 2022
* add flattened device tree/devicetree blobs specification

* dtb: add doc-ref to text versions of the specifications, add extra
sanity check for version/last_compatible_version

* dtb: add orig-ids, rename some ids, add xref, add more docs

* dtb: add links to example files, describe where on Linux systems files
can be found

* dtb: add some applications that use DTB

* dtb: clarify which strings are encoded in a subset of ASCII

* dtb: fixes according to KSY style guide

* dtb: use short(er) Git hash in doc-ref

* dtb: remove superfluous encoding lines

* dtb: rewrite offsets to comply with style guide

* dtb: style fixes, add instance to unlock some more data

* dtb: process memory block

* dtb: add `-webide-representation` to `fdt_begin_node`

* dtb: remove `.rst` link from `doc` and `doc-ref`

* dtb: use `*` instead of `-` for list bullets in `/doc`

* dtb: move from `database/` to `hardware/`

* dtb: add `serialization` to `/meta/tags`

* dtb: reorder `/types` so it makes more sense

* dtb: rename `{boundary_ => }padding`

* dtb: rename `{last => min}_compatible_version`

* dtb: link to the HTML version of the specification instead of PDF

* dtb: drop field name prefixes

* dtb: minor `/doc` fixes

* dtb: add more info to `/doc`

Co-authored-by: Petr Pucil <[email protected]>
ZetaTwo pushed a commit to ZetaTwo/kaitai_struct_formats that referenced this pull request May 17, 2022
* add flattened device tree/devicetree blobs specification

* dtb: add doc-ref to text versions of the specifications, add extra
sanity check for version/last_compatible_version

* dtb: add orig-ids, rename some ids, add xref, add more docs

* dtb: add links to example files, describe where on Linux systems files
can be found

* dtb: add some applications that use DTB

* dtb: clarify which strings are encoded in a subset of ASCII

* dtb: fixes according to KSY style guide

* dtb: use short(er) Git hash in doc-ref

* dtb: remove superfluous encoding lines

* dtb: rewrite offsets to comply with style guide

* dtb: style fixes, add instance to unlock some more data

* dtb: process memory block

* dtb: add `-webide-representation` to `fdt_begin_node`

* dtb: remove `.rst` link from `doc` and `doc-ref`

* dtb: use `*` instead of `-` for list bullets in `/doc`

* dtb: move from `database/` to `hardware/`

* dtb: add `serialization` to `/meta/tags`

* dtb: reorder `/types` so it makes more sense

* dtb: rename `{boundary_ => }padding`

* dtb: rename `{last => min}_compatible_version`

* dtb: link to the HTML version of the specification instead of PDF

* dtb: drop field name prefixes

* dtb: minor `/doc` fixes

* dtb: add more info to `/doc`

Co-authored-by: Petr Pucil <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants