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