blob: 12c54e90ef71ff4348a60a92e815925baafc0201 [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>
Simon Glass1e94b462023-09-14 18:21:46 -060020#include <linux/printk.h>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020021
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +010022static unsigned char *dfu_file_buf;
Patrick Delaunay15970d82017-07-19 16:39:23 +020023static u64 dfu_file_buf_len;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +010024static u64 dfu_file_buf_offset;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000025
Afzal Mohammed5a127c82013-09-18 01:14:50 +053026static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000027 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020028{
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010029 struct mmc *mmc;
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010030 u32 blk_start, blk_count, n = 0;
Lukasz Majewskic8151b42014-05-09 16:58:15 +020031 int ret, part_num_bkp = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020032
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010033 mmc = find_mmc_device(dfu->data.mmc.dev_num);
34 if (!mmc) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090035 pr_err("Device MMC %d - not found!", dfu->data.mmc.dev_num);
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010036 return -ENODEV;
37 }
38
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000039 /*
40 * We must ensure that we work in lba_blk_size chunks, so ALIGN
41 * this value.
42 */
43 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
44
45 blk_start = dfu->data.mmc.lba_start +
46 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
47 blk_count = *len / dfu->data.mmc.lba_blk_size;
48 if (blk_start + blk_count >
49 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
50 puts("Request would exceed designated area!\n");
51 return -EINVAL;
52 }
53
Lukasz Majewskic8151b42014-05-09 16:58:15 +020054 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glass797d1b92016-06-12 23:30:33 -060055 part_num_bkp = mmc_get_blk_desc(mmc)->hwpart;
Simon Glasse33a5c62022-08-11 19:34:59 -060056 ret = blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass69f45cd2016-05-01 13:52:29 -060057 dfu->data.mmc.dev_num,
58 dfu->data.mmc.hw_partition);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020059 if (ret)
60 return ret;
61 }
62
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010063 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warrendd648272014-06-11 16:03:33 -060064 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
65 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010066 switch (op) {
67 case DFU_OP_READ:
Simon Glass797d1b92016-06-12 23:30:33 -060068 n = blk_dread(mmc_get_blk_desc(mmc), blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010069 break;
70 case DFU_OP_WRITE:
Simon Glass797d1b92016-06-12 23:30:33 -060071 n = blk_dwrite(mmc_get_blk_desc(mmc), blk_start, blk_count,
72 buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010073 break;
74 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +090075 pr_err("Operation not supported\n");
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010076 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020077
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010078 if (n != blk_count) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090079 pr_err("MMC operation failed");
Lukasz Majewskic8151b42014-05-09 16:58:15 +020080 if (dfu->data.mmc.hw_partition >= 0)
Simon Glasse33a5c62022-08-11 19:34:59 -060081 blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass69f45cd2016-05-01 13:52:29 -060082 dfu->data.mmc.dev_num,
83 part_num_bkp);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010084 return -EIO;
85 }
86
Lukasz Majewskic8151b42014-05-09 16:58:15 +020087 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glasse33a5c62022-08-11 19:34:59 -060088 ret = blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass69f45cd2016-05-01 13:52:29 -060089 dfu->data.mmc.dev_num,
90 part_num_bkp);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020091 if (ret)
92 return ret;
93 }
94
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010095 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020096}
97
Afzal Mohammed5a127c82013-09-18 01:14:50 +053098static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Marek Szyprowski774f8b02019-12-02 12:11:15 +010099 u64 offset, void *buf, u64 *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200100{
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100101 char dev_part_str[8];
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200102 int ret;
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100103 int fstype;
104 loff_t size = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200105
Łukasz Majewski43e66272012-08-23 23:33:55 +0000106 switch (dfu->layout) {
107 case DFU_FS_FAT:
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100108 fstype = FS_TYPE_FAT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000109 break;
110 case DFU_FS_EXT4:
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100111 fstype = FS_TYPE_EXT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000112 break;
Jaehoon Chungb5f34052020-12-22 11:32:21 +0100113 case DFU_SKIP:
114 return 0;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000115 default:
116 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
117 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000118 return -1;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000119 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200120
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100121 snprintf(dev_part_str, sizeof(dev_part_str), "%d:%d",
122 dfu->data.mmc.dev, dfu->data.mmc.part);
Stephen Warren0e285b52014-06-11 12:47:27 -0600123
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100124 ret = fs_set_blk_dev("mmc", dev_part_str, fstype);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200125 if (ret) {
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100126 puts("dfu: fs_set_blk_dev error!\n");
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200127 return ret;
128 }
129
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100130 switch (op) {
131 case DFU_OP_READ:
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100132 ret = fs_read(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100133 if (ret) {
134 puts("dfu: fs_read error!\n");
135 return ret;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200136 }
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100137 *len = size;
138 break;
139 case DFU_OP_WRITE:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100140 ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100141 if (ret) {
142 puts("dfu: fs_write error!\n");
143 return ret;
144 }
145 break;
146 case DFU_OP_SIZE:
147 ret = fs_size(dfu->name, &size);
148 if (ret) {
149 puts("dfu: fs_size error!\n");
150 return ret;
151 }
152 *len = size;
153 break;
154 default:
155 return -1;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200156 }
157
158 return ret;
159}
160
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100161static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len)
162{
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100163 int ret = 0;
164
165 if (offset == 0) {
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100166 dfu_file_buf_len = 0;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100167 dfu_file_buf_offset = 0;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100168 }
169
170 /* Add to the current buffer. */
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100171 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
172 *len = CONFIG_SYS_DFU_MAX_FILE_SIZE - dfu_file_buf_len;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100173 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
174 dfu_file_buf_len += *len;
175
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100176 if (dfu_file_buf_len == CONFIG_SYS_DFU_MAX_FILE_SIZE) {
177 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
178 dfu_file_buf, &dfu_file_buf_len);
179 dfu_file_buf_offset += dfu_file_buf_len;
180 dfu_file_buf_len = 0;
181 }
182
183 return ret;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100184}
185
186static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
187{
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100188 int ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
189 dfu_file_buf, &dfu_file_buf_len);
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100190
191 /* Now that we're done */
192 dfu_file_buf_len = 0;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100193 dfu_file_buf_offset = 0;
194
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100195 return ret;
196}
197
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000198int dfu_write_medium_mmc(struct dfu_entity *dfu,
199 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200200{
201 int ret = -1;
202
203 switch (dfu->layout) {
204 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000205 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200206 break;
207 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000208 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100209 ret = mmc_file_buf_write(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200210 break;
Marek Szyprowskic533f942020-12-22 11:32:22 +0100211 case DFU_SCRIPT:
212 ret = run_command_list(buf, *len, 0);
213 break;
Jaehoon Chungb5f34052020-12-22 11:32:21 +0100214 case DFU_SKIP:
215 ret = 0;
216 break;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200217 default:
218 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
219 dfu_get_layout(dfu->layout));
220 }
221
222 return ret;
223}
224
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000225int dfu_flush_medium_mmc(struct dfu_entity *dfu)
226{
227 int ret = 0;
228
Marek Szyprowskic533f942020-12-22 11:32:22 +0100229 switch (dfu->layout) {
230 case DFU_FS_FAT:
231 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100232 ret = mmc_file_buf_write_finish(dfu);
Marek Szyprowskic533f942020-12-22 11:32:22 +0100233 break;
234 case DFU_SCRIPT:
235 /* script may have changed the dfu_alt_info */
236 dfu_reinit_needed = true;
237 break;
238 case DFU_RAW_ADDR:
239 case DFU_SKIP:
240 break;
241 default:
242 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
243 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000244 }
245
246 return ret;
247}
248
Patrick Delaunay15970d82017-07-19 16:39:23 +0200249int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
Stephen Warren0e285b52014-06-11 12:47:27 -0600250{
251 int ret;
Stephen Warren0e285b52014-06-11 12:47:27 -0600252
253 switch (dfu->layout) {
254 case DFU_RAW_ADDR:
Patrick Delaunay4de51202017-07-19 16:39:22 +0200255 *size = dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
256 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -0600257 case DFU_FS_FAT:
258 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100259 ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
Stephen Warren0e285b52014-06-11 12:47:27 -0600260 if (ret < 0)
261 return ret;
Patrick Delaunay4de51202017-07-19 16:39:22 +0200262 return 0;
Marek Szyprowskic533f942020-12-22 11:32:22 +0100263 case DFU_SCRIPT:
Jaehoon Chungb5f34052020-12-22 11:32:21 +0100264 case DFU_SKIP:
265 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -0600266 default:
267 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
268 dfu_get_layout(dfu->layout));
269 return -1;
270 }
271}
272
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100273
274static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf,
Stephen Warren411c5e52015-09-04 22:03:47 -0600275 long *len)
276{
277 int ret;
Stephen Warren411c5e52015-09-04 22:03:47 -0600278
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100279 if (offset == 0 || offset >= dfu_file_buf_offset + dfu_file_buf_len ||
280 offset + *len < dfu_file_buf_offset) {
281 u64 file_len = CONFIG_SYS_DFU_MAX_FILE_SIZE;
282
283 ret = mmc_file_op(DFU_OP_READ, dfu, offset, dfu_file_buf,
284 &file_len);
Stephen Warren411c5e52015-09-04 22:03:47 -0600285 if (ret < 0)
286 return ret;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100287 dfu_file_buf_len = file_len;
288 dfu_file_buf_offset = offset;
Stephen Warren411c5e52015-09-04 22:03:47 -0600289 }
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100290 if (offset + *len > dfu_file_buf_offset + dfu_file_buf_len)
Stephen Warren411c5e52015-09-04 22:03:47 -0600291 return -EINVAL;
292
293 /* Add to the current buffer. */
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100294 memcpy(buf, dfu_file_buf + offset - dfu_file_buf_offset, *len);
Stephen Warren411c5e52015-09-04 22:03:47 -0600295
296 return 0;
297}
298
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000299int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
300 long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200301{
302 int ret = -1;
303
304 switch (dfu->layout) {
305 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000306 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200307 break;
308 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000309 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100310 ret = mmc_file_buf_read(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200311 break;
312 default:
313 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
314 dfu_get_layout(dfu->layout));
315 }
316
317 return ret;
318}
319
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100320void dfu_free_entity_mmc(struct dfu_entity *dfu)
321{
322 if (dfu_file_buf) {
323 free(dfu_file_buf);
324 dfu_file_buf = NULL;
325 }
326}
327
Mateusz Zalega711b9312014-04-28 21:13:24 +0200328/*
329 * @param s Parameter string containing space-separated arguments:
330 * 1st:
331 * raw (raw read/write)
332 * fat (files)
333 * ext4 (^)
334 * part (partition image)
335 * 2nd and 3rd:
336 * lba_start and lba_size, for raw write
337 * mmc_dev and mmc_part, for filesystems and part
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200338 * 4th (optional):
339 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalega711b9312014-04-28 21:13:24 +0200340 */
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900341int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char **argv, int argc)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200342{
Mateusz Zalega711b9312014-04-28 21:13:24 +0200343 const char *entity_type;
Marek Szyprowskif21358a2020-12-22 11:32:20 +0100344 ssize_t second_arg;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200345 size_t third_arg;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000346 struct mmc *mmc;
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900347 char *s;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200348
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900349 if (argc < 3) {
350 pr_err("The number of parameters are not enough.\n");
351 return -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200352 }
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000353
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900354 dfu->data.mmc.dev_num = dectoul(devstr, &s);
355 if (*s)
356 return -EINVAL;
357
Mateusz Zalega711b9312014-04-28 21:13:24 +0200358 entity_type = argv[0];
Mateusz Zalegab7d42592014-04-28 21:13:25 +0200359 /*
360 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
361 * with default 10.
362 */
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900363 second_arg = simple_strtol(argv[1], &s, 0);
364 if (*s)
365 return -EINVAL;
366 third_arg = simple_strtoul(argv[2], &s, 0);
367 if (*s)
368 return -EINVAL;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000369
Stephen Warrendd648272014-06-11 16:03:33 -0600370 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200371 if (mmc == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900372 pr_err("Couldn't find MMC device no. %d.\n",
Stephen Warrendd648272014-06-11 16:03:33 -0600373 dfu->data.mmc.dev_num);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000374 return -ENODEV;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200375 }
376
Mateusz Zalega711b9312014-04-28 21:13:24 +0200377 if (mmc_init(mmc)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900378 pr_err("Couldn't init MMC device.\n");
Mateusz Zalega711b9312014-04-28 21:13:24 +0200379 return -ENODEV;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000380 }
381
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200382 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200383 if (!strcmp(entity_type, "raw")) {
384 dfu->layout = DFU_RAW_ADDR;
385 dfu->data.mmc.lba_start = second_arg;
386 dfu->data.mmc.lba_size = third_arg;
387 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200388
389 /*
Marek Vasut60d904a2023-10-29 23:37:22 +0100390 * In case the size is zero (i.e. mmc raw 0x10 0),
391 * assume the user intends to use whole device.
392 */
393 if (third_arg == 0) {
394 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
395
396 dfu->data.mmc.lba_size = blk_dev->lba;
397 }
398
399 /*
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200400 * Check for an extra entry at dfu_alt_info env variable
401 * specifying the mmc HW defined partition number
402 */
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900403 if (argc > 3) {
404 if (argc != 5 || strcmp(argv[3], "mmcpart")) {
405 pr_err("DFU mmc raw accept 'mmcpart <partnum>' option.\n");
406 return -EINVAL;
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900407 }
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900408 dfu->data.mmc.hw_partition =
409 simple_strtoul(argv[4], NULL, 0);
410 }
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200411
Mateusz Zalega711b9312014-04-28 21:13:24 +0200412 } else if (!strcmp(entity_type, "part")) {
Simon Glass05289792020-05-10 11:39:57 -0600413 struct disk_partition partinfo;
Simon Glass797d1b92016-06-12 23:30:33 -0600414 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200415 int mmcdev = second_arg;
416 int mmcpart = third_arg;
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200417 int offset = 0;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200418
Simon Glass3e8bd462016-02-29 15:25:48 -0700419 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900420 pr_err("Couldn't find part #%d on mmc device #%d\n",
Mateusz Zalega711b9312014-04-28 21:13:24 +0200421 mmcpart, mmcdev);
422 return -ENODEV;
423 }
424
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200425 /*
426 * Check for an extra entry at dfu_alt_info env variable
427 * specifying the mmc HW defined partition number
428 */
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900429 if (argc > 3) {
430 if (argc != 5 || strcmp(argv[3], "offset")) {
431 pr_err("DFU mmc raw accept 'mmcpart <partnum>' option.\n");
432 return -EINVAL;
Masami Hiramatsu8db74c12022-01-31 11:52:29 +0900433 }
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900434 dfu->data.mmc.hw_partition =
435 simple_strtoul(argv[4], NULL, 0);
436 }
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200437
Mateusz Zalega711b9312014-04-28 21:13:24 +0200438 dfu->layout = DFU_RAW_ADDR;
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200439 dfu->data.mmc.lba_start = partinfo.start + offset;
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900440 dfu->data.mmc.lba_size = partinfo.size - offset;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200441 dfu->data.mmc.lba_blk_size = partinfo.blksz;
442 } else if (!strcmp(entity_type, "fat")) {
443 dfu->layout = DFU_FS_FAT;
444 } else if (!strcmp(entity_type, "ext4")) {
445 dfu->layout = DFU_FS_EXT4;
Jaehoon Chungb5f34052020-12-22 11:32:21 +0100446 } else if (!strcmp(entity_type, "skip")) {
447 dfu->layout = DFU_SKIP;
Marek Szyprowskic533f942020-12-22 11:32:22 +0100448 } else if (!strcmp(entity_type, "script")) {
449 dfu->layout = DFU_SCRIPT;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200450 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900451 pr_err("Memory layout (%s) not supported!\n", entity_type);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200452 return -ENODEV;
453 }
454
455 /* if it's NOT a raw write */
456 if (strcmp(entity_type, "raw")) {
Marek Szyprowskif21358a2020-12-22 11:32:20 +0100457 dfu->data.mmc.dev = (second_arg != -1) ? second_arg :
458 dfu->data.mmc.dev_num;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200459 dfu->data.mmc.part = third_arg;
460 }
461
462 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren0e285b52014-06-11 12:47:27 -0600463 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200464 dfu->read_medium = dfu_read_medium_mmc;
465 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000466 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000467 dfu->inited = 0;
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100468 dfu->free_entity = dfu_free_entity_mmc;
469
470 /* Check if file buffer is ready */
471 if (!dfu_file_buf) {
472 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
473 CONFIG_SYS_DFU_MAX_FILE_SIZE);
474 if (!dfu_file_buf) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900475 pr_err("Could not memalign 0x%x bytes",
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100476 CONFIG_SYS_DFU_MAX_FILE_SIZE);
477 return -ENOMEM;
478 }
479 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200480
481 return 0;
482}