fastboot: sparse: resync common/image-sparse.c (part 1)

This file originally came from upstream code.

While retaining the storage abstraction feature, this is the first
set of the changes required to resync with the
  cmd_flash_mmc_sparse_img()
in the file
  aboot.c
from
  https://us.codeaurora.org/cgit/quic/la/kernel/lk/plain/app/aboot/aboot.c?h=LE.BR.1.2.1

Signed-off-by: Steve Rae <srae@broadcom.com>
This commit is contained in:
Steve Rae 2016-06-07 11:19:36 -07:00 committed by Tom Rini
parent 64ece84854
commit cc0f08cd34
4 changed files with 213 additions and 355 deletions

View file

@ -7,12 +7,10 @@
#include <config.h> #include <config.h>
#include <common.h> #include <common.h>
#include <blk.h> #include <blk.h>
#include <errno.h>
#include <fastboot.h> #include <fastboot.h>
#include <fb_mmc.h> #include <fb_mmc.h>
#include <image-sparse.h> #include <image-sparse.h>
#include <part.h> #include <part.h>
#include <sparse_format.h>
#include <mmc.h> #include <mmc.h>
#include <div64.h> #include <div64.h>
@ -48,22 +46,13 @@ static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
return ret; return ret;
} }
static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
static int fb_mmc_sparse_write(struct sparse_storage *storage, lbaint_t blk, lbaint_t blkcnt, const void *buffer)
void *priv,
unsigned int offset,
unsigned int size,
char *data)
{ {
struct fb_mmc_sparse *sparse = priv; struct fb_mmc_sparse *sparse = info->priv;
struct blk_desc *dev_desc = sparse->dev_desc; struct blk_desc *dev_desc = sparse->dev_desc;
int ret;
ret = blk_dwrite(dev_desc, offset, size, data); return blk_dwrite(dev_desc, blk, blkcnt, buffer);
if (!ret)
return -EIO;
return ret;
} }
static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info, static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
@ -139,26 +128,25 @@ void fb_mmc_flash_write(const char *cmd, void *download_buffer,
if (is_sparse_image(download_buffer)) { if (is_sparse_image(download_buffer)) {
struct fb_mmc_sparse sparse_priv; struct fb_mmc_sparse sparse_priv;
sparse_storage_t sparse; struct sparse_storage sparse;
sparse_priv.dev_desc = dev_desc; sparse_priv.dev_desc = dev_desc;
sparse.block_sz = info.blksz; sparse.blksz = info.blksz;
sparse.start = info.start; sparse.start = info.start;
sparse.size = info.size; sparse.size = info.size;
sparse.name = cmd;
sparse.write = fb_mmc_sparse_write; sparse.write = fb_mmc_sparse_write;
printf("Flashing sparse image at offset " LBAFU "\n", printf("Flashing sparse image at offset " LBAFU "\n",
info.start); sparse.start);
store_sparse_image(&sparse, &sparse_priv, download_buffer); sparse.priv = &sparse_priv;
write_sparse_image(&sparse, cmd, download_buffer,
download_bytes, response_str);
} else { } else {
write_raw_image(dev_desc, &info, cmd, download_buffer, write_raw_image(dev_desc, &info, cmd, download_buffer,
download_bytes); download_bytes);
} }
fastboot_okay(response_str, "");
} }
void fb_mmc_erase(const char *cmd, char *response) void fb_mmc_erase(const char *cmd, char *response)

View file

@ -10,7 +10,6 @@
#include <fastboot.h> #include <fastboot.h>
#include <image-sparse.h> #include <image-sparse.h>
#include <sparse_format.h>
#include <linux/mtd/mtd.h> #include <linux/mtd/mtd.h>
#include <jffs2/jffs2.h> #include <jffs2/jffs2.h>
@ -19,7 +18,7 @@
static char *response_str; static char *response_str;
struct fb_nand_sparse { struct fb_nand_sparse {
struct mtd_info *nand; struct mtd_info *mtd;
struct part_info *part; struct part_info *part;
}; };
@ -105,30 +104,32 @@ static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
buffer, flags); buffer, flags);
} }
static int fb_nand_sparse_write(struct sparse_storage *storage, static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
void *priv, lbaint_t blk, lbaint_t blkcnt, const void *buffer)
unsigned int offset,
unsigned int size,
char *data)
{ {
struct fb_nand_sparse *sparse = priv; struct fb_nand_sparse *sparse = info->priv;
size_t written; size_t written;
int ret; int ret;
ret = _fb_nand_write(sparse->nand, sparse->part, data, ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
offset * storage->block_sz, blk * info->blksz,
size * storage->block_sz, &written); blkcnt * info->blksz, &written);
if (ret < 0) { if (ret < 0) {
printf("Failed to write sparse chunk\n"); printf("Failed to write sparse chunk\n");
return ret; return ret;
} }
return written / storage->block_sz; /* TODO - verify that the value "written" includes the "bad-blocks" ... */
/*
* the return value must be 'blkcnt' ("good-blocks") plus the
* number of "bad-blocks" encountered within this space...
*/
return written / info->blksz;
} }
void fb_nand_flash_write(const char *partname, void fb_nand_flash_write(const char *cmd, void *download_buffer,
void *download_buffer, unsigned int download_bytes, unsigned int download_bytes, char *response)
char *response)
{ {
struct part_info *part; struct part_info *part;
struct mtd_info *mtd = NULL; struct mtd_info *mtd = NULL;
@ -137,7 +138,7 @@ void fb_nand_flash_write(const char *partname,
/* initialize the response buffer */ /* initialize the response buffer */
response_str = response; response_str = response;
ret = fb_nand_lookup(partname, response, &mtd, &part); ret = fb_nand_lookup(cmd, response, &mtd, &part);
if (ret) { if (ret) {
error("invalid NAND device"); error("invalid NAND device");
fastboot_fail(response_str, "invalid NAND device"); fastboot_fail(response_str, "invalid NAND device");
@ -150,19 +151,22 @@ void fb_nand_flash_write(const char *partname,
if (is_sparse_image(download_buffer)) { if (is_sparse_image(download_buffer)) {
struct fb_nand_sparse sparse_priv; struct fb_nand_sparse sparse_priv;
sparse_storage_t sparse; struct sparse_storage sparse;
sparse_priv.nand = mtd; sparse_priv.mtd = mtd;
sparse_priv.part = part; sparse_priv.part = part;
sparse.block_sz = mtd->writesize; sparse.blksz = mtd->writesize;
sparse.start = part->offset / sparse.block_sz; sparse.start = part->offset / sparse.blksz;
sparse.size = part->size / sparse.block_sz; sparse.size = part->size / sparse.blksz;
sparse.name = part->name;
sparse.write = fb_nand_sparse_write; sparse.write = fb_nand_sparse_write;
ret = store_sparse_image(&sparse, &sparse_priv, printf("Flashing sparse image at offset " LBAFU "\n",
download_buffer); sparse.start);
sparse.priv = &sparse_priv;
write_sparse_image(&sparse, cmd, download_buffer,
download_bytes, response_str);
} else { } else {
printf("Flashing raw image at offset 0x%llx\n", printf("Flashing raw image at offset 0x%llx\n",
part->offset); part->offset);
@ -182,7 +186,7 @@ void fb_nand_flash_write(const char *partname,
fastboot_okay(response_str, ""); fastboot_okay(response_str, "");
} }
void fb_nand_erase(const char *partname, char *response) void fb_nand_erase(const char *cmd, char *response)
{ {
struct part_info *part; struct part_info *part;
struct mtd_info *mtd = NULL; struct mtd_info *mtd = NULL;
@ -191,7 +195,7 @@ void fb_nand_erase(const char *partname, char *response)
/* initialize the response buffer */ /* initialize the response buffer */
response_str = response; response_str = response;
ret = fb_nand_lookup(partname, response, &mtd, &part); ret = fb_nand_lookup(cmd, response, &mtd, &part);
if (ret) { if (ret) {
error("invalid NAND device"); error("invalid NAND device");
fastboot_fail(response_str, "invalid NAND device"); fastboot_fail(response_str, "invalid NAND device");

View file

@ -36,54 +36,44 @@
#include <config.h> #include <config.h>
#include <common.h> #include <common.h>
#include <div64.h>
#include <errno.h>
#include <image-sparse.h> #include <image-sparse.h>
#include <div64.h>
#include <malloc.h> #include <malloc.h>
#include <part.h> #include <part.h>
#include <sparse_format.h> #include <sparse_format.h>
#include <fastboot.h>
#include <linux/math64.h> #include <linux/math64.h>
typedef struct sparse_buffer { void write_sparse_image(
void *data; struct sparse_storage *info, const char *part_name,
u32 length; void *data, unsigned sz, char *response_str)
u32 repeat;
u16 type;
} sparse_buffer_t;
static unsigned int sparse_get_chunk_data_size(sparse_header_t *sparse,
chunk_header_t *chunk)
{ {
return chunk->total_sz - sparse->chunk_hdr_sz; lbaint_t blk;
} lbaint_t blkcnt;
lbaint_t blks;
uint32_t bytes_written = 0;
unsigned int chunk;
unsigned int offset;
unsigned int chunk_data_sz;
uint32_t *fill_buf = NULL;
uint32_t fill_val;
sparse_header_t *sparse_header;
chunk_header_t *chunk_header;
uint32_t total_blocks = 0;
int i;
static unsigned int sparse_block_size_to_storage(unsigned int size,
sparse_storage_t *storage,
sparse_header_t *sparse)
{
return (unsigned int)lldiv((uint64_t)size * sparse->blk_sz,
storage->block_sz);
}
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;
}
}
static sparse_header_t *sparse_parse_header(void **data)
{
/* Read and skip over sparse image header */ /* Read and skip over sparse image header */
sparse_header_t *sparse_header = (sparse_header_t *) *data; sparse_header = (sparse_header_t *)data;
*data += sparse_header->file_hdr_sz; data += sparse_header->file_hdr_sz;
if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
/*
* Skip the remaining bytes in a header that is longer than
* we expected.
*/
data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
}
debug("=== Sparse Image Header ===\n"); debug("=== Sparse Image Header ===\n");
debug("magic: 0x%x\n", sparse_header->magic); debug("magic: 0x%x\n", sparse_header->magic);
@ -95,289 +85,164 @@ static sparse_header_t *sparse_parse_header(void **data)
debug("total_blks: %d\n", sparse_header->total_blks); debug("total_blks: %d\n", sparse_header->total_blks);
debug("total_chunks: %d\n", sparse_header->total_chunks); debug("total_chunks: %d\n", sparse_header->total_chunks);
return sparse_header;
}
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);
}
int store_sparse_image(sparse_storage_t *storage,
void *storage_priv, void *data)
{
unsigned int chunk, offset;
sparse_header_t *sparse_header;
chunk_header_t *chunk_header;
sparse_buffer_t *buffer;
uint32_t start;
uint32_t total_blocks = 0;
int i;
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);
sparse_header = sparse_parse_header(&data);
if (!sparse_header) {
printf("sparse header issue\n");
return -EINVAL;
}
/* /*
* Verify that the sparse block size is a multiple of our * Verify that the sparse block size is a multiple of our
* storage backend block size * storage backend block size
*/ */
div_u64_rem(sparse_header->blk_sz, storage->block_sz, &offset); div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
if (offset) { if (offset) {
printf("%s: Sparse image block size issue [%u]\n", printf("%s: Sparse image block size issue [%u]\n",
__func__, sparse_header->blk_sz); __func__, sparse_header->blk_sz);
return -EINVAL; fastboot_fail(response_str, "sparse image block size issue");
return;
} }
puts("Flashing Sparse Image\n"); puts("Flashing Sparse Image\n");
/* Start processing chunks */ /* Start processing chunks */
start = storage->start; blk = info->start;
for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) { for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
uint32_t blkcnt; /* Read and skip over chunk header */
chunk_header = (chunk_header_t *)data;
data += sizeof(chunk_header_t);
chunk_header = sparse_parse_chunk(sparse_header, &data); if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
if (!chunk_header) { debug("=== Chunk Header ===\n");
printf("Unknown chunk type"); debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
return -EINVAL; debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
debug("total_size: 0x%x\n", chunk_header->total_sz);
} }
if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
/* /*
* If we have a DONT_CARE type, just skip the blocks * Skip the remaining bytes in a header that is longer
* and go on parsing the rest of the chunks * than we expected.
*/ */
if (chunk_header->chunk_type == CHUNK_TYPE_DONT_CARE) { data += (sparse_header->chunk_hdr_sz -
blkcnt = sparse_block_size_to_storage(chunk_header->chunk_sz, sizeof(chunk_header_t));
storage,
sparse_header);
#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
total_blocks += blkcnt;
#endif
continue;
} }
/* Retrieve the buffer we're going to write */ chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
buffer = sparse_get_data_buffer(sparse_header, chunk_header, blkcnt = chunk_data_sz / info->blksz;
storage->block_sz, &data); switch (chunk_header->chunk_type) {
if (!buffer) case CHUNK_TYPE_RAW:
continue; if (chunk_header->total_sz !=
(sparse_header->chunk_hdr_sz + chunk_data_sz)) {
fastboot_fail(response_str,
"Bogus chunk size for chunk type Raw");
return;
}
blkcnt = (buffer->length / storage->block_sz) * buffer->repeat; if (blk + blkcnt > info->start + info->size) {
printf(
if ((start + total_blocks + blkcnt) > "%s: Request would exceed partition size!\n",
(storage->start + storage->size)) {
printf("%s: Request would exceed partition size!\n",
__func__); __func__);
return -EINVAL; fastboot_fail(response_str,
"Request would exceed partition size!");
return;
} }
for (i = 0; i < buffer->repeat; i++) { blks = info->write(info, blk, blkcnt, data);
unsigned long buffer_blk_cnt; /* blks might be > blkcnt (eg. NAND bad-blocks) */
int ret; if (blks < blkcnt) {
printf("%s: %s" LBAFU " [" LBAFU "]\n",
__func__, "Write failed, block #",
blk, blks);
fastboot_fail(response_str,
"flash write failure");
return;
}
blk += blks;
bytes_written += blkcnt * info->blksz;
total_blocks += chunk_header->chunk_sz;
data += chunk_data_sz;
break;
buffer_blk_cnt = buffer->length / storage->block_sz; case CHUNK_TYPE_FILL:
if (chunk_header->total_sz !=
ret = storage->write(storage, storage_priv, (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
start + total_blocks, fastboot_fail(response_str,
buffer_blk_cnt, "Bogus chunk size for chunk type FILL");
buffer->data); return;
if (ret < 0) {
printf("%s: Write %d failed %d\n",
__func__, i, ret);
return ret;
} }
total_blocks += ret; fill_buf = (uint32_t *)
memalign(ARCH_DMA_MINALIGN,
ROUNDUP(info->blksz,
ARCH_DMA_MINALIGN));
if (!fill_buf) {
fastboot_fail(response_str,
"Malloc failed for: CHUNK_TYPE_FILL");
return;
} }
sparse_put_data_buffer(buffer); fill_val = *(uint32_t *)data;
data = (char *)data + sizeof(uint32_t);
for (i = 0; i < (info->blksz / sizeof(fill_val)); i++)
fill_buf[i] = fill_val;
if (blk + blkcnt > info->start + info->size) {
printf(
"%s: Request would exceed partition size!\n",
__func__);
fastboot_fail(response_str,
"Request would exceed partition size!");
return;
}
for (i = 0; i < blkcnt; i++) {
blks = info->write(info, blk, 1, fill_buf);
/* blks might be > 1 (eg. NAND bad-blocks) */
if (blks < 1) {
printf("%s: %s, block # " LBAFU "\n",
__func__, "Write failed", blk);
fastboot_fail(response_str,
"flash write failure");
free(fill_buf);
return;
}
blk += blks;
}
bytes_written += blkcnt * info->blksz;
total_blocks += chunk_data_sz / sparse_header->blk_sz;
free(fill_buf);
break;
case CHUNK_TYPE_DONT_CARE:
#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
blk += blkcnt;
total_blocks += chunk_header->chunk_sz;
#endif
break;
case CHUNK_TYPE_CRC32:
if (chunk_header->total_sz !=
sparse_header->chunk_hdr_sz) {
fastboot_fail(response_str,
"Bogus chunk size for chunk type Dont Care");
return;
}
total_blocks += chunk_header->chunk_sz;
data += chunk_data_sz;
break;
default:
printf("%s: Unknown chunk type: %x\n", __func__,
chunk_header->chunk_type);
fastboot_fail(response_str, "Unknown chunk type");
return;
}
} }
debug("Wrote %d blocks, expected to write %d blocks\n", debug("Wrote %d blocks, expected to write %d blocks\n",
total_blocks, total_blocks, sparse_header->total_blks);
sparse_block_size_to_storage(sparse_header->total_blks, printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
storage, sparse_header));
printf("........ wrote %d blocks to '%s'\n", total_blocks,
storage->name);
if (total_blocks != if (total_blocks != sparse_header->total_blks)
sparse_block_size_to_storage(sparse_header->total_blks, fastboot_fail(response_str, "sparse image write failure");
storage, sparse_header)) { else
printf("sparse image write failure\n"); fastboot_okay(response_str, "");
return -EIO;
}
return 0; return;
} }

View file

@ -9,16 +9,17 @@
#define ROUNDUP(x, y) (((x) + ((y) - 1)) & ~((y) - 1)) #define ROUNDUP(x, y) (((x) + ((y) - 1)) & ~((y) - 1))
typedef struct sparse_storage { struct sparse_storage {
unsigned int block_sz; lbaint_t blksz;
unsigned int start; lbaint_t start;
unsigned int size; lbaint_t size;
const char *name; void *priv;
int (*write)(struct sparse_storage *storage, void *priv, lbaint_t (*write)(struct sparse_storage *info,
unsigned int offset, unsigned int size, lbaint_t blk,
char *data); lbaint_t blkcnt,
} sparse_storage_t; const void *buffer);
};
static inline int is_sparse_image(void *buf) static inline int is_sparse_image(void *buf)
{ {
@ -31,5 +32,5 @@ static inline int is_sparse_image(void *buf)
return 0; return 0;
} }
int store_sparse_image(sparse_storage_t *storage, void *storage_priv, void write_sparse_image(struct sparse_storage *info, const char *part_name,
void *data); void *data, unsigned sz, char *response_str);