Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * dfu.c -- DFU back-end routines |
| 3 | * |
| 4 | * Copyright (C) 2012 Samsung Electronics |
| 5 | * author: Lukasz Majewski <l.majewski@samsung.com> |
| 6 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <malloc.h> |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 13 | #include <div64.h> |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 14 | #include <dfu.h> |
| 15 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 16 | static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE) |
| 17 | dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE]; |
| 18 | static long dfu_file_buf_len; |
| 19 | |
Afzal Mohammed | b9a4a6b | 2013-09-18 01:14:50 +0530 | [diff] [blame^] | 20 | static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu, |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 21 | u64 offset, void *buf, long *len) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 22 | { |
| 23 | char cmd_buf[DFU_CMD_BUF_SIZE]; |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 24 | u32 blk_start, blk_count; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 25 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 26 | /* |
| 27 | * We must ensure that we work in lba_blk_size chunks, so ALIGN |
| 28 | * this value. |
| 29 | */ |
| 30 | *len = ALIGN(*len, dfu->data.mmc.lba_blk_size); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 31 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 32 | blk_start = dfu->data.mmc.lba_start + |
| 33 | (u32)lldiv(offset, dfu->data.mmc.lba_blk_size); |
| 34 | blk_count = *len / dfu->data.mmc.lba_blk_size; |
| 35 | if (blk_start + blk_count > |
| 36 | dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) { |
| 37 | puts("Request would exceed designated area!\n"); |
| 38 | return -EINVAL; |
| 39 | } |
| 40 | |
| 41 | sprintf(cmd_buf, "mmc %s %p %x %x", |
| 42 | op == DFU_OP_READ ? "read" : "write", |
| 43 | buf, blk_start, blk_count); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 44 | |
| 45 | debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); |
| 46 | return run_command(cmd_buf, 0); |
| 47 | } |
| 48 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 49 | static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 50 | { |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 51 | if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) { |
| 52 | dfu_file_buf_len = 0; |
| 53 | return -EINVAL; |
| 54 | } |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 55 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 56 | /* Add to the current buffer. */ |
| 57 | memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len); |
| 58 | dfu_file_buf_len += *len; |
| 59 | |
| 60 | return 0; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Afzal Mohammed | b9a4a6b | 2013-09-18 01:14:50 +0530 | [diff] [blame^] | 63 | static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu, |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 64 | void *buf, long *len) |
| 65 | { |
| 66 | char cmd_buf[DFU_CMD_BUF_SIZE]; |
| 67 | char *str_env; |
| 68 | int ret; |
| 69 | |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 70 | switch (dfu->layout) { |
| 71 | case DFU_FS_FAT: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 72 | sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s", |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 73 | op == DFU_OP_READ ? "load" : "write", |
| 74 | dfu->data.mmc.dev, dfu->data.mmc.part, |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 75 | (unsigned int) buf, dfu->name); |
| 76 | if (op == DFU_OP_WRITE) |
| 77 | sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len); |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 78 | break; |
| 79 | case DFU_FS_EXT4: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 80 | sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s", |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 81 | op == DFU_OP_READ ? "load" : "write", |
| 82 | dfu->data.mmc.dev, dfu->data.mmc.part, |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 83 | (unsigned int) buf, dfu->name); |
Łukasz Majewski | 3cc5ff0 | 2013-06-28 18:41:49 +0200 | [diff] [blame] | 84 | if (op == DFU_OP_WRITE) |
| 85 | sprintf(cmd_buf + strlen(cmd_buf), " %ld", *len); |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 86 | break; |
| 87 | default: |
| 88 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 89 | dfu_get_layout(dfu->layout)); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 90 | return -1; |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 91 | } |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 92 | |
| 93 | debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); |
| 94 | |
| 95 | ret = run_command(cmd_buf, 0); |
| 96 | if (ret) { |
| 97 | puts("dfu: Read error!\n"); |
| 98 | return ret; |
| 99 | } |
| 100 | |
Łukasz Majewski | 180e3dd | 2012-08-23 23:33:56 +0000 | [diff] [blame] | 101 | if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) { |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 102 | str_env = getenv("filesize"); |
| 103 | if (str_env == NULL) { |
| 104 | puts("dfu: Wrong file size!\n"); |
| 105 | return -1; |
| 106 | } |
| 107 | *len = simple_strtoul(str_env, NULL, 16); |
| 108 | } |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 113 | int dfu_write_medium_mmc(struct dfu_entity *dfu, |
| 114 | u64 offset, void *buf, long *len) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 115 | { |
| 116 | int ret = -1; |
| 117 | |
| 118 | switch (dfu->layout) { |
| 119 | case DFU_RAW_ADDR: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 120 | ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 121 | break; |
| 122 | case DFU_FS_FAT: |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 123 | case DFU_FS_EXT4: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 124 | ret = mmc_file_buffer(dfu, buf, len); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 125 | break; |
| 126 | default: |
| 127 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 128 | dfu_get_layout(dfu->layout)); |
| 129 | } |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 134 | int dfu_flush_medium_mmc(struct dfu_entity *dfu) |
| 135 | { |
| 136 | int ret = 0; |
| 137 | |
| 138 | if (dfu->layout != DFU_RAW_ADDR) { |
| 139 | /* Do stuff here. */ |
| 140 | ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf, |
| 141 | &dfu_file_buf_len); |
| 142 | |
| 143 | /* Now that we're done */ |
| 144 | dfu_file_buf_len = 0; |
| 145 | } |
| 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf, |
| 151 | long *len) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 152 | { |
| 153 | int ret = -1; |
| 154 | |
| 155 | switch (dfu->layout) { |
| 156 | case DFU_RAW_ADDR: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 157 | ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 158 | break; |
| 159 | case DFU_FS_FAT: |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 160 | case DFU_FS_EXT4: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 161 | ret = mmc_file_op(DFU_OP_READ, dfu, buf, len); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 162 | break; |
| 163 | default: |
| 164 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 165 | dfu_get_layout(dfu->layout)); |
| 166 | } |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) |
| 172 | { |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 173 | int dev, part; |
| 174 | struct mmc *mmc; |
| 175 | block_dev_desc_t *blk_dev; |
| 176 | disk_partition_t partinfo; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 177 | char *st; |
| 178 | |
| 179 | dfu->dev_type = DFU_DEV_MMC; |
| 180 | st = strsep(&s, " "); |
| 181 | if (!strcmp(st, "mmc")) { |
| 182 | dfu->layout = DFU_RAW_ADDR; |
| 183 | dfu->data.mmc.lba_start = simple_strtoul(s, &s, 16); |
| 184 | dfu->data.mmc.lba_size = simple_strtoul(++s, &s, 16); |
| 185 | dfu->data.mmc.lba_blk_size = get_mmc_blk_size(dfu->dev_num); |
| 186 | } else if (!strcmp(st, "fat")) { |
| 187 | dfu->layout = DFU_FS_FAT; |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 188 | } else if (!strcmp(st, "ext4")) { |
| 189 | dfu->layout = DFU_FS_EXT4; |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 190 | } else if (!strcmp(st, "part")) { |
| 191 | |
| 192 | dfu->layout = DFU_RAW_ADDR; |
| 193 | |
| 194 | dev = simple_strtoul(s, &s, 10); |
| 195 | s++; |
| 196 | part = simple_strtoul(s, &s, 10); |
| 197 | |
| 198 | mmc = find_mmc_device(dev); |
| 199 | if (mmc == NULL || mmc_init(mmc)) { |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 200 | printf("%s: could not find mmc device #%d!\n", |
| 201 | __func__, dev); |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 202 | return -ENODEV; |
| 203 | } |
| 204 | |
| 205 | blk_dev = &mmc->block_dev; |
| 206 | if (get_partition_info(blk_dev, part, &partinfo) != 0) { |
| 207 | printf("%s: could not find partition #%d on mmc device #%d!\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 208 | __func__, part, dev); |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 209 | return -ENODEV; |
| 210 | } |
| 211 | |
| 212 | dfu->data.mmc.lba_start = partinfo.start; |
| 213 | dfu->data.mmc.lba_size = partinfo.size; |
| 214 | dfu->data.mmc.lba_blk_size = partinfo.blksz; |
| 215 | |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 216 | } else { |
| 217 | printf("%s: Memory layout (%s) not supported!\n", __func__, st); |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 218 | return -ENODEV; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 221 | if (dfu->layout == DFU_FS_EXT4 || dfu->layout == DFU_FS_FAT) { |
| 222 | dfu->data.mmc.dev = simple_strtoul(s, &s, 10); |
| 223 | dfu->data.mmc.part = simple_strtoul(++s, &s, 10); |
| 224 | } |
| 225 | |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 226 | dfu->read_medium = dfu_read_medium_mmc; |
| 227 | dfu->write_medium = dfu_write_medium_mmc; |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 228 | dfu->flush_medium = dfu_flush_medium_mmc; |
| 229 | |
| 230 | /* initial state */ |
| 231 | dfu->inited = 0; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 232 | |
| 233 | return 0; |
| 234 | } |