Skip to content

Commit

Permalink
. 外部命令可以尾续压缩文件
Browse files Browse the repository at this point in the history
  • Loading branch information
yaya2007 committed Jul 16, 2023
1 parent 8d02b20 commit 2b155c9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 24 deletions.
6 changes: 6 additions & 0 deletions ChangeLog_UEFI.txt
@@ -1,4 +1,10 @@
更新说明:
2023-07-15 (yaya)
外部命令可以尾续压缩文件

2023-07-08 (yaya)
修正缺失MBR逻辑分区磁盘ID

2023-07-01 (yaya)
修正缺失GPT磁盘GUID

Expand Down
15 changes: 7 additions & 8 deletions menu.lst
Expand Up @@ -26,7 +26,7 @@ default 1
#if %a%>=801 && font --font-high=40 /efi/grub/menu40.hex
#if %a%<=800 && font /efi/grub/unifont.hex.gz && splashimage /efi/grub/lt.jpg

#判断启动环境:calc *0x8277 > nul ;; set 启动环境=%@retval% 启动环境:0/32/64=bios/uefi_x86/uefi_x64
#判断启动环境:if %@uefi%==64 0/32/64=bios/uefi_x86/uefi_x64

#设置菜单框
#setmenu --box x=4 w=60 y=6 h=9 l=2
Expand Down Expand Up @@ -117,19 +117,18 @@ uuid (hdx,y)
chainloader /efi/grub/ext/ntloader initrd=/efi/grub/ext/initrd.lz1 uuid=%?%

title 使用外部命令 NTBOOT 启动 WIM/VHD (方案1)
map --mem --no-hook /efi/grub/ext/NTBOOT (hd)
(hd-1,0)/setbcd (hdx,y)/boot/imgs/winpe.wim
find /efi/grub/ext/ntboot | set bd=
find --set-root /boot/imgs/winpe.wim
%bd%/efi/grub/ext/ntboot /boot/imgs/winpe.wim
boot

title 使用外部命令 NTBOOT 启动 WIM/VHD (方案2)
map --mem --no-hook /efi/grub/ext/NTBOOT (hd)
find --set-root /boot/imgs/winpe.vhd
(hd-1,0)/setbcd /boot/imgs/winpe.vhd winload=/windows/system32/boot/winload.efi minint=1
find /boot/imgs/winpe.vhd | set bd=
/efi/grub/ext/ntboot %bd%/boot/imgs/winpe.vhd winload=/Windows/System32/boot/winload.efi minint=1
boot

title 使用外部命令 NTBOOT 启动 Windows 系统
map --mem --no-hook /efi/grub/ext/NTBOO (hd)
(hd-1,0)/setbcd (hdx,y)/.win
/efi/grub/ext/ntboot (hdx,y)/win
boot

title 命令行
Expand Down
48 changes: 43 additions & 5 deletions stage2/builtins.c
Expand Up @@ -3897,10 +3897,7 @@ command_func (char *arg, int flags)
}

// program = (char *)((grub_size_t)(tmp + 4095) & ~4095); /* 4K align the program 4K对齐程序*/ //程序缓存
// psp = (char *)((grub_size_t)(program + prog_len + 16) & ~0x0F); //psp地址 向上舍入,否则覆盖program数据
program = tmp;
psp = (char *)((grub_size_t)(program + prog_len + 16 + 512) & ~0x0F); //psp地址 向上舍入,否则覆盖program数据
unsigned long long *end_signature = (unsigned long long *)(program + filemax - (unsigned long long)8); //程序结束签名地址
if (p_exec == NULL)
{
/* read file to buff and check exec signature. 读取文件到程序缓存并检查exec签名*/
Expand All @@ -3909,6 +3906,42 @@ command_func (char *arg, int flags)
if (! errnum)
errnum = ERR_EXEC_FORMAT;
}
//如果不是批处理
if (*(unsigned int *)program != BAT_SIGN
&& (*(unsigned long long *)program & 0xFFFFFFFFFFFFFFULL) != UTF8_BAT_SIGN)
{
char *p = program;
unsigned long long size = filemax;
//查找外部命令签名
while ((long long)size > 0)
{
if (*(unsigned long long *)p == 0xBCBAA7BA03051805ULL)
break; //查到

size -= sizeof(grub_size_t);
p += sizeof(grub_size_t);
}

unsigned long long len = p - program;
if ((long long)size <= 0) //没有签名,失败
{
errnum = ERR_EXEC_FORMAT;
}
else if (len > 8) //存在尾续文件
{
bat_md_start = (grub_size_t)(program + len + 8); //尾续文件起始
bat_md_count = filemax - (len + 8); //尾续文件尺寸
bat_md_count = (bat_md_count + 511) & (-512); //对齐512
char *tmp0 = grub_memalign(512, bat_md_count); //给尾续文件分配新地址
grub_memmove64((unsigned long long)(grub_size_t)tmp0,bat_md_start,bat_md_count); //复制(未解压缩)尾续文件到新地址
bat_md_start = (unsigned long long)(grub_size_t)tmp0 >> 9;
bat_md_count >>= 9;

filemax = len + 8; //修正外部命令尺寸
prog_len = filemax; //修正
}
}
#if 0
else if (*end_signature == 0x85848F8D0C010512ULL)
{
if (filemax < 512 || filemax > 0x80000)
Expand All @@ -3925,7 +3958,7 @@ command_func (char *arg, int flags)
{
errnum = ERR_EXEC_FORMAT;
}

#endif
grub_close ();
if (errnum)
{
Expand All @@ -3937,6 +3970,11 @@ command_func (char *arg, int flags)
{
grub_memmove(program,p_exec->data,prog_len);
}

// psp = (char *)((grub_size_t)(program + prog_len + 16) & ~0x0F); //psp地址 向上舍入,否则覆盖program数据
psp = (char *)((grub_size_t)(program + prog_len + 16 + 512) & ~0x0F); //psp地址 向上舍入,否则覆盖program数据
unsigned long long *end_signature = (unsigned long long *)(program + filemax - (unsigned long long)8); //程序结束签名地址

if (*end_signature == 0xBCBAA7BA03051805ULL)
{
// if (*(unsigned long long *)(program + prog_len - 0x20) == 0x646E655F6E69616D) //bios模式:新版本标记 main_end
Expand Down Expand Up @@ -14636,7 +14674,7 @@ static int grub_exec_run(char *program, char *psp, int flags)
grub_memmove((void *)bat_md_start,(void *)(program + size),PI->proglen - size);
sprintf(p_bat_array->md,"(md,0x%x,0x%x)",bat_md_start,bat_md_count);
bat_md_start >>= 9;
bat_md_count >>= 9;
bat_md_count = ((bat_md_count + 511) & (-512)) >> 9;
}

//判断回车换行模式
Expand Down
12 changes: 6 additions & 6 deletions stage2/fsys_initrd.c
Expand Up @@ -44,13 +44,13 @@ static int test_file(const char *dirname)
{
if (print_possibilities)
{
if (substring (dirname,(const char*)(grub_size_t)cur_file.name, 1) <= 0)
if (substring (dirname,(const char*)cur_file.name, 1) <= 0)
{
print_a_completion ((char*)(grub_size_t)cur_file.name + path_len, 1);
print_a_completion ((char*)cur_file.name + path_len, 1);
return 1;
}
}
else if (substring (dirname, (const char*)(grub_size_t)cur_file.name, 1) == 0)
else if (substring (dirname, (const char*)cur_file.name, 1) == 0)
{
filemax = cur_file.size;
return 2;
Expand All @@ -71,8 +71,8 @@ static grub_u32_t cpio_file(struct cpio_header *hdr)
return 0;
}
hdr_sz = cpio_image_align(sizeof(struct cpio_header) + namesize);
cur_file.base = (grub_u32_t)(grub_size_t)hdr + hdr_sz;
cur_file.name = (grub_u32_t)(grub_size_t)hdr + sizeof(struct cpio_header);
cur_file.base = (grub_size_t)hdr + hdr_sz;
cur_file.name = (grub_size_t)hdr + sizeof(struct cpio_header);
cur_file.isdir &= CPIO_MODE_DIR;
cur_file.name_size = namesize;

Expand Down Expand Up @@ -200,7 +200,7 @@ int initrdfs_dir (char *dirname)
while(pos < initrdfs_size)
{
cur_file.base = initrdfs_base + pos;
cur_file.size = (*(grub_u32_t*)(grub_size_t)cur_file.base == BAT_SIGN)?grub_strlen((char*)(grub_size_t)cur_file.base):initrdfs_size - pos;
cur_file.size = (*(grub_u32_t*)cur_file.base == BAT_SIGN)?grub_strlen((char*)cur_file.base):initrdfs_size - pos;
test = test_file(dirname);
if (test) found = 1;
if (test == 2) break;
Expand Down
13 changes: 8 additions & 5 deletions stage2/fsys_initrd.h
Expand Up @@ -4,10 +4,13 @@
#include "cpio.h"
struct initrdfs_file
{
grub_u32_t base;
grub_u32_t size;
grub_u32_t name;
grub_u16_t isdir;
grub_u16_t name_size;
// grub_u32_t base;
// grub_u32_t size;
// grub_u32_t name;
grub_size_t base; //基址 =hdr+对齐的(hdr尺寸+hdr->c_namesize)
grub_size_t size; //尺寸 =(hdr->c_filesize)
grub_size_t name; //名称 =hdr+hdr尺寸
grub_u16_t isdir; //属性 =hdr->c_mode & 0040000 0/1=文件/目录
grub_u16_t name_size; //名称尺寸 =hdr->c_namesize
};
#endif /* _INITRDFS_H_ */

0 comments on commit 2b155c9

Please sign in to comment.