blob: 0871a770b7b1d9b9bfc8c23a02701766345d76a1 [file] [log] [blame]
Lukasz Majewskicb383cd2012-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 Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Lukasz Majewskicb383cd2012-08-06 14:41:08 +02008 */
9
10#include <common.h>
11#include <malloc.h>
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +000012#include <errno.h>
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000013#include <div64.h>
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020014#include <dfu.h>
15
16enum dfu_mmc_op {
17 DFU_OP_READ = 1,
18 DFU_OP_WRITE,
19};
20
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000021static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
22 dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
23static long dfu_file_buf_len;
24
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020025static int mmc_block_op(enum dfu_mmc_op op, struct dfu_entity *dfu,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000026 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020027{
28 char cmd_buf[DFU_CMD_BUF_SIZE];
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000029 u32 blk_start, blk_count;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020030
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000031 /*
32 * We must ensure that we work in lba_blk_size chunks, so ALIGN
33 * this value.
34 */
35 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
36
37 blk_start = dfu->data.mmc.lba_start +
38 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
39 blk_count = *len / dfu->data.mmc.lba_blk_size;
40 if (blk_start + blk_count >
41 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
42 puts("Request would exceed designated area!\n");
43 return -EINVAL;
44 }
45
46 sprintf(cmd_buf, "mmc %s %p %x %x",
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020047 op == DFU_OP_READ ? "read" : "write",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000048 buf, blk_start, blk_count);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020049
50 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
51 return run_command(cmd_buf, 0);
52}
53
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000054static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020055{
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000056 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
57 dfu_file_buf_len = 0;
58 return -EINVAL;
59 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020060
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000061 /* Add to the current buffer. */
62 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
63 dfu_file_buf_len += *len;
64
65 return 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020066}
67
68static int mmc_file_op(enum dfu_mmc_op op, struct dfu_entity *dfu,
69 void *buf, long *len)
70{
71 char cmd_buf[DFU_CMD_BUF_SIZE];
72 char *str_env;
73 int ret;
74
Łukasz Majewski43e66272012-08-23 23:33:55 +000075 switch (dfu->layout) {
76 case DFU_FS_FAT:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000077 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
Łukasz Majewski43e66272012-08-23 23:33:55 +000078 op == DFU_OP_READ ? "load" : "write",
79 dfu->data.mmc.dev, dfu->data.mmc.part,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000080 (unsigned int) buf, dfu->name);
81 if (op == DFU_OP_WRITE)
82 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
Łukasz Majewski43e66272012-08-23 23:33:55 +000083 break;
84 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000085 sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
Łukasz Majewski43e66272012-08-23 23:33:55 +000086 op == DFU_OP_READ ? "load" : "write",
87 dfu->data.mmc.dev, dfu->data.mmc.part,
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000088 (unsigned int) buf, dfu->name);
Łukasz Majewski051f9a32013-06-28 18:41:49 +020089 if (op == DFU_OP_WRITE)
90 sprintf(cmd_buf + strlen(cmd_buf), " %ld", *len);
Łukasz Majewski43e66272012-08-23 23:33:55 +000091 break;
92 default:
93 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
94 dfu_get_layout(dfu->layout));
Pantelis Antoniouea2453d2013-03-14 05:32:48 +000095 return -1;
Łukasz Majewski43e66272012-08-23 23:33:55 +000096 }
Lukasz Majewskicb383cd2012-08-06 14:41:08 +020097
98 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
99
100 ret = run_command(cmd_buf, 0);
101 if (ret) {
102 puts("dfu: Read error!\n");
103 return ret;
104 }
105
Łukasz Majewski81c1d7b2012-08-23 23:33:56 +0000106 if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200107 str_env = getenv("filesize");
108 if (str_env == NULL) {
109 puts("dfu: Wrong file size!\n");
110 return -1;
111 }
112 *len = simple_strtoul(str_env, NULL, 16);
113 }
114
115 return ret;
116}
117
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000118int dfu_write_medium_mmc(struct dfu_entity *dfu,
119 u64 offset, void *buf, long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200120{
121 int ret = -1;
122
123 switch (dfu->layout) {
124 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000125 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200126 break;
127 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000128 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000129 ret = mmc_file_buffer(dfu, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200130 break;
131 default:
132 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
133 dfu_get_layout(dfu->layout));
134 }
135
136 return ret;
137}
138
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000139int dfu_flush_medium_mmc(struct dfu_entity *dfu)
140{
141 int ret = 0;
142
143 if (dfu->layout != DFU_RAW_ADDR) {
144 /* Do stuff here. */
145 ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf,
146 &dfu_file_buf_len);
147
148 /* Now that we're done */
149 dfu_file_buf_len = 0;
150 }
151
152 return ret;
153}
154
155int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
156 long *len)
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200157{
158 int ret = -1;
159
160 switch (dfu->layout) {
161 case DFU_RAW_ADDR:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000162 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200163 break;
164 case DFU_FS_FAT:
Łukasz Majewski43e66272012-08-23 23:33:55 +0000165 case DFU_FS_EXT4:
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000166 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200167 break;
168 default:
169 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
170 dfu_get_layout(dfu->layout));
171 }
172
173 return ret;
174}
175
176int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
177{
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000178 int dev, part;
179 struct mmc *mmc;
180 block_dev_desc_t *blk_dev;
181 disk_partition_t partinfo;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200182 char *st;
183
184 dfu->dev_type = DFU_DEV_MMC;
185 st = strsep(&s, " ");
186 if (!strcmp(st, "mmc")) {
187 dfu->layout = DFU_RAW_ADDR;
188 dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16);
189 dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16);
190 dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num);
191 } else if (!strcmp(st, "fat")) {
192 dfu->layout = DFU_FS_FAT;
Łukasz Majewski43e66272012-08-23 23:33:55 +0000193 } else if (!strcmp(st, "ext4")) {
194 dfu->layout = DFU_FS_EXT4;
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000195 } else if (!strcmp(st, "part")) {
196
197 dfu->layout = DFU_RAW_ADDR;
198
199 dev = simple_strtoul(s, &s, 10);
200 s++;
201 part = simple_strtoul(s, &s, 10);
202
203 mmc = find_mmc_device(dev);
204 if (mmc == NULL || mmc_init(mmc)) {
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000205 printf("%s: could not find mmc device #%d!\n",
206 __func__, dev);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000207 return -ENODEV;
208 }
209
210 blk_dev = &mmc->block_dev;
211 if (get_partition_info(blk_dev, part, &partinfo) != 0) {
212 printf("%s: could not find partition #%d on mmc device #%d!\n",
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000213 __func__, part, dev);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000214 return -ENODEV;
215 }
216
217 dfu->data.mmc.lba_start = partinfo.start;
218 dfu->data.mmc.lba_size = partinfo.size;
219 dfu->data.mmc.lba_blk_size = partinfo.blksz;
220
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200221 } else {
222 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
Pantelis Antoniou1b6ca182012-11-30 08:01:11 +0000223 return -ENODEV;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200224 }
225
Łukasz Majewski43e66272012-08-23 23:33:55 +0000226 if (dfu->layout == DFU_FS_EXT4 || dfu->layout == DFU_FS_FAT) {
227 dfu->data.mmc.dev = simple_strtoul(s, &s, 10);
228 dfu->data.mmc.part = simple_strtoul(++s, &s, 10);
229 }
230
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200231 dfu->read_medium = dfu_read_medium_mmc;
232 dfu->write_medium = dfu_write_medium_mmc;
Pantelis Antoniouea2453d2013-03-14 05:32:48 +0000233 dfu->flush_medium = dfu_flush_medium_mmc;
234
235 /* initial state */
236 dfu->inited = 0;
Lukasz Majewskicb383cd2012-08-06 14:41:08 +0200237
238 return 0;
239}