fastboot: sparse: make write_sparse_image useable for non-fastboot

write_sparse_image could be useful for non-fastboot users.
For ex a platform, without usb-device/fastboot support, could
get sparse images over tftp and write using the mmc command.
Or non-android systems could also leverage the sparse format.

Towards that, this patch removes anything fastboot specific from
the write_sparse_image implementation. Which includes making the
function return integer as error code and calls for fastboot logging
via an optional callback function 'mssg'.

Signed-off-by: Jassi Brar <jaswinder.singh@linaro.org>
This commit is contained in:
Jassi Brar 2018-04-06 12:05:09 +05:30 committed by Tom Rini
parent 32d3154e5c
commit 2f83f219bf
4 changed files with 45 additions and 41 deletions

View File

@ -329,6 +329,7 @@ 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;
struct sparse_storage sparse; struct sparse_storage sparse;
int err;
sparse_priv.dev_desc = dev_desc; sparse_priv.dev_desc = dev_desc;
@ -337,12 +338,15 @@ void fb_mmc_flash_write(const char *cmd, void *download_buffer,
sparse.size = info.size; sparse.size = info.size;
sparse.write = fb_mmc_sparse_write; sparse.write = fb_mmc_sparse_write;
sparse.reserve = fb_mmc_sparse_reserve; sparse.reserve = fb_mmc_sparse_reserve;
sparse.mssg = fastboot_fail;
printf("Flashing sparse image at offset " LBAFU "\n", printf("Flashing sparse image at offset " LBAFU "\n",
sparse.start); sparse.start);
sparse.priv = &sparse_priv; sparse.priv = &sparse_priv;
write_sparse_image(&sparse, cmd, download_buffer); err = write_sparse_image(&sparse, cmd, download_buffer);
if (!err)
fastboot_okay("");
} else { } else {
write_raw_image(dev_desc, &info, cmd, download_buffer, write_raw_image(dev_desc, &info, cmd, download_buffer,
download_bytes); download_bytes);

View File

@ -174,12 +174,15 @@ void fb_nand_flash_write(const char *cmd, void *download_buffer,
sparse.size = part->size / sparse.blksz; sparse.size = part->size / sparse.blksz;
sparse.write = fb_nand_sparse_write; sparse.write = fb_nand_sparse_write;
sparse.reserve = fb_nand_sparse_reserve; sparse.reserve = fb_nand_sparse_reserve;
sparse.mssg = fastboot_fail;
printf("Flashing sparse image at offset " LBAFU "\n", printf("Flashing sparse image at offset " LBAFU "\n",
sparse.start); sparse.start);
sparse.priv = &sparse_priv; sparse.priv = &sparse_priv;
write_sparse_image(&sparse, cmd, download_buffer); ret = write_sparse_image(&sparse, cmd, download_buffer);
if (!ret)
fastboot_okay("");
} else { } else {
printf("Flashing raw image at offset 0x%llx\n", printf("Flashing raw image at offset 0x%llx\n",
part->offset); part->offset);

View File

@ -41,7 +41,6 @@
#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>
@ -49,9 +48,10 @@
#define CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE (1024 * 512) #define CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE (1024 * 512)
#endif #endif
void write_sparse_image( static void default_log(const char *ignored) {}
struct sparse_storage *info, const char *part_name,
void *data) int write_sparse_image(struct sparse_storage *info,
const char *part_name, void *data)
{ {
lbaint_t blk; lbaint_t blk;
lbaint_t blkcnt; lbaint_t blkcnt;
@ -83,6 +83,9 @@ void write_sparse_image(
data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t)); data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
} }
if (!info->mssg)
info->mssg = default_log;
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);
debug("major_version: 0x%x\n", sparse_header->major_version); debug("major_version: 0x%x\n", sparse_header->major_version);
@ -101,8 +104,8 @@ void write_sparse_image(
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);
fastboot_fail("sparse image block size issue"); info->mssg("sparse image block size issue");
return; return -1;
} }
puts("Flashing Sparse Image\n"); puts("Flashing Sparse Image\n");
@ -136,18 +139,16 @@ void write_sparse_image(
case CHUNK_TYPE_RAW: case CHUNK_TYPE_RAW:
if (chunk_header->total_sz != if (chunk_header->total_sz !=
(sparse_header->chunk_hdr_sz + chunk_data_sz)) { (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
fastboot_fail( info->mssg("Bogus chunk size for chunk type Raw");
"Bogus chunk size for chunk type Raw"); return -1;
return;
} }
if (blk + blkcnt > info->start + info->size) { if (blk + blkcnt > info->start + info->size) {
printf( printf(
"%s: Request would exceed partition size!\n", "%s: Request would exceed partition size!\n",
__func__); __func__);
fastboot_fail( info->mssg("Request would exceed partition size!");
"Request would exceed partition size!"); return -1;
return;
} }
blks = info->write(info, blk, blkcnt, data); blks = info->write(info, blk, blkcnt, data);
@ -156,9 +157,8 @@ void write_sparse_image(
printf("%s: %s" LBAFU " [" LBAFU "]\n", printf("%s: %s" LBAFU " [" LBAFU "]\n",
__func__, "Write failed, block #", __func__, "Write failed, block #",
blk, blks); blk, blks);
fastboot_fail( info->mssg("flash write failure");
"flash write failure"); return -1;
return;
} }
blk += blks; blk += blks;
bytes_written += blkcnt * info->blksz; bytes_written += blkcnt * info->blksz;
@ -169,9 +169,8 @@ void write_sparse_image(
case CHUNK_TYPE_FILL: case CHUNK_TYPE_FILL:
if (chunk_header->total_sz != if (chunk_header->total_sz !=
(sparse_header->chunk_hdr_sz + sizeof(uint32_t))) { (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
fastboot_fail( info->mssg("Bogus chunk size for chunk type FILL");
"Bogus chunk size for chunk type FILL"); return -1;
return;
} }
fill_buf = (uint32_t *) fill_buf = (uint32_t *)
@ -180,9 +179,8 @@ void write_sparse_image(
info->blksz * fill_buf_num_blks, info->blksz * fill_buf_num_blks,
ARCH_DMA_MINALIGN)); ARCH_DMA_MINALIGN));
if (!fill_buf) { if (!fill_buf) {
fastboot_fail( info->mssg("Malloc failed for: CHUNK_TYPE_FILL");
"Malloc failed for: CHUNK_TYPE_FILL"); return -1;
return;
} }
fill_val = *(uint32_t *)data; fill_val = *(uint32_t *)data;
@ -198,9 +196,8 @@ void write_sparse_image(
printf( printf(
"%s: Request would exceed partition size!\n", "%s: Request would exceed partition size!\n",
__func__); __func__);
fastboot_fail( info->mssg("Request would exceed partition size!");
"Request would exceed partition size!"); return -1;
return;
} }
for (i = 0; i < blkcnt;) { for (i = 0; i < blkcnt;) {
@ -214,10 +211,9 @@ void write_sparse_image(
__func__, __func__,
"Write failed, block #", "Write failed, block #",
blk, j); blk, j);
fastboot_fail( info->mssg("flash write failure");
"flash write failure");
free(fill_buf); free(fill_buf);
return; return -1;
} }
blk += blks; blk += blks;
i += j; i += j;
@ -235,9 +231,8 @@ void write_sparse_image(
case CHUNK_TYPE_CRC32: case CHUNK_TYPE_CRC32:
if (chunk_header->total_sz != if (chunk_header->total_sz !=
sparse_header->chunk_hdr_sz) { sparse_header->chunk_hdr_sz) {
fastboot_fail( info->mssg("Bogus chunk size for chunk type Dont Care");
"Bogus chunk size for chunk type Dont Care"); return -1;
return;
} }
total_blocks += chunk_header->chunk_sz; total_blocks += chunk_header->chunk_sz;
data += chunk_data_sz; data += chunk_data_sz;
@ -246,8 +241,8 @@ void write_sparse_image(
default: default:
printf("%s: Unknown chunk type: %x\n", __func__, printf("%s: Unknown chunk type: %x\n", __func__,
chunk_header->chunk_type); chunk_header->chunk_type);
fastboot_fail("Unknown chunk type"); info->mssg("Unknown chunk type");
return; return -1;
} }
} }
@ -255,10 +250,10 @@ void write_sparse_image(
total_blocks, sparse_header->total_blks); total_blocks, sparse_header->total_blks);
printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name); printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
if (total_blocks != sparse_header->total_blks) if (total_blocks != sparse_header->total_blks) {
fastboot_fail("sparse image write failure"); info->mssg("sparse image write failure");
else return -1;
fastboot_okay(""); }
return; return 0;
} }

View File

@ -22,6 +22,8 @@ struct sparse_storage {
lbaint_t (*reserve)(struct sparse_storage *info, lbaint_t (*reserve)(struct sparse_storage *info,
lbaint_t blk, lbaint_t blk,
lbaint_t blkcnt); lbaint_t blkcnt);
void (*mssg)(const char *str);
}; };
static inline int is_sparse_image(void *buf) static inline int is_sparse_image(void *buf)
@ -35,5 +37,5 @@ static inline int is_sparse_image(void *buf)
return 0; return 0;
} }
void write_sparse_image(struct sparse_storage *info, const char *part_name, int write_sparse_image(struct sparse_storage *info, const char *part_name,
void *data); void *data);