blob: e2f39784005549170839f0e559300764715966bc [file] [log] [blame]
Lukasz Majewski34106b22012-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 *
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>
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +000024#include <errno.h>
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000025#include <div64.h>
Lukasz Majewski34106b22012-08-06 14:41:08 +020026#include <dfu.h>
27
28enum dfu_mmc_op {
29 DFU_OP_READ = 1,
30 DFU_OP_WRITE,
31};
32
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000033static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
34 dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
35static long dfu_file_buf_len;
36
Lukasz Majewski34106b22012-08-06 14:41:08 +020037static int mmc_block_op(enum dfu_mmc_op op, struct dfu_entity *dfu,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000038 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +020039{
40 char cmd_buf[DFU_CMD_BUF_SIZE];
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000041 u32 blk_start, blk_count;
Lukasz Majewski34106b22012-08-06 14:41:08 +020042
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000043 /*
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);
Lukasz Majewski34106b22012-08-06 14:41:08 +020048
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000049 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);
Lukasz Majewski34106b22012-08-06 14:41:08 +020061
62 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
63 return run_command(cmd_buf, 0);
64}
65
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000066static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +020067{
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000068 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
69 dfu_file_buf_len = 0;
70 return -EINVAL;
71 }
Lukasz Majewski34106b22012-08-06 14:41:08 +020072
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000073 /* 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;
Lukasz Majewski34106b22012-08-06 14:41:08 +020078}
79
80static 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
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000087 switch (dfu->layout) {
88 case DFU_FS_FAT:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000089 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000090 op == DFU_OP_READ ? "load" : "write",
91 dfu->data.mmc.dev, dfu->data.mmc.part,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000092 (unsigned int) buf, dfu->name);
93 if (op == DFU_OP_WRITE)
94 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000095 break;
96 case DFU_FS_EXT4:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000097 sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000098 op == DFU_OP_READ ? "load" : "write",
99 dfu->data.mmc.dev, dfu->data.mmc.part,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000100 (unsigned int) buf, dfu->name);
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000101 break;
102 default:
103 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
104 dfu_get_layout(dfu->layout));
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000105 return -1;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000106 }
Lukasz Majewski34106b22012-08-06 14:41:08 +0200107
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
Łukasz Majewski180e3dd2012-08-23 23:33:56 +0000116 if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
Lukasz Majewski34106b22012-08-06 14:41:08 +0200117 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
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000128int dfu_write_medium_mmc(struct dfu_entity *dfu,
129 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200130{
131 int ret = -1;
132
133 switch (dfu->layout) {
134 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000135 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200136 break;
137 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000138 case DFU_FS_EXT4:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000139 ret = mmc_file_buffer(dfu, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200140 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
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000149int 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
165int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
166 long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200167{
168 int ret = -1;
169
170 switch (dfu->layout) {
171 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000172 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200173 break;
174 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000175 case DFU_FS_EXT4:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000176 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200177 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
186int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
187{
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000188 int dev, part;
189 struct mmc *mmc;
190 block_dev_desc_t *blk_dev;
191 disk_partition_t partinfo;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200192 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;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000203 } else if (!strcmp(st, "ext4")) {
204 dfu->layout = DFU_FS_EXT4;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000205 } 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)) {
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000215 printf("%s: could not find mmc device #%d!\n",
216 __func__, dev);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000217 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",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000223 __func__, part, dev);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000224 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
Lukasz Majewski34106b22012-08-06 14:41:08 +0200231 } else {
232 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000233 return -ENODEV;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200234 }
235
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000236 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
Lukasz Majewski34106b22012-08-06 14:41:08 +0200241 dfu->read_medium = dfu_read_medium_mmc;
242 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000243 dfu->flush_medium = dfu_flush_medium_mmc;
244
245 /* initial state */
246 dfu->inited = 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200247
248 return 0;
249}