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