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