routerbootpart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Parser for MikroTik RouterBoot partitions.
  4. *
  5. * Copyright (C) 2020 Thibaut VARÈNE <[email protected]>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * This parser builds from the "fixed-partitions" one (see ofpart.c), but it can
  12. * handle dynamic partitions as found on routerboot devices.
  13. *
  14. * DTS nodes are defined as follows:
  15. * For fixed partitions:
  16. * node-name@unit-address {
  17. * reg = <prop-encoded-array>;
  18. * label = <string>;
  19. * read-only;
  20. * lock;
  21. * };
  22. *
  23. * reg property is mandatory; other properties are optional.
  24. * reg format is <address length>. length can be 0 if the next partition is
  25. * another fixed partition or a "well-known" partition as defined below: in that
  26. * case the partition will extend up to the next one.
  27. *
  28. * For dynamic partitions:
  29. * node-name {
  30. * size = <prop-encoded-array>;
  31. * label = <string>;
  32. * read-only;
  33. * lock;
  34. * };
  35. *
  36. * size property is normally mandatory. It can only be omitted (or set to 0) if:
  37. * - the partition is a "well-known" one (as defined below), in which case
  38. * the partition size will be automatically adjusted; or
  39. * - the next partition is a fixed one or a "well-known" one, in which case
  40. * the current partition will extend up to the next one.
  41. * Other properties are optional.
  42. * size format is <length>.
  43. * By default dynamic partitions are appended after the preceding one, except
  44. * for "well-known" ones which are automatically located on flash.
  45. *
  46. * Well-known partitions (matched via label or node-name):
  47. * - "hard_config"
  48. * - "soft_config"
  49. * - "dtb_config"
  50. *
  51. * Note: this parser will happily register 0-sized partitions if misused.
  52. *
  53. * This parser requires the DTS to list partitions in ascending order as
  54. * expected on the MTD device.
  55. *
  56. * Since only the "hard_config" and "soft_config" partitions are used in OpenWRT,
  57. * a minimal working DTS could define only these two partitions dynamically (in
  58. * the right order, usually hard_config then soft_config).
  59. *
  60. * Note: some mips RB devices encode the hard_config offset and length in two
  61. * consecutive u32 located at offset 0x14 (for ramips) or 0x24 (for ath79) on
  62. * the SPI NOR flash. Unfortunately this seems inconsistent across machines and
  63. * does not apply to e.g. ipq-based ones, so we ignore that information.
  64. *
  65. * Note: To find well-known partitions, this parser will go through the entire
  66. * top mtd partition parsed, _before_ the DTS nodes are processed. This works
  67. * well in the current state of affairs, and is a simpler implementation than
  68. * searching for known partitions in the "holes" left between fixed-partition,
  69. * _after_ processing DTS nodes.
  70. */
  71. #include <linux/module.h>
  72. #include <linux/slab.h>
  73. #include <linux/mtd/mtd.h>
  74. #include <linux/mtd/partitions.h>
  75. #include <linux/of.h>
  76. #include <linux/of_fdt.h>
  77. #include <linux/libfdt_env.h>
  78. #include <linux/string.h>
  79. #define RB_MAGIC_HARD (('H') | ('a' << 8) | ('r' << 16) | ('d' << 24))
  80. #define RB_MAGIC_SOFT (('S') | ('o' << 8) | ('f' << 16) | ('t' << 24))
  81. #define RB_BLOCK_SIZE 0x1000
  82. struct routerboot_dynpart {
  83. const char * const name;
  84. const u32 magic;
  85. int (* const size_fixup)(struct mtd_info *, struct routerboot_dynpart *);
  86. size_t offset;
  87. size_t size;
  88. bool found;
  89. };
  90. static int routerboot_dtbsfixup(struct mtd_info *, struct routerboot_dynpart *);
  91. static struct routerboot_dynpart rb_dynparts[] = {
  92. {
  93. .name = "hard_config",
  94. .magic = RB_MAGIC_HARD, // stored in CPU-endianness on flash
  95. .size_fixup = NULL,
  96. .offset = 0x0,
  97. .size = RB_BLOCK_SIZE,
  98. .found = false,
  99. }, {
  100. .name = "soft_config",
  101. .magic = RB_MAGIC_SOFT, // stored in CPU-endianness on flash
  102. .size_fixup = NULL,
  103. .offset = 0x0,
  104. .size = RB_BLOCK_SIZE,
  105. .found = false,
  106. }, {
  107. .name = "dtb_config",
  108. .magic = fdt32_to_cpu(OF_DT_HEADER), // stored BE on flash
  109. .size_fixup = routerboot_dtbsfixup,
  110. .offset = 0x0,
  111. .size = 0x0,
  112. .found = false,
  113. }
  114. };
  115. static int routerboot_dtbsfixup(struct mtd_info *master, struct routerboot_dynpart *rbdpart)
  116. {
  117. int err;
  118. size_t bytes_read, psize;
  119. struct {
  120. fdt32_t magic;
  121. fdt32_t totalsize;
  122. fdt32_t off_dt_struct;
  123. fdt32_t off_dt_strings;
  124. fdt32_t off_mem_rsvmap;
  125. fdt32_t version;
  126. fdt32_t last_comp_version;
  127. fdt32_t boot_cpuid_phys;
  128. fdt32_t size_dt_strings;
  129. fdt32_t size_dt_struct;
  130. } fdt_header;
  131. err = mtd_read(master, rbdpart->offset, sizeof(fdt_header),
  132. &bytes_read, (u8 *)&fdt_header);
  133. if (err)
  134. return err;
  135. if (bytes_read != sizeof(fdt_header))
  136. return -EIO;
  137. psize = fdt32_to_cpu(fdt_header.totalsize);
  138. if (!psize)
  139. return -EINVAL;
  140. rbdpart->size = psize;
  141. return 0;
  142. }
  143. static void routerboot_find_dynparts(struct mtd_info *master)
  144. {
  145. size_t bytes_read, offset;
  146. bool allfound;
  147. int err, i;
  148. u32 buf;
  149. /*
  150. * Dynamic RouterBoot partitions offsets are aligned to RB_BLOCK_SIZE:
  151. * read the whole partition at RB_BLOCK_SIZE intervals to find sigs.
  152. * Skip partition content when possible.
  153. */
  154. offset = 0;
  155. while (offset < master->size) {
  156. err = mtd_read(master, offset, sizeof(buf), &bytes_read, (u8 *)&buf);
  157. if (err) {
  158. pr_err("%s: mtd_read error while parsing (offset: 0x%zX): %d\n",
  159. master->name, offset, err);
  160. continue;
  161. }
  162. allfound = true;
  163. for (i = 0; i < ARRAY_SIZE(rb_dynparts); i++) {
  164. if (rb_dynparts[i].found)
  165. continue;
  166. allfound = false;
  167. if (rb_dynparts[i].magic == buf) {
  168. rb_dynparts[i].offset = offset;
  169. if (rb_dynparts[i].size_fixup) {
  170. err = rb_dynparts[i].size_fixup(master, &rb_dynparts[i]);
  171. if (err) {
  172. pr_err("%s: size fixup error while parsing \"%s\": %d\n",
  173. master->name, rb_dynparts[i].name, err);
  174. continue;
  175. }
  176. }
  177. rb_dynparts[i].found = true;
  178. /*
  179. * move offset to skip the whole partition on
  180. * next iteration if size > RB_BLOCK_SIZE.
  181. */
  182. if (rb_dynparts[i].size > RB_BLOCK_SIZE)
  183. offset += ALIGN_DOWN((rb_dynparts[i].size - RB_BLOCK_SIZE), RB_BLOCK_SIZE);
  184. break;
  185. }
  186. }
  187. offset += RB_BLOCK_SIZE;
  188. if (allfound)
  189. break;
  190. }
  191. }
  192. static int routerboot_partitions_parse(struct mtd_info *master,
  193. const struct mtd_partition **pparts,
  194. struct mtd_part_parser_data *data)
  195. {
  196. struct device_node *rbpart_node, *pp;
  197. struct mtd_partition *parts;
  198. const char *partname;
  199. size_t master_ofs;
  200. int np;
  201. /* Pull of_node from the master device node */
  202. rbpart_node = mtd_get_of_node(master);
  203. if (!rbpart_node)
  204. return 0;
  205. /* First count the subnodes */
  206. np = 0;
  207. for_each_child_of_node(rbpart_node, pp)
  208. np++;
  209. if (!np)
  210. return 0;
  211. parts = kcalloc(np, sizeof(*parts), GFP_KERNEL);
  212. if (!parts)
  213. return -ENOMEM;
  214. /* Preemptively look for known parts in flash */
  215. routerboot_find_dynparts(master);
  216. np = 0;
  217. master_ofs = 0;
  218. for_each_child_of_node(rbpart_node, pp) {
  219. const __be32 *reg, *sz;
  220. size_t offset, size;
  221. int i, len, a_cells, s_cells;
  222. partname = of_get_property(pp, "label", &len);
  223. /* Allow deprecated use of "name" instead of "label" */
  224. if (!partname)
  225. partname = of_get_property(pp, "name", &len);
  226. /* Fallback to node name per spec if all else fails: partname is always set */
  227. if (!partname)
  228. partname = pp->name;
  229. parts[np].name = partname;
  230. reg = of_get_property(pp, "reg", &len);
  231. if (reg) {
  232. /* Fixed partition */
  233. a_cells = of_n_addr_cells(pp);
  234. s_cells = of_n_size_cells(pp);
  235. if ((len / 4) != (a_cells + s_cells)) {
  236. pr_debug("%s: routerboot partition %pOF (%pOF) error parsing reg property.\n",
  237. master->name, pp, rbpart_node);
  238. goto rbpart_fail;
  239. }
  240. offset = of_read_number(reg, a_cells);
  241. size = of_read_number(reg + a_cells, s_cells);
  242. } else {
  243. /* Dynamic partition */
  244. /* Default: part starts at current offset, 0 size */
  245. offset = master_ofs;
  246. size = 0;
  247. /* Check if well-known partition */
  248. for (i = 0; i < ARRAY_SIZE(rb_dynparts); i++) {
  249. if (!strcmp(partname, rb_dynparts[i].name) && rb_dynparts[i].found) {
  250. offset = rb_dynparts[i].offset;
  251. size = rb_dynparts[i].size;
  252. break;
  253. }
  254. }
  255. /* Standalone 'size' property? Override size */
  256. sz = of_get_property(pp, "size", &len);
  257. if (sz) {
  258. s_cells = of_n_size_cells(pp);
  259. if ((len / 4) != s_cells) {
  260. pr_debug("%s: routerboot partition %pOF (%pOF) error parsing size property.\n",
  261. master->name, pp, rbpart_node);
  262. goto rbpart_fail;
  263. }
  264. size = of_read_number(sz, s_cells);
  265. }
  266. }
  267. if (np > 0) {
  268. /* Minor sanity check for overlaps */
  269. if (offset < (parts[np-1].offset + parts[np-1].size)) {
  270. pr_err("%s: routerboot partition %pOF (%pOF) \"%s\" overlaps with previous partition \"%s\".\n",
  271. master->name, pp, rbpart_node,
  272. partname, parts[np-1].name);
  273. goto rbpart_fail;
  274. }
  275. /* Fixup end of previous partition if necessary */
  276. if (!parts[np-1].size)
  277. parts[np-1].size = (offset - parts[np-1].offset);
  278. }
  279. if ((offset + size) > master->size) {
  280. pr_err("%s: routerboot partition %pOF (%pOF) \"%s\" extends past end of segment.\n",
  281. master->name, pp, rbpart_node, partname);
  282. goto rbpart_fail;
  283. }
  284. parts[np].offset = offset;
  285. parts[np].size = size;
  286. parts[np].of_node = pp;
  287. if (of_get_property(pp, "read-only", &len))
  288. parts[np].mask_flags |= MTD_WRITEABLE;
  289. if (of_get_property(pp, "lock", &len))
  290. parts[np].mask_flags |= MTD_POWERUP_LOCK;
  291. /* Keep master offset aligned to RB_BLOCK_SIZE */
  292. master_ofs = ALIGN(offset + size, RB_BLOCK_SIZE);
  293. np++;
  294. }
  295. *pparts = parts;
  296. return np;
  297. rbpart_fail:
  298. pr_err("%s: error parsing routerboot partition %pOF (%pOF)\n",
  299. master->name, pp, rbpart_node);
  300. of_node_put(pp);
  301. kfree(parts);
  302. return -EINVAL;
  303. }
  304. static const struct of_device_id parse_routerbootpart_match_table[] = {
  305. { .compatible = "mikrotik,routerboot-partitions" },
  306. {},
  307. };
  308. MODULE_DEVICE_TABLE(of, parse_routerbootpart_match_table);
  309. static struct mtd_part_parser routerbootpart_parser = {
  310. .parse_fn = routerboot_partitions_parse,
  311. .name = "routerbootpart",
  312. .of_match_table = parse_routerbootpart_match_table,
  313. };
  314. module_mtd_part_parser(routerbootpart_parser);
  315. MODULE_LICENSE("GPL v2");
  316. MODULE_DESCRIPTION("MTD partitioning for RouterBoot");
  317. MODULE_AUTHOR("Thibaut VARENE");