-
Notifications
You must be signed in to change notification settings - Fork 16
/
flash_ext4.c
149 lines (133 loc) · 3.29 KB
/
flash_ext4.c
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
#include "ofgwrite.h"
#include <stdio.h>
#include <getopt.h>
int flash_ext4_kernel(char* device, char* filename, off_t kernel_file_size, int quiet, int no_write)
{
char buffer[512];
// Open kernel file
FILE* kernel_file;
kernel_file = fopen(filename, "rb");
if (kernel_file == NULL)
{
my_printf("Error while opening kernel file %s\n", filename);
return 0;
}
// Open kernel device
FILE* kernel_dev;
kernel_dev = fopen(device, "wb");
if (kernel_dev == NULL)
{
my_printf("Error while opening kernel device %s\n", device);
return 0;
}
set_step("Writing ext4 kernel");
int ret;
long long readBytes = 0;
int current_percent = 0;
int new_percent = 0;
while (!feof(kernel_file))
{
// Don't add my_printf for debugging! Debug messages will be written to kernel device!
ret = fread(buffer, 1, sizeof(buffer), kernel_file);
if (ret == 0)
{
if (feof(kernel_file))
continue;
my_printf("Error reading kernel file.\n");
fclose(kernel_file);
fclose(kernel_dev);
return 0;
}
readBytes += ret;
new_percent = readBytes * 100/ kernel_file_size;
if (current_percent < new_percent)
{
set_step_progress(new_percent);
current_percent = new_percent;
}
if (!no_write)
{
ret = fwrite(buffer, ret, 1, kernel_dev);
if (ret != 1)
{
my_printf("Error writing kernel file to kernel device.\n");
fclose(kernel_file);
fclose(kernel_dev);
return 0;
}
}
}
fclose(kernel_file);
fclose(kernel_dev);
return 1;
}
int rm_rootfs(char* directory, int quiet, int no_write)
{
optind = 0; // reset getopt_long
char* argv[] = {
"rm", // program name
"-r", // recursive
"-f", // force
directory, // directory
NULL
};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
if (!quiet)
my_printf("Delete rootfs: rm -r -f %s\n", directory);
if (!no_write)
if (rm_main(argc, argv) != 0)
return 0;
return 1;
}
int untar_rootfs(char* filename, char* directory, int quiet, int no_write)
{
optind = 0; // reset getopt_long
char* argv[] = {
"tar", // program name
"-x", // extract
"-f",
filename, // file
"-C",
directory, // untar to directory
NULL
};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
if (!quiet)
my_printf("Untar: tar xf %s\n", filename);
if (!no_write)
if (tar_main(argc, argv) != 0)
return 0;
return 1;
}
int flash_unpack_rootfs(char* filename, int quiet, int no_write)
{
int ret;
char path[1000];
// instead of creating new filesystem just delete whole content
set_step("Deleting rootfs");
strcpy(path, "/oldroot_remount/");
if (current_rootfs_sub_dir[0] != '\0' && rootsubdir_check == 0) // box with rootSubDir feature
{
strcat(path, rootfs_sub_dir);
strcat(path, "/");
}
if (!no_write)
{
ret = rm_rootfs(path, quiet, no_write); // ignore return value as it always fails, because oldroot_remount cannot be removed
}
set_step("Extracting rootfs");
set_step_progress(0);
if (!no_write && current_rootfs_sub_dir[0] != '\0' && rootsubdir_check == 0) // box with rootSubDir feature
mkdir(path, 777); // directory is maybe not present
if (!untar_rootfs(filename, path, quiet, no_write))
{
my_printf("Error extracting rootfs\n");
return 0;
}
// sync filesystem double because of sdcard
sync();
sync();
sleep(1);
ret = chdir("/"); // needed to be able to umount filesystem
return 1;
}