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