blob: 8e1014fbf685a51bfd2611b7f5daf9489081a410 [file] [log] [blame]
Julian Hall351a0722023-01-12 11:58:42 +00001/*
2 * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <cstdio>
8#include <cstdlib>
9#include <cstring>
10#include <common/uuid/uuid.h>
11#include "cmd_update_image.h"
12
13
14int cmd_update_image(
15 fwu_app &app,
16 const std::string &img_type_uuid,
17 const std::string &img_filename)
18{
19 FILE *fp = fopen(img_filename.c_str(), "rb");
20
21 if (!fp) {
22
23 printf("Error: failed to open image file: %s\n", img_filename.c_str());
24 return -1;
25 }
26
27 /* Get file size */
28 fseek(fp, 0, SEEK_END);
29 size_t img_size = ftell(fp);
30 rewind(fp);
31
32 /* Allocate buffer for image data */
33 uint8_t *img_buf = (uint8_t *)malloc(img_size);
34
35 if (!img_buf) {
36
37 fclose(fp);
38 printf("Error: failed to allocate image buffer\n");
39 return -1;
40 }
41
42 /* Read file contents into buffer */
43 if (fread(img_buf, 1, img_size, fp)) {
44
45 fclose(fp);
46 printf("Error: failed to read image file\n");
47 return -1;
48 }
49
50 fclose(fp);
51
52 /* Apply update */
53 struct uuid_octets uuid;
54
55 uuid_guid_octets_from_canonical(&uuid, img_type_uuid.c_str());
56
57 int status = app.update_image(uuid, img_buf, img_size);
58
59 if (status)
60 printf("Error: update image failed\n");
61
62 free(img_buf);
63
64 return status;
65}