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