~aleteoryx/muditaos

ref: master muditaos/host-tools/genlittlefs/mklfs.c -rw-r--r-- 12.0 KiB
2cd0e472 — Lefucjusz [BH-000] Update Harmony 2.10.0 changelog 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <lfs.h>
#include "lfs_ioaccess.h"
#include "parse_partitions.h"
#include "parse_args.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/stat.h>

struct lfs_info_summary
{
    size_t files_added;
    size_t directories_added;
    size_t bytes_transferred;
};

static int create_dir_in_lfs(lfs_t *lfs, const char *lfs_path, bool verbose)
{
    int ret;
    if (verbose)
        fprintf(stdout, "[%s]\n", lfs_path);
    if ((ret = lfs_mkdir(lfs, lfs_path)) < 0) {
        fprintf(stderr, "can't create directory %s: error=%d\n", lfs_path, ret);
        return ret;
    }
    return 0;
}

static int create_file_in_lfs(lfs_t *lfs, const char *host_file, const char *lfs_file, bool verbose)
{
    int ret;
    if (verbose)
        fprintf(stdout, "%s\n", lfs_file);
    // Open source file
    int srcfd = open(host_file, O_RDONLY);
    if (srcfd < 0) {
        fprintf(stderr, "can't open source file %s: errno=%d (%s)\n", host_file, errno, strerror(errno));
        return -1;
    }

    // Open destination file
    lfs_file_t dstf;
    if ((ret = lfs_file_open(lfs, &dstf, lfs_file, LFS_O_WRONLY | LFS_O_CREAT)) < 0) {
        fprintf(stderr, "can't open destination file %s: error=%d\n", lfs_file, ret);
        close(srcfd);
        return ret;
    }
    do {
        char copy_buffer[16384];
        ret = read(srcfd, copy_buffer, sizeof copy_buffer);
        if (ret < 0) {
            close(srcfd);
            lfs_file_close(lfs, &dstf);
            return ret;
        }
        else if (ret == 0) {
            break;
        }
        char *lfs_wptr = copy_buffer;
        int lfs_wrleft = ret;
        do {
            int retlfs = lfs_file_write(lfs, &dstf, lfs_wptr, lfs_wrleft);
            if (retlfs <= 0) {
                close(srcfd);
                lfs_file_close(lfs, &dstf);
                fprintf(stderr, "can't write to destination file %s: error=%d\n", lfs_file, retlfs);
                return retlfs;
            }
            else {
                lfs_wrleft -= retlfs;
                lfs_wptr += retlfs;
            }
        } while (lfs_wrleft > 0);

    } while (ret > 0);

    // Close destination file
    ret = lfs_file_close(lfs, &dstf);
    if (ret < 0) {
        fprintf(stderr, "can't close destination file %s: error=%d\n", lfs_file, ret);
        close(srcfd);
        return ret;
    }

    // Close source file
    close(srcfd);
    return ret;
}

static int add_directory_to_lfs(
    lfs_t *lfs, const char *host_path, const char *lfs_path, struct lfs_info_summary *summary, bool verbose)
{
    DIR *dir;
    struct dirent *ent;
    char lfs_curr_path[PATH_MAX];
    char host_curr_path[PATH_MAX];
    int err = 0;
    dir     = opendir(host_path);
    if (dir) {
        while ((ent = readdir(dir))) {
            // Skip . and .. directories
            if ((strcmp(ent->d_name, ".") != 0) && (strcmp(ent->d_name, "..") != 0)) {
                // Update the current path
                strcpy(lfs_curr_path, lfs_path);
                strcat(lfs_curr_path, "/");
                strcat(lfs_curr_path, ent->d_name);
                // Update native current path
                strcpy(host_curr_path, host_path);
                strcat(host_curr_path, "/");
                strcat(host_curr_path, ent->d_name);

                if (ent->d_type == DT_DIR) {
                    err = create_dir_in_lfs(lfs, lfs_curr_path, verbose);
                    if (err) {
                        closedir(dir);
                        return err;
                    }
                    else {
                        summary->directories_added++;
                    }
                    err = add_directory_to_lfs(lfs, host_curr_path, lfs_curr_path, summary, verbose);
                    if (err) {
                        closedir(dir);
                        return err;
                    }
                }
                else if (ent->d_type == DT_REG) {
                    err = create_file_in_lfs(lfs, host_curr_path, lfs_curr_path, verbose);
                    if (err) {
                        closedir(dir);
                        return err;
                    }
                    else {
                        summary->files_added++;
                        struct stat statbuf;
                        if (stat(host_curr_path, &statbuf) == 0) {
                            summary->bytes_transferred += statbuf.st_size;
                        }
                    }
                }
            }
        }
        closedir(dir);
    }
    return err;
}

static int add_to_lfs(lfs_t *lfs, const char *dir, struct lfs_info_summary *summary, bool verbose)
{
    char *host_dir = canonicalize_file_name(dir);
    bool is_dir, is_file;
    off_t fsize;
    {
        struct stat stbuf;
        int err = stat(host_dir, &stbuf);
        if (err < 0) {
            free(host_dir);
            return -1;
        }
        fsize   = stbuf.st_size;
        is_dir  = stbuf.st_mode & S_IFDIR;
        is_file = stbuf.st_mode & S_IFREG;
    }
    if (!is_dir && !is_file) {
        free(host_dir);
        errno = ENOTDIR;
        return -1;
    }
    char *sep_ptr = strrchr(host_dir, '/');
    char *tgt_dir = malloc(strlen(sep_ptr + 1) + sizeof('\0') + sizeof('/'));
    if (!tgt_dir) {
        free(host_dir);
        errno = ENOMEM;
        return -1;
    }
    else {
        tgt_dir[0] = '/';
        strcpy(tgt_dir + 1, sep_ptr + 1);
    }
    int err = 0;
    if (is_dir) {
        err = create_dir_in_lfs(lfs, tgt_dir, verbose);
        if (err) {
            free(host_dir);
            free(tgt_dir);
            return err;
        }
        err = add_directory_to_lfs(lfs, host_dir, tgt_dir, summary, verbose);
        if (!err) {
            summary->directories_added++;
        }
    }
    else { // is_file
        err = create_file_in_lfs(lfs, host_dir, tgt_dir, verbose);
        if (!err) {
            summary->files_added++;
            summary->bytes_transferred += fsize;
        }
    }
    free(host_dir);
    free(tgt_dir);
    return err;
}

static void print_error(int error, const char *format, ...) __attribute__((nonnull(2)));
static void print_error(int error, const char *format, ...)
{
    va_list arglist;
    va_start(arglist, format);
    vfprintf(stderr, format, arglist);
    va_end(arglist);

    if (error == -1) {
        char buf[1024];
        fprintf(stderr, " system_error: %s\n", strerror_r(errno, buf, sizeof buf));
    }
    else {
        fprintf(stderr, " lfs_error: %i\n", error);
    }
}

static void configure_lfs_params(struct lfs_config *lfsc, const struct littlefs_opts *opts)
    __attribute__((nonnull(1, 2)));

static void configure_lfs_params(struct lfs_config *lfsc, const struct littlefs_opts *opts)
{
    memset(lfsc, 0, sizeof *lfsc);
    lfsc->block_size     = opts->block_size;
    lfsc->read_size      = opts->read_size;
    lfsc->prog_size      = opts->prog_size;
    lfsc->lookahead_size = opts->lockahead_size;
    lfsc->cache_size     = opts->cache_size;
    lfsc->block_cycles   = opts->block_cycles;
}

int main(int argc, char **argv)
{

    int err = 0;
    struct littlefs_opts lopts;
    struct lfs_config cfg;
    struct lfs_info_summary prog_summary;
    struct lfs_ioaccess_context *ioctx = NULL;
    lfs_t lfs;
    err = parse_program_args(argc, argv, &lopts);
    if (err < 0) {
        return err;
    }
    if (lopts.mode == littlefs_opts_listparts) {
        size_t elems;
        struct partition *parts = find_partitions(lopts.dst_image, scan_all_partitions, &elems);
        print_partitions(parts, elems);
        free(parts);
        free(lopts.src_dirs);
        return EXIT_SUCCESS;
    }

    configure_lfs_params(&cfg, &lopts);
    memset(&prog_summary, 0, sizeof(prog_summary));
    if (lopts.mode == littlefs_opts_parts) {
        size_t elems;
        struct partition *parts = find_partitions(lopts.dst_image, scan_all_partitions, &elems);
        if (!parts) {
            perror("Unable to list partitions:");
            free(lopts.src_dirs);
            return EXIT_FAILURE;
        }
        if (lopts.partition_num - 1 > (int)elems) {
            fprintf(stderr, "Invalid partition selected. Max partition_num is: %lu\n", elems);
            free(parts);
            free(lopts.src_dirs);
            return EXIT_FAILURE;
        }
        const struct partition *curr_part = &parts[lopts.partition_num - 1];
        cfg.block_count                   = (curr_part->end - curr_part->start) / lopts.block_size;
        ioctx                             = lfs_ioaccess_open(&cfg, lopts.dst_image, curr_part);
        if (!ioctx) {
            fprintf(stderr, "Unable to open file: %s error %s\n", lopts.dst_image, strerror(errno));
            free(parts);
            free(lopts.src_dirs);
            return EXIT_FAILURE;
        }
        free(parts);
        if (write_partition_bootunit(lopts.dst_image, lopts.partition_num, lopts.block_size)) {
            perror("Unable to write bootunit");
            free(lopts.src_dirs);
            lfs_ioaccess_close(ioctx);
            return EXIT_FAILURE;
        }
    }
    else if (lopts.mode == littlefs_opts_file) {
        int fds = open(lopts.dst_image, O_CREAT | O_WRONLY, 0644);
        if (fds < 0) {
            perror("Unable to create file");
            free(lopts.src_dirs);
            return EXIT_FAILURE;
        }
        err = ftruncate(fds, lopts.filesystem_size);
        if (err) {
            perror("Unable to truncate file");
            close(fds);
            free(lopts.src_dirs);
            return EXIT_FAILURE;
        }
        close(fds);
        fds             = -1;
        cfg.block_count = lopts.filesystem_size / lopts.block_size;
        ioctx           = lfs_ioaccess_open(&cfg, lopts.dst_image, NULL);
        if (!ioctx) {
            perror("Unable to open file:");
            free(lopts.src_dirs);
            return EXIT_FAILURE;
        }
    }
    else {
        fprintf(stderr, "Unknown option\n");
        free(lopts.src_dirs);
        lfs_ioaccess_close(ioctx);
        return EXIT_FAILURE;
    }
    if (lopts.verbose) {
        print_config_options(&lopts);
    }

    if (!lopts.overwrite_existing && lfs_ioaccess_is_lfs_filesystem(ioctx) > 0) {
        fprintf(stderr, "LFS filesystem already exists. If you want to overwrite add --overwrite flag\n");
        free(lopts.src_dirs);
        lfs_ioaccess_close(ioctx);
        return EXIT_FAILURE;
    }

    err = lfs_format(&lfs, &cfg);
    if (err < 0) {
        fprintf(stderr, "lfs format error: error=%d\n", err);
        lfs_ioaccess_close(ioctx);
        free(lopts.src_dirs);
        return EXIT_FAILURE;
    }

    err = lfs_mount(&lfs, &cfg);
    if (err < 0) {
        fprintf(stderr, "lfs mount error: error=%d\n", err);
        lfs_ioaccess_close(ioctx);
        free(lopts.src_dirs);
        return EXIT_FAILURE;
    }

    for (size_t ndir = 0; ndir < lopts.src_dirs_siz; ++ndir) {
        err = add_to_lfs(&lfs, lopts.src_dirs[ndir], &prog_summary, lopts.verbose);
        if (err) {
            print_error(err, "Unable to open file: %s", lopts.src_dirs[ndir]);
            lfs_ioaccess_close(ioctx);
            free(lopts.src_dirs);
            lfs_unmount(&lfs);
            return EXIT_FAILURE;
        }
    }
    const lfs_ssize_t used_blocks = lfs_fs_size(&lfs);
    err                           = lfs_unmount(&lfs);
    if (err < 0) {
        fprintf(stderr, "lfs umount error: error=%d\n", err);
        lfs_ioaccess_close(ioctx);
        free(lopts.src_dirs);
        return EXIT_FAILURE;
    }
    free(lopts.src_dirs);
    lfs_ioaccess_close(ioctx);
    printf("Littlefs summary:\n"
           "     Directories created: %lu, Files added: %lu, Transferred %lu kbytes.\n"
           "     Littlefs block size: %i blocks: %i/%i.\n",
           prog_summary.directories_added,
           prog_summary.files_added,
           prog_summary.bytes_transferred / 1024UL,
           cfg.block_size,
           used_blocks,
           cfg.block_count);
    return EXIT_SUCCESS;
}