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> |
Simon Glass | 85f1378 | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 11 | #include <image.h> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 12 | |
| 13 | #if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT)) |
| 14 | #error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature" |
| 15 | #endif |
| 16 | |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 17 | #if defined(CONFIG_UPDATE_TFTP) && !defined(CONFIG_MTD_NOR_FLASH) |
| 18 | #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] | 19 | #endif |
| 20 | |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 21 | #include <command.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 22 | #include <env.h> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 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> |
Heinrich Schuchardt | 8784b0b | 2020-07-22 17:46:02 +0200 | [diff] [blame] | 26 | #include <mapmem.h> |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 27 | #include <dfu.h> |
| 28 | #include <errno.h> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 29 | |
AKASHI Takahiro | 92c8080 | 2020-11-19 09:37:19 +0900 | [diff] [blame] | 30 | #if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP) |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 31 | /* env variable holding the location of the update file */ |
| 32 | #define UPDATE_FILE_ENV "updatefile" |
| 33 | |
| 34 | /* set configuration defaults if needed */ |
| 35 | #ifndef CONFIG_UPDATE_LOAD_ADDR |
| 36 | #define CONFIG_UPDATE_LOAD_ADDR 0x100000 |
| 37 | #endif |
| 38 | |
| 39 | #ifndef CONFIG_UPDATE_TFTP_MSEC_MAX |
| 40 | #define CONFIG_UPDATE_TFTP_MSEC_MAX 100 |
| 41 | #endif |
| 42 | |
| 43 | #ifndef CONFIG_UPDATE_TFTP_CNT_MAX |
| 44 | #define CONFIG_UPDATE_TFTP_CNT_MAX 0 |
| 45 | #endif |
| 46 | |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 47 | extern ulong tftp_timeout_ms; |
| 48 | extern int tftp_timeout_count_max; |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 49 | #ifdef CONFIG_MTD_NOR_FLASH |
Tom Rini | 063c938 | 2022-07-23 13:05:03 -0400 | [diff] [blame] | 50 | #include <flash.h> |
| 51 | #include <mtd/cfi_flash.h> |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 52 | static uchar *saved_prot_info; |
Lukasz Majewski | dc65f6d | 2015-08-24 00:21:44 +0200 | [diff] [blame] | 53 | #endif |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 54 | static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr) |
| 55 | { |
| 56 | int size, rv; |
| 57 | ulong saved_timeout_msecs; |
| 58 | int saved_timeout_count; |
| 59 | char *saved_netretry, *saved_bootfile; |
| 60 | |
| 61 | rv = 0; |
| 62 | /* save used globals and env variable */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 63 | saved_timeout_msecs = tftp_timeout_ms; |
| 64 | saved_timeout_count = tftp_timeout_count_max; |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 65 | saved_netretry = strdup(env_get("netretry")); |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 66 | saved_bootfile = strdup(net_boot_file_name); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 67 | |
| 68 | /* set timeouts for auto-update */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 69 | tftp_timeout_ms = msec_max; |
| 70 | tftp_timeout_count_max = cnt_max; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 71 | |
| 72 | /* we don't want to retry the connection if errors occur */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 73 | env_set("netretry", "no"); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 74 | |
| 75 | /* download the update file */ |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 76 | image_load_addr = addr; |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 77 | copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name)); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 78 | size = net_loop(TFTPGET); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 79 | |
| 80 | if (size < 0) |
| 81 | rv = 1; |
| 82 | else if (size > 0) |
| 83 | flush_cache(addr, size); |
| 84 | |
| 85 | /* restore changed globals and env variable */ |
Joe Hershberger | 6470159 | 2015-04-08 01:41:07 -0500 | [diff] [blame] | 86 | tftp_timeout_ms = saved_timeout_msecs; |
| 87 | tftp_timeout_count_max = saved_timeout_count; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 88 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 89 | env_set("netretry", saved_netretry); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 90 | if (saved_netretry != NULL) |
| 91 | free(saved_netretry); |
| 92 | |
| 93 | if (saved_bootfile != NULL) { |
Joe Hershberger | 290c899 | 2015-04-08 01:41:02 -0500 | [diff] [blame] | 94 | copy_filename(net_boot_file_name, saved_bootfile, |
| 95 | sizeof(net_boot_file_name)); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 96 | free(saved_bootfile); |
| 97 | } |
| 98 | |
| 99 | return rv; |
| 100 | } |
| 101 | |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 102 | #ifdef CONFIG_MTD_NOR_FLASH |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 103 | static int update_flash_protect(int prot, ulong addr_first, ulong addr_last) |
| 104 | { |
| 105 | uchar *sp_info_ptr; |
| 106 | ulong s; |
| 107 | int i, bank, cnt; |
| 108 | flash_info_t *info; |
| 109 | |
| 110 | sp_info_ptr = NULL; |
| 111 | |
| 112 | if (prot == 0) { |
| 113 | saved_prot_info = |
Patrick Delaunay | 6c5f560 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 114 | calloc(CFI_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 115 | if (!saved_prot_info) |
| 116 | return 1; |
| 117 | } |
| 118 | |
Patrick Delaunay | 6c5f560 | 2022-01-04 14:23:58 +0100 | [diff] [blame] | 119 | for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) { |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 120 | cnt = 0; |
| 121 | info = &flash_info[bank]; |
| 122 | |
| 123 | /* Nothing to do if the bank doesn't exist */ |
| 124 | if (info->sector_count == 0) |
| 125 | return 0; |
| 126 | |
| 127 | /* Point to current bank protection information */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 128 | sp_info_ptr = saved_prot_info + (bank * CONFIG_SYS_MAX_FLASH_SECT); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Adjust addr_first or addr_last if we are on bank boundary. |
| 132 | * Address space between banks must be continuous for other |
| 133 | * flash functions (like flash_sect_erase or flash_write) to |
| 134 | * succeed. Banks must also be numbered in correct order, |
| 135 | * according to increasing addresses. |
| 136 | */ |
| 137 | if (addr_last > info->start[0] + info->size - 1) |
| 138 | addr_last = info->start[0] + info->size - 1; |
| 139 | if (addr_first < info->start[0]) |
| 140 | addr_first = info->start[0]; |
| 141 | |
| 142 | for (i = 0; i < info->sector_count; i++) { |
| 143 | /* Save current information about protected sectors */ |
| 144 | if (prot == 0) { |
| 145 | s = info->start[i]; |
| 146 | if ((s >= addr_first) && (s <= addr_last)) |
| 147 | sp_info_ptr[i] = info->protect[i]; |
| 148 | |
| 149 | } |
| 150 | |
| 151 | /* Protect/unprotect sectors */ |
| 152 | if (sp_info_ptr[i] == 1) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 153 | #if defined(CONFIG_SYS_FLASH_PROTECTION) |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 154 | if (flash_real_protect(info, i, prot)) |
| 155 | return 1; |
| 156 | #else |
| 157 | info->protect[i] = prot; |
| 158 | #endif |
| 159 | cnt++; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | if (cnt) { |
| 164 | printf("%sProtected %d sectors\n", |
| 165 | prot ? "": "Un-", cnt); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if((prot == 1) && saved_prot_info) |
| 170 | free(saved_prot_info); |
| 171 | |
| 172 | return 0; |
| 173 | } |
Lukasz Majewski | dc65f6d | 2015-08-24 00:21:44 +0200 | [diff] [blame] | 174 | #endif |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 175 | |
| 176 | static int update_flash(ulong addr_source, ulong addr_first, ulong size) |
| 177 | { |
Masahiro Yamada | 8cea9b5 | 2017-02-11 22:43:54 +0900 | [diff] [blame] | 178 | #ifdef CONFIG_MTD_NOR_FLASH |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 179 | ulong addr_last = addr_first + size - 1; |
| 180 | |
| 181 | /* round last address to the sector boundary */ |
| 182 | if (flash_sect_roundb(&addr_last) > 0) |
| 183 | return 1; |
| 184 | |
| 185 | if (addr_first >= addr_last) { |
| 186 | printf("Error: end address exceeds addressing space\n"); |
| 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | /* remove protection on processed sectors */ |
| 191 | if (update_flash_protect(0, addr_first, addr_last) > 0) { |
| 192 | printf("Error: could not unprotect flash sectors\n"); |
| 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | printf("Erasing 0x%08lx - 0x%08lx", addr_first, addr_last); |
| 197 | if (flash_sect_erase(addr_first, addr_last) > 0) { |
| 198 | printf("Error: could not erase flash\n"); |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | printf("Copying to flash..."); |
| 203 | if (flash_write((char *)addr_source, addr_first, size) > 0) { |
| 204 | printf("Error: could not copy to flash\n"); |
| 205 | return 1; |
| 206 | } |
| 207 | printf("done\n"); |
| 208 | |
| 209 | /* enable protection on processed sectors */ |
| 210 | if (update_flash_protect(1, addr_first, addr_last) > 0) { |
| 211 | printf("Error: could not protect flash sectors\n"); |
| 212 | return 1; |
| 213 | } |
Lukasz Majewski | dc65f6d | 2015-08-24 00:21:44 +0200 | [diff] [blame] | 214 | #endif |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 215 | return 0; |
| 216 | } |
AKASHI Takahiro | 92c8080 | 2020-11-19 09:37:19 +0900 | [diff] [blame] | 217 | #endif /* CONFIG_DFU_TFTP || CONFIG_UPDATE_TFTP */ |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 218 | |
| 219 | static int update_fit_getparams(const void *fit, int noffset, ulong *addr, |
| 220 | ulong *fladdr, ulong *size) |
| 221 | { |
| 222 | const void *data; |
| 223 | |
| 224 | if (fit_image_get_data(fit, noffset, &data, (size_t *)size)) |
| 225 | return 1; |
| 226 | |
| 227 | if (fit_image_get_load(fit, noffset, (ulong *)fladdr)) |
| 228 | return 1; |
| 229 | |
| 230 | *addr = (ulong)data; |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
AKASHI Takahiro | 92c8080 | 2020-11-19 09:37:19 +0900 | [diff] [blame] | 235 | #if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP) |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 236 | int update_tftp(ulong addr, char *interface, char *devstring) |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 237 | { |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 238 | char *filename, *env_addr, *fit_image_name; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 239 | ulong update_addr, update_fladdr, update_size; |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 240 | int images_noffset, ndepth, noffset; |
| 241 | bool update_tftp_dfu; |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 242 | int ret = 0; |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 243 | void *fit; |
| 244 | |
| 245 | if (interface == NULL && devstring == NULL) { |
| 246 | update_tftp_dfu = false; |
| 247 | } else if (interface && devstring) { |
| 248 | update_tftp_dfu = true; |
| 249 | } else { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 250 | pr_err("Interface: %s and devstring: %s not supported!\n", |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 251 | interface, devstring); |
| 252 | return -EINVAL; |
| 253 | } |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 254 | |
| 255 | /* use already present image */ |
| 256 | if (addr) |
| 257 | goto got_update_file; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 258 | |
| 259 | printf("Auto-update from TFTP: "); |
| 260 | |
| 261 | /* get the file name of the update file */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 262 | filename = env_get(UPDATE_FILE_ENV); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 263 | if (filename == NULL) { |
| 264 | printf("failed, env. variable '%s' not found\n", |
| 265 | UPDATE_FILE_ENV); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 266 | return 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | printf("trying update file '%s'\n", filename); |
| 270 | |
| 271 | /* get load address of downloaded update file */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 272 | env_addr = env_get("loadaddr"); |
| 273 | if (env_addr) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 274 | addr = hextoul(env_addr, NULL); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 275 | else |
| 276 | addr = CONFIG_UPDATE_LOAD_ADDR; |
| 277 | |
| 278 | |
| 279 | if (update_load(filename, CONFIG_UPDATE_TFTP_MSEC_MAX, |
| 280 | CONFIG_UPDATE_TFTP_CNT_MAX, addr)) { |
| 281 | printf("Can't load update file, aborting auto-update\n"); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 282 | return 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 285 | got_update_file: |
Heinrich Schuchardt | 8784b0b | 2020-07-22 17:46:02 +0200 | [diff] [blame] | 286 | fit = map_sysmem(addr, 0); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 287 | |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 288 | if (fit_check_format((void *)fit, IMAGE_SIZE_INVAL)) { |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 289 | printf("Bad FIT format of the update file, aborting " |
| 290 | "auto-update\n"); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 291 | return 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* process updates */ |
| 295 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 296 | |
| 297 | ndepth = 0; |
| 298 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 299 | while (noffset >= 0 && ndepth > 0) { |
| 300 | if (ndepth != 1) |
| 301 | goto next_node; |
| 302 | |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 303 | fit_image_name = (char *)fit_get_name(fit, noffset, NULL); |
| 304 | printf("Processing update '%s' :", fit_image_name); |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 305 | |
Simon Glass | 7428ad1 | 2013-05-07 06:11:57 +0000 | [diff] [blame] | 306 | if (!fit_image_verify(fit, noffset)) { |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 307 | printf("Error: invalid update hash, aborting\n"); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 308 | ret = 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 309 | goto next_node; |
| 310 | } |
| 311 | |
| 312 | printf("\n"); |
| 313 | if (update_fit_getparams(fit, noffset, &update_addr, |
| 314 | &update_fladdr, &update_size)) { |
Heinrich Schuchardt | 814ddd3 | 2020-07-17 19:55:54 +0200 | [diff] [blame] | 315 | printf("Error: can't get update parameters, aborting\n"); |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 316 | ret = 1; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 317 | goto next_node; |
| 318 | } |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 319 | |
| 320 | if (!update_tftp_dfu) { |
| 321 | if (update_flash(update_addr, update_fladdr, |
| 322 | update_size)) { |
| 323 | printf("Error: can't flash update, aborting\n"); |
| 324 | ret = 1; |
| 325 | goto next_node; |
| 326 | } |
| 327 | } else if (fit_image_check_type(fit, noffset, |
| 328 | IH_TYPE_FIRMWARE)) { |
AKASHI Takahiro | bd4dc18 | 2020-10-29 13:47:42 +0900 | [diff] [blame] | 329 | ret = dfu_write_by_name(fit_image_name, |
| 330 | (void *)update_addr, |
AKASHI Takahiro | fb6732f | 2020-10-29 13:47:41 +0900 | [diff] [blame] | 331 | update_size, interface, |
| 332 | devstring); |
Lukasz Majewski | e32125a | 2015-08-24 00:21:47 +0200 | [diff] [blame] | 333 | if (ret) |
| 334 | return ret; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 335 | } |
| 336 | next_node: |
| 337 | noffset = fdt_next_node(fit, noffset, &ndepth); |
| 338 | } |
| 339 | |
Andreas Pretzsch | e3fd7c0 | 2011-07-16 05:50:59 +0000 | [diff] [blame] | 340 | return ret; |
Bartlomiej Sieka | e273e9f | 2008-10-01 15:26:31 +0200 | [diff] [blame] | 341 | } |
AKASHI Takahiro | 92c8080 | 2020-11-19 09:37:19 +0900 | [diff] [blame] | 342 | #endif /* CONFIG_DFU_UPDATE || CONFIG_UPDATE_TFTP */ |
AKASHI Takahiro | 768d58d | 2020-10-29 13:47:43 +0900 | [diff] [blame] | 343 | |
| 344 | #ifdef CONFIG_UPDATE_FIT |
| 345 | /** |
| 346 | * fit_update - update storage with FIT image |
| 347 | * @fit: Pointer to FIT image |
| 348 | * |
| 349 | * Update firmware on storage using FIT image as input. |
| 350 | * The storage area to be update will be identified by the name |
| 351 | * in FIT and matching it to "dfu_alt_info" variable. |
| 352 | * |
| 353 | * Return: 0 - on success, non-zero - otherwise |
| 354 | */ |
| 355 | int fit_update(const void *fit) |
| 356 | { |
| 357 | char *fit_image_name; |
| 358 | ulong update_addr, update_fladdr, update_size; |
| 359 | int images_noffset, ndepth, noffset; |
| 360 | int ret = 0; |
| 361 | |
| 362 | if (!fit) |
| 363 | return -EINVAL; |
| 364 | |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 365 | if (fit_check_format((void *)fit, IMAGE_SIZE_INVAL)) { |
AKASHI Takahiro | 768d58d | 2020-10-29 13:47:43 +0900 | [diff] [blame] | 366 | printf("Bad FIT format of the update file, aborting auto-update\n"); |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | |
| 370 | /* process updates */ |
| 371 | images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 372 | |
| 373 | ndepth = 0; |
| 374 | noffset = fdt_next_node(fit, images_noffset, &ndepth); |
| 375 | while (noffset >= 0 && ndepth > 0) { |
| 376 | if (ndepth != 1) |
| 377 | goto next_node; |
| 378 | |
| 379 | fit_image_name = (char *)fit_get_name(fit, noffset, NULL); |
| 380 | printf("Processing update '%s' :", fit_image_name); |
| 381 | |
| 382 | if (!fit_image_verify(fit, noffset)) { |
| 383 | printf("Error: invalid update hash, aborting\n"); |
| 384 | ret = 1; |
| 385 | goto next_node; |
| 386 | } |
| 387 | |
| 388 | printf("\n"); |
| 389 | if (update_fit_getparams(fit, noffset, &update_addr, |
| 390 | &update_fladdr, &update_size)) { |
| 391 | printf("Error: can't get update parameters, aborting\n"); |
| 392 | ret = 1; |
| 393 | goto next_node; |
| 394 | } |
| 395 | |
| 396 | if (fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE)) { |
| 397 | ret = dfu_write_by_name(fit_image_name, |
| 398 | (void *)update_addr, |
| 399 | update_size, NULL, NULL); |
| 400 | if (ret) |
| 401 | return ret; |
| 402 | } |
| 403 | next_node: |
| 404 | noffset = fdt_next_node(fit, noffset, &ndepth); |
| 405 | } |
| 406 | |
| 407 | return ret; |
| 408 | } |
| 409 | #endif /* CONFIG_UPDATE_FIT */ |