myloader.c 4.1 KB

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