diff mbox series

installer: consolidate bootloader environment update

Message ID 20181017132823.32492-1-christian.storm@siemens.com
State Accepted
Headers show
Series installer: consolidate bootloader environment update | expand

Commit Message

Storm, Christian Oct. 17, 2018, 1:28 p.m. UTC
Instead of the function prepare_boot_script() just storing
and the function update_bootloader_env() later applying the
stored bootloader environment modifications, make the new
update_bootloader_env() function store and apply the
modifications, if any.

Using a temporary file (BOOT_SCRIPT_SUFFIX) and feeding it
to bootloader_apply_list() is maintained to allow batch
processing by the bootloader binding.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/installer.c | 44 ++++++++++++++++----------------------------
 1 file changed, 16 insertions(+), 28 deletions(-)

Comments

Stefano Babic Oct. 22, 2018, 4:27 p.m. UTC | #1
On 17/10/18 15:28, Christian Storm wrote:
> Instead of the function prepare_boot_script() just storing
> and the function update_bootloader_env() later applying the
> stored bootloader environment modifications, make the new
> update_bootloader_env() function store and apply the
> modifications, if any.
> 
> Using a temporary file (BOOT_SCRIPT_SUFFIX) and feeding it
> to bootloader_apply_list() is maintained to allow batch
> processing by the bootloader binding.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/installer.c | 44 ++++++++++++++++----------------------------
>  1 file changed, 16 insertions(+), 28 deletions(-)
> 
> diff --git a/corelib/installer.c b/corelib/installer.c
> index 324d1d9..b35d345 100644
> --- a/corelib/installer.c
> +++ b/corelib/installer.c
> @@ -152,7 +152,7 @@ static int extract_scripts(int fd, struct imglist *head, int fromfile)
>  	return 0;
>  }
>  
> -static int prepare_boot_script(struct swupdate_cfg *cfg, const char *script)
> +static int update_bootloader_env(struct swupdate_cfg *cfg, const char *script)
>  {
>  	int fd;
>  	int ret = 0;
> @@ -171,12 +171,16 @@ static int prepare_boot_script(struct swupdate_cfg *cfg, const char *script)
>  			continue;
>  		snprintf(buf, sizeof(buf), "%s %s\n", key, value);
>  		if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
> -			  TRACE("Error saving temporary file");
> -			  ret = -1;
> -			  break;
> +			  TRACE("Error saving temporary bootloader environment file");
> +			  close(fd);
> +			  return -1;
>  		}
>  	}
>  	close(fd);
> +
> +	if ((ret = bootloader_apply_list(script)) < 0) {
> +		ERROR("Bootloader-specific error %d updating its environment", ret);
> +	}
>  	return ret;
>  }
>  
> @@ -201,20 +205,6 @@ static int run_prepost_scripts(struct imglist *list, script_fn type)
>  	return 0;
>  }
>  
> -static int update_bootloader_env(void)
> -{
> -	int ret = 0;
> -
> -	TRACE("Updating bootloader environment");
> -	char* bootscript = alloca(strlen(get_tmpdir())+strlen(BOOT_SCRIPT_SUFFIX)+1);
> -	sprintf(bootscript, "%s%s", get_tmpdir(), BOOT_SCRIPT_SUFFIX);
> -	ret = bootloader_apply_list(bootscript);
> -	if (ret < 0)
> -		ERROR("Error updating bootloader environment");
> -
> -	return ret;
> -}
> -
>  int install_single_image(struct img_type *img, int dry_run)
>  {
>  	struct installer_handler *hnd;
> @@ -282,14 +272,6 @@ int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile)
>  		}
>  	}
>  
> -	/* Update u-boot environment */
> -	char* bootscript = alloca(strlen(TMPDIR)+strlen(BOOT_SCRIPT_SUFFIX)+1);
> -	sprintf(bootscript, "%s%s", TMPDIR, BOOT_SCRIPT_SUFFIX);
> -	ret = prepare_boot_script(sw, bootscript);
> -	if (ret) {
> -		return ret;
> -	}
> -
>  	LIST_FOREACH(img, &sw->images, next) {
>  
>  		/*
> @@ -373,8 +355,14 @@ int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile)
>  		return ret;
>  	}
>  
> -	if (!LIST_EMPTY(&sw->bootloader))
> -		ret = update_bootloader_env();
> +	if (!LIST_EMPTY(&sw->bootloader)) {
> +		char* bootscript = alloca(strlen(TMPDIR)+strlen(BOOT_SCRIPT_SUFFIX)+1);
> +		sprintf(bootscript, "%s%s", TMPDIR, BOOT_SCRIPT_SUFFIX);
> +		ret = update_bootloader_env(sw, bootscript);
> +		if (ret) {
> +			return ret;
> +		}
> +	}
>  
>  	ret |= run_prepost_scripts(&sw->bootscripts, POSTINSTALL);
>  
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/installer.c b/corelib/installer.c
index 324d1d9..b35d345 100644
--- a/corelib/installer.c
+++ b/corelib/installer.c
@@ -152,7 +152,7 @@  static int extract_scripts(int fd, struct imglist *head, int fromfile)
 	return 0;
 }
 
-static int prepare_boot_script(struct swupdate_cfg *cfg, const char *script)
+static int update_bootloader_env(struct swupdate_cfg *cfg, const char *script)
 {
 	int fd;
 	int ret = 0;
@@ -171,12 +171,16 @@  static int prepare_boot_script(struct swupdate_cfg *cfg, const char *script)
 			continue;
 		snprintf(buf, sizeof(buf), "%s %s\n", key, value);
 		if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
-			  TRACE("Error saving temporary file");
-			  ret = -1;
-			  break;
+			  TRACE("Error saving temporary bootloader environment file");
+			  close(fd);
+			  return -1;
 		}
 	}
 	close(fd);
+
+	if ((ret = bootloader_apply_list(script)) < 0) {
+		ERROR("Bootloader-specific error %d updating its environment", ret);
+	}
 	return ret;
 }
 
@@ -201,20 +205,6 @@  static int run_prepost_scripts(struct imglist *list, script_fn type)
 	return 0;
 }
 
-static int update_bootloader_env(void)
-{
-	int ret = 0;
-
-	TRACE("Updating bootloader environment");
-	char* bootscript = alloca(strlen(get_tmpdir())+strlen(BOOT_SCRIPT_SUFFIX)+1);
-	sprintf(bootscript, "%s%s", get_tmpdir(), BOOT_SCRIPT_SUFFIX);
-	ret = bootloader_apply_list(bootscript);
-	if (ret < 0)
-		ERROR("Error updating bootloader environment");
-
-	return ret;
-}
-
 int install_single_image(struct img_type *img, int dry_run)
 {
 	struct installer_handler *hnd;
@@ -282,14 +272,6 @@  int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile)
 		}
 	}
 
-	/* Update u-boot environment */
-	char* bootscript = alloca(strlen(TMPDIR)+strlen(BOOT_SCRIPT_SUFFIX)+1);
-	sprintf(bootscript, "%s%s", TMPDIR, BOOT_SCRIPT_SUFFIX);
-	ret = prepare_boot_script(sw, bootscript);
-	if (ret) {
-		return ret;
-	}
-
 	LIST_FOREACH(img, &sw->images, next) {
 
 		/*
@@ -373,8 +355,14 @@  int install_images(struct swupdate_cfg *sw, int fdsw, int fromfile)
 		return ret;
 	}
 
-	if (!LIST_EMPTY(&sw->bootloader))
-		ret = update_bootloader_env();
+	if (!LIST_EMPTY(&sw->bootloader)) {
+		char* bootscript = alloca(strlen(TMPDIR)+strlen(BOOT_SCRIPT_SUFFIX)+1);
+		sprintf(bootscript, "%s%s", TMPDIR, BOOT_SCRIPT_SUFFIX);
+		ret = update_bootloader_env(sw, bootscript);
+		if (ret) {
+			return ret;
+		}
+	}
 
 	ret |= run_prepost_scripts(&sw->bootscripts, POSTINSTALL);