blob: 78724e467b24c7f816fef09767022bd8876b42a8 [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
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +010019static unsigned char *dfu_file_buf;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000020static long dfu_file_buf_len;
Stephen Warren411c5e52015-09-04 22:03:47 -060021static long dfu_file_buf_filled;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000022
Afzal Mohammed5a127c82013-09-18 01:14:50 +053023static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000024 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020025{
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010026 struct mmc *mmc;
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010027 u32 blk_start, blk_count, n = 0;
Lukasz Majewskic8151b42014-05-09 16:58:15 +020028 int ret, part_num_bkp = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020029
Przemyslaw Marczak7da6fa22014-12-15 10:34:09 +010030 mmc = find_mmc_device(dfu->data.mmc.dev_num);
31 if (!mmc) {
32 error("Device MMC %d - not found!", dfu->data.mmc.dev_num);
33 return -ENODEV;
34 }
35
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000036 /*
37 * We must ensure that we work in lba_blk_size chunks, so ALIGN
38 * this value.
39 */
40 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
41
42 blk_start = dfu->data.mmc.lba_start +
43 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
44 blk_count = *len / dfu->data.mmc.lba_blk_size;
45 if (blk_start + blk_count >
46 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
47 puts("Request would exceed designated area!\n");
48 return -EINVAL;
49 }
50
Lukasz Majewskic8151b42014-05-09 16:58:15 +020051 if (dfu->data.mmc.hw_partition >= 0) {
Stephen Warren873cc1d2015-12-07 11:38:49 -070052 part_num_bkp = mmc->block_dev.hwpart;
Simon Glass69f45cd2016-05-01 13:52:29 -060053 ret = blk_select_hwpart_devnum(IF_TYPE_MMC,
54 dfu->data.mmc.dev_num,
55 dfu->data.mmc.hw_partition);
Lukasz Majewskic8151b42014-05-09 16:58:15 +020056 if (ret)
57 return ret;
58 }
59
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010060 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
Stephen Warrendd648272014-06-11 16:03:33 -060061 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
62 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010063 switch (op) {
64 case DFU_OP_READ:
Stephen Warren7c4213f2015-12-07 11:38:48 -070065 n = mmc->block_dev.block_read(&mmc->block_dev, blk_start,
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010066 blk_count, buf);
67 break;
68 case DFU_OP_WRITE:
Stephen Warren7c4213f2015-12-07 11:38:48 -070069 n = mmc->block_dev.block_write(&mmc->block_dev, blk_start,
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010070 blk_count, buf);
71 break;
72 default:
73 error("Operation not supported\n");
74 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020075
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010076 if (n != blk_count) {
77 error("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
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000096static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020097{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000098 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
99 dfu_file_buf_len = 0;
100 return -EINVAL;
101 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200102
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000103 /* Add to the current buffer. */
104 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
105 dfu_file_buf_len += *len;
106
107 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200108}
109
Afzal Mohammed5a127c82013-09-18 01:14:50 +0530110static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200111 void *buf, long *len)
112{
Stephen Warren0e285b52014-06-11 12:47:27 -0600113 const char *fsname, *opname;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200114 char cmd_buf[DFU_CMD_BUF_SIZE];
115 char *str_env;
116 int ret;
117
Łukasz Majewski43e66272012-08-23 23:33:55 +0000118 switch (dfu->layout) {
119 case DFU_FS_FAT:
Stephen Warren0e285b52014-06-11 12:47:27 -0600120 fsname = "fat";
Łukasz Majewski43e66272012-08-23 23:33:55 +0000121 break;
122 case DFU_FS_EXT4:
Stephen Warren0e285b52014-06-11 12:47:27 -0600123 fsname = "ext4";
Łukasz Majewski43e66272012-08-23 23:33:55 +0000124 break;
125 default:
126 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
127 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000128 return -1;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000129 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200130
Stephen Warren0e285b52014-06-11 12:47:27 -0600131 switch (op) {
132 case DFU_OP_READ:
133 opname = "load";
134 break;
135 case DFU_OP_WRITE:
136 opname = "write";
137 break;
138 case DFU_OP_SIZE:
139 opname = "size";
140 break;
141 default:
142 return -1;
143 }
144
145 sprintf(cmd_buf, "%s%s mmc %d:%d", fsname, opname,
146 dfu->data.mmc.dev, dfu->data.mmc.part);
147
148 if (op != DFU_OP_SIZE)
Stephen Warrene621c7a2015-07-22 14:54:04 -0600149 sprintf(cmd_buf + strlen(cmd_buf), " %p", buf);
Stephen Warren0e285b52014-06-11 12:47:27 -0600150
151 sprintf(cmd_buf + strlen(cmd_buf), " %s", dfu->name);
152
Lukasz Majewski17eb1d82014-02-20 10:29:18 +0100153 if (op == DFU_OP_WRITE)
154 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
155
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200156 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
157
158 ret = run_command(cmd_buf, 0);
159 if (ret) {
160 puts("dfu: Read error!\n");
161 return ret;
162 }
163
Stephen Warren0e285b52014-06-11 12:47:27 -0600164 if (op != DFU_OP_WRITE) {
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200165 str_env = getenv("filesize");
166 if (str_env == NULL) {
167 puts("dfu: Wrong file size!\n");
168 return -1;
169 }
170 *len = simple_strtoul(str_env, NULL, 16);
171 }
172
173 return ret;
174}
175
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000176int dfu_write_medium_mmc(struct dfu_entity *dfu,
177 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200178{
179 int ret = -1;
180
181 switch (dfu->layout) {
182 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000183 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200184 break;
185 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000186 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000187 ret = mmc_file_buffer(dfu, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200188 break;
189 default:
190 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
191 dfu_get_layout(dfu->layout));
192 }
193
194 return ret;
195}
196
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000197int dfu_flush_medium_mmc(struct dfu_entity *dfu)
198{
199 int ret = 0;
200
201 if (dfu->layout != DFU_RAW_ADDR) {
202 /* Do stuff here. */
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100203 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000204 &dfu_file_buf_len);
205
206 /* Now that we're done */
207 dfu_file_buf_len = 0;
208 }
209
210 return ret;
211}
212
Stephen Warren0e285b52014-06-11 12:47:27 -0600213long dfu_get_medium_size_mmc(struct dfu_entity *dfu)
214{
215 int ret;
216 long len;
217
218 switch (dfu->layout) {
219 case DFU_RAW_ADDR:
220 return dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
221 case DFU_FS_FAT:
222 case DFU_FS_EXT4:
Stephen Warren411c5e52015-09-04 22:03:47 -0600223 dfu_file_buf_filled = -1;
Stephen Warren0e285b52014-06-11 12:47:27 -0600224 ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, &len);
225 if (ret < 0)
226 return ret;
Stephen Warren411c5e52015-09-04 22:03:47 -0600227 if (len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
228 return -1;
Stephen Warren0e285b52014-06-11 12:47:27 -0600229 return len;
230 default:
231 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
232 dfu_get_layout(dfu->layout));
233 return -1;
234 }
235}
236
Stephen Warren411c5e52015-09-04 22:03:47 -0600237static int mmc_file_unbuffer(struct dfu_entity *dfu, u64 offset, void *buf,
238 long *len)
239{
240 int ret;
241 long file_len;
242
243 if (dfu_file_buf_filled == -1) {
244 ret = mmc_file_op(DFU_OP_READ, dfu, dfu_file_buf, &file_len);
245 if (ret < 0)
246 return ret;
247 dfu_file_buf_filled = file_len;
248 }
249 if (offset + *len > dfu_file_buf_filled)
250 return -EINVAL;
251
252 /* Add to the current buffer. */
253 memcpy(buf, dfu_file_buf + offset, *len);
254
255 return 0;
256}
257
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000258int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
259 long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200260{
261 int ret = -1;
262
263 switch (dfu->layout) {
264 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000265 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200266 break;
267 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000268 case DFU_FS_EXT4:
Stephen Warren411c5e52015-09-04 22:03:47 -0600269 ret = mmc_file_unbuffer(dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200270 break;
271 default:
272 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
273 dfu_get_layout(dfu->layout));
274 }
275
276 return ret;
277}
278
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100279void dfu_free_entity_mmc(struct dfu_entity *dfu)
280{
281 if (dfu_file_buf) {
282 free(dfu_file_buf);
283 dfu_file_buf = NULL;
284 }
285}
286
Mateusz Zalega711b9312014-04-28 21:13:24 +0200287/*
288 * @param s Parameter string containing space-separated arguments:
289 * 1st:
290 * raw (raw read/write)
291 * fat (files)
292 * ext4 (^)
293 * part (partition image)
294 * 2nd and 3rd:
295 * lba_start and lba_size, for raw write
296 * mmc_dev and mmc_part, for filesystems and part
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200297 * 4th (optional):
298 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalega711b9312014-04-28 21:13:24 +0200299 */
Stephen Warrendd648272014-06-11 16:03:33 -0600300int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200301{
Mateusz Zalega711b9312014-04-28 21:13:24 +0200302 const char *entity_type;
303 size_t second_arg;
304 size_t third_arg;
305
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000306 struct mmc *mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200307
Mateusz Zalega711b9312014-04-28 21:13:24 +0200308 const char *argv[3];
309 const char **parg = argv;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000310
Stephen Warrendd648272014-06-11 16:03:33 -0600311 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
312
Mateusz Zalega711b9312014-04-28 21:13:24 +0200313 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
314 *parg = strsep(&s, " ");
315 if (*parg == NULL) {
316 error("Invalid number of arguments.\n");
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000317 return -ENODEV;
318 }
Mateusz Zalega711b9312014-04-28 21:13:24 +0200319 }
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000320
Mateusz Zalega711b9312014-04-28 21:13:24 +0200321 entity_type = argv[0];
Mateusz Zalegab7d42592014-04-28 21:13:25 +0200322 /*
323 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
324 * with default 10.
325 */
326 second_arg = simple_strtoul(argv[1], NULL, 0);
327 third_arg = simple_strtoul(argv[2], NULL, 0);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000328
Stephen Warrendd648272014-06-11 16:03:33 -0600329 mmc = find_mmc_device(dfu->data.mmc.dev_num);
Mateusz Zalega711b9312014-04-28 21:13:24 +0200330 if (mmc == NULL) {
Stephen Warrendd648272014-06-11 16:03:33 -0600331 error("Couldn't find MMC device no. %d.\n",
332 dfu->data.mmc.dev_num);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000333 return -ENODEV;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200334 }
335
Mateusz Zalega711b9312014-04-28 21:13:24 +0200336 if (mmc_init(mmc)) {
337 error("Couldn't init MMC device.\n");
338 return -ENODEV;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000339 }
340
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200341 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200342 if (!strcmp(entity_type, "raw")) {
343 dfu->layout = DFU_RAW_ADDR;
344 dfu->data.mmc.lba_start = second_arg;
345 dfu->data.mmc.lba_size = third_arg;
346 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200347
348 /*
349 * Check for an extra entry at dfu_alt_info env variable
350 * specifying the mmc HW defined partition number
351 */
352 if (s)
353 if (!strcmp(strsep(&s, " "), "mmcpart"))
354 dfu->data.mmc.hw_partition =
355 simple_strtoul(s, NULL, 0);
356
Mateusz Zalega711b9312014-04-28 21:13:24 +0200357 } else if (!strcmp(entity_type, "part")) {
358 disk_partition_t partinfo;
Simon Glass4101f682016-02-29 15:25:34 -0700359 struct blk_desc *blk_dev = &mmc->block_dev;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200360 int mmcdev = second_arg;
361 int mmcpart = third_arg;
362
Simon Glass3e8bd462016-02-29 15:25:48 -0700363 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
Mateusz Zalega711b9312014-04-28 21:13:24 +0200364 error("Couldn't find part #%d on mmc device #%d\n",
365 mmcpart, mmcdev);
366 return -ENODEV;
367 }
368
369 dfu->layout = DFU_RAW_ADDR;
370 dfu->data.mmc.lba_start = partinfo.start;
371 dfu->data.mmc.lba_size = partinfo.size;
372 dfu->data.mmc.lba_blk_size = partinfo.blksz;
373 } else if (!strcmp(entity_type, "fat")) {
374 dfu->layout = DFU_FS_FAT;
375 } else if (!strcmp(entity_type, "ext4")) {
376 dfu->layout = DFU_FS_EXT4;
377 } else {
378 error("Memory layout (%s) not supported!\n", entity_type);
379 return -ENODEV;
380 }
381
382 /* if it's NOT a raw write */
383 if (strcmp(entity_type, "raw")) {
384 dfu->data.mmc.dev = second_arg;
385 dfu->data.mmc.part = third_arg;
386 }
387
388 dfu->dev_type = DFU_DEV_MMC;
Stephen Warren0e285b52014-06-11 12:47:27 -0600389 dfu->get_medium_size = dfu_get_medium_size_mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200390 dfu->read_medium = dfu_read_medium_mmc;
391 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000392 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000393 dfu->inited = 0;
Przemyslaw Marczak41ac2332015-03-04 14:01:23 +0100394 dfu->free_entity = dfu_free_entity_mmc;
395
396 /* Check if file buffer is ready */
397 if (!dfu_file_buf) {
398 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
399 CONFIG_SYS_DFU_MAX_FILE_SIZE);
400 if (!dfu_file_buf) {
401 error("Could not memalign 0x%x bytes",
402 CONFIG_SYS_DFU_MAX_FILE_SIZE);
403 return -ENOMEM;
404 }
405 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200406
407 return 0;
408}