ar7part.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/magic.h>
  28. #include <linux/module.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. #ifndef SQUASHFS_MAGIC
  34. #define SQUASHFS_MAGIC 0x73717368
  35. #endif
  36. struct ar7_bin_rec {
  37. unsigned int checksum;
  38. unsigned int length;
  39. unsigned int address;
  40. };
  41. static int create_mtd_partitions(struct mtd_info *master,
  42. struct mtd_partition **pparts,
  43. struct mtd_part_parser_data *data)
  44. {
  45. struct ar7_bin_rec header;
  46. unsigned int offset;
  47. size_t len;
  48. unsigned int pre_size = master->erasesize, post_size = 0;
  49. unsigned int root_offset = ROOT_OFFSET;
  50. int retries = 10;
  51. struct mtd_partition *ar7_parts;
  52. ar7_parts = kzalloc(sizeof(*ar7_parts) * AR7_PARTS, GFP_KERNEL);
  53. if (!ar7_parts)
  54. return -ENOMEM;
  55. ar7_parts[0].name = "loader";
  56. ar7_parts[0].offset = 0;
  57. ar7_parts[0].size = master->erasesize;
  58. ar7_parts[0].mask_flags = MTD_WRITEABLE;
  59. ar7_parts[1].name = "config";
  60. ar7_parts[1].offset = 0;
  61. ar7_parts[1].size = master->erasesize;
  62. ar7_parts[1].mask_flags = 0;
  63. do { /* Try 10 blocks starting from master->erasesize */
  64. offset = pre_size;
  65. mtd_read(master, offset, sizeof(header), &len,
  66. (uint8_t *)&header);
  67. if (!strncmp((char *)&header, "TIENV0.8", 8))
  68. ar7_parts[1].offset = pre_size;
  69. if (header.checksum == LOADER_MAGIC1)
  70. break;
  71. if (header.checksum == LOADER_MAGIC2)
  72. break;
  73. pre_size += master->erasesize;
  74. } while (retries--);
  75. pre_size = offset;
  76. if (!ar7_parts[1].offset) {
  77. ar7_parts[1].offset = master->size - master->erasesize;
  78. post_size = master->erasesize;
  79. }
  80. switch (header.checksum) {
  81. case LOADER_MAGIC1:
  82. while (header.length) {
  83. offset += sizeof(header) + header.length;
  84. mtd_read(master, offset, sizeof(header), &len,
  85. (uint8_t *)&header);
  86. }
  87. root_offset = offset + sizeof(header) + 4;
  88. break;
  89. case LOADER_MAGIC2:
  90. while (header.length) {
  91. offset += sizeof(header) + header.length;
  92. mtd_read(master, offset, sizeof(header), &len,
  93. (uint8_t *)&header);
  94. }
  95. root_offset = offset + sizeof(header) + 4 + 0xff;
  96. root_offset &= ~(uint32_t)0xff;
  97. break;
  98. default:
  99. printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum);
  100. break;
  101. }
  102. mtd_read(master, root_offset, sizeof(header), &len, (u8 *)&header);
  103. if (header.checksum != SQUASHFS_MAGIC) {
  104. root_offset += master->erasesize - 1;
  105. root_offset &= ~(master->erasesize - 1);
  106. }
  107. ar7_parts[2].name = "linux";
  108. ar7_parts[2].offset = pre_size;
  109. ar7_parts[2].size = master->size - pre_size - post_size;
  110. ar7_parts[2].mask_flags = 0;
  111. ar7_parts[3].name = "rootfs";
  112. ar7_parts[3].offset = root_offset;
  113. ar7_parts[3].size = master->size - root_offset - post_size;
  114. ar7_parts[3].mask_flags = 0;
  115. *pparts = ar7_parts;
  116. return AR7_PARTS;
  117. }
  118. static struct mtd_part_parser ar7_parser = {
  119. .owner = THIS_MODULE,
  120. .parse_fn = create_mtd_partitions,
  121. .name = "ar7part",
  122. };
  123. static int __init ar7_parser_init(void)
  124. {
  125. return register_mtd_parser(&ar7_parser);
  126. }
  127. module_init(ar7_parser_init);
  128. MODULE_LICENSE("GPL");
  129. MODULE_AUTHOR( "Felix Fietkau <nbd@openwrt.org>, "
  130. "Eugene Konev <ejka@openwrt.org>");
  131. MODULE_DESCRIPTION("MTD partitioning for TI AR7");