blob: f5832083ba1a521053254b5fc1e223999b631935 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskicb383cd2012-08-06 14:41:08 +02002/*
3 * dfu.c -- DFU back-end routines
4 *
5 * Copyright (C) 2012 Samsung Electronics
6 * author: Lukasz Majewski <l.majewski@samsung.com>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +02007 */
8
9#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020011#include <malloc.h>
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +000012#include <errno.h>
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000013#include <div64.h>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020014#include <dfu.h>
Stephen Warren0e285b52014-06-11 12:47:27 -060015#include <ext4fs.h>
16#include <fat.h>
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010017#include <mmc.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060018#include <part.h>
Marek Szyprowskic533f942020-12-22 11:32:22 +010019#include <command.h>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020020
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +010021static unsigned char *dfu_file_buf;
Patrick Delaunay15970d82017-07-19 16:39:23 +020022static u64 dfu_file_buf_len;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +010023static u64 dfu_file_buf_offset;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000024
Afzal Mohammed5a127c82013-09-18 01:14:50 +053025static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000026 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020027{
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010028 struct mmc *mmc;
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010029 u32 blk_start, blk_count, n = 0;
Lukasz Majewskic8151b42014-05-09 16:58:15 +020030 int ret, part_num_bkp = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020031
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010032 mmc = find_mmc_device(dfu->data.mmc.dev_num);
33 if (!mmc) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090034 pr_err("Device MMC %d - not found!", dfu->data.mmc.dev_num);
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010035 return -ENODEV;
36 }
37
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000038 /*
39 * We must ensure that we work in lba_blk_size chunks, so ALIGN
40 * this value.
41 */
42 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
43
44 blk_start = dfu->data.mmc.lba_start +
45 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
46 blk_count = *len / dfu->data.mmc.lba_blk_size;
47 if (blk_start + blk_count >
48 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
49 puts("Request would exceed designated area!\n");
50 return -EINVAL;
51 }
52
Lukasz Majewskic8151b42014-05-09 16:58:15 +020053 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glass797d1b92016-06-12 23:30:33 -060054 part_num_bkp = mmc_get_blk_desc(mmc)->hwpart;
Simon Glasse33a5c62022-08-11 19:34:59 -060055 ret = blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass69f45cd2016-05-01 13:52:29 -060056 dfu->data.mmc.dev_num,
57 dfu->data.mmc.hw_partition);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020058 if (ret)
59 return ret;
60 }
61
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010062 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warrendd648272014-06-11 16:03:33 -060063 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
64 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010065 switch (op) {
66 case DFU_OP_READ:
Simon Glass797d1b92016-06-12 23:30:33 -060067 n = blk_dread(mmc_get_blk_desc(mmc), blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010068 break;
69 case DFU_OP_WRITE:
Simon Glass797d1b92016-06-12 23:30:33 -060070 n = blk_dwrite(mmc_get_blk_desc(mmc), blk_start, blk_count,
71 buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010072 break;
73 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +090074 pr_err("Operation not supported\n");
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010075 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020076
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010077 if (n != blk_count) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090078 pr_err("MMC operation failed");
Lukasz Majewskic8151b42014-05-09 16:58:15 +020079 if (dfu->data.mmc.hw_partition >= 0)
Simon Glasse33a5c62022-08-11 19:34:59 -060080 blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass69f45cd2016-05-01 13:52:29 -060081 dfu->data.mmc.dev_num,
82 part_num_bkp);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010083 return -EIO;
84 }
85
Lukasz Majewskic8151b42014-05-09 16:58:15 +020086 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glasse33a5c62022-08-11 19:34:59 -060087 ret = blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass69f45cd2016-05-01 13:52:29 -060088 dfu->data.mmc.dev_num,
89 part_num_bkp);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020090 if (ret)
91 return ret;
92 }
93
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010094 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020095}
96
Afzal Mohammed5a127c82013-09-18 01:14:50 +053097static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Marek Szyprowski774f8b02019-12-02 12:11:15 +010098 u64 offset, void *buf, u64 *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020099{
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100100 char dev_part_str[8];
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200101 int ret;
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100102 int fstype;
103 loff_t size = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200104
Łukasz Majewski43e66272012-08-23 23:33:55 +0000105 switch (dfu->layout) {
106 case DFU_FS_FAT:
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100107 fstype = FS_TYPE_FAT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000108 break;
109 case DFU_FS_EXT4:
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100110 fstype = FS_TYPE_EXT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000111 break;
Jaehoon Chungb5f34052020-12-22 11:32:21 +0100112 case DFU_SKIP:
113 return 0;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000114 default:
115 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
116 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000117 return -1;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000118 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200119
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100120 snprintf(dev_part_str, sizeof(dev_part_str), "%d:%d",
121 dfu->data.mmc.dev, dfu->data.mmc.part);
Stephen Warren0e285b52014-06-11 12:47:27 -0600122
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100123 ret = fs_set_blk_dev("mmc", dev_part_str, fstype);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200124 if (ret) {
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100125 puts("dfu: fs_set_blk_dev error!\n");
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200126 return ret;
127 }
128
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100129 switch (op) {
130 case DFU_OP_READ:
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100131 ret = fs_read(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100132 if (ret) {
133 puts("dfu: fs_read error!\n");
134 return ret;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200135 }
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100136 *len = size;
137 break;
138 case DFU_OP_WRITE:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100139 ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100140 if (ret) {
141 puts("dfu: fs_write error!\n");
142 return ret;
143 }
144 break;
145 case DFU_OP_SIZE:
146 ret = fs_size(dfu->name, &size);
147 if (ret) {
148 puts("dfu: fs_size error!\n");
149 return ret;
150 }
151 *len = size;
152 break;
153 default:
154 return -1;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200155 }
156
157 return ret;
158}
159
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100160static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len)
161{
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100162 int ret = 0;
163
164 if (offset == 0) {
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100165 dfu_file_buf_len = 0;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100166 dfu_file_buf_offset = 0;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100167 }
168
169 /* Add to the current buffer. */
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100170 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
171 *len = CONFIG_SYS_DFU_MAX_FILE_SIZE - dfu_file_buf_len;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100172 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
173 dfu_file_buf_len += *len;
174
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100175 if (dfu_file_buf_len == CONFIG_SYS_DFU_MAX_FILE_SIZE) {
176 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
177 dfu_file_buf, &dfu_file_buf_len);
178 dfu_file_buf_offset += dfu_file_buf_len;
179 dfu_file_buf_len = 0;
180 }
181
182 return ret;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100183}
184
185static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
186{
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100187 int ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
188 dfu_file_buf, &dfu_file_buf_len);
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100189
190 /* Now that we're done */
191 dfu_file_buf_len = 0;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100192 dfu_file_buf_offset = 0;
193
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100194 return ret;
195}
196
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000197int dfu_write_medium_mmc(struct dfu_entity *dfu,
198 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200199{
200 int ret = -1;
201
202 switch (dfu->layout) {
203 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000204 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200205 break;
206 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000207 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100208 ret = mmc_file_buf_write(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200209 break;
Marek Szyprowskic533f942020-12-22 11:32:22 +0100210 case DFU_SCRIPT:
211 ret = run_command_list(buf, *len, 0);
212 break;
Jaehoon Chungb5f34052020-12-22 11:32:21 +0100213 case DFU_SKIP:
214 ret = 0;
215 break;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200216 default:
217 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
218 dfu_get_layout(dfu->layout));
219 }
220
221 return ret;
222}
223
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000224int dfu_flush_medium_mmc(struct dfu_entity *dfu)
225{
226 int ret = 0;
227
Marek Szyprowskic533f942020-12-22 11:32:22 +0100228 switch (dfu->layout) {
229 case DFU_FS_FAT:
230 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100231 ret = mmc_file_buf_write_finish(dfu);
Marek Szyprowskic533f942020-12-22 11:32:22 +0100232 break;
233 case DFU_SCRIPT:
234 /* script may have changed the dfu_alt_info */
235 dfu_reinit_needed = true;
236 break;
237 case DFU_RAW_ADDR:
238 case DFU_SKIP:
239 break;
240 default:
241 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
242 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000243 }
244
245 return ret;
246}
247
Patrick Delaunay15970d82017-07-19 16:39:23 +0200248int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
Stephen Warren0e285b52014-06-11 12:47:27 -0600249{
250 int ret;
Stephen Warren0e285b52014-06-11 12:47:27 -0600251
252 switch (dfu->layout) {
253 case DFU_RAW_ADDR:
Patrick Delaunay4de51202017-07-19 16:39:22 +0200254 *size = dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
255 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -0600256 case DFU_FS_FAT:
257 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100258 ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
Stephen Warren0e285b52014-06-11 12:47:27 -0600259 if (ret < 0)
260 return ret;
Patrick Delaunay4de51202017-07-19 16:39:22 +0200261 return 0;
Marek Szyprowskic533f942020-12-22 11:32:22 +0100262 case DFU_SCRIPT:
Jaehoon Chungb5f34052020-12-22 11:32:21 +0100263 case DFU_SKIP:
264 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -0600265 default:
266 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
267 dfu_get_layout(dfu->layout));
268 return -1;
269 }
270}
271
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100272
273static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf,
Stephen Warren411c5e52015-09-04 22:03:47 -0600274 long *len)
275{
276 int ret;
Stephen Warren411c5e52015-09-04 22:03:47 -0600277
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100278 if (offset == 0 || offset >= dfu_file_buf_offset + dfu_file_buf_len ||
279 offset + *len < dfu_file_buf_offset) {
280 u64 file_len = CONFIG_SYS_DFU_MAX_FILE_SIZE;
281
282 ret = mmc_file_op(DFU_OP_READ, dfu, offset, dfu_file_buf,
283 &file_len);
Stephen Warren411c5e52015-09-04 22:03:47 -0600284 if (ret < 0)
285 return ret;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100286 dfu_file_buf_len = file_len;
287 dfu_file_buf_offset = offset;
Stephen Warren411c5e52015-09-04 22:03:47 -0600288 }
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100289 if (offset + *len > dfu_file_buf_offset + dfu_file_buf_len)
Stephen Warren411c5e52015-09-04 22:03:47 -0600290 return -EINVAL;
291
292 /* Add to the current buffer. */
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100293 memcpy(buf, dfu_file_buf + offset - dfu_file_buf_offset, *len);
Stephen Warren411c5e52015-09-04 22:03:47 -0600294
295 return 0;
296}
297
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000298int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
299 long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200300{
301 int ret = -1;
302
303 switch (dfu->layout) {
304 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000305 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200306 break;
307 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000308 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100309 ret = mmc_file_buf_read(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200310 break;
311 default:
312 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
313 dfu_get_layout(dfu->layout));
314 }
315
316 return ret;
317}
318
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100319void dfu_free_entity_mmc(struct dfu_entity *dfu)
320{
321 if (dfu_file_buf) {
322 free(dfu_file_buf);
323 dfu_file_buf = NULL;
324 }
325}
326
Mateusz Zalega711b9312014-04-28 21:13:24 +0200327/*
328 * @param s Parameter string containing space-separated arguments:
329 * 1st:
330 * raw (raw read/write)
331 * fat (files)
332 * ext4 (^)
333 * part (partition image)
334 * 2nd and 3rd:
335 * lba_start and lba_size, for raw write
336 * mmc_dev and mmc_part, for filesystems and part
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200337 * 4th (optional):
338 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalega711b9312014-04-28 21:13:24 +0200339 */
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900340int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char **argv, int argc)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200341{
Mateusz Zalega711b9312014-04-28 21:13:24 +0200342 const char *entity_type;
Marek Szyprowskif21358a2020-12-22 11:32:20 +0100343 ssize_t second_arg;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200344 size_t third_arg;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000345 struct mmc *mmc;
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900346 char *s;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200347
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900348 if (argc < 3) {
349 pr_err("The number of parameters are not enough.\n");
350 return -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200351 }
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000352
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900353 dfu->data.mmc.dev_num = dectoul(devstr, &s);
354 if (*s)
355 return -EINVAL;
356
Mateusz Zalega711b9312014-04-28 21:13:24 +0200357 entity_type = argv[0];
Mateusz Zalegab7d42592014-04-28 21:13:25 +0200358 /*
359 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
360 * with default 10.
361 */
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900362 second_arg = simple_strtol(argv[1], &s, 0);
363 if (*s)
364 return -EINVAL;
365 third_arg = simple_strtoul(argv[2], &s, 0);
366 if (*s)
367 return -EINVAL;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000368
Stephen Warrendd648272014-06-11 16:03:33 -0600369 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200370 if (mmc == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900371 pr_err("Couldn't find MMC device no. %d.\n",
Stephen Warrendd648272014-06-11 16:03:33 -0600372 dfu->data.mmc.dev_num);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000373 return -ENODEV;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200374 }
375
Mateusz Zalega711b9312014-04-28 21:13:24 +0200376 if (mmc_init(mmc)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900377 pr_err("Couldn't init MMC device.\n");
Mateusz Zalega711b9312014-04-28 21:13:24 +0200378 return -ENODEV;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000379 }
380
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200381 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200382 if (!strcmp(entity_type, "raw")) {
383 dfu->layout = DFU_RAW_ADDR;
384 dfu->data.mmc.lba_start = second_arg;
385 dfu->data.mmc.lba_size = third_arg;
386 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200387
388 /*
389 * Check for an extra entry at dfu_alt_info env variable
390 * specifying the mmc HW defined partition number
391 */
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900392 if (argc > 3) {
393 if (argc != 5 || strcmp(argv[3], "mmcpart")) {
394 pr_err("DFU mmc raw accept 'mmcpart <partnum>' option.\n");
395 return -EINVAL;
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900396 }
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900397 dfu->data.mmc.hw_partition =
398 simple_strtoul(argv[4], NULL, 0);
399 }
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200400
Mateusz Zalega711b9312014-04-28 21:13:24 +0200401 } else if (!strcmp(entity_type, "part")) {
Simon Glass05289792020-05-10 11:39:57 -0600402 struct disk_partition partinfo;
Simon Glass797d1b92016-06-12 23:30:33 -0600403 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200404 int mmcdev = second_arg;
405 int mmcpart = third_arg;
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200406 int offset = 0;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200407
Simon Glass3e8bd462016-02-29 15:25:48 -0700408 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900409 pr_err("Couldn't find part #%d on mmc device #%d\n",
Mateusz Zalega711b9312014-04-28 21:13:24 +0200410 mmcpart, mmcdev);
411 return -ENODEV;
412 }
413
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200414 /*
415 * Check for an extra entry at dfu_alt_info env variable
416 * specifying the mmc HW defined partition number
417 */
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900418 if (argc > 3) {
419 if (argc != 5 || strcmp(argv[3], "offset")) {
420 pr_err("DFU mmc raw accept 'mmcpart <partnum>' option.\n");
421 return -EINVAL;
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900422 }
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900423 dfu->data.mmc.hw_partition =
424 simple_strtoul(argv[4], NULL, 0);
425 }
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200426
Mateusz Zalega711b9312014-04-28 21:13:24 +0200427 dfu->layout = DFU_RAW_ADDR;
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200428 dfu->data.mmc.lba_start = partinfo.start + offset;
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900429 dfu->data.mmc.lba_size = partinfo.size - offset;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200430 dfu->data.mmc.lba_blk_size = partinfo.blksz;
431 } else if (!strcmp(entity_type, "fat")) {
432 dfu->layout = DFU_FS_FAT;
433 } else if (!strcmp(entity_type, "ext4")) {
434 dfu->layout = DFU_FS_EXT4;
Jaehoon Chungb5f34052020-12-22 11:32:21 +0100435 } else if (!strcmp(entity_type, "skip")) {
436 dfu->layout = DFU_SKIP;
Marek Szyprowskic533f942020-12-22 11:32:22 +0100437 } else if (!strcmp(entity_type, "script")) {
438 dfu->layout = DFU_SCRIPT;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200439 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900440 pr_err("Memory layout (%s) not supported!\n", entity_type);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200441 return -ENODEV;
442 }
443
444 /* if it's NOT a raw write */
445 if (strcmp(entity_type, "raw")) {
Marek Szyprowskif21358a2020-12-22 11:32:20 +0100446 dfu->data.mmc.dev = (second_arg != -1) ? second_arg :
447 dfu->data.mmc.dev_num;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200448 dfu->data.mmc.part = third_arg;
449 }
450
451 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren0e285b52014-06-11 12:47:27 -0600452 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200453 dfu->read_medium = dfu_read_medium_mmc;
454 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000455 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000456 dfu->inited = 0;
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100457 dfu->free_entity = dfu_free_entity_mmc;
458
459 /* Check if file buffer is ready */
460 if (!dfu_file_buf) {
461 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
462 CONFIG_SYS_DFU_MAX_FILE_SIZE);
463 if (!dfu_file_buf) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900464 pr_err("Could not memalign 0x%x bytes",
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100465 CONFIG_SYS_DFU_MAX_FILE_SIZE);
466 return -ENOMEM;
467 }
468 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200469
470 return 0;
471}