Merge branch 'next'
[u-boot/qq2440-u-boot.git] / drivers / dfu / dfu_mmc.c
blob651cfff5b3be4f742eb18cbeef1c9f3a6eacd1c2
1 /*
2 * dfu.c -- DFU back-end routines
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
7 * SPDX-License-Identifier: GPL-2.0+
8 */
10 #include <common.h>
11 #include <malloc.h>
12 #include <errno.h>
13 #include <div64.h>
14 #include <dfu.h>
15 #include <mmc.h>
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;
21 static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
22 u64 offset, void *buf, long *len)
24 struct mmc *mmc = find_mmc_device(dfu->dev_num);
25 u32 blk_start, blk_count, n = 0;
28 * We must ensure that we work in lba_blk_size chunks, so ALIGN
29 * this value.
31 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
33 blk_start = dfu->data.mmc.lba_start +
34 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
35 blk_count = *len / dfu->data.mmc.lba_blk_size;
36 if (blk_start + blk_count >
37 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
38 puts("Request would exceed designated area!\n");
39 return -EINVAL;
42 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
43 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE", dfu->dev_num,
44 blk_start, blk_count, buf);
45 switch (op) {
46 case DFU_OP_READ:
47 n = mmc->block_dev.block_read(dfu->dev_num, blk_start,
48 blk_count, buf);
49 break;
50 case DFU_OP_WRITE:
51 n = mmc->block_dev.block_write(dfu->dev_num, blk_start,
52 blk_count, buf);
53 break;
54 default:
55 error("Operation not supported\n");
58 if (n != blk_count) {
59 error("MMC operation failed");
60 return -EIO;
63 return 0;
66 static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
68 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
69 dfu_file_buf_len = 0;
70 return -EINVAL;
73 /* Add to the current buffer. */
74 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
75 dfu_file_buf_len += *len;
77 return 0;
80 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
81 void *buf, long *len)
83 char cmd_buf[DFU_CMD_BUF_SIZE];
84 char *str_env;
85 int ret;
87 switch (dfu->layout) {
88 case DFU_FS_FAT:
89 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
90 op == DFU_OP_READ ? "load" : "write",
91 dfu->data.mmc.dev, dfu->data.mmc.part,
92 (unsigned int) buf, dfu->name);
93 break;
94 case DFU_FS_EXT4:
95 sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
96 op == DFU_OP_READ ? "load" : "write",
97 dfu->data.mmc.dev, dfu->data.mmc.part,
98 (unsigned int) buf, dfu->name);
99 break;
100 default:
101 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
102 dfu_get_layout(dfu->layout));
103 return -1;
106 if (op == DFU_OP_WRITE)
107 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
109 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
111 ret = run_command(cmd_buf, 0);
112 if (ret) {
113 puts("dfu: Read error!\n");
114 return ret;
117 if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
118 str_env = getenv("filesize");
119 if (str_env == NULL) {
120 puts("dfu: Wrong file size!\n");
121 return -1;
123 *len = simple_strtoul(str_env, NULL, 16);
126 return ret;
129 int dfu_write_medium_mmc(struct dfu_entity *dfu,
130 u64 offset, void *buf, long *len)
132 int ret = -1;
134 switch (dfu->layout) {
135 case DFU_RAW_ADDR:
136 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
137 break;
138 case DFU_FS_FAT:
139 case DFU_FS_EXT4:
140 ret = mmc_file_buffer(dfu, buf, len);
141 break;
142 default:
143 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
144 dfu_get_layout(dfu->layout));
147 return ret;
150 int dfu_flush_medium_mmc(struct dfu_entity *dfu)
152 int ret = 0;
154 if (dfu->layout != DFU_RAW_ADDR) {
155 /* Do stuff here. */
156 ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf,
157 &dfu_file_buf_len);
159 /* Now that we're done */
160 dfu_file_buf_len = 0;
163 return ret;
166 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
167 long *len)
169 int ret = -1;
171 switch (dfu->layout) {
172 case DFU_RAW_ADDR:
173 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
174 break;
175 case DFU_FS_FAT:
176 case DFU_FS_EXT4:
177 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
178 break;
179 default:
180 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
181 dfu_get_layout(dfu->layout));
184 return ret;
187 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
189 int dev, part;
190 struct mmc *mmc;
191 block_dev_desc_t *blk_dev;
192 disk_partition_t partinfo;
193 char *st;
195 dfu->dev_type = DFU_DEV_MMC;
196 st = strsep(&s, " ");
197 if (!strcmp(st, "mmc")) {
198 dfu->layout = DFU_RAW_ADDR;
199 dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16);
200 dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16);
201 dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num);
202 } else if (!strcmp(st, "fat")) {
203 dfu->layout = DFU_FS_FAT;
204 } else if (!strcmp(st, "ext4")) {
205 dfu->layout = DFU_FS_EXT4;
206 } else if (!strcmp(st, "part")) {
208 dfu->layout = DFU_RAW_ADDR;
210 dev = simple_strtoul(s, &s, 10);
211 s++;
212 part = simple_strtoul(s, &s, 10);
214 mmc = find_mmc_device(dev);
215 if (mmc == NULL || mmc_init(mmc)) {
216 printf("%s: could not find mmc device #%d!\n",
217 __func__, dev);
218 return -ENODEV;
221 blk_dev = &mmc->block_dev;
222 if (get_partition_info(blk_dev, part, &partinfo) != 0) {
223 printf("%s: could not find partition #%d on mmc device #%d!\n",
224 __func__, part, dev);
225 return -ENODEV;
228 dfu->data.mmc.lba_start = partinfo.start;
229 dfu->data.mmc.lba_size = partinfo.size;
230 dfu->data.mmc.lba_blk_size = partinfo.blksz;
232 } else {
233 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
234 return -ENODEV;
237 if (dfu->layout == DFU_FS_EXT4 || dfu->layout == DFU_FS_FAT) {
238 dfu->data.mmc.dev = simple_strtoul(s, &s, 10);
239 dfu->data.mmc.part = simple_strtoul(++s, &s, 10);
242 dfu->read_medium = dfu_read_medium_mmc;
243 dfu->write_medium = dfu_write_medium_mmc;
244 dfu->flush_medium = dfu_flush_medium_mmc;
246 /* initial state */
247 dfu->inited = 0;
249 return 0;