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