mtdsplit_elf.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * MTD splitter for ELF loader firmware partitions
  4. *
  5. * Copyright (C) 2020 Sander Vanheule <[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 as published by the Free
  9. * Software Foundation; version 2.
  10. *
  11. * To parse the ELF kernel loader, a small ELF parser is used that can
  12. * handle both ELF32 or ELF64 class loaders. The splitter assumes that the
  13. * kernel is always located before the rootfs, whether it is embedded in the
  14. * loader or not.
  15. *
  16. * The kernel image is preferably embedded inside the ELF loader, so the end
  17. * of the loader equals the end of the kernel partition. This is due to the
  18. * way mtd_find_rootfs_from searches for the the rootfs:
  19. * - if the kernel image is embedded in the loader, the appended rootfs may
  20. * follow the loader immediately, within the same erase block.
  21. * - if the kernel image is not embedded in the loader, but placed at some
  22. * offset behind the loader (OKLI-style loader), the rootfs must be
  23. * aligned to an erase-block after the loader and kernel image.
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/of.h>
  32. #include <linux/byteorder/generic.h>
  33. #include "mtdsplit.h"
  34. #define ELF_NR_PARTS 2
  35. #define ELF_MAGIC 0x7f454c46 /* 0x7f E L F */
  36. #define ELF_CLASS_32 1
  37. #define ELF_CLASS_64 2
  38. struct elf_header_ident {
  39. uint32_t magic;
  40. uint8_t class;
  41. uint8_t data;
  42. uint8_t version;
  43. uint8_t osabi;
  44. uint8_t abiversion;
  45. uint8_t pad[7];
  46. };
  47. struct elf_header_32 {
  48. uint16_t type;
  49. uint16_t machine;
  50. uint32_t version;
  51. uint32_t entry;
  52. uint32_t phoff;
  53. uint32_t shoff;
  54. uint32_t flags;
  55. uint16_t ehsize;
  56. uint16_t phentsize;
  57. uint16_t phnum;
  58. uint16_t shentsize;
  59. uint16_t shnum;
  60. uint16_t shstrndx;
  61. };
  62. struct elf_header_64 {
  63. uint16_t type;
  64. uint16_t machine;
  65. uint32_t version;
  66. uint64_t entry;
  67. uint64_t phoff;
  68. uint64_t shoff;
  69. uint32_t flags;
  70. uint16_t ehsize;
  71. uint16_t phentsize;
  72. uint16_t phnum;
  73. uint16_t shentsize;
  74. uint16_t shnum;
  75. uint16_t shstrndx;
  76. };
  77. struct elf_header {
  78. struct elf_header_ident ident;
  79. union {
  80. struct elf_header_32 elf32;
  81. struct elf_header_64 elf64;
  82. };
  83. };
  84. struct elf_program_header_32 {
  85. uint32_t type;
  86. uint32_t offset;
  87. uint32_t vaddr;
  88. uint32_t paddr;
  89. uint32_t filesize;
  90. uint32_t memsize;
  91. uint32_t flags;
  92. };
  93. struct elf_program_header_64 {
  94. uint32_t type;
  95. uint32_t flags;
  96. uint64_t offset;
  97. uint64_t vaddr;
  98. uint64_t paddr;
  99. uint64_t filesize;
  100. uint64_t memsize;
  101. };
  102. static int mtdsplit_elf_read_mtd(struct mtd_info *mtd, size_t offset,
  103. uint8_t *dst, size_t len)
  104. {
  105. size_t retlen;
  106. int ret;
  107. ret = mtd_read(mtd, offset, len, &retlen, dst);
  108. if (ret) {
  109. pr_debug("read error in \"%s\"\n", mtd->name);
  110. return ret;
  111. }
  112. if (retlen != len) {
  113. pr_debug("short read in \"%s\"\n", mtd->name);
  114. return -EIO;
  115. }
  116. return 0;
  117. }
  118. static int elf32_determine_size(struct mtd_info *mtd, struct elf_header *hdr,
  119. size_t *size)
  120. {
  121. struct elf_header_32 *hdr32 = &(hdr->elf32);
  122. int err;
  123. size_t section_end, ph_table_end, ph_entry;
  124. struct elf_program_header_32 ph;
  125. *size = 0;
  126. if (hdr32->shoff > 0) {
  127. *size = hdr32->shoff + hdr32->shentsize * hdr32->shnum;
  128. return 0;
  129. }
  130. ph_entry = hdr32->phoff;
  131. ph_table_end = hdr32->phoff + hdr32->phentsize * hdr32->phnum;
  132. while (ph_entry < ph_table_end) {
  133. err = mtdsplit_elf_read_mtd(mtd, ph_entry, (uint8_t *)(&ph),
  134. sizeof(ph));
  135. if (err)
  136. return err;
  137. section_end = ph.offset + ph.filesize;
  138. if (section_end > *size)
  139. *size = section_end;
  140. ph_entry += hdr32->phentsize;
  141. }
  142. return 0;
  143. }
  144. static int elf64_determine_size(struct mtd_info *mtd, struct elf_header *hdr,
  145. size_t *size)
  146. {
  147. struct elf_header_64 *hdr64 = &(hdr->elf64);
  148. int err;
  149. size_t section_end, ph_table_end, ph_entry;
  150. struct elf_program_header_64 ph;
  151. *size = 0;
  152. if (hdr64->shoff > 0) {
  153. *size = hdr64->shoff + hdr64->shentsize * hdr64->shnum;
  154. return 0;
  155. }
  156. ph_entry = hdr64->phoff;
  157. ph_table_end = hdr64->phoff + hdr64->phentsize * hdr64->phnum;
  158. while (ph_entry < ph_table_end) {
  159. err = mtdsplit_elf_read_mtd(mtd, ph_entry, (uint8_t *)(&ph),
  160. sizeof(ph));
  161. if (err)
  162. return err;
  163. section_end = ph.offset + ph.filesize;
  164. if (section_end > *size)
  165. *size = section_end;
  166. ph_entry += hdr64->phentsize;
  167. }
  168. return 0;
  169. }
  170. static int mtdsplit_parse_elf(struct mtd_info *mtd,
  171. const struct mtd_partition **pparts,
  172. struct mtd_part_parser_data *data)
  173. {
  174. struct elf_header hdr;
  175. size_t loader_size, rootfs_offset;
  176. enum mtdsplit_part_type type;
  177. struct mtd_partition *parts;
  178. int err;
  179. err = mtdsplit_elf_read_mtd(mtd, 0, (uint8_t *)&hdr, sizeof(hdr));
  180. if (err)
  181. return err;
  182. if (be32_to_cpu(hdr.ident.magic) != ELF_MAGIC) {
  183. pr_debug("invalid ELF magic %08x\n",
  184. be32_to_cpu(hdr.ident.magic));
  185. return -EINVAL;
  186. }
  187. switch (hdr.ident.class) {
  188. case ELF_CLASS_32:
  189. err = elf32_determine_size(mtd, &hdr, &loader_size);
  190. break;
  191. case ELF_CLASS_64:
  192. err = elf64_determine_size(mtd, &hdr, &loader_size);
  193. break;
  194. default:
  195. pr_debug("invalid ELF class %i\n", hdr.ident.class);
  196. err = -EINVAL;
  197. }
  198. if (err)
  199. return err;
  200. err = mtd_find_rootfs_from(mtd, loader_size, mtd->size,
  201. &rootfs_offset, &type);
  202. if (err)
  203. return err;
  204. if (rootfs_offset == mtd->size) {
  205. pr_debug("no rootfs found in \"%s\"\n", mtd->name);
  206. return -ENODEV;
  207. }
  208. parts = kzalloc(ELF_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  209. if (!parts)
  210. return -ENOMEM;
  211. parts[0].name = KERNEL_PART_NAME;
  212. parts[0].offset = 0;
  213. parts[0].size = rootfs_offset;
  214. if (type == MTDSPLIT_PART_TYPE_UBI)
  215. parts[1].name = UBI_PART_NAME;
  216. else
  217. parts[1].name = ROOTFS_PART_NAME;
  218. parts[1].offset = rootfs_offset;
  219. parts[1].size = mtd->size - rootfs_offset;
  220. *pparts = parts;
  221. return ELF_NR_PARTS;
  222. }
  223. static const struct of_device_id mtdsplit_elf_of_match_table[] = {
  224. { .compatible = "openwrt,elf" },
  225. {},
  226. };
  227. MODULE_DEVICE_TABLE(of, mtdsplit_elf_of_match_table);
  228. static struct mtd_part_parser mtdsplit_elf_parser = {
  229. .owner = THIS_MODULE,
  230. .name = "elf-loader-fw",
  231. .of_match_table = mtdsplit_elf_of_match_table,
  232. .parse_fn = mtdsplit_parse_elf,
  233. .type = MTD_PARSER_TYPE_FIRMWARE,
  234. };
  235. static int __init mtdsplit_elf_init(void)
  236. {
  237. register_mtd_parser(&mtdsplit_elf_parser);
  238. return 0;
  239. }
  240. subsys_initcall(mtdsplit_elf_init);