-
Notifications
You must be signed in to change notification settings - Fork 16
/
flash_ubi_jffs2.c
248 lines (217 loc) · 5.53 KB
/
flash_ubi_jffs2.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
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
#include "ofgwrite.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <getopt.h>
#include <libubi.h>
#include <syslog.h>
#include <unistd.h>
#include <libmtd.h>
#include <errno.h>
#include <mtd/mtd-abi.h>
int getFlashType(char* device)
{
libmtd_t libmtd = libmtd_open();
if (libmtd == NULL)
{
if (errno == 0)
my_printf("MTD is not present in the system");
my_printf("cannot open libmtd %s", strerror(errno));
return -1;
}
struct mtd_dev_info mtd;
int err = mtd_get_dev_info(libmtd, device, &mtd);
if (err)
{
my_fprintf(stderr, "cannot get information about \"%s\"\n", device);
return -1;
}
libmtd_close(libmtd);
return mtd.type;
}
int flash_erase(char* device, char* context, int quiet, int no_write)
{
optind = 0; // reset getopt_long
char* argv[] = {
"flash_erase", // program name
device, // device
"0", // start offset
"0", // block count
NULL
};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
if (!quiet)
my_printf("Erasing %s: flash_erase %s 0 0\n", context, device);
if (!no_write)
if (flash_erase_main(argc, argv) != 0)
return 0;
return 1;
}
int flash_erase_jffs2(char* device, char* context, int quiet, int no_write)
{
optind = 0; // reset getopt_long
char* argv[] = {
"flash_erase", // program name
"-j", // format the device for jffs2
device, // device
"0", // start offset
"0", // block count
NULL
};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
if (!quiet)
my_printf("Erasing %s: flash_erase -j %s 0 0\n", context, device);
if (!no_write)
if (flash_erase_main(argc, argv) != 0)
return 0;
return 1;
}
int flash_write(char* device, char* filename, int quiet, int no_write)
{
optind = 0; // reset getopt_long
char opts[4];
strcpy(opts, "-pm");
char* argv[] = {
"nandwrite", // program name
opts, // options -p for pad and -m for mark bad blocks
device, // device
filename, // file to flash
NULL
};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
if (!quiet)
my_printf("Flashing kernel: nandwrite %s %s %s\n", opts, device, filename);
if (!no_write)
if (nandwrite_main(argc, argv) != 0)
return 0;
return 1;
}
int ubi_write(char* device, char* filename, int quiet, int no_write)
{
optind = 0; // reset getopt_long
char* argv[] = {
"ubiformat", // program name
device, // device
"-f", // flash file
filename, // file to flash
"-D", // no detach check
NULL
};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
my_printf("Flashing rootfs: ubiformat %s -f %s\n", device, filename);
if (!no_write)
if (ubiformat_main(argc, argv) != 0)
return 0;
return 1;
}
int ubi_detach_dev(char* device, int quiet, int no_write)
{
optind = 0; // reset getopt_long
char* argv[] = {
"ubidetach", // program name
"-p", // path to device
device, // device
NULL
};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
my_printf("Detach rootfs: ubidetach -p %s\n", device);
if (!no_write)
if (ubidetach_main(argc, argv) != 0)
return 0;
return 1;
}
int flashcp(char* device, char* filename, int reboot, int quiet, int no_write)
{
optind = 0; // reset getopt_long
char opts[4];
if (reboot)
strcpy(opts, "-vr\0");
else
strcpy(opts, "-v\0");
char* argv[] = {
"flashcp", // program name
opts, // options -v verbose -r reboot immediately after flashing
filename, // file to flash
device, // device
NULL
};
int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;
my_printf("Flashing rootfs: flashcp %s %s %s\n", opts, filename, device);
if (!no_write)
if (flashcp_main(argc, argv) != 0)
return 0;
return 1;
}
int flash_ubi_jffs2_kernel(char* device, char* filename, int quiet, int no_write)
{
int type = getFlashType(device);
if (type == -1)
return 0;
if (type == MTD_NANDFLASH || type == MTD_MLCNANDFLASH)
{
my_printf("Found NAND flash\n");
// Erase
set_step("Erasing kernel");
if (!flash_erase(device, "kernel", quiet, no_write))
{
my_printf("Error erasing kernel! System might not boot. If you have problems please flash backup!\n");
return 0;
}
// Flash
set_step("Writing kernel");
if (!flash_write(device, filename, quiet, no_write))
{
my_printf("Error flashing kernel! System won't boot. Please flash backup!\n");
return 0;
}
}
else if (type == MTD_NORFLASH)
{
my_printf("Found NOR flash\n");
if (!flashcp(device, filename, 0, quiet, no_write))
{
my_printf("Error flashing kernel! System won't boot. Please flash backup!\n");
return 0;
}
}
else
{
my_fprintf(stderr, "Flash type \"%d\" not supported\n", type);
return 0;
}
return 1;
}
int flash_ubi_jffs2_rootfs(char* device, char* filename, enum RootfsTypeEnum rootfs_type, int quiet, int no_write)
{
int type = getFlashType(device);
if (type == -1)
return 0;
if ((type == MTD_NANDFLASH || type == MTD_MLCNANDFLASH) && rootfs_type == UBIFS)
{
my_printf("Found NAND flash\n");
if (!ubi_write(device, filename, quiet, no_write))
return 0;
}
else if ((type == MTD_NANDFLASH || type == MTD_MLCNANDFLASH) && rootfs_type == JFFS2)
{
my_printf("Found NAND flash\n");
if (!flash_erase_jffs2(device, "rootfs", quiet, no_write))
return 0;
if (!flash_write(device, filename, quiet, no_write))
return 0;
}
else if (type == MTD_NORFLASH && rootfs_type == JFFS2)
{
my_printf("Found NOR flash\n");
if (!flashcp(device, filename, 1, quiet, no_write))
return 0;
}
else
{
my_fprintf(stderr, "Flash type \"%d\" in combination with rootfs type %d is not supported\n", type, rootfs_type);
return 0;
}
return 1;
}