400-mtd-add-rootfs-split-support.patch 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. --- a/drivers/mtd/Kconfig
  2. +++ b/drivers/mtd/Kconfig
  3. @@ -12,6 +12,28 @@ menuconfig MTD
  4. if MTD
  5. +menu "libreCMC specific MTD options"
  6. +
  7. +config MTD_ROOTFS_ROOT_DEV
  8. + bool "Automatically set 'rootfs' partition to be root filesystem"
  9. + default y
  10. +
  11. +config MTD_SPLIT_FIRMWARE
  12. + bool "Automatically split firmware partition for kernel+rootfs"
  13. + default y
  14. +
  15. +config MTD_SPLIT_FIRMWARE_NAME
  16. + string "Firmware partition name"
  17. + depends on MTD_SPLIT_FIRMWARE
  18. + default "firmware"
  19. +
  20. +config MTD_UIMAGE_SPLIT
  21. + bool "Enable split support for firmware partitions containing a uImage"
  22. + depends on MTD_SPLIT_FIRMWARE
  23. + default y
  24. +
  25. +endmenu
  26. +
  27. config MTD_TESTS
  28. tristate "MTD tests support (DANGEROUS)"
  29. depends on m
  30. --- a/drivers/mtd/mtdpart.c
  31. +++ b/drivers/mtd/mtdpart.c
  32. @@ -29,9 +29,11 @@
  33. #include <linux/kmod.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/partitions.h>
  36. +#include <linux/magic.h>
  37. #include <linux/err.h>
  38. #include "mtdcore.h"
  39. +#include "mtdsplit.h"
  40. /* Our partition linked list */
  41. static LIST_HEAD(mtd_partitions);
  42. @@ -45,13 +47,14 @@ struct mtd_part {
  43. struct list_head list;
  44. };
  45. +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
  46. +
  47. /*
  48. * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
  49. * the pointer to that structure with this macro.
  50. */
  51. #define PART(x) ((struct mtd_part *)(x))
  52. -
  53. /*
  54. * MTD methods which simply translate the effective address and pass through
  55. * to the _real_ device.
  56. @@ -547,8 +550,10 @@ out_register:
  57. return slave;
  58. }
  59. -int mtd_add_partition(struct mtd_info *master, const char *name,
  60. - long long offset, long long length)
  61. +
  62. +static int
  63. +__mtd_add_partition(struct mtd_info *master, const char *name,
  64. + long long offset, long long length, bool dup_check)
  65. {
  66. struct mtd_partition part;
  67. struct mtd_part *p, *new;
  68. @@ -580,21 +585,24 @@ int mtd_add_partition(struct mtd_info *m
  69. end = offset + length;
  70. mutex_lock(&mtd_partitions_mutex);
  71. - list_for_each_entry(p, &mtd_partitions, list)
  72. - if (p->master == master) {
  73. - if ((start >= p->offset) &&
  74. - (start < (p->offset + p->mtd.size)))
  75. - goto err_inv;
  76. -
  77. - if ((end >= p->offset) &&
  78. - (end < (p->offset + p->mtd.size)))
  79. - goto err_inv;
  80. - }
  81. + if (dup_check) {
  82. + list_for_each_entry(p, &mtd_partitions, list)
  83. + if (p->master == master) {
  84. + if ((start >= p->offset) &&
  85. + (start < (p->offset + p->mtd.size)))
  86. + goto err_inv;
  87. +
  88. + if ((end >= p->offset) &&
  89. + (end < (p->offset + p->mtd.size)))
  90. + goto err_inv;
  91. + }
  92. + }
  93. list_add(&new->list, &mtd_partitions);
  94. mutex_unlock(&mtd_partitions_mutex);
  95. add_mtd_device(&new->mtd);
  96. + mtd_partition_split(master, new);
  97. return ret;
  98. err_inv:
  99. @@ -604,6 +612,12 @@ err_inv:
  100. }
  101. EXPORT_SYMBOL_GPL(mtd_add_partition);
  102. +int mtd_add_partition(struct mtd_info *master, const char *name,
  103. + long long offset, long long length)
  104. +{
  105. + return __mtd_add_partition(master, name, offset, length, true);
  106. +}
  107. +
  108. int mtd_del_partition(struct mtd_info *master, int partno)
  109. {
  110. struct mtd_part *slave, *next;
  111. @@ -627,6 +641,74 @@ int mtd_del_partition(struct mtd_info *m
  112. }
  113. EXPORT_SYMBOL_GPL(mtd_del_partition);
  114. +static inline unsigned long
  115. +mtd_pad_erasesize(struct mtd_info *mtd, int offset, int len)
  116. +{
  117. + unsigned long mask = mtd->erasesize - 1;
  118. +
  119. + len += offset & mask;
  120. + len = (len + mask) & ~mask;
  121. + len -= offset & mask;
  122. + return len;
  123. +}
  124. +
  125. +#define UBOOT_MAGIC 0x27051956
  126. +
  127. +static void split_uimage(struct mtd_info *master, struct mtd_part *part)
  128. +{
  129. + struct {
  130. + __be32 magic;
  131. + __be32 pad[2];
  132. + __be32 size;
  133. + } hdr;
  134. + size_t len;
  135. +
  136. + if (mtd_read(master, part->offset, sizeof(hdr), &len, (void *) &hdr))
  137. + return;
  138. +
  139. + if (len != sizeof(hdr) || hdr.magic != cpu_to_be32(UBOOT_MAGIC))
  140. + return;
  141. +
  142. + len = be32_to_cpu(hdr.size) + 0x40;
  143. + len = mtd_pad_erasesize(master, part->offset, len);
  144. + if (len + master->erasesize > part->mtd.size)
  145. + return;
  146. +
  147. + __mtd_add_partition(master, "rootfs", part->offset + len,
  148. + part->mtd.size - len, false);
  149. +}
  150. +
  151. +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
  152. +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
  153. +#else
  154. +#define SPLIT_FIRMWARE_NAME "unused"
  155. +#endif
  156. +
  157. +static void split_firmware(struct mtd_info *master, struct mtd_part *part)
  158. +{
  159. + if (config_enabled(CONFIG_MTD_UIMAGE_SPLIT))
  160. + split_uimage(master, part);
  161. +}
  162. +
  163. +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
  164. + int offset, int size)
  165. +{
  166. +}
  167. +
  168. +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
  169. +{
  170. + static int rootfs_found = 0;
  171. +
  172. + if (rootfs_found)
  173. + return;
  174. +
  175. + if (!strcmp(part->mtd.name, SPLIT_FIRMWARE_NAME) &&
  176. + config_enabled(CONFIG_MTD_SPLIT_FIRMWARE))
  177. + split_firmware(master, part);
  178. +
  179. + arch_split_mtd_part(master, part->mtd.name, part->offset,
  180. + part->mtd.size);
  181. +}
  182. /*
  183. * This function, given a master MTD object and a partition table, creates
  184. * and registers slave MTD objects which are bound to the master according to
  185. @@ -656,6 +738,7 @@ int add_mtd_partitions(struct mtd_info *
  186. mutex_unlock(&mtd_partitions_mutex);
  187. add_mtd_device(&slave->mtd);
  188. + mtd_partition_split(master, slave);
  189. cur_offset = slave->offset + slave->mtd.size;
  190. }
  191. --- a/include/linux/mtd/partitions.h
  192. +++ b/include/linux/mtd/partitions.h
  193. @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
  194. long long offset, long long length);
  195. int mtd_del_partition(struct mtd_info *master, int partno);
  196. uint64_t mtd_get_device_size(const struct mtd_info *mtd);
  197. +extern void __weak arch_split_mtd_part(struct mtd_info *master,
  198. + const char *name, int offset, int size);
  199. #endif