ac49xpart.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * AudioCodes AC49x PSPBoot-based flash partition table
  3. * Copyright 2012 Daniel Golle <daniel.golle@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/mtd/mtd.h>
  23. #include <linux/mtd/partitions.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/magic.h>
  26. #include <linux/module.h>
  27. #include <asm/mach-ar7/prom.h>
  28. #define AC49X_MAXENVPARTS 8
  29. #define AC49X_PARTTYPE_LOADER 0
  30. #define AC49X_PARTTYPE_BOOTENV 1
  31. #define AC49X_PARTTYPE_LINUX 2
  32. #define AC49X_PARTTYPE_ROOTFS 3
  33. #define AC49X_PARTTYPE_UNKNOWN 4
  34. #define AC49X_NUM_PARTTYPES 5
  35. #define AC49X_FLASH_ADDRMASK 0x00FFFFFF
  36. #define AC49X_LOADER_MAGIC 0x40809000
  37. #define AC49X_LINUX_MAGIC 0x464c457f /* ELF */
  38. #define AC49X_BOOTENV_MAGIC 0x4578614d /* MaxE */
  39. #define ROOTFS_MIN_OFFSET 0xC0000
  40. int parse_partvar(const unsigned char *partvar, struct mtd_partition *part)
  41. {
  42. unsigned int partstart, partend;
  43. unsigned int pnum;
  44. pnum = sscanf(partvar, "0x%x,0x%x", &partstart, &partend);
  45. if (pnum != 2)
  46. return 1;
  47. part->offset = partstart & AC49X_FLASH_ADDRMASK;
  48. part->size = partend - partstart;
  49. return 0;
  50. }
  51. int detect_parttype(struct mtd_info *master, struct mtd_partition part)
  52. {
  53. unsigned int magic;
  54. size_t len;
  55. if (part.size < 4)
  56. return -1;
  57. mtd_read(master, part.offset, sizeof(magic), &len,
  58. (uint8_t *)&magic);
  59. if (len != sizeof(magic))
  60. return -1;
  61. switch (magic) {
  62. case AC49X_LOADER_MAGIC:
  63. return AC49X_PARTTYPE_LOADER;
  64. case AC49X_LINUX_MAGIC:
  65. return AC49X_PARTTYPE_LINUX;
  66. case SQUASHFS_MAGIC:
  67. case CRAMFS_MAGIC:
  68. case CRAMFS_MAGIC_WEND:
  69. return AC49X_PARTTYPE_ROOTFS;
  70. case AC49X_BOOTENV_MAGIC:
  71. return AC49X_PARTTYPE_BOOTENV;
  72. default:
  73. switch (magic & 0xFF) {
  74. case JFFS2_SUPER_MAGIC:
  75. return AC49X_PARTTYPE_ROOTFS;
  76. }
  77. switch (magic >> 8) {
  78. case JFFS2_SUPER_MAGIC:
  79. return AC49X_PARTTYPE_ROOTFS;
  80. }
  81. return AC49X_PARTTYPE_UNKNOWN;
  82. }
  83. }
  84. const char *partnames[] = {
  85. "loader",
  86. "config",
  87. "linux",
  88. "rootfs",
  89. "data"
  90. };
  91. void gen_partname(unsigned int type,
  92. unsigned int *typenumeration,
  93. struct mtd_partition *part)
  94. {
  95. char *s = kzalloc(sizeof(char) * 8, GFP_KERNEL);
  96. (typenumeration[type])++;
  97. if (typenumeration[type] == 1)
  98. sprintf(s, "%s", partnames[type]);
  99. else
  100. sprintf(s, "%s%d", partnames[type], typenumeration[type]);
  101. part->name = s;
  102. }
  103. static int create_mtd_partitions(struct mtd_info *master,
  104. struct mtd_partition **pparts,
  105. struct mtd_part_parser_data *data)
  106. {
  107. unsigned int envpartnum = 0, linuxpartnum = 0;
  108. unsigned int typenumeration[5] = { 0, 0, 0, 0, 0 };
  109. unsigned char evn[5];
  110. const unsigned char *partvar = NULL;
  111. struct mtd_partition *ac49x_parts;
  112. ac49x_parts = kzalloc(sizeof(*ac49x_parts) * AC49X_MAXENVPARTS,
  113. GFP_KERNEL);
  114. if (!ac49x_parts)
  115. return -ENOMEM;
  116. linuxpartnum = 0;
  117. for (envpartnum = 0; envpartnum < AC49X_MAXENVPARTS; envpartnum++) {
  118. struct mtd_partition parsepart;
  119. unsigned int offset, size, type;
  120. int err;
  121. sprintf(evn, "mtd%d", envpartnum);
  122. partvar = prom_getenv(evn);
  123. if (!partvar)
  124. continue;
  125. err = parse_partvar(partvar, &parsepart);
  126. if (err)
  127. continue;
  128. offset = parsepart.offset;
  129. size = parsepart.size;
  130. type = detect_parttype(master, parsepart);
  131. gen_partname(type, typenumeration, &parsepart);
  132. /* protect loader */
  133. if (type == AC49X_PARTTYPE_LOADER)
  134. parsepart.mask_flags = MTD_WRITEABLE;
  135. else
  136. parsepart.mask_flags = 0;
  137. memcpy(&(ac49x_parts[linuxpartnum]), &parsepart,
  138. sizeof(struct mtd_partition));
  139. /* scan for contained rootfs */
  140. if (type == AC49X_PARTTYPE_LINUX) {
  141. parsepart.offset += ROOTFS_MIN_OFFSET &
  142. ~(master->erasesize - 1);
  143. parsepart.size -= ROOTFS_MIN_OFFSET &
  144. ~(master->erasesize - 1);
  145. do {
  146. unsigned int size, offset;
  147. size = parsepart.size;
  148. offset = parsepart.offset;
  149. type = detect_parttype(master, parsepart);
  150. if (type == AC49X_PARTTYPE_ROOTFS) {
  151. gen_partname(type, typenumeration,
  152. &parsepart);
  153. printk(KERN_INFO
  154. "%s %s: 0x%08x@0x%08x\n",
  155. "detected sub-partition",
  156. parsepart.name,
  157. (unsigned int)parsepart.size,
  158. (unsigned int)parsepart.offset);
  159. linuxpartnum++;
  160. memcpy(&(ac49x_parts[linuxpartnum]),
  161. &parsepart,
  162. sizeof(struct mtd_partition));
  163. break;
  164. }
  165. parsepart.offset += master->erasesize;
  166. parsepart.size -= master->erasesize;
  167. } while (parsepart.size >= master->erasesize);
  168. }
  169. linuxpartnum++;
  170. }
  171. *pparts = ac49x_parts;
  172. return linuxpartnum;
  173. }
  174. static struct mtd_part_parser ac49x_parser = {
  175. .owner = THIS_MODULE,
  176. .parse_fn = create_mtd_partitions,
  177. .name = "ac49xpart",
  178. };
  179. static int __init ac49x_parser_init(void)
  180. {
  181. register_mtd_parser(&ac49x_parser);
  182. return 0;
  183. }
  184. module_init(ac49x_parser_init);
  185. MODULE_LICENSE("GPL");
  186. MODULE_AUTHOR("Daniel Golle <daniel.golle@gmail.com>");
  187. MODULE_DESCRIPTION("MTD partitioning for AudioCodes AC49x");