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