parser_trx.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Parser for TRX format partitions
  4. *
  5. * Copyright (C) 2012 - 2017 Rafał Miłecki <rafal@milecki.pl>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/mtd/mtd.h>
  10. #include <linux/mtd/partitions.h>
  11. #define TRX_PARSER_MAX_PARTS 4
  12. /* Magics */
  13. #define TRX_MAGIC 0x30524448
  14. #define UBI_EC_MAGIC 0x23494255 /* UBI# */
  15. struct trx_header {
  16. uint32_t magic;
  17. uint32_t length;
  18. uint32_t crc32;
  19. uint16_t flags;
  20. uint16_t version;
  21. uint32_t offset[3];
  22. } __packed;
  23. static const char *parser_trx_data_part_name(struct mtd_info *master,
  24. size_t offset)
  25. {
  26. uint32_t buf;
  27. size_t bytes_read;
  28. int err;
  29. err = mtd_read(master, offset, sizeof(buf), &bytes_read,
  30. (uint8_t *)&buf);
  31. if (err && !mtd_is_bitflip(err)) {
  32. pr_err("mtd_read error while parsing (offset: 0x%zX): %d\n",
  33. offset, err);
  34. goto out_default;
  35. }
  36. if (buf == UBI_EC_MAGIC)
  37. return "ubi";
  38. out_default:
  39. return "rootfs";
  40. }
  41. static int parser_trx_parse(struct mtd_info *mtd,
  42. const struct mtd_partition **pparts,
  43. struct mtd_part_parser_data *data)
  44. {
  45. struct device_node *np = mtd_get_of_node(mtd);
  46. struct mtd_partition *parts;
  47. struct mtd_partition *part;
  48. struct trx_header trx;
  49. size_t bytes_read;
  50. uint8_t curr_part = 0, i = 0;
  51. uint32_t trx_magic = TRX_MAGIC;
  52. int err;
  53. /* Get different magic from device tree if specified */
  54. err = of_property_read_u32(np, "brcm,trx-magic", &trx_magic);
  55. if (err != 0 && err != -EINVAL)
  56. pr_err("failed to parse \"brcm,trx-magic\" DT attribute, using default: %d\n", err);
  57. parts = kzalloc_objs(struct mtd_partition, TRX_PARSER_MAX_PARTS);
  58. if (!parts)
  59. return -ENOMEM;
  60. err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx);
  61. if (err) {
  62. pr_err("MTD reading error: %d\n", err);
  63. kfree(parts);
  64. return err;
  65. }
  66. if (trx.magic != trx_magic) {
  67. kfree(parts);
  68. return -ENOENT;
  69. }
  70. /* We have LZMA loader if there is address in offset[2] */
  71. if (trx.offset[2]) {
  72. part = &parts[curr_part++];
  73. part->name = "loader";
  74. part->offset = trx.offset[i];
  75. i++;
  76. }
  77. if (trx.offset[i]) {
  78. part = &parts[curr_part++];
  79. part->name = "linux";
  80. part->offset = trx.offset[i];
  81. i++;
  82. }
  83. if (trx.offset[i]) {
  84. part = &parts[curr_part++];
  85. part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
  86. part->offset = trx.offset[i];
  87. i++;
  88. }
  89. /*
  90. * Assume that every partition ends at the beginning of the one it is
  91. * followed by.
  92. */
  93. for (i = 0; i < curr_part; i++) {
  94. u64 next_part_offset = (i < curr_part - 1) ?
  95. parts[i + 1].offset : mtd->size;
  96. parts[i].size = next_part_offset - parts[i].offset;
  97. }
  98. *pparts = parts;
  99. return i;
  100. };
  101. static const struct of_device_id mtd_parser_trx_of_match_table[] = {
  102. { .compatible = "brcm,trx" },
  103. {},
  104. };
  105. MODULE_DEVICE_TABLE(of, mtd_parser_trx_of_match_table);
  106. static struct mtd_part_parser mtd_parser_trx = {
  107. .parse_fn = parser_trx_parse,
  108. .name = "trx",
  109. .of_match_table = mtd_parser_trx_of_match_table,
  110. };
  111. module_mtd_part_parser(mtd_parser_trx);
  112. MODULE_LICENSE("GPL v2");
  113. MODULE_DESCRIPTION("Parser for TRX format partitions");