kernel: allow skipping deprecated uImage.FIT partition parser
[openwrt/staging/dangole.git] / target / linux / mediatek / files / block / partitions / fit.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * fs/partitions/fit.c
4 * Copyright (C) 2021 Daniel Golle
5 *
6 * headers extracted from U-Boot mkimage sources
7 * (C) Copyright 2008 Semihalf
8 * (C) Copyright 2000-2005
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 *
11 * based on existing partition parsers
12 * Copyright (C) 1991-1998 Linus Torvalds
13 * Re-organised Feb 1998 Russell King
14 */
15
16 #define pr_fmt(fmt) fmt
17
18 #include <linux/types.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/of_fdt.h>
22 #include <linux/libfdt.h>
23 #include <linux/version.h>
24
25 #include "check.h"
26
27 #define FIT_IMAGES_PATH "/images"
28 #define FIT_CONFS_PATH "/configurations"
29
30 /* hash/signature/key node */
31 #define FIT_HASH_NODENAME "hash"
32 #define FIT_ALGO_PROP "algo"
33 #define FIT_VALUE_PROP "value"
34 #define FIT_IGNORE_PROP "uboot-ignore"
35 #define FIT_SIG_NODENAME "signature"
36 #define FIT_KEY_REQUIRED "required"
37 #define FIT_KEY_HINT "key-name-hint"
38
39 /* cipher node */
40 #define FIT_CIPHER_NODENAME "cipher"
41 #define FIT_ALGO_PROP "algo"
42
43 /* image node */
44 #define FIT_DATA_PROP "data"
45 #define FIT_DATA_POSITION_PROP "data-position"
46 #define FIT_DATA_OFFSET_PROP "data-offset"
47 #define FIT_DATA_SIZE_PROP "data-size"
48 #define FIT_TIMESTAMP_PROP "timestamp"
49 #define FIT_DESC_PROP "description"
50 #define FIT_ARCH_PROP "arch"
51 #define FIT_TYPE_PROP "type"
52 #define FIT_OS_PROP "os"
53 #define FIT_COMP_PROP "compression"
54 #define FIT_ENTRY_PROP "entry"
55 #define FIT_LOAD_PROP "load"
56
57 /* configuration node */
58 #define FIT_KERNEL_PROP "kernel"
59 #define FIT_FILESYSTEM_PROP "filesystem"
60 #define FIT_RAMDISK_PROP "ramdisk"
61 #define FIT_FDT_PROP "fdt"
62 #define FIT_LOADABLE_PROP "loadables"
63 #define FIT_DEFAULT_PROP "default"
64 #define FIT_SETUP_PROP "setup"
65 #define FIT_FPGA_PROP "fpga"
66 #define FIT_FIRMWARE_PROP "firmware"
67 #define FIT_STANDALONE_PROP "standalone"
68
69 #define FIT_MAX_HASH_LEN HASH_MAX_DIGEST_SIZE
70
71 #define MIN_FREE_SECT 16
72 #define REMAIN_VOLNAME "rootfs_data"
73
74 int parse_fit_partitions(struct parsed_partitions *state, u64 fit_start_sector, u64 sectors, int *slot, int add_remain)
75 {
76 struct block_device *bdev = state->disk->part0;
77 struct address_space *mapping = bdev->bd_inode->i_mapping;
78 struct page *page;
79 void *fit, *init_fit;
80 struct partition_meta_info *info;
81 char tmp[sizeof(info->volname)];
82 u64 dsize, dsectors, imgmaxsect = 0;
83 u32 size, image_pos, image_len;
84 const u32 *image_offset_be, *image_len_be, *image_pos_be;
85 int ret = 1, node, images, config;
86 const char *image_name, *image_type, *image_description, *config_default,
87 *config_description, *config_loadables, *bootconf_c;
88 int image_name_len, image_type_len, image_description_len, config_default_len,
89 config_description_len, config_loadables_len, bootconf_len;
90 sector_t start_sect, nr_sects;
91 size_t label_min;
92 struct device_node *np = NULL;
93 char *bootconf = NULL, *bootconf_term;
94 const char *loadable;
95 const char *select_rootfs = NULL;
96 bool found;
97 int loadables_rem_len, loadable_len;
98
99 if (fit_start_sector % (1<<(PAGE_SHIFT - SECTOR_SHIFT)))
100 return -ERANGE;
101
102 page = read_mapping_page(mapping, fit_start_sector >> (PAGE_SHIFT - SECTOR_SHIFT), NULL);
103 if (IS_ERR(page))
104 return -EFAULT;
105
106 if (PageError(page))
107 return -EFAULT;
108
109 init_fit = page_address(page);
110
111 if (!init_fit) {
112 put_page(page);
113 return -EFAULT;
114 }
115
116 if (fdt_check_header(init_fit)) {
117 put_page(page);
118 return 0;
119 }
120
121 dsectors = get_capacity(bdev->bd_disk);
122 if (sectors)
123 dsectors = (dsectors>sectors)?sectors:dsectors;
124
125 dsize = dsectors << SECTOR_SHIFT;
126 size = fdt_totalsize(init_fit);
127
128 /* silently skip non-external-data legacy FIT images */
129 if (size > PAGE_SIZE) {
130 put_page(page);
131 return 0;
132 }
133
134 if (size >= dsize) {
135 state->access_beyond_eod = 1;
136 put_page(page);
137 return -EFBIG;
138 }
139
140 fit = kmemdup(init_fit, size, GFP_KERNEL);
141 put_page(page);
142 if (!fit)
143 return -ENOMEM;
144
145 np = of_find_node_by_path("/chosen");
146 if (np) {
147 /* new fitblk driver should take over if /chosen/rootdisk is defined */
148 if (of_get_property(np, "rootdisk", NULL))
149 return 0;
150
151 bootconf_c = of_get_property(np, "u-boot,bootconf", &bootconf_len);
152 if (bootconf_c && bootconf_len)
153 bootconf = kmemdup_nul(bootconf_c, bootconf_len, GFP_KERNEL);
154 }
155
156 if (bootconf) {
157 bootconf_term = strchr(bootconf, '#');
158 if (bootconf_term)
159 *bootconf_term = '\0';
160 }
161
162 config = fdt_path_offset(fit, FIT_CONFS_PATH);
163 if (config < 0) {
164 printk(KERN_ERR "FIT: Cannot find %s node: %d\n", FIT_CONFS_PATH, images);
165 ret = -ENOENT;
166 goto ret_out;
167 }
168
169 config_default = fdt_getprop(fit, config, FIT_DEFAULT_PROP, &config_default_len);
170
171 if (!config_default && !bootconf) {
172 printk(KERN_ERR "FIT: Cannot find default configuration\n");
173 ret = -ENOENT;
174 goto ret_out;
175 }
176
177 node = fdt_subnode_offset(fit, config, bootconf?:config_default);
178 if (node < 0) {
179 printk(KERN_ERR "FIT: Cannot find %s node: %d\n", bootconf?:config_default, node);
180 ret = -ENOENT;
181 goto ret_out;
182 }
183
184 config_description = fdt_getprop(fit, node, FIT_DESC_PROP, &config_description_len);
185 config_loadables = fdt_getprop(fit, node, FIT_LOADABLE_PROP, &config_loadables_len);
186
187 printk(KERN_DEBUG "FIT: %s configuration: \"%s\"%s%s%s\n",
188 bootconf?"Selected":"Default", bootconf?:config_default,
189 config_description?" (":"", config_description?:"", config_description?")":"");
190
191 if (!config_loadables || !config_loadables_len) {
192 printk(KERN_ERR "FIT: No loadables configured in \"%s\"\n", bootconf?:config_default);
193 ret = -ENOENT;
194 goto ret_out;
195 }
196
197 images = fdt_path_offset(fit, FIT_IMAGES_PATH);
198 if (images < 0) {
199 printk(KERN_ERR "FIT: Cannot find %s node: %d\n", FIT_IMAGES_PATH, images);
200 ret = -EINVAL;
201 goto ret_out;
202 }
203
204 fdt_for_each_subnode(node, fit, images) {
205 image_name = fdt_get_name(fit, node, &image_name_len);
206 image_type = fdt_getprop(fit, node, FIT_TYPE_PROP, &image_type_len);
207 image_offset_be = fdt_getprop(fit, node, FIT_DATA_OFFSET_PROP, NULL);
208 image_pos_be = fdt_getprop(fit, node, FIT_DATA_POSITION_PROP, NULL);
209 image_len_be = fdt_getprop(fit, node, FIT_DATA_SIZE_PROP, NULL);
210 if (!image_name || !image_type || !image_len_be)
211 continue;
212
213 image_len = be32_to_cpu(*image_len_be);
214 if (!image_len)
215 continue;
216
217 if (image_offset_be)
218 image_pos = be32_to_cpu(*image_offset_be) + size;
219 else if (image_pos_be)
220 image_pos = be32_to_cpu(*image_pos_be);
221 else
222 continue;
223
224 image_description = fdt_getprop(fit, node, FIT_DESC_PROP, &image_description_len);
225
226 printk(KERN_DEBUG "FIT: %16s sub-image 0x%08x..0x%08x \"%s\" %s%s%s\n",
227 image_type, image_pos, image_pos + image_len - 1, image_name,
228 image_description?"(":"", image_description?:"", image_description?") ":"");
229
230 if (strcmp(image_type, FIT_FILESYSTEM_PROP))
231 continue;
232
233 /* check if sub-image is part of configured loadables */
234 found = false;
235 loadable = config_loadables;
236 loadables_rem_len = config_loadables_len;
237 while (loadables_rem_len > 1) {
238 loadable_len = strnlen(loadable, loadables_rem_len - 1) + 1;
239 loadables_rem_len -= loadable_len;
240 if (!strncmp(image_name, loadable, loadable_len)) {
241 found = true;
242 break;
243 }
244 loadable += loadable_len;
245 }
246 if (!found)
247 continue;
248
249 if (image_pos & ((1 << PAGE_SHIFT)-1)) {
250 printk(KERN_ERR "FIT: image %s start not aligned to page boundaries, skipping\n", image_name);
251 continue;
252 }
253
254 if (image_len & ((1 << PAGE_SHIFT)-1)) {
255 printk(KERN_ERR "FIT: sub-image %s end not aligned to page boundaries, skipping\n", image_name);
256 continue;
257 }
258
259 start_sect = image_pos >> SECTOR_SHIFT;
260 nr_sects = image_len >> SECTOR_SHIFT;
261 imgmaxsect = (imgmaxsect < (start_sect + nr_sects))?(start_sect + nr_sects):imgmaxsect;
262
263 if (start_sect + nr_sects > dsectors) {
264 state->access_beyond_eod = 1;
265 continue;
266 }
267
268 put_partition(state, ++(*slot), fit_start_sector + start_sect, nr_sects);
269 state->parts[*slot].flags = ADDPART_FLAG_READONLY;
270 state->parts[*slot].has_info = true;
271 info = &state->parts[*slot].info;
272
273 label_min = min_t(int, sizeof(info->volname) - 1, image_name_len);
274 strncpy(info->volname, image_name, label_min);
275 info->volname[label_min] = '\0';
276
277 snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
278 strlcat(state->pp_buf, tmp, PAGE_SIZE);
279
280 /* Mark first loadable listed to be mounted as rootfs */
281 if (!strcmp(image_name, config_loadables)) {
282 select_rootfs = image_name;
283 state->parts[*slot].flags |= ADDPART_FLAG_ROOTDEV;
284 }
285 }
286
287 if (select_rootfs)
288 printk(KERN_DEBUG "FIT: selecting configured loadable \"%s\" to be root filesystem\n", select_rootfs);
289
290 if (add_remain && (imgmaxsect + MIN_FREE_SECT) < dsectors) {
291 put_partition(state, ++(*slot), fit_start_sector + imgmaxsect, dsectors - imgmaxsect);
292 state->parts[*slot].flags = 0;
293 info = &state->parts[*slot].info;
294 strcpy(info->volname, REMAIN_VOLNAME);
295 snprintf(tmp, sizeof(tmp), "(%s)", REMAIN_VOLNAME);
296 strlcat(state->pp_buf, tmp, PAGE_SIZE);
297 }
298 ret_out:
299 kfree(bootconf);
300 kfree(fit);
301 return ret;
302 }
303
304 int fit_partition(struct parsed_partitions *state) {
305 int slot = 0;
306 return parse_fit_partitions(state, 0, 0, &slot, 0);
307 }