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