-
Notifications
You must be signed in to change notification settings - Fork 130
/
builtins.c
14996 lines (13796 loc) · 419 KB
/
builtins.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
/* builtins.c - the GRUB builtin commands */
/*
* 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 <term.h>
#include "iamath.h"
#include <iso9660.h>
//#ifdef SUPPORT_SERIAL
//# include <serial.h>
//# include <terminfo.h>
//#endif
#ifdef USE_MD5_PASSWORDS
# include <md5.h>
#endif
#include "freebsd.h"
#include "cpio.h"
/* The type of kernel loaded. */
kernel_t kernel_type;
//static kernel_t kernel_type_orig;
/* The boot device. */
static int bootdev = 0;
/* The default entry. */
int default_entry = 0;
/* The fallback entry. */
int fallback_entryno;
int fallback_entries[MAX_FALLBACK_ENTRIES];
/* The number of current entry. */
int current_entryno;
#ifdef SUPPORT_GFX
/* graphics file */
//char graphics_file[64];
#endif
/* The address for Multiboot command-line buffer. */
static char *mb_cmdline;// = (char *) MB_CMDLINE_BUF;
//static char kernel_option_video[64] = {0};/* initialize the first byte to 0 */
/* The password. */
char *password_buf;
static char password_str[128];
/* The password type. */
password_t password_type;
/* The flag for indicating that the user is authoritative. */
int auth = 0;
/* The timeout. */
//int grub_timeout = -1;
/* Whether to show the menu or not. */
int show_menu = 1;
/* Don't display a countdown message for the hidden menu */
int silent_hiddenmenu = 0;
int debug_prog;
int debug_bat = 0;
int debug_ptrace = 0;
//static int debug_break = 0;
//static int debug_pid = 0;
//static int debug_check_memory = 0;
static grub_u8_t msg_password[]="Password: ";
unsigned int pxe_restart_config = 0;
unsigned int configfile_in_menu_init = 0;
/* 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!
*/
//extern char *mbr /* = (char *)0x8000 */; /* 512-byte buffer for any use. */
extern int dir (char *dirname);
#ifdef SUPPORT_GRAPHICS
//extern int outline;
#endif /* SUPPORT_GRAPHICS */
/* The BIOS drive map. */
//int drive_map_slot_empty (struct drive_map_slot item);
static int chainloader_load_segment = 0;
static int chainloader_load_offset = 0;
static int chainloader_skip_length = 0;
static int chainloader_ebx = 0;
static int chainloader_ebx_set = 0;
static int chainloader_edx = 0;
static int chainloader_edx_set = 0;
static int is_io = 0;
//使用于configfile_func, command_open, GetParentUtf8Name
static char chainloader_file_orig[256];
static const char *warning_defaultfile = "# WARNING: If you want to edit this file directly, do not remove any line";
int probe_bpb (struct master_and_dos_boot_sector *BS);
int probe_mbr (struct master_and_dos_boot_sector *BS, unsigned int start_sector1, unsigned int sector_count1, unsigned int part_start1);
int parse_string (char *arg);
int envi_cmd(const char *var,char * const env,int flags);
//extern int count_lines;
//extern int use_pager;
#if defined(__i386__)
char * convert_to_ascii (char *buf, int c, int lo, int hi);
#else
char *convert_to_ascii (char *buf, int c, unsigned long long lo);
#endif
unsigned int prog_pid;
extern int errorcheck;
unsigned int next_partition_drive;
unsigned int next_partition_dest;
unsigned int *next_partition_partition;
unsigned int *next_partition_type;
unsigned long long *next_partition_start;
unsigned long long *next_partition_len;
unsigned long long *next_partition_offset;
unsigned int *next_partition_entry;
unsigned int *next_partition_ext_offset;
char *next_partition_buf;
unsigned char partition_signature[16]; //分区签名
unsigned char partition_activity_flag; //分区活动标志
int fsys_type;
extern unsigned int fats_type;
extern unsigned int iso_type;
int get_mmap_entry (struct mmar_desc *desc, int cont);
int no_install_vdisk = 0; //0/1=安装虚拟磁盘/不安装虚拟磁盘
static int ls_func (char *arg, int flags);
static char *GRUB_MOD_ADDR=0;
static char* mod_end;
static int find_func (char *arg, int flags);
static int insmod_func(char *arg,int flags);
static unsigned long long start_sector, sector_count;
unsigned long long initrd_start_sector;
grub_efi_uint64_t part_addr;
grub_efi_uint64_t part_size;
grub_efi_uint32_t cd_boot_entry;
grub_efi_uint32_t cd_boot_start;
grub_efi_uint32_t cd_boot_size;
grub_efi_uint32_t cd_Image_part_start;
grub_efi_uint32_t cd_Image_disk_size;
grub_efi_uint32_t cd_map_count = 0;
struct grub_part_data *part_data;
void set_full_path(char *dest, char *arg, grub_u32_t max_len);
void set_full_path(char *dest, char *arg, grub_u32_t max_len)
{
int len;
if (*arg != '/' && !(*arg == '(' && arg[1] == ')'))
{
grub_memmove (dest, arg, max_len);
return;
}
print_root_device(dest,0);
len = strlen(dest);
if (*arg == '/') grub_sprintf(dest + len,"%s%s",saved_dir,arg);
else grub_sprintf(dest + len,"%s",arg + 2);
}
#if 0
int drive_map_slot_empty (struct drive_map_slot item);
int
drive_map_slot_empty (struct drive_map_slot item)//判断驱动器映像插槽是否为空 为空,返回1
{
grub_size_t *array = (grub_size_t *)&item;
grub_size_t n = sizeof (struct drive_map_slot) / sizeof (grub_size_t);
while (n)
{
if (*array)
return 0;
array++;
n--;
}
return 1;
}
int disable_map_info = 0;
#endif
/* Prototypes for allowing straightfoward calling of builtins functions
inside other functions. */
static int configfile_func (char *arg, int flags);
int command_func (char *arg, int flags);
int commandline_func (char *arg, int flags);
int errnum_func (char *arg, int flags);
int checkrange_func (char *arg, int flags);
/* Check a password for correctness. Returns 0 if password was
correct, and a value != 0 for error, similarly to strcmp. */
int check_password (char* expected, password_t type);
int
check_password (char* expected, password_t type)
{
/* Do password check! */
char entered[32];
/* Wipe out any previously entered password */
memset(entered,0,sizeof(entered));
get_cmdline_str.prompt = msg_password;
get_cmdline_str.maxlen = sizeof (entered) - 1;
get_cmdline_str.echo_char = '*';
get_cmdline_str.readline = 0;
get_cmdline_str.cmdline = (unsigned char*)entered;
get_cmdline ();
switch (type)
{
case PASSWORD_PLAIN:
return strcmp (entered, expected);
#ifdef USE_MD5_PASSWORDS
case PASSWORD_MD5:
return check_md5_password (entered, expected);
#endif
default:
/* unsupported password type: be secure */
return 1;
}
}
/* Print which sector is read when loading a file. */
static void disk_read_print_func (unsigned long long sector, unsigned int offset, unsigned long long length);
static void
disk_read_print_func (unsigned long long sector, unsigned int offset, unsigned long long length)
{
grub_printf ("[0x%lx,0x%x,0x%lx]", sector, offset, length);
}
extern int rawread_ignore_memmove_overflow; /* defined in disk_io.c */
int query_block_entries;
//static unsigned long long map_start_sector[DRIVE_MAP_FRAGMENT];
//static unsigned long long map_num_sectors[DRIVE_MAP_FRAGMENT];
unsigned long long* map_start_sector=0;
unsigned long long* map_num_sectors;
static unsigned long long blklst_start_sector;
static unsigned long long blklst_num_sectors;
static unsigned int blklst_num_entries;
static unsigned int blklst_last_length;
/* Collect contiguous blocks into one entry as many as possible,
and print the blocklist notation on the screen. */
static void disk_read_blocklist_func (unsigned long long sector, unsigned int offset, unsigned long long length);
static void
disk_read_blocklist_func (unsigned long long sector, unsigned int offset, unsigned long long length)
{
unsigned int sectorsize = buf_geom.sector_size;
// unsigned char sector_bit = (sectorsize == 2048 ? 11 : sectorsize == 4096 ? 12 : 9);
unsigned char sector_bit = buf_geom.log2_sector_size;
#ifdef FSYS_INITRD
if (fsys_table[fsys_type].mount_func == initrdfs_mount)
{
if (query_block_entries >= 0)
printf("(md,0x%lx,0x%lx)+1",(sector << SECTOR_BITS) + offset,length);
return;
}
#endif
if (blklst_num_sectors > 0)
{
if (blklst_start_sector + blklst_num_sectors == sector
&& offset == 0 && blklst_last_length == 0)
{
blklst_num_sectors += (length + (sectorsize - 1)) >> sector_bit;
blklst_last_length = (length - (sectorsize - offset))&(sectorsize - 1);
return;
}
else
{
if (query_block_entries >= 0)
{
if (blklst_last_length == 0)
grub_printf ("%s0x%lx+0x%lx", (blklst_num_entries ? "," : ""),
(unsigned long long)(blklst_start_sector/* - part_start*/), blklst_num_sectors);
else if (blklst_num_sectors > 1)
grub_printf ("%s0x%lx+0x%lx,0x%lx[0-0x%x]", (blklst_num_entries ? "," : ""),
(unsigned long long)(blklst_start_sector/* - part_start*/), (blklst_num_sectors-1),
(unsigned long long)(blklst_start_sector + blklst_num_sectors-1/* - part_start*/),
blklst_last_length);
else
grub_printf ("%s0x%lx[0-0x%x]", (blklst_num_entries ? "," : ""),
(unsigned long long)(blklst_start_sector/* - part_start*/), blklst_last_length);
}
else if (blklst_last_length == 0 && blklst_num_entries < DRIVE_MAP_FRAGMENT)
{
map_start_sector[blklst_num_entries] = blklst_start_sector;
map_num_sectors[blklst_num_entries] = blklst_num_sectors;
}
blklst_num_entries++;
blklst_num_sectors = 0;
}
}
if (offset > 0)
{
if (query_block_entries >= 0)
grub_printf("%s0x%lx[0x%x-0x%x]", (blklst_num_entries ? "," : ""),
(unsigned long long)(sector/* - part_start*/), offset, (offset + length));
blklst_num_entries++;
}
else
{
blklst_start_sector = sector;
blklst_num_sectors = (length + sectorsize - 1) >> sector_bit;
blklst_last_length = (length - (sectorsize - offset))&(sectorsize - 1);
}
}
/* blocklist */
static int blocklist_func (char *arg, int flags);
static int
blocklist_func (char *arg, int flags)
{
char *dummy = NULL;
unsigned long long err;
#ifndef NO_DECOMPRESSION
int no_decompression_bak = no_decompression;
#endif
errnum = 0;
blklst_start_sector = 0;
blklst_num_sectors = 0;
blklst_num_entries = 0;
blklst_last_length = 0;
if (!map_start_sector)
{
map_start_sector = grub_zalloc(DRIVE_MAP_FRAGMENT);
map_num_sectors = grub_zalloc(DRIVE_MAP_FRAGMENT);
}
else
{
grub_memset (map_start_sector, 0, DRIVE_MAP_FRAGMENT);
grub_memset (map_num_sectors, 0, DRIVE_MAP_FRAGMENT);
}
#if 0
int i;
for (i = 0; i < DRIVE_MAP_FRAGMENT; i++)
{
map_start_sector[i] =0;
map_num_sectors[i] =0;
}
#endif
/* Open the file. */
if (! grub_open (arg))
goto fail_open;
#ifdef FSYS_INITRD
if (fsys_table[fsys_type].mount_func == initrdfs_mount)
{
disk_read_hook = disk_read_blocklist_func;
err = grub_read ((unsigned long long)(grub_size_t)dummy,-1ULL, GRUB_LISTBLK);
disk_read_hook = 0;
goto fail_read;
}
#endif
#ifndef NO_DECOMPRESSION
if (compressed_file)
{
if (query_block_entries < 0 && decomp_type != DECOMP_TYPE_VHD) //为了给vhd_start_sector赋值
{
/* compressed files are not considered contiguous. */
goto fail_read;
}
grub_close ();
no_decompression = 1;
if (! grub_open (arg))
goto fail_open;
}
#endif /* NO_DECOMPRESSION */
/* Print the device name. */
if (query_block_entries >= 0) print_root_device (NULL,1 | 0x100);
rawread_ignore_memmove_overflow = 1;
/* Read in the whole file to DUMMY. */
disk_read_hook = disk_read_blocklist_func;
err = grub_read ((unsigned long long)(grub_size_t)dummy, -1ULL, GRUB_LISTBLK);
disk_read_hook = 0;
rawread_ignore_memmove_overflow = 0;
if (fsys_table[fsys_type].mount_func == pxe_mount)
{
map_start_sector[0] = (unsigned long long)(grub_size_t)(char*)efi_pxe_buf;
printf("0x%lx+0x%lx", (unsigned long long)(grub_size_t)(char*)efi_pxe_buf >> 9, (filemax + 0x1ff) >> 9);
query_block_entries = blklst_num_entries = 1;
goto fail_read;
}
if (! err)
goto fail_read;
/* The last entry may not be printed yet. Don't check if it is a
* full sector, since it doesn't matter if we read too much. */
if (blklst_num_sectors > 0)
{
if (query_block_entries >= 0)
grub_printf ("%s0x%lx+0x%lx", (blklst_num_entries ? "," : ""),
(unsigned long long)(blklst_start_sector/* - part_start*/), blklst_num_sectors);
else if (blklst_num_entries < DRIVE_MAP_FRAGMENT)
{
map_start_sector[blklst_num_entries] = blklst_start_sector;
map_num_sectors[blklst_num_entries] = blklst_num_sectors;
}
blklst_num_entries++;
}
if (query_block_entries >= 0)
grub_putchar ('\n', 255);
else
query_block_entries = blklst_num_entries;
fail_read:
grub_close ();
fail_open:
#ifndef NO_DECOMPRESSION
no_decompression = no_decompression_bak;
#endif
if (query_block_entries < 0)
query_block_entries = 0;
return ! errnum;
}
static struct builtin builtin_blocklist =
{
"blocklist",
blocklist_func,
BUILTIN_MENU | BUILTIN_CMDLINE | BUILTIN_SCRIPT | BUILTIN_HELP_LIST | BUILTIN_IFTITLE | BUILTIN_NO_DECOMPRESSION,
"blocklist FILE",
"Print the blocklist notation of the file FILE."
};
/* WinME support by bean. Thanks! */
#define obuf_init(buf) obuf_ptr=buf
#define obuf_putc(ch) *(obuf_ptr++)=ch
#define obuf_copy(ofs,len) do { for (;len>0;obuf_ptr++,len--) *(obuf_ptr)=*(obuf_ptr-ofs); } while (0)
//static grub_efi_char16_t chainloader_type = 0;
static grub_efi_handle_t image_handle;
void hexdump(grub_u64_t ofs,char* buf,int len);
void hexdump(grub_u64_t ofs,char* buf,int len)
{
quit_print=0;
int align = len > 16;
while (len>0)
{
int cnt,k,i,j = 3;
if (align)
{
i = ofs & 0xFLL;
if (i)
ofs &= ~0xFLL;
}
else
{
i = 0;
}
if ((ofs >> 32))
j = 7;
grub_printf ("%0*lX: ", j == 3?8:10,ofs);
cnt = 16;
if (cnt > len)
cnt = len + i;
for (k=0;k<16;k++)
{
if (i>k || k >=cnt)
printf(" ");
else
printf("%02X ", (unsigned long)(unsigned char)(buf[k - i]));
if ((k!=15) && ((k & j)==j))
printf(" ");
}
printf("; ");
for (k=0;k<cnt;k++)
{
if (i>k)
putchar(' ',255);
else
{
j = k - i;
putchar((((unsigned char)buf[j]>=32) && ((unsigned char)buf[j]<0x7f))?buf[j]:'.', 255);
}
}
printf("\n");
if (quit_print)
break;
ofs+=16;
cnt -= i;
len -= cnt;
buf += cnt;
}
}
/* cat */
static int cat_func (char *arg, int flags);
static int
cat_func (char *arg, int flags)
{
unsigned char c;
unsigned char s[128];
unsigned long long Hex = 0;
unsigned int len, i, j;
char *p;
unsigned long long skip = 0;
unsigned long long length = 0xffffffffffffffffULL;
char *locate = 0;
char *replace = 0;
unsigned long long locate_align = 1;
unsigned long long len_s;
unsigned long long len_r = 0;
unsigned long long ret = 0;
unsigned long long number = -1ULL;
int locate_ignore_case = 0;
quit_print = 0;
errnum = 0;
for (;;)
{
if (grub_memcmp (arg, "--hex=", 6) == 0)
{
arg += 6;
safe_parse_maxint (&arg, &Hex);
}
else if (grub_memcmp (arg, "--hex", 5) == 0)
{
Hex = 1;
}
else if (grub_memcmp (arg, "--skip=", 7) == 0)
{
arg += 7;
safe_parse_maxint(&arg,&skip);
}
else if (grub_memcmp (arg, "--length=", 9) == 0)
{
arg += 9;
safe_parse_maxint(&arg, &length);
}
else if (grub_memcmp (arg, "--locate=", 9) == 0)
{
locate = arg += 9;
}
else if (grub_memcmp (arg, "--locatei=", 9) == 0)
{
locate_ignore_case = 1;
locate = arg += 10;
}
else if (grub_memcmp (arg, "--replace=", 10) == 0)
{
replace = arg += 10;
}
else if (grub_memcmp (arg, "--locate-align=", 15) == 0)
{
arg += 15;
if (! safe_parse_maxint (&arg, &locate_align))
return 0;
if ((unsigned int)locate_align == 0)
return ! (errnum = ERR_BAD_ARGUMENT);
}
else if (grub_memcmp (arg, "--number=",9) == 0)
{
arg += 9;
safe_parse_maxint (&arg, &number);
}
else
break;
if (errnum)
return 0;
arg = skip_to (0, arg);
}
if (! length)
{
if (grub_memcmp (arg,"()-1\0",5) == 0 )
{
if (! grub_open ("()+1"))
return 0;
filesize = filemax*(unsigned long long)part_start;
}
else if (grub_memcmp (arg,"()\0",3) == 0 )
{
if (! grub_open ("()+1"))
return 0;
filesize = filemax*(unsigned long long)part_length;
}
else
{
int no_decompression_bak = no_decompression;
no_decompression = 1;
if (! grub_open (arg))
{
no_decompression = no_decompression_bak;
return 0;
}
filesize = filemax;
no_decompression = no_decompression_bak;
}
grub_close();
printf_debug0 ("Filesize is 0x%lX\n", (unsigned long long)filesize);
return (filesize>>32)?(unsigned long long)-1:filesize;
}
if (replace)
{
p = replace;
if (*p++ == '*')
{
safe_parse_maxint (&p, &len_r);
errnum = 0;
}
if (! len_r)
{
#if 0
if (*replace == '\"')
{
for (i = 0; i < 128 && (r[i] = *(++replace)) != '\"'; i++);
}else{
for (i = 0; i < 128 && (r[i] = *(replace++)) != ' ' && r[i] != '\t'; i++);
}
r[i] = 0;
replace = (char*)r;
len_r = parse_string (replace);
#else
wee_skip_to(replace,SKIP_WITH_TERMINATE);
c = *replace;
len_r = parse_string (replace);
if (c == '\"')
++replace,len_r -= 2;
#endif
}
else
{
replace = (char*)(grub_size_t)(unsigned long long)len_r;
len_r = Hex?Hex:8;
}
if ((int)len_r <= 0)
{
return ! (errnum = ERR_BAD_ARGUMENT);
}
}
if (! grub_open (arg))
return 0;
if (length > filemax)
length = filemax;
filepos = skip;
if (locate)
{
#if 0
if (*locate == '\"')
{
for (i = 0; i < 128 && (s[i] = *(++locate)) != '\"'; i++);
}else{
for (i = 0; i < 128 && (s[i] = *(locate++)) != ' ' && s[i] != '\t'; i++);
}
s[i] = 0;
len_s = parse_string ((char *)s);
locate = s;
#else
wee_skip_to(locate,SKIP_WITH_TERMINATE);
c = *locate;
len_s = parse_string (locate);
if (c == '\"')
++locate,len_s -= 2;
#endif
if (len_s == 0 || len_s > 16)
{
grub_close();
return ! (errnum = ERR_BAD_ARGUMENT);
}
//j = skip;
grub_memset ((char *)(SCRATCHADDR), 0, 32);
length += skip;
unsigned long long k,l;
for (i = 0,j = skip; ; j += 16)
{
len = 0;
if (j < length)
{
len = grub_read ((unsigned long long)(grub_size_t)(char *)(SCRATCHADDR + 16), 16, 0xedde0d90);
}
if (len < 16)
{
k = j > length ? 16 - (j - length):16 + len;
grub_memset ((char *)(SCRATCHADDR + (int)k), 0, 32 - (int)k);
}
if (j>length)
l = 16 - (j - length);
else
l = 16;
if (j != skip)
{
while (i < l)
{
k = j - 16 + i;
if ((locate_align == 1 || ! ((grub_size_t)k % (grub_size_t)locate_align))
&& strncmpx (locate, (char *)(SCRATCHADDR + (grub_size_t)i), len_s,locate_ignore_case) == 0)
{
/* print the address */
if (!replace || debug > 1)
grub_printf (" %lX", (unsigned long long)k);
/* replace strings */
if (replace)
{
unsigned long long filepos_bak = filepos;
filepos = k;
/* write len_r bytes at string replace to file!! */
grub_read ((unsigned long long)(grub_size_t)replace,len_r, 0x900ddeed);
i += len_r;
// if (filepos < filepos_bak)
filepos = filepos_bak;
}
else
i += len_s;
ret++;
Hex = k;
if (number <= ret)
{
len = 0;
break;
}
}
else
i++;
}
if (quit_print)
break;
if (len == 0)
{
sprintf(ADDR_RET_STR,"0x%x",Hex);
break;
}
i -= 16;
}
grub_memmove ((char *)SCRATCHADDR, (char *)(SCRATCHADDR + 16), 16);
}
}else if (Hex == (++ret)) /* a trick for (ret = 1, Hex == 1) */
{
j = 16/* - (skip & 0xF)*/;
if (j > length)
j = length;
while ((len = grub_read ((unsigned long long)(grub_size_t)&s, j, 0xedde0d90)))
{
hexdump(skip,(char*)&s,len);
if (quit_print)
break;
skip += len;
length -= len;
j = (length >= 16)?16:length;
}
}else
for (j = 0; j < length && grub_read ((unsigned long long)(grub_size_t)&c, 1, 0xedde0d90) && c; j++)
{
grub_putchar (c, 255);
if (quit_print)
break;
}
grub_close ();
return ret;
}
static struct builtin builtin_cat =
{
"cat",
cat_func,
BUILTIN_MENU | BUILTIN_CMDLINE | BUILTIN_SCRIPT | BUILTIN_HELP_LIST | BUILTIN_IFTITLE,
"cat [--hex] [--skip=S] [--length=L] [--locate[i]=STRING] [--replace=REPLACE]\n"
"\t [--locate-align=A] [--number=n] FILE",
"Print the contents of the file FILE,"
"Or print the locations of the string STRING in FILE,"
"--replace replaces STRING with REPLACE in FILE."
"--number use with --locate,the max number for locate",
};
void map_to_svbus (grub_efi_physical_address_t address);
void
map_to_svbus (grub_efi_physical_address_t address)
{
#if 0
int i, j;
//复制映射插槽
for (i = 0, j = 0; i < DRIVE_MAP_SIZE; i++)
{
if (drive_map_slot_empty (disk_drive_map[i])) //判断驱动器映像插槽是否为空 为空,返回1
break;
//滤除软盘
if (disk_drive_map[i].from_drive < 0x80)
continue;
//复制映射插槽
grub_memmove ((char *)((char *)(grub_size_t)address + j*24), (char *)&disk_drive_map[i], 24);
j++;
}
#else
struct grub_disk_data *d; //磁盘数据
struct drive_map_slot *s = (struct drive_map_slot *)(grub_size_t)address;
for (d = disk_data; d; d = d->next) //从设备结构起始查; 只要设备存在,并且驱动器号不为零;
{
if (d->sector_count && d->drive >= 0x80) //如果是映射驱动器, 不是软盘
{
s->from_drive = d->drive;
s->to_drive = d->to_drive;
s->max_head = 0xfe; //避免旧svbus蓝屏。支持碎片的新版本不需要
s->fragment = d->fragment;
if (d->from_log2_sector == 0xb)
s->from_cdrom = 1;
s->to_sector = 0x3f; //避免旧svbus蓝屏。支持碎片的新版本不需要。
s->start_sector = d->start_sector;
s->sector_count = d->sector_count;
s++;
}
}
#endif
//复制碎片插槽
grub_memmove ((char *)((char *)(grub_size_t)address + 0x148), (char *)&disk_fragment_map, FRAGMENT_MAP_SLOT_SIZE);
}
//使用于get_efi_device_boot_path,find_specified_file,chainloader_func,command_func,uuid_func
static char chainloader_file[256];
int find_specified_file (int drive, int partition, char* file);
int
find_specified_file (int drive, int partition, char* file)
{
int val;
int tem_drive = current_drive;
int tem_partition = current_partition;
if (drive >= 0xa0) //光盘
sprintf (chainloader_file, "(%d)%s\0", drive, file);
else if (drive >= 0x80) //硬盘
sprintf (chainloader_file, "(hd%d,%d)%s\0", drive & 0x7f, partition >> 16, file);
else //软盘
sprintf (chainloader_file, "(%d)%s\0", drive, file);
putchar_hooked = (unsigned char*)1; //不打印ls_func信息
val = ls_func (chainloader_file, 1);
putchar_hooked = 0;
current_partition = tem_partition;
current_drive = tem_drive;
return val;
}
int get_efi_device_boot_path (int drive, int flags);
int
get_efi_device_boot_path (int drive, int flags) //获得硬盘/光盘启动分区(入口)
{
struct grub_part_data *p;
char tmp[512];
char *cache = 0;
int self_locking = 0;
int k = 0;
part_size = 0;
int v_mbr, v_bpb, i;
static grub_efi_physical_address_t address;
grub_efi_status_t status;
grub_efi_boot_services_t *b = grub_efi_system_table->boot_services;
cache = grub_zalloc (0x800); //分配缓存
if (!cache)
return 0;
if (drive >= 0x80 && drive <= 0x8f) //硬盘
{
for (p = partition_info; p; p = p->next)
{
if (p->drive != drive)
continue;
if (p->partition_type != 0xee && !self_locking) //如果是MBR分区类型, 首先启动活动分区
{
struct grub_part_data *p_back = p;
self_locking = 1;
for (; p && p->drive == drive; p = p->next) //查找活动分区
{
if (p->partition_activity_flag == 0x80)
if (find_specified_file(drive, p->partition, EFI_REMOVABLE_MEDIA_FILE_NAME) == 1)
goto complete;
}
p = p_back;
}
if (find_specified_file(drive, p->partition, EFI_REMOVABLE_MEDIA_FILE_NAME) == 1) //搜索文件 bootx64.efi
goto complete;
}
grub_free (cache);
printf_warning("bootx64.efi not found\n");
return 0;
complete:
part_data = p;
part_addr = p->partition_start;
part_size = p->partition_size;
grub_free (cache);
return 1;
}
else //光盘
{
cdrom_volume_descriptor_t *vol = (cdrom_volume_descriptor_t *)cache;
eltorito_catalog0_t *catalog = NULL;
if (get_diskinfo (drive, &tmp_geom, 0)) //获得当前驱动器的磁盘信息
{
errnum = ERR_NO_DISK;
goto fail_free_cache;
}
if (flags)
{
if (!grub_open (chainloader_file_orig))
goto fail_free_cache;
}
else
{
grub_sprintf (chainloader_file, "(0x%X)+0x%lX\0", drive, (unsigned long long)tmp_geom.total_sectors);
if (! grub_open (chainloader_file))
goto fail_free_cache;
}
filepos = 17 * 0x800;
grub_read ((unsigned long long)(grub_size_t) vol, 0x800, 0xedde0d90); //读引导描述符扇区(17,规定值,不变),0x800字节
if (vol->unknown.type != CDVOL_TYPE_STANDARD || //启动记录卷,类型 CDVOL_TYPE_STANDARD=0
grub_memcmp ((const char *)vol->boot_record_volume.system_id, (const char *)CDVOL_ELTORITO_ID, //启动记录卷,系统id CDVOL_ELTORITO_ID="EL TORITO SPECIFICATION"
sizeof (CDVOL_ELTORITO_ID) - 1) != 0)
goto fail_close_free_cache;
catalog = (eltorito_catalog0_t *) cache;
filepos = (grub_size_t)(*((grub_efi_uint32_t*) vol->boot_record_volume.elt_catalog)) * 0x800; //启动目录文件所在扇区*0x800
grub_read ((unsigned long long)(grub_size_t) catalog, 0x800, 0xedde0d90); //读启动目录文件扇区
if (catalog[0].indicator1 != ELTORITO_ID_CATALOG) //0x01
goto fail_close_free_cache;
//查找UEFI启动入口
for (k = 0; k < 8; k++)
{
if (catalog[k].indicator1 == 0x91 || catalog[k].platform_id == 0xEF || catalog[k].section_entries == 1)
break;
}
if (k >= 8)
k = 0;
if (catalog[k].indicator88 != ELTORITO_ID_SECTION_BOOTABLE)
goto fail_close_free_cache;
#define BS ((struct master_and_dos_boot_sector *)tmp)
filepos = (grub_size_t)catalog[k].lba * 0x800; //避免64位被截断 2023-04-24
grub_read ((unsigned long long)(grub_size_t) tmp, 0x200, 0xedde0d90); //读启动映像mbr
v_mbr = 1;
v_bpb = 1;
putchar_hooked = (unsigned char*)1; //不在屏幕显示信息
v_mbr = probe_mbr (BS, 0, 1, 0);
v_bpb = probe_bpb(BS);
putchar_hooked = 0;
if (BS->boot_signature != 0xAA55 //没有磁盘签名
|| (v_mbr && v_bpb)) //即无分区表,又无BPB表
goto fail_close_free_cache;
if (!v_mbr || v_mbr == 4) //如果有分区表(排除起始扇区为零)
{
for (i = 0; i < 4; i++) //
{
if (BS->P[i].total_sectors)
{
cd_Image_part_start = BS->P[i].start_lba;
cd_Image_disk_size = BS->P[i].start_lba + BS->P[i].total_sectors;
break;
}
}
}
else if (!v_bpb) //如果有BPB表
{
if (BS->total_sectors_short)