Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +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 | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 10 | #include <errno.h> |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <mmc.h> |
| 13 | #include <fat.h> |
| 14 | #include <dfu.h> |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 15 | #include <hash.h> |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | #include <linux/compiler.h> |
| 18 | |
| 19 | static LIST_HEAD(dfu_list); |
| 20 | static int dfu_alt_num; |
Przemyslaw Marczak | 4af3e08 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 21 | static int alt_num_cnt; |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 22 | static struct hash_algo *dfu_hash_algo; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 23 | |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 24 | /* |
| 25 | * The purpose of the dfu_usb_get_reset() function is to |
| 26 | * provide information if after USB_DETACH request |
| 27 | * being sent the dfu-util performed reset of USB |
| 28 | * bus. |
| 29 | * |
| 30 | * Described behaviour is the only way to distinct if |
| 31 | * user has typed -e (detach) or -R (reset) when invoking |
| 32 | * dfu-util command. |
| 33 | * |
| 34 | */ |
| 35 | __weak bool dfu_usb_get_reset(void) |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 36 | { |
B, Ravi | 4cca5cf | 2017-05-04 15:45:29 +0530 | [diff] [blame] | 37 | #ifdef CONFIG_SPL_DFU_NO_RESET |
| 38 | return false; |
| 39 | #else |
Lukasz Majewski | d9eae70 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 40 | return true; |
B, Ravi | 4cca5cf | 2017-05-04 15:45:29 +0530 | [diff] [blame] | 41 | #endif |
Lukasz Majewski | 2968aa2 | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 44 | static int dfu_find_alt_num(const char *s) |
| 45 | { |
| 46 | int i = 0; |
| 47 | |
| 48 | for (; *s; s++) |
| 49 | if (*s == ';') |
| 50 | i++; |
| 51 | |
| 52 | return ++i; |
| 53 | } |
| 54 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 55 | int dfu_init_env_entities(char *interface, char *devstr) |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 56 | { |
| 57 | const char *str_env; |
| 58 | char *env_bkp; |
Sam Protsenko | 6500cde | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 59 | int ret = 0; |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 60 | |
Przemyslaw Marczak | d0dd609 | 2015-02-17 12:24:11 +0100 | [diff] [blame] | 61 | #ifdef CONFIG_SET_DFU_ALT_INFO |
| 62 | set_dfu_alt_info(interface, devstr); |
| 63 | #endif |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 64 | str_env = env_get("dfu_alt_info"); |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 65 | if (!str_env) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 66 | pr_err("\"dfu_alt_info\" env variable not defined!\n"); |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 67 | return -EINVAL; |
| 68 | } |
| 69 | |
| 70 | env_bkp = strdup(str_env); |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 71 | ret = dfu_config_entities(env_bkp, interface, devstr); |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 72 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 73 | pr_err("DFU entities configuration failed!\n"); |
Sam Protsenko | ab79fb0 | 2018-07-13 16:35:47 +0300 | [diff] [blame^] | 74 | pr_err("(partition table does not match dfu_alt_info?)\n"); |
Sam Protsenko | 6500cde | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 75 | goto done; |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Sam Protsenko | 6500cde | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 78 | done: |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 79 | free(env_bkp); |
Sam Protsenko | 6500cde | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 80 | return ret; |
Lukasz Majewski | 35e591e | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 83 | static unsigned char *dfu_buf; |
Lukasz Majewski | 0af4df8 | 2015-07-08 23:43:18 +0200 | [diff] [blame] | 84 | static unsigned long dfu_buf_size; |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 85 | |
Lukasz Majewski | 3426423 | 2013-10-08 14:30:39 +0200 | [diff] [blame] | 86 | unsigned char *dfu_free_buf(void) |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 87 | { |
| 88 | free(dfu_buf); |
| 89 | dfu_buf = NULL; |
| 90 | return dfu_buf; |
| 91 | } |
| 92 | |
Lukasz Majewski | 031d413 | 2013-12-09 16:20:13 +0100 | [diff] [blame] | 93 | unsigned long dfu_get_buf_size(void) |
| 94 | { |
| 95 | return dfu_buf_size; |
| 96 | } |
| 97 | |
Stephen Warren | f950098 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 98 | unsigned char *dfu_get_buf(struct dfu_entity *dfu) |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 99 | { |
| 100 | char *s; |
| 101 | |
| 102 | if (dfu_buf != NULL) |
| 103 | return dfu_buf; |
| 104 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 105 | s = env_get("dfu_bufsiz"); |
Przemyslaw Marczak | 2488350 | 2014-12-15 10:34:11 +0100 | [diff] [blame] | 106 | if (s) |
| 107 | dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0); |
| 108 | |
| 109 | if (!s || !dfu_buf_size) |
| 110 | dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE; |
| 111 | |
Stephen Warren | f950098 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 112 | if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size) |
| 113 | dfu_buf_size = dfu->max_buf_size; |
Heiko Schocher | a2f831e | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 114 | |
| 115 | dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size); |
| 116 | if (dfu_buf == NULL) |
| 117 | printf("%s: Could not memalign 0x%lx bytes\n", |
| 118 | __func__, dfu_buf_size); |
| 119 | |
| 120 | return dfu_buf; |
| 121 | } |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 122 | |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 123 | static char *dfu_get_hash_algo(void) |
| 124 | { |
| 125 | char *s; |
| 126 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 127 | s = env_get("dfu_hash_algo"); |
Lukasz Majewski | 3e0d028 | 2014-06-10 12:25:59 +0200 | [diff] [blame] | 128 | if (!s) |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 129 | return NULL; |
| 130 | |
Lukasz Majewski | 3e0d028 | 2014-06-10 12:25:59 +0200 | [diff] [blame] | 131 | if (!strcmp(s, "crc32")) { |
| 132 | debug("%s: DFU hash method: %s\n", __func__, s); |
| 133 | return s; |
| 134 | } |
| 135 | |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 136 | pr_err("DFU hash method: %s not supported!\n", s); |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 140 | static int dfu_write_buffer_drain(struct dfu_entity *dfu) |
| 141 | { |
| 142 | long w_size; |
| 143 | int ret; |
| 144 | |
| 145 | /* flush size? */ |
| 146 | w_size = dfu->i_buf - dfu->i_buf_start; |
| 147 | if (w_size == 0) |
| 148 | return 0; |
| 149 | |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 150 | if (dfu_hash_algo) |
| 151 | dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc, |
| 152 | dfu->i_buf_start, w_size, 0); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 153 | |
| 154 | ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size); |
| 155 | if (ret) |
| 156 | debug("%s: Write error!\n", __func__); |
| 157 | |
| 158 | /* point back */ |
| 159 | dfu->i_buf = dfu->i_buf_start; |
| 160 | |
| 161 | /* update offset */ |
| 162 | dfu->offset += w_size; |
| 163 | |
| 164 | puts("#"); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 169 | void dfu_transaction_cleanup(struct dfu_entity *dfu) |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 170 | { |
| 171 | /* clear everything */ |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 172 | dfu->crc = 0; |
| 173 | dfu->offset = 0; |
| 174 | dfu->i_blk_seq_num = 0; |
Patrick Delaunay | dec4ecb | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 175 | dfu->i_buf_start = dfu_get_buf(dfu); |
| 176 | dfu->i_buf_end = dfu->i_buf_start; |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 177 | dfu->i_buf = dfu->i_buf_start; |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 178 | dfu->r_left = 0; |
| 179 | dfu->b_left = 0; |
| 180 | dfu->bad_skip = 0; |
| 181 | |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 182 | dfu->inited = 0; |
| 183 | } |
| 184 | |
Patrick Delaunay | dec4ecb | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 185 | int dfu_transaction_initiate(struct dfu_entity *dfu, bool read) |
| 186 | { |
| 187 | int ret = 0; |
| 188 | |
| 189 | if (dfu->inited) |
| 190 | return 0; |
| 191 | |
| 192 | dfu_transaction_cleanup(dfu); |
| 193 | |
| 194 | if (dfu->i_buf_start == NULL) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size(); |
| 198 | |
| 199 | if (read) { |
| 200 | ret = dfu->get_medium_size(dfu, &dfu->r_left); |
| 201 | if (ret < 0) |
| 202 | return ret; |
| 203 | debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left); |
| 204 | } |
| 205 | |
| 206 | dfu->inited = 1; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Heiko Schocher | 941b384 | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 211 | int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 212 | { |
| 213 | int ret = 0; |
| 214 | |
Przemyslaw Marczak | a0f6a38 | 2014-05-12 12:05:33 +0200 | [diff] [blame] | 215 | ret = dfu_write_buffer_drain(dfu); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | |
Heiko Schocher | 941b384 | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 219 | if (dfu->flush_medium) |
| 220 | ret = dfu->flush_medium(dfu); |
| 221 | |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 222 | if (dfu_hash_algo) |
| 223 | printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name, |
| 224 | dfu->crc); |
Heiko Schocher | 941b384 | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 225 | |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 226 | dfu_transaction_cleanup(dfu); |
Heiko Schocher | 941b384 | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 227 | |
| 228 | return ret; |
| 229 | } |
| 230 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 231 | int dfu_write(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 232 | { |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 233 | int ret; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 234 | |
Stephen Warren | c00315b | 2015-07-22 14:54:04 -0600 | [diff] [blame] | 235 | debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x offset: 0x%llx bufoffset: 0x%lx\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 236 | __func__, dfu->name, buf, size, blk_seq_num, dfu->offset, |
Stephen Warren | c00315b | 2015-07-22 14:54:04 -0600 | [diff] [blame] | 237 | (unsigned long)(dfu->i_buf - dfu->i_buf_start)); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 238 | |
Patrick Delaunay | dec4ecb | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 239 | ret = dfu_transaction_initiate(dfu, false); |
| 240 | if (ret < 0) |
| 241 | return ret; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 242 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 243 | if (dfu->i_blk_seq_num != blk_seq_num) { |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 244 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 245 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 246 | dfu_transaction_cleanup(dfu); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | /* DFU 1.1 standard says: |
| 251 | * The wBlockNum field is a block sequence number. It increments each |
| 252 | * time a block is transferred, wrapping to zero from 65,535. It is used |
| 253 | * to provide useful context to the DFU loader in the device." |
| 254 | * |
| 255 | * This means that it's a 16 bit counter that roll-overs at |
| 256 | * 0xffff -> 0x0000. By having a typical 4K transfer block |
| 257 | * we roll-over at exactly 256MB. Not very fun to debug. |
| 258 | * |
| 259 | * Handling rollover, and having an inited variable, |
| 260 | * makes things work. |
| 261 | */ |
| 262 | |
| 263 | /* handle rollover */ |
| 264 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
| 265 | |
| 266 | /* flush buffer if overflow */ |
| 267 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 268 | ret = dfu_write_buffer_drain(dfu); |
| 269 | if (ret) { |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 270 | dfu_transaction_cleanup(dfu); |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 271 | return ret; |
| 272 | } |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* we should be in buffer now (if not then size too large) */ |
| 276 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 277 | pr_err("Buffer overflow! (0x%p + 0x%x > 0x%p)\n", dfu->i_buf, |
Lukasz Majewski | 385c321 | 2013-09-10 15:29:22 +0200 | [diff] [blame] | 278 | size, dfu->i_buf_end); |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 279 | dfu_transaction_cleanup(dfu); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 283 | memcpy(dfu->i_buf, buf, size); |
| 284 | dfu->i_buf += size; |
| 285 | |
| 286 | /* if end or if buffer full flush */ |
| 287 | if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) { |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 288 | ret = dfu_write_buffer_drain(dfu); |
| 289 | if (ret) { |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 290 | dfu_transaction_cleanup(dfu); |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 291 | return ret; |
| 292 | } |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 293 | } |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 294 | |
Stephen Warren | 7f45023 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 295 | return 0; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 296 | } |
| 297 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 298 | static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) |
| 299 | { |
| 300 | long chunk; |
| 301 | int ret, readn; |
| 302 | |
| 303 | readn = 0; |
| 304 | while (size > 0) { |
| 305 | /* get chunk that can be read */ |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 306 | chunk = min((long)size, dfu->b_left); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 307 | /* consume */ |
| 308 | if (chunk > 0) { |
| 309 | memcpy(buf, dfu->i_buf, chunk); |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 310 | if (dfu_hash_algo) |
| 311 | dfu_hash_algo->hash_update(dfu_hash_algo, |
| 312 | &dfu->crc, buf, |
| 313 | chunk, 0); |
| 314 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 315 | dfu->i_buf += chunk; |
| 316 | dfu->b_left -= chunk; |
| 317 | size -= chunk; |
| 318 | buf += chunk; |
| 319 | readn += chunk; |
| 320 | } |
| 321 | |
| 322 | /* all done */ |
| 323 | if (size > 0) { |
| 324 | /* no more to read */ |
| 325 | if (dfu->r_left == 0) |
| 326 | break; |
| 327 | |
| 328 | dfu->i_buf = dfu->i_buf_start; |
| 329 | dfu->b_left = dfu->i_buf_end - dfu->i_buf_start; |
| 330 | |
| 331 | /* got to read, but buffer is empty */ |
| 332 | if (dfu->b_left > dfu->r_left) |
| 333 | dfu->b_left = dfu->r_left; |
| 334 | ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf, |
| 335 | &dfu->b_left); |
| 336 | if (ret != 0) { |
| 337 | debug("%s: Read error!\n", __func__); |
| 338 | return ret; |
| 339 | } |
| 340 | dfu->offset += dfu->b_left; |
| 341 | dfu->r_left -= dfu->b_left; |
| 342 | |
| 343 | puts("#"); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return readn; |
| 348 | } |
| 349 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 350 | int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 351 | { |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 352 | int ret = 0; |
| 353 | |
| 354 | debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 355 | __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf); |
| 356 | |
Patrick Delaunay | dec4ecb | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 357 | ret = dfu_transaction_initiate(dfu, true); |
| 358 | if (ret < 0) |
| 359 | return ret; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 360 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 361 | if (dfu->i_blk_seq_num != blk_seq_num) { |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 362 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 363 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 364 | return -1; |
| 365 | } |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 366 | /* handle rollover */ |
| 367 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 368 | |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 369 | ret = dfu_read_buffer_fill(dfu, buf, size); |
| 370 | if (ret < 0) { |
| 371 | printf("%s: Failed to fill buffer\n", __func__); |
| 372 | return -1; |
| 373 | } |
| 374 | |
| 375 | if (ret < size) { |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 376 | if (dfu_hash_algo) |
| 377 | debug("%s: %s %s: 0x%x\n", __func__, dfu->name, |
| 378 | dfu_hash_algo->name, dfu->crc); |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 379 | puts("\nUPLOAD ... done\nCtrl+C to exit ...\n"); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 380 | |
Patrick Delaunay | 35be83e | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 381 | dfu_transaction_cleanup(dfu); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 382 | } |
Pantelis Antoniou | a6e788d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 383 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | static int dfu_fill_entity(struct dfu_entity *dfu, char *s, int alt, |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 388 | char *interface, char *devstr) |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 389 | { |
| 390 | char *st; |
| 391 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 392 | debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 393 | st = strsep(&s, " "); |
| 394 | strcpy(dfu->name, st); |
| 395 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 396 | dfu->alt = alt; |
Stephen Warren | f950098 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 397 | dfu->max_buf_size = 0; |
Stephen Warren | a5f77f6 | 2014-06-11 16:03:35 -0600 | [diff] [blame] | 398 | dfu->free_entity = NULL; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 399 | |
| 400 | /* Specific for mmc device */ |
| 401 | if (strcmp(interface, "mmc") == 0) { |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 402 | if (dfu_fill_entity_mmc(dfu, devstr, s)) |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 403 | return -1; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 404 | } else if (strcmp(interface, "nand") == 0) { |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 405 | if (dfu_fill_entity_nand(dfu, devstr, s)) |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 406 | return -1; |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 407 | } else if (strcmp(interface, "ram") == 0) { |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 408 | if (dfu_fill_entity_ram(dfu, devstr, s)) |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 409 | return -1; |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 410 | } else if (strcmp(interface, "sf") == 0) { |
| 411 | if (dfu_fill_entity_sf(dfu, devstr, s)) |
| 412 | return -1; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 413 | } else { |
| 414 | printf("%s: Device %s not (yet) supported!\n", |
| 415 | __func__, interface); |
| 416 | return -1; |
| 417 | } |
Stephen Warren | 0898da5 | 2015-09-04 22:03:46 -0600 | [diff] [blame] | 418 | dfu_get_buf(dfu); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | void dfu_free_entities(void) |
| 424 | { |
| 425 | struct dfu_entity *dfu, *p, *t = NULL; |
| 426 | |
Stephen Warren | 0898da5 | 2015-09-04 22:03:46 -0600 | [diff] [blame] | 427 | dfu_free_buf(); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 428 | list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) { |
| 429 | list_del(&dfu->list); |
Stephen Warren | a5f77f6 | 2014-06-11 16:03:35 -0600 | [diff] [blame] | 430 | if (dfu->free_entity) |
| 431 | dfu->free_entity(dfu); |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 432 | t = dfu; |
| 433 | } |
| 434 | if (t) |
| 435 | free(t); |
| 436 | INIT_LIST_HEAD(&dfu_list); |
Przemyslaw Marczak | 4af3e08 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 437 | |
| 438 | alt_num_cnt = 0; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 441 | int dfu_config_entities(char *env, char *interface, char *devstr) |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 442 | { |
| 443 | struct dfu_entity *dfu; |
| 444 | int i, ret; |
| 445 | char *s; |
| 446 | |
| 447 | dfu_alt_num = dfu_find_alt_num(env); |
| 448 | debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num); |
| 449 | |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 450 | dfu_hash_algo = NULL; |
| 451 | s = dfu_get_hash_algo(); |
| 452 | if (s) { |
| 453 | ret = hash_lookup_algo(s, &dfu_hash_algo); |
| 454 | if (ret) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 455 | pr_err("Hash algorithm %s not supported\n", s); |
Lukasz Majewski | fa8dc1d | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 456 | } |
| 457 | |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 458 | dfu = calloc(sizeof(*dfu), dfu_alt_num); |
| 459 | if (!dfu) |
| 460 | return -1; |
| 461 | for (i = 0; i < dfu_alt_num; i++) { |
| 462 | |
| 463 | s = strsep(&env, ";"); |
Stephen Warren | 4afe50f | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 464 | ret = dfu_fill_entity(&dfu[i], s, alt_num_cnt, interface, |
| 465 | devstr); |
Peng Fan | 529d4fd | 2016-05-03 10:24:52 +0800 | [diff] [blame] | 466 | if (ret) { |
Sam Protsenko | 6d29e5c | 2018-07-13 16:35:45 +0300 | [diff] [blame] | 467 | /* We will free "dfu" in dfu_free_entities() */ |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 468 | return -1; |
Peng Fan | 529d4fd | 2016-05-03 10:24:52 +0800 | [diff] [blame] | 469 | } |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 470 | |
| 471 | list_add_tail(&dfu[i].list, &dfu_list); |
Przemyslaw Marczak | 4af3e08 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 472 | alt_num_cnt++; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | const char *dfu_get_dev_type(enum dfu_device_type t) |
| 479 | { |
Patrick Delaunay | 9eb430d | 2016-10-28 09:44:26 +0200 | [diff] [blame] | 480 | const char *dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", "SF" }; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 481 | return dev_t[t]; |
| 482 | } |
| 483 | |
| 484 | const char *dfu_get_layout(enum dfu_layout l) |
| 485 | { |
| 486 | const char *dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2", |
Afzal Mohammed | e3c687a | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 487 | "EXT3", "EXT4", "RAM_ADDR" }; |
Lukasz Majewski | d311c6e | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 488 | return dfu_layout[l]; |
| 489 | } |
| 490 | |
| 491 | void dfu_show_entities(void) |
| 492 | { |
| 493 | struct dfu_entity *dfu; |
| 494 | |
| 495 | puts("DFU alt settings list:\n"); |
| 496 | |
| 497 | list_for_each_entry(dfu, &dfu_list, list) { |
| 498 | printf("dev: %s alt: %d name: %s layout: %s\n", |
| 499 | dfu_get_dev_type(dfu->dev_type), dfu->alt, |
| 500 | dfu->name, dfu_get_layout(dfu->layout)); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | int dfu_get_alt_number(void) |
| 505 | { |
| 506 | return dfu_alt_num; |
| 507 | } |
| 508 | |
| 509 | struct dfu_entity *dfu_get_entity(int alt) |
| 510 | { |
| 511 | struct dfu_entity *dfu; |
| 512 | |
| 513 | list_for_each_entry(dfu, &dfu_list, list) { |
| 514 | if (dfu->alt == alt) |
| 515 | return dfu; |
| 516 | } |
| 517 | |
| 518 | return NULL; |
| 519 | } |
Lukasz Majewski | 1737f0f | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 520 | |
| 521 | int dfu_get_alt(char *name) |
| 522 | { |
| 523 | struct dfu_entity *dfu; |
Lukasz Majewski | 3a39d01 | 2014-11-05 10:54:16 +0100 | [diff] [blame] | 524 | char *str; |
Lukasz Majewski | 1737f0f | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 525 | |
| 526 | list_for_each_entry(dfu, &dfu_list, list) { |
Lukasz Majewski | 3a39d01 | 2014-11-05 10:54:16 +0100 | [diff] [blame] | 527 | if (dfu->name[0] != '/') { |
| 528 | if (!strncmp(dfu->name, name, strlen(dfu->name))) |
| 529 | return dfu->alt; |
| 530 | } else { |
| 531 | /* |
| 532 | * One must also consider absolute path |
| 533 | * (/boot/bin/uImage) available at dfu->name when |
| 534 | * compared "plain" file name (uImage) |
| 535 | * |
| 536 | * It is the case for e.g. thor gadget where lthor SW |
| 537 | * sends only the file name, so only the very last part |
| 538 | * of path must be checked for equality |
| 539 | */ |
| 540 | |
| 541 | str = strstr(dfu->name, name); |
| 542 | if (!str) |
| 543 | continue; |
| 544 | |
| 545 | /* |
| 546 | * Check if matching substring is the last element of |
| 547 | * dfu->name (uImage) |
| 548 | */ |
| 549 | if (strlen(dfu->name) == |
| 550 | ((str - dfu->name) + strlen(name))) |
| 551 | return dfu->alt; |
| 552 | } |
Lukasz Majewski | 1737f0f | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | return -ENODEV; |
| 556 | } |
Lukasz Majewski | cc2d026 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 557 | |
| 558 | int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size) |
| 559 | { |
| 560 | unsigned long dfu_buf_size, write, left = size; |
| 561 | int i, ret = 0; |
| 562 | void *dp = buf; |
| 563 | |
| 564 | /* |
| 565 | * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size |
| 566 | * has been properly initialized - e.g. if "dfu_bufsiz" has been taken |
| 567 | * into account. |
| 568 | */ |
| 569 | dfu_get_buf(dfu); |
| 570 | dfu_buf_size = dfu_get_buf_size(); |
| 571 | debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size); |
| 572 | |
| 573 | for (i = 0; left > 0; i++) { |
| 574 | write = min(dfu_buf_size, left); |
| 575 | |
| 576 | debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__, |
| 577 | dp, left, write); |
| 578 | ret = dfu_write(dfu, dp, write, i); |
| 579 | if (ret) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 580 | pr_err("DFU write failed\n"); |
Lukasz Majewski | cc2d026 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | dp += write; |
| 585 | left -= write; |
| 586 | } |
| 587 | |
| 588 | ret = dfu_flush(dfu, NULL, 0, i); |
| 589 | if (ret) |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 590 | pr_err("DFU flush failed!"); |
Lukasz Majewski | cc2d026 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 591 | |
| 592 | return ret; |
| 593 | } |