2 * dfu.c -- DFU back-end routines
4 * Copyright (C) 2012 Samsung Electronics
5 * author: Lukasz Majewski <l.majewski@samsung.com>
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.
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.
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
31 static int mmc_block_op(enum dfu_mmc_op op, struct dfu_entity *dfu,
34 char cmd_buf[DFU_CMD_BUF_SIZE];
36 sprintf(cmd_buf, "mmc %s 0x%x %x %x",
37 op == DFU_OP_READ ? "read" : "write",
39 dfu->data.mmc.lba_start,
40 dfu->data.mmc.lba_size);
42 if (op == DFU_OP_READ)
43 *len = dfu->data.mmc.lba_blk_size * dfu->data.mmc.lba_size;
45 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
46 return run_command(cmd_buf, 0);
49 static inline int mmc_block_write(struct dfu_entity *dfu, void *buf, long *len)
51 return mmc_block_op(DFU_OP_WRITE, dfu, buf, len);
54 static inline int mmc_block_read(struct dfu_entity *dfu, void *buf, long *len)
56 return mmc_block_op(DFU_OP_READ, dfu, buf, len);
59 static int mmc_file_op(enum dfu_mmc_op op, struct dfu_entity *dfu,
62 char cmd_buf[DFU_CMD_BUF_SIZE];
66 switch (dfu->layout) {
68 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s %lx",
69 op == DFU_OP_READ ? "load" : "write",
70 dfu->data.mmc.dev, dfu->data.mmc.part,
71 (unsigned int) buf, dfu->name, *len);
74 sprintf(cmd_buf, "ext4%s mmc %d:%d /%s 0x%x %ld",
75 op == DFU_OP_READ ? "load" : "write",
76 dfu->data.mmc.dev, dfu->data.mmc.part,
77 dfu->name, (unsigned int) buf, *len);
80 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
81 dfu_get_layout(dfu->layout));
84 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
86 ret = run_command(cmd_buf, 0);
88 puts("dfu: Read error!\n");
92 if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
93 str_env = getenv("filesize");
94 if (str_env == NULL) {
95 puts("dfu: Wrong file size!\n");
98 *len = simple_strtoul(str_env, NULL, 16);
104 static inline int mmc_file_write(struct dfu_entity *dfu, void *buf, long *len)
106 return mmc_file_op(DFU_OP_WRITE, dfu, buf, len);
109 static inline int mmc_file_read(struct dfu_entity *dfu, void *buf, long *len)
111 return mmc_file_op(DFU_OP_READ, dfu, buf, len);
114 int dfu_write_medium_mmc(struct dfu_entity *dfu, void *buf, long *len)
118 switch (dfu->layout) {
120 ret = mmc_block_write(dfu, buf, len);
124 ret = mmc_file_write(dfu, buf, len);
127 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
128 dfu_get_layout(dfu->layout));
134 int dfu_read_medium_mmc(struct dfu_entity *dfu, void *buf, long *len)
138 switch (dfu->layout) {
140 ret = mmc_block_read(dfu, buf, len);
144 ret = mmc_file_read(dfu, buf, len);
147 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
148 dfu_get_layout(dfu->layout));
154 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
158 dfu->dev_type = DFU_DEV_MMC;
159 st = strsep(&s, " ");
160 if (!strcmp(st, "mmc")) {
161 dfu->layout = DFU_RAW_ADDR;
162 dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16);
163 dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16);
164 dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num);
165 } else if (!strcmp(st, "fat")) {
166 dfu->layout = DFU_FS_FAT;
167 } else if (!strcmp(st, "ext4")) {
168 dfu->layout = DFU_FS_EXT4;
170 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
173 if (dfu->layout == DFU_FS_EXT4 || dfu->layout == DFU_FS_FAT) {
174 dfu->data.mmc.dev = simple_strtoul(s, &s, 10);
175 dfu->data.mmc.part = simple_strtoul(++s, &s, 10);
178 dfu->read_medium = dfu_read_medium_mmc;
179 dfu->write_medium = dfu_write_medium_mmc;