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