|
| 1 | +/* library_fs.c |
| 2 | + * |
| 3 | + * Copyright (C) 2025 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfBoot. |
| 6 | + * |
| 7 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfBoot is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | +#include <string.h> |
| 24 | +#include <stdlib.h> |
| 25 | + |
| 26 | +#include "image.h" |
| 27 | +#include "printf.h" |
| 28 | +#include "wolfboot/wolfboot.h" |
| 29 | + |
| 30 | +/* Helper function to convert partition ID to string */ |
| 31 | +static const char* partition_name(uint8_t part) |
| 32 | +{ |
| 33 | + switch (part) { |
| 34 | + case PART_BOOT: |
| 35 | + return "BOOT"; |
| 36 | + case PART_UPDATE: |
| 37 | + return "UPDATE"; |
| 38 | + default: |
| 39 | + return "UNKNOWN"; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/* Helper function to convert state value to string */ |
| 44 | +static const char* state_name(uint8_t state) |
| 45 | +{ |
| 46 | + switch (state) { |
| 47 | + case IMG_STATE_NEW: |
| 48 | + return "NEW"; |
| 49 | + case IMG_STATE_UPDATING: |
| 50 | + return "UPDATING"; |
| 51 | + case IMG_STATE_SUCCESS: |
| 52 | + return "SUCCESS"; |
| 53 | + default: |
| 54 | + return "UNKNOWN"; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/* Print partition state */ |
| 59 | +static int cmd_get_state(uint8_t part) |
| 60 | +{ |
| 61 | + uint8_t state; |
| 62 | + int ret; |
| 63 | + |
| 64 | + ret = wolfBoot_get_partition_state(part, &state); |
| 65 | + if (ret != 0) { |
| 66 | + wolfBoot_printf("Error: Failed to get state for %s partition (error: %d)\n", |
| 67 | + partition_name(part), ret); |
| 68 | + return -1; |
| 69 | + } |
| 70 | + |
| 71 | + wolfBoot_printf("%s partition state: %s (0x%02X)\n", |
| 72 | + partition_name(part), state_name(state), state); |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +/* Print all partition states */ |
| 77 | +static int cmd_get_all_states(void) |
| 78 | +{ |
| 79 | + int ret = 0; |
| 80 | + |
| 81 | + wolfBoot_printf("=== Partition States ===\n"); |
| 82 | + |
| 83 | + if (cmd_get_state(PART_BOOT) != 0) |
| 84 | + ret = -1; |
| 85 | + |
| 86 | + if (cmd_get_state(PART_UPDATE) != 0) |
| 87 | + ret = -1; |
| 88 | + |
| 89 | + return ret; |
| 90 | +} |
| 91 | + |
| 92 | +/* Trigger an update */ |
| 93 | +static int cmd_update_trigger(void) |
| 94 | +{ |
| 95 | + wolfBoot_printf("Triggering update...\n"); |
| 96 | + wolfBoot_update_trigger(); |
| 97 | + wolfBoot_printf("Update triggered successfully. UPDATE partition set to UPDATING state.\n"); |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +/* Mark current boot as successful */ |
| 102 | +static int cmd_success(void) |
| 103 | +{ |
| 104 | + wolfBoot_printf("Marking BOOT partition as SUCCESS...\n"); |
| 105 | + wolfBoot_success(); |
| 106 | + wolfBoot_printf("BOOT partition marked as SUCCESS.\n"); |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +/* Print usage information */ |
| 111 | +static void print_usage(const char* prog_name) |
| 112 | +{ |
| 113 | + wolfBoot_printf("wolfBoot Partition Manager CLI\n"); |
| 114 | + wolfBoot_printf("\nUsage: %s <command> [options]\n\n", prog_name); |
| 115 | + wolfBoot_printf("Commands:\n"); |
| 116 | + wolfBoot_printf(" status - Show state of all partitions\n"); |
| 117 | + wolfBoot_printf(" get-boot - Get BOOT partition state\n"); |
| 118 | + wolfBoot_printf(" get-update - Get UPDATE partition state\n"); |
| 119 | + wolfBoot_printf(" update-trigger - Trigger an update (sets UPDATE partition to UPDATING)\n"); |
| 120 | + wolfBoot_printf(" success - Mark BOOT partition as SUCCESS\n"); |
| 121 | + wolfBoot_printf(" help - Show this help message\n"); |
| 122 | + wolfBoot_printf("\nPartitions:\n"); |
| 123 | + wolfBoot_printf(" BOOT - Currently running firmware partition\n"); |
| 124 | + wolfBoot_printf(" UPDATE - Staging partition for new firmware\n"); |
| 125 | + wolfBoot_printf("\nExamples:\n"); |
| 126 | + wolfBoot_printf(" %s status - Display all partition states\n", prog_name); |
| 127 | + wolfBoot_printf(" %s update-trigger - Stage an update for next boot\n", prog_name); |
| 128 | + wolfBoot_printf(" %s success - Confirm current firmware is working\n", prog_name); |
| 129 | + wolfBoot_printf("\n"); |
| 130 | +} |
| 131 | + |
| 132 | +int main(int argc, const char* argv[]) |
| 133 | +{ |
| 134 | + int ret = 0; |
| 135 | + |
| 136 | + /* Check for argument count */ |
| 137 | + if (argc != 2) { |
| 138 | + print_usage(argv[0]); |
| 139 | + return 1; |
| 140 | + } |
| 141 | + |
| 142 | + const char* command = argv[1]; |
| 143 | + |
| 144 | + /* Process commands */ |
| 145 | + if (strcmp(command, "status") == 0) { |
| 146 | + ret = cmd_get_all_states(); |
| 147 | + } |
| 148 | + else if (strcmp(command, "get-boot") == 0) { |
| 149 | + ret = cmd_get_state(PART_BOOT); |
| 150 | + } |
| 151 | + else if (strcmp(command, "get-update") == 0) { |
| 152 | + ret = cmd_get_state(PART_UPDATE); |
| 153 | + } |
| 154 | + else if (strcmp(command, "update-trigger") == 0) { |
| 155 | + ret = cmd_update_trigger(); |
| 156 | + } |
| 157 | + else if (strcmp(command, "success") == 0) { |
| 158 | + ret = cmd_success(); |
| 159 | + } |
| 160 | + else if (strcmp(command, "help") == 0 || strcmp(command, "--help") == 0 || |
| 161 | + strcmp(command, "-h") == 0) { |
| 162 | + print_usage(argv[0]); |
| 163 | + ret = 0; |
| 164 | + } |
| 165 | + else { |
| 166 | + wolfBoot_printf("Error: Unknown command '%s'\n\n", command); |
| 167 | + print_usage(argv[0]); |
| 168 | + ret = 1; |
| 169 | + } |
| 170 | + |
| 171 | + return ret; |
| 172 | +} |
0 commit comments