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