Lukasz Majewski | cb383cd | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <malloc.h> |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 13 | #include <div64.h> |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 14 | #include <dfu.h> |
Łukasz Majewski | 7d0b605 | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 15 | #include <mmc.h> |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 16 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 17 | static unsigned char __aligned(CONFIG_SYS_CACHELINE_SIZE) |
| 18 | dfu_file_buf[CONFIG_SYS_DFU_MAX_FILE_SIZE]; |
| 19 | static long dfu_file_buf_len; |
| 20 | |
Lukasz Majewski | c8151b4 | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 21 | static int mmc_access_part(struct dfu_entity *dfu, struct mmc *mmc, int part) |
| 22 | { |
| 23 | int ret; |
| 24 | |
| 25 | if (part == mmc->part_num) |
| 26 | return 0; |
| 27 | |
| 28 | ret = mmc_switch_part(dfu->dev_num, part); |
| 29 | if (ret) { |
| 30 | error("Cannot switch to partition %d\n", part); |
| 31 | return ret; |
| 32 | } |
| 33 | mmc->part_num = part; |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
Afzal Mohammed | 5a127c8 | 2013-09-18 01:14:50 +0530 | [diff] [blame] | 38 | static int mmc_block_op(enum dfu_op op, struct dfu_entity *dfu, |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 39 | u64 offset, void *buf, long *len) |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 40 | { |
Łukasz Majewski | 7d0b605 | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 41 | struct mmc *mmc = find_mmc_device(dfu->dev_num); |
| 42 | u32 blk_start, blk_count, n = 0; |
Lukasz Majewski | c8151b4 | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 43 | int ret, part_num_bkp = 0; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 44 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 45 | /* |
| 46 | * We must ensure that we work in lba_blk_size chunks, so ALIGN |
| 47 | * this value. |
| 48 | */ |
| 49 | *len = ALIGN(*len, dfu->data.mmc.lba_blk_size); |
| 50 | |
| 51 | blk_start = dfu->data.mmc.lba_start + |
| 52 | (u32)lldiv(offset, dfu->data.mmc.lba_blk_size); |
| 53 | blk_count = *len / dfu->data.mmc.lba_blk_size; |
| 54 | if (blk_start + blk_count > |
| 55 | dfu->data.mmc.lba_start + dfu->data.mmc.lba_size) { |
| 56 | puts("Request would exceed designated area!\n"); |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
Lukasz Majewski | c8151b4 | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 60 | if (dfu->data.mmc.hw_partition >= 0) { |
| 61 | part_num_bkp = mmc->part_num; |
| 62 | ret = mmc_access_part(dfu, mmc, dfu->data.mmc.hw_partition); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | } |
| 66 | |
Łukasz Majewski | 7d0b605 | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 67 | debug("%s: %s dev: %d start: %d cnt: %d buf: 0x%p\n", __func__, |
| 68 | op == DFU_OP_READ ? "MMC READ" : "MMC WRITE", dfu->dev_num, |
| 69 | blk_start, blk_count, buf); |
| 70 | switch (op) { |
| 71 | case DFU_OP_READ: |
| 72 | n = mmc->block_dev.block_read(dfu->dev_num, blk_start, |
| 73 | blk_count, buf); |
| 74 | break; |
| 75 | case DFU_OP_WRITE: |
| 76 | n = mmc->block_dev.block_write(dfu->dev_num, blk_start, |
| 77 | blk_count, buf); |
| 78 | break; |
| 79 | default: |
| 80 | error("Operation not supported\n"); |
| 81 | } |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 82 | |
Łukasz Majewski | 7d0b605 | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 83 | if (n != blk_count) { |
| 84 | error("MMC operation failed"); |
Lukasz Majewski | c8151b4 | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 85 | if (dfu->data.mmc.hw_partition >= 0) |
| 86 | mmc_access_part(dfu, mmc, part_num_bkp); |
Łukasz Majewski | 7d0b605 | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 87 | return -EIO; |
| 88 | } |
| 89 | |
Lukasz Majewski | c8151b4 | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 90 | if (dfu->data.mmc.hw_partition >= 0) { |
| 91 | ret = mmc_access_part(dfu, mmc, part_num_bkp); |
| 92 | if (ret) |
| 93 | return ret; |
| 94 | } |
| 95 | |
Łukasz Majewski | 7d0b605 | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 96 | return 0; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 99 | static int mmc_file_buffer(struct dfu_entity *dfu, void *buf, long *len) |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 100 | { |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 101 | if (dfu_file_buf_len + *len > CONFIG_SYS_DFU_MAX_FILE_SIZE) { |
| 102 | dfu_file_buf_len = 0; |
| 103 | return -EINVAL; |
| 104 | } |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 105 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 106 | /* Add to the current buffer. */ |
| 107 | memcpy(dfu_file_buf + dfu_file_buf_len, buf, *len); |
| 108 | dfu_file_buf_len += *len; |
| 109 | |
| 110 | return 0; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 111 | } |
| 112 | |
Afzal Mohammed | 5a127c8 | 2013-09-18 01:14:50 +0530 | [diff] [blame] | 113 | static int mmc_file_op(enum dfu_op op, struct dfu_entity *dfu, |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 114 | void *buf, long *len) |
| 115 | { |
| 116 | char cmd_buf[DFU_CMD_BUF_SIZE]; |
| 117 | char *str_env; |
| 118 | int ret; |
| 119 | |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 120 | switch (dfu->layout) { |
| 121 | case DFU_FS_FAT: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 122 | sprintf(cmd_buf, "fat%s mmc %d:%d 0x%x %s", |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 123 | op == DFU_OP_READ ? "load" : "write", |
| 124 | dfu->data.mmc.dev, dfu->data.mmc.part, |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 125 | (unsigned int) buf, dfu->name); |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 126 | break; |
| 127 | case DFU_FS_EXT4: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 128 | sprintf(cmd_buf, "ext4%s mmc %d:%d 0x%x /%s", |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 129 | op == DFU_OP_READ ? "load" : "write", |
| 130 | dfu->data.mmc.dev, dfu->data.mmc.part, |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 131 | (unsigned int) buf, dfu->name); |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 132 | break; |
| 133 | default: |
| 134 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 135 | dfu_get_layout(dfu->layout)); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 136 | return -1; |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 137 | } |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 138 | |
Lukasz Majewski | 17eb1d8 | 2014-02-20 10:29:18 +0100 | [diff] [blame] | 139 | if (op == DFU_OP_WRITE) |
| 140 | sprintf(cmd_buf + strlen(cmd_buf), " %lx", *len); |
| 141 | |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 142 | debug("%s: %s 0x%p\n", __func__, cmd_buf, cmd_buf); |
| 143 | |
| 144 | ret = run_command(cmd_buf, 0); |
| 145 | if (ret) { |
| 146 | puts("dfu: Read error!\n"); |
| 147 | return ret; |
| 148 | } |
| 149 | |
Łukasz Majewski | 81c1d7b | 2012-08-23 23:33:56 +0000 | [diff] [blame] | 150 | if (dfu->layout != DFU_RAW_ADDR && op == DFU_OP_READ) { |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 151 | str_env = getenv("filesize"); |
| 152 | if (str_env == NULL) { |
| 153 | puts("dfu: Wrong file size!\n"); |
| 154 | return -1; |
| 155 | } |
| 156 | *len = simple_strtoul(str_env, NULL, 16); |
| 157 | } |
| 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 162 | int dfu_write_medium_mmc(struct dfu_entity *dfu, |
| 163 | u64 offset, void *buf, long *len) |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 164 | { |
| 165 | int ret = -1; |
| 166 | |
| 167 | switch (dfu->layout) { |
| 168 | case DFU_RAW_ADDR: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 169 | ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 170 | break; |
| 171 | case DFU_FS_FAT: |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 172 | case DFU_FS_EXT4: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 173 | ret = mmc_file_buffer(dfu, buf, len); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 174 | break; |
| 175 | default: |
| 176 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 177 | dfu_get_layout(dfu->layout)); |
| 178 | } |
| 179 | |
| 180 | return ret; |
| 181 | } |
| 182 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 183 | int dfu_flush_medium_mmc(struct dfu_entity *dfu) |
| 184 | { |
| 185 | int ret = 0; |
| 186 | |
| 187 | if (dfu->layout != DFU_RAW_ADDR) { |
| 188 | /* Do stuff here. */ |
| 189 | ret = mmc_file_op(DFU_OP_WRITE, dfu, &dfu_file_buf, |
| 190 | &dfu_file_buf_len); |
| 191 | |
| 192 | /* Now that we're done */ |
| 193 | dfu_file_buf_len = 0; |
| 194 | } |
| 195 | |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf, |
| 200 | long *len) |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 201 | { |
| 202 | int ret = -1; |
| 203 | |
| 204 | switch (dfu->layout) { |
| 205 | case DFU_RAW_ADDR: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 206 | ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 207 | break; |
| 208 | case DFU_FS_FAT: |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 209 | case DFU_FS_EXT4: |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 210 | ret = mmc_file_op(DFU_OP_READ, dfu, buf, len); |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 211 | break; |
| 212 | default: |
| 213 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 214 | dfu_get_layout(dfu->layout)); |
| 215 | } |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 220 | /* |
| 221 | * @param s Parameter string containing space-separated arguments: |
| 222 | * 1st: |
| 223 | * raw (raw read/write) |
| 224 | * fat (files) |
| 225 | * ext4 (^) |
| 226 | * part (partition image) |
| 227 | * 2nd and 3rd: |
| 228 | * lba_start and lba_size, for raw write |
| 229 | * mmc_dev and mmc_part, for filesystems and part |
Lukasz Majewski | c8151b4 | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 230 | * 4th (optional): |
| 231 | * mmcpart <num> (access to HW eMMC partitions) |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 232 | */ |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 233 | int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *s) |
| 234 | { |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 235 | const char *entity_type; |
| 236 | size_t second_arg; |
| 237 | size_t third_arg; |
| 238 | |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 239 | struct mmc *mmc; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 240 | |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 241 | const char *argv[3]; |
| 242 | const char **parg = argv; |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 243 | |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 244 | for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) { |
| 245 | *parg = strsep(&s, " "); |
| 246 | if (*parg == NULL) { |
| 247 | error("Invalid number of arguments.\n"); |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 248 | return -ENODEV; |
| 249 | } |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 250 | } |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 251 | |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 252 | entity_type = argv[0]; |
Mateusz Zalega | b7d4259 | 2014-04-28 21:13:25 +0200 | [diff] [blame] | 253 | /* |
| 254 | * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8, |
| 255 | * with default 10. |
| 256 | */ |
| 257 | second_arg = simple_strtoul(argv[1], NULL, 0); |
| 258 | third_arg = simple_strtoul(argv[2], NULL, 0); |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 259 | |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 260 | mmc = find_mmc_device(dfu->dev_num); |
| 261 | if (mmc == NULL) { |
| 262 | error("Couldn't find MMC device no. %d.\n", dfu->dev_num); |
Pantelis Antoniou | 1b6ca18 | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 263 | return -ENODEV; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 266 | if (mmc_init(mmc)) { |
| 267 | error("Couldn't init MMC device.\n"); |
| 268 | return -ENODEV; |
Łukasz Majewski | 43e6627 | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Lukasz Majewski | c8151b4 | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 271 | dfu->data.mmc.hw_partition = -EINVAL; |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 272 | if (!strcmp(entity_type, "raw")) { |
| 273 | dfu->layout = DFU_RAW_ADDR; |
| 274 | dfu->data.mmc.lba_start = second_arg; |
| 275 | dfu->data.mmc.lba_size = third_arg; |
| 276 | dfu->data.mmc.lba_blk_size = mmc->read_bl_len; |
Lukasz Majewski | c8151b4 | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 277 | |
| 278 | /* |
| 279 | * Check for an extra entry at dfu_alt_info env variable |
| 280 | * specifying the mmc HW defined partition number |
| 281 | */ |
| 282 | if (s) |
| 283 | if (!strcmp(strsep(&s, " "), "mmcpart")) |
| 284 | dfu->data.mmc.hw_partition = |
| 285 | simple_strtoul(s, NULL, 0); |
| 286 | |
Mateusz Zalega | 711b931 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 287 | } else if (!strcmp(entity_type, "part")) { |
| 288 | disk_partition_t partinfo; |
| 289 | block_dev_desc_t *blk_dev = &mmc->block_dev; |
| 290 | int mmcdev = second_arg; |
| 291 | int mmcpart = third_arg; |
| 292 | |
| 293 | if (get_partition_info(blk_dev, mmcpart, &partinfo) != 0) { |
| 294 | error("Couldn't find part #%d on mmc device #%d\n", |
| 295 | mmcpart, mmcdev); |
| 296 | return -ENODEV; |
| 297 | } |
| 298 | |
| 299 | dfu->layout = DFU_RAW_ADDR; |
| 300 | dfu->data.mmc.lba_start = partinfo.start; |
| 301 | dfu->data.mmc.lba_size = partinfo.size; |
| 302 | dfu->data.mmc.lba_blk_size = partinfo.blksz; |
| 303 | } else if (!strcmp(entity_type, "fat")) { |
| 304 | dfu->layout = DFU_FS_FAT; |
| 305 | } else if (!strcmp(entity_type, "ext4")) { |
| 306 | dfu->layout = DFU_FS_EXT4; |
| 307 | } else { |
| 308 | error("Memory layout (%s) not supported!\n", entity_type); |
| 309 | return -ENODEV; |
| 310 | } |
| 311 | |
| 312 | /* if it's NOT a raw write */ |
| 313 | if (strcmp(entity_type, "raw")) { |
| 314 | dfu->data.mmc.dev = second_arg; |
| 315 | dfu->data.mmc.part = third_arg; |
| 316 | } |
| 317 | |
| 318 | dfu->dev_type = DFU_DEV_MMC; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 319 | dfu->read_medium = dfu_read_medium_mmc; |
| 320 | dfu->write_medium = dfu_write_medium_mmc; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 321 | dfu->flush_medium = dfu_flush_medium_mmc; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 322 | dfu->inited = 0; |
Lukasz Majewski | cb383cd | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 323 | |
| 324 | return 0; |
| 325 | } |