blob: cddca0c45fe3f327e3d06543c9d208c6cd53a645 [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>
10#include <malloc.h>
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +000011#include <errno.h>
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000012#include <div64.h>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020013#include <dfu.h>
Stephen Warren0e285b52014-06-11 12:47:27 -060014#include <ext4fs.h>
15#include <fat.h>
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010016#include <mmc.h>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020017
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +010018static unsigned char *dfu_file_buf;
Patrick Delaunay15970d82017-07-19 16:39:23 +020019static u64 dfu_file_buf_len;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +010020static u64 dfu_file_buf_offset;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000021
Afzal Mohammed5a127c82013-09-18 01:14:50 +053022static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000023 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020024{
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010025 struct mmc *mmc;
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010026 u32 blk_start, blk_count, n = 0;
Lukasz Majewskic8151b42014-05-09 16:58:15 +020027 int ret, part_num_bkp = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020028
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010029 mmc = find_mmc_device(dfu->data.mmc.dev_num);
30 if (!mmc) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090031 pr_err("Device MMC %d - not found!", dfu->data.mmc.dev_num);
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010032 return -ENODEV;
33 }
34
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000035 /*
36 * We must ensure that we work in lba_blk_size chunks, so ALIGN
37 * this value.
38 */
39 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
40
41 blk_start = dfu->data.mmc.lba_start +
42 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
43 blk_count = *len / dfu->data.mmc.lba_blk_size;
44 if (blk_start + blk_count >
45 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
46 puts("Request would exceed designated area!\n");
47 return -EINVAL;
48 }
49
Lukasz Majewskic8151b42014-05-09 16:58:15 +020050 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glass797d1b92016-06-12 23:30:33 -060051 part_num_bkp = mmc_get_blk_desc(mmc)->hwpart;
Simon Glass69f45cd2016-05-01 13:52:29 -060052 ret = blk_select_hwpart_devnum(IF_TYPE_MMC,
53 dfu->data.mmc.dev_num,
54 dfu->data.mmc.hw_partition);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020055 if (ret)
56 return ret;
57 }
58
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010059 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warrendd648272014-06-11 16:03:33 -060060 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
61 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010062 switch (op) {
63 case DFU_OP_READ:
Simon Glass797d1b92016-06-12 23:30:33 -060064 n = blk_dread(mmc_get_blk_desc(mmc), blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010065 break;
66 case DFU_OP_WRITE:
Simon Glass797d1b92016-06-12 23:30:33 -060067 n = blk_dwrite(mmc_get_blk_desc(mmc), blk_start, blk_count,
68 buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010069 break;
70 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +090071 pr_err("Operation not supported\n");
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010072 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020073
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010074 if (n != blk_count) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090075 pr_err("MMC operation failed");
Lukasz Majewskic8151b42014-05-09 16:58:15 +020076 if (dfu->data.mmc.hw_partition >= 0)
Simon Glass69f45cd2016-05-01 13:52:29 -060077 blk_select_hwpart_devnum(IF_TYPE_MMC,
78 dfu->data.mmc.dev_num,
79 part_num_bkp);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010080 return -EIO;
81 }
82
Lukasz Majewskic8151b42014-05-09 16:58:15 +020083 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glass69f45cd2016-05-01 13:52:29 -060084 ret = blk_select_hwpart_devnum(IF_TYPE_MMC,
85 dfu->data.mmc.dev_num,
86 part_num_bkp);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020087 if (ret)
88 return ret;
89 }
90
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010091 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020092}
93
Afzal Mohammed5a127c82013-09-18 01:14:50 +053094static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Marek Szyprowski774f8b02019-12-02 12:11:15 +010095 u64 offset, void *buf, u64 *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020096{
Simon Goldschmidt1f92c072019-01-25 19:58:01 +010097 char dev_part_str[8];
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020098 int ret;
Simon Goldschmidt1f92c072019-01-25 19:58:01 +010099 int fstype;
100 loff_t size = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200101
Łukasz Majewski43e66272012-08-23 23:33:55 +0000102 switch (dfu->layout) {
103 case DFU_FS_FAT:
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100104 fstype = FS_TYPE_FAT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000105 break;
106 case DFU_FS_EXT4:
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100107 fstype = FS_TYPE_EXT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000108 break;
109 default:
110 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
111 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000112 return -1;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000113 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200114
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100115 snprintf(dev_part_str, sizeof(dev_part_str), "%d:%d",
116 dfu->data.mmc.dev, dfu->data.mmc.part);
Stephen Warren0e285b52014-06-11 12:47:27 -0600117
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100118 ret = fs_set_blk_dev("mmc", dev_part_str, fstype);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200119 if (ret) {
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100120 puts("dfu: fs_set_blk_dev error!\n");
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200121 return ret;
122 }
123
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100124 switch (op) {
125 case DFU_OP_READ:
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100126 ret = fs_read(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100127 if (ret) {
128 puts("dfu: fs_read error!\n");
129 return ret;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200130 }
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100131 *len = size;
132 break;
133 case DFU_OP_WRITE:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100134 ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100135 if (ret) {
136 puts("dfu: fs_write error!\n");
137 return ret;
138 }
139 break;
140 case DFU_OP_SIZE:
141 ret = fs_size(dfu->name, &size);
142 if (ret) {
143 puts("dfu: fs_size error!\n");
144 return ret;
145 }
146 *len = size;
147 break;
148 default:
149 return -1;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200150 }
151
152 return ret;
153}
154
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100155static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len)
156{
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100157 int ret = 0;
158
159 if (offset == 0) {
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100160 dfu_file_buf_len = 0;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100161 dfu_file_buf_offset = 0;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100162 }
163
164 /* Add to the current buffer. */
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100165 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
166 *len = CONFIG_SYS_DFU_MAX_FILE_SIZE - dfu_file_buf_len;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100167 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
168 dfu_file_buf_len += *len;
169
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100170 if (dfu_file_buf_len == CONFIG_SYS_DFU_MAX_FILE_SIZE) {
171 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
172 dfu_file_buf, &dfu_file_buf_len);
173 dfu_file_buf_offset += dfu_file_buf_len;
174 dfu_file_buf_len = 0;
175 }
176
177 return ret;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100178}
179
180static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
181{
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100182 int ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
183 dfu_file_buf, &dfu_file_buf_len);
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100184
185 /* Now that we're done */
186 dfu_file_buf_len = 0;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100187 dfu_file_buf_offset = 0;
188
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100189 return ret;
190}
191
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000192int dfu_write_medium_mmc(struct dfu_entity *dfu,
193 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200194{
195 int ret = -1;
196
197 switch (dfu->layout) {
198 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000199 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200200 break;
201 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000202 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100203 ret = mmc_file_buf_write(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200204 break;
205 default:
206 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
207 dfu_get_layout(dfu->layout));
208 }
209
210 return ret;
211}
212
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000213int dfu_flush_medium_mmc(struct dfu_entity *dfu)
214{
215 int ret = 0;
216
217 if (dfu->layout != DFU_RAW_ADDR) {
218 /* Do stuff here. */
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100219 ret = mmc_file_buf_write_finish(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000220 }
221
222 return ret;
223}
224
Patrick Delaunay15970d82017-07-19 16:39:23 +0200225int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
Stephen Warren0e285b52014-06-11 12:47:27 -0600226{
227 int ret;
Stephen Warren0e285b52014-06-11 12:47:27 -0600228
229 switch (dfu->layout) {
230 case DFU_RAW_ADDR:
Patrick Delaunay4de51202017-07-19 16:39:22 +0200231 *size = dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
232 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -0600233 case DFU_FS_FAT:
234 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100235 ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
Stephen Warren0e285b52014-06-11 12:47:27 -0600236 if (ret < 0)
237 return ret;
Patrick Delaunay4de51202017-07-19 16:39:22 +0200238 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -0600239 default:
240 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
241 dfu_get_layout(dfu->layout));
242 return -1;
243 }
244}
245
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100246
247static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf,
Stephen Warren411c5e52015-09-04 22:03:47 -0600248 long *len)
249{
250 int ret;
Stephen Warren411c5e52015-09-04 22:03:47 -0600251
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100252 if (offset == 0 || offset >= dfu_file_buf_offset + dfu_file_buf_len ||
253 offset + *len < dfu_file_buf_offset) {
254 u64 file_len = CONFIG_SYS_DFU_MAX_FILE_SIZE;
255
256 ret = mmc_file_op(DFU_OP_READ, dfu, offset, dfu_file_buf,
257 &file_len);
Stephen Warren411c5e52015-09-04 22:03:47 -0600258 if (ret < 0)
259 return ret;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100260 dfu_file_buf_len = file_len;
261 dfu_file_buf_offset = offset;
Stephen Warren411c5e52015-09-04 22:03:47 -0600262 }
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100263 if (offset + *len > dfu_file_buf_offset + dfu_file_buf_len)
Stephen Warren411c5e52015-09-04 22:03:47 -0600264 return -EINVAL;
265
266 /* Add to the current buffer. */
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100267 memcpy(buf, dfu_file_buf + offset - dfu_file_buf_offset, *len);
Stephen Warren411c5e52015-09-04 22:03:47 -0600268
269 return 0;
270}
271
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000272int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
273 long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200274{
275 int ret = -1;
276
277 switch (dfu->layout) {
278 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000279 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200280 break;
281 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000282 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100283 ret = mmc_file_buf_read(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200284 break;
285 default:
286 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
287 dfu_get_layout(dfu->layout));
288 }
289
290 return ret;
291}
292
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100293void dfu_free_entity_mmc(struct dfu_entity *dfu)
294{
295 if (dfu_file_buf) {
296 free(dfu_file_buf);
297 dfu_file_buf = NULL;
298 }
299}
300
Mateusz Zalega711b9312014-04-28 21:13:24 +0200301/*
302 * @param s Parameter string containing space-separated arguments:
303 * 1st:
304 * raw (raw read/write)
305 * fat (files)
306 * ext4 (^)
307 * part (partition image)
308 * 2nd and 3rd:
309 * lba_start and lba_size, for raw write
310 * mmc_dev and mmc_part, for filesystems and part
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200311 * 4th (optional):
312 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalega711b9312014-04-28 21:13:24 +0200313 */
Stephen Warrendd648272014-06-11 16:03:33 -0600314int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200315{
Mateusz Zalega711b9312014-04-28 21:13:24 +0200316 const char *entity_type;
317 size_t second_arg;
318 size_t third_arg;
319
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000320 struct mmc *mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200321
Mateusz Zalega711b9312014-04-28 21:13:24 +0200322 const char *argv[3];
323 const char **parg = argv;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000324
Stephen Warrendd648272014-06-11 16:03:33 -0600325 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
326
Mateusz Zalega711b9312014-04-28 21:13:24 +0200327 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
328 *parg = strsep(&s, " ");
329 if (*parg == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900330 pr_err("Invalid number of arguments.\n");
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000331 return -ENODEV;
332 }
Mateusz Zalega711b9312014-04-28 21:13:24 +0200333 }
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000334
Mateusz Zalega711b9312014-04-28 21:13:24 +0200335 entity_type = argv[0];
Mateusz Zalegab7d42592014-04-28 21:13:25 +0200336 /*
337 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
338 * with default 10.
339 */
340 second_arg = simple_strtoul(argv[1], NULL, 0);
341 third_arg = simple_strtoul(argv[2], NULL, 0);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000342
Stephen Warrendd648272014-06-11 16:03:33 -0600343 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200344 if (mmc == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900345 pr_err("Couldn't find MMC device no. %d.\n",
Stephen Warrendd648272014-06-11 16:03:33 -0600346 dfu->data.mmc.dev_num);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000347 return -ENODEV;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200348 }
349
Mateusz Zalega711b9312014-04-28 21:13:24 +0200350 if (mmc_init(mmc)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900351 pr_err("Couldn't init MMC device.\n");
Mateusz Zalega711b9312014-04-28 21:13:24 +0200352 return -ENODEV;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000353 }
354
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200355 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200356 if (!strcmp(entity_type, "raw")) {
357 dfu->layout = DFU_RAW_ADDR;
358 dfu->data.mmc.lba_start = second_arg;
359 dfu->data.mmc.lba_size = third_arg;
360 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200361
362 /*
363 * Check for an extra entry at dfu_alt_info env variable
364 * specifying the mmc HW defined partition number
365 */
366 if (s)
367 if (!strcmp(strsep(&s, " "), "mmcpart"))
368 dfu->data.mmc.hw_partition =
369 simple_strtoul(s, NULL, 0);
370
Mateusz Zalega711b9312014-04-28 21:13:24 +0200371 } else if (!strcmp(entity_type, "part")) {
Simon Glass05289792020-05-10 11:39:57 -0600372 struct disk_partition partinfo;
Simon Glass797d1b92016-06-12 23:30:33 -0600373 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200374 int mmcdev = second_arg;
375 int mmcpart = third_arg;
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200376 int offset = 0;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200377
Simon Glass3e8bd462016-02-29 15:25:48 -0700378 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900379 pr_err("Couldn't find part #%d on mmc device #%d\n",
Mateusz Zalega711b9312014-04-28 21:13:24 +0200380 mmcpart, mmcdev);
381 return -ENODEV;
382 }
383
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200384 /*
385 * Check for an extra entry at dfu_alt_info env variable
386 * specifying the mmc HW defined partition number
387 */
388 if (s)
389 if (!strcmp(strsep(&s, " "), "offset"))
390 offset = simple_strtoul(s, NULL, 0);
391
Mateusz Zalega711b9312014-04-28 21:13:24 +0200392 dfu->layout = DFU_RAW_ADDR;
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200393 dfu->data.mmc.lba_start = partinfo.start + offset;
394 dfu->data.mmc.lba_size = partinfo.size-offset;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200395 dfu->data.mmc.lba_blk_size = partinfo.blksz;
396 } else if (!strcmp(entity_type, "fat")) {
397 dfu->layout = DFU_FS_FAT;
398 } else if (!strcmp(entity_type, "ext4")) {
399 dfu->layout = DFU_FS_EXT4;
400 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900401 pr_err("Memory layout (%s) not supported!\n", entity_type);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200402 return -ENODEV;
403 }
404
405 /* if it's NOT a raw write */
406 if (strcmp(entity_type, "raw")) {
407 dfu->data.mmc.dev = second_arg;
408 dfu->data.mmc.part = third_arg;
409 }
410
411 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren0e285b52014-06-11 12:47:27 -0600412 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200413 dfu->read_medium = dfu_read_medium_mmc;
414 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000415 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000416 dfu->inited = 0;
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100417 dfu->free_entity = dfu_free_entity_mmc;
418
419 /* Check if file buffer is ready */
420 if (!dfu_file_buf) {
421 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
422 CONFIG_SYS_DFU_MAX_FILE_SIZE);
423 if (!dfu_file_buf) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900424 pr_err("Could not memalign 0x%x bytes",
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100425 CONFIG_SYS_DFU_MAX_FILE_SIZE);
426 return -ENOMEM;
427 }
428 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200429
430 return 0;
431}