hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
#define _GNU_SOURCE
 
#include <dirent.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <errno.h>
#include "mtdutils.h"
#include "rk29.h"
 
int run(const char *filename, char *const argv[])
{
    struct stat s;
    int status;
    pid_t pid;
 
    if (stat(filename, &s) != 0) {
        fprintf(stderr, "cannot find '%s'", filename);
        return -1;
    }
 
    printf("executing '%s'\n", filename);
 
    pid = fork();
 
    if (pid == 0) {
        setpgid(0, getpid());
        /* execute */
        execv(filename, argv);
        fprintf(stderr, "can't run %s (%s)\n", filename, strerror(errno));
        /* exit */
        _exit(0);
    }
 
    if (pid < 0) {
        fprintf(stderr, "failed to fork and start '%s'\n", filename);
        return -1;
    }
 
    if (-1 == waitpid(pid, &status, WCONTINUED | WUNTRACED)) {
        fprintf(stderr, "wait for child error\n");
        return -1;
    }
 
    if (WIFEXITED(status)) {
        printf("executed '%s' done\n", filename);
    }
 
    printf("executed '%s' return %d\n", filename, WEXITSTATUS(status));
    return 0;
}
 
int rk_check_and_resizefs(const char *filename) {
    int result;
 
    const char *const e2fsck_argv[] = { "/sbin/e2fsck", "-fy", filename, NULL };
    const char *const resizefs_argv[] = { "/sbin/resize2fs", filename, NULL  };
 
    result = run(e2fsck_argv[0], (char **) e2fsck_argv);
    if(result) {
        printf("e2fsck check '%s' failed!\n", filename);
        return result;
    }
 
    result = run(resizefs_argv[0], (char **) resizefs_argv);
    if(result) {
        printf("resizefs '%s' failed!\n", filename);
    }
 
    return result;
}
 
int rk_check_and_resizefs_f2fs(const char *filename) {
   int result;
 
   const char *const e2fsck_argv[] = { "fsck_f2fs", filename, NULL };
   const char *const resizefs_argv[] = { "resize.f2fs", filename, NULL  };
 
   result = run(e2fsck_argv[0], (char **) e2fsck_argv);
   if(result) {
       printf("fsck_f2fs check '%s' failed!\n", filename);
       return result;
   }
 
   result = run(resizefs_argv[0], (char **) resizefs_argv);
   if(result) {
       printf("resize.f2fs '%s' failed!\n", filename);
   }
 
   return result;
}
 
static int make_extfs(const char *path, const char *label, const char *type)
{
    const char *const mke2fs[] = {
        "/sbin/mke2fs", "-t", type, "-q", path, NULL,
    };
 
    // max-mount-counts(0) + time-dependent checking(0) + fslabel
    const char *const tune2fs[] = {
        "/sbin/tune2fs", "-c", "0", "-i", "0", "-L", label, path,
    };
    int result;
 
    printf("format '%s' to %s filesystem\n", path, type);
    result = run(mke2fs[0], (char **) mke2fs);
    if(result) {
        printf("failed!\n");
        return result;
    }
 
    result = run(tune2fs[0], (char **) tune2fs);
    if(result) {
        printf("failed!\n");
        return result;
    }
 
    return result;
}
 
int make_ext2(const char *path, const char *label)
{
    return make_extfs(path, label, "ext2");
}
 
int make_ext4(const char *path, const char *label)
{
    return make_extfs(path, label, "ext4");
}
 
int make_vfat(const char *path, const char *label)
{
    // fat32
    const char *const mkdosfs[] = {
        "/sbin/mkdosfs", "-F", "32", "-n", label, path, NULL,
    };
 
    printf("format '%s' to vfat filesystem\n", path);
    return run(mkdosfs[0], (char **) mkdosfs);
}
 
int make_ntfs(const char *path, const char *label)
{
    // compression
    const char *const mkntfs[] = {
        "mkntfs", "-F", "C", "Q", "-L", label, path, NULL,
    };
 
    printf("format '%s' to ntfs filesystem\n", path);
    return run(mkntfs[0], (char **) mkntfs);
}
 
#ifndef min
#define min(a,b) ((a)<(b)?(a):(b))
#endif
 
size_t rk29_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    char buf[READ_SIZE];
    int fd;
    long begin, end;
    off_t offset;
    ssize_t sz;
    size_t count = 0, total;
    char *p = ptr;
 
    if (!ptr)
        return 0;
    if (!size || !nmemb)
        return 0;
    if (!stream)
        return 0;
    fd = fileno(stream);
    if (fd < 0)
        return 0;
 
    begin = ftell(stream);
    if (begin < 0)
        begin = 0;
 
    total = size * nmemb;
    if (!total)
        return 0;
 
    end = begin + total;
    offset = begin & ~READ_MASK;
 
    if (begin & READ_MASK) {
        sz = pread(fd, buf, READ_SIZE, offset);
        if (sz < READ_SIZE)
            goto out;
        count = min(end, offset + READ_SIZE) - begin;
        memcpy(p, buf + (begin & READ_MASK), count);
        p += count;
        offset += READ_SIZE;
    }
 
    for (; offset < (end & ~READ_MASK); offset += READ_SIZE) {
        sz = pread(fd, buf, READ_SIZE, offset);
        if (sz < READ_SIZE)
            goto out;
        count += READ_SIZE;
        memcpy(p, buf, READ_SIZE);
        p += READ_SIZE;
    }
 
    if (count < total && (end & READ_MASK)) {
        offset = end & ~READ_MASK;
        sz = pread(fd, buf, READ_SIZE, offset);
        if (sz < READ_SIZE)
            goto out;
        memcpy(p, buf, end - offset);
        count += end - offset;
    }
out:
    count /= size;
    fseek(stream, begin + count * size, SEEK_SET);
    return count;
}
 
size_t rk29_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
    char buf[WRITE_SIZE];
    int fd;
    long begin, end;
    off_t offset;
    ssize_t sz;
    size_t count = 0, total;
    char *p = (char *)ptr;
 
    if (!ptr)
        return 0;
    if (!size || !nmemb)
        return 0;
    if (!stream)
        return 0;
    fd = fileno(stream);
    if (fd < 0)
        return 0;
 
    begin = ftell(stream);
    if (begin < 0)
        begin = 0;
 
    total = size * nmemb;
    if (!total)
        return 0;
 
    end = begin + total;
    offset = begin & ~WRITE_MASK;
 
    if (begin & WRITE_MASK) {
        sz = pread(fd, buf, WRITE_SIZE, offset);
        if (sz < WRITE_SIZE)
            goto out;
        count = min(end, offset + WRITE_SIZE) - begin;
        memcpy(buf + (begin & WRITE_MASK), p, count);
        sz = pwrite(fd, buf, WRITE_SIZE, offset);
        if (sz < WRITE_SIZE)
            goto out;
        p += count;
        offset += WRITE_SIZE;
    }
 
    for (; offset < (end & ~WRITE_MASK); offset += WRITE_SIZE) {
        sz = pwrite(fd, p, WRITE_SIZE, offset);
        if (sz < WRITE_SIZE)
            goto out;
        count += WRITE_SIZE;
        p += WRITE_SIZE;
    }
 
    if (count < total && (end & WRITE_MASK)) {
        offset = end & ~WRITE_MASK;
        sz = pread(fd, buf, WRITE_SIZE, offset);
        if (sz < WRITE_SIZE)
            goto out;
        memcpy(buf, p, end - offset);
        sz = pwrite(fd, buf, WRITE_SIZE, offset);
        if (sz < WRITE_SIZE)
            goto out;
        count += end - offset;
    }
out:
    count /= size;
    fseek(stream, begin + count * size, SEEK_SET);
    return count;
}