blob: 7f412ac0ad4f6620f6e150ba5a25248b6bc408e8 [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>
39#include <aboot.h>
Maxime Ripard40aeeda2015-10-15 14:34:12 +020040#include <div64.h>
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020041#include <errno.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
55static unsigned int sparse_get_chunk_data_size(sparse_header_t *sparse,
56 chunk_header_t *chunk)
57{
58 return chunk->total_sz - sparse->chunk_hdr_sz;
59}
60
Maxime Riparda5d1e042015-10-15 14:34:14 +020061static unsigned int sparse_block_size_to_storage(unsigned int size,
62 sparse_storage_t *storage,
63 sparse_header_t *sparse)
64{
65 return size * sparse->blk_sz / storage->block_sz;
66}
67
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020068static bool sparse_chunk_has_buffer(chunk_header_t *chunk)
69{
70 switch (chunk->chunk_type) {
71 case CHUNK_TYPE_RAW:
72 case CHUNK_TYPE_FILL:
73 return true;
74
75 default:
76 return false;
77 }
78}
79
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020080static sparse_header_t *sparse_parse_header(void **data)
81{
82 /* Read and skip over sparse image header */
83 sparse_header_t *sparse_header = (sparse_header_t *) *data;
84
85 *data += sparse_header->file_hdr_sz;
86
87 debug("=== Sparse Image Header ===\n");
88 debug("magic: 0x%x\n", sparse_header->magic);
89 debug("major_version: 0x%x\n", sparse_header->major_version);
90 debug("minor_version: 0x%x\n", sparse_header->minor_version);
91 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
92 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
93 debug("blk_sz: %d\n", sparse_header->blk_sz);
94 debug("total_blks: %d\n", sparse_header->total_blks);
95 debug("total_chunks: %d\n", sparse_header->total_chunks);
96
97 return sparse_header;
98}
99
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200100static int sparse_parse_fill_chunk(sparse_header_t *sparse,
101 chunk_header_t *chunk)
102{
103 unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
104
105 if (chunk_data_sz != sizeof(uint32_t))
106 return -EINVAL;
107
108 return 0;
109}
110
111static int sparse_parse_raw_chunk(sparse_header_t *sparse,
112 chunk_header_t *chunk)
113{
114 unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
115
116 /* Check if the data size is a multiple of the main block size */
117 if (chunk_data_sz % sparse->blk_sz)
118 return -EINVAL;
119
120 /* Check that the chunk size is consistent */
121 if ((chunk_data_sz / sparse->blk_sz) != chunk->chunk_sz)
122 return -EINVAL;
123
124 return 0;
125}
126
127static chunk_header_t *sparse_parse_chunk(sparse_header_t *sparse,
128 void **image)
129{
130 chunk_header_t *chunk = (chunk_header_t *) *image;
131 int ret;
132
133 debug("=== Chunk Header ===\n");
134 debug("chunk_type: 0x%x\n", chunk->chunk_type);
135 debug("chunk_data_sz: 0x%x\n", chunk->chunk_sz);
136 debug("total_size: 0x%x\n", chunk->total_sz);
137
138 switch (chunk->chunk_type) {
139 case CHUNK_TYPE_RAW:
140 ret = sparse_parse_raw_chunk(sparse, chunk);
141 if (ret)
142 return NULL;
143 break;
144
145 case CHUNK_TYPE_FILL:
146 ret = sparse_parse_fill_chunk(sparse, chunk);
147 if (ret)
148 return NULL;
149 break;
150
151 case CHUNK_TYPE_DONT_CARE:
152 case CHUNK_TYPE_CRC32:
153 debug("Ignoring chunk\n");
154 break;
155
156 default:
157 printf("%s: Unknown chunk type: %x\n", __func__,
158 chunk->chunk_type);
159 return NULL;
160 }
161
162 *image += sparse->chunk_hdr_sz;
163
164 return chunk;
165}
166
167static int sparse_get_fill_buffer(sparse_header_t *sparse,
168 chunk_header_t *chunk,
169 sparse_buffer_t *buffer,
170 unsigned int blk_sz,
171 void *data)
172{
173 int i;
174
175 buffer->type = CHUNK_TYPE_FILL;
176
177 /*
178 * We create a buffer of one block, and ask it to be
179 * repeated as many times as needed.
180 */
181 buffer->length = blk_sz;
182 buffer->repeat = (chunk->chunk_sz * sparse->blk_sz) / blk_sz;
183
184 buffer->data = memalign(ARCH_DMA_MINALIGN,
185 ROUNDUP(blk_sz,
186 ARCH_DMA_MINALIGN));
187 if (!buffer->data)
188 return -ENOMEM;
189
190 for (i = 0; i < (buffer->length / sizeof(uint32_t)); i++)
191 ((uint32_t *)buffer->data)[i] = *(uint32_t *)(data);
192
193 return 0;
194}
195
196static int sparse_get_raw_buffer(sparse_header_t *sparse,
197 chunk_header_t *chunk,
198 sparse_buffer_t *buffer,
199 unsigned int blk_sz,
200 void *data)
201{
202 unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
203
204 buffer->type = CHUNK_TYPE_RAW;
205 buffer->length = chunk_data_sz;
206 buffer->data = data;
207 buffer->repeat = 1;
208
209 return 0;
210}
211
212static sparse_buffer_t *sparse_get_data_buffer(sparse_header_t *sparse,
213 chunk_header_t *chunk,
214 unsigned int blk_sz,
215 void **image)
216{
217 unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
218 sparse_buffer_t *buffer;
219 void *data = *image;
220 int ret;
221
222 *image += chunk_data_sz;
223
224 if (!sparse_chunk_has_buffer(chunk))
225 return NULL;
226
227 buffer = calloc(sizeof(sparse_buffer_t), 1);
228 if (!buffer)
229 return NULL;
230
231 switch (chunk->chunk_type) {
232 case CHUNK_TYPE_RAW:
233 ret = sparse_get_raw_buffer(sparse, chunk, buffer, blk_sz,
234 data);
235 if (ret)
236 return NULL;
237 break;
238
239 case CHUNK_TYPE_FILL:
240 ret = sparse_get_fill_buffer(sparse, chunk, buffer, blk_sz,
241 data);
242 if (ret)
243 return NULL;
244 break;
245
246 default:
247 return NULL;
248 }
249
250 debug("=== Buffer ===\n");
251 debug("length: 0x%x\n", buffer->length);
252 debug("repeat: 0x%x\n", buffer->repeat);
253 debug("type: 0x%x\n", buffer->type);
254 debug("data: 0x%p\n", buffer->data);
255
256 return buffer;
257}
258
259static void sparse_put_data_buffer(sparse_buffer_t *buffer)
260{
261 if (buffer->type == CHUNK_TYPE_FILL)
262 free(buffer->data);
263
264 free(buffer);
265}
266
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200267int store_sparse_image(sparse_storage_t *storage, void *storage_priv,
268 unsigned int session_id, void *data)
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700269{
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200270 unsigned int chunk, offset;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700271 sparse_header_t *sparse_header;
272 chunk_header_t *chunk_header;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200273 sparse_buffer_t *buffer;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200274 uint32_t start;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700275 uint32_t total_blocks = 0;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200276 uint32_t skipped = 0;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700277 int i;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700278
Maxime Riparda5d1e042015-10-15 14:34:14 +0200279 debug("=== Storage ===\n");
280 debug("name: %s\n", storage->name);
281 debug("block_size: 0x%x\n", storage->block_sz);
282 debug("start: 0x%x\n", storage->start);
283 debug("size: 0x%x\n", storage->size);
284 debug("write: 0x%p\n", storage->write);
285 debug("priv: 0x%p\n", storage_priv);
286
Maxime Ripardbb83c0f2015-10-15 14:34:10 +0200287 sparse_header = sparse_parse_header(&data);
288 if (!sparse_header) {
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200289 printf("sparse header issue\n");
290 return -EINVAL;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700291 }
292
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200293 /*
294 * Verify that the sparse block size is a multiple of our
295 * storage backend block size
296 */
Maxime Riparda5d1e042015-10-15 14:34:14 +0200297 div_u64_rem(sparse_header->blk_sz, storage->block_sz, &offset);
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200298 if (offset) {
Steve Raee6ca1ad2014-09-03 10:05:54 -0700299 printf("%s: Sparse image block size issue [%u]\n",
300 __func__, sparse_header->blk_sz);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200301 return -EINVAL;
Steve Raee6ca1ad2014-09-03 10:05:54 -0700302 }
303
304 puts("Flashing Sparse Image\n");
305
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700306 /* Start processing chunks */
Maxime Riparda5d1e042015-10-15 14:34:14 +0200307 start = storage->start;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200308 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
Maxime Riparda5d1e042015-10-15 14:34:14 +0200309 uint32_t blkcnt;
310
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200311 chunk_header = sparse_parse_chunk(sparse_header, &data);
312 if (!chunk_header) {
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200313 printf("Unknown chunk type");
314 return -EINVAL;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700315 }
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200316
317 /*
318 * If we have a DONT_CARE type, just skip the blocks
319 * and go on parsing the rest of the chunks
320 */
321 if (chunk_header->chunk_type == CHUNK_TYPE_DONT_CARE) {
Maxime Riparda5d1e042015-10-15 14:34:14 +0200322 skipped += sparse_block_size_to_storage(chunk_header->chunk_sz,
323 storage,
324 sparse_header);
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200325 continue;
326 }
327
328 /* Retrieve the buffer we're going to write */
329 buffer = sparse_get_data_buffer(sparse_header, chunk_header,
Maxime Riparda5d1e042015-10-15 14:34:14 +0200330 storage->block_sz, &data);
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200331 if (!buffer)
332 continue;
333
Maxime Riparda5d1e042015-10-15 14:34:14 +0200334 blkcnt = (buffer->length / storage->block_sz) * buffer->repeat;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200335
336 if ((start + total_blocks + blkcnt) >
Maxime Riparda5d1e042015-10-15 14:34:14 +0200337 (storage->start + storage->size)) {
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200338 printf("%s: Request would exceed partition size!\n",
339 __func__);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200340 return -EINVAL;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200341 }
342
343 for (i = 0; i < buffer->repeat; i++) {
344 unsigned long buffer_blk_cnt;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200345 int ret;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200346
Maxime Riparda5d1e042015-10-15 14:34:14 +0200347 buffer_blk_cnt = buffer->length / storage->block_sz;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200348
Maxime Riparda5d1e042015-10-15 14:34:14 +0200349 ret = storage->write(storage, storage_priv,
350 start + total_blocks,
351 buffer_blk_cnt,
352 buffer->data);
353 if (ret < 0) {
354 printf("%s: Write %d failed %d\n",
355 __func__, i, ret);
356 return ret;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200357 }
358
Maxime Riparda5d1e042015-10-15 14:34:14 +0200359 total_blocks += ret;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200360 }
361
362 sparse_put_data_buffer(buffer);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700363 }
364
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200365 debug("Wrote %d blocks, skipped %d, expected to write %d blocks\n",
Maxime Riparda5d1e042015-10-15 14:34:14 +0200366 total_blocks, skipped,
367 sparse_block_size_to_storage(sparse_header->total_blks,
368 storage, sparse_header));
369 printf("........ wrote %d blocks to '%s'\n", total_blocks,
370 storage->name);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700371
Maxime Riparda5d1e042015-10-15 14:34:14 +0200372 if ((total_blocks + skipped) !=
373 sparse_block_size_to_storage(sparse_header->total_blks,
374 storage, sparse_header)) {
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200375 printf("sparse image write failure\n");
376 return -EIO;
377 }
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700378
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200379 return 0;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700380}