OpenWrt – Blame information for rev 3
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * Copyright (c) 2015 The Linux Foundation |
||
3 | * Copyright (C) 2014 Gabor Juhos <juhosg@openwrt.org> |
||
4 | * |
||
5 | * Permission to use, copy, modify, and/or distribute this software for any |
||
6 | * purpose with or without fee is hereby granted, provided that the above |
||
7 | * copyright notice and this permission notice appear in all copies. |
||
8 | * |
||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
||
16 | */ |
||
17 | |||
18 | #include <linux/module.h> |
||
19 | #include <linux/mtd/mtd.h> |
||
20 | #include <linux/mtd/partitions.h> |
||
21 | #include <linux/types.h> |
||
22 | #include <linux/byteorder/generic.h> |
||
23 | #include <linux/slab.h> |
||
24 | #include <linux/of_fdt.h> |
||
25 | |||
26 | #include "mtdsplit.h" |
||
27 | |||
28 | struct fdt_header { |
||
29 | uint32_t magic; /* magic word FDT_MAGIC */ |
||
30 | uint32_t totalsize; /* total size of DT block */ |
||
31 | uint32_t off_dt_struct; /* offset to structure */ |
||
32 | uint32_t off_dt_strings; /* offset to strings */ |
||
33 | uint32_t off_mem_rsvmap; /* offset to memory reserve map */ |
||
34 | uint32_t version; /* format version */ |
||
35 | uint32_t last_comp_version; /* last compatible version */ |
||
36 | |||
37 | /* version 2 fields below */ |
||
38 | uint32_t boot_cpuid_phys; /* Which physical CPU id we're |
||
39 | booting on */ |
||
40 | /* version 3 fields below */ |
||
41 | uint32_t size_dt_strings; /* size of the strings block */ |
||
42 | |||
43 | /* version 17 fields below */ |
||
44 | uint32_t size_dt_struct; /* size of the structure block */ |
||
45 | }; |
||
46 | |||
47 | static int |
||
48 | mtdsplit_fit_parse(struct mtd_info *mtd, |
||
49 | const struct mtd_partition **pparts, |
||
50 | struct mtd_part_parser_data *data) |
||
51 | { |
||
52 | struct fdt_header hdr; |
||
53 | size_t hdr_len, retlen; |
||
54 | size_t offset; |
||
55 | size_t fit_offset, fit_size; |
||
56 | size_t rootfs_offset, rootfs_size; |
||
57 | struct mtd_partition *parts; |
||
58 | int ret; |
||
59 | |||
60 | hdr_len = sizeof(struct fdt_header); |
||
61 | |||
62 | /* Parse the MTD device & search for the FIT image location */ |
||
63 | for(offset = 0; offset < mtd->size; offset += mtd->erasesize) { |
||
64 | ret = mtd_read(mtd, 0, hdr_len, &retlen, (void*) &hdr); |
||
65 | if (ret) { |
||
66 | pr_err("read error in \"%s\" at offset 0x%llx\n", |
||
67 | mtd->name, (unsigned long long) offset); |
||
68 | return ret; |
||
69 | } |
||
70 | |||
71 | if (retlen != hdr_len) { |
||
72 | pr_err("short read in \"%s\"\n", mtd->name); |
||
73 | return -EIO; |
||
74 | } |
||
75 | |||
76 | /* Check the magic - see if this is a FIT image */ |
||
77 | if (be32_to_cpu(hdr.magic) != OF_DT_HEADER) { |
||
78 | pr_debug("no valid FIT image found in \"%s\" at offset %llx\n", |
||
79 | mtd->name, (unsigned long long) offset); |
||
80 | continue; |
||
81 | } |
||
82 | |||
83 | /* We found a FIT image. Let's keep going */ |
||
84 | break; |
||
85 | } |
||
86 | |||
87 | fit_offset = offset; |
||
88 | fit_size = be32_to_cpu(hdr.totalsize); |
||
89 | |||
90 | if (fit_size == 0) { |
||
91 | pr_err("FIT image in \"%s\" at offset %llx has null size\n", |
||
92 | mtd->name, (unsigned long long) fit_offset); |
||
93 | return -ENODEV; |
||
94 | } |
||
95 | |||
96 | /* Search for the rootfs partition after the FIT image */ |
||
97 | ret = mtd_find_rootfs_from(mtd, fit_offset + fit_size, mtd->size, |
||
98 | &rootfs_offset, NULL); |
||
99 | if (ret) { |
||
100 | pr_info("no rootfs found after FIT image in \"%s\"\n", |
||
101 | mtd->name); |
||
102 | return ret; |
||
103 | } |
||
104 | |||
105 | rootfs_size = mtd->size - rootfs_offset; |
||
106 | |||
107 | parts = kzalloc(2 * sizeof(*parts), GFP_KERNEL); |
||
108 | if (!parts) |
||
109 | return -ENOMEM; |
||
110 | |||
111 | parts[0].name = KERNEL_PART_NAME; |
||
112 | parts[0].offset = fit_offset; |
||
113 | parts[0].size = mtd_rounddown_to_eb(fit_size, mtd) + mtd->erasesize; |
||
114 | |||
115 | parts[1].name = ROOTFS_PART_NAME; |
||
116 | parts[1].offset = rootfs_offset; |
||
117 | parts[1].size = rootfs_size; |
||
118 | |||
119 | *pparts = parts; |
||
120 | return 2; |
||
121 | } |
||
122 | |||
123 | static struct mtd_part_parser uimage_parser = { |
||
124 | .owner = THIS_MODULE, |
||
125 | .name = "fit-fw", |
||
126 | .parse_fn = mtdsplit_fit_parse, |
||
127 | .type = MTD_PARSER_TYPE_FIRMWARE, |
||
128 | }; |
||
129 | |||
130 | /************************************************** |
||
131 | * Init |
||
132 | **************************************************/ |
||
133 | |||
134 | static int __init mtdsplit_fit_init(void) |
||
135 | { |
||
136 | register_mtd_parser(&uimage_parser); |
||
137 | |||
138 | return 0; |
||
139 | } |
||
140 | |||
141 | module_init(mtdsplit_fit_init); |