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+
17 static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE)
18 dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE];
19 static long dfu_file_buf_len;
21 static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part)
25 if (part == mmc->part_num)
28 ret = mmc_switch_part(dfu->dev_num, part);
30 error("Cannot switch to partition %d\n", part);
38 static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
39 u64 offset, void *buf, long *len)
41 struct mmc *mmc = find_mmc_device(dfu->dev_num);
42 u32 blk_start, blk_count, n = 0;
43 int ret, part_num_bkp = 0;
46 * We must ensure that we work in lba_blk_size chunks, so ALIGN
49 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
51 blk_start = dfu->data.mmc.lba_start +
52 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
53 blk_count = *len / dfu->data.mmc.lba_blk_size;
54 if (blk_start + blk_count >
55 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
56 puts("Request would exceed designated area!\n");
60 if (dfu->data.mmc.hw_partition >= 0) {
61 part_num_bkp = mmc->part_num;
62 ret = mmc_access_part(dfu, mmc, dfu->data.mmc.hw_partition);
67 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
68 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE", dfu->dev_num,
69 blk_start, blk_count, buf);
72 n = mmc->block_dev.block_read(dfu->dev_num, blk_start,
76 n = mmc->block_dev.block_write(dfu->dev_num, blk_start,
80 error("Operation not supported\n");
84 error("MMC operation failed");
85 if (dfu->data.mmc.hw_partition >= 0)
86 mmc_access_part(dfu, mmc, part_num_bkp);
90 if (dfu->data.mmc.hw_partition >= 0) {
91 ret = mmc_access_part(dfu, mmc, part_num_bkp);
99 static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
101 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
102 dfu_file_buf_len = 0;
106 /* Add to the current buffer. */
107 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
108 dfu_file_buf_len += *len;
113 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
114 void *buf, long *len)
116 char cmd_buf[DFU_CMD_BUF_SIZE];
120 switch (dfu->layout) {
122 sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s",
123 op == DFU_OP_READ ? "load" : "write",
124 dfu->data.mmc.dev, dfu->data.mmc.part,
125 (unsigned int) buf, dfu->name);
128 sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s",
129 op == DFU_OP_READ ? "load" : "write",
130 dfu->data.mmc.dev, dfu->data.mmc.part,
131 (unsigned int) buf, dfu->name);
134 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
135 dfu_get_layout(dfu->layout));
139 if (op == DFU_OP_WRITE)
140 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
142 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
144 ret = run_command(cmd_buf, 0);
146 puts("dfu: Read error!\n");
150 if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) {
151 str_env = getenv("filesize");
152 if (str_env == NULL) {
153 puts("dfu: Wrong file size!\n");
156 *len = simple_strtoul(str_env, NULL, 16);
162 int dfu_write_medium_mmc(struct dfu_entity *dfu,
163 u64 offset, void *buf, long *len)
167 switch (dfu->layout) {
169 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
173 ret = mmc_file_buffer(dfu, buf, len);
176 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
177 dfu_get_layout(dfu->layout));
183 int dfu_flush_medium_mmc(struct dfu_entity *dfu)
187 if (dfu->layout != DFU_RAW_ADDR) {
189 ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf,
192 /* Now that we're done */
193 dfu_file_buf_len = 0;
199 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
204 switch (dfu->layout) {
206 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
210 ret = mmc_file_op(DFU_OP_READ, dfu, buf, len);
213 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
214 dfu_get_layout(dfu->layout));
221 * @param s Parameter string containing space-separated arguments:
223 * raw (raw read/write)
226 * part (partition image)
228 * lba_start and lba_size, for raw write
229 * mmc_dev and mmc_part, for filesystems and part
231 * mmcpart <num> (access to HW eMMC partitions)
233 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s)
235 const char *entity_type;
242 const char **parg = argv;
244 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
245 *parg = strsep(&s, " ");
247 error("Invalid number of arguments.\n");
252 entity_type = argv[0];
254 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
257 second_arg = simple_strtoul(argv[1], NULL, 0);
258 third_arg = simple_strtoul(argv[2], NULL, 0);
260 mmc = find_mmc_device(dfu->dev_num);
262 error("Couldn't find MMC device no. %d.\n", dfu->dev_num);
267 error("Couldn't init MMC device.\n");
271 dfu->data.mmc.hw_partition = -EINVAL;
272 if (!strcmp(entity_type, "raw")) {
273 dfu->layout = DFU_RAW_ADDR;
274 dfu->data.mmc.lba_start = second_arg;
275 dfu->data.mmc.lba_size = third_arg;
276 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
279 * Check for an extra entry at dfu_alt_info env variable
280 * specifying the mmc HW defined partition number
283 if (!strcmp(strsep(&s, " "), "mmcpart"))
284 dfu->data.mmc.hw_partition =
285 simple_strtoul(s, NULL, 0);
287 } else if (!strcmp(entity_type, "part")) {
288 disk_partition_t partinfo;
289 block_dev_desc_t *blk_dev = &mmc->block_dev;
290 int mmcdev = second_arg;
291 int mmcpart = third_arg;
293 if (get_partition_info(blk_dev, mmcpart, &partinfo) != 0) {
294 error("Couldn't find part #%d on mmc device #%d\n",
299 dfu->layout = DFU_RAW_ADDR;
300 dfu->data.mmc.lba_start = partinfo.start;
301 dfu->data.mmc.lba_size = partinfo.size;
302 dfu->data.mmc.lba_blk_size = partinfo.blksz;
303 } else if (!strcmp(entity_type, "fat")) {
304 dfu->layout = DFU_FS_FAT;
305 } else if (!strcmp(entity_type, "ext4")) {
306 dfu->layout = DFU_FS_EXT4;
308 error("Memory layout (%s) not supported!\n", entity_type);
312 /* if it's NOT a raw write */
313 if (strcmp(entity_type, "raw")) {
314 dfu->data.mmc.dev = second_arg;
315 dfu->data.mmc.part = third_arg;
318 dfu->dev_type = DFU_DEV_MMC;
319 dfu->read_medium = dfu_read_medium_mmc;
320 dfu->write_medium = dfu_write_medium_mmc;
321 dfu->flush_medium = dfu_flush_medium_mmc;