2 * dfu.c -- DFU back-end routines
4 * Copyright (C) 2012 Samsung Electronics
7 * SPDX-License-Identifier: GPL-2.0+
19 static unsigned char *dfu_file_buf;
20 static long dfu_file_buf_len;
21 static long dfu_file_buf_filled;
23 static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part)
27 if (part == mmc->part_num)
30 ret = mmc_switch_part(dfu->data.mmc.dev_num, part);
32 error("Cannot switch to partition %d\n", part);
40 static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
41 u64 offset, void *buf, long *len)
44 u32 blk_start, blk_count, n = 0;
45 int ret, part_num_bkp = 0;
47 mmc = find_mmc_device(dfu->data.mmc.dev_num);
49 error("Device MMC %d - not found!", dfu->data.mmc.dev_num);
54 * We must ensure that we work in lba_blk_size chunks, so ALIGN
57 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
59 blk_start = dfu->data.mmc.lba_start +
60 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
61 blk_count = *len / dfu->data.mmc.lba_blk_size;
62 if (blk_start + blk_count >
63 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
64 puts("Request would exceed designated area!\n");
68 if (dfu->data.mmc.hw_partition >= 0) {
69 part_num_bkp = mmc->part_num;
70 ret = mmc_access_part(dfu, mmc, dfu->data.mmc.hw_partition);
75 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
76 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
77 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
80 n = mmc->block_dev.block_read(&mmc->block_dev, blk_start,
84 n = mmc->block_dev.block_write(&mmc->block_dev, blk_start,
88 error("Operation not supported\n");
92 error("MMC operation failed");
93 if (dfu->data.mmc.hw_partition >= 0)
94 mmc_access_part(dfu, mmc, part_num_bkp);
98 if (dfu->data.mmc.hw_partition >= 0) {
99 ret = mmc_access_part(dfu, mmc, part_num_bkp);
107 static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
109 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
110 dfu_file_buf_len = 0;
114 /* Add to the current buffer. */
115 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
116 dfu_file_buf_len += *len;
121 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
122 void *buf, long *len)
124 const char *fsname, *opname;
125 char cmd_buf[DFU_CMD_BUF_SIZE];
129 switch (dfu->layout) {
137 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
138 dfu_get_layout(dfu->layout));
156 sprintf(cmd_buf, "%s%s mmc %d:%d", fsname, opname,
157 dfu->data.mmc.dev, dfu->data.mmc.part);
159 if (op != DFU_OP_SIZE)
160 sprintf(cmd_buf + strlen(cmd_buf), " %p", buf);
162 sprintf(cmd_buf + strlen(cmd_buf), " %s", dfu->name);
164 if (op == DFU_OP_WRITE)
165 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
167 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
169 ret = run_command(cmd_buf, 0);
171 puts("dfu: Read error!\n");
175 if (op != DFU_OP_WRITE) {
176 str_env = getenv("filesize");
177 if (str_env == NULL) {
178 puts("dfu: Wrong file size!\n");
181 *len = simple_strtoul(str_env, NULL, 16);
187 int dfu_write_medium_mmc(struct dfu_entity *dfu,
188 u64 offset, void *buf, long *len)
192 switch (dfu->layout) {
194 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
198 ret = mmc_file_buffer(dfu, buf, len);
201 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
202 dfu_get_layout(dfu->layout));
208 int dfu_flush_medium_mmc(struct dfu_entity *dfu)
212 if (dfu->layout != DFU_RAW_ADDR) {
214 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf,
217 /* Now that we're done */
218 dfu_file_buf_len = 0;
224 long dfu_get_medium_size_mmc(struct dfu_entity *dfu)
229 switch (dfu->layout) {
231 return dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
234 dfu_file_buf_filled = -1;
235 ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, &len);
238 if (len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
242 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
243 dfu_get_layout(dfu->layout));
248 static int mmc_file_unbuffer(struct dfu_entity *dfu, u64 offset, void *buf,
254 if (dfu_file_buf_filled == -1) {
255 ret = mmc_file_op(DFU_OP_READ, dfu, dfu_file_buf, &file_len);
258 dfu_file_buf_filled = file_len;
260 if (offset + *len > dfu_file_buf_filled)
263 /* Add to the current buffer. */
264 memcpy(buf, dfu_file_buf + offset, *len);
269 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
274 switch (dfu->layout) {
276 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
280 ret = mmc_file_unbuffer(dfu, offset, buf, len);
283 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
284 dfu_get_layout(dfu->layout));
290 void dfu_free_entity_mmc(struct dfu_entity *dfu)
299 * @param s Parameter string containing space-separated arguments:
301 * raw (raw read/write)
304 * part (partition image)
306 * lba_start and lba_size, for raw write
307 * mmc_dev and mmc_part, for filesystems and part
309 * mmcpart <num> (access to HW eMMC partitions)
311 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
313 const char *entity_type;
320 const char **parg = argv;
322 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
324 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
325 *parg = strsep(&s, " ");
327 error("Invalid number of arguments.\n");
332 entity_type = argv[0];
334 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
337 second_arg = simple_strtoul(argv[1], NULL, 0);
338 third_arg = simple_strtoul(argv[2], NULL, 0);
340 mmc = find_mmc_device(dfu->data.mmc.dev_num);
342 error("Couldn't find MMC device no. %d.\n",
343 dfu->data.mmc.dev_num);
348 error("Couldn't init MMC device.\n");
352 dfu->data.mmc.hw_partition = -EINVAL;
353 if (!strcmp(entity_type, "raw")) {
354 dfu->layout = DFU_RAW_ADDR;
355 dfu->data.mmc.lba_start = second_arg;
356 dfu->data.mmc.lba_size = third_arg;
357 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
360 * Check for an extra entry at dfu_alt_info env variable
361 * specifying the mmc HW defined partition number
364 if (!strcmp(strsep(&s, " "), "mmcpart"))
365 dfu->data.mmc.hw_partition =
366 simple_strtoul(s, NULL, 0);
368 } else if (!strcmp(entity_type, "part")) {
369 disk_partition_t partinfo;
370 block_dev_desc_t *blk_dev = &mmc->block_dev;
371 int mmcdev = second_arg;
372 int mmcpart = third_arg;
374 if (get_partition_info(blk_dev, mmcpart, &partinfo) != 0) {
375 error("Couldn't find part #%d on mmc device #%d\n",
380 dfu->layout = DFU_RAW_ADDR;
381 dfu->data.mmc.lba_start = partinfo.start;
382 dfu->data.mmc.lba_size = partinfo.size;
383 dfu->data.mmc.lba_blk_size = partinfo.blksz;
384 } else if (!strcmp(entity_type, "fat")) {
385 dfu->layout = DFU_FS_FAT;
386 } else if (!strcmp(entity_type, "ext4")) {
387 dfu->layout = DFU_FS_EXT4;
389 error("Memory layout (%s) not supported!\n", entity_type);
393 /* if it's NOT a raw write */
394 if (strcmp(entity_type, "raw")) {
395 dfu->data.mmc.dev = second_arg;
396 dfu->data.mmc.part = third_arg;
399 dfu->dev_type = DFU_DEV_MMC;
400 dfu->get_medium_size = dfu_get_medium_size_mmc;
401 dfu->read_medium = dfu_read_medium_mmc;
402 dfu->write_medium = dfu_write_medium_mmc;
403 dfu->flush_medium = dfu_flush_medium_mmc;
405 dfu->free_entity = dfu_free_entity_mmc;
407 /* Check if file buffer is ready */
409 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
410 CONFIG_SYS_DFU_MAX_FILE_SIZE);
412 error("Could not memalign 0x%x bytes",
413 CONFIG_SYS_DFU_MAX_FILE_SIZE);