-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash_ext4.c
210 lines (183 loc) · 4.73 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
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
#include "ofgwrite.h"
#include <stdio.h>
#include <getopt.h>
//NI
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.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);
//NI
char backup_file[64] = "";
if (access("/backup_flash.tar.gz", F_OK) == 0)
strcpy(backup_file, "/backup_flash.tar.gz");
else if (access("/newroot/backup_flash.tar.gz", F_OK) == 0)
strcpy(backup_file, "/newroot/backup_flash.tar.gz");
if (backup_file[0] != '\0')
{
my_printf("Found backup in %s\n", backup_file);
set_step("Copying backup to rootfs");
strcat(path, "/var");
mkdir(path, 777); //needed?
strcat(path, "/backup_flash.tar.gz");
my_printf("Copy path: %s\n", path);
ret = copy_file(backup_file, path);
if (ret != 0)
my_printf("Error copying backup_flash.tar.gz\n");
}
sync();
ret = chdir("/"); // needed to be able to umount filesystem
return 1;
}
#define BUF_SIZE 1024
copy_file(char *from, char *to)
{
int inputFd, outputFd, openFlags;
mode_t filePerms;
ssize_t numRead;
char buf[BUF_SIZE];
inputFd = open(from, O_RDONLY);
if (inputFd == -1)
return -1;
openFlags = O_CREAT | O_WRONLY | O_TRUNC;
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; // rw-rw-rw-
outputFd = open(to, openFlags, filePerms);
if (outputFd == -1)
return -1;
while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
if (write(outputFd, buf, numRead) != numRead)
my_printf("Error: Couldn't write whole buffer");
if (numRead == -1)
my_printf("Error: read");
if (close(inputFd) == -1)
my_printf("Error: close input");
if (close(outputFd) == -1)
my_printf("Error: close output");
return 0;
}