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