blob: 63cc876612c9aa44df4539e5e6c1a3b1d0af0b14 [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>
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010015#include <mmc.h>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020016
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000017static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
18 dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
19static long dfu_file_buf_len;
20
Lukasz Majewskic8151b42014-05-09 16:58:15 +020021static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part)
22{
23 int ret;
24
25 if (part == mmc->part_num)
26 return 0;
27
28 ret = mmc_switch_part(dfu->dev_num, part);
29 if (ret) {
30 error("Cannot switch to partition %d\n", part);
31 return ret;
32 }
33 mmc->part_num = part;
34
35 return 0;
36}
37
Afzal Mohammed5a127c82013-09-18 01:14:50 +053038static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000039 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020040{
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010041 struct mmc *mmc = find_mmc_device(dfu->dev_num);
42 u32 blk_start, blk_count, n = 0;
Lukasz Majewskic8151b42014-05-09 16:58:15 +020043 int ret, part_num_bkp = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020044
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000045 /*
46 * We must ensure that we work in lba_blk_size chunks, so ALIGN
47 * this value.
48 */
49 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
50
51 blk_start = dfu->data.mmc.lba_start +
52 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
53 blk_count = *len / dfu->data.mmc.lba_blk_size;
54 if (blk_start + blk_count >
55 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
56 puts("Request would exceed designated area!\n");
57 return -EINVAL;
58 }
59
Lukasz Majewskic8151b42014-05-09 16:58:15 +020060 if (dfu->data.mmc.hw_partition >= 0) {
61 part_num_bkp = mmc->part_num;
62 ret = mmc_access_part(dfu, mmc, dfu->data.mmc.hw_partition);
63 if (ret)
64 return ret;
65 }
66
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010067 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
68 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE", dfu->dev_num,
69 blk_start, blk_count, buf);
70 switch (op) {
71 case DFU_OP_READ:
72 n = mmc->block_dev.block_read(dfu->dev_num, blk_start,
73 blk_count, buf);
74 break;
75 case DFU_OP_WRITE:
76 n = mmc->block_dev.block_write(dfu->dev_num, blk_start,
77 blk_count, buf);
78 break;
79 default:
80 error("Operation not supported\n");
81 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020082
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010083 if (n != blk_count) {
84 error("MMC operation failed");
Lukasz Majewskic8151b42014-05-09 16:58:15 +020085 if (dfu->data.mmc.hw_partition >= 0)
86 mmc_access_part(dfu, mmc, part_num_bkp);
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010087 return -EIO;
88 }
89
Lukasz Majewskic8151b42014-05-09 16:58:15 +020090 if (dfu->data.mmc.hw_partition >= 0) {
91 ret = mmc_access_part(dfu, mmc, part_num_bkp);
92 if (ret)
93 return ret;
94 }
95
Łukasz Majewski7d0b6052014-02-21 08:23:00 +010096 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020097}
98
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000099static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200100{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000101 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
102 dfu_file_buf_len = 0;
103 return -EINVAL;
104 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200105
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000106 /* Add to the current buffer. */
107 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
108 dfu_file_buf_len += *len;
109
110 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200111}
112
Afzal Mohammed5a127c82013-09-18 01:14:50 +0530113static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200114 void *buf, long *len)
115{
116 char cmd_buf[DFU_CMD_BUF_SIZE];
117 char *str_env;
118 int ret;
119
Łukasz Majewski43e66272012-08-23 23:33:55 +0000120 switch (dfu->layout) {
121 case DFU_FS_FAT:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000122 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
Łukasz Majewski43e66272012-08-23 23:33:55 +0000123 op == DFU_OP_READ ? "load" : "write",
124 dfu->data.mmc.dev, dfu->data.mmc.part,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000125 (unsigned int) buf, dfu->name);
Łukasz Majewski43e66272012-08-23 23:33:55 +0000126 break;
127 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000128 sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
Łukasz Majewski43e66272012-08-23 23:33:55 +0000129 op == DFU_OP_READ ? "load" : "write",
130 dfu->data.mmc.dev, dfu->data.mmc.part,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000131 (unsigned int) buf, dfu->name);
Łukasz Majewski43e66272012-08-23 23:33:55 +0000132 break;
133 default:
134 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
135 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000136 return -1;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000137 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200138
Lukasz Majewski17eb1d82014-02-20 10:29:18 +0100139 if (op == DFU_OP_WRITE)
140 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
141
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200142 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
143
144 ret = run_command(cmd_buf, 0);
145 if (ret) {
146 puts("dfu: Read error!\n");
147 return ret;
148 }
149
Łukasz Majewski81c1d7b2012-08-23 23:33:56 +0000150 if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200151 str_env = getenv("filesize");
152 if (str_env == NULL) {
153 puts("dfu: Wrong file size!\n");
154 return -1;
155 }
156 *len = simple_strtoul(str_env, NULL, 16);
157 }
158
159 return ret;
160}
161
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000162int dfu_write_medium_mmc(struct dfu_entity *dfu,
163 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200164{
165 int ret = -1;
166
167 switch (dfu->layout) {
168 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000169 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200170 break;
171 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000172 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000173 ret = mmc_file_buffer(dfu, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200174 break;
175 default:
176 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
177 dfu_get_layout(dfu->layout));
178 }
179
180 return ret;
181}
182
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000183int dfu_flush_medium_mmc(struct dfu_entity *dfu)
184{
185 int ret = 0;
186
187 if (dfu->layout != DFU_RAW_ADDR) {
188 /* Do stuff here. */
189 ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf,
190 &dfu_file_buf_len);
191
192 /* Now that we're done */
193 dfu_file_buf_len = 0;
194 }
195
196 return ret;
197}
198
199int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
200 long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200201{
202 int ret = -1;
203
204 switch (dfu->layout) {
205 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000206 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200207 break;
208 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000209 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000210 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200211 break;
212 default:
213 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
214 dfu_get_layout(dfu->layout));
215 }
216
217 return ret;
218}
219
Mateusz Zalega711b9312014-04-28 21:13:24 +0200220/*
221 * @param s Parameter string containing space-separated arguments:
222 * 1st:
223 * raw (raw read/write)
224 * fat (files)
225 * ext4 (^)
226 * part (partition image)
227 * 2nd and 3rd:
228 * lba_start and lba_size, for raw write
229 * mmc_dev and mmc_part, for filesystems and part
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200230 * 4th (optional):
231 * mmcpart <num> (access to HW eMMC partitions)
Mateusz Zalega711b9312014-04-28 21:13:24 +0200232 */
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200233int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
234{
Mateusz Zalega711b9312014-04-28 21:13:24 +0200235 const char *entity_type;
236 size_t second_arg;
237 size_t third_arg;
238
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000239 struct mmc *mmc;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200240
Mateusz Zalega711b9312014-04-28 21:13:24 +0200241 const char *argv[3];
242 const char **parg = argv;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000243
Mateusz Zalega711b9312014-04-28 21:13:24 +0200244 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
245 *parg = strsep(&s, " ");
246 if (*parg == NULL) {
247 error("Invalid number of arguments.\n");
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000248 return -ENODEV;
249 }
Mateusz Zalega711b9312014-04-28 21:13:24 +0200250 }
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000251
Mateusz Zalega711b9312014-04-28 21:13:24 +0200252 entity_type = argv[0];
Mateusz Zalegab7d42592014-04-28 21:13:25 +0200253 /*
254 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
255 * with default 10.
256 */
257 second_arg = simple_strtoul(argv[1], NULL, 0);
258 third_arg = simple_strtoul(argv[2], NULL, 0);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000259
Mateusz Zalega711b9312014-04-28 21:13:24 +0200260 mmc = find_mmc_device(dfu->dev_num);
261 if (mmc == NULL) {
262 error("Couldn't find MMC device no. %d.\n", dfu->dev_num);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000263 return -ENODEV;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200264 }
265
Mateusz Zalega711b9312014-04-28 21:13:24 +0200266 if (mmc_init(mmc)) {
267 error("Couldn't init MMC device.\n");
268 return -ENODEV;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000269 }
270
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200271 dfu->data.mmc.hw_partition = -EINVAL;
Mateusz Zalega711b9312014-04-28 21:13:24 +0200272 if (!strcmp(entity_type, "raw")) {
273 dfu->layout = DFU_RAW_ADDR;
274 dfu->data.mmc.lba_start = second_arg;
275 dfu->data.mmc.lba_size = third_arg;
276 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
Lukasz Majewskic8151b42014-05-09 16:58:15 +0200277
278 /*
279 * Check for an extra entry at dfu_alt_info env variable
280 * specifying the mmc HW defined partition number
281 */
282 if (s)
283 if (!strcmp(strsep(&s, " "), "mmcpart"))
284 dfu->data.mmc.hw_partition =
285 simple_strtoul(s, NULL, 0);
286
Mateusz Zalega711b9312014-04-28 21:13:24 +0200287 } else if (!strcmp(entity_type, "part")) {
288 disk_partition_t partinfo;
289 block_dev_desc_t *blk_dev = &mmc->block_dev;
290 int mmcdev = second_arg;
291 int mmcpart = third_arg;
292
293 if (get_partition_info(blk_dev, mmcpart, &partinfo) != 0) {
294 error("Couldn't find part #%d on mmc device #%d\n",
295 mmcpart, mmcdev);
296 return -ENODEV;
297 }
298
299 dfu->layout = DFU_RAW_ADDR;
300 dfu->data.mmc.lba_start = partinfo.start;
301 dfu->data.mmc.lba_size = partinfo.size;
302 dfu->data.mmc.lba_blk_size = partinfo.blksz;
303 } else if (!strcmp(entity_type, "fat")) {
304 dfu->layout = DFU_FS_FAT;
305 } else if (!strcmp(entity_type, "ext4")) {
306 dfu->layout = DFU_FS_EXT4;
307 } else {
308 error("Memory layout (%s) not supported!\n", entity_type);
309 return -ENODEV;
310 }
311
312 /* if it's NOT a raw write */
313 if (strcmp(entity_type, "raw")) {
314 dfu->data.mmc.dev = second_arg;
315 dfu->data.mmc.part = third_arg;
316 }
317
318 dfu->dev_type = DFU_DEV_MMC;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200319 dfu->read_medium = dfu_read_medium_mmc;
320 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000321 dfu->flush_medium = dfu_flush_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000322 dfu->inited = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200323
324 return 0;
325}