1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
|
/*
* (C) Copyright 2013-2023
* Stefano Babic <stefano.babic@swupdate.org>
*
* SPDX-License-Identifier: GPL-2.0-only
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <ctype.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/select.h>
#include <sys/mman.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <pthread.h>
#include "cpiohdr.h"
#include "bsdqueue.h"
#include "swupdate.h"
#include "util.h"
#include "handler.h"
#ifdef CONFIG_MTD
#include "flash.h"
#endif
#include "parsers.h"
#include "network_ipc.h"
#include "network_interface.h"
#include "mongoose_interface.h"
#include "installer.h"
#include "installer_priv.h"
#include "progress.h"
#include "pctl.h"
#include "state.h"
#include "bootloader.h"
#include "hw-compatibility.h"
#include "swupdate_crypto.h"
#define BUFF_SIZE 4096
#define PERCENT_LB_INDEX 4
enum {
STREAM_WAIT_DESCRIPTION,
STREAM_WAIT_SIGNATURE,
STREAM_DATA,
STREAM_END
};
static pthread_t network_thread_id;
static bool check_reboot_enabled(struct swupdate_cfg *sw) {
if (sw->reboot_enabled != REBOOT_UNSET)
return sw->reboot_enabled == REBOOT_ENABLED;
struct swupdate_type_cfg *typecfg = sw->update_type;
if (typecfg->reboot_enabled != REBOOT_UNSET)
return typecfg->reboot_enabled == REBOOT_ENABLED;
return true;
}
/*
* NOTE: these sync vars are _not_ static, they are _shared_ between the
* installer, the display thread and the network thread
*
* 'mutex' protects the 'inst' installer data, the 'cond' variable as
* well as the 'inst.mnu_main' close request; 'cond' signals the
* reception of an install request
*
*/
bool stream_wkup = false;
pthread_mutex_t stream_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t stream_cond = PTHREAD_COND_INITIALIZER;
static struct installer inst;
static int extract_file_to_tmp(int fd, const char *fname, unsigned long *poffs,
bool encrypted, int max_size)
{
char output_file[MAX_IMAGE_FNAME];
struct filehdr fdh;
int fdout = -1;
uint32_t checksum;
const char* TMPDIR = get_tmpdir();
cipher_t cipher = AES_CBC;
int ret = -1;
#ifdef CONFIG_ASYM_ENCRYPTED_SW_DESCRIPTION
const char *cryptolib = get_cryptolib();
cipher = CMS;
if (encrypted)
set_cryptolib("opensslCMS");
#endif
if (extract_cpio_header(fd, &fdh, poffs)) {
goto err;
}
if (strcmp(fdh.filename, fname)) {
TRACE("description file name not the first of the list: %s instead of %s",
fdh.filename,
fname);
goto err;
}
if (snprintf(output_file, sizeof(output_file), "%s%s", TMPDIR,
fdh.filename) >= (int)sizeof(output_file)) {
ERROR("Path too long: %s%s", TMPDIR, fdh.filename);
goto err;
}
if (max_size && fdh.size >= max_size) {
ERROR("%s size (%ld) exceeds configured max of %d, aborting",
fdh.filename, fdh.size, max_size);
goto err;
}
TRACE("Found file");
TRACE("\tfilename %s", fdh.filename);
TRACE("\tsize %u", (unsigned int)fdh.size);
fdout = openfileoutput(output_file);
if (fdout < 0)
goto err;
struct swupdate_copy copy = {
.fdin = fd,
.out = &fdout,
.nbytes = fdh.size,
.offs = poffs,
.checksum = &checksum,
.encrypted = encrypted,
.cipher = cipher,
};
if (copyfile(©) < 0) {
goto err;
}
if (!swupdate_verify_chksum(checksum, &fdh)) {
goto err;
}
ret = 0;
err:
#ifdef CONFIG_ASYM_ENCRYPTED_SW_DESCRIPTION
if (encrypted)
set_cryptolib(cryptolib);
#endif
if (fdout >= 0)
close(fdout);
return ret;
}
static bool update_transaction_state(struct swupdate_cfg *software, update_state_t newstate)
{
if (!software->parms.dry_run && software->bootloader_transaction_marker) {
if (newstate == STATE_INSTALLED)
bootloader_env_unset(BOOTVAR_TRANSACTION);
else
bootloader_env_set(BOOTVAR_TRANSACTION, get_state_string(newstate));
}
if (!software->parms.dry_run
&& software->bootloader_state_marker
&& save_state(newstate) != SERVER_OK) {
WARN("Cannot persistently store %s update state.", get_state_string(newstate));
return false;
}
return true;
}
static int extract_files(int fd, struct swupdate_cfg *software)
{
int status = STREAM_WAIT_DESCRIPTION;
unsigned long offset;
struct filehdr fdh;
swupdate_file_t skip;
uint32_t checksum;
int fdout;
struct img_type *img, *part;
char output_file[MAX_IMAGE_FNAME];
const char* TMPDIR = get_tmpdir();
bool installed_directly = false;
bool encrypted_sw_desc = false;
#ifdef CONFIG_ENCRYPTED_SW_DESCRIPTION
encrypted_sw_desc = true;
#endif
/* preset the info about the install parts */
offset = 0;
#ifdef CONFIG_UBIVOL
mtd_init();
ubi_init();
#endif
for (;;) {
switch (status) {
/* Waiting for the first Header */
case STREAM_WAIT_DESCRIPTION:
if (extract_file_to_tmp(fd, SW_DESCRIPTION_FILENAME, &offset,
encrypted_sw_desc, software->swdesc_max_size) < 0 )
return -1;
status = STREAM_WAIT_SIGNATURE;
break;
case STREAM_WAIT_SIGNATURE:
#ifdef CONFIG_SIGNED_IMAGES
snprintf(output_file, sizeof(output_file), "%s.sig", SW_DESCRIPTION_FILENAME);
if (extract_file_to_tmp(fd, output_file, &offset, false,
software->swdesc_max_size) < 0 )
return -1;
#endif
snprintf(output_file, sizeof(output_file), "%s%s", TMPDIR, SW_DESCRIPTION_FILENAME);
if (parse(software, output_file)) {
ERROR("Compatible SW not found");
return -1;
}
if (check_hw_compatibility(&software->hw, &software->hardware)) {
ERROR("SW not compatible with hardware");
return -1;
}
if (preupdatecmd(software)) {
return -1;
}
status = STREAM_DATA;
break;
case STREAM_DATA:
if (extract_cpio_header(fd, &fdh, &offset)) {
ERROR("CPIO HEADER");
return -1;
}
if (strcmp("TRAILER!!!", fdh.filename) == 0) {
/*
* Keep reading the cpio padding, if any, up
* to 512 bytes from the socket until the
* client stops writing
*/
extract_padding(fd);
status = STREAM_END;
break;
}
struct imglist *list[] = {&software->images,
&software->scripts};
for (unsigned int i = 0; i < ARRAY_SIZE(list); i++) {
skip = check_if_required(list[i], &fdh,
get_tmpdir(),
&img);
if (skip != SKIP_FILE)
break;
}
TRACE("Found file");
TRACE("\tfilename %s", fdh.filename);
TRACE("\tsize %u %s", (unsigned int)fdh.size,
(skip == SKIP_FILE ? "Not required: skipping" : "required"));
fdout = -1;
offset = 0;
struct swupdate_copy copy = {
.fdin = fd,
.out = &fdout,
.nbytes = fdh.size,
.offs = &offset,
.checksum = &checksum,
};
/*
* If images are not streamed directly into the target
* copy them into TMPDIR to check if it is all ok
*/
switch (skip) {
case COPY_FILE:
fdout = openfileoutput(img->extract_file);
if (fdout < 0)
return -1;
if (!img_check_free_space(img, fdout)) {
close(fdout);
return -1;
}
copy.hash = img->sha256;
if (copyfile(©) < 0) {
close(fdout);
return -1;
}
if (!swupdate_verify_chksum(checksum, &fdh)) {
close(fdout);
return -1;
}
close(fdout);
break;
case SKIP_FILE:
copy.skip_file = 1;
if (copyfile(©) < 0) {
return -1;
}
if (!swupdate_verify_chksum(checksum, &fdh)) {
return -1;
}
break;
case INSTALL_FROM_STREAM:
TRACE("Installing STREAM %s, %lld bytes", img->fname, img->size);
/*
* If this is the first image to be directly installed, set transaction flag
* to on to be checked if a power-off happens. Be sure to set the flag
* just once
*/
if (!installed_directly) {
update_transaction_state(software, STATE_IN_PROGRESS);
installed_directly = true;
}
/*
* If we are streaming data to store in a UBI volume, make
* sure that the UBI partitions are adjusted beforehand
*/
LIST_FOREACH(part, &software->images, next) {
if (!part->install_directly && part->is_partitioner) {
TRACE("Need to adjust partition %s before streaming %s",
part->volname, img->fname);
if (install_single_image(part, software->parms.dry_run)) {
ERROR("Error adjusting partition %s", part->volname);
return -1;
}
/* Avoid trying to adjust again later */
part->install_directly = true;
}
}
img->fdin = fd;
if (install_single_image(img, software->parms.dry_run)) {
ERROR("Error streaming %s", img->fname);
return -1;
}
update_installed_image_version(&software->installed_sw_list, img);
TRACE("END INSTALLING STREAMING");
break;
}
break;
case STREAM_END:
/*
* Check if all required files were provided
* Update of a single file is not possible.
*/
LIST_FOREACH(img, &software->images, next) {
if ( img->skip)
continue;
if (! img->fname[0])
continue;
if (! img->provided) {
ERROR("Required image file %s missing...aborting !",
img->fname);
return -1;
}
}
return 0;
default:
return -1;
}
}
}
static int cpfiles(int fdin, int fdout, size_t max)
{
char *buf;
const int bufsize = 16 * 1024;
int ret, len;
size_t maxread;
bool cpyall = (max == 0);
buf = (char *)malloc(bufsize);
if (!buf)
return -ENOMEM;
for (;;) {
/*
* No limit set - copy the whole file
* Set max to be always not zero
*/
if (cpyall)
max = 2 * bufsize;
maxread = min(bufsize, max);
len = read(fdin, buf, maxread);
if (len < 0) {
free(buf);
return -EIO;
}
if (len == 0)
break;
ret = copy_write(&fdout, buf, len);
if (ret < 0) {
free(buf);
return -EIO;
}
max -= len;
if (max == 0)
break;
}
free(buf);
return 0;
}
#define SW_TMP_OUTPUT "swtmp-outputXXXXXXXX"
static int save_stream(int fdin, struct swupdate_cfg *software)
{
unsigned char *buf;
int fdout = -1, ret, len;
int tmpfd = -1;
char tmpfilename[MAX_IMAGE_FNAME];
struct filehdr fdh;
unsigned int tmpsize;
unsigned long offset;
char output_file[MAX_IMAGE_FNAME];
const char* TMPDIR = get_tmpdir();
bool encrypted_sw_desc = false;
int files = 1;
#ifdef CONFIG_SIGNED_IMAGES
files = 2; // sw-description and sw-description.sig
#endif
#ifdef CONFIG_ENCRYPTED_SW_DESCRIPTION
encrypted_sw_desc = true;
#endif
if (fdin < 0)
return -EINVAL;
snprintf(tmpfilename, sizeof(tmpfilename), "%s/%s", TMPDIR, SW_TMP_OUTPUT);
buf = (unsigned char *)malloc(sizeof(struct new_ascii_header));
if (!buf) {
ERROR("OOM when saving stream");
return -ENOMEM;
}
/*
* Cache the beginning of the SWU to parse
* sw-description and check if the output must be
* redirected. This allows to define the output file on demand
* setting it into sw-description.
*/
tmpfd = mkstemp(tmpfilename);
if (tmpfd < 0) {
ERROR("Cannot get space for temporary data, error %d", errno);
ret = -EFAULT;
goto no_copy_output;
}
/*
* Copy first two cpio blocks (sw-description and sw-description.sig) into tmpfd
*/
while (files-- > 0) {
len = fill_buffer(fdin, buf, sizeof(struct new_ascii_header));
if (len < 0) {
ERROR("Reading from file failed, error %d", errno);
ret = -EFAULT;
goto no_copy_output;
}
if (get_cpiohdr(buf, &fdh) < 0) {
ERROR("CPIO Header corrupted, cannot be parsed");
ret = -EINVAL;
goto no_copy_output;
}
ret = copy_write(&tmpfd, buf, len);
if (ret < 0) {
ret = -EIO;
goto no_copy_output;
}
/*
* calc remaining bytes of the cpio block
*/
tmpsize = sizeof(struct new_ascii_header) + fdh.namesize;
tmpsize += NPAD_BYTES(tmpsize) + fdh.size;
tmpsize += NPAD_BYTES(tmpsize);
tmpsize -= sizeof(struct new_ascii_header);
/*
* This doubles with check in extract_file_to_tmp, but it does not make
* sense to check after having copied everything once in tmpfd...
*/
if (software->swdesc_max_size && fdh.size >= software->swdesc_max_size) {
ERROR("sw-description size (%ld) exceeds configured max of %d, aborting",
fdh.size, software->swdesc_max_size);
ret = -EINVAL;
goto no_copy_output;
}
/*
* copy the remaining bytes to have a complete cpio block
*/
ret = cpfiles(fdin, tmpfd, tmpsize);
if (ret < 0) {
ret = -EIO;
goto no_copy_output;
}
}
lseek(tmpfd, 0, SEEK_SET);
offset = 0;
if (extract_file_to_tmp(tmpfd, SW_DESCRIPTION_FILENAME, &offset,
encrypted_sw_desc, software->swdesc_max_size) < 0) {
ERROR("%s cannot be extracted", SW_DESCRIPTION_FILENAME);
ret = -EINVAL;
goto no_copy_output;
}
#ifdef CONFIG_SIGNED_IMAGES
snprintf(output_file, sizeof(output_file), "%s.sig", SW_DESCRIPTION_FILENAME);
if (extract_file_to_tmp(tmpfd, output_file, &offset, false,
software->swdesc_max_size) < 0 ) {
ERROR("Signature cannot be extracted:%s", output_file);
ret = -EINVAL;
goto no_copy_output;
}
#endif
snprintf(output_file, sizeof(output_file), "%s%s", TMPDIR, SW_DESCRIPTION_FILENAME);
if (parse(software, output_file)) {
ERROR("Compatible SW not found");
ret = -1;
goto no_copy_output;
}
/*
* if all is ok, copy the first part of SWU (stored in tmp file)
* into the output
*/
lseek(tmpfd, 0, SEEK_SET);
fdout = openfileoutput(software->output);
/*
* Try to create directory if file cannot be opened
*/
if (fdout < 0) {
if (mkpath(software->output, 0755))
return -1;
fdout = openfileoutput(software->output);
if (fdout < 0)
return -1;
}
ret = cpfiles(tmpfd, fdout, 0);
if (ret < 0)
goto no_copy_output;
ret = cpfiles(fdin, fdout, 0);
if (ret < 0)
goto no_copy_output;
ret = 0;
no_copy_output:
free(buf);
if (fdout >= 0)
close(fdout);
if (tmpfd >= 0) {
close(tmpfd);
unlink(tmpfilename);
}
cleanup_files(software);
return ret;
}
void *network_initializer(void *data)
{
int ret;
struct swupdate_cfg *software = data;
struct swupdate_request *req;
struct swupdate_parms parms;
bool do_reboot = true;
/* No installation in progress */
memset(&inst, 0, sizeof(inst));
inst.fd = -1;
inst.status = IDLE;
inst.software = software;
/* fork off the local dialogs and network service */
network_thread_id = start_thread(network_thread, &inst);
TRACE("Main loop daemon");
thread_ready();
/* handle installation requests (from either source) */
while (1) {
ret = 0;
/* wait for someone to issue an install request */
pthread_mutex_lock(&stream_mutex);
while (stream_wkup != true) {
pthread_cond_wait(&stream_cond, &stream_mutex);
}
stream_wkup = false;
inst.status = RUN;
pthread_mutex_unlock(&stream_mutex);
notify(START, RECOVERY_NO_ERROR, INFOLEVEL, "Software Update started !");
TRACE("Software update started");
/* Create directories for scripts/datadst */
swupdate_create_directory(SCRIPTS_DIR_SUFFIX);
swupdate_create_directory(DATADST_DIR_SUFFIX);
req = &inst.req;
/*
* Save default values, they can be changed by a
* install request
*/
parms = software->parms;
/*
* Check if the dry run flag is overwritten
*/
switch (req->dry_run){
case RUN_DRYRUN:
software->parms.dry_run = true;
break;
case RUN_INSTALL:
software->parms.dry_run = false;
break;
case RUN_DEFAULT:
break;
}
/*
* Find the selection to be installed
*/
if ((strnlen(req->software_set, sizeof(req->software_set)) > 0) &&
(strnlen(req->running_mode, sizeof(req->running_mode)) > 0)) {
strlcpy(software->parms.software_set, req->software_set, sizeof(software->parms.software_set) - 1);
strlcpy(software->parms.running_mode, req->running_mode, sizeof(software->parms.running_mode) - 1);
}
/*
* Check if the stream should be saved
*/
if (!req->disable_store_swu && strlen(software->output)) {
ret = save_stream(inst.fd, software);
if (ret < 0) {
notify(FAILURE, RECOVERY_ERROR, ERRORLEVEL,
"Error saving stream, not installing ...");
}
/*
* now replace the file descriptor with
* the saved file
*/
if (!(inst.fd < 0))
close(inst.fd);
inst.fd = open(software->output, O_RDONLY, S_IRUSR);
if (inst.fd < 0) {
ERROR("%s cannot be opened", software->output);
ret = -ENODEV;
}
}
if (!ret) {
#ifdef CONFIG_MTD
mtd_cleanup();
scan_mtd_devices();
#endif
/*
* extract the meta data and relevant parts
* (flash images) from the install image
*/
ret = extract_files(inst.fd, software);
}
if (!(inst.fd < 0))
close(inst.fd);
if (!software->parms.dry_run && is_bootloader(BOOTLOADER_EBG)) {
if (!software->bootloader_transaction_marker) {
/*
* EFI Boot Guard's "in_progress" environment variable
* has special semantics hard-coded, hence the
* bootloader transaction marker cannot be disabled.
*/
TRACE("Note: Setting EFI Boot Guard's 'in_progress' "
"environment variable cannot be disabled.");
}
if (!software->bootloader_state_marker) {
/*
* With a disabled update state marker, there's no
* transaction auto-commit via
* save_state(STATE_INSTALLED)
* which effectively calls
* bootloader_env_set(STATE_KEY, STATE_INSTALLED).
* Hence, manually calling save_state(STATE_INSTALLED)
* or equivalent is required to commit the transaction.
* This can be useful to, e.g., terminate the transaction
* from an according progress interface client or an
* SWUpdate suricatta module after it has received an
* update activation request from the remote server.
*/
TRACE("Note: EFI Boot Guard environment transaction "
"will not be auto-committed.");
}
if (!software->bootloader_transaction_marker &&
!software->bootloader_state_marker) {
WARN("EFI Boot Guard environment modifications will "
"not be persisted.");
}
}
/* do carry out the installation (flash programming) */
if (ret == 0) {
TRACE("Valid image found: copying to FLASH");
/*
* If an image is loaded, the install
* must be successful. Set we have
* initiated an update
*/
update_transaction_state(software, STATE_IN_PROGRESS);
notify(RUN, RECOVERY_NO_ERROR, INFOLEVEL, "Installation in progress");
/*
* if this update is signalled to be without reboot (On the Fly),
* send a notification via progress for processes responsible for booting
*/
do_reboot = check_reboot_enabled(software);
if (!do_reboot) {
swupdate_progress_info(RUN, CAUSE_REBOOT_MODE , "{ \"reboot-mode\" : \"no-reboot\"}");
}
if (software->parms.dry_run) {
swupdate_progress_info(RUN, CAUSE_DRY_RUN_MODE , "{ \"dry-run\" : true }");
}
ret = install_images(software);
if (ret != 0) {
update_transaction_state(software, STATE_FAILED);
notify(FAILURE, RECOVERY_ERROR, ERRORLEVEL, "Installation failed !");
inst.last_install = FAILURE;
/*
* Try to run all POSTFAILURE scripts,
* their result does not change the state that remains FAILURE
* Goal is to restore to the state before update was started
* if this is needed. This is a best case attempt, ERRORs
* are just logged.
*/
if (!software->parms.dry_run) {
if (run_prepost_scripts(&software->scripts, POSTFAILURE)) {
WARN("execute POST FAILURE scripts return error, ignoring..");
}
}
} else {
/*
* Clear the recovery variable to indicate to bootloader
* that it is not required to start recovery again
*/
if (!update_transaction_state(software, STATE_INSTALLED)) {
ERROR("Cannot persistently store INSTALLED update state.");
notify(FAILURE, RECOVERY_ERROR, ERRORLEVEL, "Installation failed !");
inst.last_install = FAILURE;
} else {
notify(SUCCESS, RECOVERY_NO_ERROR, INFOLEVEL, "SWUPDATE successful !");
inst.last_install = SUCCESS;
}
}
} else {
inst.last_install = FAILURE;
notify(FAILURE, RECOVERY_ERROR, ERRORLEVEL, "Image invalid or corrupted. Not installing ...");
}
swupdate_progress_end(inst.last_install);
/*
* Reload default values for update
*/
software->parms = parms;
/* release temp files we may have created */
cleanup_files(software);
#ifndef CONFIG_NOCLEANUP
swupdate_remove_directory(SCRIPTS_DIR_SUFFIX);
swupdate_remove_directory(DATADST_DIR_SUFFIX);
#endif
/*
* Last step, if no restart is required,
* SWUpdate can send automatically the feedback.
* They are running on separate processes and should first
* know that the update is completed.
* It seems safe enough to wait uncoditionally for some
* time, enough to let all SWUpdate's processes to be scheduled,
* before sending the IPC message
*/
if (req->source == SOURCE_SURICATTA &&
/*simple check without JSON */ strstr(req->info, "hawkbit") &&
!do_reboot) {
ipc_message msg;
size_t size = sizeof(msg.data.procmsg.buf);
char *buf = msg.data.procmsg.buf;
memset(&msg, 0, sizeof(msg));
msg.magic = IPC_MAGIC;
msg.data.procmsg.source = SOURCE_SURICATTA;
msg.data.procmsg.cmd = CMD_ACTIVATION;
msg.type = SWUPDATE_SUBPROCESS;
snprintf(buf, size, "{ \"status\" : \"%s\", \"finished\" : \"%s\" ,\"execution\" : \"%s\" ,\"details\" : [ ]}",
"1",
inst.last_install == SUCCESS ? "success" : "failure",
"closed"
);
sleep(2);
TRACE("SEND CONCLUSION TO HAWKBIT");
ipc_send_cmd(&msg);
}
pthread_mutex_lock(&stream_mutex);
inst.status = IDLE;
inst.req.source = SOURCE_UNKNOWN;
pthread_mutex_unlock(&stream_mutex);
TRACE("Main thread sleep again !");
notify(IDLE, RECOVERY_NO_ERROR, INFOLEVEL, "Waiting for requests...");
}
pthread_exit((void *)0);
}
/*
* Accessors to get information about an update, they are the interface
* to the "inst" structure.
*/
void get_install_swset(char *buf, size_t len)
{
if (!buf)
return;
strncpy(buf, inst.software->parms.software_set, len - 1);
}
void get_install_running_mode(char *buf, size_t len)
{
if (!buf)
return;
strncpy(buf, inst.software->parms.running_mode, len - 1);
}
/*
* Retrieve additional info sent by the source
* The data is not locked because it is retrieve
* at different times
*/
int get_install_info(char *buf, size_t len)
{
len = min(len - 1, strlen(inst.req.info));
strncpy(buf, inst.req.info, len);
return len;
}
bool is_dryrun_install(void)
{
return inst.software->parms.dry_run;
}
sourcetype get_install_source(void)
{
return inst.req.source;
}
int set_version_range(const char *update_type, const char *minversion,
const char *maxversion, const char *current)
{
struct swupdate_type_cfg *typecfg;
typecfg = swupdate_find_update_type(&inst.software->swupdate_types,
update_type && strnlen(update_type, SWUPDATE_GENERAL_STRING_SIZE) ?
update_type : "default");
if (!typecfg) {
ERROR("Configuration setup for Update Type not found");
return -EINVAL;
}
if (minversion && strnlen(minversion, SWUPDATE_GENERAL_STRING_SIZE)) {
strlcpy(typecfg->minimum_version, minversion,
sizeof(typecfg->minimum_version));
typecfg->no_downgrading = true;
}
if (maxversion && strnlen(maxversion, SWUPDATE_GENERAL_STRING_SIZE)) {
strlcpy(typecfg->maximum_version, maxversion,
sizeof(typecfg->maximum_version));
typecfg->check_max_version = true;
}
if (current && strnlen(current, SWUPDATE_GENERAL_STRING_SIZE)) {
strlcpy(typecfg->current_version, current,
sizeof(typecfg->current_version));
typecfg->no_reinstalling = true;
}
return 0;
}
|