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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. --- a/drivers/mtd/Kconfig
  2. +++ b/drivers/mtd/Kconfig
  3. @@ -12,6 +12,23 @@ menuconfig MTD
  4. if MTD
  5. +menu "OpenWrt 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. +endmenu
  21. +
  22. config MTD_TESTS
  23. tristate "MTD tests support (DANGEROUS)"
  24. depends on m
  25. --- a/drivers/mtd/mtdpart.c
  26. +++ b/drivers/mtd/mtdpart.c
  27. @@ -30,9 +30,11 @@
  28. #include <linux/mtd/mtd.h>
  29. #include <linux/mtd/partitions.h>
  30. #include <linux/of.h>
  31. +#include <linux/magic.h>
  32. #include <linux/err.h>
  33. #include "mtdcore.h"
  34. +#include "mtdsplit/mtdsplit.h"
  35. /* Our partition linked list */
  36. static LIST_HEAD(mtd_partitions);
  37. @@ -46,13 +48,14 @@ struct mtd_part {
  38. struct list_head list;
  39. };
  40. +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
  41. +
  42. /*
  43. * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
  44. * the pointer to that structure with this macro.
  45. */
  46. #define PART(x) ((struct mtd_part *)(x))
  47. -
  48. /*
  49. * MTD methods which simply translate the effective address and pass through
  50. * to the _real_ device.
  51. @@ -548,8 +551,10 @@ out_register:
  52. return slave;
  53. }
  54. -int mtd_add_partition(struct mtd_info *master, const char *name,
  55. - long long offset, long long length)
  56. +
  57. +static int
  58. +__mtd_add_partition(struct mtd_info *master, const char *name,
  59. + long long offset, long long length, bool dup_check)
  60. {
  61. struct mtd_partition part;
  62. struct mtd_part *p, *new;
  63. @@ -581,21 +586,24 @@ int mtd_add_partition(struct mtd_info *m
  64. end = offset + length;
  65. mutex_lock(&mtd_partitions_mutex);
  66. - list_for_each_entry(p, &mtd_partitions, list)
  67. - if (p->master == master) {
  68. - if ((start >= p->offset) &&
  69. - (start < (p->offset + p->mtd.size)))
  70. - goto err_inv;
  71. -
  72. - if ((end >= p->offset) &&
  73. - (end < (p->offset + p->mtd.size)))
  74. - goto err_inv;
  75. - }
  76. + if (dup_check) {
  77. + list_for_each_entry(p, &mtd_partitions, list)
  78. + if (p->master == master) {
  79. + if ((start >= p->offset) &&
  80. + (start < (p->offset + p->mtd.size)))
  81. + goto err_inv;
  82. +
  83. + if ((end >= p->offset) &&
  84. + (end < (p->offset + p->mtd.size)))
  85. + goto err_inv;
  86. + }
  87. + }
  88. list_add(&new->list, &mtd_partitions);
  89. mutex_unlock(&mtd_partitions_mutex);
  90. add_mtd_device(&new->mtd);
  91. + mtd_partition_split(master, new);
  92. return ret;
  93. err_inv:
  94. @@ -605,6 +613,12 @@ err_inv:
  95. }
  96. EXPORT_SYMBOL_GPL(mtd_add_partition);
  97. +int mtd_add_partition(struct mtd_info *master, const char *name,
  98. + long long offset, long long length)
  99. +{
  100. + return __mtd_add_partition(master, name, offset, length, true);
  101. +}
  102. +
  103. int mtd_del_partition(struct mtd_info *master, int partno)
  104. {
  105. struct mtd_part *slave, *next;
  106. @@ -628,6 +642,35 @@ int mtd_del_partition(struct mtd_info *m
  107. }
  108. EXPORT_SYMBOL_GPL(mtd_del_partition);
  109. +#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
  110. +#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
  111. +#else
  112. +#define SPLIT_FIRMWARE_NAME "unused"
  113. +#endif
  114. +
  115. +static void split_firmware(struct mtd_info *master, struct mtd_part *part)
  116. +{
  117. +}
  118. +
  119. +void __weak arch_split_mtd_part(struct mtd_info *master, const char *name,
  120. + int offset, int size)
  121. +{
  122. +}
  123. +
  124. +static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
  125. +{
  126. + static int rootfs_found = 0;
  127. +
  128. + if (rootfs_found)
  129. + return;
  130. +
  131. + if (!strcmp(part->mtd.name, SPLIT_FIRMWARE_NAME) &&
  132. + config_enabled(CONFIG_MTD_SPLIT_FIRMWARE))
  133. + split_firmware(master, part);
  134. +
  135. + arch_split_mtd_part(master, part->mtd.name, part->offset,
  136. + part->mtd.size);
  137. +}
  138. /*
  139. * This function, given a master MTD object and a partition table, creates
  140. * and registers slave MTD objects which are bound to the master according to
  141. @@ -657,6 +700,7 @@ int add_mtd_partitions(struct mtd_info *
  142. mutex_unlock(&mtd_partitions_mutex);
  143. add_mtd_device(&slave->mtd);
  144. + mtd_partition_split(master, slave);
  145. cur_offset = slave->offset + slave->mtd.size;
  146. }
  147. --- a/include/linux/mtd/partitions.h
  148. +++ b/include/linux/mtd/partitions.h
  149. @@ -84,5 +84,7 @@ int mtd_add_partition(struct mtd_info *m
  150. long long offset, long long length);
  151. int mtd_del_partition(struct mtd_info *master, int partno);
  152. uint64_t mtd_get_device_size(const struct mtd_info *mtd);
  153. +extern void __weak arch_split_mtd_part(struct mtd_info *master,
  154. + const char *name, int offset, int size);
  155. #endif