From c308892db0c50170621f69d6f3785f1ce10381ad Mon Sep 17 00:00:00 2001 From: longpanda Date: Tue, 10 Jan 2023 12:23:35 +0800 Subject: [PATCH] Show file checksum menu title according to the existence of checksum value. --- .../grub-2.04/grub-core/ventoy/ventoy_cmd.c | 222 +++++++++++++++--- .../grub-2.04/grub-core/ventoy/ventoy_def.h | 2 + INSTALL/grub/checksum.cfg | 11 +- 3 files changed, 191 insertions(+), 44 deletions(-) diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c index 83aa105f..7b572794 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_cmd.c @@ -158,7 +158,9 @@ const char *g_menu_prefix[img_type_max] = "iso", "wim", "efi", "img", "vhd", "vtoy" }; -static const char *g_lower_chksum_name[] = { "md5", "sha1", "sha256", "sha512" }; +static const char *g_lower_chksum_name[VTOY_CHKSUM_NUM] = { "md5", "sha1", "sha256", "sha512" }; +static int g_lower_chksum_namelen[VTOY_CHKSUM_NUM] = { 3, 4, 6, 6 }; +static int g_chksum_retlen[VTOY_CHKSUM_NUM] = { 32, 40, 64, 128 }; static int g_vtoy_secondary_need_recover = 0; @@ -6305,36 +6307,6 @@ static grub_err_t ventoy_cmd_load_menu_lang(grub_extcmd_context_t ctxt, int argc VENTOY_CMD_RETURN(0); } -static grub_err_t ventoy_cmd_vtoychksum_exist(grub_extcmd_context_t ctxt, int argc, char **args) -{ - int cnt; - char c; - char *pos = NULL; - - (void)ctxt; - (void)argc; - - cnt = ventoy_str_chrcnt(args[1], '/'); - if (cnt > 1) - { - pos = grub_strrchr(args[1], '/'); - c = *pos; - *pos = 0; - if (ventoy_check_file_exist("%s%s/VENTOY_CHECKSUM", args[0], args[1])) - { - VENTOY_CMD_RETURN(GRUB_ERR_NONE); - } - *pos = c; - } - - if (ventoy_check_file_exist("%s/VENTOY_CHECKSUM", args[0])) - { - VENTOY_CMD_RETURN(GRUB_ERR_NONE); - } - - return 1; -} - static int ventoy_chksum_pathcmp(int chktype, char *rlpath, char *rdpath) { char *pos1 = NULL; @@ -6485,13 +6457,12 @@ static grub_err_t ventoy_cmd_cmp_checksum(grub_extcmd_context_t ctxt, int argc, char fchksum[64]; char readchk[256] = {0}; char filebuf[512] = {0}; - int retlen[] = { 32, 40, 64, 128 }; char uchkname[16]; (void)ctxt; index = (int)grub_strtol(args[0], NULL, 10); - if (argc != 2 || index < 0 || index >= (int)ARRAY_SIZE(retlen)) + if (argc != 2 || index < 0 || index >= VTOY_CHKSUM_NUM) { return 1; } @@ -6519,17 +6490,17 @@ static grub_err_t ventoy_cmd_cmp_checksum(grub_extcmd_context_t ctxt, int argc, if (pos) { VTOY_SKIP_SPACE_NEXT(pos, 1); - grub_memcpy(readchk, pos, retlen[index]); + grub_memcpy(readchk, pos, g_chksum_retlen[index]); } else { - grub_memcpy(readchk, filebuf, retlen[index]); + grub_memcpy(readchk, filebuf, g_chksum_retlen[index]); } } else if (chktype == 3 || chktype == 4) { grub_snprintf(fchksum, sizeof(fchksum), "global VENTOY_CHECKSUM"); - ventoy_find_checksum(file, uchkname, retlen[index], args[1], chktype, readchk); + ventoy_find_checksum(file, uchkname, g_chksum_retlen[index], args[1], chktype, readchk); if (readchk[0] == 0) { grub_printf("\n\n%s value not found in %s.\n", uchkname, fchksum); @@ -6539,7 +6510,7 @@ static grub_err_t ventoy_cmd_cmp_checksum(grub_extcmd_context_t ctxt, int argc, else { grub_snprintf(fchksum, sizeof(fchksum), "local VENTOY_CHECKSUM"); - ventoy_find_checksum(file, uchkname, retlen[index], args[1], chktype, readchk); + ventoy_find_checksum(file, uchkname, g_chksum_retlen[index], args[1], chktype, readchk); if (readchk[0] == 0) { grub_file_close(file); @@ -6547,7 +6518,7 @@ static grub_err_t ventoy_cmd_cmp_checksum(grub_extcmd_context_t ctxt, int argc, if (file) { grub_snprintf(fchksum, sizeof(fchksum), "global VENTOY_CHECKSUM"); - ventoy_find_checksum(file, uchkname, retlen[index], args[1], 3, readchk); + ventoy_find_checksum(file, uchkname, g_chksum_retlen[index], args[1], 3, readchk); if (readchk[0] == 0) { grub_printf("\n\n%s value not found in both local and global VENTOY_CHECKSUM.\n", uchkname); @@ -6573,6 +6544,181 @@ end: VENTOY_CMD_RETURN(0); } +static int ventoy_find_all_checksum +( + grub_file_t file, + char *path, + int chktype, + int exists[VTOY_CHKSUM_NUM], + int *ptotexist +) +{ + int i; + int ulen; + int tot = 0; + char c = 0; + char *pos = NULL; + char *pos1 = NULL; + char *pos2 = NULL; + char *buf = NULL; + char *currline = NULL; + char *nextline = NULL; + const char *uname = NULL; + + tot = *ptotexist; + + /* read file to buffer */ + buf = grub_malloc(file->size + 4); + if (!buf) + { + return 1; + } + grub_file_read(file, buf, file->size); + buf[file->size] = 0; + + /* parse each line */ + for (currline = buf; currline; currline = nextline) + { + nextline = ventoy_get_line(currline); + VTOY_SKIP_SPACE(currline); + + for (i = 0; i < VTOY_CHKSUM_NUM; i++) + { + if (exists[i]) + { + continue; + } + + uname = g_lower_chksum_name[i]; + ulen = g_lower_chksum_namelen[i]; + + if (grub_strncasecmp(currline, uname, ulen) == 0) + { + pos = grub_strchr(currline, '='); + pos1 = grub_strchr(currline, '('); + pos2 = grub_strchr(currline, ')'); + + if (pos && pos1 && pos2) + { + c = *pos2; + *pos2 = 0; + if (ventoy_chksum_pathcmp(chktype, path, pos1 + 1) == 0) + { + exists[i] = 1; + tot++; + } + *pos2 = c; + } + } + else if (ventoy_str_len_alnum(currline, g_chksum_retlen[i])) + { + VTOY_SKIP_SPACE_NEXT_EX(pos, currline, g_chksum_retlen[i]); + if (ventoy_chksum_pathcmp(chktype, path, pos) == 0) + { + exists[i] = 1; + tot++; + } + } + + if (tot >= VTOY_CHKSUM_NUM) + { + goto end; + } + } + } + +end: + + *ptotexist = tot; + grub_free(buf); + return 0; +} + +static grub_err_t ventoy_cmd_vtoychksum_exist(grub_extcmd_context_t ctxt, int argc, char **args) +{ + int i = 0; + int cnt = 0; + char c = 0; + int tip = 0; + char *pos = NULL; + grub_file_t file = NULL; + const char *isopart = NULL; + int exists[VTOY_CHKSUM_NUM] = { 0, 0, 0, 0 }; + int totexist = 0; + + (void)argc; + (void)ctxt; + + isopart = grub_env_get("vtoy_iso_part"); + + for (i = 0; i < VTOY_CHKSUM_NUM; i++) + { + if (ventoy_check_file_exist("%s%s.%s", isopart, args[0], g_lower_chksum_name[i])) + { + exists[i] = 1; + totexist++; + } + } + + if (totexist == VTOY_CHKSUM_NUM) + { + goto end; + } + + cnt = ventoy_str_chrcnt(args[0], '/'); + if (cnt > 1) + { + pos = grub_strrchr(args[0], '/'); + c = *pos; + *pos = 0; + file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s%s/VENTOY_CHECKSUM", isopart, args[0]); + *pos = c; + + if (file) + { + if (tip == 0 && file->size > (32 * VTOY_SIZE_1KB)) + { + tip = 1; + grub_printf("Reading checksum file...\n"); + grub_refresh(); + } + + debug("parse local VENTOY_CHECKSUM\n"); + ventoy_find_all_checksum(file, args[0], 2, exists, &totexist); + grub_file_close(file); + } + } + + if (totexist == VTOY_CHKSUM_NUM) + { + goto end; + } + + file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "%s/VENTOY_CHECKSUM", isopart); + if (file) + { + if (tip == 0 && file->size > (32 * VTOY_SIZE_1KB)) + { + tip = 1; + grub_printf("Reading checksum file...\n"); + grub_refresh(); + } + + debug("parse global VENTOY_CHECKSUM\n"); + ventoy_find_all_checksum(file, args[0], (cnt > 1) ? 3 : 4, exists, &totexist); + grub_file_close(file); + } + +end: + + ventoy_env_int_set("VT_EXIST_MD5", exists[0]); + ventoy_env_int_set("VT_EXIST_SHA1", exists[1]); + ventoy_env_int_set("VT_EXIST_SHA256", exists[2]); + ventoy_env_int_set("VT_EXIST_SHA512", exists[3]); + + VENTOY_CMD_RETURN(0); +} + static const char * ventoy_menu_lang_read_hook(struct grub_env_var *var, const char *val) { diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h index 115e2ce5..df424323 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_def.h @@ -62,6 +62,8 @@ #define VTOY_WARNING "!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!" +#define VTOY_CHKSUM_NUM 4 + #define VTOY_PLAT_I386_UEFI 0x49413332 #define VTOY_PLAT_ARM64_UEFI 0x41413634 #define VTOY_PLAT_X86_64_UEFI 0x55454649 diff --git a/INSTALL/grub/checksum.cfg b/INSTALL/grub/checksum.cfg index a3dc890d..df045ec5 100644 --- a/INSTALL/grub/checksum.cfg +++ b/INSTALL/grub/checksum.cfg @@ -1,9 +1,8 @@ unset vtchkdef -vt_vtoychksum_exist "${vtoy_iso_part}" "${VTOY_CHKSUM_FILE_PATH}" -set vtchksumfile=$? +vt_vtoychksum_exist "${VTOY_CHKSUM_FILE_PATH}" -if [ -f "${vtoy_iso_part}${VTOY_CHKSUM_FILE_PATH}.md5" -o $vtchksumfile -eq 0 ]; then +if [ "$VT_EXIST_MD5" = "1" ]; then if [ -z "$vtchkdef" ]; then set default=0 set vtchkdef=1 @@ -24,7 +23,7 @@ else } fi -if [ -f "${vtoy_iso_part}${VTOY_CHKSUM_FILE_PATH}.sha1" -o $vtchksumfile -eq 0 ]; then +if [ "$VT_EXIST_SHA1" = "1" ]; then if [ -z "$vtchkdef" ]; then set default=1 set vtchkdef=1 @@ -47,7 +46,7 @@ fi -if [ -f "${vtoy_iso_part}${VTOY_CHKSUM_FILE_PATH}.sha256" -o $vtchksumfile -eq 0 ]; then +if [ "$VT_EXIST_SHA256" = "1" ]; then if [ -z "$vtchkdef" ]; then set default=2 set vtchkdef=1 @@ -70,7 +69,7 @@ fi -if [ -f "${vtoy_iso_part}${VTOY_CHKSUM_FILE_PATH}.sha512" -o $vtchksumfile -eq 0 ]; then +if [ "$VT_EXIST_SHA512" = "1" ]; then if [ -z "$vtchkdef" ]; then set default=3 set vtchkdef=1