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> |
| 39 | #include <aboot.h> |
Maxime Ripard | 40aeeda | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 40 | #include <div64.h> |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 41 | #include <errno.h> |
Steve Rae | e6ca1ad | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 42 | #include <malloc.h> |
| 43 | #include <part.h> |
| 44 | #include <sparse_format.h> |
| 45 | |
Maxime Ripard | 40aeeda | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 46 | #include <linux/math64.h> |
| 47 | |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 48 | typedef struct sparse_buffer { |
| 49 | void *data; |
| 50 | u32 length; |
| 51 | u32 repeat; |
| 52 | u16 type; |
| 53 | } sparse_buffer_t; |
| 54 | |
| 55 | static unsigned int sparse_get_chunk_data_size(sparse_header_t *sparse, |
| 56 | chunk_header_t *chunk) |
| 57 | { |
| 58 | return chunk->total_sz - sparse->chunk_hdr_sz; |
| 59 | } |
| 60 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 61 | static unsigned int sparse_block_size_to_storage(unsigned int size, |
| 62 | sparse_storage_t *storage, |
| 63 | sparse_header_t *sparse) |
| 64 | { |
| 65 | return size * sparse->blk_sz / storage->block_sz; |
| 66 | } |
| 67 | |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 68 | static bool sparse_chunk_has_buffer(chunk_header_t *chunk) |
| 69 | { |
| 70 | switch (chunk->chunk_type) { |
| 71 | case CHUNK_TYPE_RAW: |
| 72 | case CHUNK_TYPE_FILL: |
| 73 | return true; |
| 74 | |
| 75 | default: |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
Maxime Ripard | bb83c0f | 2015-10-15 14:34:10 +0200 | [diff] [blame] | 80 | static sparse_header_t *sparse_parse_header(void **data) |
| 81 | { |
| 82 | /* Read and skip over sparse image header */ |
| 83 | sparse_header_t *sparse_header = (sparse_header_t *) *data; |
| 84 | |
| 85 | *data += sparse_header->file_hdr_sz; |
| 86 | |
| 87 | debug("=== Sparse Image Header ===\n"); |
| 88 | debug("magic: 0x%x\n", sparse_header->magic); |
| 89 | debug("major_version: 0x%x\n", sparse_header->major_version); |
| 90 | debug("minor_version: 0x%x\n", sparse_header->minor_version); |
| 91 | debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz); |
| 92 | debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz); |
| 93 | debug("blk_sz: %d\n", sparse_header->blk_sz); |
| 94 | debug("total_blks: %d\n", sparse_header->total_blks); |
| 95 | debug("total_chunks: %d\n", sparse_header->total_chunks); |
| 96 | |
| 97 | return sparse_header; |
| 98 | } |
| 99 | |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 100 | static int sparse_parse_fill_chunk(sparse_header_t *sparse, |
| 101 | chunk_header_t *chunk) |
| 102 | { |
| 103 | unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk); |
| 104 | |
| 105 | if (chunk_data_sz != sizeof(uint32_t)) |
| 106 | return -EINVAL; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int sparse_parse_raw_chunk(sparse_header_t *sparse, |
| 112 | chunk_header_t *chunk) |
| 113 | { |
| 114 | unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk); |
| 115 | |
| 116 | /* Check if the data size is a multiple of the main block size */ |
| 117 | if (chunk_data_sz % sparse->blk_sz) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | /* Check that the chunk size is consistent */ |
| 121 | if ((chunk_data_sz / sparse->blk_sz) != chunk->chunk_sz) |
| 122 | return -EINVAL; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static chunk_header_t *sparse_parse_chunk(sparse_header_t *sparse, |
| 128 | void **image) |
| 129 | { |
| 130 | chunk_header_t *chunk = (chunk_header_t *) *image; |
| 131 | int ret; |
| 132 | |
| 133 | debug("=== Chunk Header ===\n"); |
| 134 | debug("chunk_type: 0x%x\n", chunk->chunk_type); |
| 135 | debug("chunk_data_sz: 0x%x\n", chunk->chunk_sz); |
| 136 | debug("total_size: 0x%x\n", chunk->total_sz); |
| 137 | |
| 138 | switch (chunk->chunk_type) { |
| 139 | case CHUNK_TYPE_RAW: |
| 140 | ret = sparse_parse_raw_chunk(sparse, chunk); |
| 141 | if (ret) |
| 142 | return NULL; |
| 143 | break; |
| 144 | |
| 145 | case CHUNK_TYPE_FILL: |
| 146 | ret = sparse_parse_fill_chunk(sparse, chunk); |
| 147 | if (ret) |
| 148 | return NULL; |
| 149 | break; |
| 150 | |
| 151 | case CHUNK_TYPE_DONT_CARE: |
| 152 | case CHUNK_TYPE_CRC32: |
| 153 | debug("Ignoring chunk\n"); |
| 154 | break; |
| 155 | |
| 156 | default: |
| 157 | printf("%s: Unknown chunk type: %x\n", __func__, |
| 158 | chunk->chunk_type); |
| 159 | return NULL; |
| 160 | } |
| 161 | |
| 162 | *image += sparse->chunk_hdr_sz; |
| 163 | |
| 164 | return chunk; |
| 165 | } |
| 166 | |
| 167 | static int sparse_get_fill_buffer(sparse_header_t *sparse, |
| 168 | chunk_header_t *chunk, |
| 169 | sparse_buffer_t *buffer, |
| 170 | unsigned int blk_sz, |
| 171 | void *data) |
| 172 | { |
| 173 | int i; |
| 174 | |
| 175 | buffer->type = CHUNK_TYPE_FILL; |
| 176 | |
| 177 | /* |
| 178 | * We create a buffer of one block, and ask it to be |
| 179 | * repeated as many times as needed. |
| 180 | */ |
| 181 | buffer->length = blk_sz; |
| 182 | buffer->repeat = (chunk->chunk_sz * sparse->blk_sz) / blk_sz; |
| 183 | |
| 184 | buffer->data = memalign(ARCH_DMA_MINALIGN, |
| 185 | ROUNDUP(blk_sz, |
| 186 | ARCH_DMA_MINALIGN)); |
| 187 | if (!buffer->data) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | for (i = 0; i < (buffer->length / sizeof(uint32_t)); i++) |
| 191 | ((uint32_t *)buffer->data)[i] = *(uint32_t *)(data); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int sparse_get_raw_buffer(sparse_header_t *sparse, |
| 197 | chunk_header_t *chunk, |
| 198 | sparse_buffer_t *buffer, |
| 199 | unsigned int blk_sz, |
| 200 | void *data) |
| 201 | { |
| 202 | unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk); |
| 203 | |
| 204 | buffer->type = CHUNK_TYPE_RAW; |
| 205 | buffer->length = chunk_data_sz; |
| 206 | buffer->data = data; |
| 207 | buffer->repeat = 1; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static sparse_buffer_t *sparse_get_data_buffer(sparse_header_t *sparse, |
| 213 | chunk_header_t *chunk, |
| 214 | unsigned int blk_sz, |
| 215 | void **image) |
| 216 | { |
| 217 | unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk); |
| 218 | sparse_buffer_t *buffer; |
| 219 | void *data = *image; |
| 220 | int ret; |
| 221 | |
| 222 | *image += chunk_data_sz; |
| 223 | |
| 224 | if (!sparse_chunk_has_buffer(chunk)) |
| 225 | return NULL; |
| 226 | |
| 227 | buffer = calloc(sizeof(sparse_buffer_t), 1); |
| 228 | if (!buffer) |
| 229 | return NULL; |
| 230 | |
| 231 | switch (chunk->chunk_type) { |
| 232 | case CHUNK_TYPE_RAW: |
| 233 | ret = sparse_get_raw_buffer(sparse, chunk, buffer, blk_sz, |
| 234 | data); |
| 235 | if (ret) |
| 236 | return NULL; |
| 237 | break; |
| 238 | |
| 239 | case CHUNK_TYPE_FILL: |
| 240 | ret = sparse_get_fill_buffer(sparse, chunk, buffer, blk_sz, |
| 241 | data); |
| 242 | if (ret) |
| 243 | return NULL; |
| 244 | break; |
| 245 | |
| 246 | default: |
| 247 | return NULL; |
| 248 | } |
| 249 | |
| 250 | debug("=== Buffer ===\n"); |
| 251 | debug("length: 0x%x\n", buffer->length); |
| 252 | debug("repeat: 0x%x\n", buffer->repeat); |
| 253 | debug("type: 0x%x\n", buffer->type); |
| 254 | debug("data: 0x%p\n", buffer->data); |
| 255 | |
| 256 | return buffer; |
| 257 | } |
| 258 | |
| 259 | static void sparse_put_data_buffer(sparse_buffer_t *buffer) |
| 260 | { |
| 261 | if (buffer->type == CHUNK_TYPE_FILL) |
| 262 | free(buffer->data); |
| 263 | |
| 264 | free(buffer); |
| 265 | } |
| 266 | |
Maxime Ripard | 6c9e00e | 2015-10-15 14:34:15 +0200 | [diff] [blame^] | 267 | int store_sparse_image(sparse_storage_t *storage, void *storage_priv, |
| 268 | unsigned int session_id, void *data) |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 269 | { |
Maxime Ripard | 40aeeda | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 270 | unsigned int chunk, offset; |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 271 | sparse_header_t *sparse_header; |
| 272 | chunk_header_t *chunk_header; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 273 | sparse_buffer_t *buffer; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 274 | uint32_t start; |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 275 | uint32_t total_blocks = 0; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 276 | uint32_t skipped = 0; |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 277 | int i; |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 278 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 279 | debug("=== Storage ===\n"); |
| 280 | debug("name: %s\n", storage->name); |
| 281 | debug("block_size: 0x%x\n", storage->block_sz); |
| 282 | debug("start: 0x%x\n", storage->start); |
| 283 | debug("size: 0x%x\n", storage->size); |
| 284 | debug("write: 0x%p\n", storage->write); |
| 285 | debug("priv: 0x%p\n", storage_priv); |
| 286 | |
Maxime Ripard | bb83c0f | 2015-10-15 14:34:10 +0200 | [diff] [blame] | 287 | sparse_header = sparse_parse_header(&data); |
| 288 | if (!sparse_header) { |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 289 | printf("sparse header issue\n"); |
| 290 | return -EINVAL; |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Maxime Ripard | 40aeeda | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 293 | /* |
| 294 | * Verify that the sparse block size is a multiple of our |
| 295 | * storage backend block size |
| 296 | */ |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 297 | div_u64_rem(sparse_header->blk_sz, storage->block_sz, &offset); |
Maxime Ripard | 40aeeda | 2015-10-15 14:34:12 +0200 | [diff] [blame] | 298 | if (offset) { |
Steve Rae | e6ca1ad | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 299 | printf("%s: Sparse image block size issue [%u]\n", |
| 300 | __func__, sparse_header->blk_sz); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 301 | return -EINVAL; |
Steve Rae | e6ca1ad | 2014-09-03 10:05:54 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | puts("Flashing Sparse Image\n"); |
| 305 | |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 306 | /* Start processing chunks */ |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 307 | start = storage->start; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 308 | for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) { |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 309 | uint32_t blkcnt; |
| 310 | |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 311 | chunk_header = sparse_parse_chunk(sparse_header, &data); |
| 312 | if (!chunk_header) { |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 313 | printf("Unknown chunk type"); |
| 314 | return -EINVAL; |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 315 | } |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 316 | |
| 317 | /* |
| 318 | * If we have a DONT_CARE type, just skip the blocks |
| 319 | * and go on parsing the rest of the chunks |
| 320 | */ |
| 321 | if (chunk_header->chunk_type == CHUNK_TYPE_DONT_CARE) { |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 322 | skipped += sparse_block_size_to_storage(chunk_header->chunk_sz, |
| 323 | storage, |
| 324 | sparse_header); |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | |
| 328 | /* Retrieve the buffer we're going to write */ |
| 329 | buffer = sparse_get_data_buffer(sparse_header, chunk_header, |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 330 | storage->block_sz, &data); |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 331 | if (!buffer) |
| 332 | continue; |
| 333 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 334 | blkcnt = (buffer->length / storage->block_sz) * buffer->repeat; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 335 | |
| 336 | if ((start + total_blocks + blkcnt) > |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 337 | (storage->start + storage->size)) { |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 338 | printf("%s: Request would exceed partition size!\n", |
| 339 | __func__); |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 340 | return -EINVAL; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | for (i = 0; i < buffer->repeat; i++) { |
| 344 | unsigned long buffer_blk_cnt; |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 345 | int ret; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 346 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 347 | buffer_blk_cnt = buffer->length / storage->block_sz; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 348 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 349 | ret = storage->write(storage, storage_priv, |
| 350 | start + total_blocks, |
| 351 | buffer_blk_cnt, |
| 352 | buffer->data); |
| 353 | if (ret < 0) { |
| 354 | printf("%s: Write %d failed %d\n", |
| 355 | __func__, i, ret); |
| 356 | return ret; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 359 | total_blocks += ret; |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | sparse_put_data_buffer(buffer); |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Maxime Ripard | 7bfc3b1 | 2015-10-15 14:34:11 +0200 | [diff] [blame] | 365 | debug("Wrote %d blocks, skipped %d, expected to write %d blocks\n", |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 366 | total_blocks, skipped, |
| 367 | sparse_block_size_to_storage(sparse_header->total_blks, |
| 368 | storage, sparse_header)); |
| 369 | printf("........ wrote %d blocks to '%s'\n", total_blocks, |
| 370 | storage->name); |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 371 | |
Maxime Ripard | a5d1e04 | 2015-10-15 14:34:14 +0200 | [diff] [blame] | 372 | if ((total_blocks + skipped) != |
| 373 | sparse_block_size_to_storage(sparse_header->total_blks, |
| 374 | storage, sparse_header)) { |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 375 | printf("sparse image write failure\n"); |
| 376 | return -EIO; |
| 377 | } |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 378 | |
Maxime Ripard | 3c8f98f | 2015-10-15 14:34:13 +0200 | [diff] [blame] | 379 | return 0; |
Steve Rae | b4e4bbe | 2014-09-03 10:05:51 -0700 | [diff] [blame] | 380 | } |