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+
16 static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
17 dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
18 static long dfu_file_buf_len;
20 static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
21 u64 offset, void *buf, long *len)
23 char cmd_buf[DFU_CMD_BUF_SIZE];
24 u32 blk_start, blk_count;
27 * We must ensure that we work in lba_blk_size chunks, so ALIGN
30 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
32 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");
41 sprintf(cmd_buf, "mmc %s %p %x %x",
42 op == DFU_OP_READ ? "read" : "write",
43 buf, blk_start, blk_count);
45 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
46 return run_command(cmd_buf, 0);
49 static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
51 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
56 /* Add to the current buffer. */
57 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
58 dfu_file_buf_len += *len;
63 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
66 char cmd_buf[DFU_CMD_BUF_SIZE];
70 switch (dfu->layout) {
72 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
73 op == DFU_OP_READ ? "load" : "write",
74 dfu->data.mmc.dev, dfu->data.mmc.part,
75 (unsigned int) buf, dfu->name);
78 sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
79 op == DFU_OP_READ ? "load" : "write",
80 dfu->data.mmc.dev, dfu->data.mmc.part,
81 (unsigned int) buf, dfu->name);
84 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
85 dfu_get_layout(dfu->layout));
89 if (op == DFU_OP_WRITE)
90 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
92 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
94 ret = run_command(cmd_buf, 0);
96 puts("dfu: Read error!\n");
100 if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
101 str_env = getenv("filesize");
102 if (str_env == NULL) {
103 puts("dfu: Wrong file size!\n");
106 *len = simple_strtoul(str_env, NULL, 16);
112 int dfu_write_medium_mmc(struct dfu_entity *dfu,
113 u64 offset, void *buf, long *len)
117 switch (dfu->layout) {
119 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
123 ret = mmc_file_buffer(dfu, buf, len);
126 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
127 dfu_get_layout(dfu->layout));
133 int dfu_flush_medium_mmc(struct dfu_entity *dfu)
137 if (dfu->layout != DFU_RAW_ADDR) {
139 ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf,
142 /* Now that we're done */
143 dfu_file_buf_len = 0;
149 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
154 switch (dfu->layout) {
156 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
160 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
163 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
164 dfu_get_layout(dfu->layout));
170 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
174 block_dev_desc_t *blk_dev;
175 disk_partition_t partinfo;
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;
187 } else if (!strcmp(st, "ext4")) {
188 dfu->layout = DFU_FS_EXT4;
189 } else if (!strcmp(st, "part")) {
191 dfu->layout = DFU_RAW_ADDR;
193 dev = simple_strtoul(s, &s, 10);
195 part = simple_strtoul(s, &s, 10);
197 mmc = find_mmc_device(dev);
198 if (mmc == NULL || mmc_init(mmc)) {
199 printf("%s: could not find mmc device #%d!\n",
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",
207 __func__, part, dev);
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;
216 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
220 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);
225 dfu->read_medium = dfu_read_medium_mmc;
226 dfu->write_medium = dfu_write_medium_mmc;
227 dfu->flush_medium = dfu_flush_medium_mmc;