From c4ded03ef608be37db105200010d2f3f88195bd6 Mon Sep 17 00:00:00 2001 From: Alex Kiernan Date: Tue, 29 May 2018 15:30:40 +0000 Subject: [PATCH] fastboot: Refactor fastboot_okay/fail to take response 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 Reviewed-by: Joe Hershberger --- cmd/mmc.c | 2 +- common/image-sparse.c | 32 +++++++++------- drivers/fastboot/fb_mmc.c | 83 +++++++++++++++++++++++------------------ drivers/fastboot/fb_nand.c | 34 +++++++++-------- 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 | 4 +- 9 files changed, 105 insertions(+), 97 deletions(-) diff --git a/cmd/mmc.c b/cmd/mmc.c index a3357ef..a5719b8 100644 --- a/cmd/mmc.c +++ b/cmd/mmc.c @@ -367,7 +367,7 @@ static int do_mmc_sparse_write(cmd_tbl_t *cmdtp, int flag, sparse.mssg = NULL; sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz); - if (write_sparse_image(&sparse, dest, addr)) + if (write_sparse_image(&sparse, dest, addr, NULL)) return CMD_RET_FAILURE; else return CMD_RET_SUCCESS; diff --git a/common/image-sparse.c b/common/image-sparse.c index 9223b9a..1ae7a4d 100644 --- a/common/image-sparse.c +++ b/common/image-sparse.c @@ -48,10 +48,10 @@ #define CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE (1024 * 512) #endif -static void default_log(const char *ignored) {} +static void default_log(const char *ignored, char *response) {} int write_sparse_image(struct sparse_storage *info, - const char *part_name, void *data) + const char *part_name, void *data, char *response) { lbaint_t blk; lbaint_t blkcnt; @@ -104,7 +104,7 @@ int write_sparse_image(struct sparse_storage *info, if (offset) { printf("%s: Sparse image block size issue [%u]\n", __func__, sparse_header->blk_sz); - info->mssg("sparse image block size issue"); + info->mssg("sparse image block size issue", response); return -1; } @@ -139,7 +139,8 @@ int write_sparse_image(struct sparse_storage *info, case CHUNK_TYPE_RAW: if (chunk_header->total_sz != (sparse_header->chunk_hdr_sz + chunk_data_sz)) { - info->mssg("Bogus chunk size for chunk type Raw"); + info->mssg("Bogus chunk size for chunk type Raw", + response); return -1; } @@ -147,7 +148,8 @@ int write_sparse_image(struct sparse_storage *info, printf( "%s: Request would exceed partition size!\n", __func__); - info->mssg("Request would exceed partition size!"); + info->mssg("Request would exceed partition size!", + response); return -1; } @@ -157,7 +159,7 @@ int write_sparse_image(struct sparse_storage *info, printf("%s: %s" LBAFU " [" LBAFU "]\n", __func__, "Write failed, block #", blk, blks); - info->mssg("flash write failure"); + info->mssg("flash write failure", response); return -1; } blk += blks; @@ -169,7 +171,7 @@ int write_sparse_image(struct sparse_storage *info, case CHUNK_TYPE_FILL: if (chunk_header->total_sz != (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) { - info->mssg("Bogus chunk size for chunk type FILL"); + info->mssg("Bogus chunk size for chunk type FILL", response); return -1; } @@ -179,7 +181,8 @@ int write_sparse_image(struct sparse_storage *info, info->blksz * fill_buf_num_blks, ARCH_DMA_MINALIGN)); if (!fill_buf) { - info->mssg("Malloc failed for: CHUNK_TYPE_FILL"); + info->mssg("Malloc failed for: CHUNK_TYPE_FILL", + response); return -1; } @@ -196,7 +199,8 @@ int write_sparse_image(struct sparse_storage *info, printf( "%s: Request would exceed partition size!\n", __func__); - info->mssg("Request would exceed partition size!"); + info->mssg("Request would exceed partition size!", + response); return -1; } @@ -211,7 +215,8 @@ int write_sparse_image(struct sparse_storage *info, __func__, "Write failed, block #", blk, j); - info->mssg("flash write failure"); + info->mssg("flash write failure", + response); free(fill_buf); return -1; } @@ -231,7 +236,8 @@ int write_sparse_image(struct sparse_storage *info, case CHUNK_TYPE_CRC32: if (chunk_header->total_sz != sparse_header->chunk_hdr_sz) { - info->mssg("Bogus chunk size for chunk type Dont Care"); + info->mssg("Bogus chunk size for chunk type Dont Care", + response); return -1; } total_blocks += chunk_header->chunk_sz; @@ -241,7 +247,7 @@ int write_sparse_image(struct sparse_storage *info, default: printf("%s: Unknown chunk type: %x\n", __func__, chunk_header->chunk_type); - info->mssg("Unknown chunk type"); + info->mssg("Unknown chunk type", response); return -1; } } @@ -251,7 +257,7 @@ int write_sparse_image(struct sparse_storage *info, printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name); if (total_blocks != sparse_header->total_blks) { - info->mssg("sparse image write failure"); + info->mssg("sparse image write failure", response); return -1; } diff --git a/drivers/fastboot/fb_mmc.c b/drivers/fastboot/fb_mmc.c index 46f0073..1bcb2e5 100644 --- a/drivers/fastboot/fb_mmc.c +++ b/drivers/fastboot/fb_mmc.c @@ -73,7 +73,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; @@ -84,7 +84,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; } @@ -93,13 +93,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(NULL, response); } #ifdef CONFIG_ANDROID_BOOT_IMAGE @@ -114,7 +114,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 */ @@ -125,7 +126,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; } @@ -133,7 +134,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; } @@ -141,7 +143,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; } @@ -159,7 +161,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 */ @@ -179,7 +182,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; } @@ -188,17 +191,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; } @@ -216,7 +220,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; } @@ -225,7 +230,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; } @@ -237,7 +242,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; } @@ -249,18 +254,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(NULL, 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; @@ -268,7 +273,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; } @@ -279,16 +284,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(NULL, response); return; } #endif @@ -299,30 +305,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(NULL, 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,16 +352,17 @@ void fb_mmc_flash_write(const char *cmd, void *download_buffer, sparse.start); sparse.priv = &sparse_priv; - err = write_sparse_image(&sparse, cmd, download_buffer); + err = write_sparse_image(&sparse, cmd, download_buffer, + response); if (!err) - fastboot_okay(""); + fastboot_okay(NULL, 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; @@ -363,21 +372,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; } @@ -396,11 +405,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(NULL, response); } diff --git a/drivers/fastboot/fb_nand.c b/drivers/fastboot/fb_nand.c index c07655e..2ee0d64 100644 --- a/drivers/fastboot/fb_nand.c +++ b/drivers/fastboot/fb_nand.c @@ -31,7 +31,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; @@ -40,21 +41,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; } @@ -145,16 +146,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; } @@ -180,9 +181,10 @@ void fb_nand_flash_write(const char *cmd, void *download_buffer, sparse.start); sparse.priv = &sparse_priv; - ret = write_sparse_image(&sparse, cmd, download_buffer); + ret = write_sparse_image(&sparse, cmd, download_buffer, + response); if (!ret) - fastboot_okay(""); + fastboot_okay(NULL, response); } else { printf("Flashing raw image at offset 0x%llx\n", part->offset); @@ -195,23 +197,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(NULL, 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; } @@ -222,9 +224,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(NULL, response); } diff --git a/drivers/usb/gadget/f_fastboot.c b/drivers/usb/gadget/f_fastboot.c index 3acadae..bb60612 100644 --- a/drivers/usb/gadget/f_fastboot.c +++ b/drivers/usb/gadget/f_fastboot.c @@ -150,18 +150,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) @@ -597,18 +595,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); } @@ -649,15 +643,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 009f1a7..ed52dae 100644 --- a/include/fastboot.h +++ b/include/fastboot.h @@ -15,7 +15,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 a2d7c48..39a960c 100644 --- a/include/fb_mmc.h +++ b/include/fb_mmc.h @@ -4,5 +4,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 3daae8c..2c92a4e 100644 --- a/include/fb_nand.h +++ b/include/fb_nand.h @@ -5,5 +5,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 f39dc16..234c237 100644 --- a/include/image-sparse.h +++ b/include/image-sparse.h @@ -23,7 +23,7 @@ struct sparse_storage { lbaint_t blk, lbaint_t blkcnt); - void (*mssg)(const char *str); + void (*mssg)(const char *str, char *response); }; static inline int is_sparse_image(void *buf) @@ -38,4 +38,4 @@ static inline int is_sparse_image(void *buf) } int write_sparse_image(struct sparse_storage *info, const char *part_name, - void *data); + void *data, char *response);