2014-09-03 17:05:51 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009, Google Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
|
2014-09-03 17:05:54 +00:00
|
|
|
* Portions Copyright 2014 Broadcom Corporation.
|
2014-09-03 17:05:51 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of The Linux Foundation nor
|
|
|
|
* the names of its contributors may be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2014-09-03 17:05:53 +00:00
|
|
|
* NOTE:
|
|
|
|
* Although it is very similar, this license text is not identical
|
|
|
|
* to the "BSD-3-Clause", therefore, DO NOT MODIFY THIS LICENSE TEXT!
|
2014-09-03 17:05:51 +00:00
|
|
|
*/
|
|
|
|
|
2014-09-03 17:05:54 +00:00
|
|
|
#include <config.h>
|
|
|
|
#include <common.h>
|
2015-10-15 12:34:12 +00:00
|
|
|
#include <div64.h>
|
2015-10-15 12:34:11 +00:00
|
|
|
#include <errno.h>
|
2015-10-15 12:34:19 +00:00
|
|
|
#include <image-sparse.h>
|
2014-09-03 17:05:54 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <part.h>
|
|
|
|
#include <sparse_format.h>
|
|
|
|
|
2015-10-15 12:34:12 +00:00
|
|
|
#include <linux/math64.h>
|
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
typedef struct sparse_buffer {
|
|
|
|
void *data;
|
|
|
|
u32 length;
|
|
|
|
u32 repeat;
|
|
|
|
u16 type;
|
|
|
|
} sparse_buffer_t;
|
|
|
|
|
2015-10-15 12:34:16 +00:00
|
|
|
static uint32_t last_offset;
|
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
static unsigned int sparse_get_chunk_data_size(sparse_header_t *sparse,
|
|
|
|
chunk_header_t *chunk)
|
|
|
|
{
|
|
|
|
return chunk->total_sz - sparse->chunk_hdr_sz;
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:14 +00:00
|
|
|
static unsigned int sparse_block_size_to_storage(unsigned int size,
|
|
|
|
sparse_storage_t *storage,
|
|
|
|
sparse_header_t *sparse)
|
|
|
|
{
|
2016-02-03 10:13:55 +00:00
|
|
|
return (unsigned int)lldiv((uint64_t)size * sparse->blk_sz,
|
|
|
|
storage->block_sz);
|
2015-10-15 12:34:14 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
static bool sparse_chunk_has_buffer(chunk_header_t *chunk)
|
|
|
|
{
|
|
|
|
switch (chunk->chunk_type) {
|
|
|
|
case CHUNK_TYPE_RAW:
|
|
|
|
case CHUNK_TYPE_FILL:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:10 +00:00
|
|
|
static sparse_header_t *sparse_parse_header(void **data)
|
|
|
|
{
|
|
|
|
/* Read and skip over sparse image header */
|
|
|
|
sparse_header_t *sparse_header = (sparse_header_t *) *data;
|
|
|
|
|
|
|
|
*data += sparse_header->file_hdr_sz;
|
|
|
|
|
|
|
|
debug("=== Sparse Image Header ===\n");
|
|
|
|
debug("magic: 0x%x\n", sparse_header->magic);
|
|
|
|
debug("major_version: 0x%x\n", sparse_header->major_version);
|
|
|
|
debug("minor_version: 0x%x\n", sparse_header->minor_version);
|
|
|
|
debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
|
|
|
|
debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
|
|
|
|
debug("blk_sz: %d\n", sparse_header->blk_sz);
|
|
|
|
debug("total_blks: %d\n", sparse_header->total_blks);
|
|
|
|
debug("total_chunks: %d\n", sparse_header->total_chunks);
|
|
|
|
|
|
|
|
return sparse_header;
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
static int sparse_parse_fill_chunk(sparse_header_t *sparse,
|
|
|
|
chunk_header_t *chunk)
|
|
|
|
{
|
|
|
|
unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
|
|
|
|
|
|
|
|
if (chunk_data_sz != sizeof(uint32_t))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sparse_parse_raw_chunk(sparse_header_t *sparse,
|
|
|
|
chunk_header_t *chunk)
|
|
|
|
{
|
|
|
|
unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
|
|
|
|
|
|
|
|
/* Check if the data size is a multiple of the main block size */
|
|
|
|
if (chunk_data_sz % sparse->blk_sz)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Check that the chunk size is consistent */
|
|
|
|
if ((chunk_data_sz / sparse->blk_sz) != chunk->chunk_sz)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static chunk_header_t *sparse_parse_chunk(sparse_header_t *sparse,
|
|
|
|
void **image)
|
|
|
|
{
|
|
|
|
chunk_header_t *chunk = (chunk_header_t *) *image;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
debug("=== Chunk Header ===\n");
|
|
|
|
debug("chunk_type: 0x%x\n", chunk->chunk_type);
|
|
|
|
debug("chunk_data_sz: 0x%x\n", chunk->chunk_sz);
|
|
|
|
debug("total_size: 0x%x\n", chunk->total_sz);
|
|
|
|
|
|
|
|
switch (chunk->chunk_type) {
|
|
|
|
case CHUNK_TYPE_RAW:
|
|
|
|
ret = sparse_parse_raw_chunk(sparse, chunk);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_TYPE_FILL:
|
|
|
|
ret = sparse_parse_fill_chunk(sparse, chunk);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_TYPE_DONT_CARE:
|
|
|
|
case CHUNK_TYPE_CRC32:
|
|
|
|
debug("Ignoring chunk\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("%s: Unknown chunk type: %x\n", __func__,
|
|
|
|
chunk->chunk_type);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*image += sparse->chunk_hdr_sz;
|
|
|
|
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sparse_get_fill_buffer(sparse_header_t *sparse,
|
|
|
|
chunk_header_t *chunk,
|
|
|
|
sparse_buffer_t *buffer,
|
|
|
|
unsigned int blk_sz,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
buffer->type = CHUNK_TYPE_FILL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We create a buffer of one block, and ask it to be
|
|
|
|
* repeated as many times as needed.
|
|
|
|
*/
|
|
|
|
buffer->length = blk_sz;
|
|
|
|
buffer->repeat = (chunk->chunk_sz * sparse->blk_sz) / blk_sz;
|
|
|
|
|
|
|
|
buffer->data = memalign(ARCH_DMA_MINALIGN,
|
|
|
|
ROUNDUP(blk_sz,
|
|
|
|
ARCH_DMA_MINALIGN));
|
|
|
|
if (!buffer->data)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (i = 0; i < (buffer->length / sizeof(uint32_t)); i++)
|
|
|
|
((uint32_t *)buffer->data)[i] = *(uint32_t *)(data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sparse_get_raw_buffer(sparse_header_t *sparse,
|
|
|
|
chunk_header_t *chunk,
|
|
|
|
sparse_buffer_t *buffer,
|
|
|
|
unsigned int blk_sz,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
|
|
|
|
|
|
|
|
buffer->type = CHUNK_TYPE_RAW;
|
|
|
|
buffer->length = chunk_data_sz;
|
|
|
|
buffer->data = data;
|
|
|
|
buffer->repeat = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sparse_buffer_t *sparse_get_data_buffer(sparse_header_t *sparse,
|
|
|
|
chunk_header_t *chunk,
|
|
|
|
unsigned int blk_sz,
|
|
|
|
void **image)
|
|
|
|
{
|
|
|
|
unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
|
|
|
|
sparse_buffer_t *buffer;
|
|
|
|
void *data = *image;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*image += chunk_data_sz;
|
|
|
|
|
|
|
|
if (!sparse_chunk_has_buffer(chunk))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buffer = calloc(sizeof(sparse_buffer_t), 1);
|
|
|
|
if (!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
switch (chunk->chunk_type) {
|
|
|
|
case CHUNK_TYPE_RAW:
|
|
|
|
ret = sparse_get_raw_buffer(sparse, chunk, buffer, blk_sz,
|
|
|
|
data);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHUNK_TYPE_FILL:
|
|
|
|
ret = sparse_get_fill_buffer(sparse, chunk, buffer, blk_sz,
|
|
|
|
data);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("=== Buffer ===\n");
|
|
|
|
debug("length: 0x%x\n", buffer->length);
|
|
|
|
debug("repeat: 0x%x\n", buffer->repeat);
|
|
|
|
debug("type: 0x%x\n", buffer->type);
|
|
|
|
debug("data: 0x%p\n", buffer->data);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sparse_put_data_buffer(sparse_buffer_t *buffer)
|
|
|
|
{
|
|
|
|
if (buffer->type == CHUNK_TYPE_FILL)
|
|
|
|
free(buffer->data);
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
fastboot: Implement flashing session counter
The fastboot flash command that writes an image to a partition works in
several steps:
1 - Retrieve the maximum size the device can download through the
"max-download-size" variable
2 - Retrieve the partition type through the "partition-type:%s" variable,
that indicates whether or not the partition needs to be erased (even
though the fastboot client has minimal support for that)
3a - If the image is smaller than what the device can handle, send the image
and flash it.
3b - If the image is larger than what the device can handle, create a
sparse image, and split it in several chunks that would fit. Send the
chunk, flash it, repeat until we have no more data to send.
However, in the 3b case, the subsequent transfers have no particular
identifiers, the protocol just assumes that you would resume the writes
where you left it.
While doing so works well, it also means that flashing two subsequent
images on the same partition (for example because the user made a mistake)
would not work withouth flashing another partition or rebooting the board,
which is not really intuitive.
Since we have always the same pattern, we can however maintain a counter
that will be reset every time the client will retrieve max-download-size,
and incremented after each buffer will be flashed, that will allow us to
tell whether we should simply resume the flashing where we were, or start
back at the beginning of the partition.
Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
2015-10-15 12:34:15 +00:00
|
|
|
int store_sparse_image(sparse_storage_t *storage, void *storage_priv,
|
|
|
|
unsigned int session_id, void *data)
|
2014-09-03 17:05:51 +00:00
|
|
|
{
|
2015-10-15 12:34:12 +00:00
|
|
|
unsigned int chunk, offset;
|
2014-09-03 17:05:51 +00:00
|
|
|
sparse_header_t *sparse_header;
|
|
|
|
chunk_header_t *chunk_header;
|
2015-10-15 12:34:11 +00:00
|
|
|
sparse_buffer_t *buffer;
|
2015-10-15 12:34:14 +00:00
|
|
|
uint32_t start;
|
2014-09-03 17:05:51 +00:00
|
|
|
uint32_t total_blocks = 0;
|
2015-10-15 12:34:11 +00:00
|
|
|
uint32_t skipped = 0;
|
2014-09-03 17:05:51 +00:00
|
|
|
int i;
|
|
|
|
|
2015-10-15 12:34:14 +00:00
|
|
|
debug("=== Storage ===\n");
|
|
|
|
debug("name: %s\n", storage->name);
|
|
|
|
debug("block_size: 0x%x\n", storage->block_sz);
|
|
|
|
debug("start: 0x%x\n", storage->start);
|
|
|
|
debug("size: 0x%x\n", storage->size);
|
|
|
|
debug("write: 0x%p\n", storage->write);
|
|
|
|
debug("priv: 0x%p\n", storage_priv);
|
|
|
|
|
2015-10-15 12:34:10 +00:00
|
|
|
sparse_header = sparse_parse_header(&data);
|
|
|
|
if (!sparse_header) {
|
2015-10-15 12:34:13 +00:00
|
|
|
printf("sparse header issue\n");
|
|
|
|
return -EINVAL;
|
2014-09-03 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:12 +00:00
|
|
|
/*
|
|
|
|
* Verify that the sparse block size is a multiple of our
|
|
|
|
* storage backend block size
|
|
|
|
*/
|
2015-10-15 12:34:14 +00:00
|
|
|
div_u64_rem(sparse_header->blk_sz, storage->block_sz, &offset);
|
2015-10-15 12:34:12 +00:00
|
|
|
if (offset) {
|
2014-09-03 17:05:54 +00:00
|
|
|
printf("%s: Sparse image block size issue [%u]\n",
|
|
|
|
__func__, sparse_header->blk_sz);
|
2015-10-15 12:34:13 +00:00
|
|
|
return -EINVAL;
|
2014-09-03 17:05:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:16 +00:00
|
|
|
/*
|
|
|
|
* If it's a new flashing session, start at the beginning of
|
|
|
|
* the partition. If not, then simply resume where we were.
|
|
|
|
*/
|
|
|
|
if (session_id > 0)
|
|
|
|
start = last_offset;
|
|
|
|
else
|
|
|
|
start = storage->start;
|
|
|
|
|
|
|
|
printf("Flashing sparse image on partition %s at offset 0x%x (ID: %d)\n",
|
|
|
|
storage->name, start * storage->block_sz, session_id);
|
2014-09-03 17:05:54 +00:00
|
|
|
|
2014-09-03 17:05:51 +00:00
|
|
|
/* Start processing chunks */
|
2015-10-15 12:34:11 +00:00
|
|
|
for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
|
2015-10-15 12:34:14 +00:00
|
|
|
uint32_t blkcnt;
|
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
chunk_header = sparse_parse_chunk(sparse_header, &data);
|
|
|
|
if (!chunk_header) {
|
2015-10-15 12:34:13 +00:00
|
|
|
printf("Unknown chunk type");
|
|
|
|
return -EINVAL;
|
2014-09-03 17:05:54 +00:00
|
|
|
}
|
2014-09-03 17:05:51 +00:00
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
/*
|
|
|
|
* If we have a DONT_CARE type, just skip the blocks
|
|
|
|
* and go on parsing the rest of the chunks
|
|
|
|
*/
|
|
|
|
if (chunk_header->chunk_type == CHUNK_TYPE_DONT_CARE) {
|
2016-02-09 19:19:10 +00:00
|
|
|
blkcnt = sparse_block_size_to_storage(chunk_header->chunk_sz,
|
|
|
|
storage,
|
|
|
|
sparse_header);
|
|
|
|
#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
|
|
|
|
total_blocks += blkcnt;
|
|
|
|
#endif
|
|
|
|
skipped += blkcnt;
|
2015-10-15 12:34:11 +00:00
|
|
|
continue;
|
2014-09-03 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
/* Retrieve the buffer we're going to write */
|
|
|
|
buffer = sparse_get_data_buffer(sparse_header, chunk_header,
|
2015-10-15 12:34:14 +00:00
|
|
|
storage->block_sz, &data);
|
2015-10-15 12:34:11 +00:00
|
|
|
if (!buffer)
|
|
|
|
continue;
|
2014-09-03 17:05:54 +00:00
|
|
|
|
2015-10-15 12:34:14 +00:00
|
|
|
blkcnt = (buffer->length / storage->block_sz) * buffer->repeat;
|
2014-09-03 17:05:51 +00:00
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
if ((start + total_blocks + blkcnt) >
|
2015-10-15 12:34:14 +00:00
|
|
|
(storage->start + storage->size)) {
|
2015-10-15 12:34:11 +00:00
|
|
|
printf("%s: Request would exceed partition size!\n",
|
|
|
|
__func__);
|
2015-10-15 12:34:13 +00:00
|
|
|
return -EINVAL;
|
2015-10-15 12:34:11 +00:00
|
|
|
}
|
2014-09-03 17:05:51 +00:00
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
for (i = 0; i < buffer->repeat; i++) {
|
|
|
|
unsigned long buffer_blk_cnt;
|
2015-10-15 12:34:14 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
buffer_blk_cnt = buffer->length / storage->block_sz;
|
|
|
|
|
|
|
|
ret = storage->write(storage, storage_priv,
|
|
|
|
start + total_blocks,
|
|
|
|
buffer_blk_cnt,
|
|
|
|
buffer->data);
|
|
|
|
if (ret < 0) {
|
|
|
|
printf("%s: Write %d failed %d\n",
|
|
|
|
__func__, i, ret);
|
|
|
|
return ret;
|
2014-09-03 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:14 +00:00
|
|
|
total_blocks += ret;
|
2014-09-03 17:05:51 +00:00
|
|
|
}
|
2015-10-15 12:34:11 +00:00
|
|
|
|
|
|
|
sparse_put_data_buffer(buffer);
|
2014-09-03 17:05:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 12:34:11 +00:00
|
|
|
debug("Wrote %d blocks, skipped %d, expected to write %d blocks\n",
|
2015-10-15 12:34:14 +00:00
|
|
|
total_blocks, skipped,
|
|
|
|
sparse_block_size_to_storage(sparse_header->total_blks,
|
|
|
|
storage, sparse_header));
|
|
|
|
printf("........ wrote %d blocks to '%s'\n", total_blocks,
|
|
|
|
storage->name);
|
|
|
|
|
2016-02-09 19:19:10 +00:00
|
|
|
if (total_blocks !=
|
2015-10-15 12:34:14 +00:00
|
|
|
sparse_block_size_to_storage(sparse_header->total_blks,
|
|
|
|
storage, sparse_header)) {
|
2015-10-15 12:34:13 +00:00
|
|
|
printf("sparse image write failure\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2014-09-03 17:05:51 +00:00
|
|
|
|
2015-10-15 12:34:16 +00:00
|
|
|
last_offset = start + total_blocks;
|
|
|
|
|
2015-10-15 12:34:13 +00:00
|
|
|
return 0;
|
2014-09-03 17:05:51 +00:00
|
|
|
}
|