blob: 88bac76997126a0124b727f634f98ae547b7d402 [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>
Julian Hall351a0722023-01-12 11:58:42 +000010#include <sstream>
Gyorgy Szing3c446242023-03-31 01:53:15 +020011#include <string>
Julian Hall351a0722023-01-12 11:58:42 +000012#include <sys/stat.h>
Gyorgy Szing3c446242023-03-31 01:53:15 +020013
Julian Hall351a0722023-01-12 11:58:42 +000014#include "cmd_print_image_dir.h"
15#include "cmd_print_metadata_v1.h"
16#include "cmd_print_metadata_v2.h"
Gyorgy Szing3c446242023-03-31 01:53:15 +020017#include "cmd_update_image.h"
18#include "common/uuid/uuid.h"
19#include "service/fwu/app/fwu_app.h"
Julian Hall351a0722023-01-12 11:58:42 +000020
Gyorgy Szing3c446242023-03-31 01:53:15 +020021static bool option_selected(const char *option_switch, int argc, char *argv[]);
Julian Hall351a0722023-01-12 11:58:42 +000022
Gyorgy Szing3c446242023-03-31 01:53:15 +020023static std::string parse_string_option(const char *option_switch, int argc, char *argv[],
24 const char *default_val);
Julian Hall351a0722023-01-12 11:58:42 +000025
Gyorgy Szing3c446242023-03-31 01:53:15 +020026static int parse_numeric_option(const char *option_switch, int argc, char *argv[], int default_val);
Julian Hall351a0722023-01-12 11:58:42 +000027
Gyorgy Szing3c446242023-03-31 01:53:15 +020028static bool file_exists(const std::string &filename);
Julian Hall351a0722023-01-12 11:58:42 +000029
30static void print_usage(void);
31static void print_help(void);
32
Julian Hall351a0722023-01-12 11:58:42 +000033int main(int argc, char *argv[])
34{
35 fwu_app app;
36 std::string disk_img_filename;
37 std::string update_img_filename;
38 std::string img_type_uuid;
39
40 /* Check for help */
Gyorgy Szing3c446242023-03-31 01:53:15 +020041 if (option_selected("-h", argc, argv) || option_selected("-help", argc, argv) ||
42 option_selected("--help", argc, argv)) {
Julian Hall351a0722023-01-12 11:58:42 +000043 print_help();
44 return 0;
45 }
46
47 /* Handle mandatory disk image filename. Must be first argument */
48 if (argc > 1)
49 disk_img_filename = std::string(argv[1]);
50 else {
Julian Hall351a0722023-01-12 11:58:42 +000051 printf("Error: missing disk-filename argument\n");
52 print_usage();
53 return -1;
54 }
55
56 /* Check if disk image file exists */
57 if (!file_exists(disk_img_filename)) {
Julian Hall351a0722023-01-12 11:58:42 +000058 printf("Error: %s does not exist\n", disk_img_filename.c_str());
59 return -1;
60 }
61
62 /* Create fwu configuration based on the input disk image */
63 int status = app.configure(disk_img_filename.c_str());
64
65 if (status) {
Julian Hall351a0722023-01-12 11:58:42 +000066 printf("Error: failed to configure with status: %d\n", status);
67 return -1;
68 }
69
70 /* Attempt to derive boot info from metadata. Assume bootloader booted from the
71 * active index. This can be overridden via command-line parameter.
72 */
73 unsigned int boot_index;
74 unsigned int metadata_version;
75
76 status = app.get_boot_info(boot_index, metadata_version);
77
78 if (status) {
Julian Hall351a0722023-01-12 11:58:42 +000079 printf("No recognised metadata, assume default boot index and version\n");
80
81 boot_index = 0;
82 metadata_version = 2;
83 }
84
85 /* Allow for command-line overrides */
86 boot_index = parse_numeric_option("-boot-index", argc, argv, boot_index);
87 metadata_version = parse_numeric_option("-meta-ver", argc, argv, metadata_version);
88
89 /* Options for printing fwu info */
90 bool is_print_img_dir = option_selected("-dir", argc, argv);
91 bool is_print_metadata = option_selected("-meta", argc, argv);
92
93 /* Parse input image related parameters*/
94 update_img_filename = parse_string_option("-img", argc, argv, "");
95 img_type_uuid = parse_string_option("-img-type", argc, argv, "");
96
97 /* Check if image file exists (if one was specified) */
98 if (!update_img_filename.empty() && !file_exists(update_img_filename)) {
Julian Hall351a0722023-01-12 11:58:42 +000099 printf("Error: %s does not exist\n", update_img_filename.c_str());
100 return -1;
101 }
102
103 /* Check if img type canonical uuid is well formed */
104 if (!img_type_uuid.empty() && !uuid_is_valid(img_type_uuid.c_str())) {
Julian Hall351a0722023-01-12 11:58:42 +0000105 printf("Error: image type uuid invalid\n");
106 return -1;
107 }
108
109 /* Initialise the update_agent. Missing or corrupt metadata will get repaired
110 */
111 status = app.init_update_agent(boot_index, metadata_version);
112
113 if (!status) {
Gyorgy Szing3c446242023-03-31 01:53:15 +0200114 printf("Update agent started: boot index: %u metadata ver: %u\n", boot_index,
115 metadata_version);
Julian Hall351a0722023-01-12 11:58:42 +0000116
117 if (is_print_img_dir)
118 cmd_print_image_dir(app);
119
120 if (is_print_metadata) {
Julian Hall351a0722023-01-12 11:58:42 +0000121 if (metadata_version == 1)
122 cmd_print_metadata_v1(app);
123 else if (metadata_version == 2)
124 cmd_print_metadata_v2(app);
125 else
126 printf("Unsupported metadata version\n");
Julian Hall351a0722023-01-12 11:58:42 +0000127 }
128
129 if (!update_img_filename.empty() && !img_type_uuid.empty()) {
Julian Hall351a0722023-01-12 11:58:42 +0000130 status = cmd_update_image(app, img_type_uuid, update_img_filename);
131
132 } else if (!update_img_filename.empty() || !img_type_uuid.empty()) {
Julian Hall351a0722023-01-12 11:58:42 +0000133 printf("Error: both image filename and uuid arguments are needed\n");
134 return -1;
135 }
136 }
137
138 if (!status)
139 printf("OK\n");
140 else
141 printf("Error status: %d\n", status);
142
143 return status;
144}
145
Gyorgy Szing3c446242023-03-31 01:53:15 +0200146static bool option_selected(const char *option_switch, int argc, char *argv[])
Julian Hall351a0722023-01-12 11:58:42 +0000147{
148 bool is_selected = false;
149
150 for (int i = 1; (i < argc) && !is_selected; ++i) {
Julian Hall351a0722023-01-12 11:58:42 +0000151 is_selected = (strcmp(argv[i], option_switch) == 0);
152 }
153
154 return is_selected;
155}
156
Gyorgy Szing3c446242023-03-31 01:53:15 +0200157static std::string parse_string_option(const char *option_switch, int argc, char *argv[],
158 const char *default_val)
Julian Hall351a0722023-01-12 11:58:42 +0000159{
160 std::string option = std::string(default_val);
161
162 for (int i = 1; i + 1 < argc; ++i) {
Julian Hall351a0722023-01-12 11:58:42 +0000163 if (strcmp(argv[i], option_switch) == 0) {
Julian Hall351a0722023-01-12 11:58:42 +0000164 option = std::string(argv[i + 1]);
165 break;
166 }
167 }
168
169 return option;
170}
171
Gyorgy Szing3c446242023-03-31 01:53:15 +0200172static int parse_numeric_option(const char *option_switch, int argc, char *argv[], int default_val)
Julian Hall351a0722023-01-12 11:58:42 +0000173{
174 int option = default_val;
175
176 for (int i = 1; i + 1 < argc; ++i) {
Julian Hall351a0722023-01-12 11:58:42 +0000177 if (strcmp(argv[i], option_switch) == 0) {
Julian Hall351a0722023-01-12 11:58:42 +0000178 std::istringstream iss(argv[i + 1]);
179 int val;
180
181 iss >> val;
182
183 if (!iss.fail())
184 option = val;
185
186 break;
187 }
188 }
189
190 return option;
191}
192
193static bool file_exists(const std::string &filename)
194{
195 struct stat stat_buf;
196
197 return stat(filename.c_str(), &stat_buf) == 0;
198}
199
200static void print_usage(void)
201{
202 printf("Usage: fwu disk-filename [-dir -meta] [-boot-index number -meta-ver number] "
Gyorgy Szing3c446242023-03-31 01:53:15 +0200203 "[-img filename -img-type uuid]\n");
Julian Hall351a0722023-01-12 11:58:42 +0000204}
205
206static void print_help(void)
207{
208 print_usage();
209
210 printf("\n");
211 printf("\tdisk-filename\tDisk image file to update\n");
212 printf("\t-dir\t\tPrint image directory\n");
213 printf("\t-meta\t\tPrint FWU metadata\n");
214 printf("\t-boot-index\tOverride default boot index [0..n]\n");
215 printf("\t-meta-ver\tSpecify FWU metadata to use\n");
216 printf("\t-img\t\tFile containing image update\n");
217 printf("\t-img-type\tCanonical UUID of image to update\n");
218}