1 // SPDX-License-Identifier: GPL-2.0+
3 * dfu.c -- DFU back-end routines
5 * Copyright (C) 2012 Samsung Electronics
19 #include <linux/printk.h>
21 static unsigned char *dfu_file_buf;
22 static u64 dfu_file_buf_len;
23 static u64 dfu_file_buf_offset;
25 static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu,
26 u64 offset, void *buf, long *len)
29 u32 blk_start, blk_count, n = 0;
30 int ret, part_num_bkp = 0;
32 mmc = find_mmc_device(dfu->data.mmc.dev_num);
34 pr_err("Device MMC %d - not found!", dfu->data.mmc.dev_num);
39 * We must ensure that we work in lba_blk_size chunks, so ALIGN
42 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
44 blk_start = dfu->data.mmc.lba_start +
45 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
46 blk_count = *len / dfu->data.mmc.lba_blk_size;
47 if (blk_start + blk_count >
48 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
49 puts("Request would exceed designated area!\n");
53 if (dfu->data.mmc.hw_partition >= 0) {
54 part_num_bkp = mmc_get_blk_desc(mmc)->hwpart;
55 ret = blk_select_hwpart_devnum(UCLASS_MMC,
56 dfu->data.mmc.dev_num,
57 dfu->data.mmc.hw_partition);
62 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
63 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
64 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
67 n = blk_dread(mmc_get_blk_desc(mmc), blk_start, blk_count, buf);
70 n = blk_dwrite(mmc_get_blk_desc(mmc), blk_start, blk_count,
74 pr_err("Operation not supported\n");
78 pr_err("MMC operation failed");
79 if (dfu->data.mmc.hw_partition >= 0)
80 blk_select_hwpart_devnum(UCLASS_MMC,
81 dfu->data.mmc.dev_num,
86 if (dfu->data.mmc.hw_partition >= 0) {
87 ret = blk_select_hwpart_devnum(UCLASS_MMC,
88 dfu->data.mmc.dev_num,
97 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
98 u64 offset, void *buf, u64 *len)
100 char dev_part_str[8];
105 switch (dfu->layout) {
107 fstype = FS_TYPE_FAT;
110 fstype = FS_TYPE_EXT;
115 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
116 dfu_get_layout(dfu->layout));
120 snprintf(dev_part_str, sizeof(dev_part_str), "%d:%d",
121 dfu->data.mmc.dev, dfu->data.mmc.part);
123 ret = fs_set_blk_dev("mmc", dev_part_str, fstype);
125 puts("dfu: fs_set_blk_dev error!\n");
131 ret = fs_read(dfu->name, (size_t)buf, offset, *len, &size);
133 puts("dfu: fs_read error!\n");
139 ret = fs_write(dfu->name, (size_t)buf, offset, *len, &size);
141 puts("dfu: fs_write error!\n");
146 ret = fs_size(dfu->name, &size);
148 puts("dfu: fs_size error!\n");
160 static int mmc_file_buf_write(struct dfu_entity *dfu, u64 offset, void *buf, long *len)
165 dfu_file_buf_len = 0;
166 dfu_file_buf_offset = 0;
169 /* Add to the current buffer. */
170 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
171 *len = CONFIG_SYS_DFU_MAX_FILE_SIZE - dfu_file_buf_len;
172 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
173 dfu_file_buf_len += *len;
175 if (dfu_file_buf_len == CONFIG_SYS_DFU_MAX_FILE_SIZE) {
176 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
177 dfu_file_buf, &dfu_file_buf_len);
178 dfu_file_buf_offset += dfu_file_buf_len;
179 dfu_file_buf_len = 0;
185 static int mmc_file_buf_write_finish(struct dfu_entity *dfu)
187 int ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf_offset,
188 dfu_file_buf, &dfu_file_buf_len);
190 /* Now that we're done */
191 dfu_file_buf_len = 0;
192 dfu_file_buf_offset = 0;
197 int dfu_write_medium_mmc(struct dfu_entity *dfu,
198 u64 offset, void *buf, long *len)
202 switch (dfu->layout) {
204 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
208 ret = mmc_file_buf_write(dfu, offset, buf, len);
211 ret = run_command_list(buf, *len, 0);
217 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
218 dfu_get_layout(dfu->layout));
224 int dfu_flush_medium_mmc(struct dfu_entity *dfu)
228 switch (dfu->layout) {
231 ret = mmc_file_buf_write_finish(dfu);
234 /* script may have changed the dfu_alt_info */
235 dfu_reinit_needed = true;
241 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
242 dfu_get_layout(dfu->layout));
248 int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size)
252 switch (dfu->layout) {
254 *size = dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
258 ret = mmc_file_op(DFU_OP_SIZE, dfu, 0, NULL, size);
266 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
267 dfu_get_layout(dfu->layout));
273 static int mmc_file_buf_read(struct dfu_entity *dfu, u64 offset, void *buf,
278 if (offset == 0 || offset >= dfu_file_buf_offset + dfu_file_buf_len ||
279 offset + *len < dfu_file_buf_offset) {
280 u64 file_len = CONFIG_SYS_DFU_MAX_FILE_SIZE;
282 ret = mmc_file_op(DFU_OP_READ, dfu, offset, dfu_file_buf,
286 dfu_file_buf_len = file_len;
287 dfu_file_buf_offset = offset;
289 if (offset + *len > dfu_file_buf_offset + dfu_file_buf_len)
292 /* Add to the current buffer. */
293 memcpy(buf, dfu_file_buf + offset - dfu_file_buf_offset, *len);
298 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
303 switch (dfu->layout) {
305 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
309 ret = mmc_file_buf_read(dfu, offset, buf, len);
312 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
313 dfu_get_layout(dfu->layout));
319 void dfu_free_entity_mmc(struct dfu_entity *dfu)
328 * @param s Parameter string containing space-separated arguments:
330 * raw (raw read/write)
333 * part (partition image)
335 * lba_start and lba_size, for raw write
336 * mmc_dev and mmc_part, for filesystems and part
338 * mmcpart <num> (access to HW eMMC partitions)
340 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char **argv, int argc)
342 const char *entity_type;
349 pr_err("The number of parameters are not enough.\n");
353 dfu->data.mmc.dev_num = dectoul(devstr, &s);
357 entity_type = argv[0];
359 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
362 second_arg = simple_strtol(argv[1], &s, 0);
365 third_arg = simple_strtoul(argv[2], &s, 0);
369 mmc = find_mmc_device(dfu->data.mmc.dev_num);
371 pr_err("Couldn't find MMC device no. %d.\n",
372 dfu->data.mmc.dev_num);
377 pr_err("Couldn't init MMC device.\n");
381 dfu->data.mmc.hw_partition = -EINVAL;
382 if (!strcmp(entity_type, "raw")) {
383 dfu->layout = DFU_RAW_ADDR;
384 dfu->data.mmc.lba_start = second_arg;
385 dfu->data.mmc.lba_size = third_arg;
386 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
389 * In case the size is zero (i.e. mmc raw 0x10 0),
390 * assume the user intends to use whole device.
392 if (third_arg == 0) {
393 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
395 dfu->data.mmc.lba_size = blk_dev->lba;
399 * Check for an extra entry at dfu_alt_info env variable
400 * specifying the mmc HW defined partition number
403 if (argc != 5 || strcmp(argv[3], "mmcpart")) {
404 pr_err("DFU mmc raw accept 'mmcpart <partnum>' option.\n");
407 dfu->data.mmc.hw_partition =
408 simple_strtoul(argv[4], NULL, 0);
411 } else if (!strcmp(entity_type, "part")) {
412 struct disk_partition partinfo;
413 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
414 int mmcdev = second_arg;
415 int mmcpart = third_arg;
418 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
419 pr_err("Couldn't find part #%d on mmc device #%d\n",
425 * Check for an extra entry at dfu_alt_info env variable
426 * specifying the mmc HW defined partition number
429 if (argc != 5 || strcmp(argv[3], "offset")) {
430 pr_err("DFU mmc raw accept 'mmcpart <partnum>' option.\n");
433 dfu->data.mmc.hw_partition =
434 simple_strtoul(argv[4], NULL, 0);
437 dfu->layout = DFU_RAW_ADDR;
438 dfu->data.mmc.lba_start = partinfo.start + offset;
439 dfu->data.mmc.lba_size = partinfo.size - offset;
440 dfu->data.mmc.lba_blk_size = partinfo.blksz;
441 } else if (!strcmp(entity_type, "fat")) {
442 dfu->layout = DFU_FS_FAT;
443 } else if (!strcmp(entity_type, "ext4")) {
444 dfu->layout = DFU_FS_EXT4;
445 } else if (!strcmp(entity_type, "skip")) {
446 dfu->layout = DFU_SKIP;
447 } else if (!strcmp(entity_type, "script")) {
448 dfu->layout = DFU_SCRIPT;
450 pr_err("Memory layout (%s) not supported!\n", entity_type);
454 /* if it's NOT a raw write */
455 if (strcmp(entity_type, "raw")) {
456 dfu->data.mmc.dev = (second_arg != -1) ? second_arg :
457 dfu->data.mmc.dev_num;
458 dfu->data.mmc.part = third_arg;
461 dfu->dev_type = DFU_DEV_MMC;
462 dfu->get_medium_size = dfu_get_medium_size_mmc;
463 dfu->read_medium = dfu_read_medium_mmc;
464 dfu->write_medium = dfu_write_medium_mmc;
465 dfu->flush_medium = dfu_flush_medium_mmc;
467 dfu->free_entity = dfu_free_entity_mmc;
469 /* Check if file buffer is ready */
471 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
472 CONFIG_SYS_DFU_MAX_FILE_SIZE);
474 pr_err("Could not memalign 0x%x bytes",
475 CONFIG_SYS_DFU_MAX_FILE_SIZE);