Skip to content

Commit

Permalink
. 修正configfile函数文件名溢出。
Browse files Browse the repository at this point in the history
  碎片数由39增加到126。
  修正分区签名丢失。
  修正BOOTIA32.EFI启动时进不了菜单而重启。
  改进color函数帮助信息。issues #414
  避免分区项空洞。issues #416
  • Loading branch information
yaya2007 committed Jun 23, 2023
1 parent a98c09a commit c539d76
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 62 deletions.
8 changes: 8 additions & 0 deletions ChangeLog_UEFI.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
更新说明:
2023-06-23 (yaya)
修正configfile函数文件名溢出。
碎片数由39增加到126。
修正分区签名丢失。
修正BOOTIA32.EFI启动时进不了菜单而重启。
改进color函数帮助信息。issues #414
避免分区项空洞。issues #416

2023-06-06 (yaya)
适应gcc-11高版本编译。

Expand Down
43 changes: 22 additions & 21 deletions stage2/asm.S
Original file line number Diff line number Diff line change
Expand Up @@ -124,30 +124,14 @@ VARIABLE(force_lba) //0x8211 强制LBA uefi未使用
VARIABLE(version_string) //0x8212
.string VERSION //0.97\0(hd0,0)

VARIABLE(config_file) //0x8217 配置文件 最长29字节
VARIABLE(config_file) //0x821e 配置文件 最长72字节
.string "/efi/grub/menu.lst"
//G4D情况:0x8234占用
//G4D情况:0x8244占用 0x824e-0x8259 18字节未使用!!
. = EXT_C(main) + 0x38
VARIABLE(ext_timer) //0x8238 外部定时器 int*
.long 0
.long 0
// .extent 0
//#if defined(__i386__)
// .long 0
//#endif
VARIABLE(grub_timeout) //0x8240 倒计时 int
.long -1

. = EXT_C(main) + 0x60

VARIABLE(hotkey_func) //int* //0x8260 外置热键功能
.long 0
.long 0
// .extent 0
//#if defined(__i386__)
// .long 0
//#endif

. = EXT_C(main) + 0x68 //0x8268

Expand Down Expand Up @@ -184,7 +168,8 @@ VARIABLE(menu_num_ctrl) //unsigned char menu_num_ctrl[4] 4字节
#endif
#undef GRUB4DOS_INT_VER

.long 0 //0x827c 未使用
VARIABLE(grub_timeout) //0x827c 倒计时 int
.long -1

. = EXT_C(main) + 0x80 //0x8280

Expand Down Expand Up @@ -357,6 +342,12 @@ VARIABLE(current_x_resolution) //0x83
.long 0

. = EXT_C(main) + 0x150 //0x8350

VARIABLE(ext_timer) //0x8350 外部定时器 int*
.long 0
.long 0
.long 0 //未使用
.long 0 //未使用

. = EXT_C(main) + 0x160 //0x8360

Expand All @@ -381,17 +372,27 @@ VARIABLE(cdrom_orig) //光盘
VARIABLE(g4e_data) //0x8378 固定数据区 char *
.long 0, 0

. = EXT_C(main) + 0x180 //0x8380

VARIABLE(preset_menu) //0x8380 预置菜单 char * 原0x307FFC
.long 0, 0

VARIABLE(menu_mem) //0x8388 菜单 char *
.long 0, 0

. = EXT_C(main) + 0x190 //0x8390

.long 0
.long 0
.long 0
.long 0

. = EXT_C(main) + 0x200 //0x8400 以上预留

ENTRY(disk_fragment_map) .space FRAGMENT_MAP_SLOT_SIZE //映射碎片插槽 280

. = EXT_C(main) + 0x480 //0x8680
//ENTRY(disk_fragment_map) .space FRAGMENT_MAP_SLOT_SIZE //映射碎片插槽 800
VARIABLE(disk_fragment_map) //映射碎片插槽 char *
.long 0, 0
// . = EXT_C(main) + 0xa00 //0x8c00

//ENTRY(disk_drive_map) .space [DRIVE_MAP_SIZE + 1] * DRIVE_MAP_SLOT_SIZE //磁盘驱动器映射插槽 70*9=3c0

Expand Down
59 changes: 34 additions & 25 deletions stage2/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ disk_read_print_func (unsigned long long sector, unsigned int offset, unsigned l

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];
//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;
Expand Down Expand Up @@ -325,7 +327,6 @@ static int
blocklist_func (char *arg, int flags)
{
char *dummy = NULL;
int i;
unsigned long long err;
#ifndef NO_DECOMPRESSION
int no_decompression_bak = no_decompression;
Expand All @@ -335,13 +336,25 @@ blocklist_func (char *arg, int flags)
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;
Expand Down Expand Up @@ -829,7 +842,7 @@ map_to_svbus (grub_efi_physical_address_t address)
#endif

//复制碎片插槽
grub_memmove ((char *)((char *)(grub_size_t)address + 0x148), (char *)&disk_fragment_map, 0x280);
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
Expand Down Expand Up @@ -1009,6 +1022,7 @@ get_efi_device_boot_path (int drive, int flags) //获得硬盘/光盘启动分
grub_close ();
grub_sprintf (chainloader_file, "(md)0x%X+0x%X (0x%x)\0", (grub_size_t)((address >> 9) + cd_Image_part_start), cd_Image_disk_size - cd_Image_part_start, 0x60 + cd_map_count);
map_func (chainloader_file, 1);
efi_call_2 (b->free_pages, address, pages);
}
#undef BS
cd_boot_entry = k;
Expand Down Expand Up @@ -2030,15 +2044,17 @@ static struct builtin builtin_color =
"If you omit HELPTEXT and/or HEADING, then NORMAL is used.\n"
"1. Assign colors by target, the order can not be messed up.\n"
" The color can be replaced by a placeholder n.\n"
"e.g. color 0x888800000000 0x888800ffff00 0x888800880000 0x88880000ff00. (64 bit number.)\n"
"e.g. color 0x0000888800000000 0x0000888800ffff00 0x0000888800880000 0x000088880000ff00. (64 bit number."
" The upper 32 bits are the background color, and the lower 32 bits are the foreground color.)\n"
"2. Can assign colors to a specified target. NORMAL should be in the first place.\n"
"e.g. color normal=0x888800000000. (The rest is the same as NORMAL.)\n"
"e.g. color normal=0x4444440000ffff helptext=0xff0000 highlight=0x00ffff heading=0xffff00\n"
" border=0x00ff00. (Background color from NORMAL.)\n"
"e.g. color standard=0xFFFFFF. (Change the console color.)\n"
"e.g. color normal=0x00888800000000. (The rest is the same as NORMAL.)\n"
"e.g. color normal=0x004444440000ffff helptext=0x00ff0000 highlight=0x0000ffff heading=0xffff00"
" border=0x0000ff00. (Background color from NORMAL.)\n"
"e.g. color standard=0x00FFFFFF. (Change the console color.)\n"
"e.g. color --64bit 0x30. (Make numbers less than 0x100 treated in 64-bit color.)\n"
"Display color list if no parameters.\n"
"Use 'echo -rrggbb' to view colors."
"Use 'echo -rrggbb' to view colors.\n"
"note that if in graphics hi-res mode, the background colour for normal text and help text will be ignored and will be set to transparent."
};


Expand Down Expand Up @@ -2084,9 +2100,11 @@ configfile_func (char *arg, int flags)
arg = chainloader_file_orig;
nul_terminate (arg);
/* check possible filename overflow */
if (grub_strlen (arg) >= 0x49) //0x8217-0x825f
return ! (errnum = ERR_WONT_FIT);

if (grub_strlen (arg) >= 0x49) //0x821e-0x825f
{
printf_errinfo ("The full path of the configuration file should <= 72\n");
return ! (errnum = 0x1234);
}
/* Check if the file ARG is present. */
if (! grub_open (arg))
{
Expand Down Expand Up @@ -7327,10 +7345,7 @@ add_part_data (int drive)
p->partition_entry = *next_partition_entry;
p->partition_ext_offset = *next_partition_ext_offset;
p->partition_activity_flag = partition_activity_flag;
if (p->partition_type == 0xee)
grub_memcpy (&p->partition_signature, &partition_signature, 16);
else
grub_memcpy (&p->partition_signature, &d->disk_signature, 16);
grub_memcpy (&p->partition_signature, &partition_signature, 16);

p->next = 0;
if (!partition_info)
Expand Down Expand Up @@ -7410,7 +7425,6 @@ map_func (char *arg, int flags) //对设备进行映射 返回: 0/1=失败/成
int read_only = 0; //只读 若read_Only=1,则同时unsafe_boot=1
unsigned char from_log2_sector = 9;
unsigned char partmap_type = 0;
unsigned char disk_signature[16];
// unsigned long long sectors_per_track = -1ULL;
// unsigned long long heads_per_cylinder = -1ULL;
// int add_mbt = -1;
Expand Down Expand Up @@ -8353,8 +8367,6 @@ struct drive_map_slot
if (probe_bpb(mbr1)) //没有bpb
goto fail_close_free;
}
grub_memset (&disk_signature, 0, 16);
*(unsigned int *)disk_signature = PC_DISK_SIG (next_partition_buf); //MBR磁盘签名
goto get_info_ok;

get_gpt_info:
Expand All @@ -8373,7 +8385,6 @@ struct drive_map_slot
if (gpt->hdr_sig != GPT_HDR_SIG) //如果签名不符
goto fail_close_free;
}
grub_memmove(&disk_signature, &gpt->hdr_uuid, 16); //GPT磁盘签名
goto get_info_ok;
}
else //软盘
Expand Down Expand Up @@ -8770,8 +8781,6 @@ struct drive_map_slot
d->fragment = (blklst_num_entries > 1);
d->read_only = read_only;
d->vhd_disk = vhd_disk;
if (from >= 0x80 && from <= 0x8f)
grub_memmove(&d->disk_signature, &disk_signature, 16); //磁盘签名

if (vhd_file_name)
{
Expand Down
11 changes: 11 additions & 0 deletions stage2/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,7 @@ copy_grub4dos_self_address (void)
grub_efi_status_t status; //状态
grub_efi_boot_services_t *b; //引导服务
b = grub_efi_system_table->boot_services; //系统表->引导服务
grub_size_t i, address;

if (min_con_mem_start > 0x9F000)
return;
Expand All @@ -2058,6 +2059,14 @@ copy_grub4dos_self_address (void)
else
grub4dos_self_address = 0x9F000;

address = grub4dos_self_address;
for (i = grub4dos_self_address - 0x1000; i > 0x40000 ; i -= 0x1000)
{
if (*(unsigned long long *)(grub_size_t)(i + 0x100) == 0x4946453442555247) //"GRUB4EFI"
address = i;
}
//清除残留的钩子 避免测试中启动镜像后重启,热键失效。 注意不要碰触保留内存! 2023-06-21
grub_memset ((void *)address, 0, grub4dos_self_address - address + 0x1000);
status = efi_call_4 (b->allocate_pages, GRUB_EFI_ALLOCATE_ADDRESS,
GRUB_EFI_RUNTIME_SERVICES_DATA, 1, &grub4dos_self_address); //(分配页,分配类型=指定地址,存储类型=运行时数据,页数=1,返回分配地址)
if (status)
Expand Down Expand Up @@ -2212,6 +2221,7 @@ char *CMD_RUN_ON_EXIT;
char *SCRATCHADDR;
char *mbr;
char *disk_buffer;
struct fragment_map_slot *disk_fragment_map;
//char *

void grub_console_init (void);
Expand Down Expand Up @@ -2263,6 +2273,7 @@ grub_init (void)
SCRATCHADDR = grub_malloc (0x1000); //临时
mbr = grub_malloc (0x1000); //mbr
disk_buffer = grub_malloc (0x1000); //磁盘缓存
disk_fragment_map = grub_zalloc (FRAGMENT_MAP_SLOT_SIZE); //碎片插槽
//buffer=grub_malloc (byte) 分配内存
//buffer=grub_zalloc (byte) 分配内存, 并清零
//buffer=grub_memalign (align,byte) 对齐分配内存
Expand Down
3 changes: 2 additions & 1 deletion stage2/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ unsigned long long console_color_64bit[COLOR_STATE_MAX] = {
/* represents the user defined colors for heading line */
[COLOR_STATE_HEADING] = 0xAAAAAA,
/* represents the user defined colors for notes */
[COLOR_STATE_BORDER] = 0x3399
// [COLOR_STATE_BORDER] = 0x3399
[COLOR_STATE_BORDER] = 0xAAAAAA

};

Expand Down
15 changes: 6 additions & 9 deletions stage2/disk_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,8 @@ static int next_gpt_slice(void)

if (PI->starting_lba == 0LL /*|| PI->starting_lba > 0xFFFFFFFFL*/)
{
// errnum = ERR_NO_PART;
return 0;
// return 0;
goto redo; //避免分区项空洞 2023-06-20
}

//skip MS_Reserved Partition
Expand Down Expand Up @@ -804,8 +804,8 @@ next_pc_slice (void)
*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分区签名
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 ... */

Expand Down Expand Up @@ -3043,7 +3043,7 @@ grub_efidisk_readwrite (int drive, grub_disk_addr_t sector,
if (df->fragment)
{
//从碎片插槽查找Form驱动器
q = &disk_fragment_map;
q = (struct fragment_map_slot *)&disk_fragment_map;
q = fragment_map_slot_find (q, from_drive);
//确定Form扇区起始在哪个碎片
data = (struct fragment *)&q->fragment_data;
Expand Down Expand Up @@ -3149,10 +3149,7 @@ partition_info_init (struct efidisk_data *devices)
p->partition_activity_flag = partition_activity_flag;
p->next = partition_info; //0 dfb0110 dfb00e0 dfb00b0 dfb0080 dfb0050 dfaff90 dfaff60

if (p->partition_type == 0xee)
grub_memcpy (&p->partition_signature, &partition_signature, 16);
else
grub_memcpy (&p->partition_signature, &d->disk_signature, 16);
grub_memcpy (&p->partition_signature, &partition_signature, 16);
//从efidisk_data中查找有关信息
for (d1 = devices; d1; d1 = d1->next)
{
Expand Down
9 changes: 6 additions & 3 deletions stage2/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@
//#define DRIVE_MAP_SLOT_SIZE 0x70

/* The fragment of the drive map. */
#define DRIVE_MAP_FRAGMENT 0x27
//#define DRIVE_MAP_FRAGMENT 0x27
#define DRIVE_MAP_FRAGMENT 0x7E

#define FRAGMENT_MAP_SLOT_SIZE 0x280
//#define FRAGMENT_MAP_SLOT_SIZE 0x280
#define FRAGMENT_MAP_SLOT_SIZE 0x800

/* The size of the key map. */
#define KEY_MAP_SIZE 128
Expand Down Expand Up @@ -5737,7 +5739,8 @@ struct fragment

//extern struct drive_map_slot vpart_drive_map[DRIVE_MAP_SIZE + 1];
//extern struct drive_map_slot disk_drive_map[DRIVE_MAP_SIZE + 1];
extern struct fragment_map_slot disk_fragment_map;
//extern struct fragment_map_slot disk_fragment_map;
extern struct fragment_map_slot *disk_fragment_map;
//extern char disk_buffer[0x1000];
extern char *disk_buffer;
//extern int drive_map_slot_empty (struct drive_map_slot item);
Expand Down
5 changes: 2 additions & 3 deletions stage2/stage2.c
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,6 @@ cmain (void)
* menu-init command set.
*/
/* Run menu-specific commands before any other menu entry commands. */
;
{
static char *old_entry = NULL;
static char *heap = NULL; heap = CONFIG_ENTRIES + config_len;
Expand Down Expand Up @@ -2312,8 +2311,8 @@ cmain (void)
use_preset_menu = 0; /* Disable the preset menu. */ //禁用预设菜单
// pxe_restart_config = 1; /* pxe_detect will use configfile to run menu */
/* go ahead and make sure the terminal is setup */ //继续前进,确保终端的安装
if (current_term->startup)
(*current_term->startup)();
// if (current_term->startup) 无用 2023-06-13
// (*current_term->startup)();

if (! num_entries)
{
Expand Down

0 comments on commit c539d76

Please sign in to comment.