blob: 6cc113d825a4057f6126ad33083156ff3222ef43 [file] [log] [blame]
Steve Rae792ac482014-08-26 11:47:27 -07001/*
2 * Copyright 2014 Broadcom Corporation.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
Steve Rae7d059342014-12-12 15:51:54 -08007#include <config.h>
Steve Rae792ac482014-08-26 11:47:27 -07008#include <common.h>
Simon Glass2ee8ada2016-02-29 15:25:52 -07009#include <blk.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +020010#include <fastboot.h>
Steve Rae792ac482014-08-26 11:47:27 -070011#include <fb_mmc.h>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020012#include <image-sparse.h>
Steve Rae792ac482014-08-26 11:47:27 -070013#include <part.h>
Dileep Kattab1228202015-02-17 18:48:23 +053014#include <mmc.h>
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020015#include <div64.h>
Steve Rae792ac482014-08-26 11:47:27 -070016
Petr Kulhavy4ed1eca2016-09-09 10:27:18 +020017/*
18 * FIXME: Ensure we always set these names via Kconfig once xxx_PARTITION is
19 * migrated
20 */
21#ifndef CONFIG_FASTBOOT_GPT_NAME
22#define CONFIG_FASTBOOT_GPT_NAME "gpt"
Steve Rae7d059342014-12-12 15:51:54 -080023#endif
24
Petr Kulhavy9f174c92016-09-09 10:27:16 +020025
Petr Kulhavy4ed1eca2016-09-09 10:27:18 +020026#ifndef CONFIG_FASTBOOT_MBR_NAME
Petr Kulhavy9f174c92016-09-09 10:27:16 +020027#define CONFIG_FASTBOOT_MBR_NAME "mbr"
28#endif
29
Maxime Riparda58e9c72015-10-15 14:34:14 +020030struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070031 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020032};
33
Petr Kulhavy9f174c92016-09-09 10:27:16 +020034static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070035 const char *name, disk_partition_t *info)
36{
37 int ret;
38
Petr Kulhavy712257e2016-09-09 10:27:15 +020039 ret = part_get_info_by_name(dev_desc, name, info);
Michael Scottced33492015-03-11 10:02:31 -070040 if (ret) {
41 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
42 char env_alias_name[25 + 32 + 1];
43 char *aliased_part_name;
44
45 /* check for alias */
46 strcpy(env_alias_name, "fastboot_partition_alias_");
47 strncat(env_alias_name, name, 32);
48 aliased_part_name = getenv(env_alias_name);
49 if (aliased_part_name != NULL)
Petr Kulhavy712257e2016-09-09 10:27:15 +020050 ret = part_get_info_by_name(dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070051 aliased_part_name, info);
52 }
53 return ret;
54}
55
Steve Rae8cf58db2016-06-07 11:19:36 -070056static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
57 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda58e9c72015-10-15 14:34:14 +020058{
Steve Rae8cf58db2016-06-07 11:19:36 -070059 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -070060 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020061
Steve Rae8cf58db2016-06-07 11:19:36 -070062 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +020063}
64
Steve Rae8a99be82016-06-07 11:19:38 -070065static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
66 lbaint_t blk, lbaint_t blkcnt)
67{
68 return blkcnt;
69}
70
Simon Glasse3394752016-02-29 15:25:34 -070071static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Rae792ac482014-08-26 11:47:27 -070072 const char *part_name, void *buffer,
73 unsigned int download_bytes)
74{
75 lbaint_t blkcnt;
76 lbaint_t blks;
77
78 /* determine number of blocks to write */
79 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020080 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -070081
82 if (blkcnt > info->size) {
83 error("too large for partition: '%s'\n", part_name);
Steve Rae76336b22016-06-07 11:19:37 -070084 fastboot_fail("too large for partition");
Steve Rae792ac482014-08-26 11:47:27 -070085 return;
86 }
87
88 puts("Flashing Raw Image\n");
89
Simon Glass2ee8ada2016-02-29 15:25:52 -070090 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Rae792ac482014-08-26 11:47:27 -070091 if (blks != blkcnt) {
Simon Glass2f26fff2016-02-29 15:25:51 -070092 error("failed writing to device %d\n", dev_desc->devnum);
Steve Rae76336b22016-06-07 11:19:37 -070093 fastboot_fail("failed writing to device");
Steve Rae792ac482014-08-26 11:47:27 -070094 return;
95 }
96
97 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
98 part_name);
Steve Rae76336b22016-06-07 11:19:37 -070099 fastboot_okay("");
Steve Rae792ac482014-08-26 11:47:27 -0700100}
101
Steve Raef1f112f2016-06-07 11:19:35 -0700102void fb_mmc_flash_write(const char *cmd, void *download_buffer,
Steve Rae76336b22016-06-07 11:19:37 -0700103 unsigned int download_bytes)
Steve Rae792ac482014-08-26 11:47:27 -0700104{
Simon Glasse3394752016-02-29 15:25:34 -0700105 struct blk_desc *dev_desc;
Steve Rae792ac482014-08-26 11:47:27 -0700106 disk_partition_t info;
107
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700108 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Rae792ac482014-08-26 11:47:27 -0700109 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
110 error("invalid mmc device\n");
Steve Rae76336b22016-06-07 11:19:37 -0700111 fastboot_fail("invalid mmc device");
Steve Rae792ac482014-08-26 11:47:27 -0700112 return;
113 }
114
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +0100115#if CONFIG_IS_ENABLED(EFI_PARTITION)
Steve Rae7d059342014-12-12 15:51:54 -0800116 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
117 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
118 __func__);
119 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
120 printf("%s: invalid GPT - refusing to write to flash\n",
121 __func__);
Steve Rae76336b22016-06-07 11:19:37 -0700122 fastboot_fail("invalid GPT partition");
Steve Rae7d059342014-12-12 15:51:54 -0800123 return;
124 }
125 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
126 printf("%s: writing GPT partitions failed\n", __func__);
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200127 fastboot_fail("writing GPT partitions failed");
Steve Rae7d059342014-12-12 15:51:54 -0800128 return;
129 }
130 printf("........ success\n");
Steve Rae76336b22016-06-07 11:19:37 -0700131 fastboot_okay("");
Steve Rae7d059342014-12-12 15:51:54 -0800132 return;
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200133 }
134#endif
135
Patrick Delaunayf7e07722017-01-27 11:00:37 +0100136#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200137 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
138 printf("%s: updating MBR\n", __func__);
139 if (is_valid_dos_buf(download_buffer)) {
140 printf("%s: invalid MBR - refusing to write to flash\n",
141 __func__);
142 fastboot_fail("invalid MBR partition");
143 return;
144 }
145 if (write_mbr_partition(dev_desc, download_buffer)) {
146 printf("%s: writing MBR partition failed\n", __func__);
147 fastboot_fail("writing MBR partition failed");
148 return;
149 }
150 printf("........ success\n");
151 fastboot_okay("");
152 return;
153 }
154#endif
155
156 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info)) {
Steve Rae792ac482014-08-26 11:47:27 -0700157 error("cannot find partition: '%s'\n", cmd);
Steve Rae76336b22016-06-07 11:19:37 -0700158 fastboot_fail("cannot find partition");
Steve Rae792ac482014-08-26 11:47:27 -0700159 return;
160 }
161
Maxime Riparda58e9c72015-10-15 14:34:14 +0200162 if (is_sparse_image(download_buffer)) {
163 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700164 struct sparse_storage sparse;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200165
166 sparse_priv.dev_desc = dev_desc;
167
Steve Rae8cf58db2016-06-07 11:19:36 -0700168 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200169 sparse.start = info.start;
170 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200171 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700172 sparse.reserve = fb_mmc_sparse_reserve;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200173
174 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700175 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200176
Steve Rae8cf58db2016-06-07 11:19:36 -0700177 sparse.priv = &sparse_priv;
178 write_sparse_image(&sparse, cmd, download_buffer,
Steve Rae76336b22016-06-07 11:19:37 -0700179 download_bytes);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200180 } else {
Steve Raec6015672014-08-26 11:47:30 -0700181 write_raw_image(dev_desc, &info, cmd, download_buffer,
182 download_bytes);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200183 }
Steve Rae792ac482014-08-26 11:47:27 -0700184}
Dileep Kattab1228202015-02-17 18:48:23 +0530185
Steve Rae76336b22016-06-07 11:19:37 -0700186void fb_mmc_erase(const char *cmd)
Dileep Kattab1228202015-02-17 18:48:23 +0530187{
188 int ret;
Simon Glasse3394752016-02-29 15:25:34 -0700189 struct blk_desc *dev_desc;
Dileep Kattab1228202015-02-17 18:48:23 +0530190 disk_partition_t info;
191 lbaint_t blks, blks_start, blks_size, grp_size;
192 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
193
194 if (mmc == NULL) {
195 error("invalid mmc device");
Steve Rae76336b22016-06-07 11:19:37 -0700196 fastboot_fail("invalid mmc device");
Dileep Kattab1228202015-02-17 18:48:23 +0530197 return;
198 }
199
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700200 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Kattab1228202015-02-17 18:48:23 +0530201 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
202 error("invalid mmc device");
Steve Rae76336b22016-06-07 11:19:37 -0700203 fastboot_fail("invalid mmc device");
Dileep Kattab1228202015-02-17 18:48:23 +0530204 return;
205 }
206
Petr Kulhavy9f174c92016-09-09 10:27:16 +0200207 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
Dileep Kattab1228202015-02-17 18:48:23 +0530208 if (ret) {
209 error("cannot find partition: '%s'", cmd);
Steve Rae76336b22016-06-07 11:19:37 -0700210 fastboot_fail("cannot find partition");
Dileep Kattab1228202015-02-17 18:48:23 +0530211 return;
212 }
213
214 /* Align blocks to erase group size to avoid erasing other partitions */
215 grp_size = mmc->erase_grp_size;
216 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
217 if (info.size >= grp_size)
218 blks_size = (info.size - (blks_start - info.start)) &
219 (~(grp_size - 1));
220 else
221 blks_size = 0;
222
223 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
224 blks_start, blks_start + blks_size);
225
Xu Ziyuanec124ce2016-06-15 16:56:18 +0800226 blks = blk_derase(dev_desc, blks_start, blks_size);
Dileep Kattab1228202015-02-17 18:48:23 +0530227 if (blks != blks_size) {
Simon Glass2f26fff2016-02-29 15:25:51 -0700228 error("failed erasing from device %d", dev_desc->devnum);
Steve Rae76336b22016-06-07 11:19:37 -0700229 fastboot_fail("failed erasing from device");
Dileep Kattab1228202015-02-17 18:48:23 +0530230 return;
231 }
232
233 printf("........ erased " LBAFU " bytes from '%s'\n",
234 blks_size * info.blksz, cmd);
Steve Rae76336b22016-06-07 11:19:37 -0700235 fastboot_okay("");
Dileep Kattab1228202015-02-17 18:48:23 +0530236}