Skip to contentSkip to conversation

command: oftree: support load device tree from memory

PATCH In review Pascal Vizeli 4 participants 7 messages Jun 6, 2018 – Jun 7, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 command: oftree: support load device tree from memory

DON'T MERGE: this is only a concept patch.

On some device, barebox is the second bootloader. The first bootloader provide some times allready a better device tree as the linux kernel own. Mostly barebox can't boot from this device tree but the linux kernel can.

This patch allow to load a device tree from memory from first bootloader.

For implementation:

Not sure what is the best. Maybe we can create a of_unflatten_file_dtb() and of_unflatten_memory_dtb() they return fdt. There are a lot place they load a fdt from a file and repeat the same code.

---
 commands/oftree.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
commands/oftree.c +38 -1
index 8a47c0be5..831b6c280 100644
@@ -40,9 +40,40 @@
 #include <fcntl.h>
 #include <complete.h>
 
+struct fdt_header* read_memory_dtb(loff_t mem_start)
+{
+	struct fdt_header *oftree = NULL;
+	struct fdt_header *memtree;
+	int mode = O_RWSIZE_4 | O_RDONLY;
+	int fd;
+	uint32_t size;
+	void *map;
+
+	fd = open_and_lseek("/dev/mem", mode,mem_start);
+	if (fd < 0)
+		return NULL;
+
+	map = memmap(fd, PROT_READ);
+	if (map == (void *)-1)
+		goto end;
+	memtree = map;
+
+	size = be32_to_cpu(memtree->totalsize);
+	if (!size)
+		goto end;
+
+	oftree = malloc(size);
+	memcpy((void *)oftree, map, size);
+
+end:
+	close(fd);
+	return oftree;
+}
+
 static int do_oftree(int argc, char *argv[])
 {
 	struct fdt_header *fdt = NULL;
+	loff_t mem_start;
 	size_t size;
 	int opt;
 	int probe = 0;
@@ -102,7 +133,13 @@ static int do_oftree(int argc, char *argv[])
 	}
 
 	if (load) {
-		fdt = read_file(load, &size);
+		if (file_name_detect_type(load) == filetype_oftree) {
+			fdt = read_file(load, &size);
+		} else {
+			mem_start = simple_strtoull(load, NULL, 0);
+			fdt = read_memory_dtb(mem_start);
+		}
+
 		if (!fdt) {
 			printf("unable to read %s\n", load);
 			return 1;
Signature · 7 lines
2.17.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw
Quoted 6 lines · Pascal Vizeli

DON'T MERGE: this is only a concept patch.

On some device, barebox is the second bootloader. The first bootloader provide some times allready a better device tree as the linux kernel own. Mostly barebox can't boot from this device tree but the linux kernel can.

Quoted 8 lines

This patch allow to load a device tree from memory from first bootloader.

For implementation:

Not sure what is the best. Maybe we can create a of_unflatten_file_dtb() and of_unflatten_memory_dtb() they return fdt. There are a lot place they load a fdt from a file and repeat the same code.

IMHO, an ideal solution to this problem would be to either extend RAMFS or create a separate FS that would allow mounting an arbitrary blob in RAM as a file with size determined using file_name_detect_type() + custom code or specified explicitly.

That's quite a bit more work that this patch, though.

Thanks,
Andrey Smirnov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#2 lore raw
In reply to Pascal Vizeli
Quoted 7 lines · Pascal Vizeli

DON'T MERGE: this is only a concept patch.

On some device, barebox is the second bootloader. The first bootloader provide some times allready a better device tree as the linux kernel own. Mostly barebox can't boot from this device tree but the linux kernel can.

This patch allow to load a device tree from memory from first bootloader.

Note while it may be handy to have a command for that, you can already do this without writing code:

memcpy -d /mydtb <dtbaddress> 0 0x10000
oftree -l /mydtb

The only drawback is that you have to guess the size of the dtb.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw

Thanks for this workaround. I will use this.

I will later send a patch they store the device tree address on raspberry into a variable.
So that user can load this, if he need it.

The question is now, would you allow to load a device tree with a memory address on oftree utility?
And if yes, where is the best place to add this function.

greets
Pascal

2018-06-07 9:20 GMT+02:00 Sascha Hauer <[email protected]>:

Quoted 17 lines · Pascal Vizeli
Quoted 7 lines

DON'T MERGE: this is only a concept patch.

On some device, barebox is the second bootloader. The first bootloader provide some times allready a better device tree as the linux kernel own. Mostly barebox can't boot from this device tree but the linux kernel can.

This patch allow to load a device tree from memory from first bootloader.

Note while it may be handy to have a command for that, you can already do this without writing code:

memcpy -d /mydtb <dtbaddress> 0 0x10000
oftree -l /mydtb

The only drawback is that you have to guess the size of the dtb.

Quoted 7 lines

Sascha

--

Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#4 lore raw
Quoted 9 lines · Pascal Vizeli

Thanks for this workaround. I will use this.

I will later send a patch they store the device tree address on raspberry into a variable.
So that user can load this, if he need it.

The question is now, would you allow to load a device tree with a memory address on oftree utility?
And if yes, where is the best place to add this function.

Instead of a command I would prefer if you could just store r2 from the original bootloader you have been started with, and in a board specific initcall you copy the devicetree you found in memory to a file. Then the user has access to the devicetree with regular file commands and there is no need to guess the memory address where the original bootloader stored the device tree.

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw

That sounds good. Thanks for the direction.

My workaround look like:
memcpy -d /tmp/rpi.dtb 0xf42400 0 0x10000
of_dump -f /tmp/rpi.dtb > /dev/null
oftree -f -l /tmp/rpi.dtb

Without 'of_dump', the 'oftree' give a bad magic 0x5.... error.

I need add support for HassOS to raspberry and in a second round I will try to implement your suggestion.

best regards
Pascal

2018-06-07 10:53 GMT+02:00 Sascha Hauer <[email protected]>:

Quoted 18 lines · Pascal Vizeli
Quoted 9 lines

Thanks for this workaround. I will use this.

I will later send a patch they store the device tree address on raspberry into a variable.
So that user can load this, if he need it.

The question is now, would you allow to load a device tree with a memory address on oftree utility?
And if yes, where is the best place to add this function.

Instead of a command I would prefer if you could just store r2 from the original bootloader you have been started with, and in a board specific initcall you copy the devicetree you found in memory to a file. Then the user has access to the devicetree with regular file commands and there is no need to guess the memory address where the original bootloader stored the device tree.

Quoted 7 lines

Sascha

--

Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#6 lore raw

Okay. I found out the problem with bad magic and they is fix now. I was needed a higher position into memory.

Anyway I found also a bug, that the fixup for partition hungs in a loop:
Dump on internal
flatten
unflatten
Run fix
Run fixup
Fixup memory
End fixup
Run fixup
fixup boot
End fixup
Run fixup
Fixup partition

I will find the bug and provide a bugfix. After that all should work :)

Greets
Pascal

2018-06-07 12:42 GMT+02:00 Pascal Vizeli <[email protected]>:

Quoted 9 lines

That sounds good. Thanks for the direction.

My workaround look like:
memcpy -d /tmp/rpi.dtb 0xf42400 0 0x10000
of_dump -f /tmp/rpi.dtb > /dev/null
oftree -f -l /tmp/rpi.dtb

Without 'of_dump', the 'oftree' give a bad magic 0x5.... error.

Quoted 32 lines

I need add support for HassOS to raspberry and in a second round I will try to implement your suggestion.

best regards
Pascal

2018-06-07 10:53 GMT+02:00 Sascha Hauer <[email protected]>:

Quoted 25 lines · Pascal Vizeli
Quoted 9 lines

Thanks for this workaround. I will use this.

I will later send a patch they store the device tree address on raspberry into a variable.
So that user can load this, if he need it.

The question is now, would you allow to load a device tree with a memory address on oftree utility?
And if yes, where is the best place to add this function.

Instead of a command I would prefer if you could just store r2 from the original bootloader you have been started with, and in a board specific initcall you copy the devicetree you found in memory to a file. Then the user has access to the devicetree with regular file commands and there is no need to guess the memory address where the original bootloader stored the device tree.

Sascha

--

Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox