Merge branch '2022-08-04-Kconfig-migrations'
[platform/kernel/u-boot.git] / drivers / fastboot / fb_mmc.c
index 611074a..033c510 100644 (file)
@@ -10,7 +10,6 @@
 #include <fastboot.h>
 #include <fastboot-internal.h>
 #include <fb_mmc.h>
-#include <flash.h>
 #include <image-sparse.h>
 #include <image.h>
 #include <log.h>
@@ -28,30 +27,9 @@ struct fb_mmc_sparse {
        struct blk_desc *dev_desc;
 };
 
-static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
-               const char *name, struct disk_partition *info)
-{
-       int ret;
-
-       ret = part_get_info_by_name(dev_desc, name, info);
-       if (ret < 0) {
-               /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
-               char env_alias_name[25 + PART_NAME_LEN + 1];
-               char *aliased_part_name;
-
-               /* check for alias */
-               strcpy(env_alias_name, "fastboot_partition_alias_");
-               strncat(env_alias_name, name, PART_NAME_LEN);
-               aliased_part_name = env_get(env_alias_name);
-               if (aliased_part_name != NULL)
-                       ret = part_get_info_by_name(dev_desc,
-                                       aliased_part_name, info);
-       }
-       return ret;
-}
-
 static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
-               const char *name, struct disk_partition *info, int *mmcpart)
+                                    const char *name,
+                                    struct disk_partition *info)
 {
        /* strlen("fastboot_raw_partition_") + PART_NAME_LEN + 1 */
        char env_desc_name[23 + PART_NAME_LEN + 1];
@@ -61,7 +39,7 @@ static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
 
        /* check for raw partition descriptor */
        strcpy(env_desc_name, "fastboot_raw_partition_");
-       strncat(env_desc_name, name, PART_NAME_LEN);
+       strlcat(env_desc_name, name, sizeof(env_desc_name));
        raw_part_desc = strdup(env_get(env_desc_name));
        if (raw_part_desc == NULL)
                return -ENODEV;
@@ -82,16 +60,63 @@ static int raw_part_get_info_by_name(struct blk_desc *dev_desc,
        info->start = simple_strtoul(argv[0], NULL, 0);
        info->size = simple_strtoul(argv[1], NULL, 0);
        info->blksz = dev_desc->blksz;
-       strncpy((char *)info->name, name, PART_NAME_LEN);
+       strlcpy((char *)info->name, name, PART_NAME_LEN);
 
        if (raw_part_desc) {
-               if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0)
-                       *mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
+               if (strcmp(strsep(&raw_part_desc, " "), "mmcpart") == 0) {
+                       ulong mmcpart = simple_strtoul(raw_part_desc, NULL, 0);
+                       int ret = blk_dselect_hwpart(dev_desc, mmcpart);
+
+                       if (ret)
+                               return ret;
+               }
        }
 
        return 0;
 }
 
+static int do_get_part_info(struct blk_desc **dev_desc, const char *name,
+                           struct disk_partition *info)
+{
+       int ret;
+
+       /* First try partition names on the default device */
+       *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
+       if (*dev_desc) {
+               ret = part_get_info_by_name(*dev_desc, name, info);
+               if (ret >= 0)
+                       return ret;
+
+               /* Then try raw partitions */
+               ret = raw_part_get_info_by_name(*dev_desc, name, info);
+               if (ret >= 0)
+                       return ret;
+       }
+
+       /* Then try dev.hwpart:part */
+       ret = part_get_info_by_dev_and_name_or_num("mmc", name, dev_desc,
+                                                  info, true);
+       return ret;
+}
+
+static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc,
+                                         const char *name,
+                                         struct disk_partition *info)
+{
+       /* strlen("fastboot_partition_alias_") + PART_NAME_LEN + 1 */
+       char env_alias_name[25 + PART_NAME_LEN + 1];
+       char *aliased_part_name;
+
+       /* check for alias */
+       strlcpy(env_alias_name, "fastboot_partition_alias_", sizeof(env_alias_name));
+       strlcat(env_alias_name, name, sizeof(env_alias_name));
+       aliased_part_name = env_get(env_alias_name);
+       if (aliased_part_name)
+               name = aliased_part_name;
+
+       return do_get_part_info(dev_desc, name, info);
+}
+
 /**
  * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE
  *
@@ -258,7 +283,7 @@ static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer,
  * @param[in] info Boot partition info
  * @param[out] hdr Where to store read boot image header
  *
- * @return Boot image header sectors count or 0 on error
+ * Return: Boot image header sectors count or 0 on error
  */
 static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
                                       struct disk_partition *info,
@@ -305,7 +330,7 @@ static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
  * @param download_buffer Address to fastboot buffer with zImage in it
  * @param download_bytes Size of fastboot buffer, in bytes
  *
- * @return 0 on success or -1 on error
+ * Return: 0 on success or -1 on error
  */
 static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
                                void *download_buffer,
@@ -424,28 +449,49 @@ int fastboot_mmc_get_part_info(const char *part_name,
                               struct blk_desc **dev_desc,
                               struct disk_partition *part_info, char *response)
 {
-       int r = 0;
-       int mmcpart;
+       int ret;
 
-       *dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
-       if (!*dev_desc) {
-               fastboot_fail("block device not found", response);
-               return -ENOENT;
-       }
        if (!part_name || !strcmp(part_name, "")) {
                fastboot_fail("partition not given", response);
                return -ENOENT;
        }
 
-       if (raw_part_get_info_by_name(*dev_desc, part_name, part_info, &mmcpart) < 0) {
-               r = part_get_info_by_name_or_alias(*dev_desc, part_name, part_info);
-               if (r < 0) {
-                       fastboot_fail("partition not found", response);
-                       return r;
+       ret = part_get_info_by_name_or_alias(dev_desc, part_name, part_info);
+       if (ret < 0) {
+               switch (ret) {
+               case -ENOSYS:
+               case -EINVAL:
+                       fastboot_fail("invalid partition or device", response);
+                       break;
+               case -ENODEV:
+                       fastboot_fail("no such device", response);
+                       break;
+               case -ENOENT:
+                       fastboot_fail("no such partition", response);
+                       break;
+               case -EPROTONOSUPPORT:
+                       fastboot_fail("unknown partition table type", response);
+                       break;
+               default:
+                       fastboot_fail("unanticipated error", response);
+                       break;
                }
        }
 
-       return r;
+       return ret;
+}
+
+static struct blk_desc *fastboot_mmc_get_dev(char *response)
+{
+       struct blk_desc *ret = blk_get_dev("mmc",
+                                          CONFIG_FASTBOOT_FLASH_MMC_DEV);
+
+       if (!ret || ret->type == DEV_TYPE_UNKNOWN) {
+               pr_err("invalid mmc device\n");
+               fastboot_fail("invalid mmc device", response);
+               return NULL;
+       }
+       return ret;
 }
 
 /**
@@ -460,36 +506,31 @@ void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
                              u32 download_bytes, char *response)
 {
        struct blk_desc *dev_desc;
-       struct disk_partition info;
-       int mmcpart = 0;
-
-       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", response);
-               return;
-       }
+       struct disk_partition info = {0};
 
 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
        if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
-               fb_mmc_boot_ops(dev_desc, download_buffer, 1,
-                               download_bytes, response);
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (dev_desc)
+                       fb_mmc_boot_ops(dev_desc, download_buffer, 1,
+                                       download_bytes, response);
                return;
        }
        if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
-               fb_mmc_boot_ops(dev_desc, download_buffer, 2,
-                               download_bytes, response);
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (dev_desc)
+                       fb_mmc_boot_ops(dev_desc, download_buffer, 2,
+                                       download_bytes, response);
                return;
        }
 #endif
 
 #if CONFIG_IS_ENABLED(EFI_PARTITION)
-#ifndef CONFIG_FASTBOOT_MMC_USER_SUPPORT
        if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
-#else
-       if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0 ||
-           strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
-#endif
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (!dev_desc)
+                       return;
+
                printf("%s: updating MBR, Primary and Backup GPT(s)\n",
                       __func__);
                if (is_valid_gpt_buf(dev_desc, download_buffer)) {
@@ -504,6 +545,7 @@ void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
                                      response);
                        return;
                }
+               part_init(dev_desc);
                printf("........ success\n");
                fastboot_okay(NULL, response);
                return;
@@ -512,6 +554,10 @@ void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
 
 #if CONFIG_IS_ENABLED(DOS_PARTITION)
        if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (!dev_desc)
+                       return;
+
                printf("%s: updating MBR\n", __func__);
                if (is_valid_dos_buf(download_buffer)) {
                        printf("%s: invalid MBR - refusing to write to flash\n",
@@ -525,6 +571,7 @@ void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
                                      response);
                        return;
                }
+               part_init(dev_desc);
                printf("........ success\n");
                fastboot_okay(NULL, response);
                return;
@@ -533,23 +580,29 @@ void fastboot_mmc_flash_write(const char *cmd, void *download_buffer,
 
 #ifdef CONFIG_ANDROID_BOOT_IMAGE
        if (strncasecmp(cmd, "zimage", 6) == 0) {
-               fb_mmc_update_zimage(dev_desc, download_buffer,
-                                    download_bytes, response);
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (dev_desc)
+                       fb_mmc_update_zimage(dev_desc, download_buffer,
+                                            download_bytes, response);
                return;
        }
 #endif
 
-       if (raw_part_get_info_by_name(dev_desc, cmd, &info, &mmcpart) == 0) {
-               if (blk_dselect_hwpart(dev_desc, mmcpart)) {
-                       pr_err("Failed to select hwpart\n");
-                       fastboot_fail("Failed to select hwpart", response);
+#if CONFIG_IS_ENABLED(FASTBOOT_MMC_USER_SUPPORT)
+       if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (!dev_desc)
                        return;
-               }
-       } else 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", response);
-               return;
+
+               strlcpy((char *)&info.name, cmd, sizeof(info.name));
+               info.size       = dev_desc->lba;
+               info.blksz      = dev_desc->blksz;
        }
+#endif
+
+       if (!info.name[0] &&
+           fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
+               return;
 
        if (is_sparse_image(download_buffer)) {
                struct fb_mmc_sparse sparse_priv;
@@ -591,30 +644,20 @@ void fastboot_mmc_erase(const char *cmd, char *response)
        struct disk_partition info;
        lbaint_t blks, blks_start, blks_size, grp_size;
        struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
-       int mmcpart = 0;
-
-       if (mmc == NULL) {
-               pr_err("invalid mmc device\n");
-               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\n");
-               fastboot_fail("invalid mmc device", response);
-               return;
-       }
 
 #ifdef CONFIG_FASTBOOT_MMC_BOOT_SUPPORT
        if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT1_NAME) == 0) {
                /* erase EMMC boot1 */
-               fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (dev_desc)
+                       fb_mmc_boot_ops(dev_desc, NULL, 1, 0, response);
                return;
        }
        if (strcmp(cmd, CONFIG_FASTBOOT_MMC_BOOT2_NAME) == 0) {
                /* erase EMMC boot2 */
-               fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (dev_desc)
+                       fb_mmc_boot_ops(dev_desc, NULL, 2, 0, response);
                return;
        }
 #endif
@@ -622,6 +665,10 @@ void fastboot_mmc_erase(const char *cmd, char *response)
 #ifdef CONFIG_FASTBOOT_MMC_USER_SUPPORT
        if (strcmp(cmd, CONFIG_FASTBOOT_MMC_USER_NAME) == 0) {
                /* erase EMMC userdata */
+               dev_desc = fastboot_mmc_get_dev(response);
+               if (!dev_desc)
+                       return;
+
                if (fb_mmc_erase_mmc_hwpart(dev_desc))
                        fastboot_fail("Failed to erase EMMC_USER", response);
                else
@@ -630,17 +677,8 @@ void fastboot_mmc_erase(const char *cmd, char *response)
        }
 #endif
 
-       if (raw_part_get_info_by_name(dev_desc, cmd, &info, &mmcpart) == 0) {
-               if (blk_dselect_hwpart(dev_desc, mmcpart)) {
-                       pr_err("Failed to select hwpart\n");
-                       fastboot_fail("Failed to select hwpart", response);
-                       return;
-               }
-       } else 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", response);
+       if (fastboot_mmc_get_part_info(cmd, &dev_desc, &info, response) < 0)
                return;
-       }
 
        /* Align blocks to erase group size to avoid erasing other partitions */
        grp_size = mmc->erase_grp_size;