blob: 12c54e90ef71ff4348a60a92e815925baafc0201 [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
Tom Riniabb9a042024-05-18 20:20:43 -06009#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>
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +010019#include <command.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060020#include <linux/printk.h>
Lukasz Majewski34106b22012-08-06 14:41:08 +020021
Przemyslaw Marczak9d6d51a2015-03-04 14:01:23 +010022static unsigned char *dfu_file_buf;
Patrick Delaunayf4e934f2017-07-19 16:39:23 +020023static u64 dfu_file_buf_len;
Marek Szyprowski253d3b62019-12-02 12:11:16 +010024static u64 dfu_file_buf_offset;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000025
Afzal Mohammedb9a4a6b2013-09-18 01:14:50 +053026static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000027 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +020028{
Przemyslaw Marczak95a29472014-12-15 10:34:09 +010029 struct mmc *mmc;
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010030 u32 blk_start, blk_count, n = 0;
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020031 int ret, part_num_bkp = 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +020032
Przemyslaw Marczak95a29472014-12-15 10:34:09 +010033 mmc = find_mmc_device(dfu->data.mmc.dev_num);
34 if (!mmc) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090035 pr_err("Device MMC %d - not found!", dfu->data.mmc.dev_num);
Przemyslaw Marczak95a29472014-12-15 10:34:09 +010036 return -ENODEV;
37 }
38
Pantelis Antonioua6e788d2013-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);
Lukasz Majewski34106b22012-08-06 14:41:08 +020044
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000045 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 Majewski4da65ea2014-05-09 16:58:15 +020054 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glassaa7620b2016-06-12 23:30:33 -060055 part_num_bkp = mmc_get_blk_desc(mmc)->hwpart;
Simon Glassdbfa32c2022-08-11 19:34:59 -060056 ret = blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass11f2bb62016-05-01 13:52:29 -060057 dfu->data.mmc.dev_num,
58 dfu->data.mmc.hw_partition);
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020059 if (ret)
60 return ret;
61 }
62
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010063 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warren4afe50f2014-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 Majewskicf6d7db2014-02-21 08:23:00 +010066 switch (op) {
67 case DFU_OP_READ:
Simon Glassaa7620b2016-06-12 23:30:33 -060068 n = blk_dread(mmc_get_blk_desc(mmc), blk_start, blk_count, buf);
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010069 break;
70 case DFU_OP_WRITE:
Simon Glassaa7620b2016-06-12 23:30:33 -060071 n = blk_dwrite(mmc_get_blk_desc(mmc), blk_start, blk_count,
72 buf);
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010073 break;
74 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +090075 pr_err("Operation not supported\n");
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010076 }
77
78 if (n != blk_count) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090079 pr_err("MMC operation failed");
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020080 if (dfu->data.mmc.hw_partition >= 0)
Simon Glassdbfa32c2022-08-11 19:34:59 -060081 blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass11f2bb62016-05-01 13:52:29 -060082 dfu->data.mmc.dev_num,
83 part_num_bkp);
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010084 return -EIO;
85 }
Lukasz Majewski34106b22012-08-06 14:41:08 +020086
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020087 if (dfu->data.mmc.hw_partition >= 0) {
Simon Glassdbfa32c2022-08-11 19:34:59 -060088 ret = blk_select_hwpart_devnum(UCLASS_MMC,
Simon Glass11f2bb62016-05-01 13:52:29 -060089 dfu->data.mmc.dev_num,
90 part_num_bkp);
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020091 if (ret)
92 return ret;
93 }
94
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010095 return 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +020096}
97
Afzal Mohammedb9a4a6b2013-09-18 01:14:50 +053098static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Marek Szyprowski46901132019-12-02 12:11:15 +010099 u64 offset, void *buf, u64 *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200100{
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100101 char dev_part_str[8];
Lukasz Majewski34106b22012-08-06 14:41:08 +0200102 int ret;
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100103 int fstype;
104 loff_t size = 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200105
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000106 switch (dfu->layout) {
107 case DFU_FS_FAT:
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100108 fstype = FS_TYPE_FAT;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000109 break;
110 case DFU_FS_EXT4:
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100111 fstype = FS_TYPE_EXT;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000112 break;
Jaehoon Chungd7cbe122020-12-22 11:32:21 +0100113 case DFU_SKIP:
114 return 0;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000115 default:
116 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
117 dfu_get_layout(dfu->layout));
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000118 return -1;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000119 }
Lukasz Majewski34106b22012-08-06 14:41:08 +0200120
Simon Goldschmidt59fdf2e2019-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);
123
124 ret = fs_set_blk_dev("mmc", dev_part_str, fstype);
125 if (ret) {
126 puts("dfu: fs_set_blk_dev error!\n");
127 return ret;
128 }
129
Stephen Warren020e6f32014-06-11 12:47:27 -0600130 switch (op) {
131 case DFU_OP_READ:
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100132 ret = fs_read(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100133 if (ret) {
134 puts("dfu: fs_read error!\n");
135 return ret;
136 }
137 *len = size;
Stephen Warren020e6f32014-06-11 12:47:27 -0600138 break;
139 case DFU_OP_WRITE:
Marek Szyprowski46901132019-12-02 12:11:15 +0100140 ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size);
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100141 if (ret) {
142 puts("dfu: fs_write error!\n");
143 return ret;
144 }
Stephen Warren020e6f32014-06-11 12:47:27 -0600145 break;
146 case DFU_OP_SIZE:
Simon Goldschmidt59fdf2e2019-01-25 19:58:01 +0100147 ret = fs_size(dfu->name, &size);
148 if (ret) {
149 puts("dfu: fs_size error!\n");
150 return ret;
151 }
152 *len = size;
Stephen Warren020e6f32014-06-11 12:47:27 -0600153 break;
154 default:
155 return -1;
156 }
157
Marek Szyprowski46901132019-12-02 12:11:15 +0100158 return ret;
159}
160
161static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len)
162{
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100163 int ret = 0;
164
165 if (offset == 0) {
Marek Szyprowski46901132019-12-02 12:11:15 +0100166 dfu_file_buf_len = 0;
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100167 dfu_file_buf_offset = 0;
Marek Szyprowski46901132019-12-02 12:11:15 +0100168 }
169
170 /* Add to the current buffer. */
Marek Szyprowski253d3b62019-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 Szyprowski46901132019-12-02 12:11:15 +0100173 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
174 dfu_file_buf_len += *len;
175
Marek Szyprowski253d3b62019-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 Szyprowski46901132019-12-02 12:11:15 +0100184}
185
186static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
187{
Marek Szyprowski253d3b62019-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 Szyprowski46901132019-12-02 12:11:15 +0100190
191 /* Now that we're done */
192 dfu_file_buf_len = 0;
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100193 dfu_file_buf_offset = 0;
194
Lukasz Majewski34106b22012-08-06 14:41:08 +0200195 return ret;
196}
197
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000198int dfu_write_medium_mmc(struct dfu_entity *dfu,
199 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200200{
201 int ret = -1;
202
203 switch (dfu->layout) {
204 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000205 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200206 break;
207 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000208 case DFU_FS_EXT4:
Marek Szyprowski46901132019-12-02 12:11:15 +0100209 ret = mmc_file_buf_write(dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200210 break;
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100211 case DFU_SCRIPT:
212 ret = run_command_list(buf, *len, 0);
213 break;
Jaehoon Chungd7cbe122020-12-22 11:32:21 +0100214 case DFU_SKIP:
215 ret = 0;
216 break;
Lukasz Majewski34106b22012-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 Antonioua6e788d2013-03-14 05:32:48 +0000225int dfu_flush_medium_mmc(struct dfu_entity *dfu)
226{
227 int ret = 0;
228
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100229 switch (dfu->layout) {
230 case DFU_FS_FAT:
231 case DFU_FS_EXT4:
Marek Szyprowski46901132019-12-02 12:11:15 +0100232 ret = mmc_file_buf_write_finish(dfu);
Marek Szyprowskib1fa1ad2020-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 Antonioua6e788d2013-03-14 05:32:48 +0000244 }
245
246 return ret;
247}
248
Patrick Delaunayf4e934f2017-07-19 16:39:23 +0200249int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
Stephen Warren020e6f32014-06-11 12:47:27 -0600250{
251 int ret;
Stephen Warren020e6f32014-06-11 12:47:27 -0600252
253 switch (dfu->layout) {
254 case DFU_RAW_ADDR:
Patrick Delaunay5ea9b892017-07-19 16:39:22 +0200255 *size = dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
256 return 0;
Stephen Warren020e6f32014-06-11 12:47:27 -0600257 case DFU_FS_FAT:
258 case DFU_FS_EXT4:
Marek Szyprowski46901132019-12-02 12:11:15 +0100259 ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
Stephen Warren020e6f32014-06-11 12:47:27 -0600260 if (ret < 0)
261 return ret;
Patrick Delaunay5ea9b892017-07-19 16:39:22 +0200262 return 0;
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100263 case DFU_SCRIPT:
Jaehoon Chungd7cbe122020-12-22 11:32:21 +0100264 case DFU_SKIP:
265 return 0;
Stephen Warren020e6f32014-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 }
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600271}
272
Marek Szyprowski46901132019-12-02 12:11:15 +0100273
274static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf,
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600275 long *len)
276{
277 int ret;
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600278
Marek Szyprowski253d3b62019-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 Warren7ae42bd2015-09-04 22:03:47 -0600285 if (ret < 0)
286 return ret;
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100287 dfu_file_buf_len = file_len;
288 dfu_file_buf_offset = offset;
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600289 }
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100290 if (offset + *len > dfu_file_buf_offset + dfu_file_buf_len)
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600291 return -EINVAL;
292
293 /* Add to the current buffer. */
Marek Szyprowski253d3b62019-12-02 12:11:16 +0100294 memcpy(buf, dfu_file_buf + offset - dfu_file_buf_offset, *len);
Stephen Warren7ae42bd2015-09-04 22:03:47 -0600295
296 return 0;
Stephen Warren020e6f32014-06-11 12:47:27 -0600297}
298
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000299int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
300 long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200301{
302 int ret = -1;
303
304 switch (dfu->layout) {
305 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000306 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200307 break;
308 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000309 case DFU_FS_EXT4:
Marek Szyprowski46901132019-12-02 12:11:15 +0100310 ret = mmc_file_buf_read(dfu, offset, buf, len);
Lukasz Majewski34106b22012-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 Marczak9d6d51a2015-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 Zalegadcb6ea62014-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 Majewski4da65ea2014-05-09 16:58:15 +0200338 * 4th (optional):
339 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200340 */
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900341int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char **argv, int argc)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200342{
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200343 const char *entity_type;
Marek Szyprowski968f5e22020-12-22 11:32:20 +0100344 ssize_t second_arg;
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200345 size_t third_arg;
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200346 struct mmc *mmc;
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900347 char *s;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000348
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900349 if (argc < 3) {
350 pr_err("The number of parameters are not enough.\n");
351 return -EINVAL;
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200352 }
353
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900354 dfu->data.mmc.dev_num = dectoul(devstr, &s);
355 if (*s)
356 return -EINVAL;
357
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200358 entity_type = argv[0];
Mateusz Zalega0ab80bf2014-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 Hiramatsufa4282a2022-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;
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200369
Stephen Warren4afe50f2014-06-11 16:03:33 -0600370 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200371 if (mmc == NULL) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900372 pr_err("Couldn't find MMC device no. %d.\n",
Stephen Warren4afe50f2014-06-11 16:03:33 -0600373 dfu->data.mmc.dev_num);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200374 return -ENODEV;
375 }
376
377 if (mmc_init(mmc)) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900378 pr_err("Couldn't init MMC device.\n");
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200379 return -ENODEV;
380 }
381
Lukasz Majewski4da65ea2014-05-09 16:58:15 +0200382 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalegadcb6ea62014-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 Majewski4da65ea2014-05-09 16:58:15 +0200388
389 /*
Marek Vasut3ed7faf2023-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 Majewski4da65ea2014-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 Hiramatsufa4282a2022-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 Hiramatsuf815c332022-01-31 11:52:29 +0900407 }
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900408 dfu->data.mmc.hw_partition =
409 simple_strtoul(argv[4], NULL, 0);
410 }
Lukasz Majewski4da65ea2014-05-09 16:58:15 +0200411
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200412 } else if (!strcmp(entity_type, "part")) {
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600413 struct disk_partition partinfo;
Simon Glassaa7620b2016-06-12 23:30:33 -0600414 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200415 int mmcdev = second_arg;
416 int mmcpart = third_arg;
Marek Szyprowskif9378242019-10-02 12:34:37 +0200417 int offset = 0;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000418
Simon Glassb89a8442016-02-29 15:25:48 -0700419 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900420 pr_err("Couldn't find part #%d on mmc device #%d\n",
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200421 mmcpart, mmcdev);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000422 return -ENODEV;
423 }
424
Marek Szyprowskif9378242019-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 Hiramatsufa4282a2022-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 Hiramatsuf815c332022-01-31 11:52:29 +0900433 }
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900434 dfu->data.mmc.hw_partition =
435 simple_strtoul(argv[4], NULL, 0);
436 }
Marek Szyprowskif9378242019-10-02 12:34:37 +0200437
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200438 dfu->layout = DFU_RAW_ADDR;
Marek Szyprowskif9378242019-10-02 12:34:37 +0200439 dfu->data.mmc.lba_start = partinfo.start + offset;
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900440 dfu->data.mmc.lba_size = partinfo.size - offset;
Mateusz Zalegadcb6ea62014-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 Chungd7cbe122020-12-22 11:32:21 +0100446 } else if (!strcmp(entity_type, "skip")) {
447 dfu->layout = DFU_SKIP;
Marek Szyprowskib1fa1ad2020-12-22 11:32:22 +0100448 } else if (!strcmp(entity_type, "script")) {
449 dfu->layout = DFU_SCRIPT;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200450 } else {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900451 pr_err("Memory layout (%s) not supported!\n", entity_type);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000452 return -ENODEV;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200453 }
454
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200455 /* if it's NOT a raw write */
456 if (strcmp(entity_type, "raw")) {
Marek Szyprowski968f5e22020-12-22 11:32:20 +0100457 dfu->data.mmc.dev = (second_arg != -1) ? second_arg :
458 dfu->data.mmc.dev_num;
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200459 dfu->data.mmc.part = third_arg;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000460 }
461
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200462 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren020e6f32014-06-11 12:47:27 -0600463 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200464 dfu->read_medium = dfu_read_medium_mmc;
465 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000466 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000467 dfu->inited = 0;
Przemyslaw Marczak9d6d51a2015-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 Yamada81e10422017-09-16 14:10:41 +0900475 pr_err("Could not memalign 0x%x bytes",
Przemyslaw Marczak9d6d51a2015-03-04 14:01:23 +0100476 CONFIG_SYS_DFU_MAX_FILE_SIZE);
477 return -ENOMEM;
478 }
479 }
Lukasz Majewski34106b22012-08-06 14:41:08 +0200480
481 return 0;
482}