blob: 62d72fe4c69a3843974a353f28885fd8661c640e [file] [log] [blame]
Lukasz Majewskicb383cd2012-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 Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskicb383cd2012-08-06 14:41:08 +02008 */
9
10#include <common.h>
11#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>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020018
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000019static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
20 dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
21static long dfu_file_buf_len;
22
Lukasz Majewskic8151b42014-05-09 16:58:15 +020023static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part)
24{
25 int ret;
26
27 if (part == mmc->part_num)
28 return 0;
29
Stephen Warrendd648272014-06-11 16:03:33 -060030 ret = mmc_switch_part(dfu->data.mmc.dev_num, part);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020031 if (ret) {
32 error("Cannot switch to partition %d\n", part);
33 return ret;
34 }
35 mmc->part_num = part;
36
37 return 0;
38}
39
Afzal Mohammed5a127c82013-09-18 01:14:50 +053040static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000041 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020042{
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010043 struct mmc *mmc;
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010044 u32 blk_start, blk_count, n = 0;
Lukasz Majewskic8151b42014-05-09 16:58:15 +020045 int ret, part_num_bkp = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020046
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010047 mmc = find_mmc_device(dfu->data.mmc.dev_num);
48 if (!mmc) {
49 error("Device MMC %d - not found!", dfu->data.mmc.dev_num);
50 return -ENODEV;
51 }
52
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000053 /*
54 * We must ensure that we work in lba_blk_size chunks, so ALIGN
55 * this value.
56 */
57 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
58
59 blk_start = dfu->data.mmc.lba_start +
60 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
61 blk_count = *len / dfu->data.mmc.lba_blk_size;
62 if (blk_start + blk_count >
63 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
64 puts("Request would exceed designated area!\n");
65 return -EINVAL;
66 }
67
Lukasz Majewskic8151b42014-05-09 16:58:15 +020068 if (dfu->data.mmc.hw_partition >= 0) {
69 part_num_bkp = mmc->part_num;
70 ret = mmc_access_part(dfu, mmc, dfu->data.mmc.hw_partition);
71 if (ret)
72 return ret;
73 }
74
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010075 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warrendd648272014-06-11 16:03:33 -060076 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
77 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010078 switch (op) {
79 case DFU_OP_READ:
Stephen Warrendd648272014-06-11 16:03:33 -060080 n = mmc->block_dev.block_read(dfu->data.mmc.dev_num, blk_start,
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010081 blk_count, buf);
82 break;
83 case DFU_OP_WRITE:
Stephen Warrendd648272014-06-11 16:03:33 -060084 n = mmc->block_dev.block_write(dfu->data.mmc.dev_num, blk_start,
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010085 blk_count, buf);
86 break;
87 default:
88 error("Operation not supported\n");
89 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020090
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010091 if (n != blk_count) {
92 error("MMC operation failed");
Lukasz Majewskic8151b42014-05-09 16:58:15 +020093 if (dfu->data.mmc.hw_partition >= 0)
94 mmc_access_part(dfu, mmc, part_num_bkp);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010095 return -EIO;
96 }
97
Lukasz Majewskic8151b42014-05-09 16:58:15 +020098 if (dfu->data.mmc.hw_partition >= 0) {
99 ret = mmc_access_part(dfu, mmc, part_num_bkp);
100 if (ret)
101 return ret;
102 }
103
Łukasz Majewski7d0b6052014-02-21 08:23:00 +0100104 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200105}
106
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000107static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200108{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000109 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
110 dfu_file_buf_len = 0;
111 return -EINVAL;
112 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200113
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000114 /* Add to the current buffer. */
115 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
116 dfu_file_buf_len += *len;
117
118 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200119}
120
Afzal Mohammed5a127c82013-09-18 01:14:50 +0530121static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200122 void *buf, long *len)
123{
Stephen Warren0e285b52014-06-11 12:47:27 -0600124 const char *fsname, *opname;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200125 char cmd_buf[DFU_CMD_BUF_SIZE];
126 char *str_env;
127 int ret;
128
Łukasz Majewski43e66272012-08-23 23:33:55 +0000129 switch (dfu->layout) {
130 case DFU_FS_FAT:
Stephen Warren0e285b52014-06-11 12:47:27 -0600131 fsname = "fat";
Łukasz Majewski43e66272012-08-23 23:33:55 +0000132 break;
133 case DFU_FS_EXT4:
Stephen Warren0e285b52014-06-11 12:47:27 -0600134 fsname = "ext4";
Łukasz Majewski43e66272012-08-23 23:33:55 +0000135 break;
136 default:
137 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
138 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000139 return -1;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000140 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200141
Stephen Warren0e285b52014-06-11 12:47:27 -0600142 switch (op) {
143 case DFU_OP_READ:
144 opname = "load";
145 break;
146 case DFU_OP_WRITE:
147 opname = "write";
148 break;
149 case DFU_OP_SIZE:
150 opname = "size";
151 break;
152 default:
153 return -1;
154 }
155
156 sprintf(cmd_buf, "%s%s mmc %d:%d", fsname, opname,
157 dfu->data.mmc.dev, dfu->data.mmc.part);
158
159 if (op != DFU_OP_SIZE)
160 sprintf(cmd_buf + strlen(cmd_buf), " 0x%x", (unsigned int)buf);
161
162 sprintf(cmd_buf + strlen(cmd_buf), " %s", dfu->name);
163
Lukasz Majewski17eb1d82014-02-20 10:29:18 +0100164 if (op == DFU_OP_WRITE)
165 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
166
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200167 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
168
169 ret = run_command(cmd_buf, 0);
170 if (ret) {
171 puts("dfu: Read error!\n");
172 return ret;
173 }
174
Stephen Warren0e285b52014-06-11 12:47:27 -0600175 if (op != DFU_OP_WRITE) {
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200176 str_env = getenv("filesize");
177 if (str_env == NULL) {
178 puts("dfu: Wrong file size!\n");
179 return -1;
180 }
181 *len = simple_strtoul(str_env, NULL, 16);
182 }
183
184 return ret;
185}
186
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000187int dfu_write_medium_mmc(struct dfu_entity *dfu,
188 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200189{
190 int ret = -1;
191
192 switch (dfu->layout) {
193 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000194 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200195 break;
196 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000197 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000198 ret = mmc_file_buffer(dfu, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200199 break;
200 default:
201 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
202 dfu_get_layout(dfu->layout));
203 }
204
205 return ret;
206}
207
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000208int dfu_flush_medium_mmc(struct dfu_entity *dfu)
209{
210 int ret = 0;
211
212 if (dfu->layout != DFU_RAW_ADDR) {
213 /* Do stuff here. */
214 ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf,
215 &dfu_file_buf_len);
216
217 /* Now that we're done */
218 dfu_file_buf_len = 0;
219 }
220
221 return ret;
222}
223
Stephen Warren0e285b52014-06-11 12:47:27 -0600224long dfu_get_medium_size_mmc(struct dfu_entity *dfu)
225{
226 int ret;
227 long len;
228
229 switch (dfu->layout) {
230 case DFU_RAW_ADDR:
231 return dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
232 case DFU_FS_FAT:
233 case DFU_FS_EXT4:
234 ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, &len);
235 if (ret < 0)
236 return ret;
237 return len;
238 default:
239 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
240 dfu_get_layout(dfu->layout));
241 return -1;
242 }
243}
244
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000245int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
246 long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200247{
248 int ret = -1;
249
250 switch (dfu->layout) {
251 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000252 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200253 break;
254 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000255 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000256 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200257 break;
258 default:
259 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
260 dfu_get_layout(dfu->layout));
261 }
262
263 return ret;
264}
265
Mateusz Zalega711b9312014-04-28 21:13:24 +0200266/*
267 * @param s Parameter string containing space-separated arguments:
268 * 1st:
269 * raw (raw read/write)
270 * fat (files)
271 * ext4 (^)
272 * part (partition image)
273 * 2nd and 3rd:
274 * lba_start and lba_size, for raw write
275 * mmc_dev and mmc_part, for filesystems and part
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200276 * 4th (optional):
277 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalega711b9312014-04-28 21:13:24 +0200278 */
Stephen Warrendd648272014-06-11 16:03:33 -0600279int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200280{
Mateusz Zalega711b9312014-04-28 21:13:24 +0200281 const char *entity_type;
282 size_t second_arg;
283 size_t third_arg;
284
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000285 struct mmc *mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200286
Mateusz Zalega711b9312014-04-28 21:13:24 +0200287 const char *argv[3];
288 const char **parg = argv;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000289
Stephen Warrendd648272014-06-11 16:03:33 -0600290 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
291
Mateusz Zalega711b9312014-04-28 21:13:24 +0200292 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
293 *parg = strsep(&s, " ");
294 if (*parg == NULL) {
295 error("Invalid number of arguments.\n");
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000296 return -ENODEV;
297 }
Mateusz Zalega711b9312014-04-28 21:13:24 +0200298 }
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000299
Mateusz Zalega711b9312014-04-28 21:13:24 +0200300 entity_type = argv[0];
Mateusz Zalegab7d42592014-04-28 21:13:25 +0200301 /*
302 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
303 * with default 10.
304 */
305 second_arg = simple_strtoul(argv[1], NULL, 0);
306 third_arg = simple_strtoul(argv[2], NULL, 0);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000307
Stephen Warrendd648272014-06-11 16:03:33 -0600308 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200309 if (mmc == NULL) {
Stephen Warrendd648272014-06-11 16:03:33 -0600310 error("Couldn't find MMC device no. %d.\n",
311 dfu->data.mmc.dev_num);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000312 return -ENODEV;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200313 }
314
Mateusz Zalega711b9312014-04-28 21:13:24 +0200315 if (mmc_init(mmc)) {
316 error("Couldn't init MMC device.\n");
317 return -ENODEV;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000318 }
319
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200320 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200321 if (!strcmp(entity_type, "raw")) {
322 dfu->layout = DFU_RAW_ADDR;
323 dfu->data.mmc.lba_start = second_arg;
324 dfu->data.mmc.lba_size = third_arg;
325 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200326
327 /*
328 * Check for an extra entry at dfu_alt_info env variable
329 * specifying the mmc HW defined partition number
330 */
331 if (s)
332 if (!strcmp(strsep(&s, " "), "mmcpart"))
333 dfu->data.mmc.hw_partition =
334 simple_strtoul(s, NULL, 0);
335
Mateusz Zalega711b9312014-04-28 21:13:24 +0200336 } else if (!strcmp(entity_type, "part")) {
337 disk_partition_t partinfo;
338 block_dev_desc_t *blk_dev = &mmc->block_dev;
339 int mmcdev = second_arg;
340 int mmcpart = third_arg;
341
342 if (get_partition_info(blk_dev, mmcpart, &partinfo) != 0) {
343 error("Couldn't find part #%d on mmc device #%d\n",
344 mmcpart, mmcdev);
345 return -ENODEV;
346 }
347
348 dfu->layout = DFU_RAW_ADDR;
349 dfu->data.mmc.lba_start = partinfo.start;
350 dfu->data.mmc.lba_size = partinfo.size;
351 dfu->data.mmc.lba_blk_size = partinfo.blksz;
352 } else if (!strcmp(entity_type, "fat")) {
353 dfu->layout = DFU_FS_FAT;
354 } else if (!strcmp(entity_type, "ext4")) {
355 dfu->layout = DFU_FS_EXT4;
356 } else {
357 error("Memory layout (%s) not supported!\n", entity_type);
358 return -ENODEV;
359 }
360
361 /* if it's NOT a raw write */
362 if (strcmp(entity_type, "raw")) {
363 dfu->data.mmc.dev = second_arg;
364 dfu->data.mmc.part = third_arg;
365 }
366
367 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren0e285b52014-06-11 12:47:27 -0600368 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200369 dfu->read_medium = dfu_read_medium_mmc;
370 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000371 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000372 dfu->inited = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200373
374 return 0;
375}