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