dtc: Move the merging of source layers out of the grammar
v6 cover dtc: Move the merging of source layers out of the grammar
Note: v6 drops the eight patches which are now applied. It adds support for a label on a reference to the base tree in a plugin, as suggested in the v5 review, along with a libfdt fix for the symbol this gives.
Note: As of v4, this series mostly moves the merge code. The merge still happens during parsing, so none of the problems below are fixed yet. Once this one is sorted out, a new series will be started.
The parser merges each source layer of a dts file into the tree as soon as it is parsed, so nothing can look at a source layer on its own. Here a source layer is a top-level part of a dts file, such as &label { ... }; or /delete-node/ &label;, not an overlay's fragment@N node.
This causes several problems:
- A repeated node or property name gives a check failure in the first source layer, but silently overwrites when it is in a later one
- Deletes cannot be dealt with consistently
- Checks which need the whole tree give false positives for a plugin, yet checks which do not need it cannot run before the merge
The ultimate aim is to run most checks on each source layer before it is merged, and only those which need the whole tree afterwards. For this to work, we need the merge to come after dt_from_source() and build_dt_info() have returned.
As a first step, move the merge logic out of the grammar actions into livetree.c, one action at a time, passing each source layer in a new struct source_layer. The next series will have the parser return a list of source layers for main() to merge, then add a plugin's overlay fragments (fragment@N nodes) after the merge, rather than during it.
The output of these moves does not change. The only change to the errors is that some are now reported at the reference: in a plugin, a label-relative reference in a source layer which adds a label, and any error in the first source layer.
Moving the labelled-reference merge shows up a gap: in a plugin, a label on a reference to a node in the base tree gives an error, or a syntax error in the first source layer. An overlay can add such a label by putting it on an __overlay__ node, so make dtc do this, and fix the symbol which libfdt makes for it when applying the overlay.
At each commit, make check passes and dtc gives the same output and messages for every .dts and .dtso file in Linux v7.3-12284-g3799eeb906c9.
Changes in v6:
- Add new patch to allow a label on a reference to the base tree
- Add new patch to allow a label on the first source layer
- Add new patch to drop the trailing / from a symbol for __overlay__
- Add new patch to report first-layer errors at the reference
- Add patches to allow a label on a reference to the base tree
- Drop the 8 patches which are applied
- Drop the claim that dtc cannot label an overlay fragment
Changes in v5:
- Drop the merge tests and is_ref_relative() patches, which are applied
- Say that a label on a node outside the plugin is an error
- Use the term 'source layer' rather than 'fragment'
Changes in v4:
- Drop the srcpos patch, which is applied
- Explain why a fragment which adds a label does not go in an overlay
- Free the fragment after merging it
- Leave the merge after parsing and the fragment@N changes for later
- Report the label-relative error at the reference, not the label
Changes in v2:
- Combine merge_labelled_ref() into merge_ref()
- Explain the aim of the series and what it does not do
- Return true on success from merge_first_ref()
- Use dt_ref instead of fragment_ref
Simon Glass (6):
dtc: Move labelled-reference handling out of the grammar
dtc: Move first-reference handling out of the grammar
dtc: Report first-layer errors at the reference
libfdt: Drop the trailing / from a symbol for __overlay__
dtc: Allow a label on a reference to the base tree
dtc: Allow a label on the first source layer
dtc-parser.y | 65 +++++++++++--------------- dtc.h | 5 +- libfdt/fdt_overlay.c | 12 +++-- livetree.c | 52 ++++++++++++++++----- tests/merge-first-labelled-missing.dts | 6 +++ tests/merge-first-labelled.dts | 10 ++++ tests/merge-labelled-base-tree.dts | 9 ++++ tests/merge-labelled-base.dts | 19 ++++++++ tests/merge-labelled-missing.dts | 14 ------ tests/run_tests.sh | 60 +++++++++++++++++++++--- tests/stacked_overlay_symbols.dts | 19 ++++++++ 11 files changed, 193 insertions(+), 78 deletions(-) create mode 100644 tests/merge-first-labelled-missing.dts create mode 100644 tests/merge-first-labelled.dts create mode 100644 tests/merge-labelled-base-tree.dts create mode 100644 tests/merge-labelled-base.dts delete mode 100644 tests/merge-labelled-missing.dts create mode 100644 tests/stacked_overlay_symbols.dts
---
base-commit: d6c37926ff8e4d15c1aaaf2b6f1147e78cedc7c3
branch: dt-forest6
Signature · 1 line
2.43.0
PATCH v6 1/6 dtc: Move labelled-reference handling out of the grammar
The grammar action for a top-level label: &ref { ... } holds its merge logic, which adds the label to the node referred to.
Move it into merge_ref(), passing it a struct source_layer with the label.
The output does not change. In a plugin, the error for a label-relative reference now gives the position of the reference rather than the label, since the problem is with the reference. Update the merge-labelled-relative.dts test to match.
---
Changes in v6:
- Drop the claim that dtc cannot label an overlay fragment
Changes in v5:
- Say that a label on a node outside the plugin is an error
- Use the term 'source layer' rather than 'fragment'
Changes in v4:
- Explain why a fragment which adds a label does not go in an overlay
- Free the fragment after merging it
- Report the label-relative error at the reference, not the label
Changes in v2:
- Combine merge_labelled_ref() into merge_ref()
- Use dt_ref instead of fragment_ref
dtc-parser.y | 14 +++++--------- livetree.c | 8 ++++++-- tests/run_tests.sh | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-)
dtc-parser.y +5 -9
@@ -180,16 +180,12 @@ devicetree: } | devicetree DT_LABEL dt_ref nodedef { - struct node *target = get_node_by_ref($1, $3); - - if (($<flags>-1 & DTSF_PLUGIN) && is_ref_relative($3)) - ERROR(&@2, "Label-relative reference %s not supported in plugin", $3); + struct source_layer *layer; - if (target) { - add_label(&target->labels, $2); - merge_nodes(target, $4); - } else - ERROR(&@3, "Label or path %s not found", $3); + layer = build_layer_ref($2, $3, &@3, $4); + if (!merge_ref($1, layer, $<flags>-1 & DTSF_PLUGIN)) + treesource_error = true; + free_layer(layer); $$ = $1; } | devicetree dt_ref nodedef
livetree.c +6 -2
@@ -545,10 +545,12 @@ bool is_ref_relative(const char *ref)/* * Merge a source layer which refers to a label or path into the node it refers -* to. +* to, adding any label in the source layer to that node. * * In a plugin, a path always refers to the base tree and a label may do so, if * it is not found. In both cases, the source layer becomes an overlay fragment. +* A source layer which adds a label is not handled in this way: it must refer +* to a node in the plugin, or it is an error. * * Returns true on success, false on an error, which has been reported. */
@@ -565,7 +567,7 @@ bool merge_ref(struct node *dt, struct source_layer *layer, bool plugin) layer->ref); ok = false; } -if (layer->ref[0 ] =='/' || !target) { +if (!layer->label && (layer->ref[0 ] =='/' || !target)) {/* Add a fragment@N node for the reference */ add_orphan_node (dt, layer->node, layer->ref);return ok;
@@ -577,6 +579,8 @@ bool merge_ref(struct node *dt, struct source_layer *layer, bool plugin) "Label or path %s not found", layer->ref);return false; } +if (layer->label) +add_label (&target->labels, layer->label);merge_nodes (target, layer->node);return ok;
tests/run_tests.sh +1 -1
@@ -782,7 +782,7 @@ dtc_tests () { $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref.dts" run_wrap_error_msg_test "merge-first-ref-relative.dts:5.15-7.3 Label-relative reference label/node not supported in plugin" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref-relative.dts" - run_wrap_error_msg_test "merge-labelled-relative.dts:8.1-10 Label-relative reference baselabel/subnode not supported in plugin" \ + run_wrap_error_msg_test "merge-labelled-relative.dts:8.11-31 Label-relative reference baselabel/subnode not supported in plugin" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-labelled-relative.dts" run_wrap_error_msg_test "merge-path-relative.dts:8.1-18 Label-relative reference baselabel/node not supported in plugin" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-path-relative.dts"
Signature · 1 line
2.43.0
PATCH v6 2/6 dtc: Move first-reference handling out of the grammar
In reply to Simon GlassIn a plugin, the first source layer can refer to a label or path instead of being the root node. The grammar action for this holds its merge logic, which makes an overlay fragment in a new tree.
Move it into a new merge_first_ref(), passing it a struct source_layer for the reference and node. This is the last user of is_ref_relative() in the grammar, so make that function static.
The output does not change.
---
(no changes since v5)
Changes in v5:
- Use the term 'source layer' rather than 'fragment'
Changes in v4:
- Free the fragment after merging it
Changes in v2:
- Return true on success from merge_first_ref()
- Use dt_ref instead of fragment_ref
dtc-parser.y | 15 +++++++-------- dtc.h | 3 ++- livetree.c | 31 ++++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 10 deletions(-)
dtc-parser.y +7 -8
@@ -164,19 +164,18 @@ devicetree: } | dt_ref nodedef { + struct source_layer *layer; + + layer = build_layer_ref(NULL, $1, &@1, $2); /* * We rely on the rule being always: * versioninfo plugindecl memreserves devicetree * so $-1 is what we want (plugindecl) */ - if (!($<flags>-1 & DTSF_PLUGIN)) - ERROR(&@2, "Label or path %s not found", $1); - else if (is_ref_relative($1)) - ERROR(&@2, "Label-relative reference %s not supported in plugin", $1); - $$ = add_orphan_node( - name_node(build_node(NULL, NULL, NULL), - ""), - $2, $1); + if (!merge_first_ref(&$$, layer, + $<flags>-1 & DTSF_PLUGIN)) + treesource_error = true; + free_layer(layer); } | devicetree DT_LABEL dt_ref nodedef {
dtc.h +2 -1
@@ -351,6 +351,8 @@ struct source_layer *build_layer_delete_node(char *ref,struct source_layer *build_layer_omit_if_no_ref (char *ref,struct srcpos *ref_srcpos);void free_layer (struct source_layer *layer); +bool merge_first_ref (struct node **dtp,struct source_layer *layer, +bool plugin);bool merge_ref (struct node *dt,struct source_layer *layer,bool plugin);bool merge_delete_node (struct node *dt,struct source_layer *layer);bool merge_omit_if_no_ref (struct node *dt,struct source_layer *layer);
@@ -370,7 +372,6 @@ struct dt_info {struct dt_info *build_dt_info (unsigned int dtsflags,struct reserve_info *reservelist,struct node *tree,uint32_t boot_cpuid_phys); -bool is_ref_relative (const char *ref);void sort_tree (struct dt_info *dti);void generate_labels_from_tree (struct dt_info *dti,const char *name);void generate_label_tree (struct dt_info *dti,const char *name,bool allocph);
livetree.c +30 -1
@@ -538,11 +538,40 @@ struct dt_info *build_dt_info(unsigned int dtsflags, }/* Check whether a reference is relative to a label, e.g. &{label/node} */ -bool is_ref_relative (const char *ref) +static bool is_ref_relative (const char *ref) {return ref[0 ] !='/' &&strchr (&ref[1 ],'/' ); } +/* +* Make the tree at *dtp from a first source layer which refers to a label or +* path, rather than being the root node. This is only valid in a plugin, where +* the source layer becomes an overlay fragment. +* +* Returns true on success, false on an error, which has been reported. +*/ +bool merge_first_ref (struct node **dtp,struct source_layer *layer, +bool plugin) +{ +bool ok = true; + +if (!plugin) { +srcpos_error (layer->node->srcpos,"Error" , +"Label or path %s not found" , layer->ref); + ok = false; + }else if (is_ref_relative (layer->ref)) { +srcpos_error (layer->node->srcpos,"Error" , +"Label-relative reference %s not supported in plugin" , + layer->ref); + ok = false; + } +/* Make a new, empty tree with a fragment@N node for the reference */ + *dtp =add_orphan_node (name_node (build_node (NULL ,NULL ,NULL ),"" ), + layer->node, layer->ref); + +return ok; +} +/* * Merge a source layer which refers to a label or path into the node it refers * to, adding any label in the source layer to that node.
Signature · 1 line
2.43.0
PATCH v6 3/6 dtc: Report first-layer errors at the reference
In reply to Simon Glassmerge_first_ref() reports its errors at the node, while merge_ref() reports the same errors at the reference, since the problem is with the reference. So the position for an error depends on whether the source layer is the first one.
Use the position of the reference in merge_first_ref() too, and update the merge-first-ref.dts and merge-first-ref-relative.dts tests to match.
---
Changes in v6:
- Add new patch to report first-layer errors at the reference
livetree.c | 4 ++-- tests/run_tests.sh | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
livetree.c +2 -2
@@ -556,11 +556,11 @@ bool merge_first_ref(struct node **dtp, struct source_layer *layer,bool ok = true;if (!plugin) { -srcpos_error (layer->node->srcpos,"Error" , +srcpos_error (layer->ref_srcpos,"Error" ,"Label or path %s not found" , layer->ref); ok = false; }else if (is_ref_relative (layer->ref)) { -srcpos_error (layer->node->srcpos,"Error" , +srcpos_error (layer->ref_srcpos,"Error" ,"Label-relative reference %s not supported in plugin" , layer->ref); ok = false;
tests/run_tests.sh +2 -2
@@ -778,9 +778,9 @@ dtc_tests () { $DTC -I dts -O dtb -o /dev/null "$SRCDIR/test_label_ref.dts" run_wrap_error_msg_test "nonexist-node-ref2.dts:9.1-12 Label or path nosuchnode not found" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/nonexist-node-ref2.dts" - run_wrap_error_msg_test "merge-first-ref.dts:4.13-6.3 Label or path nosuchnode not found" \ + run_wrap_error_msg_test "merge-first-ref.dts:4.1-12 Label or path nosuchnode not found" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref.dts" - run_wrap_error_msg_test "merge-first-ref-relative.dts:5.15-7.3 Label-relative reference label/node not supported in plugin" \ + run_wrap_error_msg_test "merge-first-ref-relative.dts:5.1-14 Label-relative reference label/node not supported in plugin" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref-relative.dts" run_wrap_error_msg_test "merge-labelled-relative.dts:8.11-31 Label-relative reference baselabel/subnode not supported in plugin" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-labelled-relative.dts"
Signature · 1 line
2.43.0
PATCH v6 4/6 libfdt: Drop the trailing / from a symbol for __overlay__
In reply to Simon GlassWhen an overlay has a label on an __overlay__ node, its symbol is /fragment@N/__overlay__ and overlay_symbol_update() turns this into the path of the target with a / on the end, such as /foonode/ for the stacked_overlay_addlabel.dts test. The symbol should be the path of the target itself, /foonode in this case.
Only add the / and the relative path if there is a relative path, or the target is the root node.
Check the symbol in the stacked_overlay_addlabel.dts test. Add stacked_overlay_symbols.dts to check the symbols for a root target, with and without a relative path, and for a phandle target.
---
Changes in v6:
- Add new patch to drop the trailing / from a symbol for __overlay__
libfdt/fdt_overlay.c | 12 ++++++++---- tests/run_tests.sh | 12 ++++++++++++ tests/stacked_overlay_symbols.dts | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 tests/stacked_overlay_symbols.dts
libfdt/fdt_overlay.c +8 -4
@@ -1119,7 +1119,8 @@ static int overlay_symbol_update(void *fdt, void *fdto) } ret =fdt_setprop_placeholder (fdt, root_sym, name, - len + (len >1 ) + rel_path_len +1 , &p); + len + (len >1 && rel_path_len) + + rel_path_len +1 , &p);if (ret <0 )return ret;
@@ -1143,9 +1144,12 @@ static int overlay_symbol_update(void *fdt, void *fdto) }else len--; - buf[len] ='/' ; -memcpy (buf + len +1 , rel_path, rel_path_len); - buf[len +1 + rel_path_len] ='\0' ; +/* add the relative path, unless the symbol is the target */ +if (!len || rel_path_len) { + buf[len] ='/' ; +memcpy (buf + len +1 , rel_path, rel_path_len); + buf[len +1 + rel_path_len] ='\0' ; + } }return 0 ;
tests/run_tests.sh +12 -0
@@ -1186,6 +1186,18 @@ fdtoverlay_tests() { run_dtc_test -@ -I dts -O dtb -o $stacked_addlabeldtb $stacked_addlabel run_fdtoverlay_test baz "/foonode/barnode/baznode" "baz-property" "-ts" ${stacked_base_nolabeldtb} ${stacked_addlabel_targetdtb} ${stacked_addlabeldtb} ${stacked_bardtb} ${stacked_bazdtb} + run_fdtoverlay_test "/foonode" "/__symbols__" "foo" "-ts" ${stacked_base_nolabeldtb} ${stacked_addlabel_targetdtb} ${stacked_addlabeldtb} + + # test the symbols for a root target and a phandle target + stacked_symbols="$SRCDIR/stacked_overlay_symbols.dts" + stacked_symbolsdtb=stacked_overlay_symbols.test.dtb + stacked_symbols_targetdtb=stacked_overlay_target_symbols.fdtoverlay.test.dtb + + run_dtc_test -@ -I dts -O dtb -o $stacked_symbolsdtb $stacked_symbols + + run_fdtoverlay_test "/" "/__symbols__" "root" "-ts" ${stacked_basedtb} ${stacked_symbols_targetdtb} ${stacked_symbolsdtb} + run_fdtoverlay_test "/rootnode" "/__symbols__" "rootsub" "-ts" ${stacked_basedtb} ${stacked_symbols_targetdtb} ${stacked_symbolsdtb} + run_fdtoverlay_test "/foonode" "/__symbols__" "phfoo" "-ts" ${stacked_basedtb} ${stacked_symbols_targetdtb} ${stacked_symbolsdtb} # verify that phandles are not overwritten run_dtc_test -@ -I dts -O dtb -o overlay_base_phandle.test.dtb "$SRCDIR/overlay_base_phandle.dts"
tests/stacked_overlay_symbols.dts new file +19 -0
@@ -0,0 +1,19 @@ +/dts-v1/; +/plugin/; +/ { + fragment@0 { + target-path = "/"; + root: __overlay__ { + rootsub: rootnode { + rootnode-property; + }; + }; + }; + + fragment@1 { + target = <&foo>; + phfoo: __overlay__ { + overlay-property; + }; + }; +};
Signature · 1 line
2.43.0
PATCH v6 5/6 dtc: Allow a label on a reference to the base tree
In reply to Simon GlassIn a plugin, label: &ref { ... }; gives an error unless ref is a node in the plugin itself. But an overlay can add a label to a node in the base tree, by putting the label on the __overlay__ node of its fragment@N node, which libfdt supports.
Make an overlay fragment for such a source layer, as when it has no label, and add the label to its __overlay__ node.
A path now always refers to the base tree, even with a label, so
label: &{/node} { ... }; no longer adds the label to a /node in the plugin.
With -@, dtc gives each labelled node a phandle, including an __overlay__ node, and libfdt copies this to the target when it applies the overlay. This leaves two problems to deal with later:
- If two source layers label the same node in the base tree, each __overlay__ node has its own phandle and the second replaces the first, so a reference to the first label within the overlay is left dangling
- libfdt before v1.7.1 fails to apply the overlay if the target is given by a label and already has a phandle
One option is to give an __overlay__ node a phandle only when something refers to it.
Rename merge-labelled-missing.dts to merge-labelled-base.dts, add a second label on the same node and check the symbols, both in the plugin and after fdtoverlay applies it to a base tree.
---
Changes in v6:
- Add new patch to allow a label on a reference to the base tree
livetree.c | 9 +++++---- tests/merge-labelled-base-tree.dts | 9 +++++++++ tests/merge-labelled-base.dts | 19 +++++++++++++++++++ tests/merge-labelled-missing.dts | 14 -------------- tests/run_tests.sh | 29 +++++++++++++++++++++++++---- 5 files changed, 58 insertions(+), 22 deletions(-) create mode 100644 tests/merge-labelled-base-tree.dts create mode 100644 tests/merge-labelled-base.dts delete mode 100644 tests/merge-labelled-missing.dts
livetree.c +5 -4
@@ -577,9 +577,8 @@ bool merge_first_ref(struct node **dtp, struct source_layer *layer,* to, adding any label in the source layer to that node. * * In a plugin, a path always refers to the base tree and a label may do so, if -* it is not found. In both cases, the source layer becomes an overlay fragment. -* A source layer which adds a label is not handled in this way: it must refer -* to a node in the plugin, or it is an error. +* it is not found. In both cases, the source layer becomes an overlay fragment, +* with any label on its __overlay__ node. * * Returns true on success, false on an error, which has been reported. */
@@ -596,8 +595,10 @@ bool merge_ref(struct node *dt, struct source_layer *layer, bool plugin) layer->ref); ok = false; } -if (!layer->label && (layer->ref[0 ] =='/' || !target)) { +if (layer->ref[0 ] =='/' || !target) {/* Add a fragment@N node for the reference */ +if (layer->label) +add_label (&layer->node->labels, layer->label);add_orphan_node (dt, layer->node, layer->ref);return ok; }
tests/merge-labelled-base-tree.dts new file +9 -0
@@ -0,0 +1,9 @@ +/dts-v1/; + +/ { + baselabel: node { + }; + + basenode { + }; +};
tests/merge-labelled-base.dts new file +19 -0
@@ -0,0 +1,19 @@ +/dts-v1/; +/plugin/; + +/ { +}; + +/* A label on a node in the base tree goes on the __overlay__ node */ +newlabel1: &baselabel { + prop1; +}; + +newlabel2: &{/basenode} { + prop2; +}; + +/* Each source layer has its own overlay fragment, even for the same node */ +newlabel3: &baselabel { + prop3; +};
tests/merge-labelled-missing.dts deleted +0 -14
@@ -1,14 +0,0 @@ -/dts-v1/; -/plugin/; - -/ { -}; - -/* A label can only be added to a node in the plugin */ -newlabel1: &nosuchnode { - prop; -}; - -newlabel2: &{/nosuchnode} { - prop; -};
tests/run_tests.sh +25 -4
@@ -798,10 +798,6 @@ dtc_tests () { $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-errors.dts" run_wrap_error_msg_test "merge-errors.dts:14.18-30 Label or path nosuchnode3 not found" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-errors.dts" - run_wrap_error_msg_test "merge-labelled-missing.dts:8.12-23 Label or path nosuchnode not found" \ - $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-labelled-missing.dts" - run_wrap_error_msg_test "merge-labelled-missing.dts:12.12-26 Label or path /nosuchnode not found" \ - $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-labelled-missing.dts" # A labelled reference to a node in a plugin adds the label to it run_dtc_test -@ -I dts -O dtb -o merge-labelled-plugin.test.dtb \
@@ -809,6 +805,16 @@ dtc_tests () { run_fdtget_test "/node" merge-labelled-plugin.test.dtb /__symbols__ newlabel run_fdtget_test "value" merge-labelled-plugin.test.dtb /node prop + # A labelled reference to the base tree labels the __overlay__ node + run_dtc_test -@ -I dts -O dtb -o merge-labelled-base.test.dtb \ + "$SRCDIR/merge-labelled-base.dts" + run_fdtget_test "/fragment@0/__overlay__" merge-labelled-base.test.dtb \ + /__symbols__ newlabel1 + run_fdtget_test "/fragment@1/__overlay__" merge-labelled-base.test.dtb \ + /__symbols__ newlabel2 + run_fdtget_test "/fragment@2/__overlay__" merge-labelled-base.test.dtb \ + /__symbols__ newlabel3 + # In a plugin, &{label} is the same as &label run_dtc_test -I dts -O dtb -o merge-label-braces.test.dtb \ "$SRCDIR/merge-label-braces.dts"
@@ -1199,6 +1205,21 @@ fdtoverlay_tests() { run_fdtoverlay_test "/rootnode" "/__symbols__" "rootsub" "-ts" ${stacked_basedtb} ${stacked_symbols_targetdtb} ${stacked_symbolsdtb} run_fdtoverlay_test "/foonode" "/__symbols__" "phfoo" "-ts" ${stacked_basedtb} ${stacked_symbols_targetdtb} ${stacked_symbolsdtb} + # test adding a label to a node in the base tree + run_dtc_test -@ -I dts -O dtb -o merge-labelled-base-tree.test.dtb \ + "$SRCDIR/merge-labelled-base-tree.dts" + run_dtc_test -@ -I dts -O dtb -o merge-labelled-base.test.dtb \ + "$SRCDIR/merge-labelled-base.dts" + run_fdtoverlay_test "/node" "/__symbols__" "newlabel1" "-ts" \ + merge-labelled-base-tree.test.dtb merge-labelled-target.test.dtb \ + merge-labelled-base.test.dtb + run_fdtoverlay_test "/basenode" "/__symbols__" "newlabel2" "-ts" \ + merge-labelled-base-tree.test.dtb merge-labelled-target.test.dtb \ + merge-labelled-base.test.dtb + run_fdtoverlay_test "/node" "/__symbols__" "newlabel3" "-ts" \ + merge-labelled-base-tree.test.dtb merge-labelled-target.test.dtb \ + merge-labelled-base.test.dtb + # verify that phandles are not overwritten run_dtc_test -@ -I dts -O dtb -o overlay_base_phandle.test.dtb "$SRCDIR/overlay_base_phandle.dts" run_dtc_test -@ -I dts -O dtb -o overlay_overlay_phandle.test.dtb "$SRCDIR/overlay_overlay_phandle.dts"
Signature · 1 line
2.43.0
PATCH v6 6/6 dtc: Allow a label on the first source layer
In reply to Simon GlassIn a plugin, a label on the first source layer, as in
label: &ref { ... };, gives a syntax error, although a later source layer accepts it.
Add an optional label, maybe_label, to the grammar rule for this, passing the label to merge_first_ref(), which adds it to the __overlay__ node, as for a later source layer. Use maybe_label for a later source layer too, so its labelled and unlabelled rules become one. Outside a plugin, this gives the same 'Label or path ... not found' error as &ref { ... }; does.
A memreserve can also have a label. With the memreserves rule being right-recursive, the parser must decide whether the memreserves have ended when it sees the label, before it knows which this is. Make the rule left-recursive to avoid this conflict, building the list with add_reserve_entry(), and drop chain_reserve_entry() which is no longer used. The memreserves stay in the same order, since each one is now added to the end of the list, rather than the start.
The comment about $<flags>-1 names nonterminals which no longer exist. Correct it and put it once above the devicetree rule, rather than in only some of the actions which rely on it.
Add merge-first-labelled.dts to check this, along with merge-first-labelled-missing.dts for the error outside a plugin.
---
Changes in v6:
- Add new patch to allow a label on the first source layer
dtc-parser.y | 44 +++++++++++--------------- dtc.h | 2 -- livetree.c | 14 +++----- tests/merge-first-labelled-missing.dts | 6 ++++ tests/merge-first-labelled.dts | 10 ++++++ tests/run_tests.sh | 13 ++++++++ 6 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 tests/merge-first-labelled-missing.dts create mode 100644 tests/merge-first-labelled.dts
dtc-parser.y +19 -25
@@ -76,6 +76,7 @@ extern bool treesource_error; %type <prop> propdef %type <proplist> proplist %type <labelref> dt_ref +%type <labelref> maybe_label %type <node> devicetree %type <node> nodedef
@@ -133,9 +134,9 @@ memreserves: { $$ = NULL; } - | memreserve memreserves + | memreserves memreserve { - $$ = chain_reserve_entry($1, $2); + $$ = add_reserve_entry($1, $2); } ;
@@ -153,6 +154,19 @@ memreserve: dt_ref: DT_LABEL_REF | DT_PATH_REF; +maybe_label: + /* empty */ + { + $$ = NULL; + } + | DT_LABEL + ; + +/* + * The actions which check for a plugin rely on the rule being always: + * headers memreserves devicetree + * so $<flags>-1 is what we want (headers) + */ devicetree: '/' nodedef {
@@ -162,22 +176,17 @@ devicetree: { $$ = merge_nodes($1, $3); } - | dt_ref nodedef + | maybe_label dt_ref nodedef { struct source_layer *layer; - layer = build_layer_ref(NULL, $1, &@1, $2); - /* - * We rely on the rule being always: - * versioninfo plugindecl memreserves devicetree - * so $-1 is what we want (plugindecl) - */ + layer = build_layer_ref($1, $2, &@2, $3); if (!merge_first_ref(&$$, layer, $<flags>-1 & DTSF_PLUGIN)) treesource_error = true; free_layer(layer); } - | devicetree DT_LABEL dt_ref nodedef + | devicetree maybe_label dt_ref nodedef { struct source_layer *layer;
@@ -187,21 +196,6 @@ devicetree: free_layer(layer); $$ = $1; } - | devicetree dt_ref nodedef - { - struct source_layer *layer; - - layer = build_layer_ref(NULL, $2, &@2, $3); - /* - * We rely on the rule being always: - * versioninfo plugindecl memreserves devicetree - * so $-1 is what we want (plugindecl) - */ - if (!merge_ref($1, layer, $<flags>-1 & DTSF_PLUGIN)) - treesource_error = true; - free_layer(layer); - $$ = $1; - } | devicetree DT_DEL_NODE dt_ref ';' { struct source_layer *layer;
dtc.h +0 -2
@@ -313,8 +313,6 @@ struct reserve_info { };struct reserve_info *build_reserve_entry (uint64_t start,uint64_t len); -struct reserve_info *chain_reserve_entry (struct reserve_info *first, -struct reserve_info *list);struct reserve_info *add_reserve_entry (struct reserve_info *list,struct reserve_info *new);
livetree.c +4 -10
@@ -421,15 +421,6 @@ struct reserve_info *build_reserve_entry(uint64_t address, uint64_t size)return new; } -struct reserve_info *chain_reserve_entry (struct reserve_info *first, -struct reserve_info *list) -{ -assert (first->next ==NULL ); - - first->next = list; -return first; -} -struct reserve_info *add_reserve_entry (struct reserve_info *list,struct reserve_info *new) {
@@ -546,7 +537,8 @@ static bool is_ref_relative(const char *ref)/* * Make the tree at *dtp from a first source layer which refers to a label or * path, rather than being the root node. This is only valid in a plugin, where -* the source layer becomes an overlay fragment. +* the source layer becomes an overlay fragment, with any label on its +* __overlay__ node. * * Returns true on success, false on an error, which has been reported. */
@@ -565,6 +557,8 @@ bool merge_first_ref(struct node **dtp, struct source_layer *layer, layer->ref); ok = false; } +if (layer->label) +add_label (&layer->node->labels, layer->label);/* Make a new, empty tree with a fragment@N node for the reference */ *dtp =add_orphan_node (name_node (build_node (NULL ,NULL ,NULL ),"" ), layer->node, layer->ref);
tests/merge-first-labelled-missing.dts new file +6 -0
@@ -0,0 +1,6 @@ +/dts-v1/; + +/* Only a plugin can start with a reference, even with a label */ +newlabel: &nosuchnode { + prop; +};
tests/merge-first-labelled.dts new file +10 -0
@@ -0,0 +1,10 @@ +/dts-v1/; +/plugin/; + +/* The parser must tell a labelled memreserve from a labelled source layer */ +mem: /memreserve/ 0x1000 0x100; + +/* A label on the first source layer goes on the __overlay__ node */ +newlabel: &baselabel { + prop; +};
tests/run_tests.sh +13 -0
@@ -782,6 +782,8 @@ dtc_tests () { $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref.dts" run_wrap_error_msg_test "merge-first-ref-relative.dts:5.1-14 Label-relative reference label/node not supported in plugin" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-ref-relative.dts" + run_wrap_error_msg_test "merge-first-labelled-missing.dts:4.11-22 Label or path nosuchnode not found" \ + $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-first-labelled-missing.dts" run_wrap_error_msg_test "merge-labelled-relative.dts:8.11-31 Label-relative reference baselabel/subnode not supported in plugin" \ $DTC -I dts -O dtb -o /dev/null "$SRCDIR/merge-labelled-relative.dts" run_wrap_error_msg_test "merge-path-relative.dts:8.1-18 Label-relative reference baselabel/node not supported in plugin" \
@@ -815,6 +817,12 @@ dtc_tests () { run_fdtget_test "/fragment@2/__overlay__" merge-labelled-base.test.dtb \ /__symbols__ newlabel3 + # The same applies to a label on the first source layer + run_dtc_test -@ -I dts -O dtb -o merge-first-labelled.test.dtb \ + "$SRCDIR/merge-first-labelled.dts" + run_fdtget_test "/fragment@0/__overlay__" merge-first-labelled.test.dtb \ + /__symbols__ newlabel + # In a plugin, &{label} is the same as &label run_dtc_test -I dts -O dtb -o merge-label-braces.test.dtb \ "$SRCDIR/merge-label-braces.dts"
@@ -1219,6 +1227,11 @@ fdtoverlay_tests() { run_fdtoverlay_test "/node" "/__symbols__" "newlabel3" "-ts" \ merge-labelled-base-tree.test.dtb merge-labelled-target.test.dtb \ merge-labelled-base.test.dtb + run_dtc_test -@ -I dts -O dtb -o merge-first-labelled.test.dtb \ + "$SRCDIR/merge-first-labelled.dts" + run_fdtoverlay_test "/node" "/__symbols__" "newlabel" "-ts" \ + merge-labelled-base-tree.test.dtb merge-labelled-target.test.dtb \ + merge-first-labelled.test.dtb # verify that phandles are not overwritten run_dtc_test -@ -I dts -O dtb -o overlay_base_phandle.test.dtb "$SRCDIR/overlay_base_phandle.dts"
Signature · 1 line
2.43.0