2 * Copyright (c) 2009, Google Inc.
5 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of The Linux Foundation nor
15 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
36 unsigned int chunk_data_sz;
37 uint32_t *fill_buf = NULL;
39 uint32_t chunk_blk_cnt = 0;
40 sparse_header_t *sparse_header;
41 chunk_header_t *chunk_header;
42 uint32_t total_blocks = 0;
43 unsigned long long ptn = 0;
44 unsigned long long size = 0;
45 int index = INVALID_PTN;
49 index = partition_get_index(arg);
50 ptn = partition_get_offset(index);
52 fastboot_fail("partition table doesn't exist");
56 size = partition_get_size(index);
57 if (ROUND_TO_PAGE(sz,511) > size) {
58 fastboot_fail("size too large");
62 lun = partition_get_lun(index);
65 /* Read and skip over sparse image header */
66 sparse_header = (sparse_header_t *) data;
67 if ((sparse_header->total_blks * sparse_header->blk_sz) > size) {
68 fastboot_fail("size too large");
72 data += sparse_header->file_hdr_sz;
73 if(sparse_header->file_hdr_sz > sizeof(sparse_header_t))
75 /* Skip the remaining bytes in a header that is longer than
78 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
81 dprintf (SPEW, "=== Sparse Image Header ===\n");
82 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
83 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
84 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
85 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
86 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
87 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
88 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
89 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
91 /* Start processing chunks */
92 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
94 /* Read and skip over chunk header */
95 chunk_header = (chunk_header_t *) data;
96 data += sizeof(chunk_header_t);
98 dprintf (SPEW, "=== Chunk Header ===\n");
99 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
100 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
101 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
103 if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
105 /* Skip the remaining bytes in a header that is longer than
108 data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t));
111 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
112 switch (chunk_header->chunk_type)
115 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
118 fastboot_fail("Bogus chunk size for chunk type Raw");
122 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
124 (unsigned int*)data))
126 fastboot_fail("flash write failure");
129 total_blocks += chunk_header->chunk_sz;
130 data += chunk_data_sz;
133 case CHUNK_TYPE_FILL:
134 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
137 fastboot_fail("Bogus chunk size for chunk type FILL");
141 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
144 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
148 fill_val = *(uint32_t *)data;
149 data = (char *) data + sizeof(uint32_t);
150 chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz;
152 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
154 fill_buf[i] = fill_val;
157 for (i = 0; i < chunk_blk_cnt; i++)
159 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
160 sparse_header->blk_sz,
163 fastboot_fail("flash write failure");
174 case CHUNK_TYPE_DONT_CARE:
175 total_blocks += chunk_header->chunk_sz;
179 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
181 fastboot_fail("Bogus chunk size for chunk type Dont Care");
184 total_blocks += chunk_header->chunk_sz;
185 data += chunk_data_sz;
189 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
190 fastboot_fail("Unknown chunk type");
195 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
196 total_blocks, sparse_header->total_blks);
198 if(total_blocks != sparse_header->total_blks)
200 fastboot_fail("sparse image write failure");