├── image ├── a.txt ├── c.txt └── srv │ ├── b.txt │ └── http │ └── index.html ├── .gitignore ├── partitions.csv ├── sdkconfig └── sdkconfig.h ├── Makefile.files ├── README.md ├── Makefile ├── src └── main.c └── LICENSE /image/a.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /image/c.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /image/srv/b.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /image/srv/http/index.html: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | mkfatfs 2 | *.bak 3 | *.o 4 | build 5 | partition_table.bin 6 | *.img 7 | -------------------------------------------------------------------------------- /partitions.csv: -------------------------------------------------------------------------------- 1 | # Name, Type, SubType, Offset, Size, Flags 2 | # Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild 3 | phy_init, data, phy, , 0x001000 , 4 | factory, app, factory, , 1M , 5 | nvs, data, nvs, , 128k , 6 | storage, data, fat, 0x130000 , 0x270000 , 7 | -------------------------------------------------------------------------------- /sdkconfig/sdkconfig.h: -------------------------------------------------------------------------------- 1 | # pragma once 2 | 3 | #define CONFIG_WL_SECTOR_SIZE 4096 4 | #define CONFIG_LOG_DEFAULT_LEVEL 3 5 | #define CONFIG_PARTITION_TABLE_OFFSET 0x8000 6 | #define CONFIG_ESPTOOLPY_FLASHSIZE "4MB" 7 | #define CONFIG_FATFS_CODEPAGE_437 1 8 | #define CONFIG_FATFS_PER_FILE_CACHE 1 9 | #define CONFIG_FATFS_FS_LOCK 0 10 | #define CONFIG_FATFS_CODEPAGE 437 11 | #define CONFIG_FATFS_LFN_HEAP 1 12 | #define CONFIG_FATFS_TIMEOUT_MS 10000 13 | #define CONFIG_FATFS_MAX_LFN 255 14 | #define CONFIG_FATFS_API_ENCODING_ANSI_OEM 1 15 | -------------------------------------------------------------------------------- /Makefile.files: -------------------------------------------------------------------------------- 1 | SOURCE_FILES := \ 2 | src/main.c 3 | 4 | INCLUDE_DIRS := \ 5 | . \ 6 | ../src \ 7 | $(addprefix $(IDF_PATH)/components/spi_flash/sim/stubs/, \ 8 | app_update/include \ 9 | driver/include \ 10 | esp32/include \ 11 | freertos/include \ 12 | log/include \ 13 | newlib/include \ 14 | sdmmc/include \ 15 | vfs/include \ 16 | ) \ 17 | $(addprefix $(IDF_PATH)/components/, \ 18 | soc/esp32/include \ 19 | esp32/include \ 20 | bootloader_support/include \ 21 | app_update/include \ 22 | spi_flash/include \ 23 | wear_levelling/include \ 24 | fatfs/src \ 25 | fatfs/test_fatfs_host/sdkconfig \ 26 | esp_common/include \ 27 | ) 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ESP32 wear levelling mkfatfs 2 | =========================== 3 | 4 | This tool creates ESP32 fatfs images with wear levelling support. 5 | 6 | # Operation 7 | 8 | ## Requirements 9 | 10 | This tool requires esp-idf to be [set up properly](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/). 11 | If you do run into issues building the tool check that the environment variable `IDF_PATH` points to your installation directory of esp-idf. 12 | 13 | **IMPORTANT**: Please make sure you are using at recent version of esp-idf (at least v3.2). 14 | 15 | 16 | ## Configuration 17 | 18 | At the moment configuration is done via the file `sdkconfig/sdkconfig.h`. I plan on adding a menuconfig though. 19 | See section Limitations at the end of this docuemnt for limits on valid configuration values. 20 | 21 | **IMPORTANT**: Do not forget to place your partition table in the file `partitions.csv` 22 | 23 | 24 | ## Building 25 | 26 | Building the tool should be straight forward. Just type `make` 27 | 28 | This will create the tool binary called `mkfatfs` 29 | 30 | 31 | ## Usage 32 | 33 | To build a fatfs image with wear levelling support just place all files you want to have in you fatfs in a directory and run 34 | 35 | `./mkfatfs -c fatfs.img` 36 | 37 | This will create the file `fatfs.img` containing the fatfs image. 38 | 39 | To flash the image to your ESP run 40 | 41 | ``` 42 | python2 "$IDF_PATH"/components/esptool_py/esptool/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 230400 --before default_reset --after hard_reset write_flash --flash_mode dio --flash_size 4MB 0x130000 fatfs.img 43 | ``` 44 | 45 | Depending on your partition layout you might have to update the address argument in above command 46 | 47 | 48 | Issues? Check out the section Troubleshooting at the end of this document 49 | 50 | ### List of options 51 | ``` 52 | Usage: ./mkfatfs [-c ] [-t ] [-l ] 53 | Options: 54 | -c Set directory to build fatfs from to . Defaults to 'image' 55 | -t Set file to read partition table from to . Defaults to 'partition_table.bin' 56 | -l Set label of partition from partition table to use to . Defaults to 'storage' 57 | ``` 58 | 59 | # Limitations 60 | 61 | Currently this tool supports only sector sizes of 4096 because the flash mocking code of esp-idf is not suitable for other sector sizes 62 | 63 | # Troubleshooting 64 | 65 | ## Truncated file names 66 | 67 | By default your fatfs might be set up to use short file names only. Please make sure to enable CONFIG_FATFS_LFN_HEAP or CONFIG_FATFS_LFN_STACK in menuconfig (thanks @sriharsha-rbnd for pointing this out) 68 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | COMPONENT := mkfatfs 2 | 3 | COMPONENT_LIB := lib$(COMPONENT).a 4 | 5 | STUBS_LIB_DIR := $(IDF_PATH)/components/spi_flash/sim/stubs 6 | STUBS_LIB_BUILD_DIR := $(STUBS_LIB_DIR)/build 7 | STUBS_LIB := libstubs.a 8 | 9 | SPI_FLASH_SIM_DIR := $(IDF_PATH)/components/spi_flash/sim 10 | SPI_FLASH_SIM_BUILD_DIR := $(SPI_FLASH_SIM_DIR)/build 11 | SPI_FLASH_SIM_LIB := libspi_flash.a 12 | 13 | WEAR_LEVELLING_DIR := $(IDF_PATH)/components/wear_levelling/test_wl_host 14 | WEAR_LEVELLING_BUILD_DIR := $(WEAR_LEVELLING_DIR)/build 15 | WEAR_LEVELLING_LIB := libwl.a 16 | 17 | FATFS_DIR := $(IDF_PATH)/components/fatfs/test_fatfs_host 18 | FATFS_BUILD_DIR := $(FATFS_DIR)/build 19 | FATFS_LIB := libfatfs.a 20 | 21 | include Makefile.files 22 | 23 | all: mkfatfs 24 | 25 | ifndef SDKCONFIG 26 | SDKCONFIG_DIR := $(dir $(realpath sdkconfig/sdkconfig.h)) 27 | SDKCONFIG := $(SDKCONFIG_DIR)sdkconfig.h 28 | else 29 | SDKCONFIG_DIR := $(dir $(realpath $(SDKCONFIG))) 30 | endif 31 | 32 | INCLUDE_FLAGS := $(addprefix -I, $(INCLUDE_DIRS) $(SDKCONFIG_DIR) $(IDF_PATH)/tools/catch) 33 | 34 | CPPFLAGS += $(INCLUDE_FLAGS) -g -m32 -ggdb 35 | CXXFLAGS += $(INCLUDE_FLAGS) -std=c++11 -g -m32 -ggdb 36 | 37 | # Build libraries that this component is dependent on 38 | $(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB): force 39 | $(MAKE) -C $(STUBS_LIB_DIR) lib SDKCONFIG=$(SDKCONFIG) 40 | 41 | $(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB): force 42 | $(MAKE) -C $(SPI_FLASH_SIM_DIR) lib SDKCONFIG=$(SDKCONFIG) 43 | 44 | $(WEAR_LEVELLING_BUILD_DIR)/$(WEAR_LEVELLING_LIB): force 45 | $(MAKE) -C $(WEAR_LEVELLING_DIR) lib SDKCONFIG=$(SDKCONFIG) 46 | 47 | $(FATFS_BUILD_DIR)/$(FATFS_LIB): force 48 | $(MAKE) -C $(FATFS_DIR) lib SDKCONFIG=$(SDKCONFIG) 49 | 50 | # Create target for building this component as a library 51 | CFILES := $(filter %.c, $(SOURCE_FILES)) 52 | CPPFILES := $(filter %.cpp, $(SOURCE_FILES)) 53 | 54 | CTARGET = ${2}/$(patsubst %.c,%.o,$(notdir ${1})) 55 | CPPTARGET = ${2}/$(patsubst %.cpp,%.o,$(notdir ${1})) 56 | 57 | ifndef BUILD_DIR 58 | BUILD_DIR := build 59 | endif 60 | 61 | OBJ_FILES := $(addprefix $(BUILD_DIR)/, $(filter %.o, $(notdir $(SOURCE_FILES:.cpp=.o) $(SOURCE_FILES:.c=.o)))) 62 | 63 | define COMPILE_C 64 | $(call CTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG) 65 | echo $(call CTARGET, ${1}, $(BUILD_DIR)) 66 | mkdir -p $(BUILD_DIR) 67 | echo precompile 68 | $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $(call CTARGET, ${1}, $(BUILD_DIR)) ${1} 69 | echo postcompile 70 | endef 71 | 72 | define COMPILE_CPP 73 | $(call CPPTARGET, ${1}, $(BUILD_DIR)) : ${1} $(SDKCONFIG) 74 | echo $(call CPPTARGET, ${1}, $(BUILD_DIR)) 75 | mkdir -p $(BUILD_DIR) 76 | echo precompile 77 | $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $(call CPPTARGET, ${1}, $(BUILD_DIR)) ${1} 78 | echo postcompile 79 | endef 80 | 81 | $(BUILD_DIR)/$(COMPONENT_LIB): $(OBJ_FILES) $(SDKCONFIG) 82 | mkdir -p $(BUILD_DIR) 83 | echo "ar in: $@" 84 | echo "ar out: $^" 85 | $(AR) rcs $@ $^ 86 | 87 | lib: $(BUILD_DIR)/$(COMPONENT_LIB) 88 | 89 | $(foreach cfile, $(CFILES), $(eval $(call COMPILE_C, $(cfile)))) 90 | $(foreach cxxfile, $(CPPFILES), $(eval $(call COMPILE_CPP, $(cxxfile)))) 91 | 92 | # Create target for building this component as a standalone binary 93 | BIN_SOURCE_FILES = \ 94 | main.c \ 95 | 96 | BIN_OBJ_FILES = $(filter %.o, $(TEST_SOURCE_FILES:.cpp=.o) $(TEST_SOURCE_FILES:.c=.o)) 97 | 98 | mkfatfs: lib $(BIN_OBJ_FILES) $(WEAR_LEVELLING_BUILD_DIR)/$(WEAR_LEVELLING_LIB) $(SPI_FLASH_SIM_BUILD_DIR)/$(SPI_FLASH_SIM_LIB) $(STUBS_LIB_BUILD_DIR)/$(STUBS_LIB) $(FATFS_BUILD_DIR)/$(FATFS_LIB) partition_table.bin $(SDKCONFIG) 99 | g++ $(LDFLAGS) $(CXXFLAGS) -o $@ $(BIN_OBJ_FILES) -L$(BUILD_DIR) -l:$(COMPONENT_LIB) -L$(WEAR_LEVELLING_BUILD_DIR) -l:$(WEAR_LEVELLING_LIB) -L$(SPI_FLASH_SIM_BUILD_DIR) -l:$(SPI_FLASH_SIM_LIB) -L$(STUBS_LIB_BUILD_DIR) -l:$(STUBS_LIB) -L$(FATFS_BUILD_DIR) -l:$(FATFS_LIB) 100 | 101 | # Create other necessary targets 102 | partition_table.bin: partitions.csv 103 | python $(IDF_PATH)/components/partition_table/gen_esp32part.py --verify $< $@ 104 | 105 | force: 106 | 107 | # Create target to cleanup files 108 | clean: 109 | $(MAKE) -C $(STUBS_LIB_DIR) clean 110 | $(MAKE) -C $(SPI_FLASH_SIM_DIR) clean 111 | $(MAKE) -C $(WEAR_LEVELLING_DIR) clean 112 | rm -f $(OBJ_FILES) $(BIN_OBJ_FILES) mkfatfs $(COMPONENT_LIB) partition_table.bin 113 | 114 | .PHONY: all mkfatfs force 115 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "ff.h" 11 | #include "esp_partition.h" 12 | #include "wear_levelling.h" 13 | #include "diskio.h" 14 | #include "diskio_wl.h" 15 | #include "esp_spi_flash.h" 16 | 17 | #define MAX_PATH_LEN 256 18 | #define DEFAULT_IMAGE_DIR "image" 19 | #define DEFAULT_PARTITION_TABLE "partition_table.bin" 20 | #define DEFAULT_PARTITION_LABEL "storage" 21 | 22 | extern void _spi_flash_init(const char* chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partition_bin); 23 | 24 | extern esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory, const void** out_ptr, spi_flash_mmap_handle_t* out_handle); 25 | 26 | static esp_err_t fat_add_path(char* local_path, char* fat_path); 27 | 28 | #define DIRENT_FOR_EACH(cursor, dir) \ 29 | for((cursor) = readdir((dir)); (cursor); (cursor) = readdir((dir))) 30 | 31 | static void pathcat(char* path, size_t path_len, char* base, char* name) { 32 | memset(path, 0, path_len); 33 | snprintf(path, path_len, "%s/%s", base, name); 34 | } 35 | 36 | static esp_err_t fat_add_file(char* local_path, char* fat_path) { 37 | FIL file; 38 | int fd; 39 | ssize_t len; 40 | char buffer[4096]; 41 | esp_err_t err = ESP_OK; 42 | 43 | printf("\tAdding file '%s' => '%s'\n", local_path, fat_path); 44 | 45 | if((err = f_open(&file, fat_path, FA_OPEN_ALWAYS | FA_READ | FA_WRITE))) { 46 | fprintf(stderr, "Failed to open file '%s' on fatfs: %d\n", fat_path, err); 47 | goto fail; 48 | } 49 | 50 | if((fd = open(local_path, O_RDONLY)) < 0) { 51 | fprintf(stderr, "Failed to open file on local fs\n"); 52 | err = fd; 53 | goto fail_fat_open; 54 | } 55 | 56 | // Read data chunkwise and write it to the wear levelling fatfs 57 | while((len = read(fd, buffer, sizeof(buffer))) > 0) { 58 | ssize_t remainder = len; 59 | while(remainder > 0) { 60 | UINT wrlen; 61 | if((err = f_write(&file, buffer + len - remainder, remainder, &wrlen))) { 62 | fprintf(stderr, "Failed to write to fat fs: %d\n", err); 63 | goto fail_local_open; 64 | } 65 | remainder -= wrlen; 66 | } 67 | } 68 | 69 | err = ESP_OK; 70 | if(len < 0) { 71 | err = errno; 72 | fprintf(stderr, "Faled to read from local file: %s(%d)\n", strerror(err), err); 73 | } 74 | 75 | fail_local_open: 76 | close(fd); 77 | fail_fat_open: 78 | f_close(&file); 79 | fail: 80 | return err; 81 | } 82 | 83 | static esp_err_t fat_add_directory_contents(char* local_path, char* fat_path) { 84 | esp_err_t err = ESP_OK; 85 | struct dirent* cursor; 86 | DIR* dir = opendir(local_path); 87 | if(!dir) { 88 | err = errno; 89 | goto fail; 90 | } 91 | 92 | // readdir does not touch errno on success, thus we should set it to zero 93 | errno = 0; 94 | DIRENT_FOR_EACH(cursor, dir) { 95 | /* Putting this on stack might be a problem when using large, 96 | * deeply nested fs images, maybe we should move it to heap? 97 | */ 98 | char local_entry_path[MAX_PATH_LEN]; 99 | char fat_entry_path[MAX_PATH_LEN]; 100 | 101 | if(!cursor) { 102 | err = errno; 103 | break; 104 | } 105 | 106 | // Ingore current and parent directory 107 | if(!strcmp(cursor->d_name, ".") || !strcmp(cursor->d_name, "..")) { 108 | continue; 109 | } 110 | 111 | pathcat(local_entry_path, sizeof(local_entry_path), local_path, cursor->d_name); 112 | pathcat(fat_entry_path, sizeof(fat_entry_path), fat_path, cursor->d_name); 113 | if((err = fat_add_path(local_entry_path, fat_entry_path))) { 114 | goto fail; 115 | } 116 | } 117 | 118 | fail: 119 | closedir(dir); 120 | return err; 121 | } 122 | 123 | static esp_err_t fat_add_directory(char* local_path, char* fat_path) { 124 | esp_err_t err = ESP_OK; 125 | FILINFO finfo; 126 | 127 | if(!(err = f_stat(fat_path, &finfo))) { 128 | if((err = f_unlink(fat_path))) { 129 | fprintf(stderr, "Failed to unlink '%s'\n", fat_path); 130 | goto fail; 131 | } 132 | } 133 | 134 | printf("\tAdding directory '%s' => '%s'\n", local_path, fat_path); 135 | if((err = f_mkdir(fat_path))) { 136 | goto fail; 137 | } 138 | 139 | err = fat_add_directory_contents(local_path, fat_path); 140 | 141 | fail: 142 | return err; 143 | } 144 | 145 | static esp_err_t fat_add_path(char* local_path, char* fat_path) { 146 | esp_err_t err; 147 | struct stat pathinfo; 148 | 149 | if(stat(local_path, &pathinfo)) { 150 | fprintf(stderr, "Stat of '%s' failed: %s(%d)\n", local_path, strerror(errno), errno); 151 | err = errno; 152 | goto fail; 153 | } 154 | 155 | if(pathinfo.st_mode & S_IFDIR) { 156 | err = fat_add_directory(local_path, fat_path); 157 | } else if(pathinfo.st_mode & S_IFREG) { 158 | err = fat_add_file(local_path, fat_path); 159 | } else { 160 | err = EINVAL; 161 | } 162 | 163 | fail: 164 | return err; 165 | 166 | } 167 | 168 | static esp_err_t file_exists(const char* name) { 169 | struct stat pathinfo; 170 | return !stat(name, &pathinfo); 171 | } 172 | 173 | void show_usage(char* prgrm) { 174 | fprintf(stderr, "Usage: %s [-c ] [-t ] [-l ] \n", prgrm); 175 | fprintf(stderr, "Options:\n"); 176 | fprintf(stderr, "\t -c \tSet directory to build fatfs from to . Defaults to '%s'\n", DEFAULT_IMAGE_DIR); 177 | fprintf(stderr, "\t -t \tSet file to read partition table from to . Defaults to '%s'\n", DEFAULT_PARTITION_TABLE); 178 | fprintf(stderr, "\t -l \tSet label of partition from partition table to use to . Defaults to '%s'\n", DEFAULT_PARTITION_LABEL); 179 | }; 180 | 181 | int main(int argc, char** argv) { 182 | esp_err_t err; 183 | int fd, opt; 184 | char* flash_ptr; 185 | size_t offset = 0; 186 | spi_flash_mmap_handle_t hndl; 187 | wl_handle_t wl_handle; 188 | FRESULT fr_result; 189 | BYTE pdrv; 190 | FATFS fs; 191 | UINT bw; 192 | DWORD part_list[] = {100, 0, 0, 0}; 193 | BYTE work_area[FF_MAX_SS]; 194 | const esp_partition_t* partition; 195 | 196 | char* image_src_dir = DEFAULT_IMAGE_DIR; 197 | const char* fatfs_image; 198 | const char* partition_table = DEFAULT_PARTITION_TABLE; 199 | const char* partition_label = DEFAULT_PARTITION_LABEL; 200 | 201 | while((opt = getopt(argc, argv, "c:t:l:h")) >= 0) { 202 | switch(opt) { 203 | case 'c': 204 | image_src_dir = strdup(optarg); 205 | if(!image_src_dir) { 206 | err = ENOMEM; 207 | fprintf(stderr, "Failed to allocate memory for image_src_dir\n"); 208 | goto fail; 209 | } 210 | break; 211 | case 't': 212 | partition_table = strdup(optarg); 213 | if(!partition_table) { 214 | err = ENOMEM; 215 | fprintf(stderr, "Failed to allocate memory for partition_table\n"); 216 | goto fail; 217 | } 218 | break; 219 | case 'l': 220 | partition_label = strdup(optarg); 221 | if(!partition_label) { 222 | err = ENOMEM; 223 | fprintf(stderr, "Failed to allocate memory for partition_label\n"); 224 | goto fail; 225 | } 226 | break; 227 | case 'h': 228 | default: 229 | show_usage(argv[0]); 230 | err = -1; 231 | goto fail; 232 | } 233 | } 234 | 235 | if(optind >= argc) { 236 | fprintf(stderr, "Missing required positional argument \n"); 237 | show_usage(argv[0]); 238 | err = -1; 239 | goto fail; 240 | } 241 | 242 | if(!file_exists(image_src_dir)) { 243 | err = EINVAL; 244 | fprintf(stderr, "Fatfs root directory '%s' does not exist\n", image_src_dir); 245 | goto fail; 246 | } 247 | 248 | if(!file_exists(partition_table)) { 249 | err = EINVAL; 250 | fprintf(stderr, "Partition table file '%s' does not exist\n", partition_table); 251 | goto fail; 252 | } 253 | 254 | fatfs_image = argv[optind]; 255 | 256 | _spi_flash_init(CONFIG_ESPTOOLPY_FLASHSIZE, CONFIG_WL_SECTOR_SIZE * 16, CONFIG_WL_SECTOR_SIZE, CONFIG_WL_SECTOR_SIZE, partition_table); 257 | 258 | partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label); 259 | if(!partition) { 260 | err = ENOENT; 261 | fprintf(stderr, "No partition with label '%s' found\n", partition_label); 262 | goto fail; 263 | } 264 | 265 | // Mount wear-levelled partition 266 | if((err = wl_mount(partition, &wl_handle))) { 267 | fprintf(stderr, "Failed to mount partition: %d\n", err); 268 | goto fail; 269 | } 270 | 271 | 272 | // Get emulated physical drive 273 | if((err = ff_diskio_get_drive(&pdrv))) { 274 | fprintf(stderr, "Failed to get emulated drive: %d\n", err); 275 | goto fail_mount; 276 | } 277 | 278 | // Get wear levelling partition wrapper 279 | if((err = ff_diskio_register_wl_partition(pdrv, wl_handle))) { 280 | fprintf(stderr, "Failed to get wear levelling wrapper for patition: %d\n", err); 281 | goto fail_mount; 282 | } 283 | 284 | // Create fatfs partition table 285 | if((fr_result = f_fdisk(pdrv, part_list, work_area))) { 286 | err = fr_result; 287 | fprintf(stderr, "Failed to create fatfs partition table: %d\n", err); 288 | goto fail_mount; 289 | } 290 | 291 | // Create fatfs fat filesystem 292 | if((fr_result = f_mkfs("", FM_ANY, 0, work_area, sizeof(work_area)))) { 293 | err = fr_result; 294 | fprintf(stderr, "Failed to create fatfs filesystem: %d\n", err); 295 | goto fail_mount; 296 | } 297 | 298 | if((fr_result = f_mount(&fs, "", 0))) { 299 | err = fr_result; 300 | fprintf(stderr, "Failed to mount fatfs filesystem: %d\n", err); 301 | goto fail_mount; 302 | } 303 | 304 | printf("Adding files:\n"); 305 | if((err = fat_add_directory_contents(image_src_dir, ""))) { 306 | fprintf(stderr, "Failed to add files to fat image: %s(%d)\n", strerror(err), err); 307 | goto fail_mount; 308 | 309 | } 310 | 311 | // Use mmap stub wrapper to obtain pointer to flash memory buffer 312 | spi_flash_mmap(0, 0, 0, (const void**)&flash_ptr, &hndl); 313 | 314 | // Move pointer to start of data partition 315 | flash_ptr += partition->address; 316 | 317 | printf("Saving fatfs image to '%s'\n", fatfs_image); 318 | if((fd = open(fatfs_image, O_RDWR | O_CREAT, 0644)) < 0) { 319 | err = errno; 320 | fprintf(stderr, "Failed to open image file: %s(%d)\n", strerror(err), err); 321 | goto fail_mount; 322 | } 323 | 324 | printf("Saving %zu bytes to file\n", partition->size); 325 | 326 | while(offset < partition->size) { 327 | ssize_t write_len = write(fd, flash_ptr + offset, partition->size - offset); 328 | if(write_len < 0) { 329 | err = errno; 330 | fprintf(stderr, "Failed to write image file: %s(%d)\n", strerror(err), err); 331 | goto fail_image_open; 332 | } 333 | offset += write_len; 334 | } 335 | 336 | printf("Image complete\n"); 337 | 338 | fail_image_open: 339 | close(fd); 340 | fail_mount: 341 | f_mount(0, "", 0); 342 | fail: 343 | return err; 344 | } 345 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2018 Tobias Schramm 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------