-
Notifications
You must be signed in to change notification settings - Fork 130
/
disk_io.c
4468 lines (3976 loc) · 146 KB
/
disk_io.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
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
/* disk_io.c - implement abstract BIOS disk input and output */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <shared.h>
#include <filesys.h>
#include <iso9660.h>
#include "iamath.h"
/* function declaration */
unsigned long long
gunzip_read_func (unsigned long long buf, unsigned long long len, unsigned int write);
block_io_protocol_t blockio_template;
unsigned int fats_type;
unsigned int iso_type;
/* instrumentation variables */
void (*disk_read_func) (unsigned long long, unsigned int, unsigned long long) = NULL;
/* Forward declarations. */
static int next_bsd_partition (void);
static int next_pc_slice (void);
static int next_gpt_slice(void);
static char open_filename[512];
static unsigned int relative_path;
int print_possibilities;
/* patch about cur_part_start and cur_part_entry was suggested by Icecube:
*
* http://www.boot-land.net/forums/index.php?showtopic=10262
*
* with details:
*
* Grub4dos can't chainload my second logical partition (EXTLINUX installed)
* correctly:
*
* title Boot Extlinux
* root (hd0,5)
* chainloader (hd0,5)+1
*
* EXTLINUX (and SYSLINUX) trust the info from the loader that loads the boot
* sector of the partition, not the partition offset stored on the partition
* itself.
*
* A patch for Grub Legacy can be found:
*
* Logical-partition-residing bootloader chainload patch for GRUB Legacy
* http://bugs.gentoo.org/230905
* http://forums.gentoo.org/viewtopic.php?p=5142693#5142693
*
*/
static int unique;
static char *unique_string;
//static unsigned long long cur_part_offset;
//static unsigned int cur_part_addr;
//static unsigned long long cur_part_start;
//static unsigned int cur_part_entry;
static int do_completion;
static int set_filename(char *filename);
int dir (char *dirname);
static int sane_partition (void);
unsigned long long md_part_size;
unsigned long long md_part_base;
/* XX used for device completion in 'set_device' and 'print_completions' */
static int incomplete, disk_choice;
static enum
{
PART_UNSPECIFIED = 0,
PART_DISK,
PART_CHOSEN,
}
part_choice;
/* The first sector of stage2 can be reused as a tmp buffer.
* Do NOT write more than 512 bytes to this buffer!
* The stage2-body, i.e., the pre_stage2, starts at 0x8200!
* Do NOT overwrite the pre_stage2 code at 0x8200!
*/
//char *mbr = (char *)0x8000; /* 512-byte buffer for any use. */
static unsigned int dest_partition;
static unsigned int entry;
static unsigned int bsd_part_no;
static unsigned int pc_slice_no;
unsigned long long fsmax;
struct fsys_entry fsys_table[NUM_FSYS + 1] =
{
/* TFTP should come first because others don't handle net device. */
# ifdef FSYS_PXE
{"pxe", pxe_mount, pxe_read, pxe_dir, pxe_close, 0},
# endif
# ifdef FSYS_TFTP
{"tftp", tftp_mount, tftp_read, tftp_dir, tftp_close, 0},
# endif
# ifdef FSYS_FB
{"fb", fb_mount, fb_read, fb_dir, 0, 0},
#endif
# ifdef FSYS_EXT2FS
{"ext2fs", ext2fs_mount, ext2fs_read, ext2fs_dir, 0, 0},
# endif
# ifdef FSYS_FAT
{"fat", fat_mount, fat_read, fat_dir, 0, 0},
# endif
# ifdef FSYS_NTFS
{"ntfs", ntfs_mount, ntfs_read, ntfs_dir, 0, 0},
# endif
//# ifdef FSYS_MINIX
// {"minix", minix_mount, minix_read, minix_dir, 0, 0},
//# endif
//# ifdef FSYS_REISERFS
// {"reiserfs", reiserfs_mount, reiserfs_read, reiserfs_dir, 0, reiserfs_embed},
//# endif
//# ifdef FSYS_VSTAFS
// {"vstafs", vstafs_mount, vstafs_read, vstafs_dir, 0, 0},
//# endif
//# ifdef FSYS_JFS
// {"jfs", jfs_mount, jfs_read, jfs_dir, 0, jfs_embed},
//# endif
//# ifdef FSYS_XFS
// {"xfs", xfs_mount, xfs_read, xfs_dir, 0, 0},
//# endif
//# ifdef FSYS_UFS2
// {"ufs2", ufs2_mount, ufs2_read, ufs2_dir, 0, ufs2_embed},
//# endif
# ifdef FSYS_ISO9660
{"iso9660", iso9660_mount, iso9660_read, iso9660_dir, 0, 0},
# endif
/* XX FFS should come last as it's superblock is commonly crossing tracks
on floppies from track 1 to 2, while others only use 1. */
//# ifdef FSYS_FFS
// {"ffs", ffs_mount, ffs_read, ffs_dir, 0, ffs_embed},
//# endif
# ifdef FSYS_INITRD
{"initrdfs", initrdfs_mount, initrdfs_read, initrdfs_dir, initrdfs_close, 0},
# endif
{0, 0, 0, 0, 0, 0}
};
/* The register ESI should contain the address of the partition to be
used for loading a chain-loader when chain-loading the loader. */
unsigned int boot_part_addr = 0;
/*
* Global variables describing details of the filesystem
*/
/* FIXME: BSD evil hack */
#include "freebsd.h"
int bsd_evil_hack;
/* filesystem type */
int fsys_type = NUM_FSYS;
struct geometry buf_geom;
struct geometry tmp_geom; /* tmp variable used in many functions. */
int rawread_ignore_memmove_overflow = 0;/* blocklist_func() set this to 1 */
unsigned int emu_iso_sector_size_2048 = 0;
/* Convert unicode filename to UTF-8 filename. N is the max UTF-16 characters
* to be converted. The caller should asure there is enough room in the UTF8
* buffer. Return the length of the converted UTF8 string.
*/
unsigned int unicode_to_utf8 (unsigned short *filename, unsigned char *utf8, unsigned int n);
unsigned int
unicode_to_utf8 (unsigned short *filename, unsigned char *utf8, unsigned int n)
{
unsigned short uni;
unsigned int j, k;
for (j = 0, k = 0; j < n && (uni = filename[j]); j++)
{
if (uni <= 0x007F)
{
utf8[k++] = uni;
}
else if (uni <= 0x07FF)
{
utf8[k++] = 0xC0 | (uni >> 6);
utf8[k++] = 0x80 | (uni & 0x003F);
}
else
{
utf8[k++] = 0xE0 | (uni >> 12);
utf8[k++] = 0x80 | ((uni >> 6) & 0x003F);
utf8[k++] = 0x80 | (uni & 0x003F);
}
}
utf8[k] = 0;
return k;
}
/* Read bytes from DRIVE to BUF. The bytes start at BYTE_OFFSET in absolute 从驱动器DRIVE中读取字节到缓存BUF。
* sector number SECTOR and with BYTE_LEN bytes long. 字节开始于绝对扇区号SECTOR的偏移BYTE_OFFSET,字节长BYTE_LEN。
*/
//原读(驱动器号,扇区号,字节偏移,字节长度,缓存,读/写) 返回: 0/1=失败/成功
//1. 如果缓存驱动器号≠驱动器号, 获取磁盘信息
//2. 处理读列表块, 处理磁盘读挂钩
//3. 处理写磁盘
//4. 将字节长度分解为4k(1000字节)片段,磁盘每次读4k尺寸
//5. 首先从磁盘读数据到临时缓存BUFFERADDR, 然后复制到目标缓存buf
int rawread (unsigned int drive, unsigned long long sector, unsigned int byte_offset, unsigned long long byte_len, unsigned long long buf, unsigned int write);
int
rawread (unsigned int drive, unsigned long long sector, unsigned int byte_offset, unsigned long long byte_len, unsigned long long buf, unsigned int write)
{
if (write != 0x900ddeed && write != 0xedde0d90 && write != GRUB_LISTBLK) //如果“write”不是写/读/块列表. 则错误
return !(errnum = ERR_FUNC_CALL);
errnum = 0;
if (write == 0x900ddeed && ! buf) //如果是写, 而没有缓存, 假写
return 1;
/* Reset geometry and invalidate track buffer if the disk is wrong. */
//如果磁盘错误,请重置几何并使缓冲区无效。
//如果缓存驱动器≠驱动器, 获得磁盘信息
if (buf_drive != drive)
{
if (get_diskinfo (drive, &buf_geom, 0)) //如果'获得磁盘信息'返回非0, 错误
return !(errnum = ERR_NO_DISK);
buf_drive = drive;
buf_track = -1;
}
//如果是列表块, 完成它
if (write == GRUB_LISTBLK)
{
if (disk_read_func) (*disk_read_func)(sector, byte_offset, byte_len);
return 1;
}
//如果缓存为零, 不是正常读写, 完成它
if (!buf)
{ /* Don't waste time reading from disk, just call disk_read_func. */
//不要浪费时间从磁盘读取,只需调用disk_read_func“。
if (disk_read_func) //如果disk_read_func挂钩
{
unsigned int sectorsize = buf_geom.sector_size; //扇区尺寸
if (byte_offset) //如果有偏移
{
unsigned int len = sectorsize - byte_offset; //长度=扇区尺寸-偏移
if (len > byte_len) len = byte_len; //如果长度>请求长度,则长度=请求长度
(*disk_read_func) (sector++, byte_offset, len); //向(*disk_read_func)所代表的函数赋值并执行
byte_len -= len; //请求长度-长度
}
if (byte_len) //如果请求长度非零
{
while (byte_len > sectorsize) //条件:请求长度>扇区大小
{
(*disk_read_func) (sector++, 0, sectorsize); //向(*disk_read_func)所代表的函数赋值并执行
byte_len -= sectorsize; //请求长度-扇区尺寸
}
(*disk_read_func) (sector, 0, byte_len); //向(*disk_read_func)所代表的函数赋值并执行
}
}
return 1;
}
unsigned long long back_filePos = filepos;
//正常读写
while (byte_len > 0) //如果请求字节长度>0
{
unsigned int num_sect, size; //缓存扇区数, 实际读写字节
char *bufaddr; //实际读写位置
num_sect = (BUFFERLEN >> buf_geom.log2_sector_size); //缓存扇区数 缓存字节=0x10000
//如果缓存无效, 或者扇区号不在缓存范围, 则更新缓存区
if (buf_track == (unsigned long long)-1 || sector < buf_track || sector >= (buf_track + num_sect))
{
buf_track = sector & ~((0x1000 >> buf_geom.log2_sector_size) - 1); //4k对齐
if (buf_geom.vhd_disk & 1)
{
filepos = buf_track << 9;
dec_vhd_read ((unsigned long long)(grub_size_t)BUFFERADDR,BUFFERLEN,0xedde0d90);
}
else if (grub_efidisk_readwrite (buf_drive, buf_track, BUFFERLEN, BUFFERADDR, 0xedde0d90))
{
buf_track = -1; /* invalidate the buffer */
return !(errnum = ERR_READ);
}
}
//实际读写位置
bufaddr = BUFFERADDR + ((sector - buf_track) << buf_geom.log2_sector_size) + byte_offset;
//实际读写字节
if (byte_len > (unsigned long long)(BUFFERLEN - (bufaddr - BUFFERADDR)))
size = BUFFERLEN - (bufaddr - BUFFERADDR);
else
size = byte_len;
if (write == 0x900ddeed) //如果写
{
//如果待写数据与原数据一致, 则跳过
if (grub_memcmp64 (buf, (unsigned long long)(grub_size_t)bufaddr, size) == 0)
goto next; /* no need to write */
//更新缓冲区数据
grub_memmove64 ((unsigned long long)(grub_size_t)bufaddr, buf, size); /* update data at bufaddr */
/* write it! */
//更新打开的文件
if (grub_efidisk_readwrite (buf_drive, buf_track, BUFFERLEN, BUFFERADDR, 0x900ddeed))
return !(errnum = ERR_WRITE);
goto next;
}
/* Use this interface to tell which sectors were read and used. */
//使用此接口可以判断哪些扇区被读取和使用。正常读写或略
if (disk_read_func)
{
unsigned long long sector_num = sector;
unsigned int length = buf_geom.sector_size - byte_offset;
if (length > size)
length = size;
(*disk_read_func) (sector_num++, byte_offset, length);
length = size - length;
if (length > 0)
{
while (length > buf_geom.sector_size)
{
(*disk_read_func) (sector_num++, 0, buf_geom.sector_size);
length -= buf_geom.sector_size;
}
(*disk_read_func) (sector_num, 0, length);
}
}
grub_memmove64 (buf, (unsigned long long)(grub_size_t)bufaddr, size);
if (errnum == ERR_WONT_FIT)
{
if (! rawread_ignore_memmove_overflow && buf)
return 0;
errnum = 0;
buf = 0/*NULL*/; /* so that further memcheck() always fail */
}
else
next:
buf += size;
byte_len -= size; /* byte_len always >= size */
sector += (size + byte_offset) >> buf_geom.log2_sector_size;
byte_offset = 0;
} /* while (byte_len > 0) */
filepos = back_filePos + byte_len;
return 1;//(!errnum);
}
//设备读(扇区号,字节偏移,字节长度,缓冲区,读/写) 卷读(分区读)
//1. 如果是光盘, 调整扇区号, 按每扇区200字节计
//2. 检查分区边界
//3. 调整字节偏移,使其在1扇区内
//4. 相对逻辑扇区+分区起始=绝对逻辑扇区
int devread (unsigned long long sector, unsigned long long byte_offset, unsigned long long byte_len, unsigned long long buf, unsigned int write);
int
devread (unsigned long long sector, unsigned long long byte_offset, unsigned long long byte_len, unsigned long long buf, unsigned int write)
{
unsigned int rw_flag = write;
if (rw_flag != 0x900ddeed && rw_flag != 0xedde0d90 && rw_flag != GRUB_LISTBLK)
{//for old devread with 32-bit byte_offset compatibility. 为了兼容旧驱动器的32位byte_offset
rw_flag = *(unsigned int*)(&write - 1);
if (rw_flag != 0x900ddeed && rw_flag != 0xedde0d90)
return !(errnum = ERR_FUNC_CALL);
buf = *(unsigned long long*)(&write - 3);
byte_len = *(unsigned long long*)(&write - 5);
byte_offset = (unsigned int)byte_offset;
}
if (emu_iso_sector_size_2048) //如果是读光盘
{
emu_iso_sector_size_2048 = 0; //修改为每扇区0x200字节
sector <<= (ISO_SECTOR_BITS - buf_geom.log2_sector_size); //0b-09
}
/* Check partition boundaries */
//检查分区边界
//如果(扇区号+(字节偏移+字节长度-1)/扇区尺寸)>=分区长度,并且分区起始不为零
if (((unsigned long long)(sector + ((byte_offset + byte_len - 1) >> buf_geom.log2_sector_size)) >= (unsigned long long)part_length) && part_start)
return !(errnum = ERR_OUTSIDE_PART);
/* Get the read to the beginning of a partition. */
//获取分区的开头。调整字节偏移,使其在1扇区内
sector += byte_offset >> buf_geom.log2_sector_size; //扇区号+(字节偏移/扇区尺寸)
byte_offset &= buf_geom.sector_size - 1; //字节偏移&(扇区尺寸-1)
//如果磁盘读挂钩,并且debug) >= 0x7FFFFFFF,打印"扇区号,字节偏移,字节长度"
if (disk_read_hook && (((unsigned int)debug) >= 0x7FFFFFFF))
printf ("<%ld, %ld, %ld>", (unsigned long long)sector, (unsigned long long)byte_offset, (unsigned long long)byte_len);
/* Call RAWREAD, which is very similar, but: //调用RAWREAD,这是非常相似,但是
* -- It takes an extra parameter, the drive number. //- 它需要一个额外的参数,驱动器号。
* -- It requires that "sector" is relative to the beginning of the disk. //- 它要求,“扇区”是相对于在磁盘开始,即绝对逻辑扇区。
* -- It doesn't handle offsets across the sector boundary. //- 它不处理跨扇区边界的偏移量。
*/
return rawread (current_drive, (sector += part_start), byte_offset, byte_len, buf, rw_flag);
}
/* Write 1 sector at BUF onto sector number SECTOR on drive DRIVE. 从缓存区写1扇区到扇区号sector
* Only a 512-byte sector should be written with this function. 只有512字节的扇区可以使用这个功能。
* Return: 返回0/1=失败/成功
* 1 success
* 0 failure
*/
int rawwrite (unsigned int drive, unsigned long long sector, unsigned long long buf);
int
rawwrite (unsigned int drive, unsigned long long sector, unsigned long long buf)
{
/* Reset geometry and invalidate track buffer if the disk is wrong. */
//如果磁盘错误,请重置几何形状并使轨道缓冲区无效。
if (buf_drive != drive)
{
if (get_diskinfo (drive, &buf_geom, 0))
return !(errnum = ERR_NO_DISK);
buf_drive = drive;
buf_track = -1;
}
/* skip the write if possible. 如果可能的话,跳过写*/
if (grub_efidisk_readwrite (drive, sector, buf_geom.sector_size, SCRATCHADDR, 0xedde0d90))
{
errnum = ERR_READ;
return 0;
}
if (! grub_memcmp64 ((unsigned long long)(grub_size_t)SCRATCHADDR, buf, SECTOR_SIZE))
return 1;
grub_memmove64 ((unsigned long long)(grub_size_t)SCRATCHADDR, buf, SECTOR_SIZE);
if (grub_efidisk_readwrite (drive, sector, buf_geom.sector_size, SCRATCHADDR, 0x900ddeed))
{
errnum = ERR_WRITE;
return 0;
}
return 1;
}
int set_bootdev (int hdbias);
int
set_bootdev (int hdbias)
{
int i, j;
/* Copy the boot partition information to 0x7be-0x7fd for chain-loading. */
if ((current_drive & 0x80)/* && cur_part_addr*/)
{
#if 0
if (rawread (current_drive, cur_part_offset, 0, SECTOR_SIZE, (unsigned long long)(grub_size_t)SCRATCHADDR, 0xedde0d90))
{
char *dst, *src;
/* Need only the partition table.
XXX: We cannot use grub_memmove because BOOT_PART_TABLE
(0x07be) is less than 0x1000. */
dst = (char *) BOOT_PART_TABLE;
src = (char *) SCRATCHADDR + BOOTSEC_PART_OFFSET;
while (dst < (char *) BOOT_PART_TABLE + BOOTSEC_PART_LENGTH)
*dst++ = *src++;
PC_SLICE_START (BOOT_PART_TABLE - PC_SLICE_OFFSET, cur_part_entry) = cur_part_start;
/* Clear the active flag of all partitions. */
for (i = 0; i < 4; i++)
PC_SLICE_FLAG (BOOT_PART_TABLE - BOOTSEC_PART_OFFSET, i) = 0;
/* Set the active flag of the booted partition. */
*((unsigned char *) cur_part_addr) = PC_SLICE_FLAG_BOOTABLE;
boot_part_addr = cur_part_addr;
}
else
{
return 0;
}
#endif
}
/*
* Set BSD boot device.
*/
i = (current_partition >> 16) + 2;
if (current_partition == 0xFFFFFF)
i = 1;
else if ((current_partition >> 16) == 0xFF)
i = 0;
/* FIXME: extremely evil hack!!! */
j = 2;
if (current_drive & 0x80)
j = bsd_evil_hack;
return MAKEBOOTDEV (j, (i >> 4), (i & 0xF), ((current_drive - hdbias) & 0x7F), ((current_partition >> 8) & 0xFF));
}
/*
* This prints the filesystem type or gives relevant information.
*/
void print_fsys_type (void);
void
print_fsys_type (void)
{
if (do_completion) return;
printf (" Filesystem type ");
if (fsys_type != NUM_FSYS)
{
#ifdef FSYS_PXE
#if 0
#ifdef FSYS_IPXE
if (fsys_table[fsys_type].mount_func == pxe_mount)
{
printf ("is %cPXE\n",(current_partition == IPXE_PART)?'i':' ');
return;
}
#endif
#endif
#endif
if (fsys_table[fsys_type].mount_func == fat_mount)
{
switch (fats_type)
{
case 12:
printf ("is fat12, ");
break;
case 16:
printf ("is fat16, ");
break;
case 32:
printf ("is fat32, ");
break;
case 64:
printf ("is exfat, ");
break;
default:
printf ("is %s, ", fsys_table[fsys_type].name);
break;
}
}
else if (fsys_table[fsys_type].mount_func == iso9660_mount)
{
switch (iso_type)
{
case 1:
printf ("is udf, ");
break;
case 2:
printf ("is iso9660_Joliet, ");
break;
case 3:
printf ("is iso9660_RockRidge, ");
break;
default:
printf ("is %s, ", fsys_table[fsys_type].name);
break;
}
}
else
printf ("is %s, ", fsys_table[fsys_type].name);
}
else
printf ("unknown, ");
if (current_partition == 0xFFFFFF)
printf ("using whole disk\n");
else
printf ("partition type 0x%02X\n", (unsigned int)(unsigned char)current_slice);
}
/* Get next BSD partition in current PC slice. */
static int next_bsd_partition (void);
static int
next_bsd_partition (void)
{
int i;
bsd_part_no = (*next_partition_partition & 0xFF00) >> 8;
/* If this is the first time... */
if (bsd_part_no == 0xFF)
{
/* Check if the BSD label is within current PC slice. */
if (*next_partition_len < BSD_LABEL_SECTOR + 1)
{
errnum = ERR_BAD_PART_TABLE;
return 0;
}
/* Read the BSD label. */
if (! rawread (next_partition_drive, *next_partition_start + BSD_LABEL_SECTOR,
0, SECTOR_SIZE, (unsigned long long)(grub_size_t)next_partition_buf, 0xedde0d90))
return 0;
/* Check if it is valid. */
if (! BSD_LABEL_CHECK_MAG (next_partition_buf))
{
errnum = ERR_BAD_PART_TABLE;
return 0;
}
bsd_part_no = -1;
}
/* Search next valid BSD partition. */
if (BSD_LABEL_NPARTS (next_partition_buf) <= BSD_LABEL_NPARTS_MAX)
for (i = bsd_part_no + 1; i < BSD_LABEL_NPARTS (next_partition_buf); i++)
{
if (BSD_PART_TYPE (next_partition_buf, i))
{
/* Note that *TYPE and *PARTITION were set
for current PC slice. */
*next_partition_type = (BSD_PART_TYPE (next_partition_buf, i) << 8) | (*next_partition_type & 0xFF);
*next_partition_start = BSD_PART_START (next_partition_buf, i);
*next_partition_len = BSD_PART_LENGTH (next_partition_buf, i);
*next_partition_partition = (*next_partition_partition & 0xFF00FF) | (i << 8);
/* XXX */
if ((next_partition_drive & 0x80) && BSD_LABEL_DTYPE (next_partition_buf) == DTYPE_SCSI)
bsd_evil_hack = 4;
return 1;
}
}
errnum = ERR_NO_PART;
return 0;
}
/* Get next PC slice. Be careful of that this function may return
an empty PC slice (i.e. a partition whose type is zero) as well. */
#define GPT_ENTRY_SIZE 0x80
static char primary_partition_table[64];
static int partition_table_type = 0;
static unsigned int gpt_part_max;
static int next_gpt_slice(void);
static int next_gpt_slice(void)
{
redo:
if (++pc_slice_no >= gpt_part_max)
{
errnum = ERR_PARTITION_LOOP;
return 0;
}
grub_u64_t sector = *next_partition_entry + (pc_slice_no >> 2);
if (! rawread (next_partition_drive, sector,(pc_slice_no & 3) * sizeof(GPT_ENT) , sizeof(GPT_ENT), (unsigned long long)(grub_size_t)next_partition_buf, 0xedde0d90))
return 0;
P_GPT_ENT PI = (P_GPT_ENT)(grub_size_t)next_partition_buf;
if (PI->starting_lba == 0LL /*|| PI->starting_lba > 0xFFFFFFFFL*/)
{
// errnum = ERR_NO_PART;
return 0;
}
//skip MS_Reserved Partition
if (memcmp(PI->type.raw,"\x16\xE3\xC9\xE3\x5C\x0B\xB8\x4D\x81\x7D\xF9\x2D\xF0\x02\x15\xAE",16) == 0/* && next_partition_dest == 0xffffff*/)
goto redo;
*next_partition_start = PI->starting_lba;
*next_partition_len = (unsigned long long)(PI->ending_lba - PI->starting_lba + 1);
*next_partition_partition = (pc_slice_no << 16) | 0xFFFF;
*next_partition_type = PC_SLICE_TYPE_GPT;
grub_memmove(&partition_signature, &PI->uid, 16); //GPT分区GUID
return 1;
}
static int is_gpt_part(void);
static int is_gpt_part(void)
{
GPT_HDR hdr;
if (! rawread (next_partition_drive, 1, 0, sizeof(hdr), (unsigned long long)(grub_size_t)&hdr, 0xedde0d90))
return 0;
if (hdr.hdr_sig != GPT_HDR_SIG) /* Signature ("EFI PART") */
return 0;
if (hdr.hdr_size != 0x5C)/*Header size (in bytes, usually 5C 00 00 00 meaning 92 bytes)*/
return 0;
if (hdr.hdr_lba_self != 1LL) /*Current LBA (location of this header copy),must be 1*/
return 0;
if (hdr.hdr_entsz != GPT_ENTRY_SIZE) /* Size of a partition entry (usually 128) */
{
return 0;
}
*next_partition_entry = hdr.hdr_lba_table;/* Partition entries starting LBA */
gpt_part_max = hdr.hdr_entries;/* Number of partition entries */
partition_table_type = PC_SLICE_TYPE_GPT;
return 1;
}
static int next_pc_slice (void);
static int
next_pc_slice (void)
{
redo:
pc_slice_no = (*next_partition_partition & 0xFF0000) >> 16;
if (pc_slice_no == 0xFE)
{
errnum = ERR_PARTITION_LOOP;
return 0;
}
/* If this is the first time... */
if (pc_slice_no == 0xFF)
{
partition_table_type = 0;
*next_partition_offset = 0;
*next_partition_ext_offset = 0;
*next_partition_entry = -1;
pc_slice_no = -1;
}
else if (partition_table_type == PC_SLICE_TYPE_GPT)
{
return next_gpt_slice();
}
/* Read the MBR or the boot sector of the extended partition. */
if (! rawread (next_partition_drive, *next_partition_offset, 0, SECTOR_SIZE, (unsigned long long)(grub_size_t)next_partition_buf, 0xedde0d90))
return 0;
if (pc_slice_no == (unsigned int)-1 && next_partition_buf[0x1C2] == '\xEE' && is_gpt_part())
{
if (next_partition_dest != 0xffffff)
pc_slice_no = (next_partition_dest>>16) - 1;
return next_gpt_slice();
}
if (*(unsigned int *)&next_partition_buf[0x1b4] == 0x46424246 && !fb_status)
fb_status = 0xff3f003f | ((unsigned char)next_partition_drive << 8);
/* Check if it is valid. */
if (! PC_MBR_CHECK_SIG (next_partition_buf))
{
bad_part_table:
errnum = ERR_BAD_PART_TABLE;
return 0;
}
/* backup partition table in the MBR */
if (*next_partition_offset == 0)
{
grub_memmove (primary_partition_table, next_partition_buf + 0x1BE, 64);
}
else
{
int i;
/* Check if it is the same as primary_partition_table. */
if (! grub_memcmp (primary_partition_table, next_partition_buf + 0x1BE, 64))
goto bad_part_table;
/* Check if it contains extended partition entry. if yes, check if it is valid. */
for (i = 0; i < PC_SLICE_MAX; i++)
{
if (IS_PC_SLICE_TYPE_EXTENDED (PC_SLICE_TYPE (next_partition_buf, i)))
{
/* the start should not equal to the last one */
if ((*next_partition_ext_offset + PC_SLICE_START (next_partition_buf, i)) == *next_partition_offset)
goto bad_part_table;
}
}
}
next_entry:
/* Increase the entry number. */
(*next_partition_entry)++;
/* If this is out of current partition table... */
if (*next_partition_entry == PC_SLICE_MAX)
{
int i;
/* Search the first extended partition in current table. */
for (i = 0; i < PC_SLICE_MAX; i++)
{
if (IS_PC_SLICE_TYPE_EXTENDED (PC_SLICE_TYPE (next_partition_buf, i)))
{
/* Found. Set the new offset and the entry number,
and restart this function. */
unsigned long long tmp_start = (unsigned long long)(grub_size_t)(PC_SLICE_START (next_partition_buf, i));
unsigned long long tmp_ext_offset = (unsigned long long)(grub_size_t)(*next_partition_ext_offset);
unsigned long long tmp_offset = tmp_ext_offset + tmp_start;
/* if overflow ... */
/* use this to keep away from the gcc bug.
* (tmp_offset >= 0x100000000ULL) is also OK, but
* (((unsigned long *)(&tmp_offset))[1]) is not OK with the buggy gcc.
*/
if (tmp_offset >> 32) //if (tmp_offset >= 0x100000000ULL)
continue;
*next_partition_offset = tmp_offset;
if (! *next_partition_ext_offset)
*next_partition_ext_offset = tmp_start;
*next_partition_entry = -1;
goto redo;
}
}
if (next_partition_dest != 0xffffff)
errnum = ERR_NO_PART;
return 0;
}
{
unsigned long long tmp_start = (unsigned long long)(grub_size_t)(PC_SLICE_START (next_partition_buf, *next_partition_entry));
unsigned long long tmp_offset = *next_partition_offset;
tmp_start += tmp_offset;
*next_partition_start = tmp_start;
*next_partition_type = PC_SLICE_TYPE (next_partition_buf, *next_partition_entry);
*next_partition_len = PC_SLICE_LENGTH (next_partition_buf, *next_partition_entry);
grub_memset (&partition_signature, 0, 16);
*(unsigned int *)partition_signature = PC_DISK_SIG (next_partition_buf); //MBR分区签名
partition_activity_flag = PC_SLICE_FLAG(next_partition_buf, *next_partition_entry);
/* if overflow ... */
/* use this to keep away from the gcc bug.
* (tmp_start >= 0x100000000ULL) is also OK, but
* (((unsigned long *)(&tmp_start))[1]) is not OK with the buggy gcc.
*/
if (tmp_start >> 32) //if (tmp_offset >= 0x100000000ULL)
//if (((int)pc_slice_no) >= PC_SLICE_MAX - 1) /* yes, on overflow it is always a logical partition. */
goto next_entry;
/* The calculation of a PC slice number is complicated, because of
the rather odd definition of extended partitions. Even worse,
there is no guarantee that this is consistent with every
operating systems. Uggh. */
if (((int)pc_slice_no) >= PC_SLICE_MAX - 1) /* if it is a logical partition */
{
if (PC_SLICE_ENTRY_IS_EMPTY (next_partition_buf, *next_partition_entry)) /* ignore the garbage entry(typically all bytes are 0xF6). */
goto next_entry;
}
else /* primary partition */
{
if ((PC_SLICE_FLAG (next_partition_buf, *next_partition_entry)) & 0x7F) /* ignore the garbage entry with wrong boot indicator. */
goto null_entry;
if (!((PC_SLICE_SEC (next_partition_buf, *next_partition_entry)) & 0x3F)) /* ignore the garbage entry with wrong starting sector. */
goto null_entry;
if (!((PC_SLICE_ESEC (next_partition_buf, *next_partition_entry)) & 0x3F)) /* ignore the garbage entry with wrong ending sector. */
goto null_entry;
if ((PC_SLICE_HEAD (next_partition_buf, *next_partition_entry)) == 0xFF) /* ignore the garbage entry with wrong starting head. */
goto null_entry;
if ((PC_SLICE_EHEAD (next_partition_buf, *next_partition_entry)) == 0xFF) /* ignore the garbage entry with wrong ending head. */
{
null_entry:
*next_partition_start = 0;
*next_partition_type = 0;
*next_partition_len = 0;
}
}
}
if (((int)pc_slice_no) >= PC_SLICE_MAX - 1
&& IS_PC_SLICE_TYPE_EXTENDED (*next_partition_type))
goto next_entry;
/* disable partition length of 0. */
if (((int)pc_slice_no) >= PC_SLICE_MAX - 1 /* if it is a logical partition */
&& *next_partition_len == 0) /* ignore the partition with length=0. */
goto next_entry;
pc_slice_no++;
*next_partition_partition = (pc_slice_no << 16) | 0xFFFF;
return 1;
}
/* Get the information on next partition on the drive DRIVE.
The caller must not modify the contents of the arguments when
iterating this function. The partition representation in GRUB will
be stored in *PARTITION. Likewise, the partition type in *TYPE, the
start sector in *START, the length in *LEN, the offset of the
partition table in *OFFSET, the entry number in the table in *ENTRY,
the offset of the extended partition in *EXT_OFFSET.
BUF is used to store a MBR, the boot sector of a partition, or
a BSD label sector, and it must be at least 512 bytes length.
When calling this function first, *PARTITION must be initialized to
0xFFFFFF. The return value is zero if fails, otherwise non-zero. */
int next_partition (void);
int
next_partition (void)
{
/* Start the body of this function. */
if ((current_drive == NETWORK_DRIVE) || (current_drive == PXE_DRIVE) || (current_drive == FB_DRIVE))
return 0;
/* If previous partition is a BSD partition or a PC slice which
contains BSD partitions... */
if ((*next_partition_partition != 0xFFFFFF && IS_PC_SLICE_TYPE_BSD (*next_partition_type & 0xff))
|| ! (next_partition_drive & 0x80))
{
if (*next_partition_type == PC_SLICE_TYPE_NONE)
*next_partition_type = PC_SLICE_TYPE_FREEBSD;
/* Get next BSD partition, if any. */
if (next_bsd_partition ())
return 1;
/* If the destination partition is a BSD partition and current
BSD partition has any error, abort the operation. */
if ((next_partition_dest & 0xFF00) != 0xFF00
&& ((next_partition_dest & 0xFF0000) == 0xFF0000
|| (next_partition_dest & 0xFF0000) == (*next_partition_partition & 0xFF0000)))
return 0;
/* Ignore the error. */
errnum = ERR_NONE;
}
return next_pc_slice ();
}
static void attempt_mount (void);
static void
attempt_mount (void)
{
int cdrom = (current_drive >= 0xa0 && current_drive != 0xffff);
for (fsys_type = 0; fsys_type < NUM_FSYS; fsys_type++)
{
if ((cdrom && fsys_table[fsys_type].mount_func != iso9660_mount) || !(fsys_table[fsys_type].mount_func))
continue;
if (errnum = 0, ((fsys_table[fsys_type].mount_func) ()))
break;
}
if (fsys_type == NUM_FSYS && errnum == ERR_NONE)
errnum = ERR_FSYS_MOUNT;
}
/*
* This performs a "mount" on the current device, both drive and partition
* number.
*/
int open_device (void);
int
open_device (void)
{
if (open_partition ())
attempt_mount (); /* device could be pd, nd or ud */
if (errnum != ERR_NONE)
return 0;
return 1;
}
static void check_and_print_mount (void);
static void
check_and_print_mount (void)
{
/* at this point, device has normal partition table. */
attempt_mount ();
if (errnum == ERR_FSYS_MOUNT)
errnum = ERR_NONE;
if (!errnum)
print_fsys_type ();
print_error ();
}
/* Open a partition. */
int real_open_partition (int flags);
int
real_open_partition (int flags)
{
dest_partition = current_partition;
grub_memset(vol_name, 0, 256);
/* network drive */
if ((current_drive == NETWORK_DRIVE) || (current_drive==PXE_DRIVE))
{
part_length = 0;
return 1;
}