qemu/softmmu/device_tree.c
<<
>>
Prefs
   1/*
   2 * Functions to help device tree manipulation using libfdt.
   3 * It also provides functions to read entries from device tree proc
   4 * interface.
   5 *
   6 * Copyright 2008 IBM Corporation.
   7 * Authors: Jerone Young <jyoung5@us.ibm.com>
   8 *          Hollis Blanchard <hollisb@us.ibm.com>
   9 *
  10 * This work is licensed under the GNU GPL license version 2 or later.
  11 *
  12 */
  13
  14#include "qemu/osdep.h"
  15
  16#ifdef CONFIG_LINUX
  17#include <dirent.h>
  18#endif
  19
  20#include "qapi/error.h"
  21#include "qemu/error-report.h"
  22#include "qemu/option.h"
  23#include "qemu/bswap.h"
  24#include "qemu/cutils.h"
  25#include "sysemu/device_tree.h"
  26#include "sysemu/sysemu.h"
  27#include "hw/loader.h"
  28#include "hw/boards.h"
  29#include "qemu/config-file.h"
  30
  31#include <libfdt.h>
  32
  33#define FDT_MAX_SIZE  0x100000
  34
  35void *create_device_tree(int *sizep)
  36{
  37    void *fdt;
  38    int ret;
  39
  40    *sizep = FDT_MAX_SIZE;
  41    fdt = g_malloc0(FDT_MAX_SIZE);
  42    ret = fdt_create(fdt, FDT_MAX_SIZE);
  43    if (ret < 0) {
  44        goto fail;
  45    }
  46    ret = fdt_finish_reservemap(fdt);
  47    if (ret < 0) {
  48        goto fail;
  49    }
  50    ret = fdt_begin_node(fdt, "");
  51    if (ret < 0) {
  52        goto fail;
  53    }
  54    ret = fdt_end_node(fdt);
  55    if (ret < 0) {
  56        goto fail;
  57    }
  58    ret = fdt_finish(fdt);
  59    if (ret < 0) {
  60        goto fail;
  61    }
  62    ret = fdt_open_into(fdt, fdt, *sizep);
  63    if (ret) {
  64        error_report("Unable to copy device tree in memory");
  65        exit(1);
  66    }
  67
  68    return fdt;
  69fail:
  70    error_report("%s Couldn't create dt: %s", __func__, fdt_strerror(ret));
  71    exit(1);
  72}
  73
  74void *load_device_tree(const char *filename_path, int *sizep)
  75{
  76    int dt_size;
  77    int dt_file_load_size;
  78    int ret;
  79    void *fdt = NULL;
  80
  81    *sizep = 0;
  82    dt_size = get_image_size(filename_path);
  83    if (dt_size < 0) {
  84        error_report("Unable to get size of device tree file '%s'",
  85                     filename_path);
  86        goto fail;
  87    }
  88    if (dt_size > INT_MAX / 2 - 10000) {
  89        error_report("Device tree file '%s' is too large", filename_path);
  90        goto fail;
  91    }
  92
  93    /* Expand to 2x size to give enough room for manipulation.  */
  94    dt_size += 10000;
  95    dt_size *= 2;
  96    /* First allocate space in qemu for device tree */
  97    fdt = g_malloc0(dt_size);
  98
  99    dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
 100    if (dt_file_load_size < 0) {
 101        error_report("Unable to open device tree file '%s'",
 102                     filename_path);
 103        goto fail;
 104    }
 105
 106    ret = fdt_open_into(fdt, fdt, dt_size);
 107    if (ret) {
 108        error_report("Unable to copy device tree in memory");
 109        goto fail;
 110    }
 111
 112    /* Check sanity of device tree */
 113    if (fdt_check_header(fdt)) {
 114        error_report("Device tree file loaded into memory is invalid: %s",
 115                     filename_path);
 116        goto fail;
 117    }
 118    *sizep = dt_size;
 119    return fdt;
 120
 121fail:
 122    g_free(fdt);
 123    return NULL;
 124}
 125
 126#ifdef CONFIG_LINUX
 127
 128#define SYSFS_DT_BASEDIR "/proc/device-tree"
 129
 130/**
 131 * read_fstree: this function is inspired from dtc read_fstree
 132 * @fdt: preallocated fdt blob buffer, to be populated
 133 * @dirname: directory to scan under SYSFS_DT_BASEDIR
 134 * the search is recursive and the tree is searched down to the
 135 * leaves (property files).
 136 *
 137 * the function asserts in case of error
 138 */
 139static void read_fstree(void *fdt, const char *dirname)
 140{
 141    DIR *d;
 142    struct dirent *de;
 143    struct stat st;
 144    const char *root_dir = SYSFS_DT_BASEDIR;
 145    const char *parent_node;
 146
 147    if (strstr(dirname, root_dir) != dirname) {
 148        error_report("%s: %s must be searched within %s",
 149                     __func__, dirname, root_dir);
 150        exit(1);
 151    }
 152    parent_node = &dirname[strlen(SYSFS_DT_BASEDIR)];
 153
 154    d = opendir(dirname);
 155    if (!d) {
 156        error_report("%s cannot open %s", __func__, dirname);
 157        exit(1);
 158    }
 159
 160    while ((de = readdir(d)) != NULL) {
 161        char *tmpnam;
 162
 163        if (!g_strcmp0(de->d_name, ".")
 164            || !g_strcmp0(de->d_name, "..")) {
 165            continue;
 166        }
 167
 168        tmpnam = g_strdup_printf("%s/%s", dirname, de->d_name);
 169
 170        if (lstat(tmpnam, &st) < 0) {
 171            error_report("%s cannot lstat %s", __func__, tmpnam);
 172            exit(1);
 173        }
 174
 175        if (S_ISREG(st.st_mode)) {
 176            gchar *val;
 177            gsize len;
 178
 179            if (!g_file_get_contents(tmpnam, &val, &len, NULL)) {
 180                error_report("%s not able to extract info from %s",
 181                             __func__, tmpnam);
 182                exit(1);
 183            }
 184
 185            if (strlen(parent_node) > 0) {
 186                qemu_fdt_setprop(fdt, parent_node,
 187                                 de->d_name, val, len);
 188            } else {
 189                qemu_fdt_setprop(fdt, "/", de->d_name, val, len);
 190            }
 191            g_free(val);
 192        } else if (S_ISDIR(st.st_mode)) {
 193            char *node_name;
 194
 195            node_name = g_strdup_printf("%s/%s",
 196                                        parent_node, de->d_name);
 197            qemu_fdt_add_subnode(fdt, node_name);
 198            g_free(node_name);
 199            read_fstree(fdt, tmpnam);
 200        }
 201
 202        g_free(tmpnam);
 203    }
 204
 205    closedir(d);
 206}
 207
 208/* load_device_tree_from_sysfs: extract the dt blob from host sysfs */
 209void *load_device_tree_from_sysfs(void)
 210{
 211    void *host_fdt;
 212    int host_fdt_size;
 213
 214    host_fdt = create_device_tree(&host_fdt_size);
 215    read_fstree(host_fdt, SYSFS_DT_BASEDIR);
 216    if (fdt_check_header(host_fdt)) {
 217        error_report("%s host device tree extracted into memory is invalid",
 218                     __func__);
 219        exit(1);
 220    }
 221    return host_fdt;
 222}
 223
 224#endif /* CONFIG_LINUX */
 225
 226static int findnode_nofail(void *fdt, const char *node_path)
 227{
 228    int offset;
 229
 230    offset = fdt_path_offset(fdt, node_path);
 231    if (offset < 0) {
 232        error_report("%s Couldn't find node %s: %s", __func__, node_path,
 233                     fdt_strerror(offset));
 234        exit(1);
 235    }
 236
 237    return offset;
 238}
 239
 240char **qemu_fdt_node_unit_path(void *fdt, const char *name, Error **errp)
 241{
 242    char *prefix =  g_strdup_printf("%s@", name);
 243    unsigned int path_len = 16, n = 0;
 244    GSList *path_list = NULL, *iter;
 245    const char *iter_name;
 246    int offset, len, ret;
 247    char **path_array;
 248
 249    offset = fdt_next_node(fdt, -1, NULL);
 250
 251    while (offset >= 0) {
 252        iter_name = fdt_get_name(fdt, offset, &len);
 253        if (!iter_name) {
 254            offset = len;
 255            break;
 256        }
 257        if (!strcmp(iter_name, name) || g_str_has_prefix(iter_name, prefix)) {
 258            char *path;
 259
 260            path = g_malloc(path_len);
 261            while ((ret = fdt_get_path(fdt, offset, path, path_len))
 262                  == -FDT_ERR_NOSPACE) {
 263                path_len += 16;
 264                path = g_realloc(path, path_len);
 265            }
 266            path_list = g_slist_prepend(path_list, path);
 267            n++;
 268        }
 269        offset = fdt_next_node(fdt, offset, NULL);
 270    }
 271    g_free(prefix);
 272
 273    if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
 274        error_setg(errp, "%s: abort parsing dt for %s node units: %s",
 275                   __func__, name, fdt_strerror(offset));
 276        for (iter = path_list; iter; iter = iter->next) {
 277            g_free(iter->data);
 278        }
 279        g_slist_free(path_list);
 280        return NULL;
 281    }
 282
 283    path_array = g_new(char *, n + 1);
 284    path_array[n--] = NULL;
 285
 286    for (iter = path_list; iter; iter = iter->next) {
 287        path_array[n--] = iter->data;
 288    }
 289
 290    g_slist_free(path_list);
 291
 292    return path_array;
 293}
 294
 295char **qemu_fdt_node_path(void *fdt, const char *name, const char *compat,
 296                          Error **errp)
 297{
 298    int offset, len, ret;
 299    const char *iter_name;
 300    unsigned int path_len = 16, n = 0;
 301    GSList *path_list = NULL, *iter;
 302    char **path_array;
 303
 304    offset = fdt_node_offset_by_compatible(fdt, -1, compat);
 305
 306    while (offset >= 0) {
 307        iter_name = fdt_get_name(fdt, offset, &len);
 308        if (!iter_name) {
 309            offset = len;
 310            break;
 311        }
 312        if (!name || !strcmp(iter_name, name)) {
 313            char *path;
 314
 315            path = g_malloc(path_len);
 316            while ((ret = fdt_get_path(fdt, offset, path, path_len))
 317                  == -FDT_ERR_NOSPACE) {
 318                path_len += 16;
 319                path = g_realloc(path, path_len);
 320            }
 321            path_list = g_slist_prepend(path_list, path);
 322            n++;
 323        }
 324        offset = fdt_node_offset_by_compatible(fdt, offset, compat);
 325    }
 326
 327    if (offset < 0 && offset != -FDT_ERR_NOTFOUND) {
 328        error_setg(errp, "%s: abort parsing dt for %s/%s: %s",
 329                   __func__, name, compat, fdt_strerror(offset));
 330        for (iter = path_list; iter; iter = iter->next) {
 331            g_free(iter->data);
 332        }
 333        g_slist_free(path_list);
 334        return NULL;
 335    }
 336
 337    path_array = g_new(char *, n + 1);
 338    path_array[n--] = NULL;
 339
 340    for (iter = path_list; iter; iter = iter->next) {
 341        path_array[n--] = iter->data;
 342    }
 343
 344    g_slist_free(path_list);
 345
 346    return path_array;
 347}
 348
 349int qemu_fdt_setprop(void *fdt, const char *node_path,
 350                     const char *property, const void *val, int size)
 351{
 352    int r;
 353
 354    r = fdt_setprop(fdt, findnode_nofail(fdt, node_path), property, val, size);
 355    if (r < 0) {
 356        error_report("%s: Couldn't set %s/%s: %s", __func__, node_path,
 357                     property, fdt_strerror(r));
 358        exit(1);
 359    }
 360
 361    return r;
 362}
 363
 364int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
 365                          const char *property, uint32_t val)
 366{
 367    int r;
 368
 369    r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
 370    if (r < 0) {
 371        error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
 372                     node_path, property, val, fdt_strerror(r));
 373        exit(1);
 374    }
 375
 376    return r;
 377}
 378
 379int qemu_fdt_setprop_u64(void *fdt, const char *node_path,
 380                         const char *property, uint64_t val)
 381{
 382    val = cpu_to_be64(val);
 383    return qemu_fdt_setprop(fdt, node_path, property, &val, sizeof(val));
 384}
 385
 386int qemu_fdt_setprop_string(void *fdt, const char *node_path,
 387                            const char *property, const char *string)
 388{
 389    int r;
 390
 391    r = fdt_setprop_string(fdt, findnode_nofail(fdt, node_path), property, string);
 392    if (r < 0) {
 393        error_report("%s: Couldn't set %s/%s = %s: %s", __func__,
 394                     node_path, property, string, fdt_strerror(r));
 395        exit(1);
 396    }
 397
 398    return r;
 399}
 400
 401/*
 402 * libfdt doesn't allow us to add string arrays directly but they are
 403 * test a series of null terminated strings with a length. We build
 404 * the string up here so we can calculate the final length.
 405 */
 406int qemu_fdt_setprop_string_array(void *fdt, const char *node_path,
 407                                  const char *prop, char **array, int len)
 408{
 409    int ret, i, total_len = 0;
 410    char *str, *p;
 411    for (i = 0; i < len; i++) {
 412        total_len += strlen(array[i]) + 1;
 413    }
 414    p = str = g_malloc0(total_len);
 415    for (i = 0; i < len; i++) {
 416        int len = strlen(array[i]) + 1;
 417        pstrcpy(p, len, array[i]);
 418        p += len;
 419    }
 420
 421    ret = qemu_fdt_setprop(fdt, node_path, prop, str, total_len);
 422    g_free(str);
 423    return ret;
 424}
 425
 426const void *qemu_fdt_getprop(void *fdt, const char *node_path,
 427                             const char *property, int *lenp, Error **errp)
 428{
 429    int len;
 430    const void *r;
 431
 432    if (!lenp) {
 433        lenp = &len;
 434    }
 435    r = fdt_getprop(fdt, findnode_nofail(fdt, node_path), property, lenp);
 436    if (!r) {
 437        error_setg(errp, "%s: Couldn't get %s/%s: %s", __func__,
 438                  node_path, property, fdt_strerror(*lenp));
 439    }
 440    return r;
 441}
 442
 443uint32_t qemu_fdt_getprop_cell(void *fdt, const char *node_path,
 444                               const char *property, int *lenp, Error **errp)
 445{
 446    int len;
 447    const uint32_t *p;
 448
 449    if (!lenp) {
 450        lenp = &len;
 451    }
 452    p = qemu_fdt_getprop(fdt, node_path, property, lenp, errp);
 453    if (!p) {
 454        return 0;
 455    } else if (*lenp != 4) {
 456        error_setg(errp, "%s: %s/%s not 4 bytes long (not a cell?)",
 457                   __func__, node_path, property);
 458        *lenp = -EINVAL;
 459        return 0;
 460    }
 461    return be32_to_cpu(*p);
 462}
 463
 464uint32_t qemu_fdt_get_phandle(void *fdt, const char *path)
 465{
 466    uint32_t r;
 467
 468    r = fdt_get_phandle(fdt, findnode_nofail(fdt, path));
 469    if (r == 0) {
 470        error_report("%s: Couldn't get phandle for %s: %s", __func__,
 471                     path, fdt_strerror(r));
 472        exit(1);
 473    }
 474
 475    return r;
 476}
 477
 478int qemu_fdt_setprop_phandle(void *fdt, const char *node_path,
 479                             const char *property,
 480                             const char *target_node_path)
 481{
 482    uint32_t phandle = qemu_fdt_get_phandle(fdt, target_node_path);
 483    return qemu_fdt_setprop_cell(fdt, node_path, property, phandle);
 484}
 485
 486uint32_t qemu_fdt_alloc_phandle(void *fdt)
 487{
 488    static int phandle = 0x0;
 489
 490    /*
 491     * We need to find out if the user gave us special instruction at
 492     * which phandle id to start allocating phandles.
 493     */
 494    if (!phandle) {
 495        phandle = machine_phandle_start(current_machine);
 496    }
 497
 498    if (!phandle) {
 499        /*
 500         * None or invalid phandle given on the command line, so fall back to
 501         * default starting point.
 502         */
 503        phandle = 0x8000;
 504    }
 505
 506    return phandle++;
 507}
 508
 509int qemu_fdt_nop_node(void *fdt, const char *node_path)
 510{
 511    int r;
 512
 513    r = fdt_nop_node(fdt, findnode_nofail(fdt, node_path));
 514    if (r < 0) {
 515        error_report("%s: Couldn't nop node %s: %s", __func__, node_path,
 516                     fdt_strerror(r));
 517        exit(1);
 518    }
 519
 520    return r;
 521}
 522
 523int qemu_fdt_add_subnode(void *fdt, const char *name)
 524{
 525    char *dupname = g_strdup(name);
 526    char *basename = strrchr(dupname, '/');
 527    int retval;
 528    int parent = 0;
 529
 530    if (!basename) {
 531        g_free(dupname);
 532        return -1;
 533    }
 534
 535    basename[0] = '\0';
 536    basename++;
 537
 538    if (dupname[0]) {
 539        parent = findnode_nofail(fdt, dupname);
 540    }
 541
 542    retval = fdt_add_subnode(fdt, parent, basename);
 543    if (retval < 0) {
 544        error_report("FDT: Failed to create subnode %s: %s", name,
 545                     fdt_strerror(retval));
 546        exit(1);
 547    }
 548
 549    g_free(dupname);
 550    return retval;
 551}
 552
 553void qemu_fdt_dumpdtb(void *fdt, int size)
 554{
 555    const char *dumpdtb = current_machine->dumpdtb;
 556
 557    if (dumpdtb) {
 558        /* Dump the dtb to a file and quit */
 559        if (g_file_set_contents(dumpdtb, fdt, size, NULL)) {
 560            info_report("dtb dumped to %s. Exiting.", dumpdtb);
 561            exit(0);
 562        }
 563        error_report("%s: Failed dumping dtb to %s", __func__, dumpdtb);
 564        exit(1);
 565    }
 566}
 567
 568int qemu_fdt_setprop_sized_cells_from_array(void *fdt,
 569                                            const char *node_path,
 570                                            const char *property,
 571                                            int numvalues,
 572                                            uint64_t *values)
 573{
 574    uint32_t *propcells;
 575    uint64_t value;
 576    int cellnum, vnum, ncells;
 577    uint32_t hival;
 578    int ret;
 579
 580    propcells = g_new0(uint32_t, numvalues * 2);
 581
 582    cellnum = 0;
 583    for (vnum = 0; vnum < numvalues; vnum++) {
 584        ncells = values[vnum * 2];
 585        if (ncells != 1 && ncells != 2) {
 586            ret = -1;
 587            goto out;
 588        }
 589        value = values[vnum * 2 + 1];
 590        hival = cpu_to_be32(value >> 32);
 591        if (ncells > 1) {
 592            propcells[cellnum++] = hival;
 593        } else if (hival != 0) {
 594            ret = -1;
 595            goto out;
 596        }
 597        propcells[cellnum++] = cpu_to_be32(value);
 598    }
 599
 600    ret = qemu_fdt_setprop(fdt, node_path, property, propcells,
 601                           cellnum * sizeof(uint32_t));
 602out:
 603    g_free(propcells);
 604    return ret;
 605}
 606