Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ tools/tpm/policy_create
tools/tpm/policy_sign
config/*.ld
test-lib
lib-fs

# Elf preprocessing tools
tools/squashelf/**
Expand Down Expand Up @@ -255,3 +256,8 @@ lib/r_tsip_rx
Debug/
Release/
language.settings.xml

# Eclipse
.cproject
.project
.settings/
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ ifeq ($(TARGET),library)
MAIN_TARGET:=libwolfboot.a
endif

ifeq ($(TARGET),library_fs)
MAIN_TARGET:=libwolfboot.a
endif

ifeq ($(TARGET),raspi3)
MAIN_TARGET:=wolfboot.bin
endif
Expand Down Expand Up @@ -214,6 +218,10 @@ test-lib: libwolfboot.a hal/library.o
@echo "\t[BIN] $@"
$(Q)$(CC) $(CFLAGS) -o $@ hal/library.o libwolfboot.a

lib-fs: libwolfboot.a hal/library_fs.o
@echo "\t[BIN] $@"
$(Q)$(CC) $(CFLAGS) -o $@ hal/library_fs.o libwolfboot.a

wolfboot.efi: wolfboot.elf
@echo "\t[BIN] $@"
$(Q)$(OBJCOPY) -j .rodata -j .text -j .sdata -j .data \
Expand Down Expand Up @@ -440,6 +448,8 @@ clean:
$(Q)rm -f tools/keytools/otp/otp-keystore-gen
$(Q)rm -f .stack_usage
$(Q)rm -f $(WH_NVM_BIN) $(WH_NVM_HEX)
$(Q)rm -f test-lib
$(Q)rm -f lib-fs
$(Q)$(MAKE) -C test-app clean V=$(V)
$(Q)$(MAKE) -C tools/check_config -s clean
$(Q)$(MAKE) -C stage1 -s clean
Expand Down
29 changes: 29 additions & 0 deletions config/examples/library_fs.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ARCH=sim
TARGET=library_fs

SIGN?=ED25519
HASH?=SHA256
IMAGE_HEADER_SIZE?=256
DEBUG=0
SPMATH?=0
SPMATHALL?=0

# Required for library (libwolfboot.a)
NO_LOADER=1
USE_GCC_HEADLESS=0

# Flash Partition Filename
WOLFBOOT_PARTITION_FILENAME=\"/dev/mtd0\"

# Flash Sector Size
WOLFBOOT_SECTOR_SIZE=0x2000
# Application Partition Size
WOLFBOOT_PARTITION_SIZE=0xA000
# Location in flash for wolfBoot
WOLFBOOT_ORIGIN=0x0
# Location in flash for boot partition
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x10000
# Location in flash for update partition
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x8000
# Location in flash for swap
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFE00
2 changes: 2 additions & 0 deletions config/examples/zynqmp.config
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ WOLFBOOT_SECTOR_SIZE=0x20000
# Application Partition Size
WOLFBOOT_PARTITION_SIZE=0x2A00000
# Location in Flash for wolfBoot
WOLFBOOT_ORIGIN=0x0
# Location in Flash for Primary Boot Partition
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x800000
# Load Partition to RAM Address
WOLFBOOT_LOAD_ADDRESS?=0x10000000
Expand Down
61 changes: 60 additions & 1 deletion docs/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ of the manifest header.


On success, zero is returned. If the image does not contain a valid 'magic number' at the beginning
of the manifest, or if the size of the image is bigger than `WOLFBOOT_PARTITION_SIZE`, -1 is returned.
of the manifest, or if the size of the image is bigger than `WOLFBOOT_PARTITION_SIZE`, -1 is returned.


If the `open_image_address` operation is successful, two other functions can be invoked:
Expand Down Expand Up @@ -123,3 +123,62 @@ Firmware Valid
booting 0x5609e3526590(actually exiting)
```

## Library mode: Partition Manager CLI Example

An example application using filesystem access is provided in `hal/library_fs.c`.

The CLI application `lib-fs` allow querying partition states, triggering updates, and marking the boot partition as successful.

### Building the lib-fs example

Step 1: use the example configuration to compile wolfBoot in library mode:

```
cp config/examples/library_fs.config .config
```

Step 2: Adjust the configuration to fit your partition layout and file path.

Step 3: Build the CLI application:

```
make lib-fs
```

This will produce the `lib-fs` executable.

### Using the Partition Manager CLI

The example configuration points the binary to access `/dev/mtd0` for partition data. You can simulate this file path with `modprobe mtdram total_size=16384 erase_size=128`. You may need to adjust the file permissions to allow read/write access.

Run the application with one of the supported commands:

```
./lib-fs <command>
```

Available commands:

- `status` : Show state of all partitions
- `get-boot` : Get BOOT partition state
- `get-update` : Get UPDATE partition state
- `update-trigger` : Trigger an update (sets UPDATE partition to UPDATING)
- `success` : Mark BOOT partition as SUCCESS
- `help` : Show usage information

#### Example usage

Show all partition states:
```
./lib-fs status
```

Trigger an update:
```
./lib-fs update-trigger
```

Mark the boot partition as successful:
```
./lib-fs success
```
3 changes: 2 additions & 1 deletion hal/library.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ int wolfBoot_start(void)
exit:
if (ret < 0) {
wolfBoot_printf("Failure %d: Hdr %d, Hash %d, Sig %d\n", ret,
os_image.hdr_ok, os_image.sha_ok, os_image.signature_ok);
(int)os_image.hdr_ok, (int)os_image.sha_ok,
(int)os_image.signature_ok);
}

return 0;
Expand Down
172 changes: 172 additions & 0 deletions hal/library_fs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/* library_fs.c
*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "image.h"
#include "printf.h"
#include "wolfboot/wolfboot.h"

/* Helper function to convert partition ID to string */
static const char* partition_name(uint8_t part)
{
switch (part) {
case PART_BOOT:
return "BOOT";
case PART_UPDATE:
return "UPDATE";
default:
return "UNKNOWN";
}
}

/* Helper function to convert state value to string */
static const char* state_name(uint8_t state)
{
switch (state) {
case IMG_STATE_NEW:
return "NEW";
case IMG_STATE_UPDATING:
return "UPDATING";
case IMG_STATE_SUCCESS:
return "SUCCESS";
default:
return "UNKNOWN";
}
}

/* Print partition state */
static int cmd_get_state(uint8_t part)
{
uint8_t state;
int ret;

ret = wolfBoot_get_partition_state(part, &state);
if (ret != 0) {
wolfBoot_printf("Error: Failed to get state for %s partition (error: %d)\n",
partition_name(part), ret);
return -1;
}

wolfBoot_printf("%s partition state: %s (0x%02X)\n",
partition_name(part), state_name(state), state);
return 0;
}

/* Print all partition states */
static int cmd_get_all_states(void)
{
int ret = 0;

wolfBoot_printf("=== Partition States ===\n");

if (cmd_get_state(PART_BOOT) != 0)
ret = -1;

if (cmd_get_state(PART_UPDATE) != 0)
ret = -1;

return ret;
}

/* Trigger an update */
static int cmd_update_trigger(void)
{
wolfBoot_printf("Triggering update...\n");
wolfBoot_update_trigger();
wolfBoot_printf("Update triggered successfully. UPDATE partition set to UPDATING state.\n");
return 0;
}

/* Mark current boot as successful */
static int cmd_success(void)
{
wolfBoot_printf("Marking BOOT partition as SUCCESS...\n");
wolfBoot_success();
wolfBoot_printf("BOOT partition marked as SUCCESS.\n");
return 0;
}

/* Print usage information */
static void print_usage(const char* prog_name)
{
wolfBoot_printf("wolfBoot Partition Manager CLI\n");
wolfBoot_printf("\nUsage: %s <command> [options]\n\n", prog_name);
wolfBoot_printf("Commands:\n");
wolfBoot_printf(" status - Show state of all partitions\n");
wolfBoot_printf(" get-boot - Get BOOT partition state\n");
wolfBoot_printf(" get-update - Get UPDATE partition state\n");
wolfBoot_printf(" update-trigger - Trigger an update (sets UPDATE partition to UPDATING)\n");
wolfBoot_printf(" success - Mark BOOT partition as SUCCESS\n");
wolfBoot_printf(" help - Show this help message\n");
wolfBoot_printf("\nPartitions:\n");
wolfBoot_printf(" BOOT - Currently running firmware partition\n");
wolfBoot_printf(" UPDATE - Staging partition for new firmware\n");
wolfBoot_printf("\nExamples:\n");
wolfBoot_printf(" %s status - Display all partition states\n", prog_name);
wolfBoot_printf(" %s update-trigger - Stage an update for next boot\n", prog_name);
wolfBoot_printf(" %s success - Confirm current firmware is working\n", prog_name);
wolfBoot_printf("\n");
}

int main(int argc, const char* argv[])
{
int ret = 0;

/* Check for argument count */
if (argc != 2) {
print_usage(argv[0]);
return 1;
}

const char* command = argv[1];

/* Process commands */
if (strcmp(command, "status") == 0) {
ret = cmd_get_all_states();
}
else if (strcmp(command, "get-boot") == 0) {
ret = cmd_get_state(PART_BOOT);
}
else if (strcmp(command, "get-update") == 0) {
ret = cmd_get_state(PART_UPDATE);
}
else if (strcmp(command, "update-trigger") == 0) {
ret = cmd_update_trigger();
}
else if (strcmp(command, "success") == 0) {
ret = cmd_success();
}
else if (strcmp(command, "help") == 0 || strcmp(command, "--help") == 0 ||
strcmp(command, "-h") == 0) {
print_usage(argv[0]);
ret = 0;
}
else {
wolfBoot_printf("Error: Unknown command '%s'\n\n", command);
print_usage(argv[0]);
ret = 1;
}

return ret;
}
2 changes: 1 addition & 1 deletion include/target.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#ifdef WOLFBOOT_FIXED_PARTITIONS

#ifdef ARCH_SIM
#if defined(ARCH_SIM) && !defined(WOLFBOOT_PARTITION_FILENAME)
#include <stdint.h>
/* use runtime ram base for simulator */
extern uint8_t *sim_ram_base;
Expand Down
2 changes: 2 additions & 0 deletions include/user_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,9 @@ extern int tolower(int c);
#define NO_HC128
#define NO_DES3
#define NO_WRITEV
#ifndef WOLFBOOT_PARTITION_FILENAME
#define NO_FILESYSTEM
#endif
#define NO_MAIN_DRIVER
#define NO_OLD_RNGNAME
#define NO_WOLFSSL_DIR
Expand Down
4 changes: 4 additions & 0 deletions options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -1035,3 +1035,7 @@ endif
ifeq ($(USE_UART3),1)
CFLAGS += -DUSE_UART3=1
endif

ifneq ($(WOLFBOOT_PARTITION_FILENAME),)
CFLAGS += -DWOLFBOOT_PARTITION_FILENAME=$(WOLFBOOT_PARTITION_FILENAME)
endif
Loading