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_get_blk_desc(mmc)->hwpart;
53 ret = blk_select_hwpart_devnum(IF_TYPE_MMC,
54 dfu->data.mmc.dev_num,
55 dfu->data.mmc.hw_partition);
60 debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__,
61 op == DFU_OP_READ ? "MMC READ" : "MMC WRITE",
62 dfu->data.mmc.dev_num, blk_start, blk_count, buf);
65 n = blk_dread(mmc_get_blk_desc(mmc), blk_start, blk_count, buf);
68 n = blk_dwrite(mmc_get_blk_desc(mmc), blk_start, blk_count,
72 error("Operation not supported\n");
76 error("MMC operation failed");
77 if (dfu->data.mmc.hw_partition >= 0)
78 blk_select_hwpart_devnum(IF_TYPE_MMC,
79 dfu->data.mmc.dev_num,
84 if (dfu->data.mmc.hw_partition >= 0) {
85 ret = blk_select_hwpart_devnum(IF_TYPE_MMC,
86 dfu->data.mmc.dev_num,
95 static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len)
97 if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) {
102 /* Add to the current buffer. */
103 memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len);
104 dfu_file_buf_len += *len;
109 static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu,
110 void *buf, long *len)
112 const char *fsname, *opname;
113 char cmd_buf[DFU_CMD_BUF_SIZE];
117 switch (dfu->layout) {
125 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
126 dfu_get_layout(dfu->layout));
144 sprintf(cmd_buf, "%s%s mmc %d:%d", fsname, opname,
145 dfu->data.mmc.dev, dfu->data.mmc.part);
147 if (op != DFU_OP_SIZE)
148 sprintf(cmd_buf + strlen(cmd_buf), " %p", buf);
150 sprintf(cmd_buf + strlen(cmd_buf), " %s", dfu->name);
152 if (op == DFU_OP_WRITE)
153 sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len);
155 debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf);
157 ret = run_command(cmd_buf, 0);
159 puts("dfu: Read error!\n");
163 if (op != DFU_OP_WRITE) {
164 str_env = getenv("filesize");
165 if (str_env == NULL) {
166 puts("dfu: Wrong file size!\n");
169 *len = simple_strtoul(str_env, NULL, 16);
175 int dfu_write_medium_mmc(struct dfu_entity *dfu,
176 u64 offset, void *buf, long *len)
180 switch (dfu->layout) {
182 ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
186 ret = mmc_file_buffer(dfu, buf, len);
189 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
190 dfu_get_layout(dfu->layout));
196 int dfu_flush_medium_mmc(struct dfu_entity *dfu)
200 if (dfu->layout != DFU_RAW_ADDR) {
202 ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf,
205 /* Now that we're done */
206 dfu_file_buf_len = 0;
212 long dfu_get_medium_size_mmc(struct dfu_entity *dfu)
217 switch (dfu->layout) {
219 return dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size;
222 dfu_file_buf_filled = -1;
223 ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, &len);
226 if (len > CONFIG_SYS_DFU_MAX_FILE_SIZE)
230 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
231 dfu_get_layout(dfu->layout));
236 static int mmc_file_unbuffer(struct dfu_entity *dfu, u64 offset, void *buf,
242 if (dfu_file_buf_filled == -1) {
243 ret = mmc_file_op(DFU_OP_READ, dfu, dfu_file_buf, &file_len);
246 dfu_file_buf_filled = file_len;
248 if (offset + *len > dfu_file_buf_filled)
251 /* Add to the current buffer. */
252 memcpy(buf, dfu_file_buf + offset, *len);
257 int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf,
262 switch (dfu->layout) {
264 ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len);
268 ret = mmc_file_unbuffer(dfu, offset, buf, len);
271 printf("%s: Layout (%s) not (yet) supported!\n", __func__,
272 dfu_get_layout(dfu->layout));
278 void dfu_free_entity_mmc(struct dfu_entity *dfu)
287 * @param s Parameter string containing space-separated arguments:
289 * raw (raw read/write)
292 * part (partition image)
294 * lba_start and lba_size, for raw write
295 * mmc_dev and mmc_part, for filesystems and part
297 * mmcpart <num> (access to HW eMMC partitions)
299 int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s)
301 const char *entity_type;
308 const char **parg = argv;
310 dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10);
312 for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) {
313 *parg = strsep(&s, " ");
315 error("Invalid number of arguments.\n");
320 entity_type = argv[0];
322 * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8,
325 second_arg = simple_strtoul(argv[1], NULL, 0);
326 third_arg = simple_strtoul(argv[2], NULL, 0);
328 mmc = find_mmc_device(dfu->data.mmc.dev_num);
330 error("Couldn't find MMC device no. %d.\n",
331 dfu->data.mmc.dev_num);
336 error("Couldn't init MMC device.\n");
340 dfu->data.mmc.hw_partition = -EINVAL;
341 if (!strcmp(entity_type, "raw")) {
342 dfu->layout = DFU_RAW_ADDR;
343 dfu->data.mmc.lba_start = second_arg;
344 dfu->data.mmc.lba_size = third_arg;
345 dfu->data.mmc.lba_blk_size = mmc->read_bl_len;
348 * Check for an extra entry at dfu_alt_info env variable
349 * specifying the mmc HW defined partition number
352 if (!strcmp(strsep(&s, " "), "mmcpart"))
353 dfu->data.mmc.hw_partition =
354 simple_strtoul(s, NULL, 0);
356 } else if (!strcmp(entity_type, "part")) {
357 disk_partition_t partinfo;
358 struct blk_desc *blk_dev = mmc_get_blk_desc(mmc);
359 int mmcdev = second_arg;
360 int mmcpart = third_arg;
362 if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) {
363 error("Couldn't find part #%d on mmc device #%d\n",
368 dfu->layout = DFU_RAW_ADDR;
369 dfu->data.mmc.lba_start = partinfo.start;
370 dfu->data.mmc.lba_size = partinfo.size;
371 dfu->data.mmc.lba_blk_size = partinfo.blksz;
372 } else if (!strcmp(entity_type, "fat")) {
373 dfu->layout = DFU_FS_FAT;
374 } else if (!strcmp(entity_type, "ext4")) {
375 dfu->layout = DFU_FS_EXT4;
377 error("Memory layout (%s) not supported!\n", entity_type);
381 /* if it's NOT a raw write */
382 if (strcmp(entity_type, "raw")) {
383 dfu->data.mmc.dev = second_arg;
384 dfu->data.mmc.part = third_arg;
387 dfu->dev_type = DFU_DEV_MMC;
388 dfu->get_medium_size = dfu_get_medium_size_mmc;
389 dfu->read_medium = dfu_read_medium_mmc;
390 dfu->write_medium = dfu_write_medium_mmc;
391 dfu->flush_medium = dfu_flush_medium_mmc;
393 dfu->free_entity = dfu_free_entity_mmc;
395 /* Check if file buffer is ready */
397 dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
398 CONFIG_SYS_DFU_MAX_FILE_SIZE);
400 error("Could not memalign 0x%x bytes",
401 CONFIG_SYS_DFU_MAX_FILE_SIZE);