|
| 1 | +/* filesystem.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 | + |
| 23 | +#include "hal.h" |
| 24 | +#include "wolfboot/wolfboot.h" |
| 25 | +#include "printf.h" |
| 26 | + |
| 27 | +#ifndef WOLFBOOT_PARTITION_FILENAME |
| 28 | +#error "WOLFBOOT_PARTITION_FILENAME needs to be defined for filesystem HAL" |
| 29 | +#endif |
| 30 | + |
| 31 | +#ifndef MIN |
| 32 | + #define MIN(x,y) ((x)<(y)?(x):(y)) |
| 33 | +#endif |
| 34 | + |
| 35 | +/* HAL Stubs */ |
| 36 | +void hal_init(void) |
| 37 | +{ |
| 38 | + return; |
| 39 | +} |
| 40 | +int hal_flash_write(uintptr_t address, const uint8_t *data, int len) |
| 41 | +{ |
| 42 | + (void)address; |
| 43 | + (void)data; |
| 44 | + (void)len; |
| 45 | + return -1; |
| 46 | +} |
| 47 | +int hal_flash_erase(uintptr_t address, int len) |
| 48 | +{ |
| 49 | + (void)address; |
| 50 | + (void)len; |
| 51 | + return -1; |
| 52 | +} |
| 53 | +void hal_flash_unlock(void) |
| 54 | +{ |
| 55 | + return; |
| 56 | +} |
| 57 | +void hal_flash_lock(void) |
| 58 | +{ |
| 59 | + return; |
| 60 | +} |
| 61 | +void hal_prepare_boot(void) |
| 62 | +{ |
| 63 | + return; |
| 64 | +} |
| 65 | +void do_boot(const uint32_t *app_offset) |
| 66 | +{ |
| 67 | + (void)app_offset; |
| 68 | +} |
| 69 | + |
| 70 | +/* filesystem access */ |
| 71 | +static XFILE fp = XBADFILE; |
| 72 | +static byte fp_write = 0; |
| 73 | +static long fp_size = 0; |
| 74 | + |
| 75 | +static int setup_file(byte read_only) |
| 76 | +{ |
| 77 | + if (fp != XBADFILE) { |
| 78 | + if (!read_only && !fp_write) { |
| 79 | + /* Need to reopen to allow writing */ |
| 80 | + XFCLOSE(fp); |
| 81 | + fp = XBADFILE; |
| 82 | + } |
| 83 | + } |
| 84 | + if (fp == XBADFILE) { |
| 85 | + fp = XFOPEN(WOLFBOOT_PARTITION_FILENAME, read_only ? "rb" : "r+b"); |
| 86 | + if (fp != XBADFILE) { |
| 87 | + fp_write = !read_only; |
| 88 | + if (XFSEEK(fp, 0, XSEEK_END) < 0 || (fp_size = XFTELL(fp)) < 0 || |
| 89 | + XFSEEK(fp, 0, XSEEK_SET) < 0) { |
| 90 | + /* Failed to get the file size */ |
| 91 | + XFCLOSE(fp); |
| 92 | + fp = XBADFILE; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return fp != XBADFILE ? 0 : -1; |
| 97 | +} |
| 98 | + |
| 99 | +int ext_flash_write(uintptr_t address, const uint8_t *data, int len) |
| 100 | +{ |
| 101 | + if (setup_file(0) != 0) |
| 102 | + return -1; |
| 103 | + if (address + len > (uintptr_t)fp_size) |
| 104 | + return -1; /* Don't allow writing past the file size */ |
| 105 | + if (XFSEEK(fp, address, XSEEK_SET) < 0) |
| 106 | + return -1; |
| 107 | + if ((int)XFWRITE(data, 1, len, fp) != len) |
| 108 | + return -1; |
| 109 | + if (XFFLUSH(fp) != 0) |
| 110 | + return -1; |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +int ext_flash_read(uintptr_t address, uint8_t *data, int len) |
| 115 | +{ |
| 116 | + if (setup_file(1) != 0) |
| 117 | + return -1; |
| 118 | + if (XFSEEK(fp, address, XSEEK_SET) < 0) |
| 119 | + return -1; |
| 120 | + return (int)XFREAD(data, 1, len, fp); |
| 121 | +} |
| 122 | +int ext_flash_erase(uintptr_t address, int len) |
| 123 | +{ |
| 124 | + byte zeros[256]; |
| 125 | + XMEMSET(zeros, 0, sizeof(zeros)); |
| 126 | + for (; len > 0; len -= sizeof(zeros), address += sizeof(zeros)) { |
| 127 | + if (ext_flash_write(address, zeros, (int)MIN((int)sizeof(zeros), len)) != 0) |
| 128 | + return -1; |
| 129 | + } |
| 130 | + return 0; |
| 131 | +} |
| 132 | +void ext_flash_lock(void) |
| 133 | +{ |
| 134 | + return; |
| 135 | +} |
| 136 | +void ext_flash_unlock(void) |
| 137 | +{ |
| 138 | + return; |
| 139 | +} |
0 commit comments