blob: fd865e11212e13ec9dce85de6a210cf880646919 [file] [log] [blame]
Lukasz Majewski34106b22012-08-06 14:41:08 +02001/*
2 * dfu.c -- DFU back-end routines
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski34106b22012-08-06 14:41:08 +02008 */
9
10#include <common.h>
11#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>
Lukasz Majewski34106b22012-08-06 14:41:08 +020018
Przemyslaw Marczak9d6d51a2015-03-04 14:01:23 +010019static unsigned char *dfu_file_buf;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000020static long dfu_file_buf_len;
21
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020022static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part)
23{
24 int ret;
25
26 if (part == mmc->part_num)
27 return 0;
28
Stephen Warren4afe50f2014-06-11 16:03:33 -060029 ret = mmc_switch_part(dfu->data.mmc.dev_num, part);
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020030 if (ret) {
31 error("Cannot switch to partition %d\n", part);
32 return ret;
33 }
34 mmc->part_num = part;
35
36 return 0;
37}
38
Afzal Mohammedb9a4a6b2013-09-18 01:14:50 +053039static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000040 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +020041{
Przemyslaw Marczak95a29472014-12-15 10:34:09 +010042 struct mmc *mmc;
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010043 u32 blk_start, blk_count, n = 0;
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020044 int ret, part_num_bkp = 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +020045
Przemyslaw Marczak95a29472014-12-15 10:34:09 +010046 mmc = find_mmc_device(dfu->data.mmc.dev_num);
47 if (!mmc) {
48 error("Device MMC %d - not found!", dfu->data.mmc.dev_num);
49 return -ENODEV;
50 }
51
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000052 /*
53 * We must ensure that we work in lba_blk_size chunks, so ALIGN
54 * this value.
55 */
56 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
Lukasz Majewski34106b22012-08-06 14:41:08 +020057
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000058 blk_start = dfu->data.mmc.lba_start +
59 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
60 blk_count = *len / dfu->data.mmc.lba_blk_size;
61 if (blk_start + blk_count >
62 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
63 puts("Request would exceed designated area!\n");
64 return -EINVAL;
65 }
66
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020067 if (dfu->data.mmc.hw_partition >= 0) {
68 part_num_bkp = mmc->part_num;
69 ret = mmc_access_part(dfu, mmc, dfu->data.mmc.hw_partition);
70 if (ret)
71 return ret;
72 }
73
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010074 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warren4afe50f2014-06-11 16:03:33 -060075 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
76 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010077 switch (op) {
78 case DFU_OP_READ:
Stephen Warren4afe50f2014-06-11 16:03:33 -060079 n = mmc->block_dev.block_read(dfu->data.mmc.dev_num, blk_start,
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010080 blk_count, buf);
81 break;
82 case DFU_OP_WRITE:
Stephen Warren4afe50f2014-06-11 16:03:33 -060083 n = mmc->block_dev.block_write(dfu->data.mmc.dev_num, blk_start,
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010084 blk_count, buf);
85 break;
86 default:
87 error("Operation not supported\n");
88 }
89
90 if (n != blk_count) {
91 error("MMC operation failed");
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020092 if (dfu->data.mmc.hw_partition >= 0)
93 mmc_access_part(dfu, mmc, part_num_bkp);
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +010094 return -EIO;
95 }
Lukasz Majewski34106b22012-08-06 14:41:08 +020096
Lukasz Majewski4da65ea2014-05-09 16:58:15 +020097 if (dfu->data.mmc.hw_partition >= 0) {
98 ret = mmc_access_part(dfu, mmc, part_num_bkp);
99 if (ret)
100 return ret;
101 }
102
Łukasz Majewskicf6d7db2014-02-21 08:23:00 +0100103 return 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200104}
105
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000106static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200107{
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000108 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
109 dfu_file_buf_len = 0;
110 return -EINVAL;
111 }
Lukasz Majewski34106b22012-08-06 14:41:08 +0200112
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000113 /* Add to the current buffer. */
114 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
115 dfu_file_buf_len += *len;
116
117 return 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200118}
119
Afzal Mohammedb9a4a6b2013-09-18 01:14:50 +0530120static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Lukasz Majewski34106b22012-08-06 14:41:08 +0200121 void *buf, long *len)
122{
Stephen Warren020e6f32014-06-11 12:47:27 -0600123 const char *fsname, *opname;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200124 char cmd_buf[DFU_CMD_BUF_SIZE];
125 char *str_env;
126 int ret;
127
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000128 switch (dfu->layout) {
129 case DFU_FS_FAT:
Stephen Warren020e6f32014-06-11 12:47:27 -0600130 fsname = "fat";
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000131 break;
132 case DFU_FS_EXT4:
Stephen Warren020e6f32014-06-11 12:47:27 -0600133 fsname = "ext4";
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000134 break;
135 default:
136 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
137 dfu_get_layout(dfu->layout));
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000138 return -1;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000139 }
Lukasz Majewski34106b22012-08-06 14:41:08 +0200140
Stephen Warren020e6f32014-06-11 12:47:27 -0600141 switch (op) {
142 case DFU_OP_READ:
143 opname = "load";
144 break;
145 case DFU_OP_WRITE:
146 opname = "write";
147 break;
148 case DFU_OP_SIZE:
149 opname = "size";
150 break;
151 default:
152 return -1;
153 }
154
155 sprintf(cmd_buf, "%s%s mmc %d:%d", fsname, opname,
156 dfu->data.mmc.dev, dfu->data.mmc.part);
157
158 if (op != DFU_OP_SIZE)
159 sprintf(cmd_buf + strlen(cmd_buf), " 0x%x", (unsigned int)buf);
160
161 sprintf(cmd_buf + strlen(cmd_buf), " %s", dfu->name);
162
Lukasz Majewski8f957c02014-02-20 10:29:18 +0100163 if (op == DFU_OP_WRITE)
164 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
165
Lukasz Majewski34106b22012-08-06 14:41:08 +0200166 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
167
168 ret = run_command(cmd_buf, 0);
169 if (ret) {
170 puts("dfu: Read error!\n");
171 return ret;
172 }
173
Stephen Warren020e6f32014-06-11 12:47:27 -0600174 if (op != DFU_OP_WRITE) {
Lukasz Majewski34106b22012-08-06 14:41:08 +0200175 str_env = getenv("filesize");
176 if (str_env == NULL) {
177 puts("dfu: Wrong file size!\n");
178 return -1;
179 }
180 *len = simple_strtoul(str_env, NULL, 16);
181 }
182
183 return ret;
184}
185
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000186int dfu_write_medium_mmc(struct dfu_entity *dfu,
187 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200188{
189 int ret = -1;
190
191 switch (dfu->layout) {
192 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000193 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200194 break;
195 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000196 case DFU_FS_EXT4:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000197 ret = mmc_file_buffer(dfu, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200198 break;
199 default:
200 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
201 dfu_get_layout(dfu->layout));
202 }
203
204 return ret;
205}
206
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000207int dfu_flush_medium_mmc(struct dfu_entity *dfu)
208{
209 int ret = 0;
210
211 if (dfu->layout != DFU_RAW_ADDR) {
212 /* Do stuff here. */
Przemyslaw Marczak9d6d51a2015-03-04 14:01:23 +0100213 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000214 &dfu_file_buf_len);
215
216 /* Now that we're done */
217 dfu_file_buf_len = 0;
218 }
219
220 return ret;
221}
222
Stephen Warren020e6f32014-06-11 12:47:27 -0600223long dfu_get_medium_size_mmc(struct dfu_entity *dfu)
224{
225 int ret;
226 long len;
227
228 switch (dfu->layout) {
229 case DFU_RAW_ADDR:
230 return dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
231 case DFU_FS_FAT:
232 case DFU_FS_EXT4:
233 ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, &len);
234 if (ret < 0)
235 return ret;
236 return len;
237 default:
238 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
239 dfu_get_layout(dfu->layout));
240 return -1;
241 }
242}
243
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000244int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
245 long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200246{
247 int ret = -1;
248
249 switch (dfu->layout) {
250 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000251 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200252 break;
253 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000254 case DFU_FS_EXT4:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000255 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200256 break;
257 default:
258 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
259 dfu_get_layout(dfu->layout));
260 }
261
262 return ret;
263}
264
Przemyslaw Marczak9d6d51a2015-03-04 14:01:23 +0100265void dfu_free_entity_mmc(struct dfu_entity *dfu)
266{
267 if (dfu_file_buf) {
268 free(dfu_file_buf);
269 dfu_file_buf = NULL;
270 }
271}
272
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200273/*
274 * @param s Parameter string containing space-separated arguments:
275 * 1st:
276 * raw (raw read/write)
277 * fat (files)
278 * ext4 (^)
279 * part (partition image)
280 * 2nd and 3rd:
281 * lba_start and lba_size, for raw write
282 * mmc_dev and mmc_part, for filesystems and part
Lukasz Majewski4da65ea2014-05-09 16:58:15 +0200283 * 4th (optional):
284 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200285 */
Stephen Warren4afe50f2014-06-11 16:03:33 -0600286int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200287{
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200288 const char *entity_type;
289 size_t second_arg;
290 size_t third_arg;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000291
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200292 struct mmc *mmc;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000293
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200294 const char *argv[3];
295 const char **parg = argv;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000296
Stephen Warren4afe50f2014-06-11 16:03:33 -0600297 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
298
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200299 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
300 *parg = strsep(&s, " ");
301 if (*parg == NULL) {
302 error("Invalid number of arguments.\n");
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000303 return -ENODEV;
304 }
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200305 }
306
307 entity_type = argv[0];
Mateusz Zalega0ab80bf2014-04-28 21:13:25 +0200308 /*
309 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
310 * with default 10.
311 */
312 second_arg = simple_strtoul(argv[1], NULL, 0);
313 third_arg = simple_strtoul(argv[2], NULL, 0);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200314
Stephen Warren4afe50f2014-06-11 16:03:33 -0600315 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200316 if (mmc == NULL) {
Stephen Warren4afe50f2014-06-11 16:03:33 -0600317 error("Couldn't find MMC device no. %d.\n",
318 dfu->data.mmc.dev_num);
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200319 return -ENODEV;
320 }
321
322 if (mmc_init(mmc)) {
323 error("Couldn't init MMC device.\n");
324 return -ENODEV;
325 }
326
Lukasz Majewski4da65ea2014-05-09 16:58:15 +0200327 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200328 if (!strcmp(entity_type, "raw")) {
329 dfu->layout = DFU_RAW_ADDR;
330 dfu->data.mmc.lba_start = second_arg;
331 dfu->data.mmc.lba_size = third_arg;
332 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
Lukasz Majewski4da65ea2014-05-09 16:58:15 +0200333
334 /*
335 * Check for an extra entry at dfu_alt_info env variable
336 * specifying the mmc HW defined partition number
337 */
338 if (s)
339 if (!strcmp(strsep(&s, " "), "mmcpart"))
340 dfu->data.mmc.hw_partition =
341 simple_strtoul(s, NULL, 0);
342
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200343 } else if (!strcmp(entity_type, "part")) {
344 disk_partition_t partinfo;
345 block_dev_desc_t *blk_dev = &mmc->block_dev;
346 int mmcdev = second_arg;
347 int mmcpart = third_arg;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000348
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200349 if (get_partition_info(blk_dev, mmcpart, &partinfo) != 0) {
350 error("Couldn't find part #%d on mmc device #%d\n",
351 mmcpart, mmcdev);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000352 return -ENODEV;
353 }
354
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200355 dfu->layout = DFU_RAW_ADDR;
356 dfu->data.mmc.lba_start = partinfo.start;
357 dfu->data.mmc.lba_size = partinfo.size;
358 dfu->data.mmc.lba_blk_size = partinfo.blksz;
359 } else if (!strcmp(entity_type, "fat")) {
360 dfu->layout = DFU_FS_FAT;
361 } else if (!strcmp(entity_type, "ext4")) {
362 dfu->layout = DFU_FS_EXT4;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200363 } else {
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200364 error("Memory layout (%s) not supported!\n", entity_type);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000365 return -ENODEV;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200366 }
367
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200368 /* if it's NOT a raw write */
369 if (strcmp(entity_type, "raw")) {
370 dfu->data.mmc.dev = second_arg;
371 dfu->data.mmc.part = third_arg;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000372 }
373
Mateusz Zalegadcb6ea62014-04-28 21:13:24 +0200374 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren020e6f32014-06-11 12:47:27 -0600375 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200376 dfu->read_medium = dfu_read_medium_mmc;
377 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000378 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000379 dfu->inited = 0;
Przemyslaw Marczak9d6d51a2015-03-04 14:01:23 +0100380 dfu->free_entity = dfu_free_entity_mmc;
381
382 /* Check if file buffer is ready */
383 if (!dfu_file_buf) {
384 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
385 CONFIG_SYS_DFU_MAX_FILE_SIZE);
386 if (!dfu_file_buf) {
387 error("Could not memalign 0x%x bytes",
388 CONFIG_SYS_DFU_MAX_FILE_SIZE);
389 return -ENOMEM;
390 }
391 }
Lukasz Majewski34106b22012-08-06 14:41:08 +0200392
393 return 0;
394}