blob: 4bc68a76570336785678d1eaf33085cb769c82e2 [file] [log] [blame]
Steve Raec0aebb32014-08-26 11:47:27 -07001/*
2 * Copyright 2014 Broadcom Corporation.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
Steve Rae0ff7e582014-12-12 15:51:54 -08007#include <config.h>
Steve Raec0aebb32014-08-26 11:47:27 -07008#include <common.h>
Simon Glass2a981dc2016-02-29 15:25:52 -07009#include <blk.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020010#include <fastboot.h>
Steve Raec0aebb32014-08-26 11:47:27 -070011#include <fb_mmc.h>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020012#include <image-sparse.h>
Steve Raec0aebb32014-08-26 11:47:27 -070013#include <part.h>
Dileep Katta89792382015-02-17 18:48:23 +053014#include <mmc.h>
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +020015#include <div64.h>
Steve Raec0aebb32014-08-26 11:47:27 -070016
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +020017#if defined(CONFIG_EFI_PARTITION) && !defined(CONFIG_FASTBOOT_GPT_NAME)
Steve Rae0ff7e582014-12-12 15:51:54 -080018#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
19#endif
20
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +020021
22#if defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_FASTBOOT_MBR_NAME)
23#define CONFIG_FASTBOOT_MBR_NAME "mbr"
24#endif
25
Maxime Riparda5d1e042015-10-15 14:34:14 +020026struct fb_mmc_sparse {
Simon Glass4101f682016-02-29 15:25:34 -070027 struct blk_desc *dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020028};
29
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +020030static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070031 const char *name, disk_partition_t *info)
32{
33 int ret;
34
Petr Kulhavy87b85302016-09-09 10:27:15 +020035 ret = part_get_info_by_name(dev_desc, name, info);
Michael Scott8a418022015-03-11 10:02:31 -070036 if (ret) {
37 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
38 char env_alias_name[25 + 32 + 1];
39 char *aliased_part_name;
40
41 /* check for alias */
42 strcpy(env_alias_name, "fastboot_partition_alias_");
43 strncat(env_alias_name, name, 32);
44 aliased_part_name = getenv(env_alias_name);
45 if (aliased_part_name != NULL)
Petr Kulhavy87b85302016-09-09 10:27:15 +020046 ret = part_get_info_by_name(dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070047 aliased_part_name, info);
48 }
49 return ret;
50}
51
Steve Raecc0f08c2016-06-07 11:19:36 -070052static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
53 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda5d1e042015-10-15 14:34:14 +020054{
Steve Raecc0f08c2016-06-07 11:19:36 -070055 struct fb_mmc_sparse *sparse = info->priv;
Simon Glass4101f682016-02-29 15:25:34 -070056 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020057
Steve Raecc0f08c2016-06-07 11:19:36 -070058 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
Maxime Riparda5d1e042015-10-15 14:34:14 +020059}
60
Steve Rae2c724042016-06-07 11:19:38 -070061static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
62 lbaint_t blk, lbaint_t blkcnt)
63{
64 return blkcnt;
65}
66
Simon Glass4101f682016-02-29 15:25:34 -070067static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Raec0aebb32014-08-26 11:47:27 -070068 const char *part_name, void *buffer,
69 unsigned int download_bytes)
70{
71 lbaint_t blkcnt;
72 lbaint_t blks;
73
74 /* determine number of blocks to write */
75 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +020076 blkcnt = lldiv(blkcnt, info->blksz);
Steve Raec0aebb32014-08-26 11:47:27 -070077
78 if (blkcnt > info->size) {
79 error("too large for partition: '%s'\n", part_name);
Steve Rae9bc34792016-06-07 11:19:37 -070080 fastboot_fail("too large for partition");
Steve Raec0aebb32014-08-26 11:47:27 -070081 return;
82 }
83
84 puts("Flashing Raw Image\n");
85
Simon Glass2a981dc2016-02-29 15:25:52 -070086 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Raec0aebb32014-08-26 11:47:27 -070087 if (blks != blkcnt) {
Simon Glassbcce53d2016-02-29 15:25:51 -070088 error("failed writing to device %d\n", dev_desc->devnum);
Steve Rae9bc34792016-06-07 11:19:37 -070089 fastboot_fail("failed writing to device");
Steve Raec0aebb32014-08-26 11:47:27 -070090 return;
91 }
92
93 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
94 part_name);
Steve Rae9bc34792016-06-07 11:19:37 -070095 fastboot_okay("");
Steve Raec0aebb32014-08-26 11:47:27 -070096}
97
Steve Rae64ece842016-06-07 11:19:35 -070098void fb_mmc_flash_write(const char *cmd, void *download_buffer,
Steve Rae9bc34792016-06-07 11:19:37 -070099 unsigned int download_bytes)
Steve Raec0aebb32014-08-26 11:47:27 -0700100{
Simon Glass4101f682016-02-29 15:25:34 -0700101 struct blk_desc *dev_desc;
Steve Raec0aebb32014-08-26 11:47:27 -0700102 disk_partition_t info;
103
Simon Glassdb1d9e72016-02-29 15:25:42 -0700104 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Raec0aebb32014-08-26 11:47:27 -0700105 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
106 error("invalid mmc device\n");
Steve Rae9bc34792016-06-07 11:19:37 -0700107 fastboot_fail("invalid mmc device");
Steve Raec0aebb32014-08-26 11:47:27 -0700108 return;
109 }
110
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200111#ifdef CONFIG_EFI_PARTITION
Steve Rae0ff7e582014-12-12 15:51:54 -0800112 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
113 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
114 __func__);
115 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
116 printf("%s: invalid GPT - refusing to write to flash\n",
117 __func__);
Steve Rae9bc34792016-06-07 11:19:37 -0700118 fastboot_fail("invalid GPT partition");
Steve Rae0ff7e582014-12-12 15:51:54 -0800119 return;
120 }
121 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
122 printf("%s: writing GPT partitions failed\n", __func__);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200123 fastboot_fail("writing GPT partitions failed");
Steve Rae0ff7e582014-12-12 15:51:54 -0800124 return;
125 }
126 printf("........ success\n");
Steve Rae9bc34792016-06-07 11:19:37 -0700127 fastboot_okay("");
Steve Rae0ff7e582014-12-12 15:51:54 -0800128 return;
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200129 }
130#endif
131
132#ifdef CONFIG_DOS_PARTITION
133 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
134 printf("%s: updating MBR\n", __func__);
135 if (is_valid_dos_buf(download_buffer)) {
136 printf("%s: invalid MBR - refusing to write to flash\n",
137 __func__);
138 fastboot_fail("invalid MBR partition");
139 return;
140 }
141 if (write_mbr_partition(dev_desc, download_buffer)) {
142 printf("%s: writing MBR partition failed\n", __func__);
143 fastboot_fail("writing MBR partition failed");
144 return;
145 }
146 printf("........ success\n");
147 fastboot_okay("");
148 return;
149 }
150#endif
151
152 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info)) {
Steve Raec0aebb32014-08-26 11:47:27 -0700153 error("cannot find partition: '%s'\n", cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700154 fastboot_fail("cannot find partition");
Steve Raec0aebb32014-08-26 11:47:27 -0700155 return;
156 }
157
Maxime Riparda5d1e042015-10-15 14:34:14 +0200158 if (is_sparse_image(download_buffer)) {
159 struct fb_mmc_sparse sparse_priv;
Steve Raecc0f08c2016-06-07 11:19:36 -0700160 struct sparse_storage sparse;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200161
162 sparse_priv.dev_desc = dev_desc;
163
Steve Raecc0f08c2016-06-07 11:19:36 -0700164 sparse.blksz = info.blksz;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200165 sparse.start = info.start;
166 sparse.size = info.size;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200167 sparse.write = fb_mmc_sparse_write;
Steve Rae2c724042016-06-07 11:19:38 -0700168 sparse.reserve = fb_mmc_sparse_reserve;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200169
170 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Raecc0f08c2016-06-07 11:19:36 -0700171 sparse.start);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200172
Steve Raecc0f08c2016-06-07 11:19:36 -0700173 sparse.priv = &sparse_priv;
174 write_sparse_image(&sparse, cmd, download_buffer,
Steve Rae9bc34792016-06-07 11:19:37 -0700175 download_bytes);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200176 } else {
Steve Raee5bf9872014-08-26 11:47:30 -0700177 write_raw_image(dev_desc, &info, cmd, download_buffer,
178 download_bytes);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200179 }
Steve Raec0aebb32014-08-26 11:47:27 -0700180}
Dileep Katta89792382015-02-17 18:48:23 +0530181
Steve Rae9bc34792016-06-07 11:19:37 -0700182void fb_mmc_erase(const char *cmd)
Dileep Katta89792382015-02-17 18:48:23 +0530183{
184 int ret;
Simon Glass4101f682016-02-29 15:25:34 -0700185 struct blk_desc *dev_desc;
Dileep Katta89792382015-02-17 18:48:23 +0530186 disk_partition_t info;
187 lbaint_t blks, blks_start, blks_size, grp_size;
188 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
189
190 if (mmc == NULL) {
191 error("invalid mmc device");
Steve Rae9bc34792016-06-07 11:19:37 -0700192 fastboot_fail("invalid mmc device");
Dileep Katta89792382015-02-17 18:48:23 +0530193 return;
194 }
195
Simon Glassdb1d9e72016-02-29 15:25:42 -0700196 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Katta89792382015-02-17 18:48:23 +0530197 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
198 error("invalid mmc device");
Steve Rae9bc34792016-06-07 11:19:37 -0700199 fastboot_fail("invalid mmc device");
Dileep Katta89792382015-02-17 18:48:23 +0530200 return;
201 }
202
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200203 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
Dileep Katta89792382015-02-17 18:48:23 +0530204 if (ret) {
205 error("cannot find partition: '%s'", cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700206 fastboot_fail("cannot find partition");
Dileep Katta89792382015-02-17 18:48:23 +0530207 return;
208 }
209
210 /* Align blocks to erase group size to avoid erasing other partitions */
211 grp_size = mmc->erase_grp_size;
212 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
213 if (info.size >= grp_size)
214 blks_size = (info.size - (blks_start - info.start)) &
215 (~(grp_size - 1));
216 else
217 blks_size = 0;
218
219 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
220 blks_start, blks_start + blks_size);
221
Xu Ziyuanec3cde12016-06-15 16:56:18 +0800222 blks = blk_derase(dev_desc, blks_start, blks_size);
Dileep Katta89792382015-02-17 18:48:23 +0530223 if (blks != blks_size) {
Simon Glassbcce53d2016-02-29 15:25:51 -0700224 error("failed erasing from device %d", dev_desc->devnum);
Steve Rae9bc34792016-06-07 11:19:37 -0700225 fastboot_fail("failed erasing from device");
Dileep Katta89792382015-02-17 18:48:23 +0530226 return;
227 }
228
229 printf("........ erased " LBAFU " bytes from '%s'\n",
230 blks_size * info.blksz, cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700231 fastboot_okay("");
Dileep Katta89792382015-02-17 18:48:23 +0530232}