blob: 0816f46bad8ebcd6350319f98be8dbeb6ed7f046 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewski34106b22012-08-06 14:41:08 +02008 */
9
10#include <common.h>
11#include <malloc.h>
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +000012#include <errno.h>
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000013#include <div64.h>
Lukasz Majewski34106b22012-08-06 14:41:08 +020014#include <dfu.h>
15
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000016static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
17 dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
18static long dfu_file_buf_len;
19
Afzal Mohammedb9a4a6b2013-09-18 01:14:50 +053020static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000021 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +020022{
23 char cmd_buf[DFU_CMD_BUF_SIZE];
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000024 u32 blk_start, blk_count;
Lukasz Majewski34106b22012-08-06 14:41:08 +020025
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000026 /*
27 * We must ensure that we work in lba_blk_size chunks, so ALIGN
28 * this value.
29 */
30 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
Lukasz Majewski34106b22012-08-06 14:41:08 +020031
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000032 blk_start = dfu->data.mmc.lba_start +
33 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
34 blk_count = *len / dfu->data.mmc.lba_blk_size;
35 if (blk_start + blk_count >
36 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
37 puts("Request would exceed designated area!\n");
38 return -EINVAL;
39 }
40
41 sprintf(cmd_buf, "mmc %s %p %x %x",
42 op == DFU_OP_READ ? "read" : "write",
43 buf, blk_start, blk_count);
Lukasz Majewski34106b22012-08-06 14:41:08 +020044
45 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
46 return run_command(cmd_buf, 0);
47}
48
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000049static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +020050{
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000051 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
52 dfu_file_buf_len = 0;
53 return -EINVAL;
54 }
Lukasz Majewski34106b22012-08-06 14:41:08 +020055
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000056 /* Add to the current buffer. */
57 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
58 dfu_file_buf_len += *len;
59
60 return 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +020061}
62
Afzal Mohammedb9a4a6b2013-09-18 01:14:50 +053063static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
Lukasz Majewski34106b22012-08-06 14:41:08 +020064 void *buf, long *len)
65{
66 char cmd_buf[DFU_CMD_BUF_SIZE];
67 char *str_env;
68 int ret;
69
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000070 switch (dfu->layout) {
71 case DFU_FS_FAT:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000072 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000073 op == DFU_OP_READ ? "load" : "write",
74 dfu->data.mmc.dev, dfu->data.mmc.part,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000075 (unsigned int) buf, dfu->name);
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000076 break;
77 case DFU_FS_EXT4:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000078 sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000079 op == DFU_OP_READ ? "load" : "write",
80 dfu->data.mmc.dev, dfu->data.mmc.part,
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000081 (unsigned int) buf, dfu->name);
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000082 break;
83 default:
84 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
85 dfu_get_layout(dfu->layout));
Pantelis Antonioua6e788d2013-03-14 05:32:48 +000086 return -1;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +000087 }
Lukasz Majewski34106b22012-08-06 14:41:08 +020088
Lukasz Majewski8f957c02014-02-20 10:29:18 +010089 if (op == DFU_OP_WRITE)
90 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
91
Lukasz Majewski34106b22012-08-06 14:41:08 +020092 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
93
94 ret = run_command(cmd_buf, 0);
95 if (ret) {
96 puts("dfu: Read error!\n");
97 return ret;
98 }
99
Łukasz Majewski180e3dd2012-08-23 23:33:56 +0000100 if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
Lukasz Majewski34106b22012-08-06 14:41:08 +0200101 str_env = getenv("filesize");
102 if (str_env == NULL) {
103 puts("dfu: Wrong file size!\n");
104 return -1;
105 }
106 *len = simple_strtoul(str_env, NULL, 16);
107 }
108
109 return ret;
110}
111
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000112int dfu_write_medium_mmc(struct dfu_entity *dfu,
113 u64 offset, void *buf, long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200114{
115 int ret = -1;
116
117 switch (dfu->layout) {
118 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000119 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200120 break;
121 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000122 case DFU_FS_EXT4:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000123 ret = mmc_file_buffer(dfu, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200124 break;
125 default:
126 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
127 dfu_get_layout(dfu->layout));
128 }
129
130 return ret;
131}
132
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000133int dfu_flush_medium_mmc(struct dfu_entity *dfu)
134{
135 int ret = 0;
136
137 if (dfu->layout != DFU_RAW_ADDR) {
138 /* Do stuff here. */
139 ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf,
140 &dfu_file_buf_len);
141
142 /* Now that we're done */
143 dfu_file_buf_len = 0;
144 }
145
146 return ret;
147}
148
149int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
150 long *len)
Lukasz Majewski34106b22012-08-06 14:41:08 +0200151{
152 int ret = -1;
153
154 switch (dfu->layout) {
155 case DFU_RAW_ADDR:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000156 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200157 break;
158 case DFU_FS_FAT:
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000159 case DFU_FS_EXT4:
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000160 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
Lukasz Majewski34106b22012-08-06 14:41:08 +0200161 break;
162 default:
163 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
164 dfu_get_layout(dfu->layout));
165 }
166
167 return ret;
168}
169
170int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
171{
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000172 int dev, part;
173 struct mmc *mmc;
174 block_dev_desc_t *blk_dev;
175 disk_partition_t partinfo;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200176 char *st;
177
178 dfu->dev_type = DFU_DEV_MMC;
179 st = strsep(&s, " ");
180 if (!strcmp(st, "mmc")) {
181 dfu->layout = DFU_RAW_ADDR;
182 dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16);
183 dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16);
184 dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num);
185 } else if (!strcmp(st, "fat")) {
186 dfu->layout = DFU_FS_FAT;
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000187 } else if (!strcmp(st, "ext4")) {
188 dfu->layout = DFU_FS_EXT4;
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000189 } else if (!strcmp(st, "part")) {
190
191 dfu->layout = DFU_RAW_ADDR;
192
193 dev = simple_strtoul(s, &s, 10);
194 s++;
195 part = simple_strtoul(s, &s, 10);
196
197 mmc = find_mmc_device(dev);
198 if (mmc == NULL || mmc_init(mmc)) {
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000199 printf("%s: could not find mmc device #%d!\n",
200 __func__, dev);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000201 return -ENODEV;
202 }
203
204 blk_dev = &mmc->block_dev;
205 if (get_partition_info(blk_dev, part, &partinfo) != 0) {
206 printf("%s: could not find partition #%d on mmc device #%d!\n",
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000207 __func__, part, dev);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000208 return -ENODEV;
209 }
210
211 dfu->data.mmc.lba_start = partinfo.start;
212 dfu->data.mmc.lba_size = partinfo.size;
213 dfu->data.mmc.lba_blk_size = partinfo.blksz;
214
Lukasz Majewski34106b22012-08-06 14:41:08 +0200215 } else {
216 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
Pantelis Antoniou620e9dd2012-11-30 08:01:11 +0000217 return -ENODEV;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200218 }
219
Łukasz Majewski71c88eb2012-08-23 23:33:55 +0000220 if (dfu->layout == DFU_FS_EXT4 || dfu->layout == DFU_FS_FAT) {
221 dfu->data.mmc.dev = simple_strtoul(s, &s, 10);
222 dfu->data.mmc.part = simple_strtoul(++s, &s, 10);
223 }
224
Lukasz Majewski34106b22012-08-06 14:41:08 +0200225 dfu->read_medium = dfu_read_medium_mmc;
226 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antonioua6e788d2013-03-14 05:32:48 +0000227 dfu->flush_medium = dfu_flush_medium_mmc;
228
229 /* initial state */
230 dfu->inited = 0;
Lukasz Majewski34106b22012-08-06 14:41:08 +0200231
232 return 0;
233}