blob: 75899e4c285842451b519d3c8edff51014e2f35a [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>
9#include <fb_mmc.h>
10#include <part.h>
Steve Raec6015672014-08-26 11:47:30 -070011#include <aboot.h>
12#include <sparse_format.h>
Dileep Kattab1228202015-02-17 18:48:23 +053013#include <mmc.h>
Steve Rae792ac482014-08-26 11:47:27 -070014
Steve Rae7d059342014-12-12 15:51:54 -080015#ifndef CONFIG_FASTBOOT_GPT_NAME
16#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
17#endif
18
Steve Rae792ac482014-08-26 11:47:27 -070019/* The 64 defined bytes plus the '\0' */
20#define RESPONSE_LEN (64 + 1)
21
22static char *response_str;
23
Steve Raec6015672014-08-26 11:47:30 -070024void fastboot_fail(const char *s)
Steve Rae792ac482014-08-26 11:47:27 -070025{
Dileep Katta976a7ce2015-02-13 14:33:42 +080026 strncpy(response_str, "FAIL\0", 5);
Steve Raec6015672014-08-26 11:47:30 -070027 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
Steve Rae792ac482014-08-26 11:47:27 -070028}
29
Steve Raec6015672014-08-26 11:47:30 -070030void fastboot_okay(const char *s)
31{
Dileep Katta976a7ce2015-02-13 14:33:42 +080032 strncpy(response_str, "OKAY\0", 5);
Steve Raec6015672014-08-26 11:47:30 -070033 strncat(response_str, s, RESPONSE_LEN - 4 - 1);
34}
35
Steve Rae792ac482014-08-26 11:47:27 -070036static void write_raw_image(block_dev_desc_t *dev_desc, disk_partition_t *info,
37 const char *part_name, void *buffer,
38 unsigned int download_bytes)
39{
40 lbaint_t blkcnt;
41 lbaint_t blks;
42
43 /* determine number of blocks to write */
44 blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1));
45 blkcnt = blkcnt / info->blksz;
46
47 if (blkcnt > info->size) {
48 error("too large for partition: '%s'\n", part_name);
Steve Raec6015672014-08-26 11:47:30 -070049 fastboot_fail("too large for partition");
Steve Rae792ac482014-08-26 11:47:27 -070050 return;
51 }
52
53 puts("Flashing Raw Image\n");
54
55 blks = dev_desc->block_write(dev_desc->dev, info->start, blkcnt,
56 buffer);
57 if (blks != blkcnt) {
58 error("failed writing to device %d\n", dev_desc->dev);
Steve Raec6015672014-08-26 11:47:30 -070059 fastboot_fail("failed writing to device");
Steve Rae792ac482014-08-26 11:47:27 -070060 return;
61 }
62
63 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
64 part_name);
Steve Raec6015672014-08-26 11:47:30 -070065 fastboot_okay("");
Steve Rae792ac482014-08-26 11:47:27 -070066}
67
68void fb_mmc_flash_write(const char *cmd, void *download_buffer,
69 unsigned int download_bytes, char *response)
70{
Steve Rae792ac482014-08-26 11:47:27 -070071 block_dev_desc_t *dev_desc;
72 disk_partition_t info;
73
74 /* initialize the response buffer */
75 response_str = response;
76
77 dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
78 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
79 error("invalid mmc device\n");
Steve Raec6015672014-08-26 11:47:30 -070080 fastboot_fail("invalid mmc device");
Steve Rae792ac482014-08-26 11:47:27 -070081 return;
82 }
83
Steve Rae7d059342014-12-12 15:51:54 -080084 if (strcmp(cmd, CONFIG_FASTBOOT_GPT_NAME) == 0) {
85 printf("%s: updating MBR, Primary and Backup GPT(s)\n",
86 __func__);
87 if (is_valid_gpt_buf(dev_desc, download_buffer)) {
88 printf("%s: invalid GPT - refusing to write to flash\n",
89 __func__);
90 fastboot_fail("invalid GPT partition");
91 return;
92 }
93 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
94 printf("%s: writing GPT partitions failed\n", __func__);
95 fastboot_fail("writing GPT partitions failed");
96 return;
97 }
98 printf("........ success\n");
99 fastboot_okay("");
100 return;
101 } else if (get_partition_info_efi_by_name(dev_desc, cmd, &info)) {
Steve Rae792ac482014-08-26 11:47:27 -0700102 error("cannot find partition: '%s'\n", cmd);
Steve Raec6015672014-08-26 11:47:30 -0700103 fastboot_fail("cannot find partition");
Steve Rae792ac482014-08-26 11:47:27 -0700104 return;
105 }
106
Steve Raec6015672014-08-26 11:47:30 -0700107 if (is_sparse_image(download_buffer))
108 write_sparse_image(dev_desc, &info, cmd, download_buffer,
109 download_bytes);
110 else
111 write_raw_image(dev_desc, &info, cmd, download_buffer,
112 download_bytes);
Steve Rae792ac482014-08-26 11:47:27 -0700113}
Dileep Kattab1228202015-02-17 18:48:23 +0530114
115void fb_mmc_erase(const char *cmd, char *response)
116{
117 int ret;
118 block_dev_desc_t *dev_desc;
119 disk_partition_t info;
120 lbaint_t blks, blks_start, blks_size, grp_size;
121 struct mmc *mmc = find_mmc_device(CONFIG_FASTBOOT_FLASH_MMC_DEV);
122
123 if (mmc == NULL) {
124 error("invalid mmc device");
125 fastboot_fail("invalid mmc device");
126 return;
127 }
128
129 /* initialize the response buffer */
130 response_str = response;
131
132 dev_desc = get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
133 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
134 error("invalid mmc device");
135 fastboot_fail("invalid mmc device");
136 return;
137 }
138
139 ret = get_partition_info_efi_by_name(dev_desc, cmd, &info);
140 if (ret) {
141 error("cannot find partition: '%s'", cmd);
142 fastboot_fail("cannot find partition");
143 return;
144 }
145
146 /* Align blocks to erase group size to avoid erasing other partitions */
147 grp_size = mmc->erase_grp_size;
148 blks_start = (info.start + grp_size - 1) & ~(grp_size - 1);
149 if (info.size >= grp_size)
150 blks_size = (info.size - (blks_start - info.start)) &
151 (~(grp_size - 1));
152 else
153 blks_size = 0;
154
155 printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n",
156 blks_start, blks_start + blks_size);
157
158 blks = dev_desc->block_erase(dev_desc->dev, blks_start, blks_size);
159 if (blks != blks_size) {
160 error("failed erasing from device %d", dev_desc->dev);
161 fastboot_fail("failed erasing from device");
162 return;
163 }
164
165 printf("........ erased " LBAFU " bytes from '%s'\n",
166 blks_size * info.blksz, cmd);
167 fastboot_okay("");
168}