mirror of
https://github.com/dgibson/dtc.git
synced 2025-12-08 12:45:29 -05:00
At present, the blob containing a device tree is passed to the various fdt_*() functions as a (struct fdt_header *) i.e. a pointer to the header structure at the beginning of the blob. This patch changes all the functions so that they instead take a (void *) pointing to the blob. Under some circumstances can avoid the need for the caller to cast a blob pointer into a (struct fdt_header *) before passing it to the fdt_*() functions. Using a (void *) also reduce the temptation for users of the library to directly dereference toe (struct fdt_header *) to access header fields. Instead they must use the fdt_get_header() or fdt_set_header() macros, or the fdt_magic(), fdt_totalsize() etc. wrappers around them which are safer, since they will always handle endian conversion. With this change, the whole-tree moving, or manipulating functions: fdt_move(), fdt_open_into() and fdt_pack() no longer need to return a pointer to the "new" tree. The given (void *) buffer pointer they take can instead be used directly by the caller as the new tree. Those functions are thus changed to instead return an error code (which in turn reduces the number of functions using the ugly encoding of error values into pointers). Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
48 lines
814 B
C
48 lines
814 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
|
|
#include <fdt.h>
|
|
#include <libfdt.h>
|
|
#include <libfdt_env.h>
|
|
|
|
#include "testdata.h"
|
|
|
|
struct {
|
|
void *blob;
|
|
const char *filename;
|
|
} trees[] = {
|
|
#define TREE(name) { &_##name, #name ".dtb" }
|
|
TREE(test_tree1),
|
|
};
|
|
|
|
#define NUM_TREES (sizeof(trees) / sizeof(trees[0]))
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < NUM_TREES; i++) {
|
|
void *blob = trees[i].blob;
|
|
const char *filename = trees[i].filename;
|
|
int size;
|
|
int fd;
|
|
int ret;
|
|
|
|
size = fdt_totalsize(blob);
|
|
|
|
printf("Tree \"%s\", %d bytes\n", filename, size);
|
|
|
|
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
if (fd < 0)
|
|
perror("open()");
|
|
|
|
ret = write(fd, blob, size);
|
|
if (ret != size)
|
|
perror("write()");
|
|
|
|
close(fd);
|
|
}
|
|
exit(0);
|
|
}
|