-
Notifications
You must be signed in to change notification settings - Fork 0
/
unandmgr.c
136 lines (107 loc) · 3.43 KB
/
unandmgr.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
//options --8G --mode=read/write (in file) (out file)
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#define BUF_SIZE 512
#define PROGRESS_BAR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
void progress_bar(char *current, char *total, double percent);
void read_to_file();
void write_to_drive();
char * arg_in_file;
char * arg_out_file;
size_t nand_sectors;
int main(int argc, char *argv[])
{
//check for arguments
if(argc == 5){
arg_in_file = argv[3];
arg_out_file = argv[4];
if(strstr(argv[1], "8G")){
nand_sectors = 0x1090500;
}else{
nand_sectors = 0x3D70500;
}
if(strstr(argv[2], "read")){
read_to_file();
}else{
write_to_drive();
}
}
else {
printf("Four arguments expected.\n");
exit(0);
}
return 0;
}
void progress_bar(char *current, char *total, double percent){
int val = (int) (percent * 100);
int lpad = (int) (percent * 60);
int rpad = 60 - lpad;
printf ("\r%s / %s%3d%% [%.*s%*s]", current, total, val, lpad, PROGRESS_BAR, rpad, "");
fflush (stdout);
}
char* readable_fs(size_t size, char *buf)
{
int i = 0;
const char* units[] = {"B", "KB", "MB", "GB"};
while (size > 1024) {
size /= 1024;
i++;
}
sprintf(buf, "%.*zu %s", i, size, units[i]);
return buf;
}
void read_to_file()
{
ssize_t in_file_fd, out_file_fd; //file descriptors
char buffer[BUF_SIZE], buffer_2[20], buffer_3[20]; //initialize buffers and file path
printf("Now reading from %s to %s \n",arg_in_file, arg_out_file);
//input file descriptor
in_file_fd = open(arg_in_file, O_RDONLY);
if (in_file_fd == -1){
printf("Error reading drive, please run with sudo");
}
//output file descriptor
out_file_fd = open(arg_out_file, O_WRONLY | O_CREAT);
if(out_file_fd == -1){
printf("Error creating file");
}
//seek past mbr
lseek(in_file_fd, 512, SEEK_SET);
for(double i = 0; i < nand_sectors; i ++){
write(out_file_fd, &buffer, read(in_file_fd, &buffer, BUF_SIZE));
progress_bar(readable_fs(i * 512, buffer_2), readable_fs(nand_sectors * 512, buffer_3), round((i / (double) nand_sectors)*100)/100);
}
//close file descrptors
close(in_file_fd);
close(out_file_fd);
}
void write_to_drive()
{
ssize_t in_file_fd, out_file_fd; //file descriptors
char buffer[BUF_SIZE], buffer_2[20], buffer_3[20]; //initialize buffers and file path
printf("Now writing image from %s to %s \n",arg_in_file, arg_out_file);
//input file descriptor
in_file_fd = open(arg_in_file, O_RDONLY);
if (in_file_fd == -1){
printf("Error reading image, does it exist ?");
}
out_file_fd = open(arg_out_file, O_WRONLY | O_CREAT, 0666);
if(out_file_fd == -1){
printf("Error reading drive, please run with sudo");
}
//seek past mbr
lseek(out_file_fd, 512, SEEK_SET);
for(double i; i < nand_sectors; i ++){
write(out_file_fd, &buffer, read(in_file_fd, &buffer, BUF_SIZE));
progress_bar(readable_fs(i * 512, buffer_2), readable_fs(nand_sectors * 512, buffer_3), round((i / (double) nand_sectors)*100)/100);
}
//close file descriptors
close(in_file_fd);
close(out_file_fd);
}