myloader.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 <[email protected]>
  6. *
  7. * This file was based on drivers/mtd/redboot.c
  8. * Author: Red Hat, Inc. - David Woodhouse <[email protected]>
  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. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0))
  32. static int myloader_parse_partitions(struct mtd_info *master,
  33. struct mtd_partition **pparts,
  34. struct mtd_part_parser_data *data)
  35. #else
  36. static int myloader_parse_partitions(struct mtd_info *master,
  37. struct mtd_partition **pparts,
  38. unsigned long origin)
  39. #endif
  40. {
  41. struct part_data *buf;
  42. struct mylo_partition_table *tab;
  43. struct mylo_partition *part;
  44. struct mtd_partition *mtd_parts;
  45. struct mtd_partition *mtd_part;
  46. int num_parts;
  47. int ret, i;
  48. size_t retlen;
  49. char *names;
  50. unsigned long offset;
  51. unsigned long blocklen;
  52. buf = vmalloc(sizeof(*buf));
  53. if (!buf) {
  54. return -ENOMEM;
  55. goto out;
  56. }
  57. tab = &buf->tab;
  58. blocklen = master->erasesize;
  59. if (blocklen < BLOCK_LEN_MIN)
  60. blocklen = BLOCK_LEN_MIN;
  61. offset = blocklen;
  62. /* Find the partition table */
  63. for (i = 0; i < 4; i++, offset += blocklen) {
  64. printk(KERN_DEBUG "%s: searching for MyLoader partition table"
  65. " at offset 0x%lx\n", master->name, offset);
  66. ret = mtd_read(master, offset, sizeof(*buf), &retlen,
  67. (void *)buf);
  68. if (ret)
  69. goto out_free_buf;
  70. if (retlen != sizeof(*buf)) {
  71. ret = -EIO;
  72. goto out_free_buf;
  73. }
  74. /* Check for Partition Table magic number */
  75. if (tab->magic == le32_to_cpu(MYLO_MAGIC_PARTITIONS))
  76. break;
  77. }
  78. if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
  79. printk(KERN_DEBUG "%s: no MyLoader partition table found\n",
  80. master->name);
  81. ret = 0;
  82. goto out_free_buf;
  83. }
  84. /* The MyLoader and the Partition Table is always present */
  85. num_parts = 2;
  86. /* Detect number of used partitions */
  87. for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
  88. part = &tab->partitions[i];
  89. if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
  90. continue;
  91. num_parts++;
  92. }
  93. mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
  94. num_parts * PART_NAME_LEN), GFP_KERNEL);
  95. if (!mtd_parts) {
  96. ret = -ENOMEM;
  97. goto out_free_buf;
  98. }
  99. mtd_part = mtd_parts;
  100. names = (char *)&mtd_parts[num_parts];
  101. strncpy(names, "myloader", PART_NAME_LEN);
  102. mtd_part->name = names;
  103. mtd_part->offset = 0;
  104. mtd_part->size = offset;
  105. mtd_part->mask_flags = MTD_WRITEABLE;
  106. mtd_part++;
  107. names += PART_NAME_LEN;
  108. strncpy(names, "partition_table", PART_NAME_LEN);
  109. mtd_part->name = names;
  110. mtd_part->offset = offset;
  111. mtd_part->size = blocklen;
  112. mtd_part->mask_flags = MTD_WRITEABLE;
  113. mtd_part++;
  114. names += PART_NAME_LEN;
  115. for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
  116. part = &tab->partitions[i];
  117. if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
  118. continue;
  119. if ((buf->names[i][0]) && (buf->names[i][0] != '\xff'))
  120. strncpy(names, buf->names[i], PART_NAME_LEN);
  121. else
  122. snprintf(names, PART_NAME_LEN, "partition%d", i);
  123. mtd_part->offset = le32_to_cpu(part->addr);
  124. mtd_part->size = le32_to_cpu(part->size);
  125. mtd_part->name = names;
  126. mtd_part++;
  127. names += PART_NAME_LEN;
  128. }
  129. *pparts = mtd_parts;
  130. ret = num_parts;
  131. out_free_buf:
  132. vfree(buf);
  133. out:
  134. return ret;
  135. }
  136. static struct mtd_part_parser myloader_mtd_parser = {
  137. .owner = THIS_MODULE,
  138. .parse_fn = myloader_parse_partitions,
  139. .name = "MyLoader",
  140. };
  141. static int __init myloader_mtd_parser_init(void)
  142. {
  143. return register_mtd_parser(&myloader_mtd_parser);
  144. }
  145. static void __exit myloader_mtd_parser_exit(void)
  146. {
  147. deregister_mtd_parser(&myloader_mtd_parser);
  148. }
  149. module_init(myloader_mtd_parser_init);
  150. module_exit(myloader_mtd_parser_exit);
  151. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  152. MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
  153. MODULE_LICENSE("GPL v2");