parser_trx.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Parser for TRX format partitions
  4. *
  5. * Copyright (C) 2012 - 2017 Rafał Miłecki <[email protected]>
  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 = kcalloc(TRX_PARSER_MAX_PARTS, sizeof(struct mtd_partition),
  58. GFP_KERNEL);
  59. if (!parts)
  60. return -ENOMEM;
  61. err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx);
  62. if (err) {
  63. pr_err("MTD reading error: %d\n", err);
  64. kfree(parts);
  65. return err;
  66. }
  67. if (trx.magic != trx_magic) {
  68. kfree(parts);
  69. return -ENOENT;
  70. }
  71. /* We have LZMA loader if there is address in offset[2] */
  72. if (trx.offset[2]) {
  73. part = &parts[curr_part++];
  74. part->name = "loader";
  75. part->offset = trx.offset[i];
  76. i++;
  77. }
  78. if (trx.offset[i]) {
  79. part = &parts[curr_part++];
  80. part->name = "linux";
  81. part->offset = trx.offset[i];
  82. i++;
  83. }
  84. if (trx.offset[i]) {
  85. part = &parts[curr_part++];
  86. part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
  87. part->offset = trx.offset[i];
  88. i++;
  89. }
  90. /*
  91. * Assume that every partition ends at the beginning of the one it is
  92. * followed by.
  93. */
  94. for (i = 0; i < curr_part; i++) {
  95. u64 next_part_offset = (i < curr_part - 1) ?
  96. parts[i + 1].offset : mtd->size;
  97. parts[i].size = next_part_offset - parts[i].offset;
  98. }
  99. *pparts = parts;
  100. return i;
  101. };
  102. static const struct of_device_id mtd_parser_trx_of_match_table[] = {
  103. { .compatible = "brcm,trx" },
  104. {},
  105. };
  106. MODULE_DEVICE_TABLE(of, mtd_parser_trx_of_match_table);
  107. static struct mtd_part_parser mtd_parser_trx = {
  108. .parse_fn = parser_trx_parse,
  109. .name = "trx",
  110. .of_match_table = mtd_parser_trx_of_match_table,
  111. };
  112. module_mtd_part_parser(mtd_parser_trx);
  113. MODULE_LICENSE("GPL v2");
  114. MODULE_DESCRIPTION("Parser for TRX format partitions");