1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <config.h>
38#include <common.h>
39#include <blk.h>
40#include <image-sparse.h>
41#include <div64.h>
42#include <log.h>
43#include <malloc.h>
44#include <part.h>
45#include <sparse_format.h>
46#include <asm/cache.h>
47
48#include <linux/math64.h>
49#include <linux/err.h>
50
51static void default_log(const char *ignored, char *response) {}
52
53static lbaint_t write_sparse_chunk_raw(struct sparse_storage *info,
54 lbaint_t blk, lbaint_t blkcnt,
55 void *data,
56 char *response)
57{
58 lbaint_t n = blkcnt, write_blks, blks = 0, aligned_buf_blks = 100;
59 uint32_t *aligned_buf = NULL;
60
61 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF)) {
62 write_blks = info->write(info, blk, n, data);
63 if (write_blks < n)
64 goto write_fail;
65
66 return write_blks;
67 }
68
69 aligned_buf = memalign(ARCH_DMA_MINALIGN, info->blksz * aligned_buf_blks);
70 if (!aligned_buf) {
71 info->mssg("Malloc failed for: CHUNK_TYPE_RAW", response);
72 return -ENOMEM;
73 }
74
75 while (blkcnt > 0) {
76 n = min(aligned_buf_blks, blkcnt);
77 memcpy(aligned_buf, data, n * info->blksz);
78
79
80 write_blks = info->write(info, blk + blks, n, aligned_buf);
81 if (write_blks < n) {
82 free(aligned_buf);
83 goto write_fail;
84 }
85
86 blks += write_blks;
87 data += n * info->blksz;
88 blkcnt -= n;
89 }
90
91 free(aligned_buf);
92 return blks;
93
94write_fail:
95 if (IS_ERR_VALUE(write_blks)) {
96 printf("%s: Write failed, block #" LBAFU " [" LBAFU "] (%lld)\n",
97 __func__, blk + blks, n, (long long)write_blks);
98 info->mssg("flash write failure", response);
99 return write_blks;
100 }
101
102
103 printf("%s: Write failed, block #" LBAFU " [" LBAFU "]\n",
104 __func__, blk + blks, n);
105 info->mssg("flash write failure(incomplete)", response);
106 return -1;
107}
108
109int write_sparse_image(struct sparse_storage *info,
110 const char *part_name, void *data, char *response)
111{
112 lbaint_t blk;
113 lbaint_t blkcnt;
114 lbaint_t blks;
115 uint64_t bytes_written = 0;
116 unsigned int chunk;
117 unsigned int offset;
118 uint64_t chunk_data_sz;
119 uint32_t *fill_buf = NULL;
120 uint32_t fill_val;
121 sparse_header_t *sparse_header;
122 chunk_header_t *chunk_header;
123 uint32_t total_blocks = 0;
124 int fill_buf_num_blks;
125 int i;
126 int j;
127
128 fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz;
129
130
131 sparse_header = (sparse_header_t *)data;
132
133 data += sparse_header->file_hdr_sz;
134 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
135
136
137
138
139 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
140 }
141
142 if (!info->mssg)
143 info->mssg = default_log;
144
145 debug("=== Sparse Image Header ===\n");
146 debug("magic: 0x%x\n", sparse_header->magic);
147 debug("major_version: 0x%x\n", sparse_header->major_version);
148 debug("minor_version: 0x%x\n", sparse_header->minor_version);
149 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
150 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
151 debug("blk_sz: %d\n", sparse_header->blk_sz);
152 debug("total_blks: %d\n", sparse_header->total_blks);
153 debug("total_chunks: %d\n", sparse_header->total_chunks);
154
155
156
157
158
159 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
160 if (offset) {
161 printf("%s: Sparse image block size issue [%u]\n",
162 __func__, sparse_header->blk_sz);
163 info->mssg("sparse image block size issue", response);
164 return -1;
165 }
166
167 puts("Flashing Sparse Image\n");
168
169
170 blk = info->start;
171 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
172
173 chunk_header = (chunk_header_t *)data;
174 data += sizeof(chunk_header_t);
175
176 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
177 debug("=== Chunk Header ===\n");
178 debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
179 debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
180 debug("total_size: 0x%x\n", chunk_header->total_sz);
181 }
182
183 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
184
185
186
187
188 data += (sparse_header->chunk_hdr_sz -
189 sizeof(chunk_header_t));
190 }
191
192 chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
193 blkcnt = DIV_ROUND_UP_ULL(chunk_data_sz, info->blksz);
194 switch (chunk_header->chunk_type) {
195 case CHUNK_TYPE_RAW:
196 if (chunk_header->total_sz !=
197 (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
198 info->mssg("Bogus chunk size for chunk type Raw",
199 response);
200 return -1;
201 }
202
203 if (blk + blkcnt > info->start + info->size) {
204 printf(
205 "%s: Request would exceed partition size!\n",
206 __func__);
207 info->mssg("Request would exceed partition size!",
208 response);
209 return -1;
210 }
211
212 blks = write_sparse_chunk_raw(info, blk, blkcnt,
213 data, response);
214 if (blks < 0)
215 return -1;
216
217 blk += blks;
218 bytes_written += ((u64)blkcnt) * info->blksz;
219 total_blocks += chunk_header->chunk_sz;
220 data += chunk_data_sz;
221 break;
222
223 case CHUNK_TYPE_FILL:
224 if (chunk_header->total_sz !=
225 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
226 info->mssg("Bogus chunk size for chunk type FILL", response);
227 return -1;
228 }
229
230 fill_buf = (uint32_t *)
231 memalign(ARCH_DMA_MINALIGN,
232 ROUNDUP(
233 info->blksz * fill_buf_num_blks,
234 ARCH_DMA_MINALIGN));
235 if (!fill_buf) {
236 info->mssg("Malloc failed for: CHUNK_TYPE_FILL",
237 response);
238 return -1;
239 }
240
241 fill_val = *(uint32_t *)data;
242 data = (char *)data + sizeof(uint32_t);
243
244 for (i = 0;
245 i < (info->blksz * fill_buf_num_blks /
246 sizeof(fill_val));
247 i++)
248 fill_buf[i] = fill_val;
249
250 if (blk + blkcnt > info->start + info->size) {
251 printf(
252 "%s: Request would exceed partition size!\n",
253 __func__);
254 info->mssg("Request would exceed partition size!",
255 response);
256 return -1;
257 }
258
259 for (i = 0; i < blkcnt;) {
260 j = blkcnt - i;
261 if (j > fill_buf_num_blks)
262 j = fill_buf_num_blks;
263 blks = info->write(info, blk, j, fill_buf);
264
265 if (blks < j) {
266 printf("%s: %s " LBAFU " [%d]\n",
267 __func__,
268 "Write failed, block #",
269 blk, j);
270 info->mssg("flash write failure",
271 response);
272 free(fill_buf);
273 return -1;
274 }
275 blk += blks;
276 i += j;
277 }
278 bytes_written += ((u64)blkcnt) * info->blksz;
279 total_blocks += DIV_ROUND_UP_ULL(chunk_data_sz,
280 sparse_header->blk_sz);
281 free(fill_buf);
282 break;
283
284 case CHUNK_TYPE_DONT_CARE:
285 blk += info->reserve(info, blk, blkcnt);
286 total_blocks += chunk_header->chunk_sz;
287 break;
288
289 case CHUNK_TYPE_CRC32:
290 if (chunk_header->total_sz !=
291 sparse_header->chunk_hdr_sz) {
292 info->mssg("Bogus chunk size for chunk type Dont Care",
293 response);
294 return -1;
295 }
296 total_blocks += chunk_header->chunk_sz;
297 data += chunk_data_sz;
298 break;
299
300 default:
301 printf("%s: Unknown chunk type: %x\n", __func__,
302 chunk_header->chunk_type);
303 info->mssg("Unknown chunk type", response);
304 return -1;
305 }
306 }
307
308 debug("Wrote %d blocks, expected to write %d blocks\n",
309 total_blocks, sparse_header->total_blks);
310 printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
311
312 if (total_blocks != sparse_header->total_blks) {
313 info->mssg("sparse image write failure", response);
314 return -1;
315 }
316
317 return 0;
318}
319