-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvvfat.cc
2366 lines (2086 loc) · 68.9 KB
/
vvfat.cc
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////
// $Id: vvfat.cc 12788 2015-07-12 14:46:00Z vruppert $
/////////////////////////////////////////////////////////////////////////
//
// Virtual VFAT image support (shadows a local directory)
// ported from QEMU block driver with some additions (see below)
//
// Copyright (c) 2004,2005 Johannes E. Schindelin
// Copyright (C) 2010-2015 The Bochs Project
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
/////////////////////////////////////////////////////////////////////////
// ADDITIONS:
// - win32 specific directory functions (required for MSVC)
// - configurable disk geometry
// - read MBR and boot sector from file
// - FAT32 support
// - volatile runtime write support using the hdimage redolog_t class
// - ask user on Bochs exit if directory and file changes should be committed
// - save and restore FAT file attributes using a separate file
// - set file modification date and time after committing file changes
// - vvfat floppy support (1.44 MB media only)
// Define BX_PLUGGABLE in files that can be compiled into plugins. For
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
// is used to know when we are exporting symbols and when we are importing.
#define BX_PLUGGABLE
#include <dirent.h>
#include <utime.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stropts.h>
#include <linux/fs.h>
//#include "iodev.h"
//#include "hdimage.h"
#include "vvfat.h"
#define LOG_THIS bx_devices.pluginHDImageCtl->
#define VVFAT_MBR "vvfat_mbr.bin"
#define VVFAT_BOOT "vvfat_boot.bin"
#define VVFAT_ATTR "vvfat_attr.cfg"
int hdimage_open_file(const char *pathname, int flags, Bit64u *fsize, time_t *mtime)
{
int fd = ::open(pathname, flags
);
if (fd < 0) {
return fd;
}
if (fsize != NULL) {
struct stat stat_buf;
if (fstat(fd, &stat_buf)) {
printf("fstat() returns error!\n");
return -1;
}
if (S_ISBLK(stat_buf.st_mode)) { // Is this a special device file (e.g. /dev/sde) ?
ioctl(fd, BLKGETSIZE64, fsize); // yes it's!
}
else
{
*fsize = (Bit64u)stat_buf.st_size; // standard unix procedure to get size of regular files
}
if (mtime != NULL) {
*mtime = stat_buf.st_mtime;
}
}
return fd;
}
int bx_read_image(int fd, Bit64s offset, void *buf, int count)
{
if (lseek(fd, offset, SEEK_SET) == -1) {
return -1;
}
return read(fd, buf, count);
}
int bx_write_image(int fd, Bit64s offset, void *buf, int count)
{
if (lseek(fd, offset, SEEK_SET) == -1) {
return -1;
}
return write(fd, buf, count);
}
bx_bool hdimage_backup_file(int fd, const char *backup_fname)
{
char *buf;
off_t offset;
int nread, size;
bx_bool ret = 1;
int backup_fd = ::open(backup_fname, O_RDWR | O_CREAT | O_TRUNC
#ifdef O_BINARY
| O_BINARY
#endif
, S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP);
if (backup_fd >= 0) {
offset = 0;
size = 0x20000;
buf = (char*)malloc(size);
if (buf == NULL) {
::close(backup_fd);
return 0;
}
while ((nread = bx_read_image(fd, offset, buf, size)) > 0) {
if (bx_write_image(backup_fd, offset, buf, nread) < 0) {
ret = 0;
break;
}
if (nread < size) {
break;
}
offset += size;
};
if (nread < 0) {
ret = 0;
}
free(buf);
::close(backup_fd);
return ret;
}
return 0;
}
static int vvfat_count = 0;
redolog_t::redolog_t()
{
fd = -1;
catalog = NULL;
bitmap = NULL;
extent_index = (Bit32u)0;
extent_offset = (Bit32u)0;
extent_next = (Bit32u)0;
}
void redolog_t::print_header()
{
printf("redolog : Standard Header : magic='%s', type='%s', subtype='%s', version = %d.%d\n",
header.standard.magic, header.standard.type, header.standard.subtype,
dtoh32(header.standard.version)/0x10000,
dtoh32(header.standard.version)%0x10000);
if (dtoh32(header.standard.version) == STANDARD_HEADER_VERSION) {
printf("redolog : Specific Header : #entries=%d, bitmap size=%d, exent size = %d disk size = " FMT_LL "d\n",
dtoh32(header.specific.catalog),
dtoh32(header.specific.bitmap),
dtoh32(header.specific.extent),
dtoh64(header.specific.disk));
} else if (dtoh32(header.standard.version) == STANDARD_HEADER_V1) {
redolog_header_v1_t header_v1;
memcpy(&header_v1, &header, STANDARD_HEADER_SIZE);
printf("redolog : Specific Header : #entries=%d, bitmap size=%d, exent size = %d disk size = " FMT_LL "d\n",
dtoh32(header_v1.specific.catalog),
dtoh32(header_v1.specific.bitmap),
dtoh32(header_v1.specific.extent),
dtoh64(header_v1.specific.disk));
}
}
int redolog_t::make_header(const char* type, Bit64u size)
{
Bit32u entries, extent_size, bitmap_size;
Bit64u maxsize;
Bit32u flip=0;
// Set standard header values
memset(&header, 0, sizeof(redolog_header_t));
strcpy((char*)header.standard.magic, STANDARD_HEADER_MAGIC);
strcpy((char*)header.standard.type, REDOLOG_TYPE);
strcpy((char*)header.standard.subtype, type);
header.standard.version = htod32(STANDARD_HEADER_VERSION);
header.standard.header = htod32(STANDARD_HEADER_SIZE);
entries = 512;
bitmap_size = 1;
// Compute #entries and extent size values
do {
extent_size = 8 * bitmap_size * 512;
header.specific.catalog = htod32(entries);
header.specific.bitmap = htod32(bitmap_size);
header.specific.extent = htod32(extent_size);
maxsize = (Bit64u)entries * (Bit64u)extent_size;
flip++;
if(flip&0x01) bitmap_size *= 2;
else entries *= 2;
} while (maxsize < size);
header.specific.timestamp = 0;
header.specific.disk = htod64(size);
print_header();
catalog = (Bit32u*)malloc(dtoh32(header.specific.catalog) * sizeof(Bit32u));
bitmap = (Bit8u*)malloc(dtoh32(header.specific.bitmap));
if ((catalog == NULL) || (bitmap==NULL))
printf("redolog : could not malloc catalog or bitmap\n");
for (Bit32u i=0; i<dtoh32(header.specific.catalog); i++)
catalog[i] = htod32(REDOLOG_PAGE_NOT_ALLOCATED);
bitmap_blocks = 1 + (dtoh32(header.specific.bitmap) - 1) / 512;
extent_blocks = 1 + (dtoh32(header.specific.extent) - 1) / 512;
printf("redolog : each bitmap is %d blocks\n", bitmap_blocks);
printf("redolog : each extent is %d blocks\n", extent_blocks);
return 0;
}
int redolog_t::create(const char* filename, const char* type, Bit64u size)
{
printf("redolog : creating redolog %s\n", filename);
int filedes = ::open(filename, O_RDWR | O_CREAT | O_TRUNC
#ifdef O_BINARY
| O_BINARY
#endif
, S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP);
return create(filedes, type, size);
}
int redolog_t::create(int filedes, const char* type, Bit64u size)
{
fd = filedes;
if (fd < 0)
{
return -1; // open failed
}
if (make_header(type, size) < 0)
{
return -1;
}
// Write header
::write(fd, &header, dtoh32(header.standard.header));
// Write catalog
// FIXME could mmap
::write(fd, catalog, dtoh32(header.specific.catalog) * sizeof (Bit32u));
return 0;
}
int redolog_t::open(const char* filename, const char *type)
{
return open(filename, type, O_RDWR);
}
int redolog_t::open(const char* filename, const char *type, int flags)
{
Bit64u imgsize = 0;
time_t mtime;
fd = hdimage_open_file(filename, flags, &imgsize, &mtime);
if (fd < 0) {
printf("redolog : could not open image %s\n", filename);
// open failed.
return -1;
}
printf("redolog : open image %s\n", filename);
int res = check_format(fd, type);
if (res != HDIMAGE_FORMAT_OK) {
switch (res) {
case HDIMAGE_READ_ERROR:
printf("redolog : could not read header\n");
break;
case HDIMAGE_NO_SIGNATURE:
printf("redolog : Bad header magic\n");
break;
case HDIMAGE_TYPE_ERROR:
printf("redolog : Bad header type or subtype\n");
break;
case HDIMAGE_VERSION_ERROR:
printf("redolog : Bad header version\n");
break;
}
return -1;
}
if (bx_read_image(fd, 0, &header, sizeof(header)) < 0) {
return -1;
}
print_header();
if (dtoh32(header.standard.version) == STANDARD_HEADER_V1) {
redolog_header_v1_t header_v1;
memcpy(&header_v1, &header, STANDARD_HEADER_SIZE);
header.specific.disk = header_v1.specific.disk;
}
if (!strcmp(type, REDOLOG_SUBTYPE_GROWING)) {
set_timestamp(fat_datetime(mtime, 1) | (fat_datetime(mtime, 0) << 16));
}
catalog = (Bit32u*)malloc(dtoh32(header.specific.catalog) * sizeof(Bit32u));
// FIXME could mmap
res = bx_read_image(fd, dtoh32(header.standard.header), catalog, dtoh32(header.specific.catalog) * sizeof(Bit32u));
if (res != (ssize_t)(dtoh32(header.specific.catalog) * sizeof(Bit32u)))
{
printf("redolog : could not read catalog %d=%d\n",res, dtoh32(header.specific.catalog));
return -1;
}
// check last used extent
extent_next = 0;
for (Bit32u i=0; i < dtoh32(header.specific.catalog); i++)
{
if (dtoh32(catalog[i]) != REDOLOG_PAGE_NOT_ALLOCATED)
{
if (dtoh32(catalog[i]) >= extent_next)
extent_next = dtoh32(catalog[i]) + 1;
}
}
printf("redolog : next extent will be at index %d\n",extent_next);
// memory used for storing bitmaps
bitmap = (Bit8u *)malloc(dtoh32(header.specific.bitmap));
bitmap_blocks = 1 + (dtoh32(header.specific.bitmap) - 1) / 512;
extent_blocks = 1 + (dtoh32(header.specific.extent) - 1) / 512;
printf("redolog : each bitmap is %d blocks\n", bitmap_blocks);
printf("redolog : each extent is %d blocks\n", extent_blocks);
imagepos = 0;
bitmap_update = 1;
return 0;
}
void redolog_t::close()
{
if (fd >= 0)
::close(fd);
if (catalog != NULL)
free(catalog);
if (bitmap != NULL)
free(bitmap);
}
Bit64u redolog_t::get_size()
{
return dtoh64(header.specific.disk);
}
Bit32u redolog_t::get_timestamp()
{
return dtoh32(header.specific.timestamp);
}
bx_bool redolog_t::set_timestamp(Bit32u timestamp)
{
header.specific.timestamp = htod32(timestamp);
// Update header
bx_write_image(fd, 0, &header, dtoh32(header.standard.header));
return 1;
}
Bit64s redolog_t::lseek(Bit64s offset, int whence)
{
if ((offset % 512) != 0) {
printf("redolog : lseek() offset not multiple of 512\n");
return -1;
}
if (whence == SEEK_SET) {
imagepos = offset;
} else if (whence == SEEK_CUR) {
imagepos += offset;
} else {
printf("redolog: lseek() mode not supported yet\n");
return -1;
}
if (imagepos > (Bit64s)dtoh64(header.specific.disk)) {
printf("redolog : lseek() to byte %ld failed\n", (long)offset);
return -1;
}
Bit32u old_extent_index = extent_index;
extent_index = (Bit32u)(imagepos / dtoh32(header.specific.extent));
if (extent_index != old_extent_index) {
bitmap_update = 1;
}
extent_offset = (Bit32u)((imagepos % dtoh32(header.specific.extent)) / 512);
//printf("redolog : lseeking extent index %d, offset %d\n",extent_index, extent_offset);
return imagepos;
}
ssize_t redolog_t::read(void* buf, size_t count)
{
Bit64s block_offset, bitmap_offset;
ssize_t ret;
if (count != 512) {
printf("redolog : read() with count not 512\n");
return -1;
}
//printf("redolog : reading index %d, mapping to %d\n", extent_index, dtoh32(catalog[extent_index]));
if (dtoh32(catalog[extent_index]) == REDOLOG_PAGE_NOT_ALLOCATED) {
// page not allocated
return 0;
}
bitmap_offset = (Bit64s)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
bitmap_offset += (Bit64s)512 * dtoh32(catalog[extent_index]) * (extent_blocks + bitmap_blocks);
block_offset = bitmap_offset + ((Bit64s)512 * (bitmap_blocks + extent_offset));
printf("redolog : bitmap offset is %x\n", (Bit32u)bitmap_offset);
printf("redolog : block offset is %x\n", (Bit32u)block_offset);
if (bitmap_update) {
if (bx_read_image(fd, (off_t)bitmap_offset, bitmap, dtoh32(header.specific.bitmap)) != (ssize_t)dtoh32(header.specific.bitmap)) {
printf("redolog : failed to read bitmap for extent %d\n", extent_index);
return -1;
}
bitmap_update = 0;
}
if (((bitmap[extent_offset/8] >> (extent_offset%8)) & 0x01) == 0x00) {
printf("read not in redolog\n");
// bitmap says block not in redolog
return 0;
}
ret = bx_read_image(fd, (off_t)block_offset, buf, count);
if (ret >= 0) lseek(512, SEEK_CUR);
return ret;
}
ssize_t redolog_t::write(const void* buf, size_t count)
{
Bit32u i;
Bit64s block_offset, bitmap_offset, catalog_offset;
ssize_t written;
bx_bool update_catalog = 0;
if (count != 512) {
printf("redolog : write() with count not 512\n");
return -1;
}
//printf("redolog : writing index %d, mapping to %d\n", extent_index, dtoh32(catalog[extent_index]));
if (dtoh32(catalog[extent_index]) == REDOLOG_PAGE_NOT_ALLOCATED) {
if (extent_next >= dtoh32(header.specific.catalog)) {
printf("redolog : can't allocate new extent... catalog is full\n");
return -1;
}
printf("redolog : allocating new extent at %d\n", extent_next);
// Extent not allocated, allocate new
catalog[extent_index] = htod32(extent_next);
extent_next += 1;
char *zerobuffer = (char*)malloc(512);
memset(zerobuffer, 0, 512);
// Write bitmap
bitmap_offset = (Bit64s)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
bitmap_offset += (Bit64s)512 * dtoh32(catalog[extent_index]) * (extent_blocks + bitmap_blocks);
::lseek(fd, (off_t)bitmap_offset, SEEK_SET);
for (i=0; i<bitmap_blocks; i++) {
::write(fd, zerobuffer, 512);
}
// Write extent
for (i=0; i<extent_blocks; i++) {
::write(fd, zerobuffer, 512);
}
free(zerobuffer);
update_catalog = 1;
}
bitmap_offset = (Bit64s)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
bitmap_offset += (Bit64s)512 * dtoh32(catalog[extent_index]) * (extent_blocks + bitmap_blocks);
block_offset = bitmap_offset + ((Bit64s)512 * (bitmap_blocks + extent_offset));
printf("redolog : bitmap offset is %x\n", (Bit32u)bitmap_offset);
printf("redolog : block offset is %x\n", (Bit32u)block_offset);
// Write block
written = bx_write_image(fd, (off_t)block_offset, (void*)buf, count);
// Write bitmap
if (bitmap_update) {
if (bx_read_image(fd, (off_t)bitmap_offset, bitmap, dtoh32(header.specific.bitmap)) != (ssize_t)dtoh32(header.specific.bitmap)) {
printf("redolog : failed to read bitmap for extent %d\n", extent_index);
return 0;
}
bitmap_update = 0;
}
// If bloc does not belong to extent yet
if (((bitmap[extent_offset/8] >> (extent_offset%8)) & 0x01) == 0x00) {
bitmap[extent_offset/8] |= 1 << (extent_offset%8);
bx_write_image(fd, (off_t)bitmap_offset, bitmap, dtoh32(header.specific.bitmap));
}
// Write catalog
if (update_catalog) {
// FIXME if mmap
catalog_offset = (Bit64s)STANDARD_HEADER_SIZE + (extent_index * sizeof(Bit32u));
printf("redolog : writing catalog at offset %x\n", (Bit32u)catalog_offset);
bx_write_image(fd, (off_t)catalog_offset, &catalog[extent_index], sizeof(Bit32u));
}
if (written >= 0) lseek(512, SEEK_CUR);
return written;
}
int redolog_t::check_format(int fd, const char *subtype)
{
redolog_header_t temp_header;
int res = bx_read_image(fd, 0, &temp_header, sizeof(redolog_header_t));
if (res != STANDARD_HEADER_SIZE) {
return HDIMAGE_READ_ERROR;
}
if (strcmp((char*)temp_header.standard.magic, STANDARD_HEADER_MAGIC) != 0) {
return HDIMAGE_NO_SIGNATURE;
}
if (strcmp((char*)temp_header.standard.type, REDOLOG_TYPE) != 0) {
return HDIMAGE_TYPE_ERROR;
}
if (strcmp((char*)temp_header.standard.subtype, subtype) != 0) {
return HDIMAGE_TYPE_ERROR;
}
if ((dtoh32(temp_header.standard.version) != STANDARD_HEADER_VERSION) &&
(dtoh32(temp_header.standard.version) != STANDARD_HEADER_V1)) {
return HDIMAGE_VERSION_ERROR;
}
return HDIMAGE_FORMAT_OK;
}
#ifdef BXIMAGE
int redolog_t::commit(device_image_t *base_image)
{
int ret = 0;
Bit32u i;
Bit8u buffer[512];
printf("\nCommitting changes to base image file: [ 0%%]\n");
for (i = 0; i < dtoh32(header.specific.catalog); i++) {
printf("\x8\x8\x8\x8\x8%3d%%]\n", (i+1)*100/dtoh32(header.specific.catalog));
fflush(stdout);
if (dtoh32(catalog[i]) != REDOLOG_PAGE_NOT_ALLOCATED) {
Bit64s bitmap_offset;
Bit32u bitmap_size, j;
bitmap_offset = (Bit64s)STANDARD_HEADER_SIZE + (dtoh32(header.specific.catalog) * sizeof(Bit32u));
bitmap_offset += (Bit64s)512 * dtoh32(catalog[i]) * (extent_blocks + bitmap_blocks);
// Read bitmap
bitmap_size = dtoh32(header.specific.bitmap);
if ((Bit32u)bx_read_image(fd, (off_t)bitmap_offset, bitmap, bitmap_size) != bitmap_size) {
ret = -1;
break;
}
for (j = 0; j < dtoh32(header.specific.bitmap); j++) {
Bit32u bit;
for (bit = 0; bit < 8; bit++) {
if ( (bitmap[j] & (1 << bit)) != 0) {
Bit64s base_offset, block_offset;
block_offset = bitmap_offset + ((Bit64s)512 * (bitmap_blocks + ((j * 8) + bit)));
if (bx_read_image(fd, (off_t)block_offset, buffer, 512) != 512) {
ret = -1;
break;
}
base_offset = (Bit64s)i * (dtoh32(header.specific.extent));
base_offset += (Bit64s)512 * ((j * 8) + bit);
if (base_image->lseek(base_offset, SEEK_SET) < 0) {
ret = -1;
break;
}
if (base_image->write(buffer, 512) < 0) {
ret = -1;
break;
}
}
}
}
}
}
return ret;
}
#endif
#ifndef BXIMAGE
bx_bool redolog_t::save_state(const char *backup_fname)
{
return hdimage_backup_file(fd, backup_fname);
}
#endif
Bit16u fat_datetime(time_t time, int return_time)
{
struct tm* t;
struct tm t1;
t = &t1;
localtime_r(&time, t);
if (return_time)
return htod16((t->tm_sec/2) | (t->tm_min<<5) | (t->tm_hour<<11));
return htod16((t->tm_mday) | ((t->tm_mon+1)<<5) | ((t->tm_year-80)<<9));
}
// portable mkdir / rmdir
static int bx_mkdir(const char *path)
{
return mkdir(path, 0755);
}
static int bx_rmdir(const char *path)
{
return rmdir(path);
}
// dynamic array functions
static inline void array_init(array_t* array,unsigned int item_size)
{
array->pointer = NULL;
array->size = 0;
array->next = 0;
array->item_size = item_size;
}
static inline void array_free(array_t* array)
{
if (array->pointer)
free(array->pointer);
array->size=array->next = 0;
}
// does not automatically grow
static inline void* array_get(array_t* array,unsigned int index)
{
assert(index < array->next);
return array->pointer + index * array->item_size;
}
static inline int array_ensure_allocated(array_t* array, int index)
{
if ((index + 1) * array->item_size > array->size) {
int new_size = (index + 32) * array->item_size;
array->pointer = (char*)realloc(array->pointer, new_size);
if (!array->pointer)
return -1;
memset(array->pointer + array->size, 0, new_size - array->size);
array->size = new_size;
array->next = index + 1;
}
return 0;
}
static inline void* array_get_next(array_t* array)
{
unsigned int next = array->next;
void* result;
if (array_ensure_allocated(array, next) < 0)
return NULL;
array->next = next + 1;
result = array_get(array, next);
return result;
}
static inline void* array_insert(array_t* array,unsigned int index,unsigned int count)
{
if ((array->next+count)*array->item_size > array->size) {
int increment = count*array->item_size;
array->pointer = (char*)realloc(array->pointer, array->size+increment);
if (!array->pointer)
return NULL;
array->size += increment;
}
memmove(array->pointer+(index+count)*array->item_size,
array->pointer+index*array->item_size,
(array->next-index)*array->item_size);
array->next += count;
return array->pointer+index*array->item_size;
}
/* this performs a "roll", so that the element which was at index_from becomes
* index_to, but the order of all other elements is preserved. */
static inline int array_roll(array_t* array, int index_to, int index_from, int count)
{
char* buf;
char* from;
char* to;
int is;
if (!array ||
(index_to < 0) || (index_to >= (int)array->next) ||
(index_from < 0 || (index_from >= (int)array->next)))
return -1;
if (index_to == index_from)
return 0;
is = array->item_size;
from = array->pointer+index_from*is;
to = array->pointer+index_to*is;
buf = (char*)malloc(is*count);
memcpy(buf, from, is*count);
if (index_to < index_from)
memmove(to+is*count, to, from-to);
else
memmove(from, from+is*count, to-from);
memcpy(to, buf, is*count);
free(buf);
return 0;
}
typedef
struct bootsector_t {
Bit8u jump[3];
Bit8u name[8];
Bit16u sector_size;
Bit8u sectors_per_cluster;
Bit16u reserved_sectors;
Bit8u number_of_fats;
Bit16u root_entries;
Bit16u total_sectors16;
Bit8u media_type;
Bit16u sectors_per_fat;
Bit16u sectors_per_track;
Bit16u number_of_heads;
Bit32u hidden_sectors;
Bit32u total_sectors;
union {
struct {
Bit8u drive_number;
Bit8u reserved;
Bit8u signature;
Bit32u id;
Bit8u volume_label[11];
Bit8u fat_type[8];
Bit8u ignored[0x1c0];
} __attribute__ ((packed))
fat16;
struct {
Bit32u sectors_per_fat;
Bit16u flags;
Bit8u major, minor;
Bit32u first_cluster_of_root_dir;
Bit16u info_sector;
Bit16u backup_boot_sector;
Bit8u reserved1[12];
Bit8u drive_number;
Bit8u reserved2;
Bit8u signature;
Bit32u id;
Bit8u volume_label[11];
Bit8u fat_type[8];
Bit8u ignored[0x1a4];
} __attribute__ ((packed))
fat32;
} u;
Bit8u magic[2];
} __attribute__ ((packed))
bootsector_t;
typedef
struct partition_t {
Bit8u attributes; /* 0x80 = bootable */
mbr_chs_t start_CHS;
Bit8u fs_type; /* 0x1 = FAT12, 0x6 = FAT16, 0xe = FAT16_LBA, 0xb = FAT32, 0xc = FAT32_LBA */
mbr_chs_t end_CHS;
Bit32u start_sector_long;
Bit32u length_sector_long;
} __attribute__ ((packed))
partition_t;
typedef
struct mbr_t {
Bit8u ignored[0x1b8];
Bit32u nt_id;
Bit8u ignored2[2];
partition_t partition[4];
Bit8u magic[2];
} __attribute__ ((packed))
mbr_t;
typedef
struct infosector_t {
Bit32u signature1;
Bit8u ignored[0x1e0];
Bit32u signature2;
Bit32u free_clusters;
Bit32u mra_cluster; // most recently allocated cluster
Bit8u reserved[14];
Bit8u magic[2];
} __attribute__ ((packed))
infosector_t;
vvfat_image_t::vvfat_image_t(Bit64u size, const char* _redolog_name)
{
if (sizeof(bootsector_t) != 512) {
printf("system error: invalid bootsector structure size\n");
}
first_sectors = new Bit8u[0xc000];
memset(&first_sectors[0], 0, 0xc000);
hd_size = size;
redolog = new redolog_t();
redolog_temp = NULL;
redolog_name = NULL;
if (_redolog_name != NULL) {
if ((strlen(_redolog_name) > 0) && (strcmp(_redolog_name,"none") != 0)) {
redolog_name = strdup(_redolog_name);
printf("redolog name: %s\n", redolog_name);
}
}
}
vvfat_image_t::~vvfat_image_t()
{
delete [] first_sectors;
delete redolog;
}
bx_bool vvfat_image_t::sector2CHS(Bit32u spos, mbr_chs_t *chs)
{
Bit32u head, sector;
sector = spos % spt;
spos /= spt;
head = spos % heads;
spos /= heads;
if (spos > 1023) {
/* Overflow,
it happens if 32bit sector positions are used, while CHS is only 24bit.
Windows/Dos is said to take 1023/255/63 as nonrepresentable CHS */
chs->head = 0xff;
chs->sector = 0xff;
chs->cylinder = 0xff;
return 1;
}
chs->head = (Bit8u)head;
chs->sector = (Bit8u)((sector+1) | ((spos >> 8) << 6));
chs->cylinder = (Bit8u)spos;
return 0;
}
void vvfat_image_t::init_mbr(void)
{
mbr_t* real_mbr = (mbr_t*)first_sectors;
partition_t* partition = &(real_mbr->partition[0]);
bx_bool lba;
// Win NT Disk Signature
real_mbr->nt_id = htod32(0xbe1afdfa);
partition->attributes = 0x80; // bootable
// LBA is used when partition is outside the CHS geometry
lba = sector2CHS(offset_to_bootsector, &partition->start_CHS);
lba |= sector2CHS(sector_count - 1, &partition->end_CHS);
// LBA partitions are identified only by start/length_sector_long not by CHS
partition->start_sector_long = htod32(offset_to_bootsector);
partition->length_sector_long = htod32(sector_count - offset_to_bootsector);
/* FAT12/FAT16/FAT32 */
/* DOS uses different types when partition is LBA,
probably to prevent older versions from using CHS on them */
partition->fs_type = fat_type==12 ? 0x1:
fat_type==16 ? (lba?0xe:0x06):
/*fat_tyoe==32*/ (lba?0xc:0x0b);
real_mbr->magic[0] = 0x55;
real_mbr->magic[1] = 0xaa;
}
// dest is assumed to hold 258 bytes, and pads with 0xffff up to next multiple of 26
static inline int short2long_name(char* dest,const char* src)
{
int i;
int len;
for (i = 0; (i < 129) && src[i]; i++) {
dest[2*i] = src[i];
dest[2*i+1] = 0;
}
len = 2 * i;
dest[2*i] = dest[2*i+1] = 0;
for (i = 2 * i + 2; (i % 26); i++)
dest[i] = (char)0xff;
return len;
}
direntry_t* vvfat_image_t::create_long_filename(const char* filename)
{
char buffer[258];
int length = short2long_name(buffer, filename),
number_of_entries = (length+25) / 26, i;
direntry_t* entry;
for (i = 0; i < number_of_entries; i++) {
entry = (direntry_t*)array_get_next(&directory);
entry->attributes = 0xf;
entry->reserved[0] = 0;
entry->begin = 0;
entry->name[0] = (number_of_entries - i) | (i==0 ? 0x40:0);
}
for (i = 0; i < 26 * number_of_entries; i++) {
int offset = (i % 26);
if (offset < 10) offset = 1 + offset;
else if (offset < 22) offset = 14 + offset - 10;
else offset = 28 + offset - 22;
entry = (direntry_t*)array_get(&directory, directory.next - 1 - (i / 26));
entry->name[offset] = buffer[i];
}
return (direntry_t*)array_get(&directory, directory.next-number_of_entries);
}
static char is_long_name(const direntry_t* direntry)
{
return direntry->attributes == 0xf;
}
static void set_begin_of_direntry(direntry_t* direntry, Bit32u begin)
{
direntry->begin = htod16(begin & 0xffff);
direntry->begin_hi = htod16((begin >> 16) & 0xffff);
}
static inline Bit8u fat_chksum(const direntry_t* entry)
{
Bit8u chksum = 0;