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> |
Simon Glass | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 10 | #include <env.h> |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <mmc.h> |
| 15 | #include <fat.h> |
| 16 | #include <dfu.h> |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 17 | #include <hash.h> |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 18 | #include <linux/list.h> |
| 19 | #include <linux/compiler.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 20 | #include <linux/printk.h> |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 21 | |
AKASHI Takahiro | 6beaa47 | 2020-10-29 13:47:44 +0900 | [diff] [blame] | 22 | LIST_HEAD(dfu_list); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 23 | static int dfu_alt_num; |
Przemyslaw Marczak | b627eb4 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 24 | static int alt_num_cnt; |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 25 | static struct hash_algo *dfu_hash_algo; |
Andy Shevchenko | 98a8f44 | 2019-11-27 18:12:15 +0200 | [diff] [blame] | 26 | #ifdef CONFIG_DFU_TIMEOUT |
| 27 | static unsigned long dfu_timeout = 0; |
| 28 | #endif |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 29 | |
Marek Szyprowski | c533f94 | 2020-12-22 11:32:22 +0100 | [diff] [blame] | 30 | bool dfu_reinit_needed = false; |
| 31 | |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 32 | /* |
Patrick Delaunay | 067c13c | 2019-10-14 09:28:07 +0200 | [diff] [blame] | 33 | * The purpose of the dfu_flush_callback() function is to |
| 34 | * provide callback for dfu user |
| 35 | */ |
| 36 | __weak void dfu_flush_callback(struct dfu_entity *dfu) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * The purpose of the dfu_initiated_callback() function is to |
| 42 | * provide callback for dfu user |
| 43 | */ |
| 44 | __weak void dfu_initiated_callback(struct dfu_entity *dfu) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /* |
Patrick Delaunay | d471032 | 2021-05-18 15:12:12 +0200 | [diff] [blame] | 49 | * The purpose of the dfu_error_callback() function is to |
| 50 | * provide callback for dfu user |
| 51 | */ |
| 52 | __weak void dfu_error_callback(struct dfu_entity *dfu, const char *msg) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | /* |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 57 | * The purpose of the dfu_usb_get_reset() function is to |
| 58 | * provide information if after USB_DETACH request |
| 59 | * being sent the dfu-util performed reset of USB |
| 60 | * bus. |
| 61 | * |
| 62 | * Described behaviour is the only way to distinct if |
| 63 | * user has typed -e (detach) or -R (reset) when invoking |
| 64 | * dfu-util command. |
| 65 | * |
| 66 | */ |
| 67 | __weak bool dfu_usb_get_reset(void) |
Lukasz Majewski | 6bed7ce | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 68 | { |
B, Ravi | 66928af | 2017-05-04 15:45:29 +0530 | [diff] [blame] | 69 | #ifdef CONFIG_SPL_DFU_NO_RESET |
| 70 | return false; |
| 71 | #else |
Lukasz Majewski | 1cc03c5 | 2014-08-25 11:07:28 +0200 | [diff] [blame] | 72 | return true; |
B, Ravi | 66928af | 2017-05-04 15:45:29 +0530 | [diff] [blame] | 73 | #endif |
Lukasz Majewski | 6bed7ce | 2013-07-18 13:19:14 +0200 | [diff] [blame] | 74 | } |
| 75 | |
Andy Shevchenko | 98a8f44 | 2019-11-27 18:12:15 +0200 | [diff] [blame] | 76 | #ifdef CONFIG_DFU_TIMEOUT |
| 77 | void dfu_set_timeout(unsigned long timeout) |
| 78 | { |
| 79 | dfu_timeout = timeout; |
| 80 | } |
| 81 | |
| 82 | unsigned long dfu_get_timeout(void) |
| 83 | { |
| 84 | return dfu_timeout; |
| 85 | } |
| 86 | #endif |
| 87 | |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 88 | static int dfu_find_alt_num(const char *s) |
| 89 | { |
| 90 | int i = 0; |
| 91 | |
| 92 | for (; *s; s++) |
| 93 | if (*s == ';') |
| 94 | i++; |
| 95 | |
| 96 | return ++i; |
| 97 | } |
| 98 | |
Patrick Delaunay | febabe3 | 2019-10-14 09:28:02 +0200 | [diff] [blame] | 99 | /* |
| 100 | * treat dfu_alt_info with several interface information |
| 101 | * to allow DFU on several device with one command, |
| 102 | * the string format is |
| 103 | * interface devstring'='alternate list (';' separated) |
| 104 | * and each interface separated by '&' |
| 105 | */ |
| 106 | int dfu_config_interfaces(char *env) |
| 107 | { |
| 108 | struct dfu_entity *dfu; |
| 109 | char *s, *i, *d, *a, *part; |
| 110 | int ret = -EINVAL; |
| 111 | int n = 1; |
| 112 | |
| 113 | s = env; |
| 114 | for (; *s; s++) { |
| 115 | if (*s == ';') |
| 116 | n++; |
| 117 | if (*s == '&') |
| 118 | n++; |
| 119 | } |
| 120 | ret = dfu_alt_init(n, &dfu); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
| 124 | s = env; |
| 125 | while (s) { |
| 126 | ret = -EINVAL; |
Masami Hiramatsu | 8db74c1 | 2022-01-31 11:52:29 +0900 | [diff] [blame] | 127 | i = strsep(&s, " \t"); |
Patrick Delaunay | febabe3 | 2019-10-14 09:28:02 +0200 | [diff] [blame] | 128 | if (!i) |
| 129 | break; |
Masami Hiramatsu | 8db74c1 | 2022-01-31 11:52:29 +0900 | [diff] [blame] | 130 | s = skip_spaces(s); |
Patrick Delaunay | febabe3 | 2019-10-14 09:28:02 +0200 | [diff] [blame] | 131 | d = strsep(&s, "="); |
| 132 | if (!d) |
| 133 | break; |
| 134 | a = strsep(&s, "&"); |
| 135 | if (!a) |
| 136 | a = s; |
| 137 | do { |
| 138 | part = strsep(&a, ";"); |
Ravi Gunasekaran | de06083 | 2023-05-18 12:06:23 +0530 | [diff] [blame] | 139 | part = skip_spaces(part); |
Patrick Delaunay | febabe3 | 2019-10-14 09:28:02 +0200 | [diff] [blame] | 140 | ret = dfu_alt_add(dfu, i, d, part); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | } while (a); |
| 144 | } |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
Stephen Warren | dd64827 | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 149 | int dfu_init_env_entities(char *interface, char *devstr) |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 150 | { |
| 151 | const char *str_env; |
| 152 | char *env_bkp; |
Sam Protsenko | 87a8ca9 | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 153 | int ret = 0; |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 154 | |
Marek Szyprowski | c533f94 | 2020-12-22 11:32:22 +0100 | [diff] [blame] | 155 | dfu_reinit_needed = false; |
| 156 | |
Przemyslaw Marczak | 899a528 | 2015-02-17 12:24:11 +0100 | [diff] [blame] | 157 | #ifdef CONFIG_SET_DFU_ALT_INFO |
| 158 | set_dfu_alt_info(interface, devstr); |
| 159 | #endif |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 160 | str_env = env_get("dfu_alt_info"); |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 161 | if (!str_env) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 162 | pr_err("\"dfu_alt_info\" env variable not defined!\n"); |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | } |
| 165 | |
| 166 | env_bkp = strdup(str_env); |
Patrick Delaunay | febabe3 | 2019-10-14 09:28:02 +0200 | [diff] [blame] | 167 | if (!interface && !devstr) |
| 168 | ret = dfu_config_interfaces(env_bkp); |
| 169 | else |
| 170 | ret = dfu_config_entities(env_bkp, interface, devstr); |
| 171 | |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 172 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 173 | pr_err("DFU entities configuration failed!\n"); |
Sam Protsenko | 28a5c88 | 2018-07-13 16:35:47 +0300 | [diff] [blame] | 174 | pr_err("(partition table does not match dfu_alt_info?)\n"); |
Sam Protsenko | 87a8ca9 | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 175 | goto done; |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Sam Protsenko | 87a8ca9 | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 178 | done: |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 179 | free(env_bkp); |
Sam Protsenko | 87a8ca9 | 2018-07-13 16:35:46 +0300 | [diff] [blame] | 180 | return ret; |
Lukasz Majewski | 765c5ae | 2013-09-11 14:53:35 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 183 | static unsigned char *dfu_buf; |
Lukasz Majewski | a7e6892 | 2015-07-08 23:43:18 +0200 | [diff] [blame] | 184 | static unsigned long dfu_buf_size; |
Patrick Delaunay | febabe3 | 2019-10-14 09:28:02 +0200 | [diff] [blame] | 185 | static enum dfu_device_type dfu_buf_device_type; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 186 | |
Lukasz Majewski | d427826 | 2013-10-08 14:30:39 +0200 | [diff] [blame] | 187 | unsigned char *dfu_free_buf(void) |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 188 | { |
| 189 | free(dfu_buf); |
| 190 | dfu_buf = NULL; |
| 191 | return dfu_buf; |
| 192 | } |
| 193 | |
Lukasz Majewski | 4fb1278 | 2013-12-09 16:20:13 +0100 | [diff] [blame] | 194 | unsigned long dfu_get_buf_size(void) |
| 195 | { |
| 196 | return dfu_buf_size; |
| 197 | } |
| 198 | |
Stephen Warren | 7ac1b41 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 199 | unsigned char *dfu_get_buf(struct dfu_entity *dfu) |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 200 | { |
| 201 | char *s; |
| 202 | |
Patrick Delaunay | febabe3 | 2019-10-14 09:28:02 +0200 | [diff] [blame] | 203 | /* manage several entity with several contraint */ |
| 204 | if (dfu_buf && dfu->dev_type != dfu_buf_device_type) |
| 205 | dfu_free_buf(); |
| 206 | |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 207 | if (dfu_buf != NULL) |
| 208 | return dfu_buf; |
| 209 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 210 | s = env_get("dfu_bufsiz"); |
Przemyslaw Marczak | f597fc3 | 2014-12-15 10:34:11 +0100 | [diff] [blame] | 211 | if (s) |
| 212 | dfu_buf_size = (unsigned long)simple_strtol(s, NULL, 0); |
| 213 | |
| 214 | if (!s || !dfu_buf_size) |
| 215 | dfu_buf_size = CONFIG_SYS_DFU_DATA_BUF_SIZE; |
| 216 | |
Stephen Warren | 7ac1b41 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 217 | if (dfu->max_buf_size && dfu_buf_size > dfu->max_buf_size) |
| 218 | dfu_buf_size = dfu->max_buf_size; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 219 | |
| 220 | dfu_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, dfu_buf_size); |
| 221 | if (dfu_buf == NULL) |
| 222 | printf("%s: Could not memalign 0x%lx bytes\n", |
| 223 | __func__, dfu_buf_size); |
| 224 | |
Patrick Delaunay | febabe3 | 2019-10-14 09:28:02 +0200 | [diff] [blame] | 225 | dfu_buf_device_type = dfu->dev_type; |
Heiko Schocher | e7e75c7 | 2013-06-12 06:05:51 +0200 | [diff] [blame] | 226 | return dfu_buf; |
| 227 | } |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 228 | |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 229 | static char *dfu_get_hash_algo(void) |
| 230 | { |
| 231 | char *s; |
| 232 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 233 | s = env_get("dfu_hash_algo"); |
Lukasz Majewski | 3d83e67 | 2014-06-10 12:25:59 +0200 | [diff] [blame] | 234 | if (!s) |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 235 | return NULL; |
| 236 | |
Lukasz Majewski | 3d83e67 | 2014-06-10 12:25:59 +0200 | [diff] [blame] | 237 | if (!strcmp(s, "crc32")) { |
| 238 | debug("%s: DFU hash method: %s\n", __func__, s); |
| 239 | return s; |
| 240 | } |
| 241 | |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 242 | pr_err("DFU hash method: %s not supported!\n", s); |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 243 | return NULL; |
| 244 | } |
| 245 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 246 | static int dfu_write_buffer_drain(struct dfu_entity *dfu) |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 247 | { |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 248 | long w_size; |
| 249 | int ret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 250 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 251 | /* flush size? */ |
| 252 | w_size = dfu->i_buf - dfu->i_buf_start; |
| 253 | if (w_size == 0) |
| 254 | return 0; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 255 | |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 256 | if (dfu_hash_algo) |
| 257 | dfu_hash_algo->hash_update(dfu_hash_algo, &dfu->crc, |
| 258 | dfu->i_buf_start, w_size, 0); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 259 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 260 | ret = dfu->write_medium(dfu, dfu->offset, dfu->i_buf_start, &w_size); |
| 261 | if (ret) |
| 262 | debug("%s: Write error!\n", __func__); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 263 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 264 | /* point back */ |
| 265 | dfu->i_buf = dfu->i_buf_start; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 266 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 267 | /* update offset */ |
| 268 | dfu->offset += w_size; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 269 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 270 | puts("#"); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 271 | |
| 272 | return ret; |
| 273 | } |
| 274 | |
Patrick Delaunay | 57da060 | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 275 | void dfu_transaction_cleanup(struct dfu_entity *dfu) |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 276 | { |
| 277 | /* clear everything */ |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 278 | dfu->crc = 0; |
| 279 | dfu->offset = 0; |
| 280 | dfu->i_blk_seq_num = 0; |
Patrick Delaunay | 6fa8ddd | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 281 | dfu->i_buf_start = dfu_get_buf(dfu); |
| 282 | dfu->i_buf_end = dfu->i_buf_start; |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 283 | dfu->i_buf = dfu->i_buf_start; |
Patrick Delaunay | 57da060 | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 284 | dfu->r_left = 0; |
| 285 | dfu->b_left = 0; |
| 286 | dfu->bad_skip = 0; |
| 287 | |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 288 | dfu->inited = 0; |
| 289 | } |
| 290 | |
Patrick Delaunay | 6fa8ddd | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 291 | int dfu_transaction_initiate(struct dfu_entity *dfu, bool read) |
| 292 | { |
| 293 | int ret = 0; |
| 294 | |
| 295 | if (dfu->inited) |
| 296 | return 0; |
| 297 | |
| 298 | dfu_transaction_cleanup(dfu); |
| 299 | |
| 300 | if (dfu->i_buf_start == NULL) |
| 301 | return -ENOMEM; |
| 302 | |
| 303 | dfu->i_buf_end = dfu->i_buf_start + dfu_get_buf_size(); |
| 304 | |
| 305 | if (read) { |
| 306 | ret = dfu->get_medium_size(dfu, &dfu->r_left); |
| 307 | if (ret < 0) |
| 308 | return ret; |
| 309 | debug("%s: %s %lld [B]\n", __func__, dfu->name, dfu->r_left); |
| 310 | } |
| 311 | |
| 312 | dfu->inited = 1; |
Patrick Delaunay | 067c13c | 2019-10-14 09:28:07 +0200 | [diff] [blame] | 313 | dfu_initiated_callback(dfu); |
Patrick Delaunay | 6fa8ddd | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
Heiko Schocher | a2199af | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 318 | int dfu_flush(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 319 | { |
| 320 | int ret = 0; |
| 321 | |
Przemyslaw Marczak | 1aa4bdc | 2014-05-12 12:05:33 +0200 | [diff] [blame] | 322 | ret = dfu_write_buffer_drain(dfu); |
| 323 | if (ret) |
| 324 | return ret; |
| 325 | |
Heiko Schocher | a2199af | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 326 | if (dfu->flush_medium) |
| 327 | ret = dfu->flush_medium(dfu); |
| 328 | |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 329 | if (dfu_hash_algo) |
| 330 | printf("\nDFU complete %s: 0x%08x\n", dfu_hash_algo->name, |
| 331 | dfu->crc); |
Heiko Schocher | a2199af | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 332 | |
Patrick Delaunay | 067c13c | 2019-10-14 09:28:07 +0200 | [diff] [blame] | 333 | dfu_flush_callback(dfu); |
| 334 | |
Patrick Delaunay | 57da060 | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 335 | dfu_transaction_cleanup(dfu); |
Heiko Schocher | a2199af | 2014-03-18 08:09:55 +0100 | [diff] [blame] | 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 340 | 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] | 341 | { |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 342 | int ret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 343 | |
Stephen Warren | e621c7a | 2015-07-22 14:54:04 -0600 | [diff] [blame] | 344 | 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] | 345 | __func__, dfu->name, buf, size, blk_seq_num, dfu->offset, |
Stephen Warren | e621c7a | 2015-07-22 14:54:04 -0600 | [diff] [blame] | 346 | (unsigned long)(dfu->i_buf - dfu->i_buf_start)); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 347 | |
Patrick Delaunay | 6fa8ddd | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 348 | ret = dfu_transaction_initiate(dfu, false); |
| 349 | if (ret < 0) |
| 350 | return ret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 351 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 352 | if (dfu->i_blk_seq_num != blk_seq_num) { |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 353 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 354 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
Patrick Delaunay | 57da060 | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 355 | dfu_transaction_cleanup(dfu); |
Patrick Delaunay | d471032 | 2021-05-18 15:12:12 +0200 | [diff] [blame] | 356 | dfu_error_callback(dfu, "Wrong sequence number"); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 357 | return -1; |
| 358 | } |
| 359 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 360 | /* DFU 1.1 standard says: |
| 361 | * The wBlockNum field is a block sequence number. It increments each |
| 362 | * time a block is transferred, wrapping to zero from 65,535. It is used |
| 363 | * to provide useful context to the DFU loader in the device." |
| 364 | * |
| 365 | * This means that it's a 16 bit counter that roll-overs at |
| 366 | * 0xffff -> 0x0000. By having a typical 4K transfer block |
| 367 | * we roll-over at exactly 256MB. Not very fun to debug. |
| 368 | * |
| 369 | * Handling rollover, and having an inited variable, |
| 370 | * makes things work. |
| 371 | */ |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 372 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 373 | /* handle rollover */ |
| 374 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
| 375 | |
| 376 | /* flush buffer if overflow */ |
| 377 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 378 | ret = dfu_write_buffer_drain(dfu); |
| 379 | if (ret) { |
Patrick Delaunay | 57da060 | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 380 | dfu_transaction_cleanup(dfu); |
Patrick Delaunay | d471032 | 2021-05-18 15:12:12 +0200 | [diff] [blame] | 381 | dfu_error_callback(dfu, "DFU write error"); |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 382 | return ret; |
| 383 | } |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 384 | } |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 385 | |
| 386 | /* we should be in buffer now (if not then size too large) */ |
| 387 | if ((dfu->i_buf + size) > dfu->i_buf_end) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 388 | 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] | 389 | size, dfu->i_buf_end); |
Patrick Delaunay | 57da060 | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 390 | dfu_transaction_cleanup(dfu); |
Patrick Delaunay | d471032 | 2021-05-18 15:12:12 +0200 | [diff] [blame] | 391 | dfu_error_callback(dfu, "Buffer overflow"); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | memcpy(dfu->i_buf, buf, size); |
| 396 | dfu->i_buf += size; |
| 397 | |
| 398 | /* if end or if buffer full flush */ |
| 399 | if (size == 0 || (dfu->i_buf + size) > dfu->i_buf_end) { |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 400 | ret = dfu_write_buffer_drain(dfu); |
| 401 | if (ret) { |
Patrick Delaunay | 57da060 | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 402 | dfu_transaction_cleanup(dfu); |
Patrick Delaunay | d471032 | 2021-05-18 15:12:12 +0200 | [diff] [blame] | 403 | dfu_error_callback(dfu, "DFU write error"); |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 404 | return ret; |
| 405 | } |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Stephen Warren | 3ee9593 | 2014-06-11 12:48:08 -0600 | [diff] [blame] | 408 | return 0; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static int dfu_read_buffer_fill(struct dfu_entity *dfu, void *buf, int size) |
| 412 | { |
| 413 | long chunk; |
| 414 | int ret, readn; |
| 415 | |
| 416 | readn = 0; |
| 417 | while (size > 0) { |
| 418 | /* get chunk that can be read */ |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 419 | chunk = min((long)size, dfu->b_left); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 420 | /* consume */ |
| 421 | if (chunk > 0) { |
| 422 | memcpy(buf, dfu->i_buf, chunk); |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 423 | if (dfu_hash_algo) |
| 424 | dfu_hash_algo->hash_update(dfu_hash_algo, |
| 425 | &dfu->crc, buf, |
| 426 | chunk, 0); |
| 427 | |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 428 | dfu->i_buf += chunk; |
| 429 | dfu->b_left -= chunk; |
| 430 | size -= chunk; |
| 431 | buf += chunk; |
| 432 | readn += chunk; |
| 433 | } |
| 434 | |
| 435 | /* all done */ |
| 436 | if (size > 0) { |
| 437 | /* no more to read */ |
| 438 | if (dfu->r_left == 0) |
| 439 | break; |
| 440 | |
| 441 | dfu->i_buf = dfu->i_buf_start; |
| 442 | dfu->b_left = dfu->i_buf_end - dfu->i_buf_start; |
| 443 | |
| 444 | /* got to read, but buffer is empty */ |
| 445 | if (dfu->b_left > dfu->r_left) |
| 446 | dfu->b_left = dfu->r_left; |
| 447 | ret = dfu->read_medium(dfu, dfu->offset, dfu->i_buf, |
| 448 | &dfu->b_left); |
| 449 | if (ret != 0) { |
| 450 | debug("%s: Read error!\n", __func__); |
| 451 | return ret; |
| 452 | } |
Patrick Delaunay | 0de1022 | 2019-10-14 09:28:03 +0200 | [diff] [blame] | 453 | if (dfu->b_left == 0) |
| 454 | break; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 455 | dfu->offset += dfu->b_left; |
| 456 | dfu->r_left -= dfu->b_left; |
| 457 | |
| 458 | puts("#"); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return readn; |
| 463 | } |
| 464 | |
| 465 | int dfu_read(struct dfu_entity *dfu, void *buf, int size, int blk_seq_num) |
| 466 | { |
| 467 | int ret = 0; |
| 468 | |
| 469 | debug("%s: name: %s buf: 0x%p size: 0x%x p_num: 0x%x i_buf: 0x%p\n", |
| 470 | __func__, dfu->name, buf, size, blk_seq_num, dfu->i_buf); |
| 471 | |
Patrick Delaunay | 6fa8ddd | 2017-07-19 16:39:25 +0200 | [diff] [blame] | 472 | ret = dfu_transaction_initiate(dfu, true); |
| 473 | if (ret < 0) |
| 474 | return ret; |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 475 | |
| 476 | if (dfu->i_blk_seq_num != blk_seq_num) { |
| 477 | printf("%s: Wrong sequence number! [%d] [%d]\n", |
| 478 | __func__, dfu->i_blk_seq_num, blk_seq_num); |
| 479 | return -1; |
| 480 | } |
| 481 | /* handle rollover */ |
| 482 | dfu->i_blk_seq_num = (dfu->i_blk_seq_num + 1) & 0xffff; |
| 483 | |
| 484 | ret = dfu_read_buffer_fill(dfu, buf, size); |
| 485 | if (ret < 0) { |
| 486 | printf("%s: Failed to fill buffer\n", __func__); |
| 487 | return -1; |
| 488 | } |
| 489 | |
| 490 | if (ret < size) { |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 491 | if (dfu_hash_algo) |
| 492 | debug("%s: %s %s: 0x%x\n", __func__, dfu->name, |
| 493 | dfu_hash_algo->name, dfu->crc); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 494 | puts("\nUPLOAD ... done\nCtrl+C to exit ...\n"); |
| 495 | |
Patrick Delaunay | 57da060 | 2017-07-19 16:39:24 +0200 | [diff] [blame] | 496 | dfu_transaction_cleanup(dfu); |
Pantelis Antoniou | ea2453d | 2013-03-14 05:32:48 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | 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] | 503 | char *interface, char *devstr) |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 504 | { |
Masami Hiramatsu | 53b4063 | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 505 | char *argv[DFU_MAX_ENTITY_ARGS]; |
| 506 | int argc; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 507 | char *st; |
| 508 | |
Stephen Warren | dd64827 | 2014-06-11 16:03:33 -0600 | [diff] [blame] | 509 | debug("%s: %s interface: %s dev: %s\n", __func__, s, interface, devstr); |
Masami Hiramatsu | 8db74c1 | 2022-01-31 11:52:29 +0900 | [diff] [blame] | 510 | st = strsep(&s, " \t"); |
Masami Hiramatsu | d8ae90a | 2022-01-31 11:52:20 +0900 | [diff] [blame] | 511 | strlcpy(dfu->name, st, DFU_NAME_SIZE); |
Masami Hiramatsu | 53b4063 | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 512 | |
| 513 | /* Parse arguments */ |
| 514 | for (argc = 0; s && argc < DFU_MAX_ENTITY_ARGS; argc++) { |
| 515 | s = skip_spaces(s); |
| 516 | if (!*s) |
| 517 | break; |
| 518 | argv[argc] = strsep(&s, " \t"); |
| 519 | } |
| 520 | |
| 521 | if (argc == DFU_MAX_ENTITY_ARGS && s) { |
| 522 | s = skip_spaces(s); |
| 523 | if (*s) { |
| 524 | log_err("Too many arguments for %s\n", dfu->name); |
| 525 | return -EINVAL; |
| 526 | } |
| 527 | } |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 528 | |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 529 | dfu->alt = alt; |
Stephen Warren | 7ac1b41 | 2014-06-11 16:03:34 -0600 | [diff] [blame] | 530 | dfu->max_buf_size = 0; |
Stephen Warren | cb7bd2e | 2014-06-11 16:03:35 -0600 | [diff] [blame] | 531 | dfu->free_entity = NULL; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 532 | |
| 533 | /* Specific for mmc device */ |
| 534 | if (strcmp(interface, "mmc") == 0) { |
Masami Hiramatsu | 53b4063 | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 535 | if (dfu_fill_entity_mmc(dfu, devstr, argv, argc)) |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 536 | return -1; |
Patrick Delaunay | 6015af2 | 2019-10-14 09:28:04 +0200 | [diff] [blame] | 537 | } else if (strcmp(interface, "mtd") == 0) { |
Masami Hiramatsu | 53b4063 | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 538 | if (dfu_fill_entity_mtd(dfu, devstr, argv, argc)) |
Patrick Delaunay | 6015af2 | 2019-10-14 09:28:04 +0200 | [diff] [blame] | 539 | return -1; |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 540 | } else if (strcmp(interface, "nand") == 0) { |
Masami Hiramatsu | 53b4063 | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 541 | if (dfu_fill_entity_nand(dfu, devstr, argv, argc)) |
Pantelis Antoniou | c663176 | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 542 | return -1; |
Afzal Mohammed | a9479f0 | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 543 | } else if (strcmp(interface, "ram") == 0) { |
Masami Hiramatsu | 53b4063 | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 544 | if (dfu_fill_entity_ram(dfu, devstr, argv, argc)) |
Afzal Mohammed | a9479f0 | 2013-09-18 01:15:24 +0530 | [diff] [blame] | 545 | return -1; |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 546 | } else if (strcmp(interface, "sf") == 0) { |
Masami Hiramatsu | 53b4063 | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 547 | if (dfu_fill_entity_sf(dfu, devstr, argv, argc)) |
Stephen Warren | 6f12ebf | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 548 | return -1; |
Patrick Delaunay | ec44cac | 2019-10-14 09:28:06 +0200 | [diff] [blame] | 549 | } else if (strcmp(interface, "virt") == 0) { |
Masami Hiramatsu | 53b4063 | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 550 | if (dfu_fill_entity_virt(dfu, devstr, argv, argc)) |
Patrick Delaunay | ec44cac | 2019-10-14 09:28:06 +0200 | [diff] [blame] | 551 | return -1; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 552 | } else { |
| 553 | printf("%s: Device %s not (yet) supported!\n", |
| 554 | __func__, interface); |
| 555 | return -1; |
| 556 | } |
Stephen Warren | 806bd24 | 2015-09-04 22:03:46 -0600 | [diff] [blame] | 557 | dfu_get_buf(dfu); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | void dfu_free_entities(void) |
| 563 | { |
| 564 | struct dfu_entity *dfu, *p, *t = NULL; |
| 565 | |
Stephen Warren | 806bd24 | 2015-09-04 22:03:46 -0600 | [diff] [blame] | 566 | dfu_free_buf(); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 567 | list_for_each_entry_safe_reverse(dfu, p, &dfu_list, list) { |
| 568 | list_del(&dfu->list); |
Stephen Warren | cb7bd2e | 2014-06-11 16:03:35 -0600 | [diff] [blame] | 569 | if (dfu->free_entity) |
| 570 | dfu->free_entity(dfu); |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 571 | t = dfu; |
| 572 | } |
| 573 | if (t) |
| 574 | free(t); |
| 575 | INIT_LIST_HEAD(&dfu_list); |
Przemyslaw Marczak | b627eb4 | 2014-02-28 18:53:37 +0100 | [diff] [blame] | 576 | |
| 577 | alt_num_cnt = 0; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Patrick Delaunay | 9ada683 | 2019-10-14 09:28:01 +0200 | [diff] [blame] | 580 | int dfu_alt_init(int num, struct dfu_entity **dfu) |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 581 | { |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 582 | char *s; |
Patrick Delaunay | 9ada683 | 2019-10-14 09:28:01 +0200 | [diff] [blame] | 583 | int ret; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 584 | |
Patrick Delaunay | 9ada683 | 2019-10-14 09:28:01 +0200 | [diff] [blame] | 585 | dfu_alt_num = num; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 586 | debug("%s: dfu_alt_num=%d\n", __func__, dfu_alt_num); |
| 587 | |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 588 | dfu_hash_algo = NULL; |
| 589 | s = dfu_get_hash_algo(); |
| 590 | if (s) { |
| 591 | ret = hash_lookup_algo(s, &dfu_hash_algo); |
| 592 | if (ret) |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 593 | pr_err("Hash algorithm %s not supported\n", s); |
Lukasz Majewski | bd69424 | 2014-05-12 10:43:36 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Patrick Delaunay | 9ada683 | 2019-10-14 09:28:01 +0200 | [diff] [blame] | 596 | *dfu = calloc(sizeof(struct dfu_entity), dfu_alt_num); |
| 597 | if (!*dfu) |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 598 | return -1; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 599 | |
Patrick Delaunay | 9ada683 | 2019-10-14 09:28:01 +0200 | [diff] [blame] | 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | int dfu_alt_add(struct dfu_entity *dfu, char *interface, char *devstr, char *s) |
| 604 | { |
| 605 | struct dfu_entity *p_dfu; |
| 606 | int ret; |
| 607 | |
| 608 | if (alt_num_cnt >= dfu_alt_num) |
| 609 | return -1; |
| 610 | |
| 611 | p_dfu = &dfu[alt_num_cnt]; |
| 612 | ret = dfu_fill_entity(p_dfu, s, alt_num_cnt, interface, devstr); |
| 613 | if (ret) |
| 614 | return -1; |
| 615 | |
| 616 | list_add_tail(&p_dfu->list, &dfu_list); |
| 617 | alt_num_cnt++; |
| 618 | |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | int dfu_config_entities(char *env, char *interface, char *devstr) |
| 623 | { |
| 624 | struct dfu_entity *dfu; |
| 625 | int i, ret; |
| 626 | char *s; |
| 627 | |
| 628 | ret = dfu_alt_init(dfu_find_alt_num(env), &dfu); |
| 629 | if (ret) |
| 630 | return -1; |
| 631 | |
| 632 | for (i = 0; i < dfu_alt_num; i++) { |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 633 | s = strsep(&env, ";"); |
Ravi Gunasekaran | de06083 | 2023-05-18 12:06:23 +0530 | [diff] [blame] | 634 | s = skip_spaces(s); |
Patrick Delaunay | 9ada683 | 2019-10-14 09:28:01 +0200 | [diff] [blame] | 635 | ret = dfu_alt_add(dfu, interface, devstr, s); |
Peng Fan | 5d8fae7 | 2016-05-03 10:24:52 +0800 | [diff] [blame] | 636 | if (ret) { |
Sam Protsenko | feaa785 | 2018-07-13 16:35:45 +0300 | [diff] [blame] | 637 | /* We will free "dfu" in dfu_free_entities() */ |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 638 | return -1; |
Peng Fan | 5d8fae7 | 2016-05-03 10:24:52 +0800 | [diff] [blame] | 639 | } |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | const char *dfu_get_dev_type(enum dfu_device_type t) |
| 646 | { |
Patrick Delaunay | 909b690 | 2019-10-14 09:27:58 +0200 | [diff] [blame] | 647 | const char *const dev_t[] = {NULL, "eMMC", "OneNAND", "NAND", "RAM", |
Patrick Delaunay | ec44cac | 2019-10-14 09:28:06 +0200 | [diff] [blame] | 648 | "SF", "MTD", "VIRT"}; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 649 | return dev_t[t]; |
| 650 | } |
| 651 | |
| 652 | const char *dfu_get_layout(enum dfu_layout l) |
| 653 | { |
Patrick Delaunay | 909b690 | 2019-10-14 09:27:58 +0200 | [diff] [blame] | 654 | const char *const dfu_layout[] = {NULL, "RAW_ADDR", "FAT", "EXT2", |
Marek Szyprowski | c533f94 | 2020-12-22 11:32:22 +0100 | [diff] [blame] | 655 | "EXT3", "EXT4", "RAM_ADDR", "SKIP", |
| 656 | "SCRIPT" }; |
Lukasz Majewski | f22b11c | 2012-08-06 14:41:07 +0200 | [diff] [blame] | 657 | return dfu_layout[l]; |
| 658 | } |
| 659 | |
| 660 | void dfu_show_entities(void) |
| 661 | { |
| 662 | struct dfu_entity *dfu; |
| 663 | |
| 664 | puts("DFU alt settings list:\n"); |
| 665 | |
| 666 | list_for_each_entry(dfu, &dfu_list, list) { |
| 667 | printf("dev: %s alt: %d name: %s layout: %s\n", |
| 668 | dfu_get_dev_type(dfu->dev_type), dfu->alt, |
| 669 | dfu->name, dfu_get_layout(dfu->layout)); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | int dfu_get_alt_number(void) |
| 674 | { |
| 675 | return dfu_alt_num; |
| 676 | } |
| 677 | |
| 678 | struct dfu_entity *dfu_get_entity(int alt) |
| 679 | { |
| 680 | struct dfu_entity *dfu; |
| 681 | |
| 682 | list_for_each_entry(dfu, &dfu_list, list) { |
| 683 | if (dfu->alt == alt) |
| 684 | return dfu; |
| 685 | } |
| 686 | |
| 687 | return NULL; |
| 688 | } |
Lukasz Majewski | fed936e | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 689 | |
| 690 | int dfu_get_alt(char *name) |
| 691 | { |
| 692 | struct dfu_entity *dfu; |
Lukasz Majewski | 5610b05 | 2014-11-05 10:54:16 +0100 | [diff] [blame] | 693 | char *str; |
Lukasz Majewski | fed936e | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 694 | |
| 695 | list_for_each_entry(dfu, &dfu_list, list) { |
Lukasz Majewski | 5610b05 | 2014-11-05 10:54:16 +0100 | [diff] [blame] | 696 | if (dfu->name[0] != '/') { |
| 697 | if (!strncmp(dfu->name, name, strlen(dfu->name))) |
| 698 | return dfu->alt; |
| 699 | } else { |
| 700 | /* |
| 701 | * One must also consider absolute path |
| 702 | * (/boot/bin/uImage) available at dfu->name when |
| 703 | * compared "plain" file name (uImage) |
| 704 | * |
| 705 | * It is the case for e.g. thor gadget where lthor SW |
| 706 | * sends only the file name, so only the very last part |
| 707 | * of path must be checked for equality |
| 708 | */ |
| 709 | |
| 710 | str = strstr(dfu->name, name); |
| 711 | if (!str) |
| 712 | continue; |
| 713 | |
| 714 | /* |
| 715 | * Check if matching substring is the last element of |
| 716 | * dfu->name (uImage) |
| 717 | */ |
| 718 | if (strlen(dfu->name) == |
| 719 | ((str - dfu->name) + strlen(name))) |
| 720 | return dfu->alt; |
| 721 | } |
Lukasz Majewski | fed936e | 2013-10-08 14:30:38 +0200 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | return -ENODEV; |
| 725 | } |
Lukasz Majewski | 2092e46 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 726 | |
| 727 | int dfu_write_from_mem_addr(struct dfu_entity *dfu, void *buf, int size) |
| 728 | { |
| 729 | unsigned long dfu_buf_size, write, left = size; |
| 730 | int i, ret = 0; |
| 731 | void *dp = buf; |
| 732 | |
| 733 | /* |
| 734 | * Here we must call dfu_get_buf(dfu) first to be sure that dfu_buf_size |
| 735 | * has been properly initialized - e.g. if "dfu_bufsiz" has been taken |
| 736 | * into account. |
| 737 | */ |
| 738 | dfu_get_buf(dfu); |
| 739 | dfu_buf_size = dfu_get_buf_size(); |
| 740 | debug("%s: dfu buf size: %lu\n", __func__, dfu_buf_size); |
| 741 | |
| 742 | for (i = 0; left > 0; i++) { |
| 743 | write = min(dfu_buf_size, left); |
| 744 | |
| 745 | debug("%s: dp: 0x%p left: %lu write: %lu\n", __func__, |
| 746 | dp, left, write); |
| 747 | ret = dfu_write(dfu, dp, write, i); |
| 748 | if (ret) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 749 | pr_err("DFU write failed\n"); |
Lukasz Majewski | 2092e46 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 750 | return ret; |
| 751 | } |
| 752 | |
| 753 | dp += write; |
| 754 | left -= write; |
| 755 | } |
| 756 | |
| 757 | ret = dfu_flush(dfu, NULL, 0, i); |
| 758 | if (ret) |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 759 | pr_err("DFU flush failed!"); |
Heinrich Schuchardt | f8d7e6e | 2021-03-05 18:36:37 +0100 | [diff] [blame] | 760 | puts("\n"); |
Lukasz Majewski | 2092e46 | 2015-08-24 00:21:46 +0200 | [diff] [blame] | 761 | |
| 762 | return ret; |
| 763 | } |