ar7part.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright © 2007 Eugene Konev <ejka@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. * TI AR7 flash partition table.
  19. * Based on ar7 map by Felix Fietkau <nbd@openwrt.org>
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/partitions.h>
  26. #include <linux/bootmem.h>
  27. #include <linux/module.h>
  28. #include <uapi/linux/magic.h>
  29. #define AR7_PARTS 4
  30. #define ROOT_OFFSET 0xe0000
  31. #define LOADER_MAGIC1 le32_to_cpu(0xfeedfa42)
  32. #define LOADER_MAGIC2 le32_to_cpu(0xfeed1281)
  33. struct ar7_bin_rec {
  34. unsigned int checksum;
  35. unsigned int length;
  36. unsigned int address;
  37. };
  38. static int create_mtd_partitions(struct mtd_info *master,
  39. const struct mtd_partition **pparts,
  40. struct mtd_part_parser_data *data)
  41. {
  42. struct ar7_bin_rec header;
  43. unsigned int offset;
  44. size_t len;
  45. unsigned int pre_size = master->erasesize, post_size = 0;
  46. unsigned int root_offset = ROOT_OFFSET;
  47. int retries = 10;
  48. struct mtd_partition *ar7_parts;
  49. ar7_parts = kzalloc(sizeof(*ar7_parts) * AR7_PARTS, GFP_KERNEL);
  50. if (!ar7_parts)
  51. return -ENOMEM;
  52. ar7_parts[0].name = "loader";
  53. ar7_parts[0].offset = 0;
  54. ar7_parts[0].size = master->erasesize;
  55. ar7_parts[0].mask_flags = MTD_WRITEABLE;
  56. ar7_parts[1].name = "config";
  57. ar7_parts[1].offset = 0;
  58. ar7_parts[1].size = master->erasesize;
  59. ar7_parts[1].mask_flags = 0;
  60. do { /* Try 10 blocks starting from master->erasesize */
  61. offset = pre_size;
  62. mtd_read(master, offset, sizeof(header), &len,
  63. (uint8_t *)&header);
  64. if (!strncmp((char *)&header, "TIENV0.8", 8))
  65. ar7_parts[1].offset = pre_size;
  66. if (header.checksum == LOADER_MAGIC1)
  67. break;
  68. if (header.checksum == LOADER_MAGIC2)
  69. break;
  70. pre_size += master->erasesize;
  71. } while (retries--);
  72. pre_size = offset;
  73. if (!ar7_parts[1].offset) {
  74. ar7_parts[1].offset = master->size - master->erasesize;
  75. post_size = master->erasesize;
  76. }
  77. switch (header.checksum) {
  78. case LOADER_MAGIC1:
  79. while (header.length) {
  80. offset += sizeof(header) + header.length;
  81. mtd_read(master, offset, sizeof(header), &len,
  82. (uint8_t *)&header);
  83. }
  84. root_offset = offset + sizeof(header) + 4;
  85. break;
  86. case LOADER_MAGIC2:
  87. while (header.length) {
  88. offset += sizeof(header) + header.length;
  89. mtd_read(master, offset, sizeof(header), &len,
  90. (uint8_t *)&header);
  91. }
  92. root_offset = offset + sizeof(header) + 4 + 0xff;
  93. root_offset &= ~(uint32_t)0xff;
  94. break;
  95. default:
  96. printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum);
  97. break;
  98. }
  99. mtd_read(master, root_offset, sizeof(header), &len, (u8 *)&header);
  100. if (header.checksum != SQUASHFS_MAGIC) {
  101. root_offset += master->erasesize - 1;
  102. root_offset &= ~(master->erasesize - 1);
  103. }
  104. ar7_parts[2].name = "linux";
  105. ar7_parts[2].offset = pre_size;
  106. ar7_parts[2].size = master->size - pre_size - post_size;
  107. ar7_parts[2].mask_flags = 0;
  108. ar7_parts[3].name = "rootfs";
  109. ar7_parts[3].offset = root_offset;
  110. ar7_parts[3].size = master->size - root_offset - post_size;
  111. ar7_parts[3].mask_flags = 0;
  112. *pparts = ar7_parts;
  113. return AR7_PARTS;
  114. }
  115. static struct mtd_part_parser ar7_parser = {
  116. .parse_fn = create_mtd_partitions,
  117. .name = "ar7part",
  118. };
  119. module_mtd_part_parser(ar7_parser);
  120. MODULE_LICENSE("GPL");
  121. MODULE_AUTHOR( "Felix Fietkau <nbd@openwrt.org>, "
  122. "Eugene Konev <ejka@openwrt.org>");
  123. MODULE_DESCRIPTION("MTD partitioning for TI AR7");