Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | 34106b2 | 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 | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <malloc.h> |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 11 | #include <errno.h> |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 12 | #include <div64.h> |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 13 | #include <dfu.h> |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 14 | #include <ext4fs.h> |
| 15 | #include <fat.h> |
Łukasz Majewski | cf6d7db | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 16 | #include <mmc.h> |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 17 | |
Przemyslaw Marczak | 9d6d51a | 2015-03-04 14:01:23 +0100 | [diff] [blame] | 18 | static unsigned char *dfu_file_buf; |
Patrick Delaunay | f4e934f | 2017-07-19 16:39:23 +0200 | [diff] [blame] | 19 | static u64 dfu_file_buf_len; |
Stephen Warren | 7ae42bd | 2015-09-04 22:03:47 -0600 | [diff] [blame] | 20 | static long dfu_file_buf_filled; |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 21 | |
Afzal Mohammed | b9a4a6b | 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 | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 23 | u64 offset, void *buf, long *len) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 24 | { |
Przemyslaw Marczak | 95a2947 | 2014-12-15 10:34:09 +0100 | [diff] [blame] | 25 | struct mmc *mmc; |
Łukasz Majewski | cf6d7db | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 26 | u32 blk_start, blk_count, n = 0; |
Lukasz Majewski | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 27 | int ret, part_num_bkp = 0; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 28 | |
Przemyslaw Marczak | 95a2947 | 2014-12-15 10:34:09 +0100 | [diff] [blame] | 29 | mmc = find_mmc_device(dfu->data.mmc.dev_num); |
| 30 | if (!mmc) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 31 | pr_err("Device MMC %d - not found!", dfu->data.mmc.dev_num); |
Przemyslaw Marczak | 95a2947 | 2014-12-15 10:34:09 +0100 | [diff] [blame] | 32 | return -ENODEV; |
| 33 | } |
| 34 | |
Pantelis Antoniou | a6e788d | 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); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 40 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 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 | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 50 | if (dfu->data.mmc.hw_partition >= 0) { |
Simon Glass | aa7620b | 2016-06-12 23:30:33 -0600 | [diff] [blame] | 51 | part_num_bkp = mmc_get_blk_desc(mmc)->hwpart; |
Simon Glass | 11f2bb6 | 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 | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 55 | if (ret) |
| 56 | return ret; |
| 57 | } |
| 58 | |
Łukasz Majewski | cf6d7db | 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 | 4afe50f | 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 | cf6d7db | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 62 | switch (op) { |
| 63 | case DFU_OP_READ: |
Simon Glass | aa7620b | 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 | cf6d7db | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 65 | break; |
| 66 | case DFU_OP_WRITE: |
Simon Glass | aa7620b | 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 | cf6d7db | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 69 | break; |
| 70 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 71 | pr_err("Operation not supported\n"); |
Łukasz Majewski | cf6d7db | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (n != blk_count) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 75 | pr_err("MMC operation failed"); |
Lukasz Majewski | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 76 | if (dfu->data.mmc.hw_partition >= 0) |
Simon Glass | 11f2bb6 | 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 | cf6d7db | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 80 | return -EIO; |
| 81 | } |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 82 | |
Lukasz Majewski | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 83 | if (dfu->data.mmc.hw_partition >= 0) { |
Simon Glass | 11f2bb6 | 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 | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 87 | if (ret) |
| 88 | return ret; |
| 89 | } |
| 90 | |
Łukasz Majewski | cf6d7db | 2014-02-21 08:23:00 +0100 | [diff] [blame] | 91 | return 0; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Pantelis Antoniou | a6e788d | 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 | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 95 | { |
Pantelis Antoniou | a6e788d | 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 | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 100 | |
Pantelis Antoniou | a6e788d | 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 | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Afzal Mohammed | b9a4a6b | 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 | f4e934f | 2017-07-19 16:39:23 +0200 | [diff] [blame] | 109 | void *buf, u64 *len) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 110 | { |
Simon Goldschmidt | 59fdf2e | 2019-01-25 19:58:01 +0100 | [diff] [blame] | 111 | char dev_part_str[8]; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 112 | int ret; |
Simon Goldschmidt | 59fdf2e | 2019-01-25 19:58:01 +0100 | [diff] [blame] | 113 | int fstype; |
| 114 | loff_t size = 0; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 115 | |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 116 | switch (dfu->layout) { |
| 117 | case DFU_FS_FAT: |
Simon Goldschmidt | 59fdf2e | 2019-01-25 19:58:01 +0100 | [diff] [blame] | 118 | fstype = FS_TYPE_FAT; |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 119 | break; |
| 120 | case DFU_FS_EXT4: |
Simon Goldschmidt | 59fdf2e | 2019-01-25 19:58:01 +0100 | [diff] [blame] | 121 | fstype = FS_TYPE_EXT; |
Łukasz Majewski | 71c88eb | 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 | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 126 | return -1; |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 127 | } |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 128 | |
Simon Goldschmidt | 59fdf2e | 2019-01-25 19:58:01 +0100 | [diff] [blame] | 129 | snprintf(dev_part_str, sizeof(dev_part_str), "%d:%d", |
| 130 | dfu->data.mmc.dev, dfu->data.mmc.part); |
| 131 | |
| 132 | ret = fs_set_blk_dev("mmc", dev_part_str, fstype); |
| 133 | if (ret) { |
| 134 | puts("dfu: fs_set_blk_dev error!\n"); |
| 135 | return ret; |
| 136 | } |
| 137 | |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 138 | switch (op) { |
| 139 | case DFU_OP_READ: |
Simon Goldschmidt | 59fdf2e | 2019-01-25 19:58:01 +0100 | [diff] [blame] | 140 | ret = fs_read(dfu->name, (size_t)buf, 0, 0, &size); |
| 141 | if (ret) { |
| 142 | puts("dfu: fs_read error!\n"); |
| 143 | return ret; |
| 144 | } |
| 145 | *len = size; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 146 | break; |
| 147 | case DFU_OP_WRITE: |
Simon Goldschmidt | 59fdf2e | 2019-01-25 19:58:01 +0100 | [diff] [blame] | 148 | ret = fs_write(dfu->name, (size_t)buf, 0, *len, &size); |
| 149 | if (ret) { |
| 150 | puts("dfu: fs_write error!\n"); |
| 151 | return ret; |
| 152 | } |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 153 | break; |
| 154 | case DFU_OP_SIZE: |
Simon Goldschmidt | 59fdf2e | 2019-01-25 19:58:01 +0100 | [diff] [blame] | 155 | ret = fs_size(dfu->name, &size); |
| 156 | if (ret) { |
| 157 | puts("dfu: fs_size error!\n"); |
| 158 | return ret; |
| 159 | } |
| 160 | *len = size; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 161 | break; |
| 162 | default: |
| 163 | return -1; |
| 164 | } |
| 165 | |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 166 | return ret; |
| 167 | } |
| 168 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 169 | int dfu_write_medium_mmc(struct dfu_entity *dfu, |
| 170 | u64 offset, void *buf, long *len) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 171 | { |
| 172 | int ret = -1; |
| 173 | |
| 174 | switch (dfu->layout) { |
| 175 | case DFU_RAW_ADDR: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 176 | ret = mmc_block_op(DFU_OP_WRITE, dfu, offset, buf, len); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 177 | break; |
| 178 | case DFU_FS_FAT: |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 179 | case DFU_FS_EXT4: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 180 | ret = mmc_file_buffer(dfu, buf, len); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 181 | break; |
| 182 | default: |
| 183 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 184 | dfu_get_layout(dfu->layout)); |
| 185 | } |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 190 | int dfu_flush_medium_mmc(struct dfu_entity *dfu) |
| 191 | { |
| 192 | int ret = 0; |
| 193 | |
| 194 | if (dfu->layout != DFU_RAW_ADDR) { |
| 195 | /* Do stuff here. */ |
Przemyslaw Marczak | 9d6d51a | 2015-03-04 14:01:23 +0100 | [diff] [blame] | 196 | ret = mmc_file_op(DFU_OP_WRITE, dfu, dfu_file_buf, |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 197 | &dfu_file_buf_len); |
| 198 | |
| 199 | /* Now that we're done */ |
| 200 | dfu_file_buf_len = 0; |
| 201 | } |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
Patrick Delaunay | f4e934f | 2017-07-19 16:39:23 +0200 | [diff] [blame] | 206 | int dfu_get_medium_size_mmc(struct dfu_entity *dfu, u64 *size) |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 207 | { |
| 208 | int ret; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 209 | |
| 210 | switch (dfu->layout) { |
| 211 | case DFU_RAW_ADDR: |
Patrick Delaunay | 5ea9b89 | 2017-07-19 16:39:22 +0200 | [diff] [blame] | 212 | *size = dfu->data.mmc.lba_size * dfu->data.mmc.lba_blk_size; |
| 213 | return 0; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 214 | case DFU_FS_FAT: |
| 215 | case DFU_FS_EXT4: |
Stephen Warren | 7ae42bd | 2015-09-04 22:03:47 -0600 | [diff] [blame] | 216 | dfu_file_buf_filled = -1; |
Patrick Delaunay | 5ea9b89 | 2017-07-19 16:39:22 +0200 | [diff] [blame] | 217 | ret = mmc_file_op(DFU_OP_SIZE, dfu, NULL, size); |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 218 | if (ret < 0) |
| 219 | return ret; |
Patrick Delaunay | 5ea9b89 | 2017-07-19 16:39:22 +0200 | [diff] [blame] | 220 | if (*size > CONFIG_SYS_DFU_MAX_FILE_SIZE) |
Stephen Warren | 7ae42bd | 2015-09-04 22:03:47 -0600 | [diff] [blame] | 221 | return -1; |
Patrick Delaunay | 5ea9b89 | 2017-07-19 16:39:22 +0200 | [diff] [blame] | 222 | return 0; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 223 | default: |
| 224 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 225 | dfu_get_layout(dfu->layout)); |
| 226 | return -1; |
| 227 | } |
Stephen Warren | 7ae42bd | 2015-09-04 22:03:47 -0600 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static int mmc_file_unbuffer(struct dfu_entity *dfu, u64 offset, void *buf, |
| 231 | long *len) |
| 232 | { |
| 233 | int ret; |
Patrick Delaunay | f4e934f | 2017-07-19 16:39:23 +0200 | [diff] [blame] | 234 | u64 file_len; |
Stephen Warren | 7ae42bd | 2015-09-04 22:03:47 -0600 | [diff] [blame] | 235 | |
| 236 | if (dfu_file_buf_filled == -1) { |
| 237 | ret = mmc_file_op(DFU_OP_READ, dfu, dfu_file_buf, &file_len); |
| 238 | if (ret < 0) |
| 239 | return ret; |
| 240 | dfu_file_buf_filled = file_len; |
| 241 | } |
| 242 | if (offset + *len > dfu_file_buf_filled) |
| 243 | return -EINVAL; |
| 244 | |
| 245 | /* Add to the current buffer. */ |
| 246 | memcpy(buf, dfu_file_buf + offset, *len); |
| 247 | |
| 248 | return 0; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 249 | } |
| 250 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 251 | int dfu_read_medium_mmc(struct dfu_entity *dfu, u64 offset, void *buf, |
| 252 | long *len) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 253 | { |
| 254 | int ret = -1; |
| 255 | |
| 256 | switch (dfu->layout) { |
| 257 | case DFU_RAW_ADDR: |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 258 | ret = mmc_block_op(DFU_OP_READ, dfu, offset, buf, len); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 259 | break; |
| 260 | case DFU_FS_FAT: |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 261 | case DFU_FS_EXT4: |
Stephen Warren | 7ae42bd | 2015-09-04 22:03:47 -0600 | [diff] [blame] | 262 | ret = mmc_file_unbuffer(dfu, offset, buf, len); |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 263 | break; |
| 264 | default: |
| 265 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 266 | dfu_get_layout(dfu->layout)); |
| 267 | } |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | |
Przemyslaw Marczak | 9d6d51a | 2015-03-04 14:01:23 +0100 | [diff] [blame] | 272 | void dfu_free_entity_mmc(struct dfu_entity *dfu) |
| 273 | { |
| 274 | if (dfu_file_buf) { |
| 275 | free(dfu_file_buf); |
| 276 | dfu_file_buf = NULL; |
| 277 | } |
| 278 | } |
| 279 | |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 280 | /* |
| 281 | * @param s Parameter string containing space-separated arguments: |
| 282 | * 1st: |
| 283 | * raw (raw read/write) |
| 284 | * fat (files) |
| 285 | * ext4 (^) |
| 286 | * part (partition image) |
| 287 | * 2nd and 3rd: |
| 288 | * lba_start and lba_size, for raw write |
| 289 | * mmc_dev and mmc_part, for filesystems and part |
Lukasz Majewski | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 290 | * 4th (optional): |
| 291 | * mmcpart <num> (access to HW eMMC partitions) |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 292 | */ |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 293 | int dfu_fill_entity_mmc(struct dfu_entity *dfu, char *devstr, char *s) |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 294 | { |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 295 | const char *entity_type; |
| 296 | size_t second_arg; |
| 297 | size_t third_arg; |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 298 | |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 299 | struct mmc *mmc; |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 300 | |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 301 | const char *argv[3]; |
| 302 | const char **parg = argv; |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 303 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 304 | dfu->data.mmc.dev_num = simple_strtoul(devstr, NULL, 10); |
| 305 | |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 306 | for (; parg < argv + sizeof(argv) / sizeof(*argv); ++parg) { |
| 307 | *parg = strsep(&s, " "); |
| 308 | if (*parg == NULL) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 309 | pr_err("Invalid number of arguments.\n"); |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 310 | return -ENODEV; |
| 311 | } |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | entity_type = argv[0]; |
Mateusz Zalega | 0ab80bf | 2014-04-28 21:13:25 +0200 | [diff] [blame] | 315 | /* |
| 316 | * Base 0 means we'll accept (prefixed with 0x or 0) base 16, 8, |
| 317 | * with default 10. |
| 318 | */ |
| 319 | second_arg = simple_strtoul(argv[1], NULL, 0); |
| 320 | third_arg = simple_strtoul(argv[2], NULL, 0); |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 321 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 322 | mmc = find_mmc_device(dfu->data.mmc.dev_num); |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 323 | if (mmc == NULL) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 324 | pr_err("Couldn't find MMC device no. %d.\n", |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 325 | dfu->data.mmc.dev_num); |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 326 | return -ENODEV; |
| 327 | } |
| 328 | |
| 329 | if (mmc_init(mmc)) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 330 | pr_err("Couldn't init MMC device.\n"); |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 331 | return -ENODEV; |
| 332 | } |
| 333 | |
Lukasz Majewski | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 334 | dfu->data.mmc.hw_partition = -EINVAL; |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 335 | if (!strcmp(entity_type, "raw")) { |
| 336 | dfu->layout = DFU_RAW_ADDR; |
| 337 | dfu->data.mmc.lba_start = second_arg; |
| 338 | dfu->data.mmc.lba_size = third_arg; |
| 339 | dfu->data.mmc.lba_blk_size = mmc->read_bl_len; |
Lukasz Majewski | 4da65ea | 2014-05-09 16:58:15 +0200 | [diff] [blame] | 340 | |
| 341 | /* |
| 342 | * Check for an extra entry at dfu_alt_info env variable |
| 343 | * specifying the mmc HW defined partition number |
| 344 | */ |
| 345 | if (s) |
| 346 | if (!strcmp(strsep(&s, " "), "mmcpart")) |
| 347 | dfu->data.mmc.hw_partition = |
| 348 | simple_strtoul(s, NULL, 0); |
| 349 | |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 350 | } else if (!strcmp(entity_type, "part")) { |
| 351 | disk_partition_t partinfo; |
Simon Glass | aa7620b | 2016-06-12 23:30:33 -0600 | [diff] [blame] | 352 | struct blk_desc *blk_dev = mmc_get_blk_desc(mmc); |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 353 | int mmcdev = second_arg; |
| 354 | int mmcpart = third_arg; |
Marek Szyprowski | f937824 | 2019-10-02 12:34:37 +0200 | [diff] [blame] | 355 | int offset = 0; |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 356 | |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 357 | if (part_get_info(blk_dev, mmcpart, &partinfo) != 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 358 | pr_err("Couldn't find part #%d on mmc device #%d\n", |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 359 | mmcpart, mmcdev); |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 360 | return -ENODEV; |
| 361 | } |
| 362 | |
Marek Szyprowski | f937824 | 2019-10-02 12:34:37 +0200 | [diff] [blame] | 363 | /* |
| 364 | * Check for an extra entry at dfu_alt_info env variable |
| 365 | * specifying the mmc HW defined partition number |
| 366 | */ |
| 367 | if (s) |
| 368 | if (!strcmp(strsep(&s, " "), "offset")) |
| 369 | offset = simple_strtoul(s, NULL, 0); |
| 370 | |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 371 | dfu->layout = DFU_RAW_ADDR; |
Marek Szyprowski | f937824 | 2019-10-02 12:34:37 +0200 | [diff] [blame] | 372 | dfu->data.mmc.lba_start = partinfo.start + offset; |
| 373 | dfu->data.mmc.lba_size = partinfo.size-offset; |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 374 | dfu->data.mmc.lba_blk_size = partinfo.blksz; |
| 375 | } else if (!strcmp(entity_type, "fat")) { |
| 376 | dfu->layout = DFU_FS_FAT; |
| 377 | } else if (!strcmp(entity_type, "ext4")) { |
| 378 | dfu->layout = DFU_FS_EXT4; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 379 | } else { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 380 | pr_err("Memory layout (%s) not supported!\n", entity_type); |
Pantelis Antoniou | 620e9dd | 2012-11-30 08:01:11 +0000 | [diff] [blame] | 381 | return -ENODEV; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 382 | } |
| 383 | |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 384 | /* if it's NOT a raw write */ |
| 385 | if (strcmp(entity_type, "raw")) { |
| 386 | dfu->data.mmc.dev = second_arg; |
| 387 | dfu->data.mmc.part = third_arg; |
Łukasz Majewski | 71c88eb | 2012-08-23 23:33:55 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Mateusz Zalega | dcb6ea6 | 2014-04-28 21:13:24 +0200 | [diff] [blame] | 390 | dfu->dev_type = DFU_DEV_MMC; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 391 | dfu->get_medium_size = dfu_get_medium_size_mmc; |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 392 | dfu->read_medium = dfu_read_medium_mmc; |
| 393 | dfu->write_medium = dfu_write_medium_mmc; |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 394 | dfu->flush_medium = dfu_flush_medium_mmc; |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 395 | dfu->inited = 0; |
Przemyslaw Marczak | 9d6d51a | 2015-03-04 14:01:23 +0100 | [diff] [blame] | 396 | dfu->free_entity = dfu_free_entity_mmc; |
| 397 | |
| 398 | /* Check if file buffer is ready */ |
| 399 | if (!dfu_file_buf) { |
| 400 | dfu_file_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, |
| 401 | CONFIG_SYS_DFU_MAX_FILE_SIZE); |
| 402 | if (!dfu_file_buf) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 403 | pr_err("Could not memalign 0x%x bytes", |
Przemyslaw Marczak | 9d6d51a | 2015-03-04 14:01:23 +0100 | [diff] [blame] | 404 | CONFIG_SYS_DFU_MAX_FILE_SIZE); |
| 405 | return -ENOMEM; |
| 406 | } |
| 407 | } |
Lukasz Majewski | 34106b2 | 2012-08-06 14:41:08 +0200 | [diff] [blame] | 408 | |
| 409 | return 0; |
| 410 | } |