blob: 691d01c7ebdfcc301fed0cf9e0b0a46d54bac941 [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>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020019
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +010020static unsigned char *dfu_file_buf;
Patrick Delaunay15970d82017-07-19 16:39:23 +020021static u64 dfu_file_buf_len;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +010022static u64 dfu_file_buf_offset;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000023
Afzal Mohammed5a127c82013-09-18 01:14:50 +053024static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000025 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020026{
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010027 struct mmc *mmc;
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010028 u32 blk_start, blk_count, n = 0;
Lukasz Majewskic8151b42014-05-09 16:58:15 +020029 int ret, part_num_bkp = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020030
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010031 mmc = find_mmc_device(dfu->data.mmc.dev_num);
32 if (!mmc) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090033 pr_err("Device MMC %d - not found!", dfu->data.mmc.dev_num);
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010034 return -ENODEV;
35 }
36
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000037 /*
38 * We must ensure that we work in lba_blk_size chunks, so ALIGN
39 * this value.
40 */
41 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
42
43 blk_start = dfu->data.mmc.lba_start +
44 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
45 blk_count = *len / dfu->data.mmc.lba_blk_size;
46 if (blk_start + blk_count >
47 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
48 puts("Request would exceed designated area!\n");
49 return -EINVAL;
50 }
51
Lukasz Majewskic8151b42014-05-09 16:58:15 +020052 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glass797d1b92016-06-12 23:30:33 -060053 part_num_bkp = mmc_get_blk_desc(mmc)->hwpart;
Simon Glass69f45cd2016-05-01 13:52:29 -060054 ret = blk_select_hwpart_devnum(IF_TYPE_MMC,
55 dfu->data.mmc.dev_num,
56 dfu->data.mmc.hw_partition);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020057 if (ret)
58 return ret;
59 }
60
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010061 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warrendd648272014-06-11 16:03:33 -060062 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
63 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010064 switch (op) {
65 case DFU_OP_READ:
Simon Glass797d1b92016-06-12 23:30:33 -060066 n = blk_dread(mmc_get_blk_desc(mmc), blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010067 break;
68 case DFU_OP_WRITE:
Simon Glass797d1b92016-06-12 23:30:33 -060069 n = blk_dwrite(mmc_get_blk_desc(mmc), blk_start, blk_count,
70 buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010071 break;
72 default:
Masahiro Yamada9b643e32017-09-16 14:10:41 +090073 pr_err("Operation not supported\n");
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010074 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020075
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010076 if (n != blk_count) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090077 pr_err("MMC operation failed");
Lukasz Majewskic8151b42014-05-09 16:58:15 +020078 if (dfu->data.mmc.hw_partition >= 0)
Simon Glass69f45cd2016-05-01 13:52:29 -060079 blk_select_hwpart_devnum(IF_TYPE_MMC,
80 dfu->data.mmc.dev_num,
81 part_num_bkp);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010082 return -EIO;
83 }
84
Lukasz Majewskic8151b42014-05-09 16:58:15 +020085 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glass69f45cd2016-05-01 13:52:29 -060086 ret = blk_select_hwpart_devnum(IF_TYPE_MMC,
87 dfu->data.mmc.dev_num,
88 part_num_bkp);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020089 if (ret)
90 return ret;
91 }
92
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010093 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020094}
95
Afzal Mohammed5a127c82013-09-18 01:14:50 +053096static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Marek Szyprowski774f8b02019-12-02 12:11:15 +010097 u64 offset, void *buf, u64 *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020098{
Simon Goldschmidt1f92c072019-01-25 19:58:01 +010099 char dev_part_str[8];
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200100 int ret;
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100101 int fstype;
102 loff_t size = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200103
Łukasz Majewski43e66272012-08-23 23:33:55 +0000104 switch (dfu->layout) {
105 case DFU_FS_FAT:
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100106 fstype = FS_TYPE_FAT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000107 break;
108 case DFU_FS_EXT4:
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100109 fstype = FS_TYPE_EXT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000110 break;
111 default:
112 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
113 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000114 return -1;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000115 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200116
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100117 snprintf(dev_part_str, sizeof(dev_part_str), "%d:%d",
118 dfu->data.mmc.dev, dfu->data.mmc.part);
Stephen Warren0e285b52014-06-11 12:47:27 -0600119
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100120 ret = fs_set_blk_dev("mmc", dev_part_str, fstype);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200121 if (ret) {
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100122 puts("dfu: fs_set_blk_dev error!\n");
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200123 return ret;
124 }
125
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100126 switch (op) {
127 case DFU_OP_READ:
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100128 ret = fs_read(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100129 if (ret) {
130 puts("dfu: fs_read error!\n");
131 return ret;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200132 }
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100133 *len = size;
134 break;
135 case DFU_OP_WRITE:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100136 ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt1f92c072019-01-25 19:58:01 +0100137 if (ret) {
138 puts("dfu: fs_write error!\n");
139 return ret;
140 }
141 break;
142 case DFU_OP_SIZE:
143 ret = fs_size(dfu->name, &size);
144 if (ret) {
145 puts("dfu: fs_size error!\n");
146 return ret;
147 }
148 *len = size;
149 break;
150 default:
151 return -1;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200152 }
153
154 return ret;
155}
156
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100157static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len)
158{
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100159 int ret = 0;
160
161 if (offset == 0) {
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100162 dfu_file_buf_len = 0;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100163 dfu_file_buf_offset = 0;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100164 }
165
166 /* Add to the current buffer. */
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100167 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
168 *len = CONFIG_SYS_DFU_MAX_FILE_SIZE - dfu_file_buf_len;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100169 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
170 dfu_file_buf_len += *len;
171
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100172 if (dfu_file_buf_len == CONFIG_SYS_DFU_MAX_FILE_SIZE) {
173 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
174 dfu_file_buf, &dfu_file_buf_len);
175 dfu_file_buf_offset += dfu_file_buf_len;
176 dfu_file_buf_len = 0;
177 }
178
179 return ret;
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100180}
181
182static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
183{
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100184 int ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
185 dfu_file_buf, &dfu_file_buf_len);
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100186
187 /* Now that we're done */
188 dfu_file_buf_len = 0;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100189 dfu_file_buf_offset = 0;
190
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100191 return ret;
192}
193
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000194int dfu_write_medium_mmc(struct dfu_entity *dfu,
195 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200196{
197 int ret = -1;
198
199 switch (dfu->layout) {
200 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000201 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200202 break;
203 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000204 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100205 ret = mmc_file_buf_write(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200206 break;
207 default:
208 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
209 dfu_get_layout(dfu->layout));
210 }
211
212 return ret;
213}
214
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000215int dfu_flush_medium_mmc(struct dfu_entity *dfu)
216{
217 int ret = 0;
218
219 if (dfu->layout != DFU_RAW_ADDR) {
220 /* Do stuff here. */
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100221 ret = mmc_file_buf_write_finish(dfu);
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000222 }
223
224 return ret;
225}
226
Patrick Delaunay15970d82017-07-19 16:39:23 +0200227int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
Stephen Warren0e285b52014-06-11 12:47:27 -0600228{
229 int ret;
Stephen Warren0e285b52014-06-11 12:47:27 -0600230
231 switch (dfu->layout) {
232 case DFU_RAW_ADDR:
Patrick Delaunay4de51202017-07-19 16:39:22 +0200233 *size = dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
234 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -0600235 case DFU_FS_FAT:
236 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100237 ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
Stephen Warren0e285b52014-06-11 12:47:27 -0600238 if (ret < 0)
239 return ret;
Patrick Delaunay4de51202017-07-19 16:39:22 +0200240 return 0;
Stephen Warren0e285b52014-06-11 12:47:27 -0600241 default:
242 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
243 dfu_get_layout(dfu->layout));
244 return -1;
245 }
246}
247
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100248
249static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf,
Stephen Warren411c5e52015-09-04 22:03:47 -0600250 long *len)
251{
252 int ret;
Stephen Warren411c5e52015-09-04 22:03:47 -0600253
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100254 if (offset == 0 || offset >= dfu_file_buf_offset + dfu_file_buf_len ||
255 offset + *len < dfu_file_buf_offset) {
256 u64 file_len = CONFIG_SYS_DFU_MAX_FILE_SIZE;
257
258 ret = mmc_file_op(DFU_OP_READ, dfu, offset, dfu_file_buf,
259 &file_len);
Stephen Warren411c5e52015-09-04 22:03:47 -0600260 if (ret < 0)
261 return ret;
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100262 dfu_file_buf_len = file_len;
263 dfu_file_buf_offset = offset;
Stephen Warren411c5e52015-09-04 22:03:47 -0600264 }
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100265 if (offset + *len > dfu_file_buf_offset + dfu_file_buf_len)
Stephen Warren411c5e52015-09-04 22:03:47 -0600266 return -EINVAL;
267
268 /* Add to the current buffer. */
Marek Szyprowskic2a1e3d2019-12-02 12:11:16 +0100269 memcpy(buf, dfu_file_buf + offset - dfu_file_buf_offset, *len);
Stephen Warren411c5e52015-09-04 22:03:47 -0600270
271 return 0;
272}
273
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000274int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
275 long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200276{
277 int ret = -1;
278
279 switch (dfu->layout) {
280 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000281 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200282 break;
283 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000284 case DFU_FS_EXT4:
Marek Szyprowski774f8b02019-12-02 12:11:15 +0100285 ret = mmc_file_buf_read(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200286 break;
287 default:
288 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
289 dfu_get_layout(dfu->layout));
290 }
291
292 return ret;
293}
294
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100295void dfu_free_entity_mmc(struct dfu_entity *dfu)
296{
297 if (dfu_file_buf) {
298 free(dfu_file_buf);
299 dfu_file_buf = NULL;
300 }
301}
302
Mateusz Zalega711b9312014-04-28 21:13:24 +0200303/*
304 * @param s Parameter string containing space-separated arguments:
305 * 1st:
306 * raw (raw read/write)
307 * fat (files)
308 * ext4 (^)
309 * part (partition image)
310 * 2nd and 3rd:
311 * lba_start and lba_size, for raw write
312 * mmc_dev and mmc_part, for filesystems and part
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200313 * 4th (optional):
314 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalega711b9312014-04-28 21:13:24 +0200315 */
Stephen Warrendd648272014-06-11 16:03:33 -0600316int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200317{
Mateusz Zalega711b9312014-04-28 21:13:24 +0200318 const char *entity_type;
319 size_t second_arg;
320 size_t third_arg;
321
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000322 struct mmc *mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200323
Mateusz Zalega711b9312014-04-28 21:13:24 +0200324 const char *argv[3];
325 const char **parg = argv;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000326
Stephen Warrendd648272014-06-11 16:03:33 -0600327 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
328
Mateusz Zalega711b9312014-04-28 21:13:24 +0200329 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
330 *parg = strsep(&s, " ");
331 if (*parg == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900332 pr_err("Invalid number of arguments.\n");
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000333 return -ENODEV;
334 }
Mateusz Zalega711b9312014-04-28 21:13:24 +0200335 }
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000336
Mateusz Zalega711b9312014-04-28 21:13:24 +0200337 entity_type = argv[0];
Mateusz Zalegab7d42592014-04-28 21:13:25 +0200338 /*
339 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
340 * with default 10.
341 */
342 second_arg = simple_strtoul(argv[1], NULL, 0);
343 third_arg = simple_strtoul(argv[2], NULL, 0);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000344
Stephen Warrendd648272014-06-11 16:03:33 -0600345 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200346 if (mmc == NULL) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900347 pr_err("Couldn't find MMC device no. %d.\n",
Stephen Warrendd648272014-06-11 16:03:33 -0600348 dfu->data.mmc.dev_num);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000349 return -ENODEV;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200350 }
351
Mateusz Zalega711b9312014-04-28 21:13:24 +0200352 if (mmc_init(mmc)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900353 pr_err("Couldn't init MMC device.\n");
Mateusz Zalega711b9312014-04-28 21:13:24 +0200354 return -ENODEV;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000355 }
356
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200357 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200358 if (!strcmp(entity_type, "raw")) {
359 dfu->layout = DFU_RAW_ADDR;
360 dfu->data.mmc.lba_start = second_arg;
361 dfu->data.mmc.lba_size = third_arg;
362 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200363
364 /*
365 * Check for an extra entry at dfu_alt_info env variable
366 * specifying the mmc HW defined partition number
367 */
368 if (s)
369 if (!strcmp(strsep(&s, " "), "mmcpart"))
370 dfu->data.mmc.hw_partition =
371 simple_strtoul(s, NULL, 0);
372
Mateusz Zalega711b9312014-04-28 21:13:24 +0200373 } else if (!strcmp(entity_type, "part")) {
Simon Glass05289792020-05-10 11:39:57 -0600374 struct disk_partition partinfo;
Simon Glass797d1b92016-06-12 23:30:33 -0600375 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200376 int mmcdev = second_arg;
377 int mmcpart = third_arg;
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200378 int offset = 0;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200379
Simon Glass3e8bd462016-02-29 15:25:48 -0700380 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900381 pr_err("Couldn't find part #%d on mmc device #%d\n",
Mateusz Zalega711b9312014-04-28 21:13:24 +0200382 mmcpart, mmcdev);
383 return -ENODEV;
384 }
385
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200386 /*
387 * Check for an extra entry at dfu_alt_info env variable
388 * specifying the mmc HW defined partition number
389 */
390 if (s)
391 if (!strcmp(strsep(&s, " "), "offset"))
392 offset = simple_strtoul(s, NULL, 0);
393
Mateusz Zalega711b9312014-04-28 21:13:24 +0200394 dfu->layout = DFU_RAW_ADDR;
Marek Szyprowskid2d8eab2019-10-02 12:34:37 +0200395 dfu->data.mmc.lba_start = partinfo.start + offset;
396 dfu->data.mmc.lba_size = partinfo.size-offset;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200397 dfu->data.mmc.lba_blk_size = partinfo.blksz;
398 } else if (!strcmp(entity_type, "fat")) {
399 dfu->layout = DFU_FS_FAT;
400 } else if (!strcmp(entity_type, "ext4")) {
401 dfu->layout = DFU_FS_EXT4;
402 } else {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900403 pr_err("Memory layout (%s) not supported!\n", entity_type);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200404 return -ENODEV;
405 }
406
407 /* if it's NOT a raw write */
408 if (strcmp(entity_type, "raw")) {
409 dfu->data.mmc.dev = second_arg;
410 dfu->data.mmc.part = third_arg;
411 }
412
413 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren0e285b52014-06-11 12:47:27 -0600414 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200415 dfu->read_medium = dfu_read_medium_mmc;
416 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000417 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000418 dfu->inited = 0;
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100419 dfu->free_entity = dfu_free_entity_mmc;
420
421 /* Check if file buffer is ready */
422 if (!dfu_file_buf) {
423 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
424 CONFIG_SYS_DFU_MAX_FILE_SIZE);
425 if (!dfu_file_buf) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900426 pr_err("Could not memalign 0x%x bytes",
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100427 CONFIG_SYS_DFU_MAX_FILE_SIZE);
428 return -ENOMEM;
429 }
430 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200431
432 return 0;
433}