of.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/blkdev.h>
  3. #include <linux/major.h>
  4. #include <linux/of.h>
  5. #include <linux/string.h>
  6. #include "check.h"
  7. static int validate_of_partition(struct device_node *np, int slot)
  8. {
  9. u64 offset, size;
  10. int len;
  11. const __be32 *reg = of_get_property(np, "reg", &len);
  12. int a_cells = of_n_addr_cells(np);
  13. int s_cells = of_n_size_cells(np);
  14. /* Make sure reg len match the expected addr and size cells */
  15. if (len / sizeof(*reg) != a_cells + s_cells)
  16. return -EINVAL;
  17. /* Validate offset conversion from bytes to sectors */
  18. offset = of_read_number(reg, a_cells);
  19. if (offset % SECTOR_SIZE)
  20. return -EINVAL;
  21. /* Validate size conversion from bytes to sectors */
  22. size = of_read_number(reg + a_cells, s_cells);
  23. if (!size || size % SECTOR_SIZE)
  24. return -EINVAL;
  25. return 0;
  26. }
  27. static void add_of_partition(struct parsed_partitions *state, int slot,
  28. struct device_node *np)
  29. {
  30. struct partition_meta_info *info;
  31. char tmp[sizeof(info->volname) + 4];
  32. const char *partname;
  33. int len;
  34. const __be32 *reg = of_get_property(np, "reg", &len);
  35. int a_cells = of_n_addr_cells(np);
  36. int s_cells = of_n_size_cells(np);
  37. /* Convert bytes to sector size */
  38. u64 offset = of_read_number(reg, a_cells) / SECTOR_SIZE;
  39. u64 size = of_read_number(reg + a_cells, s_cells) / SECTOR_SIZE;
  40. put_partition(state, slot, offset, size);
  41. if (of_property_read_bool(np, "read-only"))
  42. state->parts[slot].flags |= ADDPART_FLAG_READONLY;
  43. /*
  44. * Follow MTD label logic, search for label property,
  45. * fallback to node name if not found.
  46. */
  47. info = &state->parts[slot].info;
  48. partname = of_get_property(np, "label", &len);
  49. if (!partname)
  50. partname = of_get_property(np, "name", &len);
  51. strscpy(info->volname, partname, sizeof(info->volname));
  52. snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
  53. strlcat(state->pp_buf, tmp, PAGE_SIZE);
  54. }
  55. int of_partition(struct parsed_partitions *state)
  56. {
  57. struct device *ddev = disk_to_dev(state->disk);
  58. struct device_node *np;
  59. int slot;
  60. struct device_node *partitions_np = of_node_get(ddev->of_node);
  61. if (!partitions_np ||
  62. !of_device_is_compatible(partitions_np, "fixed-partitions"))
  63. return 0;
  64. slot = 1;
  65. /* Validate parition offset and size */
  66. for_each_child_of_node(partitions_np, np) {
  67. if (validate_of_partition(np, slot)) {
  68. of_node_put(np);
  69. of_node_put(partitions_np);
  70. return -1;
  71. }
  72. slot++;
  73. }
  74. slot = 1;
  75. for_each_child_of_node(partitions_np, np) {
  76. if (slot >= state->limit) {
  77. of_node_put(np);
  78. break;
  79. }
  80. add_of_partition(state, slot, np);
  81. slot++;
  82. }
  83. strlcat(state->pp_buf, "\n", PAGE_SIZE);
  84. return 1;
  85. }