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