blob: dffe844d548a81d9751991f53ba2da77f8142571 [file] [log] [blame]
Steve Raeb4e4bbe2014-09-03 10:05:51 -07001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
5 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Steve Raee6ca1ad2014-09-03 10:05:54 -07006 * Portions Copyright 2014 Broadcom Corporation.
Steve Raeb4e4bbe2014-09-03 10:05:51 -07007 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of The Linux Foundation nor
16 * the names of its contributors may be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
Steve Rae1c39d852014-09-03 10:05:53 -070032 * NOTE:
33 * Although it is very similar, this license text is not identical
34 * to the "BSD-3-Clause", therefore, DO NOT MODIFY THIS LICENSE TEXT!
Steve Raeb4e4bbe2014-09-03 10:05:51 -070035 */
36
Steve Raee6ca1ad2014-09-03 10:05:54 -070037#include <config.h>
38#include <common.h>
Maxime Ripard40aeeda2015-10-15 14:34:12 +020039#include <div64.h>
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020040#include <errno.h>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020041#include <image-sparse.h>
Steve Raee6ca1ad2014-09-03 10:05:54 -070042#include <malloc.h>
43#include <part.h>
44#include <sparse_format.h>
45
Maxime Ripard40aeeda2015-10-15 14:34:12 +020046#include <linux/math64.h>
47
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020048typedef struct sparse_buffer {
49 void *data;
50 u32 length;
51 u32 repeat;
52 u16 type;
53} sparse_buffer_t;
54
Maxime Ripard1f8690a2015-10-15 14:34:16 +020055static uint32_t last_offset;
56
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020057static unsigned int sparse_get_chunk_data_size(sparse_header_t *sparse,
58 chunk_header_t *chunk)
59{
60 return chunk->total_sz - sparse->chunk_hdr_sz;
61}
62
Maxime Riparda5d1e042015-10-15 14:34:14 +020063static unsigned int sparse_block_size_to_storage(unsigned int size,
64 sparse_storage_t *storage,
65 sparse_header_t *sparse)
66{
67 return size * sparse->blk_sz / storage->block_sz;
68}
69
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020070static bool sparse_chunk_has_buffer(chunk_header_t *chunk)
71{
72 switch (chunk->chunk_type) {
73 case CHUNK_TYPE_RAW:
74 case CHUNK_TYPE_FILL:
75 return true;
76
77 default:
78 return false;
79 }
80}
81
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020082static sparse_header_t *sparse_parse_header(void **data)
83{
84 /* Read and skip over sparse image header */
85 sparse_header_t *sparse_header = (sparse_header_t *) *data;
86
87 *data += sparse_header->file_hdr_sz;
88
89 debug("=== Sparse Image Header ===\n");
90 debug("magic: 0x%x\n", sparse_header->magic);
91 debug("major_version: 0x%x\n", sparse_header->major_version);
92 debug("minor_version: 0x%x\n", sparse_header->minor_version);
93 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
94 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
95 debug("blk_sz: %d\n", sparse_header->blk_sz);
96 debug("total_blks: %d\n", sparse_header->total_blks);
97 debug("total_chunks: %d\n", sparse_header->total_chunks);
98
99 return sparse_header;
100}
101
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200102static int sparse_parse_fill_chunk(sparse_header_t *sparse,
103 chunk_header_t *chunk)
104{
105 unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
106
107 if (chunk_data_sz != sizeof(uint32_t))
108 return -EINVAL;
109
110 return 0;
111}
112
113static int sparse_parse_raw_chunk(sparse_header_t *sparse,
114 chunk_header_t *chunk)
115{
116 unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
117
118 /* Check if the data size is a multiple of the main block size */
119 if (chunk_data_sz % sparse->blk_sz)
120 return -EINVAL;
121
122 /* Check that the chunk size is consistent */
123 if ((chunk_data_sz / sparse->blk_sz) != chunk->chunk_sz)
124 return -EINVAL;
125
126 return 0;
127}
128
129static chunk_header_t *sparse_parse_chunk(sparse_header_t *sparse,
130 void **image)
131{
132 chunk_header_t *chunk = (chunk_header_t *) *image;
133 int ret;
134
135 debug("=== Chunk Header ===\n");
136 debug("chunk_type: 0x%x\n", chunk->chunk_type);
137 debug("chunk_data_sz: 0x%x\n", chunk->chunk_sz);
138 debug("total_size: 0x%x\n", chunk->total_sz);
139
140 switch (chunk->chunk_type) {
141 case CHUNK_TYPE_RAW:
142 ret = sparse_parse_raw_chunk(sparse, chunk);
143 if (ret)
144 return NULL;
145 break;
146
147 case CHUNK_TYPE_FILL:
148 ret = sparse_parse_fill_chunk(sparse, chunk);
149 if (ret)
150 return NULL;
151 break;
152
153 case CHUNK_TYPE_DONT_CARE:
154 case CHUNK_TYPE_CRC32:
155 debug("Ignoring chunk\n");
156 break;
157
158 default:
159 printf("%s: Unknown chunk type: %x\n", __func__,
160 chunk->chunk_type);
161 return NULL;
162 }
163
164 *image += sparse->chunk_hdr_sz;
165
166 return chunk;
167}
168
169static int sparse_get_fill_buffer(sparse_header_t *sparse,
170 chunk_header_t *chunk,
171 sparse_buffer_t *buffer,
172 unsigned int blk_sz,
173 void *data)
174{
175 int i;
176
177 buffer->type = CHUNK_TYPE_FILL;
178
179 /*
180 * We create a buffer of one block, and ask it to be
181 * repeated as many times as needed.
182 */
183 buffer->length = blk_sz;
184 buffer->repeat = (chunk->chunk_sz * sparse->blk_sz) / blk_sz;
185
186 buffer->data = memalign(ARCH_DMA_MINALIGN,
187 ROUNDUP(blk_sz,
188 ARCH_DMA_MINALIGN));
189 if (!buffer->data)
190 return -ENOMEM;
191
192 for (i = 0; i < (buffer->length / sizeof(uint32_t)); i++)
193 ((uint32_t *)buffer->data)[i] = *(uint32_t *)(data);
194
195 return 0;
196}
197
198static int sparse_get_raw_buffer(sparse_header_t *sparse,
199 chunk_header_t *chunk,
200 sparse_buffer_t *buffer,
201 unsigned int blk_sz,
202 void *data)
203{
204 unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
205
206 buffer->type = CHUNK_TYPE_RAW;
207 buffer->length = chunk_data_sz;
208 buffer->data = data;
209 buffer->repeat = 1;
210
211 return 0;
212}
213
214static sparse_buffer_t *sparse_get_data_buffer(sparse_header_t *sparse,
215 chunk_header_t *chunk,
216 unsigned int blk_sz,
217 void **image)
218{
219 unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
220 sparse_buffer_t *buffer;
221 void *data = *image;
222 int ret;
223
224 *image += chunk_data_sz;
225
226 if (!sparse_chunk_has_buffer(chunk))
227 return NULL;
228
229 buffer = calloc(sizeof(sparse_buffer_t), 1);
230 if (!buffer)
231 return NULL;
232
233 switch (chunk->chunk_type) {
234 case CHUNK_TYPE_RAW:
235 ret = sparse_get_raw_buffer(sparse, chunk, buffer, blk_sz,
236 data);
237 if (ret)
238 return NULL;
239 break;
240
241 case CHUNK_TYPE_FILL:
242 ret = sparse_get_fill_buffer(sparse, chunk, buffer, blk_sz,
243 data);
244 if (ret)
245 return NULL;
246 break;
247
248 default:
249 return NULL;
250 }
251
252 debug("=== Buffer ===\n");
253 debug("length: 0x%x\n", buffer->length);
254 debug("repeat: 0x%x\n", buffer->repeat);
255 debug("type: 0x%x\n", buffer->type);
256 debug("data: 0x%p\n", buffer->data);
257
258 return buffer;
259}
260
261static void sparse_put_data_buffer(sparse_buffer_t *buffer)
262{
263 if (buffer->type == CHUNK_TYPE_FILL)
264 free(buffer->data);
265
266 free(buffer);
267}
268
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200269int store_sparse_image(sparse_storage_t *storage, void *storage_priv,
270 unsigned int session_id, void *data)
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700271{
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200272 unsigned int chunk, offset;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700273 sparse_header_t *sparse_header;
274 chunk_header_t *chunk_header;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200275 sparse_buffer_t *buffer;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200276 uint32_t start;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700277 uint32_t total_blocks = 0;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200278 uint32_t skipped = 0;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700279 int i;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700280
Maxime Riparda5d1e042015-10-15 14:34:14 +0200281 debug("=== Storage ===\n");
282 debug("name: %s\n", storage->name);
283 debug("block_size: 0x%x\n", storage->block_sz);
284 debug("start: 0x%x\n", storage->start);
285 debug("size: 0x%x\n", storage->size);
286 debug("write: 0x%p\n", storage->write);
287 debug("priv: 0x%p\n", storage_priv);
288
Maxime Ripardbb83c0f2015-10-15 14:34:10 +0200289 sparse_header = sparse_parse_header(&data);
290 if (!sparse_header) {
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200291 printf("sparse header issue\n");
292 return -EINVAL;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700293 }
294
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200295 /*
296 * Verify that the sparse block size is a multiple of our
297 * storage backend block size
298 */
Maxime Riparda5d1e042015-10-15 14:34:14 +0200299 div_u64_rem(sparse_header->blk_sz, storage->block_sz, &offset);
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200300 if (offset) {
Steve Raee6ca1ad2014-09-03 10:05:54 -0700301 printf("%s: Sparse image block size issue [%u]\n",
302 __func__, sparse_header->blk_sz);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200303 return -EINVAL;
Steve Raee6ca1ad2014-09-03 10:05:54 -0700304 }
305
Maxime Ripard1f8690a2015-10-15 14:34:16 +0200306 /*
307 * If it's a new flashing session, start at the beginning of
308 * the partition. If not, then simply resume where we were.
309 */
310 if (session_id > 0)
311 start = last_offset;
312 else
313 start = storage->start;
314
315 printf("Flashing sparse image on partition %s at offset 0x%x (ID: %d)\n",
316 storage->name, start * storage->block_sz, session_id);
Steve Raee6ca1ad2014-09-03 10:05:54 -0700317
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700318 /* Start processing chunks */
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200319 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
Maxime Riparda5d1e042015-10-15 14:34:14 +0200320 uint32_t blkcnt;
321
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200322 chunk_header = sparse_parse_chunk(sparse_header, &data);
323 if (!chunk_header) {
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200324 printf("Unknown chunk type");
325 return -EINVAL;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700326 }
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200327
328 /*
329 * If we have a DONT_CARE type, just skip the blocks
330 * and go on parsing the rest of the chunks
331 */
332 if (chunk_header->chunk_type == CHUNK_TYPE_DONT_CARE) {
Maxime Riparda5d1e042015-10-15 14:34:14 +0200333 skipped += sparse_block_size_to_storage(chunk_header->chunk_sz,
334 storage,
335 sparse_header);
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200336 continue;
337 }
338
339 /* Retrieve the buffer we're going to write */
340 buffer = sparse_get_data_buffer(sparse_header, chunk_header,
Maxime Riparda5d1e042015-10-15 14:34:14 +0200341 storage->block_sz, &data);
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200342 if (!buffer)
343 continue;
344
Maxime Riparda5d1e042015-10-15 14:34:14 +0200345 blkcnt = (buffer->length / storage->block_sz) * buffer->repeat;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200346
347 if ((start + total_blocks + blkcnt) >
Maxime Riparda5d1e042015-10-15 14:34:14 +0200348 (storage->start + storage->size)) {
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200349 printf("%s: Request would exceed partition size!\n",
350 __func__);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200351 return -EINVAL;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200352 }
353
354 for (i = 0; i < buffer->repeat; i++) {
355 unsigned long buffer_blk_cnt;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200356 int ret;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200357
Maxime Riparda5d1e042015-10-15 14:34:14 +0200358 buffer_blk_cnt = buffer->length / storage->block_sz;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200359
Maxime Riparda5d1e042015-10-15 14:34:14 +0200360 ret = storage->write(storage, storage_priv,
361 start + total_blocks,
362 buffer_blk_cnt,
363 buffer->data);
364 if (ret < 0) {
365 printf("%s: Write %d failed %d\n",
366 __func__, i, ret);
367 return ret;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200368 }
369
Maxime Riparda5d1e042015-10-15 14:34:14 +0200370 total_blocks += ret;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200371 }
372
373 sparse_put_data_buffer(buffer);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700374 }
375
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200376 debug("Wrote %d blocks, skipped %d, expected to write %d blocks\n",
Maxime Riparda5d1e042015-10-15 14:34:14 +0200377 total_blocks, skipped,
378 sparse_block_size_to_storage(sparse_header->total_blks,
379 storage, sparse_header));
380 printf("........ wrote %d blocks to '%s'\n", total_blocks,
381 storage->name);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700382
Maxime Riparda5d1e042015-10-15 14:34:14 +0200383 if ((total_blocks + skipped) !=
384 sparse_block_size_to_storage(sparse_header->total_blks,
385 storage, sparse_header)) {
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200386 printf("sparse image write failure\n");
387 return -EIO;
388 }
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700389
Maxime Ripard1f8690a2015-10-15 14:34:16 +0200390 last_offset = start + total_blocks;
391
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200392 return 0;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700393}