blob: e3abcc85beea94dd26d5ee0a2cb51fd8616e7542 [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 Riparda5d1e042015-10-15 14:34:14 +020010#include <errno.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020011#include <fastboot.h>
Steve Raec0aebb32014-08-26 11:47:27 -070012#include <fb_mmc.h>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020013#include <image-sparse.h>
Steve Raec0aebb32014-08-26 11:47:27 -070014#include <part.h>
Steve Raee5bf9872014-08-26 11:47:30 -070015#include <sparse_format.h>
Dileep Katta89792382015-02-17 18:48:23 +053016#include <mmc.h>
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +020017#include <div64.h>
Steve Raec0aebb32014-08-26 11:47:27 -070018
Steve Rae0ff7e582014-12-12 15:51:54 -080019#ifndef CONFIG_FASTBOOT_GPT_NAME
20#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
21#endif
22
Steve Raec0aebb32014-08-26 11:47:27 -070023static char *response_str;
24
Maxime Riparda5d1e042015-10-15 14:34:14 +020025struct fb_mmc_sparse {
Simon Glass4101f682016-02-29 15:25:34 -070026 struct blk_desc *dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020027};
28
Simon Glass3e8bd462016-02-29 15:25:48 -070029static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070030 const char *name, disk_partition_t *info)
31{
32 int ret;
33
Simon Glass3e8bd462016-02-29 15:25:48 -070034 ret = part_get_info_efi_by_name(dev_desc, name, info);
Michael Scott8a418022015-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 Glass3e8bd462016-02-29 15:25:48 -070045 ret = part_get_info_efi_by_name(dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070046 aliased_part_name, info);
47 }
48 return ret;
49}
50
Maxime Riparda5d1e042015-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 Glass4101f682016-02-29 15:25:34 -070059 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020060 int ret;
61
Simon Glass2a981dc2016-02-29 15:25:52 -070062 ret = blk_dwrite(dev_desc, offset, size, data);
Maxime Riparda5d1e042015-10-15 14:34:14 +020063 if (!ret)
64 return -EIO;
65
66 return ret;
67}
68
Simon Glass4101f682016-02-29 15:25:34 -070069static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Raec0aebb32014-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 Siamashka5e0efc12015-10-28 06:24:16 +020078 blkcnt = lldiv(blkcnt, info->blksz);
Steve Raec0aebb32014-08-26 11:47:27 -070079
80 if (blkcnt > info->size) {
81 error("too large for partition: '%s'\n", part_name);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020082 fastboot_fail(response_str, "too large for partition");
Steve Raec0aebb32014-08-26 11:47:27 -070083 return;
84 }
85
86 puts("Flashing Raw Image\n");
87
Simon Glass2a981dc2016-02-29 15:25:52 -070088 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Raec0aebb32014-08-26 11:47:27 -070089 if (blks != blkcnt) {
Simon Glassbcce53d2016-02-29 15:25:51 -070090 error("failed writing to device %d\n", dev_desc->devnum);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020091 fastboot_fail(response_str, "failed writing to device");
Steve Raec0aebb32014-08-26 11:47:27 -070092 return;
93 }
94
95 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
96 part_name);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +020097 fastboot_okay(response_str, "");
Steve Raec0aebb32014-08-26 11:47:27 -070098}
99
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200100void fb_mmc_flash_write(const char *cmd, unsigned int session_id,
101 void *download_buffer, unsigned int download_bytes,
102 char *response)
Steve Raec0aebb32014-08-26 11:47:27 -0700103{
Simon Glass4101f682016-02-29 15:25:34 -0700104 struct blk_desc *dev_desc;
Steve Raec0aebb32014-08-26 11:47:27 -0700105 disk_partition_t info;
106
107 /* initialize the response buffer */
108 response_str = response;
109
Simon Glassdb1d9e72016-02-29 15:25:42 -0700110 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Raec0aebb32014-08-26 11:47:27 -0700111 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
112 error("invalid mmc device\n");
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200113 fastboot_fail(response_str, "invalid mmc device");
Steve Raec0aebb32014-08-26 11:47:27 -0700114 return;
115 }
116
Steve Rae0ff7e582014-12-12 15:51:54 -0800117 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
118 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
119 __func__);
120 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
121 printf("%s: invalid GPT - refusing to write to flash\n",
122 __func__);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200123 fastboot_fail(response_str, "invalid GPT partition");
Steve Rae0ff7e582014-12-12 15:51:54 -0800124 return;
125 }
126 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
127 printf("%s: writing GPT partitions failed\n", __func__);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200128 fastboot_fail(response_str,
129 "writing GPT partitions failed");
Steve Rae0ff7e582014-12-12 15:51:54 -0800130 return;
131 }
132 printf("........ success\n");
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200133 fastboot_okay(response_str, "");
Steve Rae0ff7e582014-12-12 15:51:54 -0800134 return;
Simon Glass3e8bd462016-02-29 15:25:48 -0700135 } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
Steve Raec0aebb32014-08-26 11:47:27 -0700136 error("cannot find partition: '%s'\n", cmd);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200137 fastboot_fail(response_str, "cannot find partition");
Steve Raec0aebb32014-08-26 11:47:27 -0700138 return;
139 }
140
Maxime Riparda5d1e042015-10-15 14:34:14 +0200141 if (is_sparse_image(download_buffer)) {
142 struct fb_mmc_sparse sparse_priv;
143 sparse_storage_t sparse;
144
145 sparse_priv.dev_desc = dev_desc;
146
147 sparse.block_sz = info.blksz;
148 sparse.start = info.start;
149 sparse.size = info.size;
150 sparse.name = cmd;
151 sparse.write = fb_mmc_sparse_write;
152
153 printf("Flashing sparse image at offset " LBAFU "\n",
154 info.start);
155
Maxime Ripard6c9e00e2015-10-15 14:34:15 +0200156 store_sparse_image(&sparse, &sparse_priv, session_id,
157 download_buffer);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200158 } else {
Steve Raee5bf9872014-08-26 11:47:30 -0700159 write_raw_image(dev_desc, &info, cmd, download_buffer,
160 download_bytes);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200161 }
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200162
163 fastboot_okay(response_str, "");
Steve Raec0aebb32014-08-26 11:47:27 -0700164}
Dileep Katta89792382015-02-17 18:48:23 +0530165
166void fb_mmc_erase(const char *cmd, char *response)
167{
168 int ret;
Simon Glass4101f682016-02-29 15:25:34 -0700169 struct blk_desc *dev_desc;
Dileep Katta89792382015-02-17 18:48:23 +0530170 disk_partition_t info;
171 lbaint_t blks, blks_start, blks_size, grp_size;
172 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
173
174 if (mmc == NULL) {
175 error("invalid mmc device");
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200176 fastboot_fail(response_str, "invalid mmc device");
Dileep Katta89792382015-02-17 18:48:23 +0530177 return;
178 }
179
180 /* initialize the response buffer */
181 response_str = response;
182
Simon Glassdb1d9e72016-02-29 15:25:42 -0700183 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Katta89792382015-02-17 18:48:23 +0530184 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
185 error("invalid mmc device");
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200186 fastboot_fail(response_str, "invalid mmc device");
Dileep Katta89792382015-02-17 18:48:23 +0530187 return;
188 }
189
Simon Glass3e8bd462016-02-29 15:25:48 -0700190 ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info);
Dileep Katta89792382015-02-17 18:48:23 +0530191 if (ret) {
192 error("cannot find partition: '%s'", cmd);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200193 fastboot_fail(response_str, "cannot find partition");
Dileep Katta89792382015-02-17 18:48:23 +0530194 return;
195 }
196
197 /* Align blocks to erase group size to avoid erasing other partitions */
198 grp_size = mmc->erase_grp_size;
199 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
200 if (info.size >= grp_size)
201 blks_size = (info.size - (blks_start - info.start)) &
202 (~(grp_size - 1));
203 else
204 blks_size = 0;
205
206 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
207 blks_start, blks_start + blks_size);
208
Stephen Warren7c4213f2015-12-07 11:38:48 -0700209 blks = dev_desc->block_erase(dev_desc, blks_start, blks_size);
Dileep Katta89792382015-02-17 18:48:23 +0530210 if (blks != blks_size) {
Simon Glassbcce53d2016-02-29 15:25:51 -0700211 error("failed erasing from device %d", dev_desc->devnum);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200212 fastboot_fail(response_str, "failed erasing from device");
Dileep Katta89792382015-02-17 18:48:23 +0530213 return;
214 }
215
216 printf("........ erased " LBAFU " bytes from '%s'\n",
217 blks_size * info.blksz, cmd);
Maxime Ripard3c8f98f2015-10-15 14:34:13 +0200218 fastboot_okay(response_str, "");
Dileep Katta89792382015-02-17 18:48:23 +0530219}