blob: 8d0524da78f39c5b829bc71813c7549007482ccb [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
Steve Rae0ff7e582014-12-12 15:51:54 -080017#ifndef CONFIG_FASTBOOT_GPT_NAME
18#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
19#endif
20
Maxime Riparda5d1e042015-10-15 14:34:14 +020021struct fb_mmc_sparse {
Simon Glass4101f682016-02-29 15:25:34 -070022 struct blk_desc *dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020023};
24
Simon Glass3e8bd462016-02-29 15:25:48 -070025static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070026 const char *name, disk_partition_t *info)
27{
28 int ret;
29
Simon Glass3e8bd462016-02-29 15:25:48 -070030 ret = part_get_info_efi_by_name(dev_desc, name, info);
Michael Scott8a418022015-03-11 10:02:31 -070031 if (ret) {
32 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
33 char env_alias_name[25 + 32 + 1];
34 char *aliased_part_name;
35
36 /* check for alias */
37 strcpy(env_alias_name, "fastboot_partition_alias_");
38 strncat(env_alias_name, name, 32);
39 aliased_part_name = getenv(env_alias_name);
40 if (aliased_part_name != NULL)
Simon Glass3e8bd462016-02-29 15:25:48 -070041 ret = part_get_info_efi_by_name(dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070042 aliased_part_name, info);
43 }
44 return ret;
45}
46
Steve Raecc0f08cd2016-06-07 11:19:36 -070047static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
48 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda5d1e042015-10-15 14:34:14 +020049{
Steve Raecc0f08cd2016-06-07 11:19:36 -070050 struct fb_mmc_sparse *sparse = info->priv;
Simon Glass4101f682016-02-29 15:25:34 -070051 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020052
Steve Raecc0f08cd2016-06-07 11:19:36 -070053 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
Maxime Riparda5d1e042015-10-15 14:34:14 +020054}
55
Steve Rae2c724042016-06-07 11:19:38 -070056static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
57 lbaint_t blk, lbaint_t blkcnt)
58{
59 return blkcnt;
60}
61
Simon Glass4101f682016-02-29 15:25:34 -070062static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Raec0aebb32014-08-26 11:47:27 -070063 const char *part_name, void *buffer,
64 unsigned int download_bytes)
65{
66 lbaint_t blkcnt;
67 lbaint_t blks;
68
69 /* determine number of blocks to write */
70 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +020071 blkcnt = lldiv(blkcnt, info->blksz);
Steve Raec0aebb32014-08-26 11:47:27 -070072
73 if (blkcnt > info->size) {
74 error("too large for partition: '%s'\n", part_name);
Steve Rae9bc34792016-06-07 11:19:37 -070075 fastboot_fail("too large for partition");
Steve Raec0aebb32014-08-26 11:47:27 -070076 return;
77 }
78
79 puts("Flashing Raw Image\n");
80
Simon Glass2a981dc2016-02-29 15:25:52 -070081 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Raec0aebb32014-08-26 11:47:27 -070082 if (blks != blkcnt) {
Simon Glassbcce53d2016-02-29 15:25:51 -070083 error("failed writing to device %d\n", dev_desc->devnum);
Steve Rae9bc34792016-06-07 11:19:37 -070084 fastboot_fail("failed writing to device");
Steve Raec0aebb32014-08-26 11:47:27 -070085 return;
86 }
87
88 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
89 part_name);
Steve Rae9bc34792016-06-07 11:19:37 -070090 fastboot_okay("");
Steve Raec0aebb32014-08-26 11:47:27 -070091}
92
Steve Rae64ece842016-06-07 11:19:35 -070093void fb_mmc_flash_write(const char *cmd, void *download_buffer,
Steve Rae9bc34792016-06-07 11:19:37 -070094 unsigned int download_bytes)
Steve Raec0aebb32014-08-26 11:47:27 -070095{
Simon Glass4101f682016-02-29 15:25:34 -070096 struct blk_desc *dev_desc;
Steve Raec0aebb32014-08-26 11:47:27 -070097 disk_partition_t info;
98
Simon Glassdb1d9e72016-02-29 15:25:42 -070099 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Raec0aebb32014-08-26 11:47:27 -0700100 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
101 error("invalid mmc device\n");
Steve Rae9bc34792016-06-07 11:19:37 -0700102 fastboot_fail("invalid mmc device");
Steve Raec0aebb32014-08-26 11:47:27 -0700103 return;
104 }
105
Steve Rae0ff7e582014-12-12 15:51:54 -0800106 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
107 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
108 __func__);
109 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
110 printf("%s: invalid GPT - refusing to write to flash\n",
111 __func__);
Steve Rae9bc34792016-06-07 11:19:37 -0700112 fastboot_fail("invalid GPT partition");
Steve Rae0ff7e582014-12-12 15:51:54 -0800113 return;
114 }
115 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
116 printf("%s: writing GPT partitions failed\n", __func__);
Steve Rae9bc34792016-06-07 11:19:37 -0700117 fastboot_fail(
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200118 "writing GPT partitions failed");
Steve Rae0ff7e582014-12-12 15:51:54 -0800119 return;
120 }
121 printf("........ success\n");
Steve Rae9bc34792016-06-07 11:19:37 -0700122 fastboot_okay("");
Steve Rae0ff7e582014-12-12 15:51:54 -0800123 return;
Simon Glass3e8bd462016-02-29 15:25:48 -0700124 } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
Steve Raec0aebb32014-08-26 11:47:27 -0700125 error("cannot find partition: '%s'\n", cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700126 fastboot_fail("cannot find partition");
Steve Raec0aebb32014-08-26 11:47:27 -0700127 return;
128 }
129
Maxime Riparda5d1e042015-10-15 14:34:14 +0200130 if (is_sparse_image(download_buffer)) {
131 struct fb_mmc_sparse sparse_priv;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700132 struct sparse_storage sparse;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200133
134 sparse_priv.dev_desc = dev_desc;
135
Steve Raecc0f08cd2016-06-07 11:19:36 -0700136 sparse.blksz = info.blksz;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200137 sparse.start = info.start;
138 sparse.size = info.size;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200139 sparse.write = fb_mmc_sparse_write;
Steve Rae2c724042016-06-07 11:19:38 -0700140 sparse.reserve = fb_mmc_sparse_reserve;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200141
142 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Raecc0f08cd2016-06-07 11:19:36 -0700143 sparse.start);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200144
Steve Raecc0f08cd2016-06-07 11:19:36 -0700145 sparse.priv = &sparse_priv;
146 write_sparse_image(&sparse, cmd, download_buffer,
Steve Rae9bc34792016-06-07 11:19:37 -0700147 download_bytes);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200148 } else {
Steve Raee5bf9872014-08-26 11:47:30 -0700149 write_raw_image(dev_desc, &info, cmd, download_buffer,
150 download_bytes);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200151 }
Steve Raec0aebb32014-08-26 11:47:27 -0700152}
Dileep Katta89792382015-02-17 18:48:23 +0530153
Steve Rae9bc34792016-06-07 11:19:37 -0700154void fb_mmc_erase(const char *cmd)
Dileep Katta89792382015-02-17 18:48:23 +0530155{
156 int ret;
Simon Glass4101f682016-02-29 15:25:34 -0700157 struct blk_desc *dev_desc;
Dileep Katta89792382015-02-17 18:48:23 +0530158 disk_partition_t info;
159 lbaint_t blks, blks_start, blks_size, grp_size;
160 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
161
162 if (mmc == NULL) {
163 error("invalid mmc device");
Steve Rae9bc34792016-06-07 11:19:37 -0700164 fastboot_fail("invalid mmc device");
Dileep Katta89792382015-02-17 18:48:23 +0530165 return;
166 }
167
Simon Glassdb1d9e72016-02-29 15:25:42 -0700168 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Katta89792382015-02-17 18:48:23 +0530169 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
170 error("invalid mmc device");
Steve Rae9bc34792016-06-07 11:19:37 -0700171 fastboot_fail("invalid mmc device");
Dileep Katta89792382015-02-17 18:48:23 +0530172 return;
173 }
174
Simon Glass3e8bd462016-02-29 15:25:48 -0700175 ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info);
Dileep Katta89792382015-02-17 18:48:23 +0530176 if (ret) {
177 error("cannot find partition: '%s'", cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700178 fastboot_fail("cannot find partition");
Dileep Katta89792382015-02-17 18:48:23 +0530179 return;
180 }
181
182 /* Align blocks to erase group size to avoid erasing other partitions */
183 grp_size = mmc->erase_grp_size;
184 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
185 if (info.size >= grp_size)
186 blks_size = (info.size - (blks_start - info.start)) &
187 (~(grp_size - 1));
188 else
189 blks_size = 0;
190
191 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
192 blks_start, blks_start + blks_size);
193
Xu Ziyuanec3cde12016-06-15 16:56:18 +0800194 blks = blk_derase(dev_desc, blks_start, blks_size);
Dileep Katta89792382015-02-17 18:48:23 +0530195 if (blks != blks_size) {
Simon Glassbcce53d2016-02-29 15:25:51 -0700196 error("failed erasing from device %d", dev_desc->devnum);
Steve Rae9bc34792016-06-07 11:19:37 -0700197 fastboot_fail("failed erasing from device");
Dileep Katta89792382015-02-17 18:48:23 +0530198 return;
199 }
200
201 printf("........ erased " LBAFU " bytes from '%s'\n",
202 blks_size * info.blksz, cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700203 fastboot_okay("");
Dileep Katta89792382015-02-17 18:48:23 +0530204}