blob: 47818416f61c1ad03cfa17b68178aab78230016e [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * MTD splitter for ELF loader firmware partitions
4 *
5 * Copyright (C) 2020 Sander Vanheule <sander@svanheule.net>
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
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/kernel.h>
29#include <linux/slab.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
32#include <linux/of.h>
33#include <linux/byteorder/generic.h>
34
35#include "mtdsplit.h"
36
37#define ELF_NR_PARTS 2
38
39#define ELF_MAGIC 0x7f454c46 /* 0x7f E L F */
40#define ELF_CLASS_32 1
41#define ELF_CLASS_64 2
42
43struct elf_header_ident {
44 uint32_t magic;
45 uint8_t class;
46 uint8_t data;
47 uint8_t version;
48 uint8_t osabi;
49 uint8_t abiversion;
50 uint8_t pad[7];
51};
52
53struct elf_header_32 {
54 uint16_t type;
55 uint16_t machine;
56 uint32_t version;
57 uint32_t entry;
58 uint32_t phoff;
59 uint32_t shoff;
60 uint32_t flags;
61 uint16_t ehsize;
62 uint16_t phentsize;
63 uint16_t phnum;
64 uint16_t shentsize;
65 uint16_t shnum;
66 uint16_t shstrndx;
67};
68
69struct elf_header_64 {
70 uint16_t type;
71 uint16_t machine;
72 uint32_t version;
73 uint64_t entry;
74 uint64_t phoff;
75 uint64_t shoff;
76 uint32_t flags;
77 uint16_t ehsize;
78 uint16_t phentsize;
79 uint16_t phnum;
80 uint16_t shentsize;
81 uint16_t shnum;
82 uint16_t shstrndx;
83};
84
85struct elf_header {
86 struct elf_header_ident ident;
87 union {
88 struct elf_header_32 elf32;
89 struct elf_header_64 elf64;
90 };
91};
92
93struct elf_program_header_32 {
94 uint32_t type;
95 uint32_t offset;
96 uint32_t vaddr;
97 uint32_t paddr;
98 uint32_t filesize;
99 uint32_t memsize;
100 uint32_t flags;
101};
102
103struct elf_program_header_64 {
104 uint32_t type;
105 uint32_t flags;
106 uint64_t offset;
107 uint64_t vaddr;
108 uint64_t paddr;
109 uint64_t filesize;
110 uint64_t memsize;
111};
112
113
114static int mtdsplit_elf_read_mtd(struct mtd_info *mtd, size_t offset,
115 uint8_t *dst, size_t len)
116{
117 size_t retlen;
118 int ret;
119
120 ret = mtd_read(mtd, offset, len, &retlen, dst);
121 if (ret) {
122 pr_debug("read error in \"%s\"\n", mtd->name);
123 return ret;
124 }
125
126 if (retlen != len) {
127 pr_debug("short read in \"%s\"\n", mtd->name);
128 return -EIO;
129 }
130
131 return 0;
132}
133
134static int elf32_determine_size(struct mtd_info *mtd, struct elf_header *hdr,
135 size_t *size)
136{
137 struct elf_header_32 *hdr32 = &(hdr->elf32);
138 int err;
139 size_t section_end, ph_table_end, ph_entry;
140 struct elf_program_header_32 ph;
141
142 *size = 0;
143
144 if (hdr32->shoff > 0) {
145 *size = hdr32->shoff + hdr32->shentsize * hdr32->shnum;
146 return 0;
147 }
148
149 ph_entry = hdr32->phoff;
150 ph_table_end = hdr32->phoff + hdr32->phentsize * hdr32->phnum;
151
152 while (ph_entry < ph_table_end) {
153 err = mtdsplit_elf_read_mtd(mtd, ph_entry, (uint8_t *)(&ph),
154 sizeof(ph));
155 if (err)
156 return err;
157
158 section_end = ph.offset + ph.filesize;
159 if (section_end > *size)
160 *size = section_end;
161
162 ph_entry += hdr32->phentsize;
163 }
164
165 return 0;
166}
167
168static int elf64_determine_size(struct mtd_info *mtd, struct elf_header *hdr,
169 size_t *size)
170{
171 struct elf_header_64 *hdr64 = &(hdr->elf64);
172 int err;
173 size_t section_end, ph_table_end, ph_entry;
174 struct elf_program_header_64 ph;
175
176 *size = 0;
177
178 if (hdr64->shoff > 0) {
179 *size = hdr64->shoff + hdr64->shentsize * hdr64->shnum;
180 return 0;
181 }
182
183 ph_entry = hdr64->phoff;
184 ph_table_end = hdr64->phoff + hdr64->phentsize * hdr64->phnum;
185
186 while (ph_entry < ph_table_end) {
187 err = mtdsplit_elf_read_mtd(mtd, ph_entry, (uint8_t *)(&ph),
188 sizeof(ph));
189 if (err)
190 return err;
191
192 section_end = ph.offset + ph.filesize;
193 if (section_end > *size)
194 *size = section_end;
195
196 ph_entry += hdr64->phentsize;
197 }
198
199 return 0;
200}
201
202static int mtdsplit_parse_elf(struct mtd_info *mtd,
203 const struct mtd_partition **pparts,
204 struct mtd_part_parser_data *data)
205{
206 struct elf_header hdr;
207 size_t loader_size, rootfs_offset;
208 enum mtdsplit_part_type type;
209 struct mtd_partition *parts;
210 int err;
211
212 err = mtdsplit_elf_read_mtd(mtd, 0, (uint8_t *)&hdr, sizeof(hdr));
213 if (err)
214 return err;
215
216 if (be32_to_cpu(hdr.ident.magic) != ELF_MAGIC) {
217 pr_debug("invalid ELF magic %08x\n",
218 be32_to_cpu(hdr.ident.magic));
219 return -EINVAL;
220 }
221
222 switch (hdr.ident.class) {
223 case ELF_CLASS_32:
224 err = elf32_determine_size(mtd, &hdr, &loader_size);
225 break;
226 case ELF_CLASS_64:
227 err = elf64_determine_size(mtd, &hdr, &loader_size);
228 break;
229 default:
230 pr_debug("invalid ELF class %i\n", hdr.ident.class);
231 err = -EINVAL;
232 }
233
234 if (err)
235 return err;
236
237 err = mtd_find_rootfs_from(mtd, loader_size, mtd->size,
238 &rootfs_offset, &type);
239 if (err)
240 return err;
241
242 if (rootfs_offset == mtd->size) {
243 pr_debug("no rootfs found in \"%s\"\n", mtd->name);
244 return -ENODEV;
245 }
246
247 parts = kzalloc(ELF_NR_PARTS * sizeof(*parts), GFP_KERNEL);
248 if (!parts)
249 return -ENOMEM;
250
251 parts[0].name = KERNEL_PART_NAME;
252 parts[0].offset = 0;
253 parts[0].size = rootfs_offset;
254
255 if (type == MTDSPLIT_PART_TYPE_UBI)
256 parts[1].name = UBI_PART_NAME;
257 else
258 parts[1].name = ROOTFS_PART_NAME;
259 parts[1].offset = rootfs_offset;
260 parts[1].size = mtd->size - rootfs_offset;
261
262 *pparts = parts;
263 return ELF_NR_PARTS;
264}
265
266static const struct of_device_id mtdsplit_elf_of_match_table[] = {
267 { .compatible = "openwrt,elf" },
268 {},
269};
270MODULE_DEVICE_TABLE(of, mtdsplit_elf_of_match_table);
271
272static struct mtd_part_parser mtdsplit_elf_parser = {
273 .owner = THIS_MODULE,
274 .name = "elf-loader-fw",
275 .of_match_table = mtdsplit_elf_of_match_table,
276 .parse_fn = mtdsplit_parse_elf,
277 .type = MTD_PARSER_TYPE_FIRMWARE,
278};
279
280static int __init mtdsplit_elf_init(void)
281{
282 register_mtd_parser(&mtdsplit_elf_parser);
283
284 return 0;
285}
286
287subsys_initcall(mtdsplit_elf_init);