parser_trx.c 2.9 KB

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