blob: 46f0073dbc239d8c9e3294e914a354ffadbfe719 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Steve Raec0aebb32014-08-26 11:47:27 -07002/*
3 * Copyright 2014 Broadcom Corporation.
Steve Raec0aebb32014-08-26 11:47:27 -07004 */
5
Steve Rae0ff7e582014-12-12 15:51:54 -08006#include <config.h>
Steve Raec0aebb32014-08-26 11:47:27 -07007#include <common.h>
Simon Glass2a981dc2016-02-29 15:25:52 -07008#include <blk.h>
Maxime Ripard3c8f98f2015-10-15 14:34:13 +02009#include <fastboot.h>
Steve Raec0aebb32014-08-26 11:47:27 -070010#include <fb_mmc.h>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020011#include <image-sparse.h>
Steve Raec0aebb32014-08-26 11:47:27 -070012#include <part.h>
Dileep Katta89792382015-02-17 18:48:23 +053013#include <mmc.h>
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +020014#include <div64.h>
Sam Protsenkoefeccfe2017-05-18 15:04:59 +030015#include <linux/compat.h>
16#include <android_image.h>
Steve Raec0aebb32014-08-26 11:47:27 -070017
Petr Kulhavy6f6c8632016-09-09 10:27:18 +020018/*
19 * FIXME: Ensure we always set these names via Kconfig once xxx_PARTITION is
20 * migrated
21 */
22#ifndef CONFIG_FASTBOOT_GPT_NAME
23#define CONFIG_FASTBOOT_GPT_NAME "gpt"
Steve Rae0ff7e582014-12-12 15:51:54 -080024#endif
25
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +020026
Petr Kulhavy6f6c8632016-09-09 10:27:18 +020027#ifndef CONFIG_FASTBOOT_MBR_NAME
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +020028#define CONFIG_FASTBOOT_MBR_NAME "mbr"
29#endif
30
Sam Protsenkoefeccfe2017-05-18 15:04:59 +030031#define BOOT_PARTITION_NAME "boot"
32
Maxime Riparda5d1e042015-10-15 14:34:14 +020033struct fb_mmc_sparse {
Simon Glass4101f682016-02-29 15:25:34 -070034 struct blk_desc *dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020035};
36
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +020037static int part_get_info_by_name_or_alias(struct blk_desc *dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070038 const char *name, disk_partition_t *info)
39{
40 int ret;
41
Petr Kulhavy87b85302016-09-09 10:27:15 +020042 ret = part_get_info_by_name(dev_desc, name, info);
Alex Deymo88b63292017-04-02 01:49:50 -070043 if (ret < 0) {
Michael Scott8a418022015-03-11 10:02:31 -070044 /* strlen("fastboot_partition_alias_") + 32(part_name) + 1 */
45 char env_alias_name[25 + 32 + 1];
46 char *aliased_part_name;
47
48 /* check for alias */
49 strcpy(env_alias_name, "fastboot_partition_alias_");
50 strncat(env_alias_name, name, 32);
Simon Glass00caae62017-08-03 12:22:12 -060051 aliased_part_name = env_get(env_alias_name);
Michael Scott8a418022015-03-11 10:02:31 -070052 if (aliased_part_name != NULL)
Petr Kulhavy87b85302016-09-09 10:27:15 +020053 ret = part_get_info_by_name(dev_desc,
Michael Scott8a418022015-03-11 10:02:31 -070054 aliased_part_name, info);
55 }
56 return ret;
57}
58
Steve Raecc0f08cd2016-06-07 11:19:36 -070059static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info,
60 lbaint_t blk, lbaint_t blkcnt, const void *buffer)
Maxime Riparda5d1e042015-10-15 14:34:14 +020061{
Steve Raecc0f08cd2016-06-07 11:19:36 -070062 struct fb_mmc_sparse *sparse = info->priv;
Simon Glass4101f682016-02-29 15:25:34 -070063 struct blk_desc *dev_desc = sparse->dev_desc;
Maxime Riparda5d1e042015-10-15 14:34:14 +020064
Steve Raecc0f08cd2016-06-07 11:19:36 -070065 return blk_dwrite(dev_desc, blk, blkcnt, buffer);
Maxime Riparda5d1e042015-10-15 14:34:14 +020066}
67
Steve Rae2c724042016-06-07 11:19:38 -070068static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info,
69 lbaint_t blk, lbaint_t blkcnt)
70{
71 return blkcnt;
72}
73
Simon Glass4101f682016-02-29 15:25:34 -070074static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
Steve Raec0aebb32014-08-26 11:47:27 -070075 const char *part_name, void *buffer,
76 unsigned int download_bytes)
77{
78 lbaint_t blkcnt;
79 lbaint_t blks;
80
81 /* determine number of blocks to write */
82 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
Siarhei Siamashka5e0efc12015-10-28 06:24:16 +020083 blkcnt = lldiv(blkcnt, info->blksz);
Steve Raec0aebb32014-08-26 11:47:27 -070084
85 if (blkcnt > info->size) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090086 pr_err("too large for partition: '%s'\n", part_name);
Steve Rae9bc34792016-06-07 11:19:37 -070087 fastboot_fail("too large for partition");
Steve Raec0aebb32014-08-26 11:47:27 -070088 return;
89 }
90
91 puts("Flashing Raw Image\n");
92
Simon Glass2a981dc2016-02-29 15:25:52 -070093 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
Steve Raec0aebb32014-08-26 11:47:27 -070094 if (blks != blkcnt) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090095 pr_err("failed writing to device %d\n", dev_desc->devnum);
Steve Rae9bc34792016-06-07 11:19:37 -070096 fastboot_fail("failed writing to device");
Steve Raec0aebb32014-08-26 11:47:27 -070097 return;
98 }
99
100 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
101 part_name);
Steve Rae9bc34792016-06-07 11:19:37 -0700102 fastboot_okay("");
Steve Raec0aebb32014-08-26 11:47:27 -0700103}
104
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300105#ifdef CONFIG_ANDROID_BOOT_IMAGE
106/**
107 * Read Android boot image header from boot partition.
108 *
109 * @param[in] dev_desc MMC device descriptor
110 * @param[in] info Boot partition info
111 * @param[out] hdr Where to store read boot image header
112 *
113 * @return Boot image header sectors count or 0 on error
114 */
115static lbaint_t fb_mmc_get_boot_header(struct blk_desc *dev_desc,
116 disk_partition_t *info,
117 struct andr_img_hdr *hdr)
118{
119 ulong sector_size; /* boot partition sector size */
120 lbaint_t hdr_sectors; /* boot image header sectors count */
121 int res;
122
123 /* Calculate boot image sectors count */
124 sector_size = info->blksz;
125 hdr_sectors = DIV_ROUND_UP(sizeof(struct andr_img_hdr), sector_size);
126 if (hdr_sectors == 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900127 pr_err("invalid number of boot sectors: 0");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300128 fastboot_fail("invalid number of boot sectors: 0");
129 return 0;
130 }
131
132 /* Read the boot image header */
133 res = blk_dread(dev_desc, info->start, hdr_sectors, (void *)hdr);
Tom Rini6bafa5a2017-08-14 21:00:44 -0400134 if (res != hdr_sectors) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900135 pr_err("cannot read header from boot partition");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300136 fastboot_fail("cannot read header from boot partition");
137 return 0;
138 }
139
140 /* Check boot header magic string */
141 res = android_image_check_header(hdr);
142 if (res != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900143 pr_err("bad boot image magic");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300144 fastboot_fail("boot partition not initialized");
145 return 0;
146 }
147
148 return hdr_sectors;
149}
150
151/**
152 * Write downloaded zImage to boot partition and repack it properly.
153 *
154 * @param dev_desc MMC device descriptor
155 * @param download_buffer Address to fastboot buffer with zImage in it
156 * @param download_bytes Size of fastboot buffer, in bytes
157 *
158 * @return 0 on success or -1 on error
159 */
160static int fb_mmc_update_zimage(struct blk_desc *dev_desc,
161 void *download_buffer,
162 unsigned int download_bytes)
163{
Tom Rini2341c802017-06-10 09:15:37 -0400164 uintptr_t hdr_addr; /* boot image header address */
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300165 struct andr_img_hdr *hdr; /* boot image header */
166 lbaint_t hdr_sectors; /* boot image header sectors */
167 u8 *ramdisk_buffer;
168 u32 ramdisk_sector_start;
169 u32 ramdisk_sectors;
170 u32 kernel_sector_start;
171 u32 kernel_sectors;
172 u32 sectors_per_page;
173 disk_partition_t info;
174 int res;
175
176 puts("Flashing zImage\n");
177
178 /* Get boot partition info */
179 res = part_get_info_by_name(dev_desc, BOOT_PARTITION_NAME, &info);
180 if (res < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900181 pr_err("cannot find boot partition");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300182 fastboot_fail("cannot find boot partition");
183 return -1;
184 }
185
186 /* Put boot image header in fastboot buffer after downloaded zImage */
Tom Rini2341c802017-06-10 09:15:37 -0400187 hdr_addr = (uintptr_t)download_buffer + ALIGN(download_bytes, PAGE_SIZE);
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300188 hdr = (struct andr_img_hdr *)hdr_addr;
189
190 /* Read boot image header */
191 hdr_sectors = fb_mmc_get_boot_header(dev_desc, &info, hdr);
192 if (hdr_sectors == 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900193 pr_err("unable to read boot image header");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300194 fastboot_fail("unable to read boot image header");
195 return -1;
196 }
197
198 /* Check if boot image has second stage in it (we don't support it) */
199 if (hdr->second_size > 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900200 pr_err("moving second stage is not supported yet");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300201 fastboot_fail("moving second stage is not supported yet");
202 return -1;
203 }
204
205 /* Extract ramdisk location */
206 sectors_per_page = hdr->page_size / info.blksz;
207 ramdisk_sector_start = info.start + sectors_per_page;
208 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
209 sectors_per_page;
210 ramdisk_sectors = DIV_ROUND_UP(hdr->ramdisk_size, hdr->page_size) *
211 sectors_per_page;
212
213 /* Read ramdisk and put it in fastboot buffer after boot image header */
214 ramdisk_buffer = (u8 *)hdr + (hdr_sectors * info.blksz);
215 res = blk_dread(dev_desc, ramdisk_sector_start, ramdisk_sectors,
216 ramdisk_buffer);
Tom Rini6bafa5a2017-08-14 21:00:44 -0400217 if (res != ramdisk_sectors) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900218 pr_err("cannot read ramdisk from boot partition");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300219 fastboot_fail("cannot read ramdisk from boot partition");
220 return -1;
221 }
222
223 /* Write new kernel size to boot image header */
224 hdr->kernel_size = download_bytes;
225 res = blk_dwrite(dev_desc, info.start, hdr_sectors, (void *)hdr);
226 if (res == 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900227 pr_err("cannot writeback boot image header");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300228 fastboot_fail("cannot write back boot image header");
229 return -1;
230 }
231
232 /* Write the new downloaded kernel */
233 kernel_sector_start = info.start + sectors_per_page;
234 kernel_sectors = DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
235 sectors_per_page;
236 res = blk_dwrite(dev_desc, kernel_sector_start, kernel_sectors,
237 download_buffer);
238 if (res == 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900239 pr_err("cannot write new kernel");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300240 fastboot_fail("cannot write new kernel");
241 return -1;
242 }
243
244 /* Write the saved ramdisk back */
245 ramdisk_sector_start = info.start + sectors_per_page;
246 ramdisk_sector_start += DIV_ROUND_UP(hdr->kernel_size, hdr->page_size) *
247 sectors_per_page;
248 res = blk_dwrite(dev_desc, ramdisk_sector_start, ramdisk_sectors,
249 ramdisk_buffer);
250 if (res == 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900251 pr_err("cannot write back original ramdisk");
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300252 fastboot_fail("cannot write back original ramdisk");
253 return -1;
254 }
255
256 puts("........ zImage was updated in boot partition\n");
257 fastboot_okay("");
258 return 0;
259}
260#endif
261
Steve Rae64ece842016-06-07 11:19:35 -0700262void fb_mmc_flash_write(const char *cmd, void *download_buffer,
Steve Rae9bc34792016-06-07 11:19:37 -0700263 unsigned int download_bytes)
Steve Raec0aebb32014-08-26 11:47:27 -0700264{
Simon Glass4101f682016-02-29 15:25:34 -0700265 struct blk_desc *dev_desc;
Steve Raec0aebb32014-08-26 11:47:27 -0700266 disk_partition_t info;
267
Simon Glassdb1d9e72016-02-29 15:25:42 -0700268 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Steve Raec0aebb32014-08-26 11:47:27 -0700269 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900270 pr_err("invalid mmc device\n");
Steve Rae9bc34792016-06-07 11:19:37 -0700271 fastboot_fail("invalid mmc device");
Steve Raec0aebb32014-08-26 11:47:27 -0700272 return;
273 }
274
Patrick Delaunaybd42a942017-01-27 11:00:41 +0100275#if CONFIG_IS_ENABLED(EFI_PARTITION)
Steve Rae0ff7e582014-12-12 15:51:54 -0800276 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
277 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
278 __func__);
279 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
280 printf("%s: invalid GPT - refusing to write to flash\n",
281 __func__);
Steve Rae9bc34792016-06-07 11:19:37 -0700282 fastboot_fail("invalid GPT partition");
Steve Rae0ff7e582014-12-12 15:51:54 -0800283 return;
284 }
285 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
286 printf("%s: writing GPT partitions failed\n", __func__);
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200287 fastboot_fail("writing GPT partitions failed");
Steve Rae0ff7e582014-12-12 15:51:54 -0800288 return;
289 }
290 printf("........ success\n");
Steve Rae9bc34792016-06-07 11:19:37 -0700291 fastboot_okay("");
Steve Rae0ff7e582014-12-12 15:51:54 -0800292 return;
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200293 }
294#endif
295
Patrick Delaunayb0cf7332017-01-27 11:00:37 +0100296#if CONFIG_IS_ENABLED(DOS_PARTITION)
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200297 if (strcmp(cmd, CONFIG_FASTBOOT_MBR_NAME) == 0) {
298 printf("%s: updating MBR\n", __func__);
299 if (is_valid_dos_buf(download_buffer)) {
300 printf("%s: invalid MBR - refusing to write to flash\n",
301 __func__);
302 fastboot_fail("invalid MBR partition");
303 return;
304 }
305 if (write_mbr_partition(dev_desc, download_buffer)) {
306 printf("%s: writing MBR partition failed\n", __func__);
307 fastboot_fail("writing MBR partition failed");
308 return;
309 }
310 printf("........ success\n");
311 fastboot_okay("");
312 return;
313 }
314#endif
315
Sam Protsenkoefeccfe2017-05-18 15:04:59 +0300316#ifdef CONFIG_ANDROID_BOOT_IMAGE
317 if (strncasecmp(cmd, "zimage", 6) == 0) {
318 fb_mmc_update_zimage(dev_desc, download_buffer, download_bytes);
319 return;
320 }
321#endif
322
Alex Deymo88b63292017-04-02 01:49:50 -0700323 if (part_get_info_by_name_or_alias(dev_desc, cmd, &info) < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900324 pr_err("cannot find partition: '%s'\n", cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700325 fastboot_fail("cannot find partition");
Steve Raec0aebb32014-08-26 11:47:27 -0700326 return;
327 }
328
Maxime Riparda5d1e042015-10-15 14:34:14 +0200329 if (is_sparse_image(download_buffer)) {
330 struct fb_mmc_sparse sparse_priv;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700331 struct sparse_storage sparse;
Jassi Brar2f83f212018-04-06 12:05:09 +0530332 int err;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200333
334 sparse_priv.dev_desc = dev_desc;
335
Steve Raecc0f08cd2016-06-07 11:19:36 -0700336 sparse.blksz = info.blksz;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200337 sparse.start = info.start;
338 sparse.size = info.size;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200339 sparse.write = fb_mmc_sparse_write;
Steve Rae2c724042016-06-07 11:19:38 -0700340 sparse.reserve = fb_mmc_sparse_reserve;
Jassi Brar2f83f212018-04-06 12:05:09 +0530341 sparse.mssg = fastboot_fail;
Maxime Riparda5d1e042015-10-15 14:34:14 +0200342
343 printf("Flashing sparse image at offset " LBAFU "\n",
Steve Raecc0f08cd2016-06-07 11:19:36 -0700344 sparse.start);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200345
Steve Raecc0f08cd2016-06-07 11:19:36 -0700346 sparse.priv = &sparse_priv;
Jassi Brar2f83f212018-04-06 12:05:09 +0530347 err = write_sparse_image(&sparse, cmd, download_buffer);
348 if (!err)
349 fastboot_okay("");
Maxime Riparda5d1e042015-10-15 14:34:14 +0200350 } else {
Steve Raee5bf9872014-08-26 11:47:30 -0700351 write_raw_image(dev_desc, &info, cmd, download_buffer,
352 download_bytes);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200353 }
Steve Raec0aebb32014-08-26 11:47:27 -0700354}
Dileep Katta89792382015-02-17 18:48:23 +0530355
Steve Rae9bc34792016-06-07 11:19:37 -0700356void fb_mmc_erase(const char *cmd)
Dileep Katta89792382015-02-17 18:48:23 +0530357{
358 int ret;
Simon Glass4101f682016-02-29 15:25:34 -0700359 struct blk_desc *dev_desc;
Dileep Katta89792382015-02-17 18:48:23 +0530360 disk_partition_t info;
361 lbaint_t blks, blks_start, blks_size, grp_size;
362 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
363
364 if (mmc == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900365 pr_err("invalid mmc device");
Steve Rae9bc34792016-06-07 11:19:37 -0700366 fastboot_fail("invalid mmc device");
Dileep Katta89792382015-02-17 18:48:23 +0530367 return;
368 }
369
Simon Glassdb1d9e72016-02-29 15:25:42 -0700370 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
Dileep Katta89792382015-02-17 18:48:23 +0530371 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900372 pr_err("invalid mmc device");
Steve Rae9bc34792016-06-07 11:19:37 -0700373 fastboot_fail("invalid mmc device");
Dileep Katta89792382015-02-17 18:48:23 +0530374 return;
375 }
376
Petr Kulhavyb6dd69a2016-09-09 10:27:16 +0200377 ret = part_get_info_by_name_or_alias(dev_desc, cmd, &info);
Alex Deymo88b63292017-04-02 01:49:50 -0700378 if (ret < 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900379 pr_err("cannot find partition: '%s'", cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700380 fastboot_fail("cannot find partition");
Dileep Katta89792382015-02-17 18:48:23 +0530381 return;
382 }
383
384 /* Align blocks to erase group size to avoid erasing other partitions */
385 grp_size = mmc->erase_grp_size;
386 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
387 if (info.size >= grp_size)
388 blks_size = (info.size - (blks_start - info.start)) &
389 (~(grp_size - 1));
390 else
391 blks_size = 0;
392
393 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
394 blks_start, blks_start + blks_size);
395
Xu Ziyuanec3cde12016-06-15 16:56:18 +0800396 blks = blk_derase(dev_desc, blks_start, blks_size);
Dileep Katta89792382015-02-17 18:48:23 +0530397 if (blks != blks_size) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900398 pr_err("failed erasing from device %d", dev_desc->devnum);
Steve Rae9bc34792016-06-07 11:19:37 -0700399 fastboot_fail("failed erasing from device");
Dileep Katta89792382015-02-17 18:48:23 +0530400 return;
401 }
402
403 printf("........ erased " LBAFU " bytes from '%s'\n",
404 blks_size * info.blksz, cmd);
Steve Rae9bc34792016-06-07 11:19:37 -0700405 fastboot_okay("");
Dileep Katta89792382015-02-17 18:48:23 +0530406}