blob: 9e53adba5ed973c528674a2bfc42ed538ba6c5e0 [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 Riparda58e9c72015-10-15 14:34:14 +020010#include <errno.h>
Maxime Ripard00e8eb72015-10-15 14:34:13 +020011#include <fastboot.h>
Steve Rae792ac482014-08-26 11:47:27 -070012#include <fb_mmc.h>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020013#include <image-sparse.h>
Steve Rae792ac482014-08-26 11:47:27 -070014#include <part.h>
Steve Raec6015672014-08-26 11:47:30 -070015#include <sparse_format.h>
Dileep Kattab1228202015-02-17 18:48:23 +053016#include <mmc.h>
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020017#include <div64.h>
Steve Rae792ac482014-08-26 11:47:27 -070018
Steve Rae7d059342014-12-12 15:51:54 -080019#ifndef CONFIG_FASTBOOT_GPT_NAME
20#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
21#endif
22
Steve Rae792ac482014-08-26 11:47:27 -070023static char *response_str;
24
Maxime Riparda58e9c72015-10-15 14:34:14 +020025struct fb_mmc_sparse {
Simon Glasse3394752016-02-29 15:25:34 -070026 struct blk_desc *dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020027};
28
Simon Glassb89a8442016-02-29 15:25:48 -070029static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070030 const char *name, disk_partition_t *info)
31{
32 int ret;
33
Simon Glassb89a8442016-02-29 15:25:48 -070034 ret = part_get_info_efi_by_name(dev_desc, name, info);
Michael Scottced33492015-03-11 10:02:31 -070035 if (ret) {
36 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
37 char env_alias_name[25 + 32 + 1];
38 char *aliased_part_name;
39
40 /* check for alias */
41 strcpy(env_alias_name, "fastboot_partition_alias_");
42 strncat(env_alias_name, name, 32);
43 aliased_part_name = getenv(env_alias_name);
44 if (aliased_part_name != NULL)
Simon Glassb89a8442016-02-29 15:25:48 -070045 ret = part_get_info_efi_by_name(dev_desc,
Michael Scottced33492015-03-11 10:02:31 -070046 aliased_part_name, info);
47 }
48 return ret;
49}
50
Maxime Riparda58e9c72015-10-15 14:34:14 +020051
52static int fb_mmc_sparse_write(struct sparse_storage *storage,
53 void *priv,
54 unsigned int offset,
55 unsigned int size,
56 char *data)
57{
58 struct fb_mmc_sparse *sparse = priv;
Simon Glasse3394752016-02-29 15:25:34 -070059 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda58e9c72015-10-15 14:34:14 +020060 int ret;
61
Simon Glass2ee8ada2016-02-29 15:25:52 -070062 ret = blk_dwrite(dev_desc, offset, size, data);
Maxime Riparda58e9c72015-10-15 14:34:14 +020063 if (!ret)
64 return -EIO;
65
66 return ret;
67}
68
Simon Glasse3394752016-02-29 15:25:34 -070069static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Rae792ac482014-08-26 11:47:27 -070070 const char *part_name, void *buffer,
71 unsigned int download_bytes)
72{
73 lbaint_t blkcnt;
74 lbaint_t blks;
75
76 /* determine number of blocks to write */
77 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka58f30d82015-10-28 06:24:16 +020078 blkcnt = lldiv(blkcnt, info->blksz);
Steve Rae792ac482014-08-26 11:47:27 -070079
80 if (blkcnt > info->size) {
81 error("too large for partition: '%s'\n", part_name);
Maxime Ripard00e8eb72015-10-15 14:34:13 +020082 fastboot_fail(response_str, "too large for partition");
Steve Rae792ac482014-08-26 11:47:27 -070083 return;
84 }
85
86 puts("Flashing Raw Image\n");
87
Simon Glass2ee8ada2016-02-29 15:25:52 -070088 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Rae792ac482014-08-26 11:47:27 -070089 if (blks != blkcnt) {
Simon Glass2f26fff2016-02-29 15:25:51 -070090 error("failed writing to device %d\n", dev_desc->devnum);
Maxime Ripard00e8eb72015-10-15 14:34:13 +020091 fastboot_fail(response_str, "failed writing to device");
Steve Rae792ac482014-08-26 11:47:27 -070092 return;
93 }
94
95 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
96 part_name);
Maxime Ripard00e8eb72015-10-15 14:34:13 +020097 fastboot_okay(response_str, "");
Steve Rae792ac482014-08-26 11:47:27 -070098}
99
Steve Raef1f112f2016-06-07 11:19:35 -0700100void fb_mmc_flash_write(const char *cmd, void *download_buffer,
101 unsigned int download_bytes, char *response)
Steve Rae792ac482014-08-26 11:47:27 -0700102{
Simon Glasse3394752016-02-29 15:25:34 -0700103 struct blk_desc *dev_desc;
Steve Rae792ac482014-08-26 11:47:27 -0700104 disk_partition_t info;
105
106 /* initialize the response buffer */
107 response_str = response;
108
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700109 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Rae792ac482014-08-26 11:47:27 -0700110 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
111 error("invalid mmc device\n");
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200112 fastboot_fail(response_str, "invalid mmc device");
Steve Rae792ac482014-08-26 11:47:27 -0700113 return;
114 }
115
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__);
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200122 fastboot_fail(response_str, "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__);
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200127 fastboot_fail(response_str,
128 "writing GPT partitions failed");
Steve Rae7d059342014-12-12 15:51:54 -0800129 return;
130 }
131 printf("........ success\n");
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200132 fastboot_okay(response_str, "");
Steve Rae7d059342014-12-12 15:51:54 -0800133 return;
Simon Glassb89a8442016-02-29 15:25:48 -0700134 } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
Steve Rae792ac482014-08-26 11:47:27 -0700135 error("cannot find partition: '%s'\n", cmd);
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200136 fastboot_fail(response_str, "cannot find partition");
Steve Rae792ac482014-08-26 11:47:27 -0700137 return;
138 }
139
Maxime Riparda58e9c72015-10-15 14:34:14 +0200140 if (is_sparse_image(download_buffer)) {
141 struct fb_mmc_sparse sparse_priv;
142 sparse_storage_t sparse;
143
144 sparse_priv.dev_desc = dev_desc;
145
146 sparse.block_sz = info.blksz;
147 sparse.start = info.start;
148 sparse.size = info.size;
149 sparse.name = cmd;
150 sparse.write = fb_mmc_sparse_write;
151
152 printf("Flashing sparse image at offset " LBAFU "\n",
153 info.start);
154
Steve Raef1f112f2016-06-07 11:19:35 -0700155 store_sparse_image(&sparse, &sparse_priv, download_buffer);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200156 } else {
Steve Raec6015672014-08-26 11:47:30 -0700157 write_raw_image(dev_desc, &info, cmd, download_buffer,
158 download_bytes);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200159 }
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200160
161 fastboot_okay(response_str, "");
Steve Rae792ac482014-08-26 11:47:27 -0700162}
Dileep Kattab1228202015-02-17 18:48:23 +0530163
164void fb_mmc_erase(const char *cmd, char *response)
165{
166 int ret;
Simon Glasse3394752016-02-29 15:25:34 -0700167 struct blk_desc *dev_desc;
Dileep Kattab1228202015-02-17 18:48:23 +0530168 disk_partition_t info;
169 lbaint_t blks, blks_start, blks_size, grp_size;
170 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
171
172 if (mmc == NULL) {
173 error("invalid mmc device");
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200174 fastboot_fail(response_str, "invalid mmc device");
Dileep Kattab1228202015-02-17 18:48:23 +0530175 return;
176 }
177
178 /* initialize the response buffer */
179 response_str = response;
180
Simon Glassbe1e9bb2016-02-29 15:25:42 -0700181 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Kattab1228202015-02-17 18:48:23 +0530182 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
183 error("invalid mmc device");
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200184 fastboot_fail(response_str, "invalid mmc device");
Dileep Kattab1228202015-02-17 18:48:23 +0530185 return;
186 }
187
Simon Glassb89a8442016-02-29 15:25:48 -0700188 ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info);
Dileep Kattab1228202015-02-17 18:48:23 +0530189 if (ret) {
190 error("cannot find partition: '%s'", cmd);
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200191 fastboot_fail(response_str, "cannot find partition");
Dileep Kattab1228202015-02-17 18:48:23 +0530192 return;
193 }
194
195 /* Align blocks to erase group size to avoid erasing other partitions */
196 grp_size = mmc->erase_grp_size;
197 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
198 if (info.size >= grp_size)
199 blks_size = (info.size - (blks_start - info.start)) &
200 (~(grp_size - 1));
201 else
202 blks_size = 0;
203
204 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
205 blks_start, blks_start + blks_size);
206
Stephen Warrene73f2962015-12-07 11:38:48 -0700207 blks = dev_desc->block_erase(dev_desc, blks_start, blks_size);
Dileep Kattab1228202015-02-17 18:48:23 +0530208 if (blks != blks_size) {
Simon Glass2f26fff2016-02-29 15:25:51 -0700209 error("failed erasing from device %d", dev_desc->devnum);
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200210 fastboot_fail(response_str, "failed erasing from device");
Dileep Kattab1228202015-02-17 18:48:23 +0530211 return;
212 }
213
214 printf("........ erased " LBAFU " bytes from '%s'\n",
215 blks_size * info.blksz, cmd);
Maxime Ripard00e8eb72015-10-15 14:34:13 +0200216 fastboot_okay(response_str, "");
Dileep Kattab1228202015-02-17 18:48:23 +0530217}