-
Notifications
You must be signed in to change notification settings - Fork 4
/
disk.c
321 lines (247 loc) · 8.39 KB
/
disk.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <inttypes.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <linux/fs.h> /* BLKGETSIZE64 */
#include "util.h"
#include "disk.h"
extern json_object *json_root;
unsigned disk_list_size;
disk_t *disk_list;
int disk_read(disk_t *disk, void *buffer, uint64_t block_nr, unsigned count)
{
unsigned factor = disk->block_size / disk->chunk_size;
// fprintf(stderr, "read: %llu - %u (factor = %u)\n", (unsigned long long) block_nr, count, factor);
count *= factor;
block_nr *= factor;
for(unsigned u = 0; u < count; u++, block_nr++, buffer += disk->chunk_size) {
// fprintf(stderr, "read request: disk %u, addr %08"PRIx64"\n", disk->index, block_nr * disk->chunk_size);
if(!disk_cache_read(disk, buffer, block_nr)) {
int err = disk_read_single(disk, buffer, block_nr);
if(err) return err;
disk_cache_store(disk, buffer, block_nr);
}
}
return 0;
}
int disk_read_single(disk_t *disk, void *buffer, uint64_t block_nr)
{
if(disk->fd == -1) {
// fprintf(stderr, "cache miss: disk %u, addr %08"PRIx64"\n", disk->index, block_nr * disk->chunk_size);
memset(buffer, 0, disk->chunk_size);
return 0;
}
// fprintf(stderr, "read: %llu[%llu]\n", (unsigned long long) block_nr, (unsigned long long) disk->size);
off_t offset = block_nr * disk->chunk_size;
if(lseek(disk->fd, offset, SEEK_SET) != offset) {
fprintf(stderr, "sector %"PRIu64" not found\n", block_nr);
return 2;
}
if(read(disk->fd, buffer, disk->chunk_size) != disk->chunk_size) {
fprintf(stderr, "error reading sector %"PRIu64"\n", block_nr);
return 3;
}
return 0;
}
int disk_cache_read(disk_t *disk, void *buffer, uint64_t chunk_nr)
{
for(disk_data_t *disk_data = disk->data; disk_data; disk_data = disk_data->next) {
if(disk_data->chunk_nr == chunk_nr) {
memcpy(buffer, disk_data->data, disk->chunk_size);
return 1;
}
}
return 0;
}
void disk_cache_store(disk_t *disk, void *buffer, uint64_t chunk_nr)
{
// fprintf(stderr, "cache store: disk %u, addr %08"PRIx64"\n", disk->index, chunk_nr * disk->chunk_size);
disk_data_t *disk_data = calloc(1, sizeof *disk_data);
disk_data->chunk_nr = chunk_nr;
disk_data->data = malloc(disk->chunk_size);
memcpy(disk_data->data, buffer, disk->chunk_size);
disk_data->next = disk->data;
disk->data = disk_data;
}
int disk_export(disk_t *disk, char *file_name)
{
FILE *f = stdout;
if(strcmp(file_name, "-")) {
f = fopen(file_name, "a");
if(!f) {
perror(file_name);
return 1;
}
}
fprintf(f, "# disk %u, size = %"PRIu64"\n", disk->index, disk->size_in_bytes);
uint64_t chunk_nr = 0;
disk_data_t *disk_data;
do {
disk_data = disk_cache_search(disk, &chunk_nr);
if(disk_data) {
disk_cache_dump(disk, disk_data, f);
}
}
while(chunk_nr != UINT64_MAX);
if(f != stdout) fclose(f);
return 0;
}
int disk_to_fd(disk_t *disk, uint64_t offset)
{
uint64_t chunk_nr = 0;
disk_data_t *disk_data;
int fd = syscall(SYS_memfd_create, "", 0);
if(fd == -1) return 0;
do {
disk_data = disk_cache_search(disk, &chunk_nr);
if(disk_data) {
if(disk_data->chunk_nr * disk->chunk_size >= offset) {
lseek(fd, disk_data->chunk_nr * disk->chunk_size - offset, SEEK_SET);
write(fd, disk_data->data, disk->chunk_size);
}
}
}
while(chunk_nr != UINT64_MAX);
lseek(fd, 0, SEEK_SET);
return fd;
}
void disk_cache_dump(disk_t *disk, disk_data_t *disk_data, FILE *file)
{
uint8_t all_zeros[16] = {};
uint8_t *data = disk_data->data;
uint64_t max_addr = disk->size_in_bytes - 1;
unsigned address_digits = 0;
while(max_addr >>= 4) address_digits++;
if(address_digits < 4) address_digits = 4;
for(unsigned u = 0; u < disk->chunk_size; u += 16) {
if(memcmp(data + u, &all_zeros, 16)) {
fprintf(file, "%0*"PRIx64" ", address_digits, disk_data->chunk_nr * disk->chunk_size + u);
for(unsigned u1 = 0; u1 < 16; u1++) {
fprintf(file, " %02x", data[u + u1]);
}
fprintf(file, " ");
for(unsigned u1 = 0; u1 < 16; u1++) {
fprintf(file, "%c", data[u + u1] >= 32 && data[u + u1] < 0x7f ? data[u + u1] : '.');
}
fprintf(file, "\n");
}
}
}
disk_data_t *disk_cache_search(disk_t *disk, uint64_t *chunk_nr)
{
disk_data_t *disk_data_found = NULL;
uint64_t next_chunk_nr = UINT64_MAX;
if(*chunk_nr == next_chunk_nr) return NULL;
for(disk_data_t *disk_data = disk->data; disk_data; disk_data = disk_data->next) {
if(disk_data->chunk_nr == *chunk_nr) {
disk_data_found = disk_data;
}
if(disk_data->chunk_nr > *chunk_nr && disk_data->chunk_nr < next_chunk_nr) {
next_chunk_nr = disk_data->chunk_nr;
}
}
*chunk_nr = next_chunk_nr;
return disk_data_found;
}
void disk_add_to_list(disk_t *disk)
{
json_object *json;
disk_list = reallocarray(disk_list, disk_list_size + 1, sizeof *disk_list);
disk_list[disk_list_size] = *disk;
disk_list[disk_list_size].index = disk_list_size;
disk_list[disk_list_size].json_disk =
disk_list[disk_list_size].json_current = json = json_object_new_object();
json_object_array_add(json_root, json);
disk_list_size++;
log_info("%s: %"PRIu64" bytes\n", disk->name, disk->size_in_bytes);
json_object *json_device = json_object_new_object();
json_object_object_add(json, "device", json_device);
json_object_object_add(json_device, "file_name", json_object_new_string(disk->name));
json_object_object_add(json_device, "block_size", json_object_new_int(disk->block_size));
json_object_object_add(json_device, "size", json_object_new_int64(disk->size_in_bytes / disk->block_size));
}
void disk_init(char *file_name)
{
struct stat sbuf;
disk_t disk = { .chunk_size = 512, .block_size = 512 };
disk.fd = open(file_name, O_RDONLY | O_LARGEFILE);
if(disk.fd == -1) {
perror(file_name);
exit(1);
}
disk.name = strdup(file_name);
if(!fstat(disk.fd, &sbuf)) disk.size_in_bytes = sbuf.st_size;
if(!disk.size_in_bytes && ioctl(disk.fd, BLKGETSIZE64, &disk.size_in_bytes)) disk.size_in_bytes = 0;
disk_add_to_list(&disk);
}
void disk_import(char *file_name)
{
FILE *file = fopen(file_name, "r");
if(!file) {
perror(file_name);
exit(1);
}
char *line = NULL;
size_t line_len = 0;
unsigned line_nr = 0;
disk_t disk = { .fd = -1 };
uint8_t chunk[512];
uint64_t current_chunk_nr = UINT64_MAX;
while(getline(&line, &line_len, file) > 0) {
line_nr++;
unsigned index;
uint64_t size;
uint64_t addr;
uint8_t line_data[16];
if(sscanf(line, "# disk %u, size = %"SCNu64"", &index, &size) == 2) {
if(disk.name) {
if(current_chunk_nr != UINT64_MAX) disk_cache_store(&disk, chunk, current_chunk_nr);
disk_add_to_list(&disk);
disk = (disk_t) { .index = disk_list_size };
current_chunk_nr = UINT64_MAX;
}
asprintf(&disk.name, "%s#%u", file_name, index);
disk.size_in_bytes = size;
disk.chunk_size = disk.block_size = 512;
}
else if(
sscanf(line,
"%"SCNx64" %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
&addr,
line_data, line_data + 1, line_data + 2, line_data + 3,
line_data + 4, line_data + 5, line_data + 6, line_data + 7,
line_data + 8, line_data + 9, line_data + 10, line_data + 11,
line_data + 12, line_data + 13, line_data + 14, line_data + 15
) == 17 &&
!(addr & 0xf) &&
addr <= disk.size_in_bytes + 16
) {
// fprintf(stderr, "XXX %08"PRIx64" %02x %02x\n", addr, line_data[0], line_data[1]);
uint64_t chunk_nr = addr / disk.chunk_size;
// fprintf(stderr, "ZZZ chunk_nr %"PRIu64", current_chunk_nr %"PRIu64"\n", chunk_nr, current_chunk_nr);
if(chunk_nr != current_chunk_nr) {
if(current_chunk_nr != UINT64_MAX) disk_cache_store(&disk, chunk, current_chunk_nr);
current_chunk_nr = chunk_nr;
memset(chunk, 0, sizeof chunk);
}
memcpy(chunk + (addr % disk.chunk_size), line_data, 16);
}
else {
fprintf(stderr, "%s: line %u: invalid import data: %s\n", file_name, line_nr, line);
exit(1);
}
}
free(line);
if(disk.name) {
if(current_chunk_nr != UINT64_MAX) disk_cache_store(&disk, chunk, current_chunk_nr);
disk_add_to_list(&disk);
}
}