Device tree compiler and inspector in pure MoonBit: DTS to DTB, DTB back to DTS and overlays, byte-identical to dtc, with no dtc binary present.
| Capability | Status |
|---|---|
| 40-byte blob header, version 17 | done |
| Block tiling and alignment validation | done |
| Bounds-checked primitive reads | done |
| Struct block walk (nodes, properties) | done |
| Strings block lookup | done |
| Nesting limit | done |
| DTS lexer: identifiers, strings, numbers, comments, cpp markers | done |
| DTS parser: nodes, properties, typed values | done |
| Labels, &label and &{/path} references | done |
| /bits/, /memreserve/, /plugin/ | done |
| DTB writer | done |
| Reference resolution, phandle numbering | done |
| Overlay generation, __fixups__ | done |
| Overlay application, merge semantics | done |
| CLI: dump, decompile, compile, plugin compile, apply | done |
| Source emitter, byte-identical to dtc's dump | done |
| Browser demo (JavaScript backend) | done |
| DTS emitter (tree back to readable source) | done |
| C preprocessor, /incbin/ | out of scope |
| Fixture | Size | Nodes | What it covers |
|---|---|---|---|
| minimal | 357 B | 4 | flat tree, strings and cells |
| nested | 1 086 B | 12 | CPU subtree, phandle, multi-line reg, empty ranges |
| realview-pb1176 | 9 983 B | 69 | ARM board from the Linux tree: aliases, dma-ranges, 14 phandles |
| overlay-uart | 381 B | 4 | /plugin/ patches, __fixups__ |
| Part | Lines |
|---|---|
| Library | 4 126 |
| Tests (*_wbtest.mbt) | 1 710 |
| CLI and browser entry points | 366 |
| Operation | Time |
|---|---|
| Parse and render a 9 983-byte blob (69 nodes) | 0.24 ms |
| Throughput | ~4 300 parses/s |
| Compile the same source from the CLI, process start included | 20–60 ms |
moon testmoon run cmd/main -- internal/fixtures/nested.dts -o /tmp/out.dtb
dtc -q -I dts -O dtb -o /tmp/dtc.dtb internal/fixtures/nested.dts
cmp /tmp/out.dtb /tmp/dtc.dtbmoon run cmd/main -- /tmp/out.dtb -D > /tmp/round.dts
dtc -q -I dtb -O dts -o /tmp/dtc.dts /tmp/out.dtb
diff /tmp/round.dts /tmp/dtc.dts| File | Contents |
|---|---|
| dtbscope.mbt | Error type, cursor, hex formatting |
| reader.mbt | Bounds-checked reads of the primitive types |
| header.mbt | Blob header parse and validation |
| struct_block.mbt | Token walk, node tree, structural warnings |
| dts_token.mbt | Token kinds, spans, scanner state |
| dts_lex.mbt | Character rules and token scanning |
| dts_parse.mbt | Source grammar, value parsing, source tree |
| dts_emit.mbt | Tree back to source, byte-identical to dtc's dump |
| dump.mbt | Flat tree reading, shared by the CLI and the browser module |
| phandle.mbt | Label table, phandle numbering, reference rewrite |
| dtb_write.mbt | Blob encoder |
| cmd/main/ | Command line front end |
| overlay.mbt | Overlay generation, fixups, application |
| wasm/ | JavaScript entry points for the browser demo |
| web/ | Demo page, built into web/dist by tools/build-web.sh |
| tools/gen-fixtures.sh | Fixture regeneration |
| internal/fixtures/ | DTS sources and their compiled DTBs |
# Compile a plugin source to a .dtbo
moon run cmd/main -- board-overlay.dts --plugin -o board.dtbo
# Apply it to a base blob and write the merged result
moon run cmd/main -- board.dtbo -b board.dtb -o merged.dtb./tools/build-web.sh
python3 -m http.server -d web/dist 8000pub(all) suberror DtbError {
UnexpectedEnd(offset~ : Int64, needed~ : Int64, available~ : Int64)
BadMagic(magic~ : UInt)
BadValue(field~ : String, value~ : Int64)
OutOfBounds(what~ : String, offset~ : Int64, size~ : Int64, limit~ : Int64)
BadName(offset~ : Int64)
Inconsistent(what~ : String, detail~ : String)
LimitExceeded(what~ : String, limit~ : Int64)
Unsupported(what~ : String)
}impl Show for LexerErrorpub(all) suberror OverlayError {
UnknownSymbol(label~ : String)
MissingTarget(path~ : String)
BadFixup(detail~ : String)
NoSymbols
BadFragment(name~ : String)
Resolve(inner~ : ResolveError)
}impl Show for OverlayErrorpub(all) suberror ParseError {
Unexpected(token~ : TokenKind, expected~ : String, span~ : Span)
Lexical(inner~ : LexerError)
BadValue(detail~ : String, span~ : Span)
LimitExceeded(what~ : String, limit~ : Int)
Unsupported(what~ : String, span~ : Span)
}impl Show for ParseErrorpub(all) suberror ResolveError {
UnknownLabel(label~ : String)
UnknownPath(path~ : String)
DuplicateLabel(label~ : String, first~ : String, second~ : String)
DuplicatePhandle(value~ : UInt, first~ : String, second~ : String)
BadPhandle(value~ : UInt, node~ : String)
}impl Show for ResolveErrorpub(all) struct Bounds {
bytes : Bytes
start : Int64
limit : Int64
}pub(all) enum Endian {
Little
Big
}pub(all) struct FixupLocation {
path : String
property : String
offset : Int
}pub(all) struct Header {
totalsize : Int64
off_dt_struct : Int64
off_dt_strings : Int64
off_mem_rsvmap : Int64
version : UInt
last_comp_version : UInt
boot_cpuid_phys : UInt
size_dt_strings : Int64
size_dt_struct : Int64
}pub(all) struct Lexer {
source : String
pos : Int
line : Int
column : Int
in_bytes : Bool
}pub(all) struct Property {
name : String
value : Bytes
offset : Int64
}pub(all) struct SourceFile {
root : SourceNode
memreserves : Array[(UInt64, UInt64)]
is_plugin : Bool
fragments : Array[Fragment]
}pub(all) struct SourceNode {
name : String
labels : Array[String]
properties : Array[SourceProperty]
children : Array[SourceNode]
}pub(all) struct Span {
line : Int
column : Int
}pub(all) enum StructTag {
BeginNode
EndNode
Prop
Nop
End
}pub(all) enum TokenKind {
Ident(String)
String(String)
Number(UInt64)
LBrace
RBrace
LAngle
RAngle
LBracket
RBracket
LParen
RParen
Comma
Semicolon
Equals
Slash
Amp
Colon
Minus
HexByte(Byte)
Eof
}pub(all) enum Warning {
MissingEnd(offset~ : Int64)
TrailingData(offset~ : Int64, remaining~ : Int64)
}Install
Download zipDevice tree compiler and inspector in pure MoonBit: DTS to DTB, DTB back to DTS and overlays, byte-identical to dtc, with no dtc binary present.