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