2 * Copyright (c) 2009, Google Inc.
5 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
6 * Portions Copyright 2014 Broadcom Corporation.
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
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.
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!
40 #include <image-sparse.h>
45 #include <sparse_format.h>
46 #include <asm/cache.h>
48 #include <linux/math64.h>
49 #include <linux/err.h>
51 static void default_log(const char *ignored, char *response) {}
53 static lbaint_t write_sparse_chunk_raw(struct sparse_storage *info,
54 lbaint_t blk, lbaint_t blkcnt,
58 lbaint_t n = blkcnt, write_blks, blks = 0;
59 lbaint_t aligned_buf_blks = FASTBOOT_MAX_BLK_WRITE;
60 uint32_t *aligned_buf = NULL;
62 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF)) {
63 write_blks = info->write(info, blk, n, data);
70 aligned_buf = memalign(ARCH_DMA_MINALIGN, info->blksz * aligned_buf_blks);
72 info->mssg("Malloc failed for: CHUNK_TYPE_RAW", response);
77 n = min(aligned_buf_blks, blkcnt);
78 memcpy(aligned_buf, data, n * info->blksz);
80 /* write_blks might be > n due to NAND bad-blocks */
81 write_blks = info->write(info, blk + blks, n, aligned_buf);
88 data += n * info->blksz;
96 if (IS_ERR_VALUE(write_blks)) {
97 printf("%s: Write failed, block #" LBAFU " [" LBAFU "] (%lld)\n",
98 __func__, blk + blks, n, (long long)write_blks);
99 info->mssg("flash write failure", response);
104 printf("%s: Write failed, block #" LBAFU " [" LBAFU "]\n",
105 __func__, blk + blks, n);
106 info->mssg("flash write failure(incomplete)", response);
110 int write_sparse_image(struct sparse_storage *info,
111 const char *part_name, void *data, char *response)
116 uint64_t bytes_written = 0;
119 uint64_t chunk_data_sz;
120 uint32_t *fill_buf = NULL;
122 sparse_header_t *sparse_header;
123 chunk_header_t *chunk_header;
124 uint32_t total_blocks = 0;
125 int fill_buf_num_blks;
129 fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz;
131 /* Read and skip over sparse image header */
132 sparse_header = (sparse_header_t *)data;
134 data += sparse_header->file_hdr_sz;
135 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
137 * Skip the remaining bytes in a header that is longer than
140 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
144 info->mssg = default_log;
146 debug("=== Sparse Image Header ===\n");
147 debug("magic: 0x%x\n", sparse_header->magic);
148 debug("major_version: 0x%x\n", sparse_header->major_version);
149 debug("minor_version: 0x%x\n", sparse_header->minor_version);
150 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
151 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
152 debug("blk_sz: %d\n", sparse_header->blk_sz);
153 debug("total_blks: %d\n", sparse_header->total_blks);
154 debug("total_chunks: %d\n", sparse_header->total_chunks);
157 * Verify that the sparse block size is a multiple of our
158 * storage backend block size
160 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
162 printf("%s: Sparse image block size issue [%u]\n",
163 __func__, sparse_header->blk_sz);
164 info->mssg("sparse image block size issue", response);
168 puts("Flashing Sparse Image\n");
170 /* Start processing chunks */
172 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
173 /* Read and skip over chunk header */
174 chunk_header = (chunk_header_t *)data;
175 data += sizeof(chunk_header_t);
177 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
178 debug("=== Chunk Header ===\n");
179 debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
180 debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
181 debug("total_size: 0x%x\n", chunk_header->total_sz);
184 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
186 * Skip the remaining bytes in a header that is longer
189 data += (sparse_header->chunk_hdr_sz -
190 sizeof(chunk_header_t));
193 chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
194 blkcnt = DIV_ROUND_UP_ULL(chunk_data_sz, info->blksz);
195 switch (chunk_header->chunk_type) {
197 if (chunk_header->total_sz !=
198 (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
199 info->mssg("Bogus chunk size for chunk type Raw",
204 if (blk + blkcnt > info->start + info->size) {
206 "%s: Request would exceed partition size!\n",
208 info->mssg("Request would exceed partition size!",
213 blks = write_sparse_chunk_raw(info, blk, blkcnt,
219 bytes_written += ((u64)blkcnt) * info->blksz;
220 total_blocks += chunk_header->chunk_sz;
221 data += chunk_data_sz;
224 case CHUNK_TYPE_FILL:
225 if (chunk_header->total_sz !=
226 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
227 info->mssg("Bogus chunk size for chunk type FILL", response);
231 fill_buf = (uint32_t *)
232 memalign(ARCH_DMA_MINALIGN,
234 info->blksz * fill_buf_num_blks,
237 info->mssg("Malloc failed for: CHUNK_TYPE_FILL",
242 fill_val = *(uint32_t *)data;
243 data = (char *)data + sizeof(uint32_t);
246 i < (info->blksz * fill_buf_num_blks /
249 fill_buf[i] = fill_val;
251 if (blk + blkcnt > info->start + info->size) {
253 "%s: Request would exceed partition size!\n",
255 info->mssg("Request would exceed partition size!",
260 for (i = 0; i < blkcnt;) {
262 if (j > fill_buf_num_blks)
263 j = fill_buf_num_blks;
264 blks = info->write(info, blk, j, fill_buf);
265 /* blks might be > j (eg. NAND bad-blocks) */
267 printf("%s: %s " LBAFU " [%d]\n",
269 "Write failed, block #",
271 info->mssg("flash write failure",
279 bytes_written += ((u64)blkcnt) * info->blksz;
280 total_blocks += DIV_ROUND_UP_ULL(chunk_data_sz,
281 sparse_header->blk_sz);
285 case CHUNK_TYPE_DONT_CARE:
286 blk += info->reserve(info, blk, blkcnt);
287 total_blocks += chunk_header->chunk_sz;
290 case CHUNK_TYPE_CRC32:
291 if (chunk_header->total_sz !=
292 sparse_header->chunk_hdr_sz) {
293 info->mssg("Bogus chunk size for chunk type Dont Care",
297 total_blocks += chunk_header->chunk_sz;
298 data += chunk_data_sz;
302 printf("%s: Unknown chunk type: %x\n", __func__,
303 chunk_header->chunk_type);
304 info->mssg("Unknown chunk type", response);
309 debug("Wrote %d blocks, expected to write %d blocks\n",
310 total_blocks, sparse_header->total_blks);
311 printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
313 if (total_blocks != sparse_header->total_blks) {
314 info->mssg("sparse image write failure", response);