[U-Boot] [RFC PATCH v1 1/5] dfu: Refactor fastboot_okay/fail to take response

Alex Kiernan
2018-04-24T09:37:03Z

Add the response string as a parameter to fastboot_okay/fail, instead
of modifying a global, to match the contract expected by the AOSP
U-Boot code.

Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
---

common/fb_mmc.c | 80 ++++++++++++++++++++++-------------------
common/fb_nand.c | 31 ++++++++--------
common/image-sparse.c | 41 +++++++++++----------
drivers/usb/gadget/f_fastboot.c | 35 +++++++-----------
include/fastboot.h | 4 +--
include/fb_mmc.h | 4 +--
include/fb_nand.h | 4 +--
include/image-sparse.h | 2 +-
8 files changed, 100 insertions(+), 101 deletions(-)

diff --git a/common/fb_mmc.c b/common/fb_mmc.c
index cf5b77c..02864aa 100644
--- a/common/fb_mmc.c
+++ b/common/fb_mmc.c
@@ -74,7 +74,7 @@ static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,

static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
const char *part_name, void *buffer,
- unsigned int download_bytes)
+ unsigned int download_bytes, char *response)
{
lbaint_t blkcnt;
lbaint_t blks;
@@ -85,7 +85,7 @@ static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,

if (blkcnt > info->size) {
pr_err("too large for partition: '%s'\n", part_name);
- fastboot_fail("too large for partition");
+ fastboot_fail("too large for partition", response);
return;
}

@@ -94,13 +94,13 @@ static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
if (blks != blkcnt) {
pr_err("failed writing to device %d\n", dev_desc->devnum);
- fastboot_fail("failed writing to device");
+ fastboot_fail("failed writing to device", response);
return;
}

printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
part_name);
- fastboot_okay("");
+ fastboot_okay("", response);
}

#ifdef CONFIG_ANDROID_BOOT_IMAGE
@@ -115,7 +115,8 @@ static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
*/
static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
disk_partition_t *info,
- struct andr_img_hdr *hdr)
+ struct andr_img_hdr *hdr,
+ char *response)
{
ulong sector_size; /* boot partition sector size */
lbaint_t hdr_sectors; /* boot image header sectors count */
@@ -126,7 +127,7 @@ static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
if (hdr_sectors == 0) {
pr_err("invalid number of boot sectors: 0");
- fastboot_fail("invalid number of boot sectors: 0");
+ fastboot_fail("invalid number of boot sectors: 0", response);
return 0;
}

@@ -134,7 +135,8 @@ static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
if (res != hdr_sectors) {
pr_err("cannot read header from boot partition");
- fastboot_fail("cannot read header from boot partition");
+ fastboot_fail("cannot read header from boot partition",
+ response);
return 0;
}

@@ -142,7 +144,7 @@ static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
res = android_image_check_header(hdr);
if (res != 0) {
pr_err("bad boot image magic");
- fastboot_fail("boot partition not initialized");
+ fastboot_fail("boot partition not initialized", response);
return 0;
}

@@ -160,7 +162,8 @@ static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
*/
static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
void *download_buffer,
- unsigned int download_bytes)
+ unsigned int download_bytes,
+ char *response)
{
uintptr_t hdr_addr; /* boot image header address */
struct andr_img_hdr *hdr; /* boot image header */
@@ -180,7 +183,7 @@ static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
if (res < 0) {
pr_err("cannot find boot partition");
- fastboot_fail("cannot find boot partition");
+ fastboot_fail("cannot find boot partition", response);
return -1;
}

@@ -189,17 +192,18 @@ static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
hdr = (struct andr_img_hdr *)hdr_addr;

/* Read boot image header */
- hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr);
+ hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr, response);
if (hdr_sectors == 0) {
pr_err("unable to read boot image header");
- fastboot_fail("unable to read boot image header");
+ fastboot_fail("unable to read boot image header", response);
return -1;
}

/* Check if boot image has second stage in it (we don't support it) */
if (hdr->second_size > 0) {
pr_err("moving second stage is not supported yet");
- fastboot_fail("moving second stage is not supported yet");
+ fastboot_fail("moving second stage is not supported yet",
+ response);
return -1;
}

@@ -217,7 +221,8 @@ static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
ramdisk_buffer);
if (res != ramdisk_sectors) {
pr_err("cannot read ramdisk from boot partition");
- fastboot_fail("cannot read ramdisk from boot partition");
+ fastboot_fail("cannot read ramdisk from boot partition",
+ response);
return -1;
}

@@ -226,7 +231,7 @@ static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
if (res == 0) {
pr_err("cannot writeback boot image header");
- fastboot_fail("cannot write back boot image header");
+ fastboot_fail("cannot write back boot image header", response);
return -1;
}

@@ -238,7 +243,7 @@ static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
download_buffer);
if (res == 0) {
pr_err("cannot write new kernel");
- fastboot_fail("cannot write new kernel");
+ fastboot_fail("cannot write new kernel", response);
return -1;
}

@@ -250,18 +255,18 @@ static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
ramdisk_buffer);
if (res == 0) {
pr_err("cannot write back original ramdisk");
- fastboot_fail("cannot write back original ramdisk");
+ fastboot_fail("cannot write back original ramdisk", response);
return -1;
}

puts("........ zImage was updated in boot partition\n");
- fastboot_okay("");
+ fastboot_okay("", response);
return 0;
}
#endif

void fb_mmc_flash_write(const char *cmd, void *download_buffer,
- unsigned int download_bytes)
+ unsigned int download_bytes, char *response)
{
struct blk_desc *dev_desc;
disk_partition_t info;
@@ -269,7 +274,7 @@ void fb_mmc_flash_write(const char *cmd, void *download_buffer,
dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
pr_err("invalid mmc device\n");
- fastboot_fail("invalid mmc device");
+ fastboot_fail("invalid mmc device", response);
return;
}

@@ -280,16 +285,17 @@ void fb_mmc_flash_write(const char *cmd, void *download_buffer,
if (is_valid_gpt_buf(dev_desc, download_buffer)) {
printf("%s: invalid GPT - refusing to write to flash\n",
__func__);
- fastboot_fail("invalid GPT partition");
+ fastboot_fail("invalid GPT partition", response);
return;
}
if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
printf("%s: writing GPT partitions failed\n", __func__);
- fastboot_fail("writing GPT partitions failed");
+ fastboot_fail("writing GPT partitions failed",
+ response);
return;
}
printf("........ success\n");
- fastboot_okay("");
+ fastboot_okay("", response);
return;
}
#endif
@@ -300,30 +306,32 @@ void fb_mmc_flash_write(const char *cmd, void *download_buffer,
if (is_valid_dos_buf(download_buffer)) {
printf("%s: invalid MBR - refusing to write to flash\n",
__func__);
- fastboot_fail("invalid MBR partition");
+ fastboot_fail("invalid MBR partition", response);
return;
}
if (write_mbr_partition(dev_desc, download_buffer)) {
printf("%s: writing MBR partition failed\n", __func__);
- fastboot_fail("writing MBR partition failed");
+ fastboot_fail("writing MBR partition failed",
+ response);
return;
}
printf("........ success\n");
- fastboot_okay("");
+ fastboot_okay("", response);
return;
}
#endif

#ifdef CONFIG_ANDROID_BOOT_IMAGE
if (strncasecmp(cmd, "zimage", 6) == 0) {
- fb_mmc_update_zimage(dev_desc, download_buffer, download_bytes);
+ fb_mmc_update_zimage(dev_desc, download_buffer,
+ download_bytes, response);
return;
}
#endif

if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
pr_err("cannot find partition: '%s'\n", cmd);
- fastboot_fail("cannot find partition");
+ fastboot_fail("cannot find partition", response);
return;
}

@@ -344,14 +352,14 @@ void fb_mmc_flash_write(const char *cmd, void *download_buffer,

sparse.priv = &sparse_priv;
write_sparse_image(&sparse, cmd, download_buffer,
- download_bytes);
+ download_bytes, response);
} else {
write_raw_image(dev_desc, &info, cmd, download_buffer,
- download_bytes);
+ download_bytes, response);
}
}

-void fb_mmc_erase(const char *cmd)
+void fb_mmc_erase(const char *cmd, char *response)
{
int ret;
struct blk_desc *dev_desc;
@@ -361,21 +369,21 @@ void fb_mmc_erase(const char *cmd)

if (mmc == NULL) {
pr_err("invalid mmc device");
- fastboot_fail("invalid mmc device");
+ fastboot_fail("invalid mmc device", response);
return;
}

dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
pr_err("invalid mmc device");
- fastboot_fail("invalid mmc device");
+ fastboot_fail("invalid mmc device", response);
return;
}

ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
if (ret < 0) {
pr_err("cannot find partition: '%s'", cmd);
- fastboot_fail("cannot find partition");
+ fastboot_fail("cannot find partition", response);
return;
}

@@ -394,11 +402,11 @@ void fb_mmc_erase(const char *cmd)
blks = blk_derase(dev_desc, blks_start, blks_size);
if (blks != blks_size) {
pr_err("failed erasing from device %d", dev_desc->devnum);
- fastboot_fail("failed erasing from device");
+ fastboot_fail("failed erasing from device", response);
return;
}

printf("........ erased " LBAFU " bytes from '%s'\n",
blks_size * info.blksz, cmd);
- fastboot_okay("");
+ fastboot_okay("", response);
}
diff --git a/common/fb_nand.c b/common/fb_nand.c
index aa28046..3a0b101 100644
--- a/common/fb_nand.c
+++ b/common/fb_nand.c
@@ -32,7 +32,8 @@ __weak int board_fastboot_write_partition_setup(char *name)

static int fb_nand_lookup(const char *partname,
struct mtd_info **mtd,
- struct part_info **part)
+ struct part_info **part,
+ char *response)
{
struct mtd_device *dev;
int ret;
@@ -41,21 +42,21 @@ static int fb_nand_lookup(const char *partname,
ret = mtdparts_init();
if (ret) {
pr_err("Cannot initialize MTD partitions\n");
- fastboot_fail("cannot init mtdparts");
+ fastboot_fail("cannot init mtdparts", response);
return ret;
}

ret = find_dev_and_part(partname, &dev, &pnum, part);
if (ret) {
pr_err("cannot find partition: '%s'", partname);
- fastboot_fail("cannot find partition");
+ fastboot_fail("cannot find partition", response);
return ret;
}

if (dev->id->type != MTD_DEV_TYPE_NAND) {
pr_err("partition '%s' is not stored on a NAND device",
partname);
- fastboot_fail("not a NAND device");
+ fastboot_fail("not a NAND device", response);
return -EINVAL;
}

@@ -146,16 +147,16 @@ static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
}

void fb_nand_flash_write(const char *cmd, void *download_buffer,
- unsigned int download_bytes)
+ unsigned int download_bytes, char *response)
{
struct part_info *part;
struct mtd_info *mtd = NULL;
int ret;

- ret = fb_nand_lookup(cmd, &mtd, &part);
+ ret = fb_nand_lookup(cmd, &mtd, &part, response);
if (ret) {
pr_err("invalid NAND device");
- fastboot_fail("invalid NAND device");
+ fastboot_fail("invalid NAND device", response);
return;
}

@@ -181,7 +182,7 @@ void fb_nand_flash_write(const char *cmd, void *download_buffer,

sparse.priv = &sparse_priv;
write_sparse_image(&sparse, cmd, download_buffer,
- download_bytes);
+ download_bytes, response);
} else {
printf("Flashing raw image at offset 0x%llx\n",
part->offset);
@@ -194,23 +195,23 @@ void fb_nand_flash_write(const char *cmd, void *download_buffer,
}

if (ret) {
- fastboot_fail("error writing the image");
+ fastboot_fail("error writing the image", response);
return;
}

- fastboot_okay("");
+ fastboot_okay("", response);
}

-void fb_nand_erase(const char *cmd)
+void fb_nand_erase(const char *cmd, char *response)
{
struct part_info *part;
struct mtd_info *mtd = NULL;
int ret;

- ret = fb_nand_lookup(cmd, &mtd, &part);
+ ret = fb_nand_lookup(cmd, &mtd, &part, response);
if (ret) {
pr_err("invalid NAND device");
- fastboot_fail("invalid NAND device");
+ fastboot_fail("invalid NAND device", response);
return;
}

@@ -221,9 +222,9 @@ void fb_nand_erase(const char *cmd)
ret = _fb_nand_erase(mtd, part);
if (ret) {
pr_err("failed erasing from device %s", mtd->name);
- fastboot_fail("failed erasing from device");
+ fastboot_fail("failed erasing from device", response);
return;
}

- fastboot_okay("");
+ fastboot_okay("", response);
}
diff --git a/common/image-sparse.c b/common/image-sparse.c
index ddf5772..616c2bd 100644
--- a/common/image-sparse.c
+++ b/common/image-sparse.c
@@ -51,7 +51,7 @@

void write_sparse_image(
struct sparse_storage *info, const char *part_name,
- void *data, unsigned sz)
+ void *data, unsigned int sz, char *response)
{
lbaint_t blk;
lbaint_t blkcnt;
@@ -101,7 +101,7 @@ void write_sparse_image(
if (offset) {
printf("%s: Sparse image block size issue [%u]\n",
__func__, sparse_header->blk_sz);
- fastboot_fail("sparse image block size issue");
+ fastboot_fail("sparse image block size issue", response);
return;
}

@@ -136,8 +136,8 @@ void write_sparse_image(
case CHUNK_TYPE_RAW:
if (chunk_header->total_sz !=
(sparse_header->chunk_hdr_sz + chunk_data_sz)) {
- fastboot_fail(
- "Bogus chunk size for chunk type Raw");
+ fastboot_fail("Bogus chunk size for chunk type Raw",
+ response);
return;
}

@@ -145,8 +145,8 @@ void write_sparse_image(
printf(
"%s: Request would exceed partition size!\n",
__func__);
- fastboot_fail(
- "Request would exceed partition size!");
+ fastboot_fail("Request would exceed partition size!",
+ response);
return;
}

@@ -156,8 +156,7 @@ void write_sparse_image(
printf("%s: %s" LBAFU " [" LBAFU "]\n",
__func__, "Write failed, block #",
blk, blks);
- fastboot_fail(
- "flash write failure");
+ fastboot_fail("flash write failure", response);
return;
}
blk += blks;
@@ -169,8 +168,8 @@ void write_sparse_image(
case CHUNK_TYPE_FILL:
if (chunk_header->total_sz !=
(sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
- fastboot_fail(
- "Bogus chunk size for chunk type FILL");
+ fastboot_fail("Bogus chunk size for chunk type FILL",
+ response);
return;
}

@@ -180,8 +179,8 @@ void write_sparse_image(
info->blksz * fill_buf_num_blks,
ARCH_DMA_MINALIGN));
if (!fill_buf) {
- fastboot_fail(
- "Malloc failed for: CHUNK_TYPE_FILL");
+ fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL",
+ response);
return;
}

@@ -198,8 +197,8 @@ void write_sparse_image(
printf(
"%s: Request would exceed partition size!\n",
__func__);
- fastboot_fail(
- "Request would exceed partition size!");
+ fastboot_fail("Request would exceed partition size!",
+ response);
return;
}

@@ -214,8 +213,8 @@ void write_sparse_image(
__func__,
"Write failed, block #",
blk, j);
- fastboot_fail(
- "flash write failure");
+ fastboot_fail("flash write failure",
+ response);
free(fill_buf);
return;
}
@@ -235,8 +234,8 @@ void write_sparse_image(
case CHUNK_TYPE_CRC32:
if (chunk_header->total_sz !=
sparse_header->chunk_hdr_sz) {
- fastboot_fail(
- "Bogus chunk size for chunk type Dont Care");
+ fastboot_fail("Bogus chunk size for chunk type Dont Care",
+ response);
return;
}
total_blocks += chunk_header->chunk_sz;
@@ -246,7 +245,7 @@ void write_sparse_image(
default:
printf("%s: Unknown chunk type: %x\n", __func__,
chunk_header->chunk_type);
- fastboot_fail("Unknown chunk type");
+ fastboot_fail("Unknown chunk type", response);
return;
}
}
@@ -256,9 +255,9 @@ void write_sparse_image(
printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);

if (total_blocks != sparse_header->total_blks)
- fastboot_fail("sparse image write failure");
+ fastboot_fail("sparse image write failure", response);
else
- fastboot_okay("");
+ fastboot_okay("", response);

return;
}
diff --git a/drivers/usb/gadget/f_fastboot.c b/drivers/usb/gadget/f_fastboot.c
index 7acffb6..6ae1d97 100644
--- a/drivers/usb/gadget/f_fastboot.c
+++ b/drivers/usb/gadget/f_fastboot.c
@@ -151,18 +151,16 @@ static void rx_handler_command(struct usb_ep *ep, struct usb_request *req);
static int strcmp_l1(const char *s1, const char *s2);

-static char *fb_response_str;
-
-void fastboot_fail(const char *reason)
+void fastboot_fail(const char *reason, char *response)
{
- strncpy(fb_response_str, "FAIL\0", 5);
- strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
+ strncpy(response, "FAIL\0", 5);
+ strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
}

-void fastboot_okay(const char *reason)
+void fastboot_okay(const char *reason, char *response)
{
- strncpy(fb_response_str, "OKAY\0", 5);
- strncat(fb_response_str, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
+ strncpy(response, "OKAY\0", 5);
+ strncat(response, reason, FASTBOOT_RESPONSE_LEN - 4 - 1);
}

static void fastboot_complete(struct usb_ep *ep, struct usb_request *req)
@@ -598,18 +596,14 @@ static void cb_flash(struct usb_ep *ep, struct usb_request *req)
return;
}

- /* initialize the response buffer */
- fb_response_str = response;
-
- fastboot_fail("no flash device defined");
+ fastboot_fail("no flash device defined", response);
#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
fb_mmc_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
- download_bytes);
+ download_bytes, response);
#endif
#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
- fb_nand_flash_write(cmd,
- (void *)CONFIG_FASTBOOT_BUF_ADDR,
- download_bytes);
+ fb_nand_flash_write(cmd, (void *)CONFIG_FASTBOOT_BUF_ADDR,
+ download_bytes, response);
#endif
fastboot_tx_write_str(response);
}
@@ -650,15 +644,12 @@ static void cb_erase(struct usb_ep *ep, struct usb_request *req)
return;
}

- /* initialize the response buffer */
- fb_response_str = response;
-
- fastboot_fail("no flash device defined");
+ fastboot_fail("no flash device defined", response);
#ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
- fb_mmc_erase(cmd);
+ fb_mmc_erase(cmd, response);
#endif
#ifdef CONFIG_FASTBOOT_FLASH_NAND_DEV
- fb_nand_erase(cmd);
+ fb_nand_erase(cmd, response);
#endif
fastboot_tx_write_str(response);
}
diff --git a/include/fastboot.h b/include/fastboot.h
index 616631e..f22080a 100644
--- a/include/fastboot.h
+++ b/include/fastboot.h
@@ -16,7 +16,7 @@
/* The 64 defined bytes plus \0 */
#define FASTBOOT_RESPONSE_LEN (64 + 1)

-void fastboot_fail(const char *reason);
-void fastboot_okay(const char *reason);
+void fastboot_fail(const char *reason, char *response);
+void fastboot_okay(const char *reason, char *response);

#endif /* _FASTBOOT_H_ */
diff --git a/include/fb_mmc.h b/include/fb_mmc.h
index 12b99cb..402ba9b 100644
--- a/include/fb_mmc.h
+++ b/include/fb_mmc.h
@@ -5,5 +5,5 @@
*/

void fb_mmc_flash_write(const char *cmd, void *download_buffer,
- unsigned int download_bytes);
-void fb_mmc_erase(const char *cmd);
+ unsigned int download_bytes, char *response);
+void fb_mmc_erase(const char *cmd, char *response);
diff --git a/include/fb_nand.h b/include/fb_nand.h
index aaf7cf7..88bdf36 100644
--- a/include/fb_nand.h
+++ b/include/fb_nand.h
@@ -6,5 +6,5 @@
*/

void fb_nand_flash_write(const char *cmd, void *download_buffer,
- unsigned int download_bytes);
-void fb_nand_erase(const char *cmd);
+ unsigned int download_bytes, char *response);
+void fb_nand_erase(const char *cmd, char *response);
diff --git a/include/image-sparse.h b/include/image-sparse.h
index b0cc500..02453c8 100644
--- a/include/image-sparse.h
+++ b/include/image-sparse.h
@@ -37,4 +37,4 @@ static inline int is_sparse_image(void *buf)
}

void write_sparse_image(struct sparse_storage *info, const char *part_name,
- void *data, unsigned sz);
+ void *data, unsigned int sz, char *response);
--
2.7.4