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!
39 #include <image-sparse.h>
43 #include <sparse_format.h>
46 #include <linux/math64.h>
48 #ifndef CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE
49 #define CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE (1024 * 512)
52 void write_sparse_image(
53 struct sparse_storage *info, const char *part_name,
54 void *data, unsigned sz)
59 uint32_t bytes_written = 0;
62 unsigned int chunk_data_sz;
63 uint32_t *fill_buf = NULL;
65 sparse_header_t *sparse_header;
66 chunk_header_t *chunk_header;
67 uint32_t total_blocks = 0;
68 int fill_buf_num_blks;
72 fill_buf_num_blks = CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE / info->blksz;
74 /* Read and skip over sparse image header */
75 sparse_header = (sparse_header_t *)data;
77 data += sparse_header->file_hdr_sz;
78 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
80 * Skip the remaining bytes in a header that is longer than
83 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
86 debug("=== Sparse Image Header ===\n");
87 debug("magic: 0x%x\n", sparse_header->magic);
88 debug("major_version: 0x%x\n", sparse_header->major_version);
89 debug("minor_version: 0x%x\n", sparse_header->minor_version);
90 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
91 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
92 debug("blk_sz: %d\n", sparse_header->blk_sz);
93 debug("total_blks: %d\n", sparse_header->total_blks);
94 debug("total_chunks: %d\n", sparse_header->total_chunks);
97 * Verify that the sparse block size is a multiple of our
98 * storage backend block size
100 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
102 printf("%s: Sparse image block size issue [%u]\n",
103 __func__, sparse_header->blk_sz);
104 fastboot_fail("sparse image block size issue");
108 puts("Flashing Sparse Image\n");
110 /* Start processing chunks */
112 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
113 /* Read and skip over chunk header */
114 chunk_header = (chunk_header_t *)data;
115 data += sizeof(chunk_header_t);
117 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
118 debug("=== Chunk Header ===\n");
119 debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
120 debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
121 debug("total_size: 0x%x\n", chunk_header->total_sz);
124 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
126 * Skip the remaining bytes in a header that is longer
129 data += (sparse_header->chunk_hdr_sz -
130 sizeof(chunk_header_t));
133 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
134 blkcnt = chunk_data_sz / info->blksz;
135 switch (chunk_header->chunk_type) {
137 if (chunk_header->total_sz !=
138 (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
140 "Bogus chunk size for chunk type Raw");
144 if (blk + blkcnt > info->start + info->size) {
146 "%s: Request would exceed partition size!\n",
149 "Request would exceed partition size!");
153 blks = info->write(info, blk, blkcnt, data);
154 /* blks might be > blkcnt (eg. NAND bad-blocks) */
156 printf("%s: %s" LBAFU " [" LBAFU "]\n",
157 __func__, "Write failed, block #",
160 "flash write failure");
164 bytes_written += blkcnt * info->blksz;
165 total_blocks += chunk_header->chunk_sz;
166 data += chunk_data_sz;
169 case CHUNK_TYPE_FILL:
170 if (chunk_header->total_sz !=
171 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
173 "Bogus chunk size for chunk type FILL");
177 fill_buf = (uint32_t *)
178 memalign(ARCH_DMA_MINALIGN,
180 info->blksz * fill_buf_num_blks,
184 "Malloc failed for: CHUNK_TYPE_FILL");
188 fill_val = *(uint32_t *)data;
189 data = (char *)data + sizeof(uint32_t);
192 i < (info->blksz * fill_buf_num_blks /
195 fill_buf[i] = fill_val;
197 if (blk + blkcnt > info->start + info->size) {
199 "%s: Request would exceed partition size!\n",
202 "Request would exceed partition size!");
206 for (i = 0; i < blkcnt;) {
208 if (j > fill_buf_num_blks)
209 j = fill_buf_num_blks;
210 blks = info->write(info, blk, j, fill_buf);
211 /* blks might be > j (eg. NAND bad-blocks) */
213 printf("%s: %s " LBAFU " [%d]\n",
215 "Write failed, block #",
218 "flash write failure");
225 bytes_written += blkcnt * info->blksz;
226 total_blocks += chunk_data_sz / sparse_header->blk_sz;
230 case CHUNK_TYPE_DONT_CARE:
231 blk += info->reserve(info, blk, blkcnt);
232 total_blocks += chunk_header->chunk_sz;
235 case CHUNK_TYPE_CRC32:
236 if (chunk_header->total_sz !=
237 sparse_header->chunk_hdr_sz) {
239 "Bogus chunk size for chunk type Dont Care");
242 total_blocks += chunk_header->chunk_sz;
243 data += chunk_data_sz;
247 printf("%s: Unknown chunk type: %x\n", __func__,
248 chunk_header->chunk_type);
249 fastboot_fail("Unknown chunk type");
254 debug("Wrote %d blocks, expected to write %d blocks\n",
255 total_blocks, sparse_header->total_blks);
256 printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
258 if (total_blocks != sparse_header->total_blks)
259 fastboot_fail("sparse image write failure");