blob: c739651009b0f806b68bcc998f96d7fa3bd6a757 [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
Steve Rae7d059342014-12-12 15:51:54 -080017#ifndef CONFIG_FASTBOOT_GPT_NAME
18#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
19#endif
20
Maxime Riparda58e9c72015-10-15 14:34:14 +020021struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070022 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020023};
24
Simon Glassb89a8442016-02-29 15:25:48 -070025static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070026 const char *name, disk_partition_t *info)
27{
28 int ret;
29
Simon Glassb89a8442016-02-29 15:25:48 -070030 ret = part_get_info_efi_by_name(dev_desc, name, info);
Michael Scottced33492015-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 Glassb89a8442016-02-29 15:25:48 -070041 ret = part_get_info_efi_by_name(dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070042 aliased_part_name, info);
43 }
44 return ret;
45}
46
Steve Rae8cf58db2016-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 Riparda58e9c72015-10-15 14:34:14 +020049{
Steve Rae8cf58db2016-06-07 11:19:36 -070050 struct fb_mmc_sparse *sparse = info->priv;
Simon Glasse3394752016-02-29 15:25:34 -070051 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020052
Steve Rae8cf58db2016-06-07 11:19:36 -070053 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +020054}
55
Steve Rae8a99be82016-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 Glasse3394752016-02-29 15:25:34 -070062static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Rae792ac482014-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 Siamashka58f30d82015-10-28 06:24:16 +020071 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -070072
73 if (blkcnt > info->size) {
74 error("too large for partition: '%s'\n", part_name);
Steve Rae76336b22016-06-07 11:19:37 -070075 fastboot_fail("too large for partition");
Steve Rae792ac482014-08-26 11:47:27 -070076 return;
77 }
78
79 puts("Flashing Raw Image\n");
80
Simon Glass2ee8ada2016-02-29 15:25:52 -070081 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Rae792ac482014-08-26 11:47:27 -070082 if (blks != blkcnt) {
Simon Glass2f26fff2016-02-29 15:25:51 -070083 error("failed writing to device %d\n", dev_desc->devnum);
Steve Rae76336b22016-06-07 11:19:37 -070084 fastboot_fail("failed writing to device");
Steve Rae792ac482014-08-26 11:47:27 -070085 return;
86 }
87
88 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
89 part_name);
Steve Rae76336b22016-06-07 11:19:37 -070090 fastboot_okay("");
Steve Rae792ac482014-08-26 11:47:27 -070091}
92
Steve Raef1f112f2016-06-07 11:19:35 -070093void fb_mmc_flash_write(const char *cmd, void *download_buffer,
Steve Rae76336b22016-06-07 11:19:37 -070094 unsigned int download_bytes)
Steve Rae792ac482014-08-26 11:47:27 -070095{
Simon Glasse3394752016-02-29 15:25:34 -070096 struct blk_desc *dev_desc;
Steve Rae792ac482014-08-26 11:47:27 -070097 disk_partition_t info;
98
Simon Glassbe1e9bb2016-02-29 15:25:42 -070099 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Rae792ac482014-08-26 11:47:27 -0700100 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
101 error("invalid mmc device\n");
Steve Rae76336b22016-06-07 11:19:37 -0700102 fastboot_fail("invalid mmc device");
Steve Rae792ac482014-08-26 11:47:27 -0700103 return;
104 }
105
Steve Rae7d059342014-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 Rae76336b22016-06-07 11:19:37 -0700112 fastboot_fail("invalid GPT partition");
Steve Rae7d059342014-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 Rae76336b22016-06-07 11:19:37 -0700117 fastboot_fail(
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200118 "writing GPT partitions failed");
Steve Rae7d059342014-12-12 15:51:54 -0800119 return;
120 }
121 printf("........ success\n");
Steve Rae76336b22016-06-07 11:19:37 -0700122 fastboot_okay("");
Steve Rae7d059342014-12-12 15:51:54 -0800123 return;
Simon Glassb89a8442016-02-29 15:25:48 -0700124 } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
Steve Rae792ac482014-08-26 11:47:27 -0700125 error("cannot find partition: '%s'\n", cmd);
Steve Rae76336b22016-06-07 11:19:37 -0700126 fastboot_fail("cannot find partition");
Steve Rae792ac482014-08-26 11:47:27 -0700127 return;
128 }
129
Maxime Riparda58e9c72015-10-15 14:34:14 +0200130 if (is_sparse_image(download_buffer)) {
131 struct fb_mmc_sparse sparse_priv;
Steve Rae8cf58db2016-06-07 11:19:36 -0700132 struct sparse_storage sparse;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200133
134 sparse_priv.dev_desc = dev_desc;
135
Steve Rae8cf58db2016-06-07 11:19:36 -0700136 sparse.blksz = info.blksz;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200137 sparse.start = info.start;
138 sparse.size = info.size;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200139 sparse.write = fb_mmc_sparse_write;
Steve Rae8a99be82016-06-07 11:19:38 -0700140 sparse.reserve = fb_mmc_sparse_reserve;
Maxime Riparda58e9c72015-10-15 14:34:14 +0200141
142 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700143 sparse.start);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200144
Steve Rae8cf58db2016-06-07 11:19:36 -0700145 sparse.priv = &sparse_priv;
146 write_sparse_image(&sparse, cmd, download_buffer,
Steve Rae76336b22016-06-07 11:19:37 -0700147 download_bytes);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200148 } else {
Steve Raec6015672014-08-26 11:47:30 -0700149 write_raw_image(dev_desc, &info, cmd, download_buffer,
150 download_bytes);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200151 }
Steve Rae792ac482014-08-26 11:47:27 -0700152}
Dileep Kattab1228202015-02-17 18:48:23 +0530153
Steve Rae76336b22016-06-07 11:19:37 -0700154void fb_mmc_erase(const char *cmd)
Dileep Kattab1228202015-02-17 18:48:23 +0530155{
156 int ret;
Simon Glasse3394752016-02-29 15:25:34 -0700157 struct blk_desc *dev_desc;
Dileep Kattab1228202015-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 Rae76336b22016-06-07 11:19:37 -0700164 fastboot_fail("invalid mmc device");
Dileep Kattab1228202015-02-17 18:48:23 +0530165 return;
166 }
167
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700168 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Kattab1228202015-02-17 18:48:23 +0530169 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
170 error("invalid mmc device");
Steve Rae76336b22016-06-07 11:19:37 -0700171 fastboot_fail("invalid mmc device");
Dileep Kattab1228202015-02-17 18:48:23 +0530172 return;
173 }
174
Simon Glassb89a8442016-02-29 15:25:48 -0700175 ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info);
Dileep Kattab1228202015-02-17 18:48:23 +0530176 if (ret) {
177 error("cannot find partition: '%s'", cmd);
Steve Rae76336b22016-06-07 11:19:37 -0700178 fastboot_fail("cannot find partition");
Dileep Kattab1228202015-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
Stephen Warrene73f2962015-12-07 11:38:48 -0700194 blks = dev_desc->block_erase(dev_desc, blks_start, blks_size);
Dileep Kattab1228202015-02-17 18:48:23 +0530195 if (blks != blks_size) {
Simon Glass2f26fff2016-02-29 15:25:51 -0700196 error("failed erasing from device %d", dev_desc->devnum);
Steve Rae76336b22016-06-07 11:19:37 -0700197 fastboot_fail("failed erasing from device");
Dileep Kattab1228202015-02-17 18:48:23 +0530198 return;
199 }
200
201 printf("........ erased " LBAFU " bytes from '%s'\n",
202 blks_size * info.blksz, cmd);
Steve Rae76336b22016-06-07 11:19:37 -0700203 fastboot_okay("");
Dileep Kattab1228202015-02-17 18:48:23 +0530204}