myloader.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Parse MyLoader-style flash partition tables and produce a Linux partition
  3. * array to match.
  4. *
  5. * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
  6. *
  7. * This file was based on drivers/mtd/redboot.c
  8. * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published
  12. * by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/version.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/byteorder/generic.h>
  24. #include <linux/myloader.h>
  25. #define BLOCK_LEN_MIN 0x10000
  26. #define PART_NAME_LEN 32
  27. struct part_data {
  28. struct mylo_partition_table tab;
  29. char names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
  30. };
  31. static int myloader_parse_partitions(struct mtd_info *master,
  32. struct mtd_partition **pparts,
  33. struct mtd_part_parser_data *data)
  34. {
  35. struct part_data *buf;
  36. struct mylo_partition_table *tab;
  37. struct mylo_partition *part;
  38. struct mtd_partition *mtd_parts;
  39. struct mtd_partition *mtd_part;
  40. int num_parts;
  41. int ret, i;
  42. size_t retlen;
  43. char *names;
  44. unsigned long offset;
  45. unsigned long blocklen;
  46. buf = vmalloc(sizeof(*buf));
  47. if (!buf) {
  48. return -ENOMEM;
  49. goto out;
  50. }
  51. tab = &buf->tab;
  52. blocklen = master->erasesize;
  53. if (blocklen < BLOCK_LEN_MIN)
  54. blocklen = BLOCK_LEN_MIN;
  55. offset = blocklen;
  56. /* Find the partition table */
  57. for (i = 0; i < 4; i++, offset += blocklen) {
  58. printk(KERN_DEBUG "%s: searching for MyLoader partition table"
  59. " at offset 0x%lx\n", master->name, offset);
  60. ret = mtd_read(master, offset, sizeof(*buf), &retlen,
  61. (void *)buf);
  62. if (ret)
  63. goto out_free_buf;
  64. if (retlen != sizeof(*buf)) {
  65. ret = -EIO;
  66. goto out_free_buf;
  67. }
  68. /* Check for Partition Table magic number */
  69. if (tab->magic == le32_to_cpu(MYLO_MAGIC_PARTITIONS))
  70. break;
  71. }
  72. if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
  73. printk(KERN_DEBUG "%s: no MyLoader partition table found\n",
  74. master->name);
  75. ret = 0;
  76. goto out_free_buf;
  77. }
  78. /* The MyLoader and the Partition Table is always present */
  79. num_parts = 2;
  80. /* Detect number of used partitions */
  81. for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
  82. part = &tab->partitions[i];
  83. if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
  84. continue;
  85. num_parts++;
  86. }
  87. mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
  88. num_parts * PART_NAME_LEN), GFP_KERNEL);
  89. if (!mtd_parts) {
  90. ret = -ENOMEM;
  91. goto out_free_buf;
  92. }
  93. mtd_part = mtd_parts;
  94. names = (char *)&mtd_parts[num_parts];
  95. strncpy(names, "myloader", PART_NAME_LEN);
  96. mtd_part->name = names;
  97. mtd_part->offset = 0;
  98. mtd_part->size = offset;
  99. mtd_part->mask_flags = MTD_WRITEABLE;
  100. mtd_part++;
  101. names += PART_NAME_LEN;
  102. strncpy(names, "partition_table", PART_NAME_LEN);
  103. mtd_part->name = names;
  104. mtd_part->offset = offset;
  105. mtd_part->size = blocklen;
  106. mtd_part->mask_flags = MTD_WRITEABLE;
  107. mtd_part++;
  108. names += PART_NAME_LEN;
  109. for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
  110. part = &tab->partitions[i];
  111. if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
  112. continue;
  113. if ((buf->names[i][0]) && (buf->names[i][0] != '\xff'))
  114. strncpy(names, buf->names[i], PART_NAME_LEN);
  115. else
  116. snprintf(names, PART_NAME_LEN, "partition%d", i);
  117. mtd_part->offset = le32_to_cpu(part->addr);
  118. mtd_part->size = le32_to_cpu(part->size);
  119. mtd_part->name = names;
  120. mtd_part++;
  121. names += PART_NAME_LEN;
  122. }
  123. *pparts = mtd_parts;
  124. ret = num_parts;
  125. out_free_buf:
  126. vfree(buf);
  127. out:
  128. return ret;
  129. }
  130. static struct mtd_part_parser myloader_mtd_parser = {
  131. .owner = THIS_MODULE,
  132. .parse_fn = myloader_parse_partitions,
  133. .name = "MyLoader",
  134. };
  135. static int __init myloader_mtd_parser_init(void)
  136. {
  137. register_mtd_parser(&myloader_mtd_parser);
  138. return 0;
  139. }
  140. static void __exit myloader_mtd_parser_exit(void)
  141. {
  142. deregister_mtd_parser(&myloader_mtd_parser);
  143. }
  144. module_init(myloader_mtd_parser_init);
  145. module_exit(myloader_mtd_parser_exit);
  146. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  147. MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
  148. MODULE_LICENSE("GPL v2");