Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2008 Semihalf |
| 4 | * |
| 5 | * Written by: Rafal Czubak <rcz@semihalf.com> |
| 6 | * Bartlomiej Sieka <tur@semihalf.com> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 11 | |
| 12 | #if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT)) |
| 13 | #error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature" |
| 14 | #endif |
| 15 | |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 16 | #if defined(CONFIG_UPDATE_TFTP) && !defined(CONFIG_MTD_NOR_FLASH) |
| 17 | #error "CONFIG_UPDATE_TFTP and !CONFIG_MTD_NOR_FLASH needed for legacy behaviour" |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 18 | #endif |
| 19 | |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 20 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 21 | #include <env.h> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 22 | #include <flash.h> |
| 23 | #include <net.h> |
Kim Phillips | 40c2c03 | 2012-10-29 13:34:33 +0000 | [diff] [blame] | 24 | #include <net/tftp.h> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 25 | #include <malloc.h> |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 26 | #include <dfu.h> |
| 27 | #include <errno.h> |
Marek Vasut | c646851 | 2018-02-10 16:22:07 +0100 | [diff] [blame] | 28 | #include <mtd/cfi_flash.h> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 29 | |
| 30 | /* env variable holding the location of the update file */ |
| 31 | #define UPDATE_FILE_ENV "updatefile" |
| 32 | |
| 33 | /* set configuration defaults if needed */ |
| 34 | #ifndef CONFIG_UPDATE_LOAD_ADDR |
| 35 | #define CONFIG_UPDATE_LOAD_ADDR 0x100000 |
| 36 | #endif |
| 37 | |
| 38 | #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX |
| 39 | #define CONFIG_UPDATE_TFTP_MSEC_MAX 100 |
| 40 | #endif |
| 41 | |
| 42 | #ifndef CONFIG_UPDATE_TFTP_CNT_MAX |
| 43 | #define CONFIG_UPDATE_TFTP_CNT_MAX 0 |
| 44 | #endif |
| 45 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 46 | extern ulong tftp_timeout_ms; |
| 47 | extern int tftp_timeout_count_max; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 48 | extern ulong load_addr; |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 49 | #ifdef CONFIG_MTD_NOR_FLASH |
Lukasz Majewski | dc65f6d | 2015-08-24 00:21:44 +0200 | [diff] [blame] | 50 | extern flash_info_t flash_info[]; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 51 | static uchar *saved_prot_info; |
Lukasz Majewski | dc65f6d | 2015-08-24 00:21:44 +0200 | [diff] [blame] | 52 | #endif |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 53 | static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr) |
| 54 | { |
| 55 | int size, rv; |
| 56 | ulong saved_timeout_msecs; |
| 57 | int saved_timeout_count; |
| 58 | char *saved_netretry, *saved_bootfile; |
| 59 | |
| 60 | rv = 0; |
| 61 | /* save used globals and env variable */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 62 | saved_timeout_msecs = tftp_timeout_ms; |
| 63 | saved_timeout_count = tftp_timeout_count_max; |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 64 | saved_netretry = strdup(env_get("netretry")); |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 65 | saved_bootfile = strdup(net_boot_file_name); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 66 | |
| 67 | /* set timeouts for auto-update */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 68 | tftp_timeout_ms = msec_max; |
| 69 | tftp_timeout_count_max = cnt_max; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 70 | |
| 71 | /* we don't want to retry the connection if errors occur */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 72 | env_set("netretry", "no"); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 73 | |
| 74 | /* download the update file */ |
| 75 | load_addr = addr; |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 76 | copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name)); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 77 | size = net_loop(TFTPGET); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 78 | |
| 79 | if (size < 0) |
| 80 | rv = 1; |
| 81 | else if (size > 0) |
| 82 | flush_cache(addr, size); |
| 83 | |
| 84 | /* restore changed globals and env variable */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 85 | tftp_timeout_ms = saved_timeout_msecs; |
| 86 | tftp_timeout_count_max = saved_timeout_count; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 87 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 88 | env_set("netretry", saved_netretry); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 89 | if (saved_netretry != NULL) |
| 90 | free(saved_netretry); |
| 91 | |
| 92 | if (saved_bootfile != NULL) { |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 93 | copy_filename(net_boot_file_name, saved_bootfile, |
| 94 | sizeof(net_boot_file_name)); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 95 | free(saved_bootfile); |
| 96 | } |
| 97 | |
| 98 | return rv; |
| 99 | } |
| 100 | |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 101 | #ifdef CONFIG_MTD_NOR_FLASH |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 102 | static int update_flash_protect(int prot, ulong addr_first, ulong addr_last) |
| 103 | { |
| 104 | uchar *sp_info_ptr; |
| 105 | ulong s; |
| 106 | int i, bank, cnt; |
| 107 | flash_info_t *info; |
| 108 | |
| 109 | sp_info_ptr = NULL; |
| 110 | |
| 111 | if (prot == 0) { |
| 112 | saved_prot_info = |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 113 | calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 114 | if (!saved_prot_info) |
| 115 | return 1; |
| 116 | } |
| 117 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 118 | for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) { |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 119 | cnt = 0; |
| 120 | info = &flash_info[bank]; |
| 121 | |
| 122 | /* Nothing to do if the bank doesn't exist */ |
| 123 | if (info->sector_count == 0) |
| 124 | return 0; |
| 125 | |
| 126 | /* Point to current bank protection information */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 127 | sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * Adjust addr_first or addr_last if we are on bank boundary. |
| 131 | * Address space between banks must be continuous for other |
| 132 | * flash functions (like flash_sect_erase or flash_write) to |
| 133 | * succeed. Banks must also be numbered in correct order, |
| 134 | * according to increasing addresses. |
| 135 | */ |
| 136 | if (addr_last > info->start[0] + info->size - 1) |
| 137 | addr_last = info->start[0] + info->size - 1; |
| 138 | if (addr_first < info->start[0]) |
| 139 | addr_first = info->start[0]; |
| 140 | |
| 141 | for (i = 0; i < info->sector_count; i++) { |
| 142 | /* Save current information about protected sectors */ |
| 143 | if (prot == 0) { |
| 144 | s = info->start[i]; |
| 145 | if ((s >= addr_first) && (s <= addr_last)) |
| 146 | sp_info_ptr[i] = info->protect[i]; |
| 147 | |
| 148 | } |
| 149 | |
| 150 | /* Protect/unprotect sectors */ |
| 151 | if (sp_info_ptr[i] == 1) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 152 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 153 | if (flash_real_protect(info, i, prot)) |
| 154 | return 1; |
| 155 | #else |
| 156 | info->protect[i] = prot; |
| 157 | #endif |
| 158 | cnt++; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (cnt) { |
| 163 | printf("%sProtected %d sectors\n", |
| 164 | prot ? "": "Un-", cnt); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if((prot == 1) && saved_prot_info) |
| 169 | free(saved_prot_info); |
| 170 | |
| 171 | return 0; |
| 172 | } |
Lukasz Majewski | dc65f6d | 2015-08-24 00:21:44 +0200 | [diff] [blame] | 173 | #endif |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 174 | |
| 175 | static int update_flash(ulong addr_source, ulong addr_first, ulong size) |
| 176 | { |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 177 | #ifdef CONFIG_MTD_NOR_FLASH |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 178 | ulong addr_last = addr_first + size - 1; |
| 179 | |
| 180 | /* round last address to the sector boundary */ |
| 181 | if (flash_sect_roundb(&addr_last) > 0) |
| 182 | return 1; |
| 183 | |
| 184 | if (addr_first >= addr_last) { |
| 185 | printf("Error: end address exceeds addressing space\n"); |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | /* remove protection on processed sectors */ |
| 190 | if (update_flash_protect(0, addr_first, addr_last) > 0) { |
| 191 | printf("Error: could not unprotect flash sectors\n"); |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last); |
| 196 | if (flash_sect_erase(addr_first, addr_last) > 0) { |
| 197 | printf("Error: could not erase flash\n"); |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | printf("Copying to flash..."); |
| 202 | if (flash_write((char *)addr_source, addr_first, size) > 0) { |
| 203 | printf("Error: could not copy to flash\n"); |
| 204 | return 1; |
| 205 | } |
| 206 | printf("done\n"); |
| 207 | |
| 208 | /* enable protection on processed sectors */ |
| 209 | if (update_flash_protect(1, addr_first, addr_last) > 0) { |
| 210 | printf("Error: could not protect flash sectors\n"); |
| 211 | return 1; |
| 212 | } |
Lukasz Majewski | dc65f6d | 2015-08-24 00:21:44 +0200 | [diff] [blame] | 213 | #endif |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int update_fit_getparams(const void *fit, int noffset, ulong *addr, |
| 218 | ulong *fladdr, ulong *size) |
| 219 | { |
| 220 | const void *data; |
| 221 | |
| 222 | if (fit_image_get_data(fit, noffset, &data, (size_t *)size)) |
| 223 | return 1; |
| 224 | |
| 225 | if (fit_image_get_load(fit, noffset, (ulong *)fladdr)) |
| 226 | return 1; |
| 227 | |
| 228 | *addr = (ulong)data; |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 233 | int update_tftp(ulong addr, char *interface, char *devstring) |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 234 | { |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 235 | char *filename, *env_addr, *fit_image_name; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 236 | ulong update_addr, update_fladdr, update_size; |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 237 | int images_noffset, ndepth, noffset; |
| 238 | bool update_tftp_dfu; |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 239 | int ret = 0; |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 240 | void *fit; |
| 241 | |
| 242 | if (interface == NULL && devstring == NULL) { |
| 243 | update_tftp_dfu = false; |
| 244 | } else if (interface && devstring) { |
| 245 | update_tftp_dfu = true; |
| 246 | } else { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 247 | pr_err("Interface: %s and devstring: %s not supported!\n", |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 248 | interface, devstring); |
| 249 | return -EINVAL; |
| 250 | } |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 251 | |
| 252 | /* use already present image */ |
| 253 | if (addr) |
| 254 | goto got_update_file; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 255 | |
| 256 | printf("Auto-update from TFTP: "); |
| 257 | |
| 258 | /* get the file name of the update file */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 259 | filename = env_get(UPDATE_FILE_ENV); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 260 | if (filename == NULL) { |
| 261 | printf("failed, env. variable '%s' not found\n", |
| 262 | UPDATE_FILE_ENV); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 263 | return 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | printf("trying update file '%s'\n", filename); |
| 267 | |
| 268 | /* get load address of downloaded update file */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 269 | env_addr = env_get("loadaddr"); |
| 270 | if (env_addr) |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 271 | addr = simple_strtoul(env_addr, NULL, 16); |
| 272 | else |
| 273 | addr = CONFIG_UPDATE_LOAD_ADDR; |
| 274 | |
| 275 | |
| 276 | if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX, |
| 277 | CONFIG_UPDATE_TFTP_CNT_MAX, addr)) { |
| 278 | printf("Can't load update file, aborting auto-update\n"); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 279 | return 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 282 | got_update_file: |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 283 | fit = (void *)addr; |
| 284 | |
| 285 | if (!fit_check_format((void *)fit)) { |
| 286 | printf("Bad FIT format of the update file, aborting " |
| 287 | "auto-update\n"); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 288 | return 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* process updates */ |
| 292 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 293 | |
| 294 | ndepth = 0; |
| 295 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 296 | while (noffset >= 0 && ndepth > 0) { |
| 297 | if (ndepth != 1) |
| 298 | goto next_node; |
| 299 | |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 300 | fit_image_name = (char *)fit_get_name(fit, noffset, NULL); |
| 301 | printf("Processing update '%s' :", fit_image_name); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 302 | |
Simon Glass | 7428ad1 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 303 | if (!fit_image_verify(fit, noffset)) { |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 304 | printf("Error: invalid update hash, aborting\n"); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 305 | ret = 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 306 | goto next_node; |
| 307 | } |
| 308 | |
| 309 | printf("\n"); |
| 310 | if (update_fit_getparams(fit, noffset, &update_addr, |
| 311 | &update_fladdr, &update_size)) { |
| 312 | printf("Error: can't get update parameteres, " |
| 313 | "aborting\n"); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 314 | ret = 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 315 | goto next_node; |
| 316 | } |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 317 | |
| 318 | if (!update_tftp_dfu) { |
| 319 | if (update_flash(update_addr, update_fladdr, |
| 320 | update_size)) { |
| 321 | printf("Error: can't flash update, aborting\n"); |
| 322 | ret = 1; |
| 323 | goto next_node; |
| 324 | } |
| 325 | } else if (fit_image_check_type(fit, noffset, |
| 326 | IH_TYPE_FIRMWARE)) { |
| 327 | ret = dfu_tftp_write(fit_image_name, update_addr, |
| 328 | update_size, interface, devstring); |
| 329 | if (ret) |
| 330 | return ret; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 331 | } |
| 332 | next_node: |
| 333 | noffset = fdt_next_node(fit, noffset, &ndepth); |
| 334 | } |
| 335 | |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 336 | return ret; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 337 | } |