Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 1 | /* |
| 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 Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 10 | #include <sstream> |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 11 | #include <string> |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 12 | #include <sys/stat.h> |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 13 | |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 14 | #include "cmd_print_image_dir.h" |
| 15 | #include "cmd_print_metadata_v1.h" |
| 16 | #include "cmd_print_metadata_v2.h" |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 17 | #include "cmd_update_image.h" |
| 18 | #include "common/uuid/uuid.h" |
Gyorgy Szing | 386433f | 2023-03-31 12:52:25 +0000 | [diff] [blame] | 19 | #include "app/fwu_app.h" |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 20 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 21 | static bool option_selected(const char *option_switch, int argc, char *argv[]); |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 22 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 23 | static std::string parse_string_option(const char *option_switch, int argc, char *argv[], |
| 24 | const char *default_val); |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 25 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 26 | static int parse_numeric_option(const char *option_switch, int argc, char *argv[], int default_val); |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 27 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 28 | static bool file_exists(const std::string &filename); |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 29 | |
| 30 | static void print_usage(void); |
| 31 | static void print_help(void); |
| 32 | |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 33 | int 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 41 | if (option_selected("-h", argc, argv) || option_selected("-help", argc, argv) || |
| 42 | option_selected("--help", argc, argv)) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 43 | 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 Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 51 | 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 Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 58 | 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 Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 66 | 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 Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 79 | 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 Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 99 | 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 Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 105 | 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 114 | printf("Update agent started: boot index: %u metadata ver: %u\n", boot_index, |
| 115 | metadata_version); |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 116 | |
| 117 | if (is_print_img_dir) |
| 118 | cmd_print_image_dir(app); |
| 119 | |
| 120 | if (is_print_metadata) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 121 | 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 Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (!update_img_filename.empty() && !img_type_uuid.empty()) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 130 | status = cmd_update_image(app, img_type_uuid, update_img_filename); |
| 131 | |
| 132 | } else if (!update_img_filename.empty() || !img_type_uuid.empty()) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 133 | 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 Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 146 | static bool option_selected(const char *option_switch, int argc, char *argv[]) |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 147 | { |
| 148 | bool is_selected = false; |
| 149 | |
| 150 | for (int i = 1; (i < argc) && !is_selected; ++i) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 151 | is_selected = (strcmp(argv[i], option_switch) == 0); |
| 152 | } |
| 153 | |
| 154 | return is_selected; |
| 155 | } |
| 156 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 157 | static std::string parse_string_option(const char *option_switch, int argc, char *argv[], |
| 158 | const char *default_val) |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 159 | { |
| 160 | std::string option = std::string(default_val); |
| 161 | |
| 162 | for (int i = 1; i + 1 < argc; ++i) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 163 | if (strcmp(argv[i], option_switch) == 0) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 164 | option = std::string(argv[i + 1]); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return option; |
| 170 | } |
| 171 | |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 172 | static int parse_numeric_option(const char *option_switch, int argc, char *argv[], int default_val) |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 173 | { |
| 174 | int option = default_val; |
| 175 | |
| 176 | for (int i = 1; i + 1 < argc; ++i) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 177 | if (strcmp(argv[i], option_switch) == 0) { |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 178 | 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 | |
| 193 | static 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 | |
| 200 | static void print_usage(void) |
| 201 | { |
| 202 | printf("Usage: fwu disk-filename [-dir -meta] [-boot-index number -meta-ver number] " |
Gyorgy Szing | 3c44624 | 2023-03-31 01:53:15 +0200 | [diff] [blame] | 203 | "[-img filename -img-type uuid]\n"); |
Julian Hall | 351a072 | 2023-01-12 11:58:42 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static 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 | } |