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