63 lines
1.7 KiB
C
63 lines
1.7 KiB
C
#include "ff.h"
|
|
#include "diskio.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void print_usage() {
|
|
char* usage = "Usage: fatcopy fat_image file_to_copy_in destination_fat_name\n";
|
|
fprintf(stderr, "%s", usage);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
void copy_file(char *imagefile, char *file_to_copy, char* dest_filename) {
|
|
|
|
set_image_filename(imagefile);
|
|
FATFS fatfs;
|
|
if (f_mount(&fatfs, "", 0) != FR_OK) {
|
|
fprintf(stderr, "Failed to open image file %s\n", imagefile);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
FILE *file_in = fopen(file_to_copy, "rb");
|
|
if (!file_in) {
|
|
fprintf(stderr, "Failed to open input file %s\n", file_to_copy);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
FIL file_out;
|
|
if (f_open(&file_out, dest_filename, FA_WRITE | FA_CREATE_ALWAYS) != FR_OK) {
|
|
fprintf(stderr, "Failed to open or create destination file on FAT filesystem\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
char buf[100];
|
|
size_t total = 0;
|
|
while (!feof(file_in)) {
|
|
UINT numw = 0;
|
|
size_t numr = fread(buf, 1, sizeof(buf), file_in);
|
|
if (ferror(file_in)) {
|
|
fprintf(stderr, "Error reading input file\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
if (f_write(&file_out, buf, numr, &numw) != FR_OK || numw != numr) {
|
|
fprintf(stderr, "Error writing to output file\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
total += numr;
|
|
}
|
|
fclose(file_in);
|
|
f_close(&file_out);
|
|
|
|
printf("Copied %lu bytes into %s\n", (unsigned long) total, dest_filename);
|
|
}
|
|
|
|
int main (int argc, char** argv)
|
|
{
|
|
if (argc != 4)
|
|
print_usage();
|
|
copy_file(argv[1], argv[2], argv[3]);
|
|
exit(EXIT_SUCCESS);
|
|
}
|