xref: /openbmc/u-boot/drivers/dfu/dfu_mmc.c (revision 713cb680)
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  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 #include <common.h>
23 #include <malloc.h>
24 #include <errno.h>
25 #include <div64.h>
26 #include <dfu.h>
27 
28 enum dfu_mmc_op {
29 	DFU_OP_READ = 1,
30 	DFU_OP_WRITE,
31 };
32 
33 static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
34 				dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
35 static long dfu_file_buf_len;
36 
37 static int mmc_block_op(enum dfu_mmc_op op, struct dfu_entity *dfu,
38 			u64 offset, void *buf, long *len)
39 {
40 	char cmd_buf[DFU_CMD_BUF_SIZE];
41 	u32 blk_start, blk_count;
42 
43 	/*
44 	 * We must ensure that we work in lba_blk_size chunks, so ALIGN
45 	 * this value.
46 	 */
47 	*len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
48 
49 	blk_start = dfu->data.mmc.lba_start +
50 			(u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
51 	blk_count = *len / dfu->data.mmc.lba_blk_size;
52 	if (blk_start + blk_count >
53 			dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
54 		puts("Request would exceed designated area!\n");
55 		return -EINVAL;
56 	}
57 
58 	sprintf(cmd_buf, "mmc %s %p %x %x",
59 		op == DFU_OP_READ ? "read" : "write",
60 		 buf, blk_start, blk_count);
61 
62 	debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
63 	return run_command(cmd_buf, 0);
64 }
65 
66 static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
67 {
68 	if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
69 		dfu_file_buf_len = 0;
70 		return -EINVAL;
71 	}
72 
73 	/* Add to the current buffer. */
74 	memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
75 	dfu_file_buf_len += *len;
76 
77 	return 0;
78 }
79 
80 static int mmc_file_op(enum dfu_mmc_op op, struct dfu_entity *dfu,
81 			void *buf, long *len)
82 {
83 	char cmd_buf[DFU_CMD_BUF_SIZE];
84 	char *str_env;
85 	int ret;
86 
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 		if (op == DFU_OP_WRITE)
94 			sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
95 		break;
96 	case DFU_FS_EXT4:
97 		sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
98 			op == DFU_OP_READ ? "load" : "write",
99 			dfu->data.mmc.dev, dfu->data.mmc.part,
100 			(unsigned int) buf, dfu->name);
101 		break;
102 	default:
103 		printf("%s: Layout (%s) not (yet) supported!\n", __func__,
104 		       dfu_get_layout(dfu->layout));
105 		return -1;
106 	}
107 
108 	debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
109 
110 	ret = run_command(cmd_buf, 0);
111 	if (ret) {
112 		puts("dfu: Read error!\n");
113 		return ret;
114 	}
115 
116 	if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
117 		str_env = getenv("filesize");
118 		if (str_env == NULL) {
119 			puts("dfu: Wrong file size!\n");
120 			return -1;
121 		}
122 		*len = simple_strtoul(str_env, NULL, 16);
123 	}
124 
125 	return ret;
126 }
127 
128 int dfu_write_medium_mmc(struct dfu_entity *dfu,
129 		u64 offset, void *buf, long *len)
130 {
131 	int ret = -1;
132 
133 	switch (dfu->layout) {
134 	case DFU_RAW_ADDR:
135 		ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
136 		break;
137 	case DFU_FS_FAT:
138 	case DFU_FS_EXT4:
139 		ret = mmc_file_buffer(dfu, buf, len);
140 		break;
141 	default:
142 		printf("%s: Layout (%s) not (yet) supported!\n", __func__,
143 		       dfu_get_layout(dfu->layout));
144 	}
145 
146 	return ret;
147 }
148 
149 int dfu_flush_medium_mmc(struct dfu_entity *dfu)
150 {
151 	int ret = 0;
152 
153 	if (dfu->layout != DFU_RAW_ADDR) {
154 		/* Do stuff here. */
155 		ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf,
156 				&dfu_file_buf_len);
157 
158 		/* Now that we're done */
159 		dfu_file_buf_len = 0;
160 	}
161 
162 	return ret;
163 }
164 
165 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
166 		long *len)
167 {
168 	int ret = -1;
169 
170 	switch (dfu->layout) {
171 	case DFU_RAW_ADDR:
172 		ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
173 		break;
174 	case DFU_FS_FAT:
175 	case DFU_FS_EXT4:
176 		ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
177 		break;
178 	default:
179 		printf("%s: Layout (%s) not (yet) supported!\n", __func__,
180 		       dfu_get_layout(dfu->layout));
181 	}
182 
183 	return ret;
184 }
185 
186 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
187 {
188 	int dev, part;
189 	struct mmc *mmc;
190 	block_dev_desc_t *blk_dev;
191 	disk_partition_t partinfo;
192 	char *st;
193 
194 	dfu->dev_type = DFU_DEV_MMC;
195 	st = strsep(&s, " ");
196 	if (!strcmp(st, "mmc")) {
197 		dfu->layout = DFU_RAW_ADDR;
198 		dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16);
199 		dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16);
200 		dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num);
201 	} else if (!strcmp(st, "fat")) {
202 		dfu->layout = DFU_FS_FAT;
203 	} else if (!strcmp(st, "ext4")) {
204 		dfu->layout = DFU_FS_EXT4;
205 	} else if (!strcmp(st, "part")) {
206 
207 		dfu->layout = DFU_RAW_ADDR;
208 
209 		dev = simple_strtoul(s, &s, 10);
210 		s++;
211 		part = simple_strtoul(s, &s, 10);
212 
213 		mmc = find_mmc_device(dev);
214 		if (mmc == NULL || mmc_init(mmc)) {
215 			printf("%s: could not find mmc device #%d!\n",
216 			       __func__, dev);
217 			return -ENODEV;
218 		}
219 
220 		blk_dev = &mmc->block_dev;
221 		if (get_partition_info(blk_dev, part, &partinfo) != 0) {
222 			printf("%s: could not find partition #%d on mmc device #%d!\n",
223 			       __func__, part, dev);
224 			return -ENODEV;
225 		}
226 
227 		dfu->data.mmc.lba_start = partinfo.start;
228 		dfu->data.mmc.lba_size = partinfo.size;
229 		dfu->data.mmc.lba_blk_size = partinfo.blksz;
230 
231 	} else {
232 		printf("%s: Memory layout (%s) not supported!\n", __func__, st);
233 		return -ENODEV;
234 	}
235 
236 	if (dfu->layout == DFU_FS_EXT4 || dfu->layout == DFU_FS_FAT) {
237 		dfu->data.mmc.dev = simple_strtoul(s, &s, 10);
238 		dfu->data.mmc.part = simple_strtoul(++s, &s, 10);
239 	}
240 
241 	dfu->read_medium = dfu_read_medium_mmc;
242 	dfu->write_medium = dfu_write_medium_mmc;
243 	dfu->flush_medium = dfu_flush_medium_mmc;
244 
245 	/* initial state */
246 	dfu->inited = 0;
247 
248 	return 0;
249 }
250