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+
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_block_op(enum dfu_op op, struct dfu_entity *dfu,
24 u64 offset, void *buf, long *len)
27 u32 blk_start, blk_count, n = 0;
28 int ret, part_num_bkp = 0;
30 mmc = find_mmc_device(dfu->data.mmc.dev_num);
32 error("Device MMC %d - not found!", dfu->data.mmc.dev_num);
37 * We must ensure that we work in lba_blk_size chunks, so ALIGN
40 *len = ALIGN(*len, dfu->data.mmc.lba_blk_size);
42 blk_start = dfu->data.mmc.lba_start +
43 (u32)lldiv(offset, dfu->data.mmc.lba_blk_size);
44 blk_count = *len / dfu->data.mmc.lba_blk_size;
45 if (blk_start + blk_count >
46 dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) {
47 puts("Request would exceed designated area!\n");
51 if (dfu->data.mmc.hw_partition >= 0) {
52 part_num_bkp = mmc->block_dev.hwpart;
53 ret = mmc_select_hwpart(dfu->data.mmc.dev_num,
54 dfu->data.mmc.hw_partition);
59 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
60 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
61 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
64 n = mmc->block_dev.block_read(&mmc->block_dev, blk_start,
68 n = mmc->block_dev.block_write(&mmc->block_dev, blk_start,
72 error("Operation not supported\n");
76 error("MMC operation failed");
77 if (dfu->data.mmc.hw_partition >= 0)
78 mmc_select_hwpart(dfu->data.mmc.dev_num, part_num_bkp);
82 if (dfu->data.mmc.hw_partition >= 0) {
83 ret = mmc_select_hwpart(dfu->data.mmc.dev_num, part_num_bkp);
91 static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
93 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
98 /* Add to the current buffer. */
99 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
100 dfu_file_buf_len += *len;
105 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
106 void *buf, long *len)
108 const char *fsname, *opname;
109 char cmd_buf[DFU_CMD_BUF_SIZE];
113 switch (dfu->layout) {
121 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
122 dfu_get_layout(dfu->layout));
140 sprintf(cmd_buf, "%s%s mmc %d:%d", fsname, opname,
141 dfu->data.mmc.dev, dfu->data.mmc.part);
143 if (op != DFU_OP_SIZE)
144 sprintf(cmd_buf + strlen(cmd_buf), " %p", buf);
146 sprintf(cmd_buf + strlen(cmd_buf), " %s", dfu->name);
148 if (op == DFU_OP_WRITE)
149 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
151 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
153 ret = run_command(cmd_buf, 0);
155 puts("dfu: Read error!\n");
159 if (op != DFU_OP_WRITE) {
160 str_env = getenv("filesize");
161 if (str_env == NULL) {
162 puts("dfu: Wrong file size!\n");
165 *len = simple_strtoul(str_env, NULL, 16);
171 int dfu_write_medium_mmc(struct dfu_entity *dfu,
172 u64 offset, void *buf, long *len)
176 switch (dfu->layout) {
178 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
182 ret = mmc_file_buffer(dfu, buf, len);
185 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
186 dfu_get_layout(dfu->layout));
192 int dfu_flush_medium_mmc(struct dfu_entity *dfu)
196 if (dfu->layout != DFU_RAW_ADDR) {
198 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf,
201 /* Now that we're done */
202 dfu_file_buf_len = 0;
208 long dfu_get_medium_size_mmc(struct dfu_entity *dfu)
213 switch (dfu->layout) {
215 return dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
218 dfu_file_buf_filled = -1;
219 ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, &len);
222 if (len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
226 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
227 dfu_get_layout(dfu->layout));
232 static int mmc_file_unbuffer(struct dfu_entity *dfu, u64 offset, void *buf,
238 if (dfu_file_buf_filled == -1) {
239 ret = mmc_file_op(DFU_OP_READ, dfu, dfu_file_buf, &file_len);
242 dfu_file_buf_filled = file_len;
244 if (offset + *len > dfu_file_buf_filled)
247 /* Add to the current buffer. */
248 memcpy(buf, dfu_file_buf + offset, *len);
253 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
258 switch (dfu->layout) {
260 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
264 ret = mmc_file_unbuffer(dfu, offset, buf, len);
267 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
268 dfu_get_layout(dfu->layout));
274 void dfu_free_entity_mmc(struct dfu_entity *dfu)
283 * @param s Parameter string containing space-separated arguments:
285 * raw (raw read/write)
288 * part (partition image)
290 * lba_start and lba_size, for raw write
291 * mmc_dev and mmc_part, for filesystems and part
293 * mmcpart <num> (access to HW eMMC partitions)
295 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
297 const char *entity_type;
304 const char **parg = argv;
306 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
308 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
309 *parg = strsep(&s, " ");
311 error("Invalid number of arguments.\n");
316 entity_type = argv[0];
318 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
321 second_arg = simple_strtoul(argv[1], NULL, 0);
322 third_arg = simple_strtoul(argv[2], NULL, 0);
324 mmc = find_mmc_device(dfu->data.mmc.dev_num);
326 error("Couldn't find MMC device no. %d.\n",
327 dfu->data.mmc.dev_num);
332 error("Couldn't init MMC device.\n");
336 dfu->data.mmc.hw_partition = -EINVAL;
337 if (!strcmp(entity_type, "raw")) {
338 dfu->layout = DFU_RAW_ADDR;
339 dfu->data.mmc.lba_start = second_arg;
340 dfu->data.mmc.lba_size = third_arg;
341 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
344 * Check for an extra entry at dfu_alt_info env variable
345 * specifying the mmc HW defined partition number
348 if (!strcmp(strsep(&s, " "), "mmcpart"))
349 dfu->data.mmc.hw_partition =
350 simple_strtoul(s, NULL, 0);
352 } else if (!strcmp(entity_type, "part")) {
353 disk_partition_t partinfo;
354 struct blk_desc *blk_dev = &mmc->block_dev;
355 int mmcdev = second_arg;
356 int mmcpart = third_arg;
358 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
359 error("Couldn't find part #%d on mmc device #%d\n",
364 dfu->layout = DFU_RAW_ADDR;
365 dfu->data.mmc.lba_start = partinfo.start;
366 dfu->data.mmc.lba_size = partinfo.size;
367 dfu->data.mmc.lba_blk_size = partinfo.blksz;
368 } else if (!strcmp(entity_type, "fat")) {
369 dfu->layout = DFU_FS_FAT;
370 } else if (!strcmp(entity_type, "ext4")) {
371 dfu->layout = DFU_FS_EXT4;
373 error("Memory layout (%s) not supported!\n", entity_type);
377 /* if it's NOT a raw write */
378 if (strcmp(entity_type, "raw")) {
379 dfu->data.mmc.dev = second_arg;
380 dfu->data.mmc.part = third_arg;
383 dfu->dev_type = DFU_DEV_MMC;
384 dfu->get_medium_size = dfu_get_medium_size_mmc;
385 dfu->read_medium = dfu_read_medium_mmc;
386 dfu->write_medium = dfu_write_medium_mmc;
387 dfu->flush_medium = dfu_flush_medium_mmc;
389 dfu->free_entity = dfu_free_entity_mmc;
391 /* Check if file buffer is ready */
393 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
394 CONFIG_SYS_DFU_MAX_FILE_SIZE);
396 error("Could not memalign 0x%x bytes",
397 CONFIG_SYS_DFU_MAX_FILE_SIZE);