Add capability to create property from old property
v3 cover Add capability to create property from old property
Allow construction of new values for a property using old property value.
This was originally suggested in /append-property/ Patch series [0] and can be used to achieve the same results (and much more).
In practice, it looks as follows:
dts-v1/;
/ {
str-prop = "0";
int-prop = <0>;
};/ {
str-prop = /./, "1", /./;
int-prop = /./, <1>, /./;
};dts to source output with -T -T also works as expected:
/dts-v1/;
/ { /* base.dts:3:3-5:3, base.dts:7:3-9:3 */
int-prop = <0x00 0x01>, <0x02 0x03>, <0x00 0x01>; /* base.dts:4:9-4:26, base.dts:8:9-8:36 */ }; /* base.dts:3:3-5:3, base.dts:7:3-9:3 */
[0]: https://lore.kernel.org/devicetree-compiler/Z24nldCpXpoT7RaK@zatzit/T/#t
---
Changes in v3:
- Rebase on main
- Fix Patch 2 commit message example.
- Rename data_insert_old_value to data_insert_data.
- Move data_insert_data to data.c and split into independent patch.
- Rename DT_PREV_PROP to DT_PREV_VALUE.
- Make prev_value_used local to while loop.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Compare against expected dtb instead of checking each property
individually in tests.
- Use xstrdup when copying refs so each marker has it's own copy.
- Pass struct data by value, not by pointer.
- Add srcpos_free helper to properly free srcpos which have been
extended previously.
- Remove open items since my original choices seem correct.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Ayush Singh (4):
srcpos: Define srcpos_free
dtc: Add data_insert_data function
dtc: Add /./
tests: Add test for /./
data.c | 27 +++++++++++++++++++++++++++ dtc-lexer.l | 5 +++++ dtc-parser.y | 5 +++++ dtc.h | 2 ++ livetree.c | 23 +++++++++++++++++++++-- srcpos.c | 11 +++++++++++ srcpos.h | 1 + tests/prev_prop.dts | 25 +++++++++++++++++++++++++ tests/prev_prop_expected.dts | 13 +++++++++++++ tests/run_tests.sh | 5 +++++ 10 files changed, 115 insertions(+), 2 deletions(-)
---
base-commit: e0b7749c26a9ea28a480bca4a87d238e284ac68f
change-id: 20241227-previous-value-c22d4eeaae72
Best regards,
Signature · 1 line
Ayush Singh <[email protected]>
PATCH v3 1/4 srcpos: Define srcpos_free
srcpos can be chained together using srcpos_extend. However, in such cases, we need to free all the chained nodes.
srcpos_free is a helper to recursively free all the linked srcpos.
--- livetree.c | 2 +- srcpos.c | 11 +++++++++++ srcpos.h | 1 + 3 files changed, 13 insertions(+), 1 deletion(-)
livetree.c +1 -1
@@ -174,7 +174,7 @@ struct node *merge_nodes(struct node *old_node, struct node *new_node) old_prop->val = new_prop->val; old_prop->deleted =0 ; -free (old_prop->srcpos); +srcpos_free (old_prop->srcpos); old_prop->srcpos = new_prop->srcpos;free (new_prop); new_prop =NULL ;
srcpos.c +11 -0
@@ -287,6 +287,17 @@ struct srcpos *srcpos_extend(struct srcpos *pos, struct srcpos *newtail)return pos; } +void srcpos_free (struct srcpos *pos) +{ +struct srcpos *p_next; + +while (pos) { + p_next = pos->next; +free (pos); + pos = p_next; + } +} +char *srcpos_string (struct srcpos *pos) {
srcpos.h +1 -0
@@ -88,6 +88,7 @@ extern void srcpos_update(struct srcpos *pos, const char *text, int len);extern struct srcpos *srcpos_copy (struct srcpos *pos);extern struct srcpos *srcpos_extend (struct srcpos *new_srcpos,struct srcpos *old_srcpos); +extern void srcpos_free (struct srcpos *pos);extern char *srcpos_string (struct srcpos *pos);extern char *srcpos_string_first (struct srcpos *pos,int level);extern char *srcpos_string_last (struct srcpos *pos,int level);
Signature · 1 line
2.49.0
David Gibson
AppliedRe: [PATCH v3 1/4] srcpos: Define srcpos_free
Quoted 6 lines · Ayush Singh
srcpos can be chained together using
srcpos_extend. However, in such cases, we need to free all the chained nodes.
srcpos_freeis a helper to recursively free all the linked srcpos.Signed-off-by Ayush Singh <[email protected]>
Merged, thanks.
Quoted 54 lines
--- livetree.c | 2 +- srcpos.c | 11 +++++++++++ srcpos.h | 1 + 3 files changed, 13 insertions(+), 1 deletion(-)livetree.c +1
-1@@ -174,7 +174,7 @@ struct node *merge_nodes(struct node *old_node, struct node *new_node) old_prop->val = new_prop->val; old_prop->deleted =0 ; -free (old_prop->srcpos); +srcpos_free (old_prop->srcpos); old_prop->srcpos = new_prop->srcpos;free (new_prop); new_prop =NULL ;srcpos.c +11
-0@@ -287,6 +287,17 @@ struct srcpos *srcpos_extend(struct srcpos *pos, struct srcpos *newtail)return pos; } +void srcpos_free (struct srcpos *pos) +{ +struct srcpos *p_next; + +while (pos) { + p_next = pos->next; +free (pos); + pos = p_next; + } +} +char *srcpos_string (struct srcpos *pos) {srcpos.h +1
-0@@ -88,6 +88,7 @@ extern void srcpos_update(struct srcpos *pos, const char *text, int len);extern struct srcpos *srcpos_copy (struct srcpos *pos);extern struct srcpos *srcpos_extend (struct srcpos *new_srcpos,struct srcpos *old_srcpos); +extern void srcpos_free (struct srcpos *pos);extern char *srcpos_string (struct srcpos *pos);extern char *srcpos_string_first (struct srcpos *pos,int level);extern char *srcpos_string_last (struct srcpos *pos,int level);
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
PATCH v3 2/4 dtc: Add data_insert_data function
In reply to Ayush SinghAdd helper function to insert a data struct into another.
--- data.c | 27 +++++++++++++++++++++++++++ dtc.h | 1 + 2 files changed, 28 insertions(+)
data.c +27 -0
@@ -251,6 +251,33 @@ bool data_is_one_string(struct data d)return true; } +struct data data_insert_data (struct data d,struct marker *m,struct data old) +{ +unsigned int offset = m->offset; +struct marker *next = m->next; +struct marker *marker; +struct data new_data; +char *ref; + + new_data =data_insert_at_marker (d, m, old.val, old.len); + +/* Copy all markers from old value */ + marker = old.markers; +for_each_marker (marker) { + ref =NULL ; + +if (marker->ref) + ref =xstrdup (marker->ref); + + m->next =alloc_marker (marker->offset + offset, marker->type, + ref); + m = m->next; + } + m->next = next; + +return new_data; +} +struct marker *alloc_marker (unsigned int offset,enum markertype type,char *ref) {
dtc.h +1 -0
@@ -182,6 +182,7 @@ struct data data_append_addr(struct data d, uint64_t addr);struct data data_append_byte (struct data d,uint8_t byte);struct data data_append_zeroes (struct data d,int len);struct data data_append_align (struct data d,int align); +struct data data_insert_data (struct data d,struct marker *m,struct data old);struct marker *alloc_marker (unsigned int offset,enum markertype type,char *ref);
Signature · 1 line
2.49.0
David Gibson
AppliedRe: [PATCH v3 2/4] dtc: Add data_insert_data function
Quoted 1 line · Ayush Singh
Add helper function to insert a data struct into another.
Applied, thanks.
Quoted 58 lines
Signed-off-by Ayush Singh <[email protected]>--- data.c | 27 +++++++++++++++++++++++++++ dtc.h | 1 + 2 files changed, 28 insertions(+)data.c +27
-0@@ -251,6 +251,33 @@ bool data_is_one_string(struct data d)return true; } +struct data data_insert_data (struct data d,struct marker *m,struct data old) +{ +unsigned int offset = m->offset; +struct marker *next = m->next; +struct marker *marker; +struct data new_data; +char *ref; + + new_data =data_insert_at_marker (d, m, old.val, old.len); + +/* Copy all markers from old value */ + marker = old.markers; +for_each_marker (marker) { + ref =NULL ; + +if (marker->ref) + ref =xstrdup (marker->ref); + + m->next =alloc_marker (marker->offset + offset, marker->type, + ref); + m = m->next; + } + m->next = next; + +return new_data; +} +struct marker *alloc_marker (unsigned int offset,enum markertype type,char *ref) {dtc.h +1
-0@@ -182,6 +182,7 @@ struct data data_append_addr(struct data d, uint64_t addr);struct data data_append_byte (struct data d,uint8_t byte);struct data data_append_zeroes (struct data d,int len);struct data data_append_align (struct data d,int align); +struct data data_insert_data (struct data d,struct marker *m,struct data old);struct marker *alloc_marker (unsigned int offset,enum markertype type,char *ref);
Signature · 4 lines
David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson
PATCH v3 3/4 dtc: Add /./
In reply to Ayush SinghAllow constructing new values for a property using old property values. Can be used to append, pre-append, duplicate, etc.
In practice, it looks as follows:
/dts-v1/;
/ {
str-prop = "0";
int-prop = <0>;
};/ {
str-prop = /./, "1", /./;
int-prop = /./, <1>, /./;
};dts to source output with -T -T also works as expected:
/dts-v1/;
/ { /* temp/a.dts:3:3-6:3, temp/a.dts:8:3-11:3 */
str-prop = "0", "1", "0"; /* temp/a.dts:4:5-4:20, temp/a.dts:9:5-9:30 */ int-prop = <0x00>, <0x01>, <0x00>; /* temp/a.dts:5:5-5:20, temp/a.dts:10:5-10:30 */ }; /* temp/a.dts:3:3-6:3, temp/a.dts:8:3-11:3 */
--- dtc-lexer.l | 5 +++++ dtc-parser.y | 5 +++++ dtc.h | 1 + livetree.c | 23 +++++++++++++++++++++-- 4 files changed, 32 insertions(+), 2 deletions(-)
dtc-lexer.l +5 -0
@@ -144,6 +144,11 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...); return DT_OMIT_NO_REF; } +<*>"/./" { + DPRINT("Keyword: /./\n"); + return DT_PREV_VALUE; + } + <*>{LABEL}: { DPRINT("Label: %s\n", yytext); yylval.labelref = xstrdup(yytext);
dtc-parser.y +5 -0
@@ -57,6 +57,7 @@ static bool is_ref_relative(const char *ref) %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR %token DT_BITS %token DT_DEL_PROP +%token DT_PREV_VALUE %token DT_DEL_NODE %token DT_OMIT_NO_REF %token <propnodename> DT_PROPNODENAME
@@ -308,6 +309,10 @@ propdata: { $$ = data_merge($1, $2); } + | propdataprefix DT_PREV_VALUE + { + $$ = data_add_marker($1, PREV_VALUE, NULL); + } | propdataprefix arrayprefix '>' { $$ = data_merge($1, $2.data);
dtc.h +1 -0
@@ -110,6 +110,7 @@ enum markertype {REF_PHANDLE ,REF_PATH ,LABEL , +PREV_VALUE ,TYPE_UINT8 ,TYPE_UINT16 , TYPE_UINT32,
livetree.c +21 -2
@@ -143,6 +143,7 @@ struct node *merge_nodes(struct node *old_node, struct node *new_node) {struct property *new_prop, *old_prop;struct node *new_child, *old_child; +struct marker *marker;struct label *l; old_node->deleted =0 ;
@@ -154,6 +155,8 @@ struct node *merge_nodes(struct node *old_node, struct node *new_node)/* Move properties from the new node to the old node. If there * is a collision, replace the old value with the new */ while (new_node->proplist) { +bool prev_value_used = false; +/* Pop the property off the list */ new_prop = new_node->proplist; new_node->proplist = new_prop->next;
@@ -172,10 +175,26 @@ struct node *merge_nodes(struct node *old_node, struct node *new_node)for_each_label_withdel (new_prop->labels, l)add_label (&old_prop->labels, l->label); + marker = new_prop->val.markers; +for_each_marker_of_type (marker,PREV_VALUE ) { + new_prop->val =data_insert_data ( + new_prop->val, marker, + old_prop->val); + prev_value_used = true; + } + old_prop->val = new_prop->val; old_prop->deleted =0 ; -srcpos_free (old_prop->srcpos); - old_prop->srcpos = new_prop->srcpos; + +if (prev_value_used) { + old_prop->srcpos = +srcpos_extend (old_prop->srcpos, + new_prop->srcpos); + }else { +srcpos_free (old_prop->srcpos); + old_prop->srcpos = new_prop->srcpos; + } +free (new_prop); new_prop =NULL ;break ;
Signature · 1 line
2.49.0
PATCH v3 4/4 tests: Add test for /./
In reply to Ayush Singh- Test /./ on a string and int array.
- Also test on subnode property.
--- tests/prev_prop.dts | 25 +++++++++++++++++++++++++ tests/prev_prop_expected.dts | 13 +++++++++++++ tests/run_tests.sh | 5 +++++ 3 files changed, 43 insertions(+)
tests/prev_prop.dts new file +25 -0
@@ -0,0 +1,25 @@ +/dts-v1/; + +/ { + str-prop = "1", "2"; + int-prop = <1 2>; + + mixed-prop = "1", <0>; + + subnode { + str-prop = "1", "2"; + int-prop = <1 2>; + }; +}; + +/ { + str-prop = /./, "3", "4", /./, "6"; + int-prop = /./, <3 4>, /./, <6>; + + mixed-prop = <1>, /./, "0"; + + subnode { + str-prop = /./, "3", "4", /./; + int-prop = /./, <3 4>, /./; + }; +};
tests/prev_prop_expected.dts new file +13 -0
@@ -0,0 +1,13 @@ +/dts-v1/; + +/ { + str-prop = "1", "2", "3", "4", "1", "2", "6"; + int-prop = <1 2 3 4 1 2 6>; + + mixed-prop = <1>, "1", <0>, "0"; + + subnode { + str-prop = "1", "2", "3", "4", "1", "2"; + int-prop = <1 2 3 4 1 2>; + }; +};
tests/run_tests.sh +5 -0
@@ -708,6 +708,11 @@ dtc_tests () { run_dtc_test -I dts -O dtb -o dtc_tree1_delete.test.dtb "$SRCDIR/test_tree1_delete.dts" tree1_tests dtc_tree1_delete.test.dtb + # Check previous property functionality + run_dtc_test -I dts -O dtb -o prev_prop.test.dtb "$SRCDIR/prev_prop.dts" + run_dtc_test -I dts -O dtb -o prev_prop_expected.test.dtb "$SRCDIR/prev_prop_expected.dts" + run_test dtbs_equal_ordered prev_prop.test.dtb prev_prop_expected.test.dtb + # Check omit-if-no-ref functionality run_dtc_test -I dts -O dtb -o omit-no-ref.test.dtb "$SRCDIR/omit-no-ref.dts" run_test check_path omit-no-ref.test.dtb not-exists "/node1"
Signature · 1 line
2.49.0