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