430-mtd-add-myloader-partition-parser.patch 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. From: Florian Fainelli <[email protected]>
  2. Subject: Add myloader partition table parser
  3. [[email protected]: shoud be upstreamable]
  4. lede-commit: d8bf22859b51faa09d22c056fe221a45d2f7a3b8
  5. Signed-off-by: Florian Fainelli <[email protected]>
  6. [adjust for kernel 5.4, add myloader.c to patch]
  7. Signed-off-by: Adrian Schmutzler <[email protected]>
  8. --- a/drivers/mtd/parsers/Kconfig
  9. +++ b/drivers/mtd/parsers/Kconfig
  10. @@ -67,6 +67,22 @@ config MTD_CMDLINE_PARTS
  11. If unsure, say 'N'.
  12. +config MTD_MYLOADER_PARTS
  13. + tristate "MyLoader partition parsing"
  14. + depends on ADM5120 || ATH79
  15. + help
  16. + MyLoader is a bootloader which allows the user to define partitions
  17. + in flash devices, by putting a table in the second erase block
  18. + on the device, similar to a partition table. This table gives the
  19. + offsets and lengths of the user defined partitions.
  20. +
  21. + If you need code which can detect and parse these tables, and
  22. + register MTD 'partitions' corresponding to each image detected,
  23. + enable this option.
  24. +
  25. + You will still need the parsing functions to be called by the driver
  26. + for your particular device. It won't happen automatically.
  27. +
  28. config MTD_OF_PARTS
  29. tristate "OpenFirmware (device tree) partitioning parser"
  30. default y
  31. --- a/drivers/mtd/parsers/Makefile
  32. +++ b/drivers/mtd/parsers/Makefile
  33. @@ -4,6 +4,7 @@ obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm4
  34. obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o
  35. obj-$(CONFIG_MTD_BRCM_U_BOOT) += brcm_u-boot.o
  36. obj-$(CONFIG_MTD_CMDLINE_PARTS) += cmdlinepart.o
  37. +obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
  38. obj-$(CONFIG_MTD_OF_PARTS) += ofpart.o
  39. ofpart-y += ofpart_core.o
  40. ofpart-$(CONFIG_MTD_OF_PARTS_BCM4908) += ofpart_bcm4908.o
  41. --- /dev/null
  42. +++ b/drivers/mtd/parsers/myloader.c
  43. @@ -0,0 +1,181 @@
  44. +/*
  45. + * Parse MyLoader-style flash partition tables and produce a Linux partition
  46. + * array to match.
  47. + *
  48. + * Copyright (C) 2007-2009 Gabor Juhos <[email protected]>
  49. + *
  50. + * This file was based on drivers/mtd/redboot.c
  51. + * Author: Red Hat, Inc. - David Woodhouse <[email protected]>
  52. + *
  53. + * This program is free software; you can redistribute it and/or modify it
  54. + * under the terms of the GNU General Public License version 2 as published
  55. + * by the Free Software Foundation.
  56. + */
  57. +
  58. +#include <linux/kernel.h>
  59. +#include <linux/module.h>
  60. +#include <linux/version.h>
  61. +#include <linux/slab.h>
  62. +#include <linux/init.h>
  63. +#include <linux/vmalloc.h>
  64. +#include <linux/mtd/mtd.h>
  65. +#include <linux/mtd/partitions.h>
  66. +#include <linux/byteorder/generic.h>
  67. +#include <linux/myloader.h>
  68. +
  69. +#define BLOCK_LEN_MIN 0x10000
  70. +#define PART_NAME_LEN 32
  71. +
  72. +struct part_data {
  73. + struct mylo_partition_table tab;
  74. + char names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
  75. +};
  76. +
  77. +static int myloader_parse_partitions(struct mtd_info *master,
  78. + const struct mtd_partition **pparts,
  79. + struct mtd_part_parser_data *data)
  80. +{
  81. + struct part_data *buf;
  82. + struct mylo_partition_table *tab;
  83. + struct mylo_partition *part;
  84. + struct mtd_partition *mtd_parts;
  85. + struct mtd_partition *mtd_part;
  86. + int num_parts;
  87. + int ret, i;
  88. + size_t retlen;
  89. + char *names;
  90. + unsigned long offset;
  91. + unsigned long blocklen;
  92. +
  93. + buf = vmalloc(sizeof(*buf));
  94. + if (!buf) {
  95. + return -ENOMEM;
  96. + goto out;
  97. + }
  98. + tab = &buf->tab;
  99. +
  100. + blocklen = master->erasesize;
  101. + if (blocklen < BLOCK_LEN_MIN)
  102. + blocklen = BLOCK_LEN_MIN;
  103. +
  104. + offset = blocklen;
  105. +
  106. + /* Find the partition table */
  107. + for (i = 0; i < 4; i++, offset += blocklen) {
  108. + printk(KERN_DEBUG "%s: searching for MyLoader partition table"
  109. + " at offset 0x%lx\n", master->name, offset);
  110. +
  111. + ret = mtd_read(master, offset, sizeof(*buf), &retlen,
  112. + (void *)buf);
  113. + if (ret)
  114. + goto out_free_buf;
  115. +
  116. + if (retlen != sizeof(*buf)) {
  117. + ret = -EIO;
  118. + goto out_free_buf;
  119. + }
  120. +
  121. + /* Check for Partition Table magic number */
  122. + if (tab->magic == le32_to_cpu(MYLO_MAGIC_PARTITIONS))
  123. + break;
  124. +
  125. + }
  126. +
  127. + if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
  128. + printk(KERN_DEBUG "%s: no MyLoader partition table found\n",
  129. + master->name);
  130. + ret = 0;
  131. + goto out_free_buf;
  132. + }
  133. +
  134. + /* The MyLoader and the Partition Table is always present */
  135. + num_parts = 2;
  136. +
  137. + /* Detect number of used partitions */
  138. + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
  139. + part = &tab->partitions[i];
  140. +
  141. + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
  142. + continue;
  143. +
  144. + num_parts++;
  145. + }
  146. +
  147. + mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
  148. + num_parts * PART_NAME_LEN), GFP_KERNEL);
  149. +
  150. + if (!mtd_parts) {
  151. + ret = -ENOMEM;
  152. + goto out_free_buf;
  153. + }
  154. +
  155. + mtd_part = mtd_parts;
  156. + names = (char *)&mtd_parts[num_parts];
  157. +
  158. + strncpy(names, "myloader", PART_NAME_LEN);
  159. + mtd_part->name = names;
  160. + mtd_part->offset = 0;
  161. + mtd_part->size = offset;
  162. + mtd_part->mask_flags = MTD_WRITEABLE;
  163. + mtd_part++;
  164. + names += PART_NAME_LEN;
  165. +
  166. + strncpy(names, "partition_table", PART_NAME_LEN);
  167. + mtd_part->name = names;
  168. + mtd_part->offset = offset;
  169. + mtd_part->size = blocklen;
  170. + mtd_part->mask_flags = MTD_WRITEABLE;
  171. + mtd_part++;
  172. + names += PART_NAME_LEN;
  173. +
  174. + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
  175. + part = &tab->partitions[i];
  176. +
  177. + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
  178. + continue;
  179. +
  180. + if ((buf->names[i][0]) && (buf->names[i][0] != '\xff'))
  181. + strncpy(names, buf->names[i], PART_NAME_LEN);
  182. + else
  183. + snprintf(names, PART_NAME_LEN, "partition%d", i);
  184. +
  185. + mtd_part->offset = le32_to_cpu(part->addr);
  186. + mtd_part->size = le32_to_cpu(part->size);
  187. + mtd_part->name = names;
  188. + mtd_part++;
  189. + names += PART_NAME_LEN;
  190. + }
  191. +
  192. + *pparts = mtd_parts;
  193. + ret = num_parts;
  194. +
  195. + out_free_buf:
  196. + vfree(buf);
  197. + out:
  198. + return ret;
  199. +}
  200. +
  201. +static struct mtd_part_parser myloader_mtd_parser = {
  202. + .owner = THIS_MODULE,
  203. + .parse_fn = myloader_parse_partitions,
  204. + .name = "MyLoader",
  205. +};
  206. +
  207. +static int __init myloader_mtd_parser_init(void)
  208. +{
  209. + register_mtd_parser(&myloader_mtd_parser);
  210. +
  211. + return 0;
  212. +}
  213. +
  214. +static void __exit myloader_mtd_parser_exit(void)
  215. +{
  216. + deregister_mtd_parser(&myloader_mtd_parser);
  217. +}
  218. +
  219. +module_init(myloader_mtd_parser_init);
  220. +module_exit(myloader_mtd_parser_exit);
  221. +
  222. +MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  223. +MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
  224. +MODULE_LICENSE("GPL v2");