Merge branch 'fpga' of git://www.denx.de/git/u-boot-microblaze
[platform/kernel/u-boot.git] / drivers / dfu / dfu_mmc.c
1 /*
2  * dfu.c -- DFU back-end routines
3  *
4  * Copyright (C) 2012 Samsung Electronics
5  * author: Lukasz Majewski <l.majewski@samsung.com>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9
10 #include <common.h>
11 #include <malloc.h>
12 #include <errno.h>
13 #include <div64.h>
14 #include <dfu.h>
15 #include <mmc.h>
16
17 static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
18                                 dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
19 static long dfu_file_buf_len;
20
21 static 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
38 static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
39                         u64 offset, void *buf, long *len)
40 {
41         struct mmc *mmc = find_mmc_device(dfu->dev_num);
42         u32 blk_start, blk_count, n = 0;
43         int ret, part_num_bkp = 0;
44
45         /*
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
60         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
67         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         }
82
83         if (n != blk_count) {
84                 error("MMC operation failed");
85                 if (dfu->data.mmc.hw_partition >= 0)
86                         mmc_access_part(dfu, mmc, part_num_bkp);
87                 return -EIO;
88         }
89
90         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
96         return 0;
97 }
98
99 static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
100 {
101         if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
102                 dfu_file_buf_len = 0;
103                 return -EINVAL;
104         }
105
106         /* 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;
111 }
112
113 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
114                         void *buf, long *len)
115 {
116         char cmd_buf[DFU_CMD_BUF_SIZE];
117         char *str_env;
118         int ret;
119
120         switch (dfu->layout) {
121         case DFU_FS_FAT:
122                 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
123                         op == DFU_OP_READ ? "load" : "write",
124                         dfu->data.mmc.dev, dfu->data.mmc.part,
125                         (unsigned int) buf, dfu->name);
126                 break;
127         case DFU_FS_EXT4:
128                 sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
129                         op == DFU_OP_READ ? "load" : "write",
130                         dfu->data.mmc.dev, dfu->data.mmc.part,
131                         (unsigned int) buf, dfu->name);
132                 break;
133         default:
134                 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
135                        dfu_get_layout(dfu->layout));
136                 return -1;
137         }
138
139         if (op == DFU_OP_WRITE)
140                 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
141
142         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
150         if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
151                 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
162 int dfu_write_medium_mmc(struct dfu_entity *dfu,
163                 u64 offset, void *buf, long *len)
164 {
165         int ret = -1;
166
167         switch (dfu->layout) {
168         case DFU_RAW_ADDR:
169                 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
170                 break;
171         case DFU_FS_FAT:
172         case DFU_FS_EXT4:
173                 ret = mmc_file_buffer(dfu, buf, len);
174                 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
183 int 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
199 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
200                 long *len)
201 {
202         int ret = -1;
203
204         switch (dfu->layout) {
205         case DFU_RAW_ADDR:
206                 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
207                 break;
208         case DFU_FS_FAT:
209         case DFU_FS_EXT4:
210                 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
211                 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
220 /*
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
230  *      4th (optional):
231  *              mmcpart <num> (access to HW eMMC partitions)
232  */
233 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
234 {
235         const char *entity_type;
236         size_t second_arg;
237         size_t third_arg;
238
239         struct mmc *mmc;
240
241         const char *argv[3];
242         const char **parg = argv;
243
244         for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
245                 *parg = strsep(&s, " ");
246                 if (*parg == NULL) {
247                         error("Invalid number of arguments.\n");
248                         return -ENODEV;
249                 }
250         }
251
252         entity_type = argv[0];
253         /*
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);
259
260         mmc = find_mmc_device(dfu->dev_num);
261         if (mmc == NULL) {
262                 error("Couldn't find MMC device no. %d.\n", dfu->dev_num);
263                 return -ENODEV;
264         }
265
266         if (mmc_init(mmc)) {
267                 error("Couldn't init MMC device.\n");
268                 return -ENODEV;
269         }
270
271         dfu->data.mmc.hw_partition = -EINVAL;
272         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;
277
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
287         } 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;
319         dfu->read_medium = dfu_read_medium_mmc;
320         dfu->write_medium = dfu_write_medium_mmc;
321         dfu->flush_medium = dfu_flush_medium_mmc;
322         dfu->inited = 0;
323
324         return 0;
325 }