diff options
author | Linus Walleij <linus.walleij@linaro.org> | 2019-05-02 10:30:32 -0400 |
---|---|---|
committer | Richard Weinberger <richard@nod.at> | 2019-05-06 15:48:46 -0400 |
commit | ff827b4e8d3606a275b92b159c7e9ae45ad2c361 (patch) | |
tree | cf1aef2c80a45b65920c0e3869d2d1c92d0e9bf9 /drivers/mtd | |
parent | 4aeb1594796d21f10866b7bd28365024fba347ff (diff) |
mtd: afs: factor footer parsing into the v1 part parsing
This simplifies the code by factoring in the image footer
parsing into the single function parsing the AFSv1 partitions.
Cc: Ryan Harkin <ryan.harkin@linaro.org>
Acked-by: Liviu Dudau <liviu.dudau@arm.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Richard Weinberger <richard@nod.at>
Diffstat (limited to 'drivers/mtd')
-rw-r--r-- | drivers/mtd/parsers/afs.c | 98 |
1 files changed, 39 insertions, 59 deletions
diff --git a/drivers/mtd/parsers/afs.c b/drivers/mtd/parsers/afs.c index 32ded91ae66c..8ff82a548252 100644 --- a/drivers/mtd/parsers/afs.c +++ b/drivers/mtd/parsers/afs.c | |||
@@ -89,63 +89,6 @@ static bool afs_is_v1(struct mtd_info *mtd, u_int off) | |||
89 | } | 89 | } |
90 | 90 | ||
91 | static int | 91 | static int |
92 | afs_read_footer_v1(struct mtd_info *mtd, u_int *img_start, u_int *iis_start, | ||
93 | u_int off, u_int mask) | ||
94 | { | ||
95 | struct footer_v1 fs; | ||
96 | u_int ptr = off + mtd->erasesize - sizeof(fs); | ||
97 | size_t sz; | ||
98 | int ret; | ||
99 | |||
100 | ret = mtd_read(mtd, ptr, sizeof(fs), &sz, (u_char *)&fs); | ||
101 | if (ret >= 0 && sz != sizeof(fs)) | ||
102 | ret = -EINVAL; | ||
103 | |||
104 | if (ret < 0) { | ||
105 | printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n", | ||
106 | ptr, ret); | ||
107 | return ret; | ||
108 | } | ||
109 | |||
110 | /* | ||
111 | * Does it contain the magic number? | ||
112 | */ | ||
113 | if (fs.signature != AFSV1_FOOTER_MAGIC) | ||
114 | return 0; | ||
115 | |||
116 | /* | ||
117 | * Check the checksum. | ||
118 | */ | ||
119 | if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff) | ||
120 | return 0; | ||
121 | |||
122 | /* | ||
123 | * Don't touch the SIB. | ||
124 | */ | ||
125 | if (fs.type == 2) | ||
126 | return 0; | ||
127 | |||
128 | *iis_start = fs.image_info_base & mask; | ||
129 | *img_start = fs.image_start & mask; | ||
130 | |||
131 | /* | ||
132 | * Check the image info base. This can not | ||
133 | * be located after the footer structure. | ||
134 | */ | ||
135 | if (*iis_start >= ptr) | ||
136 | return 0; | ||
137 | |||
138 | /* | ||
139 | * Check the start of this image. The image | ||
140 | * data can not be located after this block. | ||
141 | */ | ||
142 | if (*img_start > off) | ||
143 | return 0; | ||
144 | |||
145 | return 1; | ||
146 | } | ||
147 | |||
148 | static int | ||
149 | afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr) | 92 | afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr) |
150 | { | 93 | { |
151 | size_t sz; | 94 | size_t sz; |
@@ -184,6 +127,7 @@ afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr) | |||
184 | static int afs_parse_v1_partition(struct mtd_info *mtd, | 127 | static int afs_parse_v1_partition(struct mtd_info *mtd, |
185 | u_int off, struct mtd_partition *part) | 128 | u_int off, struct mtd_partition *part) |
186 | { | 129 | { |
130 | struct footer_v1 fs; | ||
187 | struct image_info_v1 iis; | 131 | struct image_info_v1 iis; |
188 | u_int mask; | 132 | u_int mask; |
189 | /* | 133 | /* |
@@ -192,6 +136,8 @@ static int afs_parse_v1_partition(struct mtd_info *mtd, | |||
192 | */ | 136 | */ |
193 | u_int uninitialized_var(iis_ptr); | 137 | u_int uninitialized_var(iis_ptr); |
194 | u_int uninitialized_var(img_ptr); | 138 | u_int uninitialized_var(img_ptr); |
139 | u_int ptr; | ||
140 | size_t sz; | ||
195 | int ret; | 141 | int ret; |
196 | 142 | ||
197 | /* | 143 | /* |
@@ -200,9 +146,43 @@ static int afs_parse_v1_partition(struct mtd_info *mtd, | |||
200 | */ | 146 | */ |
201 | mask = mtd->size - 1; | 147 | mask = mtd->size - 1; |
202 | 148 | ||
203 | ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); | 149 | ptr = off + mtd->erasesize - sizeof(fs); |
204 | if (ret < 0) | 150 | ret = mtd_read(mtd, ptr, sizeof(fs), &sz, (u_char *)&fs); |
151 | if (ret >= 0 && sz != sizeof(fs)) | ||
152 | ret = -EINVAL; | ||
153 | if (ret < 0) { | ||
154 | printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n", | ||
155 | ptr, ret); | ||
205 | return ret; | 156 | return ret; |
157 | } | ||
158 | /* | ||
159 | * Check the checksum. | ||
160 | */ | ||
161 | if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff) | ||
162 | return -EINVAL; | ||
163 | |||
164 | /* | ||
165 | * Hide the SIB (System Information Block) | ||
166 | */ | ||
167 | if (fs.type == 2) | ||
168 | return 0; | ||
169 | |||
170 | iis_ptr = fs.image_info_base & mask; | ||
171 | img_ptr = fs.image_start & mask; | ||
172 | |||
173 | /* | ||
174 | * Check the image info base. This can not | ||
175 | * be located after the footer structure. | ||
176 | */ | ||
177 | if (iis_ptr >= ptr) | ||
178 | return 0; | ||
179 | |||
180 | /* | ||
181 | * Check the start of this image. The image | ||
182 | * data can not be located after this block. | ||
183 | */ | ||
184 | if (img_ptr > off) | ||
185 | return 0; | ||
206 | 186 | ||
207 | /* Read the image info block */ | 187 | /* Read the image info block */ |
208 | ret = afs_read_iis_v1(mtd, &iis, iis_ptr); | 188 | ret = afs_read_iis_v1(mtd, &iis, iis_ptr); |