Merge branch '2022-08-04-Kconfig-migrations'
[platform/kernel/u-boot.git] / common / update.c
index 457b29f..80f16af 100644 (file)
@@ -7,6 +7,8 @@
  */
 
 #include <common.h>
+#include <cpu_func.h>
+#include <image.h>
 
 #if !(defined(CONFIG_FIT) && defined(CONFIG_OF_LIBFDT))
 #error "CONFIG_FIT and CONFIG_OF_LIBFDT are required for auto-update feature"
 
 #include <command.h>
 #include <env.h>
-#include <flash.h>
 #include <net.h>
 #include <net/tftp.h>
 #include <malloc.h>
+#include <mapmem.h>
 #include <dfu.h>
 #include <errno.h>
-#include <mtd/cfi_flash.h>
 
+#if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP)
 /* env variable holding the location of the update file */
 #define UPDATE_FILE_ENV                "updatefile"
 
@@ -44,9 +46,9 @@
 
 extern ulong tftp_timeout_ms;
 extern int tftp_timeout_count_max;
-extern ulong load_addr;
 #ifdef CONFIG_MTD_NOR_FLASH
-extern flash_info_t flash_info[];
+#include <flash.h>
+#include <mtd/cfi_flash.h>
 static uchar *saved_prot_info;
 #endif
 static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
@@ -71,7 +73,7 @@ static int update_load(char *filename, ulong msec_max, int cnt_max, ulong addr)
        env_set("netretry", "no");
 
        /* download the update file */
-       load_addr = addr;
+       image_load_addr = addr;
        copy_filename(net_boot_file_name, filename, sizeof(net_boot_file_name));
        size = net_loop(TFTPGET);
 
@@ -109,12 +111,12 @@ static int update_flash_protect(int prot, ulong addr_first, ulong addr_last)
 
        if (prot == 0) {
                saved_prot_info =
-                       calloc(CONFIG_SYS_MAX_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
+                       calloc(CFI_FLASH_BANKS * CONFIG_SYS_MAX_FLASH_SECT, 1);
                if (!saved_prot_info)
                        return 1;
        }
 
-       for (bank = 0; bank < CONFIG_SYS_MAX_FLASH_BANKS; ++bank) {
+       for (bank = 0; bank < CFI_FLASH_BANKS; ++bank) {
                cnt = 0;
                info = &flash_info[bank];
 
@@ -212,6 +214,7 @@ static int update_flash(ulong addr_source, ulong addr_first, ulong size)
 #endif
        return 0;
 }
+#endif /* CONFIG_DFU_TFTP || CONFIG_UPDATE_TFTP */
 
 static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
                                                ulong *fladdr, ulong *size)
@@ -229,6 +232,7 @@ static int update_fit_getparams(const void *fit, int noffset, ulong *addr,
        return 0;
 }
 
+#if defined(CONFIG_DFU_TFTP) || defined(CONFIG_UPDATE_TFTP)
 int update_tftp(ulong addr, char *interface, char *devstring)
 {
        char *filename, *env_addr, *fit_image_name;
@@ -267,7 +271,7 @@ int update_tftp(ulong addr, char *interface, char *devstring)
        /* get load address of downloaded update file */
        env_addr = env_get("loadaddr");
        if (env_addr)
-               addr = simple_strtoul(env_addr, NULL, 16);
+               addr = hextoul(env_addr, NULL);
        else
                addr = CONFIG_UPDATE_LOAD_ADDR;
 
@@ -279,9 +283,9 @@ int update_tftp(ulong addr, char *interface, char *devstring)
        }
 
 got_update_file:
-       fit = (void *)addr;
+       fit = map_sysmem(addr, 0);
 
-       if (!fit_check_format((void *)fit)) {
+       if (fit_check_format((void *)fit, IMAGE_SIZE_INVAL)) {
                printf("Bad FIT format of the update file, aborting "
                                                        "auto-update\n");
                return 1;
@@ -308,8 +312,7 @@ got_update_file:
                printf("\n");
                if (update_fit_getparams(fit, noffset, &update_addr,
                                        &update_fladdr, &update_size)) {
-                       printf("Error: can't get update parameteres, "
-                                                               "aborting\n");
+                       printf("Error: can't get update parameters, aborting\n");
                        ret = 1;
                        goto next_node;
                }
@@ -323,8 +326,77 @@ got_update_file:
                        }
                } else if (fit_image_check_type(fit, noffset,
                                                IH_TYPE_FIRMWARE)) {
-                       ret = dfu_tftp_write(fit_image_name, update_addr,
-                                            update_size, interface, devstring);
+                       ret = dfu_write_by_name(fit_image_name,
+                                               (void *)update_addr,
+                                               update_size, interface,
+                                               devstring);
+                       if (ret)
+                               return ret;
+               }
+next_node:
+               noffset = fdt_next_node(fit, noffset, &ndepth);
+       }
+
+       return ret;
+}
+#endif /* CONFIG_DFU_UPDATE || CONFIG_UPDATE_TFTP */
+
+#ifdef CONFIG_UPDATE_FIT
+/**
+ * fit_update - update storage with FIT image
+ * @fit:       Pointer to FIT image
+ *
+ * Update firmware on storage using FIT image as input.
+ * The storage area to be update will be identified by the name
+ * in FIT and matching it to "dfu_alt_info" variable.
+ *
+ * Return:      0 - on success, non-zero - otherwise
+ */
+int fit_update(const void *fit)
+{
+       char *fit_image_name;
+       ulong update_addr, update_fladdr, update_size;
+       int images_noffset, ndepth, noffset;
+       int ret = 0;
+
+       if (!fit)
+               return -EINVAL;
+
+       if (fit_check_format((void *)fit, IMAGE_SIZE_INVAL)) {
+               printf("Bad FIT format of the update file, aborting auto-update\n");
+               return -EINVAL;
+       }
+
+       /* process updates */
+       images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
+
+       ndepth = 0;
+       noffset = fdt_next_node(fit, images_noffset, &ndepth);
+       while (noffset >= 0 && ndepth > 0) {
+               if (ndepth != 1)
+                       goto next_node;
+
+               fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
+               printf("Processing update '%s' :", fit_image_name);
+
+               if (!fit_image_verify(fit, noffset)) {
+                       printf("Error: invalid update hash, aborting\n");
+                       ret = 1;
+                       goto next_node;
+               }
+
+               printf("\n");
+               if (update_fit_getparams(fit, noffset, &update_addr,
+                                        &update_fladdr, &update_size)) {
+                       printf("Error: can't get update parameters, aborting\n");
+                       ret = 1;
+                       goto next_node;
+               }
+
+               if (fit_image_check_type(fit, noffset, IH_TYPE_FIRMWARE)) {
+                       ret = dfu_write_by_name(fit_image_name,
+                                               (void *)update_addr,
+                                               update_size, NULL, NULL);
                        if (ret)
                                return ret;
                }
@@ -334,3 +406,4 @@ next_node:
 
        return ret;
 }
+#endif /* CONFIG_UPDATE_FIT */