Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2009, Google Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved. |
| 6 | * |
| 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 |
| 17 | * permission. |
| 18 | * |
| 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. |
| 30 | * |
| 31 | */ |
| 32 | |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 33 | void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz) |
| 34 | { |
| 35 | unsigned int chunk; |
| 36 | unsigned int chunk_data_sz; |
| 37 | uint32_t *fill_buf = NULL; |
| 38 | uint32_t fill_val; |
| 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; |
| 46 | int i; |
| 47 | uint8_t lun = 0; |
| 48 | |
| 49 | index = partition_get_index(arg); |
| 50 | ptn = partition_get_offset(index); |
| 51 | if(ptn == 0) { |
| 52 | fastboot_fail("partition table doesn't exist"); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | size = partition_get_size(index); |
| 57 | if (ROUND_TO_PAGE(sz,511) > size) { |
| 58 | fastboot_fail("size too large"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | lun = partition_get_lun(index); |
| 63 | mmc_set_lun(lun); |
| 64 | |
| 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"); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | data += sparse_header->file_hdr_sz; |
| 73 | if(sparse_header->file_hdr_sz > sizeof(sparse_header_t)) |
| 74 | { |
| 75 | /* Skip the remaining bytes in a header that is longer than |
| 76 | * we expected. |
| 77 | */ |
| 78 | data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t)); |
| 79 | } |
| 80 | |
| 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); |
| 90 | |
| 91 | /* Start processing chunks */ |
| 92 | for (chunk=0; chunk<sparse_header->total_chunks; chunk++) |
| 93 | { |
| 94 | /* Read and skip over chunk header */ |
| 95 | chunk_header = (chunk_header_t *) data; |
| 96 | data += sizeof(chunk_header_t); |
| 97 | |
| 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); |
| 102 | |
| 103 | if(sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) |
| 104 | { |
| 105 | /* Skip the remaining bytes in a header that is longer than |
| 106 | * we expected. |
| 107 | */ |
| 108 | data += (sparse_header->chunk_hdr_sz - sizeof(chunk_header_t)); |
| 109 | } |
| 110 | |
| 111 | chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz; |
| 112 | switch (chunk_header->chunk_type) |
| 113 | { |
| 114 | case CHUNK_TYPE_RAW: |
| 115 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 116 | chunk_data_sz)) |
| 117 | { |
| 118 | fastboot_fail("Bogus chunk size for chunk type Raw"); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 123 | chunk_data_sz, |
| 124 | (unsigned int*)data)) |
| 125 | { |
| 126 | fastboot_fail("flash write failure"); |
| 127 | return; |
| 128 | } |
| 129 | total_blocks += chunk_header->chunk_sz; |
| 130 | data += chunk_data_sz; |
| 131 | break; |
| 132 | |
| 133 | case CHUNK_TYPE_FILL: |
| 134 | if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz + |
| 135 | sizeof(uint32_t))) |
| 136 | { |
| 137 | fastboot_fail("Bogus chunk size for chunk type FILL"); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE)); |
| 142 | if (!fill_buf) |
| 143 | { |
| 144 | fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL"); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | fill_val = *(uint32_t *)data; |
| 149 | data = (char *) data + sizeof(uint32_t); |
| 150 | chunk_blk_cnt = chunk_data_sz / sparse_header->blk_sz; |
| 151 | |
| 152 | for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++) |
| 153 | { |
| 154 | fill_buf[i] = fill_val; |
| 155 | } |
| 156 | |
| 157 | for (i = 0; i < chunk_blk_cnt; i++) |
| 158 | { |
| 159 | if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz), |
| 160 | sparse_header->blk_sz, |
| 161 | fill_buf)) |
| 162 | { |
| 163 | fastboot_fail("flash write failure"); |
| 164 | free(fill_buf); |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | total_blocks++; |
| 169 | } |
| 170 | |
| 171 | free(fill_buf); |
| 172 | break; |
| 173 | |
| 174 | case CHUNK_TYPE_DONT_CARE: |
| 175 | total_blocks += chunk_header->chunk_sz; |
| 176 | break; |
| 177 | |
| 178 | case CHUNK_TYPE_CRC: |
| 179 | if(chunk_header->total_sz != sparse_header->chunk_hdr_sz) |
| 180 | { |
| 181 | fastboot_fail("Bogus chunk size for chunk type Dont Care"); |
| 182 | return; |
| 183 | } |
| 184 | total_blocks += chunk_header->chunk_sz; |
| 185 | data += chunk_data_sz; |
| 186 | break; |
| 187 | |
| 188 | default: |
| 189 | dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type); |
| 190 | fastboot_fail("Unknown chunk type"); |
| 191 | return; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n", |
| 196 | total_blocks, sparse_header->total_blks); |
| 197 | |
| 198 | if(total_blocks != sparse_header->total_blks) |
| 199 | { |
| 200 | fastboot_fail("sparse image write failure"); |
| 201 | } |
| 202 | |
| 203 | fastboot_okay(""); |
| 204 | return; |
| 205 | } |