"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;
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;
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;
}
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);
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);
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);
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)
{