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