common: Drop part.h from common header
[platform/kernel/u-boot.git] / lib / 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 <blk.h>
40 #include <image-sparse.h>
41 #include <div64.h>
42 #include <malloc.h>
43 #include <part.h>
44 #include <sparse_format.h>
45 #include <asm/cache.h>
46
47 #include <linux/math64.h>
48
49 static void default_log(const char *ignored, char *response) {}
50
51 int write_sparse_image(struct sparse_storage *info,
52                        const char *part_name, void *data, char *response)
53 {
54         lbaint_t blk;
55         lbaint_t blkcnt;
56         lbaint_t blks;
57         uint32_t bytes_written = 0;
58         unsigned int chunk;
59         unsigned int offset;
60         unsigned int chunk_data_sz;
61         uint32_t *fill_buf = NULL;
62         uint32_t fill_val;
63         sparse_header_t *sparse_header;
64         chunk_header_t *chunk_header;
65         uint32_t total_blocks = 0;
66         int fill_buf_num_blks;
67         int i;
68         int j;
69
70         fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz;
71
72         /* Read and skip over sparse image header */
73         sparse_header = (sparse_header_t *)data;
74
75         data += sparse_header->file_hdr_sz;
76         if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
77                 /*
78                  * Skip the remaining bytes in a header that is longer than
79                  * we expected.
80                  */
81                 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
82         }
83
84         if (!info->mssg)
85                 info->mssg = default_log;
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         /*
98          * Verify that the sparse block size is a multiple of our
99          * storage backend block size
100          */
101         div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
102         if (offset) {
103                 printf("%s: Sparse image block size issue [%u]\n",
104                        __func__, sparse_header->blk_sz);
105                 info->mssg("sparse image block size issue", response);
106                 return -1;
107         }
108
109         puts("Flashing Sparse Image\n");
110
111         /* Start processing chunks */
112         blk = info->start;
113         for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
114                 /* Read and skip over chunk header */
115                 chunk_header = (chunk_header_t *)data;
116                 data += sizeof(chunk_header_t);
117
118                 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
119                         debug("=== Chunk Header ===\n");
120                         debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
121                         debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
122                         debug("total_size: 0x%x\n", chunk_header->total_sz);
123                 }
124
125                 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
126                         /*
127                          * Skip the remaining bytes in a header that is longer
128                          * than we expected.
129                          */
130                         data += (sparse_header->chunk_hdr_sz -
131                                  sizeof(chunk_header_t));
132                 }
133
134                 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
135                 blkcnt = chunk_data_sz / info->blksz;
136                 switch (chunk_header->chunk_type) {
137                 case CHUNK_TYPE_RAW:
138                         if (chunk_header->total_sz !=
139                             (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
140                                 info->mssg("Bogus chunk size for chunk type Raw",
141                                            response);
142                                 return -1;
143                         }
144
145                         if (blk + blkcnt > info->start + info->size) {
146                                 printf(
147                                     "%s: Request would exceed partition size!\n",
148                                     __func__);
149                                 info->mssg("Request would exceed partition size!",
150                                            response);
151                                 return -1;
152                         }
153
154                         blks = info->write(info, blk, blkcnt, data);
155                         /* blks might be > blkcnt (eg. NAND bad-blocks) */
156                         if (blks < blkcnt) {
157                                 printf("%s: %s" LBAFU " [" LBAFU "]\n",
158                                        __func__, "Write failed, block #",
159                                        blk, blks);
160                                 info->mssg("flash write failure", response);
161                                 return -1;
162                         }
163                         blk += blks;
164                         bytes_written += blkcnt * info->blksz;
165                         total_blocks += chunk_header->chunk_sz;
166                         data += chunk_data_sz;
167                         break;
168
169                 case CHUNK_TYPE_FILL:
170                         if (chunk_header->total_sz !=
171                             (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
172                                 info->mssg("Bogus chunk size for chunk type FILL", response);
173                                 return -1;
174                         }
175
176                         fill_buf = (uint32_t *)
177                                    memalign(ARCH_DMA_MINALIGN,
178                                             ROUNDUP(
179                                                 info->blksz * fill_buf_num_blks,
180                                                 ARCH_DMA_MINALIGN));
181                         if (!fill_buf) {
182                                 info->mssg("Malloc failed for: CHUNK_TYPE_FILL",
183                                            response);
184                                 return -1;
185                         }
186
187                         fill_val = *(uint32_t *)data;
188                         data = (char *)data + sizeof(uint32_t);
189
190                         for (i = 0;
191                              i < (info->blksz * fill_buf_num_blks /
192                                   sizeof(fill_val));
193                              i++)
194                                 fill_buf[i] = fill_val;
195
196                         if (blk + blkcnt > info->start + info->size) {
197                                 printf(
198                                     "%s: Request would exceed partition size!\n",
199                                     __func__);
200                                 info->mssg("Request would exceed partition size!",
201                                            response);
202                                 return -1;
203                         }
204
205                         for (i = 0; i < blkcnt;) {
206                                 j = blkcnt - i;
207                                 if (j > fill_buf_num_blks)
208                                         j = fill_buf_num_blks;
209                                 blks = info->write(info, blk, j, fill_buf);
210                                 /* blks might be > j (eg. NAND bad-blocks) */
211                                 if (blks < j) {
212                                         printf("%s: %s " LBAFU " [%d]\n",
213                                                __func__,
214                                                "Write failed, block #",
215                                                blk, j);
216                                         info->mssg("flash write failure",
217                                                    response);
218                                         free(fill_buf);
219                                         return -1;
220                                 }
221                                 blk += blks;
222                                 i += j;
223                         }
224                         bytes_written += blkcnt * info->blksz;
225                         total_blocks += chunk_data_sz / sparse_header->blk_sz;
226                         free(fill_buf);
227                         break;
228
229                 case CHUNK_TYPE_DONT_CARE:
230                         blk += info->reserve(info, blk, blkcnt);
231                         total_blocks += chunk_header->chunk_sz;
232                         break;
233
234                 case CHUNK_TYPE_CRC32:
235                         if (chunk_header->total_sz !=
236                             sparse_header->chunk_hdr_sz) {
237                                 info->mssg("Bogus chunk size for chunk type Dont Care",
238                                            response);
239                                 return -1;
240                         }
241                         total_blocks += chunk_header->chunk_sz;
242                         data += chunk_data_sz;
243                         break;
244
245                 default:
246                         printf("%s: Unknown chunk type: %x\n", __func__,
247                                chunk_header->chunk_type);
248                         info->mssg("Unknown chunk type", response);
249                         return -1;
250                 }
251         }
252
253         debug("Wrote %d blocks, expected to write %d blocks\n",
254               total_blocks, sparse_header->total_blks);
255         printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
256
257         if (total_blocks != sparse_header->total_blks) {
258                 info->mssg("sparse image write failure", response);
259                 return -1;
260         }
261
262         return 0;
263 }