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