Add support for flat device tree format version 17
libfdt defined a new version of the flattened device tree format, version 17. It is backwards compatible with version 16, just adding an extra header field giving the size of the blob's structure blob.
This patch adds support to dtc allowing it to read and write version 17 blobs. It also makes version 17 the default output version for blobs.
dtc.c +1 -1
@@ -113,7 +113,7 @@ int main(int argc, char *argv[])int opt;FILE *inf =NULL ;FILE *outf =NULL ; -int outversion =0x10 ; +int outversion =0x11 ;int reservenum =1 ;int boot_cpuid_phys =0xfeedbeef ;
flat_dt.h +4 -0
@@ -25,11 +25,15 @@ struct boot_param_header {booting on */ /* version 3 fields below */ uint32_t size_dt_strings;/* size of the strings block */ + +/* version 17 fields below */ +uint32_t size_dt_struct;/* size of the DT structure block */ };#define BPH_V1_SIZE (7*sizeof(uint32_t))#define BPH_V2_SIZE (BPH_V1_SIZE + sizeof(uint32_t))#define BPH_V3_SIZE (BPH_V2_SIZE + sizeof(uint32_t)) +#define BPH_V17_SIZE (BPH_V3_SIZE + sizeof(uint32_t))struct reserve_entry {uint64_t address;
flattree.c +13 -1
@@ -26,6 +26,7 @@#define FTF_NAMEPROPS 0x4#define FTF_BOOTCPUID 0x8#define FTF_STRTABSIZE 0x10 +#define FTF_STRUCTSIZE 0x20 static struct version_info { int version;
@@ -41,6 +42,8 @@ static struct version_info { FTF_FULLPATH|FTF_VARALIGN|FTF_NAMEPROPS|FTF_BOOTCPUID|FTF_STRTABSIZE}, {0x10 ,0x10 ,BPH_V3_SIZE , FTF_BOOTCPUID|FTF_STRTABSIZE}, + {0x11, 0x10, BPH_V17_SIZE, + FTF_BOOTCPUID|FTF_STRTABSIZE|FTF_STRUCTSIZE}, }; struct emitter {
@@ -328,6 +331,8 @@ static void make_bph(struct boot_param_h bph->boot_cpuid_phys =cpu_to_be32 (boot_cpuid_phys);if (vi->flags &FTF_STRTABSIZE ) bph->size_dt_strings =cpu_to_be32 (strsize); +if (vi->flags &FTF_STRUCTSIZE ) + bph->size_dt_struct =cpu_to_be32 (dtsize); } void dt_to_blob(FILE *f, struct boot_info *bi, int version,
@@ -742,7 +747,7 @@ static struct node *unflatten_tree(strucstruct boot_info *dt_from_blob (FILE *f) { -u32 magic, totalsize, version, size_str; +u32 magic, totalsize, version, size_str, size_dt;u32 off_dt, off_str, off_mem_rsvmap;int rc;char *blob;
@@ -841,6 +846,13 @@ struct boot_info *dt_from_blob(FILE *f)if (off_str+size_str > totalsize)die ("String table extends past total size\n" ); } + +if (version >=17 ) { + size_dt =be32_to_cpu (bph->size_dt_struct); +fprintf (stderr,"\tsize_dt_struct:\t\t%d\n" , size_dt); +if (off_dt+size_dt > totalsize) +die ("Structure block extends past total size\n" ); + }if (version <0x10 ) { flags |=FTF_FULLPATH |FTF_NAMEPROPS |FTF_VARALIGN ;
Signature · 4 lines
David Gibson | 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
dtc.c int main(int argc, char *argv[]) · line 116
Quoted 10 lines · David Gibson
Index:
dtc/dtc.c
===================================================================dtc.c +1
-1@@ -113,7 +113,7 @@ int main(int argc, char *argv[])int opt;FILE *inf =NULL ;FILE *outf =NULL ; -int outversion =0x10 ; +int outversion =0x11 ;
Super-minor nit but since you seem to refer to the version as decimal '17' everywhere else, why don't you just put '17' here?
Mark
Quoted 14 lines · Mark A. Greer
dtc.c int main(int argc, char *argv[]) · line 116
Quoted 10 lines · David Gibson
Index:
dtc/dtc.c
===================================================================dtc.c +1
-1@@ -113,7 +113,7 @@ int main(int argc, char *argv[])int opt;FILE *inf =NULL ;FILE *outf =NULL ; -int outversion =0x10 ; +int outversion =0x11 ;Super-minor nit but since you seem to refer to the version as decimal '17' everywhere else, why don't you just put '17' here?
Oh, because, the old code used hex. Which it does in a totally inconsistent fashion. Yeah, I probably should take this opportunity to start phasing out the use of hex. Ok, revised patch coming.
Signature · 4 lines
David Gibson | 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
Quoted 3 lines · David Gibson
libfdt defined a new version of the flattened device tree format, version 17. It is backwards compatible with version 16, just adding an extra header field giving the size of the blob's structure blob.
Question:
Since the DTB is compatible with V16, then technically U-Boot can work with it. What would happen if U-Boot added some nodes to the DTB, but it didn't update size_dt_struct? Would the value of size_dt_struct still be correct? If not, then does that mean that U-Boot should reject V17 DTBs?
Signature · 2 lines
Timur Tabi Linux Kernel Developer @ Freescale
Quoted 8 lines · Timur Tabi
Quoted 3 lines · David Gibson
libfdt defined a new version of the flattened device tree format, version 17. It is backwards compatible with version 16, just adding an extra header field giving the size of the blob's structure blob.
Question:
Since the DTB is compatible with V16, then technically U-Boot can work with it. What would happen if U-Boot added some nodes to the DTB, but it didn't update
size_dt_struct? Would the value ofsize_dt_structstill be correct? If not, then does that mean that U-Boot should reject V17 DTBs?
Ahh, now I see your concern and don't know the answer at this point.
The best solution, which I'm making progress on but slowly, is to pull David Gibson's libfdt utilities into u-boot and use them to manipulate the tree. I very much want v17 blobs because that removes my "write-in-place" restrictions on changing the properties.
gvb
Quoted 1 line · Jerry Van Baren
The best solution, which I'm making progress on but slowly, is to pull David Gibson's libfdt utilities into u-boot and use them to manipulate the tree.
I don't think that will help, because the problem is how do you update a device tree that you don't know everything about?
Quoted 2 lines
I very much want v17 blobs because that removes my "write-in-place" restrictions on changing the properties.
Another idea which just came to light is to have the compatibility field be only useful to code that just *reads* the DTB. Any code that *writes* the DTB should look it.
Since the kernel only reads the DTB, it can use the compatibility field. Since U-Boot writes to the DTB, it should currently only accept true V16 DTBs.
If the kernel were to ever write to the DTB, then it would have to be as restrictive.
Signature · 2 lines
Timur Tabi Linux Kernel Developer @ Freescale
Quoted 1 line · Jerry Van Baren
The best solution, which I'm making progress on but slowly, is to pull David Gibson's libfdt utilities into u-boot and use them to manipulate the tree. I very much want v17 blobs because that removes my "write-in-place" restrictions on changing the properties.
Here's an alternative idea I got from a colleage (Scott Wood).
Allow U-Boot to accept V17 DTBs. When it modifies the DTB, it should change the version to 16, since that's the only version that it really knows. U-Boot currently does not have the infrastructure to support multiple DTB versions.
The DTB that U-Boot passes to the kernel will say V16, but it will have the extra length field in the header. Therefore, we need to update the documentation to say that compatibility implies that any DTB will still be able to read the DTB if the DTB version number has been changed to another compatible version. So if your DTB is V17, and it has V17 data, any code that assumes a V16 DTB should still be able to read it. That seems obvious, but is should be documented.
Signature · 2 lines
Timur Tabi Linux Kernel Developer @ Freescale
Quoted 9 lines · Timur Tabi
Quoted 3 lines · Jerry Van Baren
The best solution, which I'm making progress on but slowly, is to pull David Gibson's libfdt utilities into u-boot and use them to manipulate the tree.
I don't think that will help, because the problem is how do you update a device tree that you don't know everything about?
Quoted 6 lines
Quoted 2 lines
I very much want v17 blobs because that removes my "write-in-place" restrictions on changing the properties.
Another idea which just came to light is to have the compatibility field be only useful to code that just *reads* the DTB. Any code that *writes* the DTB should look it.
Yes, that's always been true for the compatibility version field (version 3 had exactly the same problem, for example). Code which writes the dtb has to understand everything it writes. Or to put it another way, the 'version' field records the version of the writer, 'last_comp_version' field records the version of the reader.
But a program which manipulates a tree, like u-boot is both a reader and a writer. It *can* take in a higher compatible version and alter it as long as it downgrades the version on its output to the highest thing it fully understands.
Signature · 4 lines
David Gibson | 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
Quoted 9 lines · Timur Tabi
Quoted 1 line · Jerry Van Baren
The best solution, which I'm making progress on but slowly, is to pull David Gibson's libfdt utilities into u-boot and use them to manipulate the tree. I very much want v17 blobs because that removes my "write-in-place" restrictions on changing the properties.
Here's an alternative idea I got from a colleage (Scott Wood).
Allow U-Boot to accept V17 DTBs. When it modifies the DTB, it should change the version to 16, since that's the only version that it really knows. U-Boot currently does not have the infrastructure to support multiple DTB versions.
The DTB that U-Boot passes to the kernel will say V16, but it will have the extra length field in the header. Therefore, we need to update the documentation to say that compatibility implies that any DTB will still be able to read the DTB if the DTB version number has been changed to another compatible version. So if your DTB is V17, and it has V17 data, any code that assumes a V16 DTB should still be able to read it. That seems obvious, but is should be documented.
Actually, I believe that is how the existing code works: it opens the original blob and copies it to a newly created dft. It then augments the new dft with a "chosen" node and optionally additional nodes (bd_t, env variables) and passes the new dft blob to linux. Theoretically, the existing code will read a v17 blob and create a new v16 blob from it (without the additional v17 length field - the header would be created, not copied).
Trivia: the current code is kinda dumb about it: if the original blob had a "chosen" node, the code *adds another* "chosen" node.
Back to libdft: with a v16 blob, libdft has to either modify in place (no size change) or you have to create a new blob and copy the original data over (like the existing code). With the v17 blob, libdft is able to expand and contract the contents of the blob (with limitations, I'm sure).
Best regards,
gvb
Quoted 1 line · Jerry Van Baren
Actually, I believe that is how the existing code works: it opens the original blob and copies it to a newly created dft. It then augments the new dft with a "chosen" node and optionally additional nodes (
bd_t, env variables) and passes the new dft blob to linux. Theoretically, the existing code will read a v17 blob and create a new v16 blob from it (without the additional v17 length field - the header would be created, not copied).
I'll check out the code and see what it does, since I'm not really familiar with it.
Quoted 2 lines
Trivia: the current code is kinda dumb about it: if the original blob had a "chosen" node, the code *adds another* "chosen" node.
Yes, I have this bug on my to-do list to fix.
Quoted 1 line
Back to libdft: with a v16 blob, libdft has to either modify in place (no size change) or you have to create a new blob and copy the original data over (like the existing code). With the v17 blob, libdft is able to expand and contract the contents of the blob (with limitations, I'm sure).
I agree that it would be nice if U-Boot had a robust DTB parser, but I don't have the time to do that work.
Quoted 29 lines · Jerry Van Baren
Quoted 13 lines · Timur Tabi
Quoted 4 lines · Jerry Van Baren
The best solution, which I'm making progress on but slowly, is to pull David Gibson's libfdt utilities into u-boot and use them to manipulate the tree. I very much want v17 blobs because that removes my "write-in-place" restrictions on changing the properties.
Here's an alternative idea I got from a colleage (Scott Wood).
Allow U-Boot to accept V17 DTBs. When it modifies the DTB, it should change the version to 16, since that's the only version that it really knows. U-Boot currently does not have the infrastructure to support multiple DTB versions.
Quoted 6 lines
The DTB that U-Boot passes to the kernel will say V16, but it will have the extra length field in the header. Therefore, we need to update the documentation to say that compatibility implies that any DTB will still be able to read the DTB if the DTB version number has been changed to another compatible version. So if your DTB is V17, and it has V17 data, any code that assumes a V16 DTB should still be able to read it. That seems obvious, but is should be documented.
Actually, I believe that is how the existing code works: it opens the original blob and copies it to a newly created dft. It then augments the new dft with a "chosen" node and optionally additional nodes (
bd_t, env variables) and passes the new dft blob to linux. Theoretically, the existing code will read a v17 blob and create a new v16 blob from it (without the additional v17 length field - the header would be created, not copied).
Quoted 8 lines
Trivia: the current code is kinda dumb about it: if the original blob had a "chosen" node, the code *adds another* "chosen" node.
Back to libdft: with a v16 blob, libdft has to either modify in place (no size change) or you have to create a new blob and copy the original data over (like the existing code). With the v17 blob, libdft is able to expand and contract the contents of the blob (with limitations, I'm sure).
In fact, with a v16 blob, libfdt is suppose to allow expansion and contraction by first converting it to a v17 blob. I just haven't implemented that yet.
Signature · 4 lines
David Gibson | 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
Quoted 10 lines · Timur Tabi
Quoted 1 line · Jerry Van Baren
Actually, I believe that is how the existing code works: it opens the original blob and copies it to a newly created dft. It then augments the new dft with a "chosen" node and optionally additional nodes (
bd_t, env variables) and passes the new dft blob to linux. Theoretically, the existing code will read a v17 blob and create a new v16 blob from it (without the additional v17 length field - the header would be created, not copied).I'll check out the code and see what it does, since I'm not really familiar with it.
Quoted 2 lines
Trivia: the current code is kinda dumb about it: if the original blob had a "chosen" node, the code *adds another* "chosen" node.
Yes, I have this bug on my to-do list to fix.
That is close to the top of my list. My intent with the "fdt" command is to have a subcommand that creates the "chosen" node. This will replace the auto-generation (with no opportunity to review or revise) the "chosen" node as part of the "bootm" command.
Downside: "bootm" becomes more than one command. So script it.
Upside: MUCH cleaner, allows the user to review and revise the blob before booting linux.
These are my current thoughts for the "fdt" command:
=> help fdt
fdt addr <addr> - Set the fdt location to <addr> fdt get <node> <property> - Get the value of <node> <property> fdt set <node> <property> <val> - Set the value of <property> to <val>
(creates a new property if necessary)
fdt mknode <node> - Create a new node <node>
(the path must exist except for the last node name)
fdt rmnode <node> - Deletes the node <node>
(the last node name in the path)
fdt print <node> [<property>] - Recursive print starting at <node>
(if <property> is specified, prints only that property)
fdt chosen - Add/update the "chosen" branch to the tree Hints:
* "fdt print /" prints the whole tree.
* If the property you are setting/printing has a '#' character, you MUST escape it with a \ character or quote it with " or it will be ignored as a comment.
* If the value has spaces in it, you MUST escape the spaces with \ characters or quote it with "
Examples: fdt p /cpus "#address-cells"
fdt s /cpus "#address-cells" "[00 00 00 01]"
I've actually implemented addr, set, and print (get is an alias for print).
Quoted 3 lines
Quoted 1 line
Back to libdft: with a v16 blob, libdft has to either modify in place (no size change) or you have to create a new blob and copy the original data over (like the existing code). With the v17 blob, libdft is able to expand and contract the contents of the blob (with limitations, I'm sure).
I agree that it would be nice if U-Boot had a robust DTB parser, but I don't have the time to do that work.
libfdt.
David already did the work, I'm working on the integration (works but needs some cleanup).
gvb