fdt.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Functions for working with the Flattened Device Tree data format
  3. *
  4. * Copyright 2009 Benjamin Herrenschmidt, IBM Corp
  5. * benh@kernel.crashing.org
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. */
  11. #include <linux/of.h>
  12. #include <linux/of_fdt.h>
  13. struct boot_param_header *initial_boot_params;
  14. char *find_flat_dt_string(u32 offset)
  15. {
  16. return ((char *)initial_boot_params) +
  17. initial_boot_params->off_dt_strings + offset;
  18. }
  19. /**
  20. * of_scan_flat_dt - scan flattened tree blob and call callback on each.
  21. * @it: callback function
  22. * @data: context data pointer
  23. *
  24. * This function is used to scan the flattened device-tree, it is
  25. * used to extract the memory information at boot before we can
  26. * unflatten the tree
  27. */
  28. int __init of_scan_flat_dt(int (*it)(unsigned long node,
  29. const char *uname, int depth,
  30. void *data),
  31. void *data)
  32. {
  33. unsigned long p = ((unsigned long)initial_boot_params) +
  34. initial_boot_params->off_dt_struct;
  35. int rc = 0;
  36. int depth = -1;
  37. do {
  38. u32 tag = *((u32 *)p);
  39. char *pathp;
  40. p += 4;
  41. if (tag == OF_DT_END_NODE) {
  42. depth--;
  43. continue;
  44. }
  45. if (tag == OF_DT_NOP)
  46. continue;
  47. if (tag == OF_DT_END)
  48. break;
  49. if (tag == OF_DT_PROP) {
  50. u32 sz = *((u32 *)p);
  51. p += 8;
  52. if (initial_boot_params->version < 0x10)
  53. p = _ALIGN(p, sz >= 8 ? 8 : 4);
  54. p += sz;
  55. p = _ALIGN(p, 4);
  56. continue;
  57. }
  58. if (tag != OF_DT_BEGIN_NODE) {
  59. pr_err("Invalid tag %x in flat device tree!\n", tag);
  60. return -EINVAL;
  61. }
  62. depth++;
  63. pathp = (char *)p;
  64. p = _ALIGN(p + strlen(pathp) + 1, 4);
  65. if ((*pathp) == '/') {
  66. char *lp, *np;
  67. for (lp = NULL, np = pathp; *np; np++)
  68. if ((*np) == '/')
  69. lp = np+1;
  70. if (lp != NULL)
  71. pathp = lp;
  72. }
  73. rc = it(p, pathp, depth, data);
  74. if (rc != 0)
  75. break;
  76. } while (1);
  77. return rc;
  78. }