fastboot: sparse: remove session-id logic
[oweals/u-boot.git] / common / image-sparse.c
1 /*
2  * Copyright (c) 2009, Google Inc.
3  * All rights reserved.
4  *
5  * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
6  * Portions Copyright 2014 Broadcom Corporation.
7  *
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  *
32  * 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!
35  */
36
37 #include <config.h>
38 #include <common.h>
39 #include <div64.h>
40 #include <errno.h>
41 #include <image-sparse.h>
42 #include <malloc.h>
43 #include <part.h>
44 #include <sparse_format.h>
45
46 #include <linux/math64.h>
47
48 typedef struct sparse_buffer {
49         void    *data;
50         u32     length;
51         u32     repeat;
52         u16     type;
53 } sparse_buffer_t;
54
55 static 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
61 static unsigned int sparse_block_size_to_storage(unsigned int size,
62                                                  sparse_storage_t *storage,
63                                                  sparse_header_t *sparse)
64 {
65         return (unsigned int)lldiv((uint64_t)size * sparse->blk_sz,
66                                    storage->block_sz);
67 }
68
69 static bool sparse_chunk_has_buffer(chunk_header_t *chunk)
70 {
71         switch (chunk->chunk_type) {
72         case CHUNK_TYPE_RAW:
73         case CHUNK_TYPE_FILL:
74                 return true;
75
76         default:
77                 return false;
78         }
79 }
80
81 static sparse_header_t *sparse_parse_header(void **data)
82 {
83         /* Read and skip over sparse image header */
84         sparse_header_t *sparse_header = (sparse_header_t *) *data;
85
86         *data += sparse_header->file_hdr_sz;
87
88         debug("=== Sparse Image Header ===\n");
89         debug("magic: 0x%x\n", sparse_header->magic);
90         debug("major_version: 0x%x\n", sparse_header->major_version);
91         debug("minor_version: 0x%x\n", sparse_header->minor_version);
92         debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
93         debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
94         debug("blk_sz: %d\n", sparse_header->blk_sz);
95         debug("total_blks: %d\n", sparse_header->total_blks);
96         debug("total_chunks: %d\n", sparse_header->total_chunks);
97
98         return sparse_header;
99 }
100
101 static int sparse_parse_fill_chunk(sparse_header_t *sparse,
102                                    chunk_header_t *chunk)
103 {
104         unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
105
106         if (chunk_data_sz != sizeof(uint32_t))
107                 return -EINVAL;
108
109         return 0;
110 }
111
112 static int sparse_parse_raw_chunk(sparse_header_t *sparse,
113                                   chunk_header_t *chunk)
114 {
115         unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
116
117         /* Check if the data size is a multiple of the main block size */
118         if (chunk_data_sz % sparse->blk_sz)
119                 return -EINVAL;
120
121         /* Check that the chunk size is consistent */
122         if ((chunk_data_sz / sparse->blk_sz) != chunk->chunk_sz)
123                 return -EINVAL;
124
125         return 0;
126 }
127
128 static chunk_header_t *sparse_parse_chunk(sparse_header_t *sparse,
129                                           void **image)
130 {
131         chunk_header_t *chunk = (chunk_header_t *) *image;
132         int ret;
133
134         debug("=== Chunk Header ===\n");
135         debug("chunk_type: 0x%x\n", chunk->chunk_type);
136         debug("chunk_data_sz: 0x%x\n", chunk->chunk_sz);
137         debug("total_size: 0x%x\n", chunk->total_sz);
138
139         switch (chunk->chunk_type) {
140         case CHUNK_TYPE_RAW:
141                 ret = sparse_parse_raw_chunk(sparse, chunk);
142                 if (ret)
143                         return NULL;
144                 break;
145
146         case CHUNK_TYPE_FILL:
147                 ret = sparse_parse_fill_chunk(sparse, chunk);
148                 if (ret)
149                         return NULL;
150                 break;
151
152         case CHUNK_TYPE_DONT_CARE:
153         case CHUNK_TYPE_CRC32:
154                 debug("Ignoring chunk\n");
155                 break;
156
157         default:
158                 printf("%s: Unknown chunk type: %x\n", __func__,
159                        chunk->chunk_type);
160                 return NULL;
161         }
162
163         *image += sparse->chunk_hdr_sz;
164
165         return chunk;
166 }
167
168 static int sparse_get_fill_buffer(sparse_header_t *sparse,
169                                   chunk_header_t *chunk,
170                                   sparse_buffer_t *buffer,
171                                   unsigned int blk_sz,
172                                   void *data)
173 {
174         int i;
175
176         buffer->type = CHUNK_TYPE_FILL;
177
178         /*
179          * We create a buffer of one block, and ask it to be
180          * repeated as many times as needed.
181          */
182         buffer->length = blk_sz;
183         buffer->repeat = (chunk->chunk_sz * sparse->blk_sz) / blk_sz;
184
185         buffer->data = memalign(ARCH_DMA_MINALIGN,
186                                 ROUNDUP(blk_sz,
187                                         ARCH_DMA_MINALIGN));
188         if (!buffer->data)
189                 return -ENOMEM;
190
191         for (i = 0; i < (buffer->length / sizeof(uint32_t)); i++)
192                 ((uint32_t *)buffer->data)[i] = *(uint32_t *)(data);
193
194         return 0;
195 }
196
197 static int sparse_get_raw_buffer(sparse_header_t *sparse,
198                                  chunk_header_t *chunk,
199                                  sparse_buffer_t *buffer,
200                                  unsigned int blk_sz,
201                                  void *data)
202 {
203         unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
204
205         buffer->type = CHUNK_TYPE_RAW;
206         buffer->length = chunk_data_sz;
207         buffer->data = data;
208         buffer->repeat = 1;
209
210         return 0;
211 }
212
213 static sparse_buffer_t *sparse_get_data_buffer(sparse_header_t *sparse,
214                                                chunk_header_t *chunk,
215                                                unsigned int blk_sz,
216                                                void **image)
217 {
218         unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
219         sparse_buffer_t *buffer;
220         void *data = *image;
221         int ret;
222
223         *image += chunk_data_sz;
224
225         if (!sparse_chunk_has_buffer(chunk))
226                 return NULL;
227
228         buffer = calloc(sizeof(sparse_buffer_t), 1);
229         if (!buffer)
230                 return NULL;
231
232         switch (chunk->chunk_type) {
233         case CHUNK_TYPE_RAW:
234                 ret = sparse_get_raw_buffer(sparse, chunk, buffer, blk_sz,
235                                             data);
236                 if (ret)
237                         return NULL;
238                 break;
239
240         case CHUNK_TYPE_FILL:
241                 ret = sparse_get_fill_buffer(sparse, chunk, buffer, blk_sz,
242                                              data);
243                 if (ret)
244                         return NULL;
245                 break;
246
247         default:
248                 return NULL;
249         }
250
251         debug("=== Buffer ===\n");
252         debug("length: 0x%x\n", buffer->length);
253         debug("repeat: 0x%x\n", buffer->repeat);
254         debug("type: 0x%x\n", buffer->type);
255         debug("data: 0x%p\n", buffer->data);
256
257         return buffer;
258 }
259
260 static void sparse_put_data_buffer(sparse_buffer_t *buffer)
261 {
262         if (buffer->type == CHUNK_TYPE_FILL)
263                 free(buffer->data);
264
265         free(buffer);
266 }
267
268 int store_sparse_image(sparse_storage_t *storage,
269                        void *storage_priv, void *data)
270 {
271         unsigned int chunk, offset;
272         sparse_header_t *sparse_header;
273         chunk_header_t *chunk_header;
274         sparse_buffer_t *buffer;
275         uint32_t start;
276         uint32_t total_blocks = 0;
277         int i;
278
279         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
287         sparse_header = sparse_parse_header(&data);
288         if (!sparse_header) {
289                 printf("sparse header issue\n");
290                 return -EINVAL;
291         }
292
293         /*
294          * Verify that the sparse block size is a multiple of our
295          * storage backend block size
296          */
297         div_u64_rem(sparse_header->blk_sz, storage->block_sz, &offset);
298         if (offset) {
299                 printf("%s: Sparse image block size issue [%u]\n",
300                        __func__, sparse_header->blk_sz);
301                 return -EINVAL;
302         }
303
304         puts("Flashing Sparse Image\n");
305
306         /* Start processing chunks */
307         start = storage->start;
308         for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
309                 uint32_t blkcnt;
310
311                 chunk_header = sparse_parse_chunk(sparse_header, &data);
312                 if (!chunk_header) {
313                         printf("Unknown chunk type");
314                         return -EINVAL;
315                 }
316
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) {
322                         blkcnt = sparse_block_size_to_storage(chunk_header->chunk_sz,
323                                                               storage,
324                                                               sparse_header);
325 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
326                         total_blocks += blkcnt;
327 #endif
328                         continue;
329                 }
330
331                 /* Retrieve the buffer we're going to write */
332                 buffer = sparse_get_data_buffer(sparse_header, chunk_header,
333                                                 storage->block_sz, &data);
334                 if (!buffer)
335                         continue;
336
337                 blkcnt = (buffer->length / storage->block_sz) * buffer->repeat;
338
339                 if ((start + total_blocks + blkcnt) >
340                     (storage->start + storage->size)) {
341                         printf("%s: Request would exceed partition size!\n",
342                                __func__);
343                         return -EINVAL;
344                 }
345
346                 for (i = 0; i < buffer->repeat; i++) {
347                         unsigned long buffer_blk_cnt;
348                         int ret;
349
350                         buffer_blk_cnt = buffer->length / storage->block_sz;
351
352                         ret = storage->write(storage, storage_priv,
353                                              start + total_blocks,
354                                              buffer_blk_cnt,
355                                              buffer->data);
356                         if (ret < 0) {
357                                 printf("%s: Write %d failed %d\n",
358                                        __func__, i, ret);
359                                 return ret;
360                         }
361
362                         total_blocks += ret;
363                 }
364
365                 sparse_put_data_buffer(buffer);
366         }
367
368         debug("Wrote %d blocks, expected to write %d blocks\n",
369               total_blocks,
370               sparse_block_size_to_storage(sparse_header->total_blks,
371                                            storage, sparse_header));
372         printf("........ wrote %d blocks to '%s'\n", total_blocks,
373                storage->name);
374
375         if (total_blocks !=
376             sparse_block_size_to_storage(sparse_header->total_blks,
377                                          storage, sparse_header)) {
378                 printf("sparse image write failure\n");
379                 return -EIO;
380         }
381
382         return 0;
383 }