]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/fb_mmc.c
board: axs10x: Flush entire cache after programming reset vector
[people/ms/u-boot.git] / common / fb_mmc.c
CommitLineData
c0aebb33
SR
1/*
2 * Copyright 2014 Broadcom Corporation.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
0ff7e585 7#include <config.h>
c0aebb33 8#include <common.h>
2a981dc2 9#include <blk.h>
a5d1e04a 10#include <errno.h>
3c8f98f5 11#include <fastboot.h>
c0aebb33 12#include <fb_mmc.h>
3d4ef38d 13#include <image-sparse.h>
c0aebb33 14#include <part.h>
e5bf9878 15#include <sparse_format.h>
89792381 16#include <mmc.h>
5e0efc1b 17#include <div64.h>
c0aebb33 18
0ff7e585
SR
19#ifndef CONFIG_FASTBOOT_GPT_NAME
20#define CONFIG_FASTBOOT_GPT_NAME GPT_ENTRY_NAME
21#endif
22
c0aebb33
SR
23static char *response_str;
24
a5d1e04a 25struct fb_mmc_sparse {
4101f687 26 struct blk_desc *dev_desc;
a5d1e04a
MR
27};
28
3e8bd469 29static int part_get_info_efi_by_name_or_alias(struct blk_desc *dev_desc,
8a41802f
MS
30 const char *name, disk_partition_t *info)
31{
32 int ret;
33
3e8bd469 34 ret = part_get_info_efi_by_name(dev_desc, name, info);
8a41802f
MS
35 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)
3e8bd469 45 ret = part_get_info_efi_by_name(dev_desc,
8a41802f
MS
46 aliased_part_name, info);
47 }
48 return ret;
49}
50
a5d1e04a
MR
51
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;
4101f687 59 struct blk_desc *dev_desc = sparse->dev_desc;
a5d1e04a
MR
60 int ret;
61
2a981dc2 62 ret = blk_dwrite(dev_desc, offset, size, data);
a5d1e04a
MR
63 if (!ret)
64 return -EIO;
65
66 return ret;
67}
68
4101f687 69static void write_raw_image(struct blk_desc *dev_desc, disk_partition_t *info,
c0aebb33
SR
70 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));
5e0efc1b 78 blkcnt = lldiv(blkcnt, info->blksz);
c0aebb33
SR
79
80 if (blkcnt > info->size) {
81 error("too large for partition: '%s'\n", part_name);
3c8f98f5 82 fastboot_fail(response_str, "too large for partition");
c0aebb33
SR
83 return;
84 }
85
86 puts("Flashing Raw Image\n");
87
2a981dc2 88 blks = blk_dwrite(dev_desc, info->start, blkcnt, buffer);
c0aebb33 89 if (blks != blkcnt) {
bcce53d0 90 error("failed writing to device %d\n", dev_desc->devnum);
3c8f98f5 91 fastboot_fail(response_str, "failed writing to device");
c0aebb33
SR
92 return;
93 }
94
95 printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz,
96 part_name);
3c8f98f5 97 fastboot_okay(response_str, "");
c0aebb33
SR
98}
99
6c9e00ee
MR
100void fb_mmc_flash_write(const char *cmd, unsigned int session_id,
101 void *download_buffer, unsigned int download_bytes,
102 char *response)
c0aebb33 103{
4101f687 104 struct blk_desc *dev_desc;
c0aebb33
SR
105 disk_partition_t info;
106
107 /* initialize the response buffer */
108 response_str = response;
109
db1d9e78 110 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
c0aebb33
SR
111 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
112 error("invalid mmc device\n");
3c8f98f5 113 fastboot_fail(response_str, "invalid mmc device");
c0aebb33
SR
114 return;
115 }
116
0ff7e585
SR
117 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__);
3c8f98f5 123 fastboot_fail(response_str, "invalid GPT partition");
0ff7e585
SR
124 return;
125 }
126 if (write_mbr_and_gpt_partitions(dev_desc, download_buffer)) {
127 printf("%s: writing GPT partitions failed\n", __func__);
3c8f98f5
MR
128 fastboot_fail(response_str,
129 "writing GPT partitions failed");
0ff7e585
SR
130 return;
131 }
132 printf("........ success\n");
3c8f98f5 133 fastboot_okay(response_str, "");
0ff7e585 134 return;
3e8bd469 135 } else if (part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info)) {
c0aebb33 136 error("cannot find partition: '%s'\n", cmd);
3c8f98f5 137 fastboot_fail(response_str, "cannot find partition");
c0aebb33
SR
138 return;
139 }
140
a5d1e04a
MR
141 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
6c9e00ee
MR
156 store_sparse_image(&sparse, &sparse_priv, session_id,
157 download_buffer);
a5d1e04a 158 } else {
e5bf9878
SR
159 write_raw_image(dev_desc, &info, cmd, download_buffer,
160 download_bytes);
a5d1e04a 161 }
3c8f98f5
MR
162
163 fastboot_okay(response_str, "");
c0aebb33 164}
89792381
DK
165
166void fb_mmc_erase(const char *cmd, char *response)
167{
168 int ret;
4101f687 169 struct blk_desc *dev_desc;
89792381
DK
170 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");
3c8f98f5 176 fastboot_fail(response_str, "invalid mmc device");
89792381
DK
177 return;
178 }
179
180 /* initialize the response buffer */
181 response_str = response;
182
db1d9e78 183 dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
89792381
DK
184 if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
185 error("invalid mmc device");
3c8f98f5 186 fastboot_fail(response_str, "invalid mmc device");
89792381
DK
187 return;
188 }
189
3e8bd469 190 ret = part_get_info_efi_by_name_or_alias(dev_desc, cmd, &info);
89792381
DK
191 if (ret) {
192 error("cannot find partition: '%s'", cmd);
3c8f98f5 193 fastboot_fail(response_str, "cannot find partition");
89792381
DK
194 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
7c4213f6 209 blks = dev_desc->block_erase(dev_desc, blks_start, blks_size);
89792381 210 if (blks != blks_size) {
bcce53d0 211 error("failed erasing from device %d", dev_desc->devnum);
3c8f98f5 212 fastboot_fail(response_str, "failed erasing from device");
89792381
DK
213 return;
214 }
215
216 printf("........ erased " LBAFU " bytes from '%s'\n",
217 blks_size * info.blksz, cmd);
3c8f98f5 218 fastboot_okay(response_str, "");
89792381 219}