blob: 691d01c7ebdfcc301fed0cf9e0b0a46d54bac941 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski34106b22012-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 Majewski34106b22012-08-06 14:41:08 +02007 */
8
9#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Lukasz Majewski34106b22012-08-06 14:41:08 +020011#include <malloc.h>
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +000012#include <errno.h>
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000013#include <div64.h>
Lukasz Majewski34106b22012-08-06 14:41:08 +020014#include <dfu.h>
Stephen Warren020e6f32014-06-11 12:47:27 -060015#include <ext4fs.h>
16#include <fat.h>
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010017#include <mmc.h>
Simon Glass655306c2020-05-10 11:39:58 -060018#include <part.h>
Lukasz Majewski34106b22012-08-06 14:41:08 +020019
Przemyslaw Marczak9d6d51a2015-03-04 14:01:23 +010020static unsigned char *dfu_file_buf;
Patrick Delaunayf4e934f2017-07-19 16:39:23 +020021static u64 dfu_file_buf_len;
Marek Szyprowski253d3b62019-12-02 12:11:16 +010022static u64 dfu_file_buf_offset;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000023
Afzal Mohammedb9a4a6b2013-09-18 01:14:50 +053024static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000025 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +020026{
Przemyslaw Marczak95a29472014-12-15 10:34:09 +010027 struct mmc *mmc;
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010028 u32 blk_start, blk_count, n = 0;
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020029 int ret, part_num_bkp = 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +020030
Przemyslaw Marczak95a29472014-12-15 10:34:09 +010031 mmc = find_mmc_device(dfu->data.mmc.dev_num);
32 if (!mmc) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090033 pr_err("Device MMC %d - not found!", dfu->data.mmc.dev_num);
Przemyslaw Marczak95a29472014-12-15 10:34:09 +010034 return -ENODEV;
35 }
36
Pantelis Antonioua6e788d2013-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);
Lukasz Majewski34106b22012-08-06 14:41:08 +020042
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000043 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 Majewski4da65ea2014-05-09 16:58:15 +020052 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glassaa7620b2016-06-12 23:30:33 -060053 part_num_bkp = mmc_get_blk_desc(mmc)->hwpart;
Simon Glass11f2bb62016-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 Majewski4da65ea2014-05-09 16:58:15 +020057 if (ret)
58 return ret;
59 }
60
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010061 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warren4afe50f2014-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 Majewskicf6d7db2014-02-21 08:23:00 +010064 switch (op) {
65 case DFU_OP_READ:
Simon Glassaa7620b2016-06-12 23:30:33 -060066 n = blk_dread(mmc_get_blk_desc(mmc), blk_start, blk_count, buf);
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010067 break;
68 case DFU_OP_WRITE:
Simon Glassaa7620b2016-06-12 23:30:33 -060069 n = blk_dwrite(mmc_get_blk_desc(mmc), blk_start, blk_count,
70 buf);
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010071 break;
72 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +090073 pr_err("Operation not supported\n");
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010074 }
75
76 if (n != blk_count) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090077 pr_err("MMC operation failed");
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020078 if (dfu->data.mmc.hw_partition >= 0)
Simon Glass11f2bb62016-05-01 13:52:29 -060079 blk_select_hwpart_devnum(IF_TYPE_MMC,
80 dfu->data.mmc.dev_num,
81 part_num_bkp);
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010082 return -EIO;
83 }
Lukasz Majewski34106b22012-08-06 14:41:08 +020084
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020085 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glass11f2bb62016-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 Majewski4da65ea2014-05-09 16:58:15 +020089 if (ret)
90 return ret;
91 }
92
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010093 return 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +020094}
95
Afzal Mohammedb9a4a6b2013-09-18 01:14:50 +053096static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Marek Szyprowski46901132019-12-02 12:11:15 +010097 u64 offset, void *buf, u64 *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +020098{
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +010099 char dev_part_str[8];
Lukasz Majewski34106b22012-08-06 14:41:08 +0200100 int ret;
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100101 int fstype;
102 loff_t size = 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200103
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000104 switch (dfu->layout) {
105 case DFU_FS_FAT:
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100106 fstype = FS_TYPE_FAT;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000107 break;
108 case DFU_FS_EXT4:
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100109 fstype = FS_TYPE_EXT;
Łukasz Majewski71c88eb2012-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 Antonioua6e788d2013-03-14 05:32:48 +0000114 return -1;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000115 }
Lukasz Majewski34106b22012-08-06 14:41:08 +0200116
Simon Goldschmidt59fdf2e2019-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);
119
120 ret = fs_set_blk_dev("mmc", dev_part_str, fstype);
121 if (ret) {
122 puts("dfu: fs_set_blk_dev error!\n");
123 return ret;
124 }
125
Stephen Warren020e6f32014-06-11 12:47:27 -0600126 switch (op) {
127 case DFU_OP_READ:
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100128 ret = fs_read(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100129 if (ret) {
130 puts("dfu: fs_read error!\n");
131 return ret;
132 }
133 *len = size;
Stephen Warren020e6f32014-06-11 12:47:27 -0600134 break;
135 case DFU_OP_WRITE:
Marek Szyprowski46901132019-12-02 12:11:15 +0100136 ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100137 if (ret) {
138 puts("dfu: fs_write error!\n");
139 return ret;
140 }
Stephen Warren020e6f32014-06-11 12:47:27 -0600141 break;
142 case DFU_OP_SIZE:
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100143 ret = fs_size(dfu->name, &size);
144 if (ret) {
145 puts("dfu: fs_size error!\n");
146 return ret;
147 }
148 *len = size;
Stephen Warren020e6f32014-06-11 12:47:27 -0600149 break;
150 default:
151 return -1;
152 }
153
Marek Szyprowski46901132019-12-02 12:11:15 +0100154 return ret;
155}
156
157static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len)
158{
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100159 int ret = 0;
160
161 if (offset == 0) {
Marek Szyprowski46901132019-12-02 12:11:15 +0100162 dfu_file_buf_len = 0;
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100163 dfu_file_buf_offset = 0;
Marek Szyprowski46901132019-12-02 12:11:15 +0100164 }
165
166 /* Add to the current buffer. */
Marek Szyprowski253d3b62019-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 Szyprowski46901132019-12-02 12:11:15 +0100169 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
170 dfu_file_buf_len += *len;
171
Marek Szyprowski253d3b62019-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 Szyprowski46901132019-12-02 12:11:15 +0100180}
181
182static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
183{
Marek Szyprowski253d3b62019-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 Szyprowski46901132019-12-02 12:11:15 +0100186
187 /* Now that we're done */
188 dfu_file_buf_len = 0;
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100189 dfu_file_buf_offset = 0;
190
Lukasz Majewski34106b22012-08-06 14:41:08 +0200191 return ret;
192}
193
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000194int dfu_write_medium_mmc(struct dfu_entity *dfu,
195 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200196{
197 int ret = -1;
198
199 switch (dfu->layout) {
200 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000201 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200202 break;
203 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000204 case DFU_FS_EXT4:
Marek Szyprowski46901132019-12-02 12:11:15 +0100205 ret = mmc_file_buf_write(dfu, offset, buf, len);
Lukasz Majewski34106b22012-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 Antonioua6e788d2013-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 Szyprowski46901132019-12-02 12:11:15 +0100221 ret = mmc_file_buf_write_finish(dfu);
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000222 }
223
224 return ret;
225}
226
Patrick Delaunayf4e934f2017-07-19 16:39:23 +0200227int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
Stephen Warren020e6f32014-06-11 12:47:27 -0600228{
229 int ret;
Stephen Warren020e6f32014-06-11 12:47:27 -0600230
231 switch (dfu->layout) {
232 case DFU_RAW_ADDR:
Patrick Delaunay5ea9b892017-07-19 16:39:22 +0200233 *size = dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
234 return 0;
Stephen Warren020e6f32014-06-11 12:47:27 -0600235 case DFU_FS_FAT:
236 case DFU_FS_EXT4:
Marek Szyprowski46901132019-12-02 12:11:15 +0100237 ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
Stephen Warren020e6f32014-06-11 12:47:27 -0600238 if (ret < 0)
239 return ret;
Patrick Delaunay5ea9b892017-07-19 16:39:22 +0200240 return 0;
Stephen Warren020e6f32014-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 }
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600246}
247
Marek Szyprowski46901132019-12-02 12:11:15 +0100248
249static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf,
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600250 long *len)
251{
252 int ret;
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600253
Marek Szyprowski253d3b62019-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 Warren7ae42bd2015-09-04 22:03:47 -0600260 if (ret < 0)
261 return ret;
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100262 dfu_file_buf_len = file_len;
263 dfu_file_buf_offset = offset;
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600264 }
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100265 if (offset + *len > dfu_file_buf_offset + dfu_file_buf_len)
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600266 return -EINVAL;
267
268 /* Add to the current buffer. */
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100269 memcpy(buf, dfu_file_buf + offset - dfu_file_buf_offset, *len);
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600270
271 return 0;
Stephen Warren020e6f32014-06-11 12:47:27 -0600272}
273
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000274int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
275 long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200276{
277 int ret = -1;
278
279 switch (dfu->layout) {
280 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000281 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200282 break;
283 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000284 case DFU_FS_EXT4:
Marek Szyprowski46901132019-12-02 12:11:15 +0100285 ret = mmc_file_buf_read(dfu, offset, buf, len);
Lukasz Majewski34106b22012-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 Marczak9d6d51a2015-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 Zalegadcb6ea62014-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 Majewski4da65ea2014-05-09 16:58:15 +0200313 * 4th (optional):
314 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200315 */
Stephen Warren4afe50f2014-06-11 16:03:33 -0600316int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200317{
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200318 const char *entity_type;
319 size_t second_arg;
320 size_t third_arg;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000321
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200322 struct mmc *mmc;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000323
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200324 const char *argv[3];
325 const char **parg = argv;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000326
Stephen Warren4afe50f2014-06-11 16:03:33 -0600327 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
328
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200329 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
330 *parg = strsep(&s, " ");
331 if (*parg == NULL) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900332 pr_err("Invalid number of arguments.\n");
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000333 return -ENODEV;
334 }
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200335 }
336
337 entity_type = argv[0];
Mateusz Zalega0ab80bf2014-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);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200344
Stephen Warren4afe50f2014-06-11 16:03:33 -0600345 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200346 if (mmc == NULL) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900347 pr_err("Couldn't find MMC device no. %d.\n",
Stephen Warren4afe50f2014-06-11 16:03:33 -0600348 dfu->data.mmc.dev_num);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200349 return -ENODEV;
350 }
351
352 if (mmc_init(mmc)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900353 pr_err("Couldn't init MMC device.\n");
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200354 return -ENODEV;
355 }
356
Lukasz Majewski4da65ea2014-05-09 16:58:15 +0200357 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalegadcb6ea62014-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 Majewski4da65ea2014-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 Zalegadcb6ea62014-04-28 21:13:24 +0200373 } else if (!strcmp(entity_type, "part")) {
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600374 struct disk_partition partinfo;
Simon Glassaa7620b2016-06-12 23:30:33 -0600375 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200376 int mmcdev = second_arg;
377 int mmcpart = third_arg;
Marek Szyprowskif9378242019-10-02 12:34:37 +0200378 int offset = 0;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000379
Simon Glassb89a8442016-02-29 15:25:48 -0700380 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900381 pr_err("Couldn't find part #%d on mmc device #%d\n",
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200382 mmcpart, mmcdev);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000383 return -ENODEV;
384 }
385
Marek Szyprowskif9378242019-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 Zalegadcb6ea62014-04-28 21:13:24 +0200394 dfu->layout = DFU_RAW_ADDR;
Marek Szyprowskif9378242019-10-02 12:34:37 +0200395 dfu->data.mmc.lba_start = partinfo.start + offset;
396 dfu->data.mmc.lba_size = partinfo.size-offset;
Mateusz Zalegadcb6ea62014-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;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200402 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900403 pr_err("Memory layout (%s) not supported!\n", entity_type);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000404 return -ENODEV;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200405 }
406
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200407 /* 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;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000411 }
412
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200413 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren020e6f32014-06-11 12:47:27 -0600414 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200415 dfu->read_medium = dfu_read_medium_mmc;
416 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000417 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000418 dfu->inited = 0;
Przemyslaw Marczak9d6d51a2015-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 Yamada81e10422017-09-16 14:10:41 +0900426 pr_err("Could not memalign 0x%x bytes",
Przemyslaw Marczak9d6d51a2015-03-04 14:01:23 +0100427 CONFIG_SYS_DFU_MAX_FILE_SIZE);
428 return -ENOMEM;
429 }
430 }
Lukasz Majewski34106b22012-08-06 14:41:08 +0200431
432 return 0;
433}