/* * Functions to help device tree manipulation using libfdt. * It also provides functions to read entries from device tree proc * interface. * * Copyright 2008 IBM Corporation. * Authors: Jerone Young * Hollis Blanchard * * This work is licensed under the GNU GPL license version 2 or later. * */ #include "qemu/osdep.h" #ifdef CONFIG_LINUX #include #endif #include "qapi/error.h" #include "qemu-common.h" #include "qemu/error-report.h" #include "sysemu/device_tree.h" #include "sysemu/sysemu.h" #include "hw/loader.h" #include "hw/boards.h" #include "qemu/config-file.h" #include #define FDT_MAX_SIZE 0x10000 void *create_device_tree(int *sizep) { void *fdt; int ret; *sizep = FDT_MAX_SIZE; fdt = g_malloc0(FDT_MAX_SIZE); ret = fdt_create(fdt, FDT_MAX_SIZE); if (ret < 0) { goto fail; } ret = fdt_finish_reservemap(fdt); if (ret < 0) { goto fail; } ret = fdt_begin_node(fdt, ""); if (ret < 0) { goto fail; } ret = fdt_end_node(fdt); if (ret < 0) { goto fail; } ret = fdt_finish(fdt); if (ret < 0) { goto fail; } ret = fdt_open_into(fdt, fdt, *sizep); if (ret) { error_report("Unable to copy device tree in memory"); exit(1); } return fdt; fail: error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret)); exit(1); } void *load_device_tree(const char *filename_path, int *sizep) { int dt_size; int dt_file_load_size; int ret; void *fdt = NULL; *sizep = 0; dt_size = get_image_size(filename_path); if (dt_size < 0) { error_report("Unable to get size of device tree file '%s'", filename_path); goto fail; } /* Expand to 2x size to give enough room for manipulation. */ dt_size += 10000; dt_size *= 2; /* First allocate space in qemu for device tree */ fdt = g_malloc0(dt_size); dt_file_load_size = load_image(filename_path, fdt); if (dt_file_load_size < 0) { error_report("Unable to open device tree file '%s'", filename_path); goto fail; } ret = fdt_open_into(fdt, fdt, dt_size); if (ret) { error_report("Unable to copy device tree in memory"); goto fail; } /* Check sanity of device tree */ if (fdt_check_header(fdt)) { error_report("Device tree file loaded into memory is invalid: %s", filename_path); goto fail; } *sizep = dt_size; return fdt; fail: g_free(fdt); return NULL; } #ifdef CONFIG_LINUX #define SYSFS_DT_BASEDIR "/proc/device-tree" /** * read_fstree: this function is inspired from dtc read_fstree * @fdt: preallocated fdt blob buffer, to be populated * @dirname: directory to scan under SYSFS_DT_BASEDIR * the search is recursive and the tree is searched down to the * leaves (property files). * * the function asserts in case of error */ static void read_fstree(void *fdt, const char *dirname) { DIR *d; struct dirent *de; struct stat st; const char *root_dir = SYSFS_DT_BASEDIR; const char *parent_node; if (strstr(dirname, root_dir) != dirname) { error_setg(&error_fatal, "%s: %s must be searched within %s", __func__, dirname, root_dir); } parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)]; d = opendir(dirname); if (!d) { error_setg(&error_fatal, "%s cannot open %s", __func__, dirname); } while ((de = readdir(d)) != NULL) { char *tmpnam; if (!g_strcmp0(de->d_name, ".") || !g_strcmp0(de->d_name, "..")) { continue; } tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name); if (lstat(tmpnam, &st) < 0) { error_setg(&error_fatal, "%s cannot lstat %s", __func__, tmpnam); } if (S_ISREG(st.st_mode)) { gchar *val; gsize len; if (!g_file_get_contents(tmpnam, &val, &len, NULL)) { error_setg(&error_fatal, "%s not able to extract info from %s", __func__, tmpnam); } if (strlen(parent_node) > 0) { qemu_fdt_setprop(fdt, parent_node, de->d_name, val, len); } else { qemu_fdt_setprop(fdt, "/", de->d_name, val, len); } g_free(val); } else if (S_ISDIR(st.st_mode)) { char *node_name; node_name = g_strdup_printf("%s/%s",