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> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 38 | #include <blk.h> |
Maxime Ripard | 6af49bc | 2015-10-15 14:34:19 +0200 | [diff] [blame] | 39 | #include <image-sparse.h> |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 40 | #include <div64.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 41 | #include <log.h> |
Steve Rae | 82fce73 | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 42 | #include <malloc.h> |
| 43 | #include <part.h> |
| 44 | #include <sparse_format.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 45 | #include <asm/cache.h> |
Steve Rae | 82fce73 | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 46 | |
Maxime Ripard | 61715e7 | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 47 | #include <linux/math64.h> |
qianfan Zhao | bf3544e | 2021-11-16 09:35:38 +0800 | [diff] [blame] | 48 | #include <linux/err.h> |
Maxime Ripard | 61715e7 | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 49 | |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 50 | static void default_log(const char *ignored, char *response) {} |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 51 | |
qianfan Zhao | bf3544e | 2021-11-16 09:35:38 +0800 | [diff] [blame] | 52 | static lbaint_t write_sparse_chunk_raw(struct sparse_storage *info, |
| 53 | lbaint_t blk, lbaint_t blkcnt, |
| 54 | void *data, |
| 55 | char *response) |
| 56 | { |
Mattijs Korpershoek | 8b24d14 | 2023-07-07 10:13:34 +0200 | [diff] [blame] | 57 | lbaint_t n = blkcnt, write_blks, blks = 0; |
| 58 | lbaint_t aligned_buf_blks = FASTBOOT_MAX_BLK_WRITE; |
qianfan Zhao | bf3544e | 2021-11-16 09:35:38 +0800 | [diff] [blame] | 59 | uint32_t *aligned_buf = NULL; |
| 60 | |
| 61 | if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF)) { |
| 62 | write_blks = info->write(info, blk, n, data); |
| 63 | if (write_blks < n) |
| 64 | goto write_fail; |
| 65 | |
| 66 | return write_blks; |
| 67 | } |
| 68 | |
| 69 | aligned_buf = memalign(ARCH_DMA_MINALIGN, info->blksz * aligned_buf_blks); |
| 70 | if (!aligned_buf) { |
| 71 | info->mssg("Malloc failed for: CHUNK_TYPE_RAW", response); |
| 72 | return -ENOMEM; |
| 73 | } |
| 74 | |
| 75 | while (blkcnt > 0) { |
| 76 | n = min(aligned_buf_blks, blkcnt); |
| 77 | memcpy(aligned_buf, data, n * info->blksz); |
| 78 | |
| 79 | /* write_blks might be > n due to NAND bad-blocks */ |
| 80 | write_blks = info->write(info, blk + blks, n, aligned_buf); |
| 81 | if (write_blks < n) { |
| 82 | free(aligned_buf); |
| 83 | goto write_fail; |
| 84 | } |
| 85 | |
| 86 | blks += write_blks; |
| 87 | data += n * info->blksz; |
| 88 | blkcnt -= n; |
| 89 | } |
| 90 | |
| 91 | free(aligned_buf); |
| 92 | return blks; |
| 93 | |
| 94 | write_fail: |
| 95 | if (IS_ERR_VALUE(write_blks)) { |
| 96 | printf("%s: Write failed, block #" LBAFU " [" LBAFU "] (%lld)\n", |
| 97 | __func__, blk + blks, n, (long long)write_blks); |
| 98 | info->mssg("flash write failure", response); |
| 99 | return write_blks; |
| 100 | } |
| 101 | |
| 102 | /* write_blks < n */ |
| 103 | printf("%s: Write failed, block #" LBAFU " [" LBAFU "]\n", |
| 104 | __func__, blk + blks, n); |
| 105 | info->mssg("flash write failure(incomplete)", response); |
| 106 | return -1; |
| 107 | } |
| 108 | |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 109 | int write_sparse_image(struct sparse_storage *info, |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 110 | const char *part_name, void *data, char *response) |
Maxime Ripard | 93c5610 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 111 | { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 112 | lbaint_t blk; |
| 113 | lbaint_t blkcnt; |
| 114 | lbaint_t blks; |
Sean Anderson | 2af5af3 | 2021-05-27 12:02:34 -0400 | [diff] [blame] | 115 | uint64_t bytes_written = 0; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 116 | unsigned int chunk; |
| 117 | unsigned int offset; |
Sean Anderson | 2af5af3 | 2021-05-27 12:02:34 -0400 | [diff] [blame] | 118 | uint64_t chunk_data_sz; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 119 | uint32_t *fill_buf = NULL; |
| 120 | uint32_t fill_val; |
| 121 | sparse_header_t *sparse_header; |
| 122 | chunk_header_t *chunk_header; |
| 123 | uint32_t total_blocks = 0; |
Steve Rae | 7765cf5 | 2016-06-07 11:19:39 -0700 | [diff] [blame] | 124 | int fill_buf_num_blks; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 125 | int i; |
Steve Rae | 7765cf5 | 2016-06-07 11:19:39 -0700 | [diff] [blame] | 126 | int j; |
| 127 | |
Alex Kiernan | c568bcb | 2018-05-29 15:30:52 +0000 | [diff] [blame] | 128 | fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz; |
Maxime Ripard | 93c5610 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 129 | |
Maxime Ripard | aae9674 | 2015-10-15 14:34:10 +0200 | [diff] [blame] | 130 | /* Read and skip over sparse image header */ |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 131 | sparse_header = (sparse_header_t *)data; |
Maxime Ripard | aae9674 | 2015-10-15 14:34:10 +0200 | [diff] [blame] | 132 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 133 | data += sparse_header->file_hdr_sz; |
| 134 | if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) { |
| 135 | /* |
| 136 | * Skip the remaining bytes in a header that is longer than |
| 137 | * we expected. |
| 138 | */ |
| 139 | data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t)); |
| 140 | } |
Maxime Ripard | aae9674 | 2015-10-15 14:34:10 +0200 | [diff] [blame] | 141 | |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 142 | if (!info->mssg) |
| 143 | info->mssg = default_log; |
| 144 | |
Maxime Ripard | aae9674 | 2015-10-15 14:34:10 +0200 | [diff] [blame] | 145 | debug("=== Sparse Image Header ===\n"); |
| 146 | debug("magic: 0x%x\n", sparse_header->magic); |
| 147 | debug("major_version: 0x%x\n", sparse_header->major_version); |
| 148 | debug("minor_version: 0x%x\n", sparse_header->minor_version); |
| 149 | debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 150 | debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 151 | debug("blk_sz: %d\n", sparse_header->blk_sz); |
| 152 | debug("total_blks: %d\n", sparse_header->total_blks); |
| 153 | debug("total_chunks: %d\n", sparse_header->total_chunks); |
| 154 | |
Maxime Ripard | 93c5610 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 155 | /* |
Maxime Ripard | 61715e7 | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 156 | * Verify that the sparse block size is a multiple of our |
| 157 | * storage backend block size |
| 158 | */ |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 159 | div_u64_rem(sparse_header->blk_sz, info->blksz, &offset); |
Maxime Ripard | 61715e7 | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 160 | if (offset) { |
Steve Rae | 82fce73 | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 161 | printf("%s: Sparse image block size issue [%u]\n", |
| 162 | __func__, sparse_header->blk_sz); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 163 | info->mssg("sparse image block size issue", response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 164 | return -1; |
Steve Rae | 82fce73 | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Steve Rae | f1f112f | 2016-06-07 11:19:35 -0700 | [diff] [blame] | 167 | puts("Flashing Sparse Image\n"); |
Steve Rae | 82fce73 | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 168 | |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 169 | /* Start processing chunks */ |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 170 | blk = info->start; |
Maxime Ripard | 93c5610 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 171 | for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) { |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 172 | /* Read and skip over chunk header */ |
| 173 | chunk_header = (chunk_header_t *)data; |
| 174 | data += sizeof(chunk_header_t); |
Maxime Ripard | a58e9c7 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 175 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 176 | if (chunk_header->chunk_type != CHUNK_TYPE_RAW) { |
| 177 | debug("=== Chunk Header ===\n"); |
| 178 | debug("chunk_type: 0x%x\n", chunk_header->chunk_type); |
| 179 | debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz); |
| 180 | debug("total_size: 0x%x\n", chunk_header->total_sz); |
Steve Rae | 82fce73 | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 181 | } |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 182 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 183 | if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) { |
| 184 | /* |
| 185 | * Skip the remaining bytes in a header that is longer |
| 186 | * than we expected. |
| 187 | */ |
| 188 | data += (sparse_header->chunk_hdr_sz - |
| 189 | sizeof(chunk_header_t)); |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Sean Anderson | 2af5af3 | 2021-05-27 12:02:34 -0400 | [diff] [blame] | 192 | chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz; |
| 193 | blkcnt = DIV_ROUND_UP_ULL(chunk_data_sz, info->blksz); |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 194 | switch (chunk_header->chunk_type) { |
| 195 | case CHUNK_TYPE_RAW: |
| 196 | if (chunk_header->total_sz != |
| 197 | (sparse_header->chunk_hdr_sz + chunk_data_sz)) { |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 198 | info->mssg("Bogus chunk size for chunk type Raw", |
| 199 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 200 | return -1; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 201 | } |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 202 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 203 | if (blk + blkcnt > info->start + info->size) { |
| 204 | printf( |
| 205 | "%s: Request would exceed partition size!\n", |
| 206 | __func__); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 207 | info->mssg("Request would exceed partition size!", |
| 208 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 209 | return -1; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 210 | } |
Steve Rae | 82fce73 | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 211 | |
qianfan Zhao | bf3544e | 2021-11-16 09:35:38 +0800 | [diff] [blame] | 212 | blks = write_sparse_chunk_raw(info, blk, blkcnt, |
| 213 | data, response); |
| 214 | if (blks < 0) |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 215 | return -1; |
qianfan Zhao | bf3544e | 2021-11-16 09:35:38 +0800 | [diff] [blame] | 216 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 217 | blk += blks; |
Sean Anderson | 2af5af3 | 2021-05-27 12:02:34 -0400 | [diff] [blame] | 218 | bytes_written += ((u64)blkcnt) * info->blksz; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 219 | total_blocks += chunk_header->chunk_sz; |
| 220 | data += chunk_data_sz; |
| 221 | break; |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 222 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 223 | case CHUNK_TYPE_FILL: |
| 224 | if (chunk_header->total_sz != |
| 225 | (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) { |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 226 | info->mssg("Bogus chunk size for chunk type FILL", response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 227 | return -1; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 228 | } |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 229 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 230 | fill_buf = (uint32_t *) |
| 231 | memalign(ARCH_DMA_MINALIGN, |
Steve Rae | 7765cf5 | 2016-06-07 11:19:39 -0700 | [diff] [blame] | 232 | ROUNDUP( |
| 233 | info->blksz * fill_buf_num_blks, |
| 234 | ARCH_DMA_MINALIGN)); |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 235 | if (!fill_buf) { |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 236 | info->mssg("Malloc failed for: CHUNK_TYPE_FILL", |
| 237 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 238 | return -1; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 239 | } |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 240 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 241 | fill_val = *(uint32_t *)data; |
| 242 | data = (char *)data + sizeof(uint32_t); |
| 243 | |
Steve Rae | 7765cf5 | 2016-06-07 11:19:39 -0700 | [diff] [blame] | 244 | for (i = 0; |
| 245 | i < (info->blksz * fill_buf_num_blks / |
| 246 | sizeof(fill_val)); |
| 247 | i++) |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 248 | fill_buf[i] = fill_val; |
| 249 | |
| 250 | if (blk + blkcnt > info->start + info->size) { |
| 251 | printf( |
| 252 | "%s: Request would exceed partition size!\n", |
| 253 | __func__); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 254 | info->mssg("Request would exceed partition size!", |
| 255 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 256 | return -1; |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 257 | } |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 258 | |
Steve Rae | 7765cf5 | 2016-06-07 11:19:39 -0700 | [diff] [blame] | 259 | for (i = 0; i < blkcnt;) { |
| 260 | j = blkcnt - i; |
| 261 | if (j > fill_buf_num_blks) |
| 262 | j = fill_buf_num_blks; |
| 263 | blks = info->write(info, blk, j, fill_buf); |
| 264 | /* blks might be > j (eg. NAND bad-blocks) */ |
| 265 | if (blks < j) { |
| 266 | printf("%s: %s " LBAFU " [%d]\n", |
| 267 | __func__, |
| 268 | "Write failed, block #", |
| 269 | blk, j); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 270 | info->mssg("flash write failure", |
| 271 | response); |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 272 | free(fill_buf); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 273 | return -1; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 274 | } |
| 275 | blk += blks; |
Steve Rae | 7765cf5 | 2016-06-07 11:19:39 -0700 | [diff] [blame] | 276 | i += j; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 277 | } |
Sean Anderson | 2af5af3 | 2021-05-27 12:02:34 -0400 | [diff] [blame] | 278 | bytes_written += ((u64)blkcnt) * info->blksz; |
| 279 | total_blocks += DIV_ROUND_UP_ULL(chunk_data_sz, |
| 280 | sparse_header->blk_sz); |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 281 | free(fill_buf); |
| 282 | break; |
Maxime Ripard | 93c5610 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 283 | |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 284 | case CHUNK_TYPE_DONT_CARE: |
Steve Rae | 8a99be8 | 2016-06-07 11:19:38 -0700 | [diff] [blame] | 285 | blk += info->reserve(info, blk, blkcnt); |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 286 | total_blocks += chunk_header->chunk_sz; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 287 | break; |
| 288 | |
| 289 | case CHUNK_TYPE_CRC32: |
| 290 | if (chunk_header->total_sz != |
Wojciech Nizinski | b8147eb | 2023-09-25 12:37:15 +0200 | [diff] [blame] | 291 | sparse_header->chunk_hdr_sz + sizeof(uint32_t)) { |
| 292 | info->mssg("Bogus chunk size for chunk type CRC32", |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 293 | response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 294 | return -1; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 295 | } |
| 296 | total_blocks += chunk_header->chunk_sz; |
| 297 | data += chunk_data_sz; |
| 298 | break; |
| 299 | |
| 300 | default: |
| 301 | printf("%s: Unknown chunk type: %x\n", __func__, |
| 302 | chunk_header->chunk_type); |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 303 | info->mssg("Unknown chunk type", response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 304 | return -1; |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 305 | } |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Steve Rae | 407f0f5 | 2016-02-09 11:19:11 -0800 | [diff] [blame] | 308 | debug("Wrote %d blocks, expected to write %d blocks\n", |
Steve Rae | 8cf58db | 2016-06-07 11:19:36 -0700 | [diff] [blame] | 309 | total_blocks, sparse_header->total_blks); |
Sean Anderson | 2af5af3 | 2021-05-27 12:02:34 -0400 | [diff] [blame] | 310 | printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name); |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 311 | |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 312 | if (total_blocks != sparse_header->total_blks) { |
Alex Kiernan | 27b69de | 2018-05-29 15:30:40 +0000 | [diff] [blame] | 313 | info->mssg("sparse image write failure", response); |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 314 | return -1; |
| 315 | } |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 316 | |
Jassi Brar | 427e669 | 2018-04-06 12:05:09 +0530 | [diff] [blame] | 317 | return 0; |
Steve Rae | 7b86dff | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 318 | } |