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