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